blob: 3de3e0611cb161340e33476fc4c318bfcc0be878 [file] [log] [blame]
Will McVickerefd970d2019-09-25 15:28:30 -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 */
16
Adam Lesinskiffa16862014-01-23 18:17:42 -080017#include "aidl_language.h"
Jiyong Park1deecc32018-07-17 01:14:41 +090018#include "aidl_typenames.h"
Jiyong Parke5c45292020-05-26 19:06:24 +090019#include "parser.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070020
Adam Lesinskiffa16862014-01-23 18:17:42 -080021#include <stdio.h>
Adam Lesinskiffa16862014-01-23 18:17:42 -080022#include <stdlib.h>
Christopher Wiley4a2884b2015-10-07 11:27:45 -070023#include <string.h>
Jiyong Park68bc77a2018-07-19 19:00:45 +090024#include <algorithm>
Jiyong Park1deecc32018-07-17 01:14:41 +090025#include <iostream>
Jiyong Park68bc77a2018-07-19 19:00:45 +090026#include <set>
27#include <sstream>
Casey Dahlindd691812015-09-09 17:59:06 -070028#include <string>
Jiyong Park1deecc32018-07-17 01:14:41 +090029#include <utility>
Christopher Wileyf690be52015-09-14 15:19:10 -070030
Steven Moreland1c4ba202018-08-09 10:49:54 -070031#include <android-base/parsedouble.h>
Roshan Pius9d7810a2016-07-28 08:57:50 -070032#include <android-base/parseint.h>
Elliott Hughes0a620672015-12-04 13:53:18 -080033#include <android-base/strings.h>
Christopher Wileyd76067c2015-10-19 17:00:13 -070034
Steven Moreland21780812020-09-11 01:29:45 +000035#include "aidl_language_y.h"
Christopher Wiley4a2884b2015-10-07 11:27:45 -070036#include "logging.h"
Adam Lesinskiffa16862014-01-23 18:17:42 -080037
Will McVickerd7d18df2019-09-12 13:40:50 -070038#include "aidl.h"
39
Casey Dahlin07b9dde2015-09-10 19:13:49 -070040#ifdef _WIN32
41int isatty(int fd)
42{
43 return (fd == 0);
44}
45#endif
46
Jooyung Han888c5bc2020-12-22 17:28:47 +090047using android::aidl::DiagnosticID;
Christopher Wiley4a2884b2015-10-07 11:27:45 -070048using android::aidl::IoDelegate;
Christopher Wileyd76067c2015-10-19 17:00:13 -070049using android::base::Join;
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -080050using android::base::Split;
Casey Dahlindd691812015-09-09 17:59:06 -070051using std::cerr;
Jiyong Park1deecc32018-07-17 01:14:41 +090052using std::pair;
Jiyong Park68bc77a2018-07-19 19:00:45 +090053using std::set;
Christopher Wiley4a2884b2015-10-07 11:27:45 -070054using std::string;
55using std::unique_ptr;
Jiyong Parkccf00f82018-07-17 01:39:23 +090056using std::vector;
Adam Lesinskiffa16862014-01-23 18:17:42 -080057
Jeongik Cha047c5ee2019-08-07 23:16:49 +090058namespace {
Jeongik Cha997281d2020-01-16 15:23:59 +090059bool IsJavaKeyword(const char* str) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +090060 static const std::vector<std::string> kJavaKeywords{
61 "abstract", "assert", "boolean", "break", "byte", "case", "catch",
62 "char", "class", "const", "continue", "default", "do", "double",
63 "else", "enum", "extends", "final", "finally", "float", "for",
64 "goto", "if", "implements", "import", "instanceof", "int", "interface",
65 "long", "native", "new", "package", "private", "protected", "public",
66 "return", "short", "static", "strictfp", "super", "switch", "synchronized",
67 "this", "throw", "throws", "transient", "try", "void", "volatile",
68 "while", "true", "false", "null",
69 };
70 return std::find(kJavaKeywords.begin(), kJavaKeywords.end(), str) != kJavaKeywords.end();
71}
Jeongik Cha997281d2020-01-16 15:23:59 +090072
73void AddHideComment(CodeWriter* writer) {
74 writer->Write("/* @hide */\n");
75}
76
77inline bool HasHideComment(const std::string& comment) {
78 return std::regex_search(comment, std::regex("@hide\\b"));
79}
Jeongik Cha047c5ee2019-08-07 23:16:49 +090080} // namespace
81
Devin Mooredf93ebb2020-03-25 14:03:35 -070082AidlLocation::AidlLocation(const std::string& file, Point begin, Point end, Source source)
83 : file_(file), begin_(begin), end_(end), source_(source) {}
Steven Moreland46e9da82018-07-27 15:45:29 -070084
85std::ostream& operator<<(std::ostream& os, const AidlLocation& l) {
Devin Moore5de18ed2020-04-02 13:52:29 -070086 os << l.file_;
87 if (l.LocationKnown()) {
88 os << ":" << l.begin_.line << "." << l.begin_.column << "-";
89 if (l.begin_.line != l.end_.line) {
90 os << l.end_.line << ".";
91 }
92 os << l.end_.column;
Steven Moreland46e9da82018-07-27 15:45:29 -070093 }
Steven Moreland46e9da82018-07-27 15:45:29 -070094 return os;
95}
96
97AidlNode::AidlNode(const AidlLocation& location) : location_(location) {}
98
Mathew Inwoodadb74672019-11-29 14:01:53 +000099std::string AidlNode::PrintLine() const {
Andrei Onea8714b022019-02-01 18:55:54 +0000100 std::stringstream ss;
101 ss << location_.file_ << ":" << location_.begin_.line;
102 return ss.str();
103}
104
Mathew Inwoodadb74672019-11-29 14:01:53 +0000105std::string AidlNode::PrintLocation() const {
106 std::stringstream ss;
107 ss << location_.file_ << ":" << location_.begin_.line << ":" << location_.begin_.column << ":"
108 << location_.end_.line << ":" << location_.end_.column;
109 return ss.str();
110}
111
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700112const std::vector<AidlAnnotation::Schema>& AidlAnnotation::AllSchemas() {
113 static const std::vector<Schema> kSchemas{
Jooyung Hand902a972020-10-23 17:32:44 +0900114 {AidlAnnotation::Type::NULLABLE, "nullable", {}, false},
115 {AidlAnnotation::Type::UTF8_IN_CPP, "utf8InCpp", {}, false},
Steven Morelanda7764e52020-10-27 17:29:29 +0000116 {AidlAnnotation::Type::SENSITIVE_DATA, "SensitiveData", {}, false},
Jooyung Hand902a972020-10-23 17:32:44 +0900117 {AidlAnnotation::Type::VINTF_STABILITY, "VintfStability", {}, false},
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700118 {AidlAnnotation::Type::UNSUPPORTED_APP_USAGE,
119 "UnsupportedAppUsage",
120 {{"expectedSignature", "String"},
121 {"implicitMember", "String"},
122 {"maxTargetSdk", "int"},
123 {"publicAlternatives", "String"},
Jooyung Hand902a972020-10-23 17:32:44 +0900124 {"trackingBug", "long"}},
125 false},
126 {AidlAnnotation::Type::JAVA_STABLE_PARCELABLE, "JavaOnlyStableParcelable", {}, false},
127 {AidlAnnotation::Type::HIDE, "Hide", {}, false},
Jooyung Han5721a232020-12-24 04:34:55 +0900128 {AidlAnnotation::Type::BACKING, "Backing", {{"type", "String"}}, false, {"type"}},
129 {AidlAnnotation::Type::JAVA_PASSTHROUGH,
130 "JavaPassthrough",
131 {{"annotation", "String"}},
132 true,
133 {"annotation"}},
Jiyong Park9aa3d042020-12-04 23:30:02 +0900134 {AidlAnnotation::Type::JAVA_DERIVE,
Jooyung Han5721a232020-12-24 04:34:55 +0900135 "JavaDerive",
136 {{"toString", "boolean"}, {"equals", "boolean"}},
137 false},
Jooyung Hand902a972020-10-23 17:32:44 +0900138 {AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE, "JavaOnlyImmutable", {}, false},
139 {AidlAnnotation::Type::FIXED_SIZE, "FixedSize", {}, false},
Jooyung Han5721a232020-12-24 04:34:55 +0900140 {AidlAnnotation::Type::DESCRIPTOR, "Descriptor", {{"value", "String"}}, false, {"value"}},
Andrei Homescue61feb52020-08-18 15:44:24 -0700141 {AidlAnnotation::Type::RUST_DERIVE,
142 "RustDerive",
143 {{"Copy", "boolean"},
144 {"Clone", "boolean"},
145 {"PartialOrd", "boolean"},
146 {"Ord", "boolean"},
147 {"PartialEq", "boolean"},
148 {"Eq", "boolean"},
Jooyung Hand902a972020-10-23 17:32:44 +0900149 {"Hash", "boolean"}},
150 false},
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900151 };
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700152 return kSchemas;
153}
Jiyong Park68bc77a2018-07-19 19:00:45 +0900154
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700155std::string AidlAnnotation::TypeToString(Type type) {
156 for (const Schema& schema : AllSchemas()) {
157 if (type == schema.type) return schema.name;
158 }
159 AIDL_FATAL(AIDL_LOCATION_HERE) << "Unrecognized type: " << static_cast<size_t>(type);
160 __builtin_unreachable();
161}
Andrei Onea9445fc62019-06-27 18:11:59 +0100162
163AidlAnnotation* AidlAnnotation::Parse(
164 const AidlLocation& location, const string& name,
165 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700166 const Schema* schema = nullptr;
167 for (const Schema& a_schema : AllSchemas()) {
168 if (a_schema.name == name) {
169 schema = &a_schema;
170 }
171 }
172
173 if (schema == nullptr) {
Jiyong Park68bc77a2018-07-19 19:00:45 +0900174 std::ostringstream stream;
Steven Moreland46e9da82018-07-27 15:45:29 -0700175 stream << "'" << name << "' is not a recognized annotation. ";
Jiyong Park68bc77a2018-07-19 19:00:45 +0900176 stream << "It must be one of:";
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700177 for (const Schema& s : AllSchemas()) {
178 stream << " " << s.name;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900179 }
180 stream << ".";
Steven Moreland46e9da82018-07-27 15:45:29 -0700181 AIDL_ERROR(location) << stream.str();
182 return nullptr;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900183 }
Andrei Onea9445fc62019-06-27 18:11:59 +0100184 if (parameter_list == nullptr) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700185 return new AidlAnnotation(location, *schema, {});
Andrei Onea9445fc62019-06-27 18:11:59 +0100186 }
187
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700188 return new AidlAnnotation(location, *schema, std::move(*parameter_list));
Jiyong Park68bc77a2018-07-19 19:00:45 +0900189}
190
Andrei Onea9445fc62019-06-27 18:11:59 +0100191AidlAnnotation::AidlAnnotation(
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700192 const AidlLocation& location, const Schema& schema,
Andrei Onea9445fc62019-06-27 18:11:59 +0100193 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters)
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700194 : AidlNode(location), schema_(schema), parameters_(std::move(parameters)) {}
Andrei Onea9445fc62019-06-27 18:11:59 +0100195
Jooyung Han690f5842020-12-04 13:02:04 +0900196struct ConstReferenceFinder : AidlConstantValue::Visitor {
197 AidlConstantReference* found;
198 void Visit(AidlConstantValue&) override {}
199 void Visit(AidlUnaryConstExpression&) override {}
200 void Visit(AidlBinaryConstExpression&) override {}
201 void Visit(AidlConstantReference& ref) override {
202 if (!found) found = &ref;
203 }
204};
205
Andrei Onea9445fc62019-06-27 18:11:59 +0100206bool AidlAnnotation::CheckValid() const {
Andrei Onea9445fc62019-06-27 18:11:59 +0100207 for (const auto& name_and_param : parameters_) {
208 const std::string& param_name = name_and_param.first;
209 const std::shared_ptr<AidlConstantValue>& param = name_and_param.second;
Jooyung Han690f5842020-12-04 13:02:04 +0900210
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700211 auto parameter_mapping_it = schema_.supported_parameters.find(param_name);
212 if (parameter_mapping_it == schema_.supported_parameters.end()) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100213 std::ostringstream stream;
214 stream << "Parameter " << param_name << " not supported ";
Devin Mooredecaf292020-04-30 09:16:40 -0700215 stream << "for annotation " << GetName() << ". ";
Andrei Onea9445fc62019-06-27 18:11:59 +0100216 stream << "It must be one of:";
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700217 for (const auto& kv : schema_.supported_parameters) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100218 stream << " " << kv.first;
219 }
220 AIDL_ERROR(this) << stream.str();
221 return false;
222 }
Jooyung Han690f5842020-12-04 13:02:04 +0900223
224 ConstReferenceFinder finder;
225 param->Accept(finder);
226 if (finder.found) {
227 AIDL_ERROR(finder.found) << "Value must be a constant expression but contains reference to "
228 << finder.found->GetFieldName() << ".";
229 return false;
230 }
231
232 if (!param->CheckValid()) {
233 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
234 << GetName() << ".";
235 return false;
236 }
237
Andrei Onea9445fc62019-06-27 18:11:59 +0100238 AidlTypeSpecifier type{AIDL_LOCATION_HERE, parameter_mapping_it->second, false, nullptr, ""};
Will McVickerd7d18df2019-09-12 13:40:50 -0700239 const std::string param_value = param->ValueString(type, AidlConstantValueDecorator);
Andrei Onea9445fc62019-06-27 18:11:59 +0100240 // Assume error on empty string.
241 if (param_value == "") {
242 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
243 << GetName() << ".";
244 return false;
245 }
246 }
Jooyung Han5721a232020-12-24 04:34:55 +0900247 bool success = true;
248 for (const auto& param : schema_.required_parameters) {
249 if (parameters_.count(param) == 0) {
250 AIDL_ERROR(this) << "Missing '" << param << "' on @" << GetName() << ".";
251 success = false;
252 }
253 }
254 return success;
Andrei Onea9445fc62019-06-27 18:11:59 +0100255}
256
257std::map<std::string, std::string> AidlAnnotation::AnnotationParams(
258 const ConstantValueDecorator& decorator) const {
259 std::map<std::string, std::string> raw_params;
Andrei Onea9445fc62019-06-27 18:11:59 +0100260 for (const auto& name_and_param : parameters_) {
261 const std::string& param_name = name_and_param.first;
262 const std::shared_ptr<AidlConstantValue>& param = name_and_param.second;
Devin Mooredecaf292020-04-30 09:16:40 -0700263 if (schema_.supported_parameters.find(param_name) == schema_.supported_parameters.end()) {
264 std::ostringstream stream;
265 stream << "Parameter " << param_name << " not supported ";
266 stream << "for annotation " << GetName() << ". ";
267 stream << "It must be one of:";
268 for (const auto& kv : schema_.supported_parameters) {
269 stream << " " << kv.first;
270 }
271 AIDL_ERROR(this) << stream.str();
272 continue;
273 }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700274 AidlTypeSpecifier type{AIDL_LOCATION_HERE, schema_.supported_parameters.at(param_name), false,
275 nullptr, ""};
Will McVickerd7d18df2019-09-12 13:40:50 -0700276 if (!param->CheckValid()) {
277 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
278 << GetName() << ".";
Devin Mooredecaf292020-04-30 09:16:40 -0700279 continue;
Will McVickerd7d18df2019-09-12 13:40:50 -0700280 }
281
282 raw_params.emplace(param_name, param->ValueString(type, decorator));
Andrei Onea9445fc62019-06-27 18:11:59 +0100283 }
284 return raw_params;
285}
Steven Moreland46e9da82018-07-27 15:45:29 -0700286
Jooyung Han965e31d2020-11-27 12:30:16 +0900287std::string AidlAnnotation::ToString() const {
Daniel Norman37d43dd2019-09-09 17:22:34 -0700288 if (parameters_.empty()) {
289 return "@" + GetName();
290 } else {
291 vector<string> param_strings;
Jooyung Han965e31d2020-11-27 12:30:16 +0900292 for (const auto& [name, value] : AnnotationParams(AidlConstantValueDecorator)) {
Daniel Norman37d43dd2019-09-09 17:22:34 -0700293 param_strings.emplace_back(name + "=" + value);
294 }
295 return "@" + GetName() + "(" + Join(param_strings, ", ") + ")";
296 }
297}
298
Andrei Onea9445fc62019-06-27 18:11:59 +0100299static const AidlAnnotation* GetAnnotation(const vector<AidlAnnotation>& annotations,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700300 AidlAnnotation::Type type) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100301 for (const auto& a : annotations) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700302 if (a.GetType() == type) {
Jooyung Hand902a972020-10-23 17:32:44 +0900303 AIDL_FATAL_IF(a.Repeatable(), a)
304 << "Trying to get a single annotation when it is repeatable.";
Andrei Onea9445fc62019-06-27 18:11:59 +0100305 return &a;
306 }
307 }
308 return nullptr;
309}
310
Steven Moreland46e9da82018-07-27 15:45:29 -0700311AidlAnnotatable::AidlAnnotatable(const AidlLocation& location) : AidlNode(location) {}
312
Jiyong Park68bc77a2018-07-19 19:00:45 +0900313bool AidlAnnotatable::IsNullable() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700314 return GetAnnotation(annotations_, AidlAnnotation::Type::NULLABLE);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900315}
316
Jiyong Park68bc77a2018-07-19 19:00:45 +0900317bool AidlAnnotatable::IsUtf8InCpp() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700318 return GetAnnotation(annotations_, AidlAnnotation::Type::UTF8_IN_CPP);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900319}
320
Steven Morelanda7764e52020-10-27 17:29:29 +0000321bool AidlAnnotatable::IsSensitiveData() const {
322 return GetAnnotation(annotations_, AidlAnnotation::Type::SENSITIVE_DATA);
323}
324
Steven Morelanda57d0a62019-07-30 09:41:14 -0700325bool AidlAnnotatable::IsVintfStability() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700326 return GetAnnotation(annotations_, AidlAnnotation::Type::VINTF_STABILITY);
Steven Morelanda57d0a62019-07-30 09:41:14 -0700327}
328
Jeongik Chad0a10272020-08-06 16:33:36 +0900329bool AidlAnnotatable::IsJavaOnlyImmutable() const {
330 return GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE);
Jeongik Cha36f76c32020-07-28 00:25:52 +0900331}
332
Devin Moorec7e47a32020-08-07 10:55:25 -0700333bool AidlAnnotatable::IsFixedSize() const {
334 return GetAnnotation(annotations_, AidlAnnotation::Type::FIXED_SIZE);
335}
336
Andrei Onea9445fc62019-06-27 18:11:59 +0100337const AidlAnnotation* AidlAnnotatable::UnsupportedAppUsage() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700338 return GetAnnotation(annotations_, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE);
Jiyong Parka6605ab2018-11-11 14:30:21 +0900339}
340
Andrei Homescue61feb52020-08-18 15:44:24 -0700341const AidlAnnotation* AidlAnnotatable::RustDerive() const {
342 return GetAnnotation(annotations_, AidlAnnotation::Type::RUST_DERIVE);
343}
344
Jooyung Han672557b2020-12-24 05:18:00 +0900345const AidlAnnotation* AidlAnnotatable::BackingType() const {
346 return GetAnnotation(annotations_, AidlAnnotation::Type::BACKING);
Daniel Norman85aed542019-08-21 12:01:14 -0700347}
348
Jeongik Cha88f95a82020-01-15 13:02:16 +0900349bool AidlAnnotatable::IsStableApiParcelable(Options::Language lang) const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700350 return lang == Options::Language::JAVA &&
351 GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_STABLE_PARCELABLE);
Jeongik Cha82317dd2019-02-27 20:26:11 +0900352}
353
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800354bool AidlAnnotatable::IsHide() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700355 return GetAnnotation(annotations_, AidlAnnotation::Type::HIDE);
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800356}
357
Jooyung Han829ec7c2020-12-02 12:07:36 +0900358bool AidlAnnotatable::JavaDerive(const std::string& method) const {
359 auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_DERIVE);
360 if (annotation != nullptr) {
361 auto params = annotation->AnnotationParams(AidlConstantValueDecorator);
362 if (auto it = params.find(method); it != params.end()) {
363 return it->second == "true";
364 }
365 }
366 return false;
Jiyong Park43113fb2020-07-20 16:26:19 +0900367}
368
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900369std::string AidlAnnotatable::GetDescriptor() const {
370 auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::DESCRIPTOR);
371 if (annotation != nullptr) {
372 auto params = annotation->AnnotationParams(AidlConstantValueDecorator);
373 if (auto it = params.find("value"); it != params.end()) {
374 const string& value = it->second;
375
376 AIDL_FATAL_IF(value.size() < 2, this) << value;
377 AIDL_FATAL_IF(value[0] != '"', this) << value;
378 AIDL_FATAL_IF(value[value.length() - 1] != '"', this) << value;
379 std::string unquoted_value = value.substr(1, value.length() - 2);
380 return unquoted_value;
381 }
382 }
383 return "";
384}
385
Steven Moreland7e4b9502020-02-20 18:10:42 -0800386void AidlAnnotatable::DumpAnnotations(CodeWriter* writer) const {
387 if (annotations_.empty()) return;
388
389 writer->Write("%s\n", AidlAnnotatable::ToString().c_str());
390}
391
Devin Moore24f68572020-02-26 13:20:59 -0800392bool AidlAnnotatable::CheckValid(const AidlTypenames&) const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700393 std::set<AidlAnnotation::Type> supported_annotations = GetSupportedAnnotations();
Andrei Onea9445fc62019-06-27 18:11:59 +0100394 for (const auto& annotation : GetAnnotations()) {
Jooyung Hand902a972020-10-23 17:32:44 +0900395 // check if it is allowed for this node
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700396 if (supported_annotations.find(annotation.GetType()) == supported_annotations.end()) {
Jooyung Hand902a972020-10-23 17:32:44 +0900397 std::vector<std::string> supported_annot_strings;
398 for (AidlAnnotation::Type type : supported_annotations) {
399 supported_annot_strings.push_back(AidlAnnotation::TypeToString(type));
400 }
Devin Moore24f68572020-02-26 13:20:59 -0800401 AIDL_ERROR(this) << "'" << annotation.GetName()
402 << "' is not a supported annotation for this node. "
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700403 << "It must be one of: "
404 << android::base::Join(supported_annot_strings, ", ");
Devin Moore24f68572020-02-26 13:20:59 -0800405 return false;
406 }
Jooyung Hand902a972020-10-23 17:32:44 +0900407 // CheckValid() only if it is okay to be here
408 if (!annotation.CheckValid()) {
409 return false;
410 }
411 }
412
413 std::map<AidlAnnotation::Type, AidlLocation> declared;
414 for (const auto& annotation : GetAnnotations()) {
415 const auto& [iter, inserted] = declared.emplace(annotation.GetType(), annotation.GetLocation());
416 if (!inserted && !annotation.Repeatable()) {
417 AIDL_ERROR(this) << "'" << annotation.GetName()
418 << "' is repeated, but not allowed. Previous location: " << iter->second;
419 return false;
420 }
Andrei Onea9445fc62019-06-27 18:11:59 +0100421 }
Steven Morelanda57d0a62019-07-30 09:41:14 -0700422
Andrei Onea9445fc62019-06-27 18:11:59 +0100423 return true;
424}
425
Jiyong Park68bc77a2018-07-19 19:00:45 +0900426string AidlAnnotatable::ToString() const {
427 vector<string> ret;
428 for (const auto& a : annotations_) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900429 ret.emplace_back(a.ToString());
Jiyong Park68bc77a2018-07-19 19:00:45 +0900430 }
431 std::sort(ret.begin(), ret.end());
432 return Join(ret, " ");
433}
434
Steven Moreland46e9da82018-07-27 15:45:29 -0700435AidlTypeSpecifier::AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name,
436 bool is_array,
Jiyong Park1deecc32018-07-17 01:14:41 +0900437 vector<unique_ptr<AidlTypeSpecifier>>* type_params,
Steven Moreland46e9da82018-07-27 15:45:29 -0700438 const string& comments)
439 : AidlAnnotatable(location),
Jeongik Chadf76dc72019-11-28 00:08:47 +0900440 AidlParameterizable<unique_ptr<AidlTypeSpecifier>>(type_params),
Steven Moreland46e9da82018-07-27 15:45:29 -0700441 unresolved_name_(unresolved_name),
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700442 is_array_(is_array),
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900443 comments_(comments),
444 split_name_(Split(unresolved_name, ".")) {}
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700445
Jooyung Hand2fa0232020-10-19 02:51:41 +0900446const AidlTypeSpecifier& AidlTypeSpecifier::ArrayBase() const {
Steven Moreland3f658cf2018-08-20 13:40:54 -0700447 AIDL_FATAL_IF(!is_array_, this);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900448 // Declaring array of generic type cannot happen, it is grammar error.
449 AIDL_FATAL_IF(IsGeneric(), this);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700450
Jooyung Hand2fa0232020-10-19 02:51:41 +0900451 if (!array_base_) {
452 array_base_.reset(new AidlTypeSpecifier(*this));
453 array_base_->is_array_ = false;
454 }
455 return *array_base_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700456}
457
Jeongik Cha997281d2020-01-16 15:23:59 +0900458bool AidlTypeSpecifier::IsHidden() const {
459 return HasHideComment(GetComments());
460}
461
Jooyung Han965e31d2020-11-27 12:30:16 +0900462string AidlTypeSpecifier::Signature() const {
Jiyong Park1deecc32018-07-17 01:14:41 +0900463 string ret = GetName();
464 if (IsGeneric()) {
465 vector<string> arg_names;
466 for (const auto& ta : GetTypeParameters()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900467 arg_names.emplace_back(ta->Signature());
Jiyong Parkccf00f82018-07-17 01:39:23 +0900468 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900469 ret += "<" + Join(arg_names, ",") + ">";
Jiyong Parkccf00f82018-07-17 01:39:23 +0900470 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900471 if (IsArray()) {
472 ret += "[]";
473 }
474 return ret;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900475}
476
Jooyung Han965e31d2020-11-27 12:30:16 +0900477string AidlTypeSpecifier::ToString() const {
478 string ret = Signature();
Jiyong Park02da7422018-07-16 16:00:26 +0900479 string annotations = AidlAnnotatable::ToString();
480 if (annotations != "") {
481 ret = annotations + " " + ret;
482 }
483 return ret;
484}
485
Daniel Norman716d3112019-09-10 13:11:56 -0700486bool AidlTypeSpecifier::Resolve(const AidlTypenames& typenames) {
Steven Moreland21780812020-09-11 01:29:45 +0000487 AIDL_FATAL_IF(IsResolved(), this);
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700488 AidlTypenames::ResolvedTypename result = typenames.ResolveTypename(unresolved_name_);
489 if (result.is_resolved) {
490 fully_qualified_name_ = result.canonical_name;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900491 split_name_ = Split(fully_qualified_name_, ".");
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900492 defined_type_ = result.defined_type;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900493 }
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700494 return result.is_resolved;
Casey Dahlin70078e62015-09-30 17:01:30 -0700495}
496
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900497const AidlDefinedType* AidlTypeSpecifier::GetDefinedType() const {
498 return defined_type_;
499}
500
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700501std::set<AidlAnnotation::Type> AidlTypeSpecifier::GetSupportedAnnotations() const {
Devin Moore24f68572020-02-26 13:20:59 -0800502 // kHide and kUnsupportedAppUsage are both method return annotations
503 // which we don't distinguish from other type specifiers.
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700504 return {AidlAnnotation::Type::NULLABLE, AidlAnnotation::Type::UTF8_IN_CPP,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900505 AidlAnnotation::Type::UNSUPPORTED_APP_USAGE, AidlAnnotation::Type::HIDE,
506 AidlAnnotation::Type::JAVA_PASSTHROUGH};
Devin Moore24f68572020-02-26 13:20:59 -0800507}
508
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900509bool AidlTypeSpecifier::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -0800510 if (!AidlAnnotatable::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100511 return false;
512 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900513 if (IsGeneric()) {
514 const string& type_name = GetName();
Jeongik Chae74c86d2019-12-12 16:54:03 +0900515
516 auto& types = GetTypeParameters();
517 // TODO(b/136048684) Disallow to use primitive types only if it is List or Map.
518 if (type_name == "List" || type_name == "Map") {
Jooyung Hane87cdd02020-12-11 16:47:35 +0900519 if (std::any_of(types.begin(), types.end(), [&](auto& type_ptr) {
520 return (typenames.GetEnumDeclaration(*type_ptr)) ||
521 AidlTypenames::IsPrimitiveTypename(type_ptr->GetName());
Jeongik Chae74c86d2019-12-12 16:54:03 +0900522 })) {
Devin Moore7b8d5c92020-03-17 14:14:08 -0700523 AIDL_ERROR(this) << "A generic type cannot have any primitive type parameters.";
Jeongik Chae74c86d2019-12-12 16:54:03 +0900524 return false;
525 }
526 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800527 const auto defined_type = typenames.TryGetDefinedType(type_name);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900528 const auto parameterizable =
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800529 defined_type != nullptr ? defined_type->AsParameterizable() : nullptr;
530 const bool is_user_defined_generic_type =
Jeongik Chadf76dc72019-11-28 00:08:47 +0900531 parameterizable != nullptr && parameterizable->IsGeneric();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800532 const size_t num_params = GetTypeParameters().size();
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900533 if (type_name == "List") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800534 if (num_params > 1) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900535 AIDL_ERROR(this) << "List can only have one type parameter, but got: '" << Signature()
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000536 << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900537 return false;
538 }
Jooyung Han55f96ad2020-12-13 10:08:33 +0900539 const AidlTypeSpecifier& contained_type = *GetTypeParameters()[0];
540 const string& contained_type_name = contained_type.GetName();
541 if (AidlTypenames::IsBuiltinTypename(contained_type_name)) {
542 if (contained_type_name != "String" && contained_type_name != "IBinder" &&
543 contained_type_name != "ParcelFileDescriptor") {
544 AIDL_ERROR(this) << "List<" << contained_type_name
545 << "> is not supported. List<T> supports parcelable/union, String, "
546 "IBinder, and ParcelFileDescriptor.";
547 return false;
548 }
549 } else { // Defined types
550 if (typenames.GetInterface(contained_type)) {
551 AIDL_ERROR(this) << "List<" << contained_type_name
552 << "> is not supported. List<T> supports parcelable/union, String, "
553 "IBinder, and ParcelFileDescriptor.";
554 return false;
555 }
556 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900557 } else if (type_name == "Map") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800558 if (num_params != 0 && num_params != 2) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900559 AIDL_ERROR(this) << "Map must have 0 or 2 type parameters, but got "
Jooyung Han965e31d2020-11-27 12:30:16 +0900560 << "'" << Signature() << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900561 return false;
562 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800563 if (num_params == 2) {
Jeongik Chae48d9942020-01-02 17:39:00 +0900564 const string& key_type = GetTypeParameters()[0]->GetName();
565 if (key_type != "String") {
566 AIDL_ERROR(this) << "The type of key in map must be String, but it is "
567 << "'" << key_type << "'";
568 return false;
569 }
570 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800571 } else if (is_user_defined_generic_type) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900572 const size_t allowed = parameterizable->GetTypeParameters().size();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800573 if (num_params != allowed) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900574 AIDL_ERROR(this) << type_name << " must have " << allowed << " type parameters, but got "
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800575 << num_params;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900576 return false;
577 }
578 } else {
579 AIDL_ERROR(this) << type_name << " is not a generic type.";
580 return false;
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900581 }
582 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900583
Steven Moreland11cb9452020-01-21 16:56:58 -0800584 const bool is_generic_string_list = GetName() == "List" && IsGeneric() &&
585 GetTypeParameters().size() == 1 &&
586 GetTypeParameters()[0]->GetName() == "String";
587 if (IsUtf8InCpp() && (GetName() != "String" && !is_generic_string_list)) {
588 AIDL_ERROR(this) << "@utf8InCpp can only be used on String, String[], and List<String>.";
589 return false;
590 }
591
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900592 if (GetName() == "void") {
593 if (IsArray() || IsNullable() || IsUtf8InCpp()) {
594 AIDL_ERROR(this) << "void type cannot be an array or nullable or utf8 string";
595 return false;
596 }
597 }
598
599 if (IsArray()) {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800600 const auto defined_type = typenames.TryGetDefinedType(GetName());
601 if (defined_type != nullptr && defined_type->AsInterface() != nullptr) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900602 AIDL_ERROR(this) << "Binder type cannot be an array";
603 return false;
604 }
Steven Moreland8042d2d2020-09-30 23:31:32 +0000605 if (GetName() == "ParcelableHolder") {
606 AIDL_ERROR(this) << "Arrays of ParcelableHolder are not supported.";
607 return false;
608 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900609 }
610
611 if (IsNullable()) {
612 if (AidlTypenames::IsPrimitiveTypename(GetName()) && !IsArray()) {
613 AIDL_ERROR(this) << "Primitive type cannot get nullable annotation";
614 return false;
615 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800616 const auto defined_type = typenames.TryGetDefinedType(GetName());
617 if (defined_type != nullptr && defined_type->AsEnumDeclaration() != nullptr && !IsArray()) {
Daniel Normanee8674f2019-09-20 16:07:00 -0700618 AIDL_ERROR(this) << "Enum type cannot get nullable annotation";
619 return false;
620 }
Jeongik Chaf6ec8982020-10-15 00:10:30 +0900621 if (GetName() == "ParcelableHolder") {
622 AIDL_ERROR(this) << "ParcelableHolder cannot be nullable.";
623 return false;
624 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900625 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900626 return true;
627}
628
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900629std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type,
Steven Moreland860b1942018-08-16 14:59:28 -0700630 const std::string& raw_value) {
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900631 if (type.IsArray()) {
632 return raw_value;
633 }
634
635 if (auto defined_type = type.GetDefinedType(); defined_type) {
636 auto enum_type = defined_type->AsEnumDeclaration();
637 AIDL_FATAL_IF(!enum_type, type) << "Invalid type for \"" << raw_value << "\"";
638 return type.GetName() + "." + raw_value.substr(raw_value.find_last_of('.') + 1);
639 }
Steven Moreland860b1942018-08-16 14:59:28 -0700640 return raw_value;
641}
642
Steven Moreland46e9da82018-07-27 15:45:29 -0700643AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
644 AidlTypeSpecifier* type, const std::string& name)
Steven Moreland541788d2020-05-21 22:05:52 +0000645 : AidlVariableDeclaration(location, type, name, AidlConstantValue::Default(*type)) {
646 default_user_specified_ = false;
647}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700648
Steven Moreland46e9da82018-07-27 15:45:29 -0700649AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
650 AidlTypeSpecifier* type, const std::string& name,
651 AidlConstantValue* default_value)
Jooyung Han3f347ca2020-12-01 12:41:50 +0900652 : AidlMember(location),
Steven Moreland541788d2020-05-21 22:05:52 +0000653 type_(type),
654 name_(name),
655 default_user_specified_(true),
656 default_value_(default_value) {}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700657
Jooyung Han53fb4242020-12-17 16:03:49 +0900658bool AidlVariableDeclaration::HasUsefulDefaultValue() const {
659 if (GetDefaultValue()) {
660 return true;
661 }
662 // null is accepted as a valid default value in all backends
663 if (GetType().IsNullable()) {
664 return true;
665 }
666 return false;
667}
668
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900669bool AidlVariableDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700670 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900671 valid &= type_->CheckValid(typenames);
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900672
Steven Moreland54be7bd2019-12-05 11:17:53 -0800673 if (type_->GetName() == "void") {
674 AIDL_ERROR(this) << "Declaration " << name_
675 << " is void, but declarations cannot be of void type.";
676 valid = false;
677 }
678
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900679 if (default_value_ == nullptr) return valid;
Steven Moreland25294322018-08-07 18:13:55 -0700680 valid &= default_value_->CheckValid();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700681
Steven Moreland25294322018-08-07 18:13:55 -0700682 if (!valid) return false;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700683
Steven Moreland860b1942018-08-16 14:59:28 -0700684 return !ValueString(AidlConstantValueDecorator).empty();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700685}
Steven Moreland5557f1c2018-07-02 13:50:23 -0700686
Jooyung Hanacae85d2020-10-28 16:39:09 +0900687string AidlVariableDeclaration::GetCapitalizedName() const {
688 AIDL_FATAL_IF(name_.size() <= 0, *this) << "Name can't be empty.";
689 string str = name_;
690 str[0] = static_cast<char>(toupper(str[0]));
691 return str;
692}
693
Steven Moreland5557f1c2018-07-02 13:50:23 -0700694string AidlVariableDeclaration::ToString() const {
Jooyung Han965e31d2020-11-27 12:30:16 +0900695 string ret = type_->ToString() + " " + name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000696 if (default_value_ != nullptr && default_user_specified_) {
Steven Moreland860b1942018-08-16 14:59:28 -0700697 ret += " = " + ValueString(AidlConstantValueDecorator);
Steven Moreland9ea10e32018-07-19 15:26:09 -0700698 }
699 return ret;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700700}
701
Jiyong Park02da7422018-07-16 16:00:26 +0900702string AidlVariableDeclaration::Signature() const {
703 return type_->Signature() + " " + name_;
704}
705
Steven Moreland860b1942018-08-16 14:59:28 -0700706std::string AidlVariableDeclaration::ValueString(const ConstantValueDecorator& decorator) const {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900707 if (default_value_ != nullptr) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700708 return default_value_->ValueString(GetType(), decorator);
Jiyong Parka468e2a2018-08-29 21:25:18 +0900709 } else {
710 return "";
711 }
Steven Moreland25294322018-08-07 18:13:55 -0700712}
713
Steven Moreland46e9da82018-07-27 15:45:29 -0700714AidlArgument::AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
715 AidlTypeSpecifier* type, const std::string& name)
716 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700717 direction_(direction),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700718 direction_specified_(true) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700719
Steven Moreland46e9da82018-07-27 15:45:29 -0700720AidlArgument::AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type,
721 const std::string& name)
722 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700723 direction_(AidlArgument::IN_DIR),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700724 direction_specified_(false) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700725
Jiyong Park02da7422018-07-16 16:00:26 +0900726string AidlArgument::GetDirectionSpecifier() const {
Casey Dahlinc378c992015-09-29 16:50:40 -0700727 string ret;
Casey Dahlinc378c992015-09-29 16:50:40 -0700728 if (direction_specified_) {
729 switch(direction_) {
730 case AidlArgument::IN_DIR:
Devin Mooreeccdb902020-03-24 16:22:40 -0700731 ret += "in";
Casey Dahlinc378c992015-09-29 16:50:40 -0700732 break;
733 case AidlArgument::OUT_DIR:
Devin Mooreeccdb902020-03-24 16:22:40 -0700734 ret += "out";
Casey Dahlinc378c992015-09-29 16:50:40 -0700735 break;
736 case AidlArgument::INOUT_DIR:
Devin Mooreeccdb902020-03-24 16:22:40 -0700737 ret += "inout";
Casey Dahlinc378c992015-09-29 16:50:40 -0700738 break;
739 }
740 }
Casey Dahlinc378c992015-09-29 16:50:40 -0700741 return ret;
742}
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700743
Jiyong Park02da7422018-07-16 16:00:26 +0900744string AidlArgument::ToString() const {
Devin Mooreeccdb902020-03-24 16:22:40 -0700745 if (direction_specified_) {
746 return GetDirectionSpecifier() + " " + AidlVariableDeclaration::ToString();
747 } else {
748 return AidlVariableDeclaration::ToString();
749 }
Jiyong Park02da7422018-07-16 16:00:26 +0900750}
751
Steven Moreland46e9da82018-07-27 15:45:29 -0700752AidlMember::AidlMember(const AidlLocation& location) : AidlNode(location) {}
753
Steven Moreland46e9da82018-07-27 15:45:29 -0700754AidlConstantDeclaration::AidlConstantDeclaration(const AidlLocation& location,
755 AidlTypeSpecifier* type, const std::string& name,
756 AidlConstantValue* value)
757 : AidlMember(location), type_(type), name_(name), value_(value) {}
Steven Moreland693640b2018-07-19 13:46:27 -0700758
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900759bool AidlConstantDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700760 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900761 valid &= type_->CheckValid(typenames);
Steven Moreland25294322018-08-07 18:13:55 -0700762 valid &= value_->CheckValid();
763 if (!valid) return false;
Steven Moreland693640b2018-07-19 13:46:27 -0700764
Steven Morelande689da22020-11-10 02:06:30 +0000765 const static set<string> kSupportedConstTypes = {"String", "byte", "int", "long"};
Jooyung Han965e31d2020-11-27 12:30:16 +0900766 if (kSupportedConstTypes.find(type_->Signature()) == kSupportedConstTypes.end()) {
767 AIDL_ERROR(this) << "Constant of type " << type_->Signature() << " is not supported.";
Steven Moreland693640b2018-07-19 13:46:27 -0700768 return false;
769 }
770
Will McVickerd7d18df2019-09-12 13:40:50 -0700771 return true;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700772}
773
Jiyong Parka428d212018-08-29 22:26:30 +0900774string AidlConstantDeclaration::ToString() const {
Jooyung Hanb3ca6302020-11-27 14:13:27 +0900775 return "const " + type_->ToString() + " " + name_ + " = " +
776 ValueString(AidlConstantValueDecorator);
Jiyong Parka428d212018-08-29 22:26:30 +0900777}
778
779string AidlConstantDeclaration::Signature() const {
780 return type_->Signature() + " " + name_;
781}
782
Steven Moreland46e9da82018-07-27 15:45:29 -0700783AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
784 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900785 const std::string& comments)
786 : AidlMethod(location, oneway, type, name, args, comments, 0, true) {
787 has_id_ = false;
788}
789
790AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
791 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900792 const std::string& comments, int id, bool is_user_defined)
Steven Moreland46e9da82018-07-27 15:45:29 -0700793 : AidlMember(location),
794 oneway_(oneway),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700795 comments_(comments),
796 type_(type),
797 name_(name),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700798 arguments_(std::move(*args)),
Jiyong Parkb034bf02018-07-30 17:44:33 +0900799 id_(id),
800 is_user_defined_(is_user_defined) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700801 has_id_ = true;
802 delete args;
Christopher Wileyad339272015-10-05 19:11:58 -0700803 for (const unique_ptr<AidlArgument>& a : arguments_) {
804 if (a->IsIn()) { in_arguments_.push_back(a.get()); }
805 if (a->IsOut()) { out_arguments_.push_back(a.get()); }
806 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700807}
808
Jeongik Cha997281d2020-01-16 15:23:59 +0900809bool AidlMethod::IsHidden() const {
810 return HasHideComment(GetComments());
811}
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700812
Jiyong Park02da7422018-07-16 16:00:26 +0900813string AidlMethod::Signature() const {
814 vector<string> arg_signatures;
815 for (const auto& arg : GetArguments()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900816 arg_signatures.emplace_back(arg->GetType().Signature());
Jiyong Park02da7422018-07-16 16:00:26 +0900817 }
Jiyong Park309668e2018-07-28 16:55:44 +0900818 return GetName() + "(" + Join(arg_signatures, ", ") + ")";
819}
820
821string AidlMethod::ToString() const {
822 vector<string> arg_strings;
823 for (const auto& arg : GetArguments()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900824 arg_strings.emplace_back(arg->ToString());
Jiyong Park309668e2018-07-28 16:55:44 +0900825 }
Jooyung Han965e31d2020-11-27 12:30:16 +0900826 string ret = (IsOneway() ? "oneway " : "") + GetType().ToString() + " " + GetName() + "(" +
Steven Moreland4ee68632018-12-14 15:52:46 -0800827 Join(arg_strings, ", ") + ")";
Jiyong Parked65bf42018-08-28 15:43:27 +0900828 if (HasId()) {
829 ret += " = " + std::to_string(GetId());
830 }
831 return ret;
Jiyong Park02da7422018-07-16 16:00:26 +0900832}
833
Steven Moreland46e9da82018-07-27 15:45:29 -0700834AidlDefinedType::AidlDefinedType(const AidlLocation& location, const std::string& name,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900835 const std::string& comments, const std::string& package,
836 std::vector<std::unique_ptr<AidlMember>>* members)
Jiyong Park18132182020-06-08 20:24:40 +0900837 : AidlAnnotatable(location),
838 name_(name),
839 comments_(comments),
840 package_(package),
841 split_package_(package.empty() ? std::vector<std::string>()
Jooyung Han829ec7c2020-12-02 12:07:36 +0900842 : android::base::Split(package, ".")) {
843 if (members) {
844 for (auto& m : *members) {
845 if (auto constant = m->AsConstantDeclaration(); constant) {
846 constants_.emplace_back(constant);
847 } else if (auto variable = m->AsVariableDeclaration(); variable) {
848 variables_.emplace_back(variable);
849 } else if (auto method = m->AsMethod(); method) {
850 methods_.emplace_back(method);
851 } else {
852 AIDL_FATAL(*m);
853 }
854 members_.push_back(m.release());
855 }
856 delete members;
857 }
858}
Steven Moreland787b0432018-07-03 09:00:58 -0700859
Jooyung Han888c5bc2020-12-22 17:28:47 +0900860bool AidlDefinedType::CheckValid(const AidlTypenames& typenames, DiagnosticsContext&) const {
Devin Moore24f68572020-02-26 13:20:59 -0800861 if (!AidlAnnotatable::CheckValid(typenames)) {
862 return false;
863 }
Jooyung Han829ec7c2020-12-02 12:07:36 +0900864 if (!CheckValidWithMembers(typenames)) {
865 return false;
866 }
Devin Moore24f68572020-02-26 13:20:59 -0800867 return true;
868}
869
Jeongik Cha997281d2020-01-16 15:23:59 +0900870bool AidlDefinedType::IsHidden() const {
871 return HasHideComment(GetComments());
872}
873
Steven Moreland787b0432018-07-03 09:00:58 -0700874std::string AidlDefinedType::GetCanonicalName() const {
875 if (package_.empty()) {
876 return GetName();
877 }
878 return GetPackage() + "." + GetName();
879}
880
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800881void AidlDefinedType::DumpHeader(CodeWriter* writer) const {
882 if (this->IsHidden()) {
883 AddHideComment(writer);
884 }
885 DumpAnnotations(writer);
886}
887
Jooyung Han829ec7c2020-12-02 12:07:36 +0900888bool AidlDefinedType::CheckValidWithMembers(const AidlTypenames& typenames) const {
889 bool success = true;
890
891 for (const auto& v : GetFields()) {
892 const bool field_valid = v->CheckValid(typenames);
893 success = success && field_valid;
894 }
895
896 // field names should be unique
897 std::set<std::string> fieldnames;
898 for (const auto& v : GetFields()) {
899 bool duplicated = !fieldnames.emplace(v->GetName()).second;
900 if (duplicated) {
901 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName() << "'";
902 success = false;
903 }
904 }
905
906 // immutable parcelables should have immutable fields.
907 if (IsJavaOnlyImmutable()) {
908 for (const auto& v : GetFields()) {
909 if (!typenames.CanBeJavaOnlyImmutable(v->GetType())) {
910 AIDL_ERROR(v) << "The @JavaOnlyImmutable '" << GetName() << "' has a "
911 << "non-immutable field named '" << v->GetName() << "'.";
912 success = false;
913 }
914 }
915 }
916
917 set<string> constant_names;
918 for (const auto& constant : GetConstantDeclarations()) {
919 if (constant_names.count(constant->GetName()) > 0) {
920 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
921 success = false;
922 }
923 constant_names.insert(constant->GetName());
924 success = success && constant->CheckValid(typenames);
925 }
926
927 return success;
928}
929
930bool AidlDefinedType::CheckValidForGetterNames() const {
931 bool success = true;
932 std::set<std::string> getters;
933 for (const auto& v : GetFields()) {
934 bool duplicated = !getters.emplace(v->GetCapitalizedName()).second;
935 if (duplicated) {
936 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName()
937 << "' after capitalizing the first letter";
938 success = false;
939 }
940 }
941 return success;
942}
943
Jiyong Park18132182020-06-08 20:24:40 +0900944AidlParcelable::AidlParcelable(const AidlLocation& location, const std::string& name,
945 const std::string& package, const std::string& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900946 const std::string& cpp_header, std::vector<std::string>* type_params,
947 std::vector<std::unique_ptr<AidlMember>>* members)
948 : AidlDefinedType(location, name, comments, package, members),
Jeongik Chadf76dc72019-11-28 00:08:47 +0900949 AidlParameterizable<std::string>(type_params),
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800950 cpp_header_(cpp_header) {
951 // Strip off quotation marks if we actually have a cpp header.
952 if (cpp_header_.length() >= 2) {
953 cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2);
954 }
Casey Dahlin59401da2015-10-09 18:16:45 -0700955}
Jeongik Chadf76dc72019-11-28 00:08:47 +0900956template <typename T>
957AidlParameterizable<T>::AidlParameterizable(const AidlParameterizable& other) {
958 // Copying is not supported if it has type parameters.
959 // It doesn't make a problem because only ArrayBase() makes a copy,
960 // and it can be called only if a type is not generic.
Steven Moreland21780812020-09-11 01:29:45 +0000961 AIDL_FATAL_IF(other.IsGeneric(), AIDL_LOCATION_HERE);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900962}
963
964template <typename T>
965bool AidlParameterizable<T>::CheckValid() const {
966 return true;
967};
968
969template <>
970bool AidlParameterizable<std::string>::CheckValid() const {
971 if (!IsGeneric()) {
972 return true;
973 }
974 std::unordered_set<std::string> set(GetTypeParameters().begin(), GetTypeParameters().end());
975 if (set.size() != GetTypeParameters().size()) {
976 AIDL_ERROR(this->AsAidlNode()) << "Every type parameter should be unique.";
977 return false;
978 }
979 return true;
980}
Casey Dahlin59401da2015-10-09 18:16:45 -0700981
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700982std::set<AidlAnnotation::Type> AidlParcelable::GetSupportedAnnotations() const {
Jeongik Cha36f76c32020-07-28 00:25:52 +0900983 return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900984 AidlAnnotation::Type::JAVA_STABLE_PARCELABLE, AidlAnnotation::Type::HIDE,
Jeongik Chad0a10272020-08-06 16:33:36 +0900985 AidlAnnotation::Type::JAVA_PASSTHROUGH, AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE};
Devin Moore24f68572020-02-26 13:20:59 -0800986}
987
Jooyung Han888c5bc2020-12-22 17:28:47 +0900988bool AidlParcelable::CheckValid(const AidlTypenames& typenames, DiagnosticsContext& diag) const {
989 if (!AidlDefinedType::CheckValid(typenames, diag)) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100990 return false;
991 }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900992 if (!AidlParameterizable<std::string>::CheckValid()) {
993 return false;
994 }
Jeongik Cha82317dd2019-02-27 20:26:11 +0900995
996 return true;
997}
998
Jeongik Cha997281d2020-01-16 15:23:59 +0900999void AidlParcelable::Dump(CodeWriter* writer) const {
Steven Morelanda5d9c5c2020-02-21 16:01:09 -08001000 DumpHeader(writer);
Jiyong Park02da7422018-07-16 16:00:26 +09001001 writer->Write("parcelable %s ;\n", GetName().c_str());
1002}
1003
Steven Moreland5557f1c2018-07-02 13:50:23 -07001004AidlStructuredParcelable::AidlStructuredParcelable(
Jiyong Park18132182020-06-08 20:24:40 +09001005 const AidlLocation& location, const std::string& name, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001006 const std::string& comments, std::vector<std::string>* type_params,
1007 std::vector<std::unique_ptr<AidlMember>>* members)
1008 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Steven Moreland5557f1c2018-07-02 13:50:23 -07001009
Jeongik Cha997281d2020-01-16 15:23:59 +09001010void AidlStructuredParcelable::Dump(CodeWriter* writer) const {
Steven Morelanda5d9c5c2020-02-21 16:01:09 -08001011 DumpHeader(writer);
Jiyong Park02da7422018-07-16 16:00:26 +09001012 writer->Write("parcelable %s {\n", GetName().c_str());
1013 writer->Indent();
1014 for (const auto& field : GetFields()) {
Jeongik Cha997281d2020-01-16 15:23:59 +09001015 if (field->GetType().IsHidden()) {
1016 AddHideComment(writer);
1017 }
Jiyong Parka468e2a2018-08-29 21:25:18 +09001018 writer->Write("%s;\n", field->ToString().c_str());
Jiyong Park02da7422018-07-16 16:00:26 +09001019 }
Jooyung Han3f347ca2020-12-01 12:41:50 +09001020 for (const auto& constdecl : GetConstantDeclarations()) {
1021 if (constdecl->GetType().IsHidden()) {
1022 AddHideComment(writer);
1023 }
1024 writer->Write("%s;\n", constdecl->ToString().c_str());
1025 }
Jiyong Park02da7422018-07-16 16:00:26 +09001026 writer->Dedent();
1027 writer->Write("}\n");
1028}
1029
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001030std::set<AidlAnnotation::Type> AidlStructuredParcelable::GetSupportedAnnotations() const {
Jeongik Cha36f76c32020-07-28 00:25:52 +09001031 return {AidlAnnotation::Type::VINTF_STABILITY,
1032 AidlAnnotation::Type::UNSUPPORTED_APP_USAGE,
1033 AidlAnnotation::Type::HIDE,
1034 AidlAnnotation::Type::JAVA_PASSTHROUGH,
Jooyung Han90345002020-10-23 15:28:53 +09001035 AidlAnnotation::Type::JAVA_DERIVE,
Devin Moorec7e47a32020-08-07 10:55:25 -07001036 AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE,
Andrei Homescue61feb52020-08-18 15:44:24 -07001037 AidlAnnotation::Type::FIXED_SIZE,
1038 AidlAnnotation::Type::RUST_DERIVE};
Devin Moore24f68572020-02-26 13:20:59 -08001039}
1040
Jooyung Han888c5bc2020-12-22 17:28:47 +09001041bool AidlStructuredParcelable::CheckValid(const AidlTypenames& typenames,
1042 DiagnosticsContext& diag) const {
1043 if (!AidlParcelable::CheckValid(typenames, diag)) {
Devin Moore24f68572020-02-26 13:20:59 -08001044 return false;
1045 }
Jeongik Cha13066da2020-08-06 15:43:19 +09001046
Jooyung Han59af9cc2020-10-25 21:44:14 +09001047 bool success = true;
Jeongik Cha36f76c32020-07-28 00:25:52 +09001048
Jooyung Hand4057c42020-10-23 13:28:22 +09001049 if (IsFixedSize()) {
1050 for (const auto& v : GetFields()) {
1051 if (!typenames.CanBeFixedSize(v->GetType())) {
1052 AIDL_ERROR(v) << "The @FixedSize parcelable '" << this->GetName() << "' has a "
1053 << "non-fixed size field named " << v->GetName() << ".";
1054 success = false;
1055 }
1056 }
1057 }
1058
1059 if (IsJavaOnlyImmutable()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001060 // Immutable parcelables provide getters
Jooyung Han829ec7c2020-12-02 12:07:36 +09001061 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001062 success = false;
Jooyung Hand4057c42020-10-23 13:28:22 +09001063 }
1064 }
1065
Daniel Norman85aed542019-08-21 12:01:14 -07001066 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001067}
1068
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001069// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -07001070bool AidlTypeSpecifier::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1071 Options::Language lang) const {
Andrei Homescub62afd92020-05-11 19:24:59 -07001072 if ((lang == Options::Language::NDK || lang == Options::Language::RUST) && IsArray() &&
1073 GetName() == "IBinder") {
1074 AIDL_ERROR(this) << "The " << Options::LanguageToString(lang)
1075 << " backend does not support array of IBinder";
Steven Moreland0185d9b2020-05-15 23:21:22 +00001076 return false;
1077 }
Jeongik Cha8f02a532020-10-14 00:16:28 +09001078 if (lang == Options::Language::RUST && GetName() == "ParcelableHolder") {
1079 // TODO(b/146611855): Remove it when Rust backend supports ParcelableHolder
1080 AIDL_ERROR(this) << "The Rust backend does not support ParcelableHolder yet.";
Jeongik Cha225519b2020-08-29 01:55:32 +09001081 return false;
1082 }
Andrei Homescub62afd92020-05-11 19:24:59 -07001083 if ((lang == Options::Language::NDK || lang == Options::Language::RUST) && IsArray() &&
1084 IsNullable()) {
Steven Moreland0185d9b2020-05-15 23:21:22 +00001085 if (GetName() == "ParcelFileDescriptor") {
Andrei Homescub62afd92020-05-11 19:24:59 -07001086 AIDL_ERROR(this) << "The " << Options::LanguageToString(lang)
1087 << " backend does not support nullable array of ParcelFileDescriptor";
Steven Moreland0185d9b2020-05-15 23:21:22 +00001088 return false;
1089 }
1090
Steven Morelandd59e3172020-05-11 16:42:09 -07001091 const auto defined_type = typenames.TryGetDefinedType(GetName());
1092 if (defined_type != nullptr && defined_type->AsParcelable() != nullptr) {
Andrei Homescub62afd92020-05-11 19:24:59 -07001093 AIDL_ERROR(this) << "The " << Options::LanguageToString(lang)
1094 << " backend does not support nullable array of parcelable";
Steven Morelandd59e3172020-05-11 16:42:09 -07001095 return false;
1096 }
1097 }
Andrei Homescub62afd92020-05-11 19:24:59 -07001098 if (this->GetName() == "FileDescriptor" &&
1099 (lang == Options::Language::NDK || lang == Options::Language::RUST)) {
1100 AIDL_ERROR(this) << "FileDescriptor isn't supported by the " << Options::LanguageToString(lang)
1101 << " backend.";
Steven Morelandc8a4ca82020-01-21 17:50:08 -08001102 return false;
1103 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001104 if (this->IsGeneric()) {
1105 if (this->GetName() == "List") {
Jooyung Han55f96ad2020-12-13 10:08:33 +09001106 if (lang == Options::Language::NDK) {
1107 const AidlTypeSpecifier& contained_type = *GetTypeParameters()[0];
1108 const string& contained_type_name = contained_type.GetName();
Jooyung Hane87cdd02020-12-11 16:47:35 +09001109 if (typenames.GetInterface(contained_type)) {
1110 AIDL_ERROR(this) << "List<" << contained_type_name
1111 << "> is not supported. List in NDK doesn't support interface.";
1112 return false;
1113 }
1114 if (contained_type_name == "IBinder") {
1115 AIDL_ERROR(this) << "List<" << contained_type_name
1116 << "> is not supported. List in NDK doesn't support IBinder.";
1117 return false;
Jeongik Cha08ca2182019-11-21 14:01:13 +09001118 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001119 }
Jeongik Chabb55b5e2020-01-07 23:11:26 +09001120 }
1121 }
Devin Moore6a01ca12020-08-28 10:24:19 -07001122
1123 if (this->IsArray()) {
1124 if (this->GetName() == "List" || this->GetName() == "Map" ||
1125 this->GetName() == "CharSequence") {
1126 AIDL_ERROR(this) << this->GetName() << "[] is not supported.";
Jeongik Chabb55b5e2020-01-07 23:11:26 +09001127 return false;
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001128 }
1129 }
Devin Moore6a01ca12020-08-28 10:24:19 -07001130
1131 if (lang != Options::Language::JAVA) {
1132 if (this->GetName() == "List" && !this->IsGeneric()) {
1133 AIDL_ERROR(this) << "Currently, only the Java backend supports non-generic List.";
1134 return false;
1135 }
1136 if (this->GetName() == "Map" || this->GetName() == "CharSequence") {
1137 AIDL_ERROR(this) << "Currently, only Java backend supports " << this->GetName() << ".";
1138 return false;
Jeongik Cha08ca2182019-11-21 14:01:13 +09001139 }
1140 }
1141
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001142 return true;
1143}
1144
1145// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -07001146bool AidlParcelable::LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/,
1147 Options::Language lang) const {
Andrei Homescub62afd92020-05-11 19:24:59 -07001148 if (lang == Options::Language::CPP || lang == Options::Language::NDK) {
Steven Moreland0d9c26e2020-01-22 08:52:08 -08001149 const AidlParcelable* unstructured_parcelable = this->AsUnstructuredParcelable();
1150 if (unstructured_parcelable != nullptr) {
1151 if (unstructured_parcelable->GetCppHeader().empty()) {
1152 AIDL_ERROR(unstructured_parcelable)
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001153 << "Unstructured parcelable must have C++ header defined.";
1154 return false;
1155 }
1156 }
1157 }
1158 return true;
1159}
1160
1161// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -07001162bool AidlStructuredParcelable::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1163 Options::Language lang) const {
1164 if (!AidlParcelable::LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001165 return false;
1166 }
1167 for (const auto& v : this->GetFields()) {
Steven Morelandd59e3172020-05-11 16:42:09 -07001168 if (!v->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001169 return false;
1170 }
1171 }
1172 return true;
1173}
1174
Daniel Norman85aed542019-08-21 12:01:14 -07001175AidlEnumerator::AidlEnumerator(const AidlLocation& location, const std::string& name,
Daniel Norman2e4112d2019-10-03 10:22:35 -07001176 AidlConstantValue* value, const std::string& comments)
Jooyung Han29813842020-12-08 01:28:03 +09001177 : AidlNode(location),
1178 name_(name),
1179 value_(value),
1180 comments_(comments),
1181 value_user_specified_(value != nullptr) {}
Daniel Norman85aed542019-08-21 12:01:14 -07001182
1183bool AidlEnumerator::CheckValid(const AidlTypeSpecifier& enum_backing_type) const {
1184 if (GetValue() == nullptr) {
1185 return false;
1186 }
1187 if (!GetValue()->CheckValid()) {
1188 return false;
1189 }
Will McVickerd7d18df2019-09-12 13:40:50 -07001190 if (GetValue()->ValueString(enum_backing_type, AidlConstantValueDecorator).empty()) {
Daniel Norman85aed542019-08-21 12:01:14 -07001191 AIDL_ERROR(this) << "Enumerator type differs from enum backing type.";
1192 return false;
1193 }
1194 return true;
1195}
1196
1197string AidlEnumerator::ValueString(const AidlTypeSpecifier& backing_type,
1198 const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -07001199 return GetValue()->ValueString(backing_type, decorator);
Daniel Norman85aed542019-08-21 12:01:14 -07001200}
1201
1202AidlEnumDeclaration::AidlEnumDeclaration(const AidlLocation& location, const std::string& name,
1203 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jiyong Park18132182020-06-08 20:24:40 +09001204 const std::string& package, const std::string& comments)
Jooyung Han829ec7c2020-12-02 12:07:36 +09001205 : AidlDefinedType(location, name, comments, package, nullptr),
Jooyung Han29813842020-12-08 01:28:03 +09001206 enumerators_(std::move(*enumerators)) {
Jooyung Han672557b2020-12-24 05:18:00 +09001207 // Fill missing enumerator values with <prev + 1>
1208 // This can't be done in Autofill() because type/ref resolution depends on this.
1209 // For example, with enum E { A, B = A }, B's value 'A' is a reference which can't be
1210 // resolved if A has no value set.
Daniel Normanb28684e2019-10-17 15:31:39 -07001211 const AidlEnumerator* previous = nullptr;
1212 for (const auto& enumerator : enumerators_) {
1213 if (enumerator->GetValue() == nullptr) {
Jooyung Han29813842020-12-08 01:28:03 +09001214 auto loc = enumerator->GetLocation();
Daniel Normanb28684e2019-10-17 15:31:39 -07001215 if (previous == nullptr) {
Devin Mooredf93ebb2020-03-25 14:03:35 -07001216 enumerator->SetValue(
Jooyung Han29813842020-12-08 01:28:03 +09001217 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "0")));
Daniel Normanb28684e2019-10-17 15:31:39 -07001218 } else {
Jooyung Han29813842020-12-08 01:28:03 +09001219 auto prev_value = std::make_unique<AidlConstantReference>(loc, previous->GetName(), "");
Daniel Normanb28684e2019-10-17 15:31:39 -07001220 enumerator->SetValue(std::make_unique<AidlBinaryConstExpression>(
Jooyung Han29813842020-12-08 01:28:03 +09001221 loc, std::move(prev_value), "+",
1222 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "1"))));
Daniel Normanb28684e2019-10-17 15:31:39 -07001223 }
1224 }
1225 previous = enumerator.get();
1226 }
1227}
1228
Jooyung Han672557b2020-12-24 05:18:00 +09001229bool AidlEnumDeclaration::Autofill(const AidlTypenames& typenames) {
1230 if (auto annot = BackingType(); annot != nullptr) {
1231 // Autofill() is called before the grand CheckValid(). But AidlAnnotation::AnnotationParams()
1232 // calls AidlConstantValue::ValueString() which requires CheckValid() to be called before. So we
1233 // need to call CheckValid().
1234 if (!annot->CheckValid()) {
1235 return false;
1236 }
1237 auto annotation_params = annot->AnnotationParams(AidlConstantValueDecorator);
1238 auto type = annotation_params.at("type");
1239
1240 AIDL_FATAL_IF(type.size() < 2, this) << type;
1241 AIDL_FATAL_IF(type[0] != '"', this) << type;
1242 AIDL_FATAL_IF(type[type.length() - 1] != '"', this) << type;
1243 string unquoted_type = type.substr(1, type.length() - 2);
1244
1245 backing_type_ = std::make_unique<AidlTypeSpecifier>(annot->GetLocation(), unquoted_type, false,
1246 nullptr, "");
1247 } else {
1248 // Default to byte type for enums.
1249 backing_type_ =
1250 std::make_unique<AidlTypeSpecifier>(AIDL_LOCATION_HERE, "byte", false, nullptr, "");
1251 }
1252 // Autofill() is called after type resolution, we resolve the backing type manually.
1253 if (!backing_type_->Resolve(typenames)) {
1254 AIDL_ERROR(this) << "Invalid backing type: " << backing_type_->GetName();
1255 }
1256 return true;
1257}
1258
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001259std::set<AidlAnnotation::Type> AidlEnumDeclaration::GetSupportedAnnotations() const {
1260 return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::BACKING,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +09001261 AidlAnnotation::Type::HIDE, AidlAnnotation::Type::JAVA_PASSTHROUGH};
Devin Moore24f68572020-02-26 13:20:59 -08001262}
1263
Jooyung Han888c5bc2020-12-22 17:28:47 +09001264bool AidlEnumDeclaration::CheckValid(const AidlTypenames& typenames,
1265 DiagnosticsContext& diag) const {
1266 if (!AidlDefinedType::CheckValid(typenames, diag)) {
Devin Moore24f68572020-02-26 13:20:59 -08001267 return false;
1268 }
Jooyung Han829ec7c2020-12-02 12:07:36 +09001269 if (!GetMembers().empty()) {
1270 AIDL_ERROR(this) << "Enum doesn't support fields/constants/methods.";
1271 return false;
1272 }
Daniel Norman85aed542019-08-21 12:01:14 -07001273 if (backing_type_ == nullptr) {
1274 AIDL_ERROR(this) << "Enum declaration missing backing type.";
1275 return false;
1276 }
1277 bool success = true;
1278 for (const auto& enumerator : enumerators_) {
1279 success = success && enumerator->CheckValid(GetBackingType());
1280 }
Jooyung Han3b990182020-12-22 17:44:31 +09001281
Steven Moreland26318532020-12-23 20:08:36 +00001282 if (!success) return false; // ValueString requires valid type
1283
Jooyung Han3b990182020-12-22 17:44:31 +09001284 AIDL_FATAL_IF(GetEnumerators().empty(), this)
1285 << "The enum '" << GetName() << "' has no enumerators.";
1286
1287 const auto& first = GetEnumerators()[0];
1288 if (auto first_value = first->ValueString(GetBackingType(), AidlConstantValueDecorator);
1289 first_value != "0") {
1290 diag.Report(first->GetLocation(), DiagnosticID::enum_zero)
1291 << "The first enumerator '" << first->GetName() << "' should be 0, but it is "
1292 << first_value << ".";
1293 }
1294
Steven Moreland26318532020-12-23 20:08:36 +00001295 return true;
Daniel Norman85aed542019-08-21 12:01:14 -07001296}
1297
Jeongik Cha997281d2020-01-16 15:23:59 +09001298void AidlEnumDeclaration::Dump(CodeWriter* writer) const {
Steven Morelanda5d9c5c2020-02-21 16:01:09 -08001299 DumpHeader(writer);
Daniel Norman37d43dd2019-09-09 17:22:34 -07001300 writer->Write("enum %s {\n", GetName().c_str());
Daniel Norman85aed542019-08-21 12:01:14 -07001301 writer->Indent();
1302 for (const auto& enumerator : GetEnumerators()) {
Daniel Norman85aed542019-08-21 12:01:14 -07001303 writer->Write("%s = %s,\n", enumerator->GetName().c_str(),
1304 enumerator->ValueString(GetBackingType(), AidlConstantValueDecorator).c_str());
1305 }
1306 writer->Dedent();
1307 writer->Write("}\n");
1308}
1309
Jooyung Han2946afc2020-10-05 20:29:16 +09001310AidlUnionDecl::AidlUnionDecl(const AidlLocation& location, const std::string& name,
1311 const std::string& package, const std::string& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001312 std::vector<std::string>* type_params,
1313 std::vector<std::unique_ptr<AidlMember>>* members)
1314 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Jooyung Han2946afc2020-10-05 20:29:16 +09001315
1316std::set<AidlAnnotation::Type> AidlUnionDecl::GetSupportedAnnotations() const {
Jooyung Hanf93b5bd2020-10-28 15:12:08 +09001317 return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::HIDE,
1318 AidlAnnotation::Type::JAVA_PASSTHROUGH, AidlAnnotation::Type::JAVA_DERIVE,
1319 AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE, AidlAnnotation::Type::RUST_DERIVE};
Jooyung Han2946afc2020-10-05 20:29:16 +09001320}
1321
1322void AidlUnionDecl::Dump(CodeWriter* writer) const {
1323 DumpHeader(writer);
1324 writer->Write("union %s {\n", GetName().c_str());
1325 writer->Indent();
1326 for (const auto& field : GetFields()) {
1327 if (field->GetType().IsHidden()) {
1328 AddHideComment(writer);
1329 }
1330 writer->Write("%s;\n", field->ToString().c_str());
1331 }
Jooyung Han3f347ca2020-12-01 12:41:50 +09001332 for (const auto& constdecl : GetConstantDeclarations()) {
1333 if (constdecl->GetType().IsHidden()) {
1334 AddHideComment(writer);
1335 }
1336 writer->Write("%s;\n", constdecl->ToString().c_str());
1337 }
Jooyung Han2946afc2020-10-05 20:29:16 +09001338 writer->Dedent();
1339 writer->Write("}\n");
1340}
1341
Jooyung Han888c5bc2020-12-22 17:28:47 +09001342bool AidlUnionDecl::CheckValid(const AidlTypenames& typenames, DiagnosticsContext& diag) const {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001343 // visit parents
Jooyung Han888c5bc2020-12-22 17:28:47 +09001344 if (!AidlParcelable::CheckValid(typenames, diag)) {
Jooyung Hanfe89f122020-10-14 03:49:18 +09001345 return false;
1346 }
Jooyung Han59af9cc2020-10-25 21:44:14 +09001347
1348 // unions provide getters always
Jooyung Han829ec7c2020-12-02 12:07:36 +09001349 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001350 return false;
Jooyung Hanfe89f122020-10-14 03:49:18 +09001351 }
1352
1353 // now, visit self!
1354 bool success = true;
1355
1356 // TODO(b/170807936) do we need to allow ParcelableHolder in union?
1357 for (const auto& v : GetFields()) {
1358 if (v->GetType().GetName() == "ParcelableHolder") {
1359 AIDL_ERROR(*v) << "A union can't have a member of ParcelableHolder '" << v->GetName() << "'";
1360 success = false;
1361 }
1362 }
1363
Jooyung Hanfe89f122020-10-14 03:49:18 +09001364 if (GetFields().empty()) {
1365 AIDL_ERROR(*this) << "The union '" << this->GetName() << "' has no fields.";
1366 return false;
1367 }
1368
Jooyung Han53fb4242020-12-17 16:03:49 +09001369 // first member should have useful default value (implicit or explicit)
1370 const auto& first = GetFields()[0];
1371 if (!first->HasUsefulDefaultValue()) {
1372 // Most types can be initialized without a default value. For example,
1373 // interface types are inherently nullable. But, enum types should have
1374 // an explicit default value.
1375 if (!first->GetType().IsArray() && typenames.GetEnumDeclaration(first->GetType())) {
1376 AIDL_ERROR(first)
1377 << "The union's first member should have a useful default value. Enum types can be "
1378 "initialized with a reference. (e.g. ... = MyEnum.FOO;)";
1379 return false;
1380 }
1381 // In Java, array types are initialized as null without a default value. To be sure that default
1382 // initialized unions are accepted by other backends we require arrays also have a default
1383 // value.
1384 if (first->GetType().IsArray()) {
1385 AIDL_ERROR(first)
1386 << "The union's first member should have a useful default value. Arrays can be "
1387 "initialized with values(e.g. ... = { values... };) or marked as @nullable.";
1388 return false;
1389 }
1390 }
1391
Jooyung Hanfe89f122020-10-14 03:49:18 +09001392 return success;
1393}
1394
1395// TODO: we should treat every backend all the same in future.
1396bool AidlUnionDecl::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1397 Options::Language lang) const {
1398 if (!AidlParcelable::LanguageSpecificCheckValid(typenames, lang)) {
1399 return false;
1400 }
1401 for (const auto& v : this->GetFields()) {
1402 if (!v->GetType().LanguageSpecificCheckValid(typenames, lang)) {
1403 return false;
1404 }
1405 }
1406 return true;
1407}
1408
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001409// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -07001410bool AidlInterface::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1411 Options::Language lang) const {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001412 for (const auto& m : this->GetMethods()) {
Steven Morelandd59e3172020-05-11 16:42:09 -07001413 if (!m->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001414 return false;
1415 }
1416 for (const auto& arg : m->GetArguments()) {
Steven Morelandd59e3172020-05-11 16:42:09 -07001417 if (!arg->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001418 return false;
1419 }
1420 }
1421 }
1422 return true;
1423}
1424
Steven Moreland46e9da82018-07-27 15:45:29 -07001425AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001426 const std::string& comments, bool oneway, const std::string& package,
1427 std::vector<std::unique_ptr<AidlMember>>* members)
1428 : AidlDefinedType(location, name, comments, package, members) {
1429 for (auto& m : GetMethods()) {
1430 m.get()->ApplyInterfaceOneway(oneway);
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001431 }
Casey Dahlinfb7da2e2015-10-08 17:26:09 -07001432}
1433
Jeongik Cha997281d2020-01-16 15:23:59 +09001434void AidlInterface::Dump(CodeWriter* writer) const {
Steven Morelanda5d9c5c2020-02-21 16:01:09 -08001435 DumpHeader(writer);
Jiyong Park02da7422018-07-16 16:00:26 +09001436 writer->Write("interface %s {\n", GetName().c_str());
1437 writer->Indent();
1438 for (const auto& method : GetMethods()) {
Jeongik Cha997281d2020-01-16 15:23:59 +09001439 if (method->IsHidden()) {
1440 AddHideComment(writer);
1441 }
Jiyong Park309668e2018-07-28 16:55:44 +09001442 writer->Write("%s;\n", method->ToString().c_str());
Jiyong Park02da7422018-07-16 16:00:26 +09001443 }
Jiyong Parka428d212018-08-29 22:26:30 +09001444 for (const auto& constdecl : GetConstantDeclarations()) {
Jeongik Cha997281d2020-01-16 15:23:59 +09001445 if (constdecl->GetType().IsHidden()) {
1446 AddHideComment(writer);
1447 }
Jiyong Parka428d212018-08-29 22:26:30 +09001448 writer->Write("%s;\n", constdecl->ToString().c_str());
1449 }
Jiyong Park02da7422018-07-16 16:00:26 +09001450 writer->Dedent();
1451 writer->Write("}\n");
1452}
1453
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001454std::set<AidlAnnotation::Type> AidlInterface::GetSupportedAnnotations() const {
Steven Morelanda7764e52020-10-27 17:29:29 +00001455 return {AidlAnnotation::Type::SENSITIVE_DATA, AidlAnnotation::Type::VINTF_STABILITY,
1456 AidlAnnotation::Type::UNSUPPORTED_APP_USAGE, AidlAnnotation::Type::HIDE,
1457 AidlAnnotation::Type::JAVA_PASSTHROUGH, AidlAnnotation::Type::DESCRIPTOR};
Devin Moore24f68572020-02-26 13:20:59 -08001458}
1459
Jooyung Han888c5bc2020-12-22 17:28:47 +09001460bool AidlInterface::CheckValid(const AidlTypenames& typenames, DiagnosticsContext& diag) const {
1461 if (!AidlDefinedType::CheckValid(typenames, diag)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001462 return false;
1463 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001464 // Has to be a pointer due to deleting copy constructor. No idea why.
1465 map<string, const AidlMethod*> method_names;
1466 for (const auto& m : GetMethods()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001467 if (!m->GetType().CheckValid(typenames)) {
1468 return false;
1469 }
1470
Jeongik Cha649e8a72020-03-27 17:47:40 +09001471 // TODO(b/156872582): Support it when ParcelableHolder supports every backend.
1472 if (m->GetType().GetName() == "ParcelableHolder") {
1473 AIDL_ERROR(m) << "ParcelableHolder cannot be a return type";
1474 return false;
1475 }
Steven Morelandacd53472018-12-14 10:17:26 -08001476 if (m->IsOneway() && m->GetType().GetName() != "void") {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001477 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot return a value";
1478 return false;
1479 }
1480
1481 set<string> argument_names;
1482 for (const auto& arg : m->GetArguments()) {
1483 auto it = argument_names.find(arg->GetName());
1484 if (it != argument_names.end()) {
1485 AIDL_ERROR(m) << "method '" << m->GetName() << "' has duplicate argument name '"
1486 << arg->GetName() << "'";
1487 return false;
1488 }
1489 argument_names.insert(arg->GetName());
1490
1491 if (!arg->GetType().CheckValid(typenames)) {
1492 return false;
1493 }
1494
Jeongik Cha649e8a72020-03-27 17:47:40 +09001495 // TODO(b/156872582): Support it when ParcelableHolder supports every backend.
1496 if (arg->GetType().GetName() == "ParcelableHolder") {
1497 AIDL_ERROR(arg) << "ParcelableHolder cannot be an argument type";
1498 return false;
1499 }
Steven Morelandacd53472018-12-14 10:17:26 -08001500 if (m->IsOneway() && arg->IsOut()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001501 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot have out parameters";
1502 return false;
1503 }
Jooyung Han15fd6c62020-10-23 13:54:46 +09001504
1505 const auto [can_be_out, type_aspect] = typenames.CanBeOutParameter(arg->GetType());
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001506 if (!arg->DirectionWasSpecified() && can_be_out) {
Jooyung Han965e31d2020-11-27 12:30:16 +09001507 AIDL_ERROR(arg) << "'" << arg->GetType().Signature()
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001508 << "' can be an out type, so you must declare it as in, out, or inout.";
1509 return false;
1510 }
1511
1512 if (arg->GetDirection() != AidlArgument::IN_DIR && !can_be_out) {
Jooyung Han15fd6c62020-10-23 13:54:46 +09001513 AIDL_ERROR(arg) << "'" << arg->GetName() << "' can't be an " << arg->GetDirectionSpecifier()
1514 << " parameter because " << type_aspect << " can only be an in parameter.";
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001515 return false;
1516 }
1517
1518 // check that the name doesn't match a keyword
Jeongik Cha997281d2020-01-16 15:23:59 +09001519 if (IsJavaKeyword(arg->GetName().c_str())) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001520 AIDL_ERROR(arg) << "Argument name is a Java or aidl keyword";
1521 return false;
1522 }
1523
1524 // Reserve a namespace for internal use
1525 if (android::base::StartsWith(arg->GetName(), "_aidl")) {
1526 AIDL_ERROR(arg) << "Argument name cannot begin with '_aidl'";
1527 return false;
1528 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001529 }
1530
1531 auto it = method_names.find(m->GetName());
1532 // prevent duplicate methods
1533 if (it == method_names.end()) {
1534 method_names[m->GetName()] = m.get();
1535 } else {
1536 AIDL_ERROR(m) << "attempt to redefine method " << m->GetName() << ":";
1537 AIDL_ERROR(it->second) << "previously defined here.";
1538 return false;
1539 }
1540
Paul Trautrimb77048c2020-01-21 16:39:32 +09001541 static set<string> reserved_methods{"asBinder()", "getInterfaceHash()", "getInterfaceVersion()",
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001542 "getTransactionName(int)"};
1543
1544 if (reserved_methods.find(m->Signature()) != reserved_methods.end()) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001545 AIDL_ERROR(m) << " method " << m->Signature() << " is reserved for internal use.";
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001546 return false;
1547 }
1548 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001549
1550 bool success = true;
1551 set<string> constant_names;
Jooyung Han3f347ca2020-12-01 12:41:50 +09001552 for (const auto& constant : GetConstantDeclarations()) {
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001553 if (constant_names.count(constant->GetName()) > 0) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001554 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001555 success = false;
1556 }
1557 constant_names.insert(constant->GetName());
1558 success = success && constant->CheckValid(typenames);
1559 }
1560
Jooyung Han888c5bc2020-12-22 17:28:47 +09001561 if (auto name = GetName(); name.size() < 1 || name[0] != 'I') {
1562 diag.Report(GetLocation(), DiagnosticID::interface_name)
1563 << "Interface names should start with I.";
1564 }
1565
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001566 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001567}
1568
Jiyong Park27fd7fd2020-08-27 16:25:09 +09001569std::string AidlInterface::GetDescriptor() const {
1570 std::string annotatedDescriptor = AidlAnnotatable::GetDescriptor();
1571 if (annotatedDescriptor != "") {
1572 return annotatedDescriptor;
1573 }
1574 return GetCanonicalName();
1575}
1576
Steven Moreland46e9da82018-07-27 15:45:29 -07001577AidlImport::AidlImport(const AidlLocation& location, const std::string& needed_class)
1578 : AidlNode(location), needed_class_(needed_class) {}
Jooyung Han29813842020-12-08 01:28:03 +09001579
1580// Resolves unresolved type name to fully qualified typename to import
1581// case #1: SimpleName --> import p.SimpleName
1582// case #2: Outer.Inner --> import p.Outer
1583// case #3: p.SimpleName --> (as is)
1584std::optional<std::string> AidlDocument::ResolveName(const std::string& unresolved_name) const {
1585 std::string canonical_name;
1586 const auto first_dot = unresolved_name.find_first_of('.');
1587 const std::string class_name =
1588 (first_dot == std::string::npos) ? unresolved_name : unresolved_name.substr(0, first_dot);
1589 for (const auto& import : Imports()) {
1590 const auto& fq_name = import->GetNeededClass();
1591 const auto last_dot = fq_name.find_last_of('.');
1592 const std::string imported_type_name =
1593 (last_dot == std::string::npos) ? fq_name : fq_name.substr(last_dot + 1);
1594 if (imported_type_name == class_name) {
1595 if (canonical_name != "" && canonical_name != fq_name) {
1596 AIDL_ERROR(import) << "Ambiguous type: " << canonical_name << " vs. " << fq_name;
1597 return {};
1598 }
1599 canonical_name = fq_name;
1600 }
1601 }
1602 // if not found, use unresolved_name as it is
1603 if (canonical_name == "") {
1604 return unresolved_name;
1605 }
1606 return canonical_name;
Steven Moreland26318532020-12-23 20:08:36 +00001607}