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