blob: 8dab1792d71bd8dbb67406c324ee174fe7c7ecc7 [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"
20#include "Formatter.h"
21#include "Interface.h"
22#include "Method.h"
23#include "Scope.h"
Steven Morelandaf440142016-09-07 10:09:11 -070024#include "StringHelper.h"
Andreas Huber2831d512016-08-15 09:33:47 -070025
26#include <android-base/logging.h>
27
28namespace android {
29
Andreas Huber2831d512016-08-15 09:33:47 -070030void AST::emitJavaReaderWriter(
31 Formatter &out,
32 const std::string &parcelObj,
33 const TypedVar *arg,
34 bool isReader) const {
35 if (isReader) {
36 out << arg->type().getJavaType()
37 << " "
38 << arg->name()
39 << " = ";
40 }
41
42 arg->type().emitJavaReaderWriter(out, parcelObj, arg->name(), isReader);
43}
44
Andreas Huber0fa9e392016-08-31 09:05:44 -070045status_t AST::generateJavaTypes(
46 const std::string &outputPath, const char *limitToType) const {
Andreas Huber85eabdb2016-08-25 11:24:49 -070047 // Splits types.hal up into one java file per declared type.
48
49 for (size_t i = 0; i < mRootScope->countTypes(); ++i) {
50 std::string typeName;
51 const Type *type = mRootScope->typeAt(i, &typeName);
52
53 if (type->isTypeDef()) {
54 continue;
55 }
56
Andreas Huber0fa9e392016-08-31 09:05:44 -070057 if ((limitToType != nullptr) && typeName != limitToType) {
58 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 Huber85eabdb2016-08-25 11:24:49 -070086 out << "import java.util.Vector;\n\n";
87
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(
105 const std::string &outputPath, const char *limitToType) const {
106 if (!isJavaCompatible()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700107 fprintf(stderr,
108 "ERROR: This interface is not Java compatible. The Java backend"
Andreas Huber85eabdb2016-08-25 11:24:49 -0700109 " does NOT support union types or native handles.\n");
Andreas Huber70a59e12016-08-16 12:57:01 -0700110
Andreas Huber2831d512016-08-15 09:33:47 -0700111 return UNKNOWN_ERROR;
112 }
113
Andreas Huber0fa9e392016-08-31 09:05:44 -0700114 std::string ifaceName;
115 if (!AST::isInterface(&ifaceName)) {
116 return generateJavaTypes(outputPath, limitToType);
117 }
118
119 const Interface *iface = mRootScope->getInterface();
120
Andreas Huber2831d512016-08-15 09:33:47 -0700121 // cut off the leading 'I'.
122 const std::string baseName = ifaceName.substr(1);
123
124 std::string path = outputPath;
125 path.append(mCoordinator->convertPackageRootToPath(mPackage));
126 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
127 path.append(ifaceName);
128 path.append(".java");
129
130 CHECK(Coordinator::MakeParentHierarchy(path));
131 FILE *file = fopen(path.c_str(), "w");
132
133 if (file == NULL) {
134 return -errno;
135 }
136
137 Formatter out(file);
138
139 std::vector<std::string> packageComponents;
140 getPackageAndVersionComponents(
141 &packageComponents, true /* cpp_compatible */);
142
143 out << "package " << mPackage.javaPackage() << ";\n\n";
144
145 out << "import android.os.IHwBinder;\n";
146 out << "import android.os.IHwInterface;\n";
147 out << "import android.os.HwBinder;\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700148 out << "import android.os.HwBlob;\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700149 out << "import android.os.HwParcel;\n\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700150
151 out << "import java.util.Arrays;\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700152 out << "import java.util.Vector;\n\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700153
Andreas Huber85eabdb2016-08-25 11:24:49 -0700154 for (const auto &item : mImportedNamesForJava) {
Andreas Huber2831d512016-08-15 09:33:47 -0700155 out << "import " << item.javaName() << ";\n";
156 }
157
Andreas Huber85eabdb2016-08-25 11:24:49 -0700158 if (!mImportedNamesForJava.empty()) {
Andreas Huber2831d512016-08-15 09:33:47 -0700159 out << "\n";
160 }
161
162 out.setNamespace(mPackage.javaPackage() + ".");
163
Andreas Huber2831d512016-08-15 09:33:47 -0700164 const Interface *superType = iface->superType();
165
166 out << "public interface " << ifaceName << " extends ";
167
168 if (superType != NULL) {
169 out << superType->fullJavaName();
170 } else {
171 out << "IHwInterface";
172 }
173
174 out << " {\n";
175 out.indent();
176
177 out << "public static final String kInterfaceName = \""
178 << mPackage.string()
179 << "::"
180 << ifaceName
181 << "\";\n\n";
182
183 out << "public static "
184 << ifaceName
185 << " asInterface(IHwBinder binder) {\n";
186
187 out.indent();
188
189 out << "if (binder == null) {\n";
190 out.indent();
191 out << "return null;\n";
192 out.unindent();
193 out << "}\n\n";
194
195 out << "IHwInterface iface =\n";
196 out.indent();
197 out.indent();
198 out << "binder.queryLocalInterface(kInterfaceName);\n\n";
199 out.unindent();
200 out.unindent();
201
202 out << "if ((iface != null) && (iface instanceof "
203 << ifaceName
204 << ")) {\n";
205
206 out.indent();
207 out << "return (" << ifaceName << ")iface;\n";
208 out.unindent();
209 out << "}\n\n";
210
211 out << "return new " << ifaceName << ".Proxy(binder);\n";
212
213 out.unindent();
214 out << "}\n\n";
215
216 out << "public IHwBinder asBinder();\n\n";
217
218 status_t err = emitJavaTypeDeclarations(out);
219
220 if (err != OK) {
221 return err;
222 }
223
224 const std::string base = (superType != NULL)
225 ? (superType->fullJavaName() + ".kOpEnd")
226 : "IHwBinder.FIRST_CALL_TRANSACTION";
227
228 bool first = true;
229 size_t index = 0;
230 for (const auto &method : iface->methods()) {
231 out << "public static final int kOp_"
Steven Morelandaf440142016-09-07 10:09:11 -0700232 << StringHelper::Upcase(method->name())
Andreas Huber2831d512016-08-15 09:33:47 -0700233 << " = "
234 << base;
235
236 if (!first) {
237 out << " + " << index;
238 }
239
240 out << ";\n";
241
242 ++index;
243 first = false;
244 }
245
246 out << "public static final int kOpEnd = "
247 << base
248 << " + "
249 << index
250 << ";";
251
252 out << "\n\n";
253
254 for (const auto &method : iface->methods()) {
255 const bool returnsValue = !method->results().empty();
256 const bool needsCallback = method->results().size() > 1;
257
258 if (needsCallback) {
259 out << "\npublic abstract class "
260 << method->name()
261 << "Callback {\n";
262
263 out.indent();
264
265 out << "public abstract void onValues("
Steven Morelanda7a421a2016-09-07 08:35:18 -0700266 << Method::GetJavaArgSignature(method->results())
Andreas Huber2831d512016-08-15 09:33:47 -0700267 << ");\n";
268
269 out.unindent();
270 out << "}\n\n";
271 }
272
273 if (returnsValue && !needsCallback) {
274 out << method->results()[0]->type().getJavaType();
275 } else {
276 out << "void";
277 }
278
279 out << " "
280 << method->name()
281 << "("
Steven Morelanda7a421a2016-09-07 08:35:18 -0700282 << Method::GetJavaArgSignature(method->args());
Andreas Huber2831d512016-08-15 09:33:47 -0700283
284 if (needsCallback) {
285 if (!method->args().empty()) {
286 out << ", ";
287 }
288
289 out << method->name()
290 << "Callback cb";
291 }
292
293 out << ");\n";
294 }
295
296 out << "\npublic static final class Proxy implements "
297 << ifaceName
298 << " {\n";
299
300 out.indent();
301
302 out << "private IHwBinder mRemote;\n\n";
303 out << "public Proxy(IHwBinder remote) {\n";
304 out.indent();
305 out << "mRemote = remote;\n";
306 out.unindent();
307 out << "}\n\n";
308
309 out << "public IHwBinder asBinder() {\n";
310 out.indent();
311 out << "return mRemote;\n";
312 out.unindent();
313 out << "}\n\n";
314
315 std::vector<const Interface *> chain;
316 while (iface != NULL) {
317 chain.push_back(iface);
318 iface = iface->superType();
319 }
320
321 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
322 const Interface *superInterface = *it;
323
324 out << "// Methods from "
325 << superInterface->fullName()
326 << " follow.\n";
327
328 for (const auto &method : superInterface->methods()) {
329 const bool returnsValue = !method->results().empty();
330 const bool needsCallback = method->results().size() > 1;
331
332 out << "public ";
333 if (returnsValue && !needsCallback) {
334 out << method->results()[0]->type().getJavaType();
335 } else {
336 out << "void";
337 }
338
339 out << " "
340 << method->name()
341 << "("
Steven Morelanda7a421a2016-09-07 08:35:18 -0700342 << Method::GetJavaArgSignature(method->args());
Andreas Huber2831d512016-08-15 09:33:47 -0700343
344 if (needsCallback) {
345 if (!method->args().empty()) {
346 out << ", ";
347 }
348
349 out << method->name()
350 << "Callback cb";
351 }
352
353 out << ") {\n";
354 out.indent();
355
356 out << "HwParcel request = new HwParcel();\n";
357 out << "request.writeInterfaceToken("
358 << superInterface->fullJavaName()
359 << ".kInterfaceName);\n";
360
361 for (const auto &arg : method->args()) {
362 emitJavaReaderWriter(
363 out,
364 "request",
365 arg,
366 false /* isReader */);
367 }
368
369 out << "\nHwParcel reply = new HwParcel();\n"
Andreas Huber8b5da222016-08-18 14:28:18 -0700370 << "mRemote.transact(kOp_"
Steven Morelandaf440142016-09-07 10:09:11 -0700371 << StringHelper::Upcase(method->name())
Andreas Huber8b5da222016-08-18 14:28:18 -0700372 << ", request, reply, ";
373
374 if (method->isOneway()) {
375 out << "IHwBinder.FLAG_ONEWAY";
376 } else {
377 out << "0 /* flags */";
378 }
379
380 out << ");\n";
381
382 if (!method->isOneway()) {
383 out << "reply.verifySuccess();\n";
384 } else {
385 CHECK(!returnsValue);
386 }
387
388 out << "request.releaseTemporaryStorage();\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700389
390 if (returnsValue) {
391 out << "\n";
392
393 for (const auto &arg : method->results()) {
394 emitJavaReaderWriter(
395 out,
396 "reply",
397 arg,
398 true /* isReader */);
399 }
400
401 if (needsCallback) {
402 out << "cb.onValues(";
403
404 bool firstField = true;
405 for (const auto &arg : method->results()) {
406 if (!firstField) {
407 out << ", ";
408 }
409
410 out << arg->name();
411 firstField = false;
412 }
413
414 out << ");\n";
415 } else {
416 const std::string returnName = method->results()[0]->name();
417 out << "return " << returnName << ";\n";
418 }
419 }
420
421 out.unindent();
422 out << "}\n\n";
423 }
424 }
425
426 out.unindent();
427 out << "}\n";
428
429 ////////////////////////////////////////////////////////////////////////////
430
431 out << "\npublic static abstract class Stub extends HwBinder "
432 << "implements "
433 << ifaceName << " {\n";
434
435 out.indent();
436
437 out << "public IHwBinder asBinder() {\n";
438 out.indent();
439 out << "return this;\n";
440 out.unindent();
441 out << "}\n\n";
442
443 out << "public IHwInterface queryLocalInterface(String descriptor) {\n";
444 out.indent();
445 // XXX what about potential superClasses?
446 out << "if (kInterfaceName.equals(descriptor)) {\n";
447 out.indent();
448 out << "return this;\n";
449 out.unindent();
450 out << "}\n";
451 out << "return null;\n";
452 out.unindent();
453 out << "}\n\n";
454
455 out << "public void onTransact("
456 << "int code, HwParcel request, final HwParcel reply, "
457 << "int flags) {\n";
458
459 out.indent();
460
461 out << "switch (code) {\n";
462
463 out.indent();
464
465 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
466 const Interface *superInterface = *it;
467
468 for (const auto &method : superInterface->methods()) {
469 const bool returnsValue = !method->results().empty();
470 const bool needsCallback = method->results().size() > 1;
471
472 out << "case "
473 << superInterface->fullJavaName()
474 << ".kOp_"
475 <<
Steven Morelandaf440142016-09-07 10:09:11 -0700476 StringHelper::Upcase(method->name())
Andreas Huber2831d512016-08-15 09:33:47 -0700477 << ":\n{\n";
478
479 out.indent();
480
481 out << "request.enforceInterface("
482 << superInterface->fullJavaName()
483 << ".kInterfaceName);\n\n";
484
485 for (const auto &arg : method->args()) {
486 emitJavaReaderWriter(
487 out,
488 "request",
489 arg,
490 true /* isReader */);
491 }
492
493 if (!needsCallback && returnsValue) {
494 const TypedVar *returnArg = method->results()[0];
495
496 out << returnArg->type().getJavaType()
497 << " "
498 << returnArg->name()
499 << " = ";
500 }
501
502 out << method->name()
503 << "(";
504
505 bool firstField = true;
506 for (const auto &arg : method->args()) {
507 if (!firstField) {
508 out << ", ";
509 }
510
511 out << arg->name();
512
513 firstField = false;
514 }
515
516 if (needsCallback) {
517 if (!firstField) {
518 out << ", ";
519 }
520
521 out << "new " << method->name() << "Callback() {\n";
522 out.indent();
523
524 out << "@Override\n"
525 << "public void onValues("
Steven Morelanda7a421a2016-09-07 08:35:18 -0700526 << Method::GetJavaArgSignature(method->results())
Andreas Huber2831d512016-08-15 09:33:47 -0700527 << ") {\n";
528
529 out.indent();
530 out << "reply.writeStatus(HwParcel.STATUS_SUCCESS);\n";
531
532 for (const auto &arg : method->results()) {
533 emitJavaReaderWriter(
534 out,
535 "reply",
536 arg,
537 false /* isReader */);
538 }
539
540 out << "reply.send();\n"
541 << "}}";
542
543 out.unindent();
544 out.unindent();
545 }
546
547 out << ");\n";
548
549 if (!needsCallback) {
550 out << "reply.writeStatus(HwParcel.STATUS_SUCCESS);\n";
551
552 if (returnsValue) {
553 const TypedVar *returnArg = method->results()[0];
554
555 emitJavaReaderWriter(
556 out,
557 "reply",
558 returnArg,
559 false /* isReader */);
560 }
561
562 out << "reply.send();\n";
563 }
564
565 out << "break;\n";
566 out.unindent();
567 out << "}\n\n";
568 }
569 }
570
571 out.unindent();
572 out << "}\n";
573
574 out.unindent();
575 out << "}\n";
576
577 out.unindent();
578 out << "}\n";
579
580 out.unindent();
581 out << "}\n";
582
583 return OK;
584}
585
586status_t AST::emitJavaTypeDeclarations(Formatter &out) const {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700587 return mRootScope->emitJavaTypeDeclarations(out, false /* atTopLevel */);
Andreas Huber2831d512016-08-15 09:33:47 -0700588}
589
590} // namespace android
591