blob: b30b004953cbca978e841ddd0f6d40f9799fa37d [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()) {
Steven Morelanda7560e82021-10-08 16:24:39 -0700312 if (!annot->CheckContext(context)) {
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900313 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
Jooyung Han2cb8ecd2021-07-28 18:41:30 +0900357bool ValidateCppHeader(const AidlDocument& doc) {
358 struct CppHeaderVisitor : AidlVisitor {
359 bool success = true;
360 void Visit(const AidlParcelable& p) override {
361 if (p.GetCppHeader().empty()) {
362 AIDL_ERROR(p) << "Unstructured parcelable \"" << p.GetName()
363 << "\" must have C++ header defined.";
364 success = false;
365 }
366 }
367 void Visit(const AidlTypeSpecifier& m) override {
368 auto type = m.GetDefinedType();
369 if (type) {
370 auto unstructured = type->AsUnstructuredParcelable();
371 if (unstructured && unstructured->GetCppHeader().empty()) {
372 AIDL_ERROR(m) << "Unstructured parcelable \"" << m.GetUnresolvedName()
373 << "\" must have C++ header defined.";
374 success = false;
375 }
376 }
377 }
378 };
379
380 CppHeaderVisitor validator;
381 VisitTopDown(validator, doc);
382 return validator.success;
383}
384
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700385} // namespace
386
387namespace internals {
388
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900389AidlError load_and_validate_aidl(const std::string& input_file_name, const Options& options,
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900390 const IoDelegate& io_delegate, AidlTypenames* typenames,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900391 vector<string>* imported_files) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800392 AidlError err = AidlError::OK;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800393
Jiyong Parkb034bf02018-07-30 17:44:33 +0900394 //////////////////////////////////////////////////////////////////////////
395 // Loading phase
396 //////////////////////////////////////////////////////////////////////////
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900397
Jiyong Parkb034bf02018-07-30 17:44:33 +0900398 // Parse the main input file
Jooyung Hance6733e2021-05-22 02:44:17 +0900399 const AidlDocument* document = Parser::Parse(input_file_name, io_delegate, *typenames);
400 if (document == nullptr) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900401 return AidlError::PARSE_ERROR;
402 }
Jooyung Han2946afc2020-10-05 20:29:16 +0900403 int num_top_level_decls = 0;
Jooyung Hance6733e2021-05-22 02:44:17 +0900404 for (const auto& type : document->DefinedTypes()) {
Jooyung Han2946afc2020-10-05 20:29:16 +0900405 if (type->AsUnstructuredParcelable() == nullptr) {
406 num_top_level_decls++;
407 if (num_top_level_decls > 1) {
Devin Moore5de18ed2020-04-02 13:52:29 -0700408 AIDL_ERROR(*type) << "You must declare only one type per file.";
409 return AidlError::BAD_TYPE;
410 }
Jiyong Parkda8c6932019-08-12 19:56:08 +0900411 }
412 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900413
414 // Import the preprocessed file
Jooyung Han93f48f02021-06-05 00:11:16 +0900415 for (const string& filename : options.PreprocessedFiles()) {
416 auto preprocessed = Parser::Parse(filename, io_delegate, *typenames, /*is_preprocessed=*/true);
417 if (!preprocessed) {
418 return AidlError::BAD_PRE_PROCESSED_FILE;
Christopher Wileyef140932015-11-03 09:29:19 -0800419 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700420 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800421
Jiyong Parkb034bf02018-07-30 17:44:33 +0900422 // Find files to import and parse them
Jiyong Parke59c3682018-09-11 23:10:25 +0900423 vector<string> import_paths;
Jooyung Han6529b822021-06-11 22:05:26 +0900424 ImportResolver import_resolver{io_delegate, input_file_name, options.ImportDirs()};
Jooyung Hance6733e2021-05-22 02:44:17 +0900425 for (const auto& import : document->Imports()) {
Jooyung Han29813842020-12-08 01:28:03 +0900426 if (typenames->IsIgnorableImport(import->GetNeededClass())) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700427 // There are places in the Android tree where an import doesn't resolve,
428 // but we'll pick the type up through the preprocessed types.
429 // This seems like an error, but legacy support demands we support it...
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700430 continue;
431 }
Jooyung Han29813842020-12-08 01:28:03 +0900432 string import_path = import_resolver.FindImportFile(import->GetNeededClass());
Christopher Wiley72877ac2015-10-06 14:41:42 -0700433 if (import_path.empty()) {
Jooyung Han29813842020-12-08 01:28:03 +0900434 AIDL_ERROR(input_file_name) << "Couldn't find import for class " << import->GetNeededClass();
435 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700436 continue;
437 }
Casey Dahlin2cc93162015-10-02 16:14:17 -0700438
Jiyong Parke59c3682018-09-11 23:10:25 +0900439 import_paths.emplace_back(import_path);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900440
Jooyung Hance6733e2021-05-22 02:44:17 +0900441 auto imported_doc = Parser::Parse(import_path, io_delegate, *typenames);
442 if (imported_doc == nullptr) {
Jooyung Han93f48f02021-06-05 00:11:16 +0900443 AIDL_ERROR(import_path) << "error while importing " << import_path << " for "
444 << import->GetNeededClass();
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 Han8e3b72c2021-05-22 02:54:37 +0900517 if (!defined_type->LanguageSpecificCheckValid(*typenames, options.TargetLanguage())) {
518 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) {
527 bool isStable = unstructured_parcelable->IsStableApiParcelable(options.TargetLanguage());
Jeongik Cha82317dd2019-02-27 20:26:11 +0900528 if (options.IsStructured() && !isStable) {
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000529 AIDL_ERROR(unstructured_parcelable)
Jooyung Han2cb8ecd2021-07-28 18:41:30 +0900530 << "Cannot declare unstructured parcelable in a --structured interface. Parcelable "
531 "must be defined in AIDL directly.";
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800532 return AidlError::NOT_STRUCTURED;
533 }
534 if (options.FailOnParcelable()) {
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000535 AIDL_ERROR(unstructured_parcelable)
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800536 << "Refusing to generate code with unstructured parcelables. Declared parcelables "
537 "should be in their own file and/or cannot be used with --structured interfaces.";
Jooyung Han05a532a2021-08-18 10:41:40 +0900538 return AidlError::FOUND_PARCELABLE;
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800539 }
Steven Morelande2c64b42018-09-18 15:06:37 -0700540 }
541
Devin Moore0d0e3f62020-03-30 17:45:39 -0700542 if (defined_type->IsVintfStability()) {
543 bool success = true;
544 if (options.GetStability() != Options::Stability::VINTF) {
545 AIDL_ERROR(defined_type)
546 << "Must compile @VintfStability type w/ aidl_interface 'stability: \"vintf\"'";
547 success = false;
548 }
549 if (!options.IsStructured()) {
550 AIDL_ERROR(defined_type)
551 << "Must compile @VintfStability type w/ aidl_interface --structured";
552 success = false;
553 }
554 if (!success) return AidlError::NOT_STRUCTURED;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700555 }
556
Jiyong Parkb034bf02018-07-30 17:44:33 +0900557 if (interface != nullptr) {
558 // add the meta-method 'int getInterfaceVersion()' if version is specified.
559 if (options.Version() > 0) {
Jooyung Hanaccd9192021-10-14 15:57:28 +0900560 auto ret = typenames->MakeResolvedType(AIDL_LOCATION_HERE, "int", false);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900561 vector<unique_ptr<AidlArgument>>* args = new vector<unique_ptr<AidlArgument>>();
Jooyung Han3f347ca2020-12-01 12:41:50 +0900562 auto method = std::make_unique<AidlMethod>(
Jooyung Hanaccd9192021-10-14 15:57:28 +0900563 AIDL_LOCATION_HERE, false, ret.release(), "getInterfaceVersion", args, Comments{},
Jooyung Han8451a202021-01-16 03:07:06 +0900564 kGetInterfaceVersionId, false /* is_user_defined */);
Jooyung Han3f347ca2020-12-01 12:41:50 +0900565 interface->AddMethod(std::move(method));
Jiyong Parkb034bf02018-07-30 17:44:33 +0900566 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900567 // add the meta-method 'string getInterfaceHash()' if hash is specified.
568 if (!options.Hash().empty()) {
Jooyung Hanaccd9192021-10-14 15:57:28 +0900569 auto ret = typenames->MakeResolvedType(AIDL_LOCATION_HERE, "String", false);
Paul Trautrimb77048c2020-01-21 16:39:32 +0900570 vector<unique_ptr<AidlArgument>>* args = new vector<unique_ptr<AidlArgument>>();
Jooyung Han8451a202021-01-16 03:07:06 +0900571 auto method = std::make_unique<AidlMethod>(
Jooyung Hanaccd9192021-10-14 15:57:28 +0900572 AIDL_LOCATION_HERE, false, ret.release(), kGetInterfaceHash, args, Comments{},
Jooyung Han8451a202021-01-16 03:07:06 +0900573 kGetInterfaceHashId, false /* is_user_defined */);
Jooyung Han3f347ca2020-12-01 12:41:50 +0900574 interface->AddMethod(std::move(method));
Paul Trautrimb77048c2020-01-21 16:39:32 +0900575 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900576 if (!check_and_assign_method_ids(interface->GetMethods())) {
577 return AidlError::BAD_METHOD_ID;
578 }
Jooyung Hancfe08002020-12-04 12:56:35 +0900579 }
Jooyung Han30f64ad2020-12-15 08:16:31 +0900580 // Verify the var/const declarations.
581 // const expressions should be non-empty when evaluated with the var/const type.
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900582 for (const auto& constant : defined_type->GetConstantDeclarations()) {
583 if (constant->ValueString(AidlConstantValueDecorator).empty()) {
584 return AidlError::BAD_TYPE;
Jooyung Han30f64ad2020-12-15 08:16:31 +0900585 }
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900586 }
587 for (const auto& var : defined_type->GetFields()) {
588 if (var->GetDefaultValue() && var->ValueString(AidlConstantValueDecorator).empty()) {
589 return AidlError::BAD_TYPE;
Will McVickerd7d18df2019-09-12 13:40:50 -0700590 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900591 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800592 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800593
Jooyung Han2b3cd2a2021-10-15 06:54:55 +0900594 for (const auto& doc : typenames->AllDocuments()) {
595 VisitTopDown([](const AidlNode& n) { n.MarkVisited(); }, *doc);
596 }
597
Jooyung Hance6733e2021-05-22 02:44:17 +0900598 if (!ValidateAnnotationContext(*document)) {
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900599 return AidlError::BAD_TYPE;
600 }
601
Jooyung Han2cb8ecd2021-07-28 18:41:30 +0900602 if ((options.TargetLanguage() == Options::Language::CPP ||
603 options.TargetLanguage() == Options::Language::NDK) &&
604 !ValidateCppHeader(*document)) {
605 return AidlError::BAD_TYPE;
606 }
607
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900608 if (!Diagnose(*document, options.GetDiagnosticMapping())) {
Jiyong Parkd9113322020-12-31 03:20:33 +0900609 return AidlError::BAD_TYPE;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900610 }
611
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900612 typenames->IterateTypes([&](const AidlDefinedType& type) {
Steven Moreland58d402b2021-07-27 18:35:07 -0700613 if (!type.LanguageSpecificCheckValid(*typenames, options.TargetLanguage())) {
614 err = AidlError::BAD_TYPE;
615 }
616
Steven Morelanda57d0a62019-07-30 09:41:14 -0700617 if (options.IsStructured() && type.AsUnstructuredParcelable() != nullptr &&
Jeongik Cha88f95a82020-01-15 13:02:16 +0900618 !type.AsUnstructuredParcelable()->IsStableApiParcelable(options.TargetLanguage())) {
Steven Morelanda57d0a62019-07-30 09:41:14 -0700619 err = AidlError::NOT_STRUCTURED;
Devin Moore097a3ab2020-03-11 16:08:44 -0700620 AIDL_ERROR(type) << type.GetCanonicalName()
621 << " is not structured, but this is a structured interface.";
Steven Morelanda57d0a62019-07-30 09:41:14 -0700622 }
623 if (options.GetStability() == Options::Stability::VINTF && !type.IsVintfStability()) {
624 err = AidlError::NOT_STRUCTURED;
Devin Moore097a3ab2020-03-11 16:08:44 -0700625 AIDL_ERROR(type) << type.GetCanonicalName()
626 << " does not have VINTF level stability, but this interface requires it.";
Steven Morelanda57d0a62019-07-30 09:41:14 -0700627 }
Jiyong Parkf8d53612020-05-04 14:06:13 +0900628
Jeongik Chaef44e622020-10-23 16:00:52 +0900629 // Ensure that untyped List/Map is not used in a parcelable, a union and a stable interface.
Jiyong Parkf8d53612020-05-04 14:06:13 +0900630
Jeongik Chaef44e622020-10-23 16:00:52 +0900631 std::function<void(const AidlTypeSpecifier&, const AidlNode*)> check_untyped_container =
632 [&err, &check_untyped_container](const AidlTypeSpecifier& type, const AidlNode* node) {
633 if (type.IsGeneric()) {
634 std::for_each(type.GetTypeParameters().begin(), type.GetTypeParameters().end(),
635 [&node, &check_untyped_container](auto& nested) {
636 check_untyped_container(*nested, node);
637 });
638 return;
Jiyong Parkf8d53612020-05-04 14:06:13 +0900639 }
Jeongik Chaef44e622020-10-23 16:00:52 +0900640 if (type.GetName() == "List" || type.GetName() == "Map") {
641 err = AidlError::BAD_TYPE;
642 AIDL_ERROR(node)
643 << "Encountered an untyped List or Map. The use of untyped List/Map is prohibited "
644 << "because it is not guaranteed that the objects in the list are recognizable in "
645 << "the receiving side. Consider switching to an array or a generic List/Map.";
646 }
647 };
Jeongik Chaef44e622020-10-23 16:00:52 +0900648
Jooyung Han829ec7c2020-12-02 12:07:36 +0900649 if (type.AsInterface() && options.IsStructured()) {
650 for (const auto& method : type.GetMethods()) {
Jeongik Chaef44e622020-10-23 16:00:52 +0900651 check_untyped_container(method->GetType(), method.get());
652 for (const auto& arg : method->GetArguments()) {
653 check_untyped_container(arg->GetType(), method.get());
Jiyong Parkf8d53612020-05-04 14:06:13 +0900654 }
Jeongik Chaef44e622020-10-23 16:00:52 +0900655 }
Jooyung Han829ec7c2020-12-02 12:07:36 +0900656 }
657 for (const auto& field : type.GetFields()) {
658 check_untyped_container(field->GetType(), field.get());
Jiyong Parkf8d53612020-05-04 14:06:13 +0900659 }
Steven Morelanda57d0a62019-07-30 09:41:14 -0700660 });
661
Steven Moreland6cee3482018-07-18 14:39:58 -0700662 if (err != AidlError::OK) {
663 return err;
664 }
665
Jiyong Parkb034bf02018-07-30 17:44:33 +0900666 if (imported_files != nullptr) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900667 *imported_files = import_paths;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700668 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700669
Christopher Wiley632801d2015-11-05 14:15:49 -0800670 return AidlError::OK;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700671}
672
Casey Dahlin2cc93162015-10-02 16:14:17 -0700673} // namespace internals
674
Steven Moreland4e059132021-08-11 13:36:30 -0700675bool compile_aidl(const Options& options, const IoDelegate& io_delegate) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900676 const Options::Language lang = options.TargetLanguage();
Jiyong Park74595c12018-07-23 15:22:50 +0900677 for (const string& input_file : options.InputFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900678 AidlTypenames typenames;
Jiyong Park74595c12018-07-23 15:22:50 +0900679
Jiyong Parkb034bf02018-07-30 17:44:33 +0900680 vector<string> imported_files;
Jiyong Park74595c12018-07-23 15:22:50 +0900681
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900682 AidlError aidl_err = internals::load_and_validate_aidl(input_file, options, io_delegate,
683 &typenames, &imported_files);
Jooyung Han05a532a2021-08-18 10:41:40 +0900684 if (aidl_err != AidlError::OK) {
Steven Moreland4e059132021-08-11 13:36:30 -0700685 return false;
Jiyong Park74595c12018-07-23 15:22:50 +0900686 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700687
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900688 for (const auto& defined_type : typenames.MainDocument().DefinedTypes()) {
Steven Moreland21780812020-09-11 01:29:45 +0000689 AIDL_FATAL_IF(defined_type == nullptr, input_file);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700690
Jiyong Parkb034bf02018-07-30 17:44:33 +0900691 string output_file_name = options.OutputFile();
692 // if needed, generate the output file name from the base folder
693 if (output_file_name.empty() && !options.OutputDir().empty()) {
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900694 output_file_name = GetOutputFilePath(options, *defined_type);
Jiyong Parkb03551f2018-08-06 19:20:51 +0900695 if (output_file_name.empty()) {
Steven Moreland4e059132021-08-11 13:36:30 -0700696 return false;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900697 }
698 }
Jiyong Park74595c12018-07-23 15:22:50 +0900699
Jiyong Parkb034bf02018-07-30 17:44:33 +0900700 if (!write_dep_file(options, *defined_type, imported_files, io_delegate, input_file,
701 output_file_name)) {
Steven Moreland4e059132021-08-11 13:36:30 -0700702 return false;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900703 }
Jiyong Park74595c12018-07-23 15:22:50 +0900704
Jiyong Parkb034bf02018-07-30 17:44:33 +0900705 bool success = false;
706 if (lang == Options::Language::CPP) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900707 success =
708 cpp::GenerateCpp(output_file_name, options, typenames, *defined_type, io_delegate);
Steven Morelandc26d8142018-09-17 14:25:33 -0700709 } else if (lang == Options::Language::NDK) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900710 ndk::GenerateNdk(output_file_name, options, typenames, *defined_type, io_delegate);
Steven Morelandc26d8142018-09-17 14:25:33 -0700711 success = true;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900712 } else if (lang == Options::Language::JAVA) {
Jiyong Park9ca5c7e2019-10-17 15:01:14 +0900713 if (defined_type->AsUnstructuredParcelable() != nullptr) {
714 // Legacy behavior. For parcelable declarations in Java, don't generate output file.
715 success = true;
716 } else {
Jooyung Han7445d282021-10-07 18:16:22 +0900717 java::GenerateJava(output_file_name, options, typenames, *defined_type, io_delegate);
718 success = true;
Jiyong Park9ca5c7e2019-10-17 15:01:14 +0900719 }
Andrei Homescub62afd92020-05-11 19:24:59 -0700720 } else if (lang == Options::Language::RUST) {
Jooyung Han9e3dae32021-10-08 02:32:24 +0900721 rust::GenerateRust(output_file_name, options, typenames, *defined_type, io_delegate);
722 success = true;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900723 } else {
Steven Moreland21780812020-09-11 01:29:45 +0000724 AIDL_FATAL(input_file) << "Should not reach here.";
Jiyong Parkb034bf02018-07-30 17:44:33 +0900725 }
726 if (!success) {
Steven Moreland4e059132021-08-11 13:36:30 -0700727 return false;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900728 }
Jiyong Park74595c12018-07-23 15:22:50 +0900729 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700730 }
Steven Moreland4e059132021-08-11 13:36:30 -0700731 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800732}
733
Andrei Onea8714b022019-02-01 18:55:54 +0000734bool dump_mappings(const Options& options, const IoDelegate& io_delegate) {
735 android::aidl::mappings::SignatureMap all_mappings;
736 for (const string& input_file : options.InputFiles()) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900737 AidlTypenames typenames;
Andrei Onea8714b022019-02-01 18:55:54 +0000738 vector<string> imported_files;
739
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900740 AidlError aidl_err = internals::load_and_validate_aidl(input_file, options, io_delegate,
741 &typenames, &imported_files);
Andrei Onea8714b022019-02-01 18:55:54 +0000742 if (aidl_err != AidlError::OK) {
Jiyong Park3a060392020-04-11 21:02:19 +0900743 return false;
Andrei Onea8714b022019-02-01 18:55:54 +0000744 }
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900745 for (const auto& defined_type : typenames.MainDocument().DefinedTypes()) {
746 auto mappings = mappings::generate_mappings(defined_type.get(), typenames);
Andrei Onea8714b022019-02-01 18:55:54 +0000747 all_mappings.insert(mappings.begin(), mappings.end());
748 }
749 }
750 std::stringstream mappings_str;
751 for (const auto& mapping : all_mappings) {
752 mappings_str << mapping.first << "\n" << mapping.second << "\n";
753 }
754 auto code_writer = io_delegate.GetCodeWriter(options.OutputFile());
755 code_writer->Write("%s", mappings_str.str().c_str());
756 return true;
757}
758
Steven Moreland3981d9e2020-03-31 14:11:44 -0700759int aidl_entry(const Options& options, const IoDelegate& io_delegate) {
Steven Moreland33efcf62020-04-10 16:40:43 -0700760 AidlErrorLog::clearError();
Jooyung Han2b3cd2a2021-10-15 06:54:55 +0900761 AidlNode::ClearUnvisitedNodes();
Steven Moreland33efcf62020-04-10 16:40:43 -0700762
Steven Moreland4e059132021-08-11 13:36:30 -0700763 bool success = false;
Steven Morelandf9b398b2021-08-11 13:43:48 -0700764 if (options.Ok()) {
765 switch (options.GetTask()) {
766 case Options::Task::HELP:
767 success = true;
768 break;
769 case Options::Task::COMPILE:
770 success = android::aidl::compile_aidl(options, io_delegate);
771 break;
772 case Options::Task::PREPROCESS:
773 success = android::aidl::Preprocess(options, io_delegate);
774 break;
775 case Options::Task::DUMP_API:
776 success = android::aidl::dump_api(options, io_delegate);
777 break;
778 case Options::Task::CHECK_API:
779 success = android::aidl::check_api(options, io_delegate);
780 break;
781 case Options::Task::DUMP_MAPPINGS:
782 success = android::aidl::dump_mappings(options, io_delegate);
783 break;
784 default:
785 AIDL_FATAL(AIDL_LOCATION_HERE)
786 << "Unrecognized task: " << static_cast<size_t>(options.GetTask());
787 }
788 } else {
789 AIDL_ERROR(options.GetErrorMessage()) << options.GetUsage();
Steven Moreland3981d9e2020-03-31 14:11:44 -0700790 }
791
Steven Moreland21780812020-09-11 01:29:45 +0000792 const bool reportedError = AidlErrorLog::hadError();
Steven Moreland4e059132021-08-11 13:36:30 -0700793 AIDL_FATAL_IF(success == reportedError, AIDL_LOCATION_HERE)
794 << "Compiler returned success " << success << " but did" << (reportedError ? "" : " not")
Steven Moreland21780812020-09-11 01:29:45 +0000795 << " emit error logs";
Steven Moreland3981d9e2020-03-31 14:11:44 -0700796
Jooyung Han2b3cd2a2021-10-15 06:54:55 +0900797 if (success) {
798 auto locations = AidlNode::GetLocationsOfUnvisitedNodes();
799 if (!locations.empty()) {
800 for (const auto& location : locations) {
801 AIDL_ERROR(location) << "AidlNode at location was not visited!";
802 }
803 AIDL_FATAL(AIDL_LOCATION_HERE)
804 << "The AIDL AST was not processed fully. Please report an issue.";
805 }
806 }
807
Steven Moreland4e059132021-08-11 13:36:30 -0700808 return success ? 0 : 1;
Steven Moreland3981d9e2020-03-31 14:11:44 -0700809}
810
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700811} // namespace aidl
Steven Morelandf4c64df2019-07-29 19:54:04 -0700812} // namespace android