blob: 77c5f31966d5e0efcd3984ed28cc3227318076a2 [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"
Jooyung Han1f56b702021-02-11 13:16:15 +090040#include "aidl_dumpapi.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070041#include "aidl_language.h"
Jiyong Parke05195e2018-10-08 18:24:23 +090042#include "aidl_typenames.h"
Andrei Onea8714b022019-02-01 18:55:54 +000043#include "generate_aidl_mappings.h"
Christopher Wileyeb1acc12015-09-16 11:25:13 -070044#include "generate_cpp.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070045#include "generate_java.h"
Steven Morelandc26d8142018-09-17 14:25:33 -070046#include "generate_ndk.h"
Andrei Homescub62afd92020-05-11 19:24:59 -070047#include "generate_rust.h"
Christopher Wiley72877ac2015-10-06 14:41:42 -070048#include "import_resolver.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070049#include "logging.h"
50#include "options.h"
51#include "os.h"
Jiyong Parke5c45292020-05-26 19:06:24 +090052#include "parser.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070053
Adam Lesinskiffa16862014-01-23 18:17:42 -080054#ifndef O_BINARY
55# define O_BINARY 0
56#endif
57
Christopher Wiley3a9911c2016-01-19 12:59:09 -080058using android::base::Join;
Christopher Wileyd76067c2015-10-19 17:00:13 -070059using android::base::Split;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070060using std::set;
61using std::string;
Christopher Wiley84c1eac2015-09-23 13:29:28 -070062using std::unique_ptr;
Jooyung Hand25527a2020-12-30 12:29:31 +090063using std::unordered_set;
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) {
Steven Moreland21780812020-09-11 01:29:45 +0000170 AIDL_ERROR(dep_file_name) << "Could not open dependency file.";
Jiyong Park74595c12018-07-23 15:22:50 +0900171 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
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900221// Returns the path to the destination file of `defined_type`.
222string GetOutputFilePath(const Options& options, const AidlDefinedType& defined_type) {
Jiyong Park74595c12018-07-23 15:22:50 +0900223 string result = options.OutputDir();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800224
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900225 // add the package
Jiyong Park74595c12018-07-23 15:22:50 +0900226 string package = defined_type.GetPackage();
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900227 if (!package.empty()) {
228 for (auto& c : package) {
229 if (c == '.') {
230 c = OS_PATH_SEPARATOR;
231 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800232 }
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900233 result += package;
234 result += OS_PATH_SEPARATOR;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700235 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800236
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900237 // add the filename
238 result += defined_type.GetName();
Jiyong Parkb03551f2018-08-06 19:20:51 +0900239 if (options.TargetLanguage() == Options::Language::JAVA) {
240 result += ".java";
Steven Morelandc26d8142018-09-17 14:25:33 -0700241 } else if (options.IsCppOutput()) {
Jiyong Parkb03551f2018-08-06 19:20:51 +0900242 result += ".cpp";
Andrei Homescub62afd92020-05-11 19:24:59 -0700243 } else if (options.TargetLanguage() == Options::Language::RUST) {
244 result += ".rs";
Jiyong Parkb03551f2018-08-06 19:20:51 +0900245 } else {
Steven Moreland21780812020-09-11 01:29:45 +0000246 AIDL_FATAL("Unknown target language");
Jiyong Parkb03551f2018-08-06 19:20:51 +0900247 return "";
248 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800249
Steven Moreland5557f1c2018-07-02 13:50:23 -0700250 return result;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800251}
252
Jiyong Parkb034bf02018-07-30 17:44:33 +0900253bool check_and_assign_method_ids(const std::vector<std::unique_ptr<AidlMethod>>& items) {
Jiyong Park309668e2018-07-28 16:55:44 +0900254 // Check whether there are any methods with manually assigned id's and any
255 // that are not. Either all method id's must be manually assigned or all of
256 // them must not. Also, check for uplicates of user set ID's and that the
257 // ID's are within the proper bounds.
258 set<int> usedIds;
259 bool hasUnassignedIds = false;
260 bool hasAssignedIds = false;
Steven Morelandec6f4692019-08-13 10:03:24 -0700261 int newId = kMinUserSetMethodId;
Jiyong Park309668e2018-07-28 16:55:44 +0900262 for (const auto& item : items) {
263 // However, meta transactions that are added by the AIDL compiler are
264 // exceptions. They have fixed IDs but allowed to be with user-defined
265 // methods having auto-assigned IDs. This is because the Ids of the meta
266 // transactions must be stable during the entire lifetime of an interface.
267 // In other words, their IDs must be the same even when new user-defined
268 // methods are added.
Jiyong Park3633b722019-04-11 15:38:26 +0900269 if (!item->IsUserDefined()) {
270 continue;
271 }
272 if (item->HasId()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900273 hasAssignedIds = true;
Jiyong Park309668e2018-07-28 16:55:44 +0900274 } else {
Steven Morelandec6f4692019-08-13 10:03:24 -0700275 item->SetId(newId++);
Jiyong Park309668e2018-07-28 16:55:44 +0900276 hasUnassignedIds = true;
277 }
Steven Morelandec6f4692019-08-13 10:03:24 -0700278
Jiyong Park309668e2018-07-28 16:55:44 +0900279 if (hasAssignedIds && hasUnassignedIds) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900280 AIDL_ERROR(item) << "You must either assign id's to all methods or to none of them.";
Jiyong Park309668e2018-07-28 16:55:44 +0900281 return false;
282 }
Jiyong Park309668e2018-07-28 16:55:44 +0900283
Steven Morelandec6f4692019-08-13 10:03:24 -0700284 // Ensure that the user set id is not duplicated.
285 if (usedIds.find(item->GetId()) != usedIds.end()) {
286 // We found a duplicate id, so throw an error.
287 AIDL_ERROR(item) << "Found duplicate method id (" << item->GetId() << ") for method "
288 << item->GetName();
289 return false;
290 }
291 usedIds.insert(item->GetId());
292
293 // Ensure that the user set id is within the appropriate limits
294 if (item->GetId() < kMinUserSetMethodId || item->GetId() > kMaxUserSetMethodId) {
295 AIDL_ERROR(item) << "Found out of bounds id (" << item->GetId() << ") for method "
296 << item->GetName() << ". Value for id must be between "
297 << kMinUserSetMethodId << " and " << kMaxUserSetMethodId << " inclusive.";
298 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800299 }
Jiyong Park309668e2018-07-28 16:55:44 +0900300 }
Steven Morelandec6f4692019-08-13 10:03:24 -0700301
Jiyong Park309668e2018-07-28 16:55:44 +0900302 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800303}
304
Christopher Wileyef140932015-11-03 09:29:19 -0800305// TODO: Remove this in favor of using the YACC parser b/25479378
Jiyong Park18132182020-06-08 20:24:40 +0900306bool ParsePreprocessedLine(const string& line, string* decl, std::string* package,
307 string* class_name) {
Christopher Wileyef140932015-11-03 09:29:19 -0800308 // erase all trailing whitespace and semicolons
309 const size_t end = line.find_last_not_of(" ;\t");
310 if (end == string::npos) {
311 return false;
312 }
313 if (line.rfind(';', end) != string::npos) {
314 return false;
315 }
316
317 decl->clear();
318 string type;
319 vector<string> pieces = Split(line.substr(0, end + 1), " \t");
320 for (const string& piece : pieces) {
321 if (piece.empty()) {
322 continue;
323 }
324 if (decl->empty()) {
325 *decl = std::move(piece);
326 } else if (type.empty()) {
327 type = std::move(piece);
328 } else {
329 return false;
330 }
331 }
332
333 // Note that this logic is absolutely wrong. Given a parcelable
334 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
335 // the class is just Bar. However, this was the way it was done in the past.
336 //
337 // See b/17415692
338 size_t dot_pos = type.rfind('.');
339 if (dot_pos != string::npos) {
340 *class_name = type.substr(dot_pos + 1);
Jiyong Park18132182020-06-08 20:24:40 +0900341 *package = type.substr(0, dot_pos);
Christopher Wileyef140932015-11-03 09:29:19 -0800342 } else {
343 *class_name = type;
344 package->clear();
345 }
346
347 return true;
348}
349
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900350bool ValidateAnnotationContext(const AidlDocument& doc) {
351 struct AnnotationValidator : AidlVisitor {
352 bool success = true;
353
354 void Check(const AidlAnnotatable& annotatable, AidlAnnotation::TargetContext context) {
355 for (const auto& annot : annotatable.GetAnnotations()) {
356 if (!annot.CheckContext(context)) {
357 success = false;
358 }
359 }
360 }
361 void Visit(const AidlInterface& m) override {
362 Check(m, AidlAnnotation::CONTEXT_TYPE_INTERFACE);
363 }
364 void Visit(const AidlParcelable& m) override {
365 Check(m, AidlAnnotation::CONTEXT_TYPE_UNSTRUCTURED_PARCELABLE);
366 }
367 void Visit(const AidlStructuredParcelable& m) override {
368 Check(m, AidlAnnotation::CONTEXT_TYPE_STRUCTURED_PARCELABLE);
369 }
370 void Visit(const AidlEnumDeclaration& m) override {
371 Check(m, AidlAnnotation::CONTEXT_TYPE_ENUM);
372 }
373 void Visit(const AidlUnionDecl& m) override { Check(m, AidlAnnotation::CONTEXT_TYPE_UNION); }
374 void Visit(const AidlMethod& m) override {
375 Check(m.GetType(), AidlAnnotation::CONTEXT_TYPE_SPECIFIER | AidlAnnotation::CONTEXT_METHOD);
376 for (const auto& arg : m.GetArguments()) {
377 Check(arg->GetType(), AidlAnnotation::CONTEXT_TYPE_SPECIFIER);
378 }
379 }
380 void Visit(const AidlConstantDeclaration& m) override {
381 Check(m.GetType(), AidlAnnotation::CONTEXT_TYPE_SPECIFIER | AidlAnnotation::CONTEXT_CONST);
382 }
383 void Visit(const AidlVariableDeclaration& m) override {
384 Check(m.GetType(), AidlAnnotation::CONTEXT_TYPE_SPECIFIER | AidlAnnotation::CONTEXT_FIELD);
385 }
386 void Visit(const AidlTypeSpecifier& m) override {
387 // nested generic type parameters are checked as well
388 if (m.IsGeneric()) {
389 for (const auto& tp : m.GetTypeParameters()) {
390 Check(*tp, AidlAnnotation::CONTEXT_TYPE_SPECIFIER);
391 }
392 }
393 }
394 };
395
396 AnnotationValidator validator;
397 VisitTopDown(validator, doc);
398 return validator.success;
399}
400
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700401} // namespace
402
403namespace internals {
404
Jiyong Park1deecc32018-07-17 01:14:41 +0900405bool parse_preprocessed_file(const IoDelegate& io_delegate, const string& filename,
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900406 AidlTypenames* typenames) {
Christopher Wileyef140932015-11-03 09:29:19 -0800407 bool success = true;
408 unique_ptr<LineReader> line_reader = io_delegate.GetLineReader(filename);
409 if (!line_reader) {
Steven Moreland21780812020-09-11 01:29:45 +0000410 AIDL_ERROR(filename) << "cannot open preprocessed file";
Christopher Wileyef140932015-11-03 09:29:19 -0800411 success = false;
412 return success;
413 }
414
415 string line;
Dan Willemsen609ba6d2019-12-30 10:44:00 -0800416 int lineno = 1;
Christopher Wileyef140932015-11-03 09:29:19 -0800417 for ( ; line_reader->ReadLine(&line); ++lineno) {
418 if (line.empty() || line.compare(0, 2, "//") == 0) {
419 // skip comments and empty lines
420 continue;
421 }
422
423 string decl;
Jiyong Park18132182020-06-08 20:24:40 +0900424 std::string package;
Christopher Wileyef140932015-11-03 09:29:19 -0800425 string class_name;
426 if (!ParsePreprocessedLine(line, &decl, &package, &class_name)) {
427 success = false;
428 break;
429 }
430
Steven Moreland46e9da82018-07-27 15:45:29 -0700431 AidlLocation::Point point = {.line = lineno, .column = 0 /*column*/};
Devin Mooredf93ebb2020-03-25 14:03:35 -0700432 AidlLocation location = AidlLocation(filename, point, point, AidlLocation::Source::EXTERNAL);
Steven Moreland46e9da82018-07-27 15:45:29 -0700433
Christopher Wileyef140932015-11-03 09:29:19 -0800434 if (decl == "parcelable") {
Jeongik Chace58bc62019-04-22 13:30:39 +0900435 // ParcelFileDescriptor is treated as a built-in type, but it's also in the framework.aidl.
436 // So aidl should ignore built-in types in framework.aidl to prevent duplication.
437 // (b/130899491)
438 if (AidlTypenames::IsBuiltinTypename(class_name)) {
439 continue;
440 }
Jooyung Han8451a202021-01-16 03:07:06 +0900441 AidlParcelable* doc = new AidlParcelable(location, class_name, package, Comments{});
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900442 typenames->AddPreprocessedType(unique_ptr<AidlParcelable>(doc));
Steven Morelanded83a282018-07-17 13:27:29 -0700443 } else if (decl == "structured_parcelable") {
Jooyung Han8451a202021-01-16 03:07:06 +0900444 AidlStructuredParcelable* doc =
445 new AidlStructuredParcelable(location, class_name, package, Comments{}, nullptr, nullptr);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900446 typenames->AddPreprocessedType(unique_ptr<AidlStructuredParcelable>(doc));
Christopher Wileyef140932015-11-03 09:29:19 -0800447 } else if (decl == "interface") {
Jooyung Han8451a202021-01-16 03:07:06 +0900448 AidlInterface* doc =
449 new AidlInterface(location, class_name, Comments{}, false, package, nullptr);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900450 typenames->AddPreprocessedType(unique_ptr<AidlInterface>(doc));
Christopher Wileyef140932015-11-03 09:29:19 -0800451 } else {
452 success = false;
453 break;
454 }
455 }
456 if (!success) {
Steven Moreland21780812020-09-11 01:29:45 +0000457 AIDL_ERROR(filename) << " on line " << lineno << " malformed preprocessed file line: '" << line
458 << "'";
Christopher Wileyef140932015-11-03 09:29:19 -0800459 }
460
461 return success;
462}
463
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900464AidlError load_and_validate_aidl(const std::string& input_file_name, const Options& options,
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900465 const IoDelegate& io_delegate, AidlTypenames* typenames,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900466 vector<string>* imported_files) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800467 AidlError err = AidlError::OK;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800468
Jiyong Parkb034bf02018-07-30 17:44:33 +0900469 //////////////////////////////////////////////////////////////////////////
470 // Loading phase
471 //////////////////////////////////////////////////////////////////////////
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900472
Jiyong Parkb034bf02018-07-30 17:44:33 +0900473 // Parse the main input file
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900474 std::unique_ptr<Parser> main_parser = Parser::Parse(input_file_name, io_delegate, *typenames);
Steven Moreland64e29be2018-08-08 18:52:19 -0700475 if (main_parser == nullptr) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900476 return AidlError::PARSE_ERROR;
477 }
Jooyung Han2946afc2020-10-05 20:29:16 +0900478 int num_top_level_decls = 0;
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900479 for (const auto& type : main_parser->ParsedDocument().DefinedTypes()) {
Jooyung Han2946afc2020-10-05 20:29:16 +0900480 if (type->AsUnstructuredParcelable() == nullptr) {
481 num_top_level_decls++;
482 if (num_top_level_decls > 1) {
Devin Moore5de18ed2020-04-02 13:52:29 -0700483 AIDL_ERROR(*type) << "You must declare only one type per file.";
484 return AidlError::BAD_TYPE;
485 }
Jiyong Parkda8c6932019-08-12 19:56:08 +0900486 }
487 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900488
489 // Import the preprocessed file
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900490 for (const string& s : options.PreprocessedFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900491 if (!parse_preprocessed_file(io_delegate, s, typenames)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800492 err = AidlError::BAD_PRE_PROCESSED_FILE;
Christopher Wileyef140932015-11-03 09:29:19 -0800493 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700494 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800495 if (err != AidlError::OK) {
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700496 return err;
497 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800498
Jiyong Parkb034bf02018-07-30 17:44:33 +0900499 // Find files to import and parse them
Jiyong Parke59c3682018-09-11 23:10:25 +0900500 vector<string> import_paths;
Jiyong Park8c380532018-08-30 14:55:26 +0900501 ImportResolver import_resolver{io_delegate, input_file_name, options.ImportDirs(),
502 options.InputFiles()};
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900503 for (const auto& import : main_parser->ParsedDocument().Imports()) {
Jooyung Han29813842020-12-08 01:28:03 +0900504 if (AidlTypenames::IsBuiltinTypename(import->GetNeededClass())) {
505 continue;
Jiyong Parke05195e2018-10-08 18:24:23 +0900506 }
Jooyung Han29813842020-12-08 01:28:03 +0900507 if (typenames->IsIgnorableImport(import->GetNeededClass())) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700508 // There are places in the Android tree where an import doesn't resolve,
509 // but we'll pick the type up through the preprocessed types.
510 // This seems like an error, but legacy support demands we support it...
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700511 continue;
512 }
Jooyung Han29813842020-12-08 01:28:03 +0900513 string import_path = import_resolver.FindImportFile(import->GetNeededClass());
Christopher Wiley72877ac2015-10-06 14:41:42 -0700514 if (import_path.empty()) {
Jooyung Han29813842020-12-08 01:28:03 +0900515 if (typenames->ResolveTypename(import->GetNeededClass()).is_resolved) {
516 // This could happen when the type is from the preprocessed aidl file.
517 // In that case, use the type from preprocessed aidl file
Jiyong Park8f6ec462020-01-19 20:52:47 +0900518 continue;
519 }
Jooyung Han29813842020-12-08 01:28:03 +0900520 AIDL_ERROR(input_file_name) << "Couldn't find import for class " << import->GetNeededClass();
521 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700522 continue;
523 }
Casey Dahlin2cc93162015-10-02 16:14:17 -0700524
Jiyong Parke59c3682018-09-11 23:10:25 +0900525 import_paths.emplace_back(import_path);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900526
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900527 std::unique_ptr<Parser> import_parser = Parser::Parse(import_path, io_delegate, *typenames);
Steven Moreland64e29be2018-08-08 18:52:19 -0700528 if (import_parser == nullptr) {
Devin Moore2a088902020-09-17 10:51:19 -0700529 AIDL_ERROR(import_path) << "error while importing " << import_path << " for " << import;
Christopher Wiley632801d2015-11-05 14:15:49 -0800530 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700531 continue;
532 }
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700533 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800534 if (err != AidlError::OK) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700535 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700536 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800537
Jiyong Park3c35e392018-08-30 13:10:30 +0900538 for (const auto& imported_file : options.ImportFiles()) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900539 import_paths.emplace_back(imported_file);
Jiyong Park3c35e392018-08-30 13:10:30 +0900540
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900541 std::unique_ptr<Parser> import_parser = Parser::Parse(imported_file, io_delegate, *typenames);
Jiyong Park3c35e392018-08-30 13:10:30 +0900542 if (import_parser == nullptr) {
543 AIDL_ERROR(imported_file) << "error while importing " << imported_file;
544 err = AidlError::BAD_IMPORT;
545 continue;
546 }
Jiyong Park3c35e392018-08-30 13:10:30 +0900547 }
548 if (err != AidlError::OK) {
549 return err;
550 }
Jooyung Han29813842020-12-08 01:28:03 +0900551
552 TypeResolver resolver = [&](const AidlDocument* doc, AidlTypeSpecifier* type) {
553 if (type->Resolve(*typenames)) return true;
554
555 const string unresolved_name = type->GetUnresolvedName();
556 const std::optional<string> canonical_name = doc->ResolveName(unresolved_name);
557 if (!canonical_name) {
558 return false;
559 }
560 const string import_path = import_resolver.FindImportFile(*canonical_name);
561 if (import_path.empty()) {
562 return false;
563 }
564 import_paths.push_back(import_path);
565
566 std::unique_ptr<Parser> import_parser = Parser::Parse(import_path, io_delegate, *typenames);
567 if (import_parser == nullptr) {
568 AIDL_ERROR(import_path) << "error while importing " << import_path << " for " << import_path;
569 return false;
570 }
571 if (!type->Resolve(*typenames)) {
572 AIDL_ERROR(type) << "Can't resolve " << type->GetName();
573 return false;
574 }
575 return true;
576 };
Jiyong Park96c16a92018-08-16 16:37:09 +0900577 const bool is_check_api = options.GetTask() == Options::Task::CHECK_API;
Jooyung Hane87cdd02020-12-11 16:47:35 +0900578 const bool is_dump_api = options.GetTask() == Options::Task::DUMP_API;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900579
580 // Resolve the unresolved type references found from the input file
Jooyung Han29813842020-12-08 01:28:03 +0900581 if (!is_check_api && !main_parser->Resolve(resolver)) {
Jiyong Park96c16a92018-08-16 16:37:09 +0900582 // Resolution is not need for check api because all typespecs are
583 // using fully qualified names.
Jiyong Park1deecc32018-07-17 01:14:41 +0900584 return AidlError::BAD_TYPE;
585 }
Daniel Norman85aed542019-08-21 12:01:14 -0700586
Jooyung Han672557b2020-12-24 05:18:00 +0900587 if (!typenames->Autofill()) {
588 return AidlError::BAD_TYPE;
Steven Moreland59e53e42019-11-26 20:38:08 -0800589 }
Daniel Norman85aed542019-08-21 12:01:14 -0700590
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900591 //////////////////////////////////////////////////////////////////////////
592 // Validation phase
593 //////////////////////////////////////////////////////////////////////////
594
Steven Morelande2c64b42018-09-18 15:06:37 -0700595 // For legacy reasons, by default, compiling an unstructured parcelable (which contains no output)
596 // is allowed. This must not be returned as an error until the very end of this procedure since
597 // this may be considered a success, and we should first check that there are not other, more
598 // serious failures.
599 bool contains_unstructured_parcelable = false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800600
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900601 const auto& types = main_parser->ParsedDocument().DefinedTypes();
Jiyong Park62515512020-06-08 15:57:11 +0900602 const int num_defined_types = types.size();
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900603 for (const auto& defined_type : types) {
Steven Moreland21780812020-09-11 01:29:45 +0000604 AIDL_FATAL_IF(defined_type == nullptr, main_parser->FileName());
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900605
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000606 // Ensure type is exactly one of the following:
607 AidlInterface* interface = defined_type->AsInterface();
608 AidlStructuredParcelable* parcelable = defined_type->AsStructuredParcelable();
609 AidlParcelable* unstructured_parcelable = defined_type->AsUnstructuredParcelable();
610 AidlEnumDeclaration* enum_decl = defined_type->AsEnumDeclaration();
Jooyung Han2946afc2020-10-05 20:29:16 +0900611 AidlUnionDecl* union_decl = defined_type->AsUnionDeclaration();
612 AIDL_FATAL_IF(
613 !!interface + !!parcelable + !!unstructured_parcelable + !!enum_decl + !!union_decl != 1,
614 defined_type);
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000615
616 // Ensure that foo.bar.IFoo is defined in <some_path>/foo/bar/IFoo.aidl
617 if (num_defined_types == 1 && !check_filename(input_file_name, *defined_type)) {
618 return AidlError::BAD_PACKAGE;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900619 }
620
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000621 {
622 bool valid_type = true;
623
624 if (!is_check_api) {
625 // Ideally, we could do this for check api, but we can't resolve imports
Jooyung Han808a2a02020-12-28 16:46:54 +0900626 if (!defined_type->CheckValid(*typenames)) {
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000627 valid_type = false;
628 }
629 }
630
Jooyung Hane87cdd02020-12-11 16:47:35 +0900631 if (!is_dump_api && !is_check_api) {
632 if (!defined_type->LanguageSpecificCheckValid(*typenames, options.TargetLanguage())) {
633 valid_type = false;
634 }
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000635 }
636
637 if (!valid_type) {
Jeongik Cha82317dd2019-02-27 20:26:11 +0900638 return AidlError::BAD_TYPE;
639 }
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000640 }
641
642 if (unstructured_parcelable != nullptr) {
643 bool isStable = unstructured_parcelable->IsStableApiParcelable(options.TargetLanguage());
Jeongik Cha82317dd2019-02-27 20:26:11 +0900644 if (options.IsStructured() && !isStable) {
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000645 AIDL_ERROR(unstructured_parcelable)
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800646 << "Cannot declared parcelable in a --structured interface. Parcelable must be defined "
647 "in AIDL directly.";
648 return AidlError::NOT_STRUCTURED;
649 }
650 if (options.FailOnParcelable()) {
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000651 AIDL_ERROR(unstructured_parcelable)
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800652 << "Refusing to generate code with unstructured parcelables. Declared parcelables "
653 "should be in their own file and/or cannot be used with --structured interfaces.";
654 // Continue parsing for more errors
655 }
656
Steven Morelande2c64b42018-09-18 15:06:37 -0700657 contains_unstructured_parcelable = true;
Steven Morelande2c64b42018-09-18 15:06:37 -0700658 }
659
Devin Moore0d0e3f62020-03-30 17:45:39 -0700660 if (defined_type->IsVintfStability()) {
661 bool success = true;
662 if (options.GetStability() != Options::Stability::VINTF) {
663 AIDL_ERROR(defined_type)
664 << "Must compile @VintfStability type w/ aidl_interface 'stability: \"vintf\"'";
665 success = false;
666 }
667 if (!options.IsStructured()) {
668 AIDL_ERROR(defined_type)
669 << "Must compile @VintfStability type w/ aidl_interface --structured";
670 success = false;
671 }
672 if (!success) return AidlError::NOT_STRUCTURED;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700673 }
674
Jiyong Parkb034bf02018-07-30 17:44:33 +0900675 if (interface != nullptr) {
676 // add the meta-method 'int getInterfaceVersion()' if version is specified.
677 if (options.Version() > 0) {
678 AidlTypeSpecifier* ret =
Jooyung Han8451a202021-01-16 03:07:06 +0900679 new AidlTypeSpecifier(AIDL_LOCATION_HERE, "int", false, nullptr, Comments{});
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900680 ret->Resolve(*typenames);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900681 vector<unique_ptr<AidlArgument>>* args = new vector<unique_ptr<AidlArgument>>();
Jooyung Han3f347ca2020-12-01 12:41:50 +0900682 auto method = std::make_unique<AidlMethod>(
Jooyung Han8451a202021-01-16 03:07:06 +0900683 AIDL_LOCATION_HERE, false, ret, "getInterfaceVersion", args, Comments{},
684 kGetInterfaceVersionId, false /* is_user_defined */);
Jooyung Han3f347ca2020-12-01 12:41:50 +0900685 interface->AddMethod(std::move(method));
Jiyong Parkb034bf02018-07-30 17:44:33 +0900686 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900687 // add the meta-method 'string getInterfaceHash()' if hash is specified.
688 if (!options.Hash().empty()) {
689 AidlTypeSpecifier* ret =
Jooyung Han8451a202021-01-16 03:07:06 +0900690 new AidlTypeSpecifier(AIDL_LOCATION_HERE, "String", false, nullptr, Comments{});
Paul Trautrimb77048c2020-01-21 16:39:32 +0900691 ret->Resolve(*typenames);
692 vector<unique_ptr<AidlArgument>>* args = new vector<unique_ptr<AidlArgument>>();
Jooyung Han8451a202021-01-16 03:07:06 +0900693 auto method = std::make_unique<AidlMethod>(
694 AIDL_LOCATION_HERE, false, ret, kGetInterfaceHash, args, Comments{},
695 kGetInterfaceHashId, false /* is_user_defined */);
Jooyung Han3f347ca2020-12-01 12:41:50 +0900696 interface->AddMethod(std::move(method));
Paul Trautrimb77048c2020-01-21 16:39:32 +0900697 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900698 if (!check_and_assign_method_ids(interface->GetMethods())) {
699 return AidlError::BAD_METHOD_ID;
700 }
Jooyung Hancfe08002020-12-04 12:56:35 +0900701 }
Jooyung Han30f64ad2020-12-15 08:16:31 +0900702 // Verify the var/const declarations.
703 // const expressions should be non-empty when evaluated with the var/const type.
704 if (!is_check_api) {
705 for (const auto& constant : defined_type->GetConstantDeclarations()) {
706 if (constant->ValueString(AidlConstantValueDecorator).empty()) {
707 return AidlError::BAD_TYPE;
Will McVickerd7d18df2019-09-12 13:40:50 -0700708 }
Jooyung Han30f64ad2020-12-15 08:16:31 +0900709 }
710 for (const auto& var : defined_type->GetFields()) {
711 if (var->GetDefaultValue() && var->ValueString(AidlConstantValueDecorator).empty()) {
712 return AidlError::BAD_TYPE;
713 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700714 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900715 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800716 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800717
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900718 if (!ValidateAnnotationContext(main_parser->ParsedDocument())) {
719 return AidlError::BAD_TYPE;
720 }
721
Jiyong Parkd9113322020-12-31 03:20:33 +0900722 if (!is_check_api && !Diagnose(main_parser->ParsedDocument(), options.GetDiagnosticMapping())) {
723 return AidlError::BAD_TYPE;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900724 }
725
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900726 typenames->IterateTypes([&](const AidlDefinedType& type) {
Steven Morelanda57d0a62019-07-30 09:41:14 -0700727 if (options.IsStructured() && type.AsUnstructuredParcelable() != nullptr &&
Jeongik Cha88f95a82020-01-15 13:02:16 +0900728 !type.AsUnstructuredParcelable()->IsStableApiParcelable(options.TargetLanguage())) {
Steven Morelanda57d0a62019-07-30 09:41:14 -0700729 err = AidlError::NOT_STRUCTURED;
Devin Moore097a3ab2020-03-11 16:08:44 -0700730 AIDL_ERROR(type) << type.GetCanonicalName()
731 << " is not structured, but this is a structured interface.";
Steven Morelanda57d0a62019-07-30 09:41:14 -0700732 }
733 if (options.GetStability() == Options::Stability::VINTF && !type.IsVintfStability()) {
734 err = AidlError::NOT_STRUCTURED;
Devin Moore097a3ab2020-03-11 16:08:44 -0700735 AIDL_ERROR(type) << type.GetCanonicalName()
736 << " does not have VINTF level stability, but this interface requires it.";
Steven Morelanda57d0a62019-07-30 09:41:14 -0700737 }
Jiyong Parkf8d53612020-05-04 14:06:13 +0900738
Jeongik Chaef44e622020-10-23 16:00:52 +0900739 // Ensure that untyped List/Map is not used in a parcelable, a union and a stable interface.
Jiyong Parkf8d53612020-05-04 14:06:13 +0900740
Jeongik Chaef44e622020-10-23 16:00:52 +0900741 std::function<void(const AidlTypeSpecifier&, const AidlNode*)> check_untyped_container =
742 [&err, &check_untyped_container](const AidlTypeSpecifier& type, const AidlNode* node) {
743 if (type.IsGeneric()) {
744 std::for_each(type.GetTypeParameters().begin(), type.GetTypeParameters().end(),
745 [&node, &check_untyped_container](auto& nested) {
746 check_untyped_container(*nested, node);
747 });
748 return;
Jiyong Parkf8d53612020-05-04 14:06:13 +0900749 }
Jeongik Chaef44e622020-10-23 16:00:52 +0900750 if (type.GetName() == "List" || type.GetName() == "Map") {
751 err = AidlError::BAD_TYPE;
752 AIDL_ERROR(node)
753 << "Encountered an untyped List or Map. The use of untyped List/Map is prohibited "
754 << "because it is not guaranteed that the objects in the list are recognizable in "
755 << "the receiving side. Consider switching to an array or a generic List/Map.";
756 }
757 };
Jeongik Chaef44e622020-10-23 16:00:52 +0900758
Jooyung Han829ec7c2020-12-02 12:07:36 +0900759 if (type.AsInterface() && options.IsStructured()) {
760 for (const auto& method : type.GetMethods()) {
Jeongik Chaef44e622020-10-23 16:00:52 +0900761 check_untyped_container(method->GetType(), method.get());
762 for (const auto& arg : method->GetArguments()) {
763 check_untyped_container(arg->GetType(), method.get());
Jiyong Parkf8d53612020-05-04 14:06:13 +0900764 }
Jeongik Chaef44e622020-10-23 16:00:52 +0900765 }
Jooyung Han829ec7c2020-12-02 12:07:36 +0900766 }
767 for (const auto& field : type.GetFields()) {
768 check_untyped_container(field->GetType(), field.get());
Jiyong Parkf8d53612020-05-04 14:06:13 +0900769 }
Steven Morelanda57d0a62019-07-30 09:41:14 -0700770 });
771
Steven Moreland6cee3482018-07-18 14:39:58 -0700772 if (err != AidlError::OK) {
773 return err;
774 }
775
Jiyong Parkb034bf02018-07-30 17:44:33 +0900776 if (imported_files != nullptr) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900777 *imported_files = import_paths;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700778 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700779
Steven Morelande2c64b42018-09-18 15:06:37 -0700780 if (contains_unstructured_parcelable) {
781 // Considered a success for the legacy case, so this must be returned last.
782 return AidlError::FOUND_PARCELABLE;
783 }
784
Christopher Wiley632801d2015-11-05 14:15:49 -0800785 return AidlError::OK;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700786}
787
Casey Dahlin2cc93162015-10-02 16:14:17 -0700788} // namespace internals
789
Jiyong Parkb034bf02018-07-30 17:44:33 +0900790int compile_aidl(const Options& options, const IoDelegate& io_delegate) {
791 const Options::Language lang = options.TargetLanguage();
Jiyong Park74595c12018-07-23 15:22:50 +0900792 for (const string& input_file : options.InputFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900793 AidlTypenames typenames;
Jiyong Park74595c12018-07-23 15:22:50 +0900794
Jiyong Parkb034bf02018-07-30 17:44:33 +0900795 vector<string> imported_files;
Jiyong Park74595c12018-07-23 15:22:50 +0900796
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900797 AidlError aidl_err = internals::load_and_validate_aidl(input_file, options, io_delegate,
798 &typenames, &imported_files);
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800799 bool allowError = aidl_err == AidlError::FOUND_PARCELABLE && !options.FailOnParcelable();
800 if (aidl_err != AidlError::OK && !allowError) {
Jiyong Park74595c12018-07-23 15:22:50 +0900801 return 1;
802 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700803
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900804 for (const auto& defined_type : typenames.MainDocument().DefinedTypes()) {
Steven Moreland21780812020-09-11 01:29:45 +0000805 AIDL_FATAL_IF(defined_type == nullptr, input_file);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700806
Jiyong Parkb034bf02018-07-30 17:44:33 +0900807 string output_file_name = options.OutputFile();
808 // if needed, generate the output file name from the base folder
809 if (output_file_name.empty() && !options.OutputDir().empty()) {
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900810 output_file_name = GetOutputFilePath(options, *defined_type);
Jiyong Parkb03551f2018-08-06 19:20:51 +0900811 if (output_file_name.empty()) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900812 return 1;
813 }
814 }
Jiyong Park74595c12018-07-23 15:22:50 +0900815
Jiyong Parkb034bf02018-07-30 17:44:33 +0900816 if (!write_dep_file(options, *defined_type, imported_files, io_delegate, input_file,
817 output_file_name)) {
818 return 1;
819 }
Jiyong Park74595c12018-07-23 15:22:50 +0900820
Jiyong Parkb034bf02018-07-30 17:44:33 +0900821 bool success = false;
822 if (lang == Options::Language::CPP) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900823 success =
824 cpp::GenerateCpp(output_file_name, options, typenames, *defined_type, io_delegate);
Steven Morelandc26d8142018-09-17 14:25:33 -0700825 } else if (lang == Options::Language::NDK) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900826 ndk::GenerateNdk(output_file_name, options, typenames, *defined_type, io_delegate);
Steven Morelandc26d8142018-09-17 14:25:33 -0700827 success = true;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900828 } else if (lang == Options::Language::JAVA) {
Jiyong Park9ca5c7e2019-10-17 15:01:14 +0900829 if (defined_type->AsUnstructuredParcelable() != nullptr) {
830 // Legacy behavior. For parcelable declarations in Java, don't generate output file.
831 success = true;
832 } else {
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900833 success = java::generate_java(output_file_name, defined_type.get(), typenames,
834 io_delegate, options);
Jiyong Park9ca5c7e2019-10-17 15:01:14 +0900835 }
Andrei Homescub62afd92020-05-11 19:24:59 -0700836 } else if (lang == Options::Language::RUST) {
837 success = rust::GenerateRust(output_file_name, defined_type.get(), typenames, io_delegate,
838 options);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900839 } else {
Steven Moreland21780812020-09-11 01:29:45 +0000840 AIDL_FATAL(input_file) << "Should not reach here.";
Jiyong Parkb034bf02018-07-30 17:44:33 +0900841 }
842 if (!success) {
843 return 1;
844 }
Jiyong Park74595c12018-07-23 15:22:50 +0900845 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700846 }
Jiyong Park74595c12018-07-23 15:22:50 +0900847 return 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800848}
849
Andrei Onea8714b022019-02-01 18:55:54 +0000850bool dump_mappings(const Options& options, const IoDelegate& io_delegate) {
851 android::aidl::mappings::SignatureMap all_mappings;
852 for (const string& input_file : options.InputFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900853 AidlTypenames typenames;
Andrei Onea8714b022019-02-01 18:55:54 +0000854 vector<string> imported_files;
855
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900856 AidlError aidl_err = internals::load_and_validate_aidl(input_file, options, io_delegate,
857 &typenames, &imported_files);
Andrei Onea8714b022019-02-01 18:55:54 +0000858 if (aidl_err != AidlError::OK) {
Jiyong Park3a060392020-04-11 21:02:19 +0900859 return false;
Andrei Onea8714b022019-02-01 18:55:54 +0000860 }
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900861 for (const auto& defined_type : typenames.MainDocument().DefinedTypes()) {
862 auto mappings = mappings::generate_mappings(defined_type.get(), typenames);
Andrei Onea8714b022019-02-01 18:55:54 +0000863 all_mappings.insert(mappings.begin(), mappings.end());
864 }
865 }
866 std::stringstream mappings_str;
867 for (const auto& mapping : all_mappings) {
868 mappings_str << mapping.first << "\n" << mapping.second << "\n";
869 }
870 auto code_writer = io_delegate.GetCodeWriter(options.OutputFile());
871 code_writer->Write("%s", mappings_str.str().c_str());
872 return true;
873}
874
Jiyong Park74595c12018-07-23 15:22:50 +0900875bool preprocess_aidl(const Options& options, const IoDelegate& io_delegate) {
876 unique_ptr<CodeWriter> writer = io_delegate.GetCodeWriter(options.OutputFile());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800877
Jiyong Park74595c12018-07-23 15:22:50 +0900878 for (const auto& file : options.InputFiles()) {
Jiyong Park1deecc32018-07-17 01:14:41 +0900879 AidlTypenames typenames;
Steven Moreland64e29be2018-08-08 18:52:19 -0700880 std::unique_ptr<Parser> p = Parser::Parse(file, io_delegate, typenames);
881 if (p == nullptr) return false;
Casey Dahlin59401da2015-10-09 18:16:45 -0700882
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900883 for (const auto& defined_type : p->ParsedDocument().DefinedTypes()) {
Steven Morelanded83a282018-07-17 13:27:29 -0700884 if (!writer->Write("%s %s;\n", defined_type->GetPreprocessDeclarationName().c_str(),
Steven Morelandc258abc2018-07-10 14:03:38 -0700885 defined_type->GetCanonicalName().c_str())) {
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800886 return false;
887 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800888 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800889 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800890
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800891 return writer->Close();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800892}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700893
Steven Moreland3981d9e2020-03-31 14:11:44 -0700894int aidl_entry(const Options& options, const IoDelegate& io_delegate) {
Steven Moreland33efcf62020-04-10 16:40:43 -0700895 AidlErrorLog::clearError();
896
Steven Moreland3981d9e2020-03-31 14:11:44 -0700897 int ret = 1;
898 switch (options.GetTask()) {
899 case Options::Task::COMPILE:
900 ret = android::aidl::compile_aidl(options, io_delegate);
901 break;
902 case Options::Task::PREPROCESS:
903 ret = android::aidl::preprocess_aidl(options, io_delegate) ? 0 : 1;
904 break;
905 case Options::Task::DUMP_API:
906 ret = android::aidl::dump_api(options, io_delegate) ? 0 : 1;
907 break;
908 case Options::Task::CHECK_API:
909 ret = android::aidl::check_api(options, io_delegate) ? 0 : 1;
910 break;
911 case Options::Task::DUMP_MAPPINGS:
912 ret = android::aidl::dump_mappings(options, io_delegate) ? 0 : 1;
913 break;
914 default:
915 AIDL_FATAL(AIDL_LOCATION_HERE)
916 << "Unrecognized task: " << static_cast<size_t>(options.GetTask());
917 }
918
919 // compiler invariants
Steven Moreland21780812020-09-11 01:29:45 +0000920 const bool shouldReportError = ret != 0;
921 const bool reportedError = AidlErrorLog::hadError();
922 AIDL_FATAL_IF(shouldReportError != reportedError, AIDL_LOCATION_HERE)
923 << "Compiler returned error " << ret << " but did" << (reportedError ? "" : " not")
924 << " emit error logs";
Steven Moreland3981d9e2020-03-31 14:11:44 -0700925
926 return ret;
927}
928
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700929} // namespace aidl
Steven Morelandf4c64df2019-07-29 19:54:04 -0700930} // namespace android