blob: 513aaee7f91b883b7253b27284188c63b89f6ae5 [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;
Adam Lesinskiffa16862014-01-23 18:17:42 -080080 bool valid = false;
81
Christopher Wileybc2df692016-06-02 16:27:26 -070082 if (!IoDelegate::GetAbsolutePath(filename, &fn)) {
83 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -080084 }
85
Casey Dahlinfb7da2e2015-10-08 17:26:09 -070086 if (!package.empty()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080087 expected = package;
88 expected += '.';
89 }
90
91 len = expected.length();
92 for (size_t i=0; i<len; i++) {
93 if (expected[i] == '.') {
94 expected[i] = OS_PATH_SEPARATOR;
95 }
96 }
97
Casey Dahlinfb7da2e2015-10-08 17:26:09 -070098 expected.append(name, 0, name.find('.'));
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -070099
Adam Lesinskiffa16862014-01-23 18:17:42 -0800100 expected += ".aidl";
101
102 len = fn.length();
103 valid = (len >= expected.length());
104
105 if (valid) {
106 p = fn.c_str() + (len - expected.length());
107
Elliott Hughesce310da2015-07-29 08:44:17 -0700108#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800109 if (OS_PATH_SEPARATOR != '/') {
110 // Input filename under cygwin most likely has / separators
111 // whereas the expected string uses \\ separators. Adjust
112 // them accordingly.
113 for (char *c = const_cast<char *>(p); *c; ++c) {
114 if (*c == '/') *c = OS_PATH_SEPARATOR;
115 }
116 }
117#endif
118
Yabin Cui482eefb2014-11-10 15:01:43 -0800119 // aidl assumes case-insensitivity on Mac Os and Windows.
120#if defined(__linux__)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800121 valid = (expected == p);
122#else
123 valid = !strcasecmp(expected.c_str(), p);
124#endif
125 }
126
127 if (!valid) {
128 fprintf(stderr, "%s:%d interface %s should be declared in a file"
129 " called %s.\n",
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700130 filename.c_str(), line, name.c_str(), expected.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800131 }
132
Casey Dahlin42727f82015-10-12 19:23:40 -0700133 return valid;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800134}
135
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800136bool check_filenames(const std::string& filename, const AidlDocument* doc) {
137 if (!doc)
Casey Dahlin42727f82015-10-12 19:23:40 -0700138 return true;
139
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800140 const AidlInterface* interface = doc->GetInterface();
141
142 if (interface) {
143 return check_filename(filename, interface->GetPackage(),
144 interface->GetName(), interface->GetLine());
Casey Dahlin42727f82015-10-12 19:23:40 -0700145 }
146
147 bool success = true;
148
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800149 for (const auto& item : doc->GetParcelables()) {
150 success &= check_filename(filename, item->GetPackage(), item->GetName(),
151 item->GetLine());
152 }
Casey Dahlin42727f82015-10-12 19:23:40 -0700153
154 return success;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800155}
156
Casey Dahlin0edf3422015-10-07 12:34:59 -0700157bool gather_types(const std::string& filename,
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800158 const AidlDocument* doc,
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700159 TypeNamespace* types) {
160 bool success = true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800161
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800162 const AidlInterface* interface = doc->GetInterface();
Casey Dahlin42727f82015-10-12 19:23:40 -0700163
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800164 if (interface)
165 return types->AddBinderType(*interface, filename);
Casey Dahlin42727f82015-10-12 19:23:40 -0700166
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800167 for (const auto& item : doc->GetParcelables()) {
168 success &= types->AddParcelableType(*item, filename);
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700169 }
Casey Dahlin42727f82015-10-12 19:23:40 -0700170
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700171 return success;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800172}
173
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700174int check_types(const string& filename,
Casey Dahlin98a544b2015-10-14 14:22:55 -0700175 const AidlInterface* c,
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700176 TypeNamespace* types) {
177 int err = 0;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700178
Casey Dahlinc5afb402016-03-01 13:54:05 -0800179 if (c->IsUtf8() && c->IsUtf8InCpp()) {
180 cerr << filename << ":" << c->GetLine()
181 << "Interface cannot be marked as both @utf8 and @utf8InCpp";
182 err = 1;
183 }
184
Casey Dahlinf4a93112015-10-05 16:58:09 -0700185 // Has to be a pointer due to deleting copy constructor. No idea why.
186 map<string, const AidlMethod*> method_names;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700187 for (const auto& m : c->GetMethods()) {
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700188 bool oneway = m->IsOneway() || c->IsOneway();
189
Christopher Wiley934a82d2016-01-27 13:02:24 -0800190 if (!types->MaybeAddContainerType(m->GetType())) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700191 err = 1; // return type is invalid
Adam Lesinskiffa16862014-01-23 18:17:42 -0800192 }
193
Casey Dahlina2f77c42015-12-01 18:26:02 -0800194 const ValidatableType* return_type =
Casey Dahlinc5afb402016-03-01 13:54:05 -0800195 types->GetReturnType(m->GetType(), filename, *c);
Casey Dahlina2f77c42015-12-01 18:26:02 -0800196
Casey Dahlin57dbe242015-12-04 11:44:02 -0800197 if (!return_type) {
198 err = 1;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800199 }
200
201 m->GetMutableType()->SetLanguageType(return_type);
202
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700203 if (oneway && m->GetType().GetName() != "void") {
204 cerr << filename << ":" << m->GetLine()
Christopher Wiley45db9ee2015-10-26 18:53:53 -0700205 << " oneway method '" << m->GetName() << "' cannot return a value"
206 << endl;
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700207 err = 1;
208 }
209
Adam Lesinskiffa16862014-01-23 18:17:42 -0800210 int index = 1;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700211 for (const auto& arg : m->GetArguments()) {
Christopher Wiley934a82d2016-01-27 13:02:24 -0800212 if (!types->MaybeAddContainerType(arg->GetType())) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700213 err = 1;
214 }
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700215
Casey Dahlina2f77c42015-12-01 18:26:02 -0800216 const ValidatableType* arg_type =
Casey Dahlinc5afb402016-03-01 13:54:05 -0800217 types->GetArgType(*arg, index, filename, *c);
Casey Dahlina2f77c42015-12-01 18:26:02 -0800218
Casey Dahlin57dbe242015-12-04 11:44:02 -0800219 if (!arg_type) {
220 err = 1;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800221 }
222
223 arg->GetMutableType()->SetLanguageType(arg_type);
224
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700225 if (oneway && arg->IsOut()) {
226 cerr << filename << ":" << m->GetLine()
Christopher Wiley45db9ee2015-10-26 18:53:53 -0700227 << " oneway method '" << m->GetName()
228 << "' cannot have out parameters" << endl;
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700229 err = 1;
230 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800231 }
232
Casey Dahlinf4a93112015-10-05 16:58:09 -0700233 auto it = method_names.find(m->GetName());
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700234 // prevent duplicate methods
Casey Dahlinf4a93112015-10-05 16:58:09 -0700235 if (it == method_names.end()) {
236 method_names[m->GetName()] = m.get();
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700237 } else {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700238 cerr << filename << ":" << m->GetLine()
239 << " attempt to redefine method " << m->GetName() << "," << endl
240 << filename << ":" << it->second->GetLine()
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700241 << " previously defined here." << endl;
242 err = 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800243 }
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700244 }
245 return err;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800246}
247
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800248void write_common_dep_file(const string& output_file,
249 const vector<string>& aidl_sources,
250 CodeWriter* writer) {
251 // Encode that the output file depends on aidl input files.
252 writer->Write("%s : \\\n", output_file.c_str());
253 writer->Write(" %s", Join(aidl_sources, " \\\n ").c_str());
254 writer->Write("\n\n");
Christopher Wileya30a45e2015-10-17 10:56:59 -0700255
256 // Output "<input_aidl_file>: " so make won't fail if the input .aidl file
257 // has been deleted, moved or renamed in incremental build.
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800258 for (const auto& src : aidl_sources) {
259 writer->Write("%s :\n", src.c_str());
260 }
261}
Christopher Wileya30a45e2015-10-17 10:56:59 -0700262
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800263bool write_java_dep_file(const JavaOptions& options,
264 const vector<unique_ptr<AidlImport>>& imports,
Christopher Wileyf8136192016-04-12 14:19:35 -0700265 const IoDelegate& io_delegate,
266 const string& output_file_name) {
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800267 string dep_file_name = options.DependencyFilePath();
268 if (dep_file_name.empty()) {
269 return true; // nothing to do
270 }
271 CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name);
272 if (!writer) {
273 LOG(ERROR) << "Could not open dependency file: " << dep_file_name;
274 return false;
275 }
276
277 vector<string> source_aidl = {options.input_file_name_};
Christopher Wileya30a45e2015-10-17 10:56:59 -0700278 for (const auto& import : imports) {
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800279 if (!import->GetFilename().empty()) {
280 source_aidl.push_back(import->GetFilename());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800281 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700282 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800283
Christopher Wileyf8136192016-04-12 14:19:35 -0700284 write_common_dep_file(output_file_name, source_aidl, writer.get());
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800285
286 return true;
287}
288
289bool write_cpp_dep_file(const CppOptions& options,
290 const AidlInterface& interface,
291 const vector<unique_ptr<AidlImport>>& imports,
292 const IoDelegate& io_delegate) {
293 using ::android::aidl::cpp::HeaderFile;
294 using ::android::aidl::cpp::ClassNames;
295
296 string dep_file_name = options.DependencyFilePath();
297 if (dep_file_name.empty()) {
298 return true; // nothing to do
299 }
300 CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name);
301 if (!writer) {
302 LOG(ERROR) << "Could not open dependency file: " << dep_file_name;
303 return false;
304 }
305
306 vector<string> source_aidl = {options.InputFileName()};
307 for (const auto& import : imports) {
308 if (!import->GetFilename().empty()) {
309 source_aidl.push_back(import->GetFilename());
310 }
311 }
312
313 vector<string> headers;
314 for (ClassNames c : {ClassNames::CLIENT,
315 ClassNames::SERVER,
316 ClassNames::INTERFACE}) {
317 headers.push_back(options.OutputHeaderDir() + '/' +
318 HeaderFile(interface, c, false /* use_os_sep */));
319 }
320
321 write_common_dep_file(options.OutputCppFilePath(), source_aidl, writer.get());
322 writer->Write("\n");
323
324 // Generated headers also depend on the source aidl files.
325 writer->Write("%s : \\\n %s\n", Join(headers, " \\\n ").c_str(),
326 Join(source_aidl, " \\\n ").c_str());
327
328 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800329}
330
Christopher Wiley90be4e32015-10-20 14:55:25 -0700331string generate_outputFileName(const JavaOptions& options,
332 const AidlInterface& interface) {
333 string name = interface.GetName();
334 string package = interface.GetPackage();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800335 string result;
336
337 // create the path to the destination folder based on the
338 // interface package name
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700339 result = options.output_base_folder_;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800340 result += OS_PATH_SEPARATOR;
341
342 string packageStr = package;
343 size_t len = packageStr.length();
344 for (size_t i=0; i<len; i++) {
345 if (packageStr[i] == '.') {
346 packageStr[i] = OS_PATH_SEPARATOR;
347 }
348 }
349
350 result += packageStr;
351
352 // add the filename by replacing the .aidl extension to .java
Adam Lesinskiffa16862014-01-23 18:17:42 -0800353 result += OS_PATH_SEPARATOR;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700354 result.append(name, 0, name.find('.'));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800355 result += ".java";
356
357 return result;
358}
359
Christopher Wileyf690be52015-09-14 15:19:10 -0700360int check_and_assign_method_ids(const char * filename,
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700361 const std::vector<std::unique_ptr<AidlMethod>>& items) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800362 // Check whether there are any methods with manually assigned id's and any that are not.
363 // Either all method id's must be manually assigned or all of them must not.
364 // Also, check for duplicates of user set id's and that the id's are within the proper bounds.
365 set<int> usedIds;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800366 bool hasUnassignedIds = false;
367 bool hasAssignedIds = false;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700368 for (const auto& item : items) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700369 if (item->HasId()) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700370 hasAssignedIds = true;
Casey Dahlindff80e52015-09-29 13:57:06 -0700371 // Ensure that the user set id is not duplicated.
Casey Dahlinf4a93112015-10-05 16:58:09 -0700372 if (usedIds.find(item->GetId()) != usedIds.end()) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700373 // We found a duplicate id, so throw an error.
Adam Lesinskiffa16862014-01-23 18:17:42 -0800374 fprintf(stderr,
Casey Dahlindff80e52015-09-29 13:57:06 -0700375 "%s:%d Found duplicate method id (%d) for method: %s\n",
Casey Dahlinf4a93112015-10-05 16:58:09 -0700376 filename, item->GetLine(),
377 item->GetId(), item->GetName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800378 return 1;
379 }
Casey Dahlindff80e52015-09-29 13:57:06 -0700380 // Ensure that the user set id is within the appropriate limits
Casey Dahlinf4a93112015-10-05 16:58:09 -0700381 if (item->GetId() < kMinUserSetMethodId ||
382 item->GetId() > kMaxUserSetMethodId) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700383 fprintf(stderr, "%s:%d Found out of bounds id (%d) for method: %s\n",
Casey Dahlinf4a93112015-10-05 16:58:09 -0700384 filename, item->GetLine(),
385 item->GetId(), item->GetName().c_str());
Casey Dahlindff80e52015-09-29 13:57:06 -0700386 fprintf(stderr, " Value for id must be between %d and %d inclusive.\n",
387 kMinUserSetMethodId, kMaxUserSetMethodId);
388 return 1;
389 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700390 usedIds.insert(item->GetId());
Casey Dahlindff80e52015-09-29 13:57:06 -0700391 } else {
392 hasUnassignedIds = true;
393 }
394 if (hasAssignedIds && hasUnassignedIds) {
395 fprintf(stderr,
396 "%s: You must either assign id's to all methods or to none of them.\n",
397 filename);
398 return 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800399 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800400 }
401
402 // In the case that all methods have unassigned id's, set a unique id for them.
403 if (hasUnassignedIds) {
404 int newId = 0;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700405 for (const auto& item : items) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700406 item->SetId(newId++);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800407 }
408 }
409
410 // success
411 return 0;
412}
413
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700414bool validate_constants(const AidlInterface& interface) {
415 bool success = true;
416 set<string> names;
417 for (const std::unique_ptr<AidlIntConstant>& int_constant :
418 interface.GetIntConstants()) {
419 if (names.count(int_constant->GetName()) > 0) {
420 LOG(ERROR) << "Found duplicate constant name '" << int_constant->GetName()
421 << "'";
422 success = false;
423 }
424 names.insert(int_constant->GetName());
Roshan Pius3b2203d2016-07-22 16:13:20 -0700425 // We've logged an error message for this on object construction.
426 success = success && int_constant->IsValid();
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700427 }
428 for (const std::unique_ptr<AidlStringConstant>& string_constant :
429 interface.GetStringConstants()) {
430 if (names.count(string_constant->GetName()) > 0) {
431 LOG(ERROR) << "Found duplicate constant name '" << string_constant->GetName()
432 << "'";
433 success = false;
434 }
435 names.insert(string_constant->GetName());
436 // We've logged an error message for this on object construction.
437 success = success && string_constant->IsValid();
438 }
439 return success;
440}
441
Christopher Wileyef140932015-11-03 09:29:19 -0800442// TODO: Remove this in favor of using the YACC parser b/25479378
443bool ParsePreprocessedLine(const string& line, string* decl,
444 vector<string>* package, string* class_name) {
445 // erase all trailing whitespace and semicolons
446 const size_t end = line.find_last_not_of(" ;\t");
447 if (end == string::npos) {
448 return false;
449 }
450 if (line.rfind(';', end) != string::npos) {
451 return false;
452 }
453
454 decl->clear();
455 string type;
456 vector<string> pieces = Split(line.substr(0, end + 1), " \t");
457 for (const string& piece : pieces) {
458 if (piece.empty()) {
459 continue;
460 }
461 if (decl->empty()) {
462 *decl = std::move(piece);
463 } else if (type.empty()) {
464 type = std::move(piece);
465 } else {
466 return false;
467 }
468 }
469
470 // Note that this logic is absolutely wrong. Given a parcelable
471 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
472 // the class is just Bar. However, this was the way it was done in the past.
473 //
474 // See b/17415692
475 size_t dot_pos = type.rfind('.');
476 if (dot_pos != string::npos) {
477 *class_name = type.substr(dot_pos + 1);
478 *package = Split(type.substr(0, dot_pos), ".");
479 } else {
480 *class_name = type;
481 package->clear();
482 }
483
484 return true;
485}
486
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700487} // namespace
488
489namespace internals {
490
Christopher Wileyef140932015-11-03 09:29:19 -0800491bool parse_preprocessed_file(const IoDelegate& io_delegate,
492 const string& filename, TypeNamespace* types) {
493 bool success = true;
494 unique_ptr<LineReader> line_reader = io_delegate.GetLineReader(filename);
495 if (!line_reader) {
496 LOG(ERROR) << "cannot open preprocessed file: " << filename;
497 success = false;
498 return success;
499 }
500
501 string line;
502 unsigned lineno = 1;
503 for ( ; line_reader->ReadLine(&line); ++lineno) {
504 if (line.empty() || line.compare(0, 2, "//") == 0) {
505 // skip comments and empty lines
506 continue;
507 }
508
509 string decl;
510 vector<string> package;
511 string class_name;
512 if (!ParsePreprocessedLine(line, &decl, &package, &class_name)) {
513 success = false;
514 break;
515 }
516
517 if (decl == "parcelable") {
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800518 AidlParcelable doc(new AidlQualifiedName(class_name, ""),
519 lineno, package);
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800520 types->AddParcelableType(doc, filename);
Christopher Wileyef140932015-11-03 09:29:19 -0800521 } else if (decl == "interface") {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800522 auto temp = new std::vector<std::unique_ptr<AidlMember>>();
Christopher Wileyef140932015-11-03 09:29:19 -0800523 AidlInterface doc(class_name, lineno, "", false, temp, package);
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800524 types->AddBinderType(doc, filename);
Christopher Wileyef140932015-11-03 09:29:19 -0800525 } else {
526 success = false;
527 break;
528 }
529 }
530 if (!success) {
531 LOG(ERROR) << filename << ':' << lineno
532 << " malformed preprocessed file line: '" << line << "'";
533 }
534
535 return success;
536}
537
Christopher Wiley632801d2015-11-05 14:15:49 -0800538AidlError load_and_validate_aidl(
539 const std::vector<std::string> preprocessed_files,
540 const std::vector<std::string> import_paths,
541 const std::string& input_file_name,
542 const IoDelegate& io_delegate,
543 TypeNamespace* types,
544 std::unique_ptr<AidlInterface>* returned_interface,
545 std::vector<std::unique_ptr<AidlImport>>* returned_imports) {
546 AidlError err = AidlError::OK;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800547
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800548 std::map<AidlImport*,std::unique_ptr<AidlDocument>> docs;
Casey Dahlin624358c2015-10-12 19:29:51 -0700549
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700550 // import the preprocessed file
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700551 for (const string& s : preprocessed_files) {
Christopher Wileyef140932015-11-03 09:29:19 -0800552 if (!parse_preprocessed_file(io_delegate, s, types)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800553 err = AidlError::BAD_PRE_PROCESSED_FILE;
Christopher Wileyef140932015-11-03 09:29:19 -0800554 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700555 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800556 if (err != AidlError::OK) {
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700557 return err;
558 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800559
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700560 // parse the input file
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700561 Parser p{io_delegate};
562 if (!p.ParseFile(input_file_name)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800563 return AidlError::PARSE_ERROR;
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700564 }
Casey Dahlin2cc93162015-10-02 16:14:17 -0700565
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800566 AidlDocument* parsed_doc = p.GetDocument();
567
568 unique_ptr<AidlInterface> interface(parsed_doc->ReleaseInterface());
569
570 if (!interface) {
Christopher Wileye60d99c2015-11-06 13:14:24 -0800571 LOG(ERROR) << "refusing to generate code from aidl file defining "
572 "parcelable";
Christopher Wiley632801d2015-11-05 14:15:49 -0800573 return AidlError::FOUND_PARCELABLE;
574 }
575
Casey Dahlin42727f82015-10-12 19:23:40 -0700576 if (!check_filename(input_file_name.c_str(), interface->GetPackage(),
Christopher Wiley632801d2015-11-05 14:15:49 -0800577 interface->GetName(), interface->GetLine()) ||
578 !types->IsValidPackage(interface->GetPackage())) {
579 LOG(ERROR) << "Invalid package declaration '" << interface->GetPackage()
580 << "'";
581 return AidlError::BAD_PACKAGE;
582 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800583
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700584 // parse the imports of the input file
Christopher Wiley72877ac2015-10-06 14:41:42 -0700585 ImportResolver import_resolver{io_delegate, import_paths};
Casey Dahlin0edf3422015-10-07 12:34:59 -0700586 for (auto& import : p.GetImports()) {
Christopher Wiley934a82d2016-01-27 13:02:24 -0800587 if (types->HasImportType(*import)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700588 // There are places in the Android tree where an import doesn't resolve,
589 // but we'll pick the type up through the preprocessed types.
590 // This seems like an error, but legacy support demands we support it...
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700591 continue;
592 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700593 string import_path = import_resolver.FindImportFile(import->GetNeededClass());
Christopher Wiley72877ac2015-10-06 14:41:42 -0700594 if (import_path.empty()) {
Casey Dahlin0edf3422015-10-07 12:34:59 -0700595 cerr << import->GetFileFrom() << ":" << import->GetLine()
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700596 << ": couldn't find import for class "
Casey Dahlin0edf3422015-10-07 12:34:59 -0700597 << import->GetNeededClass() << endl;
Christopher Wiley632801d2015-11-05 14:15:49 -0800598 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700599 continue;
600 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700601 import->SetFilename(import_path);
Casey Dahlin2cc93162015-10-02 16:14:17 -0700602
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700603 Parser p{io_delegate};
Casey Dahlin0edf3422015-10-07 12:34:59 -0700604 if (!p.ParseFile(import->GetFilename())) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700605 cerr << "error while parsing import for class "
Casey Dahlin0edf3422015-10-07 12:34:59 -0700606 << import->GetNeededClass() << endl;
Christopher Wiley632801d2015-11-05 14:15:49 -0800607 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700608 continue;
609 }
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700610
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800611 std::unique_ptr<AidlDocument> document(p.ReleaseDocument());
612 if (!check_filenames(import->GetFilename(), document.get()))
Christopher Wiley632801d2015-11-05 14:15:49 -0800613 err = AidlError::BAD_IMPORT;
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800614 docs[import.get()] = std::move(document);
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700615 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800616 if (err != AidlError::OK) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700617 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700618 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800619
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700620 // gather the types that have been declared
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800621 if (!types->AddBinderType(*interface.get(), input_file_name)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800622 err = AidlError::BAD_TYPE;
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700623 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800624
Christopher Wiley934a82d2016-01-27 13:02:24 -0800625 interface->SetLanguageType(types->GetInterfaceType(*interface));
Casey Dahlina2f77c42015-12-01 18:26:02 -0800626
Casey Dahlin0edf3422015-10-07 12:34:59 -0700627 for (const auto& import : p.GetImports()) {
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800628 // If we skipped an unresolved import above (see comment there) we'll have
629 // an empty bucket here.
630 const auto import_itr = docs.find(import.get());
631 if (import_itr == docs.cend()) {
632 continue;
633 }
634
635 if (!gather_types(import->GetFilename(), import_itr->second.get(), types)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800636 err = AidlError::BAD_TYPE;
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700637 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700638 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800639
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700640 // check the referenced types in parsed_doc to make sure we've imported them
Christopher Wiley632801d2015-11-05 14:15:49 -0800641 if (check_types(input_file_name, interface.get(), types) != 0) {
642 err = AidlError::BAD_TYPE;
643 }
644 if (err != AidlError::OK) {
645 return err;
646 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800647
Adam Lesinskiffa16862014-01-23 18:17:42 -0800648
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700649 // assign method ids and validate.
Christopher Wiley632801d2015-11-05 14:15:49 -0800650 if (check_and_assign_method_ids(input_file_name.c_str(),
651 interface->GetMethods()) != 0) {
652 return AidlError::BAD_METHOD_ID;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700653 }
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700654 if (!validate_constants(*interface)) {
655 return AidlError::BAD_CONSTANTS;
656 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800657
Casey Dahlin0edf3422015-10-07 12:34:59 -0700658 if (returned_interface)
Christopher Wiley90be4e32015-10-20 14:55:25 -0700659 *returned_interface = std::move(interface);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700660
661 if (returned_imports)
662 p.ReleaseImports(returned_imports);
663
Christopher Wiley632801d2015-11-05 14:15:49 -0800664 return AidlError::OK;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700665}
666
Casey Dahlin2cc93162015-10-02 16:14:17 -0700667} // namespace internals
668
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700669int compile_aidl_to_cpp(const CppOptions& options,
670 const IoDelegate& io_delegate) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700671 unique_ptr<AidlInterface> interface;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700672 std::vector<std::unique_ptr<AidlImport>> imports;
Christopher Wileye3550c62015-09-29 13:26:10 -0700673 unique_ptr<cpp::TypeNamespace> types(new cpp::TypeNamespace());
Christopher Wiley56799522015-10-31 10:17:04 -0700674 types->Init();
Christopher Wiley632801d2015-11-05 14:15:49 -0800675 AidlError err = internals::load_and_validate_aidl(
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700676 std::vector<std::string>{}, // no preprocessed files
677 options.ImportPaths(),
678 options.InputFileName(),
679 io_delegate,
680 types.get(),
681 &interface,
682 &imports);
Christopher Wiley632801d2015-11-05 14:15:49 -0800683 if (err != AidlError::OK) {
684 return 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700685 }
686
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800687 if (!write_cpp_dep_file(options, *interface, imports, io_delegate)) {
688 return 1;
Christopher Wiley19059cb2015-11-05 16:11:56 -0800689 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700690
Christopher Wiley054afbd2015-10-16 17:08:43 -0700691 return (cpp::GenerateCpp(options, *types, *interface, io_delegate)) ? 0 : 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700692}
693
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700694int compile_aidl_to_java(const JavaOptions& options,
695 const IoDelegate& io_delegate) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700696 unique_ptr<AidlInterface> interface;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700697 std::vector<std::unique_ptr<AidlImport>> imports;
Christopher Wileydb154a52015-09-28 16:32:25 -0700698 unique_ptr<java::JavaTypeNamespace> types(new java::JavaTypeNamespace());
Christopher Wiley56799522015-10-31 10:17:04 -0700699 types->Init();
Christopher Wiley632801d2015-11-05 14:15:49 -0800700 AidlError aidl_err = internals::load_and_validate_aidl(
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700701 options.preprocessed_files_,
702 options.import_paths_,
703 options.input_file_name_,
704 io_delegate,
705 types.get(),
706 &interface,
707 &imports);
Christopher Wiley632801d2015-11-05 14:15:49 -0800708 if (aidl_err == AidlError::FOUND_PARCELABLE && !options.fail_on_parcelable_) {
709 // We aborted code generation because this file contains parcelables.
710 // However, we were not told to complain if we find parcelables.
Christopher Wileyb1bbdf82016-04-21 11:43:45 -0700711 // Just generate a dep file and exit quietly. The dep file is for a legacy
712 // use case by the SDK.
713 write_java_dep_file(options, imports, io_delegate, "");
Christopher Wiley632801d2015-11-05 14:15:49 -0800714 return 0;
715 }
716 if (aidl_err != AidlError::OK) {
717 return 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700718 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700719
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700720 string output_file_name = options.output_file_name_;
721 // if needed, generate the output file name from the base folder
Christopher Wiley632801d2015-11-05 14:15:49 -0800722 if (output_file_name.empty() && !options.output_base_folder_.empty()) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700723 output_file_name = generate_outputFileName(options, *interface);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700724 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800725
Casey Dahlin64533512015-10-23 17:11:21 -0700726 // make sure the folders of the output file all exists
727 if (!io_delegate.CreatePathForFile(output_file_name)) {
728 return 1;
729 }
730
Christopher Wileyf8136192016-04-12 14:19:35 -0700731 if (!write_java_dep_file(options, imports, io_delegate, output_file_name)) {
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800732 return 1;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700733 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800734
Christopher Wiley632801d2015-11-05 14:15:49 -0800735 return generate_java(output_file_name, options.input_file_name_.c_str(),
736 interface.get(), types.get(), io_delegate);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800737}
738
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800739bool preprocess_aidl(const JavaOptions& options,
740 const IoDelegate& io_delegate) {
741 unique_ptr<CodeWriter> writer =
742 io_delegate.GetCodeWriter(options.output_file_name_);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800743
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800744 for (const auto& file : options.files_to_preprocess_) {
745 Parser p{io_delegate};
746 if (!p.ParseFile(file))
747 return false;
748 AidlDocument* doc = p.GetDocument();
749 string line;
Casey Dahlin59401da2015-10-09 18:16:45 -0700750
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800751 const AidlInterface* interface = doc->GetInterface();
Casey Dahlin59401da2015-10-09 18:16:45 -0700752
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800753 if (interface != nullptr &&
754 !writer->Write("interface %s;\n",
755 interface->GetCanonicalName().c_str())) {
756 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800757 }
758
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800759 for (const auto& parcelable : doc->GetParcelables()) {
760 if (!writer->Write("parcelable %s;\n",
761 parcelable->GetCanonicalName().c_str())) {
762 return false;
763 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800764 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800765 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800766
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800767 return writer->Close();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800768}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700769
770} // namespace android
771} // namespace aidl