blob: 3c57e6a2d3785f4e3582746496011b19ed817858 [file] [log] [blame]
Christopher Wiley89eaab52015-09-15 14:46:46 -07001/*
2 * Copyright (C) 2015, 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 */
Adam Lesinskiffa16862014-01-23 18:17:42 -080016
Christopher Wileyf690be52015-09-14 15:19:10 -070017#include "aidl.h"
Adam Lesinskiffa16862014-01-23 18:17:42 -080018
Christopher Wileyf690be52015-09-14 15:19:10 -070019#include <fcntl.h>
Christopher Wileyc16e5e72015-09-16 10:49:40 -070020#include <iostream>
Christopher Wileyf690be52015-09-14 15:19:10 -070021#include <map>
Adam Lesinskiffa16862014-01-23 18:17:42 -080022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Christopher Wileyf690be52015-09-14 15:19:10 -070025#include <sys/param.h>
26#include <sys/stat.h>
27#include <unistd.h>
Adam Lesinskiffa16862014-01-23 18:17:42 -080028
Elliott Hughes549b6e22015-08-17 12:41:46 -070029#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -080030#include <io.h>
Andrew Hsieh15ce9942014-05-07 20:14:30 +080031#include <direct.h>
Adam Lesinskiffa16862014-01-23 18:17:42 -080032#include <sys/stat.h>
33#endif
34
Christopher Wileyf690be52015-09-14 15:19:10 -070035
Christopher Wileyf690be52015-09-14 15:19:10 -070036#include "aidl_language.h"
Christopher Wileyeb1acc12015-09-16 11:25:13 -070037#include "generate_cpp.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070038#include "generate_java.h"
39#include "logging.h"
40#include "options.h"
41#include "os.h"
42#include "parse_helpers.h"
43#include "search_path.h"
Christopher Wiley775fa1f2015-09-22 15:00:12 -070044#include "type_java.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070045
Adam Lesinskiffa16862014-01-23 18:17:42 -080046#ifndef O_BINARY
47# define O_BINARY 0
48#endif
49
Christopher Wileyc16e5e72015-09-16 10:49:40 -070050using std::cerr;
51using std::endl;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070052using std::map;
53using std::set;
54using std::string;
55using std::vector;
Adam Lesinskiffa16862014-01-23 18:17:42 -080056
Christopher Wileyf690be52015-09-14 15:19:10 -070057namespace android {
58namespace aidl {
59namespace {
Adam Lesinskiffa16862014-01-23 18:17:42 -080060
Christopher Wileyf690be52015-09-14 15:19:10 -070061// The following are gotten as the offset from the allowable id's between
62// android.os.IBinder.FIRST_CALL_TRANSACTION=1 and
63// android.os.IBinder.LAST_CALL_TRANSACTION=16777215
64const int kMinUserSetMethodId = 0;
65const int kMaxUserSetMethodId = 16777214;
Adam Lesinskiffa16862014-01-23 18:17:42 -080066
Christopher Wileyf690be52015-09-14 15:19:10 -070067int check_filename(const char* filename,
68 const char* package,
69 buffer_type* name) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080070 const char* p;
71 string expected;
72 string fn;
73 size_t len;
74 char cwd[MAXPATHLEN];
75 bool valid = false;
76
Elliott Hughesce310da2015-07-29 08:44:17 -070077#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -080078 if (isalpha(filename[0]) && filename[1] == ':'
79 && filename[2] == OS_PATH_SEPARATOR) {
80#else
81 if (filename[0] == OS_PATH_SEPARATOR) {
82#endif
83 fn = filename;
84 } else {
85 fn = getcwd(cwd, sizeof(cwd));
86 len = fn.length();
87 if (fn[len-1] != OS_PATH_SEPARATOR) {
88 fn += OS_PATH_SEPARATOR;
89 }
90 fn += filename;
91 }
92
93 if (package) {
94 expected = package;
95 expected += '.';
96 }
97
98 len = expected.length();
99 for (size_t i=0; i<len; i++) {
100 if (expected[i] == '.') {
101 expected[i] = OS_PATH_SEPARATOR;
102 }
103 }
104
105 p = strchr(name->data, '.');
106 len = p ? p-name->data : strlen(name->data);
107 expected.append(name->data, len);
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700108
Adam Lesinskiffa16862014-01-23 18:17:42 -0800109 expected += ".aidl";
110
111 len = fn.length();
112 valid = (len >= expected.length());
113
114 if (valid) {
115 p = fn.c_str() + (len - expected.length());
116
Elliott Hughesce310da2015-07-29 08:44:17 -0700117#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800118 if (OS_PATH_SEPARATOR != '/') {
119 // Input filename under cygwin most likely has / separators
120 // whereas the expected string uses \\ separators. Adjust
121 // them accordingly.
122 for (char *c = const_cast<char *>(p); *c; ++c) {
123 if (*c == '/') *c = OS_PATH_SEPARATOR;
124 }
125 }
126#endif
127
Yabin Cui482eefb2014-11-10 15:01:43 -0800128 // aidl assumes case-insensitivity on Mac Os and Windows.
129#if defined(__linux__)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800130 valid = (expected == p);
131#else
132 valid = !strcasecmp(expected.c_str(), p);
133#endif
134 }
135
136 if (!valid) {
137 fprintf(stderr, "%s:%d interface %s should be declared in a file"
138 " called %s.\n",
139 filename, name->lineno, name->data, expected.c_str());
140 return 1;
141 }
142
143 return 0;
144}
145
Christopher Wileyf690be52015-09-14 15:19:10 -0700146int check_filenames(const char* filename, document_item_type* items) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800147 int err = 0;
148 while (items) {
149 if (items->item_type == USER_DATA_TYPE) {
150 user_data_type* p = (user_data_type*)items;
151 err |= check_filename(filename, p->package, &p->name);
152 }
Casey Dahlin88868fc2015-09-01 13:21:26 -0700153 else if (items->item_type == INTERFACE_TYPE_BINDER) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800154 interface_type* c = (interface_type*)items;
155 err |= check_filename(filename, c->package, &c->name);
156 }
157 else {
158 fprintf(stderr, "aidl: internal error unkown document type %d.\n",
159 items->item_type);
160 return 1;
161 }
162 items = items->next;
163 }
164 return err;
165}
166
Christopher Wileyf690be52015-09-14 15:19:10 -0700167char* rfind(char* str, char c) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800168 char* p = str + strlen(str) - 1;
169 while (p >= str) {
170 if (*p == c) {
171 return p;
172 }
173 p--;
174 }
175 return NULL;
176}
177
Christopher Wileyf690be52015-09-14 15:19:10 -0700178int gather_types(const char* filename, document_item_type* items) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800179 int err = 0;
180 while (items) {
181 Type* type;
182 if (items->item_type == USER_DATA_TYPE) {
183 user_data_type* p = (user_data_type*)items;
184 type = new UserDataType(p->package ? p->package : "", p->name.data,
Casey Dahlin88868fc2015-09-01 13:21:26 -0700185 false, p->parcelable, filename, p->name.lineno);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800186 }
Casey Dahlin88868fc2015-09-01 13:21:26 -0700187 else if (items->item_type == INTERFACE_TYPE_BINDER) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800188 interface_type* c = (interface_type*)items;
189 type = new InterfaceType(c->package ? c->package : "",
190 c->name.data, false, c->oneway,
191 filename, c->name.lineno);
192 }
193 else {
194 fprintf(stderr, "aidl: internal error %s:%d\n", __FILE__, __LINE__);
195 return 1;
196 }
197
198 Type* old = NAMES.Find(type->QualifiedName());
199 if (old == NULL) {
200 NAMES.Add(type);
201
202 if (items->item_type == INTERFACE_TYPE_BINDER) {
203 // for interfaces, also add the stub and proxy types, we don't
204 // bother checking these for duplicates, because the parser
205 // won't let us do it.
206 interface_type* c = (interface_type*)items;
207
208 string name = c->name.data;
209 name += ".Stub";
210 Type* stub = new Type(c->package ? c->package : "",
Casey Dahlin88868fc2015-09-01 13:21:26 -0700211 name, Type::GENERATED, false, false,
Adam Lesinskiffa16862014-01-23 18:17:42 -0800212 filename, c->name.lineno);
213 NAMES.Add(stub);
214
215 name = c->name.data;
216 name += ".Stub.Proxy";
217 Type* proxy = new Type(c->package ? c->package : "",
Casey Dahlin88868fc2015-09-01 13:21:26 -0700218 name, Type::GENERATED, false, false,
Adam Lesinskiffa16862014-01-23 18:17:42 -0800219 filename, c->name.lineno);
220 NAMES.Add(proxy);
221 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800222 } else {
223 if (old->Kind() == Type::BUILT_IN) {
224 fprintf(stderr, "%s:%d attempt to redefine built in class %s\n",
225 filename, type->DeclLine(),
226 type->QualifiedName().c_str());
227 err = 1;
228 }
229 else if (type->Kind() != old->Kind()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800230 fprintf(stderr, "%s:%d attempt to redefine %s as %s,\n",
231 filename, type->DeclLine(),
Christopher Wileyf690be52015-09-14 15:19:10 -0700232 type->QualifiedName().c_str(),
233 type->HumanReadableKind().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800234 fprintf(stderr, "%s:%d previously defined here as %s.\n",
Christopher Wileyf690be52015-09-14 15:19:10 -0700235 old->DeclFile().c_str(), old->DeclLine(),
236 old->HumanReadableKind().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800237 err = 1;
238 }
239 }
240
241 items = items->next;
242 }
243 return err;
244}
245
Christopher Wileyf690be52015-09-14 15:19:10 -0700246int check_method(const char* filename, method_type* m) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800247 int err = 0;
248
249 // return type
250 Type* returnType = NAMES.Search(m->type.type.data);
251 if (returnType == NULL) {
252 fprintf(stderr, "%s:%d unknown return type %s\n", filename,
253 m->type.type.lineno, m->type.type.data);
254 err = 1;
255 return err;
256 }
257
Casey Dahlin88868fc2015-09-01 13:21:26 -0700258 if (!returnType->CanWriteToParcel()) {
259 fprintf(stderr, "%s:%d return type %s can't be marshalled.\n", filename,
Adam Lesinskiffa16862014-01-23 18:17:42 -0800260 m->type.type.lineno, m->type.type.data);
Casey Dahlin88868fc2015-09-01 13:21:26 -0700261 err = 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800262 }
263
264 if (m->type.dimension > 0 && !returnType->CanBeArray()) {
265 fprintf(stderr, "%s:%d return type %s%s can't be an array.\n", filename,
266 m->type.array_token.lineno, m->type.type.data,
267 m->type.array_token.data);
268 err = 1;
269 }
270
271 if (m->type.dimension > 1) {
272 fprintf(stderr, "%s:%d return type %s%s only one"
273 " dimensional arrays are supported\n", filename,
274 m->type.array_token.lineno, m->type.type.data,
275 m->type.array_token.data);
276 err = 1;
277 }
278
279 int index = 1;
280
281 arg_type* arg = m->args;
282 while (arg) {
283 Type* t = NAMES.Search(arg->type.type.data);
284
285 // check the arg type
286 if (t == NULL) {
287 fprintf(stderr, "%s:%d parameter %s (%d) unknown type %s\n",
288 filename, m->type.type.lineno, arg->name.data, index,
289 arg->type.type.data);
290 err = 1;
291 goto next;
292 }
293
Casey Dahlin88868fc2015-09-01 13:21:26 -0700294 if (!t->CanWriteToParcel()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800295 fprintf(stderr, "%s:%d parameter %d: '%s %s' can't be marshalled.\n",
296 filename, m->type.type.lineno, index,
297 arg->type.type.data, arg->name.data);
298 err = 1;
299 }
300
Adam Lesinskiffa16862014-01-23 18:17:42 -0800301 if (arg->direction.data == NULL
302 && (arg->type.dimension != 0 || t->CanBeOutParameter())) {
303 fprintf(stderr, "%s:%d parameter %d: '%s %s' can be an out"
304 " parameter, so you must declare it as in,"
305 " out or inout.\n",
306 filename, m->type.type.lineno, index,
307 arg->type.type.data, arg->name.data);
308 err = 1;
309 }
310
311 if (convert_direction(arg->direction.data) != IN_PARAMETER
312 && !t->CanBeOutParameter()
313 && arg->type.dimension == 0) {
314 fprintf(stderr, "%s:%d parameter %d: '%s %s %s' can only be an in"
315 " parameter.\n",
316 filename, m->type.type.lineno, index,
317 arg->direction.data, arg->type.type.data,
318 arg->name.data);
319 err = 1;
320 }
321
322 if (arg->type.dimension > 0 && !t->CanBeArray()) {
323 fprintf(stderr, "%s:%d parameter %d: '%s %s%s %s' can't be an"
324 " array.\n", filename,
325 m->type.array_token.lineno, index, arg->direction.data,
326 arg->type.type.data, arg->type.array_token.data,
327 arg->name.data);
328 err = 1;
329 }
330
331 if (arg->type.dimension > 1) {
332 fprintf(stderr, "%s:%d parameter %d: '%s %s%s %s' only one"
333 " dimensional arrays are supported\n", filename,
334 m->type.array_token.lineno, index, arg->direction.data,
335 arg->type.type.data, arg->type.array_token.data,
336 arg->name.data);
337 err = 1;
338 }
339
340 // check that the name doesn't match a keyword
Christopher Wileyf690be52015-09-14 15:19:10 -0700341 if (is_java_keyword(arg->name.data)) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800342 fprintf(stderr, "%s:%d parameter %d %s is named the same as a"
343 " Java or aidl keyword\n",
344 filename, m->name.lineno, index, arg->name.data);
345 err = 1;
346 }
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700347
Adam Lesinskiffa16862014-01-23 18:17:42 -0800348next:
349 index++;
350 arg = arg->next;
351 }
352
353 return err;
354}
355
Christopher Wileyf690be52015-09-14 15:19:10 -0700356int check_types(const char* filename, document_item_type* items) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800357 int err = 0;
358 while (items) {
359 // (nothing to check for USER_DATA_TYPE)
Casey Dahlin88868fc2015-09-01 13:21:26 -0700360 if (items->item_type == INTERFACE_TYPE_BINDER) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800361 map<string,method_type*> methodNames;
362 interface_type* c = (interface_type*)items;
363
364 interface_item_type* member = c->interface_items;
365 while (member) {
366 if (member->item_type == METHOD_TYPE) {
367 method_type* m = (method_type*)member;
368
Casey Dahlin88868fc2015-09-01 13:21:26 -0700369 err |= check_method(filename, m);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800370
371 // prevent duplicate methods
372 if (methodNames.find(m->name.data) == methodNames.end()) {
373 methodNames[m->name.data] = m;
374 } else {
375 fprintf(stderr,"%s:%d attempt to redefine method %s,\n",
376 filename, m->name.lineno, m->name.data);
377 method_type* old = methodNames[m->name.data];
378 fprintf(stderr, "%s:%d previously defined here.\n",
379 filename, old->name.lineno);
380 err = 1;
381 }
382 }
383 member = member->next;
384 }
385 }
386
387 items = items->next;
388 }
389 return err;
390}
391
Christopher Wileyf690be52015-09-14 15:19:10 -0700392void generate_dep_file(const JavaOptions& options,
393 const document_item_type* items,
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700394 import_info* import_head) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800395 /* we open the file in binary mode to ensure that the same output is
396 * generated on all platforms !!
397 */
398 FILE* to = NULL;
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700399 if (options.auto_dep_file_) {
400 string fileName = options.output_file_name_ + ".d";
Adam Lesinskiffa16862014-01-23 18:17:42 -0800401 to = fopen(fileName.c_str(), "wb");
402 } else {
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700403 to = fopen(options.dep_file_name_.c_str(), "wb");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800404 }
405
406 if (to == NULL) {
407 return;
408 }
409
410 const char* slash = "\\";
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700411 import_info* import = import_head;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800412 if (import == NULL) {
413 slash = "";
414 }
415
Casey Dahlin88868fc2015-09-01 13:21:26 -0700416 if (items->item_type == INTERFACE_TYPE_BINDER) {
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700417 fprintf(to, "%s: \\\n", options.output_file_name_.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800418 } else {
419 // parcelable: there's no output file.
420 fprintf(to, " : \\\n");
421 }
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700422 fprintf(to, " %s %s\n", options.input_file_name_.c_str(), slash);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800423
424 while (import) {
425 if (import->next == NULL) {
426 slash = "";
427 }
428 if (import->filename) {
429 fprintf(to, " %s %s\n", import->filename, slash);
430 }
431 import = import->next;
432 }
433
434 fprintf(to, "\n");
435
Ying Wang0e4861a2015-07-22 17:42:35 -0700436 // Output "<input_aidl_file>: " so make won't fail if the input .aidl file
437 // has been deleted, moved or renamed in incremental build.
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700438 fprintf(to, "%s :\n", options.input_file_name_.c_str());
Ying Wang0e4861a2015-07-22 17:42:35 -0700439
Adam Lesinskiffa16862014-01-23 18:17:42 -0800440 // Output "<imported_file>: " so make won't fail if the imported file has
441 // been deleted, moved or renamed in incremental build.
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700442 import = import_head;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800443 while (import) {
444 if (import->filename) {
445 fprintf(to, "%s :\n", import->filename);
446 }
447 import = import->next;
448 }
449
450 fclose(to);
451}
452
Christopher Wileyf690be52015-09-14 15:19:10 -0700453string generate_outputFileName2(const JavaOptions& options,
454 const buffer_type& name,
455 const char* package) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800456 string result;
457
458 // create the path to the destination folder based on the
459 // interface package name
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700460 result = options.output_base_folder_;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800461 result += OS_PATH_SEPARATOR;
462
463 string packageStr = package;
464 size_t len = packageStr.length();
465 for (size_t i=0; i<len; i++) {
466 if (packageStr[i] == '.') {
467 packageStr[i] = OS_PATH_SEPARATOR;
468 }
469 }
470
471 result += packageStr;
472
473 // add the filename by replacing the .aidl extension to .java
474 const char* p = strchr(name.data, '.');
475 len = p ? p-name.data : strlen(name.data);
476
477 result += OS_PATH_SEPARATOR;
478 result.append(name.data, len);
479 result += ".java";
480
481 return result;
482}
483
Christopher Wileyf690be52015-09-14 15:19:10 -0700484string generate_outputFileName(const JavaOptions& options,
485 const document_item_type* items) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800486 // items has already been checked to have only one interface.
Casey Dahlin88868fc2015-09-01 13:21:26 -0700487 if (items->item_type == INTERFACE_TYPE_BINDER) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800488 interface_type* type = (interface_type*)items;
489
490 return generate_outputFileName2(options, type->name, type->package);
491 } else if (items->item_type == USER_DATA_TYPE) {
492 user_data_type* type = (user_data_type*)items;
493 return generate_outputFileName2(options, type->name, type->package);
494 }
495
496 // I don't think we can come here, but safer than returning NULL.
497 string result;
498 return result;
499}
500
501
Christopher Wileyf690be52015-09-14 15:19:10 -0700502void check_outputFilePath(const string& path) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800503 size_t len = path.length();
504 for (size_t i=0; i<len ; i++) {
505 if (path[i] == OS_PATH_SEPARATOR) {
506 string p = path.substr(0, i);
507 if (access(path.data(), F_OK) != 0) {
Elliott Hughes549b6e22015-08-17 12:41:46 -0700508#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800509 _mkdir(p.data());
510#else
511 mkdir(p.data(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP);
512#endif
513 }
514 }
515 }
516}
517
518
Christopher Wileyf690be52015-09-14 15:19:10 -0700519int parse_preprocessed_file(const string& filename) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800520 int err;
521
522 FILE* f = fopen(filename.c_str(), "rb");
523 if (f == NULL) {
524 fprintf(stderr, "aidl: can't open preprocessed file: %s\n",
525 filename.c_str());
526 return 1;
527 }
528
529 int lineno = 1;
530 char line[1024];
531 char type[1024];
532 char fullname[1024];
533 while (fgets(line, sizeof(line), f)) {
534 // skip comments and empty lines
535 if (!line[0] || strncmp(line, "//", 2) == 0) {
536 continue;
537 }
538
539 sscanf(line, "%s %[^; \r\n\t];", type, fullname);
540
541 char* packagename;
542 char* classname = rfind(fullname, '.');
543 if (classname != NULL) {
544 *classname = '\0';
545 classname++;
546 packagename = fullname;
547 } else {
548 classname = fullname;
549 packagename = NULL;
550 }
551
552 //printf("%s:%d:...%s...%s...%s...\n", filename.c_str(), lineno,
553 // type, packagename, classname);
554 document_item_type* doc;
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700555
Adam Lesinskiffa16862014-01-23 18:17:42 -0800556 if (0 == strcmp("parcelable", type)) {
557 user_data_type* parcl = (user_data_type*)malloc(
558 sizeof(user_data_type));
559 memset(parcl, 0, sizeof(user_data_type));
560 parcl->document_item.item_type = USER_DATA_TYPE;
561 parcl->keyword_token.lineno = lineno;
562 parcl->keyword_token.data = strdup(type);
563 parcl->package = packagename ? strdup(packagename) : NULL;
564 parcl->name.lineno = lineno;
565 parcl->name.data = strdup(classname);
566 parcl->semicolon_token.lineno = lineno;
567 parcl->semicolon_token.data = strdup(";");
Casey Dahlin88868fc2015-09-01 13:21:26 -0700568 parcl->parcelable = true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800569 doc = (document_item_type*)parcl;
570 }
571 else if (0 == strcmp("interface", type)) {
572 interface_type* iface = (interface_type*)malloc(
573 sizeof(interface_type));
574 memset(iface, 0, sizeof(interface_type));
575 iface->document_item.item_type = INTERFACE_TYPE_BINDER;
576 iface->interface_token.lineno = lineno;
577 iface->interface_token.data = strdup(type);
578 iface->package = packagename ? strdup(packagename) : NULL;
579 iface->name.lineno = lineno;
580 iface->name.data = strdup(classname);
581 iface->open_brace_token.lineno = lineno;
582 iface->open_brace_token.data = strdup("{");
583 iface->close_brace_token.lineno = lineno;
584 iface->close_brace_token.data = strdup("}");
585 doc = (document_item_type*)iface;
586 }
587 else {
588 fprintf(stderr, "%s:%d: bad type in line: %s\n",
589 filename.c_str(), lineno, line);
Elliott Hughes5cd06072013-10-29 15:25:52 -0700590 fclose(f);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800591 return 1;
592 }
593 err = gather_types(filename.c_str(), doc);
594 lineno++;
595 }
596
597 if (!feof(f)) {
598 fprintf(stderr, "%s:%d: error reading file, line to long.\n",
599 filename.c_str(), lineno);
600 return 1;
601 }
602
603 fclose(f);
604 return 0;
605}
606
Christopher Wileyf690be52015-09-14 15:19:10 -0700607int check_and_assign_method_ids(const char * filename,
608 interface_item_type* first_item) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800609 // Check whether there are any methods with manually assigned id's and any that are not.
610 // Either all method id's must be manually assigned or all of them must not.
611 // Also, check for duplicates of user set id's and that the id's are within the proper bounds.
612 set<int> usedIds;
613 interface_item_type* item = first_item;
614 bool hasUnassignedIds = false;
615 bool hasAssignedIds = false;
616 while (item != NULL) {
617 if (item->item_type == METHOD_TYPE) {
618 method_type* method_item = (method_type*)item;
619 if (method_item->hasId) {
620 hasAssignedIds = true;
621 method_item->assigned_id = atoi(method_item->id.data);
622 // Ensure that the user set id is not duplicated.
623 if (usedIds.find(method_item->assigned_id) != usedIds.end()) {
624 // We found a duplicate id, so throw an error.
625 fprintf(stderr,
626 "%s:%d Found duplicate method id (%d) for method: %s\n",
627 filename, method_item->id.lineno,
628 method_item->assigned_id, method_item->name.data);
629 return 1;
630 }
631 // Ensure that the user set id is within the appropriate limits
Christopher Wileyf690be52015-09-14 15:19:10 -0700632 if (method_item->assigned_id < kMinUserSetMethodId ||
633 method_item->assigned_id > kMaxUserSetMethodId) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800634 fprintf(stderr, "%s:%d Found out of bounds id (%d) for method: %s\n",
635 filename, method_item->id.lineno,
636 method_item->assigned_id, method_item->name.data);
637 fprintf(stderr, " Value for id must be between %d and %d inclusive.\n",
Christopher Wileyf690be52015-09-14 15:19:10 -0700638 kMinUserSetMethodId, kMaxUserSetMethodId);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800639 return 1;
640 }
641 usedIds.insert(method_item->assigned_id);
642 } else {
643 hasUnassignedIds = true;
644 }
645 if (hasAssignedIds && hasUnassignedIds) {
646 fprintf(stderr,
647 "%s: You must either assign id's to all methods or to none of them.\n",
648 filename);
649 return 1;
650 }
651 }
652 item = item->next;
653 }
654
655 // In the case that all methods have unassigned id's, set a unique id for them.
656 if (hasUnassignedIds) {
657 int newId = 0;
658 item = first_item;
659 while (item != NULL) {
660 if (item->item_type == METHOD_TYPE) {
661 method_type* method_item = (method_type*)item;
662 method_item->assigned_id = newId++;
663 }
664 item = item->next;
665 }
666 }
667
668 // success
669 return 0;
670}
671
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700672int load_and_validate_aidl(const std::vector<std::string> preprocessed_files,
673 const std::vector<std::string> import_paths,
674 const std::string& input_file_name,
675 interface_type** returned_interface,
676 import_info** returned_imports) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700677 int err = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800678
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700679 set_import_paths(import_paths);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800680
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700681 register_base_types();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800682
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700683 // import the preprocessed file
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700684 for (const string& s : preprocessed_files) {
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700685 err |= parse_preprocessed_file(s);
686 }
687 if (err != 0) {
688 return err;
689 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800690
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700691 // parse the input file
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700692 Parser p{input_file_name};
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700693 if (!p.OpenFileFromDisk() || !p.RunParser()) {
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700694 return 1;
695 }
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700696 document_item_type* parsed_doc = p.GetDocument();
697 // We could in theory declare parcelables in the same file as the interface.
698 // In practice, those parcelables would have to have the same name as
699 // the interface, since this was originally written to support Java, with its
700 // packages and names that correspond to file system structure.
701 // Since we can't have two distinct classes with the same name and package,
702 // we can't actually declare parcelables in the same file.
703 if (parsed_doc == nullptr ||
704 parsed_doc->item_type != INTERFACE_TYPE_BINDER ||
705 parsed_doc->next != nullptr) {
706 cerr << "aidl expects exactly one interface per input file";
707 err |= 1;
708 }
709 interface_type* interface = (interface_type*)parsed_doc;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700710 err |= check_filename(input_file_name.c_str(),
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700711 interface->package, &interface->name);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800712
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700713 // parse the imports of the input file
714 for (import_info* import = p.GetImports(); import; import = import->next) {
715 if (NAMES.Find(import->neededClass) != NULL) {
716 continue;
717 }
718 import->filename = find_import_file(import->neededClass);
719 if (!import->filename) {
720 cerr << import->from << ":" << import->statement.lineno
721 << ": couldn't find import for class "
722 << import->neededClass << endl;
723 err |= 1;
724 continue;
725 }
726 Parser p{import->filename};
727 if (!p.OpenFileFromDisk() || !p.RunParser() || p.GetDocument() == nullptr) {
728 cerr << "error while parsing import for class "
729 << import->neededClass << endl;
730 err |= 1;
731 continue;
732 }
733 import->doc = p.GetDocument();
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700734 err |= check_filenames(import->filename, import->doc);
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700735 }
736 if (err != 0) {
737 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700738 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800739
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700740 // gather the types that have been declared
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700741 err |= gather_types(input_file_name.c_str(), parsed_doc);
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700742 for (import_info* import = p.GetImports(); import; import = import->next) {
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700743 err |= gather_types(import->filename, import->doc);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700744 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800745
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700746 // check the referenced types in parsed_doc to make sure we've imported them
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700747 err |= check_types(input_file_name.c_str(), parsed_doc);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800748
Adam Lesinskiffa16862014-01-23 18:17:42 -0800749
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700750 // assign method ids and validate.
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700751 err |= check_and_assign_method_ids(input_file_name.c_str(),
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700752 interface->interface_items);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800753
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700754 // after this, there shouldn't be any more errors because of the
755 // input.
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700756 if (err != 0) {
757 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700758 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800759
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700760 *returned_interface = interface;
761 *returned_imports = p.GetImports();
762 return 0;
763}
764
765} // namespace
766
767int compile_aidl_to_cpp(const CppOptions& options) {
768 interface_type* interface = nullptr;
769 import_info* imports = nullptr;
770 int err = load_and_validate_aidl(std::vector<std::string>{},
771 options.ImportPaths(),
772 options.InputFileName(),
773 &interface,
774 &imports);
775 if (err != 0) {
776 return err;
777 }
778
779 // TODO(wiley) b/23600457 generate a dependency file if requested with -b
780
Christopher Wiley9a8e1d92015-09-19 10:34:33 -0700781 return (GenerateCpp(options, interface)) ? 0 : 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700782}
783
784int compile_aidl_to_java(const JavaOptions& options) {
785 interface_type* interface = nullptr;
786 import_info* imports = nullptr;
787 int err = load_and_validate_aidl(options.preprocessed_files_,
788 options.import_paths_,
789 options.input_file_name_,
790 &interface,
791 &imports);
792 if (err != 0) {
793 return err;
794 }
795 document_item_type* parsed_doc = (document_item_type*)interface;
796
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700797 string output_file_name = options.output_file_name_;
798 // if needed, generate the output file name from the base folder
799 if (output_file_name.length() == 0 &&
800 options.output_base_folder_.length() > 0) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700801 output_file_name = generate_outputFileName(options, parsed_doc);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700802 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800803
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700804 // if we were asked to, generate a make dependency file
805 // unless it's a parcelable *and* it's supposed to fail on parcelable
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700806 if (options.auto_dep_file_ || options.dep_file_name_ != "") {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800807 // make sure the folders of the output file all exists
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700808 check_outputFilePath(output_file_name);
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700809 generate_dep_file(options, parsed_doc, imports);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700810 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800811
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700812 // make sure the folders of the output file all exists
813 check_outputFilePath(output_file_name);
814
815 err = generate_java(output_file_name, options.input_file_name_.c_str(),
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700816 interface);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700817
818 return err;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800819}
820
Christopher Wileyf690be52015-09-14 15:19:10 -0700821int preprocess_aidl(const JavaOptions& options) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800822 vector<string> lines;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800823
824 // read files
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700825 int N = options.files_to_preprocess_.size();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800826 for (int i=0; i<N; i++) {
Casey Dahline2507492015-09-14 17:11:20 -0700827 Parser p{options.files_to_preprocess_[i]};
Casey Dahlin99801812015-09-15 18:19:25 -0700828 if (!p.OpenFileFromDisk())
Casey Dahline2507492015-09-14 17:11:20 -0700829 return 1;
Casey Dahlin99801812015-09-15 18:19:25 -0700830 if (!p.RunParser())
831 return 1;
Casey Dahline2507492015-09-14 17:11:20 -0700832 document_item_type* doc = p.GetDocument();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800833 string line;
834 if (doc->item_type == USER_DATA_TYPE) {
835 user_data_type* parcelable = (user_data_type*)doc;
Casey Dahlin88868fc2015-09-01 13:21:26 -0700836 if (parcelable->parcelable) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800837 line = "parcelable ";
838 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800839 if (parcelable->package) {
840 line += parcelable->package;
841 line += '.';
842 }
843 line += parcelable->name.data;
844 } else {
845 line = "interface ";
846 interface_type* iface = (interface_type*)doc;
847 if (iface->package) {
848 line += iface->package;
849 line += '.';
850 }
851 line += iface->name.data;
852 }
853 line += ";\n";
854 lines.push_back(line);
855 }
856
857 // write preprocessed file
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700858 int fd = open( options.output_file_name_.c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800859 O_RDWR|O_CREAT|O_TRUNC|O_BINARY,
Elliott Hughes549b6e22015-08-17 12:41:46 -0700860#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800861 _S_IREAD|_S_IWRITE);
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700862#else
Adam Lesinskiffa16862014-01-23 18:17:42 -0800863 S_IRUSR|S_IWUSR|S_IRGRP);
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700864#endif
Adam Lesinskiffa16862014-01-23 18:17:42 -0800865 if (fd == -1) {
866 fprintf(stderr, "aidl: could not open file for write: %s\n",
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700867 options.output_file_name_.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800868 return 1;
869 }
870
871 N = lines.size();
872 for (int i=0; i<N; i++) {
873 const string& s = lines[i];
874 int len = s.length();
875 if (len != write(fd, s.c_str(), len)) {
876 fprintf(stderr, "aidl: error writing to file %s\n",
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700877 options.output_file_name_.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800878 close(fd);
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700879 unlink(options.output_file_name_.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800880 return 1;
881 }
882 }
883
884 close(fd);
885 return 0;
886}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700887
888} // namespace android
889} // namespace aidl