blob: 6ca7bd573d20357d7849f752a16e293249507cc5 [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 Dahlina2f77c42015-12-01 18:26:02 -0800201 const ValidatableType* return_type =
202 types->GetValidatableType(m->GetType().GetName());
203
204 if (m->GetType().IsArray()) {
205 return_type = return_type->ArrayType();
206 }
207
208 m->GetMutableType()->SetLanguageType(return_type);
209
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700210 if (oneway && m->GetType().GetName() != "void") {
211 cerr << filename << ":" << m->GetLine()
Christopher Wiley45db9ee2015-10-26 18:53:53 -0700212 << " oneway method '" << m->GetName() << "' cannot return a value"
213 << endl;
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700214 err = 1;
215 }
216
Adam Lesinskiffa16862014-01-23 18:17:42 -0800217 int index = 1;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700218 for (const auto& arg : m->GetArguments()) {
Christopher Wileycd8e8972015-10-26 10:24:35 -0700219 if (!types->MaybeAddContainerType(arg->GetType().GetName()) ||
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700220 !types->IsValidArg(*arg, index, filename)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700221 err = 1;
222 }
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700223
Casey Dahlina2f77c42015-12-01 18:26:02 -0800224 const ValidatableType* arg_type =
225 types->GetValidatableType(arg->GetType().GetName());
226
227 if (arg->GetType().IsArray()) {
228 arg_type = arg_type->ArrayType();
229 }
230
231 arg->GetMutableType()->SetLanguageType(arg_type);
232
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700233 if (oneway && arg->IsOut()) {
234 cerr << filename << ":" << m->GetLine()
Christopher Wiley45db9ee2015-10-26 18:53:53 -0700235 << " oneway method '" << m->GetName()
236 << "' cannot have out parameters" << endl;
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700237 err = 1;
238 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800239 }
240
Casey Dahlinf4a93112015-10-05 16:58:09 -0700241 auto it = method_names.find(m->GetName());
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700242 // prevent duplicate methods
Casey Dahlinf4a93112015-10-05 16:58:09 -0700243 if (it == method_names.end()) {
244 method_names[m->GetName()] = m.get();
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700245 } else {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700246 cerr << filename << ":" << m->GetLine()
247 << " attempt to redefine method " << m->GetName() << "," << endl
248 << filename << ":" << it->second->GetLine()
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700249 << " previously defined here." << endl;
250 err = 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800251 }
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700252 }
253 return err;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800254}
255
Christopher Wileyef4132c2015-11-05 15:47:40 -0800256void generate_dep_file(const std::string& dep_file_name,
257 const std::string& input_file_name,
258 const std::string& output_file_name,
Christopher Wileya30a45e2015-10-17 10:56:59 -0700259 const std::vector<std::unique_ptr<AidlImport>>& imports,
260 const IoDelegate& io_delegate) {
Christopher Wileyef4132c2015-11-05 15:47:40 -0800261 CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name);
Christopher Wileya30a45e2015-10-17 10:56:59 -0700262 if (!writer) {
Christopher Wileyef4132c2015-11-05 15:47:40 -0800263 cerr << "Could not open " << dep_file_name << endl;
Christopher Wileya30a45e2015-10-17 10:56:59 -0700264 return;
265 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700266
Christopher Wileya30a45e2015-10-17 10:56:59 -0700267
Christopher Wileyef4132c2015-11-05 15:47:40 -0800268 writer->Write("%s: \\\n", output_file_name.c_str());
269 writer->Write(" %s %s\n", input_file_name.c_str(),
Christopher Wiley90be4e32015-10-20 14:55:25 -0700270 imports.empty() ? "" : "\\");
Christopher Wileya30a45e2015-10-17 10:56:59 -0700271
272 bool first = true;
273 for (const auto& import : imports) {
274 if (! first) {
275 writer->Write(" \\\n");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800276 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700277 first = false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800278
Christopher Wileya30a45e2015-10-17 10:56:59 -0700279 if (! import->GetFilename().empty()) {
280 writer->Write(" %s", import->GetFilename().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800281 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700282 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800283
Christopher Wileya30a45e2015-10-17 10:56:59 -0700284 writer->Write(first ? "\n" : "\n\n");
285
286 // Output "<input_aidl_file>: " so make won't fail if the input .aidl file
287 // has been deleted, moved or renamed in incremental build.
Christopher Wileyef4132c2015-11-05 15:47:40 -0800288 writer->Write("%s :\n", input_file_name.c_str());
Christopher Wileya30a45e2015-10-17 10:56:59 -0700289
290 // Output "<imported_file>: " so make won't fail if the imported file has
291 // been deleted, moved or renamed in incremental build.
292 for (const auto& import : imports) {
293 if (! import->GetFilename().empty()) {
294 writer->Write("%s :\n", import->GetFilename().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800295 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700296 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800297}
298
Christopher Wiley90be4e32015-10-20 14:55:25 -0700299string generate_outputFileName(const JavaOptions& options,
300 const AidlInterface& interface) {
301 string name = interface.GetName();
302 string package = interface.GetPackage();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800303 string result;
304
305 // create the path to the destination folder based on the
306 // interface package name
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700307 result = options.output_base_folder_;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800308 result += OS_PATH_SEPARATOR;
309
310 string packageStr = package;
311 size_t len = packageStr.length();
312 for (size_t i=0; i<len; i++) {
313 if (packageStr[i] == '.') {
314 packageStr[i] = OS_PATH_SEPARATOR;
315 }
316 }
317
318 result += packageStr;
319
320 // add the filename by replacing the .aidl extension to .java
Adam Lesinskiffa16862014-01-23 18:17:42 -0800321 result += OS_PATH_SEPARATOR;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700322 result.append(name, 0, name.find('.'));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800323 result += ".java";
324
325 return result;
326}
327
Christopher Wileyf690be52015-09-14 15:19:10 -0700328int check_and_assign_method_ids(const char * filename,
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700329 const std::vector<std::unique_ptr<AidlMethod>>& items) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800330 // Check whether there are any methods with manually assigned id's and any that are not.
331 // Either all method id's must be manually assigned or all of them must not.
332 // Also, check for duplicates of user set id's and that the id's are within the proper bounds.
333 set<int> usedIds;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800334 bool hasUnassignedIds = false;
335 bool hasAssignedIds = false;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700336 for (const auto& item : items) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700337 if (item->HasId()) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700338 hasAssignedIds = true;
Casey Dahlindff80e52015-09-29 13:57:06 -0700339 // Ensure that the user set id is not duplicated.
Casey Dahlinf4a93112015-10-05 16:58:09 -0700340 if (usedIds.find(item->GetId()) != usedIds.end()) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700341 // We found a duplicate id, so throw an error.
Adam Lesinskiffa16862014-01-23 18:17:42 -0800342 fprintf(stderr,
Casey Dahlindff80e52015-09-29 13:57:06 -0700343 "%s:%d Found duplicate method id (%d) for method: %s\n",
Casey Dahlinf4a93112015-10-05 16:58:09 -0700344 filename, item->GetLine(),
345 item->GetId(), item->GetName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800346 return 1;
347 }
Casey Dahlindff80e52015-09-29 13:57:06 -0700348 // Ensure that the user set id is within the appropriate limits
Casey Dahlinf4a93112015-10-05 16:58:09 -0700349 if (item->GetId() < kMinUserSetMethodId ||
350 item->GetId() > kMaxUserSetMethodId) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700351 fprintf(stderr, "%s:%d Found out of bounds id (%d) for method: %s\n",
Casey Dahlinf4a93112015-10-05 16:58:09 -0700352 filename, item->GetLine(),
353 item->GetId(), item->GetName().c_str());
Casey Dahlindff80e52015-09-29 13:57:06 -0700354 fprintf(stderr, " Value for id must be between %d and %d inclusive.\n",
355 kMinUserSetMethodId, kMaxUserSetMethodId);
356 return 1;
357 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700358 usedIds.insert(item->GetId());
Casey Dahlindff80e52015-09-29 13:57:06 -0700359 } else {
360 hasUnassignedIds = true;
361 }
362 if (hasAssignedIds && hasUnassignedIds) {
363 fprintf(stderr,
364 "%s: You must either assign id's to all methods or to none of them.\n",
365 filename);
366 return 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800367 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800368 }
369
370 // In the case that all methods have unassigned id's, set a unique id for them.
371 if (hasUnassignedIds) {
372 int newId = 0;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700373 for (const auto& item : items) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700374 item->SetId(newId++);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800375 }
376 }
377
378 // success
379 return 0;
380}
381
Christopher Wileyef140932015-11-03 09:29:19 -0800382// TODO: Remove this in favor of using the YACC parser b/25479378
383bool ParsePreprocessedLine(const string& line, string* decl,
384 vector<string>* package, string* class_name) {
385 // erase all trailing whitespace and semicolons
386 const size_t end = line.find_last_not_of(" ;\t");
387 if (end == string::npos) {
388 return false;
389 }
390 if (line.rfind(';', end) != string::npos) {
391 return false;
392 }
393
394 decl->clear();
395 string type;
396 vector<string> pieces = Split(line.substr(0, end + 1), " \t");
397 for (const string& piece : pieces) {
398 if (piece.empty()) {
399 continue;
400 }
401 if (decl->empty()) {
402 *decl = std::move(piece);
403 } else if (type.empty()) {
404 type = std::move(piece);
405 } else {
406 return false;
407 }
408 }
409
410 // Note that this logic is absolutely wrong. Given a parcelable
411 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
412 // the class is just Bar. However, this was the way it was done in the past.
413 //
414 // See b/17415692
415 size_t dot_pos = type.rfind('.');
416 if (dot_pos != string::npos) {
417 *class_name = type.substr(dot_pos + 1);
418 *package = Split(type.substr(0, dot_pos), ".");
419 } else {
420 *class_name = type;
421 package->clear();
422 }
423
424 return true;
425}
426
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700427} // namespace
428
429namespace internals {
430
Christopher Wileyef140932015-11-03 09:29:19 -0800431bool parse_preprocessed_file(const IoDelegate& io_delegate,
432 const string& filename, TypeNamespace* types) {
433 bool success = true;
434 unique_ptr<LineReader> line_reader = io_delegate.GetLineReader(filename);
435 if (!line_reader) {
436 LOG(ERROR) << "cannot open preprocessed file: " << filename;
437 success = false;
438 return success;
439 }
440
441 string line;
442 unsigned lineno = 1;
443 for ( ; line_reader->ReadLine(&line); ++lineno) {
444 if (line.empty() || line.compare(0, 2, "//") == 0) {
445 // skip comments and empty lines
446 continue;
447 }
448
449 string decl;
450 vector<string> package;
451 string class_name;
452 if (!ParsePreprocessedLine(line, &decl, &package, &class_name)) {
453 success = false;
454 break;
455 }
456
457 if (decl == "parcelable") {
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800458 AidlParcelable doc(new AidlQualifiedName(class_name, ""),
459 lineno, package);
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800460 types->AddParcelableType(doc, filename);
Christopher Wileyef140932015-11-03 09:29:19 -0800461 } else if (decl == "interface") {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800462 auto temp = new std::vector<std::unique_ptr<AidlMember>>();
Christopher Wileyef140932015-11-03 09:29:19 -0800463 AidlInterface doc(class_name, lineno, "", false, temp, package);
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800464 types->AddBinderType(doc, filename);
Christopher Wileyef140932015-11-03 09:29:19 -0800465 } else {
466 success = false;
467 break;
468 }
469 }
470 if (!success) {
471 LOG(ERROR) << filename << ':' << lineno
472 << " malformed preprocessed file line: '" << line << "'";
473 }
474
475 return success;
476}
477
Christopher Wiley632801d2015-11-05 14:15:49 -0800478AidlError load_and_validate_aidl(
479 const std::vector<std::string> preprocessed_files,
480 const std::vector<std::string> import_paths,
481 const std::string& input_file_name,
482 const IoDelegate& io_delegate,
483 TypeNamespace* types,
484 std::unique_ptr<AidlInterface>* returned_interface,
485 std::vector<std::unique_ptr<AidlImport>>* returned_imports) {
486 AidlError err = AidlError::OK;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800487
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800488 std::map<AidlImport*,std::unique_ptr<AidlDocument>> docs;
Casey Dahlin624358c2015-10-12 19:29:51 -0700489
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700490 // import the preprocessed file
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700491 for (const string& s : preprocessed_files) {
Christopher Wileyef140932015-11-03 09:29:19 -0800492 if (!parse_preprocessed_file(io_delegate, s, types)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800493 err = AidlError::BAD_PRE_PROCESSED_FILE;
Christopher Wileyef140932015-11-03 09:29:19 -0800494 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700495 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800496 if (err != AidlError::OK) {
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700497 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 Wiley632801d2015-11-05 14:15:49 -0800503 return AidlError::PARSE_ERROR;
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700504 }
Casey Dahlin2cc93162015-10-02 16:14:17 -0700505
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800506 AidlDocument* parsed_doc = p.GetDocument();
507
508 unique_ptr<AidlInterface> interface(parsed_doc->ReleaseInterface());
509
510 if (!interface) {
Christopher Wileye60d99c2015-11-06 13:14:24 -0800511 LOG(ERROR) << "refusing to generate code from aidl file defining "
512 "parcelable";
Christopher Wiley632801d2015-11-05 14:15:49 -0800513 return AidlError::FOUND_PARCELABLE;
514 }
515
Casey Dahlin42727f82015-10-12 19:23:40 -0700516 if (!check_filename(input_file_name.c_str(), interface->GetPackage(),
Christopher Wiley632801d2015-11-05 14:15:49 -0800517 interface->GetName(), interface->GetLine()) ||
518 !types->IsValidPackage(interface->GetPackage())) {
519 LOG(ERROR) << "Invalid package declaration '" << interface->GetPackage()
520 << "'";
521 return AidlError::BAD_PACKAGE;
522 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800523
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700524 // parse the imports of the input file
Christopher Wiley72877ac2015-10-06 14:41:42 -0700525 ImportResolver import_resolver{io_delegate, import_paths};
Casey Dahlin0edf3422015-10-07 12:34:59 -0700526 for (auto& import : p.GetImports()) {
527 if (types->HasType(import->GetNeededClass())) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700528 // There are places in the Android tree where an import doesn't resolve,
529 // but we'll pick the type up through the preprocessed types.
530 // This seems like an error, but legacy support demands we support it...
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700531 continue;
532 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700533 string import_path = import_resolver.FindImportFile(import->GetNeededClass());
Christopher Wiley72877ac2015-10-06 14:41:42 -0700534 if (import_path.empty()) {
Casey Dahlin0edf3422015-10-07 12:34:59 -0700535 cerr << import->GetFileFrom() << ":" << import->GetLine()
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700536 << ": couldn't find import for class "
Casey Dahlin0edf3422015-10-07 12:34:59 -0700537 << import->GetNeededClass() << endl;
Christopher Wiley632801d2015-11-05 14:15:49 -0800538 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700539 continue;
540 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700541 import->SetFilename(import_path);
Casey Dahlin2cc93162015-10-02 16:14:17 -0700542
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700543 Parser p{io_delegate};
Casey Dahlin0edf3422015-10-07 12:34:59 -0700544 if (!p.ParseFile(import->GetFilename())) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700545 cerr << "error while parsing import for class "
Casey Dahlin0edf3422015-10-07 12:34:59 -0700546 << import->GetNeededClass() << endl;
Christopher Wiley632801d2015-11-05 14:15:49 -0800547 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700548 continue;
549 }
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700550
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800551 std::unique_ptr<AidlDocument> document(p.ReleaseDocument());
552 if (!check_filenames(import->GetFilename(), document.get()))
Christopher Wiley632801d2015-11-05 14:15:49 -0800553 err = AidlError::BAD_IMPORT;
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800554 docs[import.get()] = std::move(document);
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700555 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800556 if (err != AidlError::OK) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700557 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700558 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800559
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700560 // gather the types that have been declared
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800561 if (!types->AddBinderType(*interface.get(), input_file_name)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800562 err = AidlError::BAD_TYPE;
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700563 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800564
Casey Dahlina2f77c42015-12-01 18:26:02 -0800565 interface->SetLanguageType(types->GetValidatableType(interface->GetCanonicalName()));
566
Casey Dahlin0edf3422015-10-07 12:34:59 -0700567 for (const auto& import : p.GetImports()) {
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800568 // If we skipped an unresolved import above (see comment there) we'll have
569 // an empty bucket here.
570 const auto import_itr = docs.find(import.get());
571 if (import_itr == docs.cend()) {
572 continue;
573 }
574
575 if (!gather_types(import->GetFilename(), import_itr->second.get(), types)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800576 err = AidlError::BAD_TYPE;
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700577 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700578 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800579
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700580 // check the referenced types in parsed_doc to make sure we've imported them
Christopher Wiley632801d2015-11-05 14:15:49 -0800581 if (check_types(input_file_name, interface.get(), types) != 0) {
582 err = AidlError::BAD_TYPE;
583 }
584 if (err != AidlError::OK) {
585 return err;
586 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800587
Adam Lesinskiffa16862014-01-23 18:17:42 -0800588
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700589 // assign method ids and validate.
Christopher Wiley632801d2015-11-05 14:15:49 -0800590 if (check_and_assign_method_ids(input_file_name.c_str(),
591 interface->GetMethods()) != 0) {
592 return AidlError::BAD_METHOD_ID;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700593 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800594
Casey Dahlin0edf3422015-10-07 12:34:59 -0700595 if (returned_interface)
Christopher Wiley90be4e32015-10-20 14:55:25 -0700596 *returned_interface = std::move(interface);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700597
598 if (returned_imports)
599 p.ReleaseImports(returned_imports);
600
Christopher Wiley632801d2015-11-05 14:15:49 -0800601 return AidlError::OK;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700602}
603
Casey Dahlin2cc93162015-10-02 16:14:17 -0700604} // namespace internals
605
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700606int compile_aidl_to_cpp(const CppOptions& options,
607 const IoDelegate& io_delegate) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700608 unique_ptr<AidlInterface> interface;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700609 std::vector<std::unique_ptr<AidlImport>> imports;
Christopher Wileye3550c62015-09-29 13:26:10 -0700610 unique_ptr<cpp::TypeNamespace> types(new cpp::TypeNamespace());
Christopher Wiley56799522015-10-31 10:17:04 -0700611 types->Init();
Christopher Wiley632801d2015-11-05 14:15:49 -0800612 AidlError err = internals::load_and_validate_aidl(
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700613 std::vector<std::string>{}, // no preprocessed files
614 options.ImportPaths(),
615 options.InputFileName(),
616 io_delegate,
617 types.get(),
618 &interface,
619 &imports);
Christopher Wiley632801d2015-11-05 14:15:49 -0800620 if (err != AidlError::OK) {
621 return 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700622 }
623
Christopher Wiley19059cb2015-11-05 16:11:56 -0800624 string dep_file_name = options.DependencyFilePath();
625 if (!dep_file_name.empty()) {
626 generate_dep_file(dep_file_name, options.InputFileName(),
627 options.OutputCppFilePath(), imports, io_delegate);
628 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700629
Christopher Wiley054afbd2015-10-16 17:08:43 -0700630 return (cpp::GenerateCpp(options, *types, *interface, io_delegate)) ? 0 : 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700631}
632
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700633int compile_aidl_to_java(const JavaOptions& options,
634 const IoDelegate& io_delegate) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700635 unique_ptr<AidlInterface> interface;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700636 std::vector<std::unique_ptr<AidlImport>> imports;
Christopher Wileydb154a52015-09-28 16:32:25 -0700637 unique_ptr<java::JavaTypeNamespace> types(new java::JavaTypeNamespace());
Christopher Wiley56799522015-10-31 10:17:04 -0700638 types->Init();
Christopher Wiley632801d2015-11-05 14:15:49 -0800639 AidlError aidl_err = internals::load_and_validate_aidl(
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700640 options.preprocessed_files_,
641 options.import_paths_,
642 options.input_file_name_,
643 io_delegate,
644 types.get(),
645 &interface,
646 &imports);
Christopher Wiley632801d2015-11-05 14:15:49 -0800647 if (aidl_err == AidlError::FOUND_PARCELABLE && !options.fail_on_parcelable_) {
648 // We aborted code generation because this file contains parcelables.
649 // However, we were not told to complain if we find parcelables.
650 // Just exit quietly.
651 return 0;
652 }
653 if (aidl_err != AidlError::OK) {
654 return 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700655 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700656
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700657 string output_file_name = options.output_file_name_;
658 // if needed, generate the output file name from the base folder
Christopher Wiley632801d2015-11-05 14:15:49 -0800659 if (output_file_name.empty() && !options.output_base_folder_.empty()) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700660 output_file_name = generate_outputFileName(options, *interface);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700661 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800662
Casey Dahlin64533512015-10-23 17:11:21 -0700663 // make sure the folders of the output file all exists
664 if (!io_delegate.CreatePathForFile(output_file_name)) {
665 return 1;
666 }
667
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700668 // if we were asked to, generate a make dependency file
Christopher Wileyef4132c2015-11-05 15:47:40 -0800669 string dep_file_name = options.DependencyFilePath();
670 if (!dep_file_name.empty()) {
671 generate_dep_file(dep_file_name, options.input_file_name_,
672 options.output_file_name_, imports, io_delegate);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700673 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800674
Christopher Wiley632801d2015-11-05 14:15:49 -0800675 return generate_java(output_file_name, options.input_file_name_.c_str(),
676 interface.get(), types.get(), io_delegate);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800677}
678
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800679bool preprocess_aidl(const JavaOptions& options,
680 const IoDelegate& io_delegate) {
681 unique_ptr<CodeWriter> writer =
682 io_delegate.GetCodeWriter(options.output_file_name_);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800683
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800684 for (const auto& file : options.files_to_preprocess_) {
685 Parser p{io_delegate};
686 if (!p.ParseFile(file))
687 return false;
688 AidlDocument* doc = p.GetDocument();
689 string line;
Casey Dahlin59401da2015-10-09 18:16:45 -0700690
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800691 const AidlInterface* interface = doc->GetInterface();
Casey Dahlin59401da2015-10-09 18:16:45 -0700692
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800693 if (interface != nullptr &&
694 !writer->Write("interface %s;\n",
695 interface->GetCanonicalName().c_str())) {
696 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800697 }
698
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800699 for (const auto& parcelable : doc->GetParcelables()) {
700 if (!writer->Write("parcelable %s;\n",
701 parcelable->GetCanonicalName().c_str())) {
702 return false;
703 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800704 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800705 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800706
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800707 return writer->Close();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800708}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700709
710} // namespace android
711} // namespace aidl