blob: ea6f5c2fee68289d63bfc364f324e0adecc8ebf5 [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},
128 {AidlAnnotation::Type::BACKING, "Backing", {{"type", "String"}}, false},
129 {AidlAnnotation::Type::JAVA_PASSTHROUGH, "JavaPassthrough", {{"annotation", "String"}}, true},
Jiyong Park9aa3d042020-12-04 23:30:02 +0900130 {AidlAnnotation::Type::JAVA_DERIVE,
131 "JavaDerive",
132 {{"toString", "boolean"},
133 {"equals", "boolean"}},
134 false},
Jooyung Hand902a972020-10-23 17:32:44 +0900135 {AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE, "JavaOnlyImmutable", {}, false},
136 {AidlAnnotation::Type::FIXED_SIZE, "FixedSize", {}, false},
137 {AidlAnnotation::Type::DESCRIPTOR, "Descriptor", {{"value", "String"}}, false},
Andrei Homescue61feb52020-08-18 15:44:24 -0700138 {AidlAnnotation::Type::RUST_DERIVE,
139 "RustDerive",
140 {{"Copy", "boolean"},
141 {"Clone", "boolean"},
142 {"PartialOrd", "boolean"},
143 {"Ord", "boolean"},
144 {"PartialEq", "boolean"},
145 {"Eq", "boolean"},
Jooyung Hand902a972020-10-23 17:32:44 +0900146 {"Hash", "boolean"}},
147 false},
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900148 };
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700149 return kSchemas;
150}
Jiyong Park68bc77a2018-07-19 19:00:45 +0900151
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700152std::string AidlAnnotation::TypeToString(Type type) {
153 for (const Schema& schema : AllSchemas()) {
154 if (type == schema.type) return schema.name;
155 }
156 AIDL_FATAL(AIDL_LOCATION_HERE) << "Unrecognized type: " << static_cast<size_t>(type);
157 __builtin_unreachable();
158}
Andrei Onea9445fc62019-06-27 18:11:59 +0100159
160AidlAnnotation* AidlAnnotation::Parse(
161 const AidlLocation& location, const string& name,
162 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700163 const Schema* schema = nullptr;
164 for (const Schema& a_schema : AllSchemas()) {
165 if (a_schema.name == name) {
166 schema = &a_schema;
167 }
168 }
169
170 if (schema == nullptr) {
Jiyong Park68bc77a2018-07-19 19:00:45 +0900171 std::ostringstream stream;
Steven Moreland46e9da82018-07-27 15:45:29 -0700172 stream << "'" << name << "' is not a recognized annotation. ";
Jiyong Park68bc77a2018-07-19 19:00:45 +0900173 stream << "It must be one of:";
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700174 for (const Schema& s : AllSchemas()) {
175 stream << " " << s.name;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900176 }
177 stream << ".";
Steven Moreland46e9da82018-07-27 15:45:29 -0700178 AIDL_ERROR(location) << stream.str();
179 return nullptr;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900180 }
Andrei Onea9445fc62019-06-27 18:11:59 +0100181 if (parameter_list == nullptr) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700182 return new AidlAnnotation(location, *schema, {});
Andrei Onea9445fc62019-06-27 18:11:59 +0100183 }
184
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700185 return new AidlAnnotation(location, *schema, std::move(*parameter_list));
Jiyong Park68bc77a2018-07-19 19:00:45 +0900186}
187
Andrei Onea9445fc62019-06-27 18:11:59 +0100188AidlAnnotation::AidlAnnotation(
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700189 const AidlLocation& location, const Schema& schema,
Andrei Onea9445fc62019-06-27 18:11:59 +0100190 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters)
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700191 : AidlNode(location), schema_(schema), parameters_(std::move(parameters)) {}
Andrei Onea9445fc62019-06-27 18:11:59 +0100192
Jooyung Han690f5842020-12-04 13:02:04 +0900193struct ConstReferenceFinder : AidlConstantValue::Visitor {
194 AidlConstantReference* found;
195 void Visit(AidlConstantValue&) override {}
196 void Visit(AidlUnaryConstExpression&) override {}
197 void Visit(AidlBinaryConstExpression&) override {}
198 void Visit(AidlConstantReference& ref) override {
199 if (!found) found = &ref;
200 }
201};
202
Andrei Onea9445fc62019-06-27 18:11:59 +0100203bool AidlAnnotation::CheckValid() const {
Andrei Onea9445fc62019-06-27 18:11:59 +0100204 for (const auto& name_and_param : parameters_) {
205 const std::string& param_name = name_and_param.first;
206 const std::shared_ptr<AidlConstantValue>& param = name_and_param.second;
Jooyung Han690f5842020-12-04 13:02:04 +0900207
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700208 auto parameter_mapping_it = schema_.supported_parameters.find(param_name);
209 if (parameter_mapping_it == schema_.supported_parameters.end()) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100210 std::ostringstream stream;
211 stream << "Parameter " << param_name << " not supported ";
Devin Mooredecaf292020-04-30 09:16:40 -0700212 stream << "for annotation " << GetName() << ". ";
Andrei Onea9445fc62019-06-27 18:11:59 +0100213 stream << "It must be one of:";
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700214 for (const auto& kv : schema_.supported_parameters) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100215 stream << " " << kv.first;
216 }
217 AIDL_ERROR(this) << stream.str();
218 return false;
219 }
Jooyung Han690f5842020-12-04 13:02:04 +0900220
221 ConstReferenceFinder finder;
222 param->Accept(finder);
223 if (finder.found) {
224 AIDL_ERROR(finder.found) << "Value must be a constant expression but contains reference to "
225 << finder.found->GetFieldName() << ".";
226 return false;
227 }
228
229 if (!param->CheckValid()) {
230 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
231 << GetName() << ".";
232 return false;
233 }
234
Andrei Onea9445fc62019-06-27 18:11:59 +0100235 AidlTypeSpecifier type{AIDL_LOCATION_HERE, parameter_mapping_it->second, false, nullptr, ""};
Will McVickerd7d18df2019-09-12 13:40:50 -0700236 const std::string param_value = param->ValueString(type, AidlConstantValueDecorator);
Andrei Onea9445fc62019-06-27 18:11:59 +0100237 // Assume error on empty string.
238 if (param_value == "") {
239 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
240 << GetName() << ".";
241 return false;
242 }
243 }
244 return true;
245}
246
247std::map<std::string, std::string> AidlAnnotation::AnnotationParams(
248 const ConstantValueDecorator& decorator) const {
249 std::map<std::string, std::string> raw_params;
Andrei Onea9445fc62019-06-27 18:11:59 +0100250 for (const auto& name_and_param : parameters_) {
251 const std::string& param_name = name_and_param.first;
252 const std::shared_ptr<AidlConstantValue>& param = name_and_param.second;
Devin Mooredecaf292020-04-30 09:16:40 -0700253 if (schema_.supported_parameters.find(param_name) == schema_.supported_parameters.end()) {
254 std::ostringstream stream;
255 stream << "Parameter " << param_name << " not supported ";
256 stream << "for annotation " << GetName() << ". ";
257 stream << "It must be one of:";
258 for (const auto& kv : schema_.supported_parameters) {
259 stream << " " << kv.first;
260 }
261 AIDL_ERROR(this) << stream.str();
262 continue;
263 }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700264 AidlTypeSpecifier type{AIDL_LOCATION_HERE, schema_.supported_parameters.at(param_name), false,
265 nullptr, ""};
Will McVickerd7d18df2019-09-12 13:40:50 -0700266 if (!param->CheckValid()) {
267 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
268 << GetName() << ".";
Devin Mooredecaf292020-04-30 09:16:40 -0700269 continue;
Will McVickerd7d18df2019-09-12 13:40:50 -0700270 }
271
272 raw_params.emplace(param_name, param->ValueString(type, decorator));
Andrei Onea9445fc62019-06-27 18:11:59 +0100273 }
274 return raw_params;
275}
Steven Moreland46e9da82018-07-27 15:45:29 -0700276
Jooyung Han965e31d2020-11-27 12:30:16 +0900277std::string AidlAnnotation::ToString() const {
Daniel Norman37d43dd2019-09-09 17:22:34 -0700278 if (parameters_.empty()) {
279 return "@" + GetName();
280 } else {
281 vector<string> param_strings;
Jooyung Han965e31d2020-11-27 12:30:16 +0900282 for (const auto& [name, value] : AnnotationParams(AidlConstantValueDecorator)) {
Daniel Norman37d43dd2019-09-09 17:22:34 -0700283 param_strings.emplace_back(name + "=" + value);
284 }
285 return "@" + GetName() + "(" + Join(param_strings, ", ") + ")";
286 }
287}
288
Andrei Onea9445fc62019-06-27 18:11:59 +0100289static const AidlAnnotation* GetAnnotation(const vector<AidlAnnotation>& annotations,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700290 AidlAnnotation::Type type) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100291 for (const auto& a : annotations) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700292 if (a.GetType() == type) {
Jooyung Hand902a972020-10-23 17:32:44 +0900293 AIDL_FATAL_IF(a.Repeatable(), a)
294 << "Trying to get a single annotation when it is repeatable.";
Andrei Onea9445fc62019-06-27 18:11:59 +0100295 return &a;
296 }
297 }
298 return nullptr;
299}
300
Steven Moreland46e9da82018-07-27 15:45:29 -0700301AidlAnnotatable::AidlAnnotatable(const AidlLocation& location) : AidlNode(location) {}
302
Jiyong Park68bc77a2018-07-19 19:00:45 +0900303bool AidlAnnotatable::IsNullable() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700304 return GetAnnotation(annotations_, AidlAnnotation::Type::NULLABLE);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900305}
306
Jiyong Park68bc77a2018-07-19 19:00:45 +0900307bool AidlAnnotatable::IsUtf8InCpp() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700308 return GetAnnotation(annotations_, AidlAnnotation::Type::UTF8_IN_CPP);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900309}
310
Steven Morelanda7764e52020-10-27 17:29:29 +0000311bool AidlAnnotatable::IsSensitiveData() const {
312 return GetAnnotation(annotations_, AidlAnnotation::Type::SENSITIVE_DATA);
313}
314
Steven Morelanda57d0a62019-07-30 09:41:14 -0700315bool AidlAnnotatable::IsVintfStability() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700316 return GetAnnotation(annotations_, AidlAnnotation::Type::VINTF_STABILITY);
Steven Morelanda57d0a62019-07-30 09:41:14 -0700317}
318
Jeongik Chad0a10272020-08-06 16:33:36 +0900319bool AidlAnnotatable::IsJavaOnlyImmutable() const {
320 return GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE);
Jeongik Cha36f76c32020-07-28 00:25:52 +0900321}
322
Devin Moorec7e47a32020-08-07 10:55:25 -0700323bool AidlAnnotatable::IsFixedSize() const {
324 return GetAnnotation(annotations_, AidlAnnotation::Type::FIXED_SIZE);
325}
326
Andrei Onea9445fc62019-06-27 18:11:59 +0100327const AidlAnnotation* AidlAnnotatable::UnsupportedAppUsage() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700328 return GetAnnotation(annotations_, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE);
Jiyong Parka6605ab2018-11-11 14:30:21 +0900329}
330
Andrei Homescue61feb52020-08-18 15:44:24 -0700331const AidlAnnotation* AidlAnnotatable::RustDerive() const {
332 return GetAnnotation(annotations_, AidlAnnotation::Type::RUST_DERIVE);
333}
334
Daniel Norman716d3112019-09-10 13:11:56 -0700335const AidlTypeSpecifier* AidlAnnotatable::BackingType(const AidlTypenames& typenames) const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700336 auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::BACKING);
Daniel Norman85aed542019-08-21 12:01:14 -0700337 if (annotation != nullptr) {
338 auto annotation_params = annotation->AnnotationParams(AidlConstantValueDecorator);
339 if (auto it = annotation_params.find("type"); it != annotation_params.end()) {
340 const string& type = it->second;
Steven Morelande7d5d082020-05-21 20:29:02 +0000341
342 AIDL_FATAL_IF(type.size() < 2, this) << type;
343 AIDL_FATAL_IF(type[0] != '"', this) << type;
344 AIDL_FATAL_IF(type[type.length() - 1] != '"', this) << type;
345 string unquoted_type = type.substr(1, type.length() - 2);
346
Daniel Norman716d3112019-09-10 13:11:56 -0700347 AidlTypeSpecifier* type_specifier =
Jooyung Han60b2fde2020-10-29 15:12:40 +0900348 new AidlTypeSpecifier(annotation->GetLocation(), unquoted_type, false, nullptr, "");
Daniel Norman716d3112019-09-10 13:11:56 -0700349 type_specifier->Resolve(typenames);
350 return type_specifier;
Daniel Norman85aed542019-08-21 12:01:14 -0700351 }
352 }
353 return nullptr;
354}
355
Jeongik Cha88f95a82020-01-15 13:02:16 +0900356bool AidlAnnotatable::IsStableApiParcelable(Options::Language lang) const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700357 return lang == Options::Language::JAVA &&
358 GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_STABLE_PARCELABLE);
Jeongik Cha82317dd2019-02-27 20:26:11 +0900359}
360
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800361bool AidlAnnotatable::IsHide() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700362 return GetAnnotation(annotations_, AidlAnnotation::Type::HIDE);
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800363}
364
Jooyung Han829ec7c2020-12-02 12:07:36 +0900365bool AidlAnnotatable::JavaDerive(const std::string& method) const {
366 auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_DERIVE);
367 if (annotation != nullptr) {
368 auto params = annotation->AnnotationParams(AidlConstantValueDecorator);
369 if (auto it = params.find(method); it != params.end()) {
370 return it->second == "true";
371 }
372 }
373 return false;
Jiyong Park43113fb2020-07-20 16:26:19 +0900374}
375
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900376std::string AidlAnnotatable::GetDescriptor() const {
377 auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::DESCRIPTOR);
378 if (annotation != nullptr) {
379 auto params = annotation->AnnotationParams(AidlConstantValueDecorator);
380 if (auto it = params.find("value"); it != params.end()) {
381 const string& value = it->second;
382
383 AIDL_FATAL_IF(value.size() < 2, this) << value;
384 AIDL_FATAL_IF(value[0] != '"', this) << value;
385 AIDL_FATAL_IF(value[value.length() - 1] != '"', this) << value;
386 std::string unquoted_value = value.substr(1, value.length() - 2);
387 return unquoted_value;
388 }
389 }
390 return "";
391}
392
Steven Moreland7e4b9502020-02-20 18:10:42 -0800393void AidlAnnotatable::DumpAnnotations(CodeWriter* writer) const {
394 if (annotations_.empty()) return;
395
396 writer->Write("%s\n", AidlAnnotatable::ToString().c_str());
397}
398
Devin Moore24f68572020-02-26 13:20:59 -0800399bool AidlAnnotatable::CheckValid(const AidlTypenames&) const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700400 std::set<AidlAnnotation::Type> supported_annotations = GetSupportedAnnotations();
Andrei Onea9445fc62019-06-27 18:11:59 +0100401 for (const auto& annotation : GetAnnotations()) {
Jooyung Hand902a972020-10-23 17:32:44 +0900402 // check if it is allowed for this node
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700403 if (supported_annotations.find(annotation.GetType()) == supported_annotations.end()) {
Jooyung Hand902a972020-10-23 17:32:44 +0900404 std::vector<std::string> supported_annot_strings;
405 for (AidlAnnotation::Type type : supported_annotations) {
406 supported_annot_strings.push_back(AidlAnnotation::TypeToString(type));
407 }
Devin Moore24f68572020-02-26 13:20:59 -0800408 AIDL_ERROR(this) << "'" << annotation.GetName()
409 << "' is not a supported annotation for this node. "
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700410 << "It must be one of: "
411 << android::base::Join(supported_annot_strings, ", ");
Devin Moore24f68572020-02-26 13:20:59 -0800412 return false;
413 }
Jooyung Hand902a972020-10-23 17:32:44 +0900414 // CheckValid() only if it is okay to be here
415 if (!annotation.CheckValid()) {
416 return false;
417 }
418 }
419
420 std::map<AidlAnnotation::Type, AidlLocation> declared;
421 for (const auto& annotation : GetAnnotations()) {
422 const auto& [iter, inserted] = declared.emplace(annotation.GetType(), annotation.GetLocation());
423 if (!inserted && !annotation.Repeatable()) {
424 AIDL_ERROR(this) << "'" << annotation.GetName()
425 << "' is repeated, but not allowed. Previous location: " << iter->second;
426 return false;
427 }
Andrei Onea9445fc62019-06-27 18:11:59 +0100428 }
Steven Morelanda57d0a62019-07-30 09:41:14 -0700429
Andrei Onea9445fc62019-06-27 18:11:59 +0100430 return true;
431}
432
Jiyong Park68bc77a2018-07-19 19:00:45 +0900433string AidlAnnotatable::ToString() const {
434 vector<string> ret;
435 for (const auto& a : annotations_) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900436 ret.emplace_back(a.ToString());
Jiyong Park68bc77a2018-07-19 19:00:45 +0900437 }
438 std::sort(ret.begin(), ret.end());
439 return Join(ret, " ");
440}
441
Steven Moreland46e9da82018-07-27 15:45:29 -0700442AidlTypeSpecifier::AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name,
443 bool is_array,
Jiyong Park1deecc32018-07-17 01:14:41 +0900444 vector<unique_ptr<AidlTypeSpecifier>>* type_params,
Steven Moreland46e9da82018-07-27 15:45:29 -0700445 const string& comments)
446 : AidlAnnotatable(location),
Jeongik Chadf76dc72019-11-28 00:08:47 +0900447 AidlParameterizable<unique_ptr<AidlTypeSpecifier>>(type_params),
Steven Moreland46e9da82018-07-27 15:45:29 -0700448 unresolved_name_(unresolved_name),
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700449 is_array_(is_array),
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900450 comments_(comments),
451 split_name_(Split(unresolved_name, ".")) {}
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700452
Jooyung Hand2fa0232020-10-19 02:51:41 +0900453const AidlTypeSpecifier& AidlTypeSpecifier::ArrayBase() const {
Steven Moreland3f658cf2018-08-20 13:40:54 -0700454 AIDL_FATAL_IF(!is_array_, this);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900455 // Declaring array of generic type cannot happen, it is grammar error.
456 AIDL_FATAL_IF(IsGeneric(), this);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700457
Jooyung Hand2fa0232020-10-19 02:51:41 +0900458 if (!array_base_) {
459 array_base_.reset(new AidlTypeSpecifier(*this));
460 array_base_->is_array_ = false;
461 }
462 return *array_base_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700463}
464
Jeongik Cha997281d2020-01-16 15:23:59 +0900465bool AidlTypeSpecifier::IsHidden() const {
466 return HasHideComment(GetComments());
467}
468
Jooyung Han965e31d2020-11-27 12:30:16 +0900469string AidlTypeSpecifier::Signature() const {
Jiyong Park1deecc32018-07-17 01:14:41 +0900470 string ret = GetName();
471 if (IsGeneric()) {
472 vector<string> arg_names;
473 for (const auto& ta : GetTypeParameters()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900474 arg_names.emplace_back(ta->Signature());
Jiyong Parkccf00f82018-07-17 01:39:23 +0900475 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900476 ret += "<" + Join(arg_names, ",") + ">";
Jiyong Parkccf00f82018-07-17 01:39:23 +0900477 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900478 if (IsArray()) {
479 ret += "[]";
480 }
481 return ret;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900482}
483
Jooyung Han965e31d2020-11-27 12:30:16 +0900484string AidlTypeSpecifier::ToString() const {
485 string ret = Signature();
Jiyong Park02da7422018-07-16 16:00:26 +0900486 string annotations = AidlAnnotatable::ToString();
487 if (annotations != "") {
488 ret = annotations + " " + ret;
489 }
490 return ret;
491}
492
Daniel Norman716d3112019-09-10 13:11:56 -0700493bool AidlTypeSpecifier::Resolve(const AidlTypenames& typenames) {
Steven Moreland21780812020-09-11 01:29:45 +0000494 AIDL_FATAL_IF(IsResolved(), this);
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700495 AidlTypenames::ResolvedTypename result = typenames.ResolveTypename(unresolved_name_);
496 if (result.is_resolved) {
497 fully_qualified_name_ = result.canonical_name;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900498 split_name_ = Split(fully_qualified_name_, ".");
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900499 defined_type_ = result.defined_type;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900500 }
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700501 return result.is_resolved;
Casey Dahlin70078e62015-09-30 17:01:30 -0700502}
503
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900504const AidlDefinedType* AidlTypeSpecifier::GetDefinedType() const {
505 return defined_type_;
506}
507
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700508std::set<AidlAnnotation::Type> AidlTypeSpecifier::GetSupportedAnnotations() const {
Devin Moore24f68572020-02-26 13:20:59 -0800509 // kHide and kUnsupportedAppUsage are both method return annotations
510 // which we don't distinguish from other type specifiers.
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700511 return {AidlAnnotation::Type::NULLABLE, AidlAnnotation::Type::UTF8_IN_CPP,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900512 AidlAnnotation::Type::UNSUPPORTED_APP_USAGE, AidlAnnotation::Type::HIDE,
513 AidlAnnotation::Type::JAVA_PASSTHROUGH};
Devin Moore24f68572020-02-26 13:20:59 -0800514}
515
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900516bool AidlTypeSpecifier::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -0800517 if (!AidlAnnotatable::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100518 return false;
519 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900520 if (IsGeneric()) {
521 const string& type_name = GetName();
Jeongik Chae74c86d2019-12-12 16:54:03 +0900522
523 auto& types = GetTypeParameters();
524 // TODO(b/136048684) Disallow to use primitive types only if it is List or Map.
525 if (type_name == "List" || type_name == "Map") {
Jooyung Hane87cdd02020-12-11 16:47:35 +0900526 if (std::any_of(types.begin(), types.end(), [&](auto& type_ptr) {
527 return (typenames.GetEnumDeclaration(*type_ptr)) ||
528 AidlTypenames::IsPrimitiveTypename(type_ptr->GetName());
Jeongik Chae74c86d2019-12-12 16:54:03 +0900529 })) {
Devin Moore7b8d5c92020-03-17 14:14:08 -0700530 AIDL_ERROR(this) << "A generic type cannot have any primitive type parameters.";
Jeongik Chae74c86d2019-12-12 16:54:03 +0900531 return false;
532 }
533 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800534 const auto defined_type = typenames.TryGetDefinedType(type_name);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900535 const auto parameterizable =
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800536 defined_type != nullptr ? defined_type->AsParameterizable() : nullptr;
537 const bool is_user_defined_generic_type =
Jeongik Chadf76dc72019-11-28 00:08:47 +0900538 parameterizable != nullptr && parameterizable->IsGeneric();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800539 const size_t num_params = GetTypeParameters().size();
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900540 if (type_name == "List") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800541 if (num_params > 1) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900542 AIDL_ERROR(this) << "List can only have one type parameter, but got: '" << Signature()
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000543 << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900544 return false;
545 }
Jooyung Han55f96ad2020-12-13 10:08:33 +0900546 const AidlTypeSpecifier& contained_type = *GetTypeParameters()[0];
547 const string& contained_type_name = contained_type.GetName();
548 if (AidlTypenames::IsBuiltinTypename(contained_type_name)) {
549 if (contained_type_name != "String" && contained_type_name != "IBinder" &&
550 contained_type_name != "ParcelFileDescriptor") {
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 } else { // Defined types
557 if (typenames.GetInterface(contained_type)) {
558 AIDL_ERROR(this) << "List<" << contained_type_name
559 << "> is not supported. List<T> supports parcelable/union, String, "
560 "IBinder, and ParcelFileDescriptor.";
561 return false;
562 }
563 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900564 } else if (type_name == "Map") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800565 if (num_params != 0 && num_params != 2) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900566 AIDL_ERROR(this) << "Map must have 0 or 2 type parameters, but got "
Jooyung Han965e31d2020-11-27 12:30:16 +0900567 << "'" << Signature() << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900568 return false;
569 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800570 if (num_params == 2) {
Jeongik Chae48d9942020-01-02 17:39:00 +0900571 const string& key_type = GetTypeParameters()[0]->GetName();
572 if (key_type != "String") {
573 AIDL_ERROR(this) << "The type of key in map must be String, but it is "
574 << "'" << key_type << "'";
575 return false;
576 }
577 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800578 } else if (is_user_defined_generic_type) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900579 const size_t allowed = parameterizable->GetTypeParameters().size();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800580 if (num_params != allowed) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900581 AIDL_ERROR(this) << type_name << " must have " << allowed << " type parameters, but got "
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800582 << num_params;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900583 return false;
584 }
585 } else {
586 AIDL_ERROR(this) << type_name << " is not a generic type.";
587 return false;
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900588 }
589 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900590
Steven Moreland11cb9452020-01-21 16:56:58 -0800591 const bool is_generic_string_list = GetName() == "List" && IsGeneric() &&
592 GetTypeParameters().size() == 1 &&
593 GetTypeParameters()[0]->GetName() == "String";
594 if (IsUtf8InCpp() && (GetName() != "String" && !is_generic_string_list)) {
595 AIDL_ERROR(this) << "@utf8InCpp can only be used on String, String[], and List<String>.";
596 return false;
597 }
598
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900599 if (GetName() == "void") {
600 if (IsArray() || IsNullable() || IsUtf8InCpp()) {
601 AIDL_ERROR(this) << "void type cannot be an array or nullable or utf8 string";
602 return false;
603 }
604 }
605
606 if (IsArray()) {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800607 const auto defined_type = typenames.TryGetDefinedType(GetName());
608 if (defined_type != nullptr && defined_type->AsInterface() != nullptr) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900609 AIDL_ERROR(this) << "Binder type cannot be an array";
610 return false;
611 }
Steven Moreland8042d2d2020-09-30 23:31:32 +0000612 if (GetName() == "ParcelableHolder") {
613 AIDL_ERROR(this) << "Arrays of ParcelableHolder are not supported.";
614 return false;
615 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900616 }
617
618 if (IsNullable()) {
619 if (AidlTypenames::IsPrimitiveTypename(GetName()) && !IsArray()) {
620 AIDL_ERROR(this) << "Primitive type cannot get nullable annotation";
621 return false;
622 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800623 const auto defined_type = typenames.TryGetDefinedType(GetName());
624 if (defined_type != nullptr && defined_type->AsEnumDeclaration() != nullptr && !IsArray()) {
Daniel Normanee8674f2019-09-20 16:07:00 -0700625 AIDL_ERROR(this) << "Enum type cannot get nullable annotation";
626 return false;
627 }
Jeongik Chaf6ec8982020-10-15 00:10:30 +0900628 if (GetName() == "ParcelableHolder") {
629 AIDL_ERROR(this) << "ParcelableHolder cannot be nullable.";
630 return false;
631 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900632 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900633 return true;
634}
635
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900636std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type,
Steven Moreland860b1942018-08-16 14:59:28 -0700637 const std::string& raw_value) {
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900638 if (type.IsArray()) {
639 return raw_value;
640 }
641
642 if (auto defined_type = type.GetDefinedType(); defined_type) {
643 auto enum_type = defined_type->AsEnumDeclaration();
644 AIDL_FATAL_IF(!enum_type, type) << "Invalid type for \"" << raw_value << "\"";
645 return type.GetName() + "." + raw_value.substr(raw_value.find_last_of('.') + 1);
646 }
Steven Moreland860b1942018-08-16 14:59:28 -0700647 return raw_value;
648}
649
Steven Moreland46e9da82018-07-27 15:45:29 -0700650AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
651 AidlTypeSpecifier* type, const std::string& name)
Steven Moreland541788d2020-05-21 22:05:52 +0000652 : AidlVariableDeclaration(location, type, name, AidlConstantValue::Default(*type)) {
653 default_user_specified_ = false;
654}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700655
Steven Moreland46e9da82018-07-27 15:45:29 -0700656AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
657 AidlTypeSpecifier* type, const std::string& name,
658 AidlConstantValue* default_value)
Jooyung Han3f347ca2020-12-01 12:41:50 +0900659 : AidlMember(location),
Steven Moreland541788d2020-05-21 22:05:52 +0000660 type_(type),
661 name_(name),
662 default_user_specified_(true),
663 default_value_(default_value) {}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700664
Jooyung Han53fb4242020-12-17 16:03:49 +0900665bool AidlVariableDeclaration::HasUsefulDefaultValue() const {
666 if (GetDefaultValue()) {
667 return true;
668 }
669 // null is accepted as a valid default value in all backends
670 if (GetType().IsNullable()) {
671 return true;
672 }
673 return false;
674}
675
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900676bool AidlVariableDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700677 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900678 valid &= type_->CheckValid(typenames);
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900679
Steven Moreland54be7bd2019-12-05 11:17:53 -0800680 if (type_->GetName() == "void") {
681 AIDL_ERROR(this) << "Declaration " << name_
682 << " is void, but declarations cannot be of void type.";
683 valid = false;
684 }
685
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900686 if (default_value_ == nullptr) return valid;
Steven Moreland25294322018-08-07 18:13:55 -0700687 valid &= default_value_->CheckValid();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700688
Steven Moreland25294322018-08-07 18:13:55 -0700689 if (!valid) return false;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700690
Steven Moreland860b1942018-08-16 14:59:28 -0700691 return !ValueString(AidlConstantValueDecorator).empty();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700692}
Steven Moreland5557f1c2018-07-02 13:50:23 -0700693
Jooyung Hanacae85d2020-10-28 16:39:09 +0900694string AidlVariableDeclaration::GetCapitalizedName() const {
695 AIDL_FATAL_IF(name_.size() <= 0, *this) << "Name can't be empty.";
696 string str = name_;
697 str[0] = static_cast<char>(toupper(str[0]));
698 return str;
699}
700
Steven Moreland5557f1c2018-07-02 13:50:23 -0700701string AidlVariableDeclaration::ToString() const {
Jooyung Han965e31d2020-11-27 12:30:16 +0900702 string ret = type_->ToString() + " " + name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000703 if (default_value_ != nullptr && default_user_specified_) {
Steven Moreland860b1942018-08-16 14:59:28 -0700704 ret += " = " + ValueString(AidlConstantValueDecorator);
Steven Moreland9ea10e32018-07-19 15:26:09 -0700705 }
706 return ret;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700707}
708
Jiyong Park02da7422018-07-16 16:00:26 +0900709string AidlVariableDeclaration::Signature() const {
710 return type_->Signature() + " " + name_;
711}
712
Steven Moreland860b1942018-08-16 14:59:28 -0700713std::string AidlVariableDeclaration::ValueString(const ConstantValueDecorator& decorator) const {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900714 if (default_value_ != nullptr) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700715 return default_value_->ValueString(GetType(), decorator);
Jiyong Parka468e2a2018-08-29 21:25:18 +0900716 } else {
717 return "";
718 }
Steven Moreland25294322018-08-07 18:13:55 -0700719}
720
Steven Moreland46e9da82018-07-27 15:45:29 -0700721AidlArgument::AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
722 AidlTypeSpecifier* type, const std::string& name)
723 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700724 direction_(direction),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700725 direction_specified_(true) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700726
Steven Moreland46e9da82018-07-27 15:45:29 -0700727AidlArgument::AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type,
728 const std::string& name)
729 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700730 direction_(AidlArgument::IN_DIR),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700731 direction_specified_(false) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700732
Jiyong Park02da7422018-07-16 16:00:26 +0900733string AidlArgument::GetDirectionSpecifier() const {
Casey Dahlinc378c992015-09-29 16:50:40 -0700734 string ret;
Casey Dahlinc378c992015-09-29 16:50:40 -0700735 if (direction_specified_) {
736 switch(direction_) {
737 case AidlArgument::IN_DIR:
Devin Mooreeccdb902020-03-24 16:22:40 -0700738 ret += "in";
Casey Dahlinc378c992015-09-29 16:50:40 -0700739 break;
740 case AidlArgument::OUT_DIR:
Devin Mooreeccdb902020-03-24 16:22:40 -0700741 ret += "out";
Casey Dahlinc378c992015-09-29 16:50:40 -0700742 break;
743 case AidlArgument::INOUT_DIR:
Devin Mooreeccdb902020-03-24 16:22:40 -0700744 ret += "inout";
Casey Dahlinc378c992015-09-29 16:50:40 -0700745 break;
746 }
747 }
Casey Dahlinc378c992015-09-29 16:50:40 -0700748 return ret;
749}
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700750
Jiyong Park02da7422018-07-16 16:00:26 +0900751string AidlArgument::ToString() const {
Devin Mooreeccdb902020-03-24 16:22:40 -0700752 if (direction_specified_) {
753 return GetDirectionSpecifier() + " " + AidlVariableDeclaration::ToString();
754 } else {
755 return AidlVariableDeclaration::ToString();
756 }
Jiyong Park02da7422018-07-16 16:00:26 +0900757}
758
Steven Moreland46e9da82018-07-27 15:45:29 -0700759AidlMember::AidlMember(const AidlLocation& location) : AidlNode(location) {}
760
Steven Moreland46e9da82018-07-27 15:45:29 -0700761AidlConstantDeclaration::AidlConstantDeclaration(const AidlLocation& location,
762 AidlTypeSpecifier* type, const std::string& name,
763 AidlConstantValue* value)
764 : AidlMember(location), type_(type), name_(name), value_(value) {}
Steven Moreland693640b2018-07-19 13:46:27 -0700765
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900766bool AidlConstantDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700767 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900768 valid &= type_->CheckValid(typenames);
Steven Moreland25294322018-08-07 18:13:55 -0700769 valid &= value_->CheckValid();
770 if (!valid) return false;
Steven Moreland693640b2018-07-19 13:46:27 -0700771
Steven Morelande689da22020-11-10 02:06:30 +0000772 const static set<string> kSupportedConstTypes = {"String", "byte", "int", "long"};
Jooyung Han965e31d2020-11-27 12:30:16 +0900773 if (kSupportedConstTypes.find(type_->Signature()) == kSupportedConstTypes.end()) {
774 AIDL_ERROR(this) << "Constant of type " << type_->Signature() << " is not supported.";
Steven Moreland693640b2018-07-19 13:46:27 -0700775 return false;
776 }
777
Will McVickerd7d18df2019-09-12 13:40:50 -0700778 return true;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700779}
780
Jiyong Parka428d212018-08-29 22:26:30 +0900781string AidlConstantDeclaration::ToString() const {
Jooyung Hanb3ca6302020-11-27 14:13:27 +0900782 return "const " + type_->ToString() + " " + name_ + " = " +
783 ValueString(AidlConstantValueDecorator);
Jiyong Parka428d212018-08-29 22:26:30 +0900784}
785
786string AidlConstantDeclaration::Signature() const {
787 return type_->Signature() + " " + name_;
788}
789
Steven Moreland46e9da82018-07-27 15:45:29 -0700790AidlMethod::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)
793 : AidlMethod(location, oneway, type, name, args, comments, 0, true) {
794 has_id_ = false;
795}
796
797AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
798 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900799 const std::string& comments, int id, bool is_user_defined)
Steven Moreland46e9da82018-07-27 15:45:29 -0700800 : AidlMember(location),
801 oneway_(oneway),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700802 comments_(comments),
803 type_(type),
804 name_(name),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700805 arguments_(std::move(*args)),
Jiyong Parkb034bf02018-07-30 17:44:33 +0900806 id_(id),
807 is_user_defined_(is_user_defined) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700808 has_id_ = true;
809 delete args;
Christopher Wileyad339272015-10-05 19:11:58 -0700810 for (const unique_ptr<AidlArgument>& a : arguments_) {
811 if (a->IsIn()) { in_arguments_.push_back(a.get()); }
812 if (a->IsOut()) { out_arguments_.push_back(a.get()); }
813 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700814}
815
Jeongik Cha997281d2020-01-16 15:23:59 +0900816bool AidlMethod::IsHidden() const {
817 return HasHideComment(GetComments());
818}
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700819
Jiyong Park02da7422018-07-16 16:00:26 +0900820string AidlMethod::Signature() const {
821 vector<string> arg_signatures;
822 for (const auto& arg : GetArguments()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900823 arg_signatures.emplace_back(arg->GetType().Signature());
Jiyong Park02da7422018-07-16 16:00:26 +0900824 }
Jiyong Park309668e2018-07-28 16:55:44 +0900825 return GetName() + "(" + Join(arg_signatures, ", ") + ")";
826}
827
828string AidlMethod::ToString() const {
829 vector<string> arg_strings;
830 for (const auto& arg : GetArguments()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900831 arg_strings.emplace_back(arg->ToString());
Jiyong Park309668e2018-07-28 16:55:44 +0900832 }
Jooyung Han965e31d2020-11-27 12:30:16 +0900833 string ret = (IsOneway() ? "oneway " : "") + GetType().ToString() + " " + GetName() + "(" +
Steven Moreland4ee68632018-12-14 15:52:46 -0800834 Join(arg_strings, ", ") + ")";
Jiyong Parked65bf42018-08-28 15:43:27 +0900835 if (HasId()) {
836 ret += " = " + std::to_string(GetId());
837 }
838 return ret;
Jiyong Park02da7422018-07-16 16:00:26 +0900839}
840
Steven Moreland46e9da82018-07-27 15:45:29 -0700841AidlDefinedType::AidlDefinedType(const AidlLocation& location, const std::string& name,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900842 const std::string& comments, const std::string& package,
843 std::vector<std::unique_ptr<AidlMember>>* members)
Jiyong Park18132182020-06-08 20:24:40 +0900844 : AidlAnnotatable(location),
845 name_(name),
846 comments_(comments),
847 package_(package),
848 split_package_(package.empty() ? std::vector<std::string>()
Jooyung Han829ec7c2020-12-02 12:07:36 +0900849 : android::base::Split(package, ".")) {
850 if (members) {
851 for (auto& m : *members) {
852 if (auto constant = m->AsConstantDeclaration(); constant) {
853 constants_.emplace_back(constant);
854 } else if (auto variable = m->AsVariableDeclaration(); variable) {
855 variables_.emplace_back(variable);
856 } else if (auto method = m->AsMethod(); method) {
857 methods_.emplace_back(method);
858 } else {
859 AIDL_FATAL(*m);
860 }
861 members_.push_back(m.release());
862 }
863 delete members;
864 }
865}
Steven Moreland787b0432018-07-03 09:00:58 -0700866
Jooyung Han888c5bc2020-12-22 17:28:47 +0900867bool AidlDefinedType::CheckValid(const AidlTypenames& typenames, DiagnosticsContext&) const {
Devin Moore24f68572020-02-26 13:20:59 -0800868 if (!AidlAnnotatable::CheckValid(typenames)) {
869 return false;
870 }
Jooyung Han829ec7c2020-12-02 12:07:36 +0900871 if (!CheckValidWithMembers(typenames)) {
872 return false;
873 }
Devin Moore24f68572020-02-26 13:20:59 -0800874 return true;
875}
876
Jeongik Cha997281d2020-01-16 15:23:59 +0900877bool AidlDefinedType::IsHidden() const {
878 return HasHideComment(GetComments());
879}
880
Steven Moreland787b0432018-07-03 09:00:58 -0700881std::string AidlDefinedType::GetCanonicalName() const {
882 if (package_.empty()) {
883 return GetName();
884 }
885 return GetPackage() + "." + GetName();
886}
887
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800888void AidlDefinedType::DumpHeader(CodeWriter* writer) const {
889 if (this->IsHidden()) {
890 AddHideComment(writer);
891 }
892 DumpAnnotations(writer);
893}
894
Jooyung Han829ec7c2020-12-02 12:07:36 +0900895bool AidlDefinedType::CheckValidWithMembers(const AidlTypenames& typenames) const {
896 bool success = true;
897
898 for (const auto& v : GetFields()) {
899 const bool field_valid = v->CheckValid(typenames);
900 success = success && field_valid;
901 }
902
903 // field names should be unique
904 std::set<std::string> fieldnames;
905 for (const auto& v : GetFields()) {
906 bool duplicated = !fieldnames.emplace(v->GetName()).second;
907 if (duplicated) {
908 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName() << "'";
909 success = false;
910 }
911 }
912
913 // immutable parcelables should have immutable fields.
914 if (IsJavaOnlyImmutable()) {
915 for (const auto& v : GetFields()) {
916 if (!typenames.CanBeJavaOnlyImmutable(v->GetType())) {
917 AIDL_ERROR(v) << "The @JavaOnlyImmutable '" << GetName() << "' has a "
918 << "non-immutable field named '" << v->GetName() << "'.";
919 success = false;
920 }
921 }
922 }
923
924 set<string> constant_names;
925 for (const auto& constant : GetConstantDeclarations()) {
926 if (constant_names.count(constant->GetName()) > 0) {
927 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
928 success = false;
929 }
930 constant_names.insert(constant->GetName());
931 success = success && constant->CheckValid(typenames);
932 }
933
934 return success;
935}
936
937bool AidlDefinedType::CheckValidForGetterNames() const {
938 bool success = true;
939 std::set<std::string> getters;
940 for (const auto& v : GetFields()) {
941 bool duplicated = !getters.emplace(v->GetCapitalizedName()).second;
942 if (duplicated) {
943 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName()
944 << "' after capitalizing the first letter";
945 success = false;
946 }
947 }
948 return success;
949}
950
Jiyong Park18132182020-06-08 20:24:40 +0900951AidlParcelable::AidlParcelable(const AidlLocation& location, const std::string& name,
952 const std::string& package, const std::string& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900953 const std::string& cpp_header, std::vector<std::string>* type_params,
954 std::vector<std::unique_ptr<AidlMember>>* members)
955 : AidlDefinedType(location, name, comments, package, members),
Jeongik Chadf76dc72019-11-28 00:08:47 +0900956 AidlParameterizable<std::string>(type_params),
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800957 cpp_header_(cpp_header) {
958 // Strip off quotation marks if we actually have a cpp header.
959 if (cpp_header_.length() >= 2) {
960 cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2);
961 }
Casey Dahlin59401da2015-10-09 18:16:45 -0700962}
Jeongik Chadf76dc72019-11-28 00:08:47 +0900963template <typename T>
964AidlParameterizable<T>::AidlParameterizable(const AidlParameterizable& other) {
965 // Copying is not supported if it has type parameters.
966 // It doesn't make a problem because only ArrayBase() makes a copy,
967 // and it can be called only if a type is not generic.
Steven Moreland21780812020-09-11 01:29:45 +0000968 AIDL_FATAL_IF(other.IsGeneric(), AIDL_LOCATION_HERE);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900969}
970
971template <typename T>
972bool AidlParameterizable<T>::CheckValid() const {
973 return true;
974};
975
976template <>
977bool AidlParameterizable<std::string>::CheckValid() const {
978 if (!IsGeneric()) {
979 return true;
980 }
981 std::unordered_set<std::string> set(GetTypeParameters().begin(), GetTypeParameters().end());
982 if (set.size() != GetTypeParameters().size()) {
983 AIDL_ERROR(this->AsAidlNode()) << "Every type parameter should be unique.";
984 return false;
985 }
986 return true;
987}
Casey Dahlin59401da2015-10-09 18:16:45 -0700988
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700989std::set<AidlAnnotation::Type> AidlParcelable::GetSupportedAnnotations() const {
Jeongik Cha36f76c32020-07-28 00:25:52 +0900990 return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900991 AidlAnnotation::Type::JAVA_STABLE_PARCELABLE, AidlAnnotation::Type::HIDE,
Jeongik Chad0a10272020-08-06 16:33:36 +0900992 AidlAnnotation::Type::JAVA_PASSTHROUGH, AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE};
Devin Moore24f68572020-02-26 13:20:59 -0800993}
994
Jooyung Han888c5bc2020-12-22 17:28:47 +0900995bool AidlParcelable::CheckValid(const AidlTypenames& typenames, DiagnosticsContext& diag) const {
996 if (!AidlDefinedType::CheckValid(typenames, diag)) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100997 return false;
998 }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900999 if (!AidlParameterizable<std::string>::CheckValid()) {
1000 return false;
1001 }
Jeongik Cha82317dd2019-02-27 20:26:11 +09001002
1003 return true;
1004}
1005
Jeongik Cha997281d2020-01-16 15:23:59 +09001006void AidlParcelable::Dump(CodeWriter* writer) const {
Steven Morelanda5d9c5c2020-02-21 16:01:09 -08001007 DumpHeader(writer);
Jiyong Park02da7422018-07-16 16:00:26 +09001008 writer->Write("parcelable %s ;\n", GetName().c_str());
1009}
1010
Steven Moreland5557f1c2018-07-02 13:50:23 -07001011AidlStructuredParcelable::AidlStructuredParcelable(
Jiyong Park18132182020-06-08 20:24:40 +09001012 const AidlLocation& location, const std::string& name, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001013 const std::string& comments, std::vector<std::string>* type_params,
1014 std::vector<std::unique_ptr<AidlMember>>* members)
1015 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Steven Moreland5557f1c2018-07-02 13:50:23 -07001016
Jeongik Cha997281d2020-01-16 15:23:59 +09001017void AidlStructuredParcelable::Dump(CodeWriter* writer) const {
Steven Morelanda5d9c5c2020-02-21 16:01:09 -08001018 DumpHeader(writer);
Jiyong Park02da7422018-07-16 16:00:26 +09001019 writer->Write("parcelable %s {\n", GetName().c_str());
1020 writer->Indent();
1021 for (const auto& field : GetFields()) {
Jeongik Cha997281d2020-01-16 15:23:59 +09001022 if (field->GetType().IsHidden()) {
1023 AddHideComment(writer);
1024 }
Jiyong Parka468e2a2018-08-29 21:25:18 +09001025 writer->Write("%s;\n", field->ToString().c_str());
Jiyong Park02da7422018-07-16 16:00:26 +09001026 }
Jooyung Han3f347ca2020-12-01 12:41:50 +09001027 for (const auto& constdecl : GetConstantDeclarations()) {
1028 if (constdecl->GetType().IsHidden()) {
1029 AddHideComment(writer);
1030 }
1031 writer->Write("%s;\n", constdecl->ToString().c_str());
1032 }
Jiyong Park02da7422018-07-16 16:00:26 +09001033 writer->Dedent();
1034 writer->Write("}\n");
1035}
1036
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001037std::set<AidlAnnotation::Type> AidlStructuredParcelable::GetSupportedAnnotations() const {
Jeongik Cha36f76c32020-07-28 00:25:52 +09001038 return {AidlAnnotation::Type::VINTF_STABILITY,
1039 AidlAnnotation::Type::UNSUPPORTED_APP_USAGE,
1040 AidlAnnotation::Type::HIDE,
1041 AidlAnnotation::Type::JAVA_PASSTHROUGH,
Jooyung Han90345002020-10-23 15:28:53 +09001042 AidlAnnotation::Type::JAVA_DERIVE,
Devin Moorec7e47a32020-08-07 10:55:25 -07001043 AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE,
Andrei Homescue61feb52020-08-18 15:44:24 -07001044 AidlAnnotation::Type::FIXED_SIZE,
1045 AidlAnnotation::Type::RUST_DERIVE};
Devin Moore24f68572020-02-26 13:20:59 -08001046}
1047
Jooyung Han888c5bc2020-12-22 17:28:47 +09001048bool AidlStructuredParcelable::CheckValid(const AidlTypenames& typenames,
1049 DiagnosticsContext& diag) const {
1050 if (!AidlParcelable::CheckValid(typenames, diag)) {
Devin Moore24f68572020-02-26 13:20:59 -08001051 return false;
1052 }
Jeongik Cha13066da2020-08-06 15:43:19 +09001053
Jooyung Han59af9cc2020-10-25 21:44:14 +09001054 bool success = true;
Jeongik Cha36f76c32020-07-28 00:25:52 +09001055
Jooyung Hand4057c42020-10-23 13:28:22 +09001056 if (IsFixedSize()) {
1057 for (const auto& v : GetFields()) {
1058 if (!typenames.CanBeFixedSize(v->GetType())) {
1059 AIDL_ERROR(v) << "The @FixedSize parcelable '" << this->GetName() << "' has a "
1060 << "non-fixed size field named " << v->GetName() << ".";
1061 success = false;
1062 }
1063 }
1064 }
1065
1066 if (IsJavaOnlyImmutable()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001067 // Immutable parcelables provide getters
Jooyung Han829ec7c2020-12-02 12:07:36 +09001068 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001069 success = false;
Jooyung Hand4057c42020-10-23 13:28:22 +09001070 }
1071 }
1072
Daniel Norman85aed542019-08-21 12:01:14 -07001073 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001074}
1075
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001076// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -07001077bool AidlTypeSpecifier::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1078 Options::Language lang) const {
Andrei Homescub62afd92020-05-11 19:24:59 -07001079 if ((lang == Options::Language::NDK || lang == Options::Language::RUST) && IsArray() &&
1080 GetName() == "IBinder") {
1081 AIDL_ERROR(this) << "The " << Options::LanguageToString(lang)
1082 << " backend does not support array of IBinder";
Steven Moreland0185d9b2020-05-15 23:21:22 +00001083 return false;
1084 }
Jeongik Cha8f02a532020-10-14 00:16:28 +09001085 if (lang == Options::Language::RUST && GetName() == "ParcelableHolder") {
1086 // TODO(b/146611855): Remove it when Rust backend supports ParcelableHolder
1087 AIDL_ERROR(this) << "The Rust backend does not support ParcelableHolder yet.";
Jeongik Cha225519b2020-08-29 01:55:32 +09001088 return false;
1089 }
Andrei Homescub62afd92020-05-11 19:24:59 -07001090 if ((lang == Options::Language::NDK || lang == Options::Language::RUST) && IsArray() &&
1091 IsNullable()) {
Steven Moreland0185d9b2020-05-15 23:21:22 +00001092 if (GetName() == "ParcelFileDescriptor") {
Andrei Homescub62afd92020-05-11 19:24:59 -07001093 AIDL_ERROR(this) << "The " << Options::LanguageToString(lang)
1094 << " backend does not support nullable array of ParcelFileDescriptor";
Steven Moreland0185d9b2020-05-15 23:21:22 +00001095 return false;
1096 }
1097
Steven Morelandd59e3172020-05-11 16:42:09 -07001098 const auto defined_type = typenames.TryGetDefinedType(GetName());
1099 if (defined_type != nullptr && defined_type->AsParcelable() != nullptr) {
Andrei Homescub62afd92020-05-11 19:24:59 -07001100 AIDL_ERROR(this) << "The " << Options::LanguageToString(lang)
1101 << " backend does not support nullable array of parcelable";
Steven Morelandd59e3172020-05-11 16:42:09 -07001102 return false;
1103 }
1104 }
Andrei Homescub62afd92020-05-11 19:24:59 -07001105 if (this->GetName() == "FileDescriptor" &&
1106 (lang == Options::Language::NDK || lang == Options::Language::RUST)) {
1107 AIDL_ERROR(this) << "FileDescriptor isn't supported by the " << Options::LanguageToString(lang)
1108 << " backend.";
Steven Morelandc8a4ca82020-01-21 17:50:08 -08001109 return false;
1110 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001111 if (this->IsGeneric()) {
1112 if (this->GetName() == "List") {
Jooyung Han55f96ad2020-12-13 10:08:33 +09001113 if (lang == Options::Language::NDK) {
1114 const AidlTypeSpecifier& contained_type = *GetTypeParameters()[0];
1115 const string& contained_type_name = contained_type.GetName();
Jooyung Hane87cdd02020-12-11 16:47:35 +09001116 if (typenames.GetInterface(contained_type)) {
1117 AIDL_ERROR(this) << "List<" << contained_type_name
1118 << "> is not supported. List in NDK doesn't support interface.";
1119 return false;
1120 }
1121 if (contained_type_name == "IBinder") {
1122 AIDL_ERROR(this) << "List<" << contained_type_name
1123 << "> is not supported. List in NDK doesn't support IBinder.";
1124 return false;
Jeongik Cha08ca2182019-11-21 14:01:13 +09001125 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001126 }
Jeongik Chabb55b5e2020-01-07 23:11:26 +09001127 }
1128 }
Devin Moore6a01ca12020-08-28 10:24:19 -07001129
1130 if (this->IsArray()) {
1131 if (this->GetName() == "List" || this->GetName() == "Map" ||
1132 this->GetName() == "CharSequence") {
1133 AIDL_ERROR(this) << this->GetName() << "[] is not supported.";
Jeongik Chabb55b5e2020-01-07 23:11:26 +09001134 return false;
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001135 }
1136 }
Devin Moore6a01ca12020-08-28 10:24:19 -07001137
1138 if (lang != Options::Language::JAVA) {
1139 if (this->GetName() == "List" && !this->IsGeneric()) {
1140 AIDL_ERROR(this) << "Currently, only the Java backend supports non-generic List.";
1141 return false;
1142 }
1143 if (this->GetName() == "Map" || this->GetName() == "CharSequence") {
1144 AIDL_ERROR(this) << "Currently, only Java backend supports " << this->GetName() << ".";
1145 return false;
Jeongik Cha08ca2182019-11-21 14:01:13 +09001146 }
1147 }
1148
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001149 return true;
1150}
1151
1152// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -07001153bool AidlParcelable::LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/,
1154 Options::Language lang) const {
Andrei Homescub62afd92020-05-11 19:24:59 -07001155 if (lang == Options::Language::CPP || lang == Options::Language::NDK) {
Steven Moreland0d9c26e2020-01-22 08:52:08 -08001156 const AidlParcelable* unstructured_parcelable = this->AsUnstructuredParcelable();
1157 if (unstructured_parcelable != nullptr) {
1158 if (unstructured_parcelable->GetCppHeader().empty()) {
1159 AIDL_ERROR(unstructured_parcelable)
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001160 << "Unstructured parcelable must have C++ header defined.";
1161 return false;
1162 }
1163 }
1164 }
1165 return true;
1166}
1167
1168// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -07001169bool AidlStructuredParcelable::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1170 Options::Language lang) const {
1171 if (!AidlParcelable::LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001172 return false;
1173 }
1174 for (const auto& v : this->GetFields()) {
Steven Morelandd59e3172020-05-11 16:42:09 -07001175 if (!v->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001176 return false;
1177 }
1178 }
1179 return true;
1180}
1181
Daniel Norman85aed542019-08-21 12:01:14 -07001182AidlEnumerator::AidlEnumerator(const AidlLocation& location, const std::string& name,
Daniel Norman2e4112d2019-10-03 10:22:35 -07001183 AidlConstantValue* value, const std::string& comments)
Jooyung Han29813842020-12-08 01:28:03 +09001184 : AidlNode(location),
1185 name_(name),
1186 value_(value),
1187 comments_(comments),
1188 value_user_specified_(value != nullptr) {}
Daniel Norman85aed542019-08-21 12:01:14 -07001189
1190bool AidlEnumerator::CheckValid(const AidlTypeSpecifier& enum_backing_type) const {
1191 if (GetValue() == nullptr) {
1192 return false;
1193 }
1194 if (!GetValue()->CheckValid()) {
1195 return false;
1196 }
Will McVickerd7d18df2019-09-12 13:40:50 -07001197 if (GetValue()->ValueString(enum_backing_type, AidlConstantValueDecorator).empty()) {
Daniel Norman85aed542019-08-21 12:01:14 -07001198 AIDL_ERROR(this) << "Enumerator type differs from enum backing type.";
1199 return false;
1200 }
1201 return true;
1202}
1203
1204string AidlEnumerator::ValueString(const AidlTypeSpecifier& backing_type,
1205 const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -07001206 return GetValue()->ValueString(backing_type, decorator);
Daniel Norman85aed542019-08-21 12:01:14 -07001207}
1208
1209AidlEnumDeclaration::AidlEnumDeclaration(const AidlLocation& location, const std::string& name,
1210 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jiyong Park18132182020-06-08 20:24:40 +09001211 const std::string& package, const std::string& comments)
Jooyung Han829ec7c2020-12-02 12:07:36 +09001212 : AidlDefinedType(location, name, comments, package, nullptr),
Jooyung Han29813842020-12-08 01:28:03 +09001213 enumerators_(std::move(*enumerators)) {
1214 Autofill();
1215}
Daniel Norman85aed542019-08-21 12:01:14 -07001216
1217void AidlEnumDeclaration::SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type) {
1218 backing_type_ = std::move(type);
1219}
1220
Jooyung Han29813842020-12-08 01:28:03 +09001221void AidlEnumDeclaration::Autofill() {
Daniel Normanb28684e2019-10-17 15:31:39 -07001222 const AidlEnumerator* previous = nullptr;
1223 for (const auto& enumerator : enumerators_) {
1224 if (enumerator->GetValue() == nullptr) {
Jooyung Han29813842020-12-08 01:28:03 +09001225 auto loc = enumerator->GetLocation();
Daniel Normanb28684e2019-10-17 15:31:39 -07001226 if (previous == nullptr) {
Devin Mooredf93ebb2020-03-25 14:03:35 -07001227 enumerator->SetValue(
Jooyung Han29813842020-12-08 01:28:03 +09001228 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "0")));
Daniel Normanb28684e2019-10-17 15:31:39 -07001229 } else {
Jooyung Han29813842020-12-08 01:28:03 +09001230 auto prev_value = std::make_unique<AidlConstantReference>(loc, previous->GetName(), "");
Daniel Normanb28684e2019-10-17 15:31:39 -07001231 enumerator->SetValue(std::make_unique<AidlBinaryConstExpression>(
Jooyung Han29813842020-12-08 01:28:03 +09001232 loc, std::move(prev_value), "+",
1233 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "1"))));
Daniel Normanb28684e2019-10-17 15:31:39 -07001234 }
1235 }
1236 previous = enumerator.get();
1237 }
1238}
1239
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001240std::set<AidlAnnotation::Type> AidlEnumDeclaration::GetSupportedAnnotations() const {
1241 return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::BACKING,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +09001242 AidlAnnotation::Type::HIDE, AidlAnnotation::Type::JAVA_PASSTHROUGH};
Devin Moore24f68572020-02-26 13:20:59 -08001243}
1244
Jooyung Han888c5bc2020-12-22 17:28:47 +09001245bool AidlEnumDeclaration::CheckValid(const AidlTypenames& typenames,
1246 DiagnosticsContext& diag) const {
1247 if (!AidlDefinedType::CheckValid(typenames, diag)) {
Devin Moore24f68572020-02-26 13:20:59 -08001248 return false;
1249 }
Jooyung Han829ec7c2020-12-02 12:07:36 +09001250 if (!GetMembers().empty()) {
1251 AIDL_ERROR(this) << "Enum doesn't support fields/constants/methods.";
1252 return false;
1253 }
Daniel Norman85aed542019-08-21 12:01:14 -07001254 if (backing_type_ == nullptr) {
1255 AIDL_ERROR(this) << "Enum declaration missing backing type.";
1256 return false;
1257 }
1258 bool success = true;
1259 for (const auto& enumerator : enumerators_) {
1260 success = success && enumerator->CheckValid(GetBackingType());
1261 }
Jooyung Han3b990182020-12-22 17:44:31 +09001262
Steven Moreland26318532020-12-23 20:08:36 +00001263 if (!success) return false; // ValueString requires valid type
1264
Jooyung Han3b990182020-12-22 17:44:31 +09001265 AIDL_FATAL_IF(GetEnumerators().empty(), this)
1266 << "The enum '" << GetName() << "' has no enumerators.";
1267
1268 const auto& first = GetEnumerators()[0];
1269 if (auto first_value = first->ValueString(GetBackingType(), AidlConstantValueDecorator);
1270 first_value != "0") {
1271 diag.Report(first->GetLocation(), DiagnosticID::enum_zero)
1272 << "The first enumerator '" << first->GetName() << "' should be 0, but it is "
1273 << first_value << ".";
1274 }
1275
Steven Moreland26318532020-12-23 20:08:36 +00001276 return true;
Daniel Norman85aed542019-08-21 12:01:14 -07001277}
1278
Jeongik Cha997281d2020-01-16 15:23:59 +09001279void AidlEnumDeclaration::Dump(CodeWriter* writer) const {
Steven Morelanda5d9c5c2020-02-21 16:01:09 -08001280 DumpHeader(writer);
Daniel Norman37d43dd2019-09-09 17:22:34 -07001281 writer->Write("enum %s {\n", GetName().c_str());
Daniel Norman85aed542019-08-21 12:01:14 -07001282 writer->Indent();
1283 for (const auto& enumerator : GetEnumerators()) {
Daniel Norman85aed542019-08-21 12:01:14 -07001284 writer->Write("%s = %s,\n", enumerator->GetName().c_str(),
1285 enumerator->ValueString(GetBackingType(), AidlConstantValueDecorator).c_str());
1286 }
1287 writer->Dedent();
1288 writer->Write("}\n");
1289}
1290
Jooyung Han2946afc2020-10-05 20:29:16 +09001291AidlUnionDecl::AidlUnionDecl(const AidlLocation& location, const std::string& name,
1292 const std::string& package, const std::string& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001293 std::vector<std::string>* type_params,
1294 std::vector<std::unique_ptr<AidlMember>>* members)
1295 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Jooyung Han2946afc2020-10-05 20:29:16 +09001296
1297std::set<AidlAnnotation::Type> AidlUnionDecl::GetSupportedAnnotations() const {
Jooyung Hanf93b5bd2020-10-28 15:12:08 +09001298 return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::HIDE,
1299 AidlAnnotation::Type::JAVA_PASSTHROUGH, AidlAnnotation::Type::JAVA_DERIVE,
1300 AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE, AidlAnnotation::Type::RUST_DERIVE};
Jooyung Han2946afc2020-10-05 20:29:16 +09001301}
1302
1303void AidlUnionDecl::Dump(CodeWriter* writer) const {
1304 DumpHeader(writer);
1305 writer->Write("union %s {\n", GetName().c_str());
1306 writer->Indent();
1307 for (const auto& field : GetFields()) {
1308 if (field->GetType().IsHidden()) {
1309 AddHideComment(writer);
1310 }
1311 writer->Write("%s;\n", field->ToString().c_str());
1312 }
Jooyung Han3f347ca2020-12-01 12:41:50 +09001313 for (const auto& constdecl : GetConstantDeclarations()) {
1314 if (constdecl->GetType().IsHidden()) {
1315 AddHideComment(writer);
1316 }
1317 writer->Write("%s;\n", constdecl->ToString().c_str());
1318 }
Jooyung Han2946afc2020-10-05 20:29:16 +09001319 writer->Dedent();
1320 writer->Write("}\n");
1321}
1322
Jooyung Han888c5bc2020-12-22 17:28:47 +09001323bool AidlUnionDecl::CheckValid(const AidlTypenames& typenames, DiagnosticsContext& diag) const {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001324 // visit parents
Jooyung Han888c5bc2020-12-22 17:28:47 +09001325 if (!AidlParcelable::CheckValid(typenames, diag)) {
Jooyung Hanfe89f122020-10-14 03:49:18 +09001326 return false;
1327 }
Jooyung Han59af9cc2020-10-25 21:44:14 +09001328
1329 // unions provide getters always
Jooyung Han829ec7c2020-12-02 12:07:36 +09001330 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001331 return false;
Jooyung Hanfe89f122020-10-14 03:49:18 +09001332 }
1333
1334 // now, visit self!
1335 bool success = true;
1336
1337 // TODO(b/170807936) do we need to allow ParcelableHolder in union?
1338 for (const auto& v : GetFields()) {
1339 if (v->GetType().GetName() == "ParcelableHolder") {
1340 AIDL_ERROR(*v) << "A union can't have a member of ParcelableHolder '" << v->GetName() << "'";
1341 success = false;
1342 }
1343 }
1344
Jooyung Hanfe89f122020-10-14 03:49:18 +09001345 if (GetFields().empty()) {
1346 AIDL_ERROR(*this) << "The union '" << this->GetName() << "' has no fields.";
1347 return false;
1348 }
1349
Jooyung Han53fb4242020-12-17 16:03:49 +09001350 // first member should have useful default value (implicit or explicit)
1351 const auto& first = GetFields()[0];
1352 if (!first->HasUsefulDefaultValue()) {
1353 // Most types can be initialized without a default value. For example,
1354 // interface types are inherently nullable. But, enum types should have
1355 // an explicit default value.
1356 if (!first->GetType().IsArray() && typenames.GetEnumDeclaration(first->GetType())) {
1357 AIDL_ERROR(first)
1358 << "The union's first member should have a useful default value. Enum types can be "
1359 "initialized with a reference. (e.g. ... = MyEnum.FOO;)";
1360 return false;
1361 }
1362 // In Java, array types are initialized as null without a default value. To be sure that default
1363 // initialized unions are accepted by other backends we require arrays also have a default
1364 // value.
1365 if (first->GetType().IsArray()) {
1366 AIDL_ERROR(first)
1367 << "The union's first member should have a useful default value. Arrays can be "
1368 "initialized with values(e.g. ... = { values... };) or marked as @nullable.";
1369 return false;
1370 }
1371 }
1372
Jooyung Hanfe89f122020-10-14 03:49:18 +09001373 return success;
1374}
1375
1376// TODO: we should treat every backend all the same in future.
1377bool AidlUnionDecl::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1378 Options::Language lang) const {
1379 if (!AidlParcelable::LanguageSpecificCheckValid(typenames, lang)) {
1380 return false;
1381 }
1382 for (const auto& v : this->GetFields()) {
1383 if (!v->GetType().LanguageSpecificCheckValid(typenames, lang)) {
1384 return false;
1385 }
1386 }
1387 return true;
1388}
1389
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001390// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -07001391bool AidlInterface::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1392 Options::Language lang) const {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001393 for (const auto& m : this->GetMethods()) {
Steven Morelandd59e3172020-05-11 16:42:09 -07001394 if (!m->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001395 return false;
1396 }
1397 for (const auto& arg : m->GetArguments()) {
Steven Morelandd59e3172020-05-11 16:42:09 -07001398 if (!arg->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001399 return false;
1400 }
1401 }
1402 }
1403 return true;
1404}
1405
Steven Moreland46e9da82018-07-27 15:45:29 -07001406AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001407 const std::string& comments, bool oneway, const std::string& package,
1408 std::vector<std::unique_ptr<AidlMember>>* members)
1409 : AidlDefinedType(location, name, comments, package, members) {
1410 for (auto& m : GetMethods()) {
1411 m.get()->ApplyInterfaceOneway(oneway);
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001412 }
Casey Dahlinfb7da2e2015-10-08 17:26:09 -07001413}
1414
Jeongik Cha997281d2020-01-16 15:23:59 +09001415void AidlInterface::Dump(CodeWriter* writer) const {
Steven Morelanda5d9c5c2020-02-21 16:01:09 -08001416 DumpHeader(writer);
Jiyong Park02da7422018-07-16 16:00:26 +09001417 writer->Write("interface %s {\n", GetName().c_str());
1418 writer->Indent();
1419 for (const auto& method : GetMethods()) {
Jeongik Cha997281d2020-01-16 15:23:59 +09001420 if (method->IsHidden()) {
1421 AddHideComment(writer);
1422 }
Jiyong Park309668e2018-07-28 16:55:44 +09001423 writer->Write("%s;\n", method->ToString().c_str());
Jiyong Park02da7422018-07-16 16:00:26 +09001424 }
Jiyong Parka428d212018-08-29 22:26:30 +09001425 for (const auto& constdecl : GetConstantDeclarations()) {
Jeongik Cha997281d2020-01-16 15:23:59 +09001426 if (constdecl->GetType().IsHidden()) {
1427 AddHideComment(writer);
1428 }
Jiyong Parka428d212018-08-29 22:26:30 +09001429 writer->Write("%s;\n", constdecl->ToString().c_str());
1430 }
Jiyong Park02da7422018-07-16 16:00:26 +09001431 writer->Dedent();
1432 writer->Write("}\n");
1433}
1434
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001435std::set<AidlAnnotation::Type> AidlInterface::GetSupportedAnnotations() const {
Steven Morelanda7764e52020-10-27 17:29:29 +00001436 return {AidlAnnotation::Type::SENSITIVE_DATA, AidlAnnotation::Type::VINTF_STABILITY,
1437 AidlAnnotation::Type::UNSUPPORTED_APP_USAGE, AidlAnnotation::Type::HIDE,
1438 AidlAnnotation::Type::JAVA_PASSTHROUGH, AidlAnnotation::Type::DESCRIPTOR};
Devin Moore24f68572020-02-26 13:20:59 -08001439}
1440
Jooyung Han888c5bc2020-12-22 17:28:47 +09001441bool AidlInterface::CheckValid(const AidlTypenames& typenames, DiagnosticsContext& diag) const {
1442 if (!AidlDefinedType::CheckValid(typenames, diag)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001443 return false;
1444 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001445 // Has to be a pointer due to deleting copy constructor. No idea why.
1446 map<string, const AidlMethod*> method_names;
1447 for (const auto& m : GetMethods()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001448 if (!m->GetType().CheckValid(typenames)) {
1449 return false;
1450 }
1451
Jeongik Cha649e8a72020-03-27 17:47:40 +09001452 // TODO(b/156872582): Support it when ParcelableHolder supports every backend.
1453 if (m->GetType().GetName() == "ParcelableHolder") {
1454 AIDL_ERROR(m) << "ParcelableHolder cannot be a return type";
1455 return false;
1456 }
Steven Morelandacd53472018-12-14 10:17:26 -08001457 if (m->IsOneway() && m->GetType().GetName() != "void") {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001458 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot return a value";
1459 return false;
1460 }
1461
1462 set<string> argument_names;
1463 for (const auto& arg : m->GetArguments()) {
1464 auto it = argument_names.find(arg->GetName());
1465 if (it != argument_names.end()) {
1466 AIDL_ERROR(m) << "method '" << m->GetName() << "' has duplicate argument name '"
1467 << arg->GetName() << "'";
1468 return false;
1469 }
1470 argument_names.insert(arg->GetName());
1471
1472 if (!arg->GetType().CheckValid(typenames)) {
1473 return false;
1474 }
1475
Jeongik Cha649e8a72020-03-27 17:47:40 +09001476 // TODO(b/156872582): Support it when ParcelableHolder supports every backend.
1477 if (arg->GetType().GetName() == "ParcelableHolder") {
1478 AIDL_ERROR(arg) << "ParcelableHolder cannot be an argument type";
1479 return false;
1480 }
Steven Morelandacd53472018-12-14 10:17:26 -08001481 if (m->IsOneway() && arg->IsOut()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001482 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot have out parameters";
1483 return false;
1484 }
Jooyung Han15fd6c62020-10-23 13:54:46 +09001485
1486 const auto [can_be_out, type_aspect] = typenames.CanBeOutParameter(arg->GetType());
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001487 if (!arg->DirectionWasSpecified() && can_be_out) {
Jooyung Han965e31d2020-11-27 12:30:16 +09001488 AIDL_ERROR(arg) << "'" << arg->GetType().Signature()
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001489 << "' can be an out type, so you must declare it as in, out, or inout.";
1490 return false;
1491 }
1492
1493 if (arg->GetDirection() != AidlArgument::IN_DIR && !can_be_out) {
Jooyung Han15fd6c62020-10-23 13:54:46 +09001494 AIDL_ERROR(arg) << "'" << arg->GetName() << "' can't be an " << arg->GetDirectionSpecifier()
1495 << " parameter because " << type_aspect << " can only be an in parameter.";
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001496 return false;
1497 }
1498
1499 // check that the name doesn't match a keyword
Jeongik Cha997281d2020-01-16 15:23:59 +09001500 if (IsJavaKeyword(arg->GetName().c_str())) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001501 AIDL_ERROR(arg) << "Argument name is a Java or aidl keyword";
1502 return false;
1503 }
1504
1505 // Reserve a namespace for internal use
1506 if (android::base::StartsWith(arg->GetName(), "_aidl")) {
1507 AIDL_ERROR(arg) << "Argument name cannot begin with '_aidl'";
1508 return false;
1509 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001510 }
1511
1512 auto it = method_names.find(m->GetName());
1513 // prevent duplicate methods
1514 if (it == method_names.end()) {
1515 method_names[m->GetName()] = m.get();
1516 } else {
1517 AIDL_ERROR(m) << "attempt to redefine method " << m->GetName() << ":";
1518 AIDL_ERROR(it->second) << "previously defined here.";
1519 return false;
1520 }
1521
Paul Trautrimb77048c2020-01-21 16:39:32 +09001522 static set<string> reserved_methods{"asBinder()", "getInterfaceHash()", "getInterfaceVersion()",
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001523 "getTransactionName(int)"};
1524
1525 if (reserved_methods.find(m->Signature()) != reserved_methods.end()) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001526 AIDL_ERROR(m) << " method " << m->Signature() << " is reserved for internal use.";
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001527 return false;
1528 }
1529 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001530
1531 bool success = true;
1532 set<string> constant_names;
Jooyung Han3f347ca2020-12-01 12:41:50 +09001533 for (const auto& constant : GetConstantDeclarations()) {
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001534 if (constant_names.count(constant->GetName()) > 0) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001535 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001536 success = false;
1537 }
1538 constant_names.insert(constant->GetName());
1539 success = success && constant->CheckValid(typenames);
1540 }
1541
Jooyung Han888c5bc2020-12-22 17:28:47 +09001542 if (auto name = GetName(); name.size() < 1 || name[0] != 'I') {
1543 diag.Report(GetLocation(), DiagnosticID::interface_name)
1544 << "Interface names should start with I.";
1545 }
1546
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001547 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001548}
1549
Jiyong Park27fd7fd2020-08-27 16:25:09 +09001550std::string AidlInterface::GetDescriptor() const {
1551 std::string annotatedDescriptor = AidlAnnotatable::GetDescriptor();
1552 if (annotatedDescriptor != "") {
1553 return annotatedDescriptor;
1554 }
1555 return GetCanonicalName();
1556}
1557
Steven Moreland46e9da82018-07-27 15:45:29 -07001558AidlImport::AidlImport(const AidlLocation& location, const std::string& needed_class)
1559 : AidlNode(location), needed_class_(needed_class) {}
Jooyung Han29813842020-12-08 01:28:03 +09001560
1561// Resolves unresolved type name to fully qualified typename to import
1562// case #1: SimpleName --> import p.SimpleName
1563// case #2: Outer.Inner --> import p.Outer
1564// case #3: p.SimpleName --> (as is)
1565std::optional<std::string> AidlDocument::ResolveName(const std::string& unresolved_name) const {
1566 std::string canonical_name;
1567 const auto first_dot = unresolved_name.find_first_of('.');
1568 const std::string class_name =
1569 (first_dot == std::string::npos) ? unresolved_name : unresolved_name.substr(0, first_dot);
1570 for (const auto& import : Imports()) {
1571 const auto& fq_name = import->GetNeededClass();
1572 const auto last_dot = fq_name.find_last_of('.');
1573 const std::string imported_type_name =
1574 (last_dot == std::string::npos) ? fq_name : fq_name.substr(last_dot + 1);
1575 if (imported_type_name == class_name) {
1576 if (canonical_name != "" && canonical_name != fq_name) {
1577 AIDL_ERROR(import) << "Ambiguous type: " << canonical_name << " vs. " << fq_name;
1578 return {};
1579 }
1580 canonical_name = fq_name;
1581 }
1582 }
1583 // if not found, use unresolved_name as it is
1584 if (canonical_name == "") {
1585 return unresolved_name;
1586 }
1587 return canonical_name;
Steven Moreland26318532020-12-23 20:08:36 +00001588}