EnumType.h revision 1c71fd5ab3381d1c59671adf2b8ca5c9a3f74401
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    bool isEnum() const override;
44
45    std::string getCppType(StorageMode mode,
46                           bool specifyNamespaces) const override;
47
48    std::string getJavaType(bool forInitializer) const override;
49
50    std::string getJavaSuffix() const override;
51
52    std::string getJavaWrapperType() const override;
53
54    std::string getVtsType() const override;
55
56    void emitReaderWriter(
57            Formatter &out,
58            const std::string &name,
59            const std::string &parcelObj,
60            bool parcelObjIsPointer,
61            bool isReader,
62            ErrorMode mode) const override;
63
64    void emitJavaFieldReaderWriter(
65            Formatter &out,
66            size_t depth,
67            const std::string &parcelName,
68            const std::string &blobName,
69            const std::string &fieldName,
70            const std::string &offset,
71            bool isReader) const override;
72
73    status_t emitTypeDeclarations(Formatter &out) const override;
74    status_t emitGlobalTypeDeclarations(Formatter &out) const override;
75
76    status_t emitJavaTypeDeclarations(
77            Formatter &out, bool atTopLevel) const override;
78
79    status_t emitVtsTypeDeclarations(Formatter &out) const override;
80    status_t emitVtsAttributeType(Formatter &out) const override;
81
82    void getAlignmentAndSize(size_t *align, size_t *size) const override;
83
84    void appendToExportedTypesVector(
85            std::vector<const Type *> *exportedTypes) const override;
86
87    status_t emitExportedHeader(Formatter &out, bool forJava) const override;
88
89private:
90    void getTypeChain(std::vector<const EnumType *> *out) const;
91    const Annotation *findExportAnnotation() const;
92
93    void emitEnumBitwiseOperator(
94            Formatter &out,
95            bool mutating,
96            const std::string &op) const;
97
98    std::vector<EnumValue *> mValues;
99    Type *mStorageType;
100
101    DISALLOW_COPY_AND_ASSIGN(EnumType);
102};
103
104struct EnumValue : public LocalIdentifier {
105    EnumValue(const char *name, ConstantExpression *value = nullptr);
106
107    std::string name() const;
108    std::string value(ScalarType::Kind castKind) const;
109    std::string cppValue(ScalarType::Kind castKind) const;
110    std::string javaValue(ScalarType::Kind castKind) const;
111    std::string comment() const;
112    void autofill(const EnumValue *prev, const ScalarType *type);
113    ConstantExpression *constExpr() const;
114
115    bool isAutoFill() const;
116    bool isEnumValue() const override;
117
118
119    std::string mName;
120    ConstantExpression *mValue;
121    bool mIsAutoFill;
122
123    DISALLOW_COPY_AND_ASSIGN(EnumValue);
124};
125
126}  // namespace android
127
128#endif  // ENUM_TYPE_H_
129
130