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