blob: b8b91c47922e670bf402217b359eb7cff2a11367 [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,
51 const Method *method,
52 bool specifyNamespaces) const {
53
54 method->generateCppSignature(out, className, specifyNamespaces);
55
56 out << " {\n";
57
58 out.indent();
59 out << "// TODO implement\n";
60
61 const TypedVar *elidedReturn = method->canElideCallback();
62
63 if (elidedReturn == nullptr) {
64 out << "return Void();\n";
65 } else {
66 std::string extra;
67 out << "return "
68 << elidedReturn->type().getCppResultType(&extra)
69 << " {};\n";
70 }
71
72 out.unindent();
73
74 out << "}\n\n";
75
76 return OK;
77}
78
79status_t AST::generateStubImplDeclaration(Formatter &out,
80 const std::string &className,
81 const Method *method,
82 bool specifyNamespaces) const {
83
84 method->generateCppSignature(out,
85 className,
86 specifyNamespaces);
87 out << " override;\n";
88
89 return OK;
90}
91
92status_t AST::generateStubImplHeader(const std::string &outputPath) const {
93 std::string ifaceName;
94 if (!AST::isInterface(&ifaceName)) {
95 // types.hal does not get a stub header.
96 return OK;
97 }
98
99 const Interface *iface = mRootScope->getInterface();
100
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700101 const std::string baseName = iface->getBaseName();
Steven Moreland9c387612016-09-07 09:54:26 -0700102
103 std::string path = outputPath;
104 path.append(baseName);
105 path.append(".h");
106
107 CHECK(Coordinator::MakeParentHierarchy(path));
108 FILE *file = fopen(path.c_str(), "w");
109
110 if (file == NULL) {
111 return -errno;
112 }
113
114 Formatter out(file);
115
116 const std::string guard = makeHeaderGuard(baseName);
117
118 out << "#ifndef " << guard << "\n";
119 out << "#define " << guard << "\n\n";
120
121 std::vector<std::string> packageComponents;
122 getPackageAndVersionComponents(
123 &packageComponents, false /* cpp_compatible */);
124
125 out << "#include <";
126 for (const auto &component : packageComponents) {
127 out << component << "/";
128 }
129 out << "I" << baseName << ".h>\n";
130
131 out << "#include <hidl/Status.h>\n\n";
Andreas Huber4bcf97d2016-08-30 11:27:49 -0700132 out << "#include <hidl/MQDescriptor.h>\n";
Steven Moreland9c387612016-09-07 09:54:26 -0700133
134 enterLeaveNamespace(out, true /* enter */);
135 out << "namespace implementation {\n\n";
136
137 // this is namespace aware code and doesn't require post-processing
138 out.setNamespace("");
139
Yifan Hong10fe0b52016-10-19 14:20:17 -0700140 std::vector<const Interface *> chain = iface->typeChain();
Steven Moreland9c387612016-09-07 09:54:26 -0700141
142 std::set<const FQName> usedTypes{};
143
144 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
145 const Interface *superInterface = *it;
Steven Moreland9c387612016-09-07 09:54:26 -0700146 superInterface->addNamedTypesToSet(usedTypes);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700147 }
Steven Moreland9c387612016-09-07 09:54:26 -0700148
Yifan Hong10fe0b52016-10-19 14:20:17 -0700149 for (const auto &tuple : iface->allMethodsFromRoot()) {
150 const Method *method = tuple.method();
151 for(const auto & arg : method->args()) {
152 arg->type().addNamedTypesToSet(usedTypes);
153 }
154 for(const auto & results : method->results()) {
155 results->type().addNamedTypesToSet(usedTypes);
Steven Moreland9c387612016-09-07 09:54:26 -0700156 }
157 }
158
159 std::set<const FQName> topLevelTypes{};
160
161 for (const auto &name : usedTypes) {
162 topLevelTypes.insert(name.getTopLevelType());
163 }
164
165 for (const FQName &name : topLevelTypes) {
166 out << "using " << name.cppName() << ";\n";
167 }
168
169 out << "using ::android::hardware::Return;\n";
170 out << "using ::android::hardware::Void;\n";
171 out << "using ::android::hardware::hidl_vec;\n";
172 out << "using ::android::hardware::hidl_string;\n";
173 out << "using ::android::sp;\n";
174
175 out << "\n";
176
177 out << "struct "
178 << baseName
179 << " : public "
180 << ifaceName
181 << " {\n";
182
183 out.indent();
184
185 status_t err = generateMethods(out,
186 "", /* class name */
187 MethodLocation::IMPL_HEADER,
188 false /* specify namespaces */);
189
190 if (err != OK) {
191 return err;
192 }
193
194 out.unindent();
195
196 out << "};\n\n";
197
198 out << "extern \"C\" "
199 << ifaceName
200 << "* ";
201 generateFetchSymbol(out, ifaceName);
202 out << "(const char* name);\n\n";
203
204 out << "} // namespace implementation\n";
205 enterLeaveNamespace(out, false /* leave */);
206
207 out << "\n#endif // " << guard << "\n";
208
209 return OK;
210}
211
212status_t AST::generateStubImplSource(const std::string &outputPath) const {
213 std::string ifaceName;
214 if (!AST::isInterface(&ifaceName)) {
215 // types.hal does not get a stub header.
216 return OK;
217 }
218
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700219 const Interface *iface = mRootScope->getInterface();
220 const std::string baseName = iface->getBaseName();
Steven Moreland9c387612016-09-07 09:54:26 -0700221
222 std::string path = outputPath;
223 path.append(baseName);
224 path.append(".cpp");
225
226 CHECK(Coordinator::MakeParentHierarchy(path));
227 FILE *file = fopen(path.c_str(), "w");
228
229 if (file == NULL) {
230 return -errno;
231 }
232
233 Formatter out(file);
234
235 out << "#include \"" << baseName << ".h\"\n\n";
236
237 enterLeaveNamespace(out, true /* enter */);
238 out << "namespace implementation {\n\n";
239
240 // this is namespace aware code and doesn't require post-processing
241 out.setNamespace("");
242
243 generateMethods(out,
244 baseName,
245 MethodLocation::IMPL_SOURCE,
246 false /* specify namespaces */);
247
Steven Moreland9c387612016-09-07 09:54:26 -0700248
249 out << ifaceName
250 << "* ";
251 generateFetchSymbol(out, ifaceName);
252 out << "(const char* /* name */) {\n";
253 out.indent();
254 out << "return new " << baseName << "();\n";
255 out.unindent();
256 out << "}\n\n";
257
258 out << "} // namespace implementation\n";
259 enterLeaveNamespace(out, false /* leave */);
260
261 return OK;
262}
263
264} // namespace android