blob: cae1e81516137aeec05851666c0cb187c690f5fa [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"
Christopher Wiley72877ac2015-10-06 14:41:42 -070039#include "import_resolver.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070040#include "logging.h"
41#include "options.h"
42#include "os.h"
43#include "parse_helpers.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
Casey Dahlin42727f82015-10-12 19:23:40 -070070bool check_filename(const std::string& filename,
71 const std::string& package,
72 const std::string& name,
73 unsigned line) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080074 const char* p;
75 string expected;
76 string fn;
77 size_t len;
78 char cwd[MAXPATHLEN];
79 bool valid = false;
80
Elliott Hughesce310da2015-07-29 08:44:17 -070081#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -080082 if (isalpha(filename[0]) && filename[1] == ':'
83 && filename[2] == OS_PATH_SEPARATOR) {
84#else
85 if (filename[0] == OS_PATH_SEPARATOR) {
86#endif
87 fn = filename;
88 } else {
89 fn = getcwd(cwd, sizeof(cwd));
90 len = fn.length();
91 if (fn[len-1] != OS_PATH_SEPARATOR) {
92 fn += OS_PATH_SEPARATOR;
93 }
94 fn += filename;
95 }
96
Casey Dahlinfb7da2e2015-10-08 17:26:09 -070097 if (!package.empty()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080098 expected = package;
99 expected += '.';
100 }
101
102 len = expected.length();
103 for (size_t i=0; i<len; i++) {
104 if (expected[i] == '.') {
105 expected[i] = OS_PATH_SEPARATOR;
106 }
107 }
108
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700109 expected.append(name, 0, name.find('.'));
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700110
Adam Lesinskiffa16862014-01-23 18:17:42 -0800111 expected += ".aidl";
112
113 len = fn.length();
114 valid = (len >= expected.length());
115
116 if (valid) {
117 p = fn.c_str() + (len - expected.length());
118
Elliott Hughesce310da2015-07-29 08:44:17 -0700119#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800120 if (OS_PATH_SEPARATOR != '/') {
121 // Input filename under cygwin most likely has / separators
122 // whereas the expected string uses \\ separators. Adjust
123 // them accordingly.
124 for (char *c = const_cast<char *>(p); *c; ++c) {
125 if (*c == '/') *c = OS_PATH_SEPARATOR;
126 }
127 }
128#endif
129
Yabin Cui482eefb2014-11-10 15:01:43 -0800130 // aidl assumes case-insensitivity on Mac Os and Windows.
131#if defined(__linux__)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800132 valid = (expected == p);
133#else
134 valid = !strcasecmp(expected.c_str(), p);
135#endif
136 }
137
138 if (!valid) {
139 fprintf(stderr, "%s:%d interface %s should be declared in a file"
140 " called %s.\n",
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700141 filename.c_str(), line, name.c_str(), expected.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800142 }
143
Casey Dahlin42727f82015-10-12 19:23:40 -0700144 return valid;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800145}
146
Casey Dahlin42727f82015-10-12 19:23:40 -0700147bool check_filenames(const std::string& filename, const AidlDocumentItem* items) {
148 if (! items)
149 return true;
150
151 if (items->item_type == INTERFACE_TYPE_BINDER) {
152 const AidlInterface* c = reinterpret_cast<const AidlInterface*>(items);
153 return check_filename(filename, c->GetPackage(), c->GetName(), c->GetLine());
154 }
155
156 bool success = true;
157
158 for (const AidlParcelable* p = reinterpret_cast<const AidlParcelable*>(items);
159 p; p = p->next)
160 success &= check_filename(filename, p->GetPackage(), p->GetName(),
161 p->GetLine());
162
163 return success;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800164}
165
Casey Dahlin0edf3422015-10-07 12:34:59 -0700166bool gather_types(const std::string& filename,
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700167 const AidlDocumentItem* all_items,
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700168 TypeNamespace* types) {
169 bool success = true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800170
Casey Dahlin42727f82015-10-12 19:23:40 -0700171 if (! all_items)
172 return true;
173
174 if (all_items->item_type == INTERFACE_TYPE_BINDER)
175 return types->AddBinderType(reinterpret_cast<const AidlInterface *>(all_items), filename);
176
177 for (const AidlParcelable* item =
178 reinterpret_cast<const AidlParcelable *>(all_items);
179 item; item = item->next) {
180 success &= types->AddParcelableType(item, filename);
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700181 }
Casey Dahlin42727f82015-10-12 19:23:40 -0700182
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700183 return success;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800184}
185
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700186int check_types(const string& filename,
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700187 AidlInterface* c,
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700188 TypeNamespace* types) {
189 int err = 0;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700190
191 // Has to be a pointer due to deleting copy constructor. No idea why.
192 map<string, const AidlMethod*> method_names;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700193 for (const auto& m : c->GetMethods()) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700194 if (!types->AddContainerType(m->GetType().GetName()) ||
195 !types->IsValidReturnType(m->GetType(), filename)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700196 err = 1; // return type is invalid
Adam Lesinskiffa16862014-01-23 18:17:42 -0800197 }
198
199 int index = 1;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700200 for (const auto& arg : m->GetArguments()) {
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700201 if (!types->AddContainerType(arg->GetType().GetName()) ||
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700202 !types->IsValidArg(*arg, index, filename)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700203 err = 1;
204 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800205 }
206
Casey Dahlinf4a93112015-10-05 16:58:09 -0700207 auto it = method_names.find(m->GetName());
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700208 // prevent duplicate methods
Casey Dahlinf4a93112015-10-05 16:58:09 -0700209 if (it == method_names.end()) {
210 method_names[m->GetName()] = m.get();
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700211 } else {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700212 cerr << filename << ":" << m->GetLine()
213 << " attempt to redefine method " << m->GetName() << "," << endl
214 << filename << ":" << it->second->GetLine()
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700215 << " previously defined here." << endl;
216 err = 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800217 }
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700218 }
219 return err;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800220}
221
Christopher Wileyf690be52015-09-14 15:19:10 -0700222void generate_dep_file(const JavaOptions& options,
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700223 const AidlDocumentItem* items,
Casey Dahlin0edf3422015-10-07 12:34:59 -0700224 const std::vector<std::unique_ptr<AidlImport>>& imports) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800225 /* we open the file in binary mode to ensure that the same output is
226 * generated on all platforms !!
227 */
228 FILE* to = NULL;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700229 string fileName;
230
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700231 if (options.auto_dep_file_) {
Casey Dahlin0edf3422015-10-07 12:34:59 -0700232 fileName = options.output_file_name_ + ".d";
Adam Lesinskiffa16862014-01-23 18:17:42 -0800233 } else {
Casey Dahlin0edf3422015-10-07 12:34:59 -0700234 fileName = options.dep_file_name_;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800235 }
236
Casey Dahlin0edf3422015-10-07 12:34:59 -0700237 string output_file_name;
238
239 // TODO: Mock IO and remove this weird stuff (b/24816077)
240 if (!options.output_file_name_for_deps_test_.empty())
241 output_file_name = options.output_file_name_for_deps_test_;
242 else
243 output_file_name = options.output_file_name_;
244
245 to = fopen(fileName.c_str(), "wb");
246
Adam Lesinskiffa16862014-01-23 18:17:42 -0800247 if (to == NULL) {
Casey Dahlin0edf3422015-10-07 12:34:59 -0700248 cerr << "Could not open " << fileName << endl;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800249 return;
250 }
251
Casey Dahlin88868fc2015-09-01 13:21:26 -0700252 if (items->item_type == INTERFACE_TYPE_BINDER) {
Casey Dahlin0edf3422015-10-07 12:34:59 -0700253 fprintf(to, "%s: \\\n", output_file_name.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800254 } else {
255 // parcelable: there's no output file.
256 fprintf(to, " : \\\n");
257 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700258 fprintf(to, " %s %s\n", options.input_file_name_.c_str(), imports.empty() ? "" : "\\");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800259
Casey Dahlin0edf3422015-10-07 12:34:59 -0700260 bool first = true;
261 for (const auto& import : imports) {
262 if (! first) {
263 fprintf(to, " \\\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800264 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700265 first = false;
266
267 if (! import->GetFilename().empty()) {
268 fprintf(to, " %s", import->GetFilename().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800269 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800270 }
271
Casey Dahlin0edf3422015-10-07 12:34:59 -0700272 fprintf(to, first ? "\n" : "\n\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800273
Ying Wang0e4861a2015-07-22 17:42:35 -0700274 // Output "<input_aidl_file>: " so make won't fail if the input .aidl file
275 // has been deleted, moved or renamed in incremental build.
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700276 fprintf(to, "%s :\n", options.input_file_name_.c_str());
Ying Wang0e4861a2015-07-22 17:42:35 -0700277
Adam Lesinskiffa16862014-01-23 18:17:42 -0800278 // Output "<imported_file>: " so make won't fail if the imported file has
279 // been deleted, moved or renamed in incremental build.
Casey Dahlin0edf3422015-10-07 12:34:59 -0700280 for (const auto& import : imports) {
281 if (! import->GetFilename().empty()) {
282 fprintf(to, "%s :\n", import->GetFilename().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800283 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800284 }
285
286 fclose(to);
287}
288
Christopher Wileyf690be52015-09-14 15:19:10 -0700289string generate_outputFileName2(const JavaOptions& options,
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700290 const std::string& name,
291 const std::string& package) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800292 string result;
293
294 // create the path to the destination folder based on the
295 // interface package name
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700296 result = options.output_base_folder_;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800297 result += OS_PATH_SEPARATOR;
298
299 string packageStr = package;
300 size_t len = packageStr.length();
301 for (size_t i=0; i<len; i++) {
302 if (packageStr[i] == '.') {
303 packageStr[i] = OS_PATH_SEPARATOR;
304 }
305 }
306
307 result += packageStr;
308
309 // add the filename by replacing the .aidl extension to .java
Adam Lesinskiffa16862014-01-23 18:17:42 -0800310 result += OS_PATH_SEPARATOR;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700311 result.append(name, 0, name.find('.'));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800312 result += ".java";
313
314 return result;
315}
316
Christopher Wileyf690be52015-09-14 15:19:10 -0700317string generate_outputFileName(const JavaOptions& options,
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700318 const AidlDocumentItem* items) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800319 // items has already been checked to have only one interface.
Casey Dahlin88868fc2015-09-01 13:21:26 -0700320 if (items->item_type == INTERFACE_TYPE_BINDER) {
Casey Dahlin5ac90202015-10-09 15:16:43 -0700321 const AidlInterface* type = reinterpret_cast<const AidlInterface*>(items);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800322
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700323 return generate_outputFileName2(options, type->GetName(), type->GetPackage());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800324 } else if (items->item_type == USER_DATA_TYPE) {
Casey Dahlin5ac90202015-10-09 15:16:43 -0700325 const AidlParcelable* type = reinterpret_cast<const AidlParcelable*>(items);
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700326
Casey Dahlin59401da2015-10-09 18:16:45 -0700327 return generate_outputFileName2(options, type->GetName(), type->GetPackage());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800328 }
329
330 // I don't think we can come here, but safer than returning NULL.
331 string result;
332 return result;
333}
334
335
Christopher Wileyf690be52015-09-14 15:19:10 -0700336void check_outputFilePath(const string& path) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800337 size_t len = path.length();
338 for (size_t i=0; i<len ; i++) {
339 if (path[i] == OS_PATH_SEPARATOR) {
340 string p = path.substr(0, i);
341 if (access(path.data(), F_OK) != 0) {
Elliott Hughes549b6e22015-08-17 12:41:46 -0700342#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800343 _mkdir(p.data());
344#else
345 mkdir(p.data(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP);
346#endif
347 }
348 }
349 }
350}
351
352
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700353int parse_preprocessed_file(const string& filename, TypeNamespace* types) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800354 FILE* f = fopen(filename.c_str(), "rb");
355 if (f == NULL) {
356 fprintf(stderr, "aidl: can't open preprocessed file: %s\n",
357 filename.c_str());
358 return 1;
359 }
360
361 int lineno = 1;
362 char line[1024];
363 char type[1024];
364 char fullname[1024];
365 while (fgets(line, sizeof(line), f)) {
366 // skip comments and empty lines
367 if (!line[0] || strncmp(line, "//", 2) == 0) {
368 continue;
369 }
370
371 sscanf(line, "%s %[^; \r\n\t];", type, fullname);
372
373 char* packagename;
Casey Dahlin624358c2015-10-12 19:29:51 -0700374 char* classname = strrchr(fullname, '.');
Adam Lesinskiffa16862014-01-23 18:17:42 -0800375 if (classname != NULL) {
376 *classname = '\0';
377 classname++;
378 packagename = fullname;
379 } else {
380 classname = fullname;
381 packagename = NULL;
382 }
383
384 //printf("%s:%d:...%s...%s...%s...\n", filename.c_str(), lineno,
385 // type, packagename, classname);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700386 AidlDocumentItem* doc;
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700387
Adam Lesinskiffa16862014-01-23 18:17:42 -0800388 if (0 == strcmp("parcelable", type)) {
Casey Dahlin59401da2015-10-09 18:16:45 -0700389 AidlParcelable* parcl = new AidlParcelable(classname, lineno, packagename);
Casey Dahlin5ac90202015-10-09 15:16:43 -0700390 doc = reinterpret_cast<AidlDocumentItem*>(parcl);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800391 }
392 else if (0 == strcmp("interface", type)) {
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700393 auto temp = new std::vector<std::unique_ptr<AidlMethod>>();
394 AidlInterface* iface = new AidlInterface(classname, lineno, "",
395 false, temp,
396 packagename ?: "");
Casey Dahlin5ac90202015-10-09 15:16:43 -0700397 doc = reinterpret_cast<AidlDocumentItem*>(iface);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800398 }
399 else {
400 fprintf(stderr, "%s:%d: bad type in line: %s\n",
401 filename.c_str(), lineno, line);
Elliott Hughes5cd06072013-10-29 15:25:52 -0700402 fclose(f);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800403 return 1;
404 }
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700405 if (!gather_types(filename.c_str(), doc, types)) {
406 fprintf(stderr, "Failed to gather types for preprocessed aidl.\n");
407 fclose(f);
408 return 1;
409 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800410 lineno++;
411 }
412
413 if (!feof(f)) {
414 fprintf(stderr, "%s:%d: error reading file, line to long.\n",
415 filename.c_str(), lineno);
416 return 1;
417 }
418
419 fclose(f);
420 return 0;
421}
422
Christopher Wileyf690be52015-09-14 15:19:10 -0700423int check_and_assign_method_ids(const char * filename,
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700424 const std::vector<std::unique_ptr<AidlMethod>>& items) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800425 // Check whether there are any methods with manually assigned id's and any that are not.
426 // Either all method id's must be manually assigned or all of them must not.
427 // Also, check for duplicates of user set id's and that the id's are within the proper bounds.
428 set<int> usedIds;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800429 bool hasUnassignedIds = false;
430 bool hasAssignedIds = false;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700431 for (const auto& item : items) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700432 if (item->HasId()) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700433 hasAssignedIds = true;
Casey Dahlindff80e52015-09-29 13:57:06 -0700434 // Ensure that the user set id is not duplicated.
Casey Dahlinf4a93112015-10-05 16:58:09 -0700435 if (usedIds.find(item->GetId()) != usedIds.end()) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700436 // We found a duplicate id, so throw an error.
Adam Lesinskiffa16862014-01-23 18:17:42 -0800437 fprintf(stderr,
Casey Dahlindff80e52015-09-29 13:57:06 -0700438 "%s:%d Found duplicate method id (%d) for method: %s\n",
Casey Dahlinf4a93112015-10-05 16:58:09 -0700439 filename, item->GetLine(),
440 item->GetId(), item->GetName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800441 return 1;
442 }
Casey Dahlindff80e52015-09-29 13:57:06 -0700443 // Ensure that the user set id is within the appropriate limits
Casey Dahlinf4a93112015-10-05 16:58:09 -0700444 if (item->GetId() < kMinUserSetMethodId ||
445 item->GetId() > kMaxUserSetMethodId) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700446 fprintf(stderr, "%s:%d Found out of bounds id (%d) for method: %s\n",
Casey Dahlinf4a93112015-10-05 16:58:09 -0700447 filename, item->GetLine(),
448 item->GetId(), item->GetName().c_str());
Casey Dahlindff80e52015-09-29 13:57:06 -0700449 fprintf(stderr, " Value for id must be between %d and %d inclusive.\n",
450 kMinUserSetMethodId, kMaxUserSetMethodId);
451 return 1;
452 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700453 usedIds.insert(item->GetId());
Casey Dahlindff80e52015-09-29 13:57:06 -0700454 } else {
455 hasUnassignedIds = true;
456 }
457 if (hasAssignedIds && hasUnassignedIds) {
458 fprintf(stderr,
459 "%s: You must either assign id's to all methods or to none of them.\n",
460 filename);
461 return 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800462 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800463 }
464
465 // In the case that all methods have unassigned id's, set a unique id for them.
466 if (hasUnassignedIds) {
467 int newId = 0;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700468 for (const auto& item : items) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700469 item->SetId(newId++);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800470 }
471 }
472
473 // success
474 return 0;
475}
476
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700477} // namespace
478
479namespace internals {
480
481int load_and_validate_aidl(const std::vector<std::string> preprocessed_files,
482 const std::vector<std::string> import_paths,
483 const std::string& input_file_name,
484 const IoDelegate& io_delegate,
485 TypeNamespace* types,
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700486 AidlInterface** returned_interface,
Casey Dahlin0edf3422015-10-07 12:34:59 -0700487 std::vector<std::unique_ptr<AidlImport>>* returned_imports) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700488 int err = 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800489
Casey Dahlin624358c2015-10-12 19:29:51 -0700490 std::map<AidlImport*,std::unique_ptr<AidlDocumentItem>> docs;
491
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700492 // import the preprocessed file
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700493 for (const string& s : preprocessed_files) {
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700494 err |= parse_preprocessed_file(s, types);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700495 }
496 if (err != 0) {
497 return err;
498 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800499
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700500 // parse the input file
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700501 Parser p{io_delegate};
502 if (!p.ParseFile(input_file_name)) {
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700503 return 1;
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700504 }
Casey Dahlin2cc93162015-10-02 16:14:17 -0700505
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700506 AidlDocumentItem* parsed_doc = p.GetDocument();
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700507 // We could in theory declare parcelables in the same file as the interface.
508 // In practice, those parcelables would have to have the same name as
509 // the interface, since this was originally written to support Java, with its
510 // packages and names that correspond to file system structure.
511 // Since we can't have two distinct classes with the same name and package,
512 // we can't actually declare parcelables in the same file.
513 if (parsed_doc == nullptr ||
Casey Dahlin42727f82015-10-12 19:23:40 -0700514 parsed_doc->item_type != INTERFACE_TYPE_BINDER) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700515 cerr << "aidl expects exactly one interface per input file";
Christopher Wileya2f516d2015-09-24 10:12:31 -0700516 return 1;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700517 }
Casey Dahlin5ac90202015-10-09 15:16:43 -0700518 AidlInterface* interface = reinterpret_cast<AidlInterface*>(parsed_doc);
Casey Dahlin42727f82015-10-12 19:23:40 -0700519 if (!check_filename(input_file_name.c_str(), interface->GetPackage(),
520 interface->GetName(), interface->GetLine()))
521 err |= 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800522
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700523 // parse the imports of the input file
Christopher Wiley72877ac2015-10-06 14:41:42 -0700524 ImportResolver import_resolver{io_delegate, import_paths};
Casey Dahlin0edf3422015-10-07 12:34:59 -0700525 for (auto& import : p.GetImports()) {
526 if (types->HasType(import->GetNeededClass())) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700527 // There are places in the Android tree where an import doesn't resolve,
528 // but we'll pick the type up through the preprocessed types.
529 // This seems like an error, but legacy support demands we support it...
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700530 continue;
531 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700532 string import_path = import_resolver.FindImportFile(import->GetNeededClass());
Christopher Wiley72877ac2015-10-06 14:41:42 -0700533 if (import_path.empty()) {
Casey Dahlin0edf3422015-10-07 12:34:59 -0700534 cerr << import->GetFileFrom() << ":" << import->GetLine()
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700535 << ": couldn't find import for class "
Casey Dahlin0edf3422015-10-07 12:34:59 -0700536 << import->GetNeededClass() << endl;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700537 err |= 1;
538 continue;
539 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700540 import->SetFilename(import_path);
Casey Dahlin2cc93162015-10-02 16:14:17 -0700541
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700542 Parser p{io_delegate};
Casey Dahlin0edf3422015-10-07 12:34:59 -0700543 if (!p.ParseFile(import->GetFilename())) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700544 cerr << "error while parsing import for class "
Casey Dahlin0edf3422015-10-07 12:34:59 -0700545 << import->GetNeededClass() << endl;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700546 err |= 1;
547 continue;
548 }
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700549
Casey Dahlin624358c2015-10-12 19:29:51 -0700550 AidlDocumentItem* document = p.GetDocument();
551 if (!check_filenames(import->GetFilename(), document))
Casey Dahlin42727f82015-10-12 19:23:40 -0700552 err |= 1;
Casey Dahlin624358c2015-10-12 19:29:51 -0700553 docs[import.get()] = std::unique_ptr<AidlDocumentItem>(document);
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700554 }
555 if (err != 0) {
556 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700557 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800558
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700559 // gather the types that have been declared
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700560 if (!gather_types(input_file_name.c_str(), parsed_doc, types)) {
561 err |= 1;
562 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700563 for (const auto& import : p.GetImports()) {
Casey Dahlin624358c2015-10-12 19:29:51 -0700564 if (!gather_types(import->GetFilename(), docs[import.get()].get(), types)) {
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700565 err |= 1;
566 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700567 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800568
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700569 // check the referenced types in parsed_doc to make sure we've imported them
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700570 err |= check_types(input_file_name, interface, types);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800571
Adam Lesinskiffa16862014-01-23 18:17:42 -0800572
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700573 // assign method ids and validate.
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700574 err |= check_and_assign_method_ids(input_file_name.c_str(),
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700575 interface->GetMethods());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800576
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700577 // after this, there shouldn't be any more errors because of the
578 // input.
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700579 if (err != 0) {
580 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700581 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800582
Casey Dahlin0edf3422015-10-07 12:34:59 -0700583 if (returned_interface)
584 *returned_interface = interface;
585 else
586 delete interface;
587
588 if (returned_imports)
589 p.ReleaseImports(returned_imports);
590
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700591 return 0;
592}
593
Casey Dahlin2cc93162015-10-02 16:14:17 -0700594} // namespace internals
595
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700596int compile_aidl_to_cpp(const CppOptions& options,
597 const IoDelegate& io_delegate) {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700598 AidlInterface* interface = nullptr;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700599 std::vector<std::unique_ptr<AidlImport>> imports;
Christopher Wileye3550c62015-09-29 13:26:10 -0700600 unique_ptr<cpp::TypeNamespace> types(new cpp::TypeNamespace());
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700601 int err = internals::load_and_validate_aidl(
602 std::vector<std::string>{}, // no preprocessed files
603 options.ImportPaths(),
604 options.InputFileName(),
605 io_delegate,
606 types.get(),
607 &interface,
608 &imports);
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700609 if (err != 0) {
610 return err;
611 }
612
613 // TODO(wiley) b/23600457 generate a dependency file if requested with -b
614
Christopher Wileye3550c62015-09-29 13:26:10 -0700615 return (cpp::GenerateCpp(options, *types, *interface)) ? 0 : 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700616}
617
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700618int compile_aidl_to_java(const JavaOptions& options,
619 const IoDelegate& io_delegate) {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700620 AidlInterface* interface = nullptr;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700621 std::vector<std::unique_ptr<AidlImport>> imports;
Christopher Wileydb154a52015-09-28 16:32:25 -0700622 unique_ptr<java::JavaTypeNamespace> types(new java::JavaTypeNamespace());
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700623 int err = internals::load_and_validate_aidl(
624 options.preprocessed_files_,
625 options.import_paths_,
626 options.input_file_name_,
627 io_delegate,
628 types.get(),
629 &interface,
630 &imports);
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700631 if (err != 0) {
632 return err;
633 }
Casey Dahlin5ac90202015-10-09 15:16:43 -0700634 AidlDocumentItem* parsed_doc = reinterpret_cast<AidlDocumentItem*>(interface);
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700635
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700636 string output_file_name = options.output_file_name_;
637 // if needed, generate the output file name from the base folder
638 if (output_file_name.length() == 0 &&
639 options.output_base_folder_.length() > 0) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700640 output_file_name = generate_outputFileName(options, parsed_doc);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700641 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800642
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700643 // if we were asked to, generate a make dependency file
644 // unless it's a parcelable *and* it's supposed to fail on parcelable
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700645 if (options.auto_dep_file_ || options.dep_file_name_ != "") {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800646 // make sure the folders of the output file all exists
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700647 check_outputFilePath(output_file_name);
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700648 generate_dep_file(options, parsed_doc, imports);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700649 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800650
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700651 // make sure the folders of the output file all exists
652 check_outputFilePath(output_file_name);
653
654 err = generate_java(output_file_name, options.input_file_name_.c_str(),
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700655 interface, types.get());
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700656
657 return err;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800658}
659
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700660int preprocess_aidl(const JavaOptions& options,
661 const IoDelegate& io_delegate) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800662 vector<string> lines;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800663
664 // read files
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700665 int N = options.files_to_preprocess_.size();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800666 for (int i=0; i<N; i++) {
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700667 Parser p{io_delegate};
668 if (!p.ParseFile(options.files_to_preprocess_[i]))
669 return 1;
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700670 AidlDocumentItem* doc = p.GetDocument();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800671 string line;
672 if (doc->item_type == USER_DATA_TYPE) {
Casey Dahlin5ac90202015-10-09 15:16:43 -0700673 AidlParcelable* parcelable = reinterpret_cast<AidlParcelable*>(doc);
Casey Dahlin59401da2015-10-09 18:16:45 -0700674
675 line = "parcelable ";
676
677 if (! parcelable->GetPackage().empty()) {
678 line += parcelable->GetPackage();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800679 line += '.';
680 }
Casey Dahlin59401da2015-10-09 18:16:45 -0700681 line += parcelable->GetName();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800682 } else {
683 line = "interface ";
Casey Dahlin5ac90202015-10-09 15:16:43 -0700684 AidlInterface* iface = reinterpret_cast<AidlInterface*>(doc);
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700685 if (!iface->GetPackage().empty()) {
686 line += iface->GetPackage();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800687 line += '.';
688 }
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700689 line += iface->GetName();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800690 }
691 line += ";\n";
692 lines.push_back(line);
693 }
694
695 // write preprocessed file
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700696 int fd = open( options.output_file_name_.c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800697 O_RDWR|O_CREAT|O_TRUNC|O_BINARY,
Elliott Hughes549b6e22015-08-17 12:41:46 -0700698#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800699 _S_IREAD|_S_IWRITE);
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700700#else
Adam Lesinskiffa16862014-01-23 18:17:42 -0800701 S_IRUSR|S_IWUSR|S_IRGRP);
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700702#endif
Adam Lesinskiffa16862014-01-23 18:17:42 -0800703 if (fd == -1) {
704 fprintf(stderr, "aidl: could not open file for write: %s\n",
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700705 options.output_file_name_.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800706 return 1;
707 }
708
709 N = lines.size();
710 for (int i=0; i<N; i++) {
711 const string& s = lines[i];
712 int len = s.length();
713 if (len != write(fd, s.c_str(), len)) {
714 fprintf(stderr, "aidl: error writing to file %s\n",
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700715 options.output_file_name_.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800716 close(fd);
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700717 unlink(options.output_file_name_.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800718 return 1;
719 }
720 }
721
722 close(fd);
723 return 0;
724}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700725
726} // namespace android
727} // namespace aidl