blob: 6f75d7cd6403bc9f59727fdcd90bf6460b9f04ba [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"
Nirav Atre45e76d52018-06-04 11:49:14 -070020#include "ScalarType.h"
Andreas Huberf630bc82016-09-09 14:52:25 -070021#include "VectorType.h"
Timur Iskhakovcec46c42017-08-09 00:22:02 -070022
Andreas Huber5a545442016-08-03 10:44:56 -070023#include <android-base/logging.h>
Timur Iskhakovcec46c42017-08-09 00:22:02 -070024#include <hidl-util/Formatter.h>
Timur Iskhakove8ee6a02017-09-06 11:42:10 -070025#include <iostream>
Timur Iskhakovcec46c42017-08-09 00:22:02 -070026#include <unordered_set>
Andreas Huber5a545442016-08-03 10:44:56 -070027
Andreas Huberc9410c72016-07-28 12:18:40 -070028namespace android {
29
Timur Iskhakov565b0132017-09-06 18:07:11 -070030CompoundType::CompoundType(Style style, const char* localName, const FQName& fullName,
31 const Location& location, Scope* parent)
Yi Kong56758da2018-07-24 16:21:37 -070032 : Scope(localName, fullName, location, parent), mStyle(style), mFields(nullptr) {}
Andreas Huberc9410c72016-07-28 12:18:40 -070033
Yifan Hong27e85db2016-11-09 15:45:52 -080034CompoundType::Style CompoundType::style() const {
35 return mStyle;
36}
37
Timur Iskhakovcec46c42017-08-09 00:22:02 -070038void CompoundType::setFields(std::vector<NamedReference<Type>*>* fields) {
Andreas Huberc9410c72016-07-28 12:18:40 -070039 mFields = fields;
Timur Iskhakovcec46c42017-08-09 00:22:02 -070040}
Andreas Huber5a545442016-08-03 10:44:56 -070041
Timur Iskhakovb58f4182017-08-29 15:19:24 -070042std::vector<const Reference<Type>*> CompoundType::getReferences() const {
43 std::vector<const Reference<Type>*> ret;
44 ret.insert(ret.begin(), mFields->begin(), mFields->end());
Timur Iskhakov33431e62017-08-21 17:31:23 -070045 return ret;
Timur Iskhakovcec46c42017-08-09 00:22:02 -070046}
47
48status_t CompoundType::validate() const {
49 for (const auto* field : *mFields) {
Timur Iskhakovcec46c42017-08-09 00:22:02 -070050 const Type& type = field->type();
Andreas Huber5a545442016-08-03 10:44:56 -070051
Howard Chenecfb4512017-11-21 18:28:53 +080052 if ((type.isVector() && static_cast<const VectorType*>(&type)->isVectorOfBinders())) {
Steven Morelandd927e5c2018-07-10 16:03:43 -070053 std::cerr << "ERROR: Struct/union cannot contain vectors of interfaces at "
Timur Iskhakovcec46c42017-08-09 00:22:02 -070054 << field->location() << "\n";
55 return UNKNOWN_ERROR;
Andreas Huberb95ea8a2016-08-15 15:35:42 -070056 }
57
Andreas Huber5a545442016-08-03 10:44:56 -070058 if (mStyle == STYLE_UNION) {
Steven Moreland7df0f392018-10-31 13:23:22 -070059 if (type.isInterface()) {
Steven Morelandd927e5c2018-07-10 16:03:43 -070060 std::cerr << "ERROR: Union cannot contain interfaces at " << field->location()
61 << "\n";
62 return UNKNOWN_ERROR;
63 }
64
Andreas Huber5a545442016-08-03 10:44:56 -070065 if (type.needsEmbeddedReadWrite()) {
Timur Iskhakovcec46c42017-08-09 00:22:02 -070066 std::cerr << "ERROR: Union must not contain any types that need fixup at "
67 << field->location() << "\n";
68 return UNKNOWN_ERROR;
Andreas Huber5a545442016-08-03 10:44:56 -070069 }
Andreas Huber5a545442016-08-03 10:44:56 -070070 }
71 }
72
Steven Moreland5dec82f2018-10-10 13:33:29 -070073 if (mStyle == STYLE_SAFE_UNION && mFields->size() < 2) {
74 std::cerr << "ERROR: Safe union must contain at least two types to be useful at "
75 << location() << "\n";
76 return UNKNOWN_ERROR;
77 }
78
Timur Iskhakovcec46c42017-08-09 00:22:02 -070079 status_t err = validateUniqueNames();
80 if (err != OK) return err;
81
Nirav Atre45e76d52018-06-04 11:49:14 -070082 err = validateSubTypeNames();
83 if (err != OK) return err;
84
Timur Iskhakovcec46c42017-08-09 00:22:02 -070085 return Scope::validate();
86}
87
88status_t CompoundType::validateUniqueNames() const {
89 std::unordered_set<std::string> names;
90
91 for (const auto* field : *mFields) {
92 if (names.find(field->name()) != names.end()) {
93 std::cerr << "ERROR: Redefinition of field '" << field->name() << "' at "
94 << field->location() << "\n";
95 return UNKNOWN_ERROR;
96 }
97 names.insert(field->name());
98 }
99
100 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -0700101}
102
Nirav Atre45e76d52018-06-04 11:49:14 -0700103void CompoundType::emitInvalidSubTypeNamesError(const std::string& subTypeName,
104 const Location& location) const {
105 std::cerr << "ERROR: Type name '" << subTypeName << "' defined at "
106 << location << " conflicts with a member function of "
107 << "safe_union " << localName() << ". Consider renaming or "
108 << "moving its definition outside the safe_union scope.\n";
109}
110
111status_t CompoundType::validateSubTypeNames() const {
112 if (mStyle != STYLE_SAFE_UNION) { return OK; }
113 const auto& subTypes = Scope::getSubTypes();
114
115 for (const auto& subType : subTypes) {
116 if (subType->localName() == "getDiscriminator") {
117 emitInvalidSubTypeNamesError(subType->localName(),
118 subType->location());
119 return UNKNOWN_ERROR;
120 }
121 }
122
123 return OK;
124}
125
Andreas Huberf630bc82016-09-09 14:52:25 -0700126bool CompoundType::isCompoundType() const {
127 return true;
128}
129
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700130bool CompoundType::deepCanCheckEquality(std::unordered_set<const Type*>* visited) const {
Yifan Hongc6752dc2016-12-20 14:00:14 -0800131 if (mStyle == STYLE_UNION) {
132 return false;
133 }
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700134 for (const auto* field : *mFields) {
135 if (!field->get()->canCheckEquality(visited)) {
Yifan Hongc6752dc2016-12-20 14:00:14 -0800136 return false;
137 }
138 }
139 return true;
140}
141
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700142std::string CompoundType::typeName() const {
143 switch (mStyle) {
144 case STYLE_STRUCT: {
145 return "struct " + localName();
146 }
147 case STYLE_UNION: {
148 return "union " + localName();
149 }
Nirav Atre45e76d52018-06-04 11:49:14 -0700150 case STYLE_SAFE_UNION: {
151 return "safe_union " + localName();
152 }
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700153 }
154 CHECK(!"Should not be here");
155}
156
Andreas Huber881227d2016-08-02 14:20:21 -0700157std::string CompoundType::getCppType(
Steven Moreland979e0992016-09-07 09:18:08 -0700158 StorageMode mode,
Steven Morelande30ee9b2017-05-09 13:31:01 -0700159 bool /* specifyNamespaces */) const {
160 const std::string base = fullName();
Andreas Huber881227d2016-08-02 14:20:21 -0700161
162 switch (mode) {
163 case StorageMode_Stack:
164 return base;
165
166 case StorageMode_Argument:
167 return "const " + base + "&";
168
169 case StorageMode_Result:
Howard Chenecfb4512017-11-21 18:28:53 +0800170 return base + (containsInterface()?"":"*");
Andreas Huber881227d2016-08-02 14:20:21 -0700171 }
Nirav Atre45e76d52018-06-04 11:49:14 -0700172 CHECK(!"Should not be here");
Andreas Huber881227d2016-08-02 14:20:21 -0700173}
174
Yifan Hong4ed13472016-11-02 10:44:11 -0700175std::string CompoundType::getJavaType(bool /* forInitializer */) const {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700176 return fullJavaName();
177}
178
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700179std::string CompoundType::getVtsType() const {
180 switch (mStyle) {
181 case STYLE_STRUCT:
182 {
183 return "TYPE_STRUCT";
184 }
185 case STYLE_UNION:
186 {
187 return "TYPE_UNION";
188 }
Nirav Atre45e76d52018-06-04 11:49:14 -0700189 case STYLE_SAFE_UNION:
190 {
191 return "TYPE_SAFE_UNION";
192 }
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700193 }
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700194 CHECK(!"Should not be here");
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700195}
196
Howard Chenecfb4512017-11-21 18:28:53 +0800197bool CompoundType::containsInterface() const {
198 for (const auto& field : *mFields) {
199 if (field->type().isCompoundType()) {
200 const Type& t = field->type();
201 const CompoundType* ct = static_cast<const CompoundType*>(&t);
202 if (ct->containsInterface()) {
203 return true;
204 }
205 }
206 if (field->type().isInterface()) {
207 return true;
208 }
209 }
210 return false;
211}
212
Steven Moreland5add34d2018-11-08 16:31:30 -0800213void CompoundType::emitSafeUnionUnknownDiscriminatorError(Formatter& out, const std::string& value,
214 bool fatal) const {
215 if (fatal) {
216 out << "::android::hardware::details::logAlwaysFatal(";
217 } else {
218 out << "ALOGE(\"%s\", ";
219 }
220 out << "(\n";
Nirav Atree3cae852018-07-16 12:19:16 -0700221 out.indent(2, [&] {
222 out << "\"Unknown union discriminator (value: \" +\n"
223 << "std::to_string(" << getUnionDiscriminatorType()->getCppTypeCast(value)
224 << ") + \").\").c_str());\n";
225 });
226}
227
Nirav Atreca7a5022018-06-29 20:43:49 -0700228void CompoundType::emitSafeUnionReaderWriterForInterfaces(
229 Formatter &out,
230 const std::string &name,
231 const std::string &parcelObj,
232 bool parcelObjIsPointer,
233 bool isReader,
234 ErrorMode mode) const {
235
236 CHECK(mStyle == STYLE_SAFE_UNION);
Nirav Atreca7a5022018-06-29 20:43:49 -0700237
238 out.block([&] {
239 const auto discriminatorType = getUnionDiscriminatorType();
240 if (isReader) {
241 out << discriminatorType->getCppStackType()
242 << " _hidl_d_primitive;\n";
243 } else {
244 out << "const "
245 << discriminatorType->getCppStackType()
246 << " _hidl_d_primitive = "
247 << discriminatorType->getCppTypeCast(name + ".getDiscriminator()")
248 << ";\n";
249 }
250
251 getUnionDiscriminatorType()->emitReaderWriter(out, "_hidl_d_primitive", parcelObj,
252 parcelObjIsPointer, isReader, mode);
253 out << "switch (("
254 << fullName()
255 << "::hidl_discriminator) _hidl_d_primitive) ";
256
257 out.block([&] {
258 for (const auto& field : *mFields) {
259 out << "case "
260 << fullName()
261 << "::hidl_discriminator::"
262 << field->name()
263 << ": ";
264
265 const std::string tempFieldName = "_hidl_temp_" + field->name();
266 out.block([&] {
267 if (isReader) {
268 out << field->type().getCppResultType()
269 << " "
270 << tempFieldName
271 << ";\n";
272
273 field->type().emitReaderWriter(out, tempFieldName, parcelObj,
274 parcelObjIsPointer, isReader, mode);
275
276 const std::string derefOperator = field->type().resultNeedsDeref()
277 ? "*" : "";
278 out << name
279 << "."
280 << field->name()
Nirav Atre64868d32018-07-15 19:15:18 -0700281 << "(std::move("
Nirav Atreca7a5022018-06-29 20:43:49 -0700282 << derefOperator
283 << tempFieldName
Nirav Atre64868d32018-07-15 19:15:18 -0700284 << "));\n";
Nirav Atreca7a5022018-06-29 20:43:49 -0700285 } else {
286 const std::string fieldValue = name + "." + field->name() + "()";
287 out << field->type().getCppArgumentType()
288 << " "
289 << tempFieldName
290 << " = "
291 << fieldValue
292 << ";\n";
293
294 field->type().emitReaderWriter(out, tempFieldName, parcelObj,
295 parcelObjIsPointer, isReader, mode);
296 }
297 out << "break;\n";
298 }).endl();
299 }
300
Nirav Atreca7a5022018-06-29 20:43:49 -0700301 out << "default: ";
Steven Moreland5add34d2018-11-08 16:31:30 -0800302 out.block([&] {
303 emitSafeUnionUnknownDiscriminatorError(out, "_hidl_d_primitive",
304 !isReader /*fatal*/);
305 if (isReader) {
306 out << "_hidl_err = BAD_VALUE;\n";
307 handleError(out, mode);
308 }
309 })
Nirav Atree3cae852018-07-16 12:19:16 -0700310 .endl();
Nirav Atreca7a5022018-06-29 20:43:49 -0700311 }).endl();
312 }).endl();
313}
314
Andreas Huber881227d2016-08-02 14:20:21 -0700315void CompoundType::emitReaderWriter(
316 Formatter &out,
317 const std::string &name,
318 const std::string &parcelObj,
319 bool parcelObjIsPointer,
320 bool isReader,
321 ErrorMode mode) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700322
323 const std::string parcelObjDeref =
324 parcelObj + (parcelObjIsPointer ? "->" : ".");
325
Howard Chenecfb4512017-11-21 18:28:53 +0800326 if(containsInterface()){
Nirav Atreca7a5022018-06-29 20:43:49 -0700327 if (mStyle == STYLE_SAFE_UNION) {
328 emitSafeUnionReaderWriterForInterfaces(out, name, parcelObj,
329 parcelObjIsPointer,
330 isReader, mode);
331 return;
332 }
333
Howard Chenecfb4512017-11-21 18:28:53 +0800334 for (const auto& field : *mFields) {
Nirav Atre1d565622018-07-13 15:41:21 -0700335 const std::string tempFieldName = "_hidl_temp_" + field->name();
336 const std::string fieldValue = name + "." + field->name();
337
338 out.block([&] {
339 if (isReader) {
340 out << field->type().getCppResultType()
341 << " "
342 << tempFieldName
343 << ";\n";
344 } else {
345 out << field->type().getCppArgumentType()
346 << " "
347 << tempFieldName
348 << " = "
349 << fieldValue
350 << ";\n";
351 }
352
353 field->type().emitReaderWriter(out, tempFieldName, parcelObj,
354 parcelObjIsPointer, isReader, mode);
355 if (isReader) {
356 const std::string derefOperator = field->type().resultNeedsDeref()
357 ? "*" : "";
358 out << fieldValue
359 << " = std::move("
360 << derefOperator
361 << tempFieldName
362 << ");\n";
363 }
364 }).endl();
Howard Chenecfb4512017-11-21 18:28:53 +0800365 }
Andreas Huber881227d2016-08-02 14:20:21 -0700366 } else {
Howard Chenecfb4512017-11-21 18:28:53 +0800367 const std::string parentName = "_hidl_" + name + "_parent";
Andreas Huber881227d2016-08-02 14:20:21 -0700368
Howard Chenecfb4512017-11-21 18:28:53 +0800369 out << "size_t " << parentName << ";\n\n";
370
371 if (isReader) {
372 out << "_hidl_err = " << parcelObjDeref << "readBuffer("
373 << "sizeof(*" << name << "), &" << parentName << ", "
374 << " const_cast<const void**>(reinterpret_cast<void **>("
375 << "&" << name << ")));\n";
376 handleError(out, mode);
377 } else {
378 out << "_hidl_err = "
379 << parcelObjDeref
380 << "writeBuffer(&"
381 << name
382 << ", sizeof("
383 << name
384 << "), &"
385 << parentName
386 << ");\n";
387 handleError(out, mode);
388 }
Nirav Atre45e76d52018-06-04 11:49:14 -0700389
390 bool needEmbeddedReadWrite = needsEmbeddedReadWrite();
391 CHECK(mStyle != STYLE_UNION || !needEmbeddedReadWrite);
392
393 if (needEmbeddedReadWrite) {
Howard Chenecfb4512017-11-21 18:28:53 +0800394 emitReaderWriterEmbedded(out, 0 /* depth */, name, name, /* sanitizedName */
395 isReader /* nameIsPointer */, parcelObj, parcelObjIsPointer,
396 isReader, mode, parentName, "0 /* parentOffset */");
397 }
Andreas Huber881227d2016-08-02 14:20:21 -0700398 }
Andreas Huber881227d2016-08-02 14:20:21 -0700399}
400
401void CompoundType::emitReaderWriterEmbedded(
402 Formatter &out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700403 size_t /* depth */,
Andreas Huber881227d2016-08-02 14:20:21 -0700404 const std::string &name,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700405 const std::string & /*sanitizedName */,
Andreas Huber881227d2016-08-02 14:20:21 -0700406 bool nameIsPointer,
407 const std::string &parcelObj,
408 bool parcelObjIsPointer,
409 bool isReader,
410 ErrorMode mode,
411 const std::string &parentName,
412 const std::string &offsetText) const {
413 emitReaderWriterEmbeddedForTypeName(
414 out,
415 name,
416 nameIsPointer,
417 parcelObj,
418 parcelObjIsPointer,
419 isReader,
420 mode,
421 parentName,
422 offsetText,
Andreas Huber0e00de42016-08-03 09:56:02 -0700423 fullName(),
Yifan Hong244e82d2016-11-11 11:13:57 -0800424 "" /* childName */,
425 "" /* namespace */);
Andreas Huber881227d2016-08-02 14:20:21 -0700426}
427
Andreas Huber85eabdb2016-08-25 11:24:49 -0700428void CompoundType::emitJavaReaderWriter(
429 Formatter &out,
430 const std::string &parcelObj,
431 const std::string &argName,
432 bool isReader) const {
433 if (isReader) {
434 out << "new " << fullJavaName() << "();\n";
435 }
436
Steven Morelandc22e8122018-10-10 13:06:39 -0700437 out << "(" << getJavaTypeCast(argName) << ")."
438 << (isReader ? "readFromParcel" : "writeToParcel") << "(" << parcelObj << ");\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700439}
440
441void CompoundType::emitJavaFieldInitializer(
442 Formatter &out, const std::string &fieldName) const {
Nirav Atre1d565622018-07-13 15:41:21 -0700443 const std::string fieldDeclaration = fullJavaName() + " " + fieldName;
Nirav Atre66842a92018-06-28 18:14:13 -0700444 emitJavaFieldDefaultInitialValue(out, fieldDeclaration);
445}
446
447void CompoundType::emitJavaFieldDefaultInitialValue(
448 Formatter &out, const std::string &declaredFieldName) const {
449 out << declaredFieldName
Andreas Huber85eabdb2016-08-25 11:24:49 -0700450 << " = new "
451 << fullJavaName()
452 << "();\n";
453}
454
455void CompoundType::emitJavaFieldReaderWriter(
456 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700457 size_t /* depth */,
Andreas Huber709b62d2016-09-19 11:21:18 -0700458 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700459 const std::string &blobName,
460 const std::string &fieldName,
461 const std::string &offset,
462 bool isReader) const {
463 if (isReader) {
Nirav Atre66842a92018-06-28 18:14:13 -0700464 out << "("
465 << getJavaTypeCast(fieldName)
466 << ").readEmbeddedFromParcel("
Andreas Huber709b62d2016-09-19 11:21:18 -0700467 << parcelName
468 << ", "
Andreas Huber85eabdb2016-08-25 11:24:49 -0700469 << blobName
470 << ", "
471 << offset
472 << ");\n";
473
474 return;
475 }
476
477 out << fieldName
478 << ".writeEmbeddedToBlob("
479 << blobName
480 << ", "
481 << offset
482 << ");\n";
483}
Yifan Hongbf459bc2016-08-23 16:50:37 -0700484void CompoundType::emitResolveReferences(
485 Formatter &out,
486 const std::string &name,
487 bool nameIsPointer,
488 const std::string &parcelObj,
489 bool parcelObjIsPointer,
490 bool isReader,
491 ErrorMode mode) const {
492 emitResolveReferencesEmbedded(
493 out,
494 0 /* depth */,
495 name,
496 name /* sanitizedName */,
497 nameIsPointer,
498 parcelObj,
499 parcelObjIsPointer,
500 isReader,
501 mode,
502 "_hidl_" + name + "_parent",
503 "0 /* parentOffset */");
504}
505
506void CompoundType::emitResolveReferencesEmbedded(
507 Formatter &out,
508 size_t /* depth */,
509 const std::string &name,
510 const std::string &/* sanitizedName */,
511 bool nameIsPointer,
512 const std::string &parcelObj,
513 bool parcelObjIsPointer,
514 bool isReader,
515 ErrorMode mode,
516 const std::string &parentName,
517 const std::string &offsetText) const {
518 CHECK(needsResolveReferences());
519
520 const std::string parcelObjDeref =
521 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
522
523 const std::string parcelObjPointer =
524 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
525
Yifan Hong244e82d2016-11-11 11:13:57 -0800526 const std::string nameDerefed = nameIsPointer ? ("*" + name) : name;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700527 const std::string namePointer = nameIsPointer ? name : ("&" + name);
528
529 out << "_hidl_err = ";
530
531 if (isReader) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800532 out << "readEmbeddedReferenceFromParcel(\n";
Yifan Hongbf459bc2016-08-23 16:50:37 -0700533 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800534 out << "writeEmbeddedReferenceToParcel(\n";
Yifan Hongbf459bc2016-08-23 16:50:37 -0700535 }
536
Yifan Hong33223ca2016-12-13 15:07:35 -0800537 out.indent(2, [&]{
Yifan Hong244e82d2016-11-11 11:13:57 -0800538 if (isReader) {
539 out << "const_cast<"
540 << fullName()
541 << " *"
542 << ">("
543 << namePointer
544 << "),\n"
545 << parcelObjDeref;
546 } else {
547 out << nameDerefed
548 << ",\n"
549 << parcelObjPointer;
550 }
551
552 out << ",\n"
Yifan Hong0a68a282016-10-21 16:32:34 -0700553 << parentName
554 << ",\n"
555 << offsetText
556 << ");\n\n";
557 });
Yifan Hongbf459bc2016-08-23 16:50:37 -0700558
559 handleError(out, mode);
560}
Andreas Huber85eabdb2016-08-25 11:24:49 -0700561
Nirav Atre45e76d52018-06-04 11:49:14 -0700562void CompoundType::emitLayoutAsserts(Formatter& out, const Layout& layout,
563 const std::string& layoutName) const {
564 out << "static_assert(sizeof("
565 << fullName()
566 << layoutName
567 << ") == "
568 << layout.size
569 << ", \"wrong size\");\n";
570
571 out << "static_assert(__alignof("
572 << fullName()
573 << layoutName
574 << ") == "
575 << layout.align
576 << ", \"wrong alignment\");\n";
577}
578
579void CompoundType::emitSafeUnionTypeDeclarations(Formatter& out) const {
580 out << "struct "
581 << localName()
582 << " final {\n";
583
584 out.indent();
585
586 Scope::emitTypeDeclarations(out);
587
Nirav Atre12c597a2018-06-28 09:35:24 -0700588 bool hasPointer = containsPointer();
589 CompoundLayout layout = hasPointer
590 ? CompoundLayout()
591 : getCompoundAlignmentAndSize();
592
Nirav Atre45e76d52018-06-04 11:49:14 -0700593 out << "enum class hidl_discriminator : "
594 << getUnionDiscriminatorType()->getCppType(StorageMode_Stack, false)
595 << " ";
596
597 out.block([&] {
Nirav Atre71ce0082018-08-01 15:39:49 -0700598 const auto elements = getSafeUnionEnumElements(true /* useCppTypeName */);
599 for (size_t i = 0; i < elements.size(); i++) {
600 out << elements[i].fieldName
601 << " = "
602 << i
603 << ",";
604
605 if (!elements[i].fieldTypeName.empty()) {
606 out << " // "
607 << elements[i].fieldTypeName;
608 }
609 out << "\n";
Nirav Atre45e76d52018-06-04 11:49:14 -0700610 }
Nirav Atre45e76d52018-06-04 11:49:14 -0700611 });
612 out << ";\n\n";
613
Nirav Atre64868d32018-07-15 19:15:18 -0700614 out << localName() << "();\n" // Constructor
615 << "~" << localName() << "();\n" // Destructor
616 << localName() << "(" << localName() << "&&);\n" // Move constructor
617 << localName() << "(const " << localName() << "&);\n" // Copy constructor
618 << localName() << "& operator=(" << localName() << "&&);\n" // Move assignment
619 << localName() << "& operator=(const " << localName() << "&);\n\n"; // Copy assignment
Nirav Atre45e76d52018-06-04 11:49:14 -0700620
621 for (const auto& field : *mFields) {
Nirav Atre64868d32018-07-15 19:15:18 -0700622 // Setter (copy)
Nirav Atre45e76d52018-06-04 11:49:14 -0700623 out << "void "
624 << field->name()
625 << "("
626 << field->type().getCppArgumentType()
627 << ");\n";
628
Steven Moreland492bada2018-09-13 16:12:32 -0700629 if (field->type().resolveToScalarType() == nullptr) {
Nirav Atre64868d32018-07-15 19:15:18 -0700630 // Setter (move)
631 out << "void "
632 << field->name()
633 << "("
634 << field->type().getCppStackType()
635 << "&&);\n";
636 }
637
638 // Getter (mutable)
Nirav Atre45e76d52018-06-04 11:49:14 -0700639 out << field->type().getCppStackType()
640 << "& "
641 << field->name()
642 << "();\n";
643
Nirav Atre64868d32018-07-15 19:15:18 -0700644 // Getter (immutable)
Nirav Atre45e76d52018-06-04 11:49:14 -0700645 out << field->type().getCppArgumentType()
646 << " "
647 << field->name()
648 << "() const;\n\n";
649 }
650
Nirav Atre12c597a2018-06-28 09:35:24 -0700651 out << "// Utility methods\n";
Nirav Atre45e76d52018-06-04 11:49:14 -0700652 out << "hidl_discriminator getDiscriminator() const;\n\n";
653
Nirav Atre12c597a2018-06-28 09:35:24 -0700654 out << "constexpr size_t hidl_getUnionOffset() const ";
655 out.block([&] {
656 out << "return offsetof(" << fullName() << ", hidl_u);\n";
657 }).endl().endl();
658
Nirav Atre45e76d52018-06-04 11:49:14 -0700659 out.unindent();
660 out << "private:\n";
661 out.indent();
662
663 out << "void hidl_destructUnion();\n\n";
Nirav Atre12c597a2018-06-28 09:35:24 -0700664
665 out << "hidl_discriminator hidl_d";
666 if (!hasPointer) {
667 out << " __attribute__ ((aligned("
668 << layout.discriminator.align << "))) ";
669 }
Steven Moreland967c2432018-10-10 12:50:43 -0700670 out << ";\n";
Nirav Atre45e76d52018-06-04 11:49:14 -0700671 out << "union hidl_union final {\n";
672 out.indent();
673
Nirav Atre45e76d52018-06-04 11:49:14 -0700674 for (const auto& field : *mFields) {
675
676 size_t fieldAlign, fieldSize;
677 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
678
679 out << field->type().getCppStackType()
680 << " "
681 << field->name();
682
683 if (!hasPointer) {
684 out << " __attribute__ ((aligned("
685 << fieldAlign
686 << ")))";
687 }
688 out << ";\n";
689 }
690
691 out << "\n"
692 << "hidl_union();\n"
693 << "~hidl_union();\n";
694
695 out.unindent();
Nirav Atre12c597a2018-06-28 09:35:24 -0700696 out << "} hidl_u;\n";
Nirav Atre45e76d52018-06-04 11:49:14 -0700697
698 if (!hasPointer) {
699 out << "\n";
700
701 emitLayoutAsserts(out, layout.innerStruct, "::hidl_union");
702 emitLayoutAsserts(out, layout.discriminator, "::hidl_discriminator");
703 }
704
705 out.unindent();
706 out << "};\n\n";
707
708 if (!hasPointer) {
709 emitLayoutAsserts(out, layout.overall, "");
710 out << "\n";
711 }
712}
713
Steven Moreland368e4602018-02-16 14:21:49 -0800714void CompoundType::emitTypeDeclarations(Formatter& out) const {
Nirav Atre45e76d52018-06-04 11:49:14 -0700715 if (mStyle == STYLE_SAFE_UNION) {
716 emitSafeUnionTypeDeclarations(out);
717 return;
718 }
719
Andreas Huber881227d2016-08-02 14:20:21 -0700720 out << ((mStyle == STYLE_STRUCT) ? "struct" : "union")
721 << " "
Andreas Huber0e00de42016-08-03 09:56:02 -0700722 << localName()
Yifan Hongb1d2ebc2016-12-20 17:18:07 -0800723 << " final {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700724
725 out.indent();
726
727 Scope::emitTypeDeclarations(out);
728
Andreas Huber60d3b222017-03-30 09:10:56 -0700729 if (containsPointer()) {
Andreas Huberca4bc892017-01-09 14:58:12 -0800730 for (const auto &field : *mFields) {
Steven Moreland49bad8d2018-05-17 15:45:26 -0700731 field->emitDocComment(out);
Andreas Huberca4bc892017-01-09 14:58:12 -0800732 out << field->type().getCppStackType()
733 << " "
734 << field->name()
735 << ";\n";
736 }
737
738 out.unindent();
739 out << "};\n\n";
740
Steven Moreland368e4602018-02-16 14:21:49 -0800741 return;
Andreas Huber881227d2016-08-02 14:20:21 -0700742 }
743
Andreas Huberca4bc892017-01-09 14:58:12 -0800744 for (int pass = 0; pass < 2; ++pass) {
745 size_t offset = 0;
746 for (const auto &field : *mFields) {
747 size_t fieldAlign, fieldSize;
748 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
749
Nirav Atre45e76d52018-06-04 11:49:14 -0700750 offset += Layout::getPad(offset, fieldAlign);
Andreas Huberca4bc892017-01-09 14:58:12 -0800751
752 if (pass == 0) {
753 out << field->type().getCppStackType()
754 << " "
755 << field->name()
756 << " __attribute__ ((aligned("
757 << fieldAlign
758 << ")));\n";
759 } else {
760 out << "static_assert(offsetof("
761 << fullName()
762 << ", "
763 << field->name()
764 << ") == "
765 << offset
766 << ", \"wrong offset\");\n";
767 }
768
Andreas Huber60d3b222017-03-30 09:10:56 -0700769 if (mStyle == STYLE_STRUCT) {
770 offset += fieldSize;
771 }
Andreas Huberca4bc892017-01-09 14:58:12 -0800772 }
773
774 if (pass == 0) {
775 out.unindent();
776 out << "};\n\n";
777 }
778 }
779
Nirav Atre45e76d52018-06-04 11:49:14 -0700780 CompoundLayout layout = getCompoundAlignmentAndSize();
781 emitLayoutAsserts(out, layout.overall, "");
782 out << "\n";
Yifan Hong244e82d2016-11-11 11:13:57 -0800783}
784
Timur Iskhakovfd3f2502017-09-05 16:25:02 -0700785void CompoundType::emitTypeForwardDeclaration(Formatter& out) const {
Nirav Atre45e76d52018-06-04 11:49:14 -0700786 switch (mStyle) {
787 case STYLE_UNION: {
788 out << "union";
789 break;
790 }
791 case STYLE_STRUCT:
792 case STYLE_SAFE_UNION: {
793 out << "struct";
794 break;
795 }
796 default: {
797 CHECK(!"Should not be here");
798 }
799 }
800 out << " " << localName() << ";\n";
Timur Iskhakovfd3f2502017-09-05 16:25:02 -0700801}
Yifan Hong244e82d2016-11-11 11:13:57 -0800802
Steven Moreland368e4602018-02-16 14:21:49 -0800803void CompoundType::emitPackageTypeDeclarations(Formatter& out) const {
Steven Moreland4b8f7a12017-11-17 15:39:54 -0800804 Scope::emitPackageTypeDeclarations(out);
Yifan Hongc6752dc2016-12-20 14:00:14 -0800805
Steven Morelandbf714212017-10-27 18:29:01 -0700806 out << "static inline std::string toString("
807 << getCppArgumentType()
808 << (mFields->empty() ? "" : " o")
Steven Moreland09c6ebe2018-10-09 10:15:48 -0700809 << ");\n";
810
811 if (canCheckEquality()) {
812 out << "static inline bool operator==("
813 << getCppArgumentType() << " lhs, " << getCppArgumentType() << " rhs);\n";
814
815 out << "static inline bool operator!=("
816 << getCppArgumentType() << " lhs, " << getCppArgumentType() << " rhs);\n";
817 } else {
818 out << "// operator== and operator!= are not generated for " << localName() << "\n";
819 }
820
821 out.endl();
822}
823
824void CompoundType::emitPackageTypeHeaderDefinitions(Formatter& out) const {
825 Scope::emitPackageTypeHeaderDefinitions(out);
826
827 out << "static inline std::string toString("
828 << getCppArgumentType()
829 << (mFields->empty() ? "" : " o")
Steven Morelandbf714212017-10-27 18:29:01 -0700830 << ") ";
831
832 out.block([&] {
833 // include toString for scalar types
834 out << "using ::android::hardware::toString;\n"
835 << "std::string os;\n";
836 out << "os += \"{\";\n";
837
Steven Moreland5dec82f2018-10-10 13:33:29 -0700838 if (mStyle == STYLE_SAFE_UNION) {
Nirav Atre45e76d52018-06-04 11:49:14 -0700839 out << "\nswitch (o.getDiscriminator()) {\n";
840 out.indent();
Steven Morelandbf714212017-10-27 18:29:01 -0700841 }
842
Nirav Atre45e76d52018-06-04 11:49:14 -0700843 for (const NamedReference<Type>* field : *mFields) {
844 if (mStyle == STYLE_SAFE_UNION) {
845 out << "case "
846 << fullName()
847 << "::hidl_discriminator::"
848 << field->name()
849 << ": ";
850
851 out.block([&] {
852 out << "os += \"."
853 << field->name()
854 << " = \";\n"
855 << "os += toString(o."
856 << field->name()
857 << "());\n"
858 << "break;\n";
859 }).endl();
860 } else {
861 out << "os += \"";
862 if (field != *(mFields->begin())) {
863 out << ", ";
864 }
865 out << "." << field->name() << " = \";\n";
866 field->type().emitDump(out, "os", "o." + field->name());
867 }
868 }
869
Steven Moreland5dec82f2018-10-10 13:33:29 -0700870 if (mStyle == STYLE_SAFE_UNION) {
Nirav Atre45e76d52018-06-04 11:49:14 -0700871 out << "default: ";
Steven Moreland5add34d2018-11-08 16:31:30 -0800872 out.block([&] {
873 emitSafeUnionUnknownDiscriminatorError(out, "o.getDiscriminator()",
874 true /*fatal*/);
875 })
Nirav Atree3cae852018-07-16 12:19:16 -0700876 .endl();
Nirav Atre45e76d52018-06-04 11:49:14 -0700877
878 out.unindent();
879 out << "}\n";
880 }
Steven Morelandbf714212017-10-27 18:29:01 -0700881 out << "os += \"}\"; return os;\n";
882 }).endl().endl();
883
884 if (canCheckEquality()) {
885 out << "static inline bool operator==("
886 << getCppArgumentType() << " " << (mFields->empty() ? "/* lhs */" : "lhs") << ", "
887 << getCppArgumentType() << " " << (mFields->empty() ? "/* rhs */" : "rhs") << ") ";
888 out.block([&] {
Steven Moreland5dec82f2018-10-10 13:33:29 -0700889 if (mStyle == STYLE_SAFE_UNION) {
Nirav Atre45e76d52018-06-04 11:49:14 -0700890 out.sIf("lhs.getDiscriminator() != rhs.getDiscriminator()", [&] {
Steven Morelandbf714212017-10-27 18:29:01 -0700891 out << "return false;\n";
892 }).endl();
Nirav Atre45e76d52018-06-04 11:49:14 -0700893
894 out << "switch (lhs.getDiscriminator()) {\n";
895 out.indent();
896 }
897
898 for (const auto& field : *mFields) {
899 if (mStyle == STYLE_SAFE_UNION) {
900 out << "case "
901 << fullName()
902 << "::hidl_discriminator::"
903 << field->name()
904 << ": ";
905
906 out.block([&] {
907 out << "return (lhs."
908 << field->name()
909 << "() == rhs."
910 << field->name()
911 << "());\n";
912 }).endl();
913 } else {
914 out.sIf("lhs." + field->name() + " != rhs." + field->name(), [&] {
915 out << "return false;\n";
916 }).endl();
917 }
918 }
919
Steven Moreland5dec82f2018-10-10 13:33:29 -0700920 if (mStyle == STYLE_SAFE_UNION) {
Nirav Atre45e76d52018-06-04 11:49:14 -0700921 out << "default: ";
922 out.block([&] {
Steven Moreland5add34d2018-11-08 16:31:30 -0800923 emitSafeUnionUnknownDiscriminatorError(out, "lhs.getDiscriminator()",
924 true /*fatal*/);
Nirav Atree3cae852018-07-16 12:19:16 -0700925 })
926 .endl();
Nirav Atre45e76d52018-06-04 11:49:14 -0700927
928 out.unindent();
929 out << "}\n";
Steven Morelandbf714212017-10-27 18:29:01 -0700930 }
931 out << "return true;\n";
932 }).endl().endl();
933
934 out << "static inline bool operator!=("
Steven Moreland09c6ebe2018-10-09 10:15:48 -0700935 << getCppArgumentType() << " lhs, " << getCppArgumentType() << " rhs)";
Steven Morelandbf714212017-10-27 18:29:01 -0700936 out.block([&] {
937 out << "return !(lhs == rhs);\n";
938 }).endl().endl();
Yifan Hong42c68432017-03-13 16:22:03 -0700939 } else {
Steven Morelandf91048a2017-06-23 21:38:35 +0000940 out << "// operator== and operator!= are not generated for " << localName() << "\n\n";
Yifan Hongc6752dc2016-12-20 14:00:14 -0800941 }
Yifan Hongc6752dc2016-12-20 14:00:14 -0800942}
943
Steven Moreland368e4602018-02-16 14:21:49 -0800944void CompoundType::emitPackageHwDeclarations(Formatter& out) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700945 if (needsEmbeddedReadWrite()) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800946 out << "::android::status_t readEmbeddedFromParcel(\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700947
Yifan Hong0a68a282016-10-21 16:32:34 -0700948 out.indent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700949
Martijn Coenenb2a861c2017-04-18 15:54:25 -0700950 out << "const " << fullName() << " &obj,\n"
Yifan Hong244e82d2016-11-11 11:13:57 -0800951 << "const ::android::hardware::Parcel &parcel,\n"
952 << "size_t parentHandle,\n"
953 << "size_t parentOffset);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700954
Yifan Hong0a68a282016-10-21 16:32:34 -0700955 out.unindent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700956
957 out << "::android::status_t writeEmbeddedToParcel(\n";
958
Yifan Hong0a68a282016-10-21 16:32:34 -0700959 out.indent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700960
Yifan Hong244e82d2016-11-11 11:13:57 -0800961 out << "const " << fullName() << " &obj,\n"
962 << "::android::hardware::Parcel *parcel,\n"
963 << "size_t parentHandle,\n"
964 << "size_t parentOffset);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700965
Yifan Hong0a68a282016-10-21 16:32:34 -0700966 out.unindent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700967 }
968
Yifan Hongbf459bc2016-08-23 16:50:37 -0700969 if(needsResolveReferences()) {
Yifan Hongbf459bc2016-08-23 16:50:37 -0700970 out << "::android::status_t readEmbeddedReferenceFromParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700971 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800972 out << fullName() << " *obj,\n"
973 << "const ::android::hardware::Parcel &parcel,\n"
974 << "size_t parentHandle, size_t parentOffset);\n\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700975 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700976 out << "::android::status_t writeEmbeddedReferenceToParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700977 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800978 out << "const " << fullName() << " &obj,\n"
979 << "::android::hardware::Parcel *,\n"
980 << "size_t parentHandle, size_t parentOffset);\n\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700981 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700982 }
Andreas Huber881227d2016-08-02 14:20:21 -0700983}
984
Nirav Atre64868d32018-07-15 19:15:18 -0700985static void emitSafeUnionFieldConstructor(Formatter& out,
986 const NamedReference<Type>* field,
987 const std::string& initializerObjectName) {
988 out << "new (&hidl_u."
989 << field->name()
990 << ") "
991 << field->type().getCppStackType()
992 << "("
993 << initializerObjectName
994 << ");\n";
995}
996
997static void emitSafeUnionSetterDefinition(Formatter& out,
998 const NamedReference<Type>* field,
999 const std::string& parameterName,
1000 bool usesMoveSemantics) {
1001 out.block([&] {
1002 out << "if (hidl_d != hidl_discriminator::"
1003 << field->name()
1004 << ") ";
1005
1006 const std::string argumentName = usesMoveSemantics
1007 ? ("std::move(" + parameterName + ")")
1008 : parameterName;
1009 out.block([&] {
1010 out << "hidl_destructUnion();\n"
1011 << "::std::memset(&hidl_u, 0, sizeof(hidl_u));\n\n";
1012
1013 emitSafeUnionFieldConstructor(out, field, argumentName);
1014 out << "hidl_d = hidl_discriminator::"
1015 << field->name()
1016 << ";\n";
1017 }).endl();
1018
1019 out << "else if (&(hidl_u."
1020 << field->name()
1021 << ") != &"
1022 << parameterName
1023 << ") ";
1024
1025 out.block([&] {
1026 out << "hidl_u."
1027 << field->name()
1028 << " = "
1029 << argumentName
1030 << ";\n";
1031 }).endl();
1032 }).endl().endl();
1033}
1034
1035static void emitSafeUnionGetterDefinition(Formatter& out, const std::string& fieldName) {
Nirav Atre45e76d52018-06-04 11:49:14 -07001036 out.block([&] {
1037 out << "if (CC_UNLIKELY(hidl_d != hidl_discriminator::"
1038 << fieldName
1039 << ")) ";
1040
1041 out.block([&] {
Steven Moreland35835f12018-11-08 17:04:16 -08001042 out << "LOG_ALWAYS_FATAL(\"Bad safe_union access: safe_union has discriminator %\" "
1043 << "PRIu64 \" but discriminator %\" PRIu64 \" was accessed.\",\n";
1044 out.indent(2, [&] {
1045 out << "static_cast<uint64_t>(hidl_d), "
1046 << "static_cast<uint64_t>(hidl_discriminator::" << fieldName << "));";
1047 }).endl();
Nirav Atre45e76d52018-06-04 11:49:14 -07001048 }).endl().endl();
1049
1050 out << "return hidl_u."
1051 << fieldName
1052 << ";\n";
1053 }).endl().endl();
1054}
1055
Nirav Atre71ce0082018-08-01 15:39:49 -07001056std::vector<CompoundType::SafeUnionEnumElement> CompoundType::getSafeUnionEnumElements(
1057 bool useCppTypeName) const {
1058 std::vector<SafeUnionEnumElement> elements;
Nirav Atre71ce0082018-08-01 15:39:49 -07001059
1060 for (const auto& field : *mFields) {
1061 const std::string fieldTypeName = useCppTypeName
1062 ? field->type().getCppStackType(true /* specifyNamespaces */)
1063 : field->type().getJavaType(true /* forInitializer */);
1064
1065 elements.push_back({field->name(), fieldTypeName});
1066 }
1067
1068 return elements;
1069}
1070
Nirav Atre64868d32018-07-15 19:15:18 -07001071void CompoundType::emitSafeUnionCopyAndAssignDefinition(Formatter& out,
1072 const std::string& parameterName,
1073 bool isCopyConstructor,
1074 bool usesMoveSemantics) const {
1075 out.block([&] {
1076 if (!isCopyConstructor) {
1077 out << "if (this == &"
1078 << parameterName
1079 << ") { return *this; }\n\n";
1080 }
1081
1082 out << "switch ("
1083 << parameterName
1084 << ".hidl_d) ";
1085
1086 out.block([&] {
1087 for (const auto& field : *mFields) {
1088 const std::string parameterFieldName = (parameterName + ".hidl_u." +
1089 field->name());
1090
1091 const std::string argumentName = usesMoveSemantics
1092 ? ("std::move(" + parameterFieldName + ")")
1093 : parameterFieldName;
1094
1095 out << "case hidl_discriminator::"
1096 << field->name()
1097 << ": ";
1098
1099 if (isCopyConstructor) {
1100 out.block([&] {
1101 emitSafeUnionFieldConstructor(out, field, argumentName);
1102 out << "break;\n";
1103 }).endl();
1104 } else {
1105 out.block([&] {
1106 out << field->name()
1107 << "("
1108 << argumentName
1109 << ");\n"
1110 << "break;\n";
1111 }).endl();
1112 }
1113 }
1114
Nirav Atree3cae852018-07-16 12:19:16 -07001115 out << "default: ";
Steven Moreland5add34d2018-11-08 16:31:30 -08001116 out.block([&] {
1117 emitSafeUnionUnknownDiscriminatorError(out, parameterName + ".hidl_d",
1118 true /*fatal*/);
1119 })
Nirav Atree3cae852018-07-16 12:19:16 -07001120 .endl();
Nirav Atre64868d32018-07-15 19:15:18 -07001121 }).endl();
1122
1123 if (isCopyConstructor) {
1124 out << "\nhidl_d = "
1125 << parameterName
1126 << ".hidl_d;\n";
1127 } else {
1128 out << "return *this;\n";
1129 }
1130 }).endl().endl();
1131}
1132
Nirav Atre45e76d52018-06-04 11:49:14 -07001133void CompoundType::emitSafeUnionTypeConstructors(Formatter& out) const {
1134
1135 // Default constructor
1136 out << fullName()
1137 << "::"
1138 << localName()
Nirav Atre12c597a2018-06-28 09:35:24 -07001139 << "() ";
1140
1141 out.block([&] {
1142 out << "static_assert(offsetof("
1143 << fullName()
1144 << ", hidl_d) == 0, \"wrong offset\");\n";
1145
1146 if (!containsPointer()) {
1147 CompoundLayout layout = getCompoundAlignmentAndSize();
1148 out << "static_assert(offsetof("
1149 << fullName()
1150 << ", hidl_u) == "
1151 << layout.innerStruct.offset
1152 << ", \"wrong offset\");\n";
1153 }
Steven Moreland967c2432018-10-10 12:50:43 -07001154 out.endl();
1155
1156 CHECK(!mFields->empty());
1157 out << "hidl_d = hidl_discriminator::" << mFields->at(0)->name() << ";\n";
1158 emitSafeUnionFieldConstructor(out, mFields->at(0), "");
Nirav Atre12c597a2018-06-28 09:35:24 -07001159 }).endl().endl();
Nirav Atre45e76d52018-06-04 11:49:14 -07001160
1161 // Destructor
1162 out << fullName()
1163 << "::~"
1164 << localName()
1165 << "() ";
1166
1167 out.block([&] {
1168 out << "hidl_destructUnion();\n";
1169 }).endl().endl();
1170
Nirav Atre64868d32018-07-15 19:15:18 -07001171 // Move constructor
1172 out << fullName()
1173 << "::"
1174 << localName()
1175 << "("
1176 << localName()
1177 << "&& other) ";
1178
1179 emitSafeUnionCopyAndAssignDefinition(
1180 out, "other", true /* isCopyConstructor */, true /* usesMoveSemantics */);
1181
Nirav Atre45e76d52018-06-04 11:49:14 -07001182 // Copy constructor
1183 out << fullName()
1184 << "::"
1185 << localName()
1186 << "(const "
1187 << localName()
1188 << "& other) ";
1189
Nirav Atre64868d32018-07-15 19:15:18 -07001190 emitSafeUnionCopyAndAssignDefinition(
1191 out, "other", true /* isCopyConstructor */, false /* usesMoveSemantics */);
Nirav Atre45e76d52018-06-04 11:49:14 -07001192
Nirav Atre64868d32018-07-15 19:15:18 -07001193 // Move assignment operator
1194 out << fullName()
1195 << "& ("
1196 << fullName()
1197 << "::operator=)("
1198 << localName()
1199 << "&& other) ";
Nirav Atre45e76d52018-06-04 11:49:14 -07001200
Nirav Atre64868d32018-07-15 19:15:18 -07001201 emitSafeUnionCopyAndAssignDefinition(
1202 out, "other", false /* isCopyConstructor */, true /* usesMoveSemantics */);
Nirav Atre45e76d52018-06-04 11:49:14 -07001203
Nirav Atre64868d32018-07-15 19:15:18 -07001204 // Copy assignment operator
1205 out << fullName()
1206 << "& ("
1207 << fullName()
1208 << "::operator=)(const "
1209 << localName()
1210 << "& other) ";
Nirav Atre45e76d52018-06-04 11:49:14 -07001211
Nirav Atre64868d32018-07-15 19:15:18 -07001212 emitSafeUnionCopyAndAssignDefinition(
1213 out, "other", false /* isCopyConstructor */, false /* usesMoveSemantics */);
Nirav Atre45e76d52018-06-04 11:49:14 -07001214}
1215
1216void CompoundType::emitSafeUnionTypeDefinitions(Formatter& out) const {
Nirav Atre45e76d52018-06-04 11:49:14 -07001217 emitSafeUnionTypeConstructors(out);
1218
1219 out << "void "
1220 << fullName()
1221 << "::hidl_destructUnion() ";
1222
1223 out.block([&] {
Nirav Atre66842a92018-06-28 18:14:13 -07001224 out << "switch (hidl_d) ";
Nirav Atre45e76d52018-06-04 11:49:14 -07001225 out.block([&] {
1226
1227 for (const auto& field : *mFields) {
1228 out << "case hidl_discriminator::"
1229 << field->name()
1230 << ": ";
1231
1232 out.block([&] {
Steven Moreland9b37ed12018-10-25 11:10:57 -07001233 out << "::android::hardware::details::destructElement(&(hidl_u."
Nirav Atre3b4c7ff2018-07-09 11:57:02 -07001234 << field->name()
1235 << "));\n"
1236 << "break;\n";
Nirav Atre45e76d52018-06-04 11:49:14 -07001237 }).endl();
1238 }
1239
Nirav Atree3cae852018-07-16 12:19:16 -07001240 out << "default: ";
Steven Moreland5add34d2018-11-08 16:31:30 -08001241 out.block(
1242 [&] { emitSafeUnionUnknownDiscriminatorError(out, "hidl_d", true /*fatal*/); })
1243 .endl();
Nirav Atre45e76d52018-06-04 11:49:14 -07001244 }).endl().endl();
Nirav Atre45e76d52018-06-04 11:49:14 -07001245 }).endl().endl();
1246
Nirav Atre45e76d52018-06-04 11:49:14 -07001247 for (const NamedReference<Type>* field : *mFields) {
Nirav Atre64868d32018-07-15 19:15:18 -07001248 // Setter (copy)
Nirav Atre45e76d52018-06-04 11:49:14 -07001249 out << "void "
1250 << fullName()
1251 << "::"
1252 << field->name()
1253 << "("
1254 << field->type().getCppArgumentType()
1255 << " o) ";
1256
Nirav Atre64868d32018-07-15 19:15:18 -07001257 emitSafeUnionSetterDefinition(out, field, "o", false /* usesMoveSemantics */);
1258
Steven Moreland492bada2018-09-13 16:12:32 -07001259 if (field->type().resolveToScalarType() == nullptr) {
Nirav Atre64868d32018-07-15 19:15:18 -07001260 // Setter (move)
1261 out << "void "
1262 << fullName()
1263 << "::"
Nirav Atre45e76d52018-06-04 11:49:14 -07001264 << field->name()
Nirav Atre64868d32018-07-15 19:15:18 -07001265 << "("
1266 << field->type().getCppStackType()
1267 << "&& o) ";
Nirav Atre45e76d52018-06-04 11:49:14 -07001268
Nirav Atre64868d32018-07-15 19:15:18 -07001269 emitSafeUnionSetterDefinition(out, field, "o", true /* usesMoveSemantics */);
1270 }
Nirav Atre45e76d52018-06-04 11:49:14 -07001271
1272 // Getter (mutable)
1273 out << field->type().getCppStackType()
1274 << "& ("
1275 << fullName()
1276 << "::"
1277 << field->name()
1278 << ")() ";
1279
Nirav Atre64868d32018-07-15 19:15:18 -07001280 emitSafeUnionGetterDefinition(out, field->name());
Nirav Atre45e76d52018-06-04 11:49:14 -07001281
1282 // Getter (immutable)
1283 out << field->type().getCppArgumentType()
1284 << " ("
1285 << fullName()
1286 << "::"
1287 << field->name()
1288 << ")() const ";
1289
Nirav Atre64868d32018-07-15 19:15:18 -07001290 emitSafeUnionGetterDefinition(out, field->name());
Nirav Atre45e76d52018-06-04 11:49:14 -07001291 }
1292
1293 // Trivial constructor/destructor for internal union
1294 out << fullName() << "::hidl_union::hidl_union() {}\n\n"
1295 << fullName() << "::hidl_union::~hidl_union() {}\n\n";
1296
1297 // Utility method
Nirav Atreca7a5022018-06-29 20:43:49 -07001298 out << fullName() << "::hidl_discriminator ("
1299 << fullName() << "::getDiscriminator)() const ";
Nirav Atre45e76d52018-06-04 11:49:14 -07001300
1301 out.block([&] {
Nirav Atreca7a5022018-06-29 20:43:49 -07001302 out << "return hidl_d;\n";
Nirav Atre45e76d52018-06-04 11:49:14 -07001303 }).endl().endl();
1304}
1305
Steven Moreland368e4602018-02-16 14:21:49 -08001306void CompoundType::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
Steven Morelandb158de12016-10-10 11:59:09 -07001307 std::string space = prefix.empty() ? "" : (prefix + "::");
Steven Moreland368e4602018-02-16 14:21:49 -08001308 Scope::emitTypeDefinitions(out, space + localName());
Andreas Huber881227d2016-08-02 14:20:21 -07001309
Yifan Hongbf459bc2016-08-23 16:50:37 -07001310 if (needsEmbeddedReadWrite()) {
1311 emitStructReaderWriter(out, prefix, true /* isReader */);
1312 emitStructReaderWriter(out, prefix, false /* isReader */);
Andreas Huber881227d2016-08-02 14:20:21 -07001313 }
1314
Yifan Hongbf459bc2016-08-23 16:50:37 -07001315 if (needsResolveReferences()) {
1316 emitResolveReferenceDef(out, prefix, true /* isReader */);
1317 emitResolveReferenceDef(out, prefix, false /* isReader */);
1318 }
Nirav Atre45e76d52018-06-04 11:49:14 -07001319
1320 if (mStyle == STYLE_SAFE_UNION) {
1321 emitSafeUnionTypeDefinitions(out);
1322 }
Andreas Huber881227d2016-08-02 14:20:21 -07001323}
1324
Steven Moreland5add34d2018-11-08 16:31:30 -08001325static void emitJavaSafeUnionUnknownDiscriminatorError(Formatter& out, bool fatal) {
1326 out << "throw new ";
1327
1328 if (fatal) {
1329 out << "Error";
1330 } else {
1331 out << "IllegalStateException";
1332 }
1333
1334 out << "(\"Unknown union discriminator (value: \" + hidl_d + \").\");\n";
Nirav Atree3cae852018-07-16 12:19:16 -07001335}
1336
Steven Moreland368e4602018-02-16 14:21:49 -08001337void CompoundType::emitJavaTypeDeclarations(Formatter& out, bool atTopLevel) const {
Andreas Huber85eabdb2016-08-25 11:24:49 -07001338 out << "public final ";
1339
1340 if (!atTopLevel) {
1341 out << "static ";
1342 }
1343
1344 out << "class "
1345 << localName()
1346 << " {\n";
1347
1348 out.indent();
1349
1350 Scope::emitJavaTypeDeclarations(out, false /* atTopLevel */);
1351
Steven Moreland5dec82f2018-10-10 13:33:29 -07001352 if (mStyle == STYLE_SAFE_UNION) {
Steven Moreland579b0f62018-10-25 13:40:47 -07001353 out << "public " << localName() << "() ";
1354 out.block([&] {
1355 CHECK(!mFields->empty());
1356 mFields->at(0)->type().emitJavaFieldDefaultInitialValue(out, "hidl_o");
1357 }).endl().endl();
1358
Nirav Atre66842a92018-06-28 18:14:13 -07001359 const std::string discriminatorStorageType = (
1360 getUnionDiscriminatorType()->getJavaType(false));
Steven Moreland49bad8d2018-05-17 15:45:26 -07001361
Nirav Atre66842a92018-06-28 18:14:13 -07001362 out << "public static final class hidl_discriminator ";
1363 out.block([&] {
Nirav Atre71ce0082018-08-01 15:39:49 -07001364 const auto elements = getSafeUnionEnumElements(false /* useCppTypeName */);
1365 for (size_t idx = 0; idx < elements.size(); idx++) {
Nirav Atre66842a92018-06-28 18:14:13 -07001366 out << "public static final "
1367 << discriminatorStorageType
1368 << " "
Nirav Atre71ce0082018-08-01 15:39:49 -07001369 << elements[idx].fieldName
Nirav Atre66842a92018-06-28 18:14:13 -07001370 << " = "
1371 << idx
Nirav Atre71ce0082018-08-01 15:39:49 -07001372 << ";";
1373
1374 if (!elements[idx].fieldTypeName.empty()) {
1375 out << " // "
1376 << elements[idx].fieldTypeName;
1377 }
1378 out << "\n";
Nirav Atre66842a92018-06-28 18:14:13 -07001379 }
Andreas Huber85eabdb2016-08-25 11:24:49 -07001380
Nirav Atre66842a92018-06-28 18:14:13 -07001381 out << "\n"
1382 << "public static final String getName("
1383 << discriminatorStorageType
1384 << " value) ";
1385
1386 out.block([&] {
1387 out << "switch (value) ";
1388 out.block([&] {
Nirav Atre71ce0082018-08-01 15:39:49 -07001389 for (size_t idx = 0; idx < elements.size(); idx++) {
Nirav Atre66842a92018-06-28 18:14:13 -07001390 out << "case "
1391 << idx
1392 << ": { return \""
Nirav Atre71ce0082018-08-01 15:39:49 -07001393 << elements[idx].fieldName
Nirav Atre66842a92018-06-28 18:14:13 -07001394 << "\"; }\n";
1395 }
1396 out << "default: { return \"Unknown\"; }\n";
1397 }).endl();
1398 }).endl().endl();
1399
1400 out << "private hidl_discriminator() {}\n";
1401 }).endl().endl();
1402
Steven Moreland967c2432018-10-10 12:50:43 -07001403 out << "private " << discriminatorStorageType << " hidl_d = 0;\n";
Steven Moreland579b0f62018-10-25 13:40:47 -07001404 out << "private Object hidl_o = null;\n";
Nirav Atre66842a92018-06-28 18:14:13 -07001405
1406 for (const auto& field : *mFields) {
1407 // Setter
1408 out << "public void "
1409 << field->name()
1410 << "("
1411 << field->type().getJavaType(false)
1412 << " "
1413 << field->name()
1414 << ") ";
1415
1416 out.block([&] {
1417 out << "hidl_d = hidl_discriminator."
1418 << field->name()
1419 << ";\n";
1420
1421 out << "hidl_o = "
1422 << field->name()
1423 << ";\n";
1424 }).endl().endl();
1425
1426 // Getter
1427 out << "public "
1428 << field->type().getJavaType(false)
1429 << " "
1430 << field->name()
1431 << "() ";
1432
1433 out.block([&] {
1434 out << "if (hidl_d != hidl_discriminator."
1435 << field->name()
1436 << ") ";
1437
1438 out.block([&] {
1439 out << "String className = (hidl_o != null) ? "
1440 << "hidl_o.getClass().getName() : \"null\";\n";
1441
1442 out << "throw new IllegalStateException(\n";
1443 out.indent(2, [&] {
1444 out << "\"Read access to inactive union components is disallowed. \" +\n"
1445 << "\"Discriminator value is \" + hidl_d + \" (corresponding \" +\n"
1446 << "\"to \" + hidl_discriminator.getName(hidl_d) + \"), and \" +\n"
1447 << "\"hidl_o is of type \" + className + \".\");\n";
1448 });
1449 }).endl();
1450
1451 out << "if (hidl_o != null && !"
1452 << field->type().getJavaTypeClass()
1453 << ".class.isInstance(hidl_o)) ";
1454
1455 out.block([&] {
1456 out << "throw new Error(\"Union is in a corrupted state.\");\n";
1457 }).endl();
1458
1459 out << "return ("
1460 << field->type().getJavaTypeCast("hidl_o")
1461 << ");\n";
1462 }).endl().endl();
1463 }
1464
1465 out << "// Utility method\n"
1466 << "public "
1467 << discriminatorStorageType
1468 << " getDiscriminator() { return hidl_d; }\n\n";
1469
Steven Moreland5dec82f2018-10-10 13:33:29 -07001470 } else {
Nirav Atre66842a92018-06-28 18:14:13 -07001471 for (const auto& field : *mFields) {
1472 field->emitDocComment(out);
1473
1474 out << "public ";
1475 field->type().emitJavaFieldInitializer(out, field->name());
1476 }
1477
Andreas Huber85eabdb2016-08-25 11:24:49 -07001478 out << "\n";
1479 }
1480
Yifan Hongec102272016-12-20 18:10:07 -08001481 ////////////////////////////////////////////////////////////////////////////
1482
1483 if (canCheckEquality()) {
Yifan Hong7d1839f2017-02-22 13:24:29 -08001484 out << "@Override\npublic final boolean equals(Object otherObject) ";
Yifan Hongec102272016-12-20 18:10:07 -08001485 out.block([&] {
Yifan Hong7d1839f2017-02-22 13:24:29 -08001486 out.sIf("this == otherObject", [&] {
1487 out << "return true;\n";
1488 }).endl();
1489 out.sIf("otherObject == null", [&] {
1490 out << "return false;\n";
1491 }).endl();
Yifan Hong45b331b2017-03-27 12:59:54 -07001492 // Though class is final, we use getClass instead of instanceof to be explicit.
1493 out.sIf("otherObject.getClass() != " + fullJavaName() + ".class", [&] {
Yifan Hong7d1839f2017-02-22 13:24:29 -08001494 out << "return false;\n";
1495 }).endl();
1496 out << fullJavaName() << " other = (" << fullJavaName() << ")otherObject;\n";
Nirav Atre66842a92018-06-28 18:14:13 -07001497
Steven Moreland5dec82f2018-10-10 13:33:29 -07001498 if (mStyle == STYLE_SAFE_UNION) {
Nirav Atre66842a92018-06-28 18:14:13 -07001499 out.sIf("this.hidl_d != other.hidl_d", [&] {
Yifan Hongec102272016-12-20 18:10:07 -08001500 out << "return false;\n";
1501 }).endl();
Nirav Atre66842a92018-06-28 18:14:13 -07001502 out.sIf("!android.os.HidlSupport.deepEquals(this.hidl_o, other.hidl_o)", [&] {
1503 out << "return false;\n";
1504 }).endl();
Steven Moreland5dec82f2018-10-10 13:33:29 -07001505 } else {
Nirav Atre66842a92018-06-28 18:14:13 -07001506 for (const auto &field : *mFields) {
1507 std::string condition = (field->type().isScalar() || field->type().isEnum())
1508 ? "this." + field->name() + " != other." + field->name()
1509 : ("!android.os.HidlSupport.deepEquals(this." + field->name()
1510 + ", other." + field->name() + ")");
1511 out.sIf(condition, [&] {
1512 out << "return false;\n";
1513 }).endl();
1514 }
Yifan Hongec102272016-12-20 18:10:07 -08001515 }
1516 out << "return true;\n";
1517 }).endl().endl();
1518
Yifan Hong7d1839f2017-02-22 13:24:29 -08001519 out << "@Override\npublic final int hashCode() ";
Yifan Hongec102272016-12-20 18:10:07 -08001520 out.block([&] {
Yifan Hong7d1839f2017-02-22 13:24:29 -08001521 out << "return java.util.Objects.hash(\n";
1522 out.indent(2, [&] {
Steven Moreland5dec82f2018-10-10 13:33:29 -07001523 if (mStyle == STYLE_SAFE_UNION) {
Nirav Atre66842a92018-06-28 18:14:13 -07001524 out << "android.os.HidlSupport.deepHashCode(this.hidl_o),\n"
1525 << "java.util.Objects.hashCode(this.hidl_d)";
1526 } else {
1527 out.join(mFields->begin(), mFields->end(), ", \n", [&] (const auto &field) {
1528 out << "android.os.HidlSupport.deepHashCode(this." << field->name() << ")";
1529 });
1530 }
Yifan Hong7d1839f2017-02-22 13:24:29 -08001531 });
Yifan Hongec102272016-12-20 18:10:07 -08001532 out << ");\n";
1533 }).endl().endl();
Yifan Hong42c68432017-03-13 16:22:03 -07001534 } else {
1535 out << "// equals() is not generated for " << localName() << "\n";
Yifan Hongec102272016-12-20 18:10:07 -08001536 }
1537
1538 ////////////////////////////////////////////////////////////////////////////
1539
Yifan Honge45b5302017-02-22 10:49:07 -08001540 out << "@Override\npublic final String toString() ";
1541 out.block([&] {
1542 out << "java.lang.StringBuilder builder = new java.lang.StringBuilder();\n"
1543 << "builder.append(\"{\");\n";
Nirav Atre66842a92018-06-28 18:14:13 -07001544
Steven Moreland5dec82f2018-10-10 13:33:29 -07001545 if (mStyle == STYLE_SAFE_UNION) {
Nirav Atre66842a92018-06-28 18:14:13 -07001546 out << "switch (this.hidl_d) {\n";
1547 out.indent();
Yifan Honge45b5302017-02-22 10:49:07 -08001548 }
Nirav Atre66842a92018-06-28 18:14:13 -07001549
1550 for (const auto &field : *mFields) {
1551 if (mStyle == STYLE_SAFE_UNION) {
1552 out << "case hidl_discriminator."
1553 << field->name()
1554 << ": ";
1555
1556 out.block([&] {
1557 out << "builder.append(\""
1558 << "."
1559 << field->name()
1560 << " = \");\n";
1561
1562 field->type().emitJavaDump(out, "builder", "this." + field->name() + "()");
1563 out << "break;\n";
1564 }).endl();
1565 }
1566 else {
1567 out << "builder.append(\"";
1568 if (field != *(mFields->begin())) {
1569 out << ", ";
1570 }
1571 out << "." << field->name() << " = \");\n";
1572 field->type().emitJavaDump(out, "builder", "this." + field->name());
1573 }
1574 }
1575
Steven Moreland5dec82f2018-10-10 13:33:29 -07001576 if (mStyle == STYLE_SAFE_UNION) {
Nirav Atree3cae852018-07-16 12:19:16 -07001577 out << "default: ";
Steven Moreland5add34d2018-11-08 16:31:30 -08001578 out.block([&] { emitJavaSafeUnionUnknownDiscriminatorError(out, true /*fatal*/); })
1579 .endl();
Nirav Atre66842a92018-06-28 18:14:13 -07001580
1581 out.unindent();
1582 out << "}\n";
1583 }
1584
Yifan Honge45b5302017-02-22 10:49:07 -08001585 out << "builder.append(\"}\");\nreturn builder.toString();\n";
1586 }).endl().endl();
1587
Nirav Atre66842a92018-06-28 18:14:13 -07001588 CompoundLayout layout = getCompoundAlignmentAndSize();
Martijn Coenenb2a861c2017-04-18 15:54:25 -07001589
Yifan Honge45b5302017-02-22 10:49:07 -08001590 ////////////////////////////////////////////////////////////////////////////
1591
Yifan Hong1af73532016-11-09 14:32:58 -08001592 out << "public final void readFromParcel(android.os.HwParcel parcel) {\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -07001593 out.indent();
Howard Chenaf084db2017-12-27 18:46:39 +08001594 if (containsInterface()) {
Nirav Atre66842a92018-06-28 18:14:13 -07001595
Steven Moreland5dec82f2018-10-10 13:33:29 -07001596 if (mStyle == STYLE_SAFE_UNION) {
Nirav Atre66842a92018-06-28 18:14:13 -07001597 out << "hidl_d = ";
1598 getUnionDiscriminatorType()->emitJavaReaderWriter(
1599 out, "parcel", "hidl_d", true);
1600
1601 out << "switch (hidl_d) {\n";
1602 out.indent();
1603 }
1604
Howard Chenaf084db2017-12-27 18:46:39 +08001605 for (const auto& field : *mFields) {
Nirav Atre66842a92018-06-28 18:14:13 -07001606 if (mStyle == STYLE_SAFE_UNION) {
1607 out << "case hidl_discriminator."
1608 << field->name()
1609 << ": ";
1610
1611 out.block([&] {
1612 out << "hidl_o = ";
1613 field->type().emitJavaReaderWriter(out, "parcel", "hidl_o", true);
1614
1615 out << "break;\n";
1616 }).endl();
1617 } else {
1618 out << field->name() << " = ";
1619 field->type().emitJavaReaderWriter(out, "parcel", field->name(), true);
1620 }
1621 }
1622
Steven Moreland5dec82f2018-10-10 13:33:29 -07001623 if (mStyle == STYLE_SAFE_UNION) {
Nirav Atree3cae852018-07-16 12:19:16 -07001624 out << "default: ";
Steven Moreland5add34d2018-11-08 16:31:30 -08001625 out.block([&] { emitJavaSafeUnionUnknownDiscriminatorError(out, false /*fatal*/); })
1626 .endl();
Nirav Atre66842a92018-06-28 18:14:13 -07001627
1628 out.unindent();
1629 out << "}\n";
Howard Chenaf084db2017-12-27 18:46:39 +08001630 }
1631 } else {
1632 out << "android.os.HwBlob blob = parcel.readBuffer(";
Nirav Atre66842a92018-06-28 18:14:13 -07001633 out << layout.overall.size << " /* size */);\n";
Howard Chenaf084db2017-12-27 18:46:39 +08001634 out << "readEmbeddedFromParcel(parcel, blob, 0 /* parentOffset */);\n";
1635 }
Andreas Huber85eabdb2016-08-25 11:24:49 -07001636 out.unindent();
1637 out << "}\n\n";
1638
Andreas Huberf630bc82016-09-09 14:52:25 -07001639 ////////////////////////////////////////////////////////////////////////////
1640
Martijn Coenenb2a861c2017-04-18 15:54:25 -07001641 size_t vecAlign, vecSize;
1642 VectorType::getAlignmentAndSizeStatic(&vecAlign, &vecSize);
1643
Howard Chenaf084db2017-12-27 18:46:39 +08001644 out << "public static final java.util.ArrayList<" << localName()
Yifan Hong1af73532016-11-09 14:32:58 -08001645 << "> readVectorFromParcel(android.os.HwParcel parcel) {\n";
Andreas Huberf630bc82016-09-09 14:52:25 -07001646 out.indent();
1647
Howard Chenaf084db2017-12-27 18:46:39 +08001648 out << "java.util.ArrayList<" << localName() << "> _hidl_vec = new java.util.ArrayList();\n";
Andreas Huberf630bc82016-09-09 14:52:25 -07001649
Howard Chenaf084db2017-12-27 18:46:39 +08001650 if (containsInterface()) {
1651 out << "int size = parcel.readInt32();\n";
1652 out << "for(int i = 0 ; i < size; i ++) {\n";
1653 out.indent();
1654 out << fullJavaName() << " tmp = ";
1655 emitJavaReaderWriter(out, "parcel", "tmp", true);
1656 out << "_hidl_vec.add(tmp);\n";
1657 out.unindent();
1658 out << "}\n";
1659 } else {
1660 out << "android.os.HwBlob _hidl_blob = parcel.readBuffer(";
1661 out << vecSize << " /* sizeof hidl_vec<T> */);\n\n";
Andreas Huberf630bc82016-09-09 14:52:25 -07001662
Howard Chenaf084db2017-12-27 18:46:39 +08001663 VectorType::EmitJavaFieldReaderWriterForElementType(out, 0 /* depth */, this, "parcel",
1664 "_hidl_blob", "_hidl_vec", "0",
1665 true /* isReader */);
Andreas Huber85eabdb2016-08-25 11:24:49 -07001666 }
Howard Chenaf084db2017-12-27 18:46:39 +08001667 out << "\nreturn _hidl_vec;\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -07001668 out.unindent();
1669 out << "}\n\n";
Howard Chenaf084db2017-12-27 18:46:39 +08001670 ////////////////////////////////////////////////////////////////////////////
1671 if (containsInterface()) {
1672 out << "// readEmbeddedFromParcel is not generated()\n";
1673 } else {
1674 out << "public final void readEmbeddedFromParcel(\n";
1675 out.indent(2);
1676 out << "android.os.HwParcel parcel, android.os.HwBlob _hidl_blob, long _hidl_offset) {\n";
1677 out.unindent();
Nirav Atre66842a92018-06-28 18:14:13 -07001678
Steven Moreland5dec82f2018-10-10 13:33:29 -07001679 if (mStyle == STYLE_SAFE_UNION) {
Nirav Atre66842a92018-06-28 18:14:13 -07001680 getUnionDiscriminatorType()->emitJavaFieldReaderWriter(
1681 out, 0 /* depth */, "parcel", "_hidl_blob", "hidl_d",
1682 "_hidl_offset + " + std::to_string(layout.discriminator.offset),
1683 true /* isReader */);
1684
1685 out << "switch (this.hidl_d) {\n";
1686 out.indent();
1687 }
1688
1689 size_t offset = layout.innerStruct.offset;
Howard Chenaf084db2017-12-27 18:46:39 +08001690 for (const auto& field : *mFields) {
Howard Chenaf084db2017-12-27 18:46:39 +08001691
Nirav Atre66842a92018-06-28 18:14:13 -07001692 if (mStyle == STYLE_SAFE_UNION) {
1693 out << "case hidl_discriminator."
1694 << field->name()
1695 << ": ";
Howard Chenaf084db2017-12-27 18:46:39 +08001696
Nirav Atre66842a92018-06-28 18:14:13 -07001697 out.block([&] {
1698 field->type().emitJavaFieldDefaultInitialValue(out, "hidl_o");
1699 field->type().emitJavaFieldReaderWriter(
1700 out, 0 /* depth */, "parcel", "_hidl_blob", "hidl_o",
1701 "_hidl_offset + " + std::to_string(offset), true /* isReader */);
1702
1703 out << "break;\n";
1704 }).endl();
1705 } else {
1706 size_t fieldAlign, fieldSize;
1707 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
1708
1709 offset += Layout::getPad(offset, fieldAlign);
1710 field->type().emitJavaFieldReaderWriter(
1711 out, 0 /* depth */, "parcel", "_hidl_blob", field->name(),
1712 "_hidl_offset + " + std::to_string(offset), true /* isReader */);
1713 offset += fieldSize;
1714 }
1715 }
1716
Steven Moreland5dec82f2018-10-10 13:33:29 -07001717 if (mStyle == STYLE_SAFE_UNION) {
Nirav Atree3cae852018-07-16 12:19:16 -07001718 out << "default: ";
Steven Moreland5add34d2018-11-08 16:31:30 -08001719 out.block([&] { emitJavaSafeUnionUnknownDiscriminatorError(out, false /*fatal*/); })
1720 .endl();
Nirav Atre66842a92018-06-28 18:14:13 -07001721
1722 out.unindent();
1723 out << "}\n";
Howard Chenaf084db2017-12-27 18:46:39 +08001724 }
1725 out.unindent();
1726 out << "}\n\n";
1727 }
Andreas Huber85eabdb2016-08-25 11:24:49 -07001728
Andreas Huberf630bc82016-09-09 14:52:25 -07001729 ////////////////////////////////////////////////////////////////////////////
1730
Yifan Hong1af73532016-11-09 14:32:58 -08001731 out << "public final void writeToParcel(android.os.HwParcel parcel) {\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -07001732 out.indent();
1733
Howard Chenaf084db2017-12-27 18:46:39 +08001734 if (containsInterface()) {
Steven Moreland5dec82f2018-10-10 13:33:29 -07001735 if (mStyle == STYLE_SAFE_UNION) {
Nirav Atre66842a92018-06-28 18:14:13 -07001736 getUnionDiscriminatorType()->emitJavaReaderWriter(
1737 out, "parcel", "hidl_d", false);
1738
1739 out << "switch (this.hidl_d) {\n";
1740 out.indent();
1741 }
1742
Howard Chenaf084db2017-12-27 18:46:39 +08001743 for (const auto& field : *mFields) {
Nirav Atre66842a92018-06-28 18:14:13 -07001744 if (mStyle == STYLE_SAFE_UNION) {
1745 out << "case hidl_discriminator."
1746 << field->name()
1747 << ": ";
1748
1749 out.block([&] {
1750 field->type().emitJavaReaderWriter(out, "parcel", field->name() + "()", false);
1751 out << "break;\n";
1752 }).endl();
1753 } else {
1754 field->type().emitJavaReaderWriter(out, "parcel", field->name(), false);
1755 }
1756 }
1757
Steven Moreland5dec82f2018-10-10 13:33:29 -07001758 if (mStyle == STYLE_SAFE_UNION) {
Nirav Atree3cae852018-07-16 12:19:16 -07001759 out << "default: ";
Steven Moreland5add34d2018-11-08 16:31:30 -08001760 out.block([&] { emitJavaSafeUnionUnknownDiscriminatorError(out, true /*fatal*/); })
1761 .endl();
Nirav Atre66842a92018-06-28 18:14:13 -07001762
1763 out.unindent();
1764 out << "}\n";
Howard Chenaf084db2017-12-27 18:46:39 +08001765 }
1766 } else {
Nirav Atre66842a92018-06-28 18:14:13 -07001767 out << "android.os.HwBlob _hidl_blob = new android.os.HwBlob("
1768 << layout.overall.size
Howard Chenaf084db2017-12-27 18:46:39 +08001769 << " /* size */);\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -07001770
Howard Chenaf084db2017-12-27 18:46:39 +08001771 out << "writeEmbeddedToBlob(_hidl_blob, 0 /* parentOffset */);\n"
1772 << "parcel.writeBuffer(_hidl_blob);\n";
1773 }
Andreas Huber85eabdb2016-08-25 11:24:49 -07001774 out.unindent();
1775 out << "}\n\n";
1776
Andreas Huberf630bc82016-09-09 14:52:25 -07001777 ////////////////////////////////////////////////////////////////////////////
1778
Andreas Huberf630bc82016-09-09 14:52:25 -07001779 out << "public static final void writeVectorToParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -07001780 out.indent(2);
Howard Chenaf084db2017-12-27 18:46:39 +08001781 out << "android.os.HwParcel parcel, java.util.ArrayList<" << localName() << "> _hidl_vec) {\n";
Andreas Huberf630bc82016-09-09 14:52:25 -07001782 out.unindent();
1783
Howard Chenaf084db2017-12-27 18:46:39 +08001784 if (containsInterface()) {
1785 out << "parcel.writeInt32(_hidl_vec.size());\n";
Nirav Atre66842a92018-06-28 18:14:13 -07001786 out << "for(" << fullJavaName() << " tmp: _hidl_vec) ";
1787 out.block([&] {
1788 emitJavaReaderWriter(out, "parcel", "tmp", false);
1789 }).endl();
Howard Chenaf084db2017-12-27 18:46:39 +08001790 } else {
1791 out << "android.os.HwBlob _hidl_blob = new android.os.HwBlob(" << vecSize
1792 << " /* sizeof(hidl_vec<T>) */);\n";
Andreas Huberf630bc82016-09-09 14:52:25 -07001793
Howard Chenaf084db2017-12-27 18:46:39 +08001794 VectorType::EmitJavaFieldReaderWriterForElementType(out, 0 /* depth */, this, "parcel",
1795 "_hidl_blob", "_hidl_vec", "0",
1796 false /* isReader */);
Andreas Huberf630bc82016-09-09 14:52:25 -07001797
Howard Chenaf084db2017-12-27 18:46:39 +08001798 out << "\nparcel.writeBuffer(_hidl_blob);\n";
1799 }
Andreas Huberf630bc82016-09-09 14:52:25 -07001800 out.unindent();
1801 out << "}\n\n";
Andreas Huberf630bc82016-09-09 14:52:25 -07001802 ////////////////////////////////////////////////////////////////////////////
1803
Howard Chenaf084db2017-12-27 18:46:39 +08001804 if (containsInterface()) {
Nirav Atre66842a92018-06-28 18:14:13 -07001805 out << "// writeEmbeddedToBlob() is not generated\n";
Howard Chenaf084db2017-12-27 18:46:39 +08001806 } else {
1807 out << "public final void writeEmbeddedToBlob(\n";
1808 out.indent(2);
1809 out << "android.os.HwBlob _hidl_blob, long _hidl_offset) {\n";
1810 out.unindent();
Nirav Atre45e76d52018-06-04 11:49:14 -07001811
Steven Moreland5dec82f2018-10-10 13:33:29 -07001812 if (mStyle == STYLE_SAFE_UNION) {
Nirav Atre66842a92018-06-28 18:14:13 -07001813 getUnionDiscriminatorType()->emitJavaFieldReaderWriter(
1814 out, 0 /* depth */, "parcel", "_hidl_blob", "hidl_d",
1815 "_hidl_offset + " + std::to_string(layout.discriminator.offset),
1816 false /* isReader */);
Nirav Atre45e76d52018-06-04 11:49:14 -07001817
Nirav Atre66842a92018-06-28 18:14:13 -07001818 out << "switch (this.hidl_d) {\n";
1819 out.indent();
Andreas Huber85eabdb2016-08-25 11:24:49 -07001820 }
1821
Nirav Atre66842a92018-06-28 18:14:13 -07001822 size_t offset = layout.innerStruct.offset;
1823 for (const auto& field : *mFields) {
1824 if (mStyle == STYLE_SAFE_UNION) {
1825 out << "case hidl_discriminator."
1826 << field->name()
1827 << ": ";
1828
1829 out.block([&] {
1830 field->type().emitJavaFieldReaderWriter(
1831 out, 0 /* depth */, "parcel", "_hidl_blob", field->name() + "()",
1832 "_hidl_offset + " + std::to_string(offset), false /* isReader */);
1833
1834 out << "break;\n";
1835 }).endl();
1836 } else {
1837 size_t fieldAlign, fieldSize;
1838 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
1839
1840 offset += Layout::getPad(offset, fieldAlign);
1841 field->type().emitJavaFieldReaderWriter(
1842 out, 0 /* depth */, "parcel", "_hidl_blob", field->name(),
1843 "_hidl_offset + " + std::to_string(offset), false /* isReader */);
1844 offset += fieldSize;
1845 }
1846 }
1847
Steven Moreland5dec82f2018-10-10 13:33:29 -07001848 if (mStyle == STYLE_SAFE_UNION) {
Nirav Atree3cae852018-07-16 12:19:16 -07001849 out << "default: ";
Steven Moreland5add34d2018-11-08 16:31:30 -08001850 out.block([&] { emitJavaSafeUnionUnknownDiscriminatorError(out, true /*fatal*/); })
1851 .endl();
Nirav Atre66842a92018-06-28 18:14:13 -07001852
1853 out.unindent();
1854 out << "}\n";
1855 }
Howard Chenaf084db2017-12-27 18:46:39 +08001856 out.unindent();
1857 out << "}\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -07001858 }
1859
1860 out.unindent();
Andreas Huber85eabdb2016-08-25 11:24:49 -07001861 out << "};\n\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -07001862}
1863
Andreas Huber881227d2016-08-02 14:20:21 -07001864void CompoundType::emitStructReaderWriter(
1865 Formatter &out, const std::string &prefix, bool isReader) const {
Yifan Hong244e82d2016-11-11 11:13:57 -08001866
1867 std::string space = prefix.empty() ? "" : (prefix + "::");
1868
Andreas Huber881227d2016-08-02 14:20:21 -07001869 out << "::android::status_t "
Yifan Hong244e82d2016-11-11 11:13:57 -08001870 << (isReader ? "readEmbeddedFromParcel"
1871 : "writeEmbeddedToParcel")
1872 << "(\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001873
Yifan Hong0a68a282016-10-21 16:32:34 -07001874 out.indent(2);
Andreas Huber881227d2016-08-02 14:20:21 -07001875
Nirav Atre97b7e9c2018-07-26 19:29:40 -07001876 const std::string name = "obj";
Andreas Huber881227d2016-08-02 14:20:21 -07001877 if (isReader) {
Martijn Coenenb2a861c2017-04-18 15:54:25 -07001878 out << "const " << space << localName() << " &" << name << ",\n";
Andreas Huber8a82ff72016-08-04 10:29:39 -07001879 out << "const ::android::hardware::Parcel &parcel,\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001880 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -08001881 out << "const " << space << localName() << " &" << name << ",\n";
Andreas Huber8a82ff72016-08-04 10:29:39 -07001882 out << "::android::hardware::Parcel *parcel,\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001883 }
1884
1885 out << "size_t parentHandle,\n"
1886 << "size_t parentOffset)";
1887
Andreas Huber881227d2016-08-02 14:20:21 -07001888 out << " {\n";
1889
Yifan Hong0a68a282016-10-21 16:32:34 -07001890 out.unindent(2);
Andreas Huber881227d2016-08-02 14:20:21 -07001891 out.indent();
1892
Iliyan Malchev549e2592016-08-10 08:59:12 -07001893 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001894
Steven Moreland5dec82f2018-10-10 13:33:29 -07001895 if (mStyle == STYLE_SAFE_UNION) {
Nirav Atre45e76d52018-06-04 11:49:14 -07001896 out << "switch (" << name << ".getDiscriminator()) {\n";
1897 out.indent();
1898 }
1899
Andreas Huber881227d2016-08-02 14:20:21 -07001900 for (const auto &field : *mFields) {
1901 if (!field->type().needsEmbeddedReadWrite()) {
1902 continue;
1903 }
1904
Nirav Atre45e76d52018-06-04 11:49:14 -07001905 if (mStyle == STYLE_SAFE_UNION) {
1906 out << "case " << fullName() << "::hidl_discriminator::"
1907 << field->name() << ": {\n";
1908 out.indent();
1909 }
1910
1911 const std::string fieldName = (mStyle == STYLE_SAFE_UNION)
Nirav Atre97b7e9c2018-07-26 19:29:40 -07001912 ? (name + "." + field->name() + "()")
1913 : (name + "." + field->name());
Nirav Atre45e76d52018-06-04 11:49:14 -07001914
1915 const std::string fieldOffset = (mStyle == STYLE_SAFE_UNION)
Nirav Atre12c597a2018-06-28 09:35:24 -07001916 ? (name + ".hidl_getUnionOffset() " +
1917 "/* safe_union: union offset into struct */")
Nirav Atre45e76d52018-06-04 11:49:14 -07001918 : ("offsetof(" + fullName() + ", " + field->name() + ")");
1919
Andreas Huber881227d2016-08-02 14:20:21 -07001920 field->type().emitReaderWriterEmbedded(
1921 out,
Andreas Huberf9d49f12016-09-12 14:58:36 -07001922 0 /* depth */,
Nirav Atre45e76d52018-06-04 11:49:14 -07001923 fieldName,
Yifan Hongbe2a3732016-10-05 13:33:41 -07001924 field->name() /* sanitizedName */,
Andreas Huber881227d2016-08-02 14:20:21 -07001925 false /* nameIsPointer */,
1926 "parcel",
1927 !isReader /* parcelObjIsPointer */,
1928 isReader,
Andreas Huber737080b2016-08-02 15:38:04 -07001929 ErrorMode_Return,
Andreas Huber881227d2016-08-02 14:20:21 -07001930 "parentHandle",
Nirav Atre45e76d52018-06-04 11:49:14 -07001931 "parentOffset + " + fieldOffset);
1932
1933 if (mStyle == STYLE_SAFE_UNION) {
1934 out << "break;\n";
1935 out.unindent();
1936 out << "}\n";
1937 }
1938 }
1939
Steven Moreland5dec82f2018-10-10 13:33:29 -07001940 if (mStyle == STYLE_SAFE_UNION) {
Nirav Atre45e76d52018-06-04 11:49:14 -07001941 out << "default: { break; }\n";
1942 out.unindent();
1943 out << "}\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001944 }
1945
Iliyan Malchev549e2592016-08-10 08:59:12 -07001946 out << "return _hidl_err;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001947
1948 out.unindent();
1949 out << "}\n\n";
1950}
1951
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -07001952void CompoundType::emitResolveReferenceDef(Formatter& out, const std::string& prefix,
1953 bool isReader) const {
Yifan Hong244e82d2016-11-11 11:13:57 -08001954 out << "::android::status_t ";
1955 const std::string space(prefix.empty() ? "" : (prefix + "::"));
Yifan Hong00f47172016-09-30 14:40:45 -07001956
1957 bool useParent = false;
1958 for (const auto &field : *mFields) {
1959 if (field->type().useParentInEmitResolveReferencesEmbedded()) {
1960 useParent = true;
1961 break;
1962 }
1963 }
1964
1965 std::string parentHandleName = useParent ? "parentHandle" : "/* parentHandle */";
1966 std::string parentOffsetName = useParent ? "parentOffset" : "/* parentOffset */";
1967
Yifan Hongbf459bc2016-08-23 16:50:37 -07001968 if (isReader) {
Yifan Hong244e82d2016-11-11 11:13:57 -08001969 out << "readEmbeddedReferenceFromParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -07001970 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -08001971 out << space + localName() + " *obj,\n"
1972 << "const ::android::hardware::Parcel &parcel,\n"
Yifan Hong00f47172016-09-30 14:40:45 -07001973 << "size_t " << parentHandleName << ", "
1974 << "size_t " << parentOffsetName << ")\n";
Yifan Hong0a68a282016-10-21 16:32:34 -07001975 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001976 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -08001977 out << "writeEmbeddedReferenceToParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -07001978 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -08001979 out << "const " << space + localName() + " &obj,\n"
1980 << "::android::hardware::Parcel *parcel,\n"
Yifan Hong00f47172016-09-30 14:40:45 -07001981 << "size_t " << parentHandleName << ", "
Yifan Hong244e82d2016-11-11 11:13:57 -08001982 << "size_t " << parentOffsetName << ")\n";
Yifan Hong0a68a282016-10-21 16:32:34 -07001983 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001984 }
1985
1986 out << " {\n";
1987
1988 out.indent();
1989
1990 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
1991
Yifan Hong244e82d2016-11-11 11:13:57 -08001992 const std::string nameDeref(isReader ? "obj->" : "obj.");
Yifan Hong00f47172016-09-30 14:40:45 -07001993 // if not useParent, then parentName and offsetText
1994 // should not be used at all, then the #error should not be emitted.
1995 std::string error = useParent ? "" : "\n#error\n";
1996
Nirav Atre45e76d52018-06-04 11:49:14 -07001997 if (mStyle == STYLE_SAFE_UNION) {
1998 out << "switch (" << nameDeref << "getDiscriminator()) {\n";
1999 out.indent();
2000 }
2001
Yifan Hongbf459bc2016-08-23 16:50:37 -07002002 for (const auto &field : *mFields) {
2003 if (!field->type().needsResolveReferences()) {
2004 continue;
2005 }
2006
Nirav Atre45e76d52018-06-04 11:49:14 -07002007 if (mStyle == STYLE_SAFE_UNION) {
2008 out << "case " << fullName() << "::hidl_discriminator::"
2009 << field->name() << ": {\n";
2010 out.indent();
2011 }
2012
2013 const std::string fieldName = (mStyle == STYLE_SAFE_UNION)
2014 ? (nameDeref + field->name() + "()")
2015 : (nameDeref + field->name());
2016
2017 const std::string fieldOffset = (mStyle == STYLE_SAFE_UNION)
Nirav Atre12c597a2018-06-28 09:35:24 -07002018 ? (nameDeref + "hidl_getUnionOffset() " +
2019 "/* safe_union: union offset into struct */")
Nirav Atre45e76d52018-06-04 11:49:14 -07002020 : ("offsetof(" + fullName() + ", " + field->name() + ")");
2021
Yifan Hongbf459bc2016-08-23 16:50:37 -07002022 field->type().emitResolveReferencesEmbedded(
2023 out,
2024 0 /* depth */,
Nirav Atre45e76d52018-06-04 11:49:14 -07002025 fieldName,
Yifan Hongbf459bc2016-08-23 16:50:37 -07002026 field->name() /* sanitizedName */,
2027 false, // nameIsPointer
2028 "parcel", // const std::string &parcelObj,
2029 !isReader, // bool parcelObjIsPointer,
2030 isReader, // bool isReader,
2031 ErrorMode_Return,
Yifan Hong00f47172016-09-30 14:40:45 -07002032 parentHandleName + error,
2033 parentOffsetName
Nirav Atre45e76d52018-06-04 11:49:14 -07002034 + " + "
2035 + fieldOffset
Yifan Hong00f47172016-09-30 14:40:45 -07002036 + error);
Nirav Atre45e76d52018-06-04 11:49:14 -07002037
2038 if (mStyle == STYLE_SAFE_UNION) {
2039 out << "break;\n";
2040 out.unindent();
2041 out << "}\n";
2042 }
2043 }
2044
2045 if (mStyle == STYLE_SAFE_UNION) {
2046 out << "default: { _hidl_err = ::android::BAD_VALUE; break; }\n";
2047 out.unindent();
2048 out << "}\n";
Yifan Hongbf459bc2016-08-23 16:50:37 -07002049 }
2050
Yifan Hongbf459bc2016-08-23 16:50:37 -07002051 out << "return _hidl_err;\n";
2052
2053 out.unindent();
2054 out << "}\n\n";
2055}
2056
Andreas Huber881227d2016-08-02 14:20:21 -07002057bool CompoundType::needsEmbeddedReadWrite() const {
Nirav Atre45e76d52018-06-04 11:49:14 -07002058 if (mStyle == STYLE_UNION) {
Andreas Huber881227d2016-08-02 14:20:21 -07002059 return false;
2060 }
2061
2062 for (const auto &field : *mFields) {
2063 if (field->type().needsEmbeddedReadWrite()) {
2064 return true;
2065 }
2066 }
2067
2068 return false;
2069}
2070
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07002071bool CompoundType::deepNeedsResolveReferences(std::unordered_set<const Type*>* visited) const {
Nirav Atre45e76d52018-06-04 11:49:14 -07002072 if (mStyle == STYLE_UNION) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07002073 return false;
2074 }
2075
2076 for (const auto &field : *mFields) {
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07002077 if (field->type().needsResolveReferences(visited)) {
Yifan Hongbf459bc2016-08-23 16:50:37 -07002078 return true;
2079 }
2080 }
2081
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07002082 return Scope::deepNeedsResolveReferences(visited);
Yifan Hongbf459bc2016-08-23 16:50:37 -07002083}
2084
Andreas Huber881227d2016-08-02 14:20:21 -07002085bool CompoundType::resultNeedsDeref() const {
Howard Chenecfb4512017-11-21 18:28:53 +08002086 return !containsInterface() ;
Andreas Huber881227d2016-08-02 14:20:21 -07002087}
2088
Steven Moreland368e4602018-02-16 14:21:49 -08002089void CompoundType::emitVtsTypeDeclarations(Formatter& out) const {
Zhuoyao Zhangbf828c82016-10-26 14:15:10 -07002090 out << "name: \"" << fullName() << "\"\n";
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -07002091 out << "type: " << getVtsType() << "\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07002092
2093 // Emit declaration for each subtype.
2094 for (const auto &type : getSubTypes()) {
2095 switch (mStyle) {
2096 case STYLE_STRUCT:
2097 {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -07002098 out << "sub_struct: {\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07002099 break;
2100 }
2101 case STYLE_UNION:
2102 {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -07002103 out << "sub_union: {\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07002104 break;
2105 }
Nirav Atre45e76d52018-06-04 11:49:14 -07002106 case STYLE_SAFE_UNION:
2107 {
2108 out << "sub_safe_union: {\n";
2109 break;
2110 }
2111 default:
2112 {
2113 CHECK(!"Should not be here");
2114 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07002115 }
2116 out.indent();
Steven Moreland368e4602018-02-16 14:21:49 -08002117 type->emitVtsTypeDeclarations(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07002118 out.unindent();
2119 out << "}\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07002120 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07002121
2122 // Emit declaration for each field.
2123 for (const auto &field : *mFields) {
2124 switch (mStyle) {
2125 case STYLE_STRUCT:
2126 {
2127 out << "struct_value: {\n";
2128 break;
2129 }
2130 case STYLE_UNION:
2131 {
2132 out << "union_value: {\n";
2133 break;
2134 }
Nirav Atre45e76d52018-06-04 11:49:14 -07002135 case STYLE_SAFE_UNION:
2136 {
2137 out << "safe_union_value: {\n";
2138 break;
2139 }
2140 default:
2141 {
2142 CHECK(!"Should not be here");
2143 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07002144 }
2145 out.indent();
2146 out << "name: \"" << field->name() << "\"\n";
Steven Moreland368e4602018-02-16 14:21:49 -08002147 field->type().emitVtsAttributeType(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07002148 out.unindent();
2149 out << "}\n";
2150 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07002151}
2152
Steven Moreland368e4602018-02-16 14:21:49 -08002153void CompoundType::emitVtsAttributeType(Formatter& out) const {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -07002154 out << "type: " << getVtsType() << "\n";
Zhuoyao Zhangbf828c82016-10-26 14:15:10 -07002155 out << "predefined_type: \"" << fullName() << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07002156}
2157
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07002158bool CompoundType::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
Nirav Atre66842a92018-06-28 18:14:13 -07002159 if (mStyle == STYLE_UNION) {
Andreas Huber0fa9e392016-08-31 09:05:44 -07002160 return false;
2161 }
2162
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07002163 for (const auto* field : *mFields) {
2164 if (!field->get()->isJavaCompatible(visited)) {
Andreas Huber0fa9e392016-08-31 09:05:44 -07002165 return false;
2166 }
2167 }
2168
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07002169 return Scope::deepIsJavaCompatible(visited);
Andreas Huber85eabdb2016-08-25 11:24:49 -07002170}
2171
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07002172bool CompoundType::deepContainsPointer(std::unordered_set<const Type*>* visited) const {
2173 for (const auto* field : *mFields) {
2174 if (field->get()->containsPointer(visited)) {
Andreas Huber60d3b222017-03-30 09:10:56 -07002175 return true;
2176 }
2177 }
2178
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07002179 return Scope::deepContainsPointer(visited);
Andreas Huber60d3b222017-03-30 09:10:56 -07002180}
2181
Andreas Huber85eabdb2016-08-25 11:24:49 -07002182void CompoundType::getAlignmentAndSize(size_t *align, size_t *size) const {
Nirav Atre45e76d52018-06-04 11:49:14 -07002183 CompoundLayout layout = getCompoundAlignmentAndSize();
2184 *align = layout.overall.align;
2185 *size = layout.overall.size;
2186}
Andreas Huber85eabdb2016-08-25 11:24:49 -07002187
Nirav Atre45e76d52018-06-04 11:49:14 -07002188CompoundType::CompoundLayout CompoundType::getCompoundAlignmentAndSize() const {
2189 CompoundLayout compoundLayout;
2190
2191 // Local aliases for convenience
2192 Layout& overall = compoundLayout.overall;
2193 Layout& innerStruct = compoundLayout.innerStruct;
2194 Layout& discriminator = compoundLayout.discriminator;
2195
Steven Moreland5dec82f2018-10-10 13:33:29 -07002196 if (mStyle == STYLE_SAFE_UNION) {
Nirav Atre12c597a2018-06-28 09:35:24 -07002197 getUnionDiscriminatorType()->getAlignmentAndSize(
2198 &(discriminator.align), &(discriminator.size));
2199
2200 innerStruct.offset = discriminator.size;
2201 }
2202
Andreas Huber85eabdb2016-08-25 11:24:49 -07002203 for (const auto &field : *mFields) {
Nirav Atre45e76d52018-06-04 11:49:14 -07002204
Andreas Huber85eabdb2016-08-25 11:24:49 -07002205 // Each field is aligned according to its alignment requirement.
2206 // The surrounding structure's alignment is the maximum of its
2207 // fields' aligments.
Andreas Huber85eabdb2016-08-25 11:24:49 -07002208 size_t fieldAlign, fieldSize;
2209 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
Nirav Atre45e76d52018-06-04 11:49:14 -07002210 size_t lPad = Layout::getPad(innerStruct.size, fieldAlign);
Andreas Huber85eabdb2016-08-25 11:24:49 -07002211
Nirav Atre45e76d52018-06-04 11:49:14 -07002212 innerStruct.size = (mStyle == STYLE_STRUCT)
2213 ? (innerStruct.size + lPad + fieldSize)
2214 : std::max(innerStruct.size, fieldSize);
Andreas Huber85eabdb2016-08-25 11:24:49 -07002215
Nirav Atre45e76d52018-06-04 11:49:14 -07002216 innerStruct.align = std::max(innerStruct.align, fieldAlign);
2217 }
Andreas Huber85eabdb2016-08-25 11:24:49 -07002218
Nirav Atre12c597a2018-06-28 09:35:24 -07002219 // Pad the inner structure's size
Nirav Atre45e76d52018-06-04 11:49:14 -07002220 innerStruct.size += Layout::getPad(innerStruct.size,
2221 innerStruct.align);
2222
Nirav Atre12c597a2018-06-28 09:35:24 -07002223 // Compute its final offset
2224 innerStruct.offset += Layout::getPad(innerStruct.offset,
2225 innerStruct.align);
Nirav Atre45e76d52018-06-04 11:49:14 -07002226
Nirav Atre12c597a2018-06-28 09:35:24 -07002227 overall.size = innerStruct.offset + innerStruct.size;
Nirav Atre45e76d52018-06-04 11:49:14 -07002228
2229 // An empty struct/union still occupies a byte of space in C++.
2230 if (overall.size == 0) {
2231 overall.size = 1;
2232 }
2233
Nirav Atre12c597a2018-06-28 09:35:24 -07002234 // Pad the overall structure's size
Nirav Atre45e76d52018-06-04 11:49:14 -07002235 overall.align = std::max(innerStruct.align, discriminator.align);
2236 overall.size += Layout::getPad(overall.size, overall.align);
2237
2238 return compoundLayout;
2239}
2240
2241std::unique_ptr<ScalarType> CompoundType::getUnionDiscriminatorType() const {
2242 static const std::vector<std::pair<int, ScalarType::Kind> > scalars {
2243 {8, ScalarType::Kind::KIND_UINT8},
2244 {16, ScalarType::Kind::KIND_UINT16},
2245 {32, ScalarType::Kind::KIND_UINT32},
2246 };
2247
Steven Moreland967c2432018-10-10 12:50:43 -07002248 size_t numFields = mFields->size();
Nirav Atre45e76d52018-06-04 11:49:14 -07002249 auto kind = ScalarType::Kind::KIND_UINT64;
2250
2251 for (const auto& scalar : scalars) {
2252 if (numFields <= (1ULL << scalar.first)) {
2253 kind = scalar.second; break;
Andreas Huber85eabdb2016-08-25 11:24:49 -07002254 }
2255 }
2256
Yi Kong56758da2018-07-24 16:21:37 -07002257 return std::unique_ptr<ScalarType>(new ScalarType(kind, nullptr));
Nirav Atre45e76d52018-06-04 11:49:14 -07002258}
Andreas Huber85eabdb2016-08-25 11:24:49 -07002259
Nirav Atre45e76d52018-06-04 11:49:14 -07002260size_t CompoundType::Layout::getPad(size_t offset, size_t align) {
2261 size_t remainder = offset % align;
2262 return (remainder > 0) ? (align - remainder) : 0;
Andreas Huber70a59e12016-08-16 12:57:01 -07002263}
2264
Andreas Huberc9410c72016-07-28 12:18:40 -07002265} // namespace android
2266