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