blob: 66a35d4142c555fc92cdd681c374a970639c49e6 [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
140 const Interface *ancestor = iface;
141 std::vector<const Interface *> chain;
142 while (ancestor != NULL) {
143 chain.push_back(ancestor);
144 ancestor = ancestor->superType();
145 }
146
147 std::set<const FQName> usedTypes{};
148
149 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
150 const Interface *superInterface = *it;
151
152 superInterface->addNamedTypesToSet(usedTypes);
153
154 for (const auto &method : superInterface->methods()) {
155 for(const auto & arg : method->args()) {
156 arg->type().addNamedTypesToSet(usedTypes);
157 }
158 for(const auto & results : method->results()) {
159 results->type().addNamedTypesToSet(usedTypes);
160 }
161 }
162 }
163
164 std::set<const FQName> topLevelTypes{};
165
166 for (const auto &name : usedTypes) {
167 topLevelTypes.insert(name.getTopLevelType());
168 }
169
170 for (const FQName &name : topLevelTypes) {
171 out << "using " << name.cppName() << ";\n";
172 }
173
174 out << "using ::android::hardware::Return;\n";
175 out << "using ::android::hardware::Void;\n";
176 out << "using ::android::hardware::hidl_vec;\n";
177 out << "using ::android::hardware::hidl_string;\n";
178 out << "using ::android::sp;\n";
179
180 out << "\n";
181
182 out << "struct "
183 << baseName
184 << " : public "
185 << ifaceName
186 << " {\n";
187
188 out.indent();
189
190 status_t err = generateMethods(out,
191 "", /* class name */
192 MethodLocation::IMPL_HEADER,
193 false /* specify namespaces */);
194
195 if (err != OK) {
196 return err;
197 }
198
199 out.unindent();
200
201 out << "};\n\n";
202
203 out << "extern \"C\" "
204 << ifaceName
205 << "* ";
206 generateFetchSymbol(out, ifaceName);
207 out << "(const char* name);\n\n";
208
209 out << "} // namespace implementation\n";
210 enterLeaveNamespace(out, false /* leave */);
211
212 out << "\n#endif // " << guard << "\n";
213
214 return OK;
215}
216
217status_t AST::generateStubImplSource(const std::string &outputPath) const {
218 std::string ifaceName;
219 if (!AST::isInterface(&ifaceName)) {
220 // types.hal does not get a stub header.
221 return OK;
222 }
223
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700224 const Interface *iface = mRootScope->getInterface();
225 const std::string baseName = iface->getBaseName();
Steven Moreland9c387612016-09-07 09:54:26 -0700226
227 std::string path = outputPath;
228 path.append(baseName);
229 path.append(".cpp");
230
231 CHECK(Coordinator::MakeParentHierarchy(path));
232 FILE *file = fopen(path.c_str(), "w");
233
234 if (file == NULL) {
235 return -errno;
236 }
237
238 Formatter out(file);
239
240 out << "#include \"" << baseName << ".h\"\n\n";
241
242 enterLeaveNamespace(out, true /* enter */);
243 out << "namespace implementation {\n\n";
244
245 // this is namespace aware code and doesn't require post-processing
246 out.setNamespace("");
247
248 generateMethods(out,
249 baseName,
250 MethodLocation::IMPL_SOURCE,
251 false /* specify namespaces */);
252
Steven Moreland9c387612016-09-07 09:54:26 -0700253
254 out << ifaceName
255 << "* ";
256 generateFetchSymbol(out, ifaceName);
257 out << "(const char* /* name */) {\n";
258 out.indent();
259 out << "return new " << baseName << "();\n";
260 out.unindent();
261 out << "}\n\n";
262
263 out << "} // namespace implementation\n";
264 enterLeaveNamespace(out, false /* leave */);
265
266 return OK;
267}
268
269} // namespace android