EnumType.h revision e3f769aa75dd6be6bb1ba83904def47d9e464769
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             Type *storageType = NULL);
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    const ScalarType *resolveToScalarType() const override;
41
42    bool isEnum() const override;
43
44    std::string getCppType(StorageMode mode,
45                           std::string *extra,
46                           bool specifyNamespaces) const override;
47
48    std::string getJavaType(
49            std::string *extra, 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) const override;
89
90private:
91    void getTypeChain(std::vector<const EnumType *> *out) const;
92    const Annotation *findExportAnnotation() const;
93
94    void emitEnumBitwiseOrOperator(Formatter &out, bool mutating) const;
95
96    std::vector<EnumValue *> mValues;
97    Type *mStorageType;
98
99    DISALLOW_COPY_AND_ASSIGN(EnumType);
100};
101
102struct EnumValue : public LocalIdentifier {
103    EnumValue(const char *name, ConstantExpression *value = nullptr);
104
105    std::string name() const;
106    std::string value() const;
107    std::string cppValue(ScalarType::Kind castKind) const;
108    std::string javaValue(ScalarType::Kind castKind) const;
109    std::string comment() const;
110    void autofill(const EnumValue *prev, const ScalarType *type);
111    ConstantExpression *constExpr() const;
112
113    bool isAutoFill() const;
114    bool isEnumValue() const override;
115
116
117    std::string mName;
118    ConstantExpression *mValue;
119    bool mIsAutoFill;
120
121    DISALLOW_COPY_AND_ASSIGN(EnumValue);
122};
123
124}  // namespace android
125
126#endif  // ENUM_TYPE_H_
127
128