blob: 8ae7c36c398c09276cd4ce3318c36bb70724c96f [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
Andreas Huber881227d2016-08-02 14:20:21 -070025#include <vector>
Andreas Huberc9410c72016-07-28 12:18:40 -070026
27namespace android {
28
Andreas Huber31629bc2016-08-03 09:06:40 -070029struct EnumValue;
Yifan Honge45b5302017-02-22 10:49:07 -080030struct BitFieldType;
Andreas Huberc9410c72016-07-28 12:18:40 -070031
Yifan Hongf24fa852016-09-23 11:03:15 -070032struct EnumType : public Scope {
Timur Iskhakov565b0132017-09-06 18:07:11 -070033 EnumType(const char* localName, const FQName& fullName, const Location& location,
34 const Reference<Type>& storageType, Scope* parent);
Andreas Huberc9410c72016-07-28 12:18:40 -070035
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070036 const Type *storageType() const;
37 const std::vector<EnumValue *> &values() const;
Yifan Hongf24fa852016-09-23 11:03:15 -070038 void addValue(EnumValue *value);
39
Yifan Hong0a9cc862017-10-06 16:21:55 -070040 void forEachValueFromRoot(const std::function<void(EnumValue*)> f) const;
41
Steven Moreland12f0ab12018-11-02 17:27:37 -070042 // This is the number of distinct keys (even if they have colliding values)
43 size_t numValueNames() const;
44
Yifan Hongf24fa852016-09-23 11:03:15 -070045 LocalIdentifier *lookupIdentifier(const std::string &name) const override;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070046
Steven Moreland9df52442016-12-12 08:51:14 -080047 bool isElidableType() const override;
Andreas Huber737080b2016-08-02 15:38:04 -070048 const ScalarType *resolveToScalarType() const override;
49
Steven Moreland30bb6a82016-11-30 09:18:34 -080050 std::string typeName() const override;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070051 bool isEnum() const override;
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -070052 bool deepCanCheckEquality(std::unordered_set<const Type*>* visited) const override;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070053
Steven Moreland979e0992016-09-07 09:18:08 -070054 std::string getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -070055 bool specifyNamespaces) const override;
Andreas Huber881227d2016-08-02 14:20:21 -070056
Yifan Hong4ed13472016-11-02 10:44:11 -070057 std::string getJavaType(bool forInitializer) const override;
Nirav Atre66842a92018-06-28 18:14:13 -070058 std::string getJavaTypeClass() const override;
Andreas Huber4c865b72016-09-14 15:26:27 -070059
Andreas Huber2831d512016-08-15 09:33:47 -070060 std::string getJavaSuffix() const override;
61
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -070062 std::string getVtsType() const override;
63
Yifan Hongc14dd6e2017-09-15 13:39:58 -070064 std::string getBitfieldCppType(StorageMode mode, bool specifyNamespaces = true) const;
65 std::string getBitfieldJavaType(bool forInitializer = false) const;
Nirav Atre66842a92018-06-28 18:14:13 -070066 std::string getBitfieldJavaTypeClass() const;
Yifan Hongc14dd6e2017-09-15 13:39:58 -070067
Yifan Honge45b5302017-02-22 10:49:07 -080068 // Return the type that corresponds to bitfield<T>.
Timur Iskhakov24e605b2017-08-30 14:02:55 -070069 const BitFieldType* getBitfieldType() const;
Yifan Honge45b5302017-02-22 10:49:07 -080070
Timur Iskhakovb58f4182017-08-29 15:19:24 -070071 std::vector<const Reference<Type>*> getReferences() const override;
72 std::vector<const ConstantExpression*> getConstantExpressions() const override;
Timur Iskhakov33431e62017-08-21 17:31:23 -070073
Timur Iskhakovcec46c42017-08-09 00:22:02 -070074 status_t resolveInheritance() override;
Timur Iskhakovcec46c42017-08-09 00:22:02 -070075 status_t validate() const override;
76 status_t validateUniqueNames() const;
77
Andreas Huber881227d2016-08-02 14:20:21 -070078 void emitReaderWriter(
79 Formatter &out,
80 const std::string &name,
81 const std::string &parcelObj,
82 bool parcelObjIsPointer,
83 bool isReader,
84 ErrorMode mode) const override;
85
Andreas Huber85eabdb2016-08-25 11:24:49 -070086 void emitJavaFieldReaderWriter(
87 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -070088 size_t depth,
Andreas Huber709b62d2016-09-19 11:21:18 -070089 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -070090 const std::string &blobName,
91 const std::string &fieldName,
92 const std::string &offset,
93 bool isReader) const override;
94
Steven Moreland368e4602018-02-16 14:21:49 -080095 void emitTypeDeclarations(Formatter& out) const override;
Timur Iskhakovfd3f2502017-09-05 16:25:02 -070096 void emitTypeForwardDeclaration(Formatter& out) const override;
Steven Moreland6961d3f2017-11-17 14:23:39 -080097 void emitGlobalTypeDeclarations(Formatter& out) const override;
Steven Moreland368e4602018-02-16 14:21:49 -080098 void emitPackageTypeDeclarations(Formatter& out) const override;
Steven Moreland09c6ebe2018-10-09 10:15:48 -070099 void emitPackageTypeHeaderDefinitions(Formatter& out) const override;
Andreas Huber85eabdb2016-08-25 11:24:49 -0700100
Steven Moreland368e4602018-02-16 14:21:49 -0800101 void emitJavaTypeDeclarations(Formatter& out, bool atTopLevel) const override;
Andreas Huber881227d2016-08-02 14:20:21 -0700102
Steven Moreland368e4602018-02-16 14:21:49 -0800103 void emitVtsTypeDeclarations(Formatter& out) const override;
104 void emitVtsAttributeType(Formatter& out) const override;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700105
Yifan Honge45b5302017-02-22 10:49:07 -0800106 void emitJavaDump(
107 Formatter &out,
108 const std::string &streamName,
109 const std::string &name) const override;
110
Andreas Huber85eabdb2016-08-25 11:24:49 -0700111 void getAlignmentAndSize(size_t *align, size_t *size) const override;
112
Andreas Huber019d21d2016-10-03 12:59:47 -0700113 void appendToExportedTypesVector(
114 std::vector<const Type *> *exportedTypes) const override;
115
Steven Moreland368e4602018-02-16 14:21:49 -0800116 void emitExportedHeader(Formatter& out, bool forJava) const override;
Andreas Huber019d21d2016-10-03 12:59:47 -0700117
Timur Iskhakovf1b902d2017-08-13 20:14:31 -0700118 private:
119 std::vector<const EnumType*> typeChain() const;
120 std::vector<const EnumType*> superTypeChain() const;
121
Andreas Huber019d21d2016-10-03 12:59:47 -0700122 const Annotation *findExportAnnotation() const;
123
Steven Moreland6961d3f2017-11-17 14:23:39 -0800124 void emitIteratorDeclaration(Formatter& out) const;
125 void emitIteratorDefinitions(Formatter& out) const;
126
Jayant Chowdhary2820f8a2016-11-10 12:29:09 -0800127 void emitEnumBitwiseOperator(
128 Formatter &out,
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800129 bool lhsIsEnum,
130 bool rhsIsEnum,
131 const std::string &op) const;
132
133 void emitBitFieldBitwiseAssignmentOperator(
134 Formatter &out,
Jayant Chowdhary2820f8a2016-11-10 12:29:09 -0800135 const std::string &op) const;
Andreas Hubere3f769a2016-10-10 10:54:44 -0700136
Yifan Hongf24fa852016-09-23 11:03:15 -0700137 std::vector<EnumValue *> mValues;
Timur Iskhakov505316c2017-08-05 03:38:59 +0000138 Reference<Type> mStorageType;
Andreas Huberc9410c72016-07-28 12:18:40 -0700139
140 DISALLOW_COPY_AND_ASSIGN(EnumType);
141};
142
Steven Moreland49bad8d2018-05-17 15:45:26 -0700143struct EnumValue : public LocalIdentifier, DocCommentable {
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700144 EnumValue(const char* name, ConstantExpression* value, const Location& location);
Andreas Huber31629bc2016-08-03 09:06:40 -0700145
146 std::string name() const;
Steven Morelandf21962d2018-08-09 12:44:40 -0700147 std::string rawValue(ScalarType::Kind castKind) const;
Yifan Hongfc610cd2016-09-22 13:34:45 -0700148 std::string cppValue(ScalarType::Kind castKind) const;
149 std::string javaValue(ScalarType::Kind castKind) const;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700150 void autofill(const EnumType* prevType, EnumValue* prevValue, const ScalarType* type);
Timur Iskhakov7296af12017-08-09 21:52:48 +0000151 ConstantExpression* constExpr() const override;
Andreas Huber31629bc2016-08-03 09:06:40 -0700152
Yifan Hongf24fa852016-09-23 11:03:15 -0700153 bool isAutoFill() const;
154 bool isEnumValue() const override;
155
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700156 const Location& location() const;
157
158 private:
Andreas Huber31629bc2016-08-03 09:06:40 -0700159 std::string mName;
Timur Iskhakov7296af12017-08-09 21:52:48 +0000160 ConstantExpression* mValue;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700161 const Location mLocation;
Yifan Hongf24fa852016-09-23 11:03:15 -0700162 bool mIsAutoFill;
Andreas Huber31629bc2016-08-03 09:06:40 -0700163
164 DISALLOW_COPY_AND_ASSIGN(EnumValue);
165};
166
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800167struct BitFieldType : public TemplatedType {
Timur Iskhakov63f39902017-08-29 15:47:29 -0700168 BitFieldType(Scope* parent);
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800169
Timur Iskhakov3f1d26e2017-08-30 15:35:53 -0700170 std::string templatedTypeName() const override;
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800171
Yifan Hongc14dd6e2017-09-15 13:39:58 -0700172 const EnumType* getElementEnumType() const;
173
Yifan Hongabf73ee2016-12-05 18:47:00 -0800174 bool isBitField() const override;
175
Timur Iskhakov24e605b2017-08-30 14:02:55 -0700176 bool isCompatibleElementType(const Type* elementType) const override;
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800177
Yifan Hong8c56cbe2016-12-12 15:30:12 -0800178 bool isElidableType() const override;
179
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700180 bool deepCanCheckEquality(std::unordered_set<const Type*>* visited) const override;
Yifan Hong7d1839f2017-02-22 13:24:29 -0800181
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800182 const ScalarType *resolveToScalarType() const override;
183
184 std::string getCppType(StorageMode mode,
185 bool specifyNamespaces) const override;
186
187 std::string getJavaType(bool forInitializer) const override;
Nirav Atre66842a92018-06-28 18:14:13 -0700188 std::string getJavaTypeClass() const override;
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800189
190 std::string getJavaSuffix() const override;
191
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800192 std::string getVtsType() const override;
193
Timur Iskhakov24e605b2017-08-30 14:02:55 -0700194 const EnumType* getEnumType() const;
Yifan Honge45b5302017-02-22 10:49:07 -0800195
Steven Moreland368e4602018-02-16 14:21:49 -0800196 void emitVtsAttributeType(Formatter& out) const override;
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800197
198 void getAlignmentAndSize(size_t *align, size_t *size) const override;
199
200 void emitReaderWriter(
201 Formatter &out,
202 const std::string &name,
203 const std::string &parcelObj,
204 bool parcelObjIsPointer,
205 bool isReader,
206 ErrorMode mode) const override;
207
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800208 void emitDump(
209 Formatter &out,
210 const std::string &streamName,
211 const std::string &name) const override;
212
Yifan Honge45b5302017-02-22 10:49:07 -0800213 void emitJavaDump(
214 Formatter &out,
215 const std::string &streamName,
216 const std::string &name) const override;
217
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800218 void emitJavaFieldReaderWriter(
219 Formatter &out,
220 size_t depth,
221 const std::string &parcelName,
222 const std::string &blobName,
223 const std::string &fieldName,
224 const std::string &offset,
225 bool isReader) const override;
226};
227
Andreas Huberc9410c72016-07-28 12:18:40 -0700228} // namespace android
229
230#endif // ENUM_TYPE_H_
231