blob: 4211958e06b9ab7a7eda96ae198befb34f2e0e6a [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 "CompoundType.h"
18
Yifan Hongec102272016-12-20 18:10:07 -080019#include "ArrayType.h"
Andreas Huberf630bc82016-09-09 14:52:25 -070020#include "VectorType.h"
Timur Iskhakovcec46c42017-08-09 00:22:02 -070021
Andreas Huber5a545442016-08-03 10:44:56 -070022#include <android-base/logging.h>
Timur Iskhakovcec46c42017-08-09 00:22:02 -070023#include <hidl-util/Formatter.h>
Timur Iskhakove8ee6a02017-09-06 11:42:10 -070024#include <iostream>
Timur Iskhakovcec46c42017-08-09 00:22:02 -070025#include <unordered_set>
Andreas Huber5a545442016-08-03 10:44:56 -070026
Andreas Huberc9410c72016-07-28 12:18:40 -070027namespace android {
28
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070029CompoundType::CompoundType(Style style, const char* localName, const Location& location,
30 Scope* parent)
31 : Scope(localName, location, parent), mStyle(style), mFields(NULL) {}
Andreas Huberc9410c72016-07-28 12:18:40 -070032
Yifan Hong27e85db2016-11-09 15:45:52 -080033CompoundType::Style CompoundType::style() const {
34 return mStyle;
35}
36
Timur Iskhakovcec46c42017-08-09 00:22:02 -070037void CompoundType::setFields(std::vector<NamedReference<Type>*>* fields) {
Andreas Huberc9410c72016-07-28 12:18:40 -070038 mFields = fields;
Timur Iskhakovcec46c42017-08-09 00:22:02 -070039}
Andreas Huber5a545442016-08-03 10:44:56 -070040
Timur Iskhakovb58f4182017-08-29 15:19:24 -070041std::vector<const Reference<Type>*> CompoundType::getReferences() const {
42 std::vector<const Reference<Type>*> ret;
43 ret.insert(ret.begin(), mFields->begin(), mFields->end());
Timur Iskhakov33431e62017-08-21 17:31:23 -070044 return ret;
Timur Iskhakovcec46c42017-08-09 00:22:02 -070045}
46
47status_t CompoundType::validate() const {
48 for (const auto* field : *mFields) {
Timur Iskhakovcec46c42017-08-09 00:22:02 -070049 const Type& type = field->type();
Andreas Huber5a545442016-08-03 10:44:56 -070050
Andreas Huber86a112b2016-10-19 14:25:16 -070051 if (type.isBinder()
52 || (type.isVector()
53 && static_cast<const VectorType *>(
54 &type)->isVectorOfBinders())) {
Timur Iskhakovcec46c42017-08-09 00:22:02 -070055 std::cerr << "ERROR: Struct/Union must not contain references to interfaces at "
56 << field->location() << "\n";
57 return UNKNOWN_ERROR;
Andreas Huberb95ea8a2016-08-15 15:35:42 -070058 }
59
Andreas Huber5a545442016-08-03 10:44:56 -070060 if (mStyle == STYLE_UNION) {
61 if (type.needsEmbeddedReadWrite()) {
Timur Iskhakovcec46c42017-08-09 00:22:02 -070062 std::cerr << "ERROR: Union must not contain any types that need fixup at "
63 << field->location() << "\n";
64 return UNKNOWN_ERROR;
Andreas Huber5a545442016-08-03 10:44:56 -070065 }
Andreas Huber5a545442016-08-03 10:44:56 -070066 }
67 }
68
Timur Iskhakovcec46c42017-08-09 00:22:02 -070069 status_t err = validateUniqueNames();
70 if (err != OK) return err;
71
72 return Scope::validate();
73}
74
75status_t CompoundType::validateUniqueNames() const {
76 std::unordered_set<std::string> names;
77
78 for (const auto* field : *mFields) {
79 if (names.find(field->name()) != names.end()) {
80 std::cerr << "ERROR: Redefinition of field '" << field->name() << "' at "
81 << field->location() << "\n";
82 return UNKNOWN_ERROR;
83 }
84 names.insert(field->name());
85 }
86
87 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -070088}
89
Andreas Huberf630bc82016-09-09 14:52:25 -070090bool CompoundType::isCompoundType() const {
91 return true;
92}
93
Yifan Hongc6752dc2016-12-20 14:00:14 -080094bool CompoundType::canCheckEquality() const {
95 if (mStyle == STYLE_UNION) {
96 return false;
97 }
98 for (const auto &field : *mFields) {
99 if (!field->type().canCheckEquality()) {
100 return false;
101 }
102 }
103 return true;
104}
105
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700106std::string CompoundType::typeName() const {
107 switch (mStyle) {
108 case STYLE_STRUCT: {
109 return "struct " + localName();
110 }
111 case STYLE_UNION: {
112 return "union " + localName();
113 }
114 }
115 CHECK(!"Should not be here");
116}
117
Andreas Huber881227d2016-08-02 14:20:21 -0700118std::string CompoundType::getCppType(
Steven Moreland979e0992016-09-07 09:18:08 -0700119 StorageMode mode,
Steven Morelande30ee9b2017-05-09 13:31:01 -0700120 bool /* specifyNamespaces */) const {
121 const std::string base = fullName();
Andreas Huber881227d2016-08-02 14:20:21 -0700122
123 switch (mode) {
124 case StorageMode_Stack:
125 return base;
126
127 case StorageMode_Argument:
128 return "const " + base + "&";
129
130 case StorageMode_Result:
131 return "const " + base + "*";
132 }
133}
134
Yifan Hong4ed13472016-11-02 10:44:11 -0700135std::string CompoundType::getJavaType(bool /* forInitializer */) const {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700136 return fullJavaName();
137}
138
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700139std::string CompoundType::getVtsType() const {
140 switch (mStyle) {
141 case STYLE_STRUCT:
142 {
143 return "TYPE_STRUCT";
144 }
145 case STYLE_UNION:
146 {
147 return "TYPE_UNION";
148 }
149 }
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700150 CHECK(!"Should not be here");
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700151}
152
Andreas Huber881227d2016-08-02 14:20:21 -0700153void CompoundType::emitReaderWriter(
154 Formatter &out,
155 const std::string &name,
156 const std::string &parcelObj,
157 bool parcelObjIsPointer,
158 bool isReader,
159 ErrorMode mode) const {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700160 const std::string parentName = "_hidl_" + name + "_parent";
Andreas Huber881227d2016-08-02 14:20:21 -0700161
162 out << "size_t " << parentName << ";\n\n";
163
164 const std::string parcelObjDeref =
165 parcelObj + (parcelObjIsPointer ? "->" : ".");
166
167 if (isReader) {
Martijn Coenen6a082c62017-01-11 12:47:02 +0100168 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700169 << parcelObjDeref
170 << "readBuffer("
Martijn Coenenb2a861c2017-04-18 15:54:25 -0700171 << "sizeof(*"
172 << name
173 << "), &"
Andreas Huber881227d2016-08-02 14:20:21 -0700174 << parentName
Martijn Coenen6a082c62017-01-11 12:47:02 +0100175 << ", "
176 << " reinterpret_cast<const void **>("
177 << "&" << name
178 << "));\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700179
Martijn Coenen6a082c62017-01-11 12:47:02 +0100180 handleError(out, mode);
Andreas Huber881227d2016-08-02 14:20:21 -0700181 } else {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700182 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700183 << parcelObjDeref
184 << "writeBuffer(&"
185 << name
186 << ", sizeof("
187 << name
188 << "), &"
189 << parentName
190 << ");\n";
191
192 handleError(out, mode);
193 }
194
195 if (mStyle != STYLE_STRUCT || !needsEmbeddedReadWrite()) {
196 return;
197 }
198
199 emitReaderWriterEmbedded(
200 out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700201 0 /* depth */,
Andreas Huber881227d2016-08-02 14:20:21 -0700202 name,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700203 name, /* sanitizedName */
Andreas Huber881227d2016-08-02 14:20:21 -0700204 isReader /* nameIsPointer */,
205 parcelObj,
206 parcelObjIsPointer,
207 isReader,
208 mode,
209 parentName,
210 "0 /* parentOffset */");
211}
212
213void CompoundType::emitReaderWriterEmbedded(
214 Formatter &out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700215 size_t /* depth */,
Andreas Huber881227d2016-08-02 14:20:21 -0700216 const std::string &name,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700217 const std::string & /*sanitizedName */,
Andreas Huber881227d2016-08-02 14:20:21 -0700218 bool nameIsPointer,
219 const std::string &parcelObj,
220 bool parcelObjIsPointer,
221 bool isReader,
222 ErrorMode mode,
223 const std::string &parentName,
224 const std::string &offsetText) const {
225 emitReaderWriterEmbeddedForTypeName(
226 out,
227 name,
228 nameIsPointer,
229 parcelObj,
230 parcelObjIsPointer,
231 isReader,
232 mode,
233 parentName,
234 offsetText,
Andreas Huber0e00de42016-08-03 09:56:02 -0700235 fullName(),
Yifan Hong244e82d2016-11-11 11:13:57 -0800236 "" /* childName */,
237 "" /* namespace */);
Andreas Huber881227d2016-08-02 14:20:21 -0700238}
239
Andreas Huber85eabdb2016-08-25 11:24:49 -0700240void CompoundType::emitJavaReaderWriter(
241 Formatter &out,
242 const std::string &parcelObj,
243 const std::string &argName,
244 bool isReader) const {
245 if (isReader) {
246 out << "new " << fullJavaName() << "();\n";
247 }
248
249 out << argName
250 << "."
251 << (isReader ? "readFromParcel" : "writeToParcel")
252 << "("
253 << parcelObj
254 << ");\n";
255}
256
257void CompoundType::emitJavaFieldInitializer(
258 Formatter &out, const std::string &fieldName) const {
259 out << "final "
260 << fullJavaName()
261 << " "
262 << fieldName
263 << " = new "
264 << fullJavaName()
265 << "();\n";
266}
267
268void CompoundType::emitJavaFieldReaderWriter(
269 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700270 size_t /* depth */,
Andreas Huber709b62d2016-09-19 11:21:18 -0700271 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700272 const std::string &blobName,
273 const std::string &fieldName,
274 const std::string &offset,
275 bool isReader) const {
276 if (isReader) {
277 out << fieldName
Andreas Huber709b62d2016-09-19 11:21:18 -0700278 << ".readEmbeddedFromParcel("
279 << parcelName
280 << ", "
Andreas Huber85eabdb2016-08-25 11:24:49 -0700281 << blobName
282 << ", "
283 << offset
284 << ");\n";
285
286 return;
287 }
288
289 out << fieldName
290 << ".writeEmbeddedToBlob("
291 << blobName
292 << ", "
293 << offset
294 << ");\n";
295}
Yifan Hongbf459bc2016-08-23 16:50:37 -0700296void CompoundType::emitResolveReferences(
297 Formatter &out,
298 const std::string &name,
299 bool nameIsPointer,
300 const std::string &parcelObj,
301 bool parcelObjIsPointer,
302 bool isReader,
303 ErrorMode mode) const {
304 emitResolveReferencesEmbedded(
305 out,
306 0 /* depth */,
307 name,
308 name /* sanitizedName */,
309 nameIsPointer,
310 parcelObj,
311 parcelObjIsPointer,
312 isReader,
313 mode,
314 "_hidl_" + name + "_parent",
315 "0 /* parentOffset */");
316}
317
318void CompoundType::emitResolveReferencesEmbedded(
319 Formatter &out,
320 size_t /* depth */,
321 const std::string &name,
322 const std::string &/* sanitizedName */,
323 bool nameIsPointer,
324 const std::string &parcelObj,
325 bool parcelObjIsPointer,
326 bool isReader,
327 ErrorMode mode,
328 const std::string &parentName,
329 const std::string &offsetText) const {
330 CHECK(needsResolveReferences());
331
332 const std::string parcelObjDeref =
333 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
334
335 const std::string parcelObjPointer =
336 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
337
Yifan Hong244e82d2016-11-11 11:13:57 -0800338 const std::string nameDerefed = nameIsPointer ? ("*" + name) : name;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700339 const std::string namePointer = nameIsPointer ? name : ("&" + name);
340
341 out << "_hidl_err = ";
342
343 if (isReader) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800344 out << "readEmbeddedReferenceFromParcel(\n";
Yifan Hongbf459bc2016-08-23 16:50:37 -0700345 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800346 out << "writeEmbeddedReferenceToParcel(\n";
Yifan Hongbf459bc2016-08-23 16:50:37 -0700347 }
348
Yifan Hong33223ca2016-12-13 15:07:35 -0800349 out.indent(2, [&]{
Yifan Hong244e82d2016-11-11 11:13:57 -0800350 if (isReader) {
351 out << "const_cast<"
352 << fullName()
353 << " *"
354 << ">("
355 << namePointer
356 << "),\n"
357 << parcelObjDeref;
358 } else {
359 out << nameDerefed
360 << ",\n"
361 << parcelObjPointer;
362 }
363
364 out << ",\n"
Yifan Hong0a68a282016-10-21 16:32:34 -0700365 << parentName
366 << ",\n"
367 << offsetText
368 << ");\n\n";
369 });
Yifan Hongbf459bc2016-08-23 16:50:37 -0700370
371 handleError(out, mode);
372}
Andreas Huber85eabdb2016-08-25 11:24:49 -0700373
Andreas Huber881227d2016-08-02 14:20:21 -0700374status_t CompoundType::emitTypeDeclarations(Formatter &out) const {
375 out << ((mStyle == STYLE_STRUCT) ? "struct" : "union")
376 << " "
Andreas Huber0e00de42016-08-03 09:56:02 -0700377 << localName()
Yifan Hongb1d2ebc2016-12-20 17:18:07 -0800378 << " final {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700379
380 out.indent();
381
382 Scope::emitTypeDeclarations(out);
383
Andreas Huber60d3b222017-03-30 09:10:56 -0700384 if (containsPointer()) {
Andreas Huberca4bc892017-01-09 14:58:12 -0800385 for (const auto &field : *mFields) {
386 out << field->type().getCppStackType()
387 << " "
388 << field->name()
389 << ";\n";
390 }
391
392 out.unindent();
393 out << "};\n\n";
394
395 return OK;
Andreas Huber881227d2016-08-02 14:20:21 -0700396 }
397
Andreas Huberca4bc892017-01-09 14:58:12 -0800398 for (int pass = 0; pass < 2; ++pass) {
399 size_t offset = 0;
400 for (const auto &field : *mFields) {
401 size_t fieldAlign, fieldSize;
402 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
403
404 size_t pad = offset % fieldAlign;
405 if (pad > 0) {
406 offset += fieldAlign - pad;
407 }
408
409 if (pass == 0) {
410 out << field->type().getCppStackType()
411 << " "
412 << field->name()
413 << " __attribute__ ((aligned("
414 << fieldAlign
415 << ")));\n";
416 } else {
417 out << "static_assert(offsetof("
418 << fullName()
419 << ", "
420 << field->name()
421 << ") == "
422 << offset
423 << ", \"wrong offset\");\n";
424 }
425
Andreas Huber60d3b222017-03-30 09:10:56 -0700426 if (mStyle == STYLE_STRUCT) {
427 offset += fieldSize;
428 }
Andreas Huberca4bc892017-01-09 14:58:12 -0800429 }
430
431 if (pass == 0) {
432 out.unindent();
433 out << "};\n\n";
434 }
435 }
436
437 size_t structAlign, structSize;
438 getAlignmentAndSize(&structAlign, &structSize);
439
440 out << "static_assert(sizeof("
441 << fullName()
442 << ") == "
443 << structSize
444 << ", \"wrong size\");\n";
445
446 out << "static_assert(__alignof("
447 << fullName()
448 << ") == "
449 << structAlign
450 << ", \"wrong alignment\");\n\n";
Yifan Hong244e82d2016-11-11 11:13:57 -0800451
452 return OK;
453}
454
Timur Iskhakovfd3f2502017-09-05 16:25:02 -0700455void CompoundType::emitTypeForwardDeclaration(Formatter& out) const {
456 out << ((mStyle == STYLE_STRUCT) ? "struct" : "union") << " " << localName() << ";\n";
457}
Yifan Hong244e82d2016-11-11 11:13:57 -0800458
Yifan Hongc6752dc2016-12-20 14:00:14 -0800459status_t CompoundType::emitGlobalTypeDeclarations(Formatter &out) const {
460 Scope::emitGlobalTypeDeclarations(out);
461
Steven Moreland3d98bc42017-06-23 21:36:41 +0000462 out << "std::string toString("
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800463 << getCppArgumentType()
Steven Moreland3d98bc42017-06-23 21:36:41 +0000464 << ");\n\n";
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800465
Yifan Hong42c68432017-03-13 16:22:03 -0700466 if (canCheckEquality()) {
Steven Morelandf91048a2017-06-23 21:38:35 +0000467 out << "bool operator==("
468 << getCppArgumentType() << ", " << getCppArgumentType() << ");\n\n";
Yifan Hong42c68432017-03-13 16:22:03 -0700469
Steven Morelandf91048a2017-06-23 21:38:35 +0000470 out << "bool operator!=("
471 << getCppArgumentType() << ", " << getCppArgumentType() << ");\n\n";
Yifan Hong42c68432017-03-13 16:22:03 -0700472 } else {
Steven Morelandf91048a2017-06-23 21:38:35 +0000473 out << "// operator== and operator!= are not generated for " << localName() << "\n\n";
Yifan Hongc6752dc2016-12-20 14:00:14 -0800474 }
475
Yifan Hongc6752dc2016-12-20 14:00:14 -0800476 return OK;
477}
478
Yifan Hong244e82d2016-11-11 11:13:57 -0800479status_t CompoundType::emitGlobalHwDeclarations(Formatter &out) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700480 if (needsEmbeddedReadWrite()) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800481 out << "::android::status_t readEmbeddedFromParcel(\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700482
Yifan Hong0a68a282016-10-21 16:32:34 -0700483 out.indent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700484
Martijn Coenenb2a861c2017-04-18 15:54:25 -0700485 out << "const " << fullName() << " &obj,\n"
Yifan Hong244e82d2016-11-11 11:13:57 -0800486 << "const ::android::hardware::Parcel &parcel,\n"
487 << "size_t parentHandle,\n"
488 << "size_t parentOffset);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700489
Yifan Hong0a68a282016-10-21 16:32:34 -0700490 out.unindent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700491
492 out << "::android::status_t writeEmbeddedToParcel(\n";
493
Yifan Hong0a68a282016-10-21 16:32:34 -0700494 out.indent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700495
Yifan Hong244e82d2016-11-11 11:13:57 -0800496 out << "const " << fullName() << " &obj,\n"
497 << "::android::hardware::Parcel *parcel,\n"
498 << "size_t parentHandle,\n"
499 << "size_t parentOffset);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700500
Yifan Hong0a68a282016-10-21 16:32:34 -0700501 out.unindent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700502 }
503
Yifan Hongbf459bc2016-08-23 16:50:37 -0700504 if(needsResolveReferences()) {
Yifan Hongbf459bc2016-08-23 16:50:37 -0700505 out << "::android::status_t readEmbeddedReferenceFromParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700506 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800507 out << fullName() << " *obj,\n"
508 << "const ::android::hardware::Parcel &parcel,\n"
509 << "size_t parentHandle, size_t parentOffset);\n\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700510 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700511 out << "::android::status_t writeEmbeddedReferenceToParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700512 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800513 out << "const " << fullName() << " &obj,\n"
514 << "::android::hardware::Parcel *,\n"
515 << "size_t parentHandle, size_t parentOffset);\n\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700516 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700517 }
518
Andreas Huber881227d2016-08-02 14:20:21 -0700519 return OK;
520}
521
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -0700522status_t CompoundType::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
Steven Morelandb158de12016-10-10 11:59:09 -0700523 std::string space = prefix.empty() ? "" : (prefix + "::");
524 status_t err = Scope::emitTypeDefinitions(out, space + localName());
Andreas Huber881227d2016-08-02 14:20:21 -0700525
526 if (err != OK) {
527 return err;
528 }
529
Yifan Hongbf459bc2016-08-23 16:50:37 -0700530 if (needsEmbeddedReadWrite()) {
531 emitStructReaderWriter(out, prefix, true /* isReader */);
532 emitStructReaderWriter(out, prefix, false /* isReader */);
Andreas Huber881227d2016-08-02 14:20:21 -0700533 }
534
Yifan Hongbf459bc2016-08-23 16:50:37 -0700535 if (needsResolveReferences()) {
536 emitResolveReferenceDef(out, prefix, true /* isReader */);
537 emitResolveReferenceDef(out, prefix, false /* isReader */);
538 }
Andreas Huber881227d2016-08-02 14:20:21 -0700539
Steven Moreland3d98bc42017-06-23 21:36:41 +0000540 out << "std::string toString("
541 << getCppArgumentType()
542 << (mFields->empty() ? "" : " o")
543 << ") ";
544
545 out.block([&] {
546 // include toString for scalar types
547 out << "using ::android::hardware::toString;\n"
548 << "std::string os;\n";
549 out << "os += \"{\";\n";
550
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700551 for (const NamedReference<Type>* field : *mFields) {
Steven Moreland3d98bc42017-06-23 21:36:41 +0000552 out << "os += \"";
553 if (field != *(mFields->begin())) {
554 out << ", ";
555 }
556 out << "." << field->name() << " = \";\n";
557 field->type().emitDump(out, "os", "o." + field->name());
558 }
559
560 out << "os += \"}\"; return os;\n";
561 }).endl().endl();
562
Steven Morelandf91048a2017-06-23 21:38:35 +0000563 if (canCheckEquality()) {
564 out << "bool operator==("
565 << getCppArgumentType() << " " << (mFields->empty() ? "/* lhs */" : "lhs") << ", "
566 << getCppArgumentType() << " " << (mFields->empty() ? "/* rhs */" : "rhs") << ") ";
567 out.block([&] {
568 for (const auto &field : *mFields) {
569 out.sIf("lhs." + field->name() + " != rhs." + field->name(), [&] {
570 out << "return false;\n";
571 }).endl();
572 }
573 out << "return true;\n";
574 }).endl().endl();
575
576 out << "bool operator!=("
577 << getCppArgumentType() << " lhs," << getCppArgumentType() << " rhs)";
578 out.block([&] {
579 out << "return !(lhs == rhs);\n";
580 }).endl().endl();
581 } else {
582 out << "// operator== and operator!= are not generated for " << localName() << "\n";
583 }
584
Andreas Huber881227d2016-08-02 14:20:21 -0700585 return OK;
586}
587
Andreas Huber85eabdb2016-08-25 11:24:49 -0700588status_t CompoundType::emitJavaTypeDeclarations(
589 Formatter &out, bool atTopLevel) const {
590 out << "public final ";
591
592 if (!atTopLevel) {
593 out << "static ";
594 }
595
596 out << "class "
597 << localName()
598 << " {\n";
599
600 out.indent();
601
602 Scope::emitJavaTypeDeclarations(out, false /* atTopLevel */);
603
604 for (const auto &field : *mFields) {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700605 out << "public ";
606
607 field->type().emitJavaFieldInitializer(out, field->name());
608 }
609
610 if (!mFields->empty()) {
611 out << "\n";
612 }
613
Yifan Hongec102272016-12-20 18:10:07 -0800614 ////////////////////////////////////////////////////////////////////////////
615
616 if (canCheckEquality()) {
Yifan Hong7d1839f2017-02-22 13:24:29 -0800617 out << "@Override\npublic final boolean equals(Object otherObject) ";
Yifan Hongec102272016-12-20 18:10:07 -0800618 out.block([&] {
Yifan Hong7d1839f2017-02-22 13:24:29 -0800619 out.sIf("this == otherObject", [&] {
620 out << "return true;\n";
621 }).endl();
622 out.sIf("otherObject == null", [&] {
623 out << "return false;\n";
624 }).endl();
Yifan Hong45b331b2017-03-27 12:59:54 -0700625 // Though class is final, we use getClass instead of instanceof to be explicit.
626 out.sIf("otherObject.getClass() != " + fullJavaName() + ".class", [&] {
Yifan Hong7d1839f2017-02-22 13:24:29 -0800627 out << "return false;\n";
628 }).endl();
629 out << fullJavaName() << " other = (" << fullJavaName() << ")otherObject;\n";
Yifan Hongec102272016-12-20 18:10:07 -0800630 for (const auto &field : *mFields) {
Yifan Hong45b331b2017-03-27 12:59:54 -0700631 std::string condition = (field->type().isScalar() || field->type().isEnum())
Yifan Hongec102272016-12-20 18:10:07 -0800632 ? "this." + field->name() + " != other." + field->name()
Yifan Hong45b331b2017-03-27 12:59:54 -0700633 : ("!android.os.HidlSupport.deepEquals(this." + field->name()
Yifan Hongec102272016-12-20 18:10:07 -0800634 + ", other." + field->name() + ")");
635 out.sIf(condition, [&] {
636 out << "return false;\n";
637 }).endl();
638 }
639 out << "return true;\n";
640 }).endl().endl();
641
Yifan Hong7d1839f2017-02-22 13:24:29 -0800642 out << "@Override\npublic final int hashCode() ";
Yifan Hongec102272016-12-20 18:10:07 -0800643 out.block([&] {
Yifan Hong7d1839f2017-02-22 13:24:29 -0800644 out << "return java.util.Objects.hash(\n";
645 out.indent(2, [&] {
Yifan Hong932464e2017-03-30 15:40:22 -0700646 out.join(mFields->begin(), mFields->end(), ", \n", [&] (const auto &field) {
Yifan Hong45b331b2017-03-27 12:59:54 -0700647 out << "android.os.HidlSupport.deepHashCode(this." << field->name() << ")";
Yifan Hong932464e2017-03-30 15:40:22 -0700648 });
Yifan Hong7d1839f2017-02-22 13:24:29 -0800649 });
Yifan Hongec102272016-12-20 18:10:07 -0800650 out << ");\n";
651 }).endl().endl();
Yifan Hong42c68432017-03-13 16:22:03 -0700652 } else {
653 out << "// equals() is not generated for " << localName() << "\n";
Yifan Hongec102272016-12-20 18:10:07 -0800654 }
655
656 ////////////////////////////////////////////////////////////////////////////
657
Yifan Honge45b5302017-02-22 10:49:07 -0800658 out << "@Override\npublic final String toString() ";
659 out.block([&] {
660 out << "java.lang.StringBuilder builder = new java.lang.StringBuilder();\n"
661 << "builder.append(\"{\");\n";
662 for (const auto &field : *mFields) {
663 out << "builder.append(\"";
664 if (field != *(mFields->begin())) {
665 out << ", ";
666 }
667 out << "." << field->name() << " = \");\n";
668 field->type().emitJavaDump(out, "builder", "this." + field->name());
669 }
670 out << "builder.append(\"}\");\nreturn builder.toString();\n";
671 }).endl().endl();
672
Martijn Coenenb2a861c2017-04-18 15:54:25 -0700673 size_t structAlign, structSize;
674 getAlignmentAndSize(&structAlign, &structSize);
675
Yifan Honge45b5302017-02-22 10:49:07 -0800676 ////////////////////////////////////////////////////////////////////////////
677
Yifan Hong1af73532016-11-09 14:32:58 -0800678 out << "public final void readFromParcel(android.os.HwParcel parcel) {\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700679 out.indent();
Martijn Coenenb2a861c2017-04-18 15:54:25 -0700680 out << "android.os.HwBlob blob = parcel.readBuffer(";
681 out << structSize << "/* size */);\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700682 out << "readEmbeddedFromParcel(parcel, blob, 0 /* parentOffset */);\n";
683 out.unindent();
684 out << "}\n\n";
685
Andreas Huberf630bc82016-09-09 14:52:25 -0700686 ////////////////////////////////////////////////////////////////////////////
687
Martijn Coenenb2a861c2017-04-18 15:54:25 -0700688 size_t vecAlign, vecSize;
689 VectorType::getAlignmentAndSizeStatic(&vecAlign, &vecSize);
690
Yifan Hong1af73532016-11-09 14:32:58 -0800691 out << "public static final java.util.ArrayList<"
Andreas Huberf630bc82016-09-09 14:52:25 -0700692 << localName()
Yifan Hong1af73532016-11-09 14:32:58 -0800693 << "> readVectorFromParcel(android.os.HwParcel parcel) {\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700694 out.indent();
695
Yifan Hong1af73532016-11-09 14:32:58 -0800696 out << "java.util.ArrayList<"
Andreas Huberf630bc82016-09-09 14:52:25 -0700697 << localName()
Yifan Hong1af73532016-11-09 14:32:58 -0800698 << "> _hidl_vec = new java.util.ArrayList();\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700699
Martijn Coenenb2a861c2017-04-18 15:54:25 -0700700 out << "android.os.HwBlob _hidl_blob = parcel.readBuffer(";
701 out << vecSize << " /* sizeof hidl_vec<T> */);\n\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700702
703 VectorType::EmitJavaFieldReaderWriterForElementType(
Andreas Huber4c865b72016-09-14 15:26:27 -0700704 out,
705 0 /* depth */,
706 this,
Andreas Huber709b62d2016-09-19 11:21:18 -0700707 "parcel",
Andreas Huber4c865b72016-09-14 15:26:27 -0700708 "_hidl_blob",
709 "_hidl_vec",
710 "0",
711 true /* isReader */);
Andreas Huberf630bc82016-09-09 14:52:25 -0700712
Andreas Huber1b6822b2016-10-18 09:28:40 -0700713 out << "\nreturn _hidl_vec;\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700714
715 out.unindent();
716 out << "}\n\n";
717
718 ////////////////////////////////////////////////////////////////////////////
719
Andreas Huber85eabdb2016-08-25 11:24:49 -0700720 out << "public final void readEmbeddedFromParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700721 out.indent(2);
Yifan Hong1af73532016-11-09 14:32:58 -0800722 out << "android.os.HwParcel parcel, android.os.HwBlob _hidl_blob, long _hidl_offset) {\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700723 out.unindent();
724
725 size_t offset = 0;
726 for (const auto &field : *mFields) {
727 size_t fieldAlign, fieldSize;
728 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
729
730 size_t pad = offset % fieldAlign;
731 if (pad > 0) {
732 offset += fieldAlign - pad;
733 }
734
735 field->type().emitJavaFieldReaderWriter(
736 out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700737 0 /* depth */,
Andreas Huber709b62d2016-09-19 11:21:18 -0700738 "parcel",
Andreas Huber85eabdb2016-08-25 11:24:49 -0700739 "_hidl_blob",
740 field->name(),
741 "_hidl_offset + " + std::to_string(offset),
742 true /* isReader */);
743
744 offset += fieldSize;
745 }
746
747 out.unindent();
748 out << "}\n\n";
749
Andreas Huberf630bc82016-09-09 14:52:25 -0700750 ////////////////////////////////////////////////////////////////////////////
751
Yifan Hong1af73532016-11-09 14:32:58 -0800752 out << "public final void writeToParcel(android.os.HwParcel parcel) {\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700753 out.indent();
754
Yifan Hong1af73532016-11-09 14:32:58 -0800755 out << "android.os.HwBlob _hidl_blob = new android.os.HwBlob("
Andreas Huber85eabdb2016-08-25 11:24:49 -0700756 << structSize
757 << " /* size */);\n";
758
759 out << "writeEmbeddedToBlob(_hidl_blob, 0 /* parentOffset */);\n"
760 << "parcel.writeBuffer(_hidl_blob);\n";
761
762 out.unindent();
763 out << "}\n\n";
764
Andreas Huberf630bc82016-09-09 14:52:25 -0700765 ////////////////////////////////////////////////////////////////////////////
766
Andreas Huberf630bc82016-09-09 14:52:25 -0700767 out << "public static final void writeVectorToParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700768 out.indent(2);
Yifan Hong1af73532016-11-09 14:32:58 -0800769 out << "android.os.HwParcel parcel, java.util.ArrayList<"
Andreas Huberf630bc82016-09-09 14:52:25 -0700770 << localName()
Andreas Huber1b6822b2016-10-18 09:28:40 -0700771 << "> _hidl_vec) {\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700772 out.unindent();
773
Martijn Coenenb2a861c2017-04-18 15:54:25 -0700774 out << "android.os.HwBlob _hidl_blob = new android.os.HwBlob("
775 << vecSize << " /* sizeof(hidl_vec<T>) */);\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700776
777 VectorType::EmitJavaFieldReaderWriterForElementType(
Andreas Huber4c865b72016-09-14 15:26:27 -0700778 out,
779 0 /* depth */,
780 this,
Andreas Huber709b62d2016-09-19 11:21:18 -0700781 "parcel",
Andreas Huber4c865b72016-09-14 15:26:27 -0700782 "_hidl_blob",
783 "_hidl_vec",
784 "0",
785 false /* isReader */);
Andreas Huberf630bc82016-09-09 14:52:25 -0700786
787 out << "\nparcel.writeBuffer(_hidl_blob);\n";
788
789 out.unindent();
790 out << "}\n\n";
791
792 ////////////////////////////////////////////////////////////////////////////
793
Andreas Huber85eabdb2016-08-25 11:24:49 -0700794 out << "public final void writeEmbeddedToBlob(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700795 out.indent(2);
Yifan Hong1af73532016-11-09 14:32:58 -0800796 out << "android.os.HwBlob _hidl_blob, long _hidl_offset) {\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700797 out.unindent();
798
799 offset = 0;
800 for (const auto &field : *mFields) {
801 size_t fieldAlign, fieldSize;
802 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
803
804 size_t pad = offset % fieldAlign;
805 if (pad > 0) {
806 offset += fieldAlign - pad;
807 }
808
809 field->type().emitJavaFieldReaderWriter(
810 out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700811 0 /* depth */,
Andreas Huber709b62d2016-09-19 11:21:18 -0700812 "parcel",
Andreas Huber85eabdb2016-08-25 11:24:49 -0700813 "_hidl_blob",
814 field->name(),
815 "_hidl_offset + " + std::to_string(offset),
816 false /* isReader */);
817
818 offset += fieldSize;
819 }
820
821 out.unindent();
822 out << "}\n";
823
824 out.unindent();
825 out << "};\n\n";
826
827 return OK;
828}
829
Andreas Huber881227d2016-08-02 14:20:21 -0700830void CompoundType::emitStructReaderWriter(
831 Formatter &out, const std::string &prefix, bool isReader) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800832
833 std::string space = prefix.empty() ? "" : (prefix + "::");
834
Andreas Huber881227d2016-08-02 14:20:21 -0700835 out << "::android::status_t "
Yifan Hong244e82d2016-11-11 11:13:57 -0800836 << (isReader ? "readEmbeddedFromParcel"
837 : "writeEmbeddedToParcel")
838 << "(\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700839
Yifan Hong0a68a282016-10-21 16:32:34 -0700840 out.indent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700841
Yifan Hong244e82d2016-11-11 11:13:57 -0800842 bool useName = false;
843 for (const auto &field : *mFields) {
844 if (field->type().useNameInEmitReaderWriterEmbedded(isReader)) {
845 useName = true;
846 break;
847 }
848 }
849 std::string name = useName ? "obj" : "/* obj */";
850 // if not useName, then obj should not be used at all,
851 // then the #error should not be emitted.
852 std::string error = useName ? "" : "\n#error\n";
853
Andreas Huber881227d2016-08-02 14:20:21 -0700854 if (isReader) {
Martijn Coenenb2a861c2017-04-18 15:54:25 -0700855 out << "const " << space << localName() << " &" << name << ",\n";
Andreas Huber8a82ff72016-08-04 10:29:39 -0700856 out << "const ::android::hardware::Parcel &parcel,\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700857 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800858 out << "const " << space << localName() << " &" << name << ",\n";
Andreas Huber8a82ff72016-08-04 10:29:39 -0700859 out << "::android::hardware::Parcel *parcel,\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700860 }
861
862 out << "size_t parentHandle,\n"
863 << "size_t parentOffset)";
864
Andreas Huber881227d2016-08-02 14:20:21 -0700865 out << " {\n";
866
Yifan Hong0a68a282016-10-21 16:32:34 -0700867 out.unindent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700868 out.indent();
869
Iliyan Malchev549e2592016-08-10 08:59:12 -0700870 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700871
872 for (const auto &field : *mFields) {
873 if (!field->type().needsEmbeddedReadWrite()) {
874 continue;
875 }
876
877 field->type().emitReaderWriterEmbedded(
878 out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700879 0 /* depth */,
Martijn Coenenb2a861c2017-04-18 15:54:25 -0700880 name + "." + field->name() + error,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700881 field->name() /* sanitizedName */,
Andreas Huber881227d2016-08-02 14:20:21 -0700882 false /* nameIsPointer */,
883 "parcel",
884 !isReader /* parcelObjIsPointer */,
885 isReader,
Andreas Huber737080b2016-08-02 15:38:04 -0700886 ErrorMode_Return,
Andreas Huber881227d2016-08-02 14:20:21 -0700887 "parentHandle",
Andreas Huber0e00de42016-08-03 09:56:02 -0700888 "parentOffset + offsetof("
889 + fullName()
890 + ", "
891 + field->name()
892 + ")");
Andreas Huber881227d2016-08-02 14:20:21 -0700893 }
894
Iliyan Malchev549e2592016-08-10 08:59:12 -0700895 out << "return _hidl_err;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700896
897 out.unindent();
898 out << "}\n\n";
899}
900
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -0700901void CompoundType::emitResolveReferenceDef(Formatter& out, const std::string& prefix,
902 bool isReader) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800903 out << "::android::status_t ";
904 const std::string space(prefix.empty() ? "" : (prefix + "::"));
Yifan Hong00f47172016-09-30 14:40:45 -0700905
906 bool useParent = false;
907 for (const auto &field : *mFields) {
908 if (field->type().useParentInEmitResolveReferencesEmbedded()) {
909 useParent = true;
910 break;
911 }
912 }
913
914 std::string parentHandleName = useParent ? "parentHandle" : "/* parentHandle */";
915 std::string parentOffsetName = useParent ? "parentOffset" : "/* parentOffset */";
916
Yifan Hongbf459bc2016-08-23 16:50:37 -0700917 if (isReader) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800918 out << "readEmbeddedReferenceFromParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700919 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800920 out << space + localName() + " *obj,\n"
921 << "const ::android::hardware::Parcel &parcel,\n"
Yifan Hong00f47172016-09-30 14:40:45 -0700922 << "size_t " << parentHandleName << ", "
923 << "size_t " << parentOffsetName << ")\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700924 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700925 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800926 out << "writeEmbeddedReferenceToParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700927 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800928 out << "const " << space + localName() + " &obj,\n"
929 << "::android::hardware::Parcel *parcel,\n"
Yifan Hong00f47172016-09-30 14:40:45 -0700930 << "size_t " << parentHandleName << ", "
Yifan Hong244e82d2016-11-11 11:13:57 -0800931 << "size_t " << parentOffsetName << ")\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700932 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700933 }
934
935 out << " {\n";
936
937 out.indent();
938
939 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
940
Yifan Hong244e82d2016-11-11 11:13:57 -0800941 const std::string nameDeref(isReader ? "obj->" : "obj.");
Yifan Hong00f47172016-09-30 14:40:45 -0700942 // if not useParent, then parentName and offsetText
943 // should not be used at all, then the #error should not be emitted.
944 std::string error = useParent ? "" : "\n#error\n";
945
Yifan Hongbf459bc2016-08-23 16:50:37 -0700946 for (const auto &field : *mFields) {
947 if (!field->type().needsResolveReferences()) {
948 continue;
949 }
950
951 field->type().emitResolveReferencesEmbedded(
952 out,
953 0 /* depth */,
Yifan Hong244e82d2016-11-11 11:13:57 -0800954 nameDeref + field->name(),
Yifan Hongbf459bc2016-08-23 16:50:37 -0700955 field->name() /* sanitizedName */,
956 false, // nameIsPointer
957 "parcel", // const std::string &parcelObj,
958 !isReader, // bool parcelObjIsPointer,
959 isReader, // bool isReader,
960 ErrorMode_Return,
Yifan Hong00f47172016-09-30 14:40:45 -0700961 parentHandleName + error,
962 parentOffsetName
963 + " + offsetof("
Yifan Hongbf459bc2016-08-23 16:50:37 -0700964 + fullName()
965 + ", "
966 + field->name()
Yifan Hong00f47172016-09-30 14:40:45 -0700967 + ")"
968 + error);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700969 }
970
Yifan Hongbf459bc2016-08-23 16:50:37 -0700971 out << "return _hidl_err;\n";
972
973 out.unindent();
974 out << "}\n\n";
975}
976
Andreas Huber881227d2016-08-02 14:20:21 -0700977bool CompoundType::needsEmbeddedReadWrite() const {
978 if (mStyle != STYLE_STRUCT) {
979 return false;
980 }
981
982 for (const auto &field : *mFields) {
983 if (field->type().needsEmbeddedReadWrite()) {
984 return true;
985 }
986 }
987
988 return false;
989}
990
Yifan Hongbf459bc2016-08-23 16:50:37 -0700991bool CompoundType::needsResolveReferences() const {
992 if (mStyle != STYLE_STRUCT) {
993 return false;
994 }
995
996 for (const auto &field : *mFields) {
997 if (field->type().needsResolveReferences()) {
998 return true;
999 }
1000 }
1001
1002 return false;
1003}
1004
Andreas Huber881227d2016-08-02 14:20:21 -07001005bool CompoundType::resultNeedsDeref() const {
1006 return true;
1007}
1008
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07001009status_t CompoundType::emitVtsTypeDeclarations(Formatter &out) const {
Zhuoyao Zhangbf828c82016-10-26 14:15:10 -07001010 out << "name: \"" << fullName() << "\"\n";
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -07001011 out << "type: " << getVtsType() << "\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001012
1013 // Emit declaration for each subtype.
1014 for (const auto &type : getSubTypes()) {
1015 switch (mStyle) {
1016 case STYLE_STRUCT:
1017 {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -07001018 out << "sub_struct: {\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001019 break;
1020 }
1021 case STYLE_UNION:
1022 {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -07001023 out << "sub_union: {\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001024 break;
1025 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001026 }
1027 out.indent();
1028 status_t status(type->emitVtsTypeDeclarations(out));
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07001029 if (status != OK) {
1030 return status;
1031 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001032 out.unindent();
1033 out << "}\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07001034 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001035
1036 // Emit declaration for each field.
1037 for (const auto &field : *mFields) {
1038 switch (mStyle) {
1039 case STYLE_STRUCT:
1040 {
1041 out << "struct_value: {\n";
1042 break;
1043 }
1044 case STYLE_UNION:
1045 {
1046 out << "union_value: {\n";
1047 break;
1048 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001049 }
1050 out.indent();
1051 out << "name: \"" << field->name() << "\"\n";
1052 status_t status = field->type().emitVtsAttributeType(out);
1053 if (status != OK) {
1054 return status;
1055 }
1056 out.unindent();
1057 out << "}\n";
1058 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07001059
1060 return OK;
1061}
1062
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001063status_t CompoundType::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -07001064 out << "type: " << getVtsType() << "\n";
Zhuoyao Zhangbf828c82016-10-26 14:15:10 -07001065 out << "predefined_type: \"" << fullName() << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07001066 return OK;
1067}
1068
Andreas Huber70a59e12016-08-16 12:57:01 -07001069bool CompoundType::isJavaCompatible() const {
Andreas Huber0fa9e392016-08-31 09:05:44 -07001070 if (mStyle != STYLE_STRUCT || !Scope::isJavaCompatible()) {
1071 return false;
1072 }
1073
1074 for (const auto &field : *mFields) {
1075 if (!field->type().isJavaCompatible()) {
1076 return false;
1077 }
1078 }
1079
1080 return true;
Andreas Huber85eabdb2016-08-25 11:24:49 -07001081}
1082
Andreas Huber60d3b222017-03-30 09:10:56 -07001083bool CompoundType::containsPointer() const {
1084 if (Scope::containsPointer()) {
1085 return true;
1086 }
1087
1088 for (const auto &field : *mFields) {
1089 if (field->type().containsPointer()) {
1090 return true;
1091 }
1092 }
1093
1094 return false;
1095}
1096
Andreas Huber85eabdb2016-08-25 11:24:49 -07001097void CompoundType::getAlignmentAndSize(size_t *align, size_t *size) const {
1098 *align = 1;
Andreas Huber60d3b222017-03-30 09:10:56 -07001099 *size = 0;
Andreas Huber85eabdb2016-08-25 11:24:49 -07001100
1101 size_t offset = 0;
1102 for (const auto &field : *mFields) {
1103 // Each field is aligned according to its alignment requirement.
1104 // The surrounding structure's alignment is the maximum of its
1105 // fields' aligments.
1106
1107 size_t fieldAlign, fieldSize;
1108 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
1109
1110 size_t pad = offset % fieldAlign;
1111 if (pad > 0) {
1112 offset += fieldAlign - pad;
1113 }
1114
Andreas Huber60d3b222017-03-30 09:10:56 -07001115 if (mStyle == STYLE_STRUCT) {
1116 offset += fieldSize;
1117 } else {
1118 *size = std::max(*size, fieldSize);
1119 }
Andreas Huber85eabdb2016-08-25 11:24:49 -07001120
1121 if (fieldAlign > (*align)) {
1122 *align = fieldAlign;
1123 }
1124 }
1125
Andreas Huber60d3b222017-03-30 09:10:56 -07001126 if (mStyle == STYLE_STRUCT) {
1127 *size = offset;
Andreas Huber85eabdb2016-08-25 11:24:49 -07001128 }
1129
Andreas Huber60d3b222017-03-30 09:10:56 -07001130 // Final padding to account for the structure's alignment.
1131 size_t pad = (*size) % (*align);
1132 if (pad > 0) {
1133 (*size) += (*align) - pad;
1134 }
Andreas Huberca4bc892017-01-09 14:58:12 -08001135
1136 if (*size == 0) {
1137 // An empty struct still occupies a byte of space in C++.
1138 *size = 1;
1139 }
Andreas Huber70a59e12016-08-16 12:57:01 -07001140}
1141
Andreas Huberc9410c72016-07-28 12:18:40 -07001142} // namespace android
1143