blob: 6ae84fef2394261ed0f46381d84de34d78bbaf3d [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -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
Andreas Huber2831d512016-08-15 09:33:47 -070017#include "AST.h"
18
19#include "Coordinator.h"
Andreas Huber2831d512016-08-15 09:33:47 -070020#include "Interface.h"
21#include "Method.h"
22#include "Scope.h"
23
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070024#include <hidl-util/Formatter.h>
Andreas Huber2831d512016-08-15 09:33:47 -070025#include <android-base/logging.h>
26
27namespace android {
28
Andreas Huber2831d512016-08-15 09:33:47 -070029void AST::emitJavaReaderWriter(
30 Formatter &out,
31 const std::string &parcelObj,
32 const TypedVar *arg,
33 bool isReader) const {
34 if (isReader) {
Andreas Huber4c865b72016-09-14 15:26:27 -070035 std::string extra;
36 out << arg->type().getJavaType(&extra)
37 << extra
Andreas Huber2831d512016-08-15 09:33:47 -070038 << " "
39 << arg->name()
40 << " = ";
41 }
42
43 arg->type().emitJavaReaderWriter(out, parcelObj, arg->name(), isReader);
44}
45
Andreas Huber0fa9e392016-08-31 09:05:44 -070046status_t AST::generateJavaTypes(
Andreas Huberd29724f2016-09-14 09:33:13 -070047 const std::string &outputPath, const std::string &limitToType) const {
Andreas Huber85eabdb2016-08-25 11:24:49 -070048 // Splits types.hal up into one java file per declared type.
49
Steven Morelandd537ab02016-09-12 10:32:01 -070050 for (const auto &type : mRootScope->getSubTypes()) {
51 std::string typeName = type->localName();
Andreas Huber85eabdb2016-08-25 11:24:49 -070052
53 if (type->isTypeDef()) {
54 continue;
55 }
56
Andreas Huberd29724f2016-09-14 09:33:13 -070057 if (!limitToType.empty() && typeName != limitToType) {
Andreas Huber0fa9e392016-08-31 09:05:44 -070058 continue;
59 }
60
Andreas Huber85eabdb2016-08-25 11:24:49 -070061 std::string path = outputPath;
62 path.append(mCoordinator->convertPackageRootToPath(mPackage));
63 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
64 path.append(typeName);
65 path.append(".java");
66
67 CHECK(Coordinator::MakeParentHierarchy(path));
68 FILE *file = fopen(path.c_str(), "w");
69
70 if (file == NULL) {
71 return -errno;
72 }
73
74 Formatter out(file);
75
76 std::vector<std::string> packageComponents;
77 getPackageAndVersionComponents(
78 &packageComponents, true /* cpp_compatible */);
79
80 out << "package " << mPackage.javaPackage() << ";\n\n";
81
82 out << "import android.os.HwBlob;\n";
83 out << "import android.os.HwParcel;\n\n";
84
Andreas Huberf630bc82016-09-09 14:52:25 -070085 out << "import java.util.Arrays;\n";
Andreas Huberf1abc2a2016-09-28 09:14:43 -070086 out << "import java.util.ArrayList;\n\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -070087
Iliyan Malchev800273d2016-09-02 15:25:07 -070088 for (const auto &item : mImportedNamesForJava) {
89 out << "import " << item.javaName() << ";\n";
90 }
91 out << "\n";
92
Andreas Huber85eabdb2016-08-25 11:24:49 -070093 status_t err =
94 type->emitJavaTypeDeclarations(out, true /* atTopLevel */);
95
96 if (err != OK) {
97 return err;
98 }
99 }
100
101 return OK;
102}
103
Andreas Huber0fa9e392016-08-31 09:05:44 -0700104status_t AST::generateJava(
Andreas Huberd29724f2016-09-14 09:33:13 -0700105 const std::string &outputPath, const std::string &limitToType) const {
Andreas Huber0fa9e392016-08-31 09:05:44 -0700106 if (!isJavaCompatible()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700107 fprintf(stderr,
108 "ERROR: This interface is not Java compatible. The Java backend"
Andreas Huberf03332a2016-09-22 15:35:43 -0700109 " does NOT support union types nor native handles. "
110 "In addition, vectors of arrays are limited to at most "
Andreas Huber86a112b2016-10-19 14:25:16 -0700111 "one-dimensional arrays and vectors of {vectors,interfaces} are"
112 " not supported.\n");
Andreas Huber70a59e12016-08-16 12:57:01 -0700113
Andreas Huber2831d512016-08-15 09:33:47 -0700114 return UNKNOWN_ERROR;
115 }
116
Andreas Huber0fa9e392016-08-31 09:05:44 -0700117 std::string ifaceName;
118 if (!AST::isInterface(&ifaceName)) {
119 return generateJavaTypes(outputPath, limitToType);
120 }
121
122 const Interface *iface = mRootScope->getInterface();
123
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700124 const std::string baseName = iface->getBaseName();
Andreas Huber2831d512016-08-15 09:33:47 -0700125
126 std::string path = outputPath;
127 path.append(mCoordinator->convertPackageRootToPath(mPackage));
128 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
129 path.append(ifaceName);
130 path.append(".java");
131
132 CHECK(Coordinator::MakeParentHierarchy(path));
133 FILE *file = fopen(path.c_str(), "w");
134
135 if (file == NULL) {
136 return -errno;
137 }
138
139 Formatter out(file);
140
141 std::vector<std::string> packageComponents;
142 getPackageAndVersionComponents(
143 &packageComponents, true /* cpp_compatible */);
144
145 out << "package " << mPackage.javaPackage() << ";\n\n";
146
147 out << "import android.os.IHwBinder;\n";
148 out << "import android.os.IHwInterface;\n";
149 out << "import android.os.HwBinder;\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700150 out << "import android.os.HwBlob;\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700151 out << "import android.os.HwParcel;\n\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700152
153 out << "import java.util.Arrays;\n";
Andreas Huberf1abc2a2016-09-28 09:14:43 -0700154 out << "import java.util.ArrayList;\n\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700155
Andreas Huber85eabdb2016-08-25 11:24:49 -0700156 for (const auto &item : mImportedNamesForJava) {
Andreas Huber2831d512016-08-15 09:33:47 -0700157 out << "import " << item.javaName() << ";\n";
158 }
159
Andreas Huber85eabdb2016-08-25 11:24:49 -0700160 if (!mImportedNamesForJava.empty()) {
Andreas Huber2831d512016-08-15 09:33:47 -0700161 out << "\n";
162 }
163
164 out.setNamespace(mPackage.javaPackage() + ".");
165
Andreas Huber2831d512016-08-15 09:33:47 -0700166 const Interface *superType = iface->superType();
167
168 out << "public interface " << ifaceName << " extends ";
169
170 if (superType != NULL) {
171 out << superType->fullJavaName();
172 } else {
173 out << "IHwInterface";
174 }
175
176 out << " {\n";
177 out.indent();
178
179 out << "public static final String kInterfaceName = \""
180 << mPackage.string()
181 << "::"
182 << ifaceName
183 << "\";\n\n";
184
185 out << "public static "
186 << ifaceName
187 << " asInterface(IHwBinder binder) {\n";
188
189 out.indent();
190
191 out << "if (binder == null) {\n";
192 out.indent();
193 out << "return null;\n";
194 out.unindent();
195 out << "}\n\n";
196
197 out << "IHwInterface iface =\n";
198 out.indent();
199 out.indent();
200 out << "binder.queryLocalInterface(kInterfaceName);\n\n";
201 out.unindent();
202 out.unindent();
203
204 out << "if ((iface != null) && (iface instanceof "
205 << ifaceName
206 << ")) {\n";
207
208 out.indent();
209 out << "return (" << ifaceName << ")iface;\n";
210 out.unindent();
211 out << "}\n\n";
212
213 out << "return new " << ifaceName << ".Proxy(binder);\n";
214
215 out.unindent();
216 out << "}\n\n";
217
218 out << "public IHwBinder asBinder();\n\n";
219
220 status_t err = emitJavaTypeDeclarations(out);
221
222 if (err != OK) {
223 return err;
224 }
225
Andreas Huber2831d512016-08-15 09:33:47 -0700226 for (const auto &method : iface->methods()) {
227 const bool returnsValue = !method->results().empty();
228 const bool needsCallback = method->results().size() > 1;
229
230 if (needsCallback) {
231 out << "\npublic abstract class "
232 << method->name()
233 << "Callback {\n";
234
235 out.indent();
236
237 out << "public abstract void onValues("
Steven Morelanda7a421a2016-09-07 08:35:18 -0700238 << Method::GetJavaArgSignature(method->results())
Andreas Huber2831d512016-08-15 09:33:47 -0700239 << ");\n";
240
241 out.unindent();
242 out << "}\n\n";
243 }
244
245 if (returnsValue && !needsCallback) {
Andreas Huber4c865b72016-09-14 15:26:27 -0700246 std::string extra;
247 out << method->results()[0]->type().getJavaType(&extra)
248 << extra;
Andreas Huber2831d512016-08-15 09:33:47 -0700249 } else {
250 out << "void";
251 }
252
253 out << " "
254 << method->name()
255 << "("
Steven Morelanda7a421a2016-09-07 08:35:18 -0700256 << Method::GetJavaArgSignature(method->args());
Andreas Huber2831d512016-08-15 09:33:47 -0700257
258 if (needsCallback) {
259 if (!method->args().empty()) {
260 out << ", ";
261 }
262
263 out << method->name()
264 << "Callback cb";
265 }
266
267 out << ");\n";
268 }
269
270 out << "\npublic static final class Proxy implements "
271 << ifaceName
272 << " {\n";
273
274 out.indent();
275
276 out << "private IHwBinder mRemote;\n\n";
277 out << "public Proxy(IHwBinder remote) {\n";
278 out.indent();
279 out << "mRemote = remote;\n";
280 out.unindent();
281 out << "}\n\n";
282
283 out << "public IHwBinder asBinder() {\n";
284 out.indent();
285 out << "return mRemote;\n";
286 out.unindent();
287 out << "}\n\n";
288
289 std::vector<const Interface *> chain;
290 while (iface != NULL) {
291 chain.push_back(iface);
292 iface = iface->superType();
293 }
294
295 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
296 const Interface *superInterface = *it;
297
298 out << "// Methods from "
299 << superInterface->fullName()
300 << " follow.\n";
301
302 for (const auto &method : superInterface->methods()) {
303 const bool returnsValue = !method->results().empty();
304 const bool needsCallback = method->results().size() > 1;
305
306 out << "public ";
307 if (returnsValue && !needsCallback) {
Andreas Huber4c865b72016-09-14 15:26:27 -0700308 std::string extra;
309 out << method->results()[0]->type().getJavaType(&extra)
310 << extra;
Andreas Huber2831d512016-08-15 09:33:47 -0700311 } else {
312 out << "void";
313 }
314
315 out << " "
316 << method->name()
317 << "("
Steven Morelanda7a421a2016-09-07 08:35:18 -0700318 << Method::GetJavaArgSignature(method->args());
Andreas Huber2831d512016-08-15 09:33:47 -0700319
320 if (needsCallback) {
321 if (!method->args().empty()) {
322 out << ", ";
323 }
324
325 out << method->name()
326 << "Callback cb";
327 }
328
329 out << ") {\n";
330 out.indent();
331
332 out << "HwParcel request = new HwParcel();\n";
333 out << "request.writeInterfaceToken("
334 << superInterface->fullJavaName()
335 << ".kInterfaceName);\n";
336
337 for (const auto &arg : method->args()) {
338 emitJavaReaderWriter(
339 out,
340 "request",
341 arg,
342 false /* isReader */);
343 }
344
345 out << "\nHwParcel reply = new HwParcel();\n"
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700346 << "mRemote.transact("
347 << method->getSerialId()
348 << " /* "
349 << method->name()
350 << " */, request, reply, ";
Andreas Huber8b5da222016-08-18 14:28:18 -0700351
352 if (method->isOneway()) {
353 out << "IHwBinder.FLAG_ONEWAY";
354 } else {
355 out << "0 /* flags */";
356 }
357
358 out << ");\n";
359
360 if (!method->isOneway()) {
361 out << "reply.verifySuccess();\n";
362 } else {
363 CHECK(!returnsValue);
364 }
365
366 out << "request.releaseTemporaryStorage();\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700367
368 if (returnsValue) {
369 out << "\n";
370
371 for (const auto &arg : method->results()) {
372 emitJavaReaderWriter(
373 out,
374 "reply",
375 arg,
376 true /* isReader */);
377 }
378
379 if (needsCallback) {
380 out << "cb.onValues(";
381
382 bool firstField = true;
383 for (const auto &arg : method->results()) {
384 if (!firstField) {
385 out << ", ";
386 }
387
388 out << arg->name();
389 firstField = false;
390 }
391
392 out << ");\n";
393 } else {
394 const std::string returnName = method->results()[0]->name();
395 out << "return " << returnName << ";\n";
396 }
397 }
398
399 out.unindent();
400 out << "}\n\n";
401 }
402 }
403
404 out.unindent();
405 out << "}\n";
406
407 ////////////////////////////////////////////////////////////////////////////
408
409 out << "\npublic static abstract class Stub extends HwBinder "
410 << "implements "
411 << ifaceName << " {\n";
412
413 out.indent();
414
415 out << "public IHwBinder asBinder() {\n";
416 out.indent();
417 out << "return this;\n";
418 out.unindent();
419 out << "}\n\n";
420
421 out << "public IHwInterface queryLocalInterface(String descriptor) {\n";
422 out.indent();
423 // XXX what about potential superClasses?
424 out << "if (kInterfaceName.equals(descriptor)) {\n";
425 out.indent();
426 out << "return this;\n";
427 out.unindent();
428 out << "}\n";
429 out << "return null;\n";
430 out.unindent();
431 out << "}\n\n";
432
433 out << "public void onTransact("
434 << "int code, HwParcel request, final HwParcel reply, "
435 << "int flags) {\n";
436
437 out.indent();
438
439 out << "switch (code) {\n";
440
441 out.indent();
442
443 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
444 const Interface *superInterface = *it;
445
446 for (const auto &method : superInterface->methods()) {
447 const bool returnsValue = !method->results().empty();
448 const bool needsCallback = method->results().size() > 1;
449
450 out << "case "
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700451 << method->getSerialId()
452 << " /* "
453 << method->name()
454 << " */:\n{\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700455
456 out.indent();
457
458 out << "request.enforceInterface("
459 << superInterface->fullJavaName()
460 << ".kInterfaceName);\n\n";
461
462 for (const auto &arg : method->args()) {
463 emitJavaReaderWriter(
464 out,
465 "request",
466 arg,
467 true /* isReader */);
468 }
469
470 if (!needsCallback && returnsValue) {
471 const TypedVar *returnArg = method->results()[0];
Andreas Huber4c865b72016-09-14 15:26:27 -0700472 std::string extra;
Andreas Huber2831d512016-08-15 09:33:47 -0700473
Andreas Huber4c865b72016-09-14 15:26:27 -0700474 out << returnArg->type().getJavaType(&extra)
475 << extra
Andreas Huber2831d512016-08-15 09:33:47 -0700476 << " "
477 << returnArg->name()
478 << " = ";
479 }
480
481 out << method->name()
482 << "(";
483
484 bool firstField = true;
485 for (const auto &arg : method->args()) {
486 if (!firstField) {
487 out << ", ";
488 }
489
490 out << arg->name();
491
492 firstField = false;
493 }
494
495 if (needsCallback) {
496 if (!firstField) {
497 out << ", ";
498 }
499
500 out << "new " << method->name() << "Callback() {\n";
501 out.indent();
502
503 out << "@Override\n"
504 << "public void onValues("
Steven Morelanda7a421a2016-09-07 08:35:18 -0700505 << Method::GetJavaArgSignature(method->results())
Andreas Huber2831d512016-08-15 09:33:47 -0700506 << ") {\n";
507
508 out.indent();
509 out << "reply.writeStatus(HwParcel.STATUS_SUCCESS);\n";
510
511 for (const auto &arg : method->results()) {
512 emitJavaReaderWriter(
513 out,
514 "reply",
515 arg,
516 false /* isReader */);
517 }
518
519 out << "reply.send();\n"
520 << "}}";
521
522 out.unindent();
523 out.unindent();
524 }
525
526 out << ");\n";
527
528 if (!needsCallback) {
529 out << "reply.writeStatus(HwParcel.STATUS_SUCCESS);\n";
530
531 if (returnsValue) {
532 const TypedVar *returnArg = method->results()[0];
533
534 emitJavaReaderWriter(
535 out,
536 "reply",
537 returnArg,
538 false /* isReader */);
539 }
540
541 out << "reply.send();\n";
542 }
543
544 out << "break;\n";
545 out.unindent();
546 out << "}\n\n";
547 }
548 }
549
550 out.unindent();
551 out << "}\n";
552
553 out.unindent();
554 out << "}\n";
555
556 out.unindent();
557 out << "}\n";
558
559 out.unindent();
560 out << "}\n";
561
562 return OK;
563}
564
565status_t AST::emitJavaTypeDeclarations(Formatter &out) const {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700566 return mRootScope->emitJavaTypeDeclarations(out, false /* atTopLevel */);
Andreas Huber2831d512016-08-15 09:33:47 -0700567}
568
569} // namespace android
570