blob: 8e5421124f6197b89a4859560ff96830e21611da [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 Wileyd76067c2015-10-19 17:00:13 -070035#include <base/strings.h>
Christopher Wileyf690be52015-09-14 15:19:10 -070036
Christopher Wileyf690be52015-09-14 15:19:10 -070037#include "aidl_language.h"
Christopher Wileyeb1acc12015-09-16 11:25:13 -070038#include "generate_cpp.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070039#include "generate_java.h"
Christopher Wiley72877ac2015-10-06 14:41:42 -070040#include "import_resolver.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070041#include "logging.h"
42#include "options.h"
43#include "os.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 Wileyd76067c2015-10-19 17:00:13 -070052using android::base::Split;
Christopher Wileyc16e5e72015-09-16 10:49:40 -070053using std::cerr;
54using std::endl;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070055using std::map;
56using std::set;
57using std::string;
Christopher Wiley84c1eac2015-09-23 13:29:28 -070058using std::unique_ptr;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070059using std::vector;
Adam Lesinskiffa16862014-01-23 18:17:42 -080060
Christopher Wileyf690be52015-09-14 15:19:10 -070061namespace android {
62namespace aidl {
63namespace {
Adam Lesinskiffa16862014-01-23 18:17:42 -080064
Christopher Wileyf690be52015-09-14 15:19:10 -070065// The following are gotten as the offset from the allowable id's between
66// android.os.IBinder.FIRST_CALL_TRANSACTION=1 and
67// android.os.IBinder.LAST_CALL_TRANSACTION=16777215
68const int kMinUserSetMethodId = 0;
69const int kMaxUserSetMethodId = 16777214;
Adam Lesinskiffa16862014-01-23 18:17:42 -080070
Casey Dahlin42727f82015-10-12 19:23:40 -070071bool check_filename(const std::string& filename,
72 const std::string& package,
73 const std::string& name,
74 unsigned line) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080075 const char* p;
76 string expected;
77 string fn;
78 size_t len;
79 char cwd[MAXPATHLEN];
80 bool valid = false;
81
Elliott Hughesce310da2015-07-29 08:44:17 -070082#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -080083 if (isalpha(filename[0]) && filename[1] == ':'
84 && filename[2] == OS_PATH_SEPARATOR) {
85#else
86 if (filename[0] == OS_PATH_SEPARATOR) {
87#endif
88 fn = filename;
89 } else {
90 fn = getcwd(cwd, sizeof(cwd));
91 len = fn.length();
92 if (fn[len-1] != OS_PATH_SEPARATOR) {
93 fn += OS_PATH_SEPARATOR;
94 }
95 fn += filename;
96 }
97
Casey Dahlinfb7da2e2015-10-08 17:26:09 -070098 if (!package.empty()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080099 expected = package;
100 expected += '.';
101 }
102
103 len = expected.length();
104 for (size_t i=0; i<len; i++) {
105 if (expected[i] == '.') {
106 expected[i] = OS_PATH_SEPARATOR;
107 }
108 }
109
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700110 expected.append(name, 0, name.find('.'));
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",
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700142 filename.c_str(), line, name.c_str(), expected.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800143 }
144
Casey Dahlin42727f82015-10-12 19:23:40 -0700145 return valid;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800146}
147
Casey Dahlin42727f82015-10-12 19:23:40 -0700148bool check_filenames(const std::string& filename, const AidlDocumentItem* items) {
149 if (! items)
150 return true;
151
152 if (items->item_type == INTERFACE_TYPE_BINDER) {
153 const AidlInterface* c = reinterpret_cast<const AidlInterface*>(items);
154 return check_filename(filename, c->GetPackage(), c->GetName(), c->GetLine());
155 }
156
157 bool success = true;
158
159 for (const AidlParcelable* p = reinterpret_cast<const AidlParcelable*>(items);
160 p; p = p->next)
161 success &= check_filename(filename, p->GetPackage(), p->GetName(),
162 p->GetLine());
163
164 return success;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800165}
166
Casey Dahlin0edf3422015-10-07 12:34:59 -0700167bool gather_types(const std::string& filename,
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700168 const AidlDocumentItem* all_items,
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700169 TypeNamespace* types) {
170 bool success = true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800171
Casey Dahlin42727f82015-10-12 19:23:40 -0700172 if (! all_items)
173 return true;
174
175 if (all_items->item_type == INTERFACE_TYPE_BINDER)
176 return types->AddBinderType(reinterpret_cast<const AidlInterface *>(all_items), filename);
177
178 for (const AidlParcelable* item =
179 reinterpret_cast<const AidlParcelable *>(all_items);
180 item; item = item->next) {
181 success &= types->AddParcelableType(item, filename);
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700182 }
Casey Dahlin42727f82015-10-12 19:23:40 -0700183
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700184 return success;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800185}
186
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700187int check_types(const string& filename,
Casey Dahlin98a544b2015-10-14 14:22:55 -0700188 const AidlInterface* c,
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700189 TypeNamespace* types) {
190 int err = 0;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700191
192 // Has to be a pointer due to deleting copy constructor. No idea why.
193 map<string, const AidlMethod*> method_names;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700194 for (const auto& m : c->GetMethods()) {
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700195 bool oneway = m->IsOneway() || c->IsOneway();
196
Christopher Wileycd8e8972015-10-26 10:24:35 -0700197 if (!types->MaybeAddContainerType(m->GetType().GetName()) ||
Casey Dahlinf4a93112015-10-05 16:58:09 -0700198 !types->IsValidReturnType(m->GetType(), filename)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700199 err = 1; // return type is invalid
Adam Lesinskiffa16862014-01-23 18:17:42 -0800200 }
201
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700202 if (oneway && m->GetType().GetName() != "void") {
203 cerr << filename << ":" << m->GetLine()
Christopher Wiley45db9ee2015-10-26 18:53:53 -0700204 << " oneway method '" << m->GetName() << "' cannot return a value"
205 << endl;
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700206 err = 1;
207 }
208
Adam Lesinskiffa16862014-01-23 18:17:42 -0800209 int index = 1;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700210 for (const auto& arg : m->GetArguments()) {
Christopher Wileycd8e8972015-10-26 10:24:35 -0700211 if (!types->MaybeAddContainerType(arg->GetType().GetName()) ||
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700212 !types->IsValidArg(*arg, index, filename)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700213 err = 1;
214 }
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700215
216 if (oneway && arg->IsOut()) {
217 cerr << filename << ":" << m->GetLine()
Christopher Wiley45db9ee2015-10-26 18:53:53 -0700218 << " oneway method '" << m->GetName()
219 << "' cannot have out parameters" << endl;
Casey Dahlin0c6fcec2015-10-20 13:32:21 -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 Wileyef4132c2015-11-05 15:47:40 -0800239void generate_dep_file(const std::string& dep_file_name,
240 const std::string& input_file_name,
241 const std::string& output_file_name,
Christopher Wileya30a45e2015-10-17 10:56:59 -0700242 const std::vector<std::unique_ptr<AidlImport>>& imports,
243 const IoDelegate& io_delegate) {
Christopher Wileyef4132c2015-11-05 15:47:40 -0800244 CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name);
Christopher Wileya30a45e2015-10-17 10:56:59 -0700245 if (!writer) {
Christopher Wileyef4132c2015-11-05 15:47:40 -0800246 cerr << "Could not open " << dep_file_name << endl;
Christopher Wileya30a45e2015-10-17 10:56:59 -0700247 return;
248 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700249
Christopher Wileya30a45e2015-10-17 10:56:59 -0700250
Christopher Wileyef4132c2015-11-05 15:47:40 -0800251 writer->Write("%s: \\\n", output_file_name.c_str());
252 writer->Write(" %s %s\n", input_file_name.c_str(),
Christopher Wiley90be4e32015-10-20 14:55:25 -0700253 imports.empty() ? "" : "\\");
Christopher Wileya30a45e2015-10-17 10:56:59 -0700254
255 bool first = true;
256 for (const auto& import : imports) {
257 if (! first) {
258 writer->Write(" \\\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800259 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700260 first = false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800261
Christopher Wileya30a45e2015-10-17 10:56:59 -0700262 if (! import->GetFilename().empty()) {
263 writer->Write(" %s", import->GetFilename().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800264 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700265 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800266
Christopher Wileya30a45e2015-10-17 10:56:59 -0700267 writer->Write(first ? "\n" : "\n\n");
268
269 // Output "<input_aidl_file>: " so make won't fail if the input .aidl file
270 // has been deleted, moved or renamed in incremental build.
Christopher Wileyef4132c2015-11-05 15:47:40 -0800271 writer->Write("%s :\n", input_file_name.c_str());
Christopher Wileya30a45e2015-10-17 10:56:59 -0700272
273 // Output "<imported_file>: " so make won't fail if the imported file has
274 // been deleted, moved or renamed in incremental build.
275 for (const auto& import : imports) {
276 if (! import->GetFilename().empty()) {
277 writer->Write("%s :\n", import->GetFilename().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800278 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700279 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800280}
281
Christopher Wiley90be4e32015-10-20 14:55:25 -0700282string generate_outputFileName(const JavaOptions& options,
283 const AidlInterface& interface) {
284 string name = interface.GetName();
285 string package = interface.GetPackage();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800286 string result;
287
288 // create the path to the destination folder based on the
289 // interface package name
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700290 result = options.output_base_folder_;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800291 result += OS_PATH_SEPARATOR;
292
293 string packageStr = package;
294 size_t len = packageStr.length();
295 for (size_t i=0; i<len; i++) {
296 if (packageStr[i] == '.') {
297 packageStr[i] = OS_PATH_SEPARATOR;
298 }
299 }
300
301 result += packageStr;
302
303 // add the filename by replacing the .aidl extension to .java
Adam Lesinskiffa16862014-01-23 18:17:42 -0800304 result += OS_PATH_SEPARATOR;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700305 result.append(name, 0, name.find('.'));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800306 result += ".java";
307
308 return result;
309}
310
Christopher Wileyf690be52015-09-14 15:19:10 -0700311int check_and_assign_method_ids(const char * filename,
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700312 const std::vector<std::unique_ptr<AidlMethod>>& items) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800313 // Check whether there are any methods with manually assigned id's and any that are not.
314 // Either all method id's must be manually assigned or all of them must not.
315 // Also, check for duplicates of user set id's and that the id's are within the proper bounds.
316 set<int> usedIds;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800317 bool hasUnassignedIds = false;
318 bool hasAssignedIds = false;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700319 for (const auto& item : items) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700320 if (item->HasId()) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700321 hasAssignedIds = true;
Casey Dahlindff80e52015-09-29 13:57:06 -0700322 // Ensure that the user set id is not duplicated.
Casey Dahlinf4a93112015-10-05 16:58:09 -0700323 if (usedIds.find(item->GetId()) != usedIds.end()) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700324 // We found a duplicate id, so throw an error.
Adam Lesinskiffa16862014-01-23 18:17:42 -0800325 fprintf(stderr,
Casey Dahlindff80e52015-09-29 13:57:06 -0700326 "%s:%d Found duplicate method id (%d) for method: %s\n",
Casey Dahlinf4a93112015-10-05 16:58:09 -0700327 filename, item->GetLine(),
328 item->GetId(), item->GetName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800329 return 1;
330 }
Casey Dahlindff80e52015-09-29 13:57:06 -0700331 // Ensure that the user set id is within the appropriate limits
Casey Dahlinf4a93112015-10-05 16:58:09 -0700332 if (item->GetId() < kMinUserSetMethodId ||
333 item->GetId() > kMaxUserSetMethodId) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700334 fprintf(stderr, "%s:%d Found out of bounds id (%d) for method: %s\n",
Casey Dahlinf4a93112015-10-05 16:58:09 -0700335 filename, item->GetLine(),
336 item->GetId(), item->GetName().c_str());
Casey Dahlindff80e52015-09-29 13:57:06 -0700337 fprintf(stderr, " Value for id must be between %d and %d inclusive.\n",
338 kMinUserSetMethodId, kMaxUserSetMethodId);
339 return 1;
340 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700341 usedIds.insert(item->GetId());
Casey Dahlindff80e52015-09-29 13:57:06 -0700342 } else {
343 hasUnassignedIds = true;
344 }
345 if (hasAssignedIds && hasUnassignedIds) {
346 fprintf(stderr,
347 "%s: You must either assign id's to all methods or to none of them.\n",
348 filename);
349 return 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800350 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800351 }
352
353 // In the case that all methods have unassigned id's, set a unique id for them.
354 if (hasUnassignedIds) {
355 int newId = 0;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700356 for (const auto& item : items) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700357 item->SetId(newId++);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800358 }
359 }
360
361 // success
362 return 0;
363}
364
Christopher Wileyef140932015-11-03 09:29:19 -0800365// TODO: Remove this in favor of using the YACC parser b/25479378
366bool ParsePreprocessedLine(const string& line, string* decl,
367 vector<string>* package, string* class_name) {
368 // erase all trailing whitespace and semicolons
369 const size_t end = line.find_last_not_of(" ;\t");
370 if (end == string::npos) {
371 return false;
372 }
373 if (line.rfind(';', end) != string::npos) {
374 return false;
375 }
376
377 decl->clear();
378 string type;
379 vector<string> pieces = Split(line.substr(0, end + 1), " \t");
380 for (const string& piece : pieces) {
381 if (piece.empty()) {
382 continue;
383 }
384 if (decl->empty()) {
385 *decl = std::move(piece);
386 } else if (type.empty()) {
387 type = std::move(piece);
388 } else {
389 return false;
390 }
391 }
392
393 // Note that this logic is absolutely wrong. Given a parcelable
394 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
395 // the class is just Bar. However, this was the way it was done in the past.
396 //
397 // See b/17415692
398 size_t dot_pos = type.rfind('.');
399 if (dot_pos != string::npos) {
400 *class_name = type.substr(dot_pos + 1);
401 *package = Split(type.substr(0, dot_pos), ".");
402 } else {
403 *class_name = type;
404 package->clear();
405 }
406
407 return true;
408}
409
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700410} // namespace
411
412namespace internals {
413
Christopher Wileyef140932015-11-03 09:29:19 -0800414bool parse_preprocessed_file(const IoDelegate& io_delegate,
415 const string& filename, TypeNamespace* types) {
416 bool success = true;
417 unique_ptr<LineReader> line_reader = io_delegate.GetLineReader(filename);
418 if (!line_reader) {
419 LOG(ERROR) << "cannot open preprocessed file: " << filename;
420 success = false;
421 return success;
422 }
423
424 string line;
425 unsigned lineno = 1;
426 for ( ; line_reader->ReadLine(&line); ++lineno) {
427 if (line.empty() || line.compare(0, 2, "//") == 0) {
428 // skip comments and empty lines
429 continue;
430 }
431
432 string decl;
433 vector<string> package;
434 string class_name;
435 if (!ParsePreprocessedLine(line, &decl, &package, &class_name)) {
436 success = false;
437 break;
438 }
439
440 if (decl == "parcelable") {
441 AidlParcelable doc(class_name, lineno, package);
442 types->AddParcelableType(&doc, filename);
443 } else if (decl == "interface") {
444 auto temp = new std::vector<std::unique_ptr<AidlMethod>>();
445 AidlInterface doc(class_name, lineno, "", false, temp, package);
446 types->AddBinderType(&doc, filename);
447 } else {
448 success = false;
449 break;
450 }
451 }
452 if (!success) {
453 LOG(ERROR) << filename << ':' << lineno
454 << " malformed preprocessed file line: '" << line << "'";
455 }
456
457 return success;
458}
459
Christopher Wiley632801d2015-11-05 14:15:49 -0800460AidlError load_and_validate_aidl(
461 const std::vector<std::string> preprocessed_files,
462 const std::vector<std::string> import_paths,
463 const std::string& input_file_name,
464 const IoDelegate& io_delegate,
465 TypeNamespace* types,
466 std::unique_ptr<AidlInterface>* returned_interface,
467 std::vector<std::unique_ptr<AidlImport>>* returned_imports) {
468 AidlError err = AidlError::OK;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800469
Casey Dahlin624358c2015-10-12 19:29:51 -0700470 std::map<AidlImport*,std::unique_ptr<AidlDocumentItem>> docs;
471
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700472 // import the preprocessed file
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700473 for (const string& s : preprocessed_files) {
Christopher Wileyef140932015-11-03 09:29:19 -0800474 if (!parse_preprocessed_file(io_delegate, s, types)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800475 err = AidlError::BAD_PRE_PROCESSED_FILE;
Christopher Wileyef140932015-11-03 09:29:19 -0800476 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700477 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800478 if (err != AidlError::OK) {
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700479 return err;
480 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800481
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700482 // parse the input file
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700483 Parser p{io_delegate};
484 if (!p.ParseFile(input_file_name)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800485 return AidlError::PARSE_ERROR;
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700486 }
Casey Dahlin2cc93162015-10-02 16:14:17 -0700487
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700488 AidlDocumentItem* parsed_doc = p.GetDocument();
Christopher Wiley632801d2015-11-05 14:15:49 -0800489 if (parsed_doc == nullptr) {
490 return AidlError::PARSE_ERROR;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700491 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800492 if (parsed_doc->item_type != INTERFACE_TYPE_BINDER) {
Christopher Wileye60d99c2015-11-06 13:14:24 -0800493 LOG(ERROR) << "refusing to generate code from aidl file defining "
494 "parcelable";
Christopher Wiley632801d2015-11-05 14:15:49 -0800495 return AidlError::FOUND_PARCELABLE;
496 }
497
Christopher Wiley90be4e32015-10-20 14:55:25 -0700498 unique_ptr<AidlInterface> interface(
499 reinterpret_cast<AidlInterface*>(parsed_doc));
500
Casey Dahlin42727f82015-10-12 19:23:40 -0700501 if (!check_filename(input_file_name.c_str(), interface->GetPackage(),
Christopher Wiley632801d2015-11-05 14:15:49 -0800502 interface->GetName(), interface->GetLine()) ||
503 !types->IsValidPackage(interface->GetPackage())) {
504 LOG(ERROR) << "Invalid package declaration '" << interface->GetPackage()
505 << "'";
506 return AidlError::BAD_PACKAGE;
507 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800508
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700509 // parse the imports of the input file
Christopher Wiley72877ac2015-10-06 14:41:42 -0700510 ImportResolver import_resolver{io_delegate, import_paths};
Casey Dahlin0edf3422015-10-07 12:34:59 -0700511 for (auto& import : p.GetImports()) {
512 if (types->HasType(import->GetNeededClass())) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700513 // There are places in the Android tree where an import doesn't resolve,
514 // but we'll pick the type up through the preprocessed types.
515 // This seems like an error, but legacy support demands we support it...
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700516 continue;
517 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700518 string import_path = import_resolver.FindImportFile(import->GetNeededClass());
Christopher Wiley72877ac2015-10-06 14:41:42 -0700519 if (import_path.empty()) {
Casey Dahlin0edf3422015-10-07 12:34:59 -0700520 cerr << import->GetFileFrom() << ":" << import->GetLine()
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700521 << ": couldn't find import for class "
Casey Dahlin0edf3422015-10-07 12:34:59 -0700522 << import->GetNeededClass() << endl;
Christopher Wiley632801d2015-11-05 14:15:49 -0800523 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700524 continue;
525 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700526 import->SetFilename(import_path);
Casey Dahlin2cc93162015-10-02 16:14:17 -0700527
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700528 Parser p{io_delegate};
Casey Dahlin0edf3422015-10-07 12:34:59 -0700529 if (!p.ParseFile(import->GetFilename())) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700530 cerr << "error while parsing import for class "
Casey Dahlin0edf3422015-10-07 12:34:59 -0700531 << import->GetNeededClass() << endl;
Christopher Wiley632801d2015-11-05 14:15:49 -0800532 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700533 continue;
534 }
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700535
Casey Dahlin624358c2015-10-12 19:29:51 -0700536 AidlDocumentItem* document = p.GetDocument();
537 if (!check_filenames(import->GetFilename(), document))
Christopher Wiley632801d2015-11-05 14:15:49 -0800538 err = AidlError::BAD_IMPORT;
Casey Dahlin624358c2015-10-12 19:29:51 -0700539 docs[import.get()] = std::unique_ptr<AidlDocumentItem>(document);
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700540 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800541 if (err != AidlError::OK) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700542 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700543 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800544
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700545 // gather the types that have been declared
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700546 if (!gather_types(input_file_name.c_str(), parsed_doc, types)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800547 err = AidlError::BAD_TYPE;
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700548 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700549 for (const auto& import : p.GetImports()) {
Casey Dahlin624358c2015-10-12 19:29:51 -0700550 if (!gather_types(import->GetFilename(), docs[import.get()].get(), types)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800551 err = AidlError::BAD_TYPE;
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700552 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700553 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800554
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700555 // check the referenced types in parsed_doc to make sure we've imported them
Christopher Wiley632801d2015-11-05 14:15:49 -0800556 if (check_types(input_file_name, interface.get(), types) != 0) {
557 err = AidlError::BAD_TYPE;
558 }
559 if (err != AidlError::OK) {
560 return err;
561 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800562
Adam Lesinskiffa16862014-01-23 18:17:42 -0800563
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700564 // assign method ids and validate.
Christopher Wiley632801d2015-11-05 14:15:49 -0800565 if (check_and_assign_method_ids(input_file_name.c_str(),
566 interface->GetMethods()) != 0) {
567 return AidlError::BAD_METHOD_ID;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700568 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800569
Casey Dahlin0edf3422015-10-07 12:34:59 -0700570 if (returned_interface)
Christopher Wiley90be4e32015-10-20 14:55:25 -0700571 *returned_interface = std::move(interface);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700572
573 if (returned_imports)
574 p.ReleaseImports(returned_imports);
575
Christopher Wiley632801d2015-11-05 14:15:49 -0800576 return AidlError::OK;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700577}
578
Casey Dahlin2cc93162015-10-02 16:14:17 -0700579} // namespace internals
580
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700581int compile_aidl_to_cpp(const CppOptions& options,
582 const IoDelegate& io_delegate) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700583 unique_ptr<AidlInterface> interface;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700584 std::vector<std::unique_ptr<AidlImport>> imports;
Christopher Wileye3550c62015-09-29 13:26:10 -0700585 unique_ptr<cpp::TypeNamespace> types(new cpp::TypeNamespace());
Christopher Wiley56799522015-10-31 10:17:04 -0700586 types->Init();
Christopher Wiley632801d2015-11-05 14:15:49 -0800587 AidlError err = internals::load_and_validate_aidl(
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700588 std::vector<std::string>{}, // no preprocessed files
589 options.ImportPaths(),
590 options.InputFileName(),
591 io_delegate,
592 types.get(),
593 &interface,
594 &imports);
Christopher Wiley632801d2015-11-05 14:15:49 -0800595 if (err != AidlError::OK) {
596 return 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700597 }
598
Christopher Wiley19059cb2015-11-05 16:11:56 -0800599 string dep_file_name = options.DependencyFilePath();
600 if (!dep_file_name.empty()) {
601 generate_dep_file(dep_file_name, options.InputFileName(),
602 options.OutputCppFilePath(), imports, io_delegate);
603 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700604
Christopher Wiley054afbd2015-10-16 17:08:43 -0700605 return (cpp::GenerateCpp(options, *types, *interface, io_delegate)) ? 0 : 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700606}
607
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700608int compile_aidl_to_java(const JavaOptions& options,
609 const IoDelegate& io_delegate) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700610 unique_ptr<AidlInterface> interface;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700611 std::vector<std::unique_ptr<AidlImport>> imports;
Christopher Wileydb154a52015-09-28 16:32:25 -0700612 unique_ptr<java::JavaTypeNamespace> types(new java::JavaTypeNamespace());
Christopher Wiley56799522015-10-31 10:17:04 -0700613 types->Init();
Christopher Wiley632801d2015-11-05 14:15:49 -0800614 AidlError aidl_err = internals::load_and_validate_aidl(
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700615 options.preprocessed_files_,
616 options.import_paths_,
617 options.input_file_name_,
618 io_delegate,
619 types.get(),
620 &interface,
621 &imports);
Christopher Wiley632801d2015-11-05 14:15:49 -0800622 if (aidl_err == AidlError::FOUND_PARCELABLE && !options.fail_on_parcelable_) {
623 // We aborted code generation because this file contains parcelables.
624 // However, we were not told to complain if we find parcelables.
625 // Just exit quietly.
626 return 0;
627 }
628 if (aidl_err != AidlError::OK) {
629 return 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700630 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700631
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700632 string output_file_name = options.output_file_name_;
633 // if needed, generate the output file name from the base folder
Christopher Wiley632801d2015-11-05 14:15:49 -0800634 if (output_file_name.empty() && !options.output_base_folder_.empty()) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700635 output_file_name = generate_outputFileName(options, *interface);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700636 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800637
Casey Dahlin64533512015-10-23 17:11:21 -0700638 // make sure the folders of the output file all exists
639 if (!io_delegate.CreatePathForFile(output_file_name)) {
640 return 1;
641 }
642
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700643 // if we were asked to, generate a make dependency file
Christopher Wileyef4132c2015-11-05 15:47:40 -0800644 string dep_file_name = options.DependencyFilePath();
645 if (!dep_file_name.empty()) {
646 generate_dep_file(dep_file_name, options.input_file_name_,
647 options.output_file_name_, imports, io_delegate);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700648 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800649
Christopher Wiley632801d2015-11-05 14:15:49 -0800650 return generate_java(output_file_name, options.input_file_name_.c_str(),
651 interface.get(), types.get(), io_delegate);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800652}
653
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700654int preprocess_aidl(const JavaOptions& options,
655 const IoDelegate& io_delegate) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800656 vector<string> lines;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800657
658 // read files
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700659 int N = options.files_to_preprocess_.size();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800660 for (int i=0; i<N; i++) {
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700661 Parser p{io_delegate};
662 if (!p.ParseFile(options.files_to_preprocess_[i]))
663 return 1;
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700664 AidlDocumentItem* doc = p.GetDocument();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800665 string line;
666 if (doc->item_type == USER_DATA_TYPE) {
Casey Dahlin5ac90202015-10-09 15:16:43 -0700667 AidlParcelable* parcelable = reinterpret_cast<AidlParcelable*>(doc);
Casey Dahlin59401da2015-10-09 18:16:45 -0700668
669 line = "parcelable ";
670
671 if (! parcelable->GetPackage().empty()) {
672 line += parcelable->GetPackage();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800673 line += '.';
674 }
Casey Dahlin59401da2015-10-09 18:16:45 -0700675 line += parcelable->GetName();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800676 } else {
677 line = "interface ";
Casey Dahlin5ac90202015-10-09 15:16:43 -0700678 AidlInterface* iface = reinterpret_cast<AidlInterface*>(doc);
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700679 if (!iface->GetPackage().empty()) {
680 line += iface->GetPackage();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800681 line += '.';
682 }
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700683 line += iface->GetName();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800684 }
685 line += ";\n";
686 lines.push_back(line);
687 }
688
689 // write preprocessed file
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700690 int fd = open( options.output_file_name_.c_str(),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800691 O_RDWR|O_CREAT|O_TRUNC|O_BINARY,
Elliott Hughes549b6e22015-08-17 12:41:46 -0700692#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800693 _S_IREAD|_S_IWRITE);
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700694#else
Adam Lesinskiffa16862014-01-23 18:17:42 -0800695 S_IRUSR|S_IWUSR|S_IRGRP);
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700696#endif
Adam Lesinskiffa16862014-01-23 18:17:42 -0800697 if (fd == -1) {
698 fprintf(stderr, "aidl: could not open file for write: %s\n",
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700699 options.output_file_name_.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800700 return 1;
701 }
702
703 N = lines.size();
704 for (int i=0; i<N; i++) {
705 const string& s = lines[i];
706 int len = s.length();
707 if (len != write(fd, s.c_str(), len)) {
708 fprintf(stderr, "aidl: error writing to file %s\n",
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700709 options.output_file_name_.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800710 close(fd);
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700711 unlink(options.output_file_name_.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800712 return 1;
713 }
714 }
715
716 close(fd);
717 return 0;
718}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700719
720} // namespace android
721} // namespace aidl