blob: 7a64ca145c53e7f5b28f59d1e5c5d5969e3dd7a9 [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"
Andrei Onea8714b022019-02-01 18:55:54 +000041#include "generate_aidl_mappings.h"
Christopher Wileyeb1acc12015-09-16 11:25:13 -070042#include "generate_cpp.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070043#include "generate_java.h"
Steven Morelandc26d8142018-09-17 14:25:33 -070044#include "generate_ndk.h"
Christopher Wiley72877ac2015-10-06 14:41:42 -070045#include "import_resolver.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070046#include "logging.h"
47#include "options.h"
48#include "os.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::set;
59using std::string;
Christopher Wiley84c1eac2015-09-23 13:29:28 -070060using std::unique_ptr;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070061using std::vector;
Adam Lesinskiffa16862014-01-23 18:17:42 -080062
Christopher Wileyf690be52015-09-14 15:19:10 -070063namespace android {
64namespace aidl {
65namespace {
Adam Lesinskiffa16862014-01-23 18:17:42 -080066
Jiyong Park965c5b92018-11-21 13:37:15 +090067// Copied from android.is.IBinder.[FIRST|LAST]_CALL_TRANSACTION
68const int kFirstCallTransaction = 1;
69const int kLastCallTransaction = 0x00ffffff;
70
71// Following IDs are all offsets from kFirstCallTransaction
Jiyong Park309668e2018-07-28 16:55:44 +090072
73// IDs for meta transactions. Most of the meta transactions are implemented in
74// the framework side (Binder.java or Binder.cpp). But these are the ones that
75// are auto-implemented by the AIDL compiler.
Jiyong Park965c5b92018-11-21 13:37:15 +090076const int kFirstMetaMethodId = kLastCallTransaction - kFirstCallTransaction;
77const int kGetInterfaceVersionId = kFirstMetaMethodId;
78// Additional meta transactions implemented by AIDL should use
79// kFirstMetaMethodId -1, -2, ...and so on.
80
81// Reserve 100 IDs for meta methods, which is more than enough. If we don't reserve,
82// in the future, a newly added meta transaction ID will have a chance to
83// collide with the user-defined methods that were added in the past. So,
84// let's prevent users from using IDs in this range from the beginning.
85const int kLastMetaMethodId = kFirstMetaMethodId - 99;
86
87// Range of IDs that is allowed for user-defined methods.
88const int kMinUserSetMethodId = 0;
89const int kMaxUserSetMethodId = kLastMetaMethodId - 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -080090
Steven Moreland92c55f12018-07-31 14:08:37 -070091bool check_filename(const std::string& filename, const AidlDefinedType& defined_type) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080092 const char* p;
93 string expected;
94 string fn;
95 size_t len;
Adam Lesinskiffa16862014-01-23 18:17:42 -080096 bool valid = false;
97
Christopher Wileybc2df692016-06-02 16:27:26 -070098 if (!IoDelegate::GetAbsolutePath(filename, &fn)) {
99 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800100 }
101
Steven Moreland92c55f12018-07-31 14:08:37 -0700102 const std::string package = defined_type.GetPackage();
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700103 if (!package.empty()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800104 expected = package;
105 expected += '.';
106 }
107
108 len = expected.length();
109 for (size_t i=0; i<len; i++) {
110 if (expected[i] == '.') {
111 expected[i] = OS_PATH_SEPARATOR;
112 }
113 }
114
Steven Moreland92c55f12018-07-31 14:08:37 -0700115 const std::string name = defined_type.GetName();
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700116 expected.append(name, 0, name.find('.'));
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700117
Adam Lesinskiffa16862014-01-23 18:17:42 -0800118 expected += ".aidl";
119
120 len = fn.length();
121 valid = (len >= expected.length());
122
123 if (valid) {
124 p = fn.c_str() + (len - expected.length());
125
Elliott Hughesce310da2015-07-29 08:44:17 -0700126#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800127 if (OS_PATH_SEPARATOR != '/') {
128 // Input filename under cygwin most likely has / separators
129 // whereas the expected string uses \\ separators. Adjust
130 // them accordingly.
131 for (char *c = const_cast<char *>(p); *c; ++c) {
132 if (*c == '/') *c = OS_PATH_SEPARATOR;
133 }
134 }
135#endif
136
Yabin Cui482eefb2014-11-10 15:01:43 -0800137 // aidl assumes case-insensitivity on Mac Os and Windows.
138#if defined(__linux__)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800139 valid = (expected == p);
140#else
141 valid = !strcasecmp(expected.c_str(), p);
142#endif
143 }
144
145 if (!valid) {
Steven Moreland92c55f12018-07-31 14:08:37 -0700146 AIDL_ERROR(defined_type) << name << " should be declared in a file called " << expected;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800147 }
148
Casey Dahlin42727f82015-10-12 19:23:40 -0700149 return valid;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800150}
151
Jiyong Park74595c12018-07-23 15:22:50 +0900152bool write_dep_file(const Options& options, const AidlDefinedType& defined_type,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900153 const vector<string>& imports, const IoDelegate& io_delegate,
Jiyong Park74595c12018-07-23 15:22:50 +0900154 const string& input_file, const string& output_file) {
155 string dep_file_name = options.DependencyFile();
156 if (dep_file_name.empty() && options.AutoDepFile()) {
157 dep_file_name = output_file + ".d";
158 }
159
160 if (dep_file_name.empty()) {
161 return true; // nothing to do
162 }
Jiyong Park05463732018-08-09 16:03:02 +0900163
Jiyong Park74595c12018-07-23 15:22:50 +0900164 CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name);
165 if (!writer) {
166 LOG(ERROR) << "Could not open dependency file: " << dep_file_name;
167 return false;
168 }
169
170 vector<string> source_aidl = {input_file};
171 for (const auto& import : imports) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900172 source_aidl.push_back(import);
Jiyong Park74595c12018-07-23 15:22:50 +0900173 }
174
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800175 // Encode that the output file depends on aidl input files.
176 writer->Write("%s : \\\n", output_file.c_str());
Jiyong Park74595c12018-07-23 15:22:50 +0900177 writer->Write(" %s", Join(source_aidl, " \\\n ").c_str());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800178 writer->Write("\n");
Christopher Wileya30a45e2015-10-17 10:56:59 -0700179
Jiyong Park74595c12018-07-23 15:22:50 +0900180 if (!options.DependencyFileNinja()) {
Dan Willemsen93298ee2016-11-10 23:55:55 -0800181 writer->Write("\n");
182 // Output "<input_aidl_file>: " so make won't fail if the input .aidl file
183 // has been deleted, moved or renamed in incremental build.
Jiyong Park74595c12018-07-23 15:22:50 +0900184 for (const auto& src : source_aidl) {
Dan Willemsen93298ee2016-11-10 23:55:55 -0800185 writer->Write("%s :\n", src.c_str());
186 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800187 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700188
Steven Morelandc26d8142018-09-17 14:25:33 -0700189 if (options.IsCppOutput()) {
Jiyong Park74595c12018-07-23 15:22:50 +0900190 if (!options.DependencyFileNinja()) {
191 using ::android::aidl::cpp::ClassNames;
192 using ::android::aidl::cpp::HeaderFile;
193 vector<string> headers;
Jiyong Park5b7e5322019-04-03 20:05:01 +0900194 for (ClassNames c : {ClassNames::CLIENT, ClassNames::SERVER, ClassNames::RAW}) {
Jiyong Park05463732018-08-09 16:03:02 +0900195 headers.push_back(options.OutputHeaderDir() +
Jiyong Park74595c12018-07-23 15:22:50 +0900196 HeaderFile(defined_type, c, false /* use_os_sep */));
197 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800198
Jiyong Park74595c12018-07-23 15:22:50 +0900199 writer->Write("\n");
200
201 // Generated headers also depend on the source aidl files.
202 writer->Write("%s : \\\n %s\n", Join(headers, " \\\n ").c_str(),
203 Join(source_aidl, " \\\n ").c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800204 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700205 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800206
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800207 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800208}
209
Jiyong Park74595c12018-07-23 15:22:50 +0900210string generate_outputFileName(const Options& options, const AidlDefinedType& defined_type) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700211 // create the path to the destination folder based on the
212 // defined_type package name
Jiyong Park74595c12018-07-23 15:22:50 +0900213 string result = options.OutputDir();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800214
Jiyong Park74595c12018-07-23 15:22:50 +0900215 string package = defined_type.GetPackage();
216 size_t len = package.length();
Steven Moreland5557f1c2018-07-02 13:50:23 -0700217 for (size_t i = 0; i < len; i++) {
Jiyong Park74595c12018-07-23 15:22:50 +0900218 if (package[i] == '.') {
219 package[i] = OS_PATH_SEPARATOR;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800220 }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700221 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800222
Jiyong Park74595c12018-07-23 15:22:50 +0900223 result += package;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800224
Steven Moreland5557f1c2018-07-02 13:50:23 -0700225 // add the filename by replacing the .aidl extension to .java
Jiyong Park74595c12018-07-23 15:22:50 +0900226 const string& name = defined_type.GetName();
Steven Moreland5557f1c2018-07-02 13:50:23 -0700227 result += OS_PATH_SEPARATOR;
228 result.append(name, 0, name.find('.'));
Jiyong Parkb03551f2018-08-06 19:20:51 +0900229 if (options.TargetLanguage() == Options::Language::JAVA) {
230 result += ".java";
Steven Morelandc26d8142018-09-17 14:25:33 -0700231 } else if (options.IsCppOutput()) {
Jiyong Parkb03551f2018-08-06 19:20:51 +0900232 result += ".cpp";
233 } else {
234 LOG(FATAL) << "Should not reach here" << endl;
235 return "";
236 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800237
Steven Moreland5557f1c2018-07-02 13:50:23 -0700238 return result;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800239}
240
Jiyong Parkb034bf02018-07-30 17:44:33 +0900241bool check_and_assign_method_ids(const std::vector<std::unique_ptr<AidlMethod>>& items) {
Jiyong Park309668e2018-07-28 16:55:44 +0900242 // Check whether there are any methods with manually assigned id's and any
243 // that are not. Either all method id's must be manually assigned or all of
244 // them must not. Also, check for uplicates of user set ID's and that the
245 // ID's are within the proper bounds.
246 set<int> usedIds;
247 bool hasUnassignedIds = false;
248 bool hasAssignedIds = false;
Steven Morelandec6f4692019-08-13 10:03:24 -0700249 int newId = kMinUserSetMethodId;
Jiyong Park309668e2018-07-28 16:55:44 +0900250 for (const auto& item : items) {
251 // However, meta transactions that are added by the AIDL compiler are
252 // exceptions. They have fixed IDs but allowed to be with user-defined
253 // methods having auto-assigned IDs. This is because the Ids of the meta
254 // transactions must be stable during the entire lifetime of an interface.
255 // In other words, their IDs must be the same even when new user-defined
256 // methods are added.
Jiyong Park3633b722019-04-11 15:38:26 +0900257 if (!item->IsUserDefined()) {
258 continue;
259 }
260 if (item->HasId()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900261 hasAssignedIds = true;
Jiyong Park309668e2018-07-28 16:55:44 +0900262 } else {
Steven Morelandec6f4692019-08-13 10:03:24 -0700263 item->SetId(newId++);
Jiyong Park309668e2018-07-28 16:55:44 +0900264 hasUnassignedIds = true;
265 }
Steven Morelandec6f4692019-08-13 10:03:24 -0700266
Jiyong Park309668e2018-07-28 16:55:44 +0900267 if (hasAssignedIds && hasUnassignedIds) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900268 AIDL_ERROR(item) << "You must either assign id's to all methods or to none of them.";
Jiyong Park309668e2018-07-28 16:55:44 +0900269 return false;
270 }
Jiyong Park309668e2018-07-28 16:55:44 +0900271
Steven Morelandec6f4692019-08-13 10:03:24 -0700272 // Ensure that the user set id is not duplicated.
273 if (usedIds.find(item->GetId()) != usedIds.end()) {
274 // We found a duplicate id, so throw an error.
275 AIDL_ERROR(item) << "Found duplicate method id (" << item->GetId() << ") for method "
276 << item->GetName();
277 return false;
278 }
279 usedIds.insert(item->GetId());
280
281 // Ensure that the user set id is within the appropriate limits
282 if (item->GetId() < kMinUserSetMethodId || item->GetId() > kMaxUserSetMethodId) {
283 AIDL_ERROR(item) << "Found out of bounds id (" << item->GetId() << ") for method "
284 << item->GetName() << ". Value for id must be between "
285 << kMinUserSetMethodId << " and " << kMaxUserSetMethodId << " inclusive.";
286 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800287 }
Jiyong Park309668e2018-07-28 16:55:44 +0900288 }
Steven Morelandec6f4692019-08-13 10:03:24 -0700289
Jiyong Park309668e2018-07-28 16:55:44 +0900290 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800291}
292
Christopher Wileyef140932015-11-03 09:29:19 -0800293// TODO: Remove this in favor of using the YACC parser b/25479378
294bool ParsePreprocessedLine(const string& line, string* decl,
295 vector<string>* package, string* class_name) {
296 // erase all trailing whitespace and semicolons
297 const size_t end = line.find_last_not_of(" ;\t");
298 if (end == string::npos) {
299 return false;
300 }
301 if (line.rfind(';', end) != string::npos) {
302 return false;
303 }
304
305 decl->clear();
306 string type;
307 vector<string> pieces = Split(line.substr(0, end + 1), " \t");
308 for (const string& piece : pieces) {
309 if (piece.empty()) {
310 continue;
311 }
312 if (decl->empty()) {
313 *decl = std::move(piece);
314 } else if (type.empty()) {
315 type = std::move(piece);
316 } else {
317 return false;
318 }
319 }
320
321 // Note that this logic is absolutely wrong. Given a parcelable
322 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
323 // the class is just Bar. However, this was the way it was done in the past.
324 //
325 // See b/17415692
326 size_t dot_pos = type.rfind('.');
327 if (dot_pos != string::npos) {
328 *class_name = type.substr(dot_pos + 1);
329 *package = Split(type.substr(0, dot_pos), ".");
330 } else {
331 *class_name = type;
332 package->clear();
333 }
334
335 return true;
336}
337
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700338} // namespace
339
340namespace internals {
341
Jiyong Park1deecc32018-07-17 01:14:41 +0900342bool parse_preprocessed_file(const IoDelegate& io_delegate, const string& filename,
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900343 AidlTypenames* typenames) {
Christopher Wileyef140932015-11-03 09:29:19 -0800344 bool success = true;
345 unique_ptr<LineReader> line_reader = io_delegate.GetLineReader(filename);
346 if (!line_reader) {
347 LOG(ERROR) << "cannot open preprocessed file: " << filename;
348 success = false;
349 return success;
350 }
351
352 string line;
353 unsigned lineno = 1;
354 for ( ; line_reader->ReadLine(&line); ++lineno) {
355 if (line.empty() || line.compare(0, 2, "//") == 0) {
356 // skip comments and empty lines
357 continue;
358 }
359
360 string decl;
361 vector<string> package;
362 string class_name;
363 if (!ParsePreprocessedLine(line, &decl, &package, &class_name)) {
364 success = false;
365 break;
366 }
367
Steven Moreland46e9da82018-07-27 15:45:29 -0700368 AidlLocation::Point point = {.line = lineno, .column = 0 /*column*/};
369 AidlLocation location = AidlLocation(filename, point, point);
370
Christopher Wileyef140932015-11-03 09:29:19 -0800371 if (decl == "parcelable") {
Jeongik Chace58bc62019-04-22 13:30:39 +0900372 // ParcelFileDescriptor is treated as a built-in type, but it's also in the framework.aidl.
373 // So aidl should ignore built-in types in framework.aidl to prevent duplication.
374 // (b/130899491)
375 if (AidlTypenames::IsBuiltinTypename(class_name)) {
376 continue;
377 }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900378 AidlParcelable* doc = new AidlParcelable(
379 location, new AidlQualifiedName(location, class_name, ""), package, "" /* comments */);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900380 typenames->AddPreprocessedType(unique_ptr<AidlParcelable>(doc));
Steven Morelanded83a282018-07-17 13:27:29 -0700381 } else if (decl == "structured_parcelable") {
382 auto temp = new std::vector<std::unique_ptr<AidlVariableDeclaration>>();
Jiyong Parka6605ab2018-11-11 14:30:21 +0900383 AidlStructuredParcelable* doc =
384 new AidlStructuredParcelable(location, new AidlQualifiedName(location, class_name, ""),
385 package, "" /* comments */, temp);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900386 typenames->AddPreprocessedType(unique_ptr<AidlStructuredParcelable>(doc));
Christopher Wileyef140932015-11-03 09:29:19 -0800387 } else if (decl == "interface") {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800388 auto temp = new std::vector<std::unique_ptr<AidlMember>>();
Steven Moreland46e9da82018-07-27 15:45:29 -0700389 AidlInterface* doc = new AidlInterface(location, class_name, "", false, temp, package);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900390 typenames->AddPreprocessedType(unique_ptr<AidlInterface>(doc));
Christopher Wileyef140932015-11-03 09:29:19 -0800391 } else {
392 success = false;
393 break;
394 }
395 }
396 if (!success) {
397 LOG(ERROR) << filename << ':' << lineno
398 << " malformed preprocessed file line: '" << line << "'";
399 }
400
401 return success;
402}
403
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900404AidlError load_and_validate_aidl(const std::string& input_file_name, const Options& options,
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900405 const IoDelegate& io_delegate, AidlTypenames* typenames,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900406 vector<AidlDefinedType*>* defined_types,
407 vector<string>* imported_files) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800408 AidlError err = AidlError::OK;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800409
Jiyong Parkb034bf02018-07-30 17:44:33 +0900410 //////////////////////////////////////////////////////////////////////////
411 // Loading phase
412 //////////////////////////////////////////////////////////////////////////
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900413
Jiyong Parkb034bf02018-07-30 17:44:33 +0900414 // Parse the main input file
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900415 std::unique_ptr<Parser> main_parser = Parser::Parse(input_file_name, io_delegate, *typenames);
Steven Moreland64e29be2018-08-08 18:52:19 -0700416 if (main_parser == nullptr) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900417 return AidlError::PARSE_ERROR;
418 }
Jiyong Parkda8c6932019-08-12 19:56:08 +0900419 int num_interfaces_or_structured_parcelables = 0;
420 for (AidlDefinedType* type : main_parser->GetDefinedTypes()) {
421 if (type->AsInterface() != nullptr || type->AsStructuredParcelable() != nullptr) {
422 num_interfaces_or_structured_parcelables++;
423 }
424 }
425 if (num_interfaces_or_structured_parcelables > 1) {
Jeongik Cha0e426012019-07-29 15:57:02 +0900426 AIDL_ERROR(input_file_name) << "You must declare only one type per a file.";
427 return AidlError::BAD_TYPE;
428 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900429
430 // Import the preprocessed file
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900431 for (const string& s : options.PreprocessedFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900432 if (!parse_preprocessed_file(io_delegate, s, typenames)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800433 err = AidlError::BAD_PRE_PROCESSED_FILE;
Christopher Wileyef140932015-11-03 09:29:19 -0800434 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700435 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800436 if (err != AidlError::OK) {
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700437 return err;
438 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800439
Jiyong Parkb034bf02018-07-30 17:44:33 +0900440 // Find files to import and parse them
Jiyong Parke59c3682018-09-11 23:10:25 +0900441 vector<string> import_paths;
Jiyong Park8c380532018-08-30 14:55:26 +0900442 ImportResolver import_resolver{io_delegate, input_file_name, options.ImportDirs(),
443 options.InputFiles()};
Jiyong Parke59c3682018-09-11 23:10:25 +0900444
445 set<string> type_from_import_statements;
Steven Moreland64e29be2018-08-08 18:52:19 -0700446 for (const auto& import : main_parser->GetImports()) {
Jiyong Parke05195e2018-10-08 18:24:23 +0900447 if (!AidlTypenames::IsBuiltinTypename(import->GetNeededClass())) {
448 type_from_import_statements.emplace(import->GetNeededClass());
449 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900450 }
451
452 // When referencing a type using fully qualified name it should be imported
453 // without the import statement. To support that, add all unresolved
454 // typespecs encountered during the parsing to the import_candidates list.
455 // Note that there is no guarantee that the typespecs are all fully qualified.
456 // It will be determined by calling FindImportFile().
457 set<string> unresolved_types;
458 for (const auto type : main_parser->GetUnresolvedTypespecs()) {
459 if (!AidlTypenames::IsBuiltinTypename(type->GetName())) {
460 unresolved_types.emplace(type->GetName());
461 }
462 }
463 set<string> import_candidates(type_from_import_statements);
464 import_candidates.insert(unresolved_types.begin(), unresolved_types.end());
465 for (const auto& import : import_candidates) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900466 if (typenames->IsIgnorableImport(import)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700467 // There are places in the Android tree where an import doesn't resolve,
468 // but we'll pick the type up through the preprocessed types.
469 // This seems like an error, but legacy support demands we support it...
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700470 continue;
471 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900472 string import_path = import_resolver.FindImportFile(import);
Christopher Wiley72877ac2015-10-06 14:41:42 -0700473 if (import_path.empty()) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900474 if (type_from_import_statements.find(import) != type_from_import_statements.end()) {
475 // Complain only when the import from the import statement has failed.
476 AIDL_ERROR(import) << "couldn't find import for class " << import;
477 err = AidlError::BAD_IMPORT;
478 }
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700479 continue;
480 }
Casey Dahlin2cc93162015-10-02 16:14:17 -0700481
Jiyong Parke59c3682018-09-11 23:10:25 +0900482 import_paths.emplace_back(import_path);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900483
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900484 std::unique_ptr<Parser> import_parser = Parser::Parse(import_path, io_delegate, *typenames);
Steven Moreland64e29be2018-08-08 18:52:19 -0700485 if (import_parser == nullptr) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900486 cerr << "error while importing " << import_path << " for " << import << endl;
Christopher Wiley632801d2015-11-05 14:15:49 -0800487 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700488 continue;
489 }
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700490 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800491 if (err != AidlError::OK) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700492 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700493 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800494
Jiyong Park3c35e392018-08-30 13:10:30 +0900495 for (const auto& imported_file : options.ImportFiles()) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900496 import_paths.emplace_back(imported_file);
Jiyong Park3c35e392018-08-30 13:10:30 +0900497
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900498 std::unique_ptr<Parser> import_parser = Parser::Parse(imported_file, io_delegate, *typenames);
Jiyong Park3c35e392018-08-30 13:10:30 +0900499 if (import_parser == nullptr) {
500 AIDL_ERROR(imported_file) << "error while importing " << imported_file;
501 err = AidlError::BAD_IMPORT;
502 continue;
503 }
Jiyong Park3c35e392018-08-30 13:10:30 +0900504 }
505 if (err != AidlError::OK) {
506 return err;
507 }
Jiyong Park96c16a92018-08-16 16:37:09 +0900508 const bool is_check_api = options.GetTask() == Options::Task::CHECK_API;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900509
510 // Resolve the unresolved type references found from the input file
Jiyong Park96c16a92018-08-16 16:37:09 +0900511 if (!is_check_api && !main_parser->Resolve()) {
512 // Resolution is not need for check api because all typespecs are
513 // using fully qualified names.
Jiyong Park1deecc32018-07-17 01:14:41 +0900514 return AidlError::BAD_TYPE;
515 }
Daniel Norman85aed542019-08-21 12:01:14 -0700516
517 typenames->IterateTypes([&](const AidlDefinedType& type) {
518 AidlEnumDeclaration* enum_decl = const_cast<AidlEnumDeclaration*>(type.AsEnumDeclaration());
519 if (enum_decl != nullptr) {
520 // BackingType is filled in for all known enums, including imported enums,
521 // because other types that may use enums, such as Interface or
522 // StructuredParcelable, need to know the enum BackingType when
523 // generating code.
524 if (auto backing_type = enum_decl->BackingType(); backing_type != nullptr) {
525 enum_decl->SetBackingType(std::unique_ptr<const AidlTypeSpecifier>(backing_type));
526 } else {
527 // Default to byte type for enums.
528 enum_decl->SetBackingType(std::make_unique<const AidlTypeSpecifier>(
529 AIDL_LOCATION_HERE, "byte", false, nullptr, ""));
530 }
531
532 // TODO(b/139877950): Support autofilling enumerators, and ensure that
533 // autofilling does not cause any enumerators to have a value larger than
534 // allowed by the backing type.
535 }
536 });
537
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900538 //////////////////////////////////////////////////////////////////////////
539 // Validation phase
540 //////////////////////////////////////////////////////////////////////////
541
Steven Morelande2c64b42018-09-18 15:06:37 -0700542 // For legacy reasons, by default, compiling an unstructured parcelable (which contains no output)
543 // is allowed. This must not be returned as an error until the very end of this procedure since
544 // this may be considered a success, and we should first check that there are not other, more
545 // serious failures.
546 bool contains_unstructured_parcelable = false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800547
Steven Moreland64e29be2018-08-08 18:52:19 -0700548 const int num_defined_types = main_parser->GetDefinedTypes().size();
549 for (const auto defined_type : main_parser->GetDefinedTypes()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900550 CHECK(defined_type != nullptr);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900551
552 // Language specific validation
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900553 if (!defined_type->LanguageSpecificCheckValid(options.TargetLanguage())) {
554 return AidlError::BAD_TYPE;
555 }
556
Steven Morelande2c64b42018-09-18 15:06:37 -0700557 AidlParcelable* unstructuredParcelable = defined_type->AsUnstructuredParcelable();
558 if (unstructuredParcelable != nullptr) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900559 if (!unstructuredParcelable->CheckValid(*typenames)) {
Jeongik Cha82317dd2019-02-27 20:26:11 +0900560 return AidlError::BAD_TYPE;
561 }
562 bool isStable = unstructuredParcelable->IsStableParcelable();
563 if (options.IsStructured() && !isStable) {
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800564 AIDL_ERROR(unstructuredParcelable)
565 << "Cannot declared parcelable in a --structured interface. Parcelable must be defined "
566 "in AIDL directly.";
567 return AidlError::NOT_STRUCTURED;
568 }
569 if (options.FailOnParcelable()) {
570 AIDL_ERROR(unstructuredParcelable)
571 << "Refusing to generate code with unstructured parcelables. Declared parcelables "
572 "should be in their own file and/or cannot be used with --structured interfaces.";
573 // Continue parsing for more errors
574 }
575
Steven Morelande2c64b42018-09-18 15:06:37 -0700576 contains_unstructured_parcelable = true;
577 continue;
578 }
579
Steven Morelanda57d0a62019-07-30 09:41:14 -0700580 if (defined_type->IsVintfStability() &&
581 (options.GetStability() != Options::Stability::VINTF || !options.IsStructured())) {
582 AIDL_ERROR(defined_type)
583 << "Must compile @VintfStability type w/ aidl_interface 'stability: \"vintf\"'";
584 return AidlError::NOT_STRUCTURED;
585 }
586
Daniel Norman85aed542019-08-21 12:01:14 -0700587 // Ensure that a type is either an interface, structured parcelable, or
588 // enum.
Jiyong Parkb034bf02018-07-30 17:44:33 +0900589 AidlInterface* interface = defined_type->AsInterface();
590 AidlStructuredParcelable* parcelable = defined_type->AsStructuredParcelable();
Daniel Norman85aed542019-08-21 12:01:14 -0700591 AidlEnumDeclaration* enum_decl = defined_type->AsEnumDeclaration();
592 CHECK(!!interface + !!parcelable + !!enum_decl == 1);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900593
594 // Ensure that foo.bar.IFoo is defined in <some_path>/foo/bar/IFoo.aidl
Jiyong Parke59c3682018-09-11 23:10:25 +0900595 if (num_defined_types == 1 && !check_filename(input_file_name, *defined_type)) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900596 return AidlError::BAD_PACKAGE;
597 }
598
Jiyong Parkb034bf02018-07-30 17:44:33 +0900599 // Check the referenced types in parsed_doc to make sure we've imported them
Jiyong Park96c16a92018-08-16 16:37:09 +0900600 if (!is_check_api) {
601 // No need to do this for check api because all typespecs are already
602 // using fully qualified name and we don't import in AIDL files.
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900603 if (!defined_type->CheckValid(*typenames)) {
Jiyong Park96c16a92018-08-16 16:37:09 +0900604 return AidlError::BAD_TYPE;
605 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900606 }
607
608 if (interface != nullptr) {
609 // add the meta-method 'int getInterfaceVersion()' if version is specified.
610 if (options.Version() > 0) {
611 AidlTypeSpecifier* ret =
Steven Moreland02e012e2018-08-02 14:58:10 -0700612 new AidlTypeSpecifier(AIDL_LOCATION_HERE, "int", false, nullptr, "");
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900613 ret->Resolve(*typenames);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900614 vector<unique_ptr<AidlArgument>>* args = new vector<unique_ptr<AidlArgument>>();
615 AidlMethod* method =
Steven Moreland02e012e2018-08-02 14:58:10 -0700616 new AidlMethod(AIDL_LOCATION_HERE, false, ret, "getInterfaceVersion", args, "",
Jiyong Parkb034bf02018-07-30 17:44:33 +0900617 kGetInterfaceVersionId, false /* is_user_defined */);
618 interface->GetMutableMethods().emplace_back(method);
619 }
620 if (!check_and_assign_method_ids(interface->GetMethods())) {
621 return AidlError::BAD_METHOD_ID;
622 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900623 }
Daniel Norman85aed542019-08-21 12:01:14 -0700624
625 if (enum_decl != nullptr) {
626 if (!is_check_api && (options.TargetLanguage() == Options::Language::NDK ||
627 options.TargetLanguage() == Options::Language::JAVA)) {
628 AIDL_ERROR(defined_type) << "Enums are not yet supported in Java or NDK. "
629 << "Please set \"backend: { java: { enabled: false }, "
630 << "ndk: { enabled: false },},\" if you want to use Enums.";
631 return AidlError::BAD_TYPE;
632 }
633 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800634 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800635
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900636 typenames->IterateTypes([&](const AidlDefinedType& type) {
Steven Morelanda57d0a62019-07-30 09:41:14 -0700637 if (options.IsStructured() && type.AsUnstructuredParcelable() != nullptr &&
638 !type.AsUnstructuredParcelable()->IsStableParcelable()) {
639 err = AidlError::NOT_STRUCTURED;
640 LOG(ERROR) << type.GetCanonicalName()
641 << " is not structured, but this is a structured interface.";
642 }
643 if (options.GetStability() == Options::Stability::VINTF && !type.IsVintfStability()) {
644 err = AidlError::NOT_STRUCTURED;
645 LOG(ERROR) << type.GetCanonicalName()
646 << " does not have VINTF level stability, but this interface requires it.";
647 }
648 });
649
Steven Moreland6cee3482018-07-18 14:39:58 -0700650 if (err != AidlError::OK) {
651 return err;
652 }
653
Jiyong Parkb034bf02018-07-30 17:44:33 +0900654 if (defined_types != nullptr) {
Steven Moreland64e29be2018-08-08 18:52:19 -0700655 *defined_types = main_parser->GetDefinedTypes();
Jiyong Park309668e2018-07-28 16:55:44 +0900656 }
657
Jiyong Parkb034bf02018-07-30 17:44:33 +0900658 if (imported_files != nullptr) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900659 *imported_files = import_paths;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700660 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700661
Steven Morelande2c64b42018-09-18 15:06:37 -0700662 if (contains_unstructured_parcelable) {
663 // Considered a success for the legacy case, so this must be returned last.
664 return AidlError::FOUND_PARCELABLE;
665 }
666
Christopher Wiley632801d2015-11-05 14:15:49 -0800667 return AidlError::OK;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700668}
669
Casey Dahlin2cc93162015-10-02 16:14:17 -0700670} // namespace internals
671
Jiyong Parkb034bf02018-07-30 17:44:33 +0900672int compile_aidl(const Options& options, const IoDelegate& io_delegate) {
673 const Options::Language lang = options.TargetLanguage();
Jiyong Park74595c12018-07-23 15:22:50 +0900674 for (const string& input_file : options.InputFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900675 AidlTypenames typenames;
Jiyong Park74595c12018-07-23 15:22:50 +0900676
Jiyong Parkb034bf02018-07-30 17:44:33 +0900677 vector<AidlDefinedType*> defined_types;
678 vector<string> imported_files;
Jiyong Park74595c12018-07-23 15:22:50 +0900679
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900680 AidlError aidl_err = internals::load_and_validate_aidl(
681 input_file, options, io_delegate, &typenames, &defined_types, &imported_files);
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800682 bool allowError = aidl_err == AidlError::FOUND_PARCELABLE && !options.FailOnParcelable();
683 if (aidl_err != AidlError::OK && !allowError) {
Jiyong Park74595c12018-07-23 15:22:50 +0900684 return 1;
685 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700686
Jiyong Parkb034bf02018-07-30 17:44:33 +0900687 for (const auto defined_type : defined_types) {
688 CHECK(defined_type != nullptr);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700689
Jiyong Parkb034bf02018-07-30 17:44:33 +0900690 string output_file_name = options.OutputFile();
691 // if needed, generate the output file name from the base folder
692 if (output_file_name.empty() && !options.OutputDir().empty()) {
Jiyong Parkb03551f2018-08-06 19:20:51 +0900693 output_file_name = generate_outputFileName(options, *defined_type);
694 if (output_file_name.empty()) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900695 return 1;
696 }
697 }
Jiyong Park74595c12018-07-23 15:22:50 +0900698
Jiyong Parkb034bf02018-07-30 17:44:33 +0900699 if (!write_dep_file(options, *defined_type, imported_files, io_delegate, input_file,
700 output_file_name)) {
701 return 1;
702 }
Jiyong Park74595c12018-07-23 15:22:50 +0900703
Jiyong Parkb034bf02018-07-30 17:44:33 +0900704 bool success = false;
705 if (lang == Options::Language::CPP) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900706 success =
707 cpp::GenerateCpp(output_file_name, options, typenames, *defined_type, io_delegate);
Steven Morelandc26d8142018-09-17 14:25:33 -0700708 } else if (lang == Options::Language::NDK) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900709 ndk::GenerateNdk(output_file_name, options, typenames, *defined_type, io_delegate);
Steven Morelandc26d8142018-09-17 14:25:33 -0700710 success = true;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900711 } else if (lang == Options::Language::JAVA) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900712 success =
713 java::generate_java(output_file_name, defined_type, typenames, io_delegate, options);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900714 } else {
715 LOG(FATAL) << "Should not reach here" << endl;
716 return 1;
717 }
718 if (!success) {
719 return 1;
720 }
Jiyong Park74595c12018-07-23 15:22:50 +0900721 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700722 }
Jiyong Park74595c12018-07-23 15:22:50 +0900723 return 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800724}
725
Andrei Onea8714b022019-02-01 18:55:54 +0000726bool dump_mappings(const Options& options, const IoDelegate& io_delegate) {
727 android::aidl::mappings::SignatureMap all_mappings;
728 for (const string& input_file : options.InputFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900729 AidlTypenames typenames;
Andrei Onea8714b022019-02-01 18:55:54 +0000730 vector<AidlDefinedType*> defined_types;
731 vector<string> imported_files;
732
733 AidlError aidl_err = internals::load_and_validate_aidl(
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900734 input_file, options, io_delegate, &typenames, &defined_types, &imported_files);
Andrei Onea8714b022019-02-01 18:55:54 +0000735 if (aidl_err != AidlError::OK) {
736 LOG(WARNING) << "AIDL file is invalid.\n";
737 continue;
738 }
739 for (const auto defined_type : defined_types) {
740 auto mappings = mappings::generate_mappings(defined_type);
741 all_mappings.insert(mappings.begin(), mappings.end());
742 }
743 }
744 std::stringstream mappings_str;
745 for (const auto& mapping : all_mappings) {
746 mappings_str << mapping.first << "\n" << mapping.second << "\n";
747 }
748 auto code_writer = io_delegate.GetCodeWriter(options.OutputFile());
749 code_writer->Write("%s", mappings_str.str().c_str());
750 return true;
751}
752
Jiyong Park74595c12018-07-23 15:22:50 +0900753bool preprocess_aidl(const Options& options, const IoDelegate& io_delegate) {
754 unique_ptr<CodeWriter> writer = io_delegate.GetCodeWriter(options.OutputFile());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800755
Jiyong Park74595c12018-07-23 15:22:50 +0900756 for (const auto& file : options.InputFiles()) {
Jiyong Park1deecc32018-07-17 01:14:41 +0900757 AidlTypenames typenames;
Steven Moreland64e29be2018-08-08 18:52:19 -0700758 std::unique_ptr<Parser> p = Parser::Parse(file, io_delegate, typenames);
759 if (p == nullptr) return false;
Casey Dahlin59401da2015-10-09 18:16:45 -0700760
Steven Moreland64e29be2018-08-08 18:52:19 -0700761 for (const auto& defined_type : p->GetDefinedTypes()) {
Steven Morelanded83a282018-07-17 13:27:29 -0700762 if (!writer->Write("%s %s;\n", defined_type->GetPreprocessDeclarationName().c_str(),
Steven Morelandc258abc2018-07-10 14:03:38 -0700763 defined_type->GetCanonicalName().c_str())) {
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800764 return false;
765 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800766 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800767 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800768
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800769 return writer->Close();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800770}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700771
Jiyong Parke59c3682018-09-11 23:10:25 +0900772static string GetApiDumpPathFor(const AidlDefinedType& defined_type, const Options& options) {
773 string package_as_path = Join(Split(defined_type.GetPackage(), "."), OS_PATH_SEPARATOR);
774 CHECK(!options.OutputDir().empty() && options.OutputDir().back() == '/');
775 return options.OutputDir() + package_as_path + OS_PATH_SEPARATOR + defined_type.GetName() +
776 ".aidl";
777}
Jiyong Parkb034bf02018-07-30 17:44:33 +0900778
Jiyong Parke59c3682018-09-11 23:10:25 +0900779bool dump_api(const Options& options, const IoDelegate& io_delegate) {
Jiyong Park74595c12018-07-23 15:22:50 +0900780 for (const auto& file : options.InputFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900781 AidlTypenames typenames;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900782 vector<AidlDefinedType*> defined_types;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900783 if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames, &defined_types,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900784 nullptr) == AidlError::OK) {
785 for (const auto type : defined_types) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900786 unique_ptr<CodeWriter> writer =
787 io_delegate.GetCodeWriter(GetApiDumpPathFor(*type, options));
Steven Morelandec0531d2018-09-20 11:11:20 -0700788 if (!type->GetPackage().empty()) {
789 (*writer) << "package " << type->GetPackage() << ";\n";
790 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900791 type->Write(writer.get());
Jiyong Parkb034bf02018-07-30 17:44:33 +0900792 }
Jiyong Park02da7422018-07-16 16:00:26 +0900793 } else {
794 return false;
795 }
796 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900797 return true;
Jiyong Park02da7422018-07-16 16:00:26 +0900798}
799
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700800} // namespace aidl
Steven Morelandf4c64df2019-07-29 19:54:04 -0700801} // namespace android