blob: 2093c37e8eb1ed0315ab4c8b6aee95c9a58a1ca2 [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"
Timur Iskhakov505316c2017-08-05 03:38:59 +000022#include "Reference.h"
Yifan Hongf24fa852016-09-23 11:03:15 -070023#include "Scope.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070024
Neel Mehta69920a62019-07-22 16:22:13 -070025#include <string>
Andreas Huber881227d2016-08-02 14:20:21 -070026#include <vector>
Andreas Huberc9410c72016-07-28 12:18:40 -070027
28namespace android {
29
Andreas Huber31629bc2016-08-03 09:06:40 -070030struct EnumValue;
Yifan Honge45b5302017-02-22 10:49:07 -080031struct BitFieldType;
Andreas Huberc9410c72016-07-28 12:18:40 -070032
Yifan Hongf24fa852016-09-23 11:03:15 -070033struct EnumType : public Scope {
Neel Mehta69920a62019-07-22 16:22:13 -070034 EnumType(const std::string& localName, const FQName& fullName, const Location& location,
Timur Iskhakov565b0132017-09-06 18:07:11 -070035 const Reference<Type>& storageType, Scope* parent);
Andreas Huberc9410c72016-07-28 12:18:40 -070036
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070037 const Type *storageType() const;
38 const std::vector<EnumValue *> &values() const;
Yifan Hongf24fa852016-09-23 11:03:15 -070039 void addValue(EnumValue *value);
40
Steven Morelanda81bae72019-10-24 10:40:35 -070041 void forEachValueFromRoot(const std::function<void(const EnumValue*)> f) const;
Yifan Hong0a9cc862017-10-06 16:21:55 -070042
Steven Moreland12f0ab12018-11-02 17:27:37 -070043 // This is the number of distinct keys (even if they have colliding values)
44 size_t numValueNames() const;
45
Yifan Hongf24fa852016-09-23 11:03:15 -070046 LocalIdentifier *lookupIdentifier(const std::string &name) const override;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070047
Steven Moreland9df52442016-12-12 08:51:14 -080048 bool isElidableType() const override;
Andreas Huber737080b2016-08-02 15:38:04 -070049 const ScalarType *resolveToScalarType() const override;
50
Steven Moreland30bb6a82016-11-30 09:18:34 -080051 std::string typeName() const override;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070052 bool isEnum() const override;
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -070053 bool deepCanCheckEquality(std::unordered_set<const Type*>* visited) const override;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070054
Steven Moreland979e0992016-09-07 09:18:08 -070055 std::string getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -070056 bool specifyNamespaces) const override;
Andreas Huber881227d2016-08-02 14:20:21 -070057
Yifan Hong4ed13472016-11-02 10:44:11 -070058 std::string getJavaType(bool forInitializer) const override;
Nirav Atre66842a92018-06-28 18:14:13 -070059 std::string getJavaTypeClass() const override;
Andreas Huber4c865b72016-09-14 15:26:27 -070060
Andreas Huber2831d512016-08-15 09:33:47 -070061 std::string getJavaSuffix() const override;
62
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -070063 std::string getVtsType() const override;
64
Yifan Hongc14dd6e2017-09-15 13:39:58 -070065 std::string getBitfieldCppType(StorageMode mode, bool specifyNamespaces = true) const;
66 std::string getBitfieldJavaType(bool forInitializer = false) const;
Nirav Atre66842a92018-06-28 18:14:13 -070067 std::string getBitfieldJavaTypeClass() const;
Yifan Hongc14dd6e2017-09-15 13:39:58 -070068
Yifan Honge45b5302017-02-22 10:49:07 -080069 // Return the type that corresponds to bitfield<T>.
Timur Iskhakov24e605b2017-08-30 14:02:55 -070070 const BitFieldType* getBitfieldType() const;
Yifan Honge45b5302017-02-22 10:49:07 -080071
Timur Iskhakovb58f4182017-08-29 15:19:24 -070072 std::vector<const Reference<Type>*> getReferences() const override;
73 std::vector<const ConstantExpression*> getConstantExpressions() const override;
Timur Iskhakov33431e62017-08-21 17:31:23 -070074
Timur Iskhakovcec46c42017-08-09 00:22:02 -070075 status_t resolveInheritance() override;
Timur Iskhakovcec46c42017-08-09 00:22:02 -070076 status_t validate() const override;
77 status_t validateUniqueNames() const;
78
Neel Mehta4b6f4392019-05-09 16:03:47 -070079 void emitJavaFieldInitializer(Formatter&, const std::string&) const override;
80
81 void emitJavaFieldDefaultInitialValue(Formatter&, const std::string&) const override;
82
Andreas Huber881227d2016-08-02 14:20:21 -070083 void emitReaderWriter(
84 Formatter &out,
85 const std::string &name,
86 const std::string &parcelObj,
87 bool parcelObjIsPointer,
88 bool isReader,
89 ErrorMode mode) const override;
90
Andreas Huber85eabdb2016-08-25 11:24:49 -070091 void emitJavaFieldReaderWriter(
92 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -070093 size_t depth,
Andreas Huber709b62d2016-09-19 11:21:18 -070094 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -070095 const std::string &blobName,
96 const std::string &fieldName,
97 const std::string &offset,
98 bool isReader) const override;
99
Neel Mehta3b414a82019-07-02 15:47:48 -0700100 void emitHidlDefinition(Formatter& out) const override;
Steven Moreland368e4602018-02-16 14:21:49 -0800101 void emitTypeDeclarations(Formatter& out) const override;
Timur Iskhakovfd3f2502017-09-05 16:25:02 -0700102 void emitTypeForwardDeclaration(Formatter& out) const override;
Steven Moreland6961d3f2017-11-17 14:23:39 -0800103 void emitGlobalTypeDeclarations(Formatter& out) const override;
Steven Moreland368e4602018-02-16 14:21:49 -0800104 void emitPackageTypeDeclarations(Formatter& out) const override;
Steven Moreland09c6ebe2018-10-09 10:15:48 -0700105 void emitPackageTypeHeaderDefinitions(Formatter& out) const override;
Andreas Huber85eabdb2016-08-25 11:24:49 -0700106
Steven Moreland368e4602018-02-16 14:21:49 -0800107 void emitJavaTypeDeclarations(Formatter& out, bool atTopLevel) const override;
Andreas Huber881227d2016-08-02 14:20:21 -0700108
Steven Moreland368e4602018-02-16 14:21:49 -0800109 void emitVtsTypeDeclarations(Formatter& out) const override;
110 void emitVtsAttributeType(Formatter& out) const override;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700111
Yifan Honge45b5302017-02-22 10:49:07 -0800112 void emitJavaDump(
113 Formatter &out,
114 const std::string &streamName,
115 const std::string &name) const override;
116
Andreas Huber85eabdb2016-08-25 11:24:49 -0700117 void getAlignmentAndSize(size_t *align, size_t *size) const override;
118
Andreas Huber019d21d2016-10-03 12:59:47 -0700119 void appendToExportedTypesVector(
120 std::vector<const Type *> *exportedTypes) const override;
121
Steven Moreland368e4602018-02-16 14:21:49 -0800122 void emitExportedHeader(Formatter& out, bool forJava) const override;
Andreas Huber019d21d2016-10-03 12:59:47 -0700123
Timur Iskhakovf1b902d2017-08-13 20:14:31 -0700124 private:
125 std::vector<const EnumType*> typeChain() const;
126 std::vector<const EnumType*> superTypeChain() const;
127
Andreas Huber019d21d2016-10-03 12:59:47 -0700128 const Annotation *findExportAnnotation() const;
129
Steven Moreland6961d3f2017-11-17 14:23:39 -0800130 void emitIteratorDeclaration(Formatter& out) const;
131 void emitIteratorDefinitions(Formatter& out) const;
132
Jayant Chowdhary2820f8a2016-11-10 12:29:09 -0800133 void emitEnumBitwiseOperator(
134 Formatter &out,
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800135 bool lhsIsEnum,
136 bool rhsIsEnum,
137 const std::string &op) const;
138
139 void emitBitFieldBitwiseAssignmentOperator(
140 Formatter &out,
Jayant Chowdhary2820f8a2016-11-10 12:29:09 -0800141 const std::string &op) const;
Andreas Hubere3f769a2016-10-10 10:54:44 -0700142
Yifan Hongf24fa852016-09-23 11:03:15 -0700143 std::vector<EnumValue *> mValues;
Timur Iskhakov505316c2017-08-05 03:38:59 +0000144 Reference<Type> mStorageType;
Andreas Huberc9410c72016-07-28 12:18:40 -0700145
146 DISALLOW_COPY_AND_ASSIGN(EnumType);
147};
148
Steven Moreland49bad8d2018-05-17 15:45:26 -0700149struct EnumValue : public LocalIdentifier, DocCommentable {
Neel Mehta69920a62019-07-22 16:22:13 -0700150 EnumValue(const std::string& name, ConstantExpression* value, const Location& location);
Andreas Huber31629bc2016-08-03 09:06:40 -0700151
152 std::string name() const;
Steven Morelandf21962d2018-08-09 12:44:40 -0700153 std::string rawValue(ScalarType::Kind castKind) const;
Yifan Hongfc610cd2016-09-22 13:34:45 -0700154 std::string cppValue(ScalarType::Kind castKind) const;
155 std::string javaValue(ScalarType::Kind castKind) const;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700156 void autofill(const EnumType* prevType, EnumValue* prevValue, const ScalarType* type);
Timur Iskhakov7296af12017-08-09 21:52:48 +0000157 ConstantExpression* constExpr() const override;
Andreas Huber31629bc2016-08-03 09:06:40 -0700158
Yifan Hongf24fa852016-09-23 11:03:15 -0700159 bool isAutoFill() const;
160 bool isEnumValue() const override;
161
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700162 const Location& location() const;
163
164 private:
Andreas Huber31629bc2016-08-03 09:06:40 -0700165 std::string mName;
Timur Iskhakov7296af12017-08-09 21:52:48 +0000166 ConstantExpression* mValue;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700167 const Location mLocation;
Yifan Hongf24fa852016-09-23 11:03:15 -0700168 bool mIsAutoFill;
Andreas Huber31629bc2016-08-03 09:06:40 -0700169
170 DISALLOW_COPY_AND_ASSIGN(EnumValue);
171};
172
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800173struct BitFieldType : public TemplatedType {
Timur Iskhakov63f39902017-08-29 15:47:29 -0700174 BitFieldType(Scope* parent);
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800175
Timur Iskhakov3f1d26e2017-08-30 15:35:53 -0700176 std::string templatedTypeName() const override;
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800177
Yifan Hongc14dd6e2017-09-15 13:39:58 -0700178 const EnumType* getElementEnumType() const;
179
Yifan Hongabf73ee2016-12-05 18:47:00 -0800180 bool isBitField() const override;
181
Timur Iskhakov24e605b2017-08-30 14:02:55 -0700182 bool isCompatibleElementType(const Type* elementType) const override;
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800183
Yifan Hong8c56cbe2016-12-12 15:30:12 -0800184 bool isElidableType() const override;
185
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700186 bool deepCanCheckEquality(std::unordered_set<const Type*>* visited) const override;
Yifan Hong7d1839f2017-02-22 13:24:29 -0800187
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800188 const ScalarType *resolveToScalarType() const override;
189
190 std::string getCppType(StorageMode mode,
191 bool specifyNamespaces) const override;
192
193 std::string getJavaType(bool forInitializer) const override;
Nirav Atre66842a92018-06-28 18:14:13 -0700194 std::string getJavaTypeClass() const override;
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800195
196 std::string getJavaSuffix() const override;
197
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800198 std::string getVtsType() const override;
199
Timur Iskhakov24e605b2017-08-30 14:02:55 -0700200 const EnumType* getEnumType() const;
Yifan Honge45b5302017-02-22 10:49:07 -0800201
Steven Moreland368e4602018-02-16 14:21:49 -0800202 void emitVtsAttributeType(Formatter& out) const override;
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800203
204 void getAlignmentAndSize(size_t *align, size_t *size) const override;
205
206 void emitReaderWriter(
207 Formatter &out,
208 const std::string &name,
209 const std::string &parcelObj,
210 bool parcelObjIsPointer,
211 bool isReader,
212 ErrorMode mode) const override;
213
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800214 void emitDump(
215 Formatter &out,
216 const std::string &streamName,
217 const std::string &name) const override;
218
Yifan Honge45b5302017-02-22 10:49:07 -0800219 void emitJavaDump(
220 Formatter &out,
221 const std::string &streamName,
222 const std::string &name) const override;
223
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800224 void emitJavaFieldReaderWriter(
225 Formatter &out,
226 size_t depth,
227 const std::string &parcelName,
228 const std::string &blobName,
229 const std::string &fieldName,
230 const std::string &offset,
231 bool isReader) const override;
232};
233
Andreas Huberc9410c72016-07-28 12:18:40 -0700234} // namespace android
235
236#endif // ENUM_TYPE_H_
237