blob: d8cc4b84801f3285ec1908c8f4a949665a162a30 [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>
Adam Lesinskiffa16862014-01-23 18:17:42 -080020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
Christopher Wileyf690be52015-09-14 15:19:10 -070023#include <sys/param.h>
24#include <sys/stat.h>
25#include <unistd.h>
Jiyong Park02da7422018-07-16 16:00:26 +090026#include <algorithm>
Jiyong Park1deecc32018-07-17 01:14:41 +090027#include <iostream>
28#include <map>
29#include <memory>
Adam Lesinskiffa16862014-01-23 18:17:42 -080030
Elliott Hughes549b6e22015-08-17 12:41:46 -070031#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -080032#include <io.h>
Andrew Hsieh15ce9942014-05-07 20:14:30 +080033#include <direct.h>
Adam Lesinskiffa16862014-01-23 18:17:42 -080034#include <sys/stat.h>
35#endif
36
Elliott Hughes0a620672015-12-04 13:53:18 -080037#include <android-base/strings.h>
Christopher Wileyf690be52015-09-14 15:19:10 -070038
Christopher Wileyf690be52015-09-14 15:19:10 -070039#include "aidl_language.h"
Christopher Wileyeb1acc12015-09-16 11:25:13 -070040#include "generate_cpp.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070041#include "generate_java.h"
Christopher Wiley72877ac2015-10-06 14:41:42 -070042#include "import_resolver.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070043#include "logging.h"
44#include "options.h"
45#include "os.h"
Christopher Wileye3550c62015-09-29 13:26:10 -070046#include "type_cpp.h"
Christopher Wiley775fa1f2015-09-22 15:00:12 -070047#include "type_java.h"
Christopher Wiley84c1eac2015-09-23 13:29:28 -070048#include "type_namespace.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070049
Adam Lesinskiffa16862014-01-23 18:17:42 -080050#ifndef O_BINARY
51# define O_BINARY 0
52#endif
53
Christopher Wiley3a9911c2016-01-19 12:59:09 -080054using android::base::Join;
Christopher Wileyd76067c2015-10-19 17:00:13 -070055using android::base::Split;
Christopher Wileyc16e5e72015-09-16 10:49:40 -070056using std::cerr;
57using std::endl;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070058using std::map;
59using std::set;
60using std::string;
Christopher Wiley84c1eac2015-09-23 13:29:28 -070061using std::unique_ptr;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070062using std::vector;
Adam Lesinskiffa16862014-01-23 18:17:42 -080063
Christopher Wileyf690be52015-09-14 15:19:10 -070064namespace android {
65namespace aidl {
66namespace {
Adam Lesinskiffa16862014-01-23 18:17:42 -080067
Christopher Wileyf690be52015-09-14 15:19:10 -070068// The following are gotten as the offset from the allowable id's between
69// android.os.IBinder.FIRST_CALL_TRANSACTION=1 and
70// android.os.IBinder.LAST_CALL_TRANSACTION=16777215
71const int kMinUserSetMethodId = 0;
72const int kMaxUserSetMethodId = 16777214;
Adam Lesinskiffa16862014-01-23 18:17:42 -080073
Casey Dahlin42727f82015-10-12 19:23:40 -070074bool check_filename(const std::string& filename,
75 const std::string& package,
76 const std::string& name,
77 unsigned line) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080078 const char* p;
79 string expected;
80 string fn;
81 size_t len;
Adam Lesinskiffa16862014-01-23 18:17:42 -080082 bool valid = false;
83
Christopher Wileybc2df692016-06-02 16:27:26 -070084 if (!IoDelegate::GetAbsolutePath(filename, &fn)) {
85 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -080086 }
87
Casey Dahlinfb7da2e2015-10-08 17:26:09 -070088 if (!package.empty()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080089 expected = package;
90 expected += '.';
91 }
92
93 len = expected.length();
94 for (size_t i=0; i<len; i++) {
95 if (expected[i] == '.') {
96 expected[i] = OS_PATH_SEPARATOR;
97 }
98 }
99
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700100 expected.append(name, 0, name.find('.'));
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700101
Adam Lesinskiffa16862014-01-23 18:17:42 -0800102 expected += ".aidl";
103
104 len = fn.length();
105 valid = (len >= expected.length());
106
107 if (valid) {
108 p = fn.c_str() + (len - expected.length());
109
Elliott Hughesce310da2015-07-29 08:44:17 -0700110#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800111 if (OS_PATH_SEPARATOR != '/') {
112 // Input filename under cygwin most likely has / separators
113 // whereas the expected string uses \\ separators. Adjust
114 // them accordingly.
115 for (char *c = const_cast<char *>(p); *c; ++c) {
116 if (*c == '/') *c = OS_PATH_SEPARATOR;
117 }
118 }
119#endif
120
Yabin Cui482eefb2014-11-10 15:01:43 -0800121 // aidl assumes case-insensitivity on Mac Os and Windows.
122#if defined(__linux__)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800123 valid = (expected == p);
124#else
125 valid = !strcasecmp(expected.c_str(), p);
126#endif
127 }
128
129 if (!valid) {
Steven Moreland6ddc37f2018-07-02 13:05:50 -0700130 fprintf(stderr, "%s:%d %s should be declared in a file called %s.\n",
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700131 filename.c_str(), line, name.c_str(), expected.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800132 }
133
Casey Dahlin42727f82015-10-12 19:23:40 -0700134 return valid;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800135}
136
Steven Morelandc258abc2018-07-10 14:03:38 -0700137bool check_filenames(const std::string& filename, const AidlDocument& doc) {
Casey Dahlin42727f82015-10-12 19:23:40 -0700138 bool success = true;
139
Steven Morelandc258abc2018-07-10 14:03:38 -0700140 for (const auto& item : doc.GetDefinedTypes()) {
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800141 success &= check_filename(filename, item->GetPackage(), item->GetName(),
142 item->GetLine());
143 }
Casey Dahlin42727f82015-10-12 19:23:40 -0700144
145 return success;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800146}
147
Steven Morelandc258abc2018-07-10 14:03:38 -0700148bool gather_types(const std::string& filename, const AidlDocument& doc, TypeNamespace* types) {
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700149 bool success = true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800150
Steven Morelandc258abc2018-07-10 14:03:38 -0700151 for (const auto& item : doc.GetDefinedTypes()) {
152 const AidlInterface* interface = item->AsInterface();
153 if (interface != nullptr) {
154 success &= types->AddBinderType(*interface, filename);
155 continue;
156 }
Casey Dahlin42727f82015-10-12 19:23:40 -0700157
Steven Morelandc258abc2018-07-10 14:03:38 -0700158 const AidlParcelable* parcelable = item->AsParcelable();
159 if (parcelable != nullptr) {
160 success &= types->AddParcelableType(*parcelable, filename);
161 continue;
162 }
Casey Dahlin42727f82015-10-12 19:23:40 -0700163
Steven Morelandc258abc2018-07-10 14:03:38 -0700164 CHECK(false) << "aidl internal error: unrecognized type";
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700165 }
Casey Dahlin42727f82015-10-12 19:23:40 -0700166
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700167 return success;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800168}
169
Steven Moreland5557f1c2018-07-02 13:50:23 -0700170int check_types(const string& filename, const AidlStructuredParcelable* parcel,
171 TypeNamespace* types) {
172 int err = 0;
173 for (const auto& v : parcel->GetFields()) {
Steven Moreland9ea10e32018-07-19 15:26:09 -0700174 if (!v->CheckValid()) {
175 err = 1;
176 }
177
Steven Moreland5557f1c2018-07-02 13:50:23 -0700178 if (!types->MaybeAddContainerType(v->GetType())) {
179 err = 1; // return type is invalid
180 }
181
182 const ValidatableType* type = types->GetReturnType(v->GetType(), filename, *parcel);
183 if (!type) {
184 err = 1;
185 }
186
187 v->GetMutableType()->SetLanguageType(type);
188 }
189
190 return err;
191}
192
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700193int check_types(const string& filename,
Casey Dahlin98a544b2015-10-14 14:22:55 -0700194 const AidlInterface* c,
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700195 TypeNamespace* types) {
196 int err = 0;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700197
Casey Dahlinc5afb402016-03-01 13:54:05 -0800198 if (c->IsUtf8() && c->IsUtf8InCpp()) {
199 cerr << filename << ":" << c->GetLine()
200 << "Interface cannot be marked as both @utf8 and @utf8InCpp";
201 err = 1;
202 }
203
Casey Dahlinf4a93112015-10-05 16:58:09 -0700204 // Has to be a pointer due to deleting copy constructor. No idea why.
205 map<string, const AidlMethod*> method_names;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700206 for (const auto& m : c->GetMethods()) {
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700207 bool oneway = m->IsOneway() || c->IsOneway();
208
Christopher Wiley934a82d2016-01-27 13:02:24 -0800209 if (!types->MaybeAddContainerType(m->GetType())) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700210 err = 1; // return type is invalid
Adam Lesinskiffa16862014-01-23 18:17:42 -0800211 }
212
Casey Dahlina2f77c42015-12-01 18:26:02 -0800213 const ValidatableType* return_type =
Casey Dahlinc5afb402016-03-01 13:54:05 -0800214 types->GetReturnType(m->GetType(), filename, *c);
Casey Dahlina2f77c42015-12-01 18:26:02 -0800215
Casey Dahlin57dbe242015-12-04 11:44:02 -0800216 if (!return_type) {
217 err = 1;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800218 }
219
220 m->GetMutableType()->SetLanguageType(return_type);
221
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700222 if (oneway && m->GetType().GetName() != "void") {
223 cerr << filename << ":" << m->GetLine()
Christopher Wiley45db9ee2015-10-26 18:53:53 -0700224 << " oneway method '" << m->GetName() << "' cannot return a value"
225 << endl;
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700226 err = 1;
227 }
228
Adam Lesinskiffa16862014-01-23 18:17:42 -0800229 int index = 1;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700230 for (const auto& arg : m->GetArguments()) {
Christopher Wiley934a82d2016-01-27 13:02:24 -0800231 if (!types->MaybeAddContainerType(arg->GetType())) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700232 err = 1;
233 }
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700234
Casey Dahlina2f77c42015-12-01 18:26:02 -0800235 const ValidatableType* arg_type =
Casey Dahlinc5afb402016-03-01 13:54:05 -0800236 types->GetArgType(*arg, index, filename, *c);
Casey Dahlina2f77c42015-12-01 18:26:02 -0800237
Casey Dahlin57dbe242015-12-04 11:44:02 -0800238 if (!arg_type) {
239 err = 1;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800240 }
241
242 arg->GetMutableType()->SetLanguageType(arg_type);
243
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700244 if (oneway && arg->IsOut()) {
245 cerr << filename << ":" << m->GetLine()
Christopher Wiley45db9ee2015-10-26 18:53:53 -0700246 << " oneway method '" << m->GetName()
247 << "' cannot have out parameters" << endl;
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700248 err = 1;
249 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800250 }
251
Casey Dahlinf4a93112015-10-05 16:58:09 -0700252 auto it = method_names.find(m->GetName());
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700253 // prevent duplicate methods
Casey Dahlinf4a93112015-10-05 16:58:09 -0700254 if (it == method_names.end()) {
255 method_names[m->GetName()] = m.get();
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700256 } else {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700257 cerr << filename << ":" << m->GetLine()
258 << " attempt to redefine method " << m->GetName() << "," << endl
259 << filename << ":" << it->second->GetLine()
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700260 << " previously defined here." << endl;
261 err = 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800262 }
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700263 }
264 return err;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800265}
266
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800267void write_common_dep_file(const string& output_file,
268 const vector<string>& aidl_sources,
Dan Willemsen93298ee2016-11-10 23:55:55 -0800269 CodeWriter* writer,
270 const bool ninja) {
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800271 // Encode that the output file depends on aidl input files.
272 writer->Write("%s : \\\n", output_file.c_str());
273 writer->Write(" %s", Join(aidl_sources, " \\\n ").c_str());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800274 writer->Write("\n");
Christopher Wileya30a45e2015-10-17 10:56:59 -0700275
Dan Willemsen93298ee2016-11-10 23:55:55 -0800276 if (!ninja) {
277 writer->Write("\n");
278 // Output "<input_aidl_file>: " so make won't fail if the input .aidl file
279 // has been deleted, moved or renamed in incremental build.
280 for (const auto& src : aidl_sources) {
281 writer->Write("%s :\n", src.c_str());
282 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800283 }
284}
Christopher Wileya30a45e2015-10-17 10:56:59 -0700285
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800286bool write_java_dep_file(const JavaOptions& options,
287 const vector<unique_ptr<AidlImport>>& imports,
Christopher Wileyf8136192016-04-12 14:19:35 -0700288 const IoDelegate& io_delegate,
289 const string& output_file_name) {
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800290 string dep_file_name = options.DependencyFilePath();
291 if (dep_file_name.empty()) {
292 return true; // nothing to do
293 }
294 CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name);
295 if (!writer) {
296 LOG(ERROR) << "Could not open dependency file: " << dep_file_name;
297 return false;
298 }
299
300 vector<string> source_aidl = {options.input_file_name_};
Christopher Wileya30a45e2015-10-17 10:56:59 -0700301 for (const auto& import : imports) {
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800302 if (!import->GetFilename().empty()) {
303 source_aidl.push_back(import->GetFilename());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800304 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700305 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800306
Dan Willemsen93298ee2016-11-10 23:55:55 -0800307 write_common_dep_file(output_file_name, source_aidl, writer.get(),
308 options.DependencyFileNinja());
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800309
310 return true;
311}
312
Steven Moreland5557f1c2018-07-02 13:50:23 -0700313bool write_cpp_dep_file(const CppOptions& options, const AidlDefinedType& defined_type,
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800314 const vector<unique_ptr<AidlImport>>& imports,
315 const IoDelegate& io_delegate) {
316 using ::android::aidl::cpp::HeaderFile;
317 using ::android::aidl::cpp::ClassNames;
318
319 string dep_file_name = options.DependencyFilePath();
320 if (dep_file_name.empty()) {
321 return true; // nothing to do
322 }
323 CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name);
324 if (!writer) {
325 LOG(ERROR) << "Could not open dependency file: " << dep_file_name;
326 return false;
327 }
328
329 vector<string> source_aidl = {options.InputFileName()};
330 for (const auto& import : imports) {
331 if (!import->GetFilename().empty()) {
332 source_aidl.push_back(import->GetFilename());
333 }
334 }
335
Dan Willemsen93298ee2016-11-10 23:55:55 -0800336 write_common_dep_file(options.OutputCppFilePath(), source_aidl, writer.get(),
337 options.DependencyFileNinja());
338
339 if (!options.DependencyFileNinja()) {
340 vector<string> headers;
341 for (ClassNames c : {ClassNames::CLIENT,
342 ClassNames::SERVER,
343 ClassNames::INTERFACE}) {
344 headers.push_back(options.OutputHeaderDir() + '/' +
Steven Moreland5557f1c2018-07-02 13:50:23 -0700345 HeaderFile(defined_type, c, false /* use_os_sep */));
Dan Willemsen93298ee2016-11-10 23:55:55 -0800346 }
347
348 writer->Write("\n");
349
350 // Generated headers also depend on the source aidl files.
351 writer->Write("%s : \\\n %s\n", Join(headers, " \\\n ").c_str(),
352 Join(source_aidl, " \\\n ").c_str());
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800353 }
354
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800355 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800356}
357
Steven Moreland5557f1c2018-07-02 13:50:23 -0700358string generate_outputFileName(const JavaOptions& options, const AidlDefinedType& defined_type) {
359 const string& name = defined_type.GetName();
360 string package = defined_type.GetPackage();
361 string result;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800362
Steven Moreland5557f1c2018-07-02 13:50:23 -0700363 // create the path to the destination folder based on the
364 // defined_type package name
365 result = options.output_base_folder_;
366 result += OS_PATH_SEPARATOR;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800367
Steven Moreland5557f1c2018-07-02 13:50:23 -0700368 string packageStr = package;
369 size_t len = packageStr.length();
370 for (size_t i = 0; i < len; i++) {
371 if (packageStr[i] == '.') {
372 packageStr[i] = OS_PATH_SEPARATOR;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800373 }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700374 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800375
Steven Moreland5557f1c2018-07-02 13:50:23 -0700376 result += packageStr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800377
Steven Moreland5557f1c2018-07-02 13:50:23 -0700378 // add the filename by replacing the .aidl extension to .java
379 result += OS_PATH_SEPARATOR;
380 result.append(name, 0, name.find('.'));
381 result += ".java";
Adam Lesinskiffa16862014-01-23 18:17:42 -0800382
Steven Moreland5557f1c2018-07-02 13:50:23 -0700383 return result;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800384}
385
Christopher Wileyf690be52015-09-14 15:19:10 -0700386int check_and_assign_method_ids(const char * filename,
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700387 const std::vector<std::unique_ptr<AidlMethod>>& items) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800388 // Check whether there are any methods with manually assigned id's and any that are not.
389 // Either all method id's must be manually assigned or all of them must not.
390 // Also, check for duplicates of user set id's and that the id's are within the proper bounds.
391 set<int> usedIds;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800392 bool hasUnassignedIds = false;
393 bool hasAssignedIds = false;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700394 for (const auto& item : items) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700395 if (item->HasId()) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700396 hasAssignedIds = true;
Casey Dahlindff80e52015-09-29 13:57:06 -0700397 // Ensure that the user set id is not duplicated.
Casey Dahlinf4a93112015-10-05 16:58:09 -0700398 if (usedIds.find(item->GetId()) != usedIds.end()) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700399 // We found a duplicate id, so throw an error.
Adam Lesinskiffa16862014-01-23 18:17:42 -0800400 fprintf(stderr,
Casey Dahlindff80e52015-09-29 13:57:06 -0700401 "%s:%d Found duplicate method id (%d) for method: %s\n",
Casey Dahlinf4a93112015-10-05 16:58:09 -0700402 filename, item->GetLine(),
403 item->GetId(), item->GetName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800404 return 1;
405 }
Casey Dahlindff80e52015-09-29 13:57:06 -0700406 // Ensure that the user set id is within the appropriate limits
Casey Dahlinf4a93112015-10-05 16:58:09 -0700407 if (item->GetId() < kMinUserSetMethodId ||
408 item->GetId() > kMaxUserSetMethodId) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700409 fprintf(stderr, "%s:%d Found out of bounds id (%d) for method: %s\n",
Casey Dahlinf4a93112015-10-05 16:58:09 -0700410 filename, item->GetLine(),
411 item->GetId(), item->GetName().c_str());
Casey Dahlindff80e52015-09-29 13:57:06 -0700412 fprintf(stderr, " Value for id must be between %d and %d inclusive.\n",
413 kMinUserSetMethodId, kMaxUserSetMethodId);
414 return 1;
415 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700416 usedIds.insert(item->GetId());
Casey Dahlindff80e52015-09-29 13:57:06 -0700417 } else {
418 hasUnassignedIds = true;
419 }
420 if (hasAssignedIds && hasUnassignedIds) {
421 fprintf(stderr,
422 "%s: You must either assign id's to all methods or to none of them.\n",
423 filename);
424 return 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800425 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800426 }
427
428 // In the case that all methods have unassigned id's, set a unique id for them.
429 if (hasUnassignedIds) {
430 int newId = 0;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700431 for (const auto& item : items) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700432 item->SetId(newId++);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800433 }
434 }
435
436 // success
437 return 0;
438}
439
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700440bool validate_constants(const AidlInterface& interface) {
441 bool success = true;
442 set<string> names;
Steven Moreland693640b2018-07-19 13:46:27 -0700443 for (const std::unique_ptr<AidlConstantDeclaration>& constant :
444 interface.GetConstantDeclarations()) {
445 if (names.count(constant->GetName()) > 0) {
446 LOG(ERROR) << "Found duplicate constant name '" << constant->GetName() << "'";
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700447 success = false;
448 }
Steven Moreland693640b2018-07-19 13:46:27 -0700449 names.insert(constant->GetName());
450 success = success && constant->CheckValid();
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700451 }
452 return success;
453}
454
Christopher Wileyef140932015-11-03 09:29:19 -0800455// TODO: Remove this in favor of using the YACC parser b/25479378
456bool ParsePreprocessedLine(const string& line, string* decl,
457 vector<string>* package, string* class_name) {
458 // erase all trailing whitespace and semicolons
459 const size_t end = line.find_last_not_of(" ;\t");
460 if (end == string::npos) {
461 return false;
462 }
463 if (line.rfind(';', end) != string::npos) {
464 return false;
465 }
466
467 decl->clear();
468 string type;
469 vector<string> pieces = Split(line.substr(0, end + 1), " \t");
470 for (const string& piece : pieces) {
471 if (piece.empty()) {
472 continue;
473 }
474 if (decl->empty()) {
475 *decl = std::move(piece);
476 } else if (type.empty()) {
477 type = std::move(piece);
478 } else {
479 return false;
480 }
481 }
482
483 // Note that this logic is absolutely wrong. Given a parcelable
484 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
485 // the class is just Bar. However, this was the way it was done in the past.
486 //
487 // See b/17415692
488 size_t dot_pos = type.rfind('.');
489 if (dot_pos != string::npos) {
490 *class_name = type.substr(dot_pos + 1);
491 *package = Split(type.substr(0, dot_pos), ".");
492 } else {
493 *class_name = type;
494 package->clear();
495 }
496
497 return true;
498}
499
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700500} // namespace
501
502namespace internals {
503
Jiyong Park1deecc32018-07-17 01:14:41 +0900504bool parse_preprocessed_file(const IoDelegate& io_delegate, const string& filename,
505 TypeNamespace* types, AidlTypenames& typenames) {
Christopher Wileyef140932015-11-03 09:29:19 -0800506 bool success = true;
507 unique_ptr<LineReader> line_reader = io_delegate.GetLineReader(filename);
508 if (!line_reader) {
509 LOG(ERROR) << "cannot open preprocessed file: " << filename;
510 success = false;
511 return success;
512 }
513
514 string line;
515 unsigned lineno = 1;
516 for ( ; line_reader->ReadLine(&line); ++lineno) {
517 if (line.empty() || line.compare(0, 2, "//") == 0) {
518 // skip comments and empty lines
519 continue;
520 }
521
522 string decl;
523 vector<string> package;
524 string class_name;
525 if (!ParsePreprocessedLine(line, &decl, &package, &class_name)) {
526 success = false;
527 break;
528 }
529
530 if (decl == "parcelable") {
Jiyong Park1deecc32018-07-17 01:14:41 +0900531 AidlParcelable* doc =
532 new AidlParcelable(new AidlQualifiedName(class_name, ""), lineno, package);
533 types->AddParcelableType(*doc, filename);
534 typenames.AddPreprocessedType(unique_ptr<AidlParcelable>(doc));
Steven Morelanded83a282018-07-17 13:27:29 -0700535 } else if (decl == "structured_parcelable") {
536 auto temp = new std::vector<std::unique_ptr<AidlVariableDeclaration>>();
Jiyong Park1deecc32018-07-17 01:14:41 +0900537 AidlStructuredParcelable* doc = new AidlStructuredParcelable(
538 new AidlQualifiedName(class_name, ""), lineno, package, temp);
539 types->AddParcelableType(*doc, filename);
540 typenames.AddPreprocessedType(unique_ptr<AidlStructuredParcelable>(doc));
Christopher Wileyef140932015-11-03 09:29:19 -0800541 } else if (decl == "interface") {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800542 auto temp = new std::vector<std::unique_ptr<AidlMember>>();
Jiyong Park1deecc32018-07-17 01:14:41 +0900543 AidlInterface* doc = new AidlInterface(class_name, lineno, "", false, temp, package);
544 types->AddBinderType(*doc, filename);
545 typenames.AddPreprocessedType(unique_ptr<AidlInterface>(doc));
Christopher Wileyef140932015-11-03 09:29:19 -0800546 } else {
547 success = false;
548 break;
549 }
550 }
551 if (!success) {
552 LOG(ERROR) << filename << ':' << lineno
553 << " malformed preprocessed file line: '" << line << "'";
554 }
555
556 return success;
557}
558
Steven Moreland5557f1c2018-07-02 13:50:23 -0700559AidlError load_and_validate_aidl(const std::vector<std::string>& preprocessed_files,
Jiyong Park02da7422018-07-16 16:00:26 +0900560 const ImportResolver& import_resolver,
Steven Moreland5557f1c2018-07-02 13:50:23 -0700561 const std::string& input_file_name, const bool generate_traces,
562 const IoDelegate& io_delegate, TypeNamespace* types,
563 std::unique_ptr<AidlDefinedType>* returned_type,
564 std::vector<std::unique_ptr<AidlImport>>* returned_imports) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800565 AidlError err = AidlError::OK;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800566
Jiyong Park1deecc32018-07-17 01:14:41 +0900567 AidlTypenames typenames;
568
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700569 // import the preprocessed file
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700570 for (const string& s : preprocessed_files) {
Jiyong Park1deecc32018-07-17 01:14:41 +0900571 if (!parse_preprocessed_file(io_delegate, s, types, typenames)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800572 err = AidlError::BAD_PRE_PROCESSED_FILE;
Christopher Wileyef140932015-11-03 09:29:19 -0800573 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700574 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800575 if (err != AidlError::OK) {
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700576 return err;
577 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800578
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700579 // parse the input file
Jiyong Park1deecc32018-07-17 01:14:41 +0900580 Parser p{io_delegate, &typenames};
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700581 if (!p.ParseFile(input_file_name)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800582 return AidlError::PARSE_ERROR;
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700583 }
Casey Dahlin2cc93162015-10-02 16:14:17 -0700584
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800585 AidlDocument* parsed_doc = p.GetDocument();
586
Steven Morelandc258abc2018-07-10 14:03:38 -0700587 if (parsed_doc->GetDefinedTypes().empty()) {
588 LOG(ERROR) << "Cannot generate file without any definitions.";
589 return AidlError::BAD_TYPE;
590 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800591
Steven Morelandc258abc2018-07-10 14:03:38 -0700592 bool has_only_unstructured_parcelables = true;
593 for (const auto& defined_type : parsed_doc->GetDefinedTypes()) {
594 if (defined_type->AsStructuredParcelable() != nullptr ||
595 defined_type->AsInterface() != nullptr) {
596 has_only_unstructured_parcelables = false;
597 break;
598 }
599 }
600 if (has_only_unstructured_parcelables) {
601 LOG(ERROR) << "Refusing to generate code with unstructured parcelables.";
Christopher Wiley632801d2015-11-05 14:15:49 -0800602 return AidlError::FOUND_PARCELABLE;
603 }
604
Steven Morelandc258abc2018-07-10 14:03:38 -0700605 if (parsed_doc->GetDefinedTypes().size() > 1) {
606 LOG(ERROR) << "Exactly one structured type is required to be defined.";
607 return AidlError::BAD_TYPE;
608 }
609
610 unique_ptr<AidlDefinedType> defined_type(parsed_doc->ReleaseDefinedType());
611 AidlInterface* interface = defined_type->AsInterface();
612 AidlStructuredParcelable* parcelable = defined_type->AsStructuredParcelable();
613
614 CHECK(interface != nullptr || parcelable != nullptr);
615
Steven Moreland5557f1c2018-07-02 13:50:23 -0700616 if (!check_filename(input_file_name.c_str(), defined_type->GetPackage(), defined_type->GetName(),
617 defined_type->GetLine()) ||
618 !types->IsValidPackage(defined_type->GetPackage())) {
619 LOG(ERROR) << "Invalid package declaration '" << defined_type->GetPackage() << "'";
Christopher Wiley632801d2015-11-05 14:15:49 -0800620 return AidlError::BAD_PACKAGE;
621 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800622
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700623 // parse the imports of the input file
Casey Dahlin0edf3422015-10-07 12:34:59 -0700624 for (auto& import : p.GetImports()) {
Christopher Wiley934a82d2016-01-27 13:02:24 -0800625 if (types->HasImportType(*import)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700626 // There are places in the Android tree where an import doesn't resolve,
627 // but we'll pick the type up through the preprocessed types.
628 // This seems like an error, but legacy support demands we support it...
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700629 continue;
630 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700631 string import_path = import_resolver.FindImportFile(import->GetNeededClass());
Christopher Wiley72877ac2015-10-06 14:41:42 -0700632 if (import_path.empty()) {
Casey Dahlin0edf3422015-10-07 12:34:59 -0700633 cerr << import->GetFileFrom() << ":" << import->GetLine()
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700634 << ": couldn't find import for class "
Casey Dahlin0edf3422015-10-07 12:34:59 -0700635 << import->GetNeededClass() << endl;
Christopher Wiley632801d2015-11-05 14:15:49 -0800636 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700637 continue;
638 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700639 import->SetFilename(import_path);
Casey Dahlin2cc93162015-10-02 16:14:17 -0700640
Jiyong Park1deecc32018-07-17 01:14:41 +0900641 Parser p{io_delegate, &typenames};
Casey Dahlin0edf3422015-10-07 12:34:59 -0700642 if (!p.ParseFile(import->GetFilename())) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700643 cerr << "error while parsing import for class "
Casey Dahlin0edf3422015-10-07 12:34:59 -0700644 << import->GetNeededClass() << endl;
Christopher Wiley632801d2015-11-05 14:15:49 -0800645 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700646 continue;
647 }
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700648
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800649 std::unique_ptr<AidlDocument> document(p.ReleaseDocument());
Steven Morelandc258abc2018-07-10 14:03:38 -0700650 if (!check_filenames(import->GetFilename(), *document)) err = AidlError::BAD_IMPORT;
Jiyong Park822d0aa2018-07-23 21:45:51 +0900651 import->SetAidlDocument(std::move(document));
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700652 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800653 if (err != AidlError::OK) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700654 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700655 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800656
Jiyong Park1deecc32018-07-17 01:14:41 +0900657 if (!p.Resolve()) {
658 return AidlError::BAD_TYPE;
659 }
660
Steven Moreland5557f1c2018-07-02 13:50:23 -0700661 if (interface) {
662 // gather the types that have been declared
Steven Morelandc258abc2018-07-10 14:03:38 -0700663 if (!types->AddBinderType(*interface, input_file_name)) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700664 err = AidlError::BAD_TYPE;
665 }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700666 interface->SetGenerateTraces(generate_traces);
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700667 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800668
Steven Moreland5557f1c2018-07-02 13:50:23 -0700669 if (parcelable) {
Steven Morelandc258abc2018-07-10 14:03:38 -0700670 if (!types->AddParcelableType(*parcelable, input_file_name)) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700671 err = AidlError::BAD_TYPE;
672 }
673 }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800674
Steven Moreland5557f1c2018-07-02 13:50:23 -0700675 defined_type->SetLanguageType(types->GetDefinedType(*defined_type));
Martijn Coenenf1b50782018-02-21 21:06:23 +0100676
Casey Dahlin0edf3422015-10-07 12:34:59 -0700677 for (const auto& import : p.GetImports()) {
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800678 // If we skipped an unresolved import above (see comment there) we'll have
679 // an empty bucket here.
Jiyong Park822d0aa2018-07-23 21:45:51 +0900680 const AidlDocument* doc = import->GetAidlDocument();
681 if (doc == nullptr) {
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800682 continue;
683 }
684
Jiyong Park822d0aa2018-07-23 21:45:51 +0900685 if (!gather_types(import->GetFilename(), *doc, types)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800686 err = AidlError::BAD_TYPE;
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700687 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700688 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800689
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700690 // check the referenced types in parsed_doc to make sure we've imported them
Steven Morelandc258abc2018-07-10 14:03:38 -0700691 if (interface && check_types(input_file_name, interface, types) != 0) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700692 err = AidlError::BAD_TYPE;
693 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700694 if (parcelable && check_types(input_file_name, parcelable, types) != 0) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800695 err = AidlError::BAD_TYPE;
696 }
697 if (err != AidlError::OK) {
698 return err;
699 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800700
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700701 // assign method ids and validate.
Steven Moreland5557f1c2018-07-02 13:50:23 -0700702 if (interface &&
703 check_and_assign_method_ids(input_file_name.c_str(), interface->GetMethods()) != 0) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800704 return AidlError::BAD_METHOD_ID;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700705 }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700706 if (interface && !validate_constants(*interface)) {
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700707 return AidlError::BAD_CONSTANTS;
708 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800709
Steven Morelandc258abc2018-07-10 14:03:38 -0700710 *returned_type = std::move(defined_type);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700711
712 if (returned_imports)
713 p.ReleaseImports(returned_imports);
714
Christopher Wiley632801d2015-11-05 14:15:49 -0800715 return AidlError::OK;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700716}
717
Casey Dahlin2cc93162015-10-02 16:14:17 -0700718} // namespace internals
719
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700720int compile_aidl_to_cpp(const CppOptions& options,
721 const IoDelegate& io_delegate) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700722 unique_ptr<AidlDefinedType> defined_type;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700723 std::vector<std::unique_ptr<AidlImport>> imports;
Christopher Wileye3550c62015-09-29 13:26:10 -0700724 unique_ptr<cpp::TypeNamespace> types(new cpp::TypeNamespace());
Christopher Wiley56799522015-10-31 10:17:04 -0700725 types->Init();
Jiyong Park02da7422018-07-16 16:00:26 +0900726 ImportResolver import_resolver{io_delegate, options.ImportPaths(), {}};
Christopher Wiley632801d2015-11-05 14:15:49 -0800727 AidlError err = internals::load_and_validate_aidl(
Jiyong Park02da7422018-07-16 16:00:26 +0900728 options.preprocessed_files_, import_resolver, options.InputFileName(),
Steven Moreland4b37e262018-07-17 12:49:38 -0700729 options.ShouldGenTraces(), io_delegate, types.get(), &defined_type, &imports);
Christopher Wiley632801d2015-11-05 14:15:49 -0800730 if (err != AidlError::OK) {
731 return 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700732 }
733
Steven Moreland5557f1c2018-07-02 13:50:23 -0700734 CHECK(defined_type != nullptr);
735
736 if (!write_cpp_dep_file(options, *defined_type, imports, io_delegate)) {
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800737 return 1;
Christopher Wiley19059cb2015-11-05 16:11:56 -0800738 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700739
Steven Moreland5557f1c2018-07-02 13:50:23 -0700740 return (cpp::GenerateCpp(options, *types, *defined_type, io_delegate)) ? 0 : 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700741}
742
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700743int compile_aidl_to_java(const JavaOptions& options,
744 const IoDelegate& io_delegate) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700745 unique_ptr<AidlDefinedType> defined_type;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700746 std::vector<std::unique_ptr<AidlImport>> imports;
Christopher Wileydb154a52015-09-28 16:32:25 -0700747 unique_ptr<java::JavaTypeNamespace> types(new java::JavaTypeNamespace());
Christopher Wiley56799522015-10-31 10:17:04 -0700748 types->Init();
Jiyong Park02da7422018-07-16 16:00:26 +0900749 ImportResolver import_resolver{io_delegate, options.import_paths_, {}};
Christopher Wiley632801d2015-11-05 14:15:49 -0800750 AidlError aidl_err = internals::load_and_validate_aidl(
Jiyong Park02da7422018-07-16 16:00:26 +0900751 options.preprocessed_files_, import_resolver, options.input_file_name_, options.gen_traces_,
752 io_delegate, types.get(), &defined_type, &imports);
Christopher Wiley632801d2015-11-05 14:15:49 -0800753 if (aidl_err == AidlError::FOUND_PARCELABLE && !options.fail_on_parcelable_) {
754 // We aborted code generation because this file contains parcelables.
755 // However, we were not told to complain if we find parcelables.
Christopher Wileyb1bbdf82016-04-21 11:43:45 -0700756 // Just generate a dep file and exit quietly. The dep file is for a legacy
757 // use case by the SDK.
758 write_java_dep_file(options, imports, io_delegate, "");
Christopher Wiley632801d2015-11-05 14:15:49 -0800759 return 0;
760 }
761 if (aidl_err != AidlError::OK) {
762 return 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700763 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700764
Steven Moreland5557f1c2018-07-02 13:50:23 -0700765 CHECK(defined_type != nullptr);
766
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700767 string output_file_name = options.output_file_name_;
768 // if needed, generate the output file name from the base folder
Christopher Wiley632801d2015-11-05 14:15:49 -0800769 if (output_file_name.empty() && !options.output_base_folder_.empty()) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700770 output_file_name = generate_outputFileName(options, *defined_type);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700771 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800772
Casey Dahlin64533512015-10-23 17:11:21 -0700773 // make sure the folders of the output file all exists
774 if (!io_delegate.CreatePathForFile(output_file_name)) {
775 return 1;
776 }
777
Christopher Wileyf8136192016-04-12 14:19:35 -0700778 if (!write_java_dep_file(options, imports, io_delegate, output_file_name)) {
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800779 return 1;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700780 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800781
Steven Moreland5557f1c2018-07-02 13:50:23 -0700782 return generate_java(output_file_name, options.input_file_name_.c_str(), defined_type.get(),
783 types.get(), io_delegate, options);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800784}
785
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800786bool preprocess_aidl(const JavaOptions& options,
787 const IoDelegate& io_delegate) {
788 unique_ptr<CodeWriter> writer =
789 io_delegate.GetCodeWriter(options.output_file_name_);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800790
Jiyong Park02da7422018-07-16 16:00:26 +0900791 for (const auto& file : options.input_file_names_) {
Jiyong Park1deecc32018-07-17 01:14:41 +0900792 AidlTypenames typenames;
793 Parser p{io_delegate, &typenames};
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800794 if (!p.ParseFile(file))
795 return false;
796 AidlDocument* doc = p.GetDocument();
797 string line;
Casey Dahlin59401da2015-10-09 18:16:45 -0700798
Steven Morelandc258abc2018-07-10 14:03:38 -0700799 for (const auto& defined_type : doc->GetDefinedTypes()) {
Steven Morelanded83a282018-07-17 13:27:29 -0700800 if (!writer->Write("%s %s;\n", defined_type->GetPreprocessDeclarationName().c_str(),
Steven Morelandc258abc2018-07-10 14:03:38 -0700801 defined_type->GetCanonicalName().c_str())) {
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800802 return false;
803 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800804 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800805 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800806
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800807 return writer->Close();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800808}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700809
Jiyong Park02da7422018-07-16 16:00:26 +0900810bool dump_api(const JavaOptions& options, const IoDelegate& io_delegate) {
811 ImportResolver import_resolver{io_delegate, options.import_paths_, options.input_file_names_};
812
813 map<string, vector<unique_ptr<AidlDefinedType>>> types_by_package;
814 for (const auto& file : options.input_file_names_) {
815 unique_ptr<java::JavaTypeNamespace> types(new java::JavaTypeNamespace());
816 types->Init();
817 unique_ptr<AidlDefinedType> t;
818 if (internals::load_and_validate_aidl(options.preprocessed_files_, import_resolver, file,
819 options.gen_traces_, io_delegate, types.get(), &t,
820 nullptr) == AidlError::OK) {
821 // group them by package name
822 string package = t->GetPackage();
823 types_by_package[package].emplace_back(std::move(t));
824 } else {
825 return false;
826 }
827 }
828
829 // sort types within a package by their name. packages are already sorted.
830 for (auto it = types_by_package.begin(); it != types_by_package.end(); it++) {
831 auto& list = it->second;
832 std::sort(list.begin(), list.end(), [](const auto& lhs, const auto& rhs) {
833 return lhs->GetName().compare(rhs->GetName());
834 });
835 }
836
837 // print
838 unique_ptr<CodeWriter> writer = io_delegate.GetCodeWriter(options.output_file_name_);
839 for (auto it = types_by_package.begin(); it != types_by_package.end(); it++) {
840 writer->Write("package %s {\n", it->first.c_str());
841 writer->Indent();
842 for (const auto& type : it->second) {
843 type->Write(writer.get());
844 writer->Write("\n");
845 }
846 writer->Dedent();
847 writer->Write("}\n");
848 }
849
850 return writer->Close();
851}
852
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700853} // namespace android
854} // namespace aidl