blob: c019edb141128d9563b6764fc298de0e06a85a20 [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
Andreas Huber881227d2016-08-02 14:20:21 -0700377status_t CompoundType::emitTypeDeclarations(Formatter &out) const {
378 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
398 return OK;
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 return OK;
456}
457
Timur Iskhakovfd3f2502017-09-05 16:25:02 -0700458void CompoundType::emitTypeForwardDeclaration(Formatter& out) const {
459 out << ((mStyle == STYLE_STRUCT) ? "struct" : "union") << " " << localName() << ";\n";
460}
Yifan Hong244e82d2016-11-11 11:13:57 -0800461
Steven Moreland4b8f7a12017-11-17 15:39:54 -0800462status_t CompoundType::emitPackageTypeDeclarations(Formatter& out) const {
463 Scope::emitPackageTypeDeclarations(out);
Yifan Hongc6752dc2016-12-20 14:00:14 -0800464
Steven Morelandbf714212017-10-27 18:29:01 -0700465 // TODO(b/65200821): remove these ifdefs
466 out << "#ifdef REALLY_IS_HIDL_INTERNAL_LIB" << gCurrentCompileName << "\n";
Steven Moreland3d98bc42017-06-23 21:36:41 +0000467 out << "std::string toString("
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800468 << getCppArgumentType()
Steven Moreland3d98bc42017-06-23 21:36:41 +0000469 << ");\n\n";
Yifan Hong42c68432017-03-13 16:22:03 -0700470 if (canCheckEquality()) {
Steven Morelandf91048a2017-06-23 21:38:35 +0000471 out << "bool operator==("
472 << getCppArgumentType() << ", " << getCppArgumentType() << ");\n\n";
Yifan Hong42c68432017-03-13 16:22:03 -0700473
Steven Morelandf91048a2017-06-23 21:38:35 +0000474 out << "bool operator!=("
475 << getCppArgumentType() << ", " << getCppArgumentType() << ");\n\n";
Steven Morelandbf714212017-10-27 18:29:01 -0700476 }
477 out << "#else\n";
478 out << "static inline std::string toString("
479 << getCppArgumentType()
480 << (mFields->empty() ? "" : " o")
481 << ") ";
482
483 out.block([&] {
484 // include toString for scalar types
485 out << "using ::android::hardware::toString;\n"
486 << "std::string os;\n";
487 out << "os += \"{\";\n";
488
489 for (const NamedReference<Type>* field : *mFields) {
490 out << "os += \"";
491 if (field != *(mFields->begin())) {
492 out << ", ";
493 }
494 out << "." << field->name() << " = \";\n";
495 field->type().emitDump(out, "os", "o." + field->name());
496 }
497
498 out << "os += \"}\"; return os;\n";
499 }).endl().endl();
500
501 if (canCheckEquality()) {
502 out << "static inline bool operator==("
503 << getCppArgumentType() << " " << (mFields->empty() ? "/* lhs */" : "lhs") << ", "
504 << getCppArgumentType() << " " << (mFields->empty() ? "/* rhs */" : "rhs") << ") ";
505 out.block([&] {
506 for (const auto &field : *mFields) {
507 out.sIf("lhs." + field->name() + " != rhs." + field->name(), [&] {
508 out << "return false;\n";
509 }).endl();
510 }
511 out << "return true;\n";
512 }).endl().endl();
513
514 out << "static inline bool operator!=("
515 << getCppArgumentType() << " lhs," << getCppArgumentType() << " rhs)";
516 out.block([&] {
517 out << "return !(lhs == rhs);\n";
518 }).endl().endl();
Yifan Hong42c68432017-03-13 16:22:03 -0700519 } else {
Steven Morelandf91048a2017-06-23 21:38:35 +0000520 out << "// operator== and operator!= are not generated for " << localName() << "\n\n";
Yifan Hongc6752dc2016-12-20 14:00:14 -0800521 }
Steven Morelandbf714212017-10-27 18:29:01 -0700522 out << "#endif // REALLY_IS_HIDL_INTERNAL_LIB\n";
Yifan Hongc6752dc2016-12-20 14:00:14 -0800523
Yifan Hongc6752dc2016-12-20 14:00:14 -0800524 return OK;
525}
526
Steven Moreland4b8f7a12017-11-17 15:39:54 -0800527status_t CompoundType::emitPackageHwDeclarations(Formatter& out) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700528 if (needsEmbeddedReadWrite()) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800529 out << "::android::status_t readEmbeddedFromParcel(\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700530
Yifan Hong0a68a282016-10-21 16:32:34 -0700531 out.indent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700532
Martijn Coenenb2a861c2017-04-18 15:54:25 -0700533 out << "const " << fullName() << " &obj,\n"
Yifan Hong244e82d2016-11-11 11:13:57 -0800534 << "const ::android::hardware::Parcel &parcel,\n"
535 << "size_t parentHandle,\n"
536 << "size_t parentOffset);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700537
Yifan Hong0a68a282016-10-21 16:32:34 -0700538 out.unindent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700539
540 out << "::android::status_t writeEmbeddedToParcel(\n";
541
Yifan Hong0a68a282016-10-21 16:32:34 -0700542 out.indent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700543
Yifan Hong244e82d2016-11-11 11:13:57 -0800544 out << "const " << fullName() << " &obj,\n"
545 << "::android::hardware::Parcel *parcel,\n"
546 << "size_t parentHandle,\n"
547 << "size_t parentOffset);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700548
Yifan Hong0a68a282016-10-21 16:32:34 -0700549 out.unindent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700550 }
551
Yifan Hongbf459bc2016-08-23 16:50:37 -0700552 if(needsResolveReferences()) {
Yifan Hongbf459bc2016-08-23 16:50:37 -0700553 out << "::android::status_t readEmbeddedReferenceFromParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700554 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800555 out << fullName() << " *obj,\n"
556 << "const ::android::hardware::Parcel &parcel,\n"
557 << "size_t parentHandle, size_t parentOffset);\n\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700558 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700559 out << "::android::status_t writeEmbeddedReferenceToParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700560 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800561 out << "const " << fullName() << " &obj,\n"
562 << "::android::hardware::Parcel *,\n"
563 << "size_t parentHandle, size_t parentOffset);\n\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700564 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700565 }
566
Andreas Huber881227d2016-08-02 14:20:21 -0700567 return OK;
568}
569
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -0700570status_t CompoundType::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
Steven Morelandb158de12016-10-10 11:59:09 -0700571 std::string space = prefix.empty() ? "" : (prefix + "::");
572 status_t err = Scope::emitTypeDefinitions(out, space + localName());
Andreas Huber881227d2016-08-02 14:20:21 -0700573
574 if (err != OK) {
575 return err;
576 }
577
Yifan Hongbf459bc2016-08-23 16:50:37 -0700578 if (needsEmbeddedReadWrite()) {
579 emitStructReaderWriter(out, prefix, true /* isReader */);
580 emitStructReaderWriter(out, prefix, false /* isReader */);
Andreas Huber881227d2016-08-02 14:20:21 -0700581 }
582
Yifan Hongbf459bc2016-08-23 16:50:37 -0700583 if (needsResolveReferences()) {
584 emitResolveReferenceDef(out, prefix, true /* isReader */);
585 emitResolveReferenceDef(out, prefix, false /* isReader */);
586 }
Andreas Huber881227d2016-08-02 14:20:21 -0700587
Steven Morelandbf714212017-10-27 18:29:01 -0700588 // TODO(b/65200821): remove toString + operator== from .cpp once all prebuilts are rebuilt
589
Steven Moreland3d98bc42017-06-23 21:36:41 +0000590 out << "std::string toString("
591 << getCppArgumentType()
592 << (mFields->empty() ? "" : " o")
593 << ") ";
594
595 out.block([&] {
596 // include toString for scalar types
597 out << "using ::android::hardware::toString;\n"
598 << "std::string os;\n";
599 out << "os += \"{\";\n";
600
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700601 for (const NamedReference<Type>* field : *mFields) {
Steven Moreland3d98bc42017-06-23 21:36:41 +0000602 out << "os += \"";
603 if (field != *(mFields->begin())) {
604 out << ", ";
605 }
606 out << "." << field->name() << " = \";\n";
607 field->type().emitDump(out, "os", "o." + field->name());
608 }
609
610 out << "os += \"}\"; return os;\n";
611 }).endl().endl();
612
Steven Morelandf91048a2017-06-23 21:38:35 +0000613 if (canCheckEquality()) {
614 out << "bool operator==("
615 << getCppArgumentType() << " " << (mFields->empty() ? "/* lhs */" : "lhs") << ", "
616 << getCppArgumentType() << " " << (mFields->empty() ? "/* rhs */" : "rhs") << ") ";
617 out.block([&] {
618 for (const auto &field : *mFields) {
619 out.sIf("lhs." + field->name() + " != rhs." + field->name(), [&] {
620 out << "return false;\n";
621 }).endl();
622 }
623 out << "return true;\n";
624 }).endl().endl();
625
626 out << "bool operator!=("
627 << getCppArgumentType() << " lhs," << getCppArgumentType() << " rhs)";
628 out.block([&] {
629 out << "return !(lhs == rhs);\n";
630 }).endl().endl();
631 } else {
632 out << "// operator== and operator!= are not generated for " << localName() << "\n";
633 }
634
Andreas Huber881227d2016-08-02 14:20:21 -0700635 return OK;
636}
637
Andreas Huber85eabdb2016-08-25 11:24:49 -0700638status_t CompoundType::emitJavaTypeDeclarations(
639 Formatter &out, bool atTopLevel) const {
640 out << "public final ";
641
642 if (!atTopLevel) {
643 out << "static ";
644 }
645
646 out << "class "
647 << localName()
648 << " {\n";
649
650 out.indent();
651
652 Scope::emitJavaTypeDeclarations(out, false /* atTopLevel */);
653
654 for (const auto &field : *mFields) {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700655 out << "public ";
656
657 field->type().emitJavaFieldInitializer(out, field->name());
658 }
659
660 if (!mFields->empty()) {
661 out << "\n";
662 }
663
Yifan Hongec102272016-12-20 18:10:07 -0800664 ////////////////////////////////////////////////////////////////////////////
665
666 if (canCheckEquality()) {
Yifan Hong7d1839f2017-02-22 13:24:29 -0800667 out << "@Override\npublic final boolean equals(Object otherObject) ";
Yifan Hongec102272016-12-20 18:10:07 -0800668 out.block([&] {
Yifan Hong7d1839f2017-02-22 13:24:29 -0800669 out.sIf("this == otherObject", [&] {
670 out << "return true;\n";
671 }).endl();
672 out.sIf("otherObject == null", [&] {
673 out << "return false;\n";
674 }).endl();
Yifan Hong45b331b2017-03-27 12:59:54 -0700675 // Though class is final, we use getClass instead of instanceof to be explicit.
676 out.sIf("otherObject.getClass() != " + fullJavaName() + ".class", [&] {
Yifan Hong7d1839f2017-02-22 13:24:29 -0800677 out << "return false;\n";
678 }).endl();
679 out << fullJavaName() << " other = (" << fullJavaName() << ")otherObject;\n";
Yifan Hongec102272016-12-20 18:10:07 -0800680 for (const auto &field : *mFields) {
Yifan Hong45b331b2017-03-27 12:59:54 -0700681 std::string condition = (field->type().isScalar() || field->type().isEnum())
Yifan Hongec102272016-12-20 18:10:07 -0800682 ? "this." + field->name() + " != other." + field->name()
Yifan Hong45b331b2017-03-27 12:59:54 -0700683 : ("!android.os.HidlSupport.deepEquals(this." + field->name()
Yifan Hongec102272016-12-20 18:10:07 -0800684 + ", other." + field->name() + ")");
685 out.sIf(condition, [&] {
686 out << "return false;\n";
687 }).endl();
688 }
689 out << "return true;\n";
690 }).endl().endl();
691
Yifan Hong7d1839f2017-02-22 13:24:29 -0800692 out << "@Override\npublic final int hashCode() ";
Yifan Hongec102272016-12-20 18:10:07 -0800693 out.block([&] {
Yifan Hong7d1839f2017-02-22 13:24:29 -0800694 out << "return java.util.Objects.hash(\n";
695 out.indent(2, [&] {
Yifan Hong932464e2017-03-30 15:40:22 -0700696 out.join(mFields->begin(), mFields->end(), ", \n", [&] (const auto &field) {
Yifan Hong45b331b2017-03-27 12:59:54 -0700697 out << "android.os.HidlSupport.deepHashCode(this." << field->name() << ")";
Yifan Hong932464e2017-03-30 15:40:22 -0700698 });
Yifan Hong7d1839f2017-02-22 13:24:29 -0800699 });
Yifan Hongec102272016-12-20 18:10:07 -0800700 out << ");\n";
701 }).endl().endl();
Yifan Hong42c68432017-03-13 16:22:03 -0700702 } else {
703 out << "// equals() is not generated for " << localName() << "\n";
Yifan Hongec102272016-12-20 18:10:07 -0800704 }
705
706 ////////////////////////////////////////////////////////////////////////////
707
Yifan Honge45b5302017-02-22 10:49:07 -0800708 out << "@Override\npublic final String toString() ";
709 out.block([&] {
710 out << "java.lang.StringBuilder builder = new java.lang.StringBuilder();\n"
711 << "builder.append(\"{\");\n";
712 for (const auto &field : *mFields) {
713 out << "builder.append(\"";
714 if (field != *(mFields->begin())) {
715 out << ", ";
716 }
717 out << "." << field->name() << " = \");\n";
718 field->type().emitJavaDump(out, "builder", "this." + field->name());
719 }
720 out << "builder.append(\"}\");\nreturn builder.toString();\n";
721 }).endl().endl();
722
Martijn Coenenb2a861c2017-04-18 15:54:25 -0700723 size_t structAlign, structSize;
724 getAlignmentAndSize(&structAlign, &structSize);
725
Yifan Honge45b5302017-02-22 10:49:07 -0800726 ////////////////////////////////////////////////////////////////////////////
727
Yifan Hong1af73532016-11-09 14:32:58 -0800728 out << "public final void readFromParcel(android.os.HwParcel parcel) {\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700729 out.indent();
Howard Chenaf084db2017-12-27 18:46:39 +0800730 if (containsInterface()) {
731 for (const auto& field : *mFields) {
732 out << field->name() << " = ";
733 field->type().emitJavaReaderWriter(out, "parcel", field->name(), true);
734 }
735 } else {
736 out << "android.os.HwBlob blob = parcel.readBuffer(";
737 out << structSize << "/* size */);\n";
738 out << "readEmbeddedFromParcel(parcel, blob, 0 /* parentOffset */);\n";
739 }
Andreas Huber85eabdb2016-08-25 11:24:49 -0700740 out.unindent();
741 out << "}\n\n";
742
Andreas Huberf630bc82016-09-09 14:52:25 -0700743 ////////////////////////////////////////////////////////////////////////////
744
Martijn Coenenb2a861c2017-04-18 15:54:25 -0700745 size_t vecAlign, vecSize;
746 VectorType::getAlignmentAndSizeStatic(&vecAlign, &vecSize);
747
Howard Chenaf084db2017-12-27 18:46:39 +0800748 out << "public static final java.util.ArrayList<" << localName()
Yifan Hong1af73532016-11-09 14:32:58 -0800749 << "> readVectorFromParcel(android.os.HwParcel parcel) {\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700750 out.indent();
751
Howard Chenaf084db2017-12-27 18:46:39 +0800752 out << "java.util.ArrayList<" << localName() << "> _hidl_vec = new java.util.ArrayList();\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700753
Howard Chenaf084db2017-12-27 18:46:39 +0800754 if (containsInterface()) {
755 out << "int size = parcel.readInt32();\n";
756 out << "for(int i = 0 ; i < size; i ++) {\n";
757 out.indent();
758 out << fullJavaName() << " tmp = ";
759 emitJavaReaderWriter(out, "parcel", "tmp", true);
760 out << "_hidl_vec.add(tmp);\n";
761 out.unindent();
762 out << "}\n";
763 } else {
764 out << "android.os.HwBlob _hidl_blob = parcel.readBuffer(";
765 out << vecSize << " /* sizeof hidl_vec<T> */);\n\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700766
Howard Chenaf084db2017-12-27 18:46:39 +0800767 VectorType::EmitJavaFieldReaderWriterForElementType(out, 0 /* depth */, this, "parcel",
768 "_hidl_blob", "_hidl_vec", "0",
769 true /* isReader */);
Andreas Huber85eabdb2016-08-25 11:24:49 -0700770 }
Howard Chenaf084db2017-12-27 18:46:39 +0800771 out << "\nreturn _hidl_vec;\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700772 out.unindent();
773 out << "}\n\n";
Howard Chenaf084db2017-12-27 18:46:39 +0800774 ////////////////////////////////////////////////////////////////////////////
775 if (containsInterface()) {
776 out << "// readEmbeddedFromParcel is not generated()\n";
777 } else {
778 out << "public final void readEmbeddedFromParcel(\n";
779 out.indent(2);
780 out << "android.os.HwParcel parcel, android.os.HwBlob _hidl_blob, long _hidl_offset) {\n";
781 out.unindent();
782 size_t offset = 0;
783 for (const auto& field : *mFields) {
784 size_t fieldAlign, fieldSize;
785 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
786
787 size_t pad = offset % fieldAlign;
788 if (pad > 0) {
789 offset += fieldAlign - pad;
790 }
791
792 field->type().emitJavaFieldReaderWriter(
793 out, 0 /* depth */, "parcel", "_hidl_blob", field->name(),
794 "_hidl_offset + " + std::to_string(offset), true /* isReader */);
795 offset += fieldSize;
796 }
797 out.unindent();
798 out << "}\n\n";
799 }
Andreas Huber85eabdb2016-08-25 11:24:49 -0700800
Andreas Huberf630bc82016-09-09 14:52:25 -0700801 ////////////////////////////////////////////////////////////////////////////
802
Yifan Hong1af73532016-11-09 14:32:58 -0800803 out << "public final void writeToParcel(android.os.HwParcel parcel) {\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700804 out.indent();
805
Howard Chenaf084db2017-12-27 18:46:39 +0800806 if (containsInterface()) {
807 for (const auto& field : *mFields) {
808 field->type().emitJavaReaderWriter(out, "parcel", field->name(), false);
809 }
810 } else {
811 out << "android.os.HwBlob _hidl_blob = new android.os.HwBlob(" << structSize
812 << " /* size */);\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700813
Howard Chenaf084db2017-12-27 18:46:39 +0800814 out << "writeEmbeddedToBlob(_hidl_blob, 0 /* parentOffset */);\n"
815 << "parcel.writeBuffer(_hidl_blob);\n";
816 }
Andreas Huber85eabdb2016-08-25 11:24:49 -0700817 out.unindent();
818 out << "}\n\n";
819
Andreas Huberf630bc82016-09-09 14:52:25 -0700820 ////////////////////////////////////////////////////////////////////////////
821
Andreas Huberf630bc82016-09-09 14:52:25 -0700822 out << "public static final void writeVectorToParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700823 out.indent(2);
Howard Chenaf084db2017-12-27 18:46:39 +0800824 out << "android.os.HwParcel parcel, java.util.ArrayList<" << localName() << "> _hidl_vec) {\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700825 out.unindent();
826
Howard Chenaf084db2017-12-27 18:46:39 +0800827 if (containsInterface()) {
828 out << "parcel.writeInt32(_hidl_vec.size());\n";
829 out << "for(" << fullJavaName() << " tmp: _hidl_vec)\n";
830 out.indent();
831 emitJavaReaderWriter(out, "parcel", "tmp", false);
832 out.unindent();
833 } else {
834 out << "android.os.HwBlob _hidl_blob = new android.os.HwBlob(" << vecSize
835 << " /* sizeof(hidl_vec<T>) */);\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700836
Howard Chenaf084db2017-12-27 18:46:39 +0800837 VectorType::EmitJavaFieldReaderWriterForElementType(out, 0 /* depth */, this, "parcel",
838 "_hidl_blob", "_hidl_vec", "0",
839 false /* isReader */);
Andreas Huberf630bc82016-09-09 14:52:25 -0700840
Howard Chenaf084db2017-12-27 18:46:39 +0800841 out << "\nparcel.writeBuffer(_hidl_blob);\n";
842 }
Andreas Huberf630bc82016-09-09 14:52:25 -0700843 out.unindent();
844 out << "}\n\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700845 ////////////////////////////////////////////////////////////////////////////
846
Howard Chenaf084db2017-12-27 18:46:39 +0800847 if (containsInterface()) {
848 out << "// writeEmbeddedFromParcel() is not generated\n";
849 } else {
850 out << "public final void writeEmbeddedToBlob(\n";
851 out.indent(2);
852 out << "android.os.HwBlob _hidl_blob, long _hidl_offset) {\n";
853 out.unindent();
854 size_t offset = 0;
855 for (const auto& field : *mFields) {
856 size_t fieldAlign, fieldSize;
857 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
858 size_t pad = offset % fieldAlign;
859 if (pad > 0) {
860 offset += fieldAlign - pad;
861 }
862 field->type().emitJavaFieldReaderWriter(
863 out, 0 /* depth */, "parcel", "_hidl_blob", field->name(),
864 "_hidl_offset + " + std::to_string(offset), false /* isReader */);
865 offset += fieldSize;
Andreas Huber85eabdb2016-08-25 11:24:49 -0700866 }
867
Howard Chenaf084db2017-12-27 18:46:39 +0800868 out.unindent();
869 out << "}\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700870 }
871
872 out.unindent();
Andreas Huber85eabdb2016-08-25 11:24:49 -0700873 out << "};\n\n";
874
875 return OK;
876}
877
Andreas Huber881227d2016-08-02 14:20:21 -0700878void CompoundType::emitStructReaderWriter(
879 Formatter &out, const std::string &prefix, bool isReader) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800880
881 std::string space = prefix.empty() ? "" : (prefix + "::");
882
Andreas Huber881227d2016-08-02 14:20:21 -0700883 out << "::android::status_t "
Yifan Hong244e82d2016-11-11 11:13:57 -0800884 << (isReader ? "readEmbeddedFromParcel"
885 : "writeEmbeddedToParcel")
886 << "(\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700887
Yifan Hong0a68a282016-10-21 16:32:34 -0700888 out.indent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700889
Yifan Hong244e82d2016-11-11 11:13:57 -0800890 bool useName = false;
891 for (const auto &field : *mFields) {
892 if (field->type().useNameInEmitReaderWriterEmbedded(isReader)) {
893 useName = true;
894 break;
895 }
896 }
897 std::string name = useName ? "obj" : "/* obj */";
898 // if not useName, then obj should not be used at all,
899 // then the #error should not be emitted.
900 std::string error = useName ? "" : "\n#error\n";
901
Andreas Huber881227d2016-08-02 14:20:21 -0700902 if (isReader) {
Martijn Coenenb2a861c2017-04-18 15:54:25 -0700903 out << "const " << space << localName() << " &" << name << ",\n";
Andreas Huber8a82ff72016-08-04 10:29:39 -0700904 out << "const ::android::hardware::Parcel &parcel,\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700905 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800906 out << "const " << space << localName() << " &" << name << ",\n";
Andreas Huber8a82ff72016-08-04 10:29:39 -0700907 out << "::android::hardware::Parcel *parcel,\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700908 }
909
910 out << "size_t parentHandle,\n"
911 << "size_t parentOffset)";
912
Andreas Huber881227d2016-08-02 14:20:21 -0700913 out << " {\n";
914
Yifan Hong0a68a282016-10-21 16:32:34 -0700915 out.unindent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700916 out.indent();
917
Iliyan Malchev549e2592016-08-10 08:59:12 -0700918 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700919
920 for (const auto &field : *mFields) {
921 if (!field->type().needsEmbeddedReadWrite()) {
922 continue;
923 }
924
925 field->type().emitReaderWriterEmbedded(
926 out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700927 0 /* depth */,
Martijn Coenenb2a861c2017-04-18 15:54:25 -0700928 name + "." + field->name() + error,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700929 field->name() /* sanitizedName */,
Andreas Huber881227d2016-08-02 14:20:21 -0700930 false /* nameIsPointer */,
931 "parcel",
932 !isReader /* parcelObjIsPointer */,
933 isReader,
Andreas Huber737080b2016-08-02 15:38:04 -0700934 ErrorMode_Return,
Andreas Huber881227d2016-08-02 14:20:21 -0700935 "parentHandle",
Andreas Huber0e00de42016-08-03 09:56:02 -0700936 "parentOffset + offsetof("
937 + fullName()
938 + ", "
939 + field->name()
940 + ")");
Andreas Huber881227d2016-08-02 14:20:21 -0700941 }
942
Iliyan Malchev549e2592016-08-10 08:59:12 -0700943 out << "return _hidl_err;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700944
945 out.unindent();
946 out << "}\n\n";
947}
948
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -0700949void CompoundType::emitResolveReferenceDef(Formatter& out, const std::string& prefix,
950 bool isReader) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800951 out << "::android::status_t ";
952 const std::string space(prefix.empty() ? "" : (prefix + "::"));
Yifan Hong00f47172016-09-30 14:40:45 -0700953
954 bool useParent = false;
955 for (const auto &field : *mFields) {
956 if (field->type().useParentInEmitResolveReferencesEmbedded()) {
957 useParent = true;
958 break;
959 }
960 }
961
962 std::string parentHandleName = useParent ? "parentHandle" : "/* parentHandle */";
963 std::string parentOffsetName = useParent ? "parentOffset" : "/* parentOffset */";
964
Yifan Hongbf459bc2016-08-23 16:50:37 -0700965 if (isReader) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800966 out << "readEmbeddedReferenceFromParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700967 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800968 out << space + localName() + " *obj,\n"
969 << "const ::android::hardware::Parcel &parcel,\n"
Yifan Hong00f47172016-09-30 14:40:45 -0700970 << "size_t " << parentHandleName << ", "
971 << "size_t " << parentOffsetName << ")\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700972 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700973 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800974 out << "writeEmbeddedReferenceToParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700975 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800976 out << "const " << space + localName() + " &obj,\n"
977 << "::android::hardware::Parcel *parcel,\n"
Yifan Hong00f47172016-09-30 14:40:45 -0700978 << "size_t " << parentHandleName << ", "
Yifan Hong244e82d2016-11-11 11:13:57 -0800979 << "size_t " << parentOffsetName << ")\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700980 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700981 }
982
983 out << " {\n";
984
985 out.indent();
986
987 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
988
Yifan Hong244e82d2016-11-11 11:13:57 -0800989 const std::string nameDeref(isReader ? "obj->" : "obj.");
Yifan Hong00f47172016-09-30 14:40:45 -0700990 // if not useParent, then parentName and offsetText
991 // should not be used at all, then the #error should not be emitted.
992 std::string error = useParent ? "" : "\n#error\n";
993
Yifan Hongbf459bc2016-08-23 16:50:37 -0700994 for (const auto &field : *mFields) {
995 if (!field->type().needsResolveReferences()) {
996 continue;
997 }
998
999 field->type().emitResolveReferencesEmbedded(
1000 out,
1001 0 /* depth */,
Yifan Hong244e82d2016-11-11 11:13:57 -08001002 nameDeref + field->name(),
Yifan Hongbf459bc2016-08-23 16:50:37 -07001003 field->name() /* sanitizedName */,
1004 false, // nameIsPointer
1005 "parcel", // const std::string &parcelObj,
1006 !isReader, // bool parcelObjIsPointer,
1007 isReader, // bool isReader,
1008 ErrorMode_Return,
Yifan Hong00f47172016-09-30 14:40:45 -07001009 parentHandleName + error,
1010 parentOffsetName
1011 + " + offsetof("
Yifan Hongbf459bc2016-08-23 16:50:37 -07001012 + fullName()
1013 + ", "
1014 + field->name()
Yifan Hong00f47172016-09-30 14:40:45 -07001015 + ")"
1016 + error);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001017 }
1018
Yifan Hongbf459bc2016-08-23 16:50:37 -07001019 out << "return _hidl_err;\n";
1020
1021 out.unindent();
1022 out << "}\n\n";
1023}
1024
Andreas Huber881227d2016-08-02 14:20:21 -07001025bool CompoundType::needsEmbeddedReadWrite() const {
1026 if (mStyle != STYLE_STRUCT) {
1027 return false;
1028 }
1029
1030 for (const auto &field : *mFields) {
1031 if (field->type().needsEmbeddedReadWrite()) {
1032 return true;
1033 }
1034 }
1035
1036 return false;
1037}
1038
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001039bool CompoundType::deepNeedsResolveReferences(std::unordered_set<const Type*>* visited) const {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001040 if (mStyle != STYLE_STRUCT) {
1041 return false;
1042 }
1043
1044 for (const auto &field : *mFields) {
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001045 if (field->type().needsResolveReferences(visited)) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07001046 return true;
1047 }
1048 }
1049
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001050 return Scope::deepNeedsResolveReferences(visited);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001051}
1052
Andreas Huber881227d2016-08-02 14:20:21 -07001053bool CompoundType::resultNeedsDeref() const {
Howard Chenecfb4512017-11-21 18:28:53 +08001054 return !containsInterface() ;
Andreas Huber881227d2016-08-02 14:20:21 -07001055}
1056
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07001057status_t CompoundType::emitVtsTypeDeclarations(Formatter &out) const {
Zhuoyao Zhangbf828c82016-10-26 14:15:10 -07001058 out << "name: \"" << fullName() << "\"\n";
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -07001059 out << "type: " << getVtsType() << "\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001060
1061 // Emit declaration for each subtype.
1062 for (const auto &type : getSubTypes()) {
1063 switch (mStyle) {
1064 case STYLE_STRUCT:
1065 {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -07001066 out << "sub_struct: {\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001067 break;
1068 }
1069 case STYLE_UNION:
1070 {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -07001071 out << "sub_union: {\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001072 break;
1073 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001074 }
1075 out.indent();
1076 status_t status(type->emitVtsTypeDeclarations(out));
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07001077 if (status != OK) {
1078 return status;
1079 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001080 out.unindent();
1081 out << "}\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07001082 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001083
1084 // Emit declaration for each field.
1085 for (const auto &field : *mFields) {
1086 switch (mStyle) {
1087 case STYLE_STRUCT:
1088 {
1089 out << "struct_value: {\n";
1090 break;
1091 }
1092 case STYLE_UNION:
1093 {
1094 out << "union_value: {\n";
1095 break;
1096 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001097 }
1098 out.indent();
1099 out << "name: \"" << field->name() << "\"\n";
1100 status_t status = field->type().emitVtsAttributeType(out);
1101 if (status != OK) {
1102 return status;
1103 }
1104 out.unindent();
1105 out << "}\n";
1106 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07001107
1108 return OK;
1109}
1110
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001111status_t CompoundType::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -07001112 out << "type: " << getVtsType() << "\n";
Zhuoyao Zhangbf828c82016-10-26 14:15:10 -07001113 out << "predefined_type: \"" << fullName() << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07001114 return OK;
1115}
1116
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001117bool CompoundType::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
1118 if (mStyle != STYLE_STRUCT) {
Andreas Huber0fa9e392016-08-31 09:05:44 -07001119 return false;
1120 }
1121
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001122 for (const auto* field : *mFields) {
1123 if (!field->get()->isJavaCompatible(visited)) {
Andreas Huber0fa9e392016-08-31 09:05:44 -07001124 return false;
1125 }
1126 }
1127
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001128 return Scope::deepIsJavaCompatible(visited);
Andreas Huber85eabdb2016-08-25 11:24:49 -07001129}
1130
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001131bool CompoundType::deepContainsPointer(std::unordered_set<const Type*>* visited) const {
1132 for (const auto* field : *mFields) {
1133 if (field->get()->containsPointer(visited)) {
Andreas Huber60d3b222017-03-30 09:10:56 -07001134 return true;
1135 }
1136 }
1137
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001138 return Scope::deepContainsPointer(visited);
Andreas Huber60d3b222017-03-30 09:10:56 -07001139}
1140
Andreas Huber85eabdb2016-08-25 11:24:49 -07001141void CompoundType::getAlignmentAndSize(size_t *align, size_t *size) const {
1142 *align = 1;
Andreas Huber60d3b222017-03-30 09:10:56 -07001143 *size = 0;
Andreas Huber85eabdb2016-08-25 11:24:49 -07001144
1145 size_t offset = 0;
1146 for (const auto &field : *mFields) {
1147 // Each field is aligned according to its alignment requirement.
1148 // The surrounding structure's alignment is the maximum of its
1149 // fields' aligments.
1150
1151 size_t fieldAlign, fieldSize;
1152 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
1153
1154 size_t pad = offset % fieldAlign;
1155 if (pad > 0) {
1156 offset += fieldAlign - pad;
1157 }
1158
Andreas Huber60d3b222017-03-30 09:10:56 -07001159 if (mStyle == STYLE_STRUCT) {
1160 offset += fieldSize;
1161 } else {
1162 *size = std::max(*size, fieldSize);
1163 }
Andreas Huber85eabdb2016-08-25 11:24:49 -07001164
1165 if (fieldAlign > (*align)) {
1166 *align = fieldAlign;
1167 }
1168 }
1169
Andreas Huber60d3b222017-03-30 09:10:56 -07001170 if (mStyle == STYLE_STRUCT) {
1171 *size = offset;
Andreas Huber85eabdb2016-08-25 11:24:49 -07001172 }
1173
Andreas Huber60d3b222017-03-30 09:10:56 -07001174 // Final padding to account for the structure's alignment.
1175 size_t pad = (*size) % (*align);
1176 if (pad > 0) {
1177 (*size) += (*align) - pad;
1178 }
Andreas Huberca4bc892017-01-09 14:58:12 -08001179
1180 if (*size == 0) {
1181 // An empty struct still occupies a byte of space in C++.
1182 *size = 1;
1183 }
Andreas Huber70a59e12016-08-16 12:57:01 -07001184}
1185
Andreas Huberc9410c72016-07-28 12:18:40 -07001186} // namespace android
1187