blob: 3602d1e86b692709103239a3beac77ee742e61a3 [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 Wiley3a9911c2016-01-19 12:59:09 -080052using android::base::Join;
Christopher Wileyd76067c2015-10-19 17:00:13 -070053using android::base::Split;
Christopher Wileyc16e5e72015-09-16 10:49:40 -070054using std::cerr;
55using std::endl;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070056using std::map;
57using std::set;
58using std::string;
Christopher Wiley84c1eac2015-09-23 13:29:28 -070059using std::unique_ptr;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070060using std::vector;
Adam Lesinskiffa16862014-01-23 18:17:42 -080061
Christopher Wileyf690be52015-09-14 15:19:10 -070062namespace android {
63namespace aidl {
64namespace {
Adam Lesinskiffa16862014-01-23 18:17:42 -080065
Christopher Wileyf690be52015-09-14 15:19:10 -070066// The following are gotten as the offset from the allowable id's between
67// android.os.IBinder.FIRST_CALL_TRANSACTION=1 and
68// android.os.IBinder.LAST_CALL_TRANSACTION=16777215
69const int kMinUserSetMethodId = 0;
70const int kMaxUserSetMethodId = 16777214;
Adam Lesinskiffa16862014-01-23 18:17:42 -080071
Casey Dahlin42727f82015-10-12 19:23:40 -070072bool check_filename(const std::string& filename,
73 const std::string& package,
74 const std::string& name,
75 unsigned line) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080076 const char* p;
77 string expected;
78 string fn;
79 size_t len;
80 char cwd[MAXPATHLEN];
81 bool valid = false;
82
Elliott Hughesce310da2015-07-29 08:44:17 -070083#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -080084 if (isalpha(filename[0]) && filename[1] == ':'
85 && filename[2] == OS_PATH_SEPARATOR) {
86#else
87 if (filename[0] == OS_PATH_SEPARATOR) {
88#endif
89 fn = filename;
90 } else {
91 fn = getcwd(cwd, sizeof(cwd));
92 len = fn.length();
93 if (fn[len-1] != OS_PATH_SEPARATOR) {
94 fn += OS_PATH_SEPARATOR;
95 }
96 fn += filename;
97 }
98
Casey Dahlinfb7da2e2015-10-08 17:26:09 -070099 if (!package.empty()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800100 expected = package;
101 expected += '.';
102 }
103
104 len = expected.length();
105 for (size_t i=0; i<len; i++) {
106 if (expected[i] == '.') {
107 expected[i] = OS_PATH_SEPARATOR;
108 }
109 }
110
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700111 expected.append(name, 0, name.find('.'));
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700112
Adam Lesinskiffa16862014-01-23 18:17:42 -0800113 expected += ".aidl";
114
115 len = fn.length();
116 valid = (len >= expected.length());
117
118 if (valid) {
119 p = fn.c_str() + (len - expected.length());
120
Elliott Hughesce310da2015-07-29 08:44:17 -0700121#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800122 if (OS_PATH_SEPARATOR != '/') {
123 // Input filename under cygwin most likely has / separators
124 // whereas the expected string uses \\ separators. Adjust
125 // them accordingly.
126 for (char *c = const_cast<char *>(p); *c; ++c) {
127 if (*c == '/') *c = OS_PATH_SEPARATOR;
128 }
129 }
130#endif
131
Yabin Cui482eefb2014-11-10 15:01:43 -0800132 // aidl assumes case-insensitivity on Mac Os and Windows.
133#if defined(__linux__)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800134 valid = (expected == p);
135#else
136 valid = !strcasecmp(expected.c_str(), p);
137#endif
138 }
139
140 if (!valid) {
141 fprintf(stderr, "%s:%d interface %s should be declared in a file"
142 " called %s.\n",
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700143 filename.c_str(), line, name.c_str(), expected.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800144 }
145
Casey Dahlin42727f82015-10-12 19:23:40 -0700146 return valid;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800147}
148
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800149bool check_filenames(const std::string& filename, const AidlDocument* doc) {
150 if (!doc)
Casey Dahlin42727f82015-10-12 19:23:40 -0700151 return true;
152
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800153 const AidlInterface* interface = doc->GetInterface();
154
155 if (interface) {
156 return check_filename(filename, interface->GetPackage(),
157 interface->GetName(), interface->GetLine());
Casey Dahlin42727f82015-10-12 19:23:40 -0700158 }
159
160 bool success = true;
161
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800162 for (const auto& item : doc->GetParcelables()) {
163 success &= check_filename(filename, item->GetPackage(), item->GetName(),
164 item->GetLine());
165 }
Casey Dahlin42727f82015-10-12 19:23:40 -0700166
167 return success;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800168}
169
Casey Dahlin0edf3422015-10-07 12:34:59 -0700170bool gather_types(const std::string& filename,
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800171 const AidlDocument* doc,
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700172 TypeNamespace* types) {
173 bool success = true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800174
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800175 const AidlInterface* interface = doc->GetInterface();
Casey Dahlin42727f82015-10-12 19:23:40 -0700176
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800177 if (interface)
178 return types->AddBinderType(*interface, filename);
Casey Dahlin42727f82015-10-12 19:23:40 -0700179
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800180 for (const auto& item : doc->GetParcelables()) {
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
Casey Dahlinc5afb402016-03-01 13:54:05 -0800192 if (c->IsUtf8() && c->IsUtf8InCpp()) {
193 cerr << filename << ":" << c->GetLine()
194 << "Interface cannot be marked as both @utf8 and @utf8InCpp";
195 err = 1;
196 }
197
Casey Dahlinf4a93112015-10-05 16:58:09 -0700198 // Has to be a pointer due to deleting copy constructor. No idea why.
199 map<string, const AidlMethod*> method_names;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700200 for (const auto& m : c->GetMethods()) {
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700201 bool oneway = m->IsOneway() || c->IsOneway();
202
Christopher Wiley934a82d2016-01-27 13:02:24 -0800203 if (!types->MaybeAddContainerType(m->GetType())) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700204 err = 1; // return type is invalid
Adam Lesinskiffa16862014-01-23 18:17:42 -0800205 }
206
Casey Dahlina2f77c42015-12-01 18:26:02 -0800207 const ValidatableType* return_type =
Casey Dahlinc5afb402016-03-01 13:54:05 -0800208 types->GetReturnType(m->GetType(), filename, *c);
Casey Dahlina2f77c42015-12-01 18:26:02 -0800209
Casey Dahlin57dbe242015-12-04 11:44:02 -0800210 if (!return_type) {
211 err = 1;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800212 }
213
214 m->GetMutableType()->SetLanguageType(return_type);
215
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700216 if (oneway && m->GetType().GetName() != "void") {
217 cerr << filename << ":" << m->GetLine()
Christopher Wiley45db9ee2015-10-26 18:53:53 -0700218 << " oneway method '" << m->GetName() << "' cannot return a value"
219 << endl;
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700220 err = 1;
221 }
222
Adam Lesinskiffa16862014-01-23 18:17:42 -0800223 int index = 1;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700224 for (const auto& arg : m->GetArguments()) {
Christopher Wiley934a82d2016-01-27 13:02:24 -0800225 if (!types->MaybeAddContainerType(arg->GetType())) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700226 err = 1;
227 }
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700228
Casey Dahlina2f77c42015-12-01 18:26:02 -0800229 const ValidatableType* arg_type =
Casey Dahlinc5afb402016-03-01 13:54:05 -0800230 types->GetArgType(*arg, index, filename, *c);
Casey Dahlina2f77c42015-12-01 18:26:02 -0800231
Casey Dahlin57dbe242015-12-04 11:44:02 -0800232 if (!arg_type) {
233 err = 1;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800234 }
235
236 arg->GetMutableType()->SetLanguageType(arg_type);
237
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700238 if (oneway && arg->IsOut()) {
239 cerr << filename << ":" << m->GetLine()
Christopher Wiley45db9ee2015-10-26 18:53:53 -0700240 << " oneway method '" << m->GetName()
241 << "' cannot have out parameters" << endl;
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700242 err = 1;
243 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800244 }
245
Casey Dahlinf4a93112015-10-05 16:58:09 -0700246 auto it = method_names.find(m->GetName());
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700247 // prevent duplicate methods
Casey Dahlinf4a93112015-10-05 16:58:09 -0700248 if (it == method_names.end()) {
249 method_names[m->GetName()] = m.get();
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700250 } else {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700251 cerr << filename << ":" << m->GetLine()
252 << " attempt to redefine method " << m->GetName() << "," << endl
253 << filename << ":" << it->second->GetLine()
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700254 << " previously defined here." << endl;
255 err = 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800256 }
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700257 }
258 return err;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800259}
260
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800261void write_common_dep_file(const string& output_file,
262 const vector<string>& aidl_sources,
263 CodeWriter* writer) {
264 // Encode that the output file depends on aidl input files.
265 writer->Write("%s : \\\n", output_file.c_str());
266 writer->Write(" %s", Join(aidl_sources, " \\\n ").c_str());
267 writer->Write("\n\n");
Christopher Wileya30a45e2015-10-17 10:56:59 -0700268
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 Wiley3a9911c2016-01-19 12:59:09 -0800271 for (const auto& src : aidl_sources) {
272 writer->Write("%s :\n", src.c_str());
273 }
274}
Christopher Wileya30a45e2015-10-17 10:56:59 -0700275
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800276bool write_java_dep_file(const JavaOptions& options,
277 const vector<unique_ptr<AidlImport>>& imports,
Christopher Wileyf8136192016-04-12 14:19:35 -0700278 const IoDelegate& io_delegate,
279 const string& output_file_name) {
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800280 string dep_file_name = options.DependencyFilePath();
281 if (dep_file_name.empty()) {
282 return true; // nothing to do
283 }
284 CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name);
285 if (!writer) {
286 LOG(ERROR) << "Could not open dependency file: " << dep_file_name;
287 return false;
288 }
289
290 vector<string> source_aidl = {options.input_file_name_};
Christopher Wileya30a45e2015-10-17 10:56:59 -0700291 for (const auto& import : imports) {
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800292 if (!import->GetFilename().empty()) {
293 source_aidl.push_back(import->GetFilename());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800294 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700295 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800296
Christopher Wileyf8136192016-04-12 14:19:35 -0700297 write_common_dep_file(output_file_name, source_aidl, writer.get());
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800298
299 return true;
300}
301
302bool write_cpp_dep_file(const CppOptions& options,
303 const AidlInterface& interface,
304 const vector<unique_ptr<AidlImport>>& imports,
305 const IoDelegate& io_delegate) {
306 using ::android::aidl::cpp::HeaderFile;
307 using ::android::aidl::cpp::ClassNames;
308
309 string dep_file_name = options.DependencyFilePath();
310 if (dep_file_name.empty()) {
311 return true; // nothing to do
312 }
313 CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name);
314 if (!writer) {
315 LOG(ERROR) << "Could not open dependency file: " << dep_file_name;
316 return false;
317 }
318
319 vector<string> source_aidl = {options.InputFileName()};
320 for (const auto& import : imports) {
321 if (!import->GetFilename().empty()) {
322 source_aidl.push_back(import->GetFilename());
323 }
324 }
325
326 vector<string> headers;
327 for (ClassNames c : {ClassNames::CLIENT,
328 ClassNames::SERVER,
329 ClassNames::INTERFACE}) {
330 headers.push_back(options.OutputHeaderDir() + '/' +
331 HeaderFile(interface, c, false /* use_os_sep */));
332 }
333
334 write_common_dep_file(options.OutputCppFilePath(), source_aidl, writer.get());
335 writer->Write("\n");
336
337 // Generated headers also depend on the source aidl files.
338 writer->Write("%s : \\\n %s\n", Join(headers, " \\\n ").c_str(),
339 Join(source_aidl, " \\\n ").c_str());
340
341 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800342}
343
Christopher Wiley90be4e32015-10-20 14:55:25 -0700344string generate_outputFileName(const JavaOptions& options,
345 const AidlInterface& interface) {
346 string name = interface.GetName();
347 string package = interface.GetPackage();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800348 string result;
349
350 // create the path to the destination folder based on the
351 // interface package name
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700352 result = options.output_base_folder_;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800353 result += OS_PATH_SEPARATOR;
354
355 string packageStr = package;
356 size_t len = packageStr.length();
357 for (size_t i=0; i<len; i++) {
358 if (packageStr[i] == '.') {
359 packageStr[i] = OS_PATH_SEPARATOR;
360 }
361 }
362
363 result += packageStr;
364
365 // add the filename by replacing the .aidl extension to .java
Adam Lesinskiffa16862014-01-23 18:17:42 -0800366 result += OS_PATH_SEPARATOR;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700367 result.append(name, 0, name.find('.'));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800368 result += ".java";
369
370 return result;
371}
372
Christopher Wileyf690be52015-09-14 15:19:10 -0700373int check_and_assign_method_ids(const char * filename,
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700374 const std::vector<std::unique_ptr<AidlMethod>>& items) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800375 // Check whether there are any methods with manually assigned id's and any that are not.
376 // Either all method id's must be manually assigned or all of them must not.
377 // Also, check for duplicates of user set id's and that the id's are within the proper bounds.
378 set<int> usedIds;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800379 bool hasUnassignedIds = false;
380 bool hasAssignedIds = false;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700381 for (const auto& item : items) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700382 if (item->HasId()) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700383 hasAssignedIds = true;
Casey Dahlindff80e52015-09-29 13:57:06 -0700384 // Ensure that the user set id is not duplicated.
Casey Dahlinf4a93112015-10-05 16:58:09 -0700385 if (usedIds.find(item->GetId()) != usedIds.end()) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700386 // We found a duplicate id, so throw an error.
Adam Lesinskiffa16862014-01-23 18:17:42 -0800387 fprintf(stderr,
Casey Dahlindff80e52015-09-29 13:57:06 -0700388 "%s:%d Found duplicate method id (%d) for method: %s\n",
Casey Dahlinf4a93112015-10-05 16:58:09 -0700389 filename, item->GetLine(),
390 item->GetId(), item->GetName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800391 return 1;
392 }
Casey Dahlindff80e52015-09-29 13:57:06 -0700393 // Ensure that the user set id is within the appropriate limits
Casey Dahlinf4a93112015-10-05 16:58:09 -0700394 if (item->GetId() < kMinUserSetMethodId ||
395 item->GetId() > kMaxUserSetMethodId) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700396 fprintf(stderr, "%s:%d Found out of bounds id (%d) for method: %s\n",
Casey Dahlinf4a93112015-10-05 16:58:09 -0700397 filename, item->GetLine(),
398 item->GetId(), item->GetName().c_str());
Casey Dahlindff80e52015-09-29 13:57:06 -0700399 fprintf(stderr, " Value for id must be between %d and %d inclusive.\n",
400 kMinUserSetMethodId, kMaxUserSetMethodId);
401 return 1;
402 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700403 usedIds.insert(item->GetId());
Casey Dahlindff80e52015-09-29 13:57:06 -0700404 } else {
405 hasUnassignedIds = true;
406 }
407 if (hasAssignedIds && hasUnassignedIds) {
408 fprintf(stderr,
409 "%s: You must either assign id's to all methods or to none of them.\n",
410 filename);
411 return 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800412 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800413 }
414
415 // In the case that all methods have unassigned id's, set a unique id for them.
416 if (hasUnassignedIds) {
417 int newId = 0;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700418 for (const auto& item : items) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700419 item->SetId(newId++);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800420 }
421 }
422
423 // success
424 return 0;
425}
426
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700427bool validate_constants(const AidlInterface& interface) {
428 bool success = true;
429 set<string> names;
430 for (const std::unique_ptr<AidlIntConstant>& int_constant :
431 interface.GetIntConstants()) {
432 if (names.count(int_constant->GetName()) > 0) {
433 LOG(ERROR) << "Found duplicate constant name '" << int_constant->GetName()
434 << "'";
435 success = false;
436 }
437 names.insert(int_constant->GetName());
438 }
439 for (const std::unique_ptr<AidlStringConstant>& string_constant :
440 interface.GetStringConstants()) {
441 if (names.count(string_constant->GetName()) > 0) {
442 LOG(ERROR) << "Found duplicate constant name '" << string_constant->GetName()
443 << "'";
444 success = false;
445 }
446 names.insert(string_constant->GetName());
447 // We've logged an error message for this on object construction.
448 success = success && string_constant->IsValid();
449 }
450 return success;
451}
452
Christopher Wileyef140932015-11-03 09:29:19 -0800453// TODO: Remove this in favor of using the YACC parser b/25479378
454bool ParsePreprocessedLine(const string& line, string* decl,
455 vector<string>* package, string* class_name) {
456 // erase all trailing whitespace and semicolons
457 const size_t end = line.find_last_not_of(" ;\t");
458 if (end == string::npos) {
459 return false;
460 }
461 if (line.rfind(';', end) != string::npos) {
462 return false;
463 }
464
465 decl->clear();
466 string type;
467 vector<string> pieces = Split(line.substr(0, end + 1), " \t");
468 for (const string& piece : pieces) {
469 if (piece.empty()) {
470 continue;
471 }
472 if (decl->empty()) {
473 *decl = std::move(piece);
474 } else if (type.empty()) {
475 type = std::move(piece);
476 } else {
477 return false;
478 }
479 }
480
481 // Note that this logic is absolutely wrong. Given a parcelable
482 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
483 // the class is just Bar. However, this was the way it was done in the past.
484 //
485 // See b/17415692
486 size_t dot_pos = type.rfind('.');
487 if (dot_pos != string::npos) {
488 *class_name = type.substr(dot_pos + 1);
489 *package = Split(type.substr(0, dot_pos), ".");
490 } else {
491 *class_name = type;
492 package->clear();
493 }
494
495 return true;
496}
497
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700498} // namespace
499
500namespace internals {
501
Christopher Wileyef140932015-11-03 09:29:19 -0800502bool parse_preprocessed_file(const IoDelegate& io_delegate,
503 const string& filename, TypeNamespace* types) {
504 bool success = true;
505 unique_ptr<LineReader> line_reader = io_delegate.GetLineReader(filename);
506 if (!line_reader) {
507 LOG(ERROR) << "cannot open preprocessed file: " << filename;
508 success = false;
509 return success;
510 }
511
512 string line;
513 unsigned lineno = 1;
514 for ( ; line_reader->ReadLine(&line); ++lineno) {
515 if (line.empty() || line.compare(0, 2, "//") == 0) {
516 // skip comments and empty lines
517 continue;
518 }
519
520 string decl;
521 vector<string> package;
522 string class_name;
523 if (!ParsePreprocessedLine(line, &decl, &package, &class_name)) {
524 success = false;
525 break;
526 }
527
528 if (decl == "parcelable") {
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800529 AidlParcelable doc(new AidlQualifiedName(class_name, ""),
530 lineno, package);
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800531 types->AddParcelableType(doc, filename);
Christopher Wileyef140932015-11-03 09:29:19 -0800532 } else if (decl == "interface") {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800533 auto temp = new std::vector<std::unique_ptr<AidlMember>>();
Christopher Wileyef140932015-11-03 09:29:19 -0800534 AidlInterface doc(class_name, lineno, "", false, temp, package);
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800535 types->AddBinderType(doc, filename);
Christopher Wileyef140932015-11-03 09:29:19 -0800536 } else {
537 success = false;
538 break;
539 }
540 }
541 if (!success) {
542 LOG(ERROR) << filename << ':' << lineno
543 << " malformed preprocessed file line: '" << line << "'";
544 }
545
546 return success;
547}
548
Christopher Wiley632801d2015-11-05 14:15:49 -0800549AidlError load_and_validate_aidl(
550 const std::vector<std::string> preprocessed_files,
551 const std::vector<std::string> import_paths,
552 const std::string& input_file_name,
553 const IoDelegate& io_delegate,
554 TypeNamespace* types,
555 std::unique_ptr<AidlInterface>* returned_interface,
556 std::vector<std::unique_ptr<AidlImport>>* returned_imports) {
557 AidlError err = AidlError::OK;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800558
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800559 std::map<AidlImport*,std::unique_ptr<AidlDocument>> docs;
Casey Dahlin624358c2015-10-12 19:29:51 -0700560
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700561 // import the preprocessed file
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700562 for (const string& s : preprocessed_files) {
Christopher Wileyef140932015-11-03 09:29:19 -0800563 if (!parse_preprocessed_file(io_delegate, s, types)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800564 err = AidlError::BAD_PRE_PROCESSED_FILE;
Christopher Wileyef140932015-11-03 09:29:19 -0800565 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700566 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800567 if (err != AidlError::OK) {
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700568 return err;
569 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800570
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700571 // parse the input file
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700572 Parser p{io_delegate};
573 if (!p.ParseFile(input_file_name)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800574 return AidlError::PARSE_ERROR;
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700575 }
Casey Dahlin2cc93162015-10-02 16:14:17 -0700576
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800577 AidlDocument* parsed_doc = p.GetDocument();
578
579 unique_ptr<AidlInterface> interface(parsed_doc->ReleaseInterface());
580
581 if (!interface) {
Christopher Wileye60d99c2015-11-06 13:14:24 -0800582 LOG(ERROR) << "refusing to generate code from aidl file defining "
583 "parcelable";
Christopher Wiley632801d2015-11-05 14:15:49 -0800584 return AidlError::FOUND_PARCELABLE;
585 }
586
Casey Dahlin42727f82015-10-12 19:23:40 -0700587 if (!check_filename(input_file_name.c_str(), interface->GetPackage(),
Christopher Wiley632801d2015-11-05 14:15:49 -0800588 interface->GetName(), interface->GetLine()) ||
589 !types->IsValidPackage(interface->GetPackage())) {
590 LOG(ERROR) << "Invalid package declaration '" << interface->GetPackage()
591 << "'";
592 return AidlError::BAD_PACKAGE;
593 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800594
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700595 // parse the imports of the input file
Christopher Wiley72877ac2015-10-06 14:41:42 -0700596 ImportResolver import_resolver{io_delegate, import_paths};
Casey Dahlin0edf3422015-10-07 12:34:59 -0700597 for (auto& import : p.GetImports()) {
Christopher Wiley934a82d2016-01-27 13:02:24 -0800598 if (types->HasImportType(*import)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700599 // There are places in the Android tree where an import doesn't resolve,
600 // but we'll pick the type up through the preprocessed types.
601 // This seems like an error, but legacy support demands we support it...
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700602 continue;
603 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700604 string import_path = import_resolver.FindImportFile(import->GetNeededClass());
Christopher Wiley72877ac2015-10-06 14:41:42 -0700605 if (import_path.empty()) {
Casey Dahlin0edf3422015-10-07 12:34:59 -0700606 cerr << import->GetFileFrom() << ":" << import->GetLine()
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700607 << ": couldn't find import for class "
Casey Dahlin0edf3422015-10-07 12:34:59 -0700608 << import->GetNeededClass() << endl;
Christopher Wiley632801d2015-11-05 14:15:49 -0800609 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700610 continue;
611 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700612 import->SetFilename(import_path);
Casey Dahlin2cc93162015-10-02 16:14:17 -0700613
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700614 Parser p{io_delegate};
Casey Dahlin0edf3422015-10-07 12:34:59 -0700615 if (!p.ParseFile(import->GetFilename())) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700616 cerr << "error while parsing import for class "
Casey Dahlin0edf3422015-10-07 12:34:59 -0700617 << import->GetNeededClass() << endl;
Christopher Wiley632801d2015-11-05 14:15:49 -0800618 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700619 continue;
620 }
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700621
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800622 std::unique_ptr<AidlDocument> document(p.ReleaseDocument());
623 if (!check_filenames(import->GetFilename(), document.get()))
Christopher Wiley632801d2015-11-05 14:15:49 -0800624 err = AidlError::BAD_IMPORT;
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800625 docs[import.get()] = std::move(document);
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700626 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800627 if (err != AidlError::OK) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700628 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700629 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800630
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700631 // gather the types that have been declared
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800632 if (!types->AddBinderType(*interface.get(), input_file_name)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800633 err = AidlError::BAD_TYPE;
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700634 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800635
Christopher Wiley934a82d2016-01-27 13:02:24 -0800636 interface->SetLanguageType(types->GetInterfaceType(*interface));
Casey Dahlina2f77c42015-12-01 18:26:02 -0800637
Casey Dahlin0edf3422015-10-07 12:34:59 -0700638 for (const auto& import : p.GetImports()) {
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800639 // If we skipped an unresolved import above (see comment there) we'll have
640 // an empty bucket here.
641 const auto import_itr = docs.find(import.get());
642 if (import_itr == docs.cend()) {
643 continue;
644 }
645
646 if (!gather_types(import->GetFilename(), import_itr->second.get(), types)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800647 err = AidlError::BAD_TYPE;
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700648 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700649 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800650
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700651 // check the referenced types in parsed_doc to make sure we've imported them
Christopher Wiley632801d2015-11-05 14:15:49 -0800652 if (check_types(input_file_name, interface.get(), types) != 0) {
653 err = AidlError::BAD_TYPE;
654 }
655 if (err != AidlError::OK) {
656 return err;
657 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800658
Adam Lesinskiffa16862014-01-23 18:17:42 -0800659
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700660 // assign method ids and validate.
Christopher Wiley632801d2015-11-05 14:15:49 -0800661 if (check_and_assign_method_ids(input_file_name.c_str(),
662 interface->GetMethods()) != 0) {
663 return AidlError::BAD_METHOD_ID;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700664 }
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700665 if (!validate_constants(*interface)) {
666 return AidlError::BAD_CONSTANTS;
667 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800668
Casey Dahlin0edf3422015-10-07 12:34:59 -0700669 if (returned_interface)
Christopher Wiley90be4e32015-10-20 14:55:25 -0700670 *returned_interface = std::move(interface);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700671
672 if (returned_imports)
673 p.ReleaseImports(returned_imports);
674
Christopher Wiley632801d2015-11-05 14:15:49 -0800675 return AidlError::OK;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700676}
677
Casey Dahlin2cc93162015-10-02 16:14:17 -0700678} // namespace internals
679
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700680int compile_aidl_to_cpp(const CppOptions& options,
681 const IoDelegate& io_delegate) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700682 unique_ptr<AidlInterface> interface;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700683 std::vector<std::unique_ptr<AidlImport>> imports;
Christopher Wileye3550c62015-09-29 13:26:10 -0700684 unique_ptr<cpp::TypeNamespace> types(new cpp::TypeNamespace());
Christopher Wiley56799522015-10-31 10:17:04 -0700685 types->Init();
Christopher Wiley632801d2015-11-05 14:15:49 -0800686 AidlError err = internals::load_and_validate_aidl(
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700687 std::vector<std::string>{}, // no preprocessed files
688 options.ImportPaths(),
689 options.InputFileName(),
690 io_delegate,
691 types.get(),
692 &interface,
693 &imports);
Christopher Wiley632801d2015-11-05 14:15:49 -0800694 if (err != AidlError::OK) {
695 return 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700696 }
697
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800698 if (!write_cpp_dep_file(options, *interface, imports, io_delegate)) {
699 return 1;
Christopher Wiley19059cb2015-11-05 16:11:56 -0800700 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700701
Christopher Wiley054afbd2015-10-16 17:08:43 -0700702 return (cpp::GenerateCpp(options, *types, *interface, io_delegate)) ? 0 : 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700703}
704
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700705int compile_aidl_to_java(const JavaOptions& options,
706 const IoDelegate& io_delegate) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700707 unique_ptr<AidlInterface> interface;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700708 std::vector<std::unique_ptr<AidlImport>> imports;
Christopher Wileydb154a52015-09-28 16:32:25 -0700709 unique_ptr<java::JavaTypeNamespace> types(new java::JavaTypeNamespace());
Christopher Wiley56799522015-10-31 10:17:04 -0700710 types->Init();
Christopher Wiley632801d2015-11-05 14:15:49 -0800711 AidlError aidl_err = internals::load_and_validate_aidl(
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700712 options.preprocessed_files_,
713 options.import_paths_,
714 options.input_file_name_,
715 io_delegate,
716 types.get(),
717 &interface,
718 &imports);
Christopher Wiley632801d2015-11-05 14:15:49 -0800719 if (aidl_err == AidlError::FOUND_PARCELABLE && !options.fail_on_parcelable_) {
720 // We aborted code generation because this file contains parcelables.
721 // However, we were not told to complain if we find parcelables.
Christopher Wileyb1bbdf82016-04-21 11:43:45 -0700722 // Just generate a dep file and exit quietly. The dep file is for a legacy
723 // use case by the SDK.
724 write_java_dep_file(options, imports, io_delegate, "");
Christopher Wiley632801d2015-11-05 14:15:49 -0800725 return 0;
726 }
727 if (aidl_err != AidlError::OK) {
728 return 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700729 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700730
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700731 string output_file_name = options.output_file_name_;
732 // if needed, generate the output file name from the base folder
Christopher Wiley632801d2015-11-05 14:15:49 -0800733 if (output_file_name.empty() && !options.output_base_folder_.empty()) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700734 output_file_name = generate_outputFileName(options, *interface);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700735 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800736
Casey Dahlin64533512015-10-23 17:11:21 -0700737 // make sure the folders of the output file all exists
738 if (!io_delegate.CreatePathForFile(output_file_name)) {
739 return 1;
740 }
741
Christopher Wileyf8136192016-04-12 14:19:35 -0700742 if (!write_java_dep_file(options, imports, io_delegate, output_file_name)) {
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800743 return 1;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700744 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800745
Christopher Wiley632801d2015-11-05 14:15:49 -0800746 return generate_java(output_file_name, options.input_file_name_.c_str(),
747 interface.get(), types.get(), io_delegate);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800748}
749
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800750bool preprocess_aidl(const JavaOptions& options,
751 const IoDelegate& io_delegate) {
752 unique_ptr<CodeWriter> writer =
753 io_delegate.GetCodeWriter(options.output_file_name_);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800754
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800755 for (const auto& file : options.files_to_preprocess_) {
756 Parser p{io_delegate};
757 if (!p.ParseFile(file))
758 return false;
759 AidlDocument* doc = p.GetDocument();
760 string line;
Casey Dahlin59401da2015-10-09 18:16:45 -0700761
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800762 const AidlInterface* interface = doc->GetInterface();
Casey Dahlin59401da2015-10-09 18:16:45 -0700763
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800764 if (interface != nullptr &&
765 !writer->Write("interface %s;\n",
766 interface->GetCanonicalName().c_str())) {
767 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800768 }
769
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800770 for (const auto& parcelable : doc->GetParcelables()) {
771 if (!writer->Write("parcelable %s;\n",
772 parcelable->GetCanonicalName().c_str())) {
773 return false;
774 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800775 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800776 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800777
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800778 return writer->Close();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800779}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700780
781} // namespace android
782} // namespace aidl