EnumType.h revision 30bb6a869be0f3f82497b7b11c71ec9d47652ed0
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ENUM_TYPE_H_
18
19#define ENUM_TYPE_H_
20
21#include "ConstantExpression.h"
22#include "Scope.h"
23
24#include <vector>
25
26namespace android {
27
28struct EnumValue;
29
30struct EnumType : public Scope {
31    EnumType(const char *localName,
32             const Location &location,
33             Type *storageType);
34
35    const Type *storageType() const;
36    const std::vector<EnumValue *> &values() const;
37    void addValue(EnumValue *value);
38
39    LocalIdentifier *lookupIdentifier(const std::string &name) const override;
40
41    const ScalarType *resolveToScalarType() const override;
42
43    std::string typeName() const override;
44    bool isEnum() const override;
45
46    std::string getCppType(StorageMode mode,
47                           bool specifyNamespaces) const override;
48
49    std::string getJavaType(bool forInitializer) const override;
50
51    std::string getJavaSuffix() const override;
52
53    std::string getJavaWrapperType() const override;
54
55    std::string getVtsType() const override;
56
57    void emitReaderWriter(
58            Formatter &out,
59            const std::string &name,
60            const std::string &parcelObj,
61            bool parcelObjIsPointer,
62            bool isReader,
63            ErrorMode mode) const override;
64
65    void emitJavaFieldReaderWriter(
66            Formatter &out,
67            size_t depth,
68            const std::string &parcelName,
69            const std::string &blobName,
70            const std::string &fieldName,
71            const std::string &offset,
72            bool isReader) const override;
73
74    status_t emitTypeDeclarations(Formatter &out) const override;
75    status_t emitGlobalTypeDeclarations(Formatter &out) const override;
76
77    status_t emitJavaTypeDeclarations(
78            Formatter &out, bool atTopLevel) const override;
79
80    status_t emitVtsTypeDeclarations(Formatter &out) const override;
81    status_t emitVtsAttributeType(Formatter &out) const override;
82
83    void getAlignmentAndSize(size_t *align, size_t *size) const override;
84
85    void appendToExportedTypesVector(
86            std::vector<const Type *> *exportedTypes) const override;
87
88    status_t emitExportedHeader(Formatter &out, bool forJava) const override;
89
90private:
91    void getTypeChain(std::vector<const EnumType *> *out) const;
92    const Annotation *findExportAnnotation() const;
93
94    void emitEnumBitwiseOperator(
95            Formatter &out,
96            bool mutating,
97            const std::string &op) const;
98
99    std::vector<EnumValue *> mValues;
100    Type *mStorageType;
101
102    DISALLOW_COPY_AND_ASSIGN(EnumType);
103};
104
105struct EnumValue : public LocalIdentifier {
106    EnumValue(const char *name, ConstantExpression *value = nullptr);
107
108    std::string name() const;
109    std::string value(ScalarType::Kind castKind) const;
110    std::string cppValue(ScalarType::Kind castKind) const;
111    std::string javaValue(ScalarType::Kind castKind) const;
112    std::string comment() const;
113    void autofill(const EnumValue *prev, const ScalarType *type);
114    ConstantExpression *constExpr() const;
115
116    bool isAutoFill() const;
117    bool isEnumValue() const override;
118
119
120    std::string mName;
121    ConstantExpression *mValue;
122    bool mIsAutoFill;
123
124    DISALLOW_COPY_AND_ASSIGN(EnumValue);
125};
126
127}  // namespace android
128
129#endif  // ENUM_TYPE_H_
130
131