blob: ebd8c7cab1963036aae779f0e8dc95e95a78af53 [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
Jiyong Park965c5b92018-11-21 13:37:15 +090070// Copied from android.is.IBinder.[FIRST|LAST]_CALL_TRANSACTION
71const int kFirstCallTransaction = 1;
72const int kLastCallTransaction = 0x00ffffff;
73
74// Following IDs are all offsets from kFirstCallTransaction
Jiyong Park309668e2018-07-28 16:55:44 +090075
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.
Jiyong Park965c5b92018-11-21 13:37:15 +090079const int kFirstMetaMethodId = kLastCallTransaction - kFirstCallTransaction;
80const int kGetInterfaceVersionId = kFirstMetaMethodId;
81// Additional meta transactions implemented by AIDL should use
82// kFirstMetaMethodId -1, -2, ...and so on.
83
84// Reserve 100 IDs for meta methods, which is more than enough. If we don't reserve,
85// in the future, a newly added meta transaction ID will have a chance to
86// collide with the user-defined methods that were added in the past. So,
87// let's prevent users from using IDs in this range from the beginning.
88const int kLastMetaMethodId = kFirstMetaMethodId - 99;
89
90// Range of IDs that is allowed for user-defined methods.
91const int kMinUserSetMethodId = 0;
92const int kMaxUserSetMethodId = kLastMetaMethodId - 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -080093
Steven Moreland92c55f12018-07-31 14:08:37 -070094bool check_filename(const std::string& filename, const AidlDefinedType& defined_type) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080095 const char* p;
96 string expected;
97 string fn;
98 size_t len;
Adam Lesinskiffa16862014-01-23 18:17:42 -080099 bool valid = false;
100
Christopher Wileybc2df692016-06-02 16:27:26 -0700101 if (!IoDelegate::GetAbsolutePath(filename, &fn)) {
102 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800103 }
104
Steven Moreland92c55f12018-07-31 14:08:37 -0700105 const std::string package = defined_type.GetPackage();
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700106 if (!package.empty()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800107 expected = package;
108 expected += '.';
109 }
110
111 len = expected.length();
112 for (size_t i=0; i<len; i++) {
113 if (expected[i] == '.') {
114 expected[i] = OS_PATH_SEPARATOR;
115 }
116 }
117
Steven Moreland92c55f12018-07-31 14:08:37 -0700118 const std::string name = defined_type.GetName();
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700119 expected.append(name, 0, name.find('.'));
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700120
Adam Lesinskiffa16862014-01-23 18:17:42 -0800121 expected += ".aidl";
122
123 len = fn.length();
124 valid = (len >= expected.length());
125
126 if (valid) {
127 p = fn.c_str() + (len - expected.length());
128
Elliott Hughesce310da2015-07-29 08:44:17 -0700129#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800130 if (OS_PATH_SEPARATOR != '/') {
131 // Input filename under cygwin most likely has / separators
132 // whereas the expected string uses \\ separators. Adjust
133 // them accordingly.
134 for (char *c = const_cast<char *>(p); *c; ++c) {
135 if (*c == '/') *c = OS_PATH_SEPARATOR;
136 }
137 }
138#endif
139
Yabin Cui482eefb2014-11-10 15:01:43 -0800140 // aidl assumes case-insensitivity on Mac Os and Windows.
141#if defined(__linux__)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800142 valid = (expected == p);
143#else
144 valid = !strcasecmp(expected.c_str(), p);
145#endif
146 }
147
148 if (!valid) {
Steven Moreland92c55f12018-07-31 14:08:37 -0700149 AIDL_ERROR(defined_type) << name << " should be declared in a file called " << expected;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800150 }
151
Casey Dahlin42727f82015-10-12 19:23:40 -0700152 return valid;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800153}
154
Jeongik Cha37179582018-11-13 14:30:52 +0900155bool register_types(const AidlStructuredParcelable* parcel, TypeNamespace* types) {
Jeongik Cha17f7fd42018-10-31 11:27:59 +0900156 for (const auto& v : parcel->GetFields()) {
Jeongik Cha37179582018-11-13 14:30:52 +0900157 if (!types->MaybeAddContainerType(v->GetType())) {
158 return false;
159 }
Jeongik Chae51a5ea2018-11-01 06:36:09 +0000160
161 const ValidatableType* type = types->GetReturnType(v->GetType(), *parcel);
Jeongik Cha37179582018-11-13 14:30:52 +0900162 if (type == nullptr) {
163 return false;
164 }
Jeongik Chae51a5ea2018-11-01 06:36:09 +0000165 v->GetMutableType()->SetLanguageType(type);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700166 }
Jeongik Cha37179582018-11-13 14:30:52 +0900167 return true;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700168}
169
Jeongik Cha37179582018-11-13 14:30:52 +0900170bool register_types(const AidlInterface* c, TypeNamespace* types) {
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700171 for (const auto& m : c->GetMethods()) {
Jeongik Cha37179582018-11-13 14:30:52 +0900172 if (!types->MaybeAddContainerType(m->GetType())) {
173 return false;
174 }
Jeongik Chae51a5ea2018-11-01 06:36:09 +0000175
176 const ValidatableType* return_type = types->GetReturnType(m->GetType(), *c);
177
Jeongik Cha37179582018-11-13 14:30:52 +0900178 if (return_type == nullptr) {
179 return false;
180 }
Jeongik Chae51a5ea2018-11-01 06:36:09 +0000181 m->GetMutableType()->SetLanguageType(return_type);
182
Steven Morelandb3cd3c72018-10-11 12:37:45 -0700183 set<string> argument_names;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900184
Jeongik Chae51a5ea2018-11-01 06:36:09 +0000185 int index = 1;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700186 for (const auto& arg : m->GetArguments()) {
Jeongik Cha37179582018-11-13 14:30:52 +0900187 if (!types->MaybeAddContainerType(arg->GetType())) {
188 return false;
189 }
Jeongik Chae51a5ea2018-11-01 06:36:09 +0000190
191 const ValidatableType* arg_type = types->GetArgType(*arg, index, *c);
Jeongik Cha37179582018-11-13 14:30:52 +0900192 if (arg_type == nullptr) {
193 return false;
194 }
Jeongik Chae51a5ea2018-11-01 06:36:09 +0000195 arg->GetMutableType()->SetLanguageType(arg_type);
Jiyong Park309668e2018-07-28 16:55:44 +0900196 }
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700197 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700198
199 for (const std::unique_ptr<AidlConstantDeclaration>& constant : c->GetConstantDeclarations()) {
200 AidlTypeSpecifier* specifier = constant->GetMutableType();
201 const ValidatableType* return_type = types->GetReturnType(*specifier, *c);
Jeongik Cha37179582018-11-13 14:30:52 +0900202 if (return_type == nullptr) {
203 return false;
204 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700205 specifier->SetLanguageType(return_type);
206 }
Jeongik Cha37179582018-11-13 14:30:52 +0900207
208 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800209}
210
Jiyong Park74595c12018-07-23 15:22:50 +0900211bool write_dep_file(const Options& options, const AidlDefinedType& defined_type,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900212 const vector<string>& imports, const IoDelegate& io_delegate,
Jiyong Park74595c12018-07-23 15:22:50 +0900213 const string& input_file, const string& output_file) {
214 string dep_file_name = options.DependencyFile();
215 if (dep_file_name.empty() && options.AutoDepFile()) {
216 dep_file_name = output_file + ".d";
217 }
218
219 if (dep_file_name.empty()) {
220 return true; // nothing to do
221 }
Jiyong Park05463732018-08-09 16:03:02 +0900222
Jiyong Park74595c12018-07-23 15:22:50 +0900223 CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name);
224 if (!writer) {
225 LOG(ERROR) << "Could not open dependency file: " << dep_file_name;
226 return false;
227 }
228
229 vector<string> source_aidl = {input_file};
230 for (const auto& import : imports) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900231 source_aidl.push_back(import);
Jiyong Park74595c12018-07-23 15:22:50 +0900232 }
233
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800234 // Encode that the output file depends on aidl input files.
235 writer->Write("%s : \\\n", output_file.c_str());
Jiyong Park74595c12018-07-23 15:22:50 +0900236 writer->Write(" %s", Join(source_aidl, " \\\n ").c_str());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800237 writer->Write("\n");
Christopher Wileya30a45e2015-10-17 10:56:59 -0700238
Jiyong Park74595c12018-07-23 15:22:50 +0900239 if (!options.DependencyFileNinja()) {
Dan Willemsen93298ee2016-11-10 23:55:55 -0800240 writer->Write("\n");
241 // Output "<input_aidl_file>: " so make won't fail if the input .aidl file
242 // has been deleted, moved or renamed in incremental build.
Jiyong Park74595c12018-07-23 15:22:50 +0900243 for (const auto& src : source_aidl) {
Dan Willemsen93298ee2016-11-10 23:55:55 -0800244 writer->Write("%s :\n", src.c_str());
245 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800246 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700247
Steven Morelandc26d8142018-09-17 14:25:33 -0700248 if (options.IsCppOutput()) {
Jiyong Park74595c12018-07-23 15:22:50 +0900249 if (!options.DependencyFileNinja()) {
250 using ::android::aidl::cpp::ClassNames;
251 using ::android::aidl::cpp::HeaderFile;
252 vector<string> headers;
253 for (ClassNames c : {ClassNames::CLIENT, ClassNames::SERVER, ClassNames::INTERFACE}) {
Jiyong Park05463732018-08-09 16:03:02 +0900254 headers.push_back(options.OutputHeaderDir() +
Jiyong Park74595c12018-07-23 15:22:50 +0900255 HeaderFile(defined_type, c, false /* use_os_sep */));
256 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800257
Jiyong Park74595c12018-07-23 15:22:50 +0900258 writer->Write("\n");
259
260 // Generated headers also depend on the source aidl files.
261 writer->Write("%s : \\\n %s\n", Join(headers, " \\\n ").c_str(),
262 Join(source_aidl, " \\\n ").c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800263 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700264 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800265
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800266 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800267}
268
Jiyong Park74595c12018-07-23 15:22:50 +0900269string generate_outputFileName(const Options& options, const AidlDefinedType& defined_type) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700270 // create the path to the destination folder based on the
271 // defined_type package name
Jiyong Park74595c12018-07-23 15:22:50 +0900272 string result = options.OutputDir();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800273
Jiyong Park74595c12018-07-23 15:22:50 +0900274 string package = defined_type.GetPackage();
275 size_t len = package.length();
Steven Moreland5557f1c2018-07-02 13:50:23 -0700276 for (size_t i = 0; i < len; i++) {
Jiyong Park74595c12018-07-23 15:22:50 +0900277 if (package[i] == '.') {
278 package[i] = OS_PATH_SEPARATOR;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800279 }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700280 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800281
Jiyong Park74595c12018-07-23 15:22:50 +0900282 result += package;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800283
Steven Moreland5557f1c2018-07-02 13:50:23 -0700284 // add the filename by replacing the .aidl extension to .java
Jiyong Park74595c12018-07-23 15:22:50 +0900285 const string& name = defined_type.GetName();
Steven Moreland5557f1c2018-07-02 13:50:23 -0700286 result += OS_PATH_SEPARATOR;
287 result.append(name, 0, name.find('.'));
Jiyong Parkb03551f2018-08-06 19:20:51 +0900288 if (options.TargetLanguage() == Options::Language::JAVA) {
289 result += ".java";
Steven Morelandc26d8142018-09-17 14:25:33 -0700290 } else if (options.IsCppOutput()) {
Jiyong Parkb03551f2018-08-06 19:20:51 +0900291 result += ".cpp";
292 } else {
293 LOG(FATAL) << "Should not reach here" << endl;
294 return "";
295 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800296
Steven Moreland5557f1c2018-07-02 13:50:23 -0700297 return result;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800298}
299
Jiyong Parkb034bf02018-07-30 17:44:33 +0900300bool check_and_assign_method_ids(const std::vector<std::unique_ptr<AidlMethod>>& items) {
Jiyong Park309668e2018-07-28 16:55:44 +0900301 // Check whether there are any methods with manually assigned id's and any
302 // that are not. Either all method id's must be manually assigned or all of
303 // them must not. Also, check for uplicates of user set ID's and that the
304 // ID's are within the proper bounds.
305 set<int> usedIds;
306 bool hasUnassignedIds = false;
307 bool hasAssignedIds = false;
308 for (const auto& item : items) {
309 // However, meta transactions that are added by the AIDL compiler are
310 // exceptions. They have fixed IDs but allowed to be with user-defined
311 // methods having auto-assigned IDs. This is because the Ids of the meta
312 // transactions must be stable during the entire lifetime of an interface.
313 // In other words, their IDs must be the same even when new user-defined
314 // methods are added.
315 if (item->HasId() && item->IsUserDefined()) {
316 hasAssignedIds = true;
317 // Ensure that the user set id is not duplicated.
318 if (usedIds.find(item->GetId()) != usedIds.end()) {
319 // We found a duplicate id, so throw an error.
320 AIDL_ERROR(item) << "Found duplicate method id (" << item->GetId() << ") for method "
321 << item->GetName();
322 return false;
323 }
324 // Ensure that the user set id is within the appropriate limits
325 if (item->GetId() < kMinUserSetMethodId || item->GetId() > kMaxUserSetMethodId) {
326 AIDL_ERROR(item) << "Found out of bounds id (" << item->GetId() << ") for method "
327 << item->GetName() << ". Value for id must be between "
328 << kMinUserSetMethodId << " and " << kMaxUserSetMethodId << " inclusive.";
329 return false;
330 }
331 usedIds.insert(item->GetId());
332 } else {
333 hasUnassignedIds = true;
334 }
335 if (hasAssignedIds && hasUnassignedIds) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900336 AIDL_ERROR(item) << "You must either assign id's to all methods or to none of them.";
Jiyong Park309668e2018-07-28 16:55:44 +0900337 return false;
338 }
339 }
340
341 // In the case that all methods have unassigned id's, set a unique id for them.
342 if (hasUnassignedIds) {
343 int newId = kMinUserSetMethodId;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700344 for (const auto& item : items) {
Jiyong Park309668e2018-07-28 16:55:44 +0900345 assert(newId <= kMaxUserSetMethoId);
346 if (item->IsUserDefined()) {
347 item->SetId(newId++);
Jiyong Park309668e2018-07-28 16:55:44 +0900348 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800349 }
Jiyong Park309668e2018-07-28 16:55:44 +0900350 }
351 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800352}
353
Christopher Wileyef140932015-11-03 09:29:19 -0800354// TODO: Remove this in favor of using the YACC parser b/25479378
355bool ParsePreprocessedLine(const string& line, string* decl,
356 vector<string>* package, string* class_name) {
357 // erase all trailing whitespace and semicolons
358 const size_t end = line.find_last_not_of(" ;\t");
359 if (end == string::npos) {
360 return false;
361 }
362 if (line.rfind(';', end) != string::npos) {
363 return false;
364 }
365
366 decl->clear();
367 string type;
368 vector<string> pieces = Split(line.substr(0, end + 1), " \t");
369 for (const string& piece : pieces) {
370 if (piece.empty()) {
371 continue;
372 }
373 if (decl->empty()) {
374 *decl = std::move(piece);
375 } else if (type.empty()) {
376 type = std::move(piece);
377 } else {
378 return false;
379 }
380 }
381
382 // Note that this logic is absolutely wrong. Given a parcelable
383 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
384 // the class is just Bar. However, this was the way it was done in the past.
385 //
386 // See b/17415692
387 size_t dot_pos = type.rfind('.');
388 if (dot_pos != string::npos) {
389 *class_name = type.substr(dot_pos + 1);
390 *package = Split(type.substr(0, dot_pos), ".");
391 } else {
392 *class_name = type;
393 package->clear();
394 }
395
396 return true;
397}
398
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700399} // namespace
400
401namespace internals {
402
Jiyong Park1deecc32018-07-17 01:14:41 +0900403bool parse_preprocessed_file(const IoDelegate& io_delegate, const string& filename,
404 TypeNamespace* types, AidlTypenames& typenames) {
Christopher Wileyef140932015-11-03 09:29:19 -0800405 bool success = true;
406 unique_ptr<LineReader> line_reader = io_delegate.GetLineReader(filename);
407 if (!line_reader) {
408 LOG(ERROR) << "cannot open preprocessed file: " << filename;
409 success = false;
410 return success;
411 }
412
413 string line;
414 unsigned lineno = 1;
415 for ( ; line_reader->ReadLine(&line); ++lineno) {
416 if (line.empty() || line.compare(0, 2, "//") == 0) {
417 // skip comments and empty lines
418 continue;
419 }
420
421 string decl;
422 vector<string> package;
423 string class_name;
424 if (!ParsePreprocessedLine(line, &decl, &package, &class_name)) {
425 success = false;
426 break;
427 }
428
Steven Moreland46e9da82018-07-27 15:45:29 -0700429 AidlLocation::Point point = {.line = lineno, .column = 0 /*column*/};
430 AidlLocation location = AidlLocation(filename, point, point);
431
Christopher Wileyef140932015-11-03 09:29:19 -0800432 if (decl == "parcelable") {
Jiyong Parka6605ab2018-11-11 14:30:21 +0900433 AidlParcelable* doc = new AidlParcelable(
434 location, new AidlQualifiedName(location, class_name, ""), package, "" /* comments */);
Jiyong Park1deecc32018-07-17 01:14:41 +0900435 types->AddParcelableType(*doc, filename);
436 typenames.AddPreprocessedType(unique_ptr<AidlParcelable>(doc));
Steven Morelanded83a282018-07-17 13:27:29 -0700437 } else if (decl == "structured_parcelable") {
438 auto temp = new std::vector<std::unique_ptr<AidlVariableDeclaration>>();
Jiyong Parka6605ab2018-11-11 14:30:21 +0900439 AidlStructuredParcelable* doc =
440 new AidlStructuredParcelable(location, new AidlQualifiedName(location, class_name, ""),
441 package, "" /* comments */, temp);
Jiyong Park1deecc32018-07-17 01:14:41 +0900442 types->AddParcelableType(*doc, filename);
443 typenames.AddPreprocessedType(unique_ptr<AidlStructuredParcelable>(doc));
Christopher Wileyef140932015-11-03 09:29:19 -0800444 } else if (decl == "interface") {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800445 auto temp = new std::vector<std::unique_ptr<AidlMember>>();
Steven Moreland46e9da82018-07-27 15:45:29 -0700446 AidlInterface* doc = new AidlInterface(location, class_name, "", false, temp, package);
Jiyong Park1deecc32018-07-17 01:14:41 +0900447 types->AddBinderType(*doc, filename);
448 typenames.AddPreprocessedType(unique_ptr<AidlInterface>(doc));
Christopher Wileyef140932015-11-03 09:29:19 -0800449 } else {
450 success = false;
451 break;
452 }
453 }
454 if (!success) {
455 LOG(ERROR) << filename << ':' << lineno
456 << " malformed preprocessed file line: '" << line << "'";
457 }
458
459 return success;
460}
461
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900462AidlError load_and_validate_aidl(const std::string& input_file_name, const Options& options,
463 const IoDelegate& io_delegate, TypeNamespace* types,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900464 vector<AidlDefinedType*>* defined_types,
465 vector<string>* imported_files) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800466 AidlError err = AidlError::OK;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800467
Jiyong Parkb034bf02018-07-30 17:44:33 +0900468 //////////////////////////////////////////////////////////////////////////
469 // Loading phase
470 //////////////////////////////////////////////////////////////////////////
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900471
Jiyong Parkb034bf02018-07-30 17:44:33 +0900472 // Parse the main input file
Steven Moreland64e29be2018-08-08 18:52:19 -0700473 std::unique_ptr<Parser> main_parser =
474 Parser::Parse(input_file_name, io_delegate, types->typenames_);
475 if (main_parser == nullptr) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900476 return AidlError::PARSE_ERROR;
477 }
Steven Moreland64e29be2018-08-08 18:52:19 -0700478 if (!types->AddDefinedTypes(main_parser->GetDefinedTypes(), input_file_name)) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900479 return AidlError::BAD_TYPE;
480 }
481
482 // Import the preprocessed file
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900483 for (const string& s : options.PreprocessedFiles()) {
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900484 if (!parse_preprocessed_file(io_delegate, s, types, types->typenames_)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800485 err = AidlError::BAD_PRE_PROCESSED_FILE;
Christopher Wileyef140932015-11-03 09:29:19 -0800486 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700487 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800488 if (err != AidlError::OK) {
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700489 return err;
490 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800491
Jiyong Parkb034bf02018-07-30 17:44:33 +0900492 // Find files to import and parse them
Jiyong Parke59c3682018-09-11 23:10:25 +0900493 vector<string> import_paths;
Jiyong Park8c380532018-08-30 14:55:26 +0900494 ImportResolver import_resolver{io_delegate, input_file_name, options.ImportDirs(),
495 options.InputFiles()};
Jiyong Parke59c3682018-09-11 23:10:25 +0900496
497 set<string> type_from_import_statements;
Steven Moreland64e29be2018-08-08 18:52:19 -0700498 for (const auto& import : main_parser->GetImports()) {
Jiyong Parke05195e2018-10-08 18:24:23 +0900499 if (!AidlTypenames::IsBuiltinTypename(import->GetNeededClass())) {
500 type_from_import_statements.emplace(import->GetNeededClass());
501 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900502 }
503
504 // When referencing a type using fully qualified name it should be imported
505 // without the import statement. To support that, add all unresolved
506 // typespecs encountered during the parsing to the import_candidates list.
507 // Note that there is no guarantee that the typespecs are all fully qualified.
508 // It will be determined by calling FindImportFile().
509 set<string> unresolved_types;
510 for (const auto type : main_parser->GetUnresolvedTypespecs()) {
511 if (!AidlTypenames::IsBuiltinTypename(type->GetName())) {
512 unresolved_types.emplace(type->GetName());
513 }
514 }
515 set<string> import_candidates(type_from_import_statements);
516 import_candidates.insert(unresolved_types.begin(), unresolved_types.end());
517 for (const auto& import : import_candidates) {
518 if (types->HasImportType(import)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700519 // There are places in the Android tree where an import doesn't resolve,
520 // but we'll pick the type up through the preprocessed types.
521 // This seems like an error, but legacy support demands we support it...
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700522 continue;
523 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900524 string import_path = import_resolver.FindImportFile(import);
Christopher Wiley72877ac2015-10-06 14:41:42 -0700525 if (import_path.empty()) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900526 if (type_from_import_statements.find(import) != type_from_import_statements.end()) {
527 // Complain only when the import from the import statement has failed.
528 AIDL_ERROR(import) << "couldn't find import for class " << import;
529 err = AidlError::BAD_IMPORT;
530 }
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700531 continue;
532 }
Casey Dahlin2cc93162015-10-02 16:14:17 -0700533
Jiyong Parke59c3682018-09-11 23:10:25 +0900534 import_paths.emplace_back(import_path);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900535
Steven Moreland64e29be2018-08-08 18:52:19 -0700536 std::unique_ptr<Parser> import_parser =
537 Parser::Parse(import_path, io_delegate, types->typenames_);
538 if (import_parser == nullptr) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900539 cerr << "error while importing " << import_path << " for " << import << endl;
Christopher Wiley632801d2015-11-05 14:15:49 -0800540 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700541 continue;
542 }
Steven Moreland64e29be2018-08-08 18:52:19 -0700543 if (!types->AddDefinedTypes(import_parser->GetDefinedTypes(), import_path)) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900544 return AidlError::BAD_TYPE;
545 }
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700546 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800547 if (err != AidlError::OK) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700548 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700549 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800550
Jiyong Park3c35e392018-08-30 13:10:30 +0900551 for (const auto& imported_file : options.ImportFiles()) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900552 import_paths.emplace_back(imported_file);
Jiyong Park3c35e392018-08-30 13:10:30 +0900553
554 std::unique_ptr<Parser> import_parser =
555 Parser::Parse(imported_file, io_delegate, types->typenames_);
556 if (import_parser == nullptr) {
557 AIDL_ERROR(imported_file) << "error while importing " << imported_file;
558 err = AidlError::BAD_IMPORT;
559 continue;
560 }
561 if (!types->AddDefinedTypes(import_parser->GetDefinedTypes(), imported_file)) {
562 return AidlError::BAD_TYPE;
563 }
564 }
565 if (err != AidlError::OK) {
566 return err;
567 }
Jiyong Park96c16a92018-08-16 16:37:09 +0900568 const bool is_check_api = options.GetTask() == Options::Task::CHECK_API;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900569
570 // Resolve the unresolved type references found from the input file
Jiyong Park96c16a92018-08-16 16:37:09 +0900571 if (!is_check_api && !main_parser->Resolve()) {
572 // Resolution is not need for check api because all typespecs are
573 // using fully qualified names.
Jiyong Park1deecc32018-07-17 01:14:41 +0900574 return AidlError::BAD_TYPE;
575 }
Jeongik Cha37179582018-11-13 14:30:52 +0900576 if (!is_check_api) {
577 for (const auto defined_type : main_parser->GetDefinedTypes()) {
578 AidlInterface* interface = defined_type->AsInterface();
579 AidlStructuredParcelable* parcelable = defined_type->AsStructuredParcelable();
Jiyong Park1deecc32018-07-17 01:14:41 +0900580
Jeongik Cha37179582018-11-13 14:30:52 +0900581 // Link the AIDL type with the type of the target language. This will
582 // be removed when the migration to AidlTypenames is done.
583 defined_type->SetLanguageType(types->GetDefinedType(*defined_type));
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900584
Jeongik Cha37179582018-11-13 14:30:52 +0900585 if (interface != nullptr) {
586 if (!register_types(interface, types)) {
587 return AidlError::BAD_TYPE;
588 }
589 }
590 if (parcelable != nullptr) {
591 if (!register_types(parcelable, types)) {
592 return AidlError::BAD_TYPE;
593 }
594 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900595 }
596 }
597
598 //////////////////////////////////////////////////////////////////////////
599 // Validation phase
600 //////////////////////////////////////////////////////////////////////////
601
602 AidlTypenames& typenames = types->typenames_;
603
Steven Morelande2c64b42018-09-18 15:06:37 -0700604 // For legacy reasons, by default, compiling an unstructured parcelable (which contains no output)
605 // is allowed. This must not be returned as an error until the very end of this procedure since
606 // this may be considered a success, and we should first check that there are not other, more
607 // serious failures.
608 bool contains_unstructured_parcelable = false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800609
Steven Moreland64e29be2018-08-08 18:52:19 -0700610 const int num_defined_types = main_parser->GetDefinedTypes().size();
611 for (const auto defined_type : main_parser->GetDefinedTypes()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900612 CHECK(defined_type != nullptr);
Steven Morelande2c64b42018-09-18 15:06:37 -0700613 AidlParcelable* unstructuredParcelable = defined_type->AsUnstructuredParcelable();
614 if (unstructuredParcelable != nullptr) {
615 AIDL_ERROR(unstructuredParcelable)
616 << "Refusing to generate code with unstructured parcelables. Declared parcelables should "
617 "be in their own file and/or cannot be used with --structured interfaces.";
618 contains_unstructured_parcelable = true;
619 continue;
620 }
621
Jiyong Parkb034bf02018-07-30 17:44:33 +0900622 // Ensure that a type is either an interface or a structured parcelable
623 AidlInterface* interface = defined_type->AsInterface();
624 AidlStructuredParcelable* parcelable = defined_type->AsStructuredParcelable();
625 CHECK(interface != nullptr || parcelable != nullptr);
626
627 // Ensure that foo.bar.IFoo is defined in <some_path>/foo/bar/IFoo.aidl
Jiyong Parke59c3682018-09-11 23:10:25 +0900628 if (num_defined_types == 1 && !check_filename(input_file_name, *defined_type)) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900629 return AidlError::BAD_PACKAGE;
630 }
631
Jiyong Parkb034bf02018-07-30 17:44:33 +0900632 // Check the referenced types in parsed_doc to make sure we've imported them
Jiyong Park96c16a92018-08-16 16:37:09 +0900633 if (!is_check_api) {
634 // No need to do this for check api because all typespecs are already
635 // using fully qualified name and we don't import in AIDL files.
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900636 if (!defined_type->CheckValid(typenames)) {
Jiyong Park96c16a92018-08-16 16:37:09 +0900637 return AidlError::BAD_TYPE;
638 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900639 }
640
641 if (interface != nullptr) {
642 // add the meta-method 'int getInterfaceVersion()' if version is specified.
643 if (options.Version() > 0) {
644 AidlTypeSpecifier* ret =
Steven Moreland02e012e2018-08-02 14:58:10 -0700645 new AidlTypeSpecifier(AIDL_LOCATION_HERE, "int", false, nullptr, "");
Jiyong Park640981b2018-11-19 19:54:58 +0900646 ret->Resolve(typenames);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900647 vector<unique_ptr<AidlArgument>>* args = new vector<unique_ptr<AidlArgument>>();
648 AidlMethod* method =
Steven Moreland02e012e2018-08-02 14:58:10 -0700649 new AidlMethod(AIDL_LOCATION_HERE, false, ret, "getInterfaceVersion", args, "",
Jiyong Parkb034bf02018-07-30 17:44:33 +0900650 kGetInterfaceVersionId, false /* is_user_defined */);
651 interface->GetMutableMethods().emplace_back(method);
652 }
653 if (!check_and_assign_method_ids(interface->GetMethods())) {
654 return AidlError::BAD_METHOD_ID;
655 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900656 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800657 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800658
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900659 if (options.IsStructured()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900660 typenames.IterateTypes([&](const AidlDefinedType& type) {
Steven Moreland6cee3482018-07-18 14:39:58 -0700661 if (type.AsUnstructuredParcelable() != nullptr) {
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700662 err = AidlError::NOT_STRUCTURED;
Steven Moreland6cee3482018-07-18 14:39:58 -0700663 LOG(ERROR) << type.GetCanonicalName()
664 << " is not structured, but this is a structured interface.";
665 }
666 });
667 }
668 if (err != AidlError::OK) {
669 return err;
670 }
671
Jiyong Parkb034bf02018-07-30 17:44:33 +0900672 if (defined_types != nullptr) {
Steven Moreland64e29be2018-08-08 18:52:19 -0700673 *defined_types = main_parser->GetDefinedTypes();
Jiyong Park309668e2018-07-28 16:55:44 +0900674 }
675
Jiyong Parkb034bf02018-07-30 17:44:33 +0900676 if (imported_files != nullptr) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900677 *imported_files = import_paths;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700678 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700679
Steven Morelande2c64b42018-09-18 15:06:37 -0700680 if (contains_unstructured_parcelable) {
681 // Considered a success for the legacy case, so this must be returned last.
682 return AidlError::FOUND_PARCELABLE;
683 }
684
Christopher Wiley632801d2015-11-05 14:15:49 -0800685 return AidlError::OK;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700686}
687
Casey Dahlin2cc93162015-10-02 16:14:17 -0700688} // namespace internals
689
Jiyong Parkb034bf02018-07-30 17:44:33 +0900690int compile_aidl(const Options& options, const IoDelegate& io_delegate) {
691 const Options::Language lang = options.TargetLanguage();
Jiyong Park74595c12018-07-23 15:22:50 +0900692 for (const string& input_file : options.InputFiles()) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900693 // Create type namespace that will hold the types identified by the parser.
694 // This two namespaces that are specific to the target language will be
695 // unified to AidlTypenames which is agnostic to the target language.
696 cpp::TypeNamespace cpp_types;
697 cpp_types.Init();
698
699 java::JavaTypeNamespace java_types;
700 java_types.Init();
701
702 TypeNamespace* types;
Steven Morelandc26d8142018-09-17 14:25:33 -0700703 if (options.IsCppOutput()) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900704 types = &cpp_types;
705 } else if (lang == Options::Language::JAVA) {
706 types = &java_types;
707 } else {
708 LOG(FATAL) << "Unsupported target language." << endl;
Jiyong Park74595c12018-07-23 15:22:50 +0900709 return 1;
710 }
711
Jiyong Parkb034bf02018-07-30 17:44:33 +0900712 vector<AidlDefinedType*> defined_types;
713 vector<string> imported_files;
Jiyong Park74595c12018-07-23 15:22:50 +0900714
Jiyong Parkb034bf02018-07-30 17:44:33 +0900715 AidlError aidl_err = internals::load_and_validate_aidl(input_file, options, io_delegate, types,
716 &defined_types, &imported_files);
Jiyong Park74595c12018-07-23 15:22:50 +0900717 if (aidl_err == AidlError::FOUND_PARCELABLE && !options.FailOnParcelable()) {
718 // We aborted code generation because this file contains parcelables.
719 // However, we were not told to complain if we find parcelables.
720 // Just generate a dep file and exit quietly. The dep file is for a legacy
721 // use case by the SDK.
Jiyong Parkb034bf02018-07-30 17:44:33 +0900722 for (const auto defined_type : defined_types) {
723 write_dep_file(options, *defined_type, imported_files, io_delegate, input_file, "");
724 }
Jiyong Park74595c12018-07-23 15:22:50 +0900725 return 0;
726 }
727 if (aidl_err != AidlError::OK) {
728 return 1;
729 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700730
Jiyong Parkb034bf02018-07-30 17:44:33 +0900731 for (const auto defined_type : defined_types) {
732 CHECK(defined_type != nullptr);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700733
Jiyong Parkb034bf02018-07-30 17:44:33 +0900734 string output_file_name = options.OutputFile();
735 // if needed, generate the output file name from the base folder
736 if (output_file_name.empty() && !options.OutputDir().empty()) {
Jiyong Parkb03551f2018-08-06 19:20:51 +0900737 output_file_name = generate_outputFileName(options, *defined_type);
738 if (output_file_name.empty()) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900739 return 1;
740 }
741 }
Jiyong Park74595c12018-07-23 15:22:50 +0900742
Jiyong Parkb034bf02018-07-30 17:44:33 +0900743 if (!write_dep_file(options, *defined_type, imported_files, io_delegate, input_file,
744 output_file_name)) {
745 return 1;
746 }
Jiyong Park74595c12018-07-23 15:22:50 +0900747
Jiyong Parkb034bf02018-07-30 17:44:33 +0900748 bool success = false;
749 if (lang == Options::Language::CPP) {
750 success =
751 cpp::GenerateCpp(output_file_name, options, cpp_types, *defined_type, io_delegate);
Steven Morelandc26d8142018-09-17 14:25:33 -0700752 } else if (lang == Options::Language::NDK) {
753 ndk::GenerateNdk(output_file_name, options, cpp_types.typenames_, *defined_type,
754 io_delegate);
755 success = true;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900756 } else if (lang == Options::Language::JAVA) {
757 success = java::generate_java(output_file_name, input_file, defined_type, &java_types,
758 io_delegate, options);
759 } else {
760 LOG(FATAL) << "Should not reach here" << endl;
761 return 1;
762 }
763 if (!success) {
764 return 1;
765 }
Jiyong Park74595c12018-07-23 15:22:50 +0900766 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700767 }
Jiyong Park74595c12018-07-23 15:22:50 +0900768 return 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800769}
770
Jiyong Park74595c12018-07-23 15:22:50 +0900771bool preprocess_aidl(const Options& options, const IoDelegate& io_delegate) {
772 unique_ptr<CodeWriter> writer = io_delegate.GetCodeWriter(options.OutputFile());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800773
Jiyong Park74595c12018-07-23 15:22:50 +0900774 for (const auto& file : options.InputFiles()) {
Jiyong Park1deecc32018-07-17 01:14:41 +0900775 AidlTypenames typenames;
Steven Moreland64e29be2018-08-08 18:52:19 -0700776 std::unique_ptr<Parser> p = Parser::Parse(file, io_delegate, typenames);
777 if (p == nullptr) return false;
Casey Dahlin59401da2015-10-09 18:16:45 -0700778
Steven Moreland64e29be2018-08-08 18:52:19 -0700779 for (const auto& defined_type : p->GetDefinedTypes()) {
Steven Morelanded83a282018-07-17 13:27:29 -0700780 if (!writer->Write("%s %s;\n", defined_type->GetPreprocessDeclarationName().c_str(),
Steven Morelandc258abc2018-07-10 14:03:38 -0700781 defined_type->GetCanonicalName().c_str())) {
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800782 return false;
783 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800784 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800785 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800786
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800787 return writer->Close();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800788}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700789
Jiyong Parke59c3682018-09-11 23:10:25 +0900790static string GetApiDumpPathFor(const AidlDefinedType& defined_type, const Options& options) {
791 string package_as_path = Join(Split(defined_type.GetPackage(), "."), OS_PATH_SEPARATOR);
792 CHECK(!options.OutputDir().empty() && options.OutputDir().back() == '/');
793 return options.OutputDir() + package_as_path + OS_PATH_SEPARATOR + defined_type.GetName() +
794 ".aidl";
795}
Jiyong Parkb034bf02018-07-30 17:44:33 +0900796
Jiyong Parke59c3682018-09-11 23:10:25 +0900797bool dump_api(const Options& options, const IoDelegate& io_delegate) {
Jiyong Park74595c12018-07-23 15:22:50 +0900798 for (const auto& file : options.InputFiles()) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900799 java::JavaTypeNamespace ns;
800 ns.Init();
Jiyong Parkb034bf02018-07-30 17:44:33 +0900801 vector<AidlDefinedType*> defined_types;
Jiyong Parke59c3682018-09-11 23:10:25 +0900802 if (internals::load_and_validate_aidl(file, options, io_delegate, &ns, &defined_types,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900803 nullptr) == AidlError::OK) {
804 for (const auto type : defined_types) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900805 unique_ptr<CodeWriter> writer =
806 io_delegate.GetCodeWriter(GetApiDumpPathFor(*type, options));
Steven Morelandec0531d2018-09-20 11:11:20 -0700807 if (!type->GetPackage().empty()) {
808 (*writer) << "package " << type->GetPackage() << ";\n";
809 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900810 type->Write(writer.get());
Jiyong Parkb034bf02018-07-30 17:44:33 +0900811 }
Jiyong Park02da7422018-07-16 16:00:26 +0900812 } else {
813 return false;
814 }
815 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900816 return true;
Jiyong Park02da7422018-07-16 16:00:26 +0900817}
818
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700819} // namespace android
820} // namespace aidl