-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateCpp.cpp
1931 lines (1593 loc) · 64 KB
/
generateCpp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "AST.h"
#include "Coordinator.h"
#include "EnumType.h"
#include "HidlTypeAssertion.h"
#include "Interface.h"
#include "Location.h"
#include "Method.h"
#include "Reference.h"
#include "ScalarType.h"
#include "Scope.h"
#include <algorithm>
#include <hidl-util/Formatter.h>
#include <hidl-util/StringHelper.h>
#include <android-base/logging.h>
#include <string>
#include <vector>
namespace android {
std::string AST::makeHeaderGuard(const std::string &baseName,
bool indicateGenerated) const {
std::string guard;
if (indicateGenerated) {
guard += "HIDL_GENERATED_";
}
guard += StringHelper::Uppercase(mPackage.tokenName());
guard += "_";
guard += StringHelper::Uppercase(baseName);
guard += "_H";
return guard;
}
void AST::generateCppPackageInclude(
Formatter &out,
const FQName &package,
const std::string &klass) {
out << "#include <";
std::vector<std::string> components =
package.getPackageAndVersionComponents(false /* sanitized */);
for (const auto &component : components) {
out << component << "/";
}
out << klass
<< ".h>\n";
}
void AST::enterLeaveNamespace(Formatter &out, bool enter) const {
std::vector<std::string> packageComponents =
mPackage.getPackageAndVersionComponents(true /* sanitized */);
if (enter) {
for (const auto &component : packageComponents) {
out << "namespace " << component << " {\n";
}
} else {
for (auto it = packageComponents.rbegin();
it != packageComponents.rend();
++it) {
out << "} // namespace " << *it << "\n";
}
}
}
static void declareGetService(Formatter &out, const std::string &interfaceName, bool isTry) {
const std::string functionName = isTry ? "tryGetService" : "getService";
if (isTry) {
DocComment(
"This gets the service of this type with the specified instance name. If the\n"
"service is currently not available or not in the VINTF manifest on a Trebilized\n"
"device, this will return nullptr. This is useful when you don't want to block\n"
"during device boot. If getStub is true, this will try to return an unwrapped\n"
"passthrough implementation in the same process. This is useful when getting an\n"
"implementation from the same partition/compilation group.\n\n"
"In general, prefer getService(std::string,bool)",
HIDL_LOCATION_HERE)
.emit(out);
} else {
DocComment(
"This gets the service of this type with the specified instance name. If the\n"
"service is not in the VINTF manifest on a Trebilized device, this will return\n"
"nullptr. If the service is not available, this will wait for the service to\n"
"become available. If the service is a lazy service, this will start the service\n"
"and return when it becomes available. If getStub is true, this will try to\n"
"return an unwrapped passthrough implementation in the same process. This is\n"
"useful when getting an implementation from the same partition/compilation group.",
HIDL_LOCATION_HERE)
.emit(out);
}
out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
<< "const std::string &serviceName=\"default\", bool getStub=false);\n";
DocComment("Deprecated. See " + functionName + "(std::string, bool)", HIDL_LOCATION_HERE)
.emit(out);
out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
<< "const char serviceName[], bool getStub=false)"
<< " { std::string str(serviceName ? serviceName : \"\");"
<< " return " << functionName << "(str, getStub); }\n";
DocComment("Deprecated. See " + functionName + "(std::string, bool)", HIDL_LOCATION_HERE)
.emit(out);
out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
<< "const ::android::hardware::hidl_string& serviceName, bool getStub=false)"
// without c_str the std::string constructor is ambiguous
<< " { std::string str(serviceName.c_str());"
<< " return " << functionName << "(str, getStub); }\n";
DocComment("Calls " + functionName +
"(\"default\", bool). This is the recommended instance name for singleton "
"services.",
HIDL_LOCATION_HERE)
.emit(out);
out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
<< "bool getStub) { return " << functionName << "(\"default\", getStub); }\n";
}
static void declareServiceManagerInteractions(Formatter &out, const std::string &interfaceName) {
declareGetService(out, interfaceName, true /* isTry */);
declareGetService(out, interfaceName, false /* isTry */);
DocComment(
"Registers a service with the service manager. For Trebilized devices, the service\n"
"must also be in the VINTF manifest.",
HIDL_LOCATION_HERE)
.emit(out);
out << "__attribute__ ((warn_unused_result))"
<< "::android::status_t registerAsService(const std::string &serviceName=\"default\");\n";
DocComment("Registers for notifications for when a service is registered.", HIDL_LOCATION_HERE)
.emit(out);
out << "static bool registerForNotifications(\n";
out.indent(2, [&] {
out << "const std::string &serviceName,\n"
<< "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> "
<< "¬ification);\n";
});
}
static void implementGetService(Formatter &out,
const FQName &fqName,
bool isTry) {
const std::string interfaceName = fqName.getInterfaceName();
const std::string functionName = isTry ? "tryGetService" : "getService";
out << "::android::sp<" << interfaceName << "> " << interfaceName << "::" << functionName << "("
<< "const std::string &serviceName, const bool getStub) ";
out.block([&] {
out << "return ::android::hardware::details::getServiceInternal<"
<< fqName.getInterfaceProxyName()
<< ">(serviceName, "
<< (!isTry ? "true" : "false") // retry
<< ", getStub);\n";
}).endl().endl();
}
static void implementServiceManagerInteractions(Formatter &out,
const FQName &fqName, const std::string &package) {
const std::string interfaceName = fqName.getInterfaceName();
implementGetService(out, fqName, true /* isTry */);
implementGetService(out, fqName, false /* isTry */);
out << "::android::status_t " << interfaceName << "::registerAsService("
<< "const std::string &serviceName) ";
out.block([&] {
out << "return ::android::hardware::details::registerAsServiceInternal(this, serviceName);\n";
}).endl().endl();
out << "bool " << interfaceName << "::registerForNotifications(\n";
out.indent(2, [&] {
out << "const std::string &serviceName,\n"
<< "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> "
<< "¬ification) ";
});
out.block([&] {
out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n";
out.indent(2, [&] {
out << "= ::android::hardware::defaultServiceManager();\n";
});
out.sIf("sm == nullptr", [&] {
out << "return false;\n";
}).endl();
out << "::android::hardware::Return<bool> success =\n";
out.indent(2, [&] {
out << "sm->registerForNotifications(\"" << package << "::" << interfaceName << "\",\n";
out.indent(2, [&] {
out << "serviceName, notification);\n";
});
});
out << "return success.isOk() && success;\n";
}).endl().endl();
}
void AST::generateInterfaceHeader(Formatter& out) const {
const Interface *iface = getInterface();
std::string ifaceName = iface ? iface->definedName() : "types";
const std::string guard = makeHeaderGuard(ifaceName);
out << "#ifndef " << guard << "\n";
out << "#define " << guard << "\n\n";
for (const auto &item : mImportedNames) {
generateCppPackageInclude(out, item, item.name());
}
if (!mImportedNames.empty()) {
out << "\n";
}
if (iface) {
if (isIBase()) {
out << "// skipped #include IServiceNotification.h\n\n";
} else {
out << "#include <android/hidl/manager/1.0/IServiceNotification.h>\n\n";
}
}
out << "#include <hidl/HidlSupport.h>\n";
out << "#include <hidl/MQDescriptor.h>\n";
if (iface) {
out << "#include <hidl/Status.h>\n";
}
out << "#include <utils/NativeHandle.h>\n";
out << "#include <utils/misc.h>\n\n"; /* for report_sysprop_change() */
enterLeaveNamespace(out, true /* enter */);
out << "\n";
if (iface) {
iface->emitDocComment(out);
out << "struct "
<< ifaceName;
const Interface *superType = iface->superType();
if (superType == nullptr) {
out << " : virtual public ::android::RefBase";
} else {
out << " : public "
<< superType->fullName();
}
out << " {\n";
out.indent();
DocComment("Type tag for use in template logic that indicates this is a 'pure' class.",
HIDL_LOCATION_HERE)
.emit(out);
generateCppTag(out, "::android::hardware::details::i_tag");
DocComment("Fully qualified interface name: \"" + iface->fqName().string() + "\"",
HIDL_LOCATION_HERE)
.emit(out);
out << "static const char* descriptor;\n\n";
iface->emitTypeDeclarations(out);
} else {
mRootScope.emitTypeDeclarations(out);
}
if (iface) {
DocComment(
"Returns whether this object's implementation is outside of the current process.",
HIDL_LOCATION_HERE)
.emit(out);
out << "virtual bool isRemote() const ";
if (!isIBase()) {
out << "override ";
}
out << "{ return false; }\n";
for (const auto& tuple : iface->allMethodsFromRoot()) {
const Method* method = tuple.method();
out << "\n";
const bool returnsValue = !method->results().empty();
const NamedReference<Type>* elidedReturn = method->canElideCallback();
if (elidedReturn == nullptr && returnsValue) {
DocComment("Return callback for " + method->name(), HIDL_LOCATION_HERE).emit(out);
out << "using "
<< method->name()
<< "_cb = std::function<void(";
method->emitCppResultSignature(out, true /* specify namespaces */);
out << ")>;\n";
}
method->emitDocComment(out);
if (elidedReturn) {
out << "virtual ::android::hardware::Return<";
out << elidedReturn->type().getCppResultType() << "> ";
} else {
out << "virtual ::android::hardware::Return<void> ";
}
out << method->name()
<< "(";
method->emitCppArgSignature(out, true /* specify namespaces */);
out << ")";
if (method->isHidlReserved()) {
if (!isIBase()) {
out << " override";
}
} else {
out << " = 0";
}
out << ";\n";
}
out << "\n// cast static functions\n";
std::string childTypeResult = iface->getCppResultType();
for (const Interface *superType : iface->typeChain()) {
DocComment(
"This performs a checked cast based on what the underlying implementation "
"actually is.",
HIDL_LOCATION_HERE)
.emit(out);
out << "static ::android::hardware::Return<"
<< childTypeResult
<< "> castFrom("
<< superType->getCppArgumentType()
<< " parent"
<< ", bool emitError = false);\n";
}
if (isIBase()) {
out << "\n// skipped getService, registerAsService, registerForNotifications\n\n";
} else {
out << "\n// helper methods for interactions with the hwservicemanager\n";
declareServiceManagerInteractions(out, iface->definedName());
}
}
if (iface) {
out.unindent();
out << "};\n\n";
}
out << "//\n";
out << "// type declarations for package\n";
out << "//\n\n";
mRootScope.emitPackageTypeDeclarations(out);
out << "//\n";
out << "// type header definitions for package\n";
out << "//\n\n";
mRootScope.emitPackageTypeHeaderDefinitions(out);
out << "\n";
enterLeaveNamespace(out, false /* enter */);
out << "\n";
out << "//\n";
out << "// global type declarations for package\n";
out << "//\n\n";
mRootScope.emitGlobalTypeDeclarations(out);
out << "\n#endif // " << guard << "\n";
}
void AST::generateHwBinderHeader(Formatter& out) const {
const Interface *iface = getInterface();
std::string klassName = iface ? iface->getHwName() : "hwtypes";
const std::string guard = makeHeaderGuard(klassName);
out << "#ifndef " << guard << "\n";
out << "#define " << guard << "\n\n";
generateCppPackageInclude(out, mPackage, iface ? iface->definedName() : "types");
out << "\n";
for (const auto &item : mImportedNames) {
if (item.name() == "types") {
generateCppPackageInclude(out, item, "hwtypes");
} else {
generateCppPackageInclude(out, item, item.getInterfaceStubName());
generateCppPackageInclude(out, item, item.getInterfaceProxyName());
}
}
out << "\n";
out << "#include <hidl/Status.h>\n";
out << "#include <hwbinder/IBinder.h>\n";
out << "#include <hwbinder/Parcel.h>\n";
out << "\n";
enterLeaveNamespace(out, true /* enter */);
mRootScope.emitPackageHwDeclarations(out);
enterLeaveNamespace(out, false /* enter */);
out << "\n#endif // " << guard << "\n";
}
static std::string wrapPassthroughArg(Formatter& out, const NamedReference<Type>* arg,
std::string name, std::function<void(void)> handleError) {
if (!arg->type().isInterface()) {
return name;
}
std::string wrappedName = "_hidl_wrapped_" + name;
const Interface &iface = static_cast<const Interface &>(arg->type());
out << iface.getCppStackType() << " " << wrappedName << ";\n";
// TODO(elsk): b/33754152 Should not wrap this if object is Bs*
out.sIf(name + " != nullptr && !" + name + "->isRemote()", [&] {
out << wrappedName
<< " = "
<< "::android::hardware::details::wrapPassthrough("
<< name
<< ");\n";
out.sIf(wrappedName + " == nullptr", [&] {
// Fatal error. Happens when the BsFoo class is not found in the binary
// or any dynamic libraries.
handleError();
}).endl();
}).sElse([&] {
out << wrappedName << " = " << name << ";\n";
}).endl().endl();
return wrappedName;
}
void AST::generatePassthroughMethod(Formatter& out, const Method* method, const Interface* superInterface) const {
method->generateCppSignature(out);
out << " override {\n";
out.indent();
if (method->isHidlReserved()
&& method->overridesCppImpl(IMPL_PASSTHROUGH)) {
method->cppImpl(IMPL_PASSTHROUGH, out);
out.unindent();
out << "}\n\n";
return;
}
const bool returnsValue = !method->results().empty();
const NamedReference<Type>* elidedReturn = method->canElideCallback();
generateCppInstrumentationCall(
out,
InstrumentationEvent::PASSTHROUGH_ENTRY,
method,
superInterface);
std::vector<std::string> wrappedArgNames;
for (const auto &arg : method->args()) {
std::string name = wrapPassthroughArg(out, arg, arg->name(), [&] {
out << "return ::android::hardware::Status::fromExceptionCode(\n";
out.indent(2, [&] {
out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
<< "\"Cannot wrap passthrough interface.\");\n";
});
});
wrappedArgNames.push_back(name);
}
out << "::android::hardware::Status _hidl_error = ::android::hardware::Status::ok();\n";
out << "auto _hidl_return = ";
if (method->isOneway()) {
out << "addOnewayTask([mImpl = this->mImpl\n"
<< "#ifdef __ANDROID_DEBUGGABLE__\n"
", mEnableInstrumentation = this->mEnableInstrumentation, "
"mInstrumentationCallbacks = this->mInstrumentationCallbacks\n"
<< "#endif // __ANDROID_DEBUGGABLE__\n";
for (const std::string& arg : wrappedArgNames) {
out << ", " << arg;
}
out << "] {\n";
out.indent();
}
out << "mImpl->"
<< method->name()
<< "(";
out.join(method->args().begin(), method->args().end(), ", ", [&](const auto &arg) {
out << (arg->type().isInterface() ? "_hidl_wrapped_" : "") << arg->name();
});
std::function<void(void)> kHandlePassthroughError = [&] {
out << "_hidl_error = ::android::hardware::Status::fromExceptionCode(\n";
out.indent(2, [&] {
out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
<< "\"Cannot wrap passthrough interface.\");\n";
});
};
if (returnsValue && elidedReturn == nullptr) {
// never true if oneway since oneway methods don't return values
if (!method->args().empty()) {
out << ", ";
}
out << "[&](";
out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
out << "const auto &_hidl_out_"
<< arg->name();
});
out << ") {\n";
out.indent();
generateCppInstrumentationCall(
out,
InstrumentationEvent::PASSTHROUGH_EXIT,
method,
superInterface);
std::vector<std::string> wrappedOutNames;
for (const auto &arg : method->results()) {
wrappedOutNames.push_back(
wrapPassthroughArg(out, arg, "_hidl_out_" + arg->name(), kHandlePassthroughError));
}
out << "_hidl_cb(";
out.join(wrappedOutNames.begin(), wrappedOutNames.end(), ", ",
[&](const std::string& arg) { out << arg; });
out << ");\n";
out.unindent();
out << "});\n\n";
} else {
out << ");\n\n";
if (elidedReturn != nullptr) {
const std::string outName = "_hidl_out_" + elidedReturn->name();
out << elidedReturn->type().getCppResultType() << " " << outName
<< " = _hidl_return;\n";
out << "(void) " << outName << ";\n";
const std::string wrappedName =
wrapPassthroughArg(out, elidedReturn, outName, kHandlePassthroughError);
if (outName != wrappedName) {
// update the original value since it is used by generateCppInstrumentationCall
out << outName << " = " << wrappedName << ";\n\n";
// update the value to be returned
out << "_hidl_return = " << outName << "\n;";
}
}
generateCppInstrumentationCall(
out,
InstrumentationEvent::PASSTHROUGH_EXIT,
method,
superInterface);
}
if (method->isOneway()) {
out.unindent();
out << "});\n";
} else {
out << "if (!_hidl_error.isOk()) return _hidl_error;\n";
}
out << "return _hidl_return;\n";
out.unindent();
out << "}\n";
}
void AST::generateMethods(Formatter& out, const MethodGenerator& gen, bool includeParent) const {
const Interface* iface = mRootScope.getInterface();
const Interface *prevIterface = nullptr;
for (const auto &tuple : iface->allMethodsFromRoot()) {
const Method *method = tuple.method();
const Interface *superInterface = tuple.interface();
if (!includeParent && superInterface != iface) {
continue;
}
if(prevIterface != superInterface) {
if (prevIterface != nullptr) {
out << "\n";
}
out << "// Methods from "
<< superInterface->fullName()
<< " follow.\n";
prevIterface = superInterface;
}
gen(method, superInterface);
}
out << "\n";
}
void AST::generateTemplatizationLink(Formatter& out) const {
DocComment("The pure class is what this class wraps.", HIDL_LOCATION_HERE).emit(out);
out << "typedef " << mRootScope.getInterface()->definedName() << " Pure;\n\n";
}
void AST::generateCppTag(Formatter& out, const std::string& tag) const {
out << "typedef " << tag << " _hidl_tag;\n\n";
}
void AST::generateStubHeader(Formatter& out) const {
CHECK(AST::isInterface());
const Interface* iface = mRootScope.getInterface();
const std::string klassName = iface->getStubName();
const std::string guard = makeHeaderGuard(klassName);
out << "#ifndef " << guard << "\n";
out << "#define " << guard << "\n\n";
generateCppPackageInclude(out, mPackage, iface->getHwName());
out << "\n";
enterLeaveNamespace(out, true /* enter */);
out << "\n";
out << "struct "
<< klassName;
if (iface->isIBase()) {
out << " : public ::android::hardware::BHwBinder";
out << ", public ::android::hardware::details::HidlInstrumentor {\n";
} else {
out << " : public "
<< gIBaseFqName.getInterfaceStubFqName().cppName()
<< " {\n";
}
out.indent();
out << "explicit " << klassName << "(const ::android::sp<" << iface->definedName()
<< "> &_hidl_impl);"
<< "\n";
out << "explicit " << klassName << "(const ::android::sp<" << iface->definedName()
<< "> &_hidl_impl,"
<< " const std::string& HidlInstrumentor_package,"
<< " const std::string& HidlInstrumentor_interface);"
<< "\n\n";
out << "virtual ~" << klassName << "();\n\n";
out << "::android::status_t onTransact(\n";
out.indent();
out.indent();
out << "uint32_t _hidl_code,\n";
out << "const ::android::hardware::Parcel &_hidl_data,\n";
out << "::android::hardware::Parcel *_hidl_reply,\n";
out << "uint32_t _hidl_flags = 0,\n";
out << "TransactCallback _hidl_cb = nullptr) override;\n\n";
out.unindent();
out.unindent();
out.endl();
generateTemplatizationLink(out);
DocComment("Type tag for use in template logic that indicates this is a 'native' class.",
HIDL_LOCATION_HERE)
.emit(out);
generateCppTag(out, "::android::hardware::details::bnhw_tag");
out << "::android::sp<" << iface->definedName() << "> getImpl() { return _hidl_mImpl; }\n";
// Because the Bn class hierarchy always inherits from BnHwBase (and no other parent classes)
// and also no HIDL-specific things exist in the base binder classes, whenever we want to do
// C++ HIDL things with a binder, we only have the choice to convert it into a BnHwBase.
// Other hwbinder C++ class hierarchies (namely the one used for Java binder) will still
// be libhwbinder binders, but they are not instances of BnHwBase.
if (isIBase()) {
out << "bool checkSubclass(const void* subclassID) const;\n";
}
generateMethods(out,
[&](const Method* method, const Interface*) {
if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) {
return;
}
out << "static ::android::status_t _hidl_" << method->name() << "(\n";
out.indent(2,
[&] {
out << "::android::hidl::base::V1_0::BnHwBase* _hidl_this,\n"
<< "const ::android::hardware::Parcel &_hidl_data,\n"
<< "::android::hardware::Parcel *_hidl_reply,\n"
<< "TransactCallback _hidl_cb);\n";
})
.endl()
.endl();
},
false /* include parents */);
out.unindent();
out << "private:\n";
out.indent();
generateMethods(out, [&](const Method* method, const Interface* iface) {
if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) {
return;
}
const bool returnsValue = !method->results().empty();
const NamedReference<Type>* elidedReturn = method->canElideCallback();
if (elidedReturn == nullptr && returnsValue) {
out << "using " << method->name() << "_cb = "
<< iface->fqName().cppName()
<< "::" << method->name() << "_cb;\n";
}
method->generateCppSignature(out);
out << ";\n";
});
out << "::android::sp<" << iface->definedName() << "> _hidl_mImpl;\n";
out.unindent();
out << "};\n\n";
enterLeaveNamespace(out, false /* enter */);
out << "\n#endif // " << guard << "\n";
}
void AST::generateProxyHeader(Formatter& out) const {
if (!AST::isInterface()) {
// types.hal does not get a proxy header.
return;
}
const Interface* iface = mRootScope.getInterface();
const std::string proxyName = iface->getProxyName();
const std::string guard = makeHeaderGuard(proxyName);
out << "#ifndef " << guard << "\n";
out << "#define " << guard << "\n\n";
out << "#include <hidl/HidlTransportSupport.h>\n\n";
generateCppPackageInclude(out, mPackage, iface->getHwName());
out << "\n";
enterLeaveNamespace(out, true /* enter */);
out << "\n";
out << "struct " << proxyName << " : public ::android::hardware::BpInterface<"
<< iface->definedName() << ">, public ::android::hardware::details::HidlInstrumentor {\n";
out.indent();
out << "explicit "
<< proxyName
<< "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl);"
<< "\n\n";
generateTemplatizationLink(out);
DocComment("Type tag for use in template logic that indicates this is a 'proxy' class.",
HIDL_LOCATION_HERE)
.emit(out);
generateCppTag(out, "::android::hardware::details::bphw_tag");
out << "virtual bool isRemote() const override { return true; }\n\n";
out << "void onLastStrongRef(const void* id) override;\n\n";
generateMethods(
out,
[&](const Method* method, const Interface*) {
if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) {
return;
}
out << "static ";
method->generateCppReturnType(out);
out << " _hidl_" << method->name() << "("
<< "::android::hardware::IInterface* _hidl_this, "
<< "::android::hardware::details::HidlInstrumentor *_hidl_this_instrumentor";
if (!method->hasEmptyCppArgSignature()) {
out << ", ";
}
method->emitCppArgSignature(out);
out << ");\n";
},
false /* include parents */);
generateMethods(out, [&](const Method* method, const Interface*) {
method->generateCppSignature(out);
out << " override;\n";
});
out.unindent();
out << "private:\n";
out.indent();
out << "std::mutex _hidl_mMutex;\n"
<< "std::vector<::android::sp<::android::hardware::hidl_binder_death_recipient>>"
<< " _hidl_mDeathRecipients;\n";
out.unindent();
out << "};\n\n";
enterLeaveNamespace(out, false /* enter */);
out << "\n#endif // " << guard << "\n";
}
void AST::generateCppSource(Formatter& out) const {
std::string baseName = getBaseName();
const Interface *iface = getInterface();
const std::string klassName = baseName + (baseName == "types" ? "" : "All");
out << "#define LOG_TAG \""
<< mPackage.string() << "::" << baseName
<< "\"\n\n";
out << "#include <log/log.h>\n";
out << "#include <cutils/trace.h>\n";
out << "#include <hidl/HidlTransportSupport.h>\n\n";
out << "#include <hidl/Static.h>\n";
out << "#include <hwbinder/ProcessState.h>\n";
out << "#include <utils/Trace.h>\n";
if (iface) {
// This is a no-op for IServiceManager itself.
out << "#include <android/hidl/manager/1.0/IServiceManager.h>\n";
generateCppPackageInclude(out, mPackage, iface->getProxyName());
generateCppPackageInclude(out, mPackage, iface->getStubName());
generateCppPackageInclude(out, mPackage, iface->getPassthroughName());
for (const Interface *superType : iface->superTypeChain()) {
generateCppPackageInclude(out,
superType->fqName(),
superType->fqName().getInterfaceProxyName());
}
out << "#include <hidl/ServiceManagement.h>\n";
} else {
generateCppPackageInclude(out, mPackage, "types");
generateCppPackageInclude(out, mPackage, "hwtypes");
}
out << "\n";
enterLeaveNamespace(out, true /* enter */);
out << "\n";
generateTypeSource(out, iface ? iface->definedName() : "");
if (iface) {
const Interface* iface = mRootScope.getInterface();
// need to be put here, generateStubSource is using this.
out << "const char* " << iface->definedName() << "::descriptor(\""
<< iface->fqName().string() << "\");\n\n";
out << "__attribute__((constructor)) ";
out << "static void static_constructor() {\n";
out.indent([&] {
out << "::android::hardware::details::getBnConstructorMap().set("
<< iface->definedName() << "::descriptor,\n";
out.indent(2, [&] {
out << "[](void *iIntf) -> ::android::sp<::android::hardware::IBinder> {\n";
out.indent([&] {
out << "return new " << iface->getStubName() << "(static_cast<"
<< iface->definedName() << " *>(iIntf));\n";
});
out << "});\n";
});
out << "::android::hardware::details::getBsConstructorMap().set("
<< iface->definedName() << "::descriptor,\n";
out.indent(2, [&] {
out << "[](void *iIntf) -> ::android::sp<"
<< gIBaseFqName.cppName()
<< "> {\n";
out.indent([&] {
out << "return new " << iface->getPassthroughName() << "(static_cast<"
<< iface->definedName() << " *>(iIntf));\n";
});
out << "});\n";
});
});
out << "}\n\n";
out << "__attribute__((destructor))";
out << "static void static_destructor() {\n";
out.indent([&] {
out << "::android::hardware::details::getBnConstructorMap().erase("
<< iface->definedName() << "::descriptor);\n";
out << "::android::hardware::details::getBsConstructorMap().erase("
<< iface->definedName() << "::descriptor);\n";
});
out << "}\n\n";
generateInterfaceSource(out);
generateProxySource(out, iface->fqName());
generateStubSource(out, iface);
generatePassthroughSource(out);
if (isIBase()) {
out << "// skipped getService, registerAsService, registerForNotifications\n";
} else {
std::string package = iface->fqName().package()
+ iface->fqName().atVersion();
implementServiceManagerInteractions(out, iface->fqName(), package);
}
}
HidlTypeAssertion::EmitAll(out);
out << "\n";
enterLeaveNamespace(out, false /* enter */);
}
void AST::generateTypeSource(Formatter& out, const std::string& ifaceName) const {
mRootScope.emitTypeDefinitions(out, ifaceName);
}
void AST::declareCppReaderLocals(Formatter& out, const std::vector<NamedReference<Type>*>& args,
bool forResults) const {
if (args.empty()) {
return;
}
for (const auto &arg : args) {
const Type &type = arg->type();
out << type.getCppResultType()
<< " "
<< (forResults ? "_hidl_out_" : "") + arg->name()
<< ";\n";
}
out << "\n";
}
void AST::emitCppReaderWriter(Formatter& out, const std::string& parcelObj, bool parcelObjIsPointer,
const NamedReference<Type>* arg, bool isReader, Type::ErrorMode mode,
bool addPrefixToName) const {
const Type &type = arg->type();
type.emitReaderWriter(
out,
addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(),
parcelObj,
parcelObjIsPointer,
isReader,
mode);
}
void AST::generateProxyMethodSource(Formatter& out, const std::string& klassName,
const Method* method, const Interface* superInterface) const {
method->generateCppSignature(out,
klassName,
true /* specify namespaces */);
if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) {
out.block([&] {
method->cppImpl(IMPL_PROXY, out);
}).endl().endl();
return;
}
out.block([&] {
const bool returnsValue = !method->results().empty();
const NamedReference<Type>* elidedReturn = method->canElideCallback();
method->generateCppReturnType(out);
out << " _hidl_out = "
<< superInterface->fqName().cppNamespace()
<< "::"
<< superInterface->getProxyName()
<< "::_hidl_"
<< method->name()
<< "(this, this";