EnumType.h revision 33431e6cd425c6cd179080442a8616e2baa20aae
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 Location& location, const Reference<Type>& storageType,
34             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 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    std::vector<Reference<Type>> getReferences() const override;
64
65    status_t resolveInheritance() override;
66    status_t evaluate() 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    status_t emitGlobalTypeDeclarations(Formatter &out) const override;
89    status_t emitTypeDefinitions(Formatter& out, const std::string& prefix) const override;
90
91    status_t emitJavaTypeDeclarations(
92            Formatter &out, bool atTopLevel) const override;
93
94    status_t emitVtsTypeDeclarations(Formatter &out) const override;
95    status_t emitVtsAttributeType(Formatter &out) const override;
96
97    void emitJavaDump(
98            Formatter &out,
99            const std::string &streamName,
100            const std::string &name) const override;
101
102    void getAlignmentAndSize(size_t *align, size_t *size) const override;
103
104    void appendToExportedTypesVector(
105            std::vector<const Type *> *exportedTypes) const override;
106
107    status_t emitExportedHeader(Formatter &out, bool forJava) const override;
108
109   private:
110    std::vector<const EnumType*> typeChain() const;
111    std::vector<const EnumType*> superTypeChain() const;
112
113    const Annotation *findExportAnnotation() const;
114
115    void emitEnumBitwiseOperator(
116            Formatter &out,
117            bool lhsIsEnum,
118            bool rhsIsEnum,
119            const std::string &op) const;
120
121    void emitBitFieldBitwiseAssignmentOperator(
122            Formatter &out,
123            const std::string &op) const;
124
125    std::vector<EnumValue *> mValues;
126    Reference<Type> mStorageType;
127    // TODO(b/64272670): Dot not store BitFieldType as it is not owned.
128    // It is kept here to avoid const-cast (BitFieldType owns non-const EnumType).
129    Reference<BitFieldType> mBitfieldType;
130
131    DISALLOW_COPY_AND_ASSIGN(EnumType);
132};
133
134struct EnumValue : public LocalIdentifier {
135    EnumValue(const char* name, ConstantExpression* value, const Location& location);
136
137    std::string name() const;
138    std::string value(ScalarType::Kind castKind) const;
139    std::string cppValue(ScalarType::Kind castKind) const;
140    std::string javaValue(ScalarType::Kind castKind) const;
141    std::string comment() const;
142    void autofill(const EnumType* prevType, EnumValue* prevValue, const ScalarType* type);
143    ConstantExpression* constExpr() const override;
144
145    bool isAutoFill() const;
146    bool isEnumValue() const override;
147
148    status_t evaluate() override;
149    status_t validate() const override;
150
151    const Location& location() const;
152
153   private:
154    std::string mName;
155    ConstantExpression* mValue;
156    const Location mLocation;
157    bool mIsAutoFill;
158
159    DISALLOW_COPY_AND_ASSIGN(EnumValue);
160};
161
162struct BitFieldType : public TemplatedType {
163
164    std::string typeName() const override;
165
166    bool isBitField() const override;
167
168    bool isCompatibleElementType(Type *elementType) const override;
169
170    bool isElidableType() const override;
171
172    bool canCheckEquality() const override;
173
174    const ScalarType *resolveToScalarType() const override;
175
176    std::string getCppType(StorageMode mode,
177                           bool specifyNamespaces) const override;
178
179    std::string getJavaType(bool forInitializer) const override;
180
181    std::string getJavaSuffix() const override;
182
183    std::string getJavaWrapperType() const override;
184
185    std::string getVtsType() const override;
186
187    EnumType *getEnumType() const;
188
189    status_t emitVtsAttributeType(Formatter &out) const override;
190
191    void getAlignmentAndSize(size_t *align, size_t *size) const override;
192
193    void emitReaderWriter(
194        Formatter &out,
195        const std::string &name,
196        const std::string &parcelObj,
197        bool parcelObjIsPointer,
198        bool isReader,
199        ErrorMode mode) const override;
200
201    void emitDump(
202            Formatter &out,
203            const std::string &streamName,
204            const std::string &name) const override;
205
206    void emitJavaDump(
207            Formatter &out,
208            const std::string &streamName,
209            const std::string &name) const override;
210
211    void emitJavaFieldReaderWriter(
212        Formatter &out,
213        size_t depth,
214        const std::string &parcelName,
215        const std::string &blobName,
216        const std::string &fieldName,
217        const std::string &offset,
218        bool isReader) const override;
219};
220
221}  // namespace android
222
223#endif  // ENUM_TYPE_H_
224
225