blob: bfd8950770db4959dce068a42cef8d85e21c9918 [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"
23#include "ScalarType.h"
24#include "Scope.h"
25
26#include <algorithm>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070027#include <hidl-util/Formatter.h>
Steven Moreland9c387612016-09-07 09:54:26 -070028#include <android-base/logging.h>
29#include <string>
30#include <vector>
31#include <set>
32
Steven Moreland9c387612016-09-07 09:54:26 -070033namespace android {
34
Steven Moreland9c387612016-09-07 09:54:26 -070035status_t AST::generateCppImpl(const std::string &outputPath) const {
36 status_t err = generateStubImplHeader(outputPath);
37
38 if (err == OK) {
39 err = generateStubImplSource(outputPath);
40 }
41
42 return err;
43}
44
45void AST::generateFetchSymbol(Formatter &out, const std::string& ifaceName) const {
46 out << "HIDL_FETCH_" << ifaceName;
47}
48
49status_t AST::generateStubImplMethod(Formatter &out,
50 const std::string &className,
Yifan Hong068c5522016-10-31 14:07:25 -070051 const Method *method) const {
Steven Moreland9c387612016-09-07 09:54:26 -070052
Yifan Hong1254b552016-10-27 15:03:29 -070053 // ignore HIDL reserved methods -- implemented in IFoo already.
54 if (method->isHidlReserved()) {
55 return OK;
56 }
57
Yifan Hong068c5522016-10-31 14:07:25 -070058 method->generateCppSignature(out, className, false /* specifyNamespaces */);
Steven Moreland9c387612016-09-07 09:54:26 -070059
60 out << " {\n";
61
62 out.indent();
63 out << "// TODO implement\n";
64
65 const TypedVar *elidedReturn = method->canElideCallback();
66
67 if (elidedReturn == nullptr) {
68 out << "return Void();\n";
69 } else {
Steven Moreland9c387612016-09-07 09:54:26 -070070 out << "return "
Yifan Hong3b320f82016-11-01 15:15:54 -070071 << elidedReturn->type().getCppResultType()
Steven Moreland9c387612016-09-07 09:54:26 -070072 << " {};\n";
73 }
74
75 out.unindent();
76
77 out << "}\n\n";
78
79 return OK;
80}
81
Steven Moreland9c387612016-09-07 09:54:26 -070082status_t AST::generateStubImplHeader(const std::string &outputPath) const {
83 std::string ifaceName;
84 if (!AST::isInterface(&ifaceName)) {
85 // types.hal does not get a stub header.
86 return OK;
87 }
88
89 const Interface *iface = mRootScope->getInterface();
90
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -070091 const std::string baseName = iface->getBaseName();
Steven Moreland9c387612016-09-07 09:54:26 -070092
93 std::string path = outputPath;
94 path.append(baseName);
95 path.append(".h");
96
97 CHECK(Coordinator::MakeParentHierarchy(path));
98 FILE *file = fopen(path.c_str(), "w");
99
100 if (file == NULL) {
101 return -errno;
102 }
103
104 Formatter out(file);
105
Steven Moreland5708edf2016-11-04 15:33:31 +0000106 const std::string guard = makeHeaderGuard(baseName, false /* indicateGenerated */);
Steven Moreland9c387612016-09-07 09:54:26 -0700107
108 out << "#ifndef " << guard << "\n";
109 out << "#define " << guard << "\n\n";
110
Steven Morelandee88eed2016-10-31 17:49:00 -0700111 generateCppPackageInclude(out, mPackage, "I" + baseName);
Steven Moreland9c387612016-09-07 09:54:26 -0700112
Andreas Huber4bcf97d2016-08-30 11:27:49 -0700113 out << "#include <hidl/MQDescriptor.h>\n";
Steven Moreland41c6d2e2016-11-07 12:26:54 -0800114 out << "#include <hidl/Status.h>\n\n";
Steven Moreland9c387612016-09-07 09:54:26 -0700115
116 enterLeaveNamespace(out, true /* enter */);
117 out << "namespace implementation {\n\n";
118
119 // this is namespace aware code and doesn't require post-processing
120 out.setNamespace("");
121
Yifan Hong10fe0b52016-10-19 14:20:17 -0700122 std::vector<const Interface *> chain = iface->typeChain();
Steven Moreland9c387612016-09-07 09:54:26 -0700123
124 std::set<const FQName> usedTypes{};
125
126 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
127 const Interface *superInterface = *it;
Steven Moreland9c387612016-09-07 09:54:26 -0700128 superInterface->addNamedTypesToSet(usedTypes);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700129 }
Steven Moreland9c387612016-09-07 09:54:26 -0700130
Yifan Hong10fe0b52016-10-19 14:20:17 -0700131 for (const auto &tuple : iface->allMethodsFromRoot()) {
132 const Method *method = tuple.method();
133 for(const auto & arg : method->args()) {
134 arg->type().addNamedTypesToSet(usedTypes);
135 }
136 for(const auto & results : method->results()) {
137 results->type().addNamedTypesToSet(usedTypes);
Steven Moreland9c387612016-09-07 09:54:26 -0700138 }
139 }
140
141 std::set<const FQName> topLevelTypes{};
142
143 for (const auto &name : usedTypes) {
144 topLevelTypes.insert(name.getTopLevelType());
145 }
146
147 for (const FQName &name : topLevelTypes) {
148 out << "using " << name.cppName() << ";\n";
149 }
150
151 out << "using ::android::hardware::Return;\n";
152 out << "using ::android::hardware::Void;\n";
153 out << "using ::android::hardware::hidl_vec;\n";
154 out << "using ::android::hardware::hidl_string;\n";
155 out << "using ::android::sp;\n";
156
157 out << "\n";
158
159 out << "struct "
160 << baseName
161 << " : public "
162 << ifaceName
163 << " {\n";
164
165 out.indent();
166
Yifan Hong068c5522016-10-31 14:07:25 -0700167 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
168 // ignore HIDL reserved methods -- implemented in IFoo already.
169 if (method->isHidlReserved()) {
170 return OK;
171 }
172 method->generateCppSignature(out, "" /* className */,
173 false /* specifyNamespaces */);
174 out << " override;\n";
175 return OK;
176 });
Steven Moreland9c387612016-09-07 09:54:26 -0700177
178 if (err != OK) {
179 return err;
180 }
181
182 out.unindent();
183
184 out << "};\n\n";
185
186 out << "extern \"C\" "
187 << ifaceName
188 << "* ";
189 generateFetchSymbol(out, ifaceName);
190 out << "(const char* name);\n\n";
191
192 out << "} // namespace implementation\n";
193 enterLeaveNamespace(out, false /* leave */);
194
195 out << "\n#endif // " << guard << "\n";
196
197 return OK;
198}
199
200status_t AST::generateStubImplSource(const std::string &outputPath) const {
201 std::string ifaceName;
202 if (!AST::isInterface(&ifaceName)) {
203 // types.hal does not get a stub header.
204 return OK;
205 }
206
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700207 const Interface *iface = mRootScope->getInterface();
208 const std::string baseName = iface->getBaseName();
Steven Moreland9c387612016-09-07 09:54:26 -0700209
210 std::string path = outputPath;
211 path.append(baseName);
212 path.append(".cpp");
213
214 CHECK(Coordinator::MakeParentHierarchy(path));
215 FILE *file = fopen(path.c_str(), "w");
216
217 if (file == NULL) {
218 return -errno;
219 }
220
221 Formatter out(file);
222
223 out << "#include \"" << baseName << ".h\"\n\n";
224
225 enterLeaveNamespace(out, true /* enter */);
226 out << "namespace implementation {\n\n";
227
228 // this is namespace aware code and doesn't require post-processing
229 out.setNamespace("");
230
Yifan Hong068c5522016-10-31 14:07:25 -0700231 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
232 return generateStubImplMethod(out, baseName, method);
233 });
Steven Moreland9c387612016-09-07 09:54:26 -0700234
235 out << ifaceName
236 << "* ";
237 generateFetchSymbol(out, ifaceName);
238 out << "(const char* /* name */) {\n";
239 out.indent();
240 out << "return new " << baseName << "();\n";
241 out.unindent();
242 out << "}\n\n";
243
244 out << "} // namespace implementation\n";
245 enterLeaveNamespace(out, false /* leave */);
246
247 return OK;
248}
249
250} // namespace android