1 /*
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"
21 #include "Interface.h"
22 #include "Method.h"
23 #include "ScalarType.h"
24 #include "Scope.h"
25
26 #include <algorithm>
27 #include <hidl-util/Formatter.h>
28 #include <android-base/logging.h>
29 #include <string>
30 #include <vector>
31 #include <set>
32
33 namespace android {
34
generateCppImpl(const std::string & outputPath) const35 status_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
generateFetchSymbol(Formatter & out,const std::string & ifaceName) const45 void AST::generateFetchSymbol(Formatter &out, const std::string& ifaceName) const {
46 out << "HIDL_FETCH_" << ifaceName;
47 }
48
generateStubImplMethod(Formatter & out,const std::string & className,const Method * method) const49 status_t AST::generateStubImplMethod(Formatter &out,
50 const std::string &className,
51 const Method *method) const {
52
53 // ignore HIDL reserved methods -- implemented in IFoo already.
54 if (method->isHidlReserved()) {
55 return OK;
56 }
57
58 method->generateCppSignature(out, className, false /* specifyNamespaces */);
59
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 {
70 out << "return "
71 << elidedReturn->type().getCppResultType()
72 << " {};\n";
73 }
74
75 out.unindent();
76
77 out << "}\n\n";
78
79 return OK;
80 }
81
generateStubImplHeader(const std::string & outputPath) const82 status_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
91 const std::string baseName = iface->getBaseName();
92
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
106 const std::string guard = makeHeaderGuard(baseName, false /* indicateGenerated */);
107
108 out << "#ifndef " << guard << "\n";
109 out << "#define " << guard << "\n\n";
110
111 generateCppPackageInclude(out, mPackage, iface->localName());
112
113 out << "#include <hidl/MQDescriptor.h>\n";
114 out << "#include <hidl/Status.h>\n\n";
115
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
122 std::vector<const Interface *> chain = iface->typeChain();
123
124 std::set<const FQName> usedTypes{};
125
126 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
127 const Interface *superInterface = *it;
128 superInterface->addNamedTypesToSet(usedTypes);
129 }
130
131 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);
138 }
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::hidl_array;\n";
152 out << "using ::android::hardware::hidl_memory;\n";
153 out << "using ::android::hardware::hidl_string;\n";
154 out << "using ::android::hardware::hidl_vec;\n";
155 out << "using ::android::hardware::Return;\n";
156 out << "using ::android::hardware::Void;\n";
157 out << "using ::android::sp;\n";
158
159 out << "\n";
160
161 out << "struct "
162 << baseName
163 << " : public "
164 << ifaceName
165 << " {\n";
166
167 out.indent();
168
169 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
170 // ignore HIDL reserved methods -- implemented in IFoo already.
171 if (method->isHidlReserved()) {
172 return OK;
173 }
174 method->generateCppSignature(out, "" /* className */,
175 false /* specifyNamespaces */);
176 out << " override;\n";
177 return OK;
178 });
179
180 if (err != OK) {
181 return err;
182 }
183
184 out.unindent();
185
186 out << "};\n\n";
187
188 out << "extern \"C\" "
189 << ifaceName
190 << "* ";
191 generateFetchSymbol(out, ifaceName);
192 out << "(const char* name);\n\n";
193
194 out << "} // namespace implementation\n";
195 enterLeaveNamespace(out, false /* leave */);
196
197 out << "\n#endif // " << guard << "\n";
198
199 return OK;
200 }
201
generateStubImplSource(const std::string & outputPath) const202 status_t AST::generateStubImplSource(const std::string &outputPath) const {
203 std::string ifaceName;
204 if (!AST::isInterface(&ifaceName)) {
205 // types.hal does not get a stub header.
206 return OK;
207 }
208
209 const Interface *iface = mRootScope->getInterface();
210 const std::string baseName = iface->getBaseName();
211
212 std::string path = outputPath;
213 path.append(baseName);
214 path.append(".cpp");
215
216 CHECK(Coordinator::MakeParentHierarchy(path));
217 FILE *file = fopen(path.c_str(), "w");
218
219 if (file == NULL) {
220 return -errno;
221 }
222
223 Formatter out(file);
224
225 out << "#include \"" << baseName << ".h\"\n\n";
226
227 enterLeaveNamespace(out, true /* enter */);
228 out << "namespace implementation {\n\n";
229
230 // this is namespace aware code and doesn't require post-processing
231 out.setNamespace("");
232
233 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
234 return generateStubImplMethod(out, baseName, method);
235 });
236
237 if (err != OK) {
238 return err;
239 }
240
241 out << ifaceName
242 << "* ";
243 generateFetchSymbol(out, ifaceName);
244 out << "(const char* /* name */) {\n";
245 out.indent();
246 out << "return new " << baseName << "();\n";
247 out.unindent();
248 out << "}\n\n";
249
250 out << "} // namespace implementation\n";
251 enterLeaveNamespace(out, false /* leave */);
252
253 return OK;
254 }
255
256 } // namespace android
257