blob: 19b189620bb2d9e6aa9ac89c84d2fc9caa8ae93e [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"
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070021#include <hidl-util/Formatter.h>
Andreas Huber5a545442016-08-03 10:44:56 -070022#include <android-base/logging.h>
23
Andreas Huberc9410c72016-07-28 12:18:40 -070024namespace android {
25
Yifan Honga4b53d02016-10-31 17:29:10 -070026CompoundType::CompoundType(Style style, const char *localName, const Location &location)
27 : Scope(localName, location),
Andreas Huber9ed827c2016-08-22 12:31:13 -070028 mStyle(style),
Andreas Huberc9410c72016-07-28 12:18:40 -070029 mFields(NULL) {
30}
31
Yifan Hong27e85db2016-11-09 15:45:52 -080032CompoundType::Style CompoundType::style() const {
33 return mStyle;
34}
35
Andreas Huber0d0f9a22016-08-17 10:26:11 -070036bool CompoundType::setFields(
37 std::vector<CompoundField *> *fields, std::string *errorMsg) {
Andreas Huberc9410c72016-07-28 12:18:40 -070038 mFields = fields;
Andreas Huber5a545442016-08-03 10:44:56 -070039
40 for (const auto &field : *fields) {
41 const Type &type = field->type();
42
Andreas Huber86a112b2016-10-19 14:25:16 -070043 if (type.isBinder()
44 || (type.isVector()
45 && static_cast<const VectorType *>(
46 &type)->isVectorOfBinders())) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -070047 *errorMsg =
48 "Structs/Unions must not contain references to interfaces.";
Andreas Huberb95ea8a2016-08-15 15:35:42 -070049
50 return false;
51 }
52
Andreas Huber5a545442016-08-03 10:44:56 -070053 if (mStyle == STYLE_UNION) {
54 if (type.needsEmbeddedReadWrite()) {
55 // Can't have those in a union.
56
Andreas Huber0d0f9a22016-08-17 10:26:11 -070057 *errorMsg =
58 "Unions must not contain any types that need fixup.";
Andreas Huber5a545442016-08-03 10:44:56 -070059
60 return false;
61 }
Andreas Huber5a545442016-08-03 10:44:56 -070062 }
63 }
64
65 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -070066}
67
Andreas Huberf630bc82016-09-09 14:52:25 -070068bool CompoundType::isCompoundType() const {
69 return true;
70}
71
Yifan Hongc6752dc2016-12-20 14:00:14 -080072bool CompoundType::canCheckEquality() const {
73 if (mStyle == STYLE_UNION) {
74 return false;
75 }
76 for (const auto &field : *mFields) {
77 if (!field->type().canCheckEquality()) {
78 return false;
79 }
80 }
81 return true;
82}
83
Andreas Huber881227d2016-08-02 14:20:21 -070084std::string CompoundType::getCppType(
Steven Moreland979e0992016-09-07 09:18:08 -070085 StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -070086 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -070087 const std::string base =
88 specifyNamespaces ? fullName() : partialCppName();
Andreas Huber881227d2016-08-02 14:20:21 -070089
90 switch (mode) {
91 case StorageMode_Stack:
92 return base;
93
94 case StorageMode_Argument:
95 return "const " + base + "&";
96
97 case StorageMode_Result:
98 return "const " + base + "*";
99 }
100}
101
Yifan Hong4ed13472016-11-02 10:44:11 -0700102std::string CompoundType::getJavaType(bool /* forInitializer */) const {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700103 return fullJavaName();
104}
105
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700106std::string CompoundType::getVtsType() const {
107 switch (mStyle) {
108 case STYLE_STRUCT:
109 {
110 return "TYPE_STRUCT";
111 }
112 case STYLE_UNION:
113 {
114 return "TYPE_UNION";
115 }
116 }
117}
118
Andreas Huber881227d2016-08-02 14:20:21 -0700119void CompoundType::emitReaderWriter(
120 Formatter &out,
121 const std::string &name,
122 const std::string &parcelObj,
123 bool parcelObjIsPointer,
124 bool isReader,
125 ErrorMode mode) const {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700126 const std::string parentName = "_hidl_" + name + "_parent";
Andreas Huber881227d2016-08-02 14:20:21 -0700127
128 out << "size_t " << parentName << ";\n\n";
129
130 const std::string parcelObjDeref =
131 parcelObj + (parcelObjIsPointer ? "->" : ".");
132
133 if (isReader) {
Martijn Coenen6a082c62017-01-11 12:47:02 +0100134 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700135 << parcelObjDeref
136 << "readBuffer("
137 << "&"
138 << parentName
Martijn Coenen6a082c62017-01-11 12:47:02 +0100139 << ", "
140 << " reinterpret_cast<const void **>("
141 << "&" << name
142 << "));\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700143
Martijn Coenen6a082c62017-01-11 12:47:02 +0100144 handleError(out, mode);
Andreas Huber881227d2016-08-02 14:20:21 -0700145 } else {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700146 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700147 << parcelObjDeref
148 << "writeBuffer(&"
149 << name
150 << ", sizeof("
151 << name
152 << "), &"
153 << parentName
154 << ");\n";
155
156 handleError(out, mode);
157 }
158
159 if (mStyle != STYLE_STRUCT || !needsEmbeddedReadWrite()) {
160 return;
161 }
162
163 emitReaderWriterEmbedded(
164 out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700165 0 /* depth */,
Andreas Huber881227d2016-08-02 14:20:21 -0700166 name,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700167 name, /* sanitizedName */
Andreas Huber881227d2016-08-02 14:20:21 -0700168 isReader /* nameIsPointer */,
169 parcelObj,
170 parcelObjIsPointer,
171 isReader,
172 mode,
173 parentName,
174 "0 /* parentOffset */");
175}
176
177void CompoundType::emitReaderWriterEmbedded(
178 Formatter &out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700179 size_t /* depth */,
Andreas Huber881227d2016-08-02 14:20:21 -0700180 const std::string &name,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700181 const std::string & /*sanitizedName */,
Andreas Huber881227d2016-08-02 14:20:21 -0700182 bool nameIsPointer,
183 const std::string &parcelObj,
184 bool parcelObjIsPointer,
185 bool isReader,
186 ErrorMode mode,
187 const std::string &parentName,
188 const std::string &offsetText) const {
189 emitReaderWriterEmbeddedForTypeName(
190 out,
191 name,
192 nameIsPointer,
193 parcelObj,
194 parcelObjIsPointer,
195 isReader,
196 mode,
197 parentName,
198 offsetText,
Andreas Huber0e00de42016-08-03 09:56:02 -0700199 fullName(),
Yifan Hong244e82d2016-11-11 11:13:57 -0800200 "" /* childName */,
201 "" /* namespace */);
Andreas Huber881227d2016-08-02 14:20:21 -0700202}
203
Andreas Huber85eabdb2016-08-25 11:24:49 -0700204void CompoundType::emitJavaReaderWriter(
205 Formatter &out,
206 const std::string &parcelObj,
207 const std::string &argName,
208 bool isReader) const {
209 if (isReader) {
210 out << "new " << fullJavaName() << "();\n";
211 }
212
213 out << argName
214 << "."
215 << (isReader ? "readFromParcel" : "writeToParcel")
216 << "("
217 << parcelObj
218 << ");\n";
219}
220
221void CompoundType::emitJavaFieldInitializer(
222 Formatter &out, const std::string &fieldName) const {
223 out << "final "
224 << fullJavaName()
225 << " "
226 << fieldName
227 << " = new "
228 << fullJavaName()
229 << "();\n";
230}
231
232void CompoundType::emitJavaFieldReaderWriter(
233 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700234 size_t /* depth */,
Andreas Huber709b62d2016-09-19 11:21:18 -0700235 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700236 const std::string &blobName,
237 const std::string &fieldName,
238 const std::string &offset,
239 bool isReader) const {
240 if (isReader) {
241 out << fieldName
Andreas Huber709b62d2016-09-19 11:21:18 -0700242 << ".readEmbeddedFromParcel("
243 << parcelName
244 << ", "
Andreas Huber85eabdb2016-08-25 11:24:49 -0700245 << blobName
246 << ", "
247 << offset
248 << ");\n";
249
250 return;
251 }
252
253 out << fieldName
254 << ".writeEmbeddedToBlob("
255 << blobName
256 << ", "
257 << offset
258 << ");\n";
259}
Yifan Hongbf459bc2016-08-23 16:50:37 -0700260void CompoundType::emitResolveReferences(
261 Formatter &out,
262 const std::string &name,
263 bool nameIsPointer,
264 const std::string &parcelObj,
265 bool parcelObjIsPointer,
266 bool isReader,
267 ErrorMode mode) const {
268 emitResolveReferencesEmbedded(
269 out,
270 0 /* depth */,
271 name,
272 name /* sanitizedName */,
273 nameIsPointer,
274 parcelObj,
275 parcelObjIsPointer,
276 isReader,
277 mode,
278 "_hidl_" + name + "_parent",
279 "0 /* parentOffset */");
280}
281
282void CompoundType::emitResolveReferencesEmbedded(
283 Formatter &out,
284 size_t /* depth */,
285 const std::string &name,
286 const std::string &/* sanitizedName */,
287 bool nameIsPointer,
288 const std::string &parcelObj,
289 bool parcelObjIsPointer,
290 bool isReader,
291 ErrorMode mode,
292 const std::string &parentName,
293 const std::string &offsetText) const {
294 CHECK(needsResolveReferences());
295
296 const std::string parcelObjDeref =
297 parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
298
299 const std::string parcelObjPointer =
300 parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
301
Yifan Hong244e82d2016-11-11 11:13:57 -0800302 const std::string nameDerefed = nameIsPointer ? ("*" + name) : name;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700303 const std::string namePointer = nameIsPointer ? name : ("&" + name);
304
305 out << "_hidl_err = ";
306
307 if (isReader) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800308 out << "readEmbeddedReferenceFromParcel(\n";
Yifan Hongbf459bc2016-08-23 16:50:37 -0700309 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800310 out << "writeEmbeddedReferenceToParcel(\n";
Yifan Hongbf459bc2016-08-23 16:50:37 -0700311 }
312
Yifan Hong33223ca2016-12-13 15:07:35 -0800313 out.indent(2, [&]{
Yifan Hong244e82d2016-11-11 11:13:57 -0800314 if (isReader) {
315 out << "const_cast<"
316 << fullName()
317 << " *"
318 << ">("
319 << namePointer
320 << "),\n"
321 << parcelObjDeref;
322 } else {
323 out << nameDerefed
324 << ",\n"
325 << parcelObjPointer;
326 }
327
328 out << ",\n"
Yifan Hong0a68a282016-10-21 16:32:34 -0700329 << parentName
330 << ",\n"
331 << offsetText
332 << ");\n\n";
333 });
Yifan Hongbf459bc2016-08-23 16:50:37 -0700334
335 handleError(out, mode);
336}
Andreas Huber85eabdb2016-08-25 11:24:49 -0700337
Andreas Huber881227d2016-08-02 14:20:21 -0700338status_t CompoundType::emitTypeDeclarations(Formatter &out) const {
339 out << ((mStyle == STYLE_STRUCT) ? "struct" : "union")
340 << " "
Andreas Huber0e00de42016-08-03 09:56:02 -0700341 << localName()
Yifan Hongb1d2ebc2016-12-20 17:18:07 -0800342 << " final {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700343
344 out.indent();
345
346 Scope::emitTypeDeclarations(out);
347
Andreas Huberca4bc892017-01-09 14:58:12 -0800348 if (!isJavaCompatible()) {
349 for (const auto &field : *mFields) {
350 out << field->type().getCppStackType()
351 << " "
352 << field->name()
353 << ";\n";
354 }
355
356 out.unindent();
357 out << "};\n\n";
358
359 return OK;
Andreas Huber881227d2016-08-02 14:20:21 -0700360 }
361
Andreas Huberca4bc892017-01-09 14:58:12 -0800362 for (int pass = 0; pass < 2; ++pass) {
363 size_t offset = 0;
364 for (const auto &field : *mFields) {
365 size_t fieldAlign, fieldSize;
366 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
367
368 size_t pad = offset % fieldAlign;
369 if (pad > 0) {
370 offset += fieldAlign - pad;
371 }
372
373 if (pass == 0) {
374 out << field->type().getCppStackType()
375 << " "
376 << field->name()
377 << " __attribute__ ((aligned("
378 << fieldAlign
379 << ")));\n";
380 } else {
381 out << "static_assert(offsetof("
382 << fullName()
383 << ", "
384 << field->name()
385 << ") == "
386 << offset
387 << ", \"wrong offset\");\n";
388 }
389
390 offset += fieldSize;
391 }
392
393 if (pass == 0) {
394 out.unindent();
395 out << "};\n\n";
396 }
397 }
398
399 size_t structAlign, structSize;
400 getAlignmentAndSize(&structAlign, &structSize);
401
402 out << "static_assert(sizeof("
403 << fullName()
404 << ") == "
405 << structSize
406 << ", \"wrong size\");\n";
407
408 out << "static_assert(__alignof("
409 << fullName()
410 << ") == "
411 << structAlign
412 << ", \"wrong alignment\");\n\n";
Yifan Hong244e82d2016-11-11 11:13:57 -0800413
414 return OK;
415}
416
417
Yifan Hongc6752dc2016-12-20 14:00:14 -0800418status_t CompoundType::emitGlobalTypeDeclarations(Formatter &out) const {
419 Scope::emitGlobalTypeDeclarations(out);
420
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800421 out << "std::string toString("
422 << getCppArgumentType()
Yifan Hong42c68432017-03-13 16:22:03 -0700423 << ");\n\n";
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800424
Yifan Hong42c68432017-03-13 16:22:03 -0700425 if (canCheckEquality()) {
426 out << "bool operator==("
427 << getCppArgumentType() << ", " << getCppArgumentType() << ");\n\n";
428
429 out << "bool operator!=("
430 << getCppArgumentType() << ", " << getCppArgumentType() << ");\n\n";
431 } else {
432 out << "// operator== and operator!= are not generated for " << localName() << "\n\n";
Yifan Hongc6752dc2016-12-20 14:00:14 -0800433 }
434
Yifan Hongc6752dc2016-12-20 14:00:14 -0800435 return OK;
436}
437
Yifan Hong244e82d2016-11-11 11:13:57 -0800438status_t CompoundType::emitGlobalHwDeclarations(Formatter &out) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700439 if (needsEmbeddedReadWrite()) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800440 out << "::android::status_t readEmbeddedFromParcel(\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700441
Yifan Hong0a68a282016-10-21 16:32:34 -0700442 out.indent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700443
Yifan Hong244e82d2016-11-11 11:13:57 -0800444 out << fullName() << " *obj,\n"
445 << "const ::android::hardware::Parcel &parcel,\n"
446 << "size_t parentHandle,\n"
447 << "size_t parentOffset);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700448
Yifan Hong0a68a282016-10-21 16:32:34 -0700449 out.unindent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700450
451 out << "::android::status_t writeEmbeddedToParcel(\n";
452
Yifan Hong0a68a282016-10-21 16:32:34 -0700453 out.indent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700454
Yifan Hong244e82d2016-11-11 11:13:57 -0800455 out << "const " << fullName() << " &obj,\n"
456 << "::android::hardware::Parcel *parcel,\n"
457 << "size_t parentHandle,\n"
458 << "size_t parentOffset);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700459
Yifan Hong0a68a282016-10-21 16:32:34 -0700460 out.unindent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700461 }
462
Yifan Hongbf459bc2016-08-23 16:50:37 -0700463 if(needsResolveReferences()) {
Yifan Hongbf459bc2016-08-23 16:50:37 -0700464 out << "::android::status_t readEmbeddedReferenceFromParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700465 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800466 out << fullName() << " *obj,\n"
467 << "const ::android::hardware::Parcel &parcel,\n"
468 << "size_t parentHandle, size_t parentOffset);\n\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700469 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700470 out << "::android::status_t writeEmbeddedReferenceToParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700471 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800472 out << "const " << fullName() << " &obj,\n"
473 << "::android::hardware::Parcel *,\n"
474 << "size_t parentHandle, size_t parentOffset);\n\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700475 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700476 }
477
Andreas Huber881227d2016-08-02 14:20:21 -0700478 return OK;
479}
480
481status_t CompoundType::emitTypeDefinitions(
482 Formatter &out, const std::string prefix) const {
Steven Morelandb158de12016-10-10 11:59:09 -0700483 std::string space = prefix.empty() ? "" : (prefix + "::");
484 status_t err = Scope::emitTypeDefinitions(out, space + localName());
Andreas Huber881227d2016-08-02 14:20:21 -0700485
486 if (err != OK) {
487 return err;
488 }
489
Yifan Hongbf459bc2016-08-23 16:50:37 -0700490 if (needsEmbeddedReadWrite()) {
491 emitStructReaderWriter(out, prefix, true /* isReader */);
492 emitStructReaderWriter(out, prefix, false /* isReader */);
Andreas Huber881227d2016-08-02 14:20:21 -0700493 }
494
Yifan Hongbf459bc2016-08-23 16:50:37 -0700495 if (needsResolveReferences()) {
496 emitResolveReferenceDef(out, prefix, true /* isReader */);
497 emitResolveReferenceDef(out, prefix, false /* isReader */);
498 }
Andreas Huber881227d2016-08-02 14:20:21 -0700499
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800500 out << "std::string toString("
501 << getCppArgumentType()
502 << (mFields->empty() ? "" : " o")
503 << ") ";
504
505 out.block([&] {
506 // include toString for scalar types
Hridya Valsaraju9ab1e9e2017-03-10 07:52:23 -0800507 out << "using ::android::hardware::toString;\n"
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800508 << "std::string os;\n";
509 out << "os += \"{\";\n";
510
511 for (const CompoundField *field : *mFields) {
512 out << "os += \"";
513 if (field != *(mFields->begin())) {
514 out << ", ";
515 }
516 out << "." << field->name() << " = \";\n";
517 field->type().emitDump(out, "os", "o." + field->name());
518 }
519
520 out << "os += \"}\"; return os;\n";
521 }).endl().endl();
522
Yifan Hong42c68432017-03-13 16:22:03 -0700523 if (canCheckEquality()) {
524 out << "bool operator==("
525 << getCppArgumentType() << " " << (mFields->empty() ? "/* lhs */" : "lhs") << ", "
526 << getCppArgumentType() << " " << (mFields->empty() ? "/* rhs */" : "rhs") << ") ";
527 out.block([&] {
528 for (const auto &field : *mFields) {
529 out.sIf("lhs." + field->name() + " != rhs." + field->name(), [&] {
530 out << "return false;\n";
531 }).endl();
532 }
533 out << "return true;\n";
534 }).endl().endl();
535
536 out << "bool operator!=("
537 << getCppArgumentType() << " lhs," << getCppArgumentType() << " rhs)";
538 out.block([&] {
539 out << "return !(lhs == rhs);\n";
540 }).endl().endl();
541 } else {
542 out << "// operator== and operator!= are not generated for " << localName() << "\n";
543 }
544
Andreas Huber881227d2016-08-02 14:20:21 -0700545 return OK;
546}
547
Andreas Huber85eabdb2016-08-25 11:24:49 -0700548status_t CompoundType::emitJavaTypeDeclarations(
549 Formatter &out, bool atTopLevel) const {
550 out << "public final ";
551
552 if (!atTopLevel) {
553 out << "static ";
554 }
555
556 out << "class "
557 << localName()
558 << " {\n";
559
560 out.indent();
561
562 Scope::emitJavaTypeDeclarations(out, false /* atTopLevel */);
563
564 for (const auto &field : *mFields) {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700565 out << "public ";
566
567 field->type().emitJavaFieldInitializer(out, field->name());
568 }
569
570 if (!mFields->empty()) {
571 out << "\n";
572 }
573
Yifan Hongec102272016-12-20 18:10:07 -0800574 ////////////////////////////////////////////////////////////////////////////
575
576 if (canCheckEquality()) {
Yifan Hong7d1839f2017-02-22 13:24:29 -0800577 out << "@Override\npublic final boolean equals(Object otherObject) ";
Yifan Hongec102272016-12-20 18:10:07 -0800578 out.block([&] {
Yifan Hong7d1839f2017-02-22 13:24:29 -0800579 out.sIf("this == otherObject", [&] {
580 out << "return true;\n";
581 }).endl();
582 out.sIf("otherObject == null", [&] {
583 out << "return false;\n";
584 }).endl();
Yifan Hong2593f182017-03-27 12:59:54 -0700585 // Though class is final, we use getClass instead of instanceof to be explicit.
586 out.sIf("otherObject.getClass() != " + fullJavaName() + ".class", [&] {
Yifan Hong7d1839f2017-02-22 13:24:29 -0800587 out << "return false;\n";
588 }).endl();
589 out << fullJavaName() << " other = (" << fullJavaName() << ")otherObject;\n";
Yifan Hongec102272016-12-20 18:10:07 -0800590 for (const auto &field : *mFields) {
Yifan Hong2593f182017-03-27 12:59:54 -0700591 std::string condition = (field->type().isScalar() || field->type().isEnum())
Yifan Hongec102272016-12-20 18:10:07 -0800592 ? "this." + field->name() + " != other." + field->name()
Yifan Hong2593f182017-03-27 12:59:54 -0700593 : ("!android.os.HidlSupport.deepEquals(this." + field->name()
Yifan Hongec102272016-12-20 18:10:07 -0800594 + ", other." + field->name() + ")");
595 out.sIf(condition, [&] {
596 out << "return false;\n";
597 }).endl();
598 }
599 out << "return true;\n";
600 }).endl().endl();
601
Yifan Hong7d1839f2017-02-22 13:24:29 -0800602 out << "@Override\npublic final int hashCode() ";
Yifan Hongec102272016-12-20 18:10:07 -0800603 out.block([&] {
Yifan Hong7d1839f2017-02-22 13:24:29 -0800604 out << "return java.util.Objects.hash(\n";
605 out.indent(2, [&] {
606 bool first = true;
607 for (const auto &field : *mFields) {
608 if (!first) {
609 out << ", \n";
Yifan Hongec102272016-12-20 18:10:07 -0800610 }
Yifan Hong7d1839f2017-02-22 13:24:29 -0800611 first = false;
Yifan Hong2593f182017-03-27 12:59:54 -0700612 out << "android.os.HidlSupport.deepHashCode(this." << field->name() << ")";
Yifan Hongec102272016-12-20 18:10:07 -0800613 }
Yifan Hong7d1839f2017-02-22 13:24:29 -0800614 });
Yifan Hongec102272016-12-20 18:10:07 -0800615 out << ");\n";
616 }).endl().endl();
Yifan Hong42c68432017-03-13 16:22:03 -0700617 } else {
618 out << "// equals() is not generated for " << localName() << "\n";
Yifan Hongec102272016-12-20 18:10:07 -0800619 }
620
621 ////////////////////////////////////////////////////////////////////////////
622
Yifan Honge45b5302017-02-22 10:49:07 -0800623 out << "@Override\npublic final String toString() ";
624 out.block([&] {
625 out << "java.lang.StringBuilder builder = new java.lang.StringBuilder();\n"
626 << "builder.append(\"{\");\n";
627 for (const auto &field : *mFields) {
628 out << "builder.append(\"";
629 if (field != *(mFields->begin())) {
630 out << ", ";
631 }
632 out << "." << field->name() << " = \");\n";
633 field->type().emitJavaDump(out, "builder", "this." + field->name());
634 }
635 out << "builder.append(\"}\");\nreturn builder.toString();\n";
636 }).endl().endl();
637
638 ////////////////////////////////////////////////////////////////////////////
639
Yifan Hong1af73532016-11-09 14:32:58 -0800640 out << "public final void readFromParcel(android.os.HwParcel parcel) {\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700641 out.indent();
Yifan Hong1af73532016-11-09 14:32:58 -0800642 out << "android.os.HwBlob blob = parcel.readBuffer();\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700643 out << "readEmbeddedFromParcel(parcel, blob, 0 /* parentOffset */);\n";
644 out.unindent();
645 out << "}\n\n";
646
Andreas Huberf630bc82016-09-09 14:52:25 -0700647 ////////////////////////////////////////////////////////////////////////////
648
Yifan Hong1af73532016-11-09 14:32:58 -0800649 out << "public static final java.util.ArrayList<"
Andreas Huberf630bc82016-09-09 14:52:25 -0700650 << localName()
Yifan Hong1af73532016-11-09 14:32:58 -0800651 << "> readVectorFromParcel(android.os.HwParcel parcel) {\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700652 out.indent();
653
Yifan Hong1af73532016-11-09 14:32:58 -0800654 out << "java.util.ArrayList<"
Andreas Huberf630bc82016-09-09 14:52:25 -0700655 << localName()
Yifan Hong1af73532016-11-09 14:32:58 -0800656 << "> _hidl_vec = new java.util.ArrayList();\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700657
Yifan Hong1af73532016-11-09 14:32:58 -0800658 out << "android.os.HwBlob _hidl_blob = parcel.readBuffer();\n\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700659
660 VectorType::EmitJavaFieldReaderWriterForElementType(
Andreas Huber4c865b72016-09-14 15:26:27 -0700661 out,
662 0 /* depth */,
663 this,
Andreas Huber709b62d2016-09-19 11:21:18 -0700664 "parcel",
Andreas Huber4c865b72016-09-14 15:26:27 -0700665 "_hidl_blob",
666 "_hidl_vec",
667 "0",
668 true /* isReader */);
Andreas Huberf630bc82016-09-09 14:52:25 -0700669
Andreas Huber1b6822b2016-10-18 09:28:40 -0700670 out << "\nreturn _hidl_vec;\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700671
672 out.unindent();
673 out << "}\n\n";
674
675 ////////////////////////////////////////////////////////////////////////////
676
Andreas Huber85eabdb2016-08-25 11:24:49 -0700677 out << "public final void readEmbeddedFromParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700678 out.indent(2);
Yifan Hong1af73532016-11-09 14:32:58 -0800679 out << "android.os.HwParcel parcel, android.os.HwBlob _hidl_blob, long _hidl_offset) {\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700680 out.unindent();
681
682 size_t offset = 0;
683 for (const auto &field : *mFields) {
684 size_t fieldAlign, fieldSize;
685 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
686
687 size_t pad = offset % fieldAlign;
688 if (pad > 0) {
689 offset += fieldAlign - pad;
690 }
691
692 field->type().emitJavaFieldReaderWriter(
693 out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700694 0 /* depth */,
Andreas Huber709b62d2016-09-19 11:21:18 -0700695 "parcel",
Andreas Huber85eabdb2016-08-25 11:24:49 -0700696 "_hidl_blob",
697 field->name(),
698 "_hidl_offset + " + std::to_string(offset),
699 true /* isReader */);
700
701 offset += fieldSize;
702 }
703
704 out.unindent();
705 out << "}\n\n";
706
Andreas Huberf630bc82016-09-09 14:52:25 -0700707 ////////////////////////////////////////////////////////////////////////////
708
Andreas Huber709b62d2016-09-19 11:21:18 -0700709 size_t structAlign, structSize;
710 getAlignmentAndSize(&structAlign, &structSize);
711
Yifan Hong1af73532016-11-09 14:32:58 -0800712 out << "public final void writeToParcel(android.os.HwParcel parcel) {\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700713 out.indent();
714
Yifan Hong1af73532016-11-09 14:32:58 -0800715 out << "android.os.HwBlob _hidl_blob = new android.os.HwBlob("
Andreas Huber85eabdb2016-08-25 11:24:49 -0700716 << structSize
717 << " /* size */);\n";
718
719 out << "writeEmbeddedToBlob(_hidl_blob, 0 /* parentOffset */);\n"
720 << "parcel.writeBuffer(_hidl_blob);\n";
721
722 out.unindent();
723 out << "}\n\n";
724
Andreas Huberf630bc82016-09-09 14:52:25 -0700725 ////////////////////////////////////////////////////////////////////////////
726
Andreas Huberf630bc82016-09-09 14:52:25 -0700727 out << "public static final void writeVectorToParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700728 out.indent(2);
Yifan Hong1af73532016-11-09 14:32:58 -0800729 out << "android.os.HwParcel parcel, java.util.ArrayList<"
Andreas Huberf630bc82016-09-09 14:52:25 -0700730 << localName()
Andreas Huber1b6822b2016-10-18 09:28:40 -0700731 << "> _hidl_vec) {\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700732 out.unindent();
733
Yifan Hong1af73532016-11-09 14:32:58 -0800734 out << "android.os.HwBlob _hidl_blob = new android.os.HwBlob(24 /* sizeof(hidl_vec<T>) */);\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700735
736 VectorType::EmitJavaFieldReaderWriterForElementType(
Andreas Huber4c865b72016-09-14 15:26:27 -0700737 out,
738 0 /* depth */,
739 this,
Andreas Huber709b62d2016-09-19 11:21:18 -0700740 "parcel",
Andreas Huber4c865b72016-09-14 15:26:27 -0700741 "_hidl_blob",
742 "_hidl_vec",
743 "0",
744 false /* isReader */);
Andreas Huberf630bc82016-09-09 14:52:25 -0700745
746 out << "\nparcel.writeBuffer(_hidl_blob);\n";
747
748 out.unindent();
749 out << "}\n\n";
750
751 ////////////////////////////////////////////////////////////////////////////
752
Andreas Huber85eabdb2016-08-25 11:24:49 -0700753 out << "public final void writeEmbeddedToBlob(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700754 out.indent(2);
Yifan Hong1af73532016-11-09 14:32:58 -0800755 out << "android.os.HwBlob _hidl_blob, long _hidl_offset) {\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700756 out.unindent();
757
758 offset = 0;
759 for (const auto &field : *mFields) {
760 size_t fieldAlign, fieldSize;
761 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
762
763 size_t pad = offset % fieldAlign;
764 if (pad > 0) {
765 offset += fieldAlign - pad;
766 }
767
768 field->type().emitJavaFieldReaderWriter(
769 out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700770 0 /* depth */,
Andreas Huber709b62d2016-09-19 11:21:18 -0700771 "parcel",
Andreas Huber85eabdb2016-08-25 11:24:49 -0700772 "_hidl_blob",
773 field->name(),
774 "_hidl_offset + " + std::to_string(offset),
775 false /* isReader */);
776
777 offset += fieldSize;
778 }
779
780 out.unindent();
781 out << "}\n";
782
783 out.unindent();
784 out << "};\n\n";
785
786 return OK;
787}
788
Andreas Huber881227d2016-08-02 14:20:21 -0700789void CompoundType::emitStructReaderWriter(
790 Formatter &out, const std::string &prefix, bool isReader) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800791
792 std::string space = prefix.empty() ? "" : (prefix + "::");
793
Andreas Huber881227d2016-08-02 14:20:21 -0700794 out << "::android::status_t "
Yifan Hong244e82d2016-11-11 11:13:57 -0800795 << (isReader ? "readEmbeddedFromParcel"
796 : "writeEmbeddedToParcel")
797 << "(\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700798
Yifan Hong0a68a282016-10-21 16:32:34 -0700799 out.indent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700800
Yifan Hong244e82d2016-11-11 11:13:57 -0800801 bool useName = false;
802 for (const auto &field : *mFields) {
803 if (field->type().useNameInEmitReaderWriterEmbedded(isReader)) {
804 useName = true;
805 break;
806 }
807 }
808 std::string name = useName ? "obj" : "/* obj */";
809 // if not useName, then obj should not be used at all,
810 // then the #error should not be emitted.
811 std::string error = useName ? "" : "\n#error\n";
812
Andreas Huber881227d2016-08-02 14:20:21 -0700813 if (isReader) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800814 out << space << localName() << " *" << name << ",\n";
Andreas Huber8a82ff72016-08-04 10:29:39 -0700815 out << "const ::android::hardware::Parcel &parcel,\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700816 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800817 out << "const " << space << localName() << " &" << name << ",\n";
Andreas Huber8a82ff72016-08-04 10:29:39 -0700818 out << "::android::hardware::Parcel *parcel,\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700819 }
820
821 out << "size_t parentHandle,\n"
822 << "size_t parentOffset)";
823
Andreas Huber881227d2016-08-02 14:20:21 -0700824 out << " {\n";
825
Yifan Hong0a68a282016-10-21 16:32:34 -0700826 out.unindent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700827 out.indent();
828
Iliyan Malchev549e2592016-08-10 08:59:12 -0700829 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700830
831 for (const auto &field : *mFields) {
832 if (!field->type().needsEmbeddedReadWrite()) {
833 continue;
834 }
835
836 field->type().emitReaderWriterEmbedded(
837 out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700838 0 /* depth */,
Yifan Hong244e82d2016-11-11 11:13:57 -0800839 name + (isReader ? "->" : ".") + field->name() + error,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700840 field->name() /* sanitizedName */,
Andreas Huber881227d2016-08-02 14:20:21 -0700841 false /* nameIsPointer */,
842 "parcel",
843 !isReader /* parcelObjIsPointer */,
844 isReader,
Andreas Huber737080b2016-08-02 15:38:04 -0700845 ErrorMode_Return,
Andreas Huber881227d2016-08-02 14:20:21 -0700846 "parentHandle",
Andreas Huber0e00de42016-08-03 09:56:02 -0700847 "parentOffset + offsetof("
848 + fullName()
849 + ", "
850 + field->name()
851 + ")");
Andreas Huber881227d2016-08-02 14:20:21 -0700852 }
853
Iliyan Malchev549e2592016-08-10 08:59:12 -0700854 out << "return _hidl_err;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700855
856 out.unindent();
857 out << "}\n\n";
858}
859
Yifan Hongbf459bc2016-08-23 16:50:37 -0700860void CompoundType::emitResolveReferenceDef(
861 Formatter &out, const std::string prefix, bool isReader) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800862 out << "::android::status_t ";
863 const std::string space(prefix.empty() ? "" : (prefix + "::"));
Yifan Hong00f47172016-09-30 14:40:45 -0700864
865 bool useParent = false;
866 for (const auto &field : *mFields) {
867 if (field->type().useParentInEmitResolveReferencesEmbedded()) {
868 useParent = true;
869 break;
870 }
871 }
872
873 std::string parentHandleName = useParent ? "parentHandle" : "/* parentHandle */";
874 std::string parentOffsetName = useParent ? "parentOffset" : "/* parentOffset */";
875
Yifan Hongbf459bc2016-08-23 16:50:37 -0700876 if (isReader) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800877 out << "readEmbeddedReferenceFromParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700878 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800879 out << space + localName() + " *obj,\n"
880 << "const ::android::hardware::Parcel &parcel,\n"
Yifan Hong00f47172016-09-30 14:40:45 -0700881 << "size_t " << parentHandleName << ", "
882 << "size_t " << parentOffsetName << ")\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700883 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700884 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800885 out << "writeEmbeddedReferenceToParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700886 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800887 out << "const " << space + localName() + " &obj,\n"
888 << "::android::hardware::Parcel *parcel,\n"
Yifan Hong00f47172016-09-30 14:40:45 -0700889 << "size_t " << parentHandleName << ", "
Yifan Hong244e82d2016-11-11 11:13:57 -0800890 << "size_t " << parentOffsetName << ")\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700891 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700892 }
893
894 out << " {\n";
895
896 out.indent();
897
898 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
899
Yifan Hong244e82d2016-11-11 11:13:57 -0800900 const std::string nameDeref(isReader ? "obj->" : "obj.");
Yifan Hong00f47172016-09-30 14:40:45 -0700901 // if not useParent, then parentName and offsetText
902 // should not be used at all, then the #error should not be emitted.
903 std::string error = useParent ? "" : "\n#error\n";
904
Yifan Hongbf459bc2016-08-23 16:50:37 -0700905 for (const auto &field : *mFields) {
906 if (!field->type().needsResolveReferences()) {
907 continue;
908 }
909
910 field->type().emitResolveReferencesEmbedded(
911 out,
912 0 /* depth */,
Yifan Hong244e82d2016-11-11 11:13:57 -0800913 nameDeref + field->name(),
Yifan Hongbf459bc2016-08-23 16:50:37 -0700914 field->name() /* sanitizedName */,
915 false, // nameIsPointer
916 "parcel", // const std::string &parcelObj,
917 !isReader, // bool parcelObjIsPointer,
918 isReader, // bool isReader,
919 ErrorMode_Return,
Yifan Hong00f47172016-09-30 14:40:45 -0700920 parentHandleName + error,
921 parentOffsetName
922 + " + offsetof("
Yifan Hongbf459bc2016-08-23 16:50:37 -0700923 + fullName()
924 + ", "
925 + field->name()
Yifan Hong00f47172016-09-30 14:40:45 -0700926 + ")"
927 + error);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700928 }
929
Yifan Hongbf459bc2016-08-23 16:50:37 -0700930 out << "return _hidl_err;\n";
931
932 out.unindent();
933 out << "}\n\n";
934}
935
Andreas Huber881227d2016-08-02 14:20:21 -0700936bool CompoundType::needsEmbeddedReadWrite() const {
937 if (mStyle != STYLE_STRUCT) {
938 return false;
939 }
940
941 for (const auto &field : *mFields) {
942 if (field->type().needsEmbeddedReadWrite()) {
943 return true;
944 }
945 }
946
947 return false;
948}
949
Yifan Hongbf459bc2016-08-23 16:50:37 -0700950bool CompoundType::needsResolveReferences() const {
951 if (mStyle != STYLE_STRUCT) {
952 return false;
953 }
954
955 for (const auto &field : *mFields) {
956 if (field->type().needsResolveReferences()) {
957 return true;
958 }
959 }
960
961 return false;
962}
963
Andreas Huber881227d2016-08-02 14:20:21 -0700964bool CompoundType::resultNeedsDeref() const {
965 return true;
966}
967
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700968status_t CompoundType::emitVtsTypeDeclarations(Formatter &out) const {
Zhuoyao Zhangbf828c82016-10-26 14:15:10 -0700969 out << "name: \"" << fullName() << "\"\n";
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700970 out << "type: " << getVtsType() << "\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700971
972 // Emit declaration for each subtype.
973 for (const auto &type : getSubTypes()) {
974 switch (mStyle) {
975 case STYLE_STRUCT:
976 {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700977 out << "sub_struct: {\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700978 break;
979 }
980 case STYLE_UNION:
981 {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700982 out << "sub_union: {\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700983 break;
984 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700985 }
986 out.indent();
987 status_t status(type->emitVtsTypeDeclarations(out));
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700988 if (status != OK) {
989 return status;
990 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700991 out.unindent();
992 out << "}\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700993 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700994
995 // Emit declaration for each field.
996 for (const auto &field : *mFields) {
997 switch (mStyle) {
998 case STYLE_STRUCT:
999 {
1000 out << "struct_value: {\n";
1001 break;
1002 }
1003 case STYLE_UNION:
1004 {
1005 out << "union_value: {\n";
1006 break;
1007 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001008 }
1009 out.indent();
1010 out << "name: \"" << field->name() << "\"\n";
1011 status_t status = field->type().emitVtsAttributeType(out);
1012 if (status != OK) {
1013 return status;
1014 }
1015 out.unindent();
1016 out << "}\n";
1017 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07001018
1019 return OK;
1020}
1021
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001022status_t CompoundType::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -07001023 out << "type: " << getVtsType() << "\n";
Zhuoyao Zhangbf828c82016-10-26 14:15:10 -07001024 out << "predefined_type: \"" << fullName() << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07001025 return OK;
1026}
1027
Andreas Huber70a59e12016-08-16 12:57:01 -07001028bool CompoundType::isJavaCompatible() const {
Andreas Huber0fa9e392016-08-31 09:05:44 -07001029 if (mStyle != STYLE_STRUCT || !Scope::isJavaCompatible()) {
1030 return false;
1031 }
1032
1033 for (const auto &field : *mFields) {
1034 if (!field->type().isJavaCompatible()) {
1035 return false;
1036 }
1037 }
1038
1039 return true;
Andreas Huber85eabdb2016-08-25 11:24:49 -07001040}
1041
1042void CompoundType::getAlignmentAndSize(size_t *align, size_t *size) const {
1043 *align = 1;
1044
1045 size_t offset = 0;
1046 for (const auto &field : *mFields) {
1047 // Each field is aligned according to its alignment requirement.
1048 // The surrounding structure's alignment is the maximum of its
1049 // fields' aligments.
1050
1051 size_t fieldAlign, fieldSize;
1052 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
1053
1054 size_t pad = offset % fieldAlign;
1055 if (pad > 0) {
1056 offset += fieldAlign - pad;
1057 }
1058
1059 offset += fieldSize;
1060
1061 if (fieldAlign > (*align)) {
1062 *align = fieldAlign;
1063 }
1064 }
1065
1066 // Final padding to account for the structure's alignment.
1067 size_t pad = offset % (*align);
1068 if (pad > 0) {
1069 offset += (*align) - pad;
1070 }
1071
1072 *size = offset;
Andreas Huberca4bc892017-01-09 14:58:12 -08001073
1074 if (*size == 0) {
1075 // An empty struct still occupies a byte of space in C++.
1076 *size = 1;
1077 }
Andreas Huber70a59e12016-08-16 12:57:01 -07001078}
1079
Andreas Huber31629bc2016-08-03 09:06:40 -07001080////////////////////////////////////////////////////////////////////////////////
1081
1082CompoundField::CompoundField(const char *name, Type *type)
1083 : mName(name),
1084 mType(type) {
1085}
1086
1087std::string CompoundField::name() const {
1088 return mName;
1089}
1090
1091const Type &CompoundField::type() const {
1092 return *mType;
1093}
1094
Andreas Huberc9410c72016-07-28 12:18:40 -07001095} // namespace android
1096