blob: a3a532899818e7d272965715e75703a5f56104ad [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"
Jiyong Parke05195e2018-10-08 18:24:23 +090040#include "aidl_typenames.h"
Christopher Wileyeb1acc12015-09-16 11:25:13 -070041#include "generate_cpp.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070042#include "generate_java.h"
Steven Morelandc26d8142018-09-17 14:25:33 -070043#include "generate_ndk.h"
Christopher Wiley72877ac2015-10-06 14:41:42 -070044#include "import_resolver.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070045#include "logging.h"
46#include "options.h"
47#include "os.h"
Christopher Wileye3550c62015-09-29 13:26:10 -070048#include "type_cpp.h"
Christopher Wiley775fa1f2015-09-22 15:00:12 -070049#include "type_java.h"
Christopher Wiley84c1eac2015-09-23 13:29:28 -070050#include "type_namespace.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070051
Adam Lesinskiffa16862014-01-23 18:17:42 -080052#ifndef O_BINARY
53# define O_BINARY 0
54#endif
55
Christopher Wiley3a9911c2016-01-19 12:59:09 -080056using android::base::Join;
Christopher Wileyd76067c2015-10-19 17:00:13 -070057using android::base::Split;
Christopher Wileyc16e5e72015-09-16 10:49:40 -070058using std::cerr;
59using std::endl;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070060using std::map;
61using std::set;
62using std::string;
Christopher Wiley84c1eac2015-09-23 13:29:28 -070063using std::unique_ptr;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070064using std::vector;
Adam Lesinskiffa16862014-01-23 18:17:42 -080065
Christopher Wileyf690be52015-09-14 15:19:10 -070066namespace android {
67namespace aidl {
68namespace {
Adam Lesinskiffa16862014-01-23 18:17:42 -080069
Christopher Wileyf690be52015-09-14 15:19:10 -070070// The following are gotten as the offset from the allowable id's between
71// android.os.IBinder.FIRST_CALL_TRANSACTION=1 and
72// android.os.IBinder.LAST_CALL_TRANSACTION=16777215
73const int kMinUserSetMethodId = 0;
Jiyong Park309668e2018-07-28 16:55:44 +090074const int kMaxUserSetMethodId = 0x00ffffff;
75
76// IDs for meta transactions. Most of the meta transactions are implemented in
77// the framework side (Binder.java or Binder.cpp). But these are the ones that
78// are auto-implemented by the AIDL compiler.
79const int kGetInterfaceVersionId = ('_' << 24) | ('V' << 16) | ('E' << 8) | 'R';
Adam Lesinskiffa16862014-01-23 18:17:42 -080080
Steven Moreland92c55f12018-07-31 14:08:37 -070081bool check_filename(const std::string& filename, const AidlDefinedType& defined_type) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080082 const char* p;
83 string expected;
84 string fn;
85 size_t len;
Adam Lesinskiffa16862014-01-23 18:17:42 -080086 bool valid = false;
87
Christopher Wileybc2df692016-06-02 16:27:26 -070088 if (!IoDelegate::GetAbsolutePath(filename, &fn)) {
89 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -080090 }
91
Steven Moreland92c55f12018-07-31 14:08:37 -070092 const std::string package = defined_type.GetPackage();
Casey Dahlinfb7da2e2015-10-08 17:26:09 -070093 if (!package.empty()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080094 expected = package;
95 expected += '.';
96 }
97
98 len = expected.length();
99 for (size_t i=0; i<len; i++) {
100 if (expected[i] == '.') {
101 expected[i] = OS_PATH_SEPARATOR;
102 }
103 }
104
Steven Moreland92c55f12018-07-31 14:08:37 -0700105 const std::string name = defined_type.GetName();
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700106 expected.append(name, 0, name.find('.'));
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700107
Adam Lesinskiffa16862014-01-23 18:17:42 -0800108 expected += ".aidl";
109
110 len = fn.length();
111 valid = (len >= expected.length());
112
113 if (valid) {
114 p = fn.c_str() + (len - expected.length());
115
Elliott Hughesce310da2015-07-29 08:44:17 -0700116#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800117 if (OS_PATH_SEPARATOR != '/') {
118 // Input filename under cygwin most likely has / separators
119 // whereas the expected string uses \\ separators. Adjust
120 // them accordingly.
121 for (char *c = const_cast<char *>(p); *c; ++c) {
122 if (*c == '/') *c = OS_PATH_SEPARATOR;
123 }
124 }
125#endif
126
Yabin Cui482eefb2014-11-10 15:01:43 -0800127 // aidl assumes case-insensitivity on Mac Os and Windows.
128#if defined(__linux__)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800129 valid = (expected == p);
130#else
131 valid = !strcasecmp(expected.c_str(), p);
132#endif
133 }
134
135 if (!valid) {
Steven Moreland92c55f12018-07-31 14:08:37 -0700136 AIDL_ERROR(defined_type) << name << " should be declared in a file called " << expected;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800137 }
138
Casey Dahlin42727f82015-10-12 19:23:40 -0700139 return valid;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800140}
141
Steven Moreland92c55f12018-07-31 14:08:37 -0700142int check_types(const AidlStructuredParcelable* parcel, TypeNamespace* types) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700143 int err = 0;
144 for (const auto& v : parcel->GetFields()) {
Steven Moreland9ea10e32018-07-19 15:26:09 -0700145 if (!v->CheckValid()) {
146 err = 1;
147 }
148
Steven Moreland5557f1c2018-07-02 13:50:23 -0700149 if (!types->MaybeAddContainerType(v->GetType())) {
150 err = 1; // return type is invalid
151 }
152
Steven Moreland92c55f12018-07-31 14:08:37 -0700153 const ValidatableType* type = types->GetReturnType(v->GetType(), *parcel);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700154 if (!type) {
155 err = 1;
156 }
157
158 v->GetMutableType()->SetLanguageType(type);
159 }
160
161 return err;
162}
163
Steven Moreland92c55f12018-07-31 14:08:37 -0700164int check_types(const AidlInterface* c, TypeNamespace* types) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700165 int err = 0;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700166
167 // Has to be a pointer due to deleting copy constructor. No idea why.
168 map<string, const AidlMethod*> method_names;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700169 for (const auto& m : c->GetMethods()) {
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700170 bool oneway = m->IsOneway() || c->IsOneway();
171
Christopher Wiley934a82d2016-01-27 13:02:24 -0800172 if (!types->MaybeAddContainerType(m->GetType())) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700173 err = 1; // return type is invalid
Adam Lesinskiffa16862014-01-23 18:17:42 -0800174 }
175
Steven Moreland92c55f12018-07-31 14:08:37 -0700176 const ValidatableType* return_type = types->GetReturnType(m->GetType(), *c);
Casey Dahlina2f77c42015-12-01 18:26:02 -0800177
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900178 if (!m->GetType().CheckValid()) {
179 err = 1;
180 }
181
Casey Dahlin57dbe242015-12-04 11:44:02 -0800182 if (!return_type) {
183 err = 1;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800184 }
185
186 m->GetMutableType()->SetLanguageType(return_type);
187
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700188 if (oneway && m->GetType().GetName() != "void") {
Steven Moreland92c55f12018-07-31 14:08:37 -0700189 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot return a value";
190 err = 1;
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700191 }
192
Adam Lesinskiffa16862014-01-23 18:17:42 -0800193 int index = 1;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700194 for (const auto& arg : m->GetArguments()) {
Christopher Wiley934a82d2016-01-27 13:02:24 -0800195 if (!types->MaybeAddContainerType(arg->GetType())) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700196 err = 1;
197 }
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700198
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900199 if (!arg->GetType().CheckValid()) {
200 err = 1;
201 }
202
Steven Moreland92c55f12018-07-31 14:08:37 -0700203 const ValidatableType* arg_type = types->GetArgType(*arg, index, *c);
Casey Dahlina2f77c42015-12-01 18:26:02 -0800204
Casey Dahlin57dbe242015-12-04 11:44:02 -0800205 if (!arg_type) {
206 err = 1;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800207 }
208
209 arg->GetMutableType()->SetLanguageType(arg_type);
210
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700211 if (oneway && arg->IsOut()) {
Steven Moreland92c55f12018-07-31 14:08:37 -0700212 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot have out parameters";
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700213 err = 1;
214 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800215 }
216
Casey Dahlinf4a93112015-10-05 16:58:09 -0700217 auto it = method_names.find(m->GetName());
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700218 // prevent duplicate methods
Casey Dahlinf4a93112015-10-05 16:58:09 -0700219 if (it == method_names.end()) {
220 method_names[m->GetName()] = m.get();
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700221 } else {
Steven Moreland92c55f12018-07-31 14:08:37 -0700222 AIDL_ERROR(m) << "attempt to redefine method " << m->GetName() << ":";
223 AIDL_ERROR(it->second) << "previously defined here.";
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700224 err = 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800225 }
Jiyong Park309668e2018-07-28 16:55:44 +0900226
227 static set<string> reserved_methods{"asBinder()", "getInterfaceVersion()",
228 "getTransactionName(int)"};
229
230 if (reserved_methods.find(m->Signature()) != reserved_methods.end()) {
231 AIDL_ERROR(m) << " method " << m->Signature() << " is reserved for internal use." << endl;
232 err = 1;
233 }
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700234 }
235 return err;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800236}
237
Jiyong Park74595c12018-07-23 15:22:50 +0900238bool write_dep_file(const Options& options, const AidlDefinedType& defined_type,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900239 const vector<string>& imports, const IoDelegate& io_delegate,
Jiyong Park74595c12018-07-23 15:22:50 +0900240 const string& input_file, const string& output_file) {
241 string dep_file_name = options.DependencyFile();
242 if (dep_file_name.empty() && options.AutoDepFile()) {
243 dep_file_name = output_file + ".d";
244 }
245
246 if (dep_file_name.empty()) {
247 return true; // nothing to do
248 }
Jiyong Park05463732018-08-09 16:03:02 +0900249
Jiyong Park74595c12018-07-23 15:22:50 +0900250 CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name);
251 if (!writer) {
252 LOG(ERROR) << "Could not open dependency file: " << dep_file_name;
253 return false;
254 }
255
256 vector<string> source_aidl = {input_file};
257 for (const auto& import : imports) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900258 source_aidl.push_back(import);
Jiyong Park74595c12018-07-23 15:22:50 +0900259 }
260
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800261 // Encode that the output file depends on aidl input files.
262 writer->Write("%s : \\\n", output_file.c_str());
Jiyong Park74595c12018-07-23 15:22:50 +0900263 writer->Write(" %s", Join(source_aidl, " \\\n ").c_str());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800264 writer->Write("\n");
Christopher Wileya30a45e2015-10-17 10:56:59 -0700265
Jiyong Park74595c12018-07-23 15:22:50 +0900266 if (!options.DependencyFileNinja()) {
Dan Willemsen93298ee2016-11-10 23:55:55 -0800267 writer->Write("\n");
268 // Output "<input_aidl_file>: " so make won't fail if the input .aidl file
269 // has been deleted, moved or renamed in incremental build.
Jiyong Park74595c12018-07-23 15:22:50 +0900270 for (const auto& src : source_aidl) {
Dan Willemsen93298ee2016-11-10 23:55:55 -0800271 writer->Write("%s :\n", src.c_str());
272 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800273 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700274
Steven Morelandc26d8142018-09-17 14:25:33 -0700275 if (options.IsCppOutput()) {
Jiyong Park74595c12018-07-23 15:22:50 +0900276 if (!options.DependencyFileNinja()) {
277 using ::android::aidl::cpp::ClassNames;
278 using ::android::aidl::cpp::HeaderFile;
279 vector<string> headers;
280 for (ClassNames c : {ClassNames::CLIENT, ClassNames::SERVER, ClassNames::INTERFACE}) {
Jiyong Park05463732018-08-09 16:03:02 +0900281 headers.push_back(options.OutputHeaderDir() +
Jiyong Park74595c12018-07-23 15:22:50 +0900282 HeaderFile(defined_type, c, false /* use_os_sep */));
283 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800284
Jiyong Park74595c12018-07-23 15:22:50 +0900285 writer->Write("\n");
286
287 // Generated headers also depend on the source aidl files.
288 writer->Write("%s : \\\n %s\n", Join(headers, " \\\n ").c_str(),
289 Join(source_aidl, " \\\n ").c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800290 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700291 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800292
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800293 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800294}
295
Jiyong Park74595c12018-07-23 15:22:50 +0900296string generate_outputFileName(const Options& options, const AidlDefinedType& defined_type) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700297 // create the path to the destination folder based on the
298 // defined_type package name
Jiyong Park74595c12018-07-23 15:22:50 +0900299 string result = options.OutputDir();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800300
Jiyong Park74595c12018-07-23 15:22:50 +0900301 string package = defined_type.GetPackage();
302 size_t len = package.length();
Steven Moreland5557f1c2018-07-02 13:50:23 -0700303 for (size_t i = 0; i < len; i++) {
Jiyong Park74595c12018-07-23 15:22:50 +0900304 if (package[i] == '.') {
305 package[i] = OS_PATH_SEPARATOR;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800306 }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700307 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800308
Jiyong Park74595c12018-07-23 15:22:50 +0900309 result += package;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800310
Steven Moreland5557f1c2018-07-02 13:50:23 -0700311 // add the filename by replacing the .aidl extension to .java
Jiyong Park74595c12018-07-23 15:22:50 +0900312 const string& name = defined_type.GetName();
Steven Moreland5557f1c2018-07-02 13:50:23 -0700313 result += OS_PATH_SEPARATOR;
314 result.append(name, 0, name.find('.'));
Jiyong Parkb03551f2018-08-06 19:20:51 +0900315 if (options.TargetLanguage() == Options::Language::JAVA) {
316 result += ".java";
Steven Morelandc26d8142018-09-17 14:25:33 -0700317 } else if (options.IsCppOutput()) {
Jiyong Parkb03551f2018-08-06 19:20:51 +0900318 result += ".cpp";
319 } else {
320 LOG(FATAL) << "Should not reach here" << endl;
321 return "";
322 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800323
Steven Moreland5557f1c2018-07-02 13:50:23 -0700324 return result;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800325}
326
Jiyong Parkb034bf02018-07-30 17:44:33 +0900327bool check_and_assign_method_ids(const std::vector<std::unique_ptr<AidlMethod>>& items) {
Jiyong Park309668e2018-07-28 16:55:44 +0900328 // Check whether there are any methods with manually assigned id's and any
329 // that are not. Either all method id's must be manually assigned or all of
330 // them must not. Also, check for uplicates of user set ID's and that the
331 // ID's are within the proper bounds.
332 set<int> usedIds;
333 bool hasUnassignedIds = false;
334 bool hasAssignedIds = false;
335 for (const auto& item : items) {
336 // However, meta transactions that are added by the AIDL compiler are
337 // exceptions. They have fixed IDs but allowed to be with user-defined
338 // methods having auto-assigned IDs. This is because the Ids of the meta
339 // transactions must be stable during the entire lifetime of an interface.
340 // In other words, their IDs must be the same even when new user-defined
341 // methods are added.
342 if (item->HasId() && item->IsUserDefined()) {
343 hasAssignedIds = true;
344 // Ensure that the user set id is not duplicated.
345 if (usedIds.find(item->GetId()) != usedIds.end()) {
346 // We found a duplicate id, so throw an error.
347 AIDL_ERROR(item) << "Found duplicate method id (" << item->GetId() << ") for method "
348 << item->GetName();
349 return false;
350 }
351 // Ensure that the user set id is within the appropriate limits
352 if (item->GetId() < kMinUserSetMethodId || item->GetId() > kMaxUserSetMethodId) {
353 AIDL_ERROR(item) << "Found out of bounds id (" << item->GetId() << ") for method "
354 << item->GetName() << ". Value for id must be between "
355 << kMinUserSetMethodId << " and " << kMaxUserSetMethodId << " inclusive.";
356 return false;
357 }
358 usedIds.insert(item->GetId());
359 } else {
360 hasUnassignedIds = true;
361 }
362 if (hasAssignedIds && hasUnassignedIds) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900363 AIDL_ERROR(item) << "You must either assign id's to all methods or to none of them.";
Jiyong Park309668e2018-07-28 16:55:44 +0900364 return false;
365 }
366 }
367
368 // In the case that all methods have unassigned id's, set a unique id for them.
369 if (hasUnassignedIds) {
370 int newId = kMinUserSetMethodId;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700371 for (const auto& item : items) {
Jiyong Park309668e2018-07-28 16:55:44 +0900372 assert(newId <= kMaxUserSetMethoId);
373 if (item->IsUserDefined()) {
374 item->SetId(newId++);
Jiyong Park309668e2018-07-28 16:55:44 +0900375 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800376 }
Jiyong Park309668e2018-07-28 16:55:44 +0900377 }
378 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800379}
380
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700381bool validate_constants(const AidlInterface& interface) {
382 bool success = true;
383 set<string> names;
Steven Moreland693640b2018-07-19 13:46:27 -0700384 for (const std::unique_ptr<AidlConstantDeclaration>& constant :
385 interface.GetConstantDeclarations()) {
386 if (names.count(constant->GetName()) > 0) {
387 LOG(ERROR) << "Found duplicate constant name '" << constant->GetName() << "'";
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700388 success = false;
389 }
Steven Moreland693640b2018-07-19 13:46:27 -0700390 names.insert(constant->GetName());
391 success = success && constant->CheckValid();
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700392 }
393 return success;
394}
395
Christopher Wileyef140932015-11-03 09:29:19 -0800396// TODO: Remove this in favor of using the YACC parser b/25479378
397bool ParsePreprocessedLine(const string& line, string* decl,
398 vector<string>* package, string* class_name) {
399 // erase all trailing whitespace and semicolons
400 const size_t end = line.find_last_not_of(" ;\t");
401 if (end == string::npos) {
402 return false;
403 }
404 if (line.rfind(';', end) != string::npos) {
405 return false;
406 }
407
408 decl->clear();
409 string type;
410 vector<string> pieces = Split(line.substr(0, end + 1), " \t");
411 for (const string& piece : pieces) {
412 if (piece.empty()) {
413 continue;
414 }
415 if (decl->empty()) {
416 *decl = std::move(piece);
417 } else if (type.empty()) {
418 type = std::move(piece);
419 } else {
420 return false;
421 }
422 }
423
424 // Note that this logic is absolutely wrong. Given a parcelable
425 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
426 // the class is just Bar. However, this was the way it was done in the past.
427 //
428 // See b/17415692
429 size_t dot_pos = type.rfind('.');
430 if (dot_pos != string::npos) {
431 *class_name = type.substr(dot_pos + 1);
432 *package = Split(type.substr(0, dot_pos), ".");
433 } else {
434 *class_name = type;
435 package->clear();
436 }
437
438 return true;
439}
440
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700441} // namespace
442
443namespace internals {
444
Jiyong Park1deecc32018-07-17 01:14:41 +0900445bool parse_preprocessed_file(const IoDelegate& io_delegate, const string& filename,
446 TypeNamespace* types, AidlTypenames& typenames) {
Christopher Wileyef140932015-11-03 09:29:19 -0800447 bool success = true;
448 unique_ptr<LineReader> line_reader = io_delegate.GetLineReader(filename);
449 if (!line_reader) {
450 LOG(ERROR) << "cannot open preprocessed file: " << filename;
451 success = false;
452 return success;
453 }
454
455 string line;
456 unsigned lineno = 1;
457 for ( ; line_reader->ReadLine(&line); ++lineno) {
458 if (line.empty() || line.compare(0, 2, "//") == 0) {
459 // skip comments and empty lines
460 continue;
461 }
462
463 string decl;
464 vector<string> package;
465 string class_name;
466 if (!ParsePreprocessedLine(line, &decl, &package, &class_name)) {
467 success = false;
468 break;
469 }
470
Steven Moreland46e9da82018-07-27 15:45:29 -0700471 AidlLocation::Point point = {.line = lineno, .column = 0 /*column*/};
472 AidlLocation location = AidlLocation(filename, point, point);
473
Christopher Wileyef140932015-11-03 09:29:19 -0800474 if (decl == "parcelable") {
Jiyong Park1deecc32018-07-17 01:14:41 +0900475 AidlParcelable* doc =
Steven Moreland46e9da82018-07-27 15:45:29 -0700476 new AidlParcelable(location, new AidlQualifiedName(location, class_name, ""), package);
Jiyong Park1deecc32018-07-17 01:14:41 +0900477 types->AddParcelableType(*doc, filename);
478 typenames.AddPreprocessedType(unique_ptr<AidlParcelable>(doc));
Steven Morelanded83a282018-07-17 13:27:29 -0700479 } else if (decl == "structured_parcelable") {
480 auto temp = new std::vector<std::unique_ptr<AidlVariableDeclaration>>();
Jiyong Park1deecc32018-07-17 01:14:41 +0900481 AidlStructuredParcelable* doc = new AidlStructuredParcelable(
Steven Moreland46e9da82018-07-27 15:45:29 -0700482 location, new AidlQualifiedName(location, class_name, ""), package, temp);
Jiyong Park1deecc32018-07-17 01:14:41 +0900483 types->AddParcelableType(*doc, filename);
484 typenames.AddPreprocessedType(unique_ptr<AidlStructuredParcelable>(doc));
Christopher Wileyef140932015-11-03 09:29:19 -0800485 } else if (decl == "interface") {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800486 auto temp = new std::vector<std::unique_ptr<AidlMember>>();
Steven Moreland46e9da82018-07-27 15:45:29 -0700487 AidlInterface* doc = new AidlInterface(location, class_name, "", false, temp, package);
Jiyong Park1deecc32018-07-17 01:14:41 +0900488 types->AddBinderType(*doc, filename);
489 typenames.AddPreprocessedType(unique_ptr<AidlInterface>(doc));
Christopher Wileyef140932015-11-03 09:29:19 -0800490 } else {
491 success = false;
492 break;
493 }
494 }
495 if (!success) {
496 LOG(ERROR) << filename << ':' << lineno
497 << " malformed preprocessed file line: '" << line << "'";
498 }
499
500 return success;
501}
502
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900503AidlError load_and_validate_aidl(const std::string& input_file_name, const Options& options,
504 const IoDelegate& io_delegate, TypeNamespace* types,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900505 vector<AidlDefinedType*>* defined_types,
506 vector<string>* imported_files) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800507 AidlError err = AidlError::OK;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800508
Jiyong Parkb034bf02018-07-30 17:44:33 +0900509 //////////////////////////////////////////////////////////////////////////
510 // Loading phase
511 //////////////////////////////////////////////////////////////////////////
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900512
Jiyong Parkb034bf02018-07-30 17:44:33 +0900513 // Parse the main input file
Steven Moreland64e29be2018-08-08 18:52:19 -0700514 std::unique_ptr<Parser> main_parser =
515 Parser::Parse(input_file_name, io_delegate, types->typenames_);
516 if (main_parser == nullptr) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900517 return AidlError::PARSE_ERROR;
518 }
Steven Moreland64e29be2018-08-08 18:52:19 -0700519 if (!types->AddDefinedTypes(main_parser->GetDefinedTypes(), input_file_name)) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900520 return AidlError::BAD_TYPE;
521 }
522
523 // Import the preprocessed file
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900524 for (const string& s : options.PreprocessedFiles()) {
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900525 if (!parse_preprocessed_file(io_delegate, s, types, types->typenames_)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800526 err = AidlError::BAD_PRE_PROCESSED_FILE;
Christopher Wileyef140932015-11-03 09:29:19 -0800527 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700528 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800529 if (err != AidlError::OK) {
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700530 return err;
531 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800532
Jiyong Parkb034bf02018-07-30 17:44:33 +0900533 // Find files to import and parse them
Jiyong Parke59c3682018-09-11 23:10:25 +0900534 vector<string> import_paths;
Jiyong Park8c380532018-08-30 14:55:26 +0900535 ImportResolver import_resolver{io_delegate, input_file_name, options.ImportDirs(),
536 options.InputFiles()};
Jiyong Parke59c3682018-09-11 23:10:25 +0900537
538 set<string> type_from_import_statements;
Steven Moreland64e29be2018-08-08 18:52:19 -0700539 for (const auto& import : main_parser->GetImports()) {
Jiyong Parke05195e2018-10-08 18:24:23 +0900540 if (!AidlTypenames::IsBuiltinTypename(import->GetNeededClass())) {
541 type_from_import_statements.emplace(import->GetNeededClass());
542 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900543 }
544
545 // When referencing a type using fully qualified name it should be imported
546 // without the import statement. To support that, add all unresolved
547 // typespecs encountered during the parsing to the import_candidates list.
548 // Note that there is no guarantee that the typespecs are all fully qualified.
549 // It will be determined by calling FindImportFile().
550 set<string> unresolved_types;
551 for (const auto type : main_parser->GetUnresolvedTypespecs()) {
552 if (!AidlTypenames::IsBuiltinTypename(type->GetName())) {
553 unresolved_types.emplace(type->GetName());
554 }
555 }
556 set<string> import_candidates(type_from_import_statements);
557 import_candidates.insert(unresolved_types.begin(), unresolved_types.end());
558 for (const auto& import : import_candidates) {
559 if (types->HasImportType(import)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700560 // There are places in the Android tree where an import doesn't resolve,
561 // but we'll pick the type up through the preprocessed types.
562 // This seems like an error, but legacy support demands we support it...
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700563 continue;
564 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900565 string import_path = import_resolver.FindImportFile(import);
Christopher Wiley72877ac2015-10-06 14:41:42 -0700566 if (import_path.empty()) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900567 if (type_from_import_statements.find(import) != type_from_import_statements.end()) {
568 // Complain only when the import from the import statement has failed.
569 AIDL_ERROR(import) << "couldn't find import for class " << import;
570 err = AidlError::BAD_IMPORT;
571 }
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700572 continue;
573 }
Casey Dahlin2cc93162015-10-02 16:14:17 -0700574
Jiyong Parke59c3682018-09-11 23:10:25 +0900575 import_paths.emplace_back(import_path);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900576
Steven Moreland64e29be2018-08-08 18:52:19 -0700577 std::unique_ptr<Parser> import_parser =
578 Parser::Parse(import_path, io_delegate, types->typenames_);
579 if (import_parser == nullptr) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900580 cerr << "error while importing " << import_path << " for " << import << endl;
Christopher Wiley632801d2015-11-05 14:15:49 -0800581 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700582 continue;
583 }
Steven Moreland64e29be2018-08-08 18:52:19 -0700584 if (!types->AddDefinedTypes(import_parser->GetDefinedTypes(), import_path)) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900585 return AidlError::BAD_TYPE;
586 }
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700587 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800588 if (err != AidlError::OK) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700589 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700590 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800591
Jiyong Park3c35e392018-08-30 13:10:30 +0900592 for (const auto& imported_file : options.ImportFiles()) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900593 import_paths.emplace_back(imported_file);
Jiyong Park3c35e392018-08-30 13:10:30 +0900594
595 std::unique_ptr<Parser> import_parser =
596 Parser::Parse(imported_file, io_delegate, types->typenames_);
597 if (import_parser == nullptr) {
598 AIDL_ERROR(imported_file) << "error while importing " << imported_file;
599 err = AidlError::BAD_IMPORT;
600 continue;
601 }
602 if (!types->AddDefinedTypes(import_parser->GetDefinedTypes(), imported_file)) {
603 return AidlError::BAD_TYPE;
604 }
605 }
606 if (err != AidlError::OK) {
607 return err;
608 }
609
Jiyong Parkb034bf02018-07-30 17:44:33 +0900610 //////////////////////////////////////////////////////////////////////////
611 // Validation phase
612 //////////////////////////////////////////////////////////////////////////
Jiyong Park96c16a92018-08-16 16:37:09 +0900613 const bool is_check_api = options.GetTask() == Options::Task::CHECK_API;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900614
615 // Resolve the unresolved type references found from the input file
Jiyong Park96c16a92018-08-16 16:37:09 +0900616 if (!is_check_api && !main_parser->Resolve()) {
617 // Resolution is not need for check api because all typespecs are
618 // using fully qualified names.
Jiyong Park1deecc32018-07-17 01:14:41 +0900619 return AidlError::BAD_TYPE;
620 }
621
Steven Morelande2c64b42018-09-18 15:06:37 -0700622 // For legacy reasons, by default, compiling an unstructured parcelable (which contains no output)
623 // is allowed. This must not be returned as an error until the very end of this procedure since
624 // this may be considered a success, and we should first check that there are not other, more
625 // serious failures.
626 bool contains_unstructured_parcelable = false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800627
Steven Moreland64e29be2018-08-08 18:52:19 -0700628 const int num_defined_types = main_parser->GetDefinedTypes().size();
629 for (const auto defined_type : main_parser->GetDefinedTypes()) {
Steven Morelande2c64b42018-09-18 15:06:37 -0700630 AidlParcelable* unstructuredParcelable = defined_type->AsUnstructuredParcelable();
631 if (unstructuredParcelable != nullptr) {
632 AIDL_ERROR(unstructuredParcelable)
633 << "Refusing to generate code with unstructured parcelables. Declared parcelables should "
634 "be in their own file and/or cannot be used with --structured interfaces.";
635 contains_unstructured_parcelable = true;
636 continue;
637 }
638
Jiyong Parkb034bf02018-07-30 17:44:33 +0900639 // Ensure that a type is either an interface or a structured parcelable
640 AidlInterface* interface = defined_type->AsInterface();
641 AidlStructuredParcelable* parcelable = defined_type->AsStructuredParcelable();
642 CHECK(interface != nullptr || parcelable != nullptr);
643
644 // Ensure that foo.bar.IFoo is defined in <some_path>/foo/bar/IFoo.aidl
Jiyong Parke59c3682018-09-11 23:10:25 +0900645 if (num_defined_types == 1 && !check_filename(input_file_name, *defined_type)) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900646 return AidlError::BAD_PACKAGE;
647 }
648
649 // Link the AIDL type with the type of the target language. This will
650 // be removed when the migration to AidlTypenames is done.
651 defined_type->SetLanguageType(types->GetDefinedType(*defined_type));
652
Jiyong Parkb034bf02018-07-30 17:44:33 +0900653 // Check the referenced types in parsed_doc to make sure we've imported them
Jiyong Park96c16a92018-08-16 16:37:09 +0900654 if (!is_check_api) {
655 // No need to do this for check api because all typespecs are already
656 // using fully qualified name and we don't import in AIDL files.
657 if (interface != nullptr && check_types(interface, types) != 0) {
658 return AidlError::BAD_TYPE;
659 }
660 if (parcelable != nullptr && check_types(parcelable, types) != 0) {
661 return AidlError::BAD_TYPE;
662 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900663 }
664
665 if (interface != nullptr) {
666 // add the meta-method 'int getInterfaceVersion()' if version is specified.
667 if (options.Version() > 0) {
668 AidlTypeSpecifier* ret =
Steven Moreland02e012e2018-08-02 14:58:10 -0700669 new AidlTypeSpecifier(AIDL_LOCATION_HERE, "int", false, nullptr, "");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900670 vector<unique_ptr<AidlArgument>>* args = new vector<unique_ptr<AidlArgument>>();
671 AidlMethod* method =
Steven Moreland02e012e2018-08-02 14:58:10 -0700672 new AidlMethod(AIDL_LOCATION_HERE, false, ret, "getInterfaceVersion", args, "",
Jiyong Parkb034bf02018-07-30 17:44:33 +0900673 kGetInterfaceVersionId, false /* is_user_defined */);
674 interface->GetMutableMethods().emplace_back(method);
675 }
676 if (!check_and_assign_method_ids(interface->GetMethods())) {
677 return AidlError::BAD_METHOD_ID;
678 }
679 if (!validate_constants(*interface)) {
680 return AidlError::BAD_CONSTANTS;
681 }
682 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800683 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800684
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900685 if (options.IsStructured()) {
Steven Moreland6cee3482018-07-18 14:39:58 -0700686 types->typenames_.IterateTypes([&](const AidlDefinedType& type) {
687 if (type.AsUnstructuredParcelable() != nullptr) {
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700688 err = AidlError::NOT_STRUCTURED;
Steven Moreland6cee3482018-07-18 14:39:58 -0700689 LOG(ERROR) << type.GetCanonicalName()
690 << " is not structured, but this is a structured interface.";
691 }
692 });
693 }
694 if (err != AidlError::OK) {
695 return err;
696 }
697
Jiyong Parkb034bf02018-07-30 17:44:33 +0900698 if (defined_types != nullptr) {
Steven Moreland64e29be2018-08-08 18:52:19 -0700699 *defined_types = main_parser->GetDefinedTypes();
Jiyong Park309668e2018-07-28 16:55:44 +0900700 }
701
Jiyong Parkb034bf02018-07-30 17:44:33 +0900702 if (imported_files != nullptr) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900703 *imported_files = import_paths;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700704 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700705
Steven Morelande2c64b42018-09-18 15:06:37 -0700706 if (contains_unstructured_parcelable) {
707 // Considered a success for the legacy case, so this must be returned last.
708 return AidlError::FOUND_PARCELABLE;
709 }
710
Christopher Wiley632801d2015-11-05 14:15:49 -0800711 return AidlError::OK;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700712}
713
Casey Dahlin2cc93162015-10-02 16:14:17 -0700714} // namespace internals
715
Jiyong Parkb034bf02018-07-30 17:44:33 +0900716int compile_aidl(const Options& options, const IoDelegate& io_delegate) {
717 const Options::Language lang = options.TargetLanguage();
Jiyong Park74595c12018-07-23 15:22:50 +0900718 for (const string& input_file : options.InputFiles()) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900719 // Create type namespace that will hold the types identified by the parser.
720 // This two namespaces that are specific to the target language will be
721 // unified to AidlTypenames which is agnostic to the target language.
722 cpp::TypeNamespace cpp_types;
723 cpp_types.Init();
724
725 java::JavaTypeNamespace java_types;
726 java_types.Init();
727
728 TypeNamespace* types;
Steven Morelandc26d8142018-09-17 14:25:33 -0700729 if (options.IsCppOutput()) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900730 types = &cpp_types;
731 } else if (lang == Options::Language::JAVA) {
732 types = &java_types;
733 } else {
734 LOG(FATAL) << "Unsupported target language." << endl;
Jiyong Park74595c12018-07-23 15:22:50 +0900735 return 1;
736 }
737
Jiyong Parkb034bf02018-07-30 17:44:33 +0900738 vector<AidlDefinedType*> defined_types;
739 vector<string> imported_files;
Jiyong Park74595c12018-07-23 15:22:50 +0900740
Jiyong Parkb034bf02018-07-30 17:44:33 +0900741 AidlError aidl_err = internals::load_and_validate_aidl(input_file, options, io_delegate, types,
742 &defined_types, &imported_files);
Jiyong Park74595c12018-07-23 15:22:50 +0900743 if (aidl_err == AidlError::FOUND_PARCELABLE && !options.FailOnParcelable()) {
744 // We aborted code generation because this file contains parcelables.
745 // However, we were not told to complain if we find parcelables.
746 // Just generate a dep file and exit quietly. The dep file is for a legacy
747 // use case by the SDK.
Jiyong Parkb034bf02018-07-30 17:44:33 +0900748 for (const auto defined_type : defined_types) {
749 write_dep_file(options, *defined_type, imported_files, io_delegate, input_file, "");
750 }
Jiyong Park74595c12018-07-23 15:22:50 +0900751 return 0;
752 }
753 if (aidl_err != AidlError::OK) {
754 return 1;
755 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700756
Jiyong Parkb034bf02018-07-30 17:44:33 +0900757 for (const auto defined_type : defined_types) {
758 CHECK(defined_type != nullptr);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700759
Jiyong Parkb034bf02018-07-30 17:44:33 +0900760 string output_file_name = options.OutputFile();
761 // if needed, generate the output file name from the base folder
762 if (output_file_name.empty() && !options.OutputDir().empty()) {
Jiyong Parkb03551f2018-08-06 19:20:51 +0900763 output_file_name = generate_outputFileName(options, *defined_type);
764 if (output_file_name.empty()) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900765 return 1;
766 }
767 }
Jiyong Park74595c12018-07-23 15:22:50 +0900768
Jiyong Parkb034bf02018-07-30 17:44:33 +0900769 if (!write_dep_file(options, *defined_type, imported_files, io_delegate, input_file,
770 output_file_name)) {
771 return 1;
772 }
Jiyong Park74595c12018-07-23 15:22:50 +0900773
Jiyong Parkb034bf02018-07-30 17:44:33 +0900774 bool success = false;
775 if (lang == Options::Language::CPP) {
776 success =
777 cpp::GenerateCpp(output_file_name, options, cpp_types, *defined_type, io_delegate);
Steven Morelandc26d8142018-09-17 14:25:33 -0700778 } else if (lang == Options::Language::NDK) {
779 ndk::GenerateNdk(output_file_name, options, cpp_types.typenames_, *defined_type,
780 io_delegate);
781 success = true;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900782 } else if (lang == Options::Language::JAVA) {
783 success = java::generate_java(output_file_name, input_file, defined_type, &java_types,
784 io_delegate, options);
785 } else {
786 LOG(FATAL) << "Should not reach here" << endl;
787 return 1;
788 }
789 if (!success) {
790 return 1;
791 }
Jiyong Park74595c12018-07-23 15:22:50 +0900792 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700793 }
Jiyong Park74595c12018-07-23 15:22:50 +0900794 return 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800795}
796
Jiyong Park74595c12018-07-23 15:22:50 +0900797bool preprocess_aidl(const Options& options, const IoDelegate& io_delegate) {
798 unique_ptr<CodeWriter> writer = io_delegate.GetCodeWriter(options.OutputFile());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800799
Jiyong Park74595c12018-07-23 15:22:50 +0900800 for (const auto& file : options.InputFiles()) {
Jiyong Park1deecc32018-07-17 01:14:41 +0900801 AidlTypenames typenames;
Steven Moreland64e29be2018-08-08 18:52:19 -0700802 std::unique_ptr<Parser> p = Parser::Parse(file, io_delegate, typenames);
803 if (p == nullptr) return false;
Casey Dahlin59401da2015-10-09 18:16:45 -0700804
Steven Moreland64e29be2018-08-08 18:52:19 -0700805 for (const auto& defined_type : p->GetDefinedTypes()) {
Steven Morelanded83a282018-07-17 13:27:29 -0700806 if (!writer->Write("%s %s;\n", defined_type->GetPreprocessDeclarationName().c_str(),
Steven Morelandc258abc2018-07-10 14:03:38 -0700807 defined_type->GetCanonicalName().c_str())) {
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800808 return false;
809 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800810 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800811 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800812
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800813 return writer->Close();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800814}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700815
Jiyong Parke59c3682018-09-11 23:10:25 +0900816static string GetApiDumpPathFor(const AidlDefinedType& defined_type, const Options& options) {
817 string package_as_path = Join(Split(defined_type.GetPackage(), "."), OS_PATH_SEPARATOR);
818 CHECK(!options.OutputDir().empty() && options.OutputDir().back() == '/');
819 return options.OutputDir() + package_as_path + OS_PATH_SEPARATOR + defined_type.GetName() +
820 ".aidl";
821}
Jiyong Parkb034bf02018-07-30 17:44:33 +0900822
Jiyong Parke59c3682018-09-11 23:10:25 +0900823bool dump_api(const Options& options, const IoDelegate& io_delegate) {
Jiyong Park74595c12018-07-23 15:22:50 +0900824 for (const auto& file : options.InputFiles()) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900825 java::JavaTypeNamespace ns;
826 ns.Init();
Jiyong Parkb034bf02018-07-30 17:44:33 +0900827 vector<AidlDefinedType*> defined_types;
Jiyong Parke59c3682018-09-11 23:10:25 +0900828 if (internals::load_and_validate_aidl(file, options, io_delegate, &ns, &defined_types,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900829 nullptr) == AidlError::OK) {
830 for (const auto type : defined_types) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900831 unique_ptr<CodeWriter> writer =
832 io_delegate.GetCodeWriter(GetApiDumpPathFor(*type, options));
Steven Morelandec0531d2018-09-20 11:11:20 -0700833 if (!type->GetPackage().empty()) {
834 (*writer) << "package " << type->GetPackage() << ";\n";
835 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900836 type->Write(writer.get());
Jiyong Parkb034bf02018-07-30 17:44:33 +0900837 }
Jiyong Park02da7422018-07-16 16:00:26 +0900838 } else {
839 return false;
840 }
841 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900842 return true;
Jiyong Park02da7422018-07-16 16:00:26 +0900843}
844
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700845} // namespace android
846} // namespace aidl