blob: bed1d1d176020757e39e83d54f6e194bd7e82c28 [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
101 // cut off the leading 'I'.
102 const std::string baseName = ifaceName.substr(1);
103
104 std::string path = outputPath;
105 path.append(baseName);
106 path.append(".h");
107
108 CHECK(Coordinator::MakeParentHierarchy(path));
109 FILE *file = fopen(path.c_str(), "w");
110
111 if (file == NULL) {
112 return -errno;
113 }
114
115 Formatter out(file);
116
117 const std::string guard = makeHeaderGuard(baseName);
118
119 out << "#ifndef " << guard << "\n";
120 out << "#define " << guard << "\n\n";
121
122 std::vector<std::string> packageComponents;
123 getPackageAndVersionComponents(
124 &packageComponents, false /* cpp_compatible */);
125
126 out << "#include <";
127 for (const auto &component : packageComponents) {
128 out << component << "/";
129 }
130 out << "I" << baseName << ".h>\n";
131
132 out << "#include <hidl/Status.h>\n\n";
Andreas Huber4bcf97d2016-08-30 11:27:49 -0700133 out << "#include <hidl/MQDescriptor.h>\n";
Steven Moreland9c387612016-09-07 09:54:26 -0700134
135 enterLeaveNamespace(out, true /* enter */);
136 out << "namespace implementation {\n\n";
137
138 // this is namespace aware code and doesn't require post-processing
139 out.setNamespace("");
140
141 const Interface *ancestor = iface;
142 std::vector<const Interface *> chain;
143 while (ancestor != NULL) {
144 chain.push_back(ancestor);
145 ancestor = ancestor->superType();
146 }
147
148 std::set<const FQName> usedTypes{};
149
150 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
151 const Interface *superInterface = *it;
152
153 superInterface->addNamedTypesToSet(usedTypes);
154
155 for (const auto &method : superInterface->methods()) {
156 for(const auto & arg : method->args()) {
157 arg->type().addNamedTypesToSet(usedTypes);
158 }
159 for(const auto & results : method->results()) {
160 results->type().addNamedTypesToSet(usedTypes);
161 }
162 }
163 }
164
165 std::set<const FQName> topLevelTypes{};
166
167 for (const auto &name : usedTypes) {
168 topLevelTypes.insert(name.getTopLevelType());
169 }
170
171 for (const FQName &name : topLevelTypes) {
172 out << "using " << name.cppName() << ";\n";
173 }
174
175 out << "using ::android::hardware::Return;\n";
176 out << "using ::android::hardware::Void;\n";
177 out << "using ::android::hardware::hidl_vec;\n";
178 out << "using ::android::hardware::hidl_string;\n";
179 out << "using ::android::sp;\n";
180
181 out << "\n";
182
183 out << "struct "
184 << baseName
185 << " : public "
186 << ifaceName
187 << " {\n";
188
189 out.indent();
190
191 status_t err = generateMethods(out,
192 "", /* class name */
193 MethodLocation::IMPL_HEADER,
194 false /* specify namespaces */);
195
196 if (err != OK) {
197 return err;
198 }
199
200 out.unindent();
201
202 out << "};\n\n";
203
204 out << "extern \"C\" "
205 << ifaceName
206 << "* ";
207 generateFetchSymbol(out, ifaceName);
208 out << "(const char* name);\n\n";
209
210 out << "} // namespace implementation\n";
211 enterLeaveNamespace(out, false /* leave */);
212
213 out << "\n#endif // " << guard << "\n";
214
215 return OK;
216}
217
218status_t AST::generateStubImplSource(const std::string &outputPath) const {
219 std::string ifaceName;
220 if (!AST::isInterface(&ifaceName)) {
221 // types.hal does not get a stub header.
222 return OK;
223 }
224
225 // cut off the leading 'I'.
226 const std::string baseName = ifaceName.substr(1);
227
228 std::string path = outputPath;
229 path.append(baseName);
230 path.append(".cpp");
231
232 CHECK(Coordinator::MakeParentHierarchy(path));
233 FILE *file = fopen(path.c_str(), "w");
234
235 if (file == NULL) {
236 return -errno;
237 }
238
239 Formatter out(file);
240
241 out << "#include \"" << baseName << ".h\"\n\n";
242
243 enterLeaveNamespace(out, true /* enter */);
244 out << "namespace implementation {\n\n";
245
246 // this is namespace aware code and doesn't require post-processing
247 out.setNamespace("");
248
249 generateMethods(out,
250 baseName,
251 MethodLocation::IMPL_SOURCE,
252 false /* specify namespaces */);
253
254 const Interface *iface = mRootScope->getInterface();
255
256 out << ifaceName
257 << "* ";
258 generateFetchSymbol(out, ifaceName);
259 out << "(const char* /* name */) {\n";
260 out.indent();
261 out << "return new " << baseName << "();\n";
262 out.unindent();
263 out << "}\n\n";
264
265 out << "} // namespace implementation\n";
266 enterLeaveNamespace(out, false /* leave */);
267
268 return OK;
269}
270
271} // namespace android