blob: 25f0f8bdaff1192bdbbe77478b155c1b44ab2128 [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 Huber2639fa32017-03-30 09:10:56 -0700348 if (containsPointer()) {
Andreas Huberca4bc892017-01-09 14:58:12 -0800349 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
Andreas Huber2639fa32017-03-30 09:10:56 -0700390 if (mStyle == STYLE_STRUCT) {
391 offset += fieldSize;
392 }
Andreas Huberca4bc892017-01-09 14:58:12 -0800393 }
394
395 if (pass == 0) {
396 out.unindent();
397 out << "};\n\n";
398 }
399 }
400
401 size_t structAlign, structSize;
402 getAlignmentAndSize(&structAlign, &structSize);
403
404 out << "static_assert(sizeof("
405 << fullName()
406 << ") == "
407 << structSize
408 << ", \"wrong size\");\n";
409
410 out << "static_assert(__alignof("
411 << fullName()
412 << ") == "
413 << structAlign
414 << ", \"wrong alignment\");\n\n";
Yifan Hong244e82d2016-11-11 11:13:57 -0800415
416 return OK;
417}
418
419
Yifan Hongc6752dc2016-12-20 14:00:14 -0800420status_t CompoundType::emitGlobalTypeDeclarations(Formatter &out) const {
421 Scope::emitGlobalTypeDeclarations(out);
422
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800423 out << "std::string toString("
424 << getCppArgumentType()
Yifan Hong42c68432017-03-13 16:22:03 -0700425 << ");\n\n";
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800426
Yifan Hong42c68432017-03-13 16:22:03 -0700427 if (canCheckEquality()) {
428 out << "bool operator==("
429 << getCppArgumentType() << ", " << getCppArgumentType() << ");\n\n";
430
431 out << "bool operator!=("
432 << getCppArgumentType() << ", " << getCppArgumentType() << ");\n\n";
433 } else {
434 out << "// operator== and operator!= are not generated for " << localName() << "\n\n";
Yifan Hongc6752dc2016-12-20 14:00:14 -0800435 }
436
Yifan Hongc6752dc2016-12-20 14:00:14 -0800437 return OK;
438}
439
Yifan Hong244e82d2016-11-11 11:13:57 -0800440status_t CompoundType::emitGlobalHwDeclarations(Formatter &out) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700441 if (needsEmbeddedReadWrite()) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800442 out << "::android::status_t readEmbeddedFromParcel(\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700443
Yifan Hong0a68a282016-10-21 16:32:34 -0700444 out.indent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700445
Yifan Hong244e82d2016-11-11 11:13:57 -0800446 out << fullName() << " *obj,\n"
447 << "const ::android::hardware::Parcel &parcel,\n"
448 << "size_t parentHandle,\n"
449 << "size_t parentOffset);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700450
Yifan Hong0a68a282016-10-21 16:32:34 -0700451 out.unindent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700452
453 out << "::android::status_t writeEmbeddedToParcel(\n";
454
Yifan Hong0a68a282016-10-21 16:32:34 -0700455 out.indent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700456
Yifan Hong244e82d2016-11-11 11:13:57 -0800457 out << "const " << fullName() << " &obj,\n"
458 << "::android::hardware::Parcel *parcel,\n"
459 << "size_t parentHandle,\n"
460 << "size_t parentOffset);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700461
Yifan Hong0a68a282016-10-21 16:32:34 -0700462 out.unindent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700463 }
464
Yifan Hongbf459bc2016-08-23 16:50:37 -0700465 if(needsResolveReferences()) {
Yifan Hongbf459bc2016-08-23 16:50:37 -0700466 out << "::android::status_t readEmbeddedReferenceFromParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700467 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800468 out << fullName() << " *obj,\n"
469 << "const ::android::hardware::Parcel &parcel,\n"
470 << "size_t parentHandle, size_t parentOffset);\n\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700471 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700472 out << "::android::status_t writeEmbeddedReferenceToParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700473 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800474 out << "const " << fullName() << " &obj,\n"
475 << "::android::hardware::Parcel *,\n"
476 << "size_t parentHandle, size_t parentOffset);\n\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700477 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700478 }
479
Andreas Huber881227d2016-08-02 14:20:21 -0700480 return OK;
481}
482
483status_t CompoundType::emitTypeDefinitions(
484 Formatter &out, const std::string prefix) const {
Steven Morelandb158de12016-10-10 11:59:09 -0700485 std::string space = prefix.empty() ? "" : (prefix + "::");
486 status_t err = Scope::emitTypeDefinitions(out, space + localName());
Andreas Huber881227d2016-08-02 14:20:21 -0700487
488 if (err != OK) {
489 return err;
490 }
491
Yifan Hongbf459bc2016-08-23 16:50:37 -0700492 if (needsEmbeddedReadWrite()) {
493 emitStructReaderWriter(out, prefix, true /* isReader */);
494 emitStructReaderWriter(out, prefix, false /* isReader */);
Andreas Huber881227d2016-08-02 14:20:21 -0700495 }
496
Yifan Hongbf459bc2016-08-23 16:50:37 -0700497 if (needsResolveReferences()) {
498 emitResolveReferenceDef(out, prefix, true /* isReader */);
499 emitResolveReferenceDef(out, prefix, false /* isReader */);
500 }
Andreas Huber881227d2016-08-02 14:20:21 -0700501
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800502 out << "std::string toString("
503 << getCppArgumentType()
504 << (mFields->empty() ? "" : " o")
505 << ") ";
506
507 out.block([&] {
508 // include toString for scalar types
Hridya Valsaraju9ab1e9e2017-03-10 07:52:23 -0800509 out << "using ::android::hardware::toString;\n"
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800510 << "std::string os;\n";
511 out << "os += \"{\";\n";
512
513 for (const CompoundField *field : *mFields) {
514 out << "os += \"";
515 if (field != *(mFields->begin())) {
516 out << ", ";
517 }
518 out << "." << field->name() << " = \";\n";
519 field->type().emitDump(out, "os", "o." + field->name());
520 }
521
522 out << "os += \"}\"; return os;\n";
523 }).endl().endl();
524
Yifan Hong42c68432017-03-13 16:22:03 -0700525 if (canCheckEquality()) {
526 out << "bool operator==("
527 << getCppArgumentType() << " " << (mFields->empty() ? "/* lhs */" : "lhs") << ", "
528 << getCppArgumentType() << " " << (mFields->empty() ? "/* rhs */" : "rhs") << ") ";
529 out.block([&] {
530 for (const auto &field : *mFields) {
531 out.sIf("lhs." + field->name() + " != rhs." + field->name(), [&] {
532 out << "return false;\n";
533 }).endl();
534 }
535 out << "return true;\n";
536 }).endl().endl();
537
538 out << "bool operator!=("
539 << getCppArgumentType() << " lhs," << getCppArgumentType() << " rhs)";
540 out.block([&] {
541 out << "return !(lhs == rhs);\n";
542 }).endl().endl();
543 } else {
544 out << "// operator== and operator!= are not generated for " << localName() << "\n";
545 }
546
Andreas Huber881227d2016-08-02 14:20:21 -0700547 return OK;
548}
549
Andreas Huber85eabdb2016-08-25 11:24:49 -0700550status_t CompoundType::emitJavaTypeDeclarations(
551 Formatter &out, bool atTopLevel) const {
552 out << "public final ";
553
554 if (!atTopLevel) {
555 out << "static ";
556 }
557
558 out << "class "
559 << localName()
560 << " {\n";
561
562 out.indent();
563
564 Scope::emitJavaTypeDeclarations(out, false /* atTopLevel */);
565
566 for (const auto &field : *mFields) {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700567 out << "public ";
568
569 field->type().emitJavaFieldInitializer(out, field->name());
570 }
571
572 if (!mFields->empty()) {
573 out << "\n";
574 }
575
Yifan Hongec102272016-12-20 18:10:07 -0800576 ////////////////////////////////////////////////////////////////////////////
577
578 if (canCheckEquality()) {
Yifan Hong7d1839f2017-02-22 13:24:29 -0800579 out << "@Override\npublic final boolean equals(Object otherObject) ";
Yifan Hongec102272016-12-20 18:10:07 -0800580 out.block([&] {
Yifan Hong7d1839f2017-02-22 13:24:29 -0800581 out.sIf("this == otherObject", [&] {
582 out << "return true;\n";
583 }).endl();
584 out.sIf("otherObject == null", [&] {
585 out << "return false;\n";
586 }).endl();
587 // Class is final, so we only need to check instanceof, not getClass
588 out.sIf("!(otherObject instanceof " + fullJavaName() + ")", [&] {
589 out << "return false;\n";
590 }).endl();
591 out << fullJavaName() << " other = (" << fullJavaName() << ")otherObject;\n";
Yifan Hongec102272016-12-20 18:10:07 -0800592 for (const auto &field : *mFields) {
593 std::string condition = field->type().isScalar()
594 ? "this." + field->name() + " != other." + field->name()
595 : ("!java.util.Objects.deepEquals(this." + field->name()
596 + ", other." + field->name() + ")");
597 out.sIf(condition, [&] {
598 out << "return false;\n";
599 }).endl();
600 }
601 out << "return true;\n";
602 }).endl().endl();
603
Yifan Hong7d1839f2017-02-22 13:24:29 -0800604 out << "@Override\npublic final int hashCode() ";
Yifan Hongec102272016-12-20 18:10:07 -0800605 out.block([&] {
Yifan Hong7d1839f2017-02-22 13:24:29 -0800606 out << "return java.util.Objects.hash(\n";
607 out.indent(2, [&] {
608 bool first = true;
609 for (const auto &field : *mFields) {
610 if (!first) {
611 out << ", \n";
Yifan Hongec102272016-12-20 18:10:07 -0800612 }
Yifan Hong7d1839f2017-02-22 13:24:29 -0800613 first = false;
614 if (field->type().isArray()) {
615 const ArrayType &type = static_cast<const ArrayType &>(field->type());
616 if (type.countDimensions() == 1 &&
617 type.getElementType()->resolveToScalarType() != nullptr) {
618 out << "java.util.Arrays.hashCode(this." << field->name() << ")";
619 } else {
620 out << "java.util.Arrays.deepHashCode(this." << field->name() << ")";
621 }
622 } else {
623 out << "this." << field->name();
624 }
Yifan Hongec102272016-12-20 18:10:07 -0800625 }
Yifan Hong7d1839f2017-02-22 13:24:29 -0800626 });
Yifan Hongec102272016-12-20 18:10:07 -0800627 out << ");\n";
628 }).endl().endl();
Yifan Hong42c68432017-03-13 16:22:03 -0700629 } else {
630 out << "// equals() is not generated for " << localName() << "\n";
Yifan Hongec102272016-12-20 18:10:07 -0800631 }
632
633 ////////////////////////////////////////////////////////////////////////////
634
Yifan Honge45b5302017-02-22 10:49:07 -0800635 out << "@Override\npublic final String toString() ";
636 out.block([&] {
637 out << "java.lang.StringBuilder builder = new java.lang.StringBuilder();\n"
638 << "builder.append(\"{\");\n";
639 for (const auto &field : *mFields) {
640 out << "builder.append(\"";
641 if (field != *(mFields->begin())) {
642 out << ", ";
643 }
644 out << "." << field->name() << " = \");\n";
645 field->type().emitJavaDump(out, "builder", "this." + field->name());
646 }
647 out << "builder.append(\"}\");\nreturn builder.toString();\n";
648 }).endl().endl();
649
650 ////////////////////////////////////////////////////////////////////////////
651
Yifan Hong1af73532016-11-09 14:32:58 -0800652 out << "public final void readFromParcel(android.os.HwParcel parcel) {\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700653 out.indent();
Yifan Hong1af73532016-11-09 14:32:58 -0800654 out << "android.os.HwBlob blob = parcel.readBuffer();\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700655 out << "readEmbeddedFromParcel(parcel, blob, 0 /* parentOffset */);\n";
656 out.unindent();
657 out << "}\n\n";
658
Andreas Huberf630bc82016-09-09 14:52:25 -0700659 ////////////////////////////////////////////////////////////////////////////
660
Yifan Hong1af73532016-11-09 14:32:58 -0800661 out << "public static final java.util.ArrayList<"
Andreas Huberf630bc82016-09-09 14:52:25 -0700662 << localName()
Yifan Hong1af73532016-11-09 14:32:58 -0800663 << "> readVectorFromParcel(android.os.HwParcel parcel) {\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700664 out.indent();
665
Yifan Hong1af73532016-11-09 14:32:58 -0800666 out << "java.util.ArrayList<"
Andreas Huberf630bc82016-09-09 14:52:25 -0700667 << localName()
Yifan Hong1af73532016-11-09 14:32:58 -0800668 << "> _hidl_vec = new java.util.ArrayList();\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700669
Yifan Hong1af73532016-11-09 14:32:58 -0800670 out << "android.os.HwBlob _hidl_blob = parcel.readBuffer();\n\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700671
672 VectorType::EmitJavaFieldReaderWriterForElementType(
Andreas Huber4c865b72016-09-14 15:26:27 -0700673 out,
674 0 /* depth */,
675 this,
Andreas Huber709b62d2016-09-19 11:21:18 -0700676 "parcel",
Andreas Huber4c865b72016-09-14 15:26:27 -0700677 "_hidl_blob",
678 "_hidl_vec",
679 "0",
680 true /* isReader */);
Andreas Huberf630bc82016-09-09 14:52:25 -0700681
Andreas Huber1b6822b2016-10-18 09:28:40 -0700682 out << "\nreturn _hidl_vec;\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700683
684 out.unindent();
685 out << "}\n\n";
686
687 ////////////////////////////////////////////////////////////////////////////
688
Andreas Huber85eabdb2016-08-25 11:24:49 -0700689 out << "public final void readEmbeddedFromParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700690 out.indent(2);
Yifan Hong1af73532016-11-09 14:32:58 -0800691 out << "android.os.HwParcel parcel, android.os.HwBlob _hidl_blob, long _hidl_offset) {\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700692 out.unindent();
693
694 size_t offset = 0;
695 for (const auto &field : *mFields) {
696 size_t fieldAlign, fieldSize;
697 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
698
699 size_t pad = offset % fieldAlign;
700 if (pad > 0) {
701 offset += fieldAlign - pad;
702 }
703
704 field->type().emitJavaFieldReaderWriter(
705 out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700706 0 /* depth */,
Andreas Huber709b62d2016-09-19 11:21:18 -0700707 "parcel",
Andreas Huber85eabdb2016-08-25 11:24:49 -0700708 "_hidl_blob",
709 field->name(),
710 "_hidl_offset + " + std::to_string(offset),
711 true /* isReader */);
712
713 offset += fieldSize;
714 }
715
716 out.unindent();
717 out << "}\n\n";
718
Andreas Huberf630bc82016-09-09 14:52:25 -0700719 ////////////////////////////////////////////////////////////////////////////
720
Andreas Huber709b62d2016-09-19 11:21:18 -0700721 size_t structAlign, structSize;
722 getAlignmentAndSize(&structAlign, &structSize);
723
Yifan Hong1af73532016-11-09 14:32:58 -0800724 out << "public final void writeToParcel(android.os.HwParcel parcel) {\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700725 out.indent();
726
Yifan Hong1af73532016-11-09 14:32:58 -0800727 out << "android.os.HwBlob _hidl_blob = new android.os.HwBlob("
Andreas Huber85eabdb2016-08-25 11:24:49 -0700728 << structSize
729 << " /* size */);\n";
730
731 out << "writeEmbeddedToBlob(_hidl_blob, 0 /* parentOffset */);\n"
732 << "parcel.writeBuffer(_hidl_blob);\n";
733
734 out.unindent();
735 out << "}\n\n";
736
Andreas Huberf630bc82016-09-09 14:52:25 -0700737 ////////////////////////////////////////////////////////////////////////////
738
Andreas Huberf630bc82016-09-09 14:52:25 -0700739 out << "public static final void writeVectorToParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700740 out.indent(2);
Yifan Hong1af73532016-11-09 14:32:58 -0800741 out << "android.os.HwParcel parcel, java.util.ArrayList<"
Andreas Huberf630bc82016-09-09 14:52:25 -0700742 << localName()
Andreas Huber1b6822b2016-10-18 09:28:40 -0700743 << "> _hidl_vec) {\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700744 out.unindent();
745
Yifan Hong1af73532016-11-09 14:32:58 -0800746 out << "android.os.HwBlob _hidl_blob = new android.os.HwBlob(24 /* sizeof(hidl_vec<T>) */);\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700747
748 VectorType::EmitJavaFieldReaderWriterForElementType(
Andreas Huber4c865b72016-09-14 15:26:27 -0700749 out,
750 0 /* depth */,
751 this,
Andreas Huber709b62d2016-09-19 11:21:18 -0700752 "parcel",
Andreas Huber4c865b72016-09-14 15:26:27 -0700753 "_hidl_blob",
754 "_hidl_vec",
755 "0",
756 false /* isReader */);
Andreas Huberf630bc82016-09-09 14:52:25 -0700757
758 out << "\nparcel.writeBuffer(_hidl_blob);\n";
759
760 out.unindent();
761 out << "}\n\n";
762
763 ////////////////////////////////////////////////////////////////////////////
764
Andreas Huber85eabdb2016-08-25 11:24:49 -0700765 out << "public final void writeEmbeddedToBlob(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700766 out.indent(2);
Yifan Hong1af73532016-11-09 14:32:58 -0800767 out << "android.os.HwBlob _hidl_blob, long _hidl_offset) {\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700768 out.unindent();
769
770 offset = 0;
771 for (const auto &field : *mFields) {
772 size_t fieldAlign, fieldSize;
773 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
774
775 size_t pad = offset % fieldAlign;
776 if (pad > 0) {
777 offset += fieldAlign - pad;
778 }
779
780 field->type().emitJavaFieldReaderWriter(
781 out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700782 0 /* depth */,
Andreas Huber709b62d2016-09-19 11:21:18 -0700783 "parcel",
Andreas Huber85eabdb2016-08-25 11:24:49 -0700784 "_hidl_blob",
785 field->name(),
786 "_hidl_offset + " + std::to_string(offset),
787 false /* isReader */);
788
789 offset += fieldSize;
790 }
791
792 out.unindent();
793 out << "}\n";
794
795 out.unindent();
796 out << "};\n\n";
797
798 return OK;
799}
800
Andreas Huber881227d2016-08-02 14:20:21 -0700801void CompoundType::emitStructReaderWriter(
802 Formatter &out, const std::string &prefix, bool isReader) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800803
804 std::string space = prefix.empty() ? "" : (prefix + "::");
805
Andreas Huber881227d2016-08-02 14:20:21 -0700806 out << "::android::status_t "
Yifan Hong244e82d2016-11-11 11:13:57 -0800807 << (isReader ? "readEmbeddedFromParcel"
808 : "writeEmbeddedToParcel")
809 << "(\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700810
Yifan Hong0a68a282016-10-21 16:32:34 -0700811 out.indent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700812
Yifan Hong244e82d2016-11-11 11:13:57 -0800813 bool useName = false;
814 for (const auto &field : *mFields) {
815 if (field->type().useNameInEmitReaderWriterEmbedded(isReader)) {
816 useName = true;
817 break;
818 }
819 }
820 std::string name = useName ? "obj" : "/* obj */";
821 // if not useName, then obj should not be used at all,
822 // then the #error should not be emitted.
823 std::string error = useName ? "" : "\n#error\n";
824
Andreas Huber881227d2016-08-02 14:20:21 -0700825 if (isReader) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800826 out << space << localName() << " *" << name << ",\n";
Andreas Huber8a82ff72016-08-04 10:29:39 -0700827 out << "const ::android::hardware::Parcel &parcel,\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700828 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800829 out << "const " << space << localName() << " &" << name << ",\n";
Andreas Huber8a82ff72016-08-04 10:29:39 -0700830 out << "::android::hardware::Parcel *parcel,\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700831 }
832
833 out << "size_t parentHandle,\n"
834 << "size_t parentOffset)";
835
Andreas Huber881227d2016-08-02 14:20:21 -0700836 out << " {\n";
837
Yifan Hong0a68a282016-10-21 16:32:34 -0700838 out.unindent(2);
Andreas Huber881227d2016-08-02 14:20:21 -0700839 out.indent();
840
Iliyan Malchev549e2592016-08-10 08:59:12 -0700841 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700842
843 for (const auto &field : *mFields) {
844 if (!field->type().needsEmbeddedReadWrite()) {
845 continue;
846 }
847
848 field->type().emitReaderWriterEmbedded(
849 out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700850 0 /* depth */,
Yifan Hong244e82d2016-11-11 11:13:57 -0800851 name + (isReader ? "->" : ".") + field->name() + error,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700852 field->name() /* sanitizedName */,
Andreas Huber881227d2016-08-02 14:20:21 -0700853 false /* nameIsPointer */,
854 "parcel",
855 !isReader /* parcelObjIsPointer */,
856 isReader,
Andreas Huber737080b2016-08-02 15:38:04 -0700857 ErrorMode_Return,
Andreas Huber881227d2016-08-02 14:20:21 -0700858 "parentHandle",
Andreas Huber0e00de42016-08-03 09:56:02 -0700859 "parentOffset + offsetof("
860 + fullName()
861 + ", "
862 + field->name()
863 + ")");
Andreas Huber881227d2016-08-02 14:20:21 -0700864 }
865
Iliyan Malchev549e2592016-08-10 08:59:12 -0700866 out << "return _hidl_err;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700867
868 out.unindent();
869 out << "}\n\n";
870}
871
Yifan Hongbf459bc2016-08-23 16:50:37 -0700872void CompoundType::emitResolveReferenceDef(
873 Formatter &out, const std::string prefix, bool isReader) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800874 out << "::android::status_t ";
875 const std::string space(prefix.empty() ? "" : (prefix + "::"));
Yifan Hong00f47172016-09-30 14:40:45 -0700876
877 bool useParent = false;
878 for (const auto &field : *mFields) {
879 if (field->type().useParentInEmitResolveReferencesEmbedded()) {
880 useParent = true;
881 break;
882 }
883 }
884
885 std::string parentHandleName = useParent ? "parentHandle" : "/* parentHandle */";
886 std::string parentOffsetName = useParent ? "parentOffset" : "/* parentOffset */";
887
Yifan Hongbf459bc2016-08-23 16:50:37 -0700888 if (isReader) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800889 out << "readEmbeddedReferenceFromParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700890 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800891 out << space + localName() + " *obj,\n"
892 << "const ::android::hardware::Parcel &parcel,\n"
Yifan Hong00f47172016-09-30 14:40:45 -0700893 << "size_t " << parentHandleName << ", "
894 << "size_t " << parentOffsetName << ")\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700895 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700896 } else {
Yifan Hong244e82d2016-11-11 11:13:57 -0800897 out << "writeEmbeddedReferenceToParcel(\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700898 out.indent(2);
Yifan Hong244e82d2016-11-11 11:13:57 -0800899 out << "const " << space + localName() + " &obj,\n"
900 << "::android::hardware::Parcel *parcel,\n"
Yifan Hong00f47172016-09-30 14:40:45 -0700901 << "size_t " << parentHandleName << ", "
Yifan Hong244e82d2016-11-11 11:13:57 -0800902 << "size_t " << parentOffsetName << ")\n";
Yifan Hong0a68a282016-10-21 16:32:34 -0700903 out.unindent(2);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700904 }
905
906 out << " {\n";
907
908 out.indent();
909
910 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
911
Yifan Hong244e82d2016-11-11 11:13:57 -0800912 const std::string nameDeref(isReader ? "obj->" : "obj.");
Yifan Hong00f47172016-09-30 14:40:45 -0700913 // if not useParent, then parentName and offsetText
914 // should not be used at all, then the #error should not be emitted.
915 std::string error = useParent ? "" : "\n#error\n";
916
Yifan Hongbf459bc2016-08-23 16:50:37 -0700917 for (const auto &field : *mFields) {
918 if (!field->type().needsResolveReferences()) {
919 continue;
920 }
921
922 field->type().emitResolveReferencesEmbedded(
923 out,
924 0 /* depth */,
Yifan Hong244e82d2016-11-11 11:13:57 -0800925 nameDeref + field->name(),
Yifan Hongbf459bc2016-08-23 16:50:37 -0700926 field->name() /* sanitizedName */,
927 false, // nameIsPointer
928 "parcel", // const std::string &parcelObj,
929 !isReader, // bool parcelObjIsPointer,
930 isReader, // bool isReader,
931 ErrorMode_Return,
Yifan Hong00f47172016-09-30 14:40:45 -0700932 parentHandleName + error,
933 parentOffsetName
934 + " + offsetof("
Yifan Hongbf459bc2016-08-23 16:50:37 -0700935 + fullName()
936 + ", "
937 + field->name()
Yifan Hong00f47172016-09-30 14:40:45 -0700938 + ")"
939 + error);
Yifan Hongbf459bc2016-08-23 16:50:37 -0700940 }
941
Yifan Hongbf459bc2016-08-23 16:50:37 -0700942 out << "return _hidl_err;\n";
943
944 out.unindent();
945 out << "}\n\n";
946}
947
Andreas Huber881227d2016-08-02 14:20:21 -0700948bool CompoundType::needsEmbeddedReadWrite() const {
949 if (mStyle != STYLE_STRUCT) {
950 return false;
951 }
952
953 for (const auto &field : *mFields) {
954 if (field->type().needsEmbeddedReadWrite()) {
955 return true;
956 }
957 }
958
959 return false;
960}
961
Yifan Hongbf459bc2016-08-23 16:50:37 -0700962bool CompoundType::needsResolveReferences() const {
963 if (mStyle != STYLE_STRUCT) {
964 return false;
965 }
966
967 for (const auto &field : *mFields) {
968 if (field->type().needsResolveReferences()) {
969 return true;
970 }
971 }
972
973 return false;
974}
975
Andreas Huber881227d2016-08-02 14:20:21 -0700976bool CompoundType::resultNeedsDeref() const {
977 return true;
978}
979
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700980status_t CompoundType::emitVtsTypeDeclarations(Formatter &out) const {
Zhuoyao Zhangbf828c82016-10-26 14:15:10 -0700981 out << "name: \"" << fullName() << "\"\n";
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700982 out << "type: " << getVtsType() << "\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700983
984 // Emit declaration for each subtype.
985 for (const auto &type : getSubTypes()) {
986 switch (mStyle) {
987 case STYLE_STRUCT:
988 {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700989 out << "sub_struct: {\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700990 break;
991 }
992 case STYLE_UNION:
993 {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700994 out << "sub_union: {\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700995 break;
996 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700997 }
998 out.indent();
999 status_t status(type->emitVtsTypeDeclarations(out));
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07001000 if (status != OK) {
1001 return status;
1002 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001003 out.unindent();
1004 out << "}\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07001005 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001006
1007 // Emit declaration for each field.
1008 for (const auto &field : *mFields) {
1009 switch (mStyle) {
1010 case STYLE_STRUCT:
1011 {
1012 out << "struct_value: {\n";
1013 break;
1014 }
1015 case STYLE_UNION:
1016 {
1017 out << "union_value: {\n";
1018 break;
1019 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001020 }
1021 out.indent();
1022 out << "name: \"" << field->name() << "\"\n";
1023 status_t status = field->type().emitVtsAttributeType(out);
1024 if (status != OK) {
1025 return status;
1026 }
1027 out.unindent();
1028 out << "}\n";
1029 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07001030
1031 return OK;
1032}
1033
Zhuoyao Zhang864c7712016-08-16 15:35:28 -07001034status_t CompoundType::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -07001035 out << "type: " << getVtsType() << "\n";
Zhuoyao Zhangbf828c82016-10-26 14:15:10 -07001036 out << "predefined_type: \"" << fullName() << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -07001037 return OK;
1038}
1039
Andreas Huber70a59e12016-08-16 12:57:01 -07001040bool CompoundType::isJavaCompatible() const {
Andreas Huber0fa9e392016-08-31 09:05:44 -07001041 if (mStyle != STYLE_STRUCT || !Scope::isJavaCompatible()) {
1042 return false;
1043 }
1044
1045 for (const auto &field : *mFields) {
1046 if (!field->type().isJavaCompatible()) {
1047 return false;
1048 }
1049 }
1050
1051 return true;
Andreas Huber85eabdb2016-08-25 11:24:49 -07001052}
1053
Andreas Huber2639fa32017-03-30 09:10:56 -07001054bool CompoundType::containsPointer() const {
1055 if (Scope::containsPointer()) {
1056 return true;
1057 }
1058
1059 for (const auto &field : *mFields) {
1060 if (field->type().containsPointer()) {
1061 return true;
1062 }
1063 }
1064
1065 return false;
1066}
1067
Andreas Huber85eabdb2016-08-25 11:24:49 -07001068void CompoundType::getAlignmentAndSize(size_t *align, size_t *size) const {
1069 *align = 1;
Andreas Huber2639fa32017-03-30 09:10:56 -07001070 *size = 0;
Andreas Huber85eabdb2016-08-25 11:24:49 -07001071
1072 size_t offset = 0;
1073 for (const auto &field : *mFields) {
1074 // Each field is aligned according to its alignment requirement.
1075 // The surrounding structure's alignment is the maximum of its
1076 // fields' aligments.
1077
1078 size_t fieldAlign, fieldSize;
1079 field->type().getAlignmentAndSize(&fieldAlign, &fieldSize);
1080
1081 size_t pad = offset % fieldAlign;
1082 if (pad > 0) {
1083 offset += fieldAlign - pad;
1084 }
1085
Andreas Huber2639fa32017-03-30 09:10:56 -07001086 if (mStyle == STYLE_STRUCT) {
1087 offset += fieldSize;
1088 } else {
1089 *size = std::max(*size, fieldSize);
1090 }
Andreas Huber85eabdb2016-08-25 11:24:49 -07001091
1092 if (fieldAlign > (*align)) {
1093 *align = fieldAlign;
1094 }
1095 }
1096
Andreas Huber2639fa32017-03-30 09:10:56 -07001097 if (mStyle == STYLE_STRUCT) {
1098 *size = offset;
Andreas Huber85eabdb2016-08-25 11:24:49 -07001099 }
1100
Andreas Huber2639fa32017-03-30 09:10:56 -07001101 // Final padding to account for the structure's alignment.
1102 size_t pad = (*size) % (*align);
1103 if (pad > 0) {
1104 (*size) += (*align) - pad;
1105 }
Andreas Huberca4bc892017-01-09 14:58:12 -08001106
1107 if (*size == 0) {
1108 // An empty struct still occupies a byte of space in C++.
1109 *size = 1;
1110 }
Andreas Huber70a59e12016-08-16 12:57:01 -07001111}
1112
Andreas Huber31629bc2016-08-03 09:06:40 -07001113////////////////////////////////////////////////////////////////////////////////
1114
1115CompoundField::CompoundField(const char *name, Type *type)
1116 : mName(name),
1117 mType(type) {
1118}
1119
1120std::string CompoundField::name() const {
1121 return mName;
1122}
1123
1124const Type &CompoundField::type() const {
1125 return *mType;
1126}
1127
Andreas Huberc9410c72016-07-28 12:18:40 -07001128} // namespace android
1129