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