blob: 64ec2c2b5dcb34c35704ef373929bd5f457ed8bd [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 Iskhakov565b0132017-09-06 18:07:11 -070029CompoundType::CompoundType(Style style, const char* localName, const FQName& fullName,
30 const Location& location, Scope* parent)
31 : Scope(localName, fullName, 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
Howard Chenecfb4512017-11-21 18:28:53 +080051 if ((type.isVector() && static_cast<const VectorType*>(&type)->isVectorOfBinders())) {
Timur Iskhakovcec46c42017-08-09 00:22:02 -070052 std::cerr << "ERROR: Struct/Union must not contain references to interfaces at "
53 << field->location() << "\n";
54 return UNKNOWN_ERROR;
Andreas Huberb95ea8a2016-08-15 15:35:42 -070055 }
56
Andreas Huber5a545442016-08-03 10:44:56 -070057 if (mStyle == STYLE_UNION) {
58 if (type.needsEmbeddedReadWrite()) {
Timur Iskhakovcec46c42017-08-09 00:22:02 -070059 std::cerr << "ERROR: Union must not contain any types that need fixup at "
60 << field->location() << "\n";
61 return UNKNOWN_ERROR;
Andreas Huber5a545442016-08-03 10:44:56 -070062 }
Andreas Huber5a545442016-08-03 10:44:56 -070063 }
64 }
65
Timur Iskhakovcec46c42017-08-09 00:22:02 -070066 status_t err = validateUniqueNames();
67 if (err != OK) return err;
68
69 return Scope::validate();
70}
71
72status_t CompoundType::validateUniqueNames() const {
73 std::unordered_set<std::string> names;
74
75 for (const auto* field : *mFields) {
76 if (names.find(field->name()) != names.end()) {
77 std::cerr << "ERROR: Redefinition of field '" << field->name() << "' at "
78 << field->location() << "\n";
79 return UNKNOWN_ERROR;
80 }
81 names.insert(field->name());
82 }
83
84 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -070085}
86
Andreas Huberf630bc82016-09-09 14:52:25 -070087bool CompoundType::isCompoundType() const {
88 return true;
89}
90
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -070091bool CompoundType::deepCanCheckEquality(std::unordered_set<const Type*>* visited) const {
Yifan Hongc6752dc2016-12-20 14:00:14 -080092 if (mStyle == STYLE_UNION) {
93 return false;
94 }
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -070095 for (const auto* field : *mFields) {
96 if (!field->get()->canCheckEquality(visited)) {
Yifan Hongc6752dc2016-12-20 14:00:14 -080097 return false;
98 }
99 }
100 return true;
101}
102
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700103std::string CompoundType::typeName() const {
104 switch (mStyle) {
105 case STYLE_STRUCT: {
106 return "struct " + localName();
107 }
108 case STYLE_UNION: {
109 return "union " + localName();
110 }
111 }
112 CHECK(!"Should not be here");
113}
114
Andreas Huber881227d2016-08-02 14:20:21 -0700115std::string CompoundType::getCppType(
Steven Moreland979e0992016-09-07 09:18:08 -0700116 StorageMode mode,
Steven Morelande30ee9b2017-05-09 13:31:01 -0700117 bool /* specifyNamespaces */) const {
118 const std::string base = fullName();
Andreas Huber881227d2016-08-02 14:20:21 -0700119
120 switch (mode) {
121 case StorageMode_Stack:
122 return base;
123
124 case StorageMode_Argument:
125 return "const " + base + "&";
126
127 case StorageMode_Result:
Howard Chenecfb4512017-11-21 18:28:53 +0800128 return base + (containsInterface()?"":"*");
Andreas Huber881227d2016-08-02 14:20:21 -0700129 }
130}
131
Yifan Hong4ed13472016-11-02 10:44:11 -0700132std::string CompoundType::getJavaType(bool /* forInitializer */) const {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700133 return fullJavaName();
134}
135
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700136std::string CompoundType::getVtsType() const {
137 switch (mStyle) {
138 case STYLE_STRUCT:
139 {
140 return "TYPE_STRUCT";
141 }
142 case STYLE_UNION:
143 {
144 return "TYPE_UNION";
145 }
146 }
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700147 CHECK(!"Should not be here");
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700148}
149
Howard Chenecfb4512017-11-21 18:28:53 +0800150bool CompoundType::containsInterface() const {
151 for (const auto& field : *mFields) {
152 if (field->type().isCompoundType()) {
153 const Type& t = field->type();
154 const CompoundType* ct = static_cast<const CompoundType*>(&t);
155 if (ct->containsInterface()) {
156 return true;
157 }
158 }
159 if (field->type().isInterface()) {
160 return true;
161 }
162 }
163 return false;
164}
165
Andreas Huber881227d2016-08-02 14:20:21 -0700166void CompoundType::emitReaderWriter(
167 Formatter &out,
168 const std::string &name,
169 const std::string &parcelObj,
170 bool parcelObjIsPointer,
171 bool isReader,
172 ErrorMode mode) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700173
174 const std::string parcelObjDeref =
175 parcelObj + (parcelObjIsPointer ? "->" : ".");
176
Howard Chenecfb4512017-11-21 18:28:53 +0800177 if(containsInterface()){
178 for (const auto& field : *mFields) {
179 field->type().emitReaderWriter(out, name + "." + field->name(),
180 parcelObj, parcelObjIsPointer, isReader, mode);
181 }
Andreas Huber881227d2016-08-02 14:20:21 -0700182 } else {
Howard Chenecfb4512017-11-21 18:28:53 +0800183 const std::string parentName = "_hidl_" + name + "_parent";
Andreas Huber881227d2016-08-02 14:20:21 -0700184
Howard Chenecfb4512017-11-21 18:28:53 +0800185 out << "size_t " << parentName << ";\n\n";
186
187 if (isReader) {
188 out << "_hidl_err = " << parcelObjDeref << "readBuffer("
189 << "sizeof(*" << name << "), &" << parentName << ", "
190 << " const_cast<const void**>(reinterpret_cast<void **>("
191 << "&" << name << ")));\n";
192 handleError(out, mode);
193 } else {
194 out << "_hidl_err = "
195 << parcelObjDeref
196 << "writeBuffer(&"
197 << name
198 << ", sizeof("
199 << name
200 << "), &"
201 << parentName
202 << ");\n";
203 handleError(out, mode);
204 }
205 if (mStyle != STYLE_STRUCT) {
206 return;
207 }
208 if (needsEmbeddedReadWrite()) {
209 emitReaderWriterEmbedded(out, 0 /* depth */, name, name, /* sanitizedName */
210 isReader /* nameIsPointer */, parcelObj, parcelObjIsPointer,
211 isReader, mode, parentName, "0 /* parentOffset */");
212 }
Andreas Huber881227d2016-08-02 14:20:21 -0700213 }
Andreas Huber881227d2016-08-02 14:20:21 -0700214}
215
216void CompoundType::emitReaderWriterEmbedded(
217 Formatter &out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700218 size_t /* depth */,
Andreas Huber881227d2016-08-02 14:20:21 -0700219 const std::string &name,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700220 const std::string & /*sanitizedName */,
Andreas Huber881227d2016-08-02 14:20:21 -0700221 bool nameIsPointer,
222 const std::string &parcelObj,
223 bool parcelObjIsPointer,
224 bool isReader,
225 ErrorMode mode,
226 const std::string &parentName,
227 const std::string &offsetText) const {
228 emitReaderWriterEmbeddedForTypeName(
229 out,
230 name,
231 nameIsPointer,
232 parcelObj,
233 parcelObjIsPointer,
234 isReader,
235 mode,
236 parentName,
237 offsetText,
Andreas Huber0e00de42016-08-03 09:56:02 -0700238 fullName(),
Yifan Hong244e82d2016-11-11 11:13:57 -0800239 "" /* childName */,
240 "" /* namespace */);
Andreas Huber881227d2016-08-02 14:20:21 -0700241}
242
Andreas Huber85eabdb2016-08-25 11:24:49 -0700243void CompoundType::emitJavaReaderWriter(
244 Formatter &out,
245 const std::string &parcelObj,
246 const std::string &argName,
247 bool isReader) const {
248 if (isReader) {
249 out << "new " << fullJavaName() << "();\n";
250 }
251
252 out << argName
253 << "."
254 << (isReader ? "readFromParcel" : "writeToParcel")
255 << "("
256 << parcelObj
257 << ");\n";
258}
259
260void CompoundType::emitJavaFieldInitializer(
261 Formatter &out, const std::string &fieldName) const {
262 out << "final "
263 << fullJavaName()
264 << " "
265 << fieldName
266 << " = new "
267 << fullJavaName()
268 << "();\n";
269}
270
271void CompoundType::emitJavaFieldReaderWriter(
272 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700273 size_t /* depth */,
Andreas Huber709b62d2016-09-19 11:21:18 -0700274 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700275 const std::string &blobName,
276 const std::string &fieldName,
277 const std::string &offset,
278 bool isReader) const {
279 if (isReader) {
280 out << fieldName
Andreas Huber709b62d2016-09-19 11:21:18 -0700281 << ".readEmbeddedFromParcel("
282 << parcelName
283 << ", "
Andreas Huber85eabdb2016-08-25 11:24:49 -0700284 << blobName
285 << ", "
286 << offset
287 << ");\n";
288
289 return;
290 }
291
292 out << fieldName
293 << ".writeEmbeddedToBlob("
294 << blobName
295 << ", "
296 << offset
297 << ");\n";
298}
Yifan Hongbf459bc2016-08-23 16:50:37 -0700299void CompoundType::emitResolveReferences(
300 Formatter &out,
301 const std::string &name,
302 bool nameIsPointer,
303 const std::string &parcelObj,
304 bool parcelObjIsPointer,
305 bool isReader,
306 ErrorMode mode) const {
307 emitResolveReferencesEmbedded(
308 out,
309 0 /* depth */,
310 name,
311 name /* sanitizedName */,
312 nameIsPointer,
313 parcelObj,
314 parcelObjIsPointer,
315 isReader,
316 mode,
317 "_hidl_" + name + "_parent",
318 "0 /* parentOffset */");
319}
320
321void CompoundType::emitResolveReferencesEmbedded(
322 Formatter &out,
323 size_t /* depth */,
324 const std::string &name,
325 const std::string &/* sanitizedName */,
326 bool nameIsPointer,
327 const std::string &parcelObj,
328 bool parcelObjIsPointer,
329 bool isReader,
330 ErrorMode mode,
331 const std::string &parentName,
332 const std::string &offsetText) const {
333 CHECK(needsResolveReferences());
334
335 const std::string parcelObjDeref =
336 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
337
338 const std::string parcelObjPointer =
339 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
340
Yifan Hong244e82d2016-11-11 11:13:57 -0800341 const std::string nameDerefed = nameIsPointer ? ("*" + name) : name;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700342 const std::string namePointer = nameIsPointer ? name : ("&" + name);
343
344 out << "_hidl_err = ";
345
346 if (isReader) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800347 out << "readEmbeddedReferenceFromParcel(\n";
Yifan Hongbf459bc2016-08-23 16:50:37 -0700348 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800349 out << "writeEmbeddedReferenceToParcel(\n";
Yifan Hongbf459bc2016-08-23 16:50:37 -0700350 }
351
Yifan Hong33223ca2016-12-13 15:07:35 -0800352 out.indent(2, [&]{
Yifan Hong244e82d2016-11-11 11:13:57 -0800353 if (isReader) {
354 out << "const_cast<"
355 << fullName()
356 << " *"
357 << ">("
358 << namePointer
359 << "),\n"
360 << parcelObjDeref;
361 } else {
362 out << nameDerefed
363 << ",\n"
364 << parcelObjPointer;
365 }
366
367 out << ",\n"
Yifan Hong0a68a282016-10-21 16:32:34 -0700368 << parentName
369 << ",\n"
370 << offsetText
371 << ");\n\n";
372 });
Yifan Hongbf459bc2016-08-23 16:50:37 -0700373
374 handleError(out, mode);
375}
Andreas Huber85eabdb2016-08-25 11:24:49 -0700376
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800377void CompoundType::emitTypeDeclarations(Formatter& out) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700378 out << ((mStyle == STYLE_STRUCT) ? "struct" : "union")
379 << " "
Andreas Huber0e00de42016-08-03 09:56:02 -0700380 << localName()
Yifan Hongb1d2ebc2016-12-20 17:18:07 -0800381 << " final {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700382
383 out.indent();
384
385 Scope::emitTypeDeclarations(out);
386
Andreas Huber60d3b222017-03-30 09:10:56 -0700387 if (containsPointer()) {
Andreas Huberca4bc892017-01-09 14:58:12 -0800388 for (const auto &field : *mFields) {
389 out << field->type().getCppStackType()
390 << " "
391 << field->name()
392 << ";\n";
393 }
394
395 out.unindent();
396 out << "};\n\n";
397
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800398 return;
Andreas Huber881227d2016-08-02 14:20:21 -0700399 }
400
Andreas Huberca4bc892017-01-09 14:58:12 -0800401 for (int pass = 0; pass < 2; ++pass) {
402 size_t offset = 0;
403 for (const auto &field : *mFields) {
404 size_t fieldAlign, fieldSize;
405 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
406
407 size_t pad = offset % fieldAlign;
408 if (pad > 0) {
409 offset += fieldAlign - pad;
410 }
411
412 if (pass == 0) {
413 out << field->type().getCppStackType()
414 << " "
415 << field->name()
416 << " __attribute__ ((aligned("
417 << fieldAlign
418 << ")));\n";
419 } else {
420 out << "static_assert(offsetof("
421 << fullName()
422 << ", "
423 << field->name()
424 << ") == "
425 << offset
426 << ", \"wrong offset\");\n";
427 }
428
Andreas Huber60d3b222017-03-30 09:10:56 -0700429 if (mStyle == STYLE_STRUCT) {
430 offset += fieldSize;
431 }
Andreas Huberca4bc892017-01-09 14:58:12 -0800432 }
433
434 if (pass == 0) {
435 out.unindent();
436 out << "};\n\n";
437 }
438 }
439
440 size_t structAlign, structSize;
441 getAlignmentAndSize(&structAlign, &structSize);
442
443 out << "static_assert(sizeof("
444 << fullName()
445 << ") == "
446 << structSize
447 << ", \"wrong size\");\n";
448
449 out << "static_assert(__alignof("
450 << fullName()
451 << ") == "
452 << structAlign
453 << ", \"wrong alignment\");\n\n";
Yifan Hong244e82d2016-11-11 11:13:57 -0800454}
455
Timur Iskhakovfd3f2502017-09-05 16:25:02 -0700456void CompoundType::emitTypeForwardDeclaration(Formatter& out) const {
457 out << ((mStyle == STYLE_STRUCT) ? "struct" : "union") << " " << localName() << ";\n";
458}
Yifan Hong244e82d2016-11-11 11:13:57 -0800459
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800460void CompoundType::emitPackageTypeDeclarations(Formatter& out) const {
Steven Moreland4b8f7a12017-11-17 15:39:54 -0800461 Scope::emitPackageTypeDeclarations(out);
Yifan Hongc6752dc2016-12-20 14:00:14 -0800462
Steven Morelandbf714212017-10-27 18:29:01 -0700463 // TODO(b/65200821): remove these ifdefs
464 out << "#ifdef REALLY_IS_HIDL_INTERNAL_LIB" << gCurrentCompileName << "\n";
Steven Moreland3d98bc42017-06-23 21:36:41 +0000465 out << "std::string toString("
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800466 << getCppArgumentType()
Steven Moreland3d98bc42017-06-23 21:36:41 +0000467 << ");\n\n";
Yifan Hong42c68432017-03-13 16:22:03 -0700468 if (canCheckEquality()) {
Steven Morelandf91048a2017-06-23 21:38:35 +0000469 out << "bool operator==("
470 << getCppArgumentType() << ", " << getCppArgumentType() << ");\n\n";
Yifan Hong42c68432017-03-13 16:22:03 -0700471
Steven Morelandf91048a2017-06-23 21:38:35 +0000472 out << "bool operator!=("
473 << getCppArgumentType() << ", " << getCppArgumentType() << ");\n\n";
Steven Morelandbf714212017-10-27 18:29:01 -0700474 }
475 out << "#else\n";
476 out << "static inline std::string toString("
477 << getCppArgumentType()
478 << (mFields->empty() ? "" : " o")
479 << ") ";
480
481 out.block([&] {
482 // include toString for scalar types
483 out << "using ::android::hardware::toString;\n"
484 << "std::string os;\n";
485 out << "os += \"{\";\n";
486
487 for (const NamedReference<Type>* field : *mFields) {
488 out << "os += \"";
489 if (field != *(mFields->begin())) {
490 out << ", ";
491 }
492 out << "." << field->name() << " = \";\n";
493 field->type().emitDump(out, "os", "o." + field->name());
494 }
495
496 out << "os += \"}\"; return os;\n";
497 }).endl().endl();
498
499 if (canCheckEquality()) {
500 out << "static inline bool operator==("
501 << getCppArgumentType() << " " << (mFields->empty() ? "/* lhs */" : "lhs") << ", "
502 << getCppArgumentType() << " " << (mFields->empty() ? "/* rhs */" : "rhs") << ") ";
503 out.block([&] {
504 for (const auto &field : *mFields) {
505 out.sIf("lhs." + field->name() + " != rhs." + field->name(), [&] {
506 out << "return false;\n";
507 }).endl();
508 }
509 out << "return true;\n";
510 }).endl().endl();
511
512 out << "static inline bool operator!=("
513 << getCppArgumentType() << " lhs," << getCppArgumentType() << " rhs)";
514 out.block([&] {
515 out << "return !(lhs == rhs);\n";
516 }).endl().endl();
Yifan Hong42c68432017-03-13 16:22:03 -0700517 } else {
Steven Morelandf91048a2017-06-23 21:38:35 +0000518 out << "// operator== and operator!= are not generated for " << localName() << "\n\n";
Yifan Hongc6752dc2016-12-20 14:00:14 -0800519 }
Steven Morelandbf714212017-10-27 18:29:01 -0700520 out << "#endif // REALLY_IS_HIDL_INTERNAL_LIB\n";
Yifan Hongc6752dc2016-12-20 14:00:14 -0800521}
522
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800523void CompoundType::emitPackageHwDeclarations(Formatter& out) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700524 if (needsEmbeddedReadWrite()) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800525 out << "::android::status_t readEmbeddedFromParcel(\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700526
Yifan Hong0a68a282016-10-21 16:32:34 -0700527 out.indent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700528
Martijn Coenenb2a861c2017-04-18 15:54:25 -0700529 out << "const " << fullName() << " &obj,\n"
Yifan Hong244e82d2016-11-11 11:13:57 -0800530 << "const ::android::hardware::Parcel &parcel,\n"
531 << "size_t parentHandle,\n"
532 << "size_t parentOffset);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700533
Yifan Hong0a68a282016-10-21 16:32:34 -0700534 out.unindent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700535
536 out << "::android::status_t writeEmbeddedToParcel(\n";
537
Yifan Hong0a68a282016-10-21 16:32:34 -0700538 out.indent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700539
Yifan Hong244e82d2016-11-11 11:13:57 -0800540 out << "const " << fullName() << " &obj,\n"
541 << "::android::hardware::Parcel *parcel,\n"
542 << "size_t parentHandle,\n"
543 << "size_t parentOffset);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700544
Yifan Hong0a68a282016-10-21 16:32:34 -0700545 out.unindent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700546 }
547
Yifan Hongbf459bc2016-08-23 16:50:37 -0700548 if(needsResolveReferences()) {
Yifan Hongbf459bc2016-08-23 16:50:37 -0700549 out << "::android::status_t readEmbeddedReferenceFromParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700550 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800551 out << fullName() << " *obj,\n"
552 << "const ::android::hardware::Parcel &parcel,\n"
553 << "size_t parentHandle, size_t parentOffset);\n\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700554 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700555 out << "::android::status_t writeEmbeddedReferenceToParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700556 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800557 out << "const " << fullName() << " &obj,\n"
558 << "::android::hardware::Parcel *,\n"
559 << "size_t parentHandle, size_t parentOffset);\n\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700560 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700561 }
Andreas Huber881227d2016-08-02 14:20:21 -0700562}
563
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800564void CompoundType::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
Steven Morelandb158de12016-10-10 11:59:09 -0700565 std::string space = prefix.empty() ? "" : (prefix + "::");
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800566 Scope::emitTypeDefinitions(out, space + localName());
Andreas Huber881227d2016-08-02 14:20:21 -0700567
Yifan Hongbf459bc2016-08-23 16:50:37 -0700568 if (needsEmbeddedReadWrite()) {
569 emitStructReaderWriter(out, prefix, true /* isReader */);
570 emitStructReaderWriter(out, prefix, false /* isReader */);
Andreas Huber881227d2016-08-02 14:20:21 -0700571 }
572
Yifan Hongbf459bc2016-08-23 16:50:37 -0700573 if (needsResolveReferences()) {
574 emitResolveReferenceDef(out, prefix, true /* isReader */);
575 emitResolveReferenceDef(out, prefix, false /* isReader */);
576 }
Andreas Huber881227d2016-08-02 14:20:21 -0700577
Steven Morelandbf714212017-10-27 18:29:01 -0700578 // TODO(b/65200821): remove toString + operator== from .cpp once all prebuilts are rebuilt
579
Steven Moreland3d98bc42017-06-23 21:36:41 +0000580 out << "std::string toString("
581 << getCppArgumentType()
582 << (mFields->empty() ? "" : " o")
583 << ") ";
584
585 out.block([&] {
586 // include toString for scalar types
587 out << "using ::android::hardware::toString;\n"
588 << "std::string os;\n";
589 out << "os += \"{\";\n";
590
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700591 for (const NamedReference<Type>* field : *mFields) {
Steven Moreland3d98bc42017-06-23 21:36:41 +0000592 out << "os += \"";
593 if (field != *(mFields->begin())) {
594 out << ", ";
595 }
596 out << "." << field->name() << " = \";\n";
597 field->type().emitDump(out, "os", "o." + field->name());
598 }
599
600 out << "os += \"}\"; return os;\n";
601 }).endl().endl();
602
Steven Morelandf91048a2017-06-23 21:38:35 +0000603 if (canCheckEquality()) {
604 out << "bool operator==("
605 << getCppArgumentType() << " " << (mFields->empty() ? "/* lhs */" : "lhs") << ", "
606 << getCppArgumentType() << " " << (mFields->empty() ? "/* rhs */" : "rhs") << ") ";
607 out.block([&] {
608 for (const auto &field : *mFields) {
609 out.sIf("lhs." + field->name() + " != rhs." + field->name(), [&] {
610 out << "return false;\n";
611 }).endl();
612 }
613 out << "return true;\n";
614 }).endl().endl();
615
616 out << "bool operator!=("
617 << getCppArgumentType() << " lhs," << getCppArgumentType() << " rhs)";
618 out.block([&] {
619 out << "return !(lhs == rhs);\n";
620 }).endl().endl();
621 } else {
622 out << "// operator== and operator!= are not generated for " << localName() << "\n";
623 }
Andreas Huber881227d2016-08-02 14:20:21 -0700624}
625
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800626void CompoundType::emitJavaTypeDeclarations(Formatter& out, bool atTopLevel) const {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700627 out << "public final ";
628
629 if (!atTopLevel) {
630 out << "static ";
631 }
632
633 out << "class "
634 << localName()
635 << " {\n";
636
637 out.indent();
638
639 Scope::emitJavaTypeDeclarations(out, false /* atTopLevel */);
640
641 for (const auto &field : *mFields) {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700642 out << "public ";
643
644 field->type().emitJavaFieldInitializer(out, field->name());
645 }
646
647 if (!mFields->empty()) {
648 out << "\n";
649 }
650
Yifan Hongec102272016-12-20 18:10:07 -0800651 ////////////////////////////////////////////////////////////////////////////
652
653 if (canCheckEquality()) {
Yifan Hong7d1839f2017-02-22 13:24:29 -0800654 out << "@Override\npublic final boolean equals(Object otherObject) ";
Yifan Hongec102272016-12-20 18:10:07 -0800655 out.block([&] {
Yifan Hong7d1839f2017-02-22 13:24:29 -0800656 out.sIf("this == otherObject", [&] {
657 out << "return true;\n";
658 }).endl();
659 out.sIf("otherObject == null", [&] {
660 out << "return false;\n";
661 }).endl();
Yifan Hong45b331b2017-03-27 12:59:54 -0700662 // Though class is final, we use getClass instead of instanceof to be explicit.
663 out.sIf("otherObject.getClass() != " + fullJavaName() + ".class", [&] {
Yifan Hong7d1839f2017-02-22 13:24:29 -0800664 out << "return false;\n";
665 }).endl();
666 out << fullJavaName() << " other = (" << fullJavaName() << ")otherObject;\n";
Yifan Hongec102272016-12-20 18:10:07 -0800667 for (const auto &field : *mFields) {
Yifan Hong45b331b2017-03-27 12:59:54 -0700668 std::string condition = (field->type().isScalar() || field->type().isEnum())
Yifan Hongec102272016-12-20 18:10:07 -0800669 ? "this." + field->name() + " != other." + field->name()
Yifan Hong45b331b2017-03-27 12:59:54 -0700670 : ("!android.os.HidlSupport.deepEquals(this." + field->name()
Yifan Hongec102272016-12-20 18:10:07 -0800671 + ", other." + field->name() + ")");
672 out.sIf(condition, [&] {
673 out << "return false;\n";
674 }).endl();
675 }
676 out << "return true;\n";
677 }).endl().endl();
678
Yifan Hong7d1839f2017-02-22 13:24:29 -0800679 out << "@Override\npublic final int hashCode() ";
Yifan Hongec102272016-12-20 18:10:07 -0800680 out.block([&] {
Yifan Hong7d1839f2017-02-22 13:24:29 -0800681 out << "return java.util.Objects.hash(\n";
682 out.indent(2, [&] {
Yifan Hong932464e2017-03-30 15:40:22 -0700683 out.join(mFields->begin(), mFields->end(), ", \n", [&] (const auto &field) {
Yifan Hong45b331b2017-03-27 12:59:54 -0700684 out << "android.os.HidlSupport.deepHashCode(this." << field->name() << ")";
Yifan Hong932464e2017-03-30 15:40:22 -0700685 });
Yifan Hong7d1839f2017-02-22 13:24:29 -0800686 });
Yifan Hongec102272016-12-20 18:10:07 -0800687 out << ");\n";
688 }).endl().endl();
Yifan Hong42c68432017-03-13 16:22:03 -0700689 } else {
690 out << "// equals() is not generated for " << localName() << "\n";
Yifan Hongec102272016-12-20 18:10:07 -0800691 }
692
693 ////////////////////////////////////////////////////////////////////////////
694
Yifan Honge45b5302017-02-22 10:49:07 -0800695 out << "@Override\npublic final String toString() ";
696 out.block([&] {
697 out << "java.lang.StringBuilder builder = new java.lang.StringBuilder();\n"
698 << "builder.append(\"{\");\n";
699 for (const auto &field : *mFields) {
700 out << "builder.append(\"";
701 if (field != *(mFields->begin())) {
702 out << ", ";
703 }
704 out << "." << field->name() << " = \");\n";
705 field->type().emitJavaDump(out, "builder", "this." + field->name());
706 }
707 out << "builder.append(\"}\");\nreturn builder.toString();\n";
708 }).endl().endl();
709
Martijn Coenenb2a861c2017-04-18 15:54:25 -0700710 size_t structAlign, structSize;
711 getAlignmentAndSize(&structAlign, &structSize);
712
Yifan Honge45b5302017-02-22 10:49:07 -0800713 ////////////////////////////////////////////////////////////////////////////
714
Yifan Hong1af73532016-11-09 14:32:58 -0800715 out << "public final void readFromParcel(android.os.HwParcel parcel) {\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700716 out.indent();
Howard Chenaf084db2017-12-27 18:46:39 +0800717 if (containsInterface()) {
718 for (const auto& field : *mFields) {
719 out << field->name() << " = ";
720 field->type().emitJavaReaderWriter(out, "parcel", field->name(), true);
721 }
722 } else {
723 out << "android.os.HwBlob blob = parcel.readBuffer(";
724 out << structSize << "/* size */);\n";
725 out << "readEmbeddedFromParcel(parcel, blob, 0 /* parentOffset */);\n";
726 }
Andreas Huber85eabdb2016-08-25 11:24:49 -0700727 out.unindent();
728 out << "}\n\n";
729
Andreas Huberf630bc82016-09-09 14:52:25 -0700730 ////////////////////////////////////////////////////////////////////////////
731
Martijn Coenenb2a861c2017-04-18 15:54:25 -0700732 size_t vecAlign, vecSize;
733 VectorType::getAlignmentAndSizeStatic(&vecAlign, &vecSize);
734
Howard Chenaf084db2017-12-27 18:46:39 +0800735 out << "public static final java.util.ArrayList<" << localName()
Yifan Hong1af73532016-11-09 14:32:58 -0800736 << "> readVectorFromParcel(android.os.HwParcel parcel) {\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700737 out.indent();
738
Howard Chenaf084db2017-12-27 18:46:39 +0800739 out << "java.util.ArrayList<" << localName() << "> _hidl_vec = new java.util.ArrayList();\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700740
Howard Chenaf084db2017-12-27 18:46:39 +0800741 if (containsInterface()) {
742 out << "int size = parcel.readInt32();\n";
743 out << "for(int i = 0 ; i < size; i ++) {\n";
744 out.indent();
745 out << fullJavaName() << " tmp = ";
746 emitJavaReaderWriter(out, "parcel", "tmp", true);
747 out << "_hidl_vec.add(tmp);\n";
748 out.unindent();
749 out << "}\n";
750 } else {
751 out << "android.os.HwBlob _hidl_blob = parcel.readBuffer(";
752 out << vecSize << " /* sizeof hidl_vec<T> */);\n\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700753
Howard Chenaf084db2017-12-27 18:46:39 +0800754 VectorType::EmitJavaFieldReaderWriterForElementType(out, 0 /* depth */, this, "parcel",
755 "_hidl_blob", "_hidl_vec", "0",
756 true /* isReader */);
Andreas Huber85eabdb2016-08-25 11:24:49 -0700757 }
Howard Chenaf084db2017-12-27 18:46:39 +0800758 out << "\nreturn _hidl_vec;\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700759 out.unindent();
760 out << "}\n\n";
Howard Chenaf084db2017-12-27 18:46:39 +0800761 ////////////////////////////////////////////////////////////////////////////
762 if (containsInterface()) {
763 out << "// readEmbeddedFromParcel is not generated()\n";
764 } else {
765 out << "public final void readEmbeddedFromParcel(\n";
766 out.indent(2);
767 out << "android.os.HwParcel parcel, android.os.HwBlob _hidl_blob, long _hidl_offset) {\n";
768 out.unindent();
769 size_t offset = 0;
770 for (const auto& field : *mFields) {
771 size_t fieldAlign, fieldSize;
772 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
773
774 size_t pad = offset % fieldAlign;
775 if (pad > 0) {
776 offset += fieldAlign - pad;
777 }
778
779 field->type().emitJavaFieldReaderWriter(
780 out, 0 /* depth */, "parcel", "_hidl_blob", field->name(),
781 "_hidl_offset + " + std::to_string(offset), true /* isReader */);
782 offset += fieldSize;
783 }
784 out.unindent();
785 out << "}\n\n";
786 }
Andreas Huber85eabdb2016-08-25 11:24:49 -0700787
Andreas Huberf630bc82016-09-09 14:52:25 -0700788 ////////////////////////////////////////////////////////////////////////////
789
Yifan Hong1af73532016-11-09 14:32:58 -0800790 out << "public final void writeToParcel(android.os.HwParcel parcel) {\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700791 out.indent();
792
Howard Chenaf084db2017-12-27 18:46:39 +0800793 if (containsInterface()) {
794 for (const auto& field : *mFields) {
795 field->type().emitJavaReaderWriter(out, "parcel", field->name(), false);
796 }
797 } else {
798 out << "android.os.HwBlob _hidl_blob = new android.os.HwBlob(" << structSize
799 << " /* size */);\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700800
Howard Chenaf084db2017-12-27 18:46:39 +0800801 out << "writeEmbeddedToBlob(_hidl_blob, 0 /* parentOffset */);\n"
802 << "parcel.writeBuffer(_hidl_blob);\n";
803 }
Andreas Huber85eabdb2016-08-25 11:24:49 -0700804 out.unindent();
805 out << "}\n\n";
806
Andreas Huberf630bc82016-09-09 14:52:25 -0700807 ////////////////////////////////////////////////////////////////////////////
808
Andreas Huberf630bc82016-09-09 14:52:25 -0700809 out << "public static final void writeVectorToParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700810 out.indent(2);
Howard Chenaf084db2017-12-27 18:46:39 +0800811 out << "android.os.HwParcel parcel, java.util.ArrayList<" << localName() << "> _hidl_vec) {\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700812 out.unindent();
813
Howard Chenaf084db2017-12-27 18:46:39 +0800814 if (containsInterface()) {
815 out << "parcel.writeInt32(_hidl_vec.size());\n";
816 out << "for(" << fullJavaName() << " tmp: _hidl_vec)\n";
817 out.indent();
818 emitJavaReaderWriter(out, "parcel", "tmp", false);
819 out.unindent();
820 } else {
821 out << "android.os.HwBlob _hidl_blob = new android.os.HwBlob(" << vecSize
822 << " /* sizeof(hidl_vec<T>) */);\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700823
Howard Chenaf084db2017-12-27 18:46:39 +0800824 VectorType::EmitJavaFieldReaderWriterForElementType(out, 0 /* depth */, this, "parcel",
825 "_hidl_blob", "_hidl_vec", "0",
826 false /* isReader */);
Andreas Huberf630bc82016-09-09 14:52:25 -0700827
Howard Chenaf084db2017-12-27 18:46:39 +0800828 out << "\nparcel.writeBuffer(_hidl_blob);\n";
829 }
Andreas Huberf630bc82016-09-09 14:52:25 -0700830 out.unindent();
831 out << "}\n\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700832 ////////////////////////////////////////////////////////////////////////////
833
Howard Chenaf084db2017-12-27 18:46:39 +0800834 if (containsInterface()) {
835 out << "// writeEmbeddedFromParcel() is not generated\n";
836 } else {
837 out << "public final void writeEmbeddedToBlob(\n";
838 out.indent(2);
839 out << "android.os.HwBlob _hidl_blob, long _hidl_offset) {\n";
840 out.unindent();
841 size_t offset = 0;
842 for (const auto& field : *mFields) {
843 size_t fieldAlign, fieldSize;
844 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
845 size_t pad = offset % fieldAlign;
846 if (pad > 0) {
847 offset += fieldAlign - pad;
848 }
849 field->type().emitJavaFieldReaderWriter(
850 out, 0 /* depth */, "parcel", "_hidl_blob", field->name(),
851 "_hidl_offset + " + std::to_string(offset), false /* isReader */);
852 offset += fieldSize;
Andreas Huber85eabdb2016-08-25 11:24:49 -0700853 }
854
Howard Chenaf084db2017-12-27 18:46:39 +0800855 out.unindent();
856 out << "}\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700857 }
858
859 out.unindent();
Andreas Huber85eabdb2016-08-25 11:24:49 -0700860 out << "};\n\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700861}
862
Andreas Huber881227d2016-08-02 14:20:21 -0700863void CompoundType::emitStructReaderWriter(
864 Formatter &out, const std::string &prefix, bool isReader) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800865
866 std::string space = prefix.empty() ? "" : (prefix + "::");
867
Andreas Huber881227d2016-08-02 14:20:21 -0700868 out << "::android::status_t "
Yifan Hong244e82d2016-11-11 11:13:57 -0800869 << (isReader ? "readEmbeddedFromParcel"
870 : "writeEmbeddedToParcel")
871 << "(\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700872
Yifan Hong0a68a282016-10-21 16:32:34 -0700873 out.indent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700874
Yifan Hong244e82d2016-11-11 11:13:57 -0800875 bool useName = false;
876 for (const auto &field : *mFields) {
877 if (field->type().useNameInEmitReaderWriterEmbedded(isReader)) {
878 useName = true;
879 break;
880 }
881 }
882 std::string name = useName ? "obj" : "/* obj */";
883 // if not useName, then obj should not be used at all,
884 // then the #error should not be emitted.
885 std::string error = useName ? "" : "\n#error\n";
886
Andreas Huber881227d2016-08-02 14:20:21 -0700887 if (isReader) {
Martijn Coenenb2a861c2017-04-18 15:54:25 -0700888 out << "const " << space << localName() << " &" << name << ",\n";
Andreas Huber8a82ff72016-08-04 10:29:39 -0700889 out << "const ::android::hardware::Parcel &parcel,\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700890 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800891 out << "const " << space << localName() << " &" << name << ",\n";
Andreas Huber8a82ff72016-08-04 10:29:39 -0700892 out << "::android::hardware::Parcel *parcel,\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700893 }
894
895 out << "size_t parentHandle,\n"
896 << "size_t parentOffset)";
897
Andreas Huber881227d2016-08-02 14:20:21 -0700898 out << " {\n";
899
Yifan Hong0a68a282016-10-21 16:32:34 -0700900 out.unindent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700901 out.indent();
902
Iliyan Malchev549e2592016-08-10 08:59:12 -0700903 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700904
905 for (const auto &field : *mFields) {
906 if (!field->type().needsEmbeddedReadWrite()) {
907 continue;
908 }
909
910 field->type().emitReaderWriterEmbedded(
911 out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700912 0 /* depth */,
Martijn Coenenb2a861c2017-04-18 15:54:25 -0700913 name + "." + field->name() + error,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700914 field->name() /* sanitizedName */,
Andreas Huber881227d2016-08-02 14:20:21 -0700915 false /* nameIsPointer */,
916 "parcel",
917 !isReader /* parcelObjIsPointer */,
918 isReader,
Andreas Huber737080b2016-08-02 15:38:04 -0700919 ErrorMode_Return,
Andreas Huber881227d2016-08-02 14:20:21 -0700920 "parentHandle",
Andreas Huber0e00de42016-08-03 09:56:02 -0700921 "parentOffset + offsetof("
922 + fullName()
923 + ", "
924 + field->name()
925 + ")");
Andreas Huber881227d2016-08-02 14:20:21 -0700926 }
927
Iliyan Malchev549e2592016-08-10 08:59:12 -0700928 out << "return _hidl_err;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700929
930 out.unindent();
931 out << "}\n\n";
932}
933
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -0700934void CompoundType::emitResolveReferenceDef(Formatter& out, const std::string& prefix,
935 bool isReader) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800936 out << "::android::status_t ";
937 const std::string space(prefix.empty() ? "" : (prefix + "::"));
Yifan Hong00f47172016-09-30 14:40:45 -0700938
939 bool useParent = false;
940 for (const auto &field : *mFields) {
941 if (field->type().useParentInEmitResolveReferencesEmbedded()) {
942 useParent = true;
943 break;
944 }
945 }
946
947 std::string parentHandleName = useParent ? "parentHandle" : "/* parentHandle */";
948 std::string parentOffsetName = useParent ? "parentOffset" : "/* parentOffset */";
949
Yifan Hongbf459bc2016-08-23 16:50:37 -0700950 if (isReader) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800951 out << "readEmbeddedReferenceFromParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700952 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800953 out << space + localName() + " *obj,\n"
954 << "const ::android::hardware::Parcel &parcel,\n"
Yifan Hong00f47172016-09-30 14:40:45 -0700955 << "size_t " << parentHandleName << ", "
956 << "size_t " << parentOffsetName << ")\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700957 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700958 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800959 out << "writeEmbeddedReferenceToParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700960 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800961 out << "const " << space + localName() + " &obj,\n"
962 << "::android::hardware::Parcel *parcel,\n"
Yifan Hong00f47172016-09-30 14:40:45 -0700963 << "size_t " << parentHandleName << ", "
Yifan Hong244e82d2016-11-11 11:13:57 -0800964 << "size_t " << parentOffsetName << ")\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700965 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700966 }
967
968 out << " {\n";
969
970 out.indent();
971
972 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
973
Yifan Hong244e82d2016-11-11 11:13:57 -0800974 const std::string nameDeref(isReader ? "obj->" : "obj.");
Yifan Hong00f47172016-09-30 14:40:45 -0700975 // if not useParent, then parentName and offsetText
976 // should not be used at all, then the #error should not be emitted.
977 std::string error = useParent ? "" : "\n#error\n";
978
Yifan Hongbf459bc2016-08-23 16:50:37 -0700979 for (const auto &field : *mFields) {
980 if (!field->type().needsResolveReferences()) {
981 continue;
982 }
983
984 field->type().emitResolveReferencesEmbedded(
985 out,
986 0 /* depth */,
Yifan Hong244e82d2016-11-11 11:13:57 -0800987 nameDeref + field->name(),
Yifan Hongbf459bc2016-08-23 16:50:37 -0700988 field->name() /* sanitizedName */,
989 false, // nameIsPointer
990 "parcel", // const std::string &parcelObj,
991 !isReader, // bool parcelObjIsPointer,
992 isReader, // bool isReader,
993 ErrorMode_Return,
Yifan Hong00f47172016-09-30 14:40:45 -0700994 parentHandleName + error,
995 parentOffsetName
996 + " + offsetof("
Yifan Hongbf459bc2016-08-23 16:50:37 -0700997 + fullName()
998 + ", "
999 + field->name()
Yifan Hong00f47172016-09-30 14:40:45 -07001000 + ")"
1001 + error);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001002 }
1003
Yifan Hongbf459bc2016-08-23 16:50:37 -07001004 out << "return _hidl_err;\n";
1005
1006 out.unindent();
1007 out << "}\n\n";
1008}
1009
Andreas Huber881227d2016-08-02 14:20:21 -07001010bool CompoundType::needsEmbeddedReadWrite() const {
1011 if (mStyle != STYLE_STRUCT) {
1012 return false;
1013 }
1014
1015 for (const auto &field : *mFields) {
1016 if (field->type().needsEmbeddedReadWrite()) {
1017 return true;
1018 }
1019 }
1020
1021 return false;
1022}
1023
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001024bool CompoundType::deepNeedsResolveReferences(std::unordered_set<const Type*>* visited) const {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001025 if (mStyle != STYLE_STRUCT) {
1026 return false;
1027 }
1028
1029 for (const auto &field : *mFields) {
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001030 if (field->type().needsResolveReferences(visited)) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001031 return true;
1032 }
1033 }
1034
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001035 return Scope::deepNeedsResolveReferences(visited);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001036}
1037
Andreas Huber881227d2016-08-02 14:20:21 -07001038bool CompoundType::resultNeedsDeref() const {
Howard Chenecfb4512017-11-21 18:28:53 +08001039 return !containsInterface() ;
Andreas Huber881227d2016-08-02 14:20:21 -07001040}
1041
Steven Moreland6ec9eb92018-02-16 14:21:49 -08001042void CompoundType::emitVtsTypeDeclarations(Formatter& out) const {
Zhuoyao Zhangbf828c82016-10-26 14:15:10 -07001043 out << "name: \"" << fullName() << "\"\n";
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -07001044 out << "type: " << getVtsType() << "\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001045
1046 // Emit declaration for each subtype.
1047 for (const auto &type : getSubTypes()) {
1048 switch (mStyle) {
1049 case STYLE_STRUCT:
1050 {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -07001051 out << "sub_struct: {\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001052 break;
1053 }
1054 case STYLE_UNION:
1055 {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -07001056 out << "sub_union: {\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001057 break;
1058 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001059 }
1060 out.indent();
Steven Moreland6ec9eb92018-02-16 14:21:49 -08001061 type->emitVtsTypeDeclarations(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001062 out.unindent();
1063 out << "}\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07001064 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001065
1066 // Emit declaration for each field.
1067 for (const auto &field : *mFields) {
1068 switch (mStyle) {
1069 case STYLE_STRUCT:
1070 {
1071 out << "struct_value: {\n";
1072 break;
1073 }
1074 case STYLE_UNION:
1075 {
1076 out << "union_value: {\n";
1077 break;
1078 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001079 }
1080 out.indent();
1081 out << "name: \"" << field->name() << "\"\n";
Steven Moreland6ec9eb92018-02-16 14:21:49 -08001082 field->type().emitVtsAttributeType(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001083 out.unindent();
1084 out << "}\n";
1085 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07001086}
1087
Steven Moreland6ec9eb92018-02-16 14:21:49 -08001088void CompoundType::emitVtsAttributeType(Formatter& out) const {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -07001089 out << "type: " << getVtsType() << "\n";
Zhuoyao Zhangbf828c82016-10-26 14:15:10 -07001090 out << "predefined_type: \"" << fullName() << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07001091}
1092
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001093bool CompoundType::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
1094 if (mStyle != STYLE_STRUCT) {
Andreas Huber0fa9e392016-08-31 09:05:44 -07001095 return false;
1096 }
1097
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001098 for (const auto* field : *mFields) {
1099 if (!field->get()->isJavaCompatible(visited)) {
Andreas Huber0fa9e392016-08-31 09:05:44 -07001100 return false;
1101 }
1102 }
1103
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001104 return Scope::deepIsJavaCompatible(visited);
Andreas Huber85eabdb2016-08-25 11:24:49 -07001105}
1106
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001107bool CompoundType::deepContainsPointer(std::unordered_set<const Type*>* visited) const {
1108 for (const auto* field : *mFields) {
1109 if (field->get()->containsPointer(visited)) {
Andreas Huber60d3b222017-03-30 09:10:56 -07001110 return true;
1111 }
1112 }
1113
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001114 return Scope::deepContainsPointer(visited);
Andreas Huber60d3b222017-03-30 09:10:56 -07001115}
1116
Andreas Huber85eabdb2016-08-25 11:24:49 -07001117void CompoundType::getAlignmentAndSize(size_t *align, size_t *size) const {
1118 *align = 1;
Andreas Huber60d3b222017-03-30 09:10:56 -07001119 *size = 0;
Andreas Huber85eabdb2016-08-25 11:24:49 -07001120
1121 size_t offset = 0;
1122 for (const auto &field : *mFields) {
1123 // Each field is aligned according to its alignment requirement.
1124 // The surrounding structure's alignment is the maximum of its
1125 // fields' aligments.
1126
1127 size_t fieldAlign, fieldSize;
1128 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
1129
1130 size_t pad = offset % fieldAlign;
1131 if (pad > 0) {
1132 offset += fieldAlign - pad;
1133 }
1134
Andreas Huber60d3b222017-03-30 09:10:56 -07001135 if (mStyle == STYLE_STRUCT) {
1136 offset += fieldSize;
1137 } else {
1138 *size = std::max(*size, fieldSize);
1139 }
Andreas Huber85eabdb2016-08-25 11:24:49 -07001140
1141 if (fieldAlign > (*align)) {
1142 *align = fieldAlign;
1143 }
1144 }
1145
Andreas Huber60d3b222017-03-30 09:10:56 -07001146 if (mStyle == STYLE_STRUCT) {
1147 *size = offset;
Andreas Huber85eabdb2016-08-25 11:24:49 -07001148 }
1149
Andreas Huber60d3b222017-03-30 09:10:56 -07001150 // Final padding to account for the structure's alignment.
1151 size_t pad = (*size) % (*align);
1152 if (pad > 0) {
1153 (*size) += (*align) - pad;
1154 }
Andreas Huberca4bc892017-01-09 14:58:12 -08001155
1156 if (*size == 0) {
1157 // An empty struct still occupies a byte of space in C++.
1158 *size = 1;
1159 }
Andreas Huber70a59e12016-08-16 12:57:01 -07001160}
1161
Andreas Huberc9410c72016-07-28 12:18:40 -07001162} // namespace android
1163