blob: 18fac6b42377145338cb452f931a8c85e1db8aa8 [file] [log] [blame]
Steven Moreland9c387612016-09-07 09:54:26 -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
17#include "AST.h"
18
19#include "Coordinator.h"
20#include "EnumType.h"
Steven Moreland9c387612016-09-07 09:54:26 -070021#include "Interface.h"
22#include "Method.h"
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070023#include "Reference.h"
Steven Moreland9c387612016-09-07 09:54:26 -070024#include "ScalarType.h"
25#include "Scope.h"
26
27#include <algorithm>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070028#include <hidl-util/Formatter.h>
Steven Moreland9c387612016-09-07 09:54:26 -070029#include <android-base/logging.h>
30#include <string>
31#include <vector>
32#include <set>
33
Steven Moreland9c387612016-09-07 09:54:26 -070034namespace android {
35
Steven Moreland9c387612016-09-07 09:54:26 -070036void AST::generateFetchSymbol(Formatter &out, const std::string& ifaceName) const {
37 out << "HIDL_FETCH_" << ifaceName;
38}
39
Steven Moreland6ec9eb92018-02-16 14:21:49 -080040void AST::generateStubImplMethod(Formatter& out, const std::string& className,
41 const Method* method) const {
Yifan Hong1254b552016-10-27 15:03:29 -070042 // ignore HIDL reserved methods -- implemented in IFoo already.
43 if (method->isHidlReserved()) {
Steven Moreland6ec9eb92018-02-16 14:21:49 -080044 return;
Yifan Hong1254b552016-10-27 15:03:29 -070045 }
46
Yifan Hong068c5522016-10-31 14:07:25 -070047 method->generateCppSignature(out, className, false /* specifyNamespaces */);
Steven Moreland9c387612016-09-07 09:54:26 -070048
49 out << " {\n";
50
51 out.indent();
52 out << "// TODO implement\n";
53
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070054 const NamedReference<Type>* elidedReturn = method->canElideCallback();
Steven Moreland9c387612016-09-07 09:54:26 -070055
56 if (elidedReturn == nullptr) {
57 out << "return Void();\n";
58 } else {
Steven Moreland9c387612016-09-07 09:54:26 -070059 out << "return "
Yifan Hong3b320f82016-11-01 15:15:54 -070060 << elidedReturn->type().getCppResultType()
Steven Moreland9c387612016-09-07 09:54:26 -070061 << " {};\n";
62 }
63
64 out.unindent();
65
66 out << "}\n\n";
67
Steven Moreland6ec9eb92018-02-16 14:21:49 -080068 return;
Steven Moreland9c387612016-09-07 09:54:26 -070069}
70
Steven Moreland6ec9eb92018-02-16 14:21:49 -080071void AST::generateCppImplHeader(Formatter& out) const {
Steven Moreland19f11b52017-05-12 18:22:21 -070072 if (!AST::isInterface()) {
Steven Moreland9c387612016-09-07 09:54:26 -070073 // types.hal does not get a stub header.
Steven Moreland6ec9eb92018-02-16 14:21:49 -080074 return;
Steven Moreland9c387612016-09-07 09:54:26 -070075 }
76
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070077 const Interface* iface = mRootScope.getInterface();
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -070078 const std::string baseName = iface->getBaseName();
Steven Moreland9c387612016-09-07 09:54:26 -070079
Steven Moreland5708edf2016-11-04 15:33:31 +000080 const std::string guard = makeHeaderGuard(baseName, false /* indicateGenerated */);
Steven Moreland9c387612016-09-07 09:54:26 -070081
82 out << "#ifndef " << guard << "\n";
83 out << "#define " << guard << "\n\n";
84
Yifan Hongeefe4f22017-01-04 15:32:42 -080085 generateCppPackageInclude(out, mPackage, iface->localName());
Steven Moreland9c387612016-09-07 09:54:26 -070086
Andreas Huber4bcf97d2016-08-30 11:27:49 -070087 out << "#include <hidl/MQDescriptor.h>\n";
Steven Moreland41c6d2e2016-11-07 12:26:54 -080088 out << "#include <hidl/Status.h>\n\n";
Steven Moreland9c387612016-09-07 09:54:26 -070089
90 enterLeaveNamespace(out, true /* enter */);
91 out << "namespace implementation {\n\n";
92
Steven Morelande429a262016-11-15 09:54:32 -080093 out << "using ::android::hardware::hidl_array;\n";
Martijn Coenen99e6beb2016-12-01 15:48:42 +010094 out << "using ::android::hardware::hidl_memory;\n";
Steven Morelande429a262016-11-15 09:54:32 -080095 out << "using ::android::hardware::hidl_string;\n";
96 out << "using ::android::hardware::hidl_vec;\n";
Steven Moreland9c387612016-09-07 09:54:26 -070097 out << "using ::android::hardware::Return;\n";
98 out << "using ::android::hardware::Void;\n";
Steven Moreland9c387612016-09-07 09:54:26 -070099 out << "using ::android::sp;\n";
100
101 out << "\n";
102
103 out << "struct "
104 << baseName
105 << " : public "
Steven Moreland19f11b52017-05-12 18:22:21 -0700106 << iface->localName()
Steven Moreland9c387612016-09-07 09:54:26 -0700107 << " {\n";
108
109 out.indent();
110
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800111 generateMethods(out, [&](const Method* method, const Interface*) {
Yifan Hong068c5522016-10-31 14:07:25 -0700112 // ignore HIDL reserved methods -- implemented in IFoo already.
113 if (method->isHidlReserved()) {
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800114 return;
Yifan Hong068c5522016-10-31 14:07:25 -0700115 }
116 method->generateCppSignature(out, "" /* className */,
117 false /* specifyNamespaces */);
118 out << " override;\n";
Yifan Hong068c5522016-10-31 14:07:25 -0700119 });
Steven Moreland9c387612016-09-07 09:54:26 -0700120
Steven Moreland9c387612016-09-07 09:54:26 -0700121 out.unindent();
122
123 out << "};\n\n";
124
Steven Moreland3918fcc2017-04-21 10:54:27 -0700125 out << "// FIXME: most likely delete, this is only for passthrough implementations\n"
126 << "// extern \"C\" "
Steven Moreland19f11b52017-05-12 18:22:21 -0700127 << iface->localName()
Steven Moreland9c387612016-09-07 09:54:26 -0700128 << "* ";
Steven Moreland19f11b52017-05-12 18:22:21 -0700129 generateFetchSymbol(out, iface->localName());
Steven Moreland9c387612016-09-07 09:54:26 -0700130 out << "(const char* name);\n\n";
131
132 out << "} // namespace implementation\n";
133 enterLeaveNamespace(out, false /* leave */);
134
135 out << "\n#endif // " << guard << "\n";
Steven Moreland9c387612016-09-07 09:54:26 -0700136}
137
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800138void AST::generateCppImplSource(Formatter& out) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700139 if (!AST::isInterface()) {
Steven Moreland9c387612016-09-07 09:54:26 -0700140 // types.hal does not get a stub header.
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800141 return;
Steven Moreland9c387612016-09-07 09:54:26 -0700142 }
143
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700144 const Interface* iface = mRootScope.getInterface();
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700145 const std::string baseName = iface->getBaseName();
Steven Moreland9c387612016-09-07 09:54:26 -0700146
Steven Moreland9c387612016-09-07 09:54:26 -0700147 out << "#include \"" << baseName << ".h\"\n\n";
148
149 enterLeaveNamespace(out, true /* enter */);
150 out << "namespace implementation {\n\n";
151
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800152 generateMethods(out, [&](const Method* method, const Interface*) {
153 generateStubImplMethod(out, baseName, method);
Yifan Hong068c5522016-10-31 14:07:25 -0700154 });
Steven Moreland9c387612016-09-07 09:54:26 -0700155
Steven Moreland3918fcc2017-04-21 10:54:27 -0700156 out.setLinePrefix("//");
Steven Moreland19f11b52017-05-12 18:22:21 -0700157 out << iface->localName()
Steven Moreland9c387612016-09-07 09:54:26 -0700158 << "* ";
Steven Moreland19f11b52017-05-12 18:22:21 -0700159 generateFetchSymbol(out, iface->localName());
Steven Moreland9c387612016-09-07 09:54:26 -0700160 out << "(const char* /* name */) {\n";
161 out.indent();
162 out << "return new " << baseName << "();\n";
163 out.unindent();
164 out << "}\n\n";
Steven Moreland3918fcc2017-04-21 10:54:27 -0700165 out.unsetLinePrefix();
Steven Moreland9c387612016-09-07 09:54:26 -0700166
Yifan Hongdfc4b9c2016-12-02 16:31:31 -0800167 out << "} // namespace implementation\n";
Steven Moreland9c387612016-09-07 09:54:26 -0700168 enterLeaveNamespace(out, false /* leave */);
Steven Moreland9c387612016-09-07 09:54:26 -0700169}
170
171} // namespace android