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