blob: 0f0542a07acfb558c5c9a0bb539eba20bbbcbed6 [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
Steven Moreland9df52442016-12-12 08:51:14 -080063bool EnumType::isElidableType() const {
64 return mStorageType->isElidableType();
65}
66
Andreas Huber737080b2016-08-02 15:38:04 -070067const ScalarType *EnumType::resolveToScalarType() const {
68 return mStorageType->resolveToScalarType();
69}
70
Steven Moreland30bb6a82016-11-30 09:18:34 -080071std::string EnumType::typeName() const {
72 return "enum " + localName();
73}
74
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070075bool EnumType::isEnum() const {
76 return true;
77}
78
Yifan Hongc6752dc2016-12-20 14:00:14 -080079bool EnumType::canCheckEquality() const {
80 return true;
81}
82
Steven Moreland979e0992016-09-07 09:18:08 -070083std::string EnumType::getCppType(StorageMode,
Steven Moreland979e0992016-09-07 09:18:08 -070084 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -070085 return specifyNamespaces ? fullName() : partialCppName();
Andreas Huber881227d2016-08-02 14:20:21 -070086}
87
Yifan Hong4ed13472016-11-02 10:44:11 -070088std::string EnumType::getJavaType(bool forInitializer) const {
89 return mStorageType->resolveToScalarType()->getJavaType(forInitializer);
Andreas Huber2831d512016-08-15 09:33:47 -070090}
91
92std::string EnumType::getJavaSuffix() const {
93 return mStorageType->resolveToScalarType()->getJavaSuffix();
94}
95
Andreas Hubera3558b32016-09-14 09:12:42 -070096std::string EnumType::getJavaWrapperType() const {
97 return mStorageType->resolveToScalarType()->getJavaWrapperType();
98}
99
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700100std::string EnumType::getVtsType() const {
101 return "TYPE_ENUM";
102}
103
Yifan Hongf24fa852016-09-23 11:03:15 -0700104LocalIdentifier *EnumType::lookupIdentifier(const std::string &name) const {
105 std::vector<const EnumType *> chain;
106 getTypeChain(&chain);
107 for (auto it = chain.begin(); it != chain.end(); ++it) {
108 const auto &type = *it;
109 for(EnumValue *v : type->values()) {
110 if(v->name() == name) {
111 return v;
112 }
113 }
114 }
115 return nullptr;
116}
117
Andreas Huber881227d2016-08-02 14:20:21 -0700118void EnumType::emitReaderWriter(
119 Formatter &out,
120 const std::string &name,
121 const std::string &parcelObj,
122 bool parcelObjIsPointer,
123 bool isReader,
124 ErrorMode mode) const {
Andreas Huber737080b2016-08-02 15:38:04 -0700125 const ScalarType *scalarType = mStorageType->resolveToScalarType();
126 CHECK(scalarType != NULL);
127
128 scalarType->emitReaderWriterWithCast(
129 out,
130 name,
131 parcelObj,
132 parcelObjIsPointer,
133 isReader,
134 mode,
135 true /* needsCast */);
Andreas Huber881227d2016-08-02 14:20:21 -0700136}
137
Andreas Huber85eabdb2016-08-25 11:24:49 -0700138void EnumType::emitJavaFieldReaderWriter(
139 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700140 size_t depth,
Andreas Huber709b62d2016-09-19 11:21:18 -0700141 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700142 const std::string &blobName,
143 const std::string &fieldName,
144 const std::string &offset,
145 bool isReader) const {
146 return mStorageType->emitJavaFieldReaderWriter(
Andreas Huber709b62d2016-09-19 11:21:18 -0700147 out, depth, parcelName, blobName, fieldName, offset, isReader);
Andreas Huber85eabdb2016-08-25 11:24:49 -0700148}
149
Andreas Huber881227d2016-08-02 14:20:21 -0700150status_t EnumType::emitTypeDeclarations(Formatter &out) const {
Andreas Huber737080b2016-08-02 15:38:04 -0700151 const ScalarType *scalarType = mStorageType->resolveToScalarType();
Andreas Hubere3f769a2016-10-10 10:54:44 -0700152 CHECK(scalarType != nullptr);
Andreas Huber737080b2016-08-02 15:38:04 -0700153
Yifan Hong3b320f82016-11-01 15:15:54 -0700154 const std::string storageType = scalarType->getCppStackType();
Andreas Huber881227d2016-08-02 14:20:21 -0700155
156 out << "enum class "
Andreas Huber0e00de42016-08-03 09:56:02 -0700157 << localName()
Andreas Huber881227d2016-08-02 14:20:21 -0700158 << " : "
Andreas Hubere3f769a2016-10-10 10:54:44 -0700159 << storageType
Andreas Huber881227d2016-08-02 14:20:21 -0700160 << " {\n";
161
162 out.indent();
163
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700164 std::vector<const EnumType *> chain;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700165 getTypeChain(&chain);
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700166
167 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
168 const auto &type = *it;
169
170 for (const auto &entry : type->values()) {
171 out << entry->name();
172
Yifan Hongfc610cd2016-09-22 13:34:45 -0700173 std::string value = entry->cppValue(scalarType->getKind());
Yifan Hongf24fa852016-09-23 11:03:15 -0700174 CHECK(!value.empty()); // use autofilled values for c++.
175 out << " = " << value;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700176
Yifan Hong57886972016-08-17 10:42:15 -0700177 out << ",";
178
Yifan Hongfc610cd2016-09-22 13:34:45 -0700179 std::string comment = entry->comment();
180 if (!comment.empty() && comment != value) {
Yifan Hong57886972016-08-17 10:42:15 -0700181 out << " // " << comment;
182 }
183
184 out << "\n";
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700185 }
Andreas Huber881227d2016-08-02 14:20:21 -0700186 }
187
188 out.unindent();
189 out << "};\n\n";
190
191 return OK;
192}
193
Jayant Chowdhary2820f8a2016-11-10 12:29:09 -0800194void EnumType::emitEnumBitwiseOperator(
195 Formatter &out,
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800196 bool lhsIsEnum,
197 bool rhsIsEnum,
Jayant Chowdhary2820f8a2016-11-10 12:29:09 -0800198 const std::string &op) const {
Andreas Hubere3f769a2016-10-10 10:54:44 -0700199 const ScalarType *scalarType = mStorageType->resolveToScalarType();
200 CHECK(scalarType != nullptr);
201
Yifan Hong3b320f82016-11-01 15:15:54 -0700202 const std::string storageType = scalarType->getCppStackType();
Andreas Hubere3f769a2016-10-10 10:54:44 -0700203
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800204 out << "constexpr "
205 << storageType
Jayant Chowdhary2820f8a2016-11-10 12:29:09 -0800206 << " operator"
207 << op
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800208 << "(const "
209 << (lhsIsEnum ? fullName() : storageType)
210 << " lhs, const "
211 << (rhsIsEnum ? fullName() : storageType)
Andreas Hubere3f769a2016-10-10 10:54:44 -0700212 << " rhs) {\n";
Andreas Hubere3f769a2016-10-10 10:54:44 -0700213
Yifan Hong33223ca2016-12-13 15:07:35 -0800214 out.indent([&] {
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800215 out << "return static_cast<"
216 << storageType
217 << ">(";
Andreas Hubere3f769a2016-10-10 10:54:44 -0700218
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800219 if (lhsIsEnum) {
220 out << "static_cast<"
221 << storageType
222 << ">(lhs)";
223 } else {
224 out << "lhs";
225 }
226 out << " " << op << " ";
227 if (rhsIsEnum) {
228 out << "static_cast<"
229 << storageType
230 << ">(rhs)";
231 } else {
232 out << "rhs";
233 }
234 out << ");\n";
235 });
Andreas Hubere3f769a2016-10-10 10:54:44 -0700236
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800237 out << "}\n\n";
238}
239
240void EnumType::emitBitFieldBitwiseAssignmentOperator(
241 Formatter &out,
242 const std::string &op) const {
243 const ScalarType *scalarType = mStorageType->resolveToScalarType();
244 CHECK(scalarType != nullptr);
245
246 const std::string storageType = scalarType->getCppStackType();
247
248 out << "constexpr " << storageType << " &operator" << op << "=("
249 << storageType << "& v, const " << fullName() << " e) {\n";
250
Yifan Hong33223ca2016-12-13 15:07:35 -0800251 out.indent([&] {
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800252 out << "v " << op << "= static_cast<" << storageType << ">(e);\n";
253 out << "return v;\n";
254 });
Andreas Hubere3f769a2016-10-10 10:54:44 -0700255
256 out << "}\n\n";
257}
258
259status_t EnumType::emitGlobalTypeDeclarations(Formatter &out) const {
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800260 emitEnumBitwiseOperator(out, true /* lhsIsEnum */, true /* rhsIsEnum */, "|");
261 emitEnumBitwiseOperator(out, false /* lhsIsEnum */, true /* rhsIsEnum */, "|");
262 emitEnumBitwiseOperator(out, true /* lhsIsEnum */, false /* rhsIsEnum */, "|");
263 emitEnumBitwiseOperator(out, true /* lhsIsEnum */, true /* rhsIsEnum */, "&");
264 emitEnumBitwiseOperator(out, false /* lhsIsEnum */, true /* rhsIsEnum */, "&");
265 emitEnumBitwiseOperator(out, true /* lhsIsEnum */, false /* rhsIsEnum */, "&");
266
267 emitBitFieldBitwiseAssignmentOperator(out, "|");
268 emitBitFieldBitwiseAssignmentOperator(out, "&");
Andreas Hubere3f769a2016-10-10 10:54:44 -0700269
270 return OK;
271}
272
Andreas Huber85eabdb2016-08-25 11:24:49 -0700273status_t EnumType::emitJavaTypeDeclarations(Formatter &out, bool) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700274 const ScalarType *scalarType = mStorageType->resolveToScalarType();
275 CHECK(scalarType != NULL);
276
277 out << "public final class "
278 << localName()
279 << " {\n";
280
281 out.indent();
282
Andreas Huber4c865b72016-09-14 15:26:27 -0700283 const std::string typeName =
Yifan Hong4ed13472016-11-02 10:44:11 -0700284 scalarType->getJavaType(false /* forInitializer */);
Andreas Huber2831d512016-08-15 09:33:47 -0700285
286 std::vector<const EnumType *> chain;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700287 getTypeChain(&chain);
Andreas Huber2831d512016-08-15 09:33:47 -0700288
289 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
290 const auto &type = *it;
291
292 for (const auto &entry : type->values()) {
293 out << "public static final "
294 << typeName
295 << " "
Andreas Huberab647c02016-09-14 09:44:00 -0700296 << entry->name()
297 << " = ";
Andreas Huber2831d512016-08-15 09:33:47 -0700298
Yifan Hongf24fa852016-09-23 11:03:15 -0700299 // javaValue will make the number signed.
Yifan Hongfc610cd2016-09-22 13:34:45 -0700300 std::string value = entry->javaValue(scalarType->getKind());
Yifan Hongf24fa852016-09-23 11:03:15 -0700301 CHECK(!value.empty()); // use autofilled values for java.
302 out << value;
Andreas Huber2831d512016-08-15 09:33:47 -0700303
Yifan Hong19ca75a2016-08-31 10:20:03 -0700304 out << ";";
305
Yifan Hongfc610cd2016-09-22 13:34:45 -0700306 std::string comment = entry->comment();
307 if (!comment.empty() && comment != value) {
Yifan Hong19ca75a2016-08-31 10:20:03 -0700308 out << " // " << comment;
309 }
310
311 out << "\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700312 }
313 }
314
315 out.unindent();
316 out << "};\n\n";
317
318 return OK;
319}
320
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700321status_t EnumType::emitVtsTypeDeclarations(Formatter &out) const {
Yifan Hongc07b2022016-11-08 12:44:24 -0800322 const ScalarType *scalarType = mStorageType->resolveToScalarType();
323
Zhuoyao Zhangbf828c82016-10-26 14:15:10 -0700324 out << "name: \"" << fullName() << "\"\n";
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700325 out << "type: " << getVtsType() << "\n";
326 out << "enum_value: {\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700327 out.indent();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700328
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700329 out << "scalar_type: \""
Yifan Hongc07b2022016-11-08 12:44:24 -0800330 << scalarType->getVtsScalarType()
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700331 << "\"\n\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700332 std::vector<const EnumType *> chain;
333 getTypeChain(&chain);
334
335 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
336 const auto &type = *it;
337
338 for (const auto &entry : type->values()) {
339 out << "enumerator: \"" << entry->name() << "\"\n";
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700340 out << "scalar_value: {\n";
341 out.indent();
Yifan Hongc07b2022016-11-08 12:44:24 -0800342 // use autofilled values for vts.
343 std::string value = entry->value(scalarType->getKind());
344 CHECK(!value.empty());
345 out << mStorageType->resolveToScalarType()->getVtsScalarType()
346 << ": "
347 << value
348 << "\n";
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700349 out.unindent();
350 out << "}\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700351 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700352 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700353
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700354 out.unindent();
355 out << "}\n";
356 return OK;
357}
358
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700359status_t EnumType::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700360 out << "type: " << getVtsType() << "\n";
Zhuoyao Zhangbf828c82016-10-26 14:15:10 -0700361 out << "predefined_type: \"" << fullName() << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700362 return OK;
363}
364
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700365void EnumType::getTypeChain(std::vector<const EnumType *> *out) const {
366 out->clear();
367 const EnumType *type = this;
368 for (;;) {
369 out->push_back(type);
370
371 const Type *superType = type->storageType();
372 if (superType == NULL || !superType->isEnum()) {
373 break;
374 }
375
376 type = static_cast<const EnumType *>(superType);
377 }
378}
379
Andreas Huber85eabdb2016-08-25 11:24:49 -0700380void EnumType::getAlignmentAndSize(size_t *align, size_t *size) const {
381 mStorageType->getAlignmentAndSize(align, size);
382}
383
Andreas Huber019d21d2016-10-03 12:59:47 -0700384const Annotation *EnumType::findExportAnnotation() const {
385 for (const auto &annotation : annotations()) {
386 if (annotation->name() == "export") {
387 return annotation;
388 }
389 }
390
391 return nullptr;
392}
393
394void EnumType::appendToExportedTypesVector(
395 std::vector<const Type *> *exportedTypes) const {
396 if (findExportAnnotation() != nullptr) {
397 exportedTypes->push_back(this);
398 }
399}
400
Andreas Huber1c507272016-10-05 14:33:21 -0700401status_t EnumType::emitExportedHeader(Formatter &out, bool forJava) const {
Andreas Huber019d21d2016-10-03 12:59:47 -0700402 const Annotation *annotation = findExportAnnotation();
403 CHECK(annotation != nullptr);
404
405 std::string name = localName();
406
407 const AnnotationParam *nameParam = annotation->getParam("name");
408 if (nameParam != nullptr) {
409 CHECK_EQ(nameParam->getValues()->size(), 1u);
410
411 std::string quotedString = nameParam->getValues()->at(0);
412 name = quotedString.substr(1, quotedString.size() - 2);
413 }
414
Andreas Huberb0627fb2016-10-10 09:39:28 -0700415 std::string valuePrefix;
Andreas Huberb0627fb2016-10-10 09:39:28 -0700416 const AnnotationParam *prefixParam = annotation->getParam("value_prefix");
417 if (prefixParam != nullptr) {
418 CHECK_EQ(prefixParam->getValues()->size(), 1u);
419
420 std::string quotedString = prefixParam->getValues()->at(0);
421 valuePrefix = quotedString.substr(1, quotedString.size() - 2);
422 }
423
Steven Moreland73cdc882016-11-21 16:43:50 -0800424 std::string valueSuffix;
425 const AnnotationParam *suffixParam = annotation->getParam("value_suffix");
426 if (suffixParam != nullptr) {
427 CHECK_EQ(suffixParam->getValues()->size(), 1u);
428
429 std::string quotedString = suffixParam->getValues()->at(0);
430 valueSuffix = quotedString.substr(1, quotedString.size() - 2);
431 }
432
Andreas Huber019d21d2016-10-03 12:59:47 -0700433 const ScalarType *scalarType = mStorageType->resolveToScalarType();
Andreas Huber1c507272016-10-05 14:33:21 -0700434 CHECK(scalarType != nullptr);
Andreas Huber019d21d2016-10-03 12:59:47 -0700435
Andreas Huber1c507272016-10-05 14:33:21 -0700436 if (forJava) {
437 if (!name.empty()) {
438 out << "public final class "
439 << name
440 << " {\n";
441
442 out.indent();
443 } else {
444 out << "// Values declared in " << localName() << " follow.\n";
445 }
446
Andreas Huber1c507272016-10-05 14:33:21 -0700447 const std::string typeName =
Yifan Hong4ed13472016-11-02 10:44:11 -0700448 scalarType->getJavaType(false /* forInitializer */);
Andreas Huber1c507272016-10-05 14:33:21 -0700449
450 std::vector<const EnumType *> chain;
451 getTypeChain(&chain);
452
453 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
454 const auto &type = *it;
455
456 for (const auto &entry : type->values()) {
457 out << "public static final "
458 << typeName
459 << " "
460 << valuePrefix
461 << entry->name()
Steven Moreland73cdc882016-11-21 16:43:50 -0800462 << valueSuffix
Andreas Huber1c507272016-10-05 14:33:21 -0700463 << " = ";
464
465 // javaValue will make the number signed.
466 std::string value = entry->javaValue(scalarType->getKind());
467 CHECK(!value.empty()); // use autofilled values for java.
468 out << value;
469
470 out << ";";
471
472 std::string comment = entry->comment();
473 if (!comment.empty() && comment != value) {
474 out << " // " << comment;
475 }
476
477 out << "\n";
478 }
479 }
480
481 if (!name.empty()) {
482 out.unindent();
483 out << "};\n";
484 }
485 out << "\n";
486
487 return OK;
488 }
Andreas Huber019d21d2016-10-03 12:59:47 -0700489
490 if (!name.empty()) {
491 out << "typedef ";
492 }
493
494 out << "enum {\n";
495
496 out.indent();
497
498 std::vector<const EnumType *> chain;
499 getTypeChain(&chain);
500
501 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
502 const auto &type = *it;
503
504 for (const auto &entry : type->values()) {
Steven Moreland73cdc882016-11-21 16:43:50 -0800505 out << valuePrefix << entry->name() << valueSuffix;
Andreas Huber019d21d2016-10-03 12:59:47 -0700506
507 std::string value = entry->cppValue(scalarType->getKind());
508 CHECK(!value.empty()); // use autofilled values for c++.
509 out << " = " << value;
510
511 out << ",";
512
513 std::string comment = entry->comment();
514 if (!comment.empty() && comment != value) {
515 out << " // " << comment;
516 }
517
518 out << "\n";
519 }
520 }
521
522 out.unindent();
523 out << "}";
524
525 if (!name.empty()) {
526 out << " " << name;
527 }
528
529 out << ";\n\n";
530
531 return OK;
532}
533
Andreas Huber31629bc2016-08-03 09:06:40 -0700534////////////////////////////////////////////////////////////////////////////////
535
Yifan Hongf24fa852016-09-23 11:03:15 -0700536EnumValue::EnumValue(const char *name, ConstantExpression *value)
Andreas Huber31629bc2016-08-03 09:06:40 -0700537 : mName(name),
Yifan Hongf24fa852016-09-23 11:03:15 -0700538 mValue(value),
539 mIsAutoFill(false) {
Andreas Huber31629bc2016-08-03 09:06:40 -0700540}
541
542std::string EnumValue::name() const {
543 return mName;
544}
545
Yifan Hongc07b2022016-11-08 12:44:24 -0800546std::string EnumValue::value(ScalarType::Kind castKind) const {
Yifan Hongf24fa852016-09-23 11:03:15 -0700547 CHECK(mValue != nullptr);
Yifan Hongc07b2022016-11-08 12:44:24 -0800548 return mValue->value(castKind);
Yifan Hong57886972016-08-17 10:42:15 -0700549}
550
Yifan Hongfc610cd2016-09-22 13:34:45 -0700551std::string EnumValue::cppValue(ScalarType::Kind castKind) const {
Yifan Hongf24fa852016-09-23 11:03:15 -0700552 CHECK(mValue != nullptr);
553 return mValue->cppValue(castKind);
Yifan Hong57886972016-08-17 10:42:15 -0700554}
Yifan Hongfc610cd2016-09-22 13:34:45 -0700555std::string EnumValue::javaValue(ScalarType::Kind castKind) const {
Yifan Hongf24fa852016-09-23 11:03:15 -0700556 CHECK(mValue != nullptr);
557 return mValue->javaValue(castKind);
Yifan Hong19ca75a2016-08-31 10:20:03 -0700558}
Yifan Hong57886972016-08-17 10:42:15 -0700559
Yifan Hongfc610cd2016-09-22 13:34:45 -0700560std::string EnumValue::comment() const {
Yifan Hongf24fa852016-09-23 11:03:15 -0700561 CHECK(mValue != nullptr);
562 return mValue->description();
563}
564
565ConstantExpression *EnumValue::constExpr() const {
566 CHECK(mValue != nullptr);
567 return mValue;
568}
569
570void EnumValue::autofill(const EnumValue *prev, const ScalarType *type) {
571 if(mValue != nullptr)
572 return;
573 mIsAutoFill = true;
574 ConstantExpression *value = new ConstantExpression();
575 if(prev == nullptr) {
576 *value = ConstantExpression::Zero(type->getKind());
577 } else {
578 CHECK(prev->mValue != nullptr);
579 *value = prev->mValue->addOne();
580 }
581 mValue = value;
582}
583
584bool EnumValue::isAutoFill() const {
585 return mIsAutoFill;
586}
587
588bool EnumValue::isEnumValue() const {
589 return true;
Andreas Huber31629bc2016-08-03 09:06:40 -0700590}
591
Yifan Hongabf73ee2016-12-05 18:47:00 -0800592////////////////////////////////////////////////////////////////////////////////
593
594bool BitFieldType::isBitField() const {
595 return true;
596}
597
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800598std::string BitFieldType::typeName() const {
599 return "mask" + (mElementType == nullptr ? "" : (" of " + mElementType->typeName()));
600}
601
602void BitFieldType::addNamedTypesToSet(std::set<const FQName> &) const {
603}
604
605bool BitFieldType::isCompatibleElementType(Type *elementType) const {
606 return elementType->isEnum();
607}
608
609const ScalarType *BitFieldType::resolveToScalarType() const {
610 return mElementType->resolveToScalarType();
611}
612
613std::string BitFieldType::getCppType(StorageMode mode,
614 bool specifyNamespaces) const {
615 return resolveToScalarType()->getCppType(mode, specifyNamespaces);
616}
617
618std::string BitFieldType::getJavaType(bool forInitializer) const {
619 return resolveToScalarType()->getJavaType(forInitializer);
620}
621
622std::string BitFieldType::getJavaSuffix() const {
623 return resolveToScalarType()->getJavaSuffix();
624}
625
626std::string BitFieldType::getJavaWrapperType() const {
627 return resolveToScalarType()->getJavaWrapperType();
628}
629
630std::string BitFieldType::getVtsType() const {
631 return "TYPE_MASK";
632}
633
Yifan Hong8c56cbe2016-12-12 15:30:12 -0800634bool BitFieldType::isElidableType() const {
635 return resolveToScalarType()->isElidableType();
636}
637
Yifan Hongc57c8bb2016-12-01 11:37:18 -0800638status_t BitFieldType::emitVtsTypeDeclarations(Formatter &out) const {
639 out << "type: " << getVtsType() << "\n";
640 out << "enum_value: {\n";
641 out.indent();
642 status_t err = mElementType->emitVtsTypeDeclarations(out);
643 if (err != OK) {
644 return err;
645 }
646 out.unindent();
647 out << "}\n";
648 return OK;
649}
650
651status_t BitFieldType::emitVtsAttributeType(Formatter &out) const {
652 out << "type: " << getVtsType() << "\n";
653 out << "enum_value: {\n";
654 out.indent();
655 status_t err = mElementType->emitVtsAttributeType(out);
656 if (err != OK) {
657 return err;
658 }
659 out.unindent();
660 out << "}\n";
661 return OK;
662}
663
664void BitFieldType::getAlignmentAndSize(size_t *align, size_t *size) const {
665 resolveToScalarType()->getAlignmentAndSize(align, size);
666}
667
668void BitFieldType::emitReaderWriter(
669 Formatter &out,
670 const std::string &name,
671 const std::string &parcelObj,
672 bool parcelObjIsPointer,
673 bool isReader,
674 ErrorMode mode) const {
675 resolveToScalarType()->emitReaderWriterWithCast(
676 out,
677 name,
678 parcelObj,
679 parcelObjIsPointer,
680 isReader,
681 mode,
682 true /* needsCast */);
683}
684
685void BitFieldType::emitJavaFieldReaderWriter(
686 Formatter &out,
687 size_t depth,
688 const std::string &parcelName,
689 const std::string &blobName,
690 const std::string &fieldName,
691 const std::string &offset,
692 bool isReader) const {
693 return resolveToScalarType()->emitJavaFieldReaderWriter(
694 out, depth, parcelName, blobName, fieldName, offset, isReader);
695}
696
Andreas Huberc9410c72016-07-28 12:18:40 -0700697} // namespace android
698