blob: 90331b6a9b7227308469b3279a2ca45e242161f0 [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"
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070023#include "Reference.h"
Steven Moreland9c387612016-09-07 09:54:26 -070024#include "ScalarType.h"
25#include "Scope.h"
26
27#include <algorithm>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070028#include <hidl-util/Formatter.h>
Steven Moreland9c387612016-09-07 09:54:26 -070029#include <android-base/logging.h>
30#include <string>
31#include <vector>
32#include <set>
33
Steven Moreland9c387612016-09-07 09:54:26 -070034namespace android {
35
Steven Moreland9c387612016-09-07 09:54:26 -070036status_t AST::generateCppImpl(const std::string &outputPath) const {
37 status_t err = generateStubImplHeader(outputPath);
38
39 if (err == OK) {
40 err = generateStubImplSource(outputPath);
41 }
42
43 return err;
44}
45
46void AST::generateFetchSymbol(Formatter &out, const std::string& ifaceName) const {
47 out << "HIDL_FETCH_" << ifaceName;
48}
49
50status_t AST::generateStubImplMethod(Formatter &out,
51 const std::string &className,
Yifan Hong068c5522016-10-31 14:07:25 -070052 const Method *method) const {
Steven Moreland9c387612016-09-07 09:54:26 -070053
Yifan Hong1254b552016-10-27 15:03:29 -070054 // ignore HIDL reserved methods -- implemented in IFoo already.
55 if (method->isHidlReserved()) {
56 return OK;
57 }
58
Yifan Hong068c5522016-10-31 14:07:25 -070059 method->generateCppSignature(out, className, false /* specifyNamespaces */);
Steven Moreland9c387612016-09-07 09:54:26 -070060
61 out << " {\n";
62
63 out.indent();
64 out << "// TODO implement\n";
65
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070066 const NamedReference<Type>* elidedReturn = method->canElideCallback();
Steven Moreland9c387612016-09-07 09:54:26 -070067
68 if (elidedReturn == nullptr) {
69 out << "return Void();\n";
70 } else {
Steven Moreland9c387612016-09-07 09:54:26 -070071 out << "return "
Yifan Hong3b320f82016-11-01 15:15:54 -070072 << elidedReturn->type().getCppResultType()
Steven Moreland9c387612016-09-07 09:54:26 -070073 << " {};\n";
74 }
75
76 out.unindent();
77
78 out << "}\n\n";
79
80 return OK;
81}
82
Steven Moreland9c387612016-09-07 09:54:26 -070083status_t AST::generateStubImplHeader(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -070084 if (!AST::isInterface()) {
Steven Moreland9c387612016-09-07 09:54:26 -070085 // types.hal does not get a stub header.
86 return OK;
87 }
88
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070089 const Interface* iface = mRootScope.getInterface();
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -070090 const std::string baseName = iface->getBaseName();
Steven Moreland9c387612016-09-07 09:54:26 -070091
92 std::string path = outputPath;
93 path.append(baseName);
94 path.append(".h");
95
96 CHECK(Coordinator::MakeParentHierarchy(path));
97 FILE *file = fopen(path.c_str(), "w");
98
99 if (file == NULL) {
100 return -errno;
101 }
102
103 Formatter out(file);
104
Steven Moreland5708edf2016-11-04 15:33:31 +0000105 const std::string guard = makeHeaderGuard(baseName, false /* indicateGenerated */);
Steven Moreland9c387612016-09-07 09:54:26 -0700106
107 out << "#ifndef " << guard << "\n";
108 out << "#define " << guard << "\n\n";
109
Yifan Hongeefe4f22017-01-04 15:32:42 -0800110 generateCppPackageInclude(out, mPackage, iface->localName());
Steven Moreland9c387612016-09-07 09:54:26 -0700111
Andreas Huber4bcf97d2016-08-30 11:27:49 -0700112 out << "#include <hidl/MQDescriptor.h>\n";
Steven Moreland41c6d2e2016-11-07 12:26:54 -0800113 out << "#include <hidl/Status.h>\n\n";
Steven Moreland9c387612016-09-07 09:54:26 -0700114
115 enterLeaveNamespace(out, true /* enter */);
116 out << "namespace implementation {\n\n";
117
Steven Morelande429a262016-11-15 09:54:32 -0800118 out << "using ::android::hardware::hidl_array;\n";
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100119 out << "using ::android::hardware::hidl_memory;\n";
Steven Morelande429a262016-11-15 09:54:32 -0800120 out << "using ::android::hardware::hidl_string;\n";
121 out << "using ::android::hardware::hidl_vec;\n";
Steven Moreland9c387612016-09-07 09:54:26 -0700122 out << "using ::android::hardware::Return;\n";
123 out << "using ::android::hardware::Void;\n";
Steven Moreland9c387612016-09-07 09:54:26 -0700124 out << "using ::android::sp;\n";
125
126 out << "\n";
127
128 out << "struct "
129 << baseName
130 << " : public "
Steven Moreland19f11b52017-05-12 18:22:21 -0700131 << iface->localName()
Steven Moreland9c387612016-09-07 09:54:26 -0700132 << " {\n";
133
134 out.indent();
135
Yifan Hong068c5522016-10-31 14:07:25 -0700136 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
137 // ignore HIDL reserved methods -- implemented in IFoo already.
138 if (method->isHidlReserved()) {
139 return OK;
140 }
141 method->generateCppSignature(out, "" /* className */,
142 false /* specifyNamespaces */);
143 out << " override;\n";
144 return OK;
145 });
Steven Moreland9c387612016-09-07 09:54:26 -0700146
147 if (err != OK) {
148 return err;
149 }
150
151 out.unindent();
152
153 out << "};\n\n";
154
Steven Moreland3918fcc2017-04-21 10:54:27 -0700155 out << "// FIXME: most likely delete, this is only for passthrough implementations\n"
156 << "// extern \"C\" "
Steven Moreland19f11b52017-05-12 18:22:21 -0700157 << iface->localName()
Steven Moreland9c387612016-09-07 09:54:26 -0700158 << "* ";
Steven Moreland19f11b52017-05-12 18:22:21 -0700159 generateFetchSymbol(out, iface->localName());
Steven Moreland9c387612016-09-07 09:54:26 -0700160 out << "(const char* name);\n\n";
161
162 out << "} // namespace implementation\n";
163 enterLeaveNamespace(out, false /* leave */);
164
165 out << "\n#endif // " << guard << "\n";
166
167 return OK;
168}
169
170status_t AST::generateStubImplSource(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700171 if (!AST::isInterface()) {
Steven Moreland9c387612016-09-07 09:54:26 -0700172 // types.hal does not get a stub header.
173 return OK;
174 }
175
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700176 const Interface* iface = mRootScope.getInterface();
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700177 const std::string baseName = iface->getBaseName();
Steven Moreland9c387612016-09-07 09:54:26 -0700178
179 std::string path = outputPath;
180 path.append(baseName);
181 path.append(".cpp");
182
183 CHECK(Coordinator::MakeParentHierarchy(path));
184 FILE *file = fopen(path.c_str(), "w");
185
186 if (file == NULL) {
187 return -errno;
188 }
189
190 Formatter out(file);
191
192 out << "#include \"" << baseName << ".h\"\n\n";
193
194 enterLeaveNamespace(out, true /* enter */);
195 out << "namespace implementation {\n\n";
196
Yifan Hong068c5522016-10-31 14:07:25 -0700197 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
198 return generateStubImplMethod(out, baseName, method);
199 });
Steven Moreland9c387612016-09-07 09:54:26 -0700200
Steven Moreland60818632017-02-04 00:33:42 -0800201 if (err != OK) {
202 return err;
203 }
204
Steven Moreland3918fcc2017-04-21 10:54:27 -0700205 out.setLinePrefix("//");
Steven Moreland19f11b52017-05-12 18:22:21 -0700206 out << iface->localName()
Steven Moreland9c387612016-09-07 09:54:26 -0700207 << "* ";
Steven Moreland19f11b52017-05-12 18:22:21 -0700208 generateFetchSymbol(out, iface->localName());
Steven Moreland9c387612016-09-07 09:54:26 -0700209 out << "(const char* /* name */) {\n";
210 out.indent();
211 out << "return new " << baseName << "();\n";
212 out.unindent();
213 out << "}\n\n";
Steven Moreland3918fcc2017-04-21 10:54:27 -0700214 out.unsetLinePrefix();
Steven Moreland9c387612016-09-07 09:54:26 -0700215
Yifan Hongdfc4b9c2016-12-02 16:31:31 -0800216 out << "} // namespace implementation\n";
Steven Moreland9c387612016-09-07 09:54:26 -0700217 enterLeaveNamespace(out, false /* leave */);
218
219 return OK;
220}
221
222} // namespace android