blob: f596b93e507fd7f2387940e716ec828e7d0a3515 [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 Wileye3550c62015-09-29 13:26:10 -070044#include "type_cpp.h"
Christopher Wiley775fa1f2015-09-22 15:00:12 -070045#include "type_java.h"
Christopher Wiley84c1eac2015-09-23 13:29:28 -070046#include "type_namespace.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070047
Adam Lesinskiffa16862014-01-23 18:17:42 -080048#ifndef O_BINARY
49# define O_BINARY 0
50#endif
51
Christopher Wileyc16e5e72015-09-16 10:49:40 -070052using std::cerr;
53using std::endl;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070054using std::map;
55using std::set;
56using std::string;
Christopher Wiley84c1eac2015-09-23 13:29:28 -070057using std::unique_ptr;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070058using std::vector;
Adam Lesinskiffa16862014-01-23 18:17:42 -080059
Christopher Wileyf690be52015-09-14 15:19:10 -070060namespace android {
61namespace aidl {
62namespace {
Adam Lesinskiffa16862014-01-23 18:17:42 -080063
Christopher Wileyf690be52015-09-14 15:19:10 -070064// The following are gotten as the offset from the allowable id's between
65// android.os.IBinder.FIRST_CALL_TRANSACTION=1 and
66// android.os.IBinder.LAST_CALL_TRANSACTION=16777215
67const int kMinUserSetMethodId = 0;
68const int kMaxUserSetMethodId = 16777214;
Adam Lesinskiffa16862014-01-23 18:17:42 -080069
Christopher Wileyf690be52015-09-14 15:19:10 -070070int check_filename(const char* filename,
71 const char* package,
72 buffer_type* name) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080073 const char* p;
74 string expected;
75 string fn;
76 size_t len;
77 char cwd[MAXPATHLEN];
78 bool valid = false;
79
Elliott Hughesce310da2015-07-29 08:44:17 -070080#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -080081 if (isalpha(filename[0]) && filename[1] == ':'
82 && filename[2] == OS_PATH_SEPARATOR) {
83#else
84 if (filename[0] == OS_PATH_SEPARATOR) {
85#endif
86 fn = filename;
87 } else {
88 fn = getcwd(cwd, sizeof(cwd));
89 len = fn.length();
90 if (fn[len-1] != OS_PATH_SEPARATOR) {
91 fn += OS_PATH_SEPARATOR;
92 }
93 fn += filename;
94 }
95
96 if (package) {
97 expected = package;
98 expected += '.';
99 }
100
101 len = expected.length();
102 for (size_t i=0; i<len; i++) {
103 if (expected[i] == '.') {
104 expected[i] = OS_PATH_SEPARATOR;
105 }
106 }
107
108 p = strchr(name->data, '.');
109 len = p ? p-name->data : strlen(name->data);
110 expected.append(name->data, len);
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700111
Adam Lesinskiffa16862014-01-23 18:17:42 -0800112 expected += ".aidl";
113
114 len = fn.length();
115 valid = (len >= expected.length());
116
117 if (valid) {
118 p = fn.c_str() + (len - expected.length());
119
Elliott Hughesce310da2015-07-29 08:44:17 -0700120#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800121 if (OS_PATH_SEPARATOR != '/') {
122 // Input filename under cygwin most likely has / separators
123 // whereas the expected string uses \\ separators. Adjust
124 // them accordingly.
125 for (char *c = const_cast<char *>(p); *c; ++c) {
126 if (*c == '/') *c = OS_PATH_SEPARATOR;
127 }
128 }
129#endif
130
Yabin Cui482eefb2014-11-10 15:01:43 -0800131 // aidl assumes case-insensitivity on Mac Os and Windows.
132#if defined(__linux__)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800133 valid = (expected == p);
134#else
135 valid = !strcasecmp(expected.c_str(), p);
136#endif
137 }
138
139 if (!valid) {
140 fprintf(stderr, "%s:%d interface %s should be declared in a file"
141 " called %s.\n",
142 filename, name->lineno, name->data, expected.c_str());
143 return 1;
144 }
145
146 return 0;
147}
148
Christopher Wileyf690be52015-09-14 15:19:10 -0700149int check_filenames(const char* filename, document_item_type* items) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800150 int err = 0;
151 while (items) {
152 if (items->item_type == USER_DATA_TYPE) {
153 user_data_type* p = (user_data_type*)items;
154 err |= check_filename(filename, p->package, &p->name);
155 }
Casey Dahlin88868fc2015-09-01 13:21:26 -0700156 else if (items->item_type == INTERFACE_TYPE_BINDER) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800157 interface_type* c = (interface_type*)items;
158 err |= check_filename(filename, c->package, &c->name);
159 }
160 else {
161 fprintf(stderr, "aidl: internal error unkown document type %d.\n",
162 items->item_type);
163 return 1;
164 }
165 items = items->next;
166 }
167 return err;
168}
169
Christopher Wileyf690be52015-09-14 15:19:10 -0700170char* rfind(char* str, char c) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800171 char* p = str + strlen(str) - 1;
172 while (p >= str) {
173 if (*p == c) {
174 return p;
175 }
176 p--;
177 }
178 return NULL;
179}
180
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700181bool gather_types(const char* raw_filename,
182 document_item_type* all_items,
183 TypeNamespace* types) {
184 bool success = true;
185 if (raw_filename == nullptr)
186 raw_filename = "";
187 const std::string filename{raw_filename};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800188
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700189 for (document_item_type* item = all_items; item; item = item->next) {
190 if (item->item_type == USER_DATA_TYPE) {
191 user_data_type* p = (user_data_type*)item;
192 success &= types->AddParcelableType(p, filename);
193 } else if (item->item_type == INTERFACE_TYPE_BINDER) {
194 interface_type* c = (interface_type*)item;
195 success &= types->AddBinderType(c, filename);
196 } else {
197 LOG(FATAL) << "internal error";
Adam Lesinskiffa16862014-01-23 18:17:42 -0800198 }
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700199 }
200 return success;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800201}
202
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700203int check_types(const string& filename,
204 interface_type* c,
205 TypeNamespace* types) {
206 int err = 0;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700207
208 // Has to be a pointer due to deleting copy constructor. No idea why.
209 map<string, const AidlMethod*> method_names;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700210 for (const auto& m : *c->methods) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700211 if (!types->AddContainerType(m->GetType().GetName()) ||
212 !types->IsValidReturnType(m->GetType(), filename)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700213 err = 1; // return type is invalid
Adam Lesinskiffa16862014-01-23 18:17:42 -0800214 }
215
216 int index = 1;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700217 for (const auto& arg : m->GetArguments()) {
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700218 if (!types->AddContainerType(arg->GetType().GetName()) ||
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700219 !types->IsValidArg(*arg, index, filename)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700220 err = 1;
221 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800222 }
223
Casey Dahlinf4a93112015-10-05 16:58:09 -0700224 auto it = method_names.find(m->GetName());
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700225 // prevent duplicate methods
Casey Dahlinf4a93112015-10-05 16:58:09 -0700226 if (it == method_names.end()) {
227 method_names[m->GetName()] = m.get();
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700228 } else {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700229 cerr << filename << ":" << m->GetLine()
230 << " attempt to redefine method " << m->GetName() << "," << endl
231 << filename << ":" << it->second->GetLine()
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700232 << " previously defined here." << endl;
233 err = 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800234 }
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700235 }
236 return err;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800237}
238
Christopher Wileyf690be52015-09-14 15:19:10 -0700239void generate_dep_file(const JavaOptions& options,
240 const document_item_type* items,
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700241 import_info* import_head) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800242 /* we open the file in binary mode to ensure that the same output is
243 * generated on all platforms !!
244 */
245 FILE* to = NULL;
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700246 if (options.auto_dep_file_) {
247 string fileName = options.output_file_name_ + ".d";
Adam Lesinskiffa16862014-01-23 18:17:42 -0800248 to = fopen(fileName.c_str(), "wb");
249 } else {
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700250 to = fopen(options.dep_file_name_.c_str(), "wb");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800251 }
252
253 if (to == NULL) {
254 return;
255 }
256
257 const char* slash = "\\";
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700258 import_info* import = import_head;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800259 if (import == NULL) {
260 slash = "";
261 }
262
Casey Dahlin88868fc2015-09-01 13:21:26 -0700263 if (items->item_type == INTERFACE_TYPE_BINDER) {
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700264 fprintf(to, "%s: \\\n", options.output_file_name_.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800265 } else {
266 // parcelable: there's no output file.
267 fprintf(to, " : \\\n");
268 }
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700269 fprintf(to, " %s %s\n", options.input_file_name_.c_str(), slash);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800270
271 while (import) {
272 if (import->next == NULL) {
273 slash = "";
274 }
275 if (import->filename) {
276 fprintf(to, " %s %s\n", import->filename, slash);
277 }
278 import = import->next;
279 }
280
281 fprintf(to, "\n");
282
Ying Wang0e4861a2015-07-22 17:42:35 -0700283 // Output "<input_aidl_file>: " so make won't fail if the input .aidl file
284 // has been deleted, moved or renamed in incremental build.
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700285 fprintf(to, "%s :\n", options.input_file_name_.c_str());
Ying Wang0e4861a2015-07-22 17:42:35 -0700286
Adam Lesinskiffa16862014-01-23 18:17:42 -0800287 // Output "<imported_file>: " so make won't fail if the imported file has
288 // been deleted, moved or renamed in incremental build.
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700289 import = import_head;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800290 while (import) {
291 if (import->filename) {
292 fprintf(to, "%s :\n", import->filename);
293 }
294 import = import->next;
295 }
296
297 fclose(to);
298}
299
Christopher Wileyf690be52015-09-14 15:19:10 -0700300string generate_outputFileName2(const JavaOptions& options,
301 const buffer_type& name,
302 const char* package) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800303 string result;
304
305 // create the path to the destination folder based on the
306 // interface package name
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700307 result = options.output_base_folder_;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800308 result += OS_PATH_SEPARATOR;
309
310 string packageStr = package;
311 size_t len = packageStr.length();
312 for (size_t i=0; i<len; i++) {
313 if (packageStr[i] == '.') {
314 packageStr[i] = OS_PATH_SEPARATOR;
315 }
316 }
317
318 result += packageStr;
319
320 // add the filename by replacing the .aidl extension to .java
321 const char* p = strchr(name.data, '.');
322 len = p ? p-name.data : strlen(name.data);
323
324 result += OS_PATH_SEPARATOR;
325 result.append(name.data, len);
326 result += ".java";
327
328 return result;
329}
330
Christopher Wileyf690be52015-09-14 15:19:10 -0700331string generate_outputFileName(const JavaOptions& options,
332 const document_item_type* items) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800333 // items has already been checked to have only one interface.
Casey Dahlin88868fc2015-09-01 13:21:26 -0700334 if (items->item_type == INTERFACE_TYPE_BINDER) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800335 interface_type* type = (interface_type*)items;
336
337 return generate_outputFileName2(options, type->name, type->package);
338 } else if (items->item_type == USER_DATA_TYPE) {
339 user_data_type* type = (user_data_type*)items;
340 return generate_outputFileName2(options, type->name, type->package);
341 }
342
343 // I don't think we can come here, but safer than returning NULL.
344 string result;
345 return result;
346}
347
348
Christopher Wileyf690be52015-09-14 15:19:10 -0700349void check_outputFilePath(const string& path) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800350 size_t len = path.length();
351 for (size_t i=0; i<len ; i++) {
352 if (path[i] == OS_PATH_SEPARATOR) {
353 string p = path.substr(0, i);
354 if (access(path.data(), F_OK) != 0) {
Elliott Hughes549b6e22015-08-17 12:41:46 -0700355#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800356 _mkdir(p.data());
357#else
358 mkdir(p.data(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP);
359#endif
360 }
361 }
362 }
363}
364
365
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700366int parse_preprocessed_file(const string& filename, TypeNamespace* types) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800367 FILE* f = fopen(filename.c_str(), "rb");
368 if (f == NULL) {
369 fprintf(stderr, "aidl: can't open preprocessed file: %s\n",
370 filename.c_str());
371 return 1;
372 }
373
374 int lineno = 1;
375 char line[1024];
376 char type[1024];
377 char fullname[1024];
378 while (fgets(line, sizeof(line), f)) {
379 // skip comments and empty lines
380 if (!line[0] || strncmp(line, "//", 2) == 0) {
381 continue;
382 }
383
384 sscanf(line, "%s %[^; \r\n\t];", type, fullname);
385
386 char* packagename;
387 char* classname = rfind(fullname, '.');
388 if (classname != NULL) {
389 *classname = '\0';
390 classname++;
391 packagename = fullname;
392 } else {
393 classname = fullname;
394 packagename = NULL;
395 }
396
397 //printf("%s:%d:...%s...%s...%s...\n", filename.c_str(), lineno,
398 // type, packagename, classname);
399 document_item_type* doc;
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700400
Adam Lesinskiffa16862014-01-23 18:17:42 -0800401 if (0 == strcmp("parcelable", type)) {
Casey Dahlin030977a2015-09-29 11:29:35 -0700402 user_data_type* parcl = new user_data_type();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800403 memset(parcl, 0, sizeof(user_data_type));
404 parcl->document_item.item_type = USER_DATA_TYPE;
405 parcl->keyword_token.lineno = lineno;
Casey Dahlin030977a2015-09-29 11:29:35 -0700406 parcl->keyword_token.data = cpp_strdup(type);
407 parcl->package = packagename ? cpp_strdup(packagename) : NULL;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800408 parcl->name.lineno = lineno;
Casey Dahlin030977a2015-09-29 11:29:35 -0700409 parcl->name.data = cpp_strdup(classname);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800410 parcl->semicolon_token.lineno = lineno;
Casey Dahlin030977a2015-09-29 11:29:35 -0700411 parcl->semicolon_token.data = cpp_strdup(";");
Casey Dahlin88868fc2015-09-01 13:21:26 -0700412 parcl->parcelable = true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800413 doc = (document_item_type*)parcl;
414 }
415 else if (0 == strcmp("interface", type)) {
Casey Dahlin030977a2015-09-29 11:29:35 -0700416 interface_type* iface = new interface_type();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800417 memset(iface, 0, sizeof(interface_type));
418 iface->document_item.item_type = INTERFACE_TYPE_BINDER;
419 iface->interface_token.lineno = lineno;
Casey Dahlin030977a2015-09-29 11:29:35 -0700420 iface->interface_token.data = cpp_strdup(type);
421 iface->package = packagename ? cpp_strdup(packagename) : NULL;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800422 iface->name.lineno = lineno;
Casey Dahlin030977a2015-09-29 11:29:35 -0700423 iface->name.data = cpp_strdup(classname);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800424 iface->open_brace_token.lineno = lineno;
Casey Dahlin030977a2015-09-29 11:29:35 -0700425 iface->open_brace_token.data = cpp_strdup("{");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800426 iface->close_brace_token.lineno = lineno;
Casey Dahlin030977a2015-09-29 11:29:35 -0700427 iface->close_brace_token.data = cpp_strdup("}");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800428 doc = (document_item_type*)iface;
429 }
430 else {
431 fprintf(stderr, "%s:%d: bad type in line: %s\n",
432 filename.c_str(), lineno, line);
Elliott Hughes5cd06072013-10-29 15:25:52 -0700433 fclose(f);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800434 return 1;
435 }
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700436 if (!gather_types(filename.c_str(), doc, types)) {
437 fprintf(stderr, "Failed to gather types for preprocessed aidl.\n");
438 fclose(f);
439 return 1;
440 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800441 lineno++;
442 }
443
444 if (!feof(f)) {
445 fprintf(stderr, "%s:%d: error reading file, line to long.\n",
446 filename.c_str(), lineno);
447 return 1;
448 }
449
450 fclose(f);
451 return 0;
452}
453
Christopher Wileyf690be52015-09-14 15:19:10 -0700454int check_and_assign_method_ids(const char * filename,
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700455 const std::vector<std::unique_ptr<AidlMethod>>& items) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800456 // Check whether there are any methods with manually assigned id's and any that are not.
457 // Either all method id's must be manually assigned or all of them must not.
458 // Also, check for duplicates of user set id's and that the id's are within the proper bounds.
459 set<int> usedIds;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800460 bool hasUnassignedIds = false;
461 bool hasAssignedIds = false;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700462 for (const auto& item : items) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700463 if (item->HasId()) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700464 hasAssignedIds = true;
Casey Dahlindff80e52015-09-29 13:57:06 -0700465 // Ensure that the user set id is not duplicated.
Casey Dahlinf4a93112015-10-05 16:58:09 -0700466 if (usedIds.find(item->GetId()) != usedIds.end()) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700467 // We found a duplicate id, so throw an error.
Adam Lesinskiffa16862014-01-23 18:17:42 -0800468 fprintf(stderr,
Casey Dahlindff80e52015-09-29 13:57:06 -0700469 "%s:%d Found duplicate method id (%d) for method: %s\n",
Casey Dahlinf4a93112015-10-05 16:58:09 -0700470 filename, item->GetLine(),
471 item->GetId(), item->GetName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800472 return 1;
473 }
Casey Dahlindff80e52015-09-29 13:57:06 -0700474 // Ensure that the user set id is within the appropriate limits
Casey Dahlinf4a93112015-10-05 16:58:09 -0700475 if (item->GetId() < kMinUserSetMethodId ||
476 item->GetId() > kMaxUserSetMethodId) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700477 fprintf(stderr, "%s:%d Found out of bounds id (%d) for method: %s\n",
Casey Dahlinf4a93112015-10-05 16:58:09 -0700478 filename, item->GetLine(),
479 item->GetId(), item->GetName().c_str());
Casey Dahlindff80e52015-09-29 13:57:06 -0700480 fprintf(stderr, " Value for id must be between %d and %d inclusive.\n",
481 kMinUserSetMethodId, kMaxUserSetMethodId);
482 return 1;
483 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700484 usedIds.insert(item->GetId());
Casey Dahlindff80e52015-09-29 13:57:06 -0700485 } else {
486 hasUnassignedIds = true;
487 }
488 if (hasAssignedIds && hasUnassignedIds) {
489 fprintf(stderr,
490 "%s: You must either assign id's to all methods or to none of them.\n",
491 filename);
492 return 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800493 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800494 }
495
496 // In the case that all methods have unassigned id's, set a unique id for them.
497 if (hasUnassignedIds) {
498 int newId = 0;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700499 for (const auto& item : items) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700500 item->SetId(newId++);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800501 }
502 }
503
504 // success
505 return 0;
506}
507
Casey Dahlin2cc93162015-10-02 16:14:17 -0700508int load_and_validate_aidl_internal(const std::vector<std::string> preprocessed_files,
509 const std::vector<std::string> import_paths,
510 const std::string& input_file_name,
511 TypeNamespace* types,
512 interface_type** returned_interface,
513 import_info** returned_imports,
514 bool use_data,
515 std::string data) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700516 int err = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800517
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700518 set_import_paths(import_paths);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800519
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700520 // import the preprocessed file
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700521 for (const string& s : preprocessed_files) {
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700522 err |= parse_preprocessed_file(s, types);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700523 }
524 if (err != 0) {
525 return err;
526 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800527
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700528 // parse the input file
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700529 Parser p{input_file_name};
Casey Dahlin2cc93162015-10-02 16:14:17 -0700530 if (use_data)
531 p.SetFileContents(data);
532 else if (!p.OpenFileFromDisk())
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700533 return 1;
Casey Dahlin2cc93162015-10-02 16:14:17 -0700534
535 if (!p.RunParser())
536 return 1;
537
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700538 document_item_type* parsed_doc = p.GetDocument();
539 // We could in theory declare parcelables in the same file as the interface.
540 // In practice, those parcelables would have to have the same name as
541 // the interface, since this was originally written to support Java, with its
542 // packages and names that correspond to file system structure.
543 // Since we can't have two distinct classes with the same name and package,
544 // we can't actually declare parcelables in the same file.
545 if (parsed_doc == nullptr ||
546 parsed_doc->item_type != INTERFACE_TYPE_BINDER ||
547 parsed_doc->next != nullptr) {
548 cerr << "aidl expects exactly one interface per input file";
Christopher Wileya2f516d2015-09-24 10:12:31 -0700549 return 1;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700550 }
551 interface_type* interface = (interface_type*)parsed_doc;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700552 err |= check_filename(input_file_name.c_str(),
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700553 interface->package, &interface->name);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800554
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700555 // parse the imports of the input file
556 for (import_info* import = p.GetImports(); import; import = import->next) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700557 if (types->HasType(import->neededClass)) {
558 // There are places in the Android tree where an import doesn't resolve,
559 // but we'll pick the type up through the preprocessed types.
560 // This seems like an error, but legacy support demands we support it...
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700561 continue;
562 }
563 import->filename = find_import_file(import->neededClass);
564 if (!import->filename) {
565 cerr << import->from << ":" << import->statement.lineno
566 << ": couldn't find import for class "
567 << import->neededClass << endl;
568 err |= 1;
569 continue;
570 }
Casey Dahlin2cc93162015-10-02 16:14:17 -0700571
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700572 Parser p{import->filename};
Casey Dahlin2cc93162015-10-02 16:14:17 -0700573
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700574 if (!p.OpenFileFromDisk() || !p.RunParser() || p.GetDocument() == nullptr) {
575 cerr << "error while parsing import for class "
576 << import->neededClass << endl;
577 err |= 1;
578 continue;
579 }
580 import->doc = p.GetDocument();
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700581 err |= check_filenames(import->filename, import->doc);
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700582 }
583 if (err != 0) {
584 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700585 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800586
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700587 // gather the types that have been declared
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700588 if (!gather_types(input_file_name.c_str(), parsed_doc, types)) {
589 err |= 1;
590 }
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700591 for (import_info* import = p.GetImports(); import; import = import->next) {
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700592 if (!gather_types(import->filename, import->doc, types)) {
593 err |= 1;
594 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700595 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800596
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700597 // check the referenced types in parsed_doc to make sure we've imported them
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700598 err |= check_types(input_file_name, interface, types);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800599
Adam Lesinskiffa16862014-01-23 18:17:42 -0800600
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700601 // assign method ids and validate.
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700602 err |= check_and_assign_method_ids(input_file_name.c_str(),
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700603 *interface->methods);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800604
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700605 // after this, there shouldn't be any more errors because of the
606 // input.
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700607 if (err != 0) {
608 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700609 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800610
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700611 *returned_interface = interface;
612 *returned_imports = p.GetImports();
613 return 0;
614}
615
Casey Dahlin2cc93162015-10-02 16:14:17 -0700616int load_and_validate_aidl(const std::vector<std::string> preprocessed_files,
617 const std::vector<std::string> import_paths,
618 const std::string& input_file_name,
619 TypeNamespace* types,
620 interface_type** returned_interface,
621 import_info** returned_imports) {
622 return load_and_validate_aidl_internal(preprocessed_files,
623 import_paths,
624 input_file_name,
625 types,
626 returned_interface,
627 returned_imports,
628 false,
629 "");
630}
631
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700632} // namespace
633
Casey Dahlin2cc93162015-10-02 16:14:17 -0700634namespace internals {
635
636int load_aidl_for_test(const std::string& input_file_name,
637 const std::string& data,
638 TypeNamespace* types,
639 interface_type** returned_interface) {
640 import_info *throwaway;
641 int ret = load_and_validate_aidl_internal({}, {}, input_file_name, types,
642 returned_interface, &throwaway,
643 true, data);
644 return ret;
645}
646
647} // namespace internals
648
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700649int compile_aidl_to_cpp(const CppOptions& options) {
650 interface_type* interface = nullptr;
651 import_info* imports = nullptr;
Christopher Wileye3550c62015-09-29 13:26:10 -0700652 unique_ptr<cpp::TypeNamespace> types(new cpp::TypeNamespace());
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700653 int err = load_and_validate_aidl(std::vector<std::string>{},
654 options.ImportPaths(),
655 options.InputFileName(),
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700656 types.get(),
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700657 &interface,
658 &imports);
659 if (err != 0) {
660 return err;
661 }
662
663 // TODO(wiley) b/23600457 generate a dependency file if requested with -b
664
Christopher Wileye3550c62015-09-29 13:26:10 -0700665 return (cpp::GenerateCpp(options, *types, *interface)) ? 0 : 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700666}
667
668int compile_aidl_to_java(const JavaOptions& options) {
669 interface_type* interface = nullptr;
670 import_info* imports = nullptr;
Christopher Wileydb154a52015-09-28 16:32:25 -0700671 unique_ptr<java::JavaTypeNamespace> types(new java::JavaTypeNamespace());
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700672 int err = load_and_validate_aidl(options.preprocessed_files_,
673 options.import_paths_,
674 options.input_file_name_,
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700675 types.get(),
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700676 &interface,
677 &imports);
678 if (err != 0) {
679 return err;
680 }
681 document_item_type* parsed_doc = (document_item_type*)interface;
682
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700683 string output_file_name = options.output_file_name_;
684 // if needed, generate the output file name from the base folder
685 if (output_file_name.length() == 0 &&
686 options.output_base_folder_.length() > 0) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700687 output_file_name = generate_outputFileName(options, parsed_doc);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700688 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800689
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700690 // if we were asked to, generate a make dependency file
691 // unless it's a parcelable *and* it's supposed to fail on parcelable
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700692 if (options.auto_dep_file_ || options.dep_file_name_ != "") {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800693 // make sure the folders of the output file all exists
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700694 check_outputFilePath(output_file_name);
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700695 generate_dep_file(options, parsed_doc, imports);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700696 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800697
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700698 // make sure the folders of the output file all exists
699 check_outputFilePath(output_file_name);
700
701 err = generate_java(output_file_name, options.input_file_name_.c_str(),
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700702 interface, types.get());
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700703
704 return err;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800705}
706
Christopher Wileyf690be52015-09-14 15:19:10 -0700707int preprocess_aidl(const JavaOptions& options) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800708 vector<string> lines;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800709
710 // read files
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700711 int N = options.files_to_preprocess_.size();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800712 for (int i=0; i<N; i++) {
Casey Dahline2507492015-09-14 17:11:20 -0700713 Parser p{options.files_to_preprocess_[i]};
Casey Dahlin99801812015-09-15 18:19:25 -0700714 if (!p.OpenFileFromDisk())
Casey Dahline2507492015-09-14 17:11:20 -0700715 return 1;
Casey Dahlin99801812015-09-15 18:19:25 -0700716 if (!p.RunParser())
717 return 1;
Casey Dahline2507492015-09-14 17:11:20 -0700718 document_item_type* doc = p.GetDocument();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800719 string line;
720 if (doc->item_type == USER_DATA_TYPE) {
721 user_data_type* parcelable = (user_data_type*)doc;
Casey Dahlin88868fc2015-09-01 13:21:26 -0700722 if (parcelable->parcelable) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800723 line = "parcelable ";
724 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800725 if (parcelable->package) {
726 line += parcelable->package;
727 line += '.';
728 }
729 line += parcelable->name.data;
730 } else {
731 line = "interface ";
732 interface_type* iface = (interface_type*)doc;
733 if (iface->package) {
734 line += iface->package;
735 line += '.';
736 }
737 line += iface->name.data;
738 }
739 line += ";\n";
740 lines.push_back(line);
741 }
742
743 // write preprocessed file
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700744 int fd = open( options.output_file_name_.c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800745 O_RDWR|O_CREAT|O_TRUNC|O_BINARY,
Elliott Hughes549b6e22015-08-17 12:41:46 -0700746#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800747 _S_IREAD|_S_IWRITE);
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700748#else
Adam Lesinskiffa16862014-01-23 18:17:42 -0800749 S_IRUSR|S_IWUSR|S_IRGRP);
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700750#endif
Adam Lesinskiffa16862014-01-23 18:17:42 -0800751 if (fd == -1) {
752 fprintf(stderr, "aidl: could not open file for write: %s\n",
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700753 options.output_file_name_.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800754 return 1;
755 }
756
757 N = lines.size();
758 for (int i=0; i<N; i++) {
759 const string& s = lines[i];
760 int len = s.length();
761 if (len != write(fd, s.c_str(), len)) {
762 fprintf(stderr, "aidl: error writing to file %s\n",
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700763 options.output_file_name_.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800764 close(fd);
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700765 unlink(options.output_file_name_.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800766 return 1;
767 }
768 }
769
770 close(fd);
771 return 0;
772}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700773
774} // namespace android
775} // namespace aidl