blob: 4c22851410091e0dd570d286c394713b43edd2ff [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 Huber881227d2016-08-02 14:20:21 -070017#include "AST.h"
18
19#include "Coordinator.h"
Iliyan Malchev40d474a2016-08-16 06:20:17 -070020#include "EnumType.h"
Andreas Huber881227d2016-08-02 14:20:21 -070021#include "Formatter.h"
22#include "Interface.h"
23#include "Method.h"
Iliyan Malchev40d474a2016-08-16 06:20:17 -070024#include "ScalarType.h"
Andreas Huber881227d2016-08-02 14:20:21 -070025#include "Scope.h"
26
Andreas Huberdca261f2016-08-04 13:47:51 -070027#include <algorithm>
Andreas Huber881227d2016-08-02 14:20:21 -070028#include <android-base/logging.h>
Andreas Huberdca261f2016-08-04 13:47:51 -070029#include <string>
Andreas Huber881227d2016-08-02 14:20:21 -070030#include <vector>
31
32namespace android {
33
Andreas Huber881227d2016-08-02 14:20:21 -070034static std::string upcase(const std::string in) {
35 std::string out{in};
36
37 for (auto &ch : out) {
38 ch = toupper(ch);
39 }
40
41 return out;
42}
43
Andreas Huberb82318c2016-08-02 14:45:54 -070044status_t AST::generateCpp(const std::string &outputPath) const {
45 status_t err = generateInterfaceHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070046
47 if (err == OK) {
Andreas Huberb82318c2016-08-02 14:45:54 -070048 err = generateStubHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070049 }
50
51 if (err == OK) {
Steven Moreland40786312016-08-16 10:29:40 -070052 err = generateHwBinderHeader(outputPath);
53 }
54
55 if (err == OK) {
Andreas Huberb82318c2016-08-02 14:45:54 -070056 err = generateProxyHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070057 }
58
59 if (err == OK) {
Andreas Huberb82318c2016-08-02 14:45:54 -070060 err = generateAllSource(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070061 }
62
63 return err;
64}
65
Andreas Huber737080b2016-08-02 15:38:04 -070066void AST::getPackageComponents(
67 std::vector<std::string> *components) const {
Andreas Huber0e00de42016-08-03 09:56:02 -070068 mPackage.getPackageComponents(components);
Andreas Huber737080b2016-08-02 15:38:04 -070069}
70
71void AST::getPackageAndVersionComponents(
72 std::vector<std::string> *components, bool cpp_compatible) const {
Andreas Huber0e00de42016-08-03 09:56:02 -070073 mPackage.getPackageAndVersionComponents(components, cpp_compatible);
Andreas Huber737080b2016-08-02 15:38:04 -070074}
75
Andreas Huber881227d2016-08-02 14:20:21 -070076std::string AST::makeHeaderGuard(const std::string &baseName) const {
77 std::vector<std::string> packageComponents;
78 getPackageAndVersionComponents(
79 &packageComponents, true /* cpp_compatible */);
80
81 std::string guard = "HIDL_GENERATED";
82 for (const auto &component : packageComponents) {
83 guard += "_";
84 guard += component;
85 }
86
87 guard += "_";
88 guard += baseName;
89 guard += "_H_";
90
91 return guard;
92}
93
94void AST::enterLeaveNamespace(Formatter &out, bool enter) const {
95 std::vector<std::string> packageComponents;
96 getPackageAndVersionComponents(
97 &packageComponents, true /* cpp_compatible */);
98
99 if (enter) {
100 for (const auto &component : packageComponents) {
101 out << "namespace " << component << " {\n";
102 }
Andreas Huber0e00de42016-08-03 09:56:02 -0700103
Andreas Huber2831d512016-08-15 09:33:47 -0700104 out.setNamespace(mPackage.cppNamespace() + "::");
Andreas Huber881227d2016-08-02 14:20:21 -0700105 } else {
Andreas Huber0e00de42016-08-03 09:56:02 -0700106 out.setNamespace(std::string());
107
Andreas Huber881227d2016-08-02 14:20:21 -0700108 for (auto it = packageComponents.rbegin();
109 it != packageComponents.rend();
110 ++it) {
111 out << "} // namespace " << *it << "\n";
112 }
113 }
114}
115
Andreas Huberb82318c2016-08-02 14:45:54 -0700116status_t AST::generateInterfaceHeader(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700117
Andreas Huberb82318c2016-08-02 14:45:54 -0700118 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700119 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700120 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Andreas Huber881227d2016-08-02 14:20:21 -0700121
122 std::string ifaceName;
123 bool isInterface = true;
124 if (!AST::isInterface(&ifaceName)) {
125 ifaceName = "types";
126 isInterface = false;
127 }
128 path.append(ifaceName);
129 path.append(".h");
130
Andreas Huberd2943e12016-08-05 11:59:31 -0700131 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700132 FILE *file = fopen(path.c_str(), "w");
133
134 if (file == NULL) {
135 return -errno;
136 }
137
138 Formatter out(file);
139
140 const std::string guard = makeHeaderGuard(ifaceName);
141
142 out << "#ifndef " << guard << "\n";
143 out << "#define " << guard << "\n\n";
144
Andreas Huber737080b2016-08-02 15:38:04 -0700145 for (const auto &item : mImportedNames) {
146 out << "#include <";
147
148 std::vector<std::string> components;
Andreas Huber0e00de42016-08-03 09:56:02 -0700149 item.getPackageAndVersionComponents(
150 &components, false /* cpp_compatible */);
Andreas Huber737080b2016-08-02 15:38:04 -0700151
152 for (const auto &component : components) {
153 out << component << "/";
154 }
155
156 out << item.name()
157 << ".h>\n";
158 }
159
160 if (!mImportedNames.empty()) {
161 out << "\n";
162 }
163
Martijn Coenen7473fab2016-08-19 14:05:40 +0200164 out << "#include <hidl/HidlSupport.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700165 out << "#include <hidl/IServiceManager.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700166
167 if (isInterface) {
Martijn Coenen93915102016-09-01 01:35:52 +0200168 out << "#include <hidl/Status.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700169 }
170
171 out << "#include <utils/NativeHandle.h>\n\n";
172
173 enterLeaveNamespace(out, true /* enter */);
174 out << "\n";
175
Steven Moreland40786312016-08-16 10:29:40 -0700176 // cut off the leading 'I'.
177 const std::string baseName = ifaceName.substr(1);
178
Andreas Huber881227d2016-08-02 14:20:21 -0700179 if (isInterface) {
180 out << "struct "
Steven Moreland40786312016-08-16 10:29:40 -0700181 << ifaceName;
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700182
183 const Interface *iface = mRootScope->getInterface();
184 const Interface *superType = iface->superType();
185
Steven Moreland40786312016-08-16 10:29:40 -0700186 if (superType == NULL) {
187 out << " : virtual public RefBase";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700188 } else {
Steven Moreland40786312016-08-16 10:29:40 -0700189 out << " : public "
190 << superType->fullName();
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700191 }
192
193 out << " {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700194
195 out.indent();
196
Andreas Huber881227d2016-08-02 14:20:21 -0700197 }
198
199 status_t err = emitTypeDeclarations(out);
200
201 if (err != OK) {
202 return err;
203 }
204
205 if (isInterface) {
206 const Interface *iface = mRootScope->getInterface();
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700207 const Interface *superType = iface->superType();
Andreas Huber881227d2016-08-02 14:20:21 -0700208
Iliyan Malchev795dd432016-09-02 11:48:26 -0700209 out << "virtual bool isRemote() const { return false; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700210 bool haveCallbacks = false;
211 for (const auto &method : iface->methods()) {
212 const bool returnsValue = !method->results().empty();
213
214 if (!returnsValue) {
215 continue;
216 }
217
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700218 if (method->canElideCallback() != nullptr) {
219 continue;
220 }
221
Andreas Huber881227d2016-08-02 14:20:21 -0700222 haveCallbacks = true;
223
224 out << "using "
225 << method->name()
226 << "_cb = std::function<void("
227 << Method::GetSignature(method->results())
228 << ")>;\n";
229 }
230
231 if (haveCallbacks) {
232 out << "\n";
233 }
234
235 for (const auto &method : iface->methods()) {
236 const bool returnsValue = !method->results().empty();
237
Andreas Huber3599d922016-08-09 10:42:57 -0700238 method->dumpAnnotations(out);
239
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700240 const TypedVar *elidedReturn = method->canElideCallback();
241 if (elidedReturn) {
242 std::string extra;
Iliyan Malchev2b6591b2016-08-18 19:15:19 -0700243 out << "virtual ::android::hardware::Return<";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700244 out << elidedReturn->type().getCppResultType(&extra) << "> ";
245 } else {
246 out << "virtual ::android::hardware::Status ";
247 }
248
249 out << method->name()
Andreas Huber881227d2016-08-02 14:20:21 -0700250 << "("
251 << Method::GetSignature(method->args());
252
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700253 if (returnsValue && elidedReturn == nullptr) {
Andreas Huber881227d2016-08-02 14:20:21 -0700254 if (!method->args().empty()) {
255 out << ", ";
256 }
257
Iliyan Malchev549e2592016-08-10 08:59:12 -0700258 out << method->name() << "_cb _hidl_cb = nullptr";
Andreas Huber881227d2016-08-02 14:20:21 -0700259 }
260
261 out << ") = 0;\n";
262 }
Steven Moreland40786312016-08-16 10:29:40 -0700263
264 out << "DECLARE_REGISTER_AND_GET_SERVICE(" << baseName << ")\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700265 }
266
267 if (isInterface) {
268 out.unindent();
269
270 out << "};\n";
271 }
272
273 out << "\n";
274 enterLeaveNamespace(out, false /* enter */);
275
276 out << "\n#endif // " << guard << "\n";
277
278 return OK;
279}
280
Steven Moreland40786312016-08-16 10:29:40 -0700281status_t AST::generateHwBinderHeader(const std::string &outputPath) const {
282 std::string ifaceName;
283 if(!AST::isInterface(&ifaceName)) {
284 // types.hal does not get an HwBinder header.
285 return OK;
286 }
287
288 // cut off the leading 'I'.
289 const std::string baseName = ifaceName.substr(1);
290
291 const std::string klassName = "IHw" + baseName;
292
293 std::string path = outputPath;
294 path.append(mCoordinator->convertPackageRootToPath(mPackage));
295 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
296 path.append(klassName + ".h");
297
298 FILE* file = fopen(path.c_str(), "w");
299
300 if (file == NULL) {
301 return -errno;
302 }
303
304 Formatter out(file);
305
306 const std::string guard = makeHeaderGuard(klassName);
307
308 out << "#ifndef " << guard << "\n";
309 out << "#define " << guard << "\n\n";
310
311 std::vector<std::string> packageComponents;
312 getPackageAndVersionComponents(
313 &packageComponents, false /* cpp_compatible */);
314
315 out << "#include <";
316 for (const auto &component : packageComponents) {
317 out << component << "/";
318 }
319 out << ifaceName << ".h>\n\n";
320
321 for (const auto &item : mImportedNames) {
322 if (item.name() == "types") {
323 continue;
324 }
325
326 out << "#include <";
327
328 std::vector<std::string> components;
329 item.getPackageAndVersionComponents(
330 &components, false /* cpp_compatible */);
331
332 for (const auto &component : components) {
333 out << component << "/";
334 }
335
336 // cut off the leading I
337 const std::string itemBaseName = item.name().substr(1);
338
339 out << "Bn"
340 << itemBaseName
341 << ".h>\n";
342 }
343
344 out << "\n";
345
346 out << "#include <hidl/HidlSupport.h>\n";
Martijn Coenen93915102016-09-01 01:35:52 +0200347 out << "#include <hidl/Status.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700348 out << "#include <hwbinder/IBinder.h>\n";
349 out << "#include <hwbinder/IInterface.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700350
351 out << "\n";
352
353 enterLeaveNamespace(out, true /* enter */);
354 out << "\n";
355
356 out << "struct "
357 << klassName
358 << " : public "
359 << ifaceName;
360
361 const Interface *iface = mRootScope->getInterface();
362 const Interface *superType = iface->superType();
363
364 out << ", public ::android::hardware::IInterface";
365
366 out << " {\n";
367
368 out.indent();
369
370 out << "DECLARE_HWBINDER_META_INTERFACE(" << baseName << ");\n\n";
371
372 out << "enum Call {\n";
373 out.indent();
374
375 bool first = true;
376 for (const auto &method : iface->methods()) {
377 out << upcase(method->name());
378
379 if (first) {
380 out << " = ";
381 if (superType != NULL) {
382 out << superType->fqName().cppNamespace()
383 << "::IHw"
384 << superType->getBaseName()
385 << "::Call::CallCount";
386 } else {
387 out << "::android::hardware::IBinder::FIRST_CALL_TRANSACTION";
388 }
389
390 first = false;
391 }
392
393 out << ",\n";
394 }
395
396 out << "CallCount\n";
397
398 out.unindent();
399 out << "};\n\n";
400
401 out.unindent();
402
403 out << "};\n\n";
404
405 enterLeaveNamespace(out, false /* enter */);
406
407 out << "\n#endif // " << guard << "\n";
408
409 return OK;
410}
411
Andreas Huber881227d2016-08-02 14:20:21 -0700412status_t AST::emitTypeDeclarations(Formatter &out) const {
413 return mRootScope->emitTypeDeclarations(out);
414}
415
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700416status_t AST::generateHeaderMethodSignatures(
Steven Moreland40786312016-08-16 10:29:40 -0700417 Formatter &out, bool stub) const {
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700418 const Interface *iface = mRootScope->getInterface();
419
420 std::vector<const Interface *> chain;
421 while (iface != NULL) {
422 chain.push_back(iface);
423 iface = iface->superType();
424 }
425
426 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
427 const Interface *superInterface = *it;
428
429 out << "// Methods from "
430 << superInterface->fullName()
431 << " follow.\n";
432
433 for (const auto &method : superInterface->methods()) {
Steven Moreland40786312016-08-16 10:29:40 -0700434 if (stub) {
435 out << "inline ";
436 }
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700437 const bool returnsValue = !method->results().empty();
438
439 const TypedVar *elidedReturn = method->canElideCallback();
440
441 if (elidedReturn == nullptr) {
442 out << "::android::hardware::Status ";
443 } else {
444 std::string extra;
Iliyan Malchev2b6591b2016-08-18 19:15:19 -0700445 out << "::android::hardware::Return<";
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700446 out << elidedReturn->type().getCppResultType(&extra) << "> ";
447 }
448 out << method->name()
449 << "("
450 << Method::GetSignature(method->args());
451
452 if (returnsValue && elidedReturn == nullptr) {
453 if (!method->args().empty()) {
454 out << ", ";
455 }
456
457 out << method->name() << "_cb _hidl_cb";
458 }
459
460 out << ") ";
Steven Moreland40786312016-08-16 10:29:40 -0700461 if (stub) {
462 out << " {\n";
463 out.indent();
464 out << "return mImpl->"
465 << method->name()
466 << "(";
467 bool first = true;
468 for (const auto &arg : method->args()) {
469 if (!first) {
470 out << ", ";
471 }
472 first = false;
473 out << arg->name();
474 }
475 if (returnsValue && elidedReturn == nullptr) {
476 if (!method->args().empty()) {
477 out << ", ";
478 }
479
480 out << "_hidl_cb";
481 }
482 out << ");\n";
483 out.unindent();
484 out << "}";
485 } else {
486 out << "override";
487 }
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700488 out << ";\n";
489 }
490
491 out << "\n";
492 }
493
494 return OK;
495}
496
Andreas Huberb82318c2016-08-02 14:45:54 -0700497status_t AST::generateStubHeader(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700498 std::string ifaceName;
499 if (!AST::isInterface(&ifaceName)) {
500 // types.hal does not get a stub header.
501 return OK;
502 }
503
Andreas Huber881227d2016-08-02 14:20:21 -0700504 // cut off the leading 'I'.
505 const std::string baseName = ifaceName.substr(1);
Steven Moreland40786312016-08-16 10:29:40 -0700506 const std::string klassName = "Bn" + baseName;
Andreas Huber881227d2016-08-02 14:20:21 -0700507
Andreas Huberb82318c2016-08-02 14:45:54 -0700508 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700509 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700510 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Steven Moreland40786312016-08-16 10:29:40 -0700511 path.append(klassName);
Andreas Huber881227d2016-08-02 14:20:21 -0700512 path.append(".h");
513
Andreas Huberd2943e12016-08-05 11:59:31 -0700514 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700515 FILE *file = fopen(path.c_str(), "w");
516
517 if (file == NULL) {
518 return -errno;
519 }
520
521 Formatter out(file);
522
Steven Moreland40786312016-08-16 10:29:40 -0700523 const std::string guard = makeHeaderGuard(klassName);
Andreas Huber881227d2016-08-02 14:20:21 -0700524
525 out << "#ifndef " << guard << "\n";
526 out << "#define " << guard << "\n\n";
527
528 std::vector<std::string> packageComponents;
529 getPackageAndVersionComponents(
530 &packageComponents, false /* cpp_compatible */);
531
532 out << "#include <";
533 for (const auto &component : packageComponents) {
534 out << component << "/";
535 }
Steven Moreland40786312016-08-16 10:29:40 -0700536 out << "IHw" << baseName << ".h>\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700537
538 enterLeaveNamespace(out, true /* enter */);
539 out << "\n";
540
541 out << "struct "
542 << "Bn"
543 << baseName
Steven Moreland40786312016-08-16 10:29:40 -0700544 << " : public ::android::hardware::BnInterface<I"
545 << baseName << ", IHw" << baseName
Andreas Huber881227d2016-08-02 14:20:21 -0700546 << "> {\n";
547
548 out.indent();
Steven Moreland40786312016-08-16 10:29:40 -0700549 out << "explicit Bn"
550 << baseName
551 << "(const ::android::sp<" << ifaceName << "> &_hidl_impl);"
552 << "\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700553 out << "::android::status_t onTransact(\n";
554 out.indent();
555 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -0700556 out << "uint32_t _hidl_code,\n";
557 out << "const ::android::hardware::Parcel &_hidl_data,\n";
558 out << "::android::hardware::Parcel *_hidl_reply,\n";
559 out << "uint32_t _hidl_flags = 0,\n";
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700560 out << "TransactCallback _hidl_cb = nullptr) override;\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700561 out.unindent();
562 out.unindent();
563
Steven Moreland40786312016-08-16 10:29:40 -0700564 generateHeaderMethodSignatures(out, true); // stub
Andreas Huber881227d2016-08-02 14:20:21 -0700565 out.unindent();
566
567 out << "};\n\n";
568
569 enterLeaveNamespace(out, false /* enter */);
570
571 out << "\n#endif // " << guard << "\n";
572
573 return OK;
574}
575
Andreas Huberb82318c2016-08-02 14:45:54 -0700576status_t AST::generateProxyHeader(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700577 std::string ifaceName;
578 if (!AST::isInterface(&ifaceName)) {
579 // types.hal does not get a proxy header.
580 return OK;
581 }
582
Andreas Huber881227d2016-08-02 14:20:21 -0700583 // cut off the leading 'I'.
584 const std::string baseName = ifaceName.substr(1);
585
Andreas Huberb82318c2016-08-02 14:45:54 -0700586 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700587 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700588 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Andreas Huber881227d2016-08-02 14:20:21 -0700589 path.append("Bp");
590 path.append(baseName);
591 path.append(".h");
592
Andreas Huberd2943e12016-08-05 11:59:31 -0700593 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700594 FILE *file = fopen(path.c_str(), "w");
595
596 if (file == NULL) {
597 return -errno;
598 }
599
600 Formatter out(file);
601
602 const std::string guard = makeHeaderGuard("Bp" + baseName);
603
604 out << "#ifndef " << guard << "\n";
605 out << "#define " << guard << "\n\n";
606
607 std::vector<std::string> packageComponents;
608 getPackageAndVersionComponents(
609 &packageComponents, false /* cpp_compatible */);
610
611 out << "#include <";
612 for (const auto &component : packageComponents) {
613 out << component << "/";
614 }
Steven Moreland40786312016-08-16 10:29:40 -0700615 out << "IHw" << baseName << ".h>\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700616
617 enterLeaveNamespace(out, true /* enter */);
618 out << "\n";
619
620 out << "struct "
621 << "Bp"
622 << baseName
Steven Moreland40786312016-08-16 10:29:40 -0700623 << " : public ::android::hardware::BpInterface<IHw"
624 << baseName
Andreas Huber881227d2016-08-02 14:20:21 -0700625 << "> {\n";
626
627 out.indent();
628
629 out << "explicit Bp"
630 << baseName
Iliyan Malchev549e2592016-08-10 08:59:12 -0700631 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl);"
Andreas Huber881227d2016-08-02 14:20:21 -0700632 << "\n\n";
633
Iliyan Malchev795dd432016-09-02 11:48:26 -0700634 out << "virtual bool isRemote() const { return true; }\n\n";
Steven Moreland40786312016-08-16 10:29:40 -0700635
636 generateHeaderMethodSignatures(out, false); // proxy
Andreas Huber881227d2016-08-02 14:20:21 -0700637
638 out.unindent();
639
640 out << "};\n\n";
641
642 enterLeaveNamespace(out, false /* enter */);
643
644 out << "\n#endif // " << guard << "\n";
645
646 return OK;
647}
648
Andreas Huberb82318c2016-08-02 14:45:54 -0700649status_t AST::generateAllSource(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700650
Andreas Huberb82318c2016-08-02 14:45:54 -0700651 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700652 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700653 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Andreas Huber881227d2016-08-02 14:20:21 -0700654
655 std::string ifaceName;
656 std::string baseName;
657
658 bool isInterface = true;
659 if (!AST::isInterface(&ifaceName)) {
660 baseName = "types";
661 isInterface = false;
662 } else {
663 baseName = ifaceName.substr(1); // cut off the leading 'I'.
664 }
665
666 path.append(baseName);
667
668 if (baseName != "types") {
669 path.append("All");
670 }
671
672 path.append(".cpp");
673
Andreas Huberd2943e12016-08-05 11:59:31 -0700674 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700675 FILE *file = fopen(path.c_str(), "w");
676
677 if (file == NULL) {
678 return -errno;
679 }
680
681 Formatter out(file);
682
683 std::vector<std::string> packageComponents;
684 getPackageAndVersionComponents(
685 &packageComponents, false /* cpp_compatible */);
686
687 std::string prefix;
688 for (const auto &component : packageComponents) {
689 prefix += component;
690 prefix += "/";
691 }
692
693 if (isInterface) {
694 out << "#include <" << prefix << "/Bp" << baseName << ".h>\n";
695 out << "#include <" << prefix << "/Bn" << baseName << ".h>\n";
696 } else {
697 out << "#include <" << prefix << "types.h>\n";
698 }
699
700 out << "\n";
701
702 enterLeaveNamespace(out, true /* enter */);
703 out << "\n";
704
705 status_t err = generateTypeSource(out, ifaceName);
706
707 if (err == OK && isInterface) {
708 err = generateProxySource(out, baseName);
709 }
710
711 if (err == OK && isInterface) {
712 err = generateStubSource(out, baseName);
713 }
714
Steven Moreland40786312016-08-16 10:29:40 -0700715 if (err == OK && isInterface) {
716 out << "IMPLEMENT_REGISTER_AND_GET_SERVICE(" << baseName << ")\n";
717 }
718
Andreas Huber881227d2016-08-02 14:20:21 -0700719 enterLeaveNamespace(out, false /* enter */);
720
721 return err;
722}
723
724status_t AST::generateTypeSource(
725 Formatter &out, const std::string &ifaceName) const {
726 return mRootScope->emitTypeDefinitions(out, ifaceName);
727}
728
Andreas Hubere7ff2282016-08-16 13:50:03 -0700729void AST::declareCppReaderLocals(
730 Formatter &out, const std::vector<TypedVar *> &args) const {
731 if (args.empty()) {
732 return;
733 }
734
735 for (const auto &arg : args) {
736 const Type &type = arg->type();
737
738 std::string extra;
739 out << type.getCppResultType(&extra)
740 << " "
741 << arg->name()
742 << extra
743 << ";\n";
744 }
745
746 out << "\n";
747}
748
Andreas Huber881227d2016-08-02 14:20:21 -0700749void AST::emitCppReaderWriter(
750 Formatter &out,
751 const std::string &parcelObj,
752 bool parcelObjIsPointer,
753 const TypedVar *arg,
754 bool isReader,
755 Type::ErrorMode mode) const {
756 const Type &type = arg->type();
757
Andreas Huber881227d2016-08-02 14:20:21 -0700758 type.emitReaderWriter(
759 out,
760 arg->name(),
761 parcelObj,
762 parcelObjIsPointer,
763 isReader,
764 mode);
765}
766
767status_t AST::generateProxySource(
768 Formatter &out, const std::string &baseName) const {
769 const std::string klassName = "Bp" + baseName;
770
771 out << klassName
772 << "::"
773 << klassName
Iliyan Malchev549e2592016-08-10 08:59:12 -0700774 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl)\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700775
776 out.indent();
777 out.indent();
778
779 out << ": BpInterface"
Steven Moreland40786312016-08-16 10:29:40 -0700780 << "<IHw"
Andreas Huber881227d2016-08-02 14:20:21 -0700781 << baseName
Iliyan Malchev549e2592016-08-10 08:59:12 -0700782 << ">(_hidl_impl) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700783
784 out.unindent();
785 out.unindent();
786 out << "}\n\n";
787
788 const Interface *iface = mRootScope->getInterface();
789
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700790 std::vector<const Interface *> chain;
791 while (iface != NULL) {
792 chain.push_back(iface);
793 iface = iface->superType();
794 }
Andreas Huber881227d2016-08-02 14:20:21 -0700795
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700796 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
797 const Interface *superInterface = *it;
Andreas Huber881227d2016-08-02 14:20:21 -0700798
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700799 for (const auto &method : superInterface->methods()) {
800 const bool returnsValue = !method->results().empty();
Andreas Huber881227d2016-08-02 14:20:21 -0700801
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700802 const TypedVar *elidedReturn = method->canElideCallback();
803 if (elidedReturn) {
804 std::string extra;
Iliyan Malchev2b6591b2016-08-18 19:15:19 -0700805 out << "::android::hardware::Return<";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700806 out << elidedReturn->type().getCppResultType(&extra) << "> ";
807 } else {
808 out << "::android::hardware::Status ";
809 }
810
811 out << klassName
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700812 << "::"
813 << method->name()
814 << "("
815 << Method::GetSignature(method->args());
Andreas Huber881227d2016-08-02 14:20:21 -0700816
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700817 if (returnsValue && elidedReturn == nullptr) {
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700818 if (!method->args().empty()) {
Andreas Huber881227d2016-08-02 14:20:21 -0700819 out << ", ";
820 }
821
Iliyan Malchev549e2592016-08-10 08:59:12 -0700822 out << method->name() << "_cb _hidl_cb";
Andreas Huber881227d2016-08-02 14:20:21 -0700823 }
824
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700825 out << ") {\n";
826
827 out.indent();
828
Iliyan Malchev549e2592016-08-10 08:59:12 -0700829 out << "::android::hardware::Parcel _hidl_data;\n";
830 out << "::android::hardware::Parcel _hidl_reply;\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700831 out << "::android::status_t _hidl_err;\n";
832 out << "::android::hardware::Status _hidl_status;\n\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700833 declareCppReaderLocals(out, method->results());
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700834
Iliyan Malchev549e2592016-08-10 08:59:12 -0700835 out << "_hidl_err = _hidl_data.writeInterfaceToken("
Steven Moreland40786312016-08-16 10:29:40 -0700836 << superInterface->fqName().cppNamespace()
837 << "::IHw"
838 << superInterface->getBaseName()
839 << "::descriptor);\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700840
Iliyan Malchev549e2592016-08-10 08:59:12 -0700841 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700842
843 for (const auto &arg : method->args()) {
844 emitCppReaderWriter(
845 out,
Iliyan Malchev549e2592016-08-10 08:59:12 -0700846 "_hidl_data",
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700847 false /* parcelObjIsPointer */,
848 arg,
849 false /* reader */,
850 Type::ErrorMode_Goto);
851 }
852
Steven Moreland40786312016-08-16 10:29:40 -0700853 out << "_hidl_err = remote()->transact("
854 << superInterface->fqName().cppNamespace()
855 << "::IHw"
856 << superInterface->getBaseName()
857 << "::Call::"
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700858 << upcase(method->name())
Iliyan Malchev639bff82016-08-13 14:24:11 -0700859 << ", _hidl_data, &_hidl_reply";
860 if (method->isOneway()) {
861 out << ", ::android::hardware::IBinder::FLAG_ONEWAY";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700862 }
Iliyan Malchev639bff82016-08-13 14:24:11 -0700863 out << ");\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700864
Iliyan Malchev639bff82016-08-13 14:24:11 -0700865 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700866
Iliyan Malchev639bff82016-08-13 14:24:11 -0700867 if (!method->isOneway()) {
868 out << "_hidl_err = _hidl_status.readFromParcel(_hidl_reply);\n";
869 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Iliyan Malchev639bff82016-08-13 14:24:11 -0700870 out << "if (!_hidl_status.isOk()) { return _hidl_status; }\n\n";
871
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700872 for (const auto &arg : method->results()) {
Iliyan Malchev639bff82016-08-13 14:24:11 -0700873 emitCppReaderWriter(
874 out,
875 "_hidl_reply",
876 false /* parcelObjIsPointer */,
877 arg,
878 true /* reader */,
879 Type::ErrorMode_Goto);
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700880 }
881
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700882 if (returnsValue && elidedReturn == nullptr) {
Iliyan Malchev639bff82016-08-13 14:24:11 -0700883 out << "if (_hidl_cb != nullptr) {\n";
884 out.indent();
885 out << "_hidl_cb(";
886
887 bool first = true;
888 for (const auto &arg : method->results()) {
889 if (!first) {
890 out << ", ";
891 }
892
893 if (arg->type().resultNeedsDeref()) {
894 out << "*";
895 }
896 out << arg->name();
897
898 first = false;
899 }
900
901 out << ");\n";
902 out.unindent();
903 out << "}\n\n";
904 }
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700905 }
906
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700907 if (elidedReturn != nullptr) {
908 std::string extra;
909
910 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
Iliyan Malchev2b6591b2016-08-18 19:15:19 -0700911 out << "return ::android::hardware::Return<";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700912 out << elidedReturn->type().getCppResultType(&extra)
913 << ">(" << elidedReturn->name() << ");\n\n";
914
915 out.unindent();
916 out << "_hidl_error:\n";
917 out.indent();
918 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
Iliyan Malchev2b6591b2016-08-18 19:15:19 -0700919 out << "return ::android::hardware::Return<";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700920 out << method->results().at(0)->type().getCppResultType(&extra)
921 << ">(_hidl_status);\n";
922 } else {
923 out.unindent();
924 out << "_hidl_error:\n";
925 out.indent();
926 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
927 out << "return _hidl_status;\n";
928 }
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700929
Andreas Huber881227d2016-08-02 14:20:21 -0700930 out.unindent();
931 out << "}\n\n";
932 }
Andreas Huber881227d2016-08-02 14:20:21 -0700933 }
934
935 return OK;
936}
937
938status_t AST::generateStubSource(
939 Formatter &out, const std::string &baseName) const {
940 out << "IMPLEMENT_HWBINDER_META_INTERFACE("
941 << baseName
942 << ", \""
943 << mPackage.string()
944 << "::I"
945 << baseName
946 << "\");\n\n";
947
948 const std::string klassName = "Bn" + baseName;
949
Steven Moreland40786312016-08-16 10:29:40 -0700950 out << klassName
951 << "::"
952 << klassName
953 << "(const ::android::sp<I" << baseName <<"> &_hidl_impl)\n";
954
955 out.indent();
956 out.indent();
957
958 out << ": BnInterface"
959 << "<I"
960 << baseName
961 << ", IHw"
962 << baseName
963 << ">(_hidl_impl) {\n";
964
965 out.unindent();
966 out.unindent();
967 out << "}\n\n";
968
Andreas Huber881227d2016-08-02 14:20:21 -0700969 out << "::android::status_t " << klassName << "::onTransact(\n";
970
971 out.indent();
972 out.indent();
973
Iliyan Malchev549e2592016-08-10 08:59:12 -0700974 out << "uint32_t _hidl_code,\n"
975 << "const ::android::hardware::Parcel &_hidl_data,\n"
976 << "::android::hardware::Parcel *_hidl_reply,\n"
977 << "uint32_t _hidl_flags,\n"
978 << "TransactCallback _hidl_cb) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700979
980 out.unindent();
981
Iliyan Malchev549e2592016-08-10 08:59:12 -0700982 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
Iliyan Malchev549e2592016-08-10 08:59:12 -0700983 out << "switch (_hidl_code) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700984 out.indent();
985
986 const Interface *iface = mRootScope->getInterface();
987
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700988 std::vector<const Interface *> chain;
989 while (iface != NULL) {
990 chain.push_back(iface);
991 iface = iface->superType();
992 }
Andreas Huber881227d2016-08-02 14:20:21 -0700993
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700994 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
995 const Interface *superInterface = *it;
Andreas Huber881227d2016-08-02 14:20:21 -0700996
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700997 for (const auto &method : superInterface->methods()) {
998 out << "case "
Steven Moreland40786312016-08-16 10:29:40 -0700999 << superInterface->fqName().cppNamespace()
1000 << "::IHw"
1001 << superInterface->getBaseName()
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001002 << "::Call::"
1003 << upcase(method->name())
1004 << ":\n{\n";
1005
1006 out.indent();
1007
1008 status_t err =
1009 generateStubSourceForMethod(out, superInterface, method);
1010
1011 if (err != OK) {
1012 return err;
1013 }
1014
1015 out.unindent();
1016 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001017 }
Andreas Huber881227d2016-08-02 14:20:21 -07001018 }
1019
1020 out << "default:\n{\n";
1021 out.indent();
1022
Andreas Huber8a82ff72016-08-04 10:29:39 -07001023 out << "return ::android::hardware::BnInterface<I"
Steven Moreland40786312016-08-16 10:29:40 -07001024 << baseName << ", IHw" << baseName
Andreas Huber881227d2016-08-02 14:20:21 -07001025 << ">::onTransact(\n";
1026
1027 out.indent();
1028 out.indent();
1029
Iliyan Malchev549e2592016-08-10 08:59:12 -07001030 out << "_hidl_code, _hidl_data, _hidl_reply, "
1031 << "_hidl_flags, _hidl_cb);\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001032
1033 out.unindent();
1034 out.unindent();
1035
1036 out.unindent();
1037 out << "}\n";
1038
1039 out.unindent();
1040 out << "}\n\n";
1041
Iliyan Malchev549e2592016-08-10 08:59:12 -07001042 out << "if (_hidl_err == ::android::UNEXPECTED_NULL) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001043 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -07001044 out << "_hidl_err = ::android::hardware::Status::fromExceptionCode(\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001045 out.indent();
1046 out.indent();
Andreas Huber8a82ff72016-08-04 10:29:39 -07001047 out << "::android::hardware::Status::EX_NULL_POINTER)\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001048 out.indent();
1049 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -07001050 out << ".writeToParcel(_hidl_reply);\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001051 out.unindent();
1052 out.unindent();
1053 out.unindent();
1054 out.unindent();
1055
1056 out.unindent();
1057 out << "}\n\n";
1058
Iliyan Malchev549e2592016-08-10 08:59:12 -07001059 out << "return _hidl_err;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001060
1061 out.unindent();
1062 out << "}\n\n";
1063
1064 return OK;
1065}
1066
1067status_t AST::generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001068 Formatter &out, const Interface *iface, const Method *method) const {
Iliyan Malchev549e2592016-08-10 08:59:12 -07001069 out << "if (!_hidl_data.enforceInterface("
Steven Moreland40786312016-08-16 10:29:40 -07001070 << iface->fqName().cppNamespace()
1071 << "::IHw"
1072 << iface->getBaseName()
1073 << "::descriptor)) {\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001074
Andreas Huber881227d2016-08-02 14:20:21 -07001075 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -07001076 out << "_hidl_err = ::android::BAD_TYPE;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001077 out << "break;\n";
1078 out.unindent();
1079 out << "}\n\n";
1080
Andreas Hubere7ff2282016-08-16 13:50:03 -07001081 declareCppReaderLocals(out, method->args());
1082
Andreas Huber881227d2016-08-02 14:20:21 -07001083 for (const auto &arg : method->args()) {
1084 emitCppReaderWriter(
1085 out,
Iliyan Malchev549e2592016-08-10 08:59:12 -07001086 "_hidl_data",
Andreas Huber881227d2016-08-02 14:20:21 -07001087 false /* parcelObjIsPointer */,
1088 arg,
1089 true /* reader */,
1090 Type::ErrorMode_Break);
1091 }
1092
1093 const bool returnsValue = !method->results().empty();
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001094 const TypedVar *elidedReturn = method->canElideCallback();
Andreas Huber881227d2016-08-02 14:20:21 -07001095
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001096 if (elidedReturn != nullptr) {
1097 std::string extra;
Andreas Huber881227d2016-08-02 14:20:21 -07001098
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001099 out << elidedReturn->type().getCppResultType(&extra) << " ";
1100 out << elidedReturn->name() << " = ";
1101 out << method->name() << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001102
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001103 bool first = true;
1104 for (const auto &arg : method->args()) {
Andreas Huber881227d2016-08-02 14:20:21 -07001105 if (!first) {
1106 out << ", ";
1107 }
1108
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001109 if (arg->type().resultNeedsDeref()) {
1110 out << "*";
1111 }
1112
1113 out << arg->name();
Andreas Huber881227d2016-08-02 14:20:21 -07001114
1115 first = false;
1116 }
1117
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001118 out << ");\n\n";
Andreas Huber8a82ff72016-08-04 10:29:39 -07001119 out << "::android::hardware::Status::ok()"
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001120 << ".writeToParcel(_hidl_reply);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001121
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001122 elidedReturn->type().emitReaderWriter(
1123 out,
1124 elidedReturn->name(),
1125 "_hidl_reply",
1126 true, /* parcelObjIsPointer */
1127 false, /* isReader */
1128 Type::ErrorMode_Ignore);
Andreas Huber881227d2016-08-02 14:20:21 -07001129
Iliyan Malchev549e2592016-08-10 08:59:12 -07001130 out << "_hidl_cb(*_hidl_reply);\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001131 } else {
1132 if (returnsValue) {
1133 out << "bool _hidl_callbackCalled = false;\n\n";
1134 }
Andreas Huber881227d2016-08-02 14:20:21 -07001135
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001136 out << "::android::hardware::Status _hidl_status(\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001137 out.indent();
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001138 out.indent();
1139 out << method->name() << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001140
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001141 bool first = true;
1142 for (const auto &arg : method->args()) {
1143 if (!first) {
1144 out << ", ";
1145 }
Andreas Huber881227d2016-08-02 14:20:21 -07001146
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001147 if (arg->type().resultNeedsDeref()) {
1148 out << "*";
1149 }
1150
1151 out << arg->name();
1152
1153 first = false;
1154 }
1155
1156 if (returnsValue) {
1157 if (!first) {
1158 out << ", ";
1159 }
1160
1161 out << "[&](";
1162
1163 first = true;
1164 for (const auto &arg : method->results()) {
1165 if (!first) {
1166 out << ", ";
1167 }
1168
1169 out << "const auto &" << arg->name();
1170
1171 first = false;
1172 }
1173
1174 out << ") {\n";
1175 out.indent();
1176 out << "_hidl_callbackCalled = true;\n\n";
1177
1178 out << "::android::hardware::Status::ok()"
1179 << ".writeToParcel(_hidl_reply);\n\n";
1180
1181 for (const auto &arg : method->results()) {
1182 emitCppReaderWriter(
1183 out,
1184 "_hidl_reply",
1185 true /* parcelObjIsPointer */,
1186 arg,
1187 false /* reader */,
1188 Type::ErrorMode_Ignore);
1189 }
1190
1191 out << "_hidl_cb(*_hidl_reply);\n";
1192
1193 out.unindent();
1194 out << "}\n";
1195 }
1196
Andreas Huber881227d2016-08-02 14:20:21 -07001197 out.unindent();
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001198 out.unindent();
1199 out << "));\n\n";
1200
1201 if (returnsValue) {
1202 out << "if (!_hidl_callbackCalled) {\n";
1203 out.indent();
1204 }
1205
1206 out << "_hidl_err = _hidl_status.writeToParcel(_hidl_reply);\n";
1207
1208 if (returnsValue) {
1209 out.unindent();
1210 out << "}\n\n";
1211 }
Andreas Huber881227d2016-08-02 14:20:21 -07001212 }
1213
1214 out << "break;\n";
1215
1216 return OK;
1217}
1218
1219} // namespace android
1220