blob: b8530549689a952ea5d2b6c1d861212fc3792f15 [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;
Yifan Honge45b5302017-02-22 10:49:07 -080029struct BitFieldType;
Andreas Huberc9410c72016-07-28 12:18:40 -070030
Yifan Hongf24fa852016-09-23 11:03:15 -070031struct EnumType : public Scope {
Andreas Huber9ed827c2016-08-22 12:31:13 -070032 EnumType(const char *localName,
Yifan Honga4b53d02016-10-31 17:29:10 -070033 const Location &location,
Steven Moreland1c71fd52016-11-29 14:03:33 -080034 Type *storageType);
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
40 LocalIdentifier *lookupIdentifier(const std::string &name) const override;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070041
Steven Moreland9df52442016-12-12 08:51:14 -080042 bool isElidableType() const override;
Andreas Huber737080b2016-08-02 15:38:04 -070043 const ScalarType *resolveToScalarType() const override;
44
Steven Moreland30bb6a82016-11-30 09:18:34 -080045 std::string typeName() const override;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070046 bool isEnum() const override;
Yifan Hongc6752dc2016-12-20 14:00:14 -080047 bool canCheckEquality() const override;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070048
Steven Moreland979e0992016-09-07 09:18:08 -070049 std::string getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -070050 bool specifyNamespaces) const override;
Andreas Huber881227d2016-08-02 14:20:21 -070051
Yifan Hong4ed13472016-11-02 10:44:11 -070052 std::string getJavaType(bool forInitializer) const override;
Andreas Huber4c865b72016-09-14 15:26:27 -070053
Andreas Huber2831d512016-08-15 09:33:47 -070054 std::string getJavaSuffix() const override;
55
Andreas Hubera3558b32016-09-14 09:12:42 -070056 std::string getJavaWrapperType() const override;
57
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -070058 std::string getVtsType() const override;
59
Yifan Honge45b5302017-02-22 10:49:07 -080060 // Return the type that corresponds to bitfield<T>.
61 BitFieldType *getBitfieldType() const;
62
Andreas Huber881227d2016-08-02 14:20:21 -070063 void emitReaderWriter(
64 Formatter &out,
65 const std::string &name,
66 const std::string &parcelObj,
67 bool parcelObjIsPointer,
68 bool isReader,
69 ErrorMode mode) const override;
70
Andreas Huber85eabdb2016-08-25 11:24:49 -070071 void emitJavaFieldReaderWriter(
72 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -070073 size_t depth,
Andreas Huber709b62d2016-09-19 11:21:18 -070074 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -070075 const std::string &blobName,
76 const std::string &fieldName,
77 const std::string &offset,
78 bool isReader) const override;
79
Andreas Huber881227d2016-08-02 14:20:21 -070080 status_t emitTypeDeclarations(Formatter &out) const override;
Andreas Hubere3f769a2016-10-10 10:54:44 -070081 status_t emitGlobalTypeDeclarations(Formatter &out) const override;
Yifan Hongf5cc2f72017-01-04 18:02:34 -080082 status_t emitTypeDefinitions(Formatter &out, const std::string prefix) const override;
Andreas Huber85eabdb2016-08-25 11:24:49 -070083
84 status_t emitJavaTypeDeclarations(
85 Formatter &out, bool atTopLevel) const override;
Andreas Huber881227d2016-08-02 14:20:21 -070086
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070087 status_t emitVtsTypeDeclarations(Formatter &out) const override;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070088 status_t emitVtsAttributeType(Formatter &out) const override;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070089
Yifan Honge45b5302017-02-22 10:49:07 -080090 void emitJavaDump(
91 Formatter &out,
92 const std::string &streamName,
93 const std::string &name) const override;
94
Andreas Huber85eabdb2016-08-25 11:24:49 -070095 void getAlignmentAndSize(size_t *align, size_t *size) const override;
96
Andreas Huber019d21d2016-10-03 12:59:47 -070097 void appendToExportedTypesVector(
98 std::vector<const Type *> *exportedTypes) const override;
99
Andreas Huber1c507272016-10-05 14:33:21 -0700100 status_t emitExportedHeader(Formatter &out, bool forJava) const override;
Andreas Huber019d21d2016-10-03 12:59:47 -0700101
Andreas Huberc9410c72016-07-28 12:18:40 -0700102private:
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700103 void getTypeChain(std::vector<const EnumType *> *out) const;
Andreas Huber019d21d2016-10-03 12:59:47 -0700104 const Annotation *findExportAnnotation() const;
105
Jayant Chowdhary2820f8a2016-11-10 12:29:09 -0800106 void emitEnumBitwiseOperator(
107 Formatter &out,
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800108 bool lhsIsEnum,
109 bool rhsIsEnum,
110 const std::string &op) const;
111
112 void emitBitFieldBitwiseAssignmentOperator(
113 Formatter &out,
Jayant Chowdhary2820f8a2016-11-10 12:29:09 -0800114 const std::string &op) const;
Andreas Hubere3f769a2016-10-10 10:54:44 -0700115
Yifan Hongf24fa852016-09-23 11:03:15 -0700116 std::vector<EnumValue *> mValues;
Andreas Huberc9410c72016-07-28 12:18:40 -0700117 Type *mStorageType;
Yifan Honge45b5302017-02-22 10:49:07 -0800118 BitFieldType *mBitfieldType;
Andreas Huberc9410c72016-07-28 12:18:40 -0700119
120 DISALLOW_COPY_AND_ASSIGN(EnumType);
121};
122
Yifan Hongf24fa852016-09-23 11:03:15 -0700123struct EnumValue : public LocalIdentifier {
124 EnumValue(const char *name, ConstantExpression *value = nullptr);
Andreas Huber31629bc2016-08-03 09:06:40 -0700125
126 std::string name() const;
Yifan Hongc07b2022016-11-08 12:44:24 -0800127 std::string value(ScalarType::Kind castKind) const;
Yifan Hongfc610cd2016-09-22 13:34:45 -0700128 std::string cppValue(ScalarType::Kind castKind) const;
129 std::string javaValue(ScalarType::Kind castKind) const;
130 std::string comment() const;
Yifan Hongf24fa852016-09-23 11:03:15 -0700131 void autofill(const EnumValue *prev, const ScalarType *type);
132 ConstantExpression *constExpr() const;
Andreas Huber31629bc2016-08-03 09:06:40 -0700133
Yifan Hongf24fa852016-09-23 11:03:15 -0700134 bool isAutoFill() const;
135 bool isEnumValue() const override;
136
137
Andreas Huber31629bc2016-08-03 09:06:40 -0700138 std::string mName;
Yifan Hongf24fa852016-09-23 11:03:15 -0700139 ConstantExpression *mValue;
140 bool mIsAutoFill;
Andreas Huber31629bc2016-08-03 09:06:40 -0700141
142 DISALLOW_COPY_AND_ASSIGN(EnumValue);
143};
144
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800145struct BitFieldType : public TemplatedType {
146
147 std::string typeName() const override;
148
Yifan Hongabf73ee2016-12-05 18:47:00 -0800149 bool isBitField() const override;
150
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800151 void addNamedTypesToSet(std::set<const FQName> &set) const override;
152
153 bool isCompatibleElementType(Type *elementType) const override;
154
Yifan Hong8c56cbe2016-12-12 15:30:12 -0800155 bool isElidableType() const override;
156
Yifan Hong7d1839f2017-02-22 13:24:29 -0800157 bool canCheckEquality() const override;
158
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800159 const ScalarType *resolveToScalarType() const override;
160
161 std::string getCppType(StorageMode mode,
162 bool specifyNamespaces) const override;
163
164 std::string getJavaType(bool forInitializer) const override;
165
166 std::string getJavaSuffix() const override;
167
168 std::string getJavaWrapperType() const override;
169
170 std::string getVtsType() const override;
171
Yifan Honge45b5302017-02-22 10:49:07 -0800172 EnumType *getEnumType() const;
173
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800174 status_t emitVtsAttributeType(Formatter &out) const override;
175
176 void getAlignmentAndSize(size_t *align, size_t *size) const override;
177
178 void emitReaderWriter(
179 Formatter &out,
180 const std::string &name,
181 const std::string &parcelObj,
182 bool parcelObjIsPointer,
183 bool isReader,
184 ErrorMode mode) const override;
185
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800186 void emitDump(
187 Formatter &out,
188 const std::string &streamName,
189 const std::string &name) const override;
190
Yifan Honge45b5302017-02-22 10:49:07 -0800191 void emitJavaDump(
192 Formatter &out,
193 const std::string &streamName,
194 const std::string &name) const override;
195
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800196 void emitJavaFieldReaderWriter(
197 Formatter &out,
198 size_t depth,
199 const std::string &parcelName,
200 const std::string &blobName,
201 const std::string &fieldName,
202 const std::string &offset,
203 bool isReader) const override;
204};
205
Andreas Huberc9410c72016-07-28 12:18:40 -0700206} // namespace android
207
208#endif // ENUM_TYPE_H_
209