blob: 6938b53e91398067527ce328198a0f13bf4e2815 [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"
Jooyung Han74406c52021-11-06 06:17:36 +090043#include "check_valid.h"
Andrei Onea8714b022019-02-01 18:55:54 +000044#include "generate_aidl_mappings.h"
Christopher Wileyeb1acc12015-09-16 11:25:13 -070045#include "generate_cpp.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070046#include "generate_java.h"
Steven Morelandc26d8142018-09-17 14:25:33 -070047#include "generate_ndk.h"
Andrei Homescub62afd92020-05-11 19:24:59 -070048#include "generate_rust.h"
Christopher Wiley72877ac2015-10-06 14:41:42 -070049#include "import_resolver.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070050#include "logging.h"
51#include "options.h"
52#include "os.h"
Jiyong Parke5c45292020-05-26 19:06:24 +090053#include "parser.h"
Jooyung Han93f48f02021-06-05 00:11:16 +090054#include "preprocess.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070055
Adam Lesinskiffa16862014-01-23 18:17:42 -080056#ifndef O_BINARY
57# define O_BINARY 0
58#endif
59
Christopher Wiley3a9911c2016-01-19 12:59:09 -080060using android::base::Join;
Christopher Wileyd76067c2015-10-19 17:00:13 -070061using android::base::Split;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070062using std::set;
63using std::string;
Christopher Wiley84c1eac2015-09-23 13:29:28 -070064using std::unique_ptr;
Jooyung Hand25527a2020-12-30 12:29:31 +090065using std::unordered_set;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070066using std::vector;
Adam Lesinskiffa16862014-01-23 18:17:42 -080067
Christopher Wileyf690be52015-09-14 15:19:10 -070068namespace android {
69namespace aidl {
70namespace {
Adam Lesinskiffa16862014-01-23 18:17:42 -080071
Jiyong Park965c5b92018-11-21 13:37:15 +090072// Copied from android.is.IBinder.[FIRST|LAST]_CALL_TRANSACTION
73const int kFirstCallTransaction = 1;
74const int kLastCallTransaction = 0x00ffffff;
75
76// Following IDs are all offsets from kFirstCallTransaction
Jiyong Park309668e2018-07-28 16:55:44 +090077
78// IDs for meta transactions. Most of the meta transactions are implemented in
79// the framework side (Binder.java or Binder.cpp). But these are the ones that
80// are auto-implemented by the AIDL compiler.
Jiyong Park965c5b92018-11-21 13:37:15 +090081const int kFirstMetaMethodId = kLastCallTransaction - kFirstCallTransaction;
82const int kGetInterfaceVersionId = kFirstMetaMethodId;
Paul Trautrimb77048c2020-01-21 16:39:32 +090083const int kGetInterfaceHashId = kFirstMetaMethodId - 1;
Jiyong Park965c5b92018-11-21 13:37:15 +090084// Additional meta transactions implemented by AIDL should use
85// kFirstMetaMethodId -1, -2, ...and so on.
86
87// Reserve 100 IDs for meta methods, which is more than enough. If we don't reserve,
88// in the future, a newly added meta transaction ID will have a chance to
89// collide with the user-defined methods that were added in the past. So,
90// let's prevent users from using IDs in this range from the beginning.
91const int kLastMetaMethodId = kFirstMetaMethodId - 99;
92
93// Range of IDs that is allowed for user-defined methods.
94const int kMinUserSetMethodId = 0;
95const int kMaxUserSetMethodId = kLastMetaMethodId - 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -080096
Steven Moreland92c55f12018-07-31 14:08:37 -070097bool check_filename(const std::string& filename, const AidlDefinedType& defined_type) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080098 const char* p;
99 string expected;
100 string fn;
101 size_t len;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800102 bool valid = false;
103
Christopher Wileybc2df692016-06-02 16:27:26 -0700104 if (!IoDelegate::GetAbsolutePath(filename, &fn)) {
105 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800106 }
107
Steven Moreland92c55f12018-07-31 14:08:37 -0700108 const std::string package = defined_type.GetPackage();
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700109 if (!package.empty()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800110 expected = package;
111 expected += '.';
112 }
113
114 len = expected.length();
115 for (size_t i=0; i<len; i++) {
116 if (expected[i] == '.') {
117 expected[i] = OS_PATH_SEPARATOR;
118 }
119 }
120
Steven Moreland92c55f12018-07-31 14:08:37 -0700121 const std::string name = defined_type.GetName();
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700122 expected.append(name, 0, name.find('.'));
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700123
Adam Lesinskiffa16862014-01-23 18:17:42 -0800124 expected += ".aidl";
125
126 len = fn.length();
127 valid = (len >= expected.length());
128
129 if (valid) {
130 p = fn.c_str() + (len - expected.length());
131
Elliott Hughesce310da2015-07-29 08:44:17 -0700132#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800133 if (OS_PATH_SEPARATOR != '/') {
134 // Input filename under cygwin most likely has / separators
135 // whereas the expected string uses \\ separators. Adjust
136 // them accordingly.
137 for (char *c = const_cast<char *>(p); *c; ++c) {
138 if (*c == '/') *c = OS_PATH_SEPARATOR;
139 }
140 }
141#endif
142
Yabin Cui482eefb2014-11-10 15:01:43 -0800143 // aidl assumes case-insensitivity on Mac Os and Windows.
144#if defined(__linux__)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800145 valid = (expected == p);
146#else
147 valid = !strcasecmp(expected.c_str(), p);
148#endif
149 }
150
151 if (!valid) {
Steven Moreland92c55f12018-07-31 14:08:37 -0700152 AIDL_ERROR(defined_type) << name << " should be declared in a file called " << expected;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800153 }
154
Casey Dahlin42727f82015-10-12 19:23:40 -0700155 return valid;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800156}
157
Jiyong Park74595c12018-07-23 15:22:50 +0900158bool write_dep_file(const Options& options, const AidlDefinedType& defined_type,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900159 const vector<string>& imports, const IoDelegate& io_delegate,
Jiyong Park74595c12018-07-23 15:22:50 +0900160 const string& input_file, const string& output_file) {
161 string dep_file_name = options.DependencyFile();
162 if (dep_file_name.empty() && options.AutoDepFile()) {
163 dep_file_name = output_file + ".d";
164 }
165
166 if (dep_file_name.empty()) {
167 return true; // nothing to do
168 }
Jiyong Park05463732018-08-09 16:03:02 +0900169
Jiyong Park74595c12018-07-23 15:22:50 +0900170 CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name);
171 if (!writer) {
Steven Moreland21780812020-09-11 01:29:45 +0000172 AIDL_ERROR(dep_file_name) << "Could not open dependency file.";
Jiyong Park74595c12018-07-23 15:22:50 +0900173 return false;
174 }
175
176 vector<string> source_aidl = {input_file};
177 for (const auto& import : imports) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900178 source_aidl.push_back(import);
Jiyong Park74595c12018-07-23 15:22:50 +0900179 }
180
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800181 // Encode that the output file depends on aidl input files.
Jiyong Parkdf202122019-09-30 20:48:35 +0900182 if (defined_type.AsUnstructuredParcelable() != nullptr &&
183 options.TargetLanguage() == Options::Language::JAVA) {
184 // Legacy behavior. For parcelable declarations in Java, don't emit output file as
185 // the dependency target. b/141372861
186 writer->Write(" : \\\n");
187 } else {
188 writer->Write("%s : \\\n", output_file.c_str());
189 }
Jiyong Park74595c12018-07-23 15:22:50 +0900190 writer->Write(" %s", Join(source_aidl, " \\\n ").c_str());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800191 writer->Write("\n");
Christopher Wileya30a45e2015-10-17 10:56:59 -0700192
Jiyong Park74595c12018-07-23 15:22:50 +0900193 if (!options.DependencyFileNinja()) {
Dan Willemsen93298ee2016-11-10 23:55:55 -0800194 writer->Write("\n");
195 // Output "<input_aidl_file>: " so make won't fail if the input .aidl file
196 // has been deleted, moved or renamed in incremental build.
Jiyong Park74595c12018-07-23 15:22:50 +0900197 for (const auto& src : source_aidl) {
Dan Willemsen93298ee2016-11-10 23:55:55 -0800198 writer->Write("%s :\n", src.c_str());
199 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800200 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700201
Steven Morelandc26d8142018-09-17 14:25:33 -0700202 if (options.IsCppOutput()) {
Jiyong Park74595c12018-07-23 15:22:50 +0900203 if (!options.DependencyFileNinja()) {
204 using ::android::aidl::cpp::ClassNames;
205 using ::android::aidl::cpp::HeaderFile;
206 vector<string> headers;
Jiyong Park5b7e5322019-04-03 20:05:01 +0900207 for (ClassNames c : {ClassNames::CLIENT, ClassNames::SERVER, ClassNames::RAW}) {
Jiyong Park05463732018-08-09 16:03:02 +0900208 headers.push_back(options.OutputHeaderDir() +
Jiyong Park74595c12018-07-23 15:22:50 +0900209 HeaderFile(defined_type, c, false /* use_os_sep */));
210 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800211
Jiyong Park74595c12018-07-23 15:22:50 +0900212 writer->Write("\n");
213
214 // Generated headers also depend on the source aidl files.
215 writer->Write("%s : \\\n %s\n", Join(headers, " \\\n ").c_str(),
216 Join(source_aidl, " \\\n ").c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800217 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700218 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800219
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800220 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800221}
222
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900223// Returns the path to the destination file of `defined_type`.
224string GetOutputFilePath(const Options& options, const AidlDefinedType& defined_type) {
Jiyong Park74595c12018-07-23 15:22:50 +0900225 string result = options.OutputDir();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800226
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900227 // add the package
Jiyong Park74595c12018-07-23 15:22:50 +0900228 string package = defined_type.GetPackage();
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900229 if (!package.empty()) {
230 for (auto& c : package) {
231 if (c == '.') {
232 c = OS_PATH_SEPARATOR;
233 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800234 }
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900235 result += package;
236 result += OS_PATH_SEPARATOR;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700237 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800238
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900239 // add the filename
240 result += defined_type.GetName();
Jiyong Parkb03551f2018-08-06 19:20:51 +0900241 if (options.TargetLanguage() == Options::Language::JAVA) {
242 result += ".java";
Steven Morelandc26d8142018-09-17 14:25:33 -0700243 } else if (options.IsCppOutput()) {
Jiyong Parkb03551f2018-08-06 19:20:51 +0900244 result += ".cpp";
Andrei Homescub62afd92020-05-11 19:24:59 -0700245 } else if (options.TargetLanguage() == Options::Language::RUST) {
246 result += ".rs";
Jiyong Parkb03551f2018-08-06 19:20:51 +0900247 } else {
Steven Moreland21780812020-09-11 01:29:45 +0000248 AIDL_FATAL("Unknown target language");
Jiyong Parkb03551f2018-08-06 19:20:51 +0900249 return "";
250 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800251
Steven Moreland5557f1c2018-07-02 13:50:23 -0700252 return result;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800253}
254
Jooyung Han2a86ba82021-12-02 01:41:19 +0900255bool CheckAndAssignMethodIDs(const std::vector<std::unique_ptr<AidlMethod>>& items) {
Jiyong Park309668e2018-07-28 16:55:44 +0900256 // Check whether there are any methods with manually assigned id's and any
257 // that are not. Either all method id's must be manually assigned or all of
258 // them must not. Also, check for uplicates of user set ID's and that the
259 // ID's are within the proper bounds.
260 set<int> usedIds;
261 bool hasUnassignedIds = false;
262 bool hasAssignedIds = false;
Steven Morelandec6f4692019-08-13 10:03:24 -0700263 int newId = kMinUserSetMethodId;
Jiyong Park309668e2018-07-28 16:55:44 +0900264 for (const auto& item : items) {
265 // However, meta transactions that are added by the AIDL compiler are
266 // exceptions. They have fixed IDs but allowed to be with user-defined
267 // methods having auto-assigned IDs. This is because the Ids of the meta
268 // transactions must be stable during the entire lifetime of an interface.
269 // In other words, their IDs must be the same even when new user-defined
270 // methods are added.
Jiyong Park3633b722019-04-11 15:38:26 +0900271 if (!item->IsUserDefined()) {
272 continue;
273 }
274 if (item->HasId()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900275 hasAssignedIds = true;
Jiyong Park309668e2018-07-28 16:55:44 +0900276 } else {
Steven Morelandec6f4692019-08-13 10:03:24 -0700277 item->SetId(newId++);
Jiyong Park309668e2018-07-28 16:55:44 +0900278 hasUnassignedIds = true;
279 }
Steven Morelandec6f4692019-08-13 10:03:24 -0700280
Jiyong Park309668e2018-07-28 16:55:44 +0900281 if (hasAssignedIds && hasUnassignedIds) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900282 AIDL_ERROR(item) << "You must either assign id's to all methods or to none of them.";
Jiyong Park309668e2018-07-28 16:55:44 +0900283 return false;
284 }
Jiyong Park309668e2018-07-28 16:55:44 +0900285
Steven Morelandec6f4692019-08-13 10:03:24 -0700286 // Ensure that the user set id is not duplicated.
287 if (usedIds.find(item->GetId()) != usedIds.end()) {
288 // We found a duplicate id, so throw an error.
289 AIDL_ERROR(item) << "Found duplicate method id (" << item->GetId() << ") for method "
290 << item->GetName();
291 return false;
292 }
293 usedIds.insert(item->GetId());
294
295 // Ensure that the user set id is within the appropriate limits
296 if (item->GetId() < kMinUserSetMethodId || item->GetId() > kMaxUserSetMethodId) {
297 AIDL_ERROR(item) << "Found out of bounds id (" << item->GetId() << ") for method "
298 << item->GetName() << ". Value for id must be between "
299 << kMinUserSetMethodId << " and " << kMaxUserSetMethodId << " inclusive.";
300 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800301 }
Jiyong Park309668e2018-07-28 16:55:44 +0900302 }
Steven Morelandec6f4692019-08-13 10:03:24 -0700303
Jiyong Park309668e2018-07-28 16:55:44 +0900304 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800305}
306
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900307bool ValidateAnnotationContext(const AidlDocument& doc) {
308 struct AnnotationValidator : AidlVisitor {
309 bool success = true;
310
311 void Check(const AidlAnnotatable& annotatable, AidlAnnotation::TargetContext context) {
312 for (const auto& annot : annotatable.GetAnnotations()) {
Steven Morelanda7560e82021-10-08 16:24:39 -0700313 if (!annot->CheckContext(context)) {
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900314 success = false;
315 }
316 }
317 }
318 void Visit(const AidlInterface& m) override {
319 Check(m, AidlAnnotation::CONTEXT_TYPE_INTERFACE);
320 }
321 void Visit(const AidlParcelable& m) override {
322 Check(m, AidlAnnotation::CONTEXT_TYPE_UNSTRUCTURED_PARCELABLE);
323 }
324 void Visit(const AidlStructuredParcelable& m) override {
325 Check(m, AidlAnnotation::CONTEXT_TYPE_STRUCTURED_PARCELABLE);
326 }
327 void Visit(const AidlEnumDeclaration& m) override {
328 Check(m, AidlAnnotation::CONTEXT_TYPE_ENUM);
329 }
330 void Visit(const AidlUnionDecl& m) override { Check(m, AidlAnnotation::CONTEXT_TYPE_UNION); }
331 void Visit(const AidlMethod& m) override {
332 Check(m.GetType(), AidlAnnotation::CONTEXT_TYPE_SPECIFIER | AidlAnnotation::CONTEXT_METHOD);
333 for (const auto& arg : m.GetArguments()) {
334 Check(arg->GetType(), AidlAnnotation::CONTEXT_TYPE_SPECIFIER);
335 }
336 }
337 void Visit(const AidlConstantDeclaration& m) override {
338 Check(m.GetType(), AidlAnnotation::CONTEXT_TYPE_SPECIFIER | AidlAnnotation::CONTEXT_CONST);
339 }
340 void Visit(const AidlVariableDeclaration& m) override {
341 Check(m.GetType(), AidlAnnotation::CONTEXT_TYPE_SPECIFIER | AidlAnnotation::CONTEXT_FIELD);
342 }
343 void Visit(const AidlTypeSpecifier& m) override {
344 // nested generic type parameters are checked as well
345 if (m.IsGeneric()) {
346 for (const auto& tp : m.GetTypeParameters()) {
347 Check(*tp, AidlAnnotation::CONTEXT_TYPE_SPECIFIER);
348 }
349 }
350 }
351 };
352
353 AnnotationValidator validator;
354 VisitTopDown(validator, doc);
355 return validator.success;
356}
357
Jooyung Han2cb8ecd2021-07-28 18:41:30 +0900358bool ValidateCppHeader(const AidlDocument& doc) {
359 struct CppHeaderVisitor : AidlVisitor {
360 bool success = true;
361 void Visit(const AidlParcelable& p) override {
362 if (p.GetCppHeader().empty()) {
363 AIDL_ERROR(p) << "Unstructured parcelable \"" << p.GetName()
364 << "\" must have C++ header defined.";
365 success = false;
366 }
367 }
368 void Visit(const AidlTypeSpecifier& m) override {
369 auto type = m.GetDefinedType();
370 if (type) {
371 auto unstructured = type->AsUnstructuredParcelable();
372 if (unstructured && unstructured->GetCppHeader().empty()) {
373 AIDL_ERROR(m) << "Unstructured parcelable \"" << m.GetUnresolvedName()
374 << "\" must have C++ header defined.";
375 success = false;
376 }
377 }
378 }
379 };
380
381 CppHeaderVisitor validator;
382 VisitTopDown(validator, doc);
383 return validator.success;
384}
385
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700386} // namespace
387
388namespace internals {
389
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900390AidlError load_and_validate_aidl(const std::string& input_file_name, const Options& options,
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900391 const IoDelegate& io_delegate, AidlTypenames* typenames,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900392 vector<string>* imported_files) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800393 AidlError err = AidlError::OK;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800394
Jiyong Parkb034bf02018-07-30 17:44:33 +0900395 //////////////////////////////////////////////////////////////////////////
396 // Loading phase
397 //////////////////////////////////////////////////////////////////////////
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900398
Jiyong Parkb034bf02018-07-30 17:44:33 +0900399 // Parse the main input file
Jooyung Hance6733e2021-05-22 02:44:17 +0900400 const AidlDocument* document = Parser::Parse(input_file_name, io_delegate, *typenames);
401 if (document == nullptr) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900402 return AidlError::PARSE_ERROR;
403 }
Jooyung Han2946afc2020-10-05 20:29:16 +0900404 int num_top_level_decls = 0;
Jooyung Hance6733e2021-05-22 02:44:17 +0900405 for (const auto& type : document->DefinedTypes()) {
Jooyung Han2946afc2020-10-05 20:29:16 +0900406 if (type->AsUnstructuredParcelable() == nullptr) {
407 num_top_level_decls++;
408 if (num_top_level_decls > 1) {
Devin Moore5de18ed2020-04-02 13:52:29 -0700409 AIDL_ERROR(*type) << "You must declare only one type per file.";
410 return AidlError::BAD_TYPE;
411 }
Jiyong Parkda8c6932019-08-12 19:56:08 +0900412 }
413 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900414
415 // Import the preprocessed file
Jooyung Han93f48f02021-06-05 00:11:16 +0900416 for (const string& filename : options.PreprocessedFiles()) {
417 auto preprocessed = Parser::Parse(filename, io_delegate, *typenames, /*is_preprocessed=*/true);
418 if (!preprocessed) {
419 return AidlError::BAD_PRE_PROCESSED_FILE;
Christopher Wileyef140932015-11-03 09:29:19 -0800420 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700421 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800422
Jiyong Parkb034bf02018-07-30 17:44:33 +0900423 // Find files to import and parse them
Jiyong Parke59c3682018-09-11 23:10:25 +0900424 vector<string> import_paths;
Jooyung Han6529b822021-06-11 22:05:26 +0900425 ImportResolver import_resolver{io_delegate, input_file_name, options.ImportDirs()};
Jooyung Hance6733e2021-05-22 02:44:17 +0900426 for (const auto& import : document->Imports()) {
Jooyung Hancdf89ec2021-10-29 14:08:30 +0900427 if (typenames->IsIgnorableImport(import)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700428 // There are places in the Android tree where an import doesn't resolve,
429 // but we'll pick the type up through the preprocessed types.
430 // This seems like an error, but legacy support demands we support it...
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700431 continue;
432 }
Jooyung Hancdf89ec2021-10-29 14:08:30 +0900433 string import_path = import_resolver.FindImportFile(import);
Christopher Wiley72877ac2015-10-06 14:41:42 -0700434 if (import_path.empty()) {
Jooyung Hancdf89ec2021-10-29 14:08:30 +0900435 AIDL_ERROR(input_file_name) << "Couldn't find import for class " << import;
Jooyung Han29813842020-12-08 01:28:03 +0900436 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700437 continue;
438 }
Casey Dahlin2cc93162015-10-02 16:14:17 -0700439
Jiyong Parke59c3682018-09-11 23:10:25 +0900440 import_paths.emplace_back(import_path);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900441
Jooyung Hance6733e2021-05-22 02:44:17 +0900442 auto imported_doc = Parser::Parse(import_path, io_delegate, *typenames);
443 if (imported_doc == nullptr) {
Jooyung Hancdf89ec2021-10-29 14:08:30 +0900444 AIDL_ERROR(import_path) << "error while importing " << import_path << " for " << import;
Christopher Wiley632801d2015-11-05 14:15:49 -0800445 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700446 continue;
447 }
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700448 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800449 if (err != AidlError::OK) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700450 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700451 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800452
Jooyung Hance6733e2021-05-22 02:44:17 +0900453 TypeResolver resolver = [&](const AidlDefinedType* scope, AidlTypeSpecifier* type) {
Jooyung Han13f1fa52021-06-11 18:06:12 +0900454 // resolve with already loaded types
455 if (type->Resolve(*typenames, scope)) {
456 return true;
Jooyung Han29813842020-12-08 01:28:03 +0900457 }
Jooyung Han13f1fa52021-06-11 18:06:12 +0900458 const string import_path = import_resolver.FindImportFile(scope->ResolveName(type->GetName()));
Jooyung Han29813842020-12-08 01:28:03 +0900459 if (import_path.empty()) {
460 return false;
461 }
462 import_paths.push_back(import_path);
Jooyung Hance6733e2021-05-22 02:44:17 +0900463 auto imported_doc = Parser::Parse(import_path, io_delegate, *typenames);
464 if (imported_doc == nullptr) {
Jooyung Han29813842020-12-08 01:28:03 +0900465 AIDL_ERROR(import_path) << "error while importing " << import_path << " for " << import_path;
466 return false;
467 }
Jooyung Han13f1fa52021-06-11 18:06:12 +0900468
469 // now, try to resolve it again
470 if (!type->Resolve(*typenames, scope)) {
Jooyung Han29813842020-12-08 01:28:03 +0900471 AIDL_ERROR(type) << "Can't resolve " << type->GetName();
472 return false;
473 }
474 return true;
475 };
Jiyong Parkb034bf02018-07-30 17:44:33 +0900476
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900477 // Resolve the unresolved references
478 if (!ResolveReferences(*document, resolver)) {
Jiyong Park1deecc32018-07-17 01:14:41 +0900479 return AidlError::BAD_TYPE;
480 }
Daniel Norman85aed542019-08-21 12:01:14 -0700481
Jooyung Han672557b2020-12-24 05:18:00 +0900482 if (!typenames->Autofill()) {
483 return AidlError::BAD_TYPE;
Steven Moreland59e53e42019-11-26 20:38:08 -0800484 }
Daniel Norman85aed542019-08-21 12:01:14 -0700485
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900486 //////////////////////////////////////////////////////////////////////////
487 // Validation phase
488 //////////////////////////////////////////////////////////////////////////
489
Jooyung Hance6733e2021-05-22 02:44:17 +0900490 const auto& types = document->DefinedTypes();
Jiyong Park62515512020-06-08 15:57:11 +0900491 const int num_defined_types = types.size();
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900492 for (const auto& defined_type : types) {
Jooyung Hance6733e2021-05-22 02:44:17 +0900493 AIDL_FATAL_IF(defined_type == nullptr, document);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900494
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000495 // Ensure type is exactly one of the following:
496 AidlInterface* interface = defined_type->AsInterface();
497 AidlStructuredParcelable* parcelable = defined_type->AsStructuredParcelable();
498 AidlParcelable* unstructured_parcelable = defined_type->AsUnstructuredParcelable();
499 AidlEnumDeclaration* enum_decl = defined_type->AsEnumDeclaration();
Jooyung Han2946afc2020-10-05 20:29:16 +0900500 AidlUnionDecl* union_decl = defined_type->AsUnionDeclaration();
501 AIDL_FATAL_IF(
502 !!interface + !!parcelable + !!unstructured_parcelable + !!enum_decl + !!union_decl != 1,
503 defined_type);
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000504
505 // Ensure that foo.bar.IFoo is defined in <some_path>/foo/bar/IFoo.aidl
506 if (num_defined_types == 1 && !check_filename(input_file_name, *defined_type)) {
507 return AidlError::BAD_PACKAGE;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900508 }
509
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000510 {
511 bool valid_type = true;
512
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900513 if (!defined_type->CheckValid(*typenames)) {
514 valid_type = false;
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000515 }
516
Jooyung Hanb4997aa2021-10-16 03:26:12 +0900517 if (!defined_type->LanguageSpecificCheckValid(options.TargetLanguage())) {
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900518 valid_type = false;
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000519 }
520
521 if (!valid_type) {
Jeongik Cha82317dd2019-02-27 20:26:11 +0900522 return AidlError::BAD_TYPE;
523 }
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000524 }
525
526 if (unstructured_parcelable != nullptr) {
Jooyung Hana97b5882021-11-02 10:32:54 +0900527 auto lang = options.TargetLanguage();
528 bool isStable = unstructured_parcelable->IsStableApiParcelable(lang);
Jeongik Cha82317dd2019-02-27 20:26:11 +0900529 if (options.IsStructured() && !isStable) {
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000530 AIDL_ERROR(unstructured_parcelable)
Jooyung Han2cb8ecd2021-07-28 18:41:30 +0900531 << "Cannot declare unstructured parcelable in a --structured interface. Parcelable "
532 "must be defined in AIDL directly.";
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800533 return AidlError::NOT_STRUCTURED;
534 }
Jooyung Hana97b5882021-11-02 10:32:54 +0900535 if (options.FailOnParcelable() || lang == Options::Language::NDK ||
536 lang == Options::Language::RUST) {
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000537 AIDL_ERROR(unstructured_parcelable)
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800538 << "Refusing to generate code with unstructured parcelables. Declared parcelables "
539 "should be in their own file and/or cannot be used with --structured interfaces.";
Jooyung Han05a532a2021-08-18 10:41:40 +0900540 return AidlError::FOUND_PARCELABLE;
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800541 }
Steven Morelande2c64b42018-09-18 15:06:37 -0700542 }
543
Devin Moore0d0e3f62020-03-30 17:45:39 -0700544 if (defined_type->IsVintfStability()) {
545 bool success = true;
546 if (options.GetStability() != Options::Stability::VINTF) {
547 AIDL_ERROR(defined_type)
548 << "Must compile @VintfStability type w/ aidl_interface 'stability: \"vintf\"'";
549 success = false;
550 }
551 if (!options.IsStructured()) {
552 AIDL_ERROR(defined_type)
553 << "Must compile @VintfStability type w/ aidl_interface --structured";
554 success = false;
555 }
556 if (!success) return AidlError::NOT_STRUCTURED;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700557 }
558
Jooyung Han2a86ba82021-12-02 01:41:19 +0900559 // Verify the var/const declarations.
560 // const expressions should be non-empty when evaluated with the var/const type.
561 for (const auto& constant : defined_type->GetConstantDeclarations()) {
562 if (constant->ValueString(AidlConstantValueDecorator).empty()) {
563 return AidlError::BAD_TYPE;
564 }
565 }
566 for (const auto& var : defined_type->GetFields()) {
567 if (var->GetDefaultValue() && var->ValueString(AidlConstantValueDecorator).empty()) {
568 return AidlError::BAD_TYPE;
569 }
570 }
571 }
572
573 // Add meta methods and assign method IDs to each interface
574 typenames->IterateTypes([&](const AidlDefinedType& type) {
575 auto interface = const_cast<AidlInterface*>(type.AsInterface());
Jiyong Parkb034bf02018-07-30 17:44:33 +0900576 if (interface != nullptr) {
577 // add the meta-method 'int getInterfaceVersion()' if version is specified.
578 if (options.Version() > 0) {
Jooyung Hanaccd9192021-10-14 15:57:28 +0900579 auto ret = typenames->MakeResolvedType(AIDL_LOCATION_HERE, "int", false);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900580 vector<unique_ptr<AidlArgument>>* args = new vector<unique_ptr<AidlArgument>>();
Jooyung Hanae016752022-02-26 21:33:43 +0900581 auto method = std::make_unique<AidlMethod>(AIDL_LOCATION_HERE, false, ret.release(),
582 "getInterfaceVersion", args, Comments{},
583 kGetInterfaceVersionId);
Jooyung Han3f347ca2020-12-01 12:41:50 +0900584 interface->AddMethod(std::move(method));
Jiyong Parkb034bf02018-07-30 17:44:33 +0900585 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900586 // add the meta-method 'string getInterfaceHash()' if hash is specified.
587 if (!options.Hash().empty()) {
Jooyung Hanaccd9192021-10-14 15:57:28 +0900588 auto ret = typenames->MakeResolvedType(AIDL_LOCATION_HERE, "String", false);
Paul Trautrimb77048c2020-01-21 16:39:32 +0900589 vector<unique_ptr<AidlArgument>>* args = new vector<unique_ptr<AidlArgument>>();
Jooyung Hanae016752022-02-26 21:33:43 +0900590 auto method =
591 std::make_unique<AidlMethod>(AIDL_LOCATION_HERE, false, ret.release(),
592 kGetInterfaceHash, args, Comments{}, kGetInterfaceHashId);
Jooyung Han3f347ca2020-12-01 12:41:50 +0900593 interface->AddMethod(std::move(method));
Paul Trautrimb77048c2020-01-21 16:39:32 +0900594 }
Jooyung Han2a86ba82021-12-02 01:41:19 +0900595 if (!CheckAndAssignMethodIDs(interface->GetMethods())) {
596 err = AidlError::BAD_METHOD_ID;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900597 }
Jooyung Hancfe08002020-12-04 12:56:35 +0900598 }
Jooyung Han2a86ba82021-12-02 01:41:19 +0900599 });
600 if (err != AidlError::OK) {
601 return err;
Christopher Wiley632801d2015-11-05 14:15:49 -0800602 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800603
Jooyung Han2b3cd2a2021-10-15 06:54:55 +0900604 for (const auto& doc : typenames->AllDocuments()) {
605 VisitTopDown([](const AidlNode& n) { n.MarkVisited(); }, *doc);
606 }
607
Jooyung Han74406c52021-11-06 06:17:36 +0900608 if (!CheckValid(*document, options)) {
609 return AidlError::BAD_TYPE;
610 }
611
Jooyung Hance6733e2021-05-22 02:44:17 +0900612 if (!ValidateAnnotationContext(*document)) {
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900613 return AidlError::BAD_TYPE;
614 }
615
Jooyung Han2cb8ecd2021-07-28 18:41:30 +0900616 if ((options.TargetLanguage() == Options::Language::CPP ||
617 options.TargetLanguage() == Options::Language::NDK) &&
618 !ValidateCppHeader(*document)) {
619 return AidlError::BAD_TYPE;
620 }
621
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900622 if (!Diagnose(*document, options.GetDiagnosticMapping())) {
Jiyong Parkd9113322020-12-31 03:20:33 +0900623 return AidlError::BAD_TYPE;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900624 }
625
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900626 typenames->IterateTypes([&](const AidlDefinedType& type) {
Jooyung Hanb4997aa2021-10-16 03:26:12 +0900627 if (!type.LanguageSpecificCheckValid(options.TargetLanguage())) {
Steven Moreland58d402b2021-07-27 18:35:07 -0700628 err = AidlError::BAD_TYPE;
629 }
630
Steven Morelanda57d0a62019-07-30 09:41:14 -0700631 if (options.IsStructured() && type.AsUnstructuredParcelable() != nullptr &&
Jeongik Cha88f95a82020-01-15 13:02:16 +0900632 !type.AsUnstructuredParcelable()->IsStableApiParcelable(options.TargetLanguage())) {
Steven Morelanda57d0a62019-07-30 09:41:14 -0700633 err = AidlError::NOT_STRUCTURED;
Devin Moore097a3ab2020-03-11 16:08:44 -0700634 AIDL_ERROR(type) << type.GetCanonicalName()
635 << " is not structured, but this is a structured interface.";
Steven Morelanda57d0a62019-07-30 09:41:14 -0700636 }
637 if (options.GetStability() == Options::Stability::VINTF && !type.IsVintfStability()) {
638 err = AidlError::NOT_STRUCTURED;
Devin Moore097a3ab2020-03-11 16:08:44 -0700639 AIDL_ERROR(type) << type.GetCanonicalName()
640 << " does not have VINTF level stability, but this interface requires it.";
Steven Morelanda57d0a62019-07-30 09:41:14 -0700641 }
Jiyong Parkf8d53612020-05-04 14:06:13 +0900642
Jeongik Chaef44e622020-10-23 16:00:52 +0900643 // Ensure that untyped List/Map is not used in a parcelable, a union and a stable interface.
Jiyong Parkf8d53612020-05-04 14:06:13 +0900644
Jeongik Chaef44e622020-10-23 16:00:52 +0900645 std::function<void(const AidlTypeSpecifier&, const AidlNode*)> check_untyped_container =
646 [&err, &check_untyped_container](const AidlTypeSpecifier& type, const AidlNode* node) {
647 if (type.IsGeneric()) {
648 std::for_each(type.GetTypeParameters().begin(), type.GetTypeParameters().end(),
649 [&node, &check_untyped_container](auto& nested) {
650 check_untyped_container(*nested, node);
651 });
652 return;
Jiyong Parkf8d53612020-05-04 14:06:13 +0900653 }
Jeongik Chaef44e622020-10-23 16:00:52 +0900654 if (type.GetName() == "List" || type.GetName() == "Map") {
655 err = AidlError::BAD_TYPE;
656 AIDL_ERROR(node)
657 << "Encountered an untyped List or Map. The use of untyped List/Map is prohibited "
658 << "because it is not guaranteed that the objects in the list are recognizable in "
659 << "the receiving side. Consider switching to an array or a generic List/Map.";
660 }
661 };
Jeongik Chaef44e622020-10-23 16:00:52 +0900662
Jooyung Han829ec7c2020-12-02 12:07:36 +0900663 if (type.AsInterface() && options.IsStructured()) {
664 for (const auto& method : type.GetMethods()) {
Jeongik Chaef44e622020-10-23 16:00:52 +0900665 check_untyped_container(method->GetType(), method.get());
666 for (const auto& arg : method->GetArguments()) {
667 check_untyped_container(arg->GetType(), method.get());
Jiyong Parkf8d53612020-05-04 14:06:13 +0900668 }
Jeongik Chaef44e622020-10-23 16:00:52 +0900669 }
Jooyung Han829ec7c2020-12-02 12:07:36 +0900670 }
671 for (const auto& field : type.GetFields()) {
672 check_untyped_container(field->GetType(), field.get());
Jiyong Parkf8d53612020-05-04 14:06:13 +0900673 }
Steven Morelanda57d0a62019-07-30 09:41:14 -0700674 });
675
Steven Moreland6cee3482018-07-18 14:39:58 -0700676 if (err != AidlError::OK) {
677 return err;
678 }
679
Jiyong Parkb034bf02018-07-30 17:44:33 +0900680 if (imported_files != nullptr) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900681 *imported_files = import_paths;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700682 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700683
Christopher Wiley632801d2015-11-05 14:15:49 -0800684 return AidlError::OK;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700685}
686
Casey Dahlin2cc93162015-10-02 16:14:17 -0700687} // namespace internals
688
Steven Moreland4e059132021-08-11 13:36:30 -0700689bool compile_aidl(const Options& options, const IoDelegate& io_delegate) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900690 const Options::Language lang = options.TargetLanguage();
Jiyong Park74595c12018-07-23 15:22:50 +0900691 for (const string& input_file : options.InputFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900692 AidlTypenames typenames;
Jiyong Park74595c12018-07-23 15:22:50 +0900693
Jiyong Parkb034bf02018-07-30 17:44:33 +0900694 vector<string> imported_files;
Jiyong Park74595c12018-07-23 15:22:50 +0900695
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900696 AidlError aidl_err = internals::load_and_validate_aidl(input_file, options, io_delegate,
697 &typenames, &imported_files);
Jooyung Han05a532a2021-08-18 10:41:40 +0900698 if (aidl_err != AidlError::OK) {
Steven Moreland4e059132021-08-11 13:36:30 -0700699 return false;
Jiyong Park74595c12018-07-23 15:22:50 +0900700 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700701
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900702 for (const auto& defined_type : typenames.MainDocument().DefinedTypes()) {
Steven Moreland21780812020-09-11 01:29:45 +0000703 AIDL_FATAL_IF(defined_type == nullptr, input_file);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700704
Jiyong Parkb034bf02018-07-30 17:44:33 +0900705 string output_file_name = options.OutputFile();
706 // if needed, generate the output file name from the base folder
707 if (output_file_name.empty() && !options.OutputDir().empty()) {
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900708 output_file_name = GetOutputFilePath(options, *defined_type);
Jiyong Parkb03551f2018-08-06 19:20:51 +0900709 if (output_file_name.empty()) {
Steven Moreland4e059132021-08-11 13:36:30 -0700710 return false;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900711 }
712 }
Jiyong Park74595c12018-07-23 15:22:50 +0900713
Jiyong Parkb034bf02018-07-30 17:44:33 +0900714 if (!write_dep_file(options, *defined_type, imported_files, io_delegate, input_file,
715 output_file_name)) {
Steven Moreland4e059132021-08-11 13:36:30 -0700716 return false;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900717 }
Jiyong Park74595c12018-07-23 15:22:50 +0900718
Jiyong Parkb034bf02018-07-30 17:44:33 +0900719 bool success = false;
720 if (lang == Options::Language::CPP) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900721 success =
722 cpp::GenerateCpp(output_file_name, options, typenames, *defined_type, io_delegate);
Steven Morelandc26d8142018-09-17 14:25:33 -0700723 } else if (lang == Options::Language::NDK) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900724 ndk::GenerateNdk(output_file_name, options, typenames, *defined_type, io_delegate);
Steven Morelandc26d8142018-09-17 14:25:33 -0700725 success = true;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900726 } else if (lang == Options::Language::JAVA) {
Jiyong Park9ca5c7e2019-10-17 15:01:14 +0900727 if (defined_type->AsUnstructuredParcelable() != nullptr) {
728 // Legacy behavior. For parcelable declarations in Java, don't generate output file.
729 success = true;
730 } else {
Jooyung Han7445d282021-10-07 18:16:22 +0900731 java::GenerateJava(output_file_name, options, typenames, *defined_type, io_delegate);
732 success = true;
Jiyong Park9ca5c7e2019-10-17 15:01:14 +0900733 }
Andrei Homescub62afd92020-05-11 19:24:59 -0700734 } else if (lang == Options::Language::RUST) {
Jooyung Han9e3dae32021-10-08 02:32:24 +0900735 rust::GenerateRust(output_file_name, options, typenames, *defined_type, io_delegate);
736 success = true;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900737 } else {
Steven Moreland21780812020-09-11 01:29:45 +0000738 AIDL_FATAL(input_file) << "Should not reach here.";
Jiyong Parkb034bf02018-07-30 17:44:33 +0900739 }
740 if (!success) {
Steven Moreland4e059132021-08-11 13:36:30 -0700741 return false;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900742 }
Jiyong Park74595c12018-07-23 15:22:50 +0900743 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700744 }
Steven Moreland4e059132021-08-11 13:36:30 -0700745 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800746}
747
Andrei Onea8714b022019-02-01 18:55:54 +0000748bool dump_mappings(const Options& options, const IoDelegate& io_delegate) {
749 android::aidl::mappings::SignatureMap all_mappings;
750 for (const string& input_file : options.InputFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900751 AidlTypenames typenames;
Andrei Onea8714b022019-02-01 18:55:54 +0000752 vector<string> imported_files;
753
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900754 AidlError aidl_err = internals::load_and_validate_aidl(input_file, options, io_delegate,
755 &typenames, &imported_files);
Andrei Onea8714b022019-02-01 18:55:54 +0000756 if (aidl_err != AidlError::OK) {
Jiyong Park3a060392020-04-11 21:02:19 +0900757 return false;
Andrei Onea8714b022019-02-01 18:55:54 +0000758 }
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900759 for (const auto& defined_type : typenames.MainDocument().DefinedTypes()) {
Jooyung Han2ca04f52021-11-26 15:11:45 +0900760 auto mappings = mappings::generate_mappings(defined_type.get());
Andrei Onea8714b022019-02-01 18:55:54 +0000761 all_mappings.insert(mappings.begin(), mappings.end());
762 }
763 }
764 std::stringstream mappings_str;
765 for (const auto& mapping : all_mappings) {
766 mappings_str << mapping.first << "\n" << mapping.second << "\n";
767 }
768 auto code_writer = io_delegate.GetCodeWriter(options.OutputFile());
769 code_writer->Write("%s", mappings_str.str().c_str());
770 return true;
771}
772
Steven Moreland3981d9e2020-03-31 14:11:44 -0700773int aidl_entry(const Options& options, const IoDelegate& io_delegate) {
Steven Moreland33efcf62020-04-10 16:40:43 -0700774 AidlErrorLog::clearError();
Jooyung Han2b3cd2a2021-10-15 06:54:55 +0900775 AidlNode::ClearUnvisitedNodes();
Steven Moreland33efcf62020-04-10 16:40:43 -0700776
Steven Moreland4e059132021-08-11 13:36:30 -0700777 bool success = false;
Steven Morelandf9b398b2021-08-11 13:43:48 -0700778 if (options.Ok()) {
779 switch (options.GetTask()) {
780 case Options::Task::HELP:
781 success = true;
782 break;
783 case Options::Task::COMPILE:
784 success = android::aidl::compile_aidl(options, io_delegate);
785 break;
786 case Options::Task::PREPROCESS:
787 success = android::aidl::Preprocess(options, io_delegate);
788 break;
789 case Options::Task::DUMP_API:
790 success = android::aidl::dump_api(options, io_delegate);
791 break;
792 case Options::Task::CHECK_API:
793 success = android::aidl::check_api(options, io_delegate);
794 break;
795 case Options::Task::DUMP_MAPPINGS:
796 success = android::aidl::dump_mappings(options, io_delegate);
797 break;
798 default:
799 AIDL_FATAL(AIDL_LOCATION_HERE)
800 << "Unrecognized task: " << static_cast<size_t>(options.GetTask());
801 }
802 } else {
803 AIDL_ERROR(options.GetErrorMessage()) << options.GetUsage();
Steven Moreland3981d9e2020-03-31 14:11:44 -0700804 }
805
Steven Moreland21780812020-09-11 01:29:45 +0000806 const bool reportedError = AidlErrorLog::hadError();
Steven Moreland4e059132021-08-11 13:36:30 -0700807 AIDL_FATAL_IF(success == reportedError, AIDL_LOCATION_HERE)
808 << "Compiler returned success " << success << " but did" << (reportedError ? "" : " not")
Steven Moreland21780812020-09-11 01:29:45 +0000809 << " emit error logs";
Steven Moreland3981d9e2020-03-31 14:11:44 -0700810
Jooyung Han2b3cd2a2021-10-15 06:54:55 +0900811 if (success) {
812 auto locations = AidlNode::GetLocationsOfUnvisitedNodes();
813 if (!locations.empty()) {
814 for (const auto& location : locations) {
815 AIDL_ERROR(location) << "AidlNode at location was not visited!";
816 }
817 AIDL_FATAL(AIDL_LOCATION_HERE)
818 << "The AIDL AST was not processed fully. Please report an issue.";
819 }
820 }
821
Steven Moreland4e059132021-08-11 13:36:30 -0700822 return success ? 0 : 1;
Steven Moreland3981d9e2020-03-31 14:11:44 -0700823}
824
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700825} // namespace aidl
Steven Morelandf4c64df2019-07-29 19:54:04 -0700826} // namespace android