blob: 2c9bd2ec0bc24e8c46f5cc6848436d913c83c9e0 [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;
Paul Trautrimb77048c2020-01-21 16:39:32 +090078const int kGetInterfaceHashId = kFirstMetaMethodId - 1;
Jiyong Park965c5b92018-11-21 13:37:15 +090079// Additional meta transactions implemented by AIDL should use
80// kFirstMetaMethodId -1, -2, ...and so on.
81
82// Reserve 100 IDs for meta methods, which is more than enough. If we don't reserve,
83// in the future, a newly added meta transaction ID will have a chance to
84// collide with the user-defined methods that were added in the past. So,
85// let's prevent users from using IDs in this range from the beginning.
86const int kLastMetaMethodId = kFirstMetaMethodId - 99;
87
88// Range of IDs that is allowed for user-defined methods.
89const int kMinUserSetMethodId = 0;
90const int kMaxUserSetMethodId = kLastMetaMethodId - 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -080091
Steven Moreland92c55f12018-07-31 14:08:37 -070092bool check_filename(const std::string& filename, const AidlDefinedType& defined_type) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080093 const char* p;
94 string expected;
95 string fn;
96 size_t len;
Adam Lesinskiffa16862014-01-23 18:17:42 -080097 bool valid = false;
98
Christopher Wileybc2df692016-06-02 16:27:26 -070099 if (!IoDelegate::GetAbsolutePath(filename, &fn)) {
100 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800101 }
102
Steven Moreland92c55f12018-07-31 14:08:37 -0700103 const std::string package = defined_type.GetPackage();
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700104 if (!package.empty()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800105 expected = package;
106 expected += '.';
107 }
108
109 len = expected.length();
110 for (size_t i=0; i<len; i++) {
111 if (expected[i] == '.') {
112 expected[i] = OS_PATH_SEPARATOR;
113 }
114 }
115
Steven Moreland92c55f12018-07-31 14:08:37 -0700116 const std::string name = defined_type.GetName();
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700117 expected.append(name, 0, name.find('.'));
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700118
Adam Lesinskiffa16862014-01-23 18:17:42 -0800119 expected += ".aidl";
120
121 len = fn.length();
122 valid = (len >= expected.length());
123
124 if (valid) {
125 p = fn.c_str() + (len - expected.length());
126
Elliott Hughesce310da2015-07-29 08:44:17 -0700127#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800128 if (OS_PATH_SEPARATOR != '/') {
129 // Input filename under cygwin most likely has / separators
130 // whereas the expected string uses \\ separators. Adjust
131 // them accordingly.
132 for (char *c = const_cast<char *>(p); *c; ++c) {
133 if (*c == '/') *c = OS_PATH_SEPARATOR;
134 }
135 }
136#endif
137
Yabin Cui482eefb2014-11-10 15:01:43 -0800138 // aidl assumes case-insensitivity on Mac Os and Windows.
139#if defined(__linux__)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800140 valid = (expected == p);
141#else
142 valid = !strcasecmp(expected.c_str(), p);
143#endif
144 }
145
146 if (!valid) {
Steven Moreland92c55f12018-07-31 14:08:37 -0700147 AIDL_ERROR(defined_type) << name << " should be declared in a file called " << expected;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800148 }
149
Casey Dahlin42727f82015-10-12 19:23:40 -0700150 return valid;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800151}
152
Jiyong Park74595c12018-07-23 15:22:50 +0900153bool write_dep_file(const Options& options, const AidlDefinedType& defined_type,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900154 const vector<string>& imports, const IoDelegate& io_delegate,
Jiyong Park74595c12018-07-23 15:22:50 +0900155 const string& input_file, const string& output_file) {
156 string dep_file_name = options.DependencyFile();
157 if (dep_file_name.empty() && options.AutoDepFile()) {
158 dep_file_name = output_file + ".d";
159 }
160
161 if (dep_file_name.empty()) {
162 return true; // nothing to do
163 }
Jiyong Park05463732018-08-09 16:03:02 +0900164
Jiyong Park74595c12018-07-23 15:22:50 +0900165 CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name);
166 if (!writer) {
167 LOG(ERROR) << "Could not open dependency file: " << dep_file_name;
168 return false;
169 }
170
171 vector<string> source_aidl = {input_file};
172 for (const auto& import : imports) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900173 source_aidl.push_back(import);
Jiyong Park74595c12018-07-23 15:22:50 +0900174 }
175
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800176 // Encode that the output file depends on aidl input files.
Jiyong Parkdf202122019-09-30 20:48:35 +0900177 if (defined_type.AsUnstructuredParcelable() != nullptr &&
178 options.TargetLanguage() == Options::Language::JAVA) {
179 // Legacy behavior. For parcelable declarations in Java, don't emit output file as
180 // the dependency target. b/141372861
181 writer->Write(" : \\\n");
182 } else {
183 writer->Write("%s : \\\n", output_file.c_str());
184 }
Jiyong Park74595c12018-07-23 15:22:50 +0900185 writer->Write(" %s", Join(source_aidl, " \\\n ").c_str());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800186 writer->Write("\n");
Christopher Wileya30a45e2015-10-17 10:56:59 -0700187
Jiyong Park74595c12018-07-23 15:22:50 +0900188 if (!options.DependencyFileNinja()) {
Dan Willemsen93298ee2016-11-10 23:55:55 -0800189 writer->Write("\n");
190 // Output "<input_aidl_file>: " so make won't fail if the input .aidl file
191 // has been deleted, moved or renamed in incremental build.
Jiyong Park74595c12018-07-23 15:22:50 +0900192 for (const auto& src : source_aidl) {
Dan Willemsen93298ee2016-11-10 23:55:55 -0800193 writer->Write("%s :\n", src.c_str());
194 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800195 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700196
Steven Morelandc26d8142018-09-17 14:25:33 -0700197 if (options.IsCppOutput()) {
Jiyong Park74595c12018-07-23 15:22:50 +0900198 if (!options.DependencyFileNinja()) {
199 using ::android::aidl::cpp::ClassNames;
200 using ::android::aidl::cpp::HeaderFile;
201 vector<string> headers;
Jiyong Park5b7e5322019-04-03 20:05:01 +0900202 for (ClassNames c : {ClassNames::CLIENT, ClassNames::SERVER, ClassNames::RAW}) {
Jiyong Park05463732018-08-09 16:03:02 +0900203 headers.push_back(options.OutputHeaderDir() +
Jiyong Park74595c12018-07-23 15:22:50 +0900204 HeaderFile(defined_type, c, false /* use_os_sep */));
205 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800206
Jiyong Park74595c12018-07-23 15:22:50 +0900207 writer->Write("\n");
208
209 // Generated headers also depend on the source aidl files.
210 writer->Write("%s : \\\n %s\n", Join(headers, " \\\n ").c_str(),
211 Join(source_aidl, " \\\n ").c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800212 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700213 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800214
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800215 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800216}
217
Jiyong Park74595c12018-07-23 15:22:50 +0900218string generate_outputFileName(const Options& options, const AidlDefinedType& defined_type) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700219 // create the path to the destination folder based on the
220 // defined_type package name
Jiyong Park74595c12018-07-23 15:22:50 +0900221 string result = options.OutputDir();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800222
Jiyong Park74595c12018-07-23 15:22:50 +0900223 string package = defined_type.GetPackage();
224 size_t len = package.length();
Steven Moreland5557f1c2018-07-02 13:50:23 -0700225 for (size_t i = 0; i < len; i++) {
Jiyong Park74595c12018-07-23 15:22:50 +0900226 if (package[i] == '.') {
227 package[i] = OS_PATH_SEPARATOR;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800228 }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700229 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800230
Jiyong Park74595c12018-07-23 15:22:50 +0900231 result += package;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800232
Steven Moreland5557f1c2018-07-02 13:50:23 -0700233 // add the filename by replacing the .aidl extension to .java
Jiyong Park74595c12018-07-23 15:22:50 +0900234 const string& name = defined_type.GetName();
Steven Moreland5557f1c2018-07-02 13:50:23 -0700235 result += OS_PATH_SEPARATOR;
236 result.append(name, 0, name.find('.'));
Jiyong Parkb03551f2018-08-06 19:20:51 +0900237 if (options.TargetLanguage() == Options::Language::JAVA) {
238 result += ".java";
Steven Morelandc26d8142018-09-17 14:25:33 -0700239 } else if (options.IsCppOutput()) {
Jiyong Parkb03551f2018-08-06 19:20:51 +0900240 result += ".cpp";
241 } else {
242 LOG(FATAL) << "Should not reach here" << endl;
243 return "";
244 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800245
Steven Moreland5557f1c2018-07-02 13:50:23 -0700246 return result;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800247}
248
Jiyong Parkb034bf02018-07-30 17:44:33 +0900249bool check_and_assign_method_ids(const std::vector<std::unique_ptr<AidlMethod>>& items) {
Jiyong Park309668e2018-07-28 16:55:44 +0900250 // Check whether there are any methods with manually assigned id's and any
251 // that are not. Either all method id's must be manually assigned or all of
252 // them must not. Also, check for uplicates of user set ID's and that the
253 // ID's are within the proper bounds.
254 set<int> usedIds;
255 bool hasUnassignedIds = false;
256 bool hasAssignedIds = false;
Steven Morelandec6f4692019-08-13 10:03:24 -0700257 int newId = kMinUserSetMethodId;
Jiyong Park309668e2018-07-28 16:55:44 +0900258 for (const auto& item : items) {
259 // However, meta transactions that are added by the AIDL compiler are
260 // exceptions. They have fixed IDs but allowed to be with user-defined
261 // methods having auto-assigned IDs. This is because the Ids of the meta
262 // transactions must be stable during the entire lifetime of an interface.
263 // In other words, their IDs must be the same even when new user-defined
264 // methods are added.
Jiyong Park3633b722019-04-11 15:38:26 +0900265 if (!item->IsUserDefined()) {
266 continue;
267 }
268 if (item->HasId()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900269 hasAssignedIds = true;
Jiyong Park309668e2018-07-28 16:55:44 +0900270 } else {
Steven Morelandec6f4692019-08-13 10:03:24 -0700271 item->SetId(newId++);
Jiyong Park309668e2018-07-28 16:55:44 +0900272 hasUnassignedIds = true;
273 }
Steven Morelandec6f4692019-08-13 10:03:24 -0700274
Jiyong Park309668e2018-07-28 16:55:44 +0900275 if (hasAssignedIds && hasUnassignedIds) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900276 AIDL_ERROR(item) << "You must either assign id's to all methods or to none of them.";
Jiyong Park309668e2018-07-28 16:55:44 +0900277 return false;
278 }
Jiyong Park309668e2018-07-28 16:55:44 +0900279
Steven Morelandec6f4692019-08-13 10:03:24 -0700280 // Ensure that the user set id is not duplicated.
281 if (usedIds.find(item->GetId()) != usedIds.end()) {
282 // We found a duplicate id, so throw an error.
283 AIDL_ERROR(item) << "Found duplicate method id (" << item->GetId() << ") for method "
284 << item->GetName();
285 return false;
286 }
287 usedIds.insert(item->GetId());
288
289 // Ensure that the user set id is within the appropriate limits
290 if (item->GetId() < kMinUserSetMethodId || item->GetId() > kMaxUserSetMethodId) {
291 AIDL_ERROR(item) << "Found out of bounds id (" << item->GetId() << ") for method "
292 << item->GetName() << ". Value for id must be between "
293 << kMinUserSetMethodId << " and " << kMaxUserSetMethodId << " inclusive.";
294 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800295 }
Jiyong Park309668e2018-07-28 16:55:44 +0900296 }
Steven Morelandec6f4692019-08-13 10:03:24 -0700297
Jiyong Park309668e2018-07-28 16:55:44 +0900298 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800299}
300
Christopher Wileyef140932015-11-03 09:29:19 -0800301// TODO: Remove this in favor of using the YACC parser b/25479378
302bool ParsePreprocessedLine(const string& line, string* decl,
303 vector<string>* package, string* class_name) {
304 // erase all trailing whitespace and semicolons
305 const size_t end = line.find_last_not_of(" ;\t");
306 if (end == string::npos) {
307 return false;
308 }
309 if (line.rfind(';', end) != string::npos) {
310 return false;
311 }
312
313 decl->clear();
314 string type;
315 vector<string> pieces = Split(line.substr(0, end + 1), " \t");
316 for (const string& piece : pieces) {
317 if (piece.empty()) {
318 continue;
319 }
320 if (decl->empty()) {
321 *decl = std::move(piece);
322 } else if (type.empty()) {
323 type = std::move(piece);
324 } else {
325 return false;
326 }
327 }
328
329 // Note that this logic is absolutely wrong. Given a parcelable
330 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
331 // the class is just Bar. However, this was the way it was done in the past.
332 //
333 // See b/17415692
334 size_t dot_pos = type.rfind('.');
335 if (dot_pos != string::npos) {
336 *class_name = type.substr(dot_pos + 1);
337 *package = Split(type.substr(0, dot_pos), ".");
338 } else {
339 *class_name = type;
340 package->clear();
341 }
342
343 return true;
344}
345
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700346} // namespace
347
348namespace internals {
349
Jiyong Park1deecc32018-07-17 01:14:41 +0900350bool parse_preprocessed_file(const IoDelegate& io_delegate, const string& filename,
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900351 AidlTypenames* typenames) {
Christopher Wileyef140932015-11-03 09:29:19 -0800352 bool success = true;
353 unique_ptr<LineReader> line_reader = io_delegate.GetLineReader(filename);
354 if (!line_reader) {
355 LOG(ERROR) << "cannot open preprocessed file: " << filename;
356 success = false;
357 return success;
358 }
359
360 string line;
Dan Willemsen609ba6d2019-12-30 10:44:00 -0800361 int lineno = 1;
Christopher Wileyef140932015-11-03 09:29:19 -0800362 for ( ; line_reader->ReadLine(&line); ++lineno) {
363 if (line.empty() || line.compare(0, 2, "//") == 0) {
364 // skip comments and empty lines
365 continue;
366 }
367
368 string decl;
369 vector<string> package;
370 string class_name;
371 if (!ParsePreprocessedLine(line, &decl, &package, &class_name)) {
372 success = false;
373 break;
374 }
375
Steven Moreland46e9da82018-07-27 15:45:29 -0700376 AidlLocation::Point point = {.line = lineno, .column = 0 /*column*/};
377 AidlLocation location = AidlLocation(filename, point, point);
378
Christopher Wileyef140932015-11-03 09:29:19 -0800379 if (decl == "parcelable") {
Jeongik Chace58bc62019-04-22 13:30:39 +0900380 // ParcelFileDescriptor is treated as a built-in type, but it's also in the framework.aidl.
381 // So aidl should ignore built-in types in framework.aidl to prevent duplication.
382 // (b/130899491)
383 if (AidlTypenames::IsBuiltinTypename(class_name)) {
384 continue;
385 }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900386 AidlParcelable* doc = new AidlParcelable(
387 location, new AidlQualifiedName(location, class_name, ""), package, "" /* comments */);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900388 typenames->AddPreprocessedType(unique_ptr<AidlParcelable>(doc));
Steven Morelanded83a282018-07-17 13:27:29 -0700389 } else if (decl == "structured_parcelable") {
390 auto temp = new std::vector<std::unique_ptr<AidlVariableDeclaration>>();
Jiyong Parka6605ab2018-11-11 14:30:21 +0900391 AidlStructuredParcelable* doc =
392 new AidlStructuredParcelable(location, new AidlQualifiedName(location, class_name, ""),
393 package, "" /* comments */, temp);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900394 typenames->AddPreprocessedType(unique_ptr<AidlStructuredParcelable>(doc));
Christopher Wileyef140932015-11-03 09:29:19 -0800395 } else if (decl == "interface") {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800396 auto temp = new std::vector<std::unique_ptr<AidlMember>>();
Steven Moreland46e9da82018-07-27 15:45:29 -0700397 AidlInterface* doc = new AidlInterface(location, class_name, "", false, temp, package);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900398 typenames->AddPreprocessedType(unique_ptr<AidlInterface>(doc));
Christopher Wileyef140932015-11-03 09:29:19 -0800399 } else {
400 success = false;
401 break;
402 }
403 }
404 if (!success) {
405 LOG(ERROR) << filename << ':' << lineno
406 << " malformed preprocessed file line: '" << line << "'";
407 }
408
409 return success;
410}
411
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900412AidlError load_and_validate_aidl(const std::string& input_file_name, const Options& options,
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900413 const IoDelegate& io_delegate, AidlTypenames* typenames,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900414 vector<AidlDefinedType*>* defined_types,
415 vector<string>* imported_files) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800416 AidlError err = AidlError::OK;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800417
Jiyong Parkb034bf02018-07-30 17:44:33 +0900418 //////////////////////////////////////////////////////////////////////////
419 // Loading phase
420 //////////////////////////////////////////////////////////////////////////
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900421
Jiyong Parkb034bf02018-07-30 17:44:33 +0900422 // Parse the main input file
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900423 std::unique_ptr<Parser> main_parser = Parser::Parse(input_file_name, io_delegate, *typenames);
Steven Moreland64e29be2018-08-08 18:52:19 -0700424 if (main_parser == nullptr) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900425 return AidlError::PARSE_ERROR;
426 }
Jiyong Parkda8c6932019-08-12 19:56:08 +0900427 int num_interfaces_or_structured_parcelables = 0;
428 for (AidlDefinedType* type : main_parser->GetDefinedTypes()) {
429 if (type->AsInterface() != nullptr || type->AsStructuredParcelable() != nullptr) {
430 num_interfaces_or_structured_parcelables++;
431 }
432 }
433 if (num_interfaces_or_structured_parcelables > 1) {
Jeongik Cha0e426012019-07-29 15:57:02 +0900434 AIDL_ERROR(input_file_name) << "You must declare only one type per a file.";
435 return AidlError::BAD_TYPE;
436 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900437
438 // Import the preprocessed file
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900439 for (const string& s : options.PreprocessedFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900440 if (!parse_preprocessed_file(io_delegate, s, typenames)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800441 err = AidlError::BAD_PRE_PROCESSED_FILE;
Christopher Wileyef140932015-11-03 09:29:19 -0800442 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700443 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800444 if (err != AidlError::OK) {
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700445 return err;
446 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800447
Jiyong Parkb034bf02018-07-30 17:44:33 +0900448 // Find files to import and parse them
Jiyong Parke59c3682018-09-11 23:10:25 +0900449 vector<string> import_paths;
Jiyong Park8c380532018-08-30 14:55:26 +0900450 ImportResolver import_resolver{io_delegate, input_file_name, options.ImportDirs(),
451 options.InputFiles()};
Jiyong Parke59c3682018-09-11 23:10:25 +0900452
Jiyong Park8f6ec462020-01-19 20:52:47 +0900453 vector<string> type_from_import_statements;
Steven Moreland64e29be2018-08-08 18:52:19 -0700454 for (const auto& import : main_parser->GetImports()) {
Jiyong Parke05195e2018-10-08 18:24:23 +0900455 if (!AidlTypenames::IsBuiltinTypename(import->GetNeededClass())) {
Jiyong Park8f6ec462020-01-19 20:52:47 +0900456 type_from_import_statements.emplace_back(import->GetNeededClass());
Jiyong Parke05195e2018-10-08 18:24:23 +0900457 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900458 }
459
460 // When referencing a type using fully qualified name it should be imported
461 // without the import statement. To support that, add all unresolved
462 // typespecs encountered during the parsing to the import_candidates list.
463 // Note that there is no guarantee that the typespecs are all fully qualified.
464 // It will be determined by calling FindImportFile().
465 set<string> unresolved_types;
466 for (const auto type : main_parser->GetUnresolvedTypespecs()) {
467 if (!AidlTypenames::IsBuiltinTypename(type->GetName())) {
468 unresolved_types.emplace(type->GetName());
469 }
470 }
Jiyong Park8f6ec462020-01-19 20:52:47 +0900471 vector<string> import_candidates(type_from_import_statements);
472 import_candidates.insert(import_candidates.end(), unresolved_types.begin(),
473 unresolved_types.end());
Jiyong Parke59c3682018-09-11 23:10:25 +0900474 for (const auto& import : import_candidates) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900475 if (typenames->IsIgnorableImport(import)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700476 // There are places in the Android tree where an import doesn't resolve,
477 // but we'll pick the type up through the preprocessed types.
478 // This seems like an error, but legacy support demands we support it...
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700479 continue;
480 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900481 string import_path = import_resolver.FindImportFile(import);
Christopher Wiley72877ac2015-10-06 14:41:42 -0700482 if (import_path.empty()) {
Jiyong Park8f6ec462020-01-19 20:52:47 +0900483 if (typenames->ResolveTypename(import).second) {
484 // Couldn't find the *.aidl file for the type from the include paths, but we
485 // have the type already resolved. This could happen when the type is
486 // from the preprocessed aidl file. In that case, use the type from the
487 // preprocessed aidl file as a last resort.
488 continue;
489 }
490
491 if (std::find(type_from_import_statements.begin(), type_from_import_statements.end(),
492 import) != type_from_import_statements.end()) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900493 // Complain only when the import from the import statement has failed.
494 AIDL_ERROR(import) << "couldn't find import for class " << import;
495 err = AidlError::BAD_IMPORT;
496 }
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700497 continue;
498 }
Casey Dahlin2cc93162015-10-02 16:14:17 -0700499
Jiyong Parke59c3682018-09-11 23:10:25 +0900500 import_paths.emplace_back(import_path);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900501
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900502 std::unique_ptr<Parser> import_parser = Parser::Parse(import_path, io_delegate, *typenames);
Steven Moreland64e29be2018-08-08 18:52:19 -0700503 if (import_parser == nullptr) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900504 cerr << "error while importing " << import_path << " for " << import << endl;
Christopher Wiley632801d2015-11-05 14:15:49 -0800505 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700506 continue;
507 }
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700508 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800509 if (err != AidlError::OK) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700510 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700511 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800512
Jiyong Park3c35e392018-08-30 13:10:30 +0900513 for (const auto& imported_file : options.ImportFiles()) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900514 import_paths.emplace_back(imported_file);
Jiyong Park3c35e392018-08-30 13:10:30 +0900515
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900516 std::unique_ptr<Parser> import_parser = Parser::Parse(imported_file, io_delegate, *typenames);
Jiyong Park3c35e392018-08-30 13:10:30 +0900517 if (import_parser == nullptr) {
518 AIDL_ERROR(imported_file) << "error while importing " << imported_file;
519 err = AidlError::BAD_IMPORT;
520 continue;
521 }
Jiyong Park3c35e392018-08-30 13:10:30 +0900522 }
523 if (err != AidlError::OK) {
524 return err;
525 }
Jiyong Park96c16a92018-08-16 16:37:09 +0900526 const bool is_check_api = options.GetTask() == Options::Task::CHECK_API;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900527
528 // Resolve the unresolved type references found from the input file
Jiyong Park96c16a92018-08-16 16:37:09 +0900529 if (!is_check_api && !main_parser->Resolve()) {
530 // Resolution is not need for check api because all typespecs are
531 // using fully qualified names.
Jiyong Park1deecc32018-07-17 01:14:41 +0900532 return AidlError::BAD_TYPE;
533 }
Daniel Norman85aed542019-08-21 12:01:14 -0700534
535 typenames->IterateTypes([&](const AidlDefinedType& type) {
536 AidlEnumDeclaration* enum_decl = const_cast<AidlEnumDeclaration*>(type.AsEnumDeclaration());
537 if (enum_decl != nullptr) {
538 // BackingType is filled in for all known enums, including imported enums,
539 // because other types that may use enums, such as Interface or
540 // StructuredParcelable, need to know the enum BackingType when
541 // generating code.
Daniel Norman716d3112019-09-10 13:11:56 -0700542 if (auto backing_type = enum_decl->BackingType(*typenames); backing_type != nullptr) {
Daniel Norman85aed542019-08-21 12:01:14 -0700543 enum_decl->SetBackingType(std::unique_ptr<const AidlTypeSpecifier>(backing_type));
544 } else {
545 // Default to byte type for enums.
Daniel Norman716d3112019-09-10 13:11:56 -0700546 auto byte_type =
547 std::make_unique<AidlTypeSpecifier>(AIDL_LOCATION_HERE, "byte", false, nullptr, "");
548 byte_type->Resolve(*typenames);
549 enum_decl->SetBackingType(std::move(byte_type));
Daniel Norman85aed542019-08-21 12:01:14 -0700550 }
551
Steven Moreland59e53e42019-11-26 20:38:08 -0800552 if (!enum_decl->Autofill()) {
553 err = AidlError::BAD_TYPE;
554 }
Daniel Norman85aed542019-08-21 12:01:14 -0700555 }
556 });
Steven Moreland59e53e42019-11-26 20:38:08 -0800557 if (err != AidlError::OK) {
558 return err;
559 }
Daniel Norman85aed542019-08-21 12:01:14 -0700560
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900561 //////////////////////////////////////////////////////////////////////////
562 // Validation phase
563 //////////////////////////////////////////////////////////////////////////
564
Steven Morelande2c64b42018-09-18 15:06:37 -0700565 // For legacy reasons, by default, compiling an unstructured parcelable (which contains no output)
566 // is allowed. This must not be returned as an error until the very end of this procedure since
567 // this may be considered a success, and we should first check that there are not other, more
568 // serious failures.
569 bool contains_unstructured_parcelable = false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800570
Steven Moreland64e29be2018-08-08 18:52:19 -0700571 const int num_defined_types = main_parser->GetDefinedTypes().size();
572 for (const auto defined_type : main_parser->GetDefinedTypes()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900573 CHECK(defined_type != nullptr);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900574
575 // Language specific validation
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900576 if (!defined_type->LanguageSpecificCheckValid(options.TargetLanguage())) {
577 return AidlError::BAD_TYPE;
578 }
579
Steven Morelande2c64b42018-09-18 15:06:37 -0700580 AidlParcelable* unstructuredParcelable = defined_type->AsUnstructuredParcelable();
581 if (unstructuredParcelable != nullptr) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900582 if (!unstructuredParcelable->CheckValid(*typenames)) {
Jeongik Cha82317dd2019-02-27 20:26:11 +0900583 return AidlError::BAD_TYPE;
584 }
Jeongik Cha88f95a82020-01-15 13:02:16 +0900585 bool isStable = unstructuredParcelable->IsStableApiParcelable(options.TargetLanguage());
Jeongik Cha82317dd2019-02-27 20:26:11 +0900586 if (options.IsStructured() && !isStable) {
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800587 AIDL_ERROR(unstructuredParcelable)
588 << "Cannot declared parcelable in a --structured interface. Parcelable must be defined "
589 "in AIDL directly.";
590 return AidlError::NOT_STRUCTURED;
591 }
592 if (options.FailOnParcelable()) {
593 AIDL_ERROR(unstructuredParcelable)
594 << "Refusing to generate code with unstructured parcelables. Declared parcelables "
595 "should be in their own file and/or cannot be used with --structured interfaces.";
596 // Continue parsing for more errors
597 }
598
Steven Morelande2c64b42018-09-18 15:06:37 -0700599 contains_unstructured_parcelable = true;
600 continue;
601 }
602
Steven Morelanda57d0a62019-07-30 09:41:14 -0700603 if (defined_type->IsVintfStability() &&
604 (options.GetStability() != Options::Stability::VINTF || !options.IsStructured())) {
605 AIDL_ERROR(defined_type)
606 << "Must compile @VintfStability type w/ aidl_interface 'stability: \"vintf\"'";
607 return AidlError::NOT_STRUCTURED;
608 }
609
Daniel Norman85aed542019-08-21 12:01:14 -0700610 // Ensure that a type is either an interface, structured parcelable, or
611 // enum.
Jiyong Parkb034bf02018-07-30 17:44:33 +0900612 AidlInterface* interface = defined_type->AsInterface();
613 AidlStructuredParcelable* parcelable = defined_type->AsStructuredParcelable();
Daniel Norman85aed542019-08-21 12:01:14 -0700614 AidlEnumDeclaration* enum_decl = defined_type->AsEnumDeclaration();
615 CHECK(!!interface + !!parcelable + !!enum_decl == 1);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900616
617 // Ensure that foo.bar.IFoo is defined in <some_path>/foo/bar/IFoo.aidl
Jiyong Parke59c3682018-09-11 23:10:25 +0900618 if (num_defined_types == 1 && !check_filename(input_file_name, *defined_type)) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900619 return AidlError::BAD_PACKAGE;
620 }
621
Jiyong Parkb034bf02018-07-30 17:44:33 +0900622 // Check the referenced types in parsed_doc to make sure we've imported them
Jiyong Park96c16a92018-08-16 16:37:09 +0900623 if (!is_check_api) {
624 // No need to do this for check api because all typespecs are already
625 // using fully qualified name and we don't import in AIDL files.
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900626 if (!defined_type->CheckValid(*typenames)) {
Jiyong Park96c16a92018-08-16 16:37:09 +0900627 return AidlError::BAD_TYPE;
628 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900629 }
630
631 if (interface != nullptr) {
632 // add the meta-method 'int getInterfaceVersion()' if version is specified.
633 if (options.Version() > 0) {
634 AidlTypeSpecifier* ret =
Steven Moreland02e012e2018-08-02 14:58:10 -0700635 new AidlTypeSpecifier(AIDL_LOCATION_HERE, "int", false, nullptr, "");
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900636 ret->Resolve(*typenames);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900637 vector<unique_ptr<AidlArgument>>* args = new vector<unique_ptr<AidlArgument>>();
638 AidlMethod* method =
Steven Moreland02e012e2018-08-02 14:58:10 -0700639 new AidlMethod(AIDL_LOCATION_HERE, false, ret, "getInterfaceVersion", args, "",
Jiyong Parkb034bf02018-07-30 17:44:33 +0900640 kGetInterfaceVersionId, false /* is_user_defined */);
641 interface->GetMutableMethods().emplace_back(method);
642 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900643 // add the meta-method 'string getInterfaceHash()' if hash is specified.
644 if (!options.Hash().empty()) {
645 AidlTypeSpecifier* ret =
646 new AidlTypeSpecifier(AIDL_LOCATION_HERE, "String", false, nullptr, "");
647 ret->Resolve(*typenames);
648 vector<unique_ptr<AidlArgument>>* args = new vector<unique_ptr<AidlArgument>>();
649 AidlMethod* method = new AidlMethod(AIDL_LOCATION_HERE, false, ret, kGetInterfaceHash, args,
650 "", kGetInterfaceHashId, false /* is_user_defined */);
651 interface->GetMutableMethods().emplace_back(method);
652 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900653 if (!check_and_assign_method_ids(interface->GetMethods())) {
654 return AidlError::BAD_METHOD_ID;
655 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700656
657 // Verify and resolve the constant declarations
658 for (const auto& constant : interface->GetConstantDeclarations()) {
659 switch (constant->GetValue().GetType()) {
660 case AidlConstantValue::Type::STRING: // fall-through
661 case AidlConstantValue::Type::INT8: // fall-through
662 case AidlConstantValue::Type::INT32: // fall-through
663 case AidlConstantValue::Type::INT64: // fall-through
664 case AidlConstantValue::Type::FLOATING: // fall-through
665 case AidlConstantValue::Type::UNARY: // fall-through
666 case AidlConstantValue::Type::BINARY: {
667 bool success = constant->CheckValid(*typenames);
668 if (!success) {
669 return AidlError::BAD_TYPE;
670 }
671 if (constant->ValueString(cpp::ConstantValueDecorator).empty()) {
672 return AidlError::BAD_TYPE;
673 }
674 break;
675 }
676 default:
677 LOG(FATAL) << "Unrecognized constant type: "
678 << static_cast<int>(constant->GetValue().GetType());
679 break;
680 }
681 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900682 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800683 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800684
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900685 typenames->IterateTypes([&](const AidlDefinedType& type) {
Steven Morelanda57d0a62019-07-30 09:41:14 -0700686 if (options.IsStructured() && type.AsUnstructuredParcelable() != nullptr &&
Jeongik Cha88f95a82020-01-15 13:02:16 +0900687 !type.AsUnstructuredParcelable()->IsStableApiParcelable(options.TargetLanguage())) {
Steven Morelanda57d0a62019-07-30 09:41:14 -0700688 err = AidlError::NOT_STRUCTURED;
689 LOG(ERROR) << type.GetCanonicalName()
690 << " is not structured, but this is a structured interface.";
691 }
692 if (options.GetStability() == Options::Stability::VINTF && !type.IsVintfStability()) {
693 err = AidlError::NOT_STRUCTURED;
694 LOG(ERROR) << type.GetCanonicalName()
695 << " does not have VINTF level stability, but this interface requires it.";
696 }
697 });
698
Steven Moreland6cee3482018-07-18 14:39:58 -0700699 if (err != AidlError::OK) {
700 return err;
701 }
702
Jiyong Parkb034bf02018-07-30 17:44:33 +0900703 if (defined_types != nullptr) {
Steven Moreland64e29be2018-08-08 18:52:19 -0700704 *defined_types = main_parser->GetDefinedTypes();
Jiyong Park309668e2018-07-28 16:55:44 +0900705 }
706
Jiyong Parkb034bf02018-07-30 17:44:33 +0900707 if (imported_files != nullptr) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900708 *imported_files = import_paths;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700709 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700710
Steven Morelande2c64b42018-09-18 15:06:37 -0700711 if (contains_unstructured_parcelable) {
712 // Considered a success for the legacy case, so this must be returned last.
713 return AidlError::FOUND_PARCELABLE;
714 }
715
Christopher Wiley632801d2015-11-05 14:15:49 -0800716 return AidlError::OK;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700717}
718
Casey Dahlin2cc93162015-10-02 16:14:17 -0700719} // namespace internals
720
Jiyong Parkb034bf02018-07-30 17:44:33 +0900721int compile_aidl(const Options& options, const IoDelegate& io_delegate) {
722 const Options::Language lang = options.TargetLanguage();
Jiyong Park74595c12018-07-23 15:22:50 +0900723 for (const string& input_file : options.InputFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900724 AidlTypenames typenames;
Jiyong Park74595c12018-07-23 15:22:50 +0900725
Jiyong Parkb034bf02018-07-30 17:44:33 +0900726 vector<AidlDefinedType*> defined_types;
727 vector<string> imported_files;
Jiyong Park74595c12018-07-23 15:22:50 +0900728
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900729 AidlError aidl_err = internals::load_and_validate_aidl(
730 input_file, options, io_delegate, &typenames, &defined_types, &imported_files);
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800731 bool allowError = aidl_err == AidlError::FOUND_PARCELABLE && !options.FailOnParcelable();
732 if (aidl_err != AidlError::OK && !allowError) {
Jiyong Park74595c12018-07-23 15:22:50 +0900733 return 1;
734 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700735
Jiyong Parkb034bf02018-07-30 17:44:33 +0900736 for (const auto defined_type : defined_types) {
737 CHECK(defined_type != nullptr);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700738
Jiyong Parkb034bf02018-07-30 17:44:33 +0900739 string output_file_name = options.OutputFile();
740 // if needed, generate the output file name from the base folder
741 if (output_file_name.empty() && !options.OutputDir().empty()) {
Jiyong Parkb03551f2018-08-06 19:20:51 +0900742 output_file_name = generate_outputFileName(options, *defined_type);
743 if (output_file_name.empty()) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900744 return 1;
745 }
746 }
Jiyong Park74595c12018-07-23 15:22:50 +0900747
Jiyong Parkb034bf02018-07-30 17:44:33 +0900748 if (!write_dep_file(options, *defined_type, imported_files, io_delegate, input_file,
749 output_file_name)) {
750 return 1;
751 }
Jiyong Park74595c12018-07-23 15:22:50 +0900752
Jiyong Parkb034bf02018-07-30 17:44:33 +0900753 bool success = false;
754 if (lang == Options::Language::CPP) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900755 success =
756 cpp::GenerateCpp(output_file_name, options, typenames, *defined_type, io_delegate);
Steven Morelandc26d8142018-09-17 14:25:33 -0700757 } else if (lang == Options::Language::NDK) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900758 ndk::GenerateNdk(output_file_name, options, typenames, *defined_type, io_delegate);
Steven Morelandc26d8142018-09-17 14:25:33 -0700759 success = true;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900760 } else if (lang == Options::Language::JAVA) {
Jiyong Park9ca5c7e2019-10-17 15:01:14 +0900761 if (defined_type->AsUnstructuredParcelable() != nullptr) {
762 // Legacy behavior. For parcelable declarations in Java, don't generate output file.
763 success = true;
764 } else {
765 success =
766 java::generate_java(output_file_name, defined_type, typenames, io_delegate, options);
767 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900768 } else {
769 LOG(FATAL) << "Should not reach here" << endl;
770 return 1;
771 }
772 if (!success) {
773 return 1;
774 }
Jiyong Park74595c12018-07-23 15:22:50 +0900775 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700776 }
Jiyong Park74595c12018-07-23 15:22:50 +0900777 return 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800778}
779
Andrei Onea8714b022019-02-01 18:55:54 +0000780bool dump_mappings(const Options& options, const IoDelegate& io_delegate) {
781 android::aidl::mappings::SignatureMap all_mappings;
782 for (const string& input_file : options.InputFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900783 AidlTypenames typenames;
Andrei Onea8714b022019-02-01 18:55:54 +0000784 vector<AidlDefinedType*> defined_types;
785 vector<string> imported_files;
786
787 AidlError aidl_err = internals::load_and_validate_aidl(
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900788 input_file, options, io_delegate, &typenames, &defined_types, &imported_files);
Andrei Onea8714b022019-02-01 18:55:54 +0000789 if (aidl_err != AidlError::OK) {
790 LOG(WARNING) << "AIDL file is invalid.\n";
791 continue;
792 }
793 for (const auto defined_type : defined_types) {
Daniel Norman716d3112019-09-10 13:11:56 -0700794 auto mappings = mappings::generate_mappings(defined_type, typenames);
Andrei Onea8714b022019-02-01 18:55:54 +0000795 all_mappings.insert(mappings.begin(), mappings.end());
796 }
797 }
798 std::stringstream mappings_str;
799 for (const auto& mapping : all_mappings) {
800 mappings_str << mapping.first << "\n" << mapping.second << "\n";
801 }
802 auto code_writer = io_delegate.GetCodeWriter(options.OutputFile());
803 code_writer->Write("%s", mappings_str.str().c_str());
804 return true;
805}
806
Jiyong Park74595c12018-07-23 15:22:50 +0900807bool preprocess_aidl(const Options& options, const IoDelegate& io_delegate) {
808 unique_ptr<CodeWriter> writer = io_delegate.GetCodeWriter(options.OutputFile());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800809
Jiyong Park74595c12018-07-23 15:22:50 +0900810 for (const auto& file : options.InputFiles()) {
Jiyong Park1deecc32018-07-17 01:14:41 +0900811 AidlTypenames typenames;
Steven Moreland64e29be2018-08-08 18:52:19 -0700812 std::unique_ptr<Parser> p = Parser::Parse(file, io_delegate, typenames);
813 if (p == nullptr) return false;
Casey Dahlin59401da2015-10-09 18:16:45 -0700814
Steven Moreland64e29be2018-08-08 18:52:19 -0700815 for (const auto& defined_type : p->GetDefinedTypes()) {
Steven Morelanded83a282018-07-17 13:27:29 -0700816 if (!writer->Write("%s %s;\n", defined_type->GetPreprocessDeclarationName().c_str(),
Steven Morelandc258abc2018-07-10 14:03:38 -0700817 defined_type->GetCanonicalName().c_str())) {
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800818 return false;
819 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800820 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800821 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800822
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800823 return writer->Close();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800824}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700825
Jiyong Parke59c3682018-09-11 23:10:25 +0900826static string GetApiDumpPathFor(const AidlDefinedType& defined_type, const Options& options) {
827 string package_as_path = Join(Split(defined_type.GetPackage(), "."), OS_PATH_SEPARATOR);
828 CHECK(!options.OutputDir().empty() && options.OutputDir().back() == '/');
829 return options.OutputDir() + package_as_path + OS_PATH_SEPARATOR + defined_type.GetName() +
830 ".aidl";
831}
Jiyong Parkb034bf02018-07-30 17:44:33 +0900832
Jiyong Parke59c3682018-09-11 23:10:25 +0900833bool dump_api(const Options& options, const IoDelegate& io_delegate) {
Jiyong Park74595c12018-07-23 15:22:50 +0900834 for (const auto& file : options.InputFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900835 AidlTypenames typenames;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900836 vector<AidlDefinedType*> defined_types;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900837 if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames, &defined_types,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900838 nullptr) == AidlError::OK) {
839 for (const auto type : defined_types) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900840 unique_ptr<CodeWriter> writer =
841 io_delegate.GetCodeWriter(GetApiDumpPathFor(*type, options));
Steven Morelandec0531d2018-09-20 11:11:20 -0700842 if (!type->GetPackage().empty()) {
Paul Trautrimb5ff1a22020-02-27 13:10:16 +0900843 (*writer) << kPreamble << "package " << type->GetPackage() << ";\n";
Steven Morelandec0531d2018-09-20 11:11:20 -0700844 }
Jeongik Cha997281d2020-01-16 15:23:59 +0900845 type->Dump(writer.get());
Jiyong Parkb034bf02018-07-30 17:44:33 +0900846 }
Jiyong Park02da7422018-07-16 16:00:26 +0900847 } else {
848 return false;
849 }
850 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900851 return true;
Jiyong Park02da7422018-07-16 16:00:26 +0900852}
853
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700854} // namespace aidl
Steven Morelandf4c64df2019-07-29 19:54:04 -0700855} // namespace android