EnumType.h revision 4b8f7a11f794d9b4899af92a856b4a03b80b31e8
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 "Reference.h"
23#include "Scope.h"
24
25#include <vector>
26
27namespace android {
28
29struct EnumValue;
30struct BitFieldType;
31
32struct EnumType : public Scope {
33    EnumType(const char* localName, const FQName& fullName, const Location& location,
34             const Reference<Type>& storageType, Scope* parent);
35
36    const Type *storageType() const;
37    const std::vector<EnumValue *> &values() const;
38    void addValue(EnumValue *value);
39
40    void forEachValueFromRoot(const std::function<void(EnumValue*)> f) const;
41
42    LocalIdentifier *lookupIdentifier(const std::string &name) const override;
43
44    bool isElidableType() const override;
45    const ScalarType *resolveToScalarType() const override;
46
47    std::string typeName() const override;
48    bool isEnum() const override;
49    bool deepCanCheckEquality(std::unordered_set<const Type*>* visited) const override;
50
51    std::string getCppType(StorageMode mode,
52                           bool specifyNamespaces) const override;
53
54    std::string getJavaType(bool forInitializer) const override;
55
56    std::string getJavaSuffix() const override;
57
58    std::string getJavaWrapperType() const override;
59
60    std::string getVtsType() const override;
61
62    std::string getBitfieldCppType(StorageMode mode, bool specifyNamespaces = true) const;
63    std::string getBitfieldJavaType(bool forInitializer = false) const;
64    std::string getBitfieldJavaWrapperType() const;
65
66    // Return the type that corresponds to bitfield<T>.
67    const BitFieldType* getBitfieldType() const;
68
69    std::vector<const Reference<Type>*> getReferences() const override;
70    std::vector<const ConstantExpression*> getConstantExpressions() const override;
71
72    status_t resolveInheritance() override;
73    status_t validate() const override;
74    status_t validateUniqueNames() const;
75
76    void emitReaderWriter(
77            Formatter &out,
78            const std::string &name,
79            const std::string &parcelObj,
80            bool parcelObjIsPointer,
81            bool isReader,
82            ErrorMode mode) const override;
83
84    void emitJavaFieldReaderWriter(
85            Formatter &out,
86            size_t depth,
87            const std::string &parcelName,
88            const std::string &blobName,
89            const std::string &fieldName,
90            const std::string &offset,
91            bool isReader) const override;
92
93    status_t emitTypeDeclarations(Formatter &out) const override;
94    void emitTypeForwardDeclaration(Formatter& out) const override;
95    status_t emitPackageTypeDeclarations(Formatter& out) const override;
96    status_t emitTypeDefinitions(Formatter& out, const std::string& prefix) const override;
97
98    status_t emitJavaTypeDeclarations(
99            Formatter &out, bool atTopLevel) const override;
100
101    status_t emitVtsTypeDeclarations(Formatter &out) const override;
102    status_t emitVtsAttributeType(Formatter &out) const override;
103
104    void emitJavaDump(
105            Formatter &out,
106            const std::string &streamName,
107            const std::string &name) const override;
108
109    void getAlignmentAndSize(size_t *align, size_t *size) const override;
110
111    void appendToExportedTypesVector(
112            std::vector<const Type *> *exportedTypes) const override;
113
114    status_t emitExportedHeader(Formatter &out, bool forJava) const override;
115
116   private:
117    std::vector<const EnumType*> typeChain() const;
118    std::vector<const EnumType*> superTypeChain() const;
119
120    const Annotation *findExportAnnotation() const;
121
122    void emitEnumBitwiseOperator(
123            Formatter &out,
124            bool lhsIsEnum,
125            bool rhsIsEnum,
126            const std::string &op) const;
127
128    void emitBitFieldBitwiseAssignmentOperator(
129            Formatter &out,
130            const std::string &op) const;
131
132    std::vector<EnumValue *> mValues;
133    Reference<Type> mStorageType;
134
135    DISALLOW_COPY_AND_ASSIGN(EnumType);
136};
137
138struct EnumValue : public LocalIdentifier {
139    EnumValue(const char* name, ConstantExpression* value, const Location& location);
140
141    std::string name() const;
142    std::string value(ScalarType::Kind castKind) const;
143    std::string cppValue(ScalarType::Kind castKind) const;
144    std::string javaValue(ScalarType::Kind castKind) const;
145    std::string comment() const;
146    void autofill(const EnumType* prevType, EnumValue* prevValue, const ScalarType* type);
147    ConstantExpression* constExpr() const override;
148
149    bool isAutoFill() const;
150    bool isEnumValue() const override;
151
152    const Location& location() const;
153
154   private:
155    std::string mName;
156    ConstantExpression* mValue;
157    const Location mLocation;
158    bool mIsAutoFill;
159
160    DISALLOW_COPY_AND_ASSIGN(EnumValue);
161};
162
163struct BitFieldType : public TemplatedType {
164    BitFieldType(Scope* parent);
165
166    std::string templatedTypeName() const override;
167
168    const EnumType* getElementEnumType() const;
169
170    bool isBitField() const override;
171
172    bool isCompatibleElementType(const Type* elementType) const override;
173
174    bool isElidableType() const override;
175
176    bool deepCanCheckEquality(std::unordered_set<const Type*>* visited) const override;
177
178    const ScalarType *resolveToScalarType() const override;
179
180    std::string getCppType(StorageMode mode,
181                           bool specifyNamespaces) const override;
182
183    std::string getJavaType(bool forInitializer) const override;
184
185    std::string getJavaSuffix() const override;
186
187    std::string getJavaWrapperType() const override;
188
189    std::string getVtsType() const override;
190
191    const EnumType* getEnumType() const;
192
193    status_t emitVtsAttributeType(Formatter &out) const override;
194
195    void getAlignmentAndSize(size_t *align, size_t *size) const override;
196
197    void emitReaderWriter(
198        Formatter &out,
199        const std::string &name,
200        const std::string &parcelObj,
201        bool parcelObjIsPointer,
202        bool isReader,
203        ErrorMode mode) const override;
204
205    void emitDump(
206            Formatter &out,
207            const std::string &streamName,
208            const std::string &name) const override;
209
210    void emitJavaDump(
211            Formatter &out,
212            const std::string &streamName,
213            const std::string &name) const override;
214
215    void emitJavaFieldReaderWriter(
216        Formatter &out,
217        size_t depth,
218        const std::string &parcelName,
219        const std::string &blobName,
220        const std::string &fieldName,
221        const std::string &offset,
222        bool isReader) const override;
223};
224
225}  // namespace android
226
227#endif  // ENUM_TYPE_H_
228
229