blob: 830b3ae7ce5623c4f84b188d3bb15da670a30207 [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
Steven Moreland3981d9e2020-03-31 14:11:44 -070039#include "aidl_checkapi.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070040#include "aidl_language.h"
Jiyong Parke05195e2018-10-08 18:24:23 +090041#include "aidl_typenames.h"
Andrei Onea8714b022019-02-01 18:55:54 +000042#include "generate_aidl_mappings.h"
Christopher Wileyeb1acc12015-09-16 11:25:13 -070043#include "generate_cpp.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070044#include "generate_java.h"
Steven Morelandc26d8142018-09-17 14:25:33 -070045#include "generate_ndk.h"
Andrei Homescub62afd92020-05-11 19:24:59 -070046#include "generate_rust.h"
Christopher Wiley72877ac2015-10-06 14:41:42 -070047#include "import_resolver.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070048#include "logging.h"
49#include "options.h"
50#include "os.h"
Jiyong Parke5c45292020-05-26 19:06:24 +090051#include "parser.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070052
Adam Lesinskiffa16862014-01-23 18:17:42 -080053#ifndef O_BINARY
54# define O_BINARY 0
55#endif
56
Christopher Wiley3a9911c2016-01-19 12:59:09 -080057using android::base::Join;
Christopher Wileyd76067c2015-10-19 17:00:13 -070058using android::base::Split;
Christopher Wileyc16e5e72015-09-16 10:49:40 -070059using std::cerr;
60using std::endl;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070061using 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;
Paul Trautrimb77048c2020-01-21 16:39:32 +090081const int kGetInterfaceHashId = kFirstMetaMethodId - 1;
Jiyong Park965c5b92018-11-21 13:37:15 +090082// Additional meta transactions implemented by AIDL should use
83// kFirstMetaMethodId -1, -2, ...and so on.
84
85// Reserve 100 IDs for meta methods, which is more than enough. If we don't reserve,
86// in the future, a newly added meta transaction ID will have a chance to
87// collide with the user-defined methods that were added in the past. So,
88// let's prevent users from using IDs in this range from the beginning.
89const int kLastMetaMethodId = kFirstMetaMethodId - 99;
90
91// Range of IDs that is allowed for user-defined methods.
92const int kMinUserSetMethodId = 0;
93const int kMaxUserSetMethodId = kLastMetaMethodId - 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -080094
Steven Moreland92c55f12018-07-31 14:08:37 -070095bool check_filename(const std::string& filename, const AidlDefinedType& defined_type) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080096 const char* p;
97 string expected;
98 string fn;
99 size_t len;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800100 bool valid = false;
101
Christopher Wileybc2df692016-06-02 16:27:26 -0700102 if (!IoDelegate::GetAbsolutePath(filename, &fn)) {
103 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800104 }
105
Steven Moreland92c55f12018-07-31 14:08:37 -0700106 const std::string package = defined_type.GetPackage();
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700107 if (!package.empty()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800108 expected = package;
109 expected += '.';
110 }
111
112 len = expected.length();
113 for (size_t i=0; i<len; i++) {
114 if (expected[i] == '.') {
115 expected[i] = OS_PATH_SEPARATOR;
116 }
117 }
118
Steven Moreland92c55f12018-07-31 14:08:37 -0700119 const std::string name = defined_type.GetName();
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700120 expected.append(name, 0, name.find('.'));
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700121
Adam Lesinskiffa16862014-01-23 18:17:42 -0800122 expected += ".aidl";
123
124 len = fn.length();
125 valid = (len >= expected.length());
126
127 if (valid) {
128 p = fn.c_str() + (len - expected.length());
129
Elliott Hughesce310da2015-07-29 08:44:17 -0700130#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800131 if (OS_PATH_SEPARATOR != '/') {
132 // Input filename under cygwin most likely has / separators
133 // whereas the expected string uses \\ separators. Adjust
134 // them accordingly.
135 for (char *c = const_cast<char *>(p); *c; ++c) {
136 if (*c == '/') *c = OS_PATH_SEPARATOR;
137 }
138 }
139#endif
140
Yabin Cui482eefb2014-11-10 15:01:43 -0800141 // aidl assumes case-insensitivity on Mac Os and Windows.
142#if defined(__linux__)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800143 valid = (expected == p);
144#else
145 valid = !strcasecmp(expected.c_str(), p);
146#endif
147 }
148
149 if (!valid) {
Steven Moreland92c55f12018-07-31 14:08:37 -0700150 AIDL_ERROR(defined_type) << name << " should be declared in a file called " << expected;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800151 }
152
Casey Dahlin42727f82015-10-12 19:23:40 -0700153 return valid;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800154}
155
Jiyong Park74595c12018-07-23 15:22:50 +0900156bool write_dep_file(const Options& options, const AidlDefinedType& defined_type,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900157 const vector<string>& imports, const IoDelegate& io_delegate,
Jiyong Park74595c12018-07-23 15:22:50 +0900158 const string& input_file, const string& output_file) {
159 string dep_file_name = options.DependencyFile();
160 if (dep_file_name.empty() && options.AutoDepFile()) {
161 dep_file_name = output_file + ".d";
162 }
163
164 if (dep_file_name.empty()) {
165 return true; // nothing to do
166 }
Jiyong Park05463732018-08-09 16:03:02 +0900167
Jiyong Park74595c12018-07-23 15:22:50 +0900168 CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name);
169 if (!writer) {
170 LOG(ERROR) << "Could not open dependency file: " << dep_file_name;
171 return false;
172 }
173
174 vector<string> source_aidl = {input_file};
175 for (const auto& import : imports) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900176 source_aidl.push_back(import);
Jiyong Park74595c12018-07-23 15:22:50 +0900177 }
178
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800179 // Encode that the output file depends on aidl input files.
Jiyong Parkdf202122019-09-30 20:48:35 +0900180 if (defined_type.AsUnstructuredParcelable() != nullptr &&
181 options.TargetLanguage() == Options::Language::JAVA) {
182 // Legacy behavior. For parcelable declarations in Java, don't emit output file as
183 // the dependency target. b/141372861
184 writer->Write(" : \\\n");
185 } else {
186 writer->Write("%s : \\\n", output_file.c_str());
187 }
Jiyong Park74595c12018-07-23 15:22:50 +0900188 writer->Write(" %s", Join(source_aidl, " \\\n ").c_str());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800189 writer->Write("\n");
Christopher Wileya30a45e2015-10-17 10:56:59 -0700190
Jiyong Park74595c12018-07-23 15:22:50 +0900191 if (!options.DependencyFileNinja()) {
Dan Willemsen93298ee2016-11-10 23:55:55 -0800192 writer->Write("\n");
193 // Output "<input_aidl_file>: " so make won't fail if the input .aidl file
194 // has been deleted, moved or renamed in incremental build.
Jiyong Park74595c12018-07-23 15:22:50 +0900195 for (const auto& src : source_aidl) {
Dan Willemsen93298ee2016-11-10 23:55:55 -0800196 writer->Write("%s :\n", src.c_str());
197 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800198 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700199
Steven Morelandc26d8142018-09-17 14:25:33 -0700200 if (options.IsCppOutput()) {
Jiyong Park74595c12018-07-23 15:22:50 +0900201 if (!options.DependencyFileNinja()) {
202 using ::android::aidl::cpp::ClassNames;
203 using ::android::aidl::cpp::HeaderFile;
204 vector<string> headers;
Jiyong Park5b7e5322019-04-03 20:05:01 +0900205 for (ClassNames c : {ClassNames::CLIENT, ClassNames::SERVER, ClassNames::RAW}) {
Jiyong Park05463732018-08-09 16:03:02 +0900206 headers.push_back(options.OutputHeaderDir() +
Jiyong Park74595c12018-07-23 15:22:50 +0900207 HeaderFile(defined_type, c, false /* use_os_sep */));
208 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800209
Jiyong Park74595c12018-07-23 15:22:50 +0900210 writer->Write("\n");
211
212 // Generated headers also depend on the source aidl files.
213 writer->Write("%s : \\\n %s\n", Join(headers, " \\\n ").c_str(),
214 Join(source_aidl, " \\\n ").c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800215 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700216 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800217
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800218 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800219}
220
Jiyong Park74595c12018-07-23 15:22:50 +0900221string generate_outputFileName(const Options& options, const AidlDefinedType& defined_type) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700222 // create the path to the destination folder based on the
223 // defined_type package name
Jiyong Park74595c12018-07-23 15:22:50 +0900224 string result = options.OutputDir();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800225
Jiyong Park74595c12018-07-23 15:22:50 +0900226 string package = defined_type.GetPackage();
227 size_t len = package.length();
Steven Moreland5557f1c2018-07-02 13:50:23 -0700228 for (size_t i = 0; i < len; i++) {
Jiyong Park74595c12018-07-23 15:22:50 +0900229 if (package[i] == '.') {
230 package[i] = OS_PATH_SEPARATOR;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800231 }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700232 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800233
Jiyong Park74595c12018-07-23 15:22:50 +0900234 result += package;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800235
Steven Moreland5557f1c2018-07-02 13:50:23 -0700236 // add the filename by replacing the .aidl extension to .java
Jiyong Park74595c12018-07-23 15:22:50 +0900237 const string& name = defined_type.GetName();
Steven Moreland5557f1c2018-07-02 13:50:23 -0700238 result += OS_PATH_SEPARATOR;
239 result.append(name, 0, name.find('.'));
Jiyong Parkb03551f2018-08-06 19:20:51 +0900240 if (options.TargetLanguage() == Options::Language::JAVA) {
241 result += ".java";
Steven Morelandc26d8142018-09-17 14:25:33 -0700242 } else if (options.IsCppOutput()) {
Jiyong Parkb03551f2018-08-06 19:20:51 +0900243 result += ".cpp";
Andrei Homescub62afd92020-05-11 19:24:59 -0700244 } else if (options.TargetLanguage() == Options::Language::RUST) {
245 result += ".rs";
Jiyong Parkb03551f2018-08-06 19:20:51 +0900246 } else {
247 LOG(FATAL) << "Should not reach here" << endl;
248 return "";
249 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800250
Steven Moreland5557f1c2018-07-02 13:50:23 -0700251 return result;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800252}
253
Jiyong Parkb034bf02018-07-30 17:44:33 +0900254bool check_and_assign_method_ids(const std::vector<std::unique_ptr<AidlMethod>>& items) {
Jiyong Park309668e2018-07-28 16:55:44 +0900255 // Check whether there are any methods with manually assigned id's and any
256 // that are not. Either all method id's must be manually assigned or all of
257 // them must not. Also, check for uplicates of user set ID's and that the
258 // ID's are within the proper bounds.
259 set<int> usedIds;
260 bool hasUnassignedIds = false;
261 bool hasAssignedIds = false;
Steven Morelandec6f4692019-08-13 10:03:24 -0700262 int newId = kMinUserSetMethodId;
Jiyong Park309668e2018-07-28 16:55:44 +0900263 for (const auto& item : items) {
264 // However, meta transactions that are added by the AIDL compiler are
265 // exceptions. They have fixed IDs but allowed to be with user-defined
266 // methods having auto-assigned IDs. This is because the Ids of the meta
267 // transactions must be stable during the entire lifetime of an interface.
268 // In other words, their IDs must be the same even when new user-defined
269 // methods are added.
Jiyong Park3633b722019-04-11 15:38:26 +0900270 if (!item->IsUserDefined()) {
271 continue;
272 }
273 if (item->HasId()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900274 hasAssignedIds = true;
Jiyong Park309668e2018-07-28 16:55:44 +0900275 } else {
Steven Morelandec6f4692019-08-13 10:03:24 -0700276 item->SetId(newId++);
Jiyong Park309668e2018-07-28 16:55:44 +0900277 hasUnassignedIds = true;
278 }
Steven Morelandec6f4692019-08-13 10:03:24 -0700279
Jiyong Park309668e2018-07-28 16:55:44 +0900280 if (hasAssignedIds && hasUnassignedIds) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900281 AIDL_ERROR(item) << "You must either assign id's to all methods or to none of them.";
Jiyong Park309668e2018-07-28 16:55:44 +0900282 return false;
283 }
Jiyong Park309668e2018-07-28 16:55:44 +0900284
Steven Morelandec6f4692019-08-13 10:03:24 -0700285 // Ensure that the user set id is not duplicated.
286 if (usedIds.find(item->GetId()) != usedIds.end()) {
287 // We found a duplicate id, so throw an error.
288 AIDL_ERROR(item) << "Found duplicate method id (" << item->GetId() << ") for method "
289 << item->GetName();
290 return false;
291 }
292 usedIds.insert(item->GetId());
293
294 // Ensure that the user set id is within the appropriate limits
295 if (item->GetId() < kMinUserSetMethodId || item->GetId() > kMaxUserSetMethodId) {
296 AIDL_ERROR(item) << "Found out of bounds id (" << item->GetId() << ") for method "
297 << item->GetName() << ". Value for id must be between "
298 << kMinUserSetMethodId << " and " << kMaxUserSetMethodId << " inclusive.";
299 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800300 }
Jiyong Park309668e2018-07-28 16:55:44 +0900301 }
Steven Morelandec6f4692019-08-13 10:03:24 -0700302
Jiyong Park309668e2018-07-28 16:55:44 +0900303 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800304}
305
Christopher Wileyef140932015-11-03 09:29:19 -0800306// TODO: Remove this in favor of using the YACC parser b/25479378
Jiyong Park18132182020-06-08 20:24:40 +0900307bool ParsePreprocessedLine(const string& line, string* decl, std::string* package,
308 string* class_name) {
Christopher Wileyef140932015-11-03 09:29:19 -0800309 // erase all trailing whitespace and semicolons
310 const size_t end = line.find_last_not_of(" ;\t");
311 if (end == string::npos) {
312 return false;
313 }
314 if (line.rfind(';', end) != string::npos) {
315 return false;
316 }
317
318 decl->clear();
319 string type;
320 vector<string> pieces = Split(line.substr(0, end + 1), " \t");
321 for (const string& piece : pieces) {
322 if (piece.empty()) {
323 continue;
324 }
325 if (decl->empty()) {
326 *decl = std::move(piece);
327 } else if (type.empty()) {
328 type = std::move(piece);
329 } else {
330 return false;
331 }
332 }
333
334 // Note that this logic is absolutely wrong. Given a parcelable
335 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
336 // the class is just Bar. However, this was the way it was done in the past.
337 //
338 // See b/17415692
339 size_t dot_pos = type.rfind('.');
340 if (dot_pos != string::npos) {
341 *class_name = type.substr(dot_pos + 1);
Jiyong Park18132182020-06-08 20:24:40 +0900342 *package = type.substr(0, dot_pos);
Christopher Wileyef140932015-11-03 09:29:19 -0800343 } else {
344 *class_name = type;
345 package->clear();
346 }
347
348 return true;
349}
350
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700351} // namespace
352
353namespace internals {
354
Jiyong Park1deecc32018-07-17 01:14:41 +0900355bool parse_preprocessed_file(const IoDelegate& io_delegate, const string& filename,
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900356 AidlTypenames* typenames) {
Christopher Wileyef140932015-11-03 09:29:19 -0800357 bool success = true;
358 unique_ptr<LineReader> line_reader = io_delegate.GetLineReader(filename);
359 if (!line_reader) {
360 LOG(ERROR) << "cannot open preprocessed file: " << filename;
361 success = false;
362 return success;
363 }
364
365 string line;
Dan Willemsen609ba6d2019-12-30 10:44:00 -0800366 int lineno = 1;
Christopher Wileyef140932015-11-03 09:29:19 -0800367 for ( ; line_reader->ReadLine(&line); ++lineno) {
368 if (line.empty() || line.compare(0, 2, "//") == 0) {
369 // skip comments and empty lines
370 continue;
371 }
372
373 string decl;
Jiyong Park18132182020-06-08 20:24:40 +0900374 std::string package;
Christopher Wileyef140932015-11-03 09:29:19 -0800375 string class_name;
376 if (!ParsePreprocessedLine(line, &decl, &package, &class_name)) {
377 success = false;
378 break;
379 }
380
Steven Moreland46e9da82018-07-27 15:45:29 -0700381 AidlLocation::Point point = {.line = lineno, .column = 0 /*column*/};
Devin Mooredf93ebb2020-03-25 14:03:35 -0700382 AidlLocation location = AidlLocation(filename, point, point, AidlLocation::Source::EXTERNAL);
Steven Moreland46e9da82018-07-27 15:45:29 -0700383
Christopher Wileyef140932015-11-03 09:29:19 -0800384 if (decl == "parcelable") {
Jeongik Chace58bc62019-04-22 13:30:39 +0900385 // ParcelFileDescriptor is treated as a built-in type, but it's also in the framework.aidl.
386 // So aidl should ignore built-in types in framework.aidl to prevent duplication.
387 // (b/130899491)
388 if (AidlTypenames::IsBuiltinTypename(class_name)) {
389 continue;
390 }
Jiyong Park18132182020-06-08 20:24:40 +0900391 AidlParcelable* doc = new AidlParcelable(location, class_name, package, "" /* comments */);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900392 typenames->AddPreprocessedType(unique_ptr<AidlParcelable>(doc));
Steven Morelanded83a282018-07-17 13:27:29 -0700393 } else if (decl == "structured_parcelable") {
394 auto temp = new std::vector<std::unique_ptr<AidlVariableDeclaration>>();
Devin Moore53fc99c2020-08-12 08:07:52 -0700395 AidlStructuredParcelable* doc = new AidlStructuredParcelable(
396 location, class_name, package, "" /* comments */, temp, nullptr);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900397 typenames->AddPreprocessedType(unique_ptr<AidlStructuredParcelable>(doc));
Christopher Wileyef140932015-11-03 09:29:19 -0800398 } else if (decl == "interface") {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800399 auto temp = new std::vector<std::unique_ptr<AidlMember>>();
Steven Moreland46e9da82018-07-27 15:45:29 -0700400 AidlInterface* doc = new AidlInterface(location, class_name, "", false, temp, package);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900401 typenames->AddPreprocessedType(unique_ptr<AidlInterface>(doc));
Christopher Wileyef140932015-11-03 09:29:19 -0800402 } else {
403 success = false;
404 break;
405 }
406 }
407 if (!success) {
408 LOG(ERROR) << filename << ':' << lineno
409 << " malformed preprocessed file line: '" << line << "'";
410 }
411
412 return success;
413}
414
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900415AidlError load_and_validate_aidl(const std::string& input_file_name, const Options& options,
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900416 const IoDelegate& io_delegate, AidlTypenames* typenames,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900417 vector<string>* imported_files) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800418 AidlError err = AidlError::OK;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800419
Jiyong Parkb034bf02018-07-30 17:44:33 +0900420 //////////////////////////////////////////////////////////////////////////
421 // Loading phase
422 //////////////////////////////////////////////////////////////////////////
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900423
Jiyong Parkb034bf02018-07-30 17:44:33 +0900424 // Parse the main input file
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900425 std::unique_ptr<Parser> main_parser = Parser::Parse(input_file_name, io_delegate, *typenames);
Steven Moreland64e29be2018-08-08 18:52:19 -0700426 if (main_parser == nullptr) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900427 return AidlError::PARSE_ERROR;
428 }
Jiyong Parkda8c6932019-08-12 19:56:08 +0900429 int num_interfaces_or_structured_parcelables = 0;
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900430 for (const auto& type : main_parser->ParsedDocument().DefinedTypes()) {
Jiyong Parkda8c6932019-08-12 19:56:08 +0900431 if (type->AsInterface() != nullptr || type->AsStructuredParcelable() != nullptr) {
432 num_interfaces_or_structured_parcelables++;
Devin Moore5de18ed2020-04-02 13:52:29 -0700433 if (num_interfaces_or_structured_parcelables > 1) {
434 AIDL_ERROR(*type) << "You must declare only one type per file.";
435 return AidlError::BAD_TYPE;
436 }
Jiyong Parkda8c6932019-08-12 19:56:08 +0900437 }
438 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900439
440 // Import the preprocessed file
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900441 for (const string& s : options.PreprocessedFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900442 if (!parse_preprocessed_file(io_delegate, s, typenames)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800443 err = AidlError::BAD_PRE_PROCESSED_FILE;
Christopher Wileyef140932015-11-03 09:29:19 -0800444 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700445 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800446 if (err != AidlError::OK) {
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700447 return err;
448 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800449
Jiyong Parkb034bf02018-07-30 17:44:33 +0900450 // Find files to import and parse them
Jiyong Parke59c3682018-09-11 23:10:25 +0900451 vector<string> import_paths;
Jiyong Park8c380532018-08-30 14:55:26 +0900452 ImportResolver import_resolver{io_delegate, input_file_name, options.ImportDirs(),
453 options.InputFiles()};
Jiyong Parke59c3682018-09-11 23:10:25 +0900454
Jiyong Park8f6ec462020-01-19 20:52:47 +0900455 vector<string> type_from_import_statements;
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900456 for (const auto& import : main_parser->ParsedDocument().Imports()) {
Jiyong Parke05195e2018-10-08 18:24:23 +0900457 if (!AidlTypenames::IsBuiltinTypename(import->GetNeededClass())) {
Jiyong Park8f6ec462020-01-19 20:52:47 +0900458 type_from_import_statements.emplace_back(import->GetNeededClass());
Jiyong Parke05195e2018-10-08 18:24:23 +0900459 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900460 }
461
462 // When referencing a type using fully qualified name it should be imported
463 // without the import statement. To support that, add all unresolved
464 // typespecs encountered during the parsing to the import_candidates list.
465 // Note that there is no guarantee that the typespecs are all fully qualified.
466 // It will be determined by calling FindImportFile().
467 set<string> unresolved_types;
468 for (const auto type : main_parser->GetUnresolvedTypespecs()) {
469 if (!AidlTypenames::IsBuiltinTypename(type->GetName())) {
470 unresolved_types.emplace(type->GetName());
471 }
472 }
Jiyong Park8f6ec462020-01-19 20:52:47 +0900473 vector<string> import_candidates(type_from_import_statements);
474 import_candidates.insert(import_candidates.end(), unresolved_types.begin(),
475 unresolved_types.end());
Jiyong Parke59c3682018-09-11 23:10:25 +0900476 for (const auto& import : import_candidates) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900477 if (typenames->IsIgnorableImport(import)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700478 // There are places in the Android tree where an import doesn't resolve,
479 // but we'll pick the type up through the preprocessed types.
480 // This seems like an error, but legacy support demands we support it...
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700481 continue;
482 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900483 string import_path = import_resolver.FindImportFile(import);
Christopher Wiley72877ac2015-10-06 14:41:42 -0700484 if (import_path.empty()) {
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700485 if (typenames->ResolveTypename(import).is_resolved) {
Jiyong Park8f6ec462020-01-19 20:52:47 +0900486 // Couldn't find the *.aidl file for the type from the include paths, but we
487 // have the type already resolved. This could happen when the type is
488 // from the preprocessed aidl file. In that case, use the type from the
489 // preprocessed aidl file as a last resort.
490 continue;
491 }
492
493 if (std::find(type_from_import_statements.begin(), type_from_import_statements.end(),
494 import) != type_from_import_statements.end()) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900495 // Complain only when the import from the import statement has failed.
Devin Moore5de18ed2020-04-02 13:52:29 -0700496 AIDL_ERROR(input_file_name) << "Couldn't find import for class " << import;
Jiyong Parke59c3682018-09-11 23:10:25 +0900497 err = AidlError::BAD_IMPORT;
498 }
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700499 continue;
500 }
Casey Dahlin2cc93162015-10-02 16:14:17 -0700501
Jiyong Parke59c3682018-09-11 23:10:25 +0900502 import_paths.emplace_back(import_path);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900503
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900504 std::unique_ptr<Parser> import_parser = Parser::Parse(import_path, io_delegate, *typenames);
Steven Moreland64e29be2018-08-08 18:52:19 -0700505 if (import_parser == nullptr) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900506 cerr << "error while importing " << import_path << " for " << import << endl;
Christopher Wiley632801d2015-11-05 14:15:49 -0800507 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700508 continue;
509 }
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700510 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800511 if (err != AidlError::OK) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700512 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700513 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800514
Jiyong Park3c35e392018-08-30 13:10:30 +0900515 for (const auto& imported_file : options.ImportFiles()) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900516 import_paths.emplace_back(imported_file);
Jiyong Park3c35e392018-08-30 13:10:30 +0900517
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900518 std::unique_ptr<Parser> import_parser = Parser::Parse(imported_file, io_delegate, *typenames);
Jiyong Park3c35e392018-08-30 13:10:30 +0900519 if (import_parser == nullptr) {
520 AIDL_ERROR(imported_file) << "error while importing " << imported_file;
521 err = AidlError::BAD_IMPORT;
522 continue;
523 }
Jiyong Park3c35e392018-08-30 13:10:30 +0900524 }
525 if (err != AidlError::OK) {
526 return err;
527 }
Jiyong Park96c16a92018-08-16 16:37:09 +0900528 const bool is_check_api = options.GetTask() == Options::Task::CHECK_API;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900529
530 // Resolve the unresolved type references found from the input file
Jiyong Park96c16a92018-08-16 16:37:09 +0900531 if (!is_check_api && !main_parser->Resolve()) {
532 // Resolution is not need for check api because all typespecs are
533 // using fully qualified names.
Jiyong Park1deecc32018-07-17 01:14:41 +0900534 return AidlError::BAD_TYPE;
535 }
Daniel Norman85aed542019-08-21 12:01:14 -0700536
537 typenames->IterateTypes([&](const AidlDefinedType& type) {
538 AidlEnumDeclaration* enum_decl = const_cast<AidlEnumDeclaration*>(type.AsEnumDeclaration());
539 if (enum_decl != nullptr) {
540 // BackingType is filled in for all known enums, including imported enums,
541 // because other types that may use enums, such as Interface or
542 // StructuredParcelable, need to know the enum BackingType when
543 // generating code.
Daniel Norman716d3112019-09-10 13:11:56 -0700544 if (auto backing_type = enum_decl->BackingType(*typenames); backing_type != nullptr) {
Daniel Norman85aed542019-08-21 12:01:14 -0700545 enum_decl->SetBackingType(std::unique_ptr<const AidlTypeSpecifier>(backing_type));
546 } else {
547 // Default to byte type for enums.
Daniel Norman716d3112019-09-10 13:11:56 -0700548 auto byte_type =
549 std::make_unique<AidlTypeSpecifier>(AIDL_LOCATION_HERE, "byte", false, nullptr, "");
550 byte_type->Resolve(*typenames);
551 enum_decl->SetBackingType(std::move(byte_type));
Daniel Norman85aed542019-08-21 12:01:14 -0700552 }
553
Steven Moreland59e53e42019-11-26 20:38:08 -0800554 if (!enum_decl->Autofill()) {
555 err = AidlError::BAD_TYPE;
556 }
Daniel Norman85aed542019-08-21 12:01:14 -0700557 }
558 });
Steven Moreland59e53e42019-11-26 20:38:08 -0800559 if (err != AidlError::OK) {
560 return err;
561 }
Daniel Norman85aed542019-08-21 12:01:14 -0700562
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900563 //////////////////////////////////////////////////////////////////////////
564 // Validation phase
565 //////////////////////////////////////////////////////////////////////////
566
Steven Morelande2c64b42018-09-18 15:06:37 -0700567 // For legacy reasons, by default, compiling an unstructured parcelable (which contains no output)
568 // is allowed. This must not be returned as an error until the very end of this procedure since
569 // this may be considered a success, and we should first check that there are not other, more
570 // serious failures.
571 bool contains_unstructured_parcelable = false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800572
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900573 const auto& types = main_parser->ParsedDocument().DefinedTypes();
Jiyong Park62515512020-06-08 15:57:11 +0900574 const int num_defined_types = types.size();
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900575 for (const auto& defined_type : types) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900576 CHECK(defined_type != nullptr);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900577
578 // Language specific validation
Steven Morelandd59e3172020-05-11 16:42:09 -0700579 if (!defined_type->LanguageSpecificCheckValid(*typenames, options.TargetLanguage())) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900580 return AidlError::BAD_TYPE;
581 }
582
Steven Morelande2c64b42018-09-18 15:06:37 -0700583 AidlParcelable* unstructuredParcelable = defined_type->AsUnstructuredParcelable();
584 if (unstructuredParcelable != nullptr) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900585 if (!unstructuredParcelable->CheckValid(*typenames)) {
Jeongik Cha82317dd2019-02-27 20:26:11 +0900586 return AidlError::BAD_TYPE;
587 }
Jeongik Cha88f95a82020-01-15 13:02:16 +0900588 bool isStable = unstructuredParcelable->IsStableApiParcelable(options.TargetLanguage());
Jeongik Cha82317dd2019-02-27 20:26:11 +0900589 if (options.IsStructured() && !isStable) {
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800590 AIDL_ERROR(unstructuredParcelable)
591 << "Cannot declared parcelable in a --structured interface. Parcelable must be defined "
592 "in AIDL directly.";
593 return AidlError::NOT_STRUCTURED;
594 }
595 if (options.FailOnParcelable()) {
596 AIDL_ERROR(unstructuredParcelable)
597 << "Refusing to generate code with unstructured parcelables. Declared parcelables "
598 "should be in their own file and/or cannot be used with --structured interfaces.";
599 // Continue parsing for more errors
600 }
601
Steven Morelande2c64b42018-09-18 15:06:37 -0700602 contains_unstructured_parcelable = true;
603 continue;
604 }
605
Devin Moore0d0e3f62020-03-30 17:45:39 -0700606 if (defined_type->IsVintfStability()) {
607 bool success = true;
608 if (options.GetStability() != Options::Stability::VINTF) {
609 AIDL_ERROR(defined_type)
610 << "Must compile @VintfStability type w/ aidl_interface 'stability: \"vintf\"'";
611 success = false;
612 }
613 if (!options.IsStructured()) {
614 AIDL_ERROR(defined_type)
615 << "Must compile @VintfStability type w/ aidl_interface --structured";
616 success = false;
617 }
618 if (!success) return AidlError::NOT_STRUCTURED;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700619 }
620
Daniel Norman85aed542019-08-21 12:01:14 -0700621 // Ensure that a type is either an interface, structured parcelable, or
622 // enum.
Jiyong Parkb034bf02018-07-30 17:44:33 +0900623 AidlInterface* interface = defined_type->AsInterface();
624 AidlStructuredParcelable* parcelable = defined_type->AsStructuredParcelable();
Daniel Norman85aed542019-08-21 12:01:14 -0700625 AidlEnumDeclaration* enum_decl = defined_type->AsEnumDeclaration();
626 CHECK(!!interface + !!parcelable + !!enum_decl == 1);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900627
628 // Ensure that foo.bar.IFoo is defined in <some_path>/foo/bar/IFoo.aidl
Jiyong Parke59c3682018-09-11 23:10:25 +0900629 if (num_defined_types == 1 && !check_filename(input_file_name, *defined_type)) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900630 return AidlError::BAD_PACKAGE;
631 }
632
Jiyong Parkb034bf02018-07-30 17:44:33 +0900633 // Check the referenced types in parsed_doc to make sure we've imported them
Jiyong Park96c16a92018-08-16 16:37:09 +0900634 if (!is_check_api) {
635 // No need to do this for check api because all typespecs are already
636 // using fully qualified name and we don't import in AIDL files.
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900637 if (!defined_type->CheckValid(*typenames)) {
Jiyong Park96c16a92018-08-16 16:37:09 +0900638 return AidlError::BAD_TYPE;
639 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900640 }
641
642 if (interface != nullptr) {
643 // add the meta-method 'int getInterfaceVersion()' if version is specified.
644 if (options.Version() > 0) {
645 AidlTypeSpecifier* ret =
Steven Moreland02e012e2018-08-02 14:58:10 -0700646 new AidlTypeSpecifier(AIDL_LOCATION_HERE, "int", false, nullptr, "");
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900647 ret->Resolve(*typenames);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900648 vector<unique_ptr<AidlArgument>>* args = new vector<unique_ptr<AidlArgument>>();
649 AidlMethod* method =
Steven Moreland02e012e2018-08-02 14:58:10 -0700650 new AidlMethod(AIDL_LOCATION_HERE, false, ret, "getInterfaceVersion", args, "",
Jiyong Parkb034bf02018-07-30 17:44:33 +0900651 kGetInterfaceVersionId, false /* is_user_defined */);
652 interface->GetMutableMethods().emplace_back(method);
653 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900654 // add the meta-method 'string getInterfaceHash()' if hash is specified.
655 if (!options.Hash().empty()) {
656 AidlTypeSpecifier* ret =
657 new AidlTypeSpecifier(AIDL_LOCATION_HERE, "String", false, nullptr, "");
658 ret->Resolve(*typenames);
659 vector<unique_ptr<AidlArgument>>* args = new vector<unique_ptr<AidlArgument>>();
660 AidlMethod* method = new AidlMethod(AIDL_LOCATION_HERE, false, ret, kGetInterfaceHash, args,
661 "", kGetInterfaceHashId, false /* is_user_defined */);
662 interface->GetMutableMethods().emplace_back(method);
663 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900664 if (!check_and_assign_method_ids(interface->GetMethods())) {
665 return AidlError::BAD_METHOD_ID;
666 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700667
668 // Verify and resolve the constant declarations
669 for (const auto& constant : interface->GetConstantDeclarations()) {
670 switch (constant->GetValue().GetType()) {
671 case AidlConstantValue::Type::STRING: // fall-through
672 case AidlConstantValue::Type::INT8: // fall-through
673 case AidlConstantValue::Type::INT32: // fall-through
674 case AidlConstantValue::Type::INT64: // fall-through
675 case AidlConstantValue::Type::FLOATING: // fall-through
676 case AidlConstantValue::Type::UNARY: // fall-through
677 case AidlConstantValue::Type::BINARY: {
678 bool success = constant->CheckValid(*typenames);
679 if (!success) {
680 return AidlError::BAD_TYPE;
681 }
682 if (constant->ValueString(cpp::ConstantValueDecorator).empty()) {
683 return AidlError::BAD_TYPE;
684 }
685 break;
686 }
687 default:
688 LOG(FATAL) << "Unrecognized constant type: "
689 << static_cast<int>(constant->GetValue().GetType());
690 break;
691 }
692 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900693 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800694 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800695
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900696 typenames->IterateTypes([&](const AidlDefinedType& type) {
Steven Morelanda57d0a62019-07-30 09:41:14 -0700697 if (options.IsStructured() && type.AsUnstructuredParcelable() != nullptr &&
Jeongik Cha88f95a82020-01-15 13:02:16 +0900698 !type.AsUnstructuredParcelable()->IsStableApiParcelable(options.TargetLanguage())) {
Steven Morelanda57d0a62019-07-30 09:41:14 -0700699 err = AidlError::NOT_STRUCTURED;
Devin Moore097a3ab2020-03-11 16:08:44 -0700700 AIDL_ERROR(type) << type.GetCanonicalName()
701 << " is not structured, but this is a structured interface.";
Steven Morelanda57d0a62019-07-30 09:41:14 -0700702 }
703 if (options.GetStability() == Options::Stability::VINTF && !type.IsVintfStability()) {
704 err = AidlError::NOT_STRUCTURED;
Devin Moore097a3ab2020-03-11 16:08:44 -0700705 AIDL_ERROR(type) << type.GetCanonicalName()
706 << " does not have VINTF level stability, but this interface requires it.";
Steven Morelanda57d0a62019-07-30 09:41:14 -0700707 }
Jiyong Parkf8d53612020-05-04 14:06:13 +0900708
709 // Ensure that untyped List/Map is not used in stable AIDL.
710 if (options.IsStructured()) {
711 const AidlInterface* iface = type.AsInterface();
712 const AidlStructuredParcelable* parcelable = type.AsStructuredParcelable();
713
714 auto check = [&err](const AidlTypeSpecifier& type, const AidlNode* node) {
715 if (!type.IsGeneric() && (type.GetName() == "List" || type.GetName() == "Map")) {
716 err = AidlError::BAD_TYPE;
717 AIDL_ERROR(node)
718 << "Encountered an untyped List or Map. The use of untyped List/Map is prohibited "
719 << "because it is not guaranteed that the objects in the list are recognizable in "
720 << "the receiving side. Consider switching to an array or a generic List/Map.";
721 }
722 };
723
724 if (iface != nullptr) {
725 for (const auto& method : iface->GetMethods()) {
726 check(method->GetType(), method.get());
727 for (const auto& arg : method->GetArguments()) {
728 check(arg->GetType(), method.get());
729 }
730 }
731 } else if (parcelable != nullptr) {
732 for (const auto& field : parcelable->GetFields()) {
733 check(field->GetType(), field.get());
734 }
735 }
736 }
Steven Morelanda57d0a62019-07-30 09:41:14 -0700737 });
738
Steven Moreland6cee3482018-07-18 14:39:58 -0700739 if (err != AidlError::OK) {
740 return err;
741 }
742
Jiyong Parkb034bf02018-07-30 17:44:33 +0900743 if (imported_files != nullptr) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900744 *imported_files = import_paths;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700745 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700746
Steven Morelande2c64b42018-09-18 15:06:37 -0700747 if (contains_unstructured_parcelable) {
748 // Considered a success for the legacy case, so this must be returned last.
749 return AidlError::FOUND_PARCELABLE;
750 }
751
Christopher Wiley632801d2015-11-05 14:15:49 -0800752 return AidlError::OK;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700753}
754
Casey Dahlin2cc93162015-10-02 16:14:17 -0700755} // namespace internals
756
Jiyong Parkb034bf02018-07-30 17:44:33 +0900757int compile_aidl(const Options& options, const IoDelegate& io_delegate) {
758 const Options::Language lang = options.TargetLanguage();
Jiyong Park74595c12018-07-23 15:22:50 +0900759 for (const string& input_file : options.InputFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900760 AidlTypenames typenames;
Jiyong Park74595c12018-07-23 15:22:50 +0900761
Jiyong Parkb034bf02018-07-30 17:44:33 +0900762 vector<string> imported_files;
Jiyong Park74595c12018-07-23 15:22:50 +0900763
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900764 AidlError aidl_err = internals::load_and_validate_aidl(input_file, options, io_delegate,
765 &typenames, &imported_files);
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800766 bool allowError = aidl_err == AidlError::FOUND_PARCELABLE && !options.FailOnParcelable();
767 if (aidl_err != AidlError::OK && !allowError) {
Jiyong Park74595c12018-07-23 15:22:50 +0900768 return 1;
769 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700770
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900771 for (const auto& defined_type : typenames.MainDocument().DefinedTypes()) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900772 CHECK(defined_type != nullptr);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700773
Jiyong Parkb034bf02018-07-30 17:44:33 +0900774 string output_file_name = options.OutputFile();
775 // if needed, generate the output file name from the base folder
776 if (output_file_name.empty() && !options.OutputDir().empty()) {
Jiyong Parkb03551f2018-08-06 19:20:51 +0900777 output_file_name = generate_outputFileName(options, *defined_type);
778 if (output_file_name.empty()) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900779 return 1;
780 }
781 }
Jiyong Park74595c12018-07-23 15:22:50 +0900782
Jiyong Parkb034bf02018-07-30 17:44:33 +0900783 if (!write_dep_file(options, *defined_type, imported_files, io_delegate, input_file,
784 output_file_name)) {
785 return 1;
786 }
Jiyong Park74595c12018-07-23 15:22:50 +0900787
Jiyong Parkb034bf02018-07-30 17:44:33 +0900788 bool success = false;
789 if (lang == Options::Language::CPP) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900790 success =
791 cpp::GenerateCpp(output_file_name, options, typenames, *defined_type, io_delegate);
Steven Morelandc26d8142018-09-17 14:25:33 -0700792 } else if (lang == Options::Language::NDK) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900793 ndk::GenerateNdk(output_file_name, options, typenames, *defined_type, io_delegate);
Steven Morelandc26d8142018-09-17 14:25:33 -0700794 success = true;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900795 } else if (lang == Options::Language::JAVA) {
Jiyong Park9ca5c7e2019-10-17 15:01:14 +0900796 if (defined_type->AsUnstructuredParcelable() != nullptr) {
797 // Legacy behavior. For parcelable declarations in Java, don't generate output file.
798 success = true;
799 } else {
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900800 success = java::generate_java(output_file_name, defined_type.get(), typenames,
801 io_delegate, options);
Jiyong Park9ca5c7e2019-10-17 15:01:14 +0900802 }
Andrei Homescub62afd92020-05-11 19:24:59 -0700803 } else if (lang == Options::Language::RUST) {
804 success = rust::GenerateRust(output_file_name, defined_type.get(), typenames, io_delegate,
805 options);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900806 } else {
807 LOG(FATAL) << "Should not reach here" << endl;
808 return 1;
809 }
810 if (!success) {
811 return 1;
812 }
Jiyong Park74595c12018-07-23 15:22:50 +0900813 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700814 }
Jiyong Park74595c12018-07-23 15:22:50 +0900815 return 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800816}
817
Andrei Onea8714b022019-02-01 18:55:54 +0000818bool dump_mappings(const Options& options, const IoDelegate& io_delegate) {
819 android::aidl::mappings::SignatureMap all_mappings;
820 for (const string& input_file : options.InputFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900821 AidlTypenames typenames;
Andrei Onea8714b022019-02-01 18:55:54 +0000822 vector<string> imported_files;
823
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900824 AidlError aidl_err = internals::load_and_validate_aidl(input_file, options, io_delegate,
825 &typenames, &imported_files);
Andrei Onea8714b022019-02-01 18:55:54 +0000826 if (aidl_err != AidlError::OK) {
Jiyong Park3a060392020-04-11 21:02:19 +0900827 return false;
Andrei Onea8714b022019-02-01 18:55:54 +0000828 }
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900829 for (const auto& defined_type : typenames.MainDocument().DefinedTypes()) {
830 auto mappings = mappings::generate_mappings(defined_type.get(), typenames);
Andrei Onea8714b022019-02-01 18:55:54 +0000831 all_mappings.insert(mappings.begin(), mappings.end());
832 }
833 }
834 std::stringstream mappings_str;
835 for (const auto& mapping : all_mappings) {
836 mappings_str << mapping.first << "\n" << mapping.second << "\n";
837 }
838 auto code_writer = io_delegate.GetCodeWriter(options.OutputFile());
839 code_writer->Write("%s", mappings_str.str().c_str());
840 return true;
841}
842
Jiyong Park74595c12018-07-23 15:22:50 +0900843bool preprocess_aidl(const Options& options, const IoDelegate& io_delegate) {
844 unique_ptr<CodeWriter> writer = io_delegate.GetCodeWriter(options.OutputFile());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800845
Jiyong Park74595c12018-07-23 15:22:50 +0900846 for (const auto& file : options.InputFiles()) {
Jiyong Park1deecc32018-07-17 01:14:41 +0900847 AidlTypenames typenames;
Steven Moreland64e29be2018-08-08 18:52:19 -0700848 std::unique_ptr<Parser> p = Parser::Parse(file, io_delegate, typenames);
849 if (p == nullptr) return false;
Casey Dahlin59401da2015-10-09 18:16:45 -0700850
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900851 for (const auto& defined_type : p->ParsedDocument().DefinedTypes()) {
Steven Morelanded83a282018-07-17 13:27:29 -0700852 if (!writer->Write("%s %s;\n", defined_type->GetPreprocessDeclarationName().c_str(),
Steven Morelandc258abc2018-07-10 14:03:38 -0700853 defined_type->GetCanonicalName().c_str())) {
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800854 return false;
855 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800856 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800857 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800858
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800859 return writer->Close();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800860}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700861
Jiyong Parke59c3682018-09-11 23:10:25 +0900862static string GetApiDumpPathFor(const AidlDefinedType& defined_type, const Options& options) {
863 string package_as_path = Join(Split(defined_type.GetPackage(), "."), OS_PATH_SEPARATOR);
864 CHECK(!options.OutputDir().empty() && options.OutputDir().back() == '/');
865 return options.OutputDir() + package_as_path + OS_PATH_SEPARATOR + defined_type.GetName() +
866 ".aidl";
867}
Jiyong Parkb034bf02018-07-30 17:44:33 +0900868
Jiyong Parke59c3682018-09-11 23:10:25 +0900869bool dump_api(const Options& options, const IoDelegate& io_delegate) {
Jiyong Park74595c12018-07-23 15:22:50 +0900870 for (const auto& file : options.InputFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900871 AidlTypenames typenames;
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900872 if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames, nullptr) ==
873 AidlError::OK) {
874 for (const auto& type : typenames.MainDocument().DefinedTypes()) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900875 unique_ptr<CodeWriter> writer =
876 io_delegate.GetCodeWriter(GetApiDumpPathFor(*type, options));
Steven Morelandec0531d2018-09-20 11:11:20 -0700877 if (!type->GetPackage().empty()) {
Paul Trautrimb01451d2020-02-27 13:10:16 +0900878 (*writer) << kPreamble << "package " << type->GetPackage() << ";\n";
Steven Morelandec0531d2018-09-20 11:11:20 -0700879 }
Jeongik Cha997281d2020-01-16 15:23:59 +0900880 type->Dump(writer.get());
Jiyong Parkb034bf02018-07-30 17:44:33 +0900881 }
Jiyong Park02da7422018-07-16 16:00:26 +0900882 } else {
883 return false;
884 }
885 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900886 return true;
Jiyong Park02da7422018-07-16 16:00:26 +0900887}
888
Steven Moreland3981d9e2020-03-31 14:11:44 -0700889int aidl_entry(const Options& options, const IoDelegate& io_delegate) {
Steven Moreland33efcf62020-04-10 16:40:43 -0700890 AidlErrorLog::clearError();
891
Steven Moreland3981d9e2020-03-31 14:11:44 -0700892 int ret = 1;
893 switch (options.GetTask()) {
894 case Options::Task::COMPILE:
895 ret = android::aidl::compile_aidl(options, io_delegate);
896 break;
897 case Options::Task::PREPROCESS:
898 ret = android::aidl::preprocess_aidl(options, io_delegate) ? 0 : 1;
899 break;
900 case Options::Task::DUMP_API:
901 ret = android::aidl::dump_api(options, io_delegate) ? 0 : 1;
902 break;
903 case Options::Task::CHECK_API:
904 ret = android::aidl::check_api(options, io_delegate) ? 0 : 1;
905 break;
906 case Options::Task::DUMP_MAPPINGS:
907 ret = android::aidl::dump_mappings(options, io_delegate) ? 0 : 1;
908 break;
909 default:
910 AIDL_FATAL(AIDL_LOCATION_HERE)
911 << "Unrecognized task: " << static_cast<size_t>(options.GetTask());
912 }
913
914 // compiler invariants
915
916 // once AIDL_ERROR/AIDL_FATAL are used everywhere instead of std::cerr/LOG, we
917 // can make this assertion in both directions.
918 if (ret == 0) {
919 AIDL_FATAL_IF(AidlErrorLog::hadError(), "Compiler success, but error emitted");
920 }
921
922 return ret;
923}
924
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700925} // namespace aidl
Steven Morelandf4c64df2019-07-29 19:54:04 -0700926} // namespace android