• 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 "Scope.h"
23 
24 #include <vector>
25 
26 namespace android {
27 
28 struct EnumValue;
29 struct BitFieldType;
30 
31 struct EnumType : public Scope {
32     EnumType(const char *localName,
33              const Location &location,
34              Type *storageType);
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 canCheckEquality() 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     BitFieldType *getBitfieldType() const;
62 
63     void emitReaderWriter(
64             Formatter &out,
65             const std::string &name,
66             const std::string &parcelObj,
67             bool parcelObjIsPointer,
68             bool isReader,
69             ErrorMode mode) const override;
70 
71     void emitJavaFieldReaderWriter(
72             Formatter &out,
73             size_t depth,
74             const std::string &parcelName,
75             const std::string &blobName,
76             const std::string &fieldName,
77             const std::string &offset,
78             bool isReader) const override;
79 
80     status_t emitTypeDeclarations(Formatter &out) const override;
81     status_t emitGlobalTypeDeclarations(Formatter &out) const override;
82     status_t emitTypeDefinitions(Formatter &out, const std::string prefix) const override;
83 
84     status_t emitJavaTypeDeclarations(
85             Formatter &out, bool atTopLevel) const override;
86 
87     status_t emitVtsTypeDeclarations(Formatter &out) const override;
88     status_t emitVtsAttributeType(Formatter &out) const override;
89 
90     void emitJavaDump(
91             Formatter &out,
92             const std::string &streamName,
93             const std::string &name) const override;
94 
95     void getAlignmentAndSize(size_t *align, size_t *size) const override;
96 
97     void appendToExportedTypesVector(
98             std::vector<const Type *> *exportedTypes) const override;
99 
100     status_t emitExportedHeader(Formatter &out, bool forJava) const override;
101 
102 private:
103     void getTypeChain(std::vector<const EnumType *> *out) const;
104     const Annotation *findExportAnnotation() const;
105 
106     void emitEnumBitwiseOperator(
107             Formatter &out,
108             bool lhsIsEnum,
109             bool rhsIsEnum,
110             const std::string &op) const;
111 
112     void emitBitFieldBitwiseAssignmentOperator(
113             Formatter &out,
114             const std::string &op) const;
115 
116     std::vector<EnumValue *> mValues;
117     Type *mStorageType;
118     BitFieldType *mBitfieldType;
119 
120     DISALLOW_COPY_AND_ASSIGN(EnumType);
121 };
122 
123 struct EnumValue : public LocalIdentifier {
124     EnumValue(const char *name, ConstantExpression *value = nullptr);
125 
126     std::string name() const;
127     std::string value(ScalarType::Kind castKind) const;
128     std::string cppValue(ScalarType::Kind castKind) const;
129     std::string javaValue(ScalarType::Kind castKind) const;
130     std::string comment() const;
131     void autofill(const EnumValue *prev, const ScalarType *type);
132     ConstantExpression *constExpr() const;
133 
134     bool isAutoFill() const;
135     bool isEnumValue() const override;
136 
137 
138     std::string mName;
139     ConstantExpression *mValue;
140     bool mIsAutoFill;
141 
142     DISALLOW_COPY_AND_ASSIGN(EnumValue);
143 };
144 
145 struct BitFieldType : public TemplatedType {
146 
147     std::string typeName() const override;
148 
149     bool isBitField() const override;
150 
151     void addNamedTypesToSet(std::set<const FQName> &set) const override;
152 
153     bool isCompatibleElementType(Type *elementType) const override;
154 
155     bool isElidableType() const override;
156 
157     bool canCheckEquality() const override;
158 
159     const ScalarType *resolveToScalarType() const override;
160 
161     std::string getCppType(StorageMode mode,
162                            bool specifyNamespaces) const override;
163 
164     std::string getJavaType(bool forInitializer) const override;
165 
166     std::string getJavaSuffix() const override;
167 
168     std::string getJavaWrapperType() const override;
169 
170     std::string getVtsType() const override;
171 
172     EnumType *getEnumType() const;
173 
174     status_t emitVtsAttributeType(Formatter &out) const override;
175 
176     void getAlignmentAndSize(size_t *align, size_t *size) const override;
177 
178     void emitReaderWriter(
179         Formatter &out,
180         const std::string &name,
181         const std::string &parcelObj,
182         bool parcelObjIsPointer,
183         bool isReader,
184         ErrorMode mode) const override;
185 
186     void emitDump(
187             Formatter &out,
188             const std::string &streamName,
189             const std::string &name) const override;
190 
191     void emitJavaDump(
192             Formatter &out,
193             const std::string &streamName,
194             const std::string &name) const override;
195 
196     void emitJavaFieldReaderWriter(
197         Formatter &out,
198         size_t depth,
199         const std::string &parcelName,
200         const std::string &blobName,
201         const std::string &fieldName,
202         const std::string &offset,
203         bool isReader) const override;
204 };
205 
206 }  // namespace android
207 
208 #endif  // ENUM_TYPE_H_
209 
210