blob: 92cb3a7026c7368a1dc736db0d16060a59fbaf77 [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#include "EnumType.h"
18
Andreas Huber019d21d2016-10-03 12:59:47 -070019#include "Annotation.h"
Andreas Huber881227d2016-08-02 14:20:21 -070020#include "ScalarType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070021
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070022#include <inttypes.h>
23#include <hidl-util/Formatter.h>
Andreas Huber737080b2016-08-02 15:38:04 -070024#include <android-base/logging.h>
25
Andreas Huberc9410c72016-07-28 12:18:40 -070026namespace android {
27
Andreas Huberc9410c72016-07-28 12:18:40 -070028EnumType::EnumType(
Andreas Huber9ed827c2016-08-22 12:31:13 -070029 const char *localName,
Yifan Honga4b53d02016-10-31 17:29:10 -070030 const Location &location,
Andreas Huber9ed827c2016-08-22 12:31:13 -070031 Type *storageType)
Yifan Honga4b53d02016-10-31 17:29:10 -070032 : Scope(localName, location),
Yifan Hongf24fa852016-09-23 11:03:15 -070033 mValues(),
Steven Moreland1c71fd52016-11-29 14:03:33 -080034 mStorageType(storageType) {
Andreas Huberc9410c72016-07-28 12:18:40 -070035}
36
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070037const Type *EnumType::storageType() const {
38 return mStorageType;
39}
40
41const std::vector<EnumValue *> &EnumType::values() const {
Yifan Hongf24fa852016-09-23 11:03:15 -070042 return mValues;
43}
44
45void EnumType::addValue(EnumValue *value) {
46 CHECK(value != nullptr);
47
48 EnumValue *prev = nullptr;
49 std::vector<const EnumType *> chain;
50 getTypeChain(&chain);
51 for (auto it = chain.begin(); it != chain.end(); ++it) {
52 const auto &type = *it;
53 if(!type->values().empty()) {
54 prev = type->values().back();
55 break;
56 }
57 }
58
59 value->autofill(prev, resolveToScalarType());
60 mValues.push_back(value);
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070061}
62
Andreas Huber737080b2016-08-02 15:38:04 -070063const ScalarType *EnumType::resolveToScalarType() const {
64 return mStorageType->resolveToScalarType();
65}
66
Steven Moreland30bb6a82016-11-30 09:18:34 -080067std::string EnumType::typeName() const {
68 return "enum " + localName();
69}
70
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070071bool EnumType::isEnum() const {
72 return true;
73}
74
Steven Moreland979e0992016-09-07 09:18:08 -070075std::string EnumType::getCppType(StorageMode,
Steven Moreland979e0992016-09-07 09:18:08 -070076 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -070077 return specifyNamespaces ? fullName() : partialCppName();
Andreas Huber881227d2016-08-02 14:20:21 -070078}
79
Yifan Hong4ed13472016-11-02 10:44:11 -070080std::string EnumType::getJavaType(bool forInitializer) const {
81 return mStorageType->resolveToScalarType()->getJavaType(forInitializer);
Andreas Huber2831d512016-08-15 09:33:47 -070082}
83
84std::string EnumType::getJavaSuffix() const {
85 return mStorageType->resolveToScalarType()->getJavaSuffix();
86}
87
Andreas Hubera3558b32016-09-14 09:12:42 -070088std::string EnumType::getJavaWrapperType() const {
89 return mStorageType->resolveToScalarType()->getJavaWrapperType();
90}
91
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -070092std::string EnumType::getVtsType() const {
93 return "TYPE_ENUM";
94}
95
Yifan Hongf24fa852016-09-23 11:03:15 -070096LocalIdentifier *EnumType::lookupIdentifier(const std::string &name) const {
97 std::vector<const EnumType *> chain;
98 getTypeChain(&chain);
99 for (auto it = chain.begin(); it != chain.end(); ++it) {
100 const auto &type = *it;
101 for(EnumValue *v : type->values()) {
102 if(v->name() == name) {
103 return v;
104 }
105 }
106 }
107 return nullptr;
108}
109
Andreas Huber881227d2016-08-02 14:20:21 -0700110void EnumType::emitReaderWriter(
111 Formatter &out,
112 const std::string &name,
113 const std::string &parcelObj,
114 bool parcelObjIsPointer,
115 bool isReader,
116 ErrorMode mode) const {
Andreas Huber737080b2016-08-02 15:38:04 -0700117 const ScalarType *scalarType = mStorageType->resolveToScalarType();
118 CHECK(scalarType != NULL);
119
120 scalarType->emitReaderWriterWithCast(
121 out,
122 name,
123 parcelObj,
124 parcelObjIsPointer,
125 isReader,
126 mode,
127 true /* needsCast */);
Andreas Huber881227d2016-08-02 14:20:21 -0700128}
129
Andreas Huber85eabdb2016-08-25 11:24:49 -0700130void EnumType::emitJavaFieldReaderWriter(
131 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700132 size_t depth,
Andreas Huber709b62d2016-09-19 11:21:18 -0700133 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700134 const std::string &blobName,
135 const std::string &fieldName,
136 const std::string &offset,
137 bool isReader) const {
138 return mStorageType->emitJavaFieldReaderWriter(
Andreas Huber709b62d2016-09-19 11:21:18 -0700139 out, depth, parcelName, blobName, fieldName, offset, isReader);
Andreas Huber85eabdb2016-08-25 11:24:49 -0700140}
141
Andreas Huber881227d2016-08-02 14:20:21 -0700142status_t EnumType::emitTypeDeclarations(Formatter &out) const {
Andreas Huber737080b2016-08-02 15:38:04 -0700143 const ScalarType *scalarType = mStorageType->resolveToScalarType();
Andreas Hubere3f769a2016-10-10 10:54:44 -0700144 CHECK(scalarType != nullptr);
Andreas Huber737080b2016-08-02 15:38:04 -0700145
Yifan Hong3b320f82016-11-01 15:15:54 -0700146 const std::string storageType = scalarType->getCppStackType();
Andreas Huber881227d2016-08-02 14:20:21 -0700147
148 out << "enum class "
Andreas Huber0e00de42016-08-03 09:56:02 -0700149 << localName()
Andreas Huber881227d2016-08-02 14:20:21 -0700150 << " : "
Andreas Hubere3f769a2016-10-10 10:54:44 -0700151 << storageType
Andreas Huber881227d2016-08-02 14:20:21 -0700152 << " {\n";
153
154 out.indent();
155
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700156 std::vector<const EnumType *> chain;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700157 getTypeChain(&chain);
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700158
159 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
160 const auto &type = *it;
161
162 for (const auto &entry : type->values()) {
163 out << entry->name();
164
Yifan Hongfc610cd2016-09-22 13:34:45 -0700165 std::string value = entry->cppValue(scalarType->getKind());
Yifan Hongf24fa852016-09-23 11:03:15 -0700166 CHECK(!value.empty()); // use autofilled values for c++.
167 out << " = " << value;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700168
Yifan Hong57886972016-08-17 10:42:15 -0700169 out << ",";
170
Yifan Hongfc610cd2016-09-22 13:34:45 -0700171 std::string comment = entry->comment();
172 if (!comment.empty() && comment != value) {
Yifan Hong57886972016-08-17 10:42:15 -0700173 out << " // " << comment;
174 }
175
176 out << "\n";
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700177 }
Andreas Huber881227d2016-08-02 14:20:21 -0700178 }
179
180 out.unindent();
181 out << "};\n\n";
182
183 return OK;
184}
185
Jayant Chowdhary2820f8a2016-11-10 12:29:09 -0800186void EnumType::emitEnumBitwiseOperator(
187 Formatter &out,
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800188 bool lhsIsEnum,
189 bool rhsIsEnum,
Jayant Chowdhary2820f8a2016-11-10 12:29:09 -0800190 const std::string &op) const {
Andreas Hubere3f769a2016-10-10 10:54:44 -0700191 const ScalarType *scalarType = mStorageType->resolveToScalarType();
192 CHECK(scalarType != nullptr);
193
Yifan Hong3b320f82016-11-01 15:15:54 -0700194 const std::string storageType = scalarType->getCppStackType();
Andreas Hubere3f769a2016-10-10 10:54:44 -0700195
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800196 out << "constexpr "
197 << storageType
Jayant Chowdhary2820f8a2016-11-10 12:29:09 -0800198 << " operator"
199 << op
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800200 << "(const "
201 << (lhsIsEnum ? fullName() : storageType)
202 << " lhs, const "
203 << (rhsIsEnum ? fullName() : storageType)
Andreas Hubere3f769a2016-10-10 10:54:44 -0700204 << " rhs) {\n";
Andreas Hubere3f769a2016-10-10 10:54:44 -0700205
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800206 out.indentBlock([&] {
207 out << "return static_cast<"
208 << storageType
209 << ">(";
Andreas Hubere3f769a2016-10-10 10:54:44 -0700210
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800211 if (lhsIsEnum) {
212 out << "static_cast<"
213 << storageType
214 << ">(lhs)";
215 } else {
216 out << "lhs";
217 }
218 out << " " << op << " ";
219 if (rhsIsEnum) {
220 out << "static_cast<"
221 << storageType
222 << ">(rhs)";
223 } else {
224 out << "rhs";
225 }
226 out << ");\n";
227 });
Andreas Hubere3f769a2016-10-10 10:54:44 -0700228
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800229 out << "}\n\n";
230}
231
232void EnumType::emitBitFieldBitwiseAssignmentOperator(
233 Formatter &out,
234 const std::string &op) const {
235 const ScalarType *scalarType = mStorageType->resolveToScalarType();
236 CHECK(scalarType != nullptr);
237
238 const std::string storageType = scalarType->getCppStackType();
239
240 out << "constexpr " << storageType << " &operator" << op << "=("
241 << storageType << "& v, const " << fullName() << " e) {\n";
242
243 out.indentBlock([&] {
244 out << "v " << op << "= static_cast<" << storageType << ">(e);\n";
245 out << "return v;\n";
246 });
Andreas Hubere3f769a2016-10-10 10:54:44 -0700247
248 out << "}\n\n";
249}
250
251status_t EnumType::emitGlobalTypeDeclarations(Formatter &out) const {
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800252 emitEnumBitwiseOperator(out, true /* lhsIsEnum */, true /* rhsIsEnum */, "|");
253 emitEnumBitwiseOperator(out, false /* lhsIsEnum */, true /* rhsIsEnum */, "|");
254 emitEnumBitwiseOperator(out, true /* lhsIsEnum */, false /* rhsIsEnum */, "|");
255 emitEnumBitwiseOperator(out, true /* lhsIsEnum */, true /* rhsIsEnum */, "&");
256 emitEnumBitwiseOperator(out, false /* lhsIsEnum */, true /* rhsIsEnum */, "&");
257 emitEnumBitwiseOperator(out, true /* lhsIsEnum */, false /* rhsIsEnum */, "&");
258
259 emitBitFieldBitwiseAssignmentOperator(out, "|");
260 emitBitFieldBitwiseAssignmentOperator(out, "&");
Andreas Hubere3f769a2016-10-10 10:54:44 -0700261
262 return OK;
263}
264
Andreas Huber85eabdb2016-08-25 11:24:49 -0700265status_t EnumType::emitJavaTypeDeclarations(Formatter &out, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700266 const ScalarType *scalarType = mStorageType->resolveToScalarType();
267 CHECK(scalarType != NULL);
268
269 out << "public final class "
270 << localName()
271 << " {\n";
272
273 out.indent();
274
Andreas Huber4c865b72016-09-14 15:26:27 -0700275 const std::string typeName =
Yifan Hong4ed13472016-11-02 10:44:11 -0700276 scalarType->getJavaType(false /* forInitializer */);
Andreas Huber2831d512016-08-15 09:33:47 -0700277
278 std::vector<const EnumType *> chain;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700279 getTypeChain(&chain);
Andreas Huber2831d512016-08-15 09:33:47 -0700280
281 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
282 const auto &type = *it;
283
284 for (const auto &entry : type->values()) {
285 out << "public static final "
286 << typeName
287 << " "
Andreas Huberab647c02016-09-14 09:44:00 -0700288 << entry->name()
289 << " = ";
Andreas Huber2831d512016-08-15 09:33:47 -0700290
Yifan Hongf24fa852016-09-23 11:03:15 -0700291 // javaValue will make the number signed.
Yifan Hongfc610cd2016-09-22 13:34:45 -0700292 std::string value = entry->javaValue(scalarType->getKind());
Yifan Hongf24fa852016-09-23 11:03:15 -0700293 CHECK(!value.empty()); // use autofilled values for java.
294 out << value;
Andreas Huber2831d512016-08-15 09:33:47 -0700295
Yifan Hong19ca75a2016-08-31 10:20:03 -0700296 out << ";";
297
Yifan Hongfc610cd2016-09-22 13:34:45 -0700298 std::string comment = entry->comment();
299 if (!comment.empty() && comment != value) {
Yifan Hong19ca75a2016-08-31 10:20:03 -0700300 out << " // " << comment;
301 }
302
303 out << "\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700304 }
305 }
306
307 out.unindent();
308 out << "};\n\n";
309
310 return OK;
311}
312
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700313status_t EnumType::emitVtsTypeDeclarations(Formatter &out) const {
Yifan Hongc07b2022016-11-08 12:44:24 -0800314 const ScalarType *scalarType = mStorageType->resolveToScalarType();
315
Zhuoyao Zhangbf828c82016-10-26 14:15:10 -0700316 out << "name: \"" << fullName() << "\"\n";
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700317 out << "type: " << getVtsType() << "\n";
318 out << "enum_value: {\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700319 out.indent();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700320
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700321 out << "scalar_type: \""
Yifan Hongc07b2022016-11-08 12:44:24 -0800322 << scalarType->getVtsScalarType()
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700323 << "\"\n\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700324 std::vector<const EnumType *> chain;
325 getTypeChain(&chain);
326
327 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
328 const auto &type = *it;
329
330 for (const auto &entry : type->values()) {
331 out << "enumerator: \"" << entry->name() << "\"\n";
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700332 out << "scalar_value: {\n";
333 out.indent();
Yifan Hongc07b2022016-11-08 12:44:24 -0800334 // use autofilled values for vts.
335 std::string value = entry->value(scalarType->getKind());
336 CHECK(!value.empty());
337 out << mStorageType->resolveToScalarType()->getVtsScalarType()
338 << ": "
339 << value
340 << "\n";
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700341 out.unindent();
342 out << "}\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700343 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700344 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700345
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700346 out.unindent();
347 out << "}\n";
348 return OK;
349}
350
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700351status_t EnumType::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700352 out << "type: " << getVtsType() << "\n";
Zhuoyao Zhangbf828c82016-10-26 14:15:10 -0700353 out << "predefined_type: \"" << fullName() << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700354 return OK;
355}
356
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700357void EnumType::getTypeChain(std::vector<const EnumType *> *out) const {
358 out->clear();
359 const EnumType *type = this;
360 for (;;) {
361 out->push_back(type);
362
363 const Type *superType = type->storageType();
364 if (superType == NULL || !superType->isEnum()) {
365 break;
366 }
367
368 type = static_cast<const EnumType *>(superType);
369 }
370}
371
Andreas Huber85eabdb2016-08-25 11:24:49 -0700372void EnumType::getAlignmentAndSize(size_t *align, size_t *size) const {
373 mStorageType->getAlignmentAndSize(align, size);
374}
375
Andreas Huber019d21d2016-10-03 12:59:47 -0700376const Annotation *EnumType::findExportAnnotation() const {
377 for (const auto &annotation : annotations()) {
378 if (annotation->name() == "export") {
379 return annotation;
380 }
381 }
382
383 return nullptr;
384}
385
386void EnumType::appendToExportedTypesVector(
387 std::vector<const Type *> *exportedTypes) const {
388 if (findExportAnnotation() != nullptr) {
389 exportedTypes->push_back(this);
390 }
391}
392
Andreas Huber1c507272016-10-05 14:33:21 -0700393status_t EnumType::emitExportedHeader(Formatter &out, bool forJava) const {
Andreas Huber019d21d2016-10-03 12:59:47 -0700394 const Annotation *annotation = findExportAnnotation();
395 CHECK(annotation != nullptr);
396
397 std::string name = localName();
398
399 const AnnotationParam *nameParam = annotation->getParam("name");
400 if (nameParam != nullptr) {
401 CHECK_EQ(nameParam->getValues()->size(), 1u);
402
403 std::string quotedString = nameParam->getValues()->at(0);
404 name = quotedString.substr(1, quotedString.size() - 2);
405 }
406
Andreas Huberb0627fb2016-10-10 09:39:28 -0700407 std::string valuePrefix;
Andreas Huberb0627fb2016-10-10 09:39:28 -0700408 const AnnotationParam *prefixParam = annotation->getParam("value_prefix");
409 if (prefixParam != nullptr) {
410 CHECK_EQ(prefixParam->getValues()->size(), 1u);
411
412 std::string quotedString = prefixParam->getValues()->at(0);
413 valuePrefix = quotedString.substr(1, quotedString.size() - 2);
414 }
415
Steven Moreland73cdc882016-11-21 16:43:50 -0800416 std::string valueSuffix;
417 const AnnotationParam *suffixParam = annotation->getParam("value_suffix");
418 if (suffixParam != nullptr) {
419 CHECK_EQ(suffixParam->getValues()->size(), 1u);
420
421 std::string quotedString = suffixParam->getValues()->at(0);
422 valueSuffix = quotedString.substr(1, quotedString.size() - 2);
423 }
424
Andreas Huber019d21d2016-10-03 12:59:47 -0700425 const ScalarType *scalarType = mStorageType->resolveToScalarType();
Andreas Huber1c507272016-10-05 14:33:21 -0700426 CHECK(scalarType != nullptr);
Andreas Huber019d21d2016-10-03 12:59:47 -0700427
Andreas Huber1c507272016-10-05 14:33:21 -0700428 if (forJava) {
429 if (!name.empty()) {
430 out << "public final class "
431 << name
432 << " {\n";
433
434 out.indent();
435 } else {
436 out << "// Values declared in " << localName() << " follow.\n";
437 }
438
Andreas Huber1c507272016-10-05 14:33:21 -0700439 const std::string typeName =
Yifan Hong4ed13472016-11-02 10:44:11 -0700440 scalarType->getJavaType(false /* forInitializer */);
Andreas Huber1c507272016-10-05 14:33:21 -0700441
442 std::vector<const EnumType *> chain;
443 getTypeChain(&chain);
444
445 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
446 const auto &type = *it;
447
448 for (const auto &entry : type->values()) {
449 out << "public static final "
450 << typeName
451 << " "
452 << valuePrefix
453 << entry->name()
Steven Moreland73cdc882016-11-21 16:43:50 -0800454 << valueSuffix
Andreas Huber1c507272016-10-05 14:33:21 -0700455 << " = ";
456
457 // javaValue will make the number signed.
458 std::string value = entry->javaValue(scalarType->getKind());
459 CHECK(!value.empty()); // use autofilled values for java.
460 out << value;
461
462 out << ";";
463
464 std::string comment = entry->comment();
465 if (!comment.empty() && comment != value) {
466 out << " // " << comment;
467 }
468
469 out << "\n";
470 }
471 }
472
473 if (!name.empty()) {
474 out.unindent();
475 out << "};\n";
476 }
477 out << "\n";
478
479 return OK;
480 }
Andreas Huber019d21d2016-10-03 12:59:47 -0700481
482 if (!name.empty()) {
483 out << "typedef ";
484 }
485
486 out << "enum {\n";
487
488 out.indent();
489
490 std::vector<const EnumType *> chain;
491 getTypeChain(&chain);
492
493 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
494 const auto &type = *it;
495
496 for (const auto &entry : type->values()) {
Steven Moreland73cdc882016-11-21 16:43:50 -0800497 out << valuePrefix << entry->name() << valueSuffix;
Andreas Huber019d21d2016-10-03 12:59:47 -0700498
499 std::string value = entry->cppValue(scalarType->getKind());
500 CHECK(!value.empty()); // use autofilled values for c++.
501 out << " = " << value;
502
503 out << ",";
504
505 std::string comment = entry->comment();
506 if (!comment.empty() && comment != value) {
507 out << " // " << comment;
508 }
509
510 out << "\n";
511 }
512 }
513
514 out.unindent();
515 out << "}";
516
517 if (!name.empty()) {
518 out << " " << name;
519 }
520
521 out << ";\n\n";
522
523 return OK;
524}
525
Andreas Huber31629bc2016-08-03 09:06:40 -0700526////////////////////////////////////////////////////////////////////////////////
527
Yifan Hongf24fa852016-09-23 11:03:15 -0700528EnumValue::EnumValue(const char *name, ConstantExpression *value)
Andreas Huber31629bc2016-08-03 09:06:40 -0700529 : mName(name),
Yifan Hongf24fa852016-09-23 11:03:15 -0700530 mValue(value),
531 mIsAutoFill(false) {
Andreas Huber31629bc2016-08-03 09:06:40 -0700532}
533
534std::string EnumValue::name() const {
535 return mName;
536}
537
Yifan Hongc07b2022016-11-08 12:44:24 -0800538std::string EnumValue::value(ScalarType::Kind castKind) const {
Yifan Hongf24fa852016-09-23 11:03:15 -0700539 CHECK(mValue != nullptr);
Yifan Hongc07b2022016-11-08 12:44:24 -0800540 return mValue->value(castKind);
Yifan Hong57886972016-08-17 10:42:15 -0700541}
542
Yifan Hongfc610cd2016-09-22 13:34:45 -0700543std::string EnumValue::cppValue(ScalarType::Kind castKind) const {
Yifan Hongf24fa852016-09-23 11:03:15 -0700544 CHECK(mValue != nullptr);
545 return mValue->cppValue(castKind);
Yifan Hong57886972016-08-17 10:42:15 -0700546}
Yifan Hongfc610cd2016-09-22 13:34:45 -0700547std::string EnumValue::javaValue(ScalarType::Kind castKind) const {
Yifan Hongf24fa852016-09-23 11:03:15 -0700548 CHECK(mValue != nullptr);
549 return mValue->javaValue(castKind);
Yifan Hong19ca75a2016-08-31 10:20:03 -0700550}
Yifan Hong57886972016-08-17 10:42:15 -0700551
Yifan Hongfc610cd2016-09-22 13:34:45 -0700552std::string EnumValue::comment() const {
Yifan Hongf24fa852016-09-23 11:03:15 -0700553 CHECK(mValue != nullptr);
554 return mValue->description();
555}
556
557ConstantExpression *EnumValue::constExpr() const {
558 CHECK(mValue != nullptr);
559 return mValue;
560}
561
562void EnumValue::autofill(const EnumValue *prev, const ScalarType *type) {
563 if(mValue != nullptr)
564 return;
565 mIsAutoFill = true;
566 ConstantExpression *value = new ConstantExpression();
567 if(prev == nullptr) {
568 *value = ConstantExpression::Zero(type->getKind());
569 } else {
570 CHECK(prev->mValue != nullptr);
571 *value = prev->mValue->addOne();
572 }
573 mValue = value;
574}
575
576bool EnumValue::isAutoFill() const {
577 return mIsAutoFill;
578}
579
580bool EnumValue::isEnumValue() const {
581 return true;
Andreas Huber31629bc2016-08-03 09:06:40 -0700582}
583
Yifan Hongabf73ee2016-12-05 18:47:00 -0800584////////////////////////////////////////////////////////////////////////////////
585
586bool BitFieldType::isBitField() const {
587 return true;
588}
589
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800590std::string BitFieldType::typeName() const {
591 return "mask" + (mElementType == nullptr ? "" : (" of " + mElementType->typeName()));
592}
593
594void BitFieldType::addNamedTypesToSet(std::set<const FQName> &) const {
595}
596
597bool BitFieldType::isCompatibleElementType(Type *elementType) const {
598 return elementType->isEnum();
599}
600
601const ScalarType *BitFieldType::resolveToScalarType() const {
602 return mElementType->resolveToScalarType();
603}
604
605std::string BitFieldType::getCppType(StorageMode mode,
606 bool specifyNamespaces) const {
607 return resolveToScalarType()->getCppType(mode, specifyNamespaces);
608}
609
610std::string BitFieldType::getJavaType(bool forInitializer) const {
611 return resolveToScalarType()->getJavaType(forInitializer);
612}
613
614std::string BitFieldType::getJavaSuffix() const {
615 return resolveToScalarType()->getJavaSuffix();
616}
617
618std::string BitFieldType::getJavaWrapperType() const {
619 return resolveToScalarType()->getJavaWrapperType();
620}
621
622std::string BitFieldType::getVtsType() const {
623 return "TYPE_MASK";
624}
625
626status_t BitFieldType::emitVtsTypeDeclarations(Formatter &out) const {
627 out << "type: " << getVtsType() << "\n";
628 out << "enum_value: {\n";
629 out.indent();
630 status_t err = mElementType->emitVtsTypeDeclarations(out);
631 if (err != OK) {
632 return err;
633 }
634 out.unindent();
635 out << "}\n";
636 return OK;
637}
638
639status_t BitFieldType::emitVtsAttributeType(Formatter &out) const {
640 out << "type: " << getVtsType() << "\n";
641 out << "enum_value: {\n";
642 out.indent();
643 status_t err = mElementType->emitVtsAttributeType(out);
644 if (err != OK) {
645 return err;
646 }
647 out.unindent();
648 out << "}\n";
649 return OK;
650}
651
652void BitFieldType::getAlignmentAndSize(size_t *align, size_t *size) const {
653 resolveToScalarType()->getAlignmentAndSize(align, size);
654}
655
656void BitFieldType::emitReaderWriter(
657 Formatter &out,
658 const std::string &name,
659 const std::string &parcelObj,
660 bool parcelObjIsPointer,
661 bool isReader,
662 ErrorMode mode) const {
663 resolveToScalarType()->emitReaderWriterWithCast(
664 out,
665 name,
666 parcelObj,
667 parcelObjIsPointer,
668 isReader,
669 mode,
670 true /* needsCast */);
671}
672
673void BitFieldType::emitJavaFieldReaderWriter(
674 Formatter &out,
675 size_t depth,
676 const std::string &parcelName,
677 const std::string &blobName,
678 const std::string &fieldName,
679 const std::string &offset,
680 bool isReader) const {
681 return resolveToScalarType()->emitJavaFieldReaderWriter(
682 out, depth, parcelName, blobName, fieldName, offset, isReader);
683}
684
Andreas Huberc9410c72016-07-28 12:18:40 -0700685} // namespace android
686