blob: db5efdc61e98d2d427d32d8f2d361672795c1373 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
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
Andreas Huberc9410c72016-07-28 12:18:40 -070017#ifndef ENUM_TYPE_H_
18
19#define ENUM_TYPE_H_
20
Yifan Hong57886972016-08-17 10:42:15 -070021#include "ConstantExpression.h"
Yifan Hongf24fa852016-09-23 11:03:15 -070022#include "Scope.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070023
Andreas Huber881227d2016-08-02 14:20:21 -070024#include <vector>
Andreas Huberc9410c72016-07-28 12:18:40 -070025
26namespace android {
27
Andreas Huber31629bc2016-08-03 09:06:40 -070028struct EnumValue;
Andreas Huberc9410c72016-07-28 12:18:40 -070029
Yifan Hongf24fa852016-09-23 11:03:15 -070030struct EnumType : public Scope {
Andreas Huber9ed827c2016-08-22 12:31:13 -070031 EnumType(const char *localName,
Yifan Honga4b53d02016-10-31 17:29:10 -070032 const Location &location,
Steven Moreland1c71fd52016-11-29 14:03:33 -080033 Type *storageType);
Andreas Huberc9410c72016-07-28 12:18:40 -070034
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070035 const Type *storageType() const;
36 const std::vector<EnumValue *> &values() const;
Yifan Hongf24fa852016-09-23 11:03:15 -070037 void addValue(EnumValue *value);
38
39 LocalIdentifier *lookupIdentifier(const std::string &name) const override;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070040
Steven Moreland9df52442016-12-12 08:51:14 -080041 bool isElidableType() const override;
Andreas Huber737080b2016-08-02 15:38:04 -070042 const ScalarType *resolveToScalarType() const override;
43
Steven Moreland30bb6a82016-11-30 09:18:34 -080044 std::string typeName() const override;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070045 bool isEnum() const override;
Yifan Hongc6752dc2016-12-20 14:00:14 -080046 bool canCheckEquality() const override;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070047
Steven Moreland979e0992016-09-07 09:18:08 -070048 std::string getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -070049 bool specifyNamespaces) const override;
Andreas Huber881227d2016-08-02 14:20:21 -070050
Yifan Hong4ed13472016-11-02 10:44:11 -070051 std::string getJavaType(bool forInitializer) const override;
Andreas Huber4c865b72016-09-14 15:26:27 -070052
Andreas Huber2831d512016-08-15 09:33:47 -070053 std::string getJavaSuffix() const override;
54
Andreas Hubera3558b32016-09-14 09:12:42 -070055 std::string getJavaWrapperType() const override;
56
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -070057 std::string getVtsType() const override;
58
Andreas Huber881227d2016-08-02 14:20:21 -070059 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
Andreas Huber85eabdb2016-08-25 11:24:49 -070067 void emitJavaFieldReaderWriter(
68 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -070069 size_t depth,
Andreas Huber709b62d2016-09-19 11:21:18 -070070 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -070071 const std::string &blobName,
72 const std::string &fieldName,
73 const std::string &offset,
74 bool isReader) const override;
75
Andreas Huber881227d2016-08-02 14:20:21 -070076 status_t emitTypeDeclarations(Formatter &out) const override;
Andreas Hubere3f769a2016-10-10 10:54:44 -070077 status_t emitGlobalTypeDeclarations(Formatter &out) const override;
Yifan Hongf5cc2f72017-01-04 18:02:34 -080078 status_t emitTypeDefinitions(Formatter &out, const std::string prefix) const override;
Andreas Huber85eabdb2016-08-25 11:24:49 -070079
80 status_t emitJavaTypeDeclarations(
81 Formatter &out, bool atTopLevel) const override;
Andreas Huber881227d2016-08-02 14:20:21 -070082
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070083 status_t emitVtsTypeDeclarations(Formatter &out) const override;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070084 status_t emitVtsAttributeType(Formatter &out) const override;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070085
Andreas Huber85eabdb2016-08-25 11:24:49 -070086 void getAlignmentAndSize(size_t *align, size_t *size) const override;
87
Andreas Huber019d21d2016-10-03 12:59:47 -070088 void appendToExportedTypesVector(
89 std::vector<const Type *> *exportedTypes) const override;
90
Andreas Huber1c507272016-10-05 14:33:21 -070091 status_t emitExportedHeader(Formatter &out, bool forJava) const override;
Andreas Huber019d21d2016-10-03 12:59:47 -070092
Andreas Huberc9410c72016-07-28 12:18:40 -070093private:
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070094 void getTypeChain(std::vector<const EnumType *> *out) const;
Andreas Huber019d21d2016-10-03 12:59:47 -070095 const Annotation *findExportAnnotation() const;
96
Jayant Chowdhary2820f8a2016-11-10 12:29:09 -080097 void emitEnumBitwiseOperator(
98 Formatter &out,
Yifan Hongc57c8bb2016-12-01 11:37:18 -080099 bool lhsIsEnum,
100 bool rhsIsEnum,
101 const std::string &op) const;
102
103 void emitBitFieldBitwiseAssignmentOperator(
104 Formatter &out,
Jayant Chowdhary2820f8a2016-11-10 12:29:09 -0800105 const std::string &op) const;
Andreas Hubere3f769a2016-10-10 10:54:44 -0700106
Yifan Hongf24fa852016-09-23 11:03:15 -0700107 std::vector<EnumValue *> mValues;
Andreas Huberc9410c72016-07-28 12:18:40 -0700108 Type *mStorageType;
109
110 DISALLOW_COPY_AND_ASSIGN(EnumType);
111};
112
Yifan Hongf24fa852016-09-23 11:03:15 -0700113struct EnumValue : public LocalIdentifier {
114 EnumValue(const char *name, ConstantExpression *value = nullptr);
Andreas Huber31629bc2016-08-03 09:06:40 -0700115
116 std::string name() const;
Yifan Hongc07b2022016-11-08 12:44:24 -0800117 std::string value(ScalarType::Kind castKind) const;
Yifan Hongfc610cd2016-09-22 13:34:45 -0700118 std::string cppValue(ScalarType::Kind castKind) const;
119 std::string javaValue(ScalarType::Kind castKind) const;
120 std::string comment() const;
Yifan Hongf24fa852016-09-23 11:03:15 -0700121 void autofill(const EnumValue *prev, const ScalarType *type);
122 ConstantExpression *constExpr() const;
Andreas Huber31629bc2016-08-03 09:06:40 -0700123
Yifan Hongf24fa852016-09-23 11:03:15 -0700124 bool isAutoFill() const;
125 bool isEnumValue() const override;
126
127
Andreas Huber31629bc2016-08-03 09:06:40 -0700128 std::string mName;
Yifan Hongf24fa852016-09-23 11:03:15 -0700129 ConstantExpression *mValue;
130 bool mIsAutoFill;
Andreas Huber31629bc2016-08-03 09:06:40 -0700131
132 DISALLOW_COPY_AND_ASSIGN(EnumValue);
133};
134
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800135struct BitFieldType : public TemplatedType {
136
137 std::string typeName() const override;
138
Yifan Hongabf73ee2016-12-05 18:47:00 -0800139 bool isBitField() const override;
140
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800141 void addNamedTypesToSet(std::set<const FQName> &set) const override;
142
143 bool isCompatibleElementType(Type *elementType) const override;
144
Yifan Hong8c56cbe2016-12-12 15:30:12 -0800145 bool isElidableType() const override;
146
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800147 const ScalarType *resolveToScalarType() const override;
148
149 std::string getCppType(StorageMode mode,
150 bool specifyNamespaces) const override;
151
152 std::string getJavaType(bool forInitializer) const override;
153
154 std::string getJavaSuffix() const override;
155
156 std::string getJavaWrapperType() const override;
157
158 std::string getVtsType() const override;
159
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800160 status_t emitVtsAttributeType(Formatter &out) const override;
161
162 void getAlignmentAndSize(size_t *align, size_t *size) const override;
163
164 void emitReaderWriter(
165 Formatter &out,
166 const std::string &name,
167 const std::string &parcelObj,
168 bool parcelObjIsPointer,
169 bool isReader,
170 ErrorMode mode) const override;
171
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800172 void emitDump(
173 Formatter &out,
174 const std::string &streamName,
175 const std::string &name) const override;
176
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800177 void emitJavaFieldReaderWriter(
178 Formatter &out,
179 size_t depth,
180 const std::string &parcelName,
181 const std::string &blobName,
182 const std::string &fieldName,
183 const std::string &offset,
184 bool isReader) const override;
185};
186
Andreas Huberc9410c72016-07-28 12:18:40 -0700187} // namespace android
188
189#endif // ENUM_TYPE_H_
190