blob: 85d9772023b54ddc69c57240d85871fb5911c400 [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
Elliott Hughes0a620672015-12-04 13:53:18 -080035#include <android-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 Dahlinc1f39b42015-11-24 10:34:34 -0800148bool check_filenames(const std::string& filename, const AidlDocument* doc) {
149 if (!doc)
Casey Dahlin42727f82015-10-12 19:23:40 -0700150 return true;
151
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800152 const AidlInterface* interface = doc->GetInterface();
153
154 if (interface) {
155 return check_filename(filename, interface->GetPackage(),
156 interface->GetName(), interface->GetLine());
Casey Dahlin42727f82015-10-12 19:23:40 -0700157 }
158
159 bool success = true;
160
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800161 for (const auto& item : doc->GetParcelables()) {
162 success &= check_filename(filename, item->GetPackage(), item->GetName(),
163 item->GetLine());
164 }
Casey Dahlin42727f82015-10-12 19:23:40 -0700165
166 return success;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800167}
168
Casey Dahlin0edf3422015-10-07 12:34:59 -0700169bool gather_types(const std::string& filename,
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800170 const AidlDocument* doc,
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700171 TypeNamespace* types) {
172 bool success = true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800173
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800174 const AidlInterface* interface = doc->GetInterface();
Casey Dahlin42727f82015-10-12 19:23:40 -0700175
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800176 if (interface)
177 return types->AddBinderType(*interface, filename);
Casey Dahlin42727f82015-10-12 19:23:40 -0700178
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800179 for (const auto& item : doc->GetParcelables()) {
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 Dahlin98a544b2015-10-14 14:22:55 -0700187 const 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 Dahlin0c6fcec2015-10-20 13:32:21 -0700194 bool oneway = m->IsOneway() || c->IsOneway();
195
Christopher Wileycd8e8972015-10-26 10:24:35 -0700196 if (!types->MaybeAddContainerType(m->GetType().GetName()) ||
Casey Dahlinf4a93112015-10-05 16:58:09 -0700197 !types->IsValidReturnType(m->GetType(), filename)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700198 err = 1; // return type is invalid
Adam Lesinskiffa16862014-01-23 18:17:42 -0800199 }
200
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700201 if (oneway && m->GetType().GetName() != "void") {
202 cerr << filename << ":" << m->GetLine()
Christopher Wiley45db9ee2015-10-26 18:53:53 -0700203 << " oneway method '" << m->GetName() << "' cannot return a value"
204 << endl;
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700205 err = 1;
206 }
207
Adam Lesinskiffa16862014-01-23 18:17:42 -0800208 int index = 1;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700209 for (const auto& arg : m->GetArguments()) {
Christopher Wileycd8e8972015-10-26 10:24:35 -0700210 if (!types->MaybeAddContainerType(arg->GetType().GetName()) ||
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700211 !types->IsValidArg(*arg, index, filename)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700212 err = 1;
213 }
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700214
215 if (oneway && arg->IsOut()) {
216 cerr << filename << ":" << m->GetLine()
Christopher Wiley45db9ee2015-10-26 18:53:53 -0700217 << " oneway method '" << m->GetName()
218 << "' cannot have out parameters" << endl;
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700219 err = 1;
220 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800221 }
222
Casey Dahlinf4a93112015-10-05 16:58:09 -0700223 auto it = method_names.find(m->GetName());
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700224 // prevent duplicate methods
Casey Dahlinf4a93112015-10-05 16:58:09 -0700225 if (it == method_names.end()) {
226 method_names[m->GetName()] = m.get();
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700227 } else {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700228 cerr << filename << ":" << m->GetLine()
229 << " attempt to redefine method " << m->GetName() << "," << endl
230 << filename << ":" << it->second->GetLine()
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700231 << " previously defined here." << endl;
232 err = 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800233 }
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700234 }
235 return err;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800236}
237
Christopher Wileyef4132c2015-11-05 15:47:40 -0800238void generate_dep_file(const std::string& dep_file_name,
239 const std::string& input_file_name,
240 const std::string& output_file_name,
Christopher Wileya30a45e2015-10-17 10:56:59 -0700241 const std::vector<std::unique_ptr<AidlImport>>& imports,
242 const IoDelegate& io_delegate) {
Christopher Wileyef4132c2015-11-05 15:47:40 -0800243 CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name);
Christopher Wileya30a45e2015-10-17 10:56:59 -0700244 if (!writer) {
Christopher Wileyef4132c2015-11-05 15:47:40 -0800245 cerr << "Could not open " << dep_file_name << endl;
Christopher Wileya30a45e2015-10-17 10:56:59 -0700246 return;
247 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700248
Christopher Wileya30a45e2015-10-17 10:56:59 -0700249
Christopher Wileyef4132c2015-11-05 15:47:40 -0800250 writer->Write("%s: \\\n", output_file_name.c_str());
251 writer->Write(" %s %s\n", input_file_name.c_str(),
Christopher Wiley90be4e32015-10-20 14:55:25 -0700252 imports.empty() ? "" : "\\");
Christopher Wileya30a45e2015-10-17 10:56:59 -0700253
254 bool first = true;
255 for (const auto& import : imports) {
256 if (! first) {
257 writer->Write(" \\\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800258 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700259 first = false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800260
Christopher Wileya30a45e2015-10-17 10:56:59 -0700261 if (! import->GetFilename().empty()) {
262 writer->Write(" %s", import->GetFilename().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800263 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700264 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800265
Christopher Wileya30a45e2015-10-17 10:56:59 -0700266 writer->Write(first ? "\n" : "\n\n");
267
268 // Output "<input_aidl_file>: " so make won't fail if the input .aidl file
269 // has been deleted, moved or renamed in incremental build.
Christopher Wileyef4132c2015-11-05 15:47:40 -0800270 writer->Write("%s :\n", input_file_name.c_str());
Christopher Wileya30a45e2015-10-17 10:56:59 -0700271
272 // Output "<imported_file>: " so make won't fail if the imported file has
273 // been deleted, moved or renamed in incremental build.
274 for (const auto& import : imports) {
275 if (! import->GetFilename().empty()) {
276 writer->Write("%s :\n", import->GetFilename().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800277 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700278 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800279}
280
Christopher Wiley90be4e32015-10-20 14:55:25 -0700281string generate_outputFileName(const JavaOptions& options,
282 const AidlInterface& interface) {
283 string name = interface.GetName();
284 string package = interface.GetPackage();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800285 string result;
286
287 // create the path to the destination folder based on the
288 // interface package name
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700289 result = options.output_base_folder_;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800290 result += OS_PATH_SEPARATOR;
291
292 string packageStr = package;
293 size_t len = packageStr.length();
294 for (size_t i=0; i<len; i++) {
295 if (packageStr[i] == '.') {
296 packageStr[i] = OS_PATH_SEPARATOR;
297 }
298 }
299
300 result += packageStr;
301
302 // add the filename by replacing the .aidl extension to .java
Adam Lesinskiffa16862014-01-23 18:17:42 -0800303 result += OS_PATH_SEPARATOR;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700304 result.append(name, 0, name.find('.'));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800305 result += ".java";
306
307 return result;
308}
309
Christopher Wileyf690be52015-09-14 15:19:10 -0700310int check_and_assign_method_ids(const char * filename,
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700311 const std::vector<std::unique_ptr<AidlMethod>>& items) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800312 // Check whether there are any methods with manually assigned id's and any that are not.
313 // Either all method id's must be manually assigned or all of them must not.
314 // Also, check for duplicates of user set id's and that the id's are within the proper bounds.
315 set<int> usedIds;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800316 bool hasUnassignedIds = false;
317 bool hasAssignedIds = false;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700318 for (const auto& item : items) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700319 if (item->HasId()) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700320 hasAssignedIds = true;
Casey Dahlindff80e52015-09-29 13:57:06 -0700321 // Ensure that the user set id is not duplicated.
Casey Dahlinf4a93112015-10-05 16:58:09 -0700322 if (usedIds.find(item->GetId()) != usedIds.end()) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700323 // We found a duplicate id, so throw an error.
Adam Lesinskiffa16862014-01-23 18:17:42 -0800324 fprintf(stderr,
Casey Dahlindff80e52015-09-29 13:57:06 -0700325 "%s:%d Found duplicate method id (%d) for method: %s\n",
Casey Dahlinf4a93112015-10-05 16:58:09 -0700326 filename, item->GetLine(),
327 item->GetId(), item->GetName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800328 return 1;
329 }
Casey Dahlindff80e52015-09-29 13:57:06 -0700330 // Ensure that the user set id is within the appropriate limits
Casey Dahlinf4a93112015-10-05 16:58:09 -0700331 if (item->GetId() < kMinUserSetMethodId ||
332 item->GetId() > kMaxUserSetMethodId) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700333 fprintf(stderr, "%s:%d Found out of bounds id (%d) for method: %s\n",
Casey Dahlinf4a93112015-10-05 16:58:09 -0700334 filename, item->GetLine(),
335 item->GetId(), item->GetName().c_str());
Casey Dahlindff80e52015-09-29 13:57:06 -0700336 fprintf(stderr, " Value for id must be between %d and %d inclusive.\n",
337 kMinUserSetMethodId, kMaxUserSetMethodId);
338 return 1;
339 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700340 usedIds.insert(item->GetId());
Casey Dahlindff80e52015-09-29 13:57:06 -0700341 } else {
342 hasUnassignedIds = true;
343 }
344 if (hasAssignedIds && hasUnassignedIds) {
345 fprintf(stderr,
346 "%s: You must either assign id's to all methods or to none of them.\n",
347 filename);
348 return 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800349 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800350 }
351
352 // In the case that all methods have unassigned id's, set a unique id for them.
353 if (hasUnassignedIds) {
354 int newId = 0;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700355 for (const auto& item : items) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700356 item->SetId(newId++);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800357 }
358 }
359
360 // success
361 return 0;
362}
363
Christopher Wileyef140932015-11-03 09:29:19 -0800364// TODO: Remove this in favor of using the YACC parser b/25479378
365bool ParsePreprocessedLine(const string& line, string* decl,
366 vector<string>* package, string* class_name) {
367 // erase all trailing whitespace and semicolons
368 const size_t end = line.find_last_not_of(" ;\t");
369 if (end == string::npos) {
370 return false;
371 }
372 if (line.rfind(';', end) != string::npos) {
373 return false;
374 }
375
376 decl->clear();
377 string type;
378 vector<string> pieces = Split(line.substr(0, end + 1), " \t");
379 for (const string& piece : pieces) {
380 if (piece.empty()) {
381 continue;
382 }
383 if (decl->empty()) {
384 *decl = std::move(piece);
385 } else if (type.empty()) {
386 type = std::move(piece);
387 } else {
388 return false;
389 }
390 }
391
392 // Note that this logic is absolutely wrong. Given a parcelable
393 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
394 // the class is just Bar. However, this was the way it was done in the past.
395 //
396 // See b/17415692
397 size_t dot_pos = type.rfind('.');
398 if (dot_pos != string::npos) {
399 *class_name = type.substr(dot_pos + 1);
400 *package = Split(type.substr(0, dot_pos), ".");
401 } else {
402 *class_name = type;
403 package->clear();
404 }
405
406 return true;
407}
408
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700409} // namespace
410
411namespace internals {
412
Christopher Wileyef140932015-11-03 09:29:19 -0800413bool parse_preprocessed_file(const IoDelegate& io_delegate,
414 const string& filename, TypeNamespace* types) {
415 bool success = true;
416 unique_ptr<LineReader> line_reader = io_delegate.GetLineReader(filename);
417 if (!line_reader) {
418 LOG(ERROR) << "cannot open preprocessed file: " << filename;
419 success = false;
420 return success;
421 }
422
423 string line;
424 unsigned lineno = 1;
425 for ( ; line_reader->ReadLine(&line); ++lineno) {
426 if (line.empty() || line.compare(0, 2, "//") == 0) {
427 // skip comments and empty lines
428 continue;
429 }
430
431 string decl;
432 vector<string> package;
433 string class_name;
434 if (!ParsePreprocessedLine(line, &decl, &package, &class_name)) {
435 success = false;
436 break;
437 }
438
439 if (decl == "parcelable") {
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800440 AidlParcelable doc(new AidlQualifiedName(class_name, ""),
441 lineno, package);
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800442 types->AddParcelableType(doc, filename);
Christopher Wileyef140932015-11-03 09:29:19 -0800443 } else if (decl == "interface") {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800444 auto temp = new std::vector<std::unique_ptr<AidlMember>>();
Christopher Wileyef140932015-11-03 09:29:19 -0800445 AidlInterface doc(class_name, lineno, "", false, temp, package);
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800446 types->AddBinderType(doc, filename);
Christopher Wileyef140932015-11-03 09:29:19 -0800447 } 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 Dahlinc1f39b42015-11-24 10:34:34 -0800470 std::map<AidlImport*,std::unique_ptr<AidlDocument>> docs;
Casey Dahlin624358c2015-10-12 19:29:51 -0700471
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 Dahlinc1f39b42015-11-24 10:34:34 -0800488 AidlDocument* parsed_doc = p.GetDocument();
489
490 unique_ptr<AidlInterface> interface(parsed_doc->ReleaseInterface());
491
492 if (!interface) {
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
Casey Dahlin42727f82015-10-12 19:23:40 -0700498 if (!check_filename(input_file_name.c_str(), interface->GetPackage(),
Christopher Wiley632801d2015-11-05 14:15:49 -0800499 interface->GetName(), interface->GetLine()) ||
500 !types->IsValidPackage(interface->GetPackage())) {
501 LOG(ERROR) << "Invalid package declaration '" << interface->GetPackage()
502 << "'";
503 return AidlError::BAD_PACKAGE;
504 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800505
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700506 // parse the imports of the input file
Christopher Wiley72877ac2015-10-06 14:41:42 -0700507 ImportResolver import_resolver{io_delegate, import_paths};
Casey Dahlin0edf3422015-10-07 12:34:59 -0700508 for (auto& import : p.GetImports()) {
509 if (types->HasType(import->GetNeededClass())) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700510 // There are places in the Android tree where an import doesn't resolve,
511 // but we'll pick the type up through the preprocessed types.
512 // This seems like an error, but legacy support demands we support it...
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700513 continue;
514 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700515 string import_path = import_resolver.FindImportFile(import->GetNeededClass());
Christopher Wiley72877ac2015-10-06 14:41:42 -0700516 if (import_path.empty()) {
Casey Dahlin0edf3422015-10-07 12:34:59 -0700517 cerr << import->GetFileFrom() << ":" << import->GetLine()
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700518 << ": couldn't find import for class "
Casey Dahlin0edf3422015-10-07 12:34:59 -0700519 << import->GetNeededClass() << endl;
Christopher Wiley632801d2015-11-05 14:15:49 -0800520 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700521 continue;
522 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700523 import->SetFilename(import_path);
Casey Dahlin2cc93162015-10-02 16:14:17 -0700524
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700525 Parser p{io_delegate};
Casey Dahlin0edf3422015-10-07 12:34:59 -0700526 if (!p.ParseFile(import->GetFilename())) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700527 cerr << "error while parsing import for class "
Casey Dahlin0edf3422015-10-07 12:34:59 -0700528 << import->GetNeededClass() << endl;
Christopher Wiley632801d2015-11-05 14:15:49 -0800529 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700530 continue;
531 }
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700532
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800533 std::unique_ptr<AidlDocument> document(p.ReleaseDocument());
534 if (!check_filenames(import->GetFilename(), document.get()))
Christopher Wiley632801d2015-11-05 14:15:49 -0800535 err = AidlError::BAD_IMPORT;
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800536 docs[import.get()] = std::move(document);
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700537 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800538 if (err != AidlError::OK) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700539 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700540 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800541
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700542 // gather the types that have been declared
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800543 if (!types->AddBinderType(*interface.get(), input_file_name)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800544 err = AidlError::BAD_TYPE;
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700545 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800546
Casey Dahlin0edf3422015-10-07 12:34:59 -0700547 for (const auto& import : p.GetImports()) {
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800548 // If we skipped an unresolved import above (see comment there) we'll have
549 // an empty bucket here.
550 const auto import_itr = docs.find(import.get());
551 if (import_itr == docs.cend()) {
552 continue;
553 }
554
555 if (!gather_types(import->GetFilename(), import_itr->second.get(), types)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800556 err = AidlError::BAD_TYPE;
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700557 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700558 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800559
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700560 // check the referenced types in parsed_doc to make sure we've imported them
Christopher Wiley632801d2015-11-05 14:15:49 -0800561 if (check_types(input_file_name, interface.get(), types) != 0) {
562 err = AidlError::BAD_TYPE;
563 }
564 if (err != AidlError::OK) {
565 return err;
566 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800567
Adam Lesinskiffa16862014-01-23 18:17:42 -0800568
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700569 // assign method ids and validate.
Christopher Wiley632801d2015-11-05 14:15:49 -0800570 if (check_and_assign_method_ids(input_file_name.c_str(),
571 interface->GetMethods()) != 0) {
572 return AidlError::BAD_METHOD_ID;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700573 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800574
Casey Dahlin0edf3422015-10-07 12:34:59 -0700575 if (returned_interface)
Christopher Wiley90be4e32015-10-20 14:55:25 -0700576 *returned_interface = std::move(interface);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700577
578 if (returned_imports)
579 p.ReleaseImports(returned_imports);
580
Christopher Wiley632801d2015-11-05 14:15:49 -0800581 return AidlError::OK;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700582}
583
Casey Dahlin2cc93162015-10-02 16:14:17 -0700584} // namespace internals
585
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700586int compile_aidl_to_cpp(const CppOptions& options,
587 const IoDelegate& io_delegate) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700588 unique_ptr<AidlInterface> interface;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700589 std::vector<std::unique_ptr<AidlImport>> imports;
Christopher Wileye3550c62015-09-29 13:26:10 -0700590 unique_ptr<cpp::TypeNamespace> types(new cpp::TypeNamespace());
Christopher Wiley56799522015-10-31 10:17:04 -0700591 types->Init();
Christopher Wiley632801d2015-11-05 14:15:49 -0800592 AidlError err = internals::load_and_validate_aidl(
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700593 std::vector<std::string>{}, // no preprocessed files
594 options.ImportPaths(),
595 options.InputFileName(),
596 io_delegate,
597 types.get(),
598 &interface,
599 &imports);
Christopher Wiley632801d2015-11-05 14:15:49 -0800600 if (err != AidlError::OK) {
601 return 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700602 }
603
Christopher Wiley19059cb2015-11-05 16:11:56 -0800604 string dep_file_name = options.DependencyFilePath();
605 if (!dep_file_name.empty()) {
606 generate_dep_file(dep_file_name, options.InputFileName(),
607 options.OutputCppFilePath(), imports, io_delegate);
608 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700609
Christopher Wiley054afbd2015-10-16 17:08:43 -0700610 return (cpp::GenerateCpp(options, *types, *interface, io_delegate)) ? 0 : 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700611}
612
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700613int compile_aidl_to_java(const JavaOptions& options,
614 const IoDelegate& io_delegate) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700615 unique_ptr<AidlInterface> interface;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700616 std::vector<std::unique_ptr<AidlImport>> imports;
Christopher Wileydb154a52015-09-28 16:32:25 -0700617 unique_ptr<java::JavaTypeNamespace> types(new java::JavaTypeNamespace());
Christopher Wiley56799522015-10-31 10:17:04 -0700618 types->Init();
Christopher Wiley632801d2015-11-05 14:15:49 -0800619 AidlError aidl_err = internals::load_and_validate_aidl(
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700620 options.preprocessed_files_,
621 options.import_paths_,
622 options.input_file_name_,
623 io_delegate,
624 types.get(),
625 &interface,
626 &imports);
Christopher Wiley632801d2015-11-05 14:15:49 -0800627 if (aidl_err == AidlError::FOUND_PARCELABLE && !options.fail_on_parcelable_) {
628 // We aborted code generation because this file contains parcelables.
629 // However, we were not told to complain if we find parcelables.
630 // Just exit quietly.
631 return 0;
632 }
633 if (aidl_err != AidlError::OK) {
634 return 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700635 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700636
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700637 string output_file_name = options.output_file_name_;
638 // if needed, generate the output file name from the base folder
Christopher Wiley632801d2015-11-05 14:15:49 -0800639 if (output_file_name.empty() && !options.output_base_folder_.empty()) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700640 output_file_name = generate_outputFileName(options, *interface);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700641 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800642
Casey Dahlin64533512015-10-23 17:11:21 -0700643 // make sure the folders of the output file all exists
644 if (!io_delegate.CreatePathForFile(output_file_name)) {
645 return 1;
646 }
647
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700648 // if we were asked to, generate a make dependency file
Christopher Wileyef4132c2015-11-05 15:47:40 -0800649 string dep_file_name = options.DependencyFilePath();
650 if (!dep_file_name.empty()) {
651 generate_dep_file(dep_file_name, options.input_file_name_,
652 options.output_file_name_, imports, io_delegate);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700653 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800654
Christopher Wiley632801d2015-11-05 14:15:49 -0800655 return generate_java(output_file_name, options.input_file_name_.c_str(),
656 interface.get(), types.get(), io_delegate);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800657}
658
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800659bool preprocess_aidl(const JavaOptions& options,
660 const IoDelegate& io_delegate) {
661 unique_ptr<CodeWriter> writer =
662 io_delegate.GetCodeWriter(options.output_file_name_);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800663
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800664 for (const auto& file : options.files_to_preprocess_) {
665 Parser p{io_delegate};
666 if (!p.ParseFile(file))
667 return false;
668 AidlDocument* doc = p.GetDocument();
669 string line;
Casey Dahlin59401da2015-10-09 18:16:45 -0700670
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800671 const AidlInterface* interface = doc->GetInterface();
Casey Dahlin59401da2015-10-09 18:16:45 -0700672
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800673 if (interface != nullptr &&
674 !writer->Write("interface %s;\n",
675 interface->GetCanonicalName().c_str())) {
676 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800677 }
678
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800679 for (const auto& parcelable : doc->GetParcelables()) {
680 if (!writer->Write("parcelable %s;\n",
681 parcelable->GetCanonicalName().c_str())) {
682 return false;
683 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800684 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800685 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800686
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800687 return writer->Close();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800688}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700689
690} // namespace android
691} // namespace aidl