blob: 5b1295de8fb4cc868451175609a4138a83290ef6 [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 Wiley9f4c7ae2015-08-24 14:07:32 -070059using std::set;
60using std::string;
Christopher Wiley84c1eac2015-09-23 13:29:28 -070061using std::unique_ptr;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070062using std::vector;
Adam Lesinskiffa16862014-01-23 18:17:42 -080063
Christopher Wileyf690be52015-09-14 15:19:10 -070064namespace android {
65namespace aidl {
66namespace {
Adam Lesinskiffa16862014-01-23 18:17:42 -080067
Jiyong Park965c5b92018-11-21 13:37:15 +090068// Copied from android.is.IBinder.[FIRST|LAST]_CALL_TRANSACTION
69const int kFirstCallTransaction = 1;
70const int kLastCallTransaction = 0x00ffffff;
71
72// Following IDs are all offsets from kFirstCallTransaction
Jiyong Park309668e2018-07-28 16:55:44 +090073
74// IDs for meta transactions. Most of the meta transactions are implemented in
75// the framework side (Binder.java or Binder.cpp). But these are the ones that
76// are auto-implemented by the AIDL compiler.
Jiyong Park965c5b92018-11-21 13:37:15 +090077const int kFirstMetaMethodId = kLastCallTransaction - kFirstCallTransaction;
78const int kGetInterfaceVersionId = kFirstMetaMethodId;
Paul Trautrimb77048c2020-01-21 16:39:32 +090079const int kGetInterfaceHashId = kFirstMetaMethodId - 1;
Jiyong Park965c5b92018-11-21 13:37:15 +090080// Additional meta transactions implemented by AIDL should use
81// kFirstMetaMethodId -1, -2, ...and so on.
82
83// Reserve 100 IDs for meta methods, which is more than enough. If we don't reserve,
84// in the future, a newly added meta transaction ID will have a chance to
85// collide with the user-defined methods that were added in the past. So,
86// let's prevent users from using IDs in this range from the beginning.
87const int kLastMetaMethodId = kFirstMetaMethodId - 99;
88
89// Range of IDs that is allowed for user-defined methods.
90const int kMinUserSetMethodId = 0;
91const int kMaxUserSetMethodId = kLastMetaMethodId - 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -080092
Steven Moreland92c55f12018-07-31 14:08:37 -070093bool check_filename(const std::string& filename, const AidlDefinedType& defined_type) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080094 const char* p;
95 string expected;
96 string fn;
97 size_t len;
Adam Lesinskiffa16862014-01-23 18:17:42 -080098 bool valid = false;
99
Christopher Wileybc2df692016-06-02 16:27:26 -0700100 if (!IoDelegate::GetAbsolutePath(filename, &fn)) {
101 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800102 }
103
Steven Moreland92c55f12018-07-31 14:08:37 -0700104 const std::string package = defined_type.GetPackage();
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700105 if (!package.empty()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800106 expected = package;
107 expected += '.';
108 }
109
110 len = expected.length();
111 for (size_t i=0; i<len; i++) {
112 if (expected[i] == '.') {
113 expected[i] = OS_PATH_SEPARATOR;
114 }
115 }
116
Steven Moreland92c55f12018-07-31 14:08:37 -0700117 const std::string name = defined_type.GetName();
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700118 expected.append(name, 0, name.find('.'));
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700119
Adam Lesinskiffa16862014-01-23 18:17:42 -0800120 expected += ".aidl";
121
122 len = fn.length();
123 valid = (len >= expected.length());
124
125 if (valid) {
126 p = fn.c_str() + (len - expected.length());
127
Elliott Hughesce310da2015-07-29 08:44:17 -0700128#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800129 if (OS_PATH_SEPARATOR != '/') {
130 // Input filename under cygwin most likely has / separators
131 // whereas the expected string uses \\ separators. Adjust
132 // them accordingly.
133 for (char *c = const_cast<char *>(p); *c; ++c) {
134 if (*c == '/') *c = OS_PATH_SEPARATOR;
135 }
136 }
137#endif
138
Yabin Cui482eefb2014-11-10 15:01:43 -0800139 // aidl assumes case-insensitivity on Mac Os and Windows.
140#if defined(__linux__)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800141 valid = (expected == p);
142#else
143 valid = !strcasecmp(expected.c_str(), p);
144#endif
145 }
146
147 if (!valid) {
Steven Moreland92c55f12018-07-31 14:08:37 -0700148 AIDL_ERROR(defined_type) << name << " should be declared in a file called " << expected;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800149 }
150
Casey Dahlin42727f82015-10-12 19:23:40 -0700151 return valid;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800152}
153
Jiyong Park74595c12018-07-23 15:22:50 +0900154bool write_dep_file(const Options& options, const AidlDefinedType& defined_type,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900155 const vector<string>& imports, const IoDelegate& io_delegate,
Jiyong Park74595c12018-07-23 15:22:50 +0900156 const string& input_file, const string& output_file) {
157 string dep_file_name = options.DependencyFile();
158 if (dep_file_name.empty() && options.AutoDepFile()) {
159 dep_file_name = output_file + ".d";
160 }
161
162 if (dep_file_name.empty()) {
163 return true; // nothing to do
164 }
Jiyong Park05463732018-08-09 16:03:02 +0900165
Jiyong Park74595c12018-07-23 15:22:50 +0900166 CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name);
167 if (!writer) {
Steven Moreland21780812020-09-11 01:29:45 +0000168 AIDL_ERROR(dep_file_name) << "Could not open dependency file.";
Jiyong Park74595c12018-07-23 15:22:50 +0900169 return false;
170 }
171
172 vector<string> source_aidl = {input_file};
173 for (const auto& import : imports) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900174 source_aidl.push_back(import);
Jiyong Park74595c12018-07-23 15:22:50 +0900175 }
176
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800177 // Encode that the output file depends on aidl input files.
Jiyong Parkdf202122019-09-30 20:48:35 +0900178 if (defined_type.AsUnstructuredParcelable() != nullptr &&
179 options.TargetLanguage() == Options::Language::JAVA) {
180 // Legacy behavior. For parcelable declarations in Java, don't emit output file as
181 // the dependency target. b/141372861
182 writer->Write(" : \\\n");
183 } else {
184 writer->Write("%s : \\\n", output_file.c_str());
185 }
Jiyong Park74595c12018-07-23 15:22:50 +0900186 writer->Write(" %s", Join(source_aidl, " \\\n ").c_str());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800187 writer->Write("\n");
Christopher Wileya30a45e2015-10-17 10:56:59 -0700188
Jiyong Park74595c12018-07-23 15:22:50 +0900189 if (!options.DependencyFileNinja()) {
Dan Willemsen93298ee2016-11-10 23:55:55 -0800190 writer->Write("\n");
191 // Output "<input_aidl_file>: " so make won't fail if the input .aidl file
192 // has been deleted, moved or renamed in incremental build.
Jiyong Park74595c12018-07-23 15:22:50 +0900193 for (const auto& src : source_aidl) {
Dan Willemsen93298ee2016-11-10 23:55:55 -0800194 writer->Write("%s :\n", src.c_str());
195 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800196 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700197
Steven Morelandc26d8142018-09-17 14:25:33 -0700198 if (options.IsCppOutput()) {
Jiyong Park74595c12018-07-23 15:22:50 +0900199 if (!options.DependencyFileNinja()) {
200 using ::android::aidl::cpp::ClassNames;
201 using ::android::aidl::cpp::HeaderFile;
202 vector<string> headers;
Jiyong Park5b7e5322019-04-03 20:05:01 +0900203 for (ClassNames c : {ClassNames::CLIENT, ClassNames::SERVER, ClassNames::RAW}) {
Jiyong Park05463732018-08-09 16:03:02 +0900204 headers.push_back(options.OutputHeaderDir() +
Jiyong Park74595c12018-07-23 15:22:50 +0900205 HeaderFile(defined_type, c, false /* use_os_sep */));
206 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800207
Jiyong Park74595c12018-07-23 15:22:50 +0900208 writer->Write("\n");
209
210 // Generated headers also depend on the source aidl files.
211 writer->Write("%s : \\\n %s\n", Join(headers, " \\\n ").c_str(),
212 Join(source_aidl, " \\\n ").c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800213 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700214 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800215
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800216 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800217}
218
Jiyong Park74595c12018-07-23 15:22:50 +0900219string generate_outputFileName(const Options& options, const AidlDefinedType& defined_type) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700220 // create the path to the destination folder based on the
221 // defined_type package name
Jiyong Park74595c12018-07-23 15:22:50 +0900222 string result = options.OutputDir();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800223
Jiyong Park74595c12018-07-23 15:22:50 +0900224 string package = defined_type.GetPackage();
225 size_t len = package.length();
Steven Moreland5557f1c2018-07-02 13:50:23 -0700226 for (size_t i = 0; i < len; i++) {
Jiyong Park74595c12018-07-23 15:22:50 +0900227 if (package[i] == '.') {
228 package[i] = OS_PATH_SEPARATOR;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800229 }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700230 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800231
Jiyong Park74595c12018-07-23 15:22:50 +0900232 result += package;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800233
Steven Moreland5557f1c2018-07-02 13:50:23 -0700234 // add the filename by replacing the .aidl extension to .java
Jiyong Park74595c12018-07-23 15:22:50 +0900235 const string& name = defined_type.GetName();
Steven Moreland5557f1c2018-07-02 13:50:23 -0700236 result += OS_PATH_SEPARATOR;
237 result.append(name, 0, name.find('.'));
Jiyong Parkb03551f2018-08-06 19:20:51 +0900238 if (options.TargetLanguage() == Options::Language::JAVA) {
239 result += ".java";
Steven Morelandc26d8142018-09-17 14:25:33 -0700240 } else if (options.IsCppOutput()) {
Jiyong Parkb03551f2018-08-06 19:20:51 +0900241 result += ".cpp";
Andrei Homescub62afd92020-05-11 19:24:59 -0700242 } else if (options.TargetLanguage() == Options::Language::RUST) {
243 result += ".rs";
Jiyong Parkb03551f2018-08-06 19:20:51 +0900244 } else {
Steven Moreland21780812020-09-11 01:29:45 +0000245 AIDL_FATAL("Unknown target language");
Jiyong Parkb03551f2018-08-06 19:20:51 +0900246 return "";
247 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800248
Steven Moreland5557f1c2018-07-02 13:50:23 -0700249 return result;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800250}
251
Jiyong Parkb034bf02018-07-30 17:44:33 +0900252bool check_and_assign_method_ids(const std::vector<std::unique_ptr<AidlMethod>>& items) {
Jiyong Park309668e2018-07-28 16:55:44 +0900253 // Check whether there are any methods with manually assigned id's and any
254 // that are not. Either all method id's must be manually assigned or all of
255 // them must not. Also, check for uplicates of user set ID's and that the
256 // ID's are within the proper bounds.
257 set<int> usedIds;
258 bool hasUnassignedIds = false;
259 bool hasAssignedIds = false;
Steven Morelandec6f4692019-08-13 10:03:24 -0700260 int newId = kMinUserSetMethodId;
Jiyong Park309668e2018-07-28 16:55:44 +0900261 for (const auto& item : items) {
262 // However, meta transactions that are added by the AIDL compiler are
263 // exceptions. They have fixed IDs but allowed to be with user-defined
264 // methods having auto-assigned IDs. This is because the Ids of the meta
265 // transactions must be stable during the entire lifetime of an interface.
266 // In other words, their IDs must be the same even when new user-defined
267 // methods are added.
Jiyong Park3633b722019-04-11 15:38:26 +0900268 if (!item->IsUserDefined()) {
269 continue;
270 }
271 if (item->HasId()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900272 hasAssignedIds = true;
Jiyong Park309668e2018-07-28 16:55:44 +0900273 } else {
Steven Morelandec6f4692019-08-13 10:03:24 -0700274 item->SetId(newId++);
Jiyong Park309668e2018-07-28 16:55:44 +0900275 hasUnassignedIds = true;
276 }
Steven Morelandec6f4692019-08-13 10:03:24 -0700277
Jiyong Park309668e2018-07-28 16:55:44 +0900278 if (hasAssignedIds && hasUnassignedIds) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900279 AIDL_ERROR(item) << "You must either assign id's to all methods or to none of them.";
Jiyong Park309668e2018-07-28 16:55:44 +0900280 return false;
281 }
Jiyong Park309668e2018-07-28 16:55:44 +0900282
Steven Morelandec6f4692019-08-13 10:03:24 -0700283 // Ensure that the user set id is not duplicated.
284 if (usedIds.find(item->GetId()) != usedIds.end()) {
285 // We found a duplicate id, so throw an error.
286 AIDL_ERROR(item) << "Found duplicate method id (" << item->GetId() << ") for method "
287 << item->GetName();
288 return false;
289 }
290 usedIds.insert(item->GetId());
291
292 // Ensure that the user set id is within the appropriate limits
293 if (item->GetId() < kMinUserSetMethodId || item->GetId() > kMaxUserSetMethodId) {
294 AIDL_ERROR(item) << "Found out of bounds id (" << item->GetId() << ") for method "
295 << item->GetName() << ". Value for id must be between "
296 << kMinUserSetMethodId << " and " << kMaxUserSetMethodId << " inclusive.";
297 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800298 }
Jiyong Park309668e2018-07-28 16:55:44 +0900299 }
Steven Morelandec6f4692019-08-13 10:03:24 -0700300
Jiyong Park309668e2018-07-28 16:55:44 +0900301 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800302}
303
Christopher Wileyef140932015-11-03 09:29:19 -0800304// TODO: Remove this in favor of using the YACC parser b/25479378
Jiyong Park18132182020-06-08 20:24:40 +0900305bool ParsePreprocessedLine(const string& line, string* decl, std::string* package,
306 string* class_name) {
Christopher Wileyef140932015-11-03 09:29:19 -0800307 // erase all trailing whitespace and semicolons
308 const size_t end = line.find_last_not_of(" ;\t");
309 if (end == string::npos) {
310 return false;
311 }
312 if (line.rfind(';', end) != string::npos) {
313 return false;
314 }
315
316 decl->clear();
317 string type;
318 vector<string> pieces = Split(line.substr(0, end + 1), " \t");
319 for (const string& piece : pieces) {
320 if (piece.empty()) {
321 continue;
322 }
323 if (decl->empty()) {
324 *decl = std::move(piece);
325 } else if (type.empty()) {
326 type = std::move(piece);
327 } else {
328 return false;
329 }
330 }
331
332 // Note that this logic is absolutely wrong. Given a parcelable
333 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
334 // the class is just Bar. However, this was the way it was done in the past.
335 //
336 // See b/17415692
337 size_t dot_pos = type.rfind('.');
338 if (dot_pos != string::npos) {
339 *class_name = type.substr(dot_pos + 1);
Jiyong Park18132182020-06-08 20:24:40 +0900340 *package = type.substr(0, dot_pos);
Christopher Wileyef140932015-11-03 09:29:19 -0800341 } else {
342 *class_name = type;
343 package->clear();
344 }
345
346 return true;
347}
348
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700349} // namespace
350
351namespace internals {
352
Jiyong Park1deecc32018-07-17 01:14:41 +0900353bool parse_preprocessed_file(const IoDelegate& io_delegate, const string& filename,
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900354 AidlTypenames* typenames) {
Christopher Wileyef140932015-11-03 09:29:19 -0800355 bool success = true;
356 unique_ptr<LineReader> line_reader = io_delegate.GetLineReader(filename);
357 if (!line_reader) {
Steven Moreland21780812020-09-11 01:29:45 +0000358 AIDL_ERROR(filename) << "cannot open preprocessed file";
Christopher Wileyef140932015-11-03 09:29:19 -0800359 success = false;
360 return success;
361 }
362
363 string line;
Dan Willemsen609ba6d2019-12-30 10:44:00 -0800364 int lineno = 1;
Christopher Wileyef140932015-11-03 09:29:19 -0800365 for ( ; line_reader->ReadLine(&line); ++lineno) {
366 if (line.empty() || line.compare(0, 2, "//") == 0) {
367 // skip comments and empty lines
368 continue;
369 }
370
371 string decl;
Jiyong Park18132182020-06-08 20:24:40 +0900372 std::string package;
Christopher Wileyef140932015-11-03 09:29:19 -0800373 string class_name;
374 if (!ParsePreprocessedLine(line, &decl, &package, &class_name)) {
375 success = false;
376 break;
377 }
378
Steven Moreland46e9da82018-07-27 15:45:29 -0700379 AidlLocation::Point point = {.line = lineno, .column = 0 /*column*/};
Devin Mooredf93ebb2020-03-25 14:03:35 -0700380 AidlLocation location = AidlLocation(filename, point, point, AidlLocation::Source::EXTERNAL);
Steven Moreland46e9da82018-07-27 15:45:29 -0700381
Christopher Wileyef140932015-11-03 09:29:19 -0800382 if (decl == "parcelable") {
Jeongik Chace58bc62019-04-22 13:30:39 +0900383 // ParcelFileDescriptor is treated as a built-in type, but it's also in the framework.aidl.
384 // So aidl should ignore built-in types in framework.aidl to prevent duplication.
385 // (b/130899491)
386 if (AidlTypenames::IsBuiltinTypename(class_name)) {
387 continue;
388 }
Jiyong Park18132182020-06-08 20:24:40 +0900389 AidlParcelable* doc = new AidlParcelable(location, class_name, package, "" /* comments */);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900390 typenames->AddPreprocessedType(unique_ptr<AidlParcelable>(doc));
Steven Morelanded83a282018-07-17 13:27:29 -0700391 } else if (decl == "structured_parcelable") {
Devin Moore53fc99c2020-08-12 08:07:52 -0700392 AidlStructuredParcelable* doc = new AidlStructuredParcelable(
Jooyung Han829ec7c2020-12-02 12:07:36 +0900393 location, class_name, package, "" /* comments */, nullptr, nullptr);
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") {
Jooyung Han829ec7c2020-12-02 12:07:36 +0900396 AidlInterface* doc = new AidlInterface(location, class_name, "", false, package, nullptr);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900397 typenames->AddPreprocessedType(unique_ptr<AidlInterface>(doc));
Christopher Wileyef140932015-11-03 09:29:19 -0800398 } else {
399 success = false;
400 break;
401 }
402 }
403 if (!success) {
Steven Moreland21780812020-09-11 01:29:45 +0000404 AIDL_ERROR(filename) << " on line " << lineno << " malformed preprocessed file line: '" << line
405 << "'";
Christopher Wileyef140932015-11-03 09:29:19 -0800406 }
407
408 return success;
409}
410
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900411AidlError load_and_validate_aidl(const std::string& input_file_name, const Options& options,
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900412 const IoDelegate& io_delegate, AidlTypenames* typenames,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900413 vector<string>* imported_files) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800414 AidlError err = AidlError::OK;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800415
Jiyong Parkb034bf02018-07-30 17:44:33 +0900416 //////////////////////////////////////////////////////////////////////////
417 // Loading phase
418 //////////////////////////////////////////////////////////////////////////
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900419
Jiyong Parkb034bf02018-07-30 17:44:33 +0900420 // Parse the main input file
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900421 std::unique_ptr<Parser> main_parser = Parser::Parse(input_file_name, io_delegate, *typenames);
Steven Moreland64e29be2018-08-08 18:52:19 -0700422 if (main_parser == nullptr) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900423 return AidlError::PARSE_ERROR;
424 }
Jooyung Han2946afc2020-10-05 20:29:16 +0900425 int num_top_level_decls = 0;
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900426 for (const auto& type : main_parser->ParsedDocument().DefinedTypes()) {
Jooyung Han2946afc2020-10-05 20:29:16 +0900427 if (type->AsUnstructuredParcelable() == nullptr) {
428 num_top_level_decls++;
429 if (num_top_level_decls > 1) {
Devin Moore5de18ed2020-04-02 13:52:29 -0700430 AIDL_ERROR(*type) << "You must declare only one type per file.";
431 return AidlError::BAD_TYPE;
432 }
Jiyong Parkda8c6932019-08-12 19:56:08 +0900433 }
434 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900435
436 // Import the preprocessed file
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900437 for (const string& s : options.PreprocessedFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900438 if (!parse_preprocessed_file(io_delegate, s, typenames)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800439 err = AidlError::BAD_PRE_PROCESSED_FILE;
Christopher Wileyef140932015-11-03 09:29:19 -0800440 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700441 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800442 if (err != AidlError::OK) {
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700443 return err;
444 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800445
Jiyong Parkb034bf02018-07-30 17:44:33 +0900446 // Find files to import and parse them
Jiyong Parke59c3682018-09-11 23:10:25 +0900447 vector<string> import_paths;
Jiyong Park8c380532018-08-30 14:55:26 +0900448 ImportResolver import_resolver{io_delegate, input_file_name, options.ImportDirs(),
449 options.InputFiles()};
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900450 for (const auto& import : main_parser->ParsedDocument().Imports()) {
Jooyung Han29813842020-12-08 01:28:03 +0900451 if (AidlTypenames::IsBuiltinTypename(import->GetNeededClass())) {
452 continue;
Jiyong Parke05195e2018-10-08 18:24:23 +0900453 }
Jooyung Han29813842020-12-08 01:28:03 +0900454 if (typenames->IsIgnorableImport(import->GetNeededClass())) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700455 // There are places in the Android tree where an import doesn't resolve,
456 // but we'll pick the type up through the preprocessed types.
457 // This seems like an error, but legacy support demands we support it...
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700458 continue;
459 }
Jooyung Han29813842020-12-08 01:28:03 +0900460 string import_path = import_resolver.FindImportFile(import->GetNeededClass());
Christopher Wiley72877ac2015-10-06 14:41:42 -0700461 if (import_path.empty()) {
Jooyung Han29813842020-12-08 01:28:03 +0900462 if (typenames->ResolveTypename(import->GetNeededClass()).is_resolved) {
463 // This could happen when the type is from the preprocessed aidl file.
464 // In that case, use the type from preprocessed aidl file
Jiyong Park8f6ec462020-01-19 20:52:47 +0900465 continue;
466 }
Jooyung Han29813842020-12-08 01:28:03 +0900467 AIDL_ERROR(input_file_name) << "Couldn't find import for class " << import->GetNeededClass();
468 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700469 continue;
470 }
Casey Dahlin2cc93162015-10-02 16:14:17 -0700471
Jiyong Parke59c3682018-09-11 23:10:25 +0900472 import_paths.emplace_back(import_path);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900473
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900474 std::unique_ptr<Parser> import_parser = Parser::Parse(import_path, io_delegate, *typenames);
Steven Moreland64e29be2018-08-08 18:52:19 -0700475 if (import_parser == nullptr) {
Devin Moore2a088902020-09-17 10:51:19 -0700476 AIDL_ERROR(import_path) << "error while importing " << import_path << " for " << import;
Christopher Wiley632801d2015-11-05 14:15:49 -0800477 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700478 continue;
479 }
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700480 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800481 if (err != AidlError::OK) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700482 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700483 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800484
Jiyong Park3c35e392018-08-30 13:10:30 +0900485 for (const auto& imported_file : options.ImportFiles()) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900486 import_paths.emplace_back(imported_file);
Jiyong Park3c35e392018-08-30 13:10:30 +0900487
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900488 std::unique_ptr<Parser> import_parser = Parser::Parse(imported_file, io_delegate, *typenames);
Jiyong Park3c35e392018-08-30 13:10:30 +0900489 if (import_parser == nullptr) {
490 AIDL_ERROR(imported_file) << "error while importing " << imported_file;
491 err = AidlError::BAD_IMPORT;
492 continue;
493 }
Jiyong Park3c35e392018-08-30 13:10:30 +0900494 }
495 if (err != AidlError::OK) {
496 return err;
497 }
Jooyung Han29813842020-12-08 01:28:03 +0900498
499 TypeResolver resolver = [&](const AidlDocument* doc, AidlTypeSpecifier* type) {
500 if (type->Resolve(*typenames)) return true;
501
502 const string unresolved_name = type->GetUnresolvedName();
503 const std::optional<string> canonical_name = doc->ResolveName(unresolved_name);
504 if (!canonical_name) {
505 return false;
506 }
507 const string import_path = import_resolver.FindImportFile(*canonical_name);
508 if (import_path.empty()) {
509 return false;
510 }
511 import_paths.push_back(import_path);
512
513 std::unique_ptr<Parser> import_parser = Parser::Parse(import_path, io_delegate, *typenames);
514 if (import_parser == nullptr) {
515 AIDL_ERROR(import_path) << "error while importing " << import_path << " for " << import_path;
516 return false;
517 }
518 if (!type->Resolve(*typenames)) {
519 AIDL_ERROR(type) << "Can't resolve " << type->GetName();
520 return false;
521 }
522 return true;
523 };
Jiyong Park96c16a92018-08-16 16:37:09 +0900524 const bool is_check_api = options.GetTask() == Options::Task::CHECK_API;
Jooyung Hane87cdd02020-12-11 16:47:35 +0900525 const bool is_dump_api = options.GetTask() == Options::Task::DUMP_API;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900526
527 // Resolve the unresolved type references found from the input file
Jooyung Han29813842020-12-08 01:28:03 +0900528 if (!is_check_api && !main_parser->Resolve(resolver)) {
Jiyong Park96c16a92018-08-16 16:37:09 +0900529 // Resolution is not need for check api because all typespecs are
530 // using fully qualified names.
Jiyong Park1deecc32018-07-17 01:14:41 +0900531 return AidlError::BAD_TYPE;
532 }
Daniel Norman85aed542019-08-21 12:01:14 -0700533
534 typenames->IterateTypes([&](const AidlDefinedType& type) {
535 AidlEnumDeclaration* enum_decl = const_cast<AidlEnumDeclaration*>(type.AsEnumDeclaration());
536 if (enum_decl != nullptr) {
537 // BackingType is filled in for all known enums, including imported enums,
538 // because other types that may use enums, such as Interface or
539 // StructuredParcelable, need to know the enum BackingType when
540 // generating code.
Daniel Norman716d3112019-09-10 13:11:56 -0700541 if (auto backing_type = enum_decl->BackingType(*typenames); backing_type != nullptr) {
Daniel Norman85aed542019-08-21 12:01:14 -0700542 enum_decl->SetBackingType(std::unique_ptr<const AidlTypeSpecifier>(backing_type));
543 } else {
544 // Default to byte type for enums.
Daniel Norman716d3112019-09-10 13:11:56 -0700545 auto byte_type =
546 std::make_unique<AidlTypeSpecifier>(AIDL_LOCATION_HERE, "byte", false, nullptr, "");
547 byte_type->Resolve(*typenames);
548 enum_decl->SetBackingType(std::move(byte_type));
Daniel Norman85aed542019-08-21 12:01:14 -0700549 }
Daniel Norman85aed542019-08-21 12:01:14 -0700550 }
551 });
Steven Moreland59e53e42019-11-26 20:38:08 -0800552 if (err != AidlError::OK) {
553 return err;
554 }
Daniel Norman85aed542019-08-21 12:01:14 -0700555
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900556 //////////////////////////////////////////////////////////////////////////
557 // Validation phase
558 //////////////////////////////////////////////////////////////////////////
559
Steven Morelande2c64b42018-09-18 15:06:37 -0700560 // For legacy reasons, by default, compiling an unstructured parcelable (which contains no output)
561 // is allowed. This must not be returned as an error until the very end of this procedure since
562 // this may be considered a success, and we should first check that there are not other, more
563 // serious failures.
564 bool contains_unstructured_parcelable = false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800565
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900566 const auto& types = main_parser->ParsedDocument().DefinedTypes();
Jiyong Park62515512020-06-08 15:57:11 +0900567 const int num_defined_types = types.size();
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900568 for (const auto& defined_type : types) {
Steven Moreland21780812020-09-11 01:29:45 +0000569 AIDL_FATAL_IF(defined_type == nullptr, main_parser->FileName());
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900570
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000571 // Ensure type is exactly one of the following:
572 AidlInterface* interface = defined_type->AsInterface();
573 AidlStructuredParcelable* parcelable = defined_type->AsStructuredParcelable();
574 AidlParcelable* unstructured_parcelable = defined_type->AsUnstructuredParcelable();
575 AidlEnumDeclaration* enum_decl = defined_type->AsEnumDeclaration();
Jooyung Han2946afc2020-10-05 20:29:16 +0900576 AidlUnionDecl* union_decl = defined_type->AsUnionDeclaration();
577 AIDL_FATAL_IF(
578 !!interface + !!parcelable + !!unstructured_parcelable + !!enum_decl + !!union_decl != 1,
579 defined_type);
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000580
581 // Ensure that foo.bar.IFoo is defined in <some_path>/foo/bar/IFoo.aidl
582 if (num_defined_types == 1 && !check_filename(input_file_name, *defined_type)) {
583 return AidlError::BAD_PACKAGE;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900584 }
585
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000586 {
587 bool valid_type = true;
588
589 if (!is_check_api) {
590 // Ideally, we could do this for check api, but we can't resolve imports
591 if (!defined_type->CheckValid(*typenames)) {
592 valid_type = false;
593 }
594 }
595
Jooyung Hane87cdd02020-12-11 16:47:35 +0900596 if (!is_dump_api && !is_check_api) {
597 if (!defined_type->LanguageSpecificCheckValid(*typenames, options.TargetLanguage())) {
598 valid_type = false;
599 }
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000600 }
601
602 if (!valid_type) {
Jeongik Cha82317dd2019-02-27 20:26:11 +0900603 return AidlError::BAD_TYPE;
604 }
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000605 }
606
607 if (unstructured_parcelable != nullptr) {
608 bool isStable = unstructured_parcelable->IsStableApiParcelable(options.TargetLanguage());
Jeongik Cha82317dd2019-02-27 20:26:11 +0900609 if (options.IsStructured() && !isStable) {
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000610 AIDL_ERROR(unstructured_parcelable)
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800611 << "Cannot declared parcelable in a --structured interface. Parcelable must be defined "
612 "in AIDL directly.";
613 return AidlError::NOT_STRUCTURED;
614 }
615 if (options.FailOnParcelable()) {
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000616 AIDL_ERROR(unstructured_parcelable)
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800617 << "Refusing to generate code with unstructured parcelables. Declared parcelables "
618 "should be in their own file and/or cannot be used with --structured interfaces.";
619 // Continue parsing for more errors
620 }
621
Steven Morelande2c64b42018-09-18 15:06:37 -0700622 contains_unstructured_parcelable = true;
Steven Morelande2c64b42018-09-18 15:06:37 -0700623 }
624
Devin Moore0d0e3f62020-03-30 17:45:39 -0700625 if (defined_type->IsVintfStability()) {
626 bool success = true;
627 if (options.GetStability() != Options::Stability::VINTF) {
628 AIDL_ERROR(defined_type)
629 << "Must compile @VintfStability type w/ aidl_interface 'stability: \"vintf\"'";
630 success = false;
631 }
632 if (!options.IsStructured()) {
633 AIDL_ERROR(defined_type)
634 << "Must compile @VintfStability type w/ aidl_interface --structured";
635 success = false;
636 }
637 if (!success) return AidlError::NOT_STRUCTURED;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700638 }
639
Jiyong Parkb034bf02018-07-30 17:44:33 +0900640 if (interface != nullptr) {
641 // add the meta-method 'int getInterfaceVersion()' if version is specified.
642 if (options.Version() > 0) {
643 AidlTypeSpecifier* ret =
Steven Moreland02e012e2018-08-02 14:58:10 -0700644 new AidlTypeSpecifier(AIDL_LOCATION_HERE, "int", false, nullptr, "");
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900645 ret->Resolve(*typenames);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900646 vector<unique_ptr<AidlArgument>>* args = new vector<unique_ptr<AidlArgument>>();
Jooyung Han3f347ca2020-12-01 12:41:50 +0900647 auto method = std::make_unique<AidlMethod>(
648 AIDL_LOCATION_HERE, false, ret, "getInterfaceVersion", args, "", kGetInterfaceVersionId,
649 false /* is_user_defined */);
650 interface->AddMethod(std::move(method));
Jiyong Parkb034bf02018-07-30 17:44:33 +0900651 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900652 // add the meta-method 'string getInterfaceHash()' if hash is specified.
653 if (!options.Hash().empty()) {
654 AidlTypeSpecifier* ret =
655 new AidlTypeSpecifier(AIDL_LOCATION_HERE, "String", false, nullptr, "");
656 ret->Resolve(*typenames);
657 vector<unique_ptr<AidlArgument>>* args = new vector<unique_ptr<AidlArgument>>();
Jooyung Han3f347ca2020-12-01 12:41:50 +0900658 auto method =
659 std::make_unique<AidlMethod>(AIDL_LOCATION_HERE, false, ret, kGetInterfaceHash, args,
660 "", kGetInterfaceHashId, false /* is_user_defined */);
661 interface->AddMethod(std::move(method));
Paul Trautrimb77048c2020-01-21 16:39:32 +0900662 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900663 if (!check_and_assign_method_ids(interface->GetMethods())) {
664 return AidlError::BAD_METHOD_ID;
665 }
Jooyung Hancfe08002020-12-04 12:56:35 +0900666 }
Jooyung Han30f64ad2020-12-15 08:16:31 +0900667 // Verify the var/const declarations.
668 // const expressions should be non-empty when evaluated with the var/const type.
669 if (!is_check_api) {
670 for (const auto& constant : defined_type->GetConstantDeclarations()) {
671 if (constant->ValueString(AidlConstantValueDecorator).empty()) {
672 return AidlError::BAD_TYPE;
Will McVickerd7d18df2019-09-12 13:40:50 -0700673 }
Jooyung Han30f64ad2020-12-15 08:16:31 +0900674 }
675 for (const auto& var : defined_type->GetFields()) {
676 if (var->GetDefaultValue() && var->ValueString(AidlConstantValueDecorator).empty()) {
677 return AidlError::BAD_TYPE;
678 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700679 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900680 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800681 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800682
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900683 typenames->IterateTypes([&](const AidlDefinedType& type) {
Steven Morelanda57d0a62019-07-30 09:41:14 -0700684 if (options.IsStructured() && type.AsUnstructuredParcelable() != nullptr &&
Jeongik Cha88f95a82020-01-15 13:02:16 +0900685 !type.AsUnstructuredParcelable()->IsStableApiParcelable(options.TargetLanguage())) {
Steven Morelanda57d0a62019-07-30 09:41:14 -0700686 err = AidlError::NOT_STRUCTURED;
Devin Moore097a3ab2020-03-11 16:08:44 -0700687 AIDL_ERROR(type) << type.GetCanonicalName()
688 << " is not structured, but this is a structured interface.";
Steven Morelanda57d0a62019-07-30 09:41:14 -0700689 }
690 if (options.GetStability() == Options::Stability::VINTF && !type.IsVintfStability()) {
691 err = AidlError::NOT_STRUCTURED;
Devin Moore097a3ab2020-03-11 16:08:44 -0700692 AIDL_ERROR(type) << type.GetCanonicalName()
693 << " does not have VINTF level stability, but this interface requires it.";
Steven Morelanda57d0a62019-07-30 09:41:14 -0700694 }
Jiyong Parkf8d53612020-05-04 14:06:13 +0900695
Jeongik Chaef44e622020-10-23 16:00:52 +0900696 // Ensure that untyped List/Map is not used in a parcelable, a union and a stable interface.
Jiyong Parkf8d53612020-05-04 14:06:13 +0900697
Jeongik Chaef44e622020-10-23 16:00:52 +0900698 std::function<void(const AidlTypeSpecifier&, const AidlNode*)> check_untyped_container =
699 [&err, &check_untyped_container](const AidlTypeSpecifier& type, const AidlNode* node) {
700 if (type.IsGeneric()) {
701 std::for_each(type.GetTypeParameters().begin(), type.GetTypeParameters().end(),
702 [&node, &check_untyped_container](auto& nested) {
703 check_untyped_container(*nested, node);
704 });
705 return;
Jiyong Parkf8d53612020-05-04 14:06:13 +0900706 }
Jeongik Chaef44e622020-10-23 16:00:52 +0900707 if (type.GetName() == "List" || type.GetName() == "Map") {
708 err = AidlError::BAD_TYPE;
709 AIDL_ERROR(node)
710 << "Encountered an untyped List or Map. The use of untyped List/Map is prohibited "
711 << "because it is not guaranteed that the objects in the list are recognizable in "
712 << "the receiving side. Consider switching to an array or a generic List/Map.";
713 }
714 };
Jeongik Chaef44e622020-10-23 16:00:52 +0900715
Jooyung Han829ec7c2020-12-02 12:07:36 +0900716 if (type.AsInterface() && options.IsStructured()) {
717 for (const auto& method : type.GetMethods()) {
Jeongik Chaef44e622020-10-23 16:00:52 +0900718 check_untyped_container(method->GetType(), method.get());
719 for (const auto& arg : method->GetArguments()) {
720 check_untyped_container(arg->GetType(), method.get());
Jiyong Parkf8d53612020-05-04 14:06:13 +0900721 }
Jeongik Chaef44e622020-10-23 16:00:52 +0900722 }
Jooyung Han829ec7c2020-12-02 12:07:36 +0900723 }
724 for (const auto& field : type.GetFields()) {
725 check_untyped_container(field->GetType(), field.get());
Jiyong Parkf8d53612020-05-04 14:06:13 +0900726 }
Steven Morelanda57d0a62019-07-30 09:41:14 -0700727 });
728
Steven Moreland6cee3482018-07-18 14:39:58 -0700729 if (err != AidlError::OK) {
730 return err;
731 }
732
Jiyong Parkb034bf02018-07-30 17:44:33 +0900733 if (imported_files != nullptr) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900734 *imported_files = import_paths;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700735 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700736
Steven Morelande2c64b42018-09-18 15:06:37 -0700737 if (contains_unstructured_parcelable) {
738 // Considered a success for the legacy case, so this must be returned last.
739 return AidlError::FOUND_PARCELABLE;
740 }
741
Christopher Wiley632801d2015-11-05 14:15:49 -0800742 return AidlError::OK;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700743}
744
Casey Dahlin2cc93162015-10-02 16:14:17 -0700745} // namespace internals
746
Jiyong Parkb034bf02018-07-30 17:44:33 +0900747int compile_aidl(const Options& options, const IoDelegate& io_delegate) {
748 const Options::Language lang = options.TargetLanguage();
Jiyong Park74595c12018-07-23 15:22:50 +0900749 for (const string& input_file : options.InputFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900750 AidlTypenames typenames;
Jiyong Park74595c12018-07-23 15:22:50 +0900751
Jiyong Parkb034bf02018-07-30 17:44:33 +0900752 vector<string> imported_files;
Jiyong Park74595c12018-07-23 15:22:50 +0900753
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900754 AidlError aidl_err = internals::load_and_validate_aidl(input_file, options, io_delegate,
755 &typenames, &imported_files);
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800756 bool allowError = aidl_err == AidlError::FOUND_PARCELABLE && !options.FailOnParcelable();
757 if (aidl_err != AidlError::OK && !allowError) {
Jiyong Park74595c12018-07-23 15:22:50 +0900758 return 1;
759 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700760
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900761 for (const auto& defined_type : typenames.MainDocument().DefinedTypes()) {
Steven Moreland21780812020-09-11 01:29:45 +0000762 AIDL_FATAL_IF(defined_type == nullptr, input_file);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700763
Jiyong Parkb034bf02018-07-30 17:44:33 +0900764 string output_file_name = options.OutputFile();
765 // if needed, generate the output file name from the base folder
766 if (output_file_name.empty() && !options.OutputDir().empty()) {
Jiyong Parkb03551f2018-08-06 19:20:51 +0900767 output_file_name = generate_outputFileName(options, *defined_type);
768 if (output_file_name.empty()) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900769 return 1;
770 }
771 }
Jiyong Park74595c12018-07-23 15:22:50 +0900772
Jiyong Parkb034bf02018-07-30 17:44:33 +0900773 if (!write_dep_file(options, *defined_type, imported_files, io_delegate, input_file,
774 output_file_name)) {
775 return 1;
776 }
Jiyong Park74595c12018-07-23 15:22:50 +0900777
Jiyong Parkb034bf02018-07-30 17:44:33 +0900778 bool success = false;
779 if (lang == Options::Language::CPP) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900780 success =
781 cpp::GenerateCpp(output_file_name, options, typenames, *defined_type, io_delegate);
Steven Morelandc26d8142018-09-17 14:25:33 -0700782 } else if (lang == Options::Language::NDK) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900783 ndk::GenerateNdk(output_file_name, options, typenames, *defined_type, io_delegate);
Steven Morelandc26d8142018-09-17 14:25:33 -0700784 success = true;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900785 } else if (lang == Options::Language::JAVA) {
Jiyong Park9ca5c7e2019-10-17 15:01:14 +0900786 if (defined_type->AsUnstructuredParcelable() != nullptr) {
787 // Legacy behavior. For parcelable declarations in Java, don't generate output file.
788 success = true;
789 } else {
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900790 success = java::generate_java(output_file_name, defined_type.get(), typenames,
791 io_delegate, options);
Jiyong Park9ca5c7e2019-10-17 15:01:14 +0900792 }
Andrei Homescub62afd92020-05-11 19:24:59 -0700793 } else if (lang == Options::Language::RUST) {
794 success = rust::GenerateRust(output_file_name, defined_type.get(), typenames, io_delegate,
795 options);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900796 } else {
Steven Moreland21780812020-09-11 01:29:45 +0000797 AIDL_FATAL(input_file) << "Should not reach here.";
Jiyong Parkb034bf02018-07-30 17:44:33 +0900798 }
799 if (!success) {
800 return 1;
801 }
Jiyong Park74595c12018-07-23 15:22:50 +0900802 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700803 }
Jiyong Park74595c12018-07-23 15:22:50 +0900804 return 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800805}
806
Andrei Onea8714b022019-02-01 18:55:54 +0000807bool dump_mappings(const Options& options, const IoDelegate& io_delegate) {
808 android::aidl::mappings::SignatureMap all_mappings;
809 for (const string& input_file : options.InputFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900810 AidlTypenames typenames;
Andrei Onea8714b022019-02-01 18:55:54 +0000811 vector<string> imported_files;
812
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900813 AidlError aidl_err = internals::load_and_validate_aidl(input_file, options, io_delegate,
814 &typenames, &imported_files);
Andrei Onea8714b022019-02-01 18:55:54 +0000815 if (aidl_err != AidlError::OK) {
Jiyong Park3a060392020-04-11 21:02:19 +0900816 return false;
Andrei Onea8714b022019-02-01 18:55:54 +0000817 }
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900818 for (const auto& defined_type : typenames.MainDocument().DefinedTypes()) {
819 auto mappings = mappings::generate_mappings(defined_type.get(), typenames);
Andrei Onea8714b022019-02-01 18:55:54 +0000820 all_mappings.insert(mappings.begin(), mappings.end());
821 }
822 }
823 std::stringstream mappings_str;
824 for (const auto& mapping : all_mappings) {
825 mappings_str << mapping.first << "\n" << mapping.second << "\n";
826 }
827 auto code_writer = io_delegate.GetCodeWriter(options.OutputFile());
828 code_writer->Write("%s", mappings_str.str().c_str());
829 return true;
830}
831
Jiyong Park74595c12018-07-23 15:22:50 +0900832bool preprocess_aidl(const Options& options, const IoDelegate& io_delegate) {
833 unique_ptr<CodeWriter> writer = io_delegate.GetCodeWriter(options.OutputFile());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800834
Jiyong Park74595c12018-07-23 15:22:50 +0900835 for (const auto& file : options.InputFiles()) {
Jiyong Park1deecc32018-07-17 01:14:41 +0900836 AidlTypenames typenames;
Steven Moreland64e29be2018-08-08 18:52:19 -0700837 std::unique_ptr<Parser> p = Parser::Parse(file, io_delegate, typenames);
838 if (p == nullptr) return false;
Casey Dahlin59401da2015-10-09 18:16:45 -0700839
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900840 for (const auto& defined_type : p->ParsedDocument().DefinedTypes()) {
Steven Morelanded83a282018-07-17 13:27:29 -0700841 if (!writer->Write("%s %s;\n", defined_type->GetPreprocessDeclarationName().c_str(),
Steven Morelandc258abc2018-07-10 14:03:38 -0700842 defined_type->GetCanonicalName().c_str())) {
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800843 return false;
844 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800845 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800846 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800847
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800848 return writer->Close();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800849}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700850
Jiyong Parke59c3682018-09-11 23:10:25 +0900851static string GetApiDumpPathFor(const AidlDefinedType& defined_type, const Options& options) {
852 string package_as_path = Join(Split(defined_type.GetPackage(), "."), OS_PATH_SEPARATOR);
Steven Moreland21780812020-09-11 01:29:45 +0000853 AIDL_FATAL_IF(options.OutputDir().empty() || options.OutputDir().back() != '/', defined_type);
Jiyong Parke59c3682018-09-11 23:10:25 +0900854 return options.OutputDir() + package_as_path + OS_PATH_SEPARATOR + defined_type.GetName() +
855 ".aidl";
856}
Jiyong Parkb034bf02018-07-30 17:44:33 +0900857
Jiyong Parke59c3682018-09-11 23:10:25 +0900858bool dump_api(const Options& options, const IoDelegate& io_delegate) {
Jiyong Park74595c12018-07-23 15:22:50 +0900859 for (const auto& file : options.InputFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900860 AidlTypenames typenames;
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900861 if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames, nullptr) ==
862 AidlError::OK) {
863 for (const auto& type : typenames.MainDocument().DefinedTypes()) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900864 unique_ptr<CodeWriter> writer =
865 io_delegate.GetCodeWriter(GetApiDumpPathFor(*type, options));
Steven Morelandec0531d2018-09-20 11:11:20 -0700866 if (!type->GetPackage().empty()) {
Paul Trautrimb01451d2020-02-27 13:10:16 +0900867 (*writer) << kPreamble << "package " << type->GetPackage() << ";\n";
Steven Morelandec0531d2018-09-20 11:11:20 -0700868 }
Jeongik Cha997281d2020-01-16 15:23:59 +0900869 type->Dump(writer.get());
Jiyong Parkb034bf02018-07-30 17:44:33 +0900870 }
Jiyong Park02da7422018-07-16 16:00:26 +0900871 } else {
872 return false;
873 }
874 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900875 return true;
Jiyong Park02da7422018-07-16 16:00:26 +0900876}
877
Steven Moreland3981d9e2020-03-31 14:11:44 -0700878int aidl_entry(const Options& options, const IoDelegate& io_delegate) {
Steven Moreland33efcf62020-04-10 16:40:43 -0700879 AidlErrorLog::clearError();
880
Steven Moreland3981d9e2020-03-31 14:11:44 -0700881 int ret = 1;
882 switch (options.GetTask()) {
883 case Options::Task::COMPILE:
884 ret = android::aidl::compile_aidl(options, io_delegate);
885 break;
886 case Options::Task::PREPROCESS:
887 ret = android::aidl::preprocess_aidl(options, io_delegate) ? 0 : 1;
888 break;
889 case Options::Task::DUMP_API:
890 ret = android::aidl::dump_api(options, io_delegate) ? 0 : 1;
891 break;
892 case Options::Task::CHECK_API:
893 ret = android::aidl::check_api(options, io_delegate) ? 0 : 1;
894 break;
895 case Options::Task::DUMP_MAPPINGS:
896 ret = android::aidl::dump_mappings(options, io_delegate) ? 0 : 1;
897 break;
898 default:
899 AIDL_FATAL(AIDL_LOCATION_HERE)
900 << "Unrecognized task: " << static_cast<size_t>(options.GetTask());
901 }
902
903 // compiler invariants
Steven Moreland21780812020-09-11 01:29:45 +0000904 const bool shouldReportError = ret != 0;
905 const bool reportedError = AidlErrorLog::hadError();
906 AIDL_FATAL_IF(shouldReportError != reportedError, AIDL_LOCATION_HERE)
907 << "Compiler returned error " << ret << " but did" << (reportedError ? "" : " not")
908 << " emit error logs";
Steven Moreland3981d9e2020-03-31 14:11:44 -0700909
910 return ret;
911}
912
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700913} // namespace aidl
Steven Morelandf4c64df2019-07-29 19:54:04 -0700914} // namespace android