blob: fe4ae3f799b6c75406fb31135c0089c1e756b288 [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"
Jooyung Han93f48f02021-06-05 00:11:16 +090053#include "preprocess.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070054
Adam Lesinskiffa16862014-01-23 18:17:42 -080055#ifndef O_BINARY
56# define O_BINARY 0
57#endif
58
Christopher Wiley3a9911c2016-01-19 12:59:09 -080059using android::base::Join;
Christopher Wileyd76067c2015-10-19 17:00:13 -070060using android::base::Split;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070061using std::set;
62using std::string;
Christopher Wiley84c1eac2015-09-23 13:29:28 -070063using std::unique_ptr;
Jooyung Hand25527a2020-12-30 12:29:31 +090064using std::unordered_set;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070065using std::vector;
Adam Lesinskiffa16862014-01-23 18:17:42 -080066
Christopher Wileyf690be52015-09-14 15:19:10 -070067namespace android {
68namespace aidl {
69namespace {
Adam Lesinskiffa16862014-01-23 18:17:42 -080070
Jiyong Park965c5b92018-11-21 13:37:15 +090071// Copied from android.is.IBinder.[FIRST|LAST]_CALL_TRANSACTION
72const int kFirstCallTransaction = 1;
73const int kLastCallTransaction = 0x00ffffff;
74
75// Following IDs are all offsets from kFirstCallTransaction
Jiyong Park309668e2018-07-28 16:55:44 +090076
77// IDs for meta transactions. Most of the meta transactions are implemented in
78// the framework side (Binder.java or Binder.cpp). But these are the ones that
79// are auto-implemented by the AIDL compiler.
Jiyong Park965c5b92018-11-21 13:37:15 +090080const int kFirstMetaMethodId = kLastCallTransaction - kFirstCallTransaction;
81const int kGetInterfaceVersionId = kFirstMetaMethodId;
Paul Trautrimb77048c2020-01-21 16:39:32 +090082const int kGetInterfaceHashId = kFirstMetaMethodId - 1;
Jiyong Park965c5b92018-11-21 13:37:15 +090083// Additional meta transactions implemented by AIDL should use
84// kFirstMetaMethodId -1, -2, ...and so on.
85
86// Reserve 100 IDs for meta methods, which is more than enough. If we don't reserve,
87// in the future, a newly added meta transaction ID will have a chance to
88// collide with the user-defined methods that were added in the past. So,
89// let's prevent users from using IDs in this range from the beginning.
90const int kLastMetaMethodId = kFirstMetaMethodId - 99;
91
92// Range of IDs that is allowed for user-defined methods.
93const int kMinUserSetMethodId = 0;
94const int kMaxUserSetMethodId = kLastMetaMethodId - 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -080095
Steven Moreland92c55f12018-07-31 14:08:37 -070096bool check_filename(const std::string& filename, const AidlDefinedType& defined_type) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080097 const char* p;
98 string expected;
99 string fn;
100 size_t len;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800101 bool valid = false;
102
Christopher Wileybc2df692016-06-02 16:27:26 -0700103 if (!IoDelegate::GetAbsolutePath(filename, &fn)) {
104 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800105 }
106
Steven Moreland92c55f12018-07-31 14:08:37 -0700107 const std::string package = defined_type.GetPackage();
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700108 if (!package.empty()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800109 expected = package;
110 expected += '.';
111 }
112
113 len = expected.length();
114 for (size_t i=0; i<len; i++) {
115 if (expected[i] == '.') {
116 expected[i] = OS_PATH_SEPARATOR;
117 }
118 }
119
Steven Moreland92c55f12018-07-31 14:08:37 -0700120 const std::string name = defined_type.GetName();
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700121 expected.append(name, 0, name.find('.'));
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700122
Adam Lesinskiffa16862014-01-23 18:17:42 -0800123 expected += ".aidl";
124
125 len = fn.length();
126 valid = (len >= expected.length());
127
128 if (valid) {
129 p = fn.c_str() + (len - expected.length());
130
Elliott Hughesce310da2015-07-29 08:44:17 -0700131#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800132 if (OS_PATH_SEPARATOR != '/') {
133 // Input filename under cygwin most likely has / separators
134 // whereas the expected string uses \\ separators. Adjust
135 // them accordingly.
136 for (char *c = const_cast<char *>(p); *c; ++c) {
137 if (*c == '/') *c = OS_PATH_SEPARATOR;
138 }
139 }
140#endif
141
Yabin Cui482eefb2014-11-10 15:01:43 -0800142 // aidl assumes case-insensitivity on Mac Os and Windows.
143#if defined(__linux__)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800144 valid = (expected == p);
145#else
146 valid = !strcasecmp(expected.c_str(), p);
147#endif
148 }
149
150 if (!valid) {
Steven Moreland92c55f12018-07-31 14:08:37 -0700151 AIDL_ERROR(defined_type) << name << " should be declared in a file called " << expected;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800152 }
153
Casey Dahlin42727f82015-10-12 19:23:40 -0700154 return valid;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800155}
156
Jiyong Park74595c12018-07-23 15:22:50 +0900157bool write_dep_file(const Options& options, const AidlDefinedType& defined_type,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900158 const vector<string>& imports, const IoDelegate& io_delegate,
Jiyong Park74595c12018-07-23 15:22:50 +0900159 const string& input_file, const string& output_file) {
160 string dep_file_name = options.DependencyFile();
161 if (dep_file_name.empty() && options.AutoDepFile()) {
162 dep_file_name = output_file + ".d";
163 }
164
165 if (dep_file_name.empty()) {
166 return true; // nothing to do
167 }
Jiyong Park05463732018-08-09 16:03:02 +0900168
Jiyong Park74595c12018-07-23 15:22:50 +0900169 CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name);
170 if (!writer) {
Steven Moreland21780812020-09-11 01:29:45 +0000171 AIDL_ERROR(dep_file_name) << "Could not open dependency file.";
Jiyong Park74595c12018-07-23 15:22:50 +0900172 return false;
173 }
174
175 vector<string> source_aidl = {input_file};
176 for (const auto& import : imports) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900177 source_aidl.push_back(import);
Jiyong Park74595c12018-07-23 15:22:50 +0900178 }
179
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800180 // Encode that the output file depends on aidl input files.
Jiyong Parkdf202122019-09-30 20:48:35 +0900181 if (defined_type.AsUnstructuredParcelable() != nullptr &&
182 options.TargetLanguage() == Options::Language::JAVA) {
183 // Legacy behavior. For parcelable declarations in Java, don't emit output file as
184 // the dependency target. b/141372861
185 writer->Write(" : \\\n");
186 } else {
187 writer->Write("%s : \\\n", output_file.c_str());
188 }
Jiyong Park74595c12018-07-23 15:22:50 +0900189 writer->Write(" %s", Join(source_aidl, " \\\n ").c_str());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800190 writer->Write("\n");
Christopher Wileya30a45e2015-10-17 10:56:59 -0700191
Jiyong Park74595c12018-07-23 15:22:50 +0900192 if (!options.DependencyFileNinja()) {
Dan Willemsen93298ee2016-11-10 23:55:55 -0800193 writer->Write("\n");
194 // Output "<input_aidl_file>: " so make won't fail if the input .aidl file
195 // has been deleted, moved or renamed in incremental build.
Jiyong Park74595c12018-07-23 15:22:50 +0900196 for (const auto& src : source_aidl) {
Dan Willemsen93298ee2016-11-10 23:55:55 -0800197 writer->Write("%s :\n", src.c_str());
198 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800199 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700200
Steven Morelandc26d8142018-09-17 14:25:33 -0700201 if (options.IsCppOutput()) {
Jiyong Park74595c12018-07-23 15:22:50 +0900202 if (!options.DependencyFileNinja()) {
203 using ::android::aidl::cpp::ClassNames;
204 using ::android::aidl::cpp::HeaderFile;
205 vector<string> headers;
Jiyong Park5b7e5322019-04-03 20:05:01 +0900206 for (ClassNames c : {ClassNames::CLIENT, ClassNames::SERVER, ClassNames::RAW}) {
Jiyong Park05463732018-08-09 16:03:02 +0900207 headers.push_back(options.OutputHeaderDir() +
Jiyong Park74595c12018-07-23 15:22:50 +0900208 HeaderFile(defined_type, c, false /* use_os_sep */));
209 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800210
Jiyong Park74595c12018-07-23 15:22:50 +0900211 writer->Write("\n");
212
213 // Generated headers also depend on the source aidl files.
214 writer->Write("%s : \\\n %s\n", Join(headers, " \\\n ").c_str(),
215 Join(source_aidl, " \\\n ").c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800216 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700217 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800218
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800219 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800220}
221
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900222// Returns the path to the destination file of `defined_type`.
223string GetOutputFilePath(const Options& options, const AidlDefinedType& defined_type) {
Jiyong Park74595c12018-07-23 15:22:50 +0900224 string result = options.OutputDir();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800225
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900226 // add the package
Jiyong Park74595c12018-07-23 15:22:50 +0900227 string package = defined_type.GetPackage();
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900228 if (!package.empty()) {
229 for (auto& c : package) {
230 if (c == '.') {
231 c = OS_PATH_SEPARATOR;
232 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800233 }
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900234 result += package;
235 result += OS_PATH_SEPARATOR;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700236 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800237
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900238 // add the filename
239 result += defined_type.GetName();
Jiyong Parkb03551f2018-08-06 19:20:51 +0900240 if (options.TargetLanguage() == Options::Language::JAVA) {
241 result += ".java";
Steven Morelandc26d8142018-09-17 14:25:33 -0700242 } else if (options.IsCppOutput()) {
Jiyong Parkb03551f2018-08-06 19:20:51 +0900243 result += ".cpp";
Andrei Homescub62afd92020-05-11 19:24:59 -0700244 } else if (options.TargetLanguage() == Options::Language::RUST) {
245 result += ".rs";
Jiyong Parkb03551f2018-08-06 19:20:51 +0900246 } else {
Steven Moreland21780812020-09-11 01:29:45 +0000247 AIDL_FATAL("Unknown target language");
Jiyong Parkb03551f2018-08-06 19:20:51 +0900248 return "";
249 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800250
Steven Moreland5557f1c2018-07-02 13:50:23 -0700251 return result;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800252}
253
Jiyong Parkb034bf02018-07-30 17:44:33 +0900254bool check_and_assign_method_ids(const std::vector<std::unique_ptr<AidlMethod>>& items) {
Jiyong Park309668e2018-07-28 16:55:44 +0900255 // Check whether there are any methods with manually assigned id's and any
256 // that are not. Either all method id's must be manually assigned or all of
257 // them must not. Also, check for uplicates of user set ID's and that the
258 // ID's are within the proper bounds.
259 set<int> usedIds;
260 bool hasUnassignedIds = false;
261 bool hasAssignedIds = false;
Steven Morelandec6f4692019-08-13 10:03:24 -0700262 int newId = kMinUserSetMethodId;
Jiyong Park309668e2018-07-28 16:55:44 +0900263 for (const auto& item : items) {
264 // However, meta transactions that are added by the AIDL compiler are
265 // exceptions. They have fixed IDs but allowed to be with user-defined
266 // methods having auto-assigned IDs. This is because the Ids of the meta
267 // transactions must be stable during the entire lifetime of an interface.
268 // In other words, their IDs must be the same even when new user-defined
269 // methods are added.
Jiyong Park3633b722019-04-11 15:38:26 +0900270 if (!item->IsUserDefined()) {
271 continue;
272 }
273 if (item->HasId()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900274 hasAssignedIds = true;
Jiyong Park309668e2018-07-28 16:55:44 +0900275 } else {
Steven Morelandec6f4692019-08-13 10:03:24 -0700276 item->SetId(newId++);
Jiyong Park309668e2018-07-28 16:55:44 +0900277 hasUnassignedIds = true;
278 }
Steven Morelandec6f4692019-08-13 10:03:24 -0700279
Jiyong Park309668e2018-07-28 16:55:44 +0900280 if (hasAssignedIds && hasUnassignedIds) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900281 AIDL_ERROR(item) << "You must either assign id's to all methods or to none of them.";
Jiyong Park309668e2018-07-28 16:55:44 +0900282 return false;
283 }
Jiyong Park309668e2018-07-28 16:55:44 +0900284
Steven Morelandec6f4692019-08-13 10:03:24 -0700285 // Ensure that the user set id is not duplicated.
286 if (usedIds.find(item->GetId()) != usedIds.end()) {
287 // We found a duplicate id, so throw an error.
288 AIDL_ERROR(item) << "Found duplicate method id (" << item->GetId() << ") for method "
289 << item->GetName();
290 return false;
291 }
292 usedIds.insert(item->GetId());
293
294 // Ensure that the user set id is within the appropriate limits
295 if (item->GetId() < kMinUserSetMethodId || item->GetId() > kMaxUserSetMethodId) {
296 AIDL_ERROR(item) << "Found out of bounds id (" << item->GetId() << ") for method "
297 << item->GetName() << ". Value for id must be between "
298 << kMinUserSetMethodId << " and " << kMaxUserSetMethodId << " inclusive.";
299 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800300 }
Jiyong Park309668e2018-07-28 16:55:44 +0900301 }
Steven Morelandec6f4692019-08-13 10:03:24 -0700302
Jiyong Park309668e2018-07-28 16:55:44 +0900303 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800304}
305
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900306bool ValidateAnnotationContext(const AidlDocument& doc) {
307 struct AnnotationValidator : AidlVisitor {
308 bool success = true;
309
310 void Check(const AidlAnnotatable& annotatable, AidlAnnotation::TargetContext context) {
311 for (const auto& annot : annotatable.GetAnnotations()) {
312 if (!annot.CheckContext(context)) {
313 success = false;
314 }
315 }
316 }
317 void Visit(const AidlInterface& m) override {
318 Check(m, AidlAnnotation::CONTEXT_TYPE_INTERFACE);
319 }
320 void Visit(const AidlParcelable& m) override {
321 Check(m, AidlAnnotation::CONTEXT_TYPE_UNSTRUCTURED_PARCELABLE);
322 }
323 void Visit(const AidlStructuredParcelable& m) override {
324 Check(m, AidlAnnotation::CONTEXT_TYPE_STRUCTURED_PARCELABLE);
325 }
326 void Visit(const AidlEnumDeclaration& m) override {
327 Check(m, AidlAnnotation::CONTEXT_TYPE_ENUM);
328 }
329 void Visit(const AidlUnionDecl& m) override { Check(m, AidlAnnotation::CONTEXT_TYPE_UNION); }
330 void Visit(const AidlMethod& m) override {
331 Check(m.GetType(), AidlAnnotation::CONTEXT_TYPE_SPECIFIER | AidlAnnotation::CONTEXT_METHOD);
332 for (const auto& arg : m.GetArguments()) {
333 Check(arg->GetType(), AidlAnnotation::CONTEXT_TYPE_SPECIFIER);
334 }
335 }
336 void Visit(const AidlConstantDeclaration& m) override {
337 Check(m.GetType(), AidlAnnotation::CONTEXT_TYPE_SPECIFIER | AidlAnnotation::CONTEXT_CONST);
338 }
339 void Visit(const AidlVariableDeclaration& m) override {
340 Check(m.GetType(), AidlAnnotation::CONTEXT_TYPE_SPECIFIER | AidlAnnotation::CONTEXT_FIELD);
341 }
342 void Visit(const AidlTypeSpecifier& m) override {
343 // nested generic type parameters are checked as well
344 if (m.IsGeneric()) {
345 for (const auto& tp : m.GetTypeParameters()) {
346 Check(*tp, AidlAnnotation::CONTEXT_TYPE_SPECIFIER);
347 }
348 }
349 }
350 };
351
352 AnnotationValidator validator;
353 VisitTopDown(validator, doc);
354 return validator.success;
355}
356
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700357} // namespace
358
359namespace internals {
360
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900361AidlError load_and_validate_aidl(const std::string& input_file_name, const Options& options,
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900362 const IoDelegate& io_delegate, AidlTypenames* typenames,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900363 vector<string>* imported_files) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800364 AidlError err = AidlError::OK;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800365
Jiyong Parkb034bf02018-07-30 17:44:33 +0900366 //////////////////////////////////////////////////////////////////////////
367 // Loading phase
368 //////////////////////////////////////////////////////////////////////////
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900369
Jiyong Parkb034bf02018-07-30 17:44:33 +0900370 // Parse the main input file
Jooyung Hance6733e2021-05-22 02:44:17 +0900371 const AidlDocument* document = Parser::Parse(input_file_name, io_delegate, *typenames);
372 if (document == nullptr) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900373 return AidlError::PARSE_ERROR;
374 }
Jooyung Han2946afc2020-10-05 20:29:16 +0900375 int num_top_level_decls = 0;
Jooyung Hance6733e2021-05-22 02:44:17 +0900376 for (const auto& type : document->DefinedTypes()) {
Jooyung Han2946afc2020-10-05 20:29:16 +0900377 if (type->AsUnstructuredParcelable() == nullptr) {
378 num_top_level_decls++;
379 if (num_top_level_decls > 1) {
Devin Moore5de18ed2020-04-02 13:52:29 -0700380 AIDL_ERROR(*type) << "You must declare only one type per file.";
381 return AidlError::BAD_TYPE;
382 }
Jiyong Parkda8c6932019-08-12 19:56:08 +0900383 }
384 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900385
386 // Import the preprocessed file
Jooyung Han93f48f02021-06-05 00:11:16 +0900387 for (const string& filename : options.PreprocessedFiles()) {
388 auto preprocessed = Parser::Parse(filename, io_delegate, *typenames, /*is_preprocessed=*/true);
389 if (!preprocessed) {
390 return AidlError::BAD_PRE_PROCESSED_FILE;
Christopher Wileyef140932015-11-03 09:29:19 -0800391 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700392 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800393
Jiyong Parkb034bf02018-07-30 17:44:33 +0900394 // Find files to import and parse them
Jiyong Parke59c3682018-09-11 23:10:25 +0900395 vector<string> import_paths;
Jiyong Park8c380532018-08-30 14:55:26 +0900396 ImportResolver import_resolver{io_delegate, input_file_name, options.ImportDirs(),
397 options.InputFiles()};
Jooyung Hance6733e2021-05-22 02:44:17 +0900398 for (const auto& import : document->Imports()) {
Jooyung Han29813842020-12-08 01:28:03 +0900399 if (AidlTypenames::IsBuiltinTypename(import->GetNeededClass())) {
400 continue;
Jiyong Parke05195e2018-10-08 18:24:23 +0900401 }
Jooyung Han29813842020-12-08 01:28:03 +0900402 if (typenames->IsIgnorableImport(import->GetNeededClass())) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700403 // There are places in the Android tree where an import doesn't resolve,
404 // but we'll pick the type up through the preprocessed types.
405 // This seems like an error, but legacy support demands we support it...
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700406 continue;
407 }
Jooyung Han29813842020-12-08 01:28:03 +0900408 string import_path = import_resolver.FindImportFile(import->GetNeededClass());
Christopher Wiley72877ac2015-10-06 14:41:42 -0700409 if (import_path.empty()) {
Jooyung Han29813842020-12-08 01:28:03 +0900410 if (typenames->ResolveTypename(import->GetNeededClass()).is_resolved) {
411 // This could happen when the type is from the preprocessed aidl file.
412 // In that case, use the type from preprocessed aidl file
Jiyong Park8f6ec462020-01-19 20:52:47 +0900413 continue;
414 }
Jooyung Han29813842020-12-08 01:28:03 +0900415 AIDL_ERROR(input_file_name) << "Couldn't find import for class " << import->GetNeededClass();
416 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700417 continue;
418 }
Casey Dahlin2cc93162015-10-02 16:14:17 -0700419
Jiyong Parke59c3682018-09-11 23:10:25 +0900420 import_paths.emplace_back(import_path);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900421
Jooyung Hance6733e2021-05-22 02:44:17 +0900422 auto imported_doc = Parser::Parse(import_path, io_delegate, *typenames);
423 if (imported_doc == nullptr) {
Jooyung Han93f48f02021-06-05 00:11:16 +0900424 AIDL_ERROR(import_path) << "error while importing " << import_path << " for "
425 << import->GetNeededClass();
Christopher Wiley632801d2015-11-05 14:15:49 -0800426 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700427 continue;
428 }
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700429 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800430 if (err != AidlError::OK) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700431 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700432 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800433
Jooyung Hance6733e2021-05-22 02:44:17 +0900434 TypeResolver resolver = [&](const AidlDefinedType* scope, AidlTypeSpecifier* type) {
Jooyung Han29813842020-12-08 01:28:03 +0900435 if (type->Resolve(*typenames)) return true;
436
437 const string unresolved_name = type->GetUnresolvedName();
Jooyung Hance6733e2021-05-22 02:44:17 +0900438 const auto doc = typenames->GetDocumentFor(scope);
Jooyung Han29813842020-12-08 01:28:03 +0900439 const std::optional<string> canonical_name = doc->ResolveName(unresolved_name);
440 if (!canonical_name) {
441 return false;
442 }
443 const string import_path = import_resolver.FindImportFile(*canonical_name);
444 if (import_path.empty()) {
445 return false;
446 }
447 import_paths.push_back(import_path);
448
Jooyung Hance6733e2021-05-22 02:44:17 +0900449 auto imported_doc = Parser::Parse(import_path, io_delegate, *typenames);
450 if (imported_doc == nullptr) {
Jooyung Han29813842020-12-08 01:28:03 +0900451 AIDL_ERROR(import_path) << "error while importing " << import_path << " for " << import_path;
452 return false;
453 }
454 if (!type->Resolve(*typenames)) {
455 AIDL_ERROR(type) << "Can't resolve " << type->GetName();
456 return false;
457 }
458 return true;
459 };
Jiyong Parkb034bf02018-07-30 17:44:33 +0900460
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900461 // Resolve the unresolved references
462 if (!ResolveReferences(*document, resolver)) {
Jiyong Park1deecc32018-07-17 01:14:41 +0900463 return AidlError::BAD_TYPE;
464 }
Daniel Norman85aed542019-08-21 12:01:14 -0700465
Jooyung Han672557b2020-12-24 05:18:00 +0900466 if (!typenames->Autofill()) {
467 return AidlError::BAD_TYPE;
Steven Moreland59e53e42019-11-26 20:38:08 -0800468 }
Daniel Norman85aed542019-08-21 12:01:14 -0700469
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900470 //////////////////////////////////////////////////////////////////////////
471 // Validation phase
472 //////////////////////////////////////////////////////////////////////////
473
Steven Morelande2c64b42018-09-18 15:06:37 -0700474 // For legacy reasons, by default, compiling an unstructured parcelable (which contains no output)
475 // is allowed. This must not be returned as an error until the very end of this procedure since
476 // this may be considered a success, and we should first check that there are not other, more
477 // serious failures.
478 bool contains_unstructured_parcelable = false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800479
Jooyung Hance6733e2021-05-22 02:44:17 +0900480 const auto& types = document->DefinedTypes();
Jiyong Park62515512020-06-08 15:57:11 +0900481 const int num_defined_types = types.size();
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900482 for (const auto& defined_type : types) {
Jooyung Hance6733e2021-05-22 02:44:17 +0900483 AIDL_FATAL_IF(defined_type == nullptr, document);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900484
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000485 // Ensure type is exactly one of the following:
486 AidlInterface* interface = defined_type->AsInterface();
487 AidlStructuredParcelable* parcelable = defined_type->AsStructuredParcelable();
488 AidlParcelable* unstructured_parcelable = defined_type->AsUnstructuredParcelable();
489 AidlEnumDeclaration* enum_decl = defined_type->AsEnumDeclaration();
Jooyung Han2946afc2020-10-05 20:29:16 +0900490 AidlUnionDecl* union_decl = defined_type->AsUnionDeclaration();
491 AIDL_FATAL_IF(
492 !!interface + !!parcelable + !!unstructured_parcelable + !!enum_decl + !!union_decl != 1,
493 defined_type);
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000494
495 // Ensure that foo.bar.IFoo is defined in <some_path>/foo/bar/IFoo.aidl
496 if (num_defined_types == 1 && !check_filename(input_file_name, *defined_type)) {
497 return AidlError::BAD_PACKAGE;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900498 }
499
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000500 {
501 bool valid_type = true;
502
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900503 if (!defined_type->CheckValid(*typenames)) {
504 valid_type = false;
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000505 }
506
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900507 if (!defined_type->LanguageSpecificCheckValid(*typenames, options.TargetLanguage())) {
508 valid_type = false;
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000509 }
510
511 if (!valid_type) {
Jeongik Cha82317dd2019-02-27 20:26:11 +0900512 return AidlError::BAD_TYPE;
513 }
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000514 }
515
516 if (unstructured_parcelable != nullptr) {
517 bool isStable = unstructured_parcelable->IsStableApiParcelable(options.TargetLanguage());
Jeongik Cha82317dd2019-02-27 20:26:11 +0900518 if (options.IsStructured() && !isStable) {
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000519 AIDL_ERROR(unstructured_parcelable)
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800520 << "Cannot declared parcelable in a --structured interface. Parcelable must be defined "
521 "in AIDL directly.";
522 return AidlError::NOT_STRUCTURED;
523 }
524 if (options.FailOnParcelable()) {
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000525 AIDL_ERROR(unstructured_parcelable)
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800526 << "Refusing to generate code with unstructured parcelables. Declared parcelables "
527 "should be in their own file and/or cannot be used with --structured interfaces.";
528 // Continue parsing for more errors
529 }
530
Steven Morelande2c64b42018-09-18 15:06:37 -0700531 contains_unstructured_parcelable = true;
Steven Morelande2c64b42018-09-18 15:06:37 -0700532 }
533
Devin Moore0d0e3f62020-03-30 17:45:39 -0700534 if (defined_type->IsVintfStability()) {
535 bool success = true;
536 if (options.GetStability() != Options::Stability::VINTF) {
537 AIDL_ERROR(defined_type)
538 << "Must compile @VintfStability type w/ aidl_interface 'stability: \"vintf\"'";
539 success = false;
540 }
541 if (!options.IsStructured()) {
542 AIDL_ERROR(defined_type)
543 << "Must compile @VintfStability type w/ aidl_interface --structured";
544 success = false;
545 }
546 if (!success) return AidlError::NOT_STRUCTURED;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700547 }
548
Jiyong Parkb034bf02018-07-30 17:44:33 +0900549 if (interface != nullptr) {
550 // add the meta-method 'int getInterfaceVersion()' if version is specified.
551 if (options.Version() > 0) {
552 AidlTypeSpecifier* ret =
Jooyung Han8451a202021-01-16 03:07:06 +0900553 new AidlTypeSpecifier(AIDL_LOCATION_HERE, "int", false, nullptr, Comments{});
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900554 ret->Resolve(*typenames);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900555 vector<unique_ptr<AidlArgument>>* args = new vector<unique_ptr<AidlArgument>>();
Jooyung Han3f347ca2020-12-01 12:41:50 +0900556 auto method = std::make_unique<AidlMethod>(
Jooyung Han8451a202021-01-16 03:07:06 +0900557 AIDL_LOCATION_HERE, false, ret, "getInterfaceVersion", args, Comments{},
558 kGetInterfaceVersionId, false /* is_user_defined */);
Jooyung Han3f347ca2020-12-01 12:41:50 +0900559 interface->AddMethod(std::move(method));
Jiyong Parkb034bf02018-07-30 17:44:33 +0900560 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900561 // add the meta-method 'string getInterfaceHash()' if hash is specified.
562 if (!options.Hash().empty()) {
563 AidlTypeSpecifier* ret =
Jooyung Han8451a202021-01-16 03:07:06 +0900564 new AidlTypeSpecifier(AIDL_LOCATION_HERE, "String", false, nullptr, Comments{});
Paul Trautrimb77048c2020-01-21 16:39:32 +0900565 ret->Resolve(*typenames);
566 vector<unique_ptr<AidlArgument>>* args = new vector<unique_ptr<AidlArgument>>();
Jooyung Han8451a202021-01-16 03:07:06 +0900567 auto method = std::make_unique<AidlMethod>(
568 AIDL_LOCATION_HERE, false, ret, kGetInterfaceHash, args, Comments{},
569 kGetInterfaceHashId, false /* is_user_defined */);
Jooyung Han3f347ca2020-12-01 12:41:50 +0900570 interface->AddMethod(std::move(method));
Paul Trautrimb77048c2020-01-21 16:39:32 +0900571 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900572 if (!check_and_assign_method_ids(interface->GetMethods())) {
573 return AidlError::BAD_METHOD_ID;
574 }
Jooyung Hancfe08002020-12-04 12:56:35 +0900575 }
Jooyung Han30f64ad2020-12-15 08:16:31 +0900576 // Verify the var/const declarations.
577 // const expressions should be non-empty when evaluated with the var/const type.
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900578 for (const auto& constant : defined_type->GetConstantDeclarations()) {
579 if (constant->ValueString(AidlConstantValueDecorator).empty()) {
580 return AidlError::BAD_TYPE;
Jooyung Han30f64ad2020-12-15 08:16:31 +0900581 }
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900582 }
583 for (const auto& var : defined_type->GetFields()) {
584 if (var->GetDefaultValue() && var->ValueString(AidlConstantValueDecorator).empty()) {
585 return AidlError::BAD_TYPE;
Will McVickerd7d18df2019-09-12 13:40:50 -0700586 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900587 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800588 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800589
Jooyung Hance6733e2021-05-22 02:44:17 +0900590 if (!ValidateAnnotationContext(*document)) {
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900591 return AidlError::BAD_TYPE;
592 }
593
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900594 if (!Diagnose(*document, options.GetDiagnosticMapping())) {
Jiyong Parkd9113322020-12-31 03:20:33 +0900595 return AidlError::BAD_TYPE;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900596 }
597
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900598 typenames->IterateTypes([&](const AidlDefinedType& type) {
Steven Morelanda57d0a62019-07-30 09:41:14 -0700599 if (options.IsStructured() && type.AsUnstructuredParcelable() != nullptr &&
Jeongik Cha88f95a82020-01-15 13:02:16 +0900600 !type.AsUnstructuredParcelable()->IsStableApiParcelable(options.TargetLanguage())) {
Steven Morelanda57d0a62019-07-30 09:41:14 -0700601 err = AidlError::NOT_STRUCTURED;
Devin Moore097a3ab2020-03-11 16:08:44 -0700602 AIDL_ERROR(type) << type.GetCanonicalName()
603 << " is not structured, but this is a structured interface.";
Steven Morelanda57d0a62019-07-30 09:41:14 -0700604 }
605 if (options.GetStability() == Options::Stability::VINTF && !type.IsVintfStability()) {
606 err = AidlError::NOT_STRUCTURED;
Devin Moore097a3ab2020-03-11 16:08:44 -0700607 AIDL_ERROR(type) << type.GetCanonicalName()
608 << " does not have VINTF level stability, but this interface requires it.";
Steven Morelanda57d0a62019-07-30 09:41:14 -0700609 }
Jiyong Parkf8d53612020-05-04 14:06:13 +0900610
Jeongik Chaef44e622020-10-23 16:00:52 +0900611 // Ensure that untyped List/Map is not used in a parcelable, a union and a stable interface.
Jiyong Parkf8d53612020-05-04 14:06:13 +0900612
Jeongik Chaef44e622020-10-23 16:00:52 +0900613 std::function<void(const AidlTypeSpecifier&, const AidlNode*)> check_untyped_container =
614 [&err, &check_untyped_container](const AidlTypeSpecifier& type, const AidlNode* node) {
615 if (type.IsGeneric()) {
616 std::for_each(type.GetTypeParameters().begin(), type.GetTypeParameters().end(),
617 [&node, &check_untyped_container](auto& nested) {
618 check_untyped_container(*nested, node);
619 });
620 return;
Jiyong Parkf8d53612020-05-04 14:06:13 +0900621 }
Jeongik Chaef44e622020-10-23 16:00:52 +0900622 if (type.GetName() == "List" || type.GetName() == "Map") {
623 err = AidlError::BAD_TYPE;
624 AIDL_ERROR(node)
625 << "Encountered an untyped List or Map. The use of untyped List/Map is prohibited "
626 << "because it is not guaranteed that the objects in the list are recognizable in "
627 << "the receiving side. Consider switching to an array or a generic List/Map.";
628 }
629 };
Jeongik Chaef44e622020-10-23 16:00:52 +0900630
Jooyung Han829ec7c2020-12-02 12:07:36 +0900631 if (type.AsInterface() && options.IsStructured()) {
632 for (const auto& method : type.GetMethods()) {
Jeongik Chaef44e622020-10-23 16:00:52 +0900633 check_untyped_container(method->GetType(), method.get());
634 for (const auto& arg : method->GetArguments()) {
635 check_untyped_container(arg->GetType(), method.get());
Jiyong Parkf8d53612020-05-04 14:06:13 +0900636 }
Jeongik Chaef44e622020-10-23 16:00:52 +0900637 }
Jooyung Han829ec7c2020-12-02 12:07:36 +0900638 }
639 for (const auto& field : type.GetFields()) {
640 check_untyped_container(field->GetType(), field.get());
Jiyong Parkf8d53612020-05-04 14:06:13 +0900641 }
Steven Morelanda57d0a62019-07-30 09:41:14 -0700642 });
643
Steven Moreland6cee3482018-07-18 14:39:58 -0700644 if (err != AidlError::OK) {
645 return err;
646 }
647
Jiyong Parkb034bf02018-07-30 17:44:33 +0900648 if (imported_files != nullptr) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900649 *imported_files = import_paths;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700650 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700651
Steven Morelande2c64b42018-09-18 15:06:37 -0700652 if (contains_unstructured_parcelable) {
653 // Considered a success for the legacy case, so this must be returned last.
654 return AidlError::FOUND_PARCELABLE;
655 }
656
Christopher Wiley632801d2015-11-05 14:15:49 -0800657 return AidlError::OK;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700658}
659
Casey Dahlin2cc93162015-10-02 16:14:17 -0700660} // namespace internals
661
Jiyong Parkb034bf02018-07-30 17:44:33 +0900662int compile_aidl(const Options& options, const IoDelegate& io_delegate) {
663 const Options::Language lang = options.TargetLanguage();
Jiyong Park74595c12018-07-23 15:22:50 +0900664 for (const string& input_file : options.InputFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900665 AidlTypenames typenames;
Jiyong Park74595c12018-07-23 15:22:50 +0900666
Jiyong Parkb034bf02018-07-30 17:44:33 +0900667 vector<string> imported_files;
Jiyong Park74595c12018-07-23 15:22:50 +0900668
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900669 AidlError aidl_err = internals::load_and_validate_aidl(input_file, options, io_delegate,
670 &typenames, &imported_files);
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800671 bool allowError = aidl_err == AidlError::FOUND_PARCELABLE && !options.FailOnParcelable();
672 if (aidl_err != AidlError::OK && !allowError) {
Jiyong Park74595c12018-07-23 15:22:50 +0900673 return 1;
674 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700675
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900676 for (const auto& defined_type : typenames.MainDocument().DefinedTypes()) {
Steven Moreland21780812020-09-11 01:29:45 +0000677 AIDL_FATAL_IF(defined_type == nullptr, input_file);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700678
Jiyong Parkb034bf02018-07-30 17:44:33 +0900679 string output_file_name = options.OutputFile();
680 // if needed, generate the output file name from the base folder
681 if (output_file_name.empty() && !options.OutputDir().empty()) {
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900682 output_file_name = GetOutputFilePath(options, *defined_type);
Jiyong Parkb03551f2018-08-06 19:20:51 +0900683 if (output_file_name.empty()) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900684 return 1;
685 }
686 }
Jiyong Park74595c12018-07-23 15:22:50 +0900687
Jiyong Parkb034bf02018-07-30 17:44:33 +0900688 if (!write_dep_file(options, *defined_type, imported_files, io_delegate, input_file,
689 output_file_name)) {
690 return 1;
691 }
Jiyong Park74595c12018-07-23 15:22:50 +0900692
Jiyong Parkb034bf02018-07-30 17:44:33 +0900693 bool success = false;
694 if (lang == Options::Language::CPP) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900695 success =
696 cpp::GenerateCpp(output_file_name, options, typenames, *defined_type, io_delegate);
Steven Morelandc26d8142018-09-17 14:25:33 -0700697 } else if (lang == Options::Language::NDK) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900698 ndk::GenerateNdk(output_file_name, options, typenames, *defined_type, io_delegate);
Steven Morelandc26d8142018-09-17 14:25:33 -0700699 success = true;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900700 } else if (lang == Options::Language::JAVA) {
Jiyong Park9ca5c7e2019-10-17 15:01:14 +0900701 if (defined_type->AsUnstructuredParcelable() != nullptr) {
702 // Legacy behavior. For parcelable declarations in Java, don't generate output file.
703 success = true;
704 } else {
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900705 success = java::generate_java(output_file_name, defined_type.get(), typenames,
706 io_delegate, options);
Jiyong Park9ca5c7e2019-10-17 15:01:14 +0900707 }
Andrei Homescub62afd92020-05-11 19:24:59 -0700708 } else if (lang == Options::Language::RUST) {
709 success = rust::GenerateRust(output_file_name, defined_type.get(), typenames, io_delegate,
710 options);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900711 } else {
Steven Moreland21780812020-09-11 01:29:45 +0000712 AIDL_FATAL(input_file) << "Should not reach here.";
Jiyong Parkb034bf02018-07-30 17:44:33 +0900713 }
714 if (!success) {
715 return 1;
716 }
Jiyong Park74595c12018-07-23 15:22:50 +0900717 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700718 }
Jiyong Park74595c12018-07-23 15:22:50 +0900719 return 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800720}
721
Andrei Onea8714b022019-02-01 18:55:54 +0000722bool dump_mappings(const Options& options, const IoDelegate& io_delegate) {
723 android::aidl::mappings::SignatureMap all_mappings;
724 for (const string& input_file : options.InputFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900725 AidlTypenames typenames;
Andrei Onea8714b022019-02-01 18:55:54 +0000726 vector<string> imported_files;
727
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900728 AidlError aidl_err = internals::load_and_validate_aidl(input_file, options, io_delegate,
729 &typenames, &imported_files);
Andrei Onea8714b022019-02-01 18:55:54 +0000730 if (aidl_err != AidlError::OK) {
Jiyong Park3a060392020-04-11 21:02:19 +0900731 return false;
Andrei Onea8714b022019-02-01 18:55:54 +0000732 }
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900733 for (const auto& defined_type : typenames.MainDocument().DefinedTypes()) {
734 auto mappings = mappings::generate_mappings(defined_type.get(), typenames);
Andrei Onea8714b022019-02-01 18:55:54 +0000735 all_mappings.insert(mappings.begin(), mappings.end());
736 }
737 }
738 std::stringstream mappings_str;
739 for (const auto& mapping : all_mappings) {
740 mappings_str << mapping.first << "\n" << mapping.second << "\n";
741 }
742 auto code_writer = io_delegate.GetCodeWriter(options.OutputFile());
743 code_writer->Write("%s", mappings_str.str().c_str());
744 return true;
745}
746
Steven Moreland3981d9e2020-03-31 14:11:44 -0700747int aidl_entry(const Options& options, const IoDelegate& io_delegate) {
Steven Moreland33efcf62020-04-10 16:40:43 -0700748 AidlErrorLog::clearError();
749
Steven Moreland3981d9e2020-03-31 14:11:44 -0700750 int ret = 1;
751 switch (options.GetTask()) {
752 case Options::Task::COMPILE:
753 ret = android::aidl::compile_aidl(options, io_delegate);
754 break;
755 case Options::Task::PREPROCESS:
Jooyung Han93f48f02021-06-05 00:11:16 +0900756 ret = android::aidl::Preprocess(options, io_delegate) ? 0 : 1;
Steven Moreland3981d9e2020-03-31 14:11:44 -0700757 break;
758 case Options::Task::DUMP_API:
759 ret = android::aidl::dump_api(options, io_delegate) ? 0 : 1;
760 break;
761 case Options::Task::CHECK_API:
762 ret = android::aidl::check_api(options, io_delegate) ? 0 : 1;
763 break;
764 case Options::Task::DUMP_MAPPINGS:
765 ret = android::aidl::dump_mappings(options, io_delegate) ? 0 : 1;
766 break;
767 default:
768 AIDL_FATAL(AIDL_LOCATION_HERE)
769 << "Unrecognized task: " << static_cast<size_t>(options.GetTask());
770 }
771
772 // compiler invariants
Steven Moreland21780812020-09-11 01:29:45 +0000773 const bool shouldReportError = ret != 0;
774 const bool reportedError = AidlErrorLog::hadError();
775 AIDL_FATAL_IF(shouldReportError != reportedError, AIDL_LOCATION_HERE)
776 << "Compiler returned error " << ret << " but did" << (reportedError ? "" : " not")
777 << " emit error logs";
Steven Moreland3981d9e2020-03-31 14:11:44 -0700778
779 return ret;
780}
781
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700782} // namespace aidl
Steven Morelandf4c64df2019-07-29 19:54:04 -0700783} // namespace android