EnumType.h revision 8c90cc59bf93bd0c08970b4488067a33015d4a1c
13d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel/*
23d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel * Copyright (C) 2016 The Android Open Source Project
33d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel *
43d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel * Licensed under the Apache License, Version 2.0 (the "License");
53d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel * you may not use this file except in compliance with the License.
63d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel * You may obtain a copy of the License at
73d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel *
83d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel *      http://www.apache.org/licenses/LICENSE-2.0
93d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel *
103d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel * Unless required by applicable law or agreed to in writing, software
113d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel * distributed under the License is distributed on an "AS IS" BASIS,
123d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel * See the License for the specific language governing permissions and
143d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel * limitations under the License.
153d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel */
163d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel
173d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel#ifndef ENUM_TYPE_H_
183d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel
193d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel#define ENUM_TYPE_H_
203d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel
213d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel#include "ConstantExpression.h"
223d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel#include "Reference.h"
233d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel#include "Scope.h"
243d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel
253d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel#include <vector>
263d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel
273d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Rousselnamespace android {
283d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel
293d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Rousselstruct EnumValue;
303d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Rousselstruct BitFieldType;
313d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel
323d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Rousselstruct EnumType : public Scope {
333d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel    EnumType(const char* localName, const Location& location, const Reference<Type>& storageType,
343d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel             Scope* parent);
353d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel
363d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel    const Type *storageType() const;
373d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel    const std::vector<EnumValue *> &values() const;
383d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel    void addValue(EnumValue *value);
393d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel
403d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel    LocalIdentifier *lookupIdentifier(const std::string &name) const override;
413d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel
423d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel    bool isElidableType() const override;
433d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel    const ScalarType *resolveToScalarType() const override;
443d736bc13c9d3d4f87e1295a215bbe13af49e7b4Yohann Roussel
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    std::vector<const EnumType*> typeChain() const;
104    std::vector<const EnumType*> superTypeChain() const;
105
106    const Annotation *findExportAnnotation() const;
107
108    void emitEnumBitwiseOperator(
109            Formatter &out,
110            bool lhsIsEnum,
111            bool rhsIsEnum,
112            const std::string &op) const;
113
114    void emitBitFieldBitwiseAssignmentOperator(
115            Formatter &out,
116            const std::string &op) const;
117
118    std::vector<EnumValue *> mValues;
119    Reference<Type> mStorageType;
120    // TODO(b/64272670): Dot not store BitFieldType as it is not owned.
121    // It is kept here to avoid const-cast (BitFieldType owns non-const EnumType).
122    Reference<BitFieldType> mBitfieldType;
123
124    DISALLOW_COPY_AND_ASSIGN(EnumType);
125};
126
127struct EnumValue : public LocalIdentifier {
128    EnumValue(const char *name, ConstantExpression *value = nullptr);
129
130    std::string name() const;
131    std::string value(ScalarType::Kind castKind) const;
132    std::string cppValue(ScalarType::Kind castKind) const;
133    std::string javaValue(ScalarType::Kind castKind) const;
134    std::string comment() const;
135    void autofill(const EnumValue *prev, const ScalarType *type);
136    ConstantExpression* constExpr() const override;
137
138    bool isAutoFill() const;
139    bool isEnumValue() const override;
140
141
142    std::string mName;
143    ConstantExpression* mValue;
144    bool mIsAutoFill;
145
146    DISALLOW_COPY_AND_ASSIGN(EnumValue);
147};
148
149struct BitFieldType : public TemplatedType {
150
151    std::string typeName() const override;
152
153    bool isBitField() const override;
154
155    bool isCompatibleElementType(Type *elementType) const override;
156
157    bool isElidableType() const override;
158
159    bool canCheckEquality() const override;
160
161    const ScalarType *resolveToScalarType() const override;
162
163    std::string getCppType(StorageMode mode,
164                           bool specifyNamespaces) const override;
165
166    std::string getJavaType(bool forInitializer) const override;
167
168    std::string getJavaSuffix() const override;
169
170    std::string getJavaWrapperType() const override;
171
172    std::string getVtsType() const override;
173
174    EnumType *getEnumType() const;
175
176    status_t emitVtsAttributeType(Formatter &out) const override;
177
178    void getAlignmentAndSize(size_t *align, size_t *size) const override;
179
180    void emitReaderWriter(
181        Formatter &out,
182        const std::string &name,
183        const std::string &parcelObj,
184        bool parcelObjIsPointer,
185        bool isReader,
186        ErrorMode mode) const override;
187
188    void emitDump(
189            Formatter &out,
190            const std::string &streamName,
191            const std::string &name) const override;
192
193    void emitJavaDump(
194            Formatter &out,
195            const std::string &streamName,
196            const std::string &name) const override;
197
198    void emitJavaFieldReaderWriter(
199        Formatter &out,
200        size_t depth,
201        const std::string &parcelName,
202        const std::string &blobName,
203        const std::string &fieldName,
204        const std::string &offset,
205        bool isReader) const override;
206};
207
208}  // namespace android
209
210#endif  // ENUM_TYPE_H_
211
212