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