blob: 3b30ee603c5a2c752865004d9074e2ffe5aa2c78 [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>
Thiébaud Weksteen9ab59122021-09-20 09:37:38 +020024
Jiyong Park68bc77a2018-07-19 19:00:45 +090025#include <algorithm>
Jiyong Park1deecc32018-07-17 01:14:41 +090026#include <iostream>
Jiyong Park68bc77a2018-07-19 19:00:45 +090027#include <set>
28#include <sstream>
Casey Dahlindd691812015-09-09 17:59:06 -070029#include <string>
Jiyong Park1deecc32018-07-17 01:14:41 +090030#include <utility>
Christopher Wileyf690be52015-09-14 15:19:10 -070031
Steven Moreland1c4ba202018-08-09 10:49:54 -070032#include <android-base/parsedouble.h>
Roshan Pius9d7810a2016-07-28 08:57:50 -070033#include <android-base/parseint.h>
Thiébaud Weksteen9ab59122021-09-20 09:37:38 +020034#include <android-base/result.h>
Elliott Hughes0a620672015-12-04 13:53:18 -080035#include <android-base/strings.h>
Christopher Wileyd76067c2015-10-19 17:00:13 -070036
Thiébaud Weksteen9ab59122021-09-20 09:37:38 +020037#include "aidl.h"
Steven Moreland21780812020-09-11 01:29:45 +000038#include "aidl_language_y.h"
Jooyung Hand4fe00e2021-01-11 16:21:53 +090039#include "comments.h"
Christopher Wiley4a2884b2015-10-07 11:27:45 -070040#include "logging.h"
Thiébaud Weksteen5a4db212021-09-02 17:09:34 +020041#include "permission/parser.h"
Adam Lesinskiffa16862014-01-23 18:17:42 -080042
Casey Dahlin07b9dde2015-09-10 19:13:49 -070043#ifdef _WIN32
44int isatty(int fd)
45{
46 return (fd == 0);
47}
48#endif
49
Christopher Wiley4a2884b2015-10-07 11:27:45 -070050using android::aidl::IoDelegate;
Thiébaud Weksteen9ab59122021-09-20 09:37:38 +020051using android::base::Error;
Christopher Wileyd76067c2015-10-19 17:00:13 -070052using android::base::Join;
Thiébaud Weksteen9ab59122021-09-20 09:37:38 +020053using android::base::Result;
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -080054using android::base::Split;
Casey Dahlindd691812015-09-09 17:59:06 -070055using std::cerr;
Jiyong Park1deecc32018-07-17 01:14:41 +090056using std::pair;
Jiyong Park68bc77a2018-07-19 19:00:45 +090057using std::set;
Christopher Wiley4a2884b2015-10-07 11:27:45 -070058using std::string;
59using std::unique_ptr;
Jiyong Parkccf00f82018-07-17 01:39:23 +090060using std::vector;
Adam Lesinskiffa16862014-01-23 18:17:42 -080061
Jeongik Cha047c5ee2019-08-07 23:16:49 +090062namespace {
Jeongik Cha997281d2020-01-16 15:23:59 +090063bool IsJavaKeyword(const char* str) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +090064 static const std::vector<std::string> kJavaKeywords{
65 "abstract", "assert", "boolean", "break", "byte", "case", "catch",
66 "char", "class", "const", "continue", "default", "do", "double",
67 "else", "enum", "extends", "final", "finally", "float", "for",
68 "goto", "if", "implements", "import", "instanceof", "int", "interface",
69 "long", "native", "new", "package", "private", "protected", "public",
70 "return", "short", "static", "strictfp", "super", "switch", "synchronized",
71 "this", "throw", "throws", "transient", "try", "void", "volatile",
72 "while", "true", "false", "null",
73 };
74 return std::find(kJavaKeywords.begin(), kJavaKeywords.end(), str) != kJavaKeywords.end();
75}
76} // namespace
77
Jooyung Han8451a202021-01-16 03:07:06 +090078AidlNode::AidlNode(const AidlLocation& location, const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +090079 : location_(location), comments_(comments) {}
Steven Moreland46e9da82018-07-27 15:45:29 -070080
Mathew Inwoodadb74672019-11-29 14:01:53 +000081std::string AidlNode::PrintLine() const {
Andrei Onea8714b022019-02-01 18:55:54 +000082 std::stringstream ss;
83 ss << location_.file_ << ":" << location_.begin_.line;
84 return ss.str();
85}
86
Mathew Inwoodadb74672019-11-29 14:01:53 +000087std::string AidlNode::PrintLocation() const {
88 std::stringstream ss;
89 ss << location_.file_ << ":" << location_.begin_.line << ":" << location_.begin_.column << ":"
90 << location_.end_.line << ":" << location_.end_.column;
91 return ss.str();
92}
93
Jooyung Han8451a202021-01-16 03:07:06 +090094static const AidlTypeSpecifier kStringType{AIDL_LOCATION_HERE, "String", false, nullptr,
95 Comments{}};
96static const AidlTypeSpecifier kStringArrayType{AIDL_LOCATION_HERE, "String", true, nullptr,
97 Comments{}};
98static const AidlTypeSpecifier kIntType{AIDL_LOCATION_HERE, "int", false, nullptr, Comments{}};
99static const AidlTypeSpecifier kLongType{AIDL_LOCATION_HERE, "long", false, nullptr, Comments{}};
100static const AidlTypeSpecifier kBooleanType{AIDL_LOCATION_HERE, "boolean", false, nullptr,
101 Comments{}};
Jooyung Han5c2fcae2020-12-26 00:04:39 +0900102
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700103const std::vector<AidlAnnotation::Schema>& AidlAnnotation::AllSchemas() {
104 static const std::vector<Schema> kSchemas{
Jooyung Han01720ed2021-08-13 07:46:07 +0900105 {AidlAnnotation::Type::NULLABLE,
106 "nullable",
107 CONTEXT_TYPE_SPECIFIER,
108 {{"heap", kBooleanType}}},
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900109 {AidlAnnotation::Type::UTF8_IN_CPP, "utf8InCpp", CONTEXT_TYPE_SPECIFIER, {}},
110 {AidlAnnotation::Type::SENSITIVE_DATA, "SensitiveData", CONTEXT_TYPE_INTERFACE, {}},
111 {AidlAnnotation::Type::VINTF_STABILITY, "VintfStability", CONTEXT_TYPE, {}},
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700112 {AidlAnnotation::Type::UNSUPPORTED_APP_USAGE,
113 "UnsupportedAppUsage",
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900114 CONTEXT_TYPE | CONTEXT_MEMBER,
Jooyung Han5c2fcae2020-12-26 00:04:39 +0900115 {{"expectedSignature", kStringType},
116 {"implicitMember", kStringType},
117 {"maxTargetSdk", kIntType},
118 {"publicAlternatives", kStringType},
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900119 {"trackingBug", kLongType}}},
120 {AidlAnnotation::Type::JAVA_STABLE_PARCELABLE,
121 "JavaOnlyStableParcelable",
122 CONTEXT_TYPE_UNSTRUCTURED_PARCELABLE,
123 {}},
124 {AidlAnnotation::Type::HIDE, "Hide", CONTEXT_TYPE | CONTEXT_MEMBER, {}},
125 {AidlAnnotation::Type::BACKING,
126 "Backing",
127 CONTEXT_TYPE_ENUM,
128 {{"type", kStringType, /* required= */ true}}},
Jooyung Han5721a232020-12-24 04:34:55 +0900129 {AidlAnnotation::Type::JAVA_PASSTHROUGH,
130 "JavaPassthrough",
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900131 CONTEXT_ALL,
132 {{"annotation", kStringType, /* required= */ true}},
133 /* repeatable= */ true},
Jiyong Park9aa3d042020-12-04 23:30:02 +0900134 {AidlAnnotation::Type::JAVA_DERIVE,
Jooyung Han5721a232020-12-24 04:34:55 +0900135 "JavaDerive",
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900136 CONTEXT_TYPE_STRUCTURED_PARCELABLE | CONTEXT_TYPE_UNION,
137 {{"toString", kBooleanType}, {"equals", kBooleanType}}},
138 {AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE,
139 "JavaOnlyImmutable",
140 CONTEXT_TYPE_STRUCTURED_PARCELABLE | CONTEXT_TYPE_UNION |
141 CONTEXT_TYPE_UNSTRUCTURED_PARCELABLE,
142 {}},
143 {AidlAnnotation::Type::FIXED_SIZE, "FixedSize", CONTEXT_TYPE_STRUCTURED_PARCELABLE, {}},
144 {AidlAnnotation::Type::DESCRIPTOR,
145 "Descriptor",
146 CONTEXT_TYPE_INTERFACE,
147 {{"value", kStringType, /* required= */ true}}},
Andrei Homescue61feb52020-08-18 15:44:24 -0700148 {AidlAnnotation::Type::RUST_DERIVE,
149 "RustDerive",
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900150 CONTEXT_TYPE_STRUCTURED_PARCELABLE | CONTEXT_TYPE_UNION,
Jooyung Han5c2fcae2020-12-26 00:04:39 +0900151 {{"Copy", kBooleanType},
152 {"Clone", kBooleanType},
153 {"PartialOrd", kBooleanType},
154 {"Ord", kBooleanType},
155 {"PartialEq", kBooleanType},
156 {"Eq", kBooleanType},
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900157 {"Hash", kBooleanType}}},
Jooyung Hanf8dbbcc2020-12-26 03:05:55 +0900158 {AidlAnnotation::Type::SUPPRESS_WARNINGS,
159 "SuppressWarnings",
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900160 CONTEXT_TYPE | CONTEXT_MEMBER,
161 {{"value", kStringArrayType, /* required= */ true}}},
Thiébaud Weksteen9ab59122021-09-20 09:37:38 +0200162 {AidlAnnotation::Type::ENFORCE,
163 "Enforce",
164 CONTEXT_METHOD,
165 {{"condition", kStringType, /* required= */ true}}},
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900166 };
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700167 return kSchemas;
168}
Jiyong Park68bc77a2018-07-19 19:00:45 +0900169
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700170std::string AidlAnnotation::TypeToString(Type type) {
171 for (const Schema& schema : AllSchemas()) {
172 if (type == schema.type) return schema.name;
173 }
174 AIDL_FATAL(AIDL_LOCATION_HERE) << "Unrecognized type: " << static_cast<size_t>(type);
175 __builtin_unreachable();
176}
Andrei Onea9445fc62019-06-27 18:11:59 +0100177
Jooyung Han442cacf2021-09-13 17:44:56 +0900178std::unique_ptr<AidlAnnotation> AidlAnnotation::Parse(
Andrei Onea9445fc62019-06-27 18:11:59 +0100179 const AidlLocation& location, const string& name,
Jooyung Han442cacf2021-09-13 17:44:56 +0900180 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameter_list,
Jooyung Han8451a202021-01-16 03:07:06 +0900181 const Comments& comments) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700182 const Schema* schema = nullptr;
183 for (const Schema& a_schema : AllSchemas()) {
184 if (a_schema.name == name) {
185 schema = &a_schema;
186 }
187 }
188
189 if (schema == nullptr) {
Jiyong Park68bc77a2018-07-19 19:00:45 +0900190 std::ostringstream stream;
Steven Moreland46e9da82018-07-27 15:45:29 -0700191 stream << "'" << name << "' is not a recognized annotation. ";
Jiyong Park68bc77a2018-07-19 19:00:45 +0900192 stream << "It must be one of:";
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700193 for (const Schema& s : AllSchemas()) {
194 stream << " " << s.name;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900195 }
196 stream << ".";
Steven Moreland46e9da82018-07-27 15:45:29 -0700197 AIDL_ERROR(location) << stream.str();
Jooyung Han442cacf2021-09-13 17:44:56 +0900198 return {};
Andrei Onea9445fc62019-06-27 18:11:59 +0100199 }
200
Jooyung Han442cacf2021-09-13 17:44:56 +0900201 return std::unique_ptr<AidlAnnotation>(
202 new AidlAnnotation(location, *schema, std::move(parameter_list), comments));
Jiyong Park68bc77a2018-07-19 19:00:45 +0900203}
204
Jooyung Han442cacf2021-09-13 17:44:56 +0900205AidlAnnotation::AidlAnnotation(const AidlLocation& location, const Schema& schema,
206 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters,
207 const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +0900208 : AidlNode(location, comments), schema_(schema), parameters_(std::move(parameters)) {}
Andrei Onea9445fc62019-06-27 18:11:59 +0100209
Jooyung Hanc5688f72021-01-05 15:41:48 +0900210struct ConstReferenceFinder : AidlVisitor {
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900211 const AidlConstantReference* found;
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900212 void Visit(const AidlConstantReference& ref) override {
Jooyung Han690f5842020-12-04 13:02:04 +0900213 if (!found) found = &ref;
214 }
Jooyung Hanc5688f72021-01-05 15:41:48 +0900215 static const AidlConstantReference* Find(const AidlConstantValue& c) {
216 ConstReferenceFinder finder;
217 VisitTopDown(finder, c);
218 return finder.found;
219 }
Jooyung Han690f5842020-12-04 13:02:04 +0900220};
221
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900222// Checks if annotation complies with the schema
223// - every parameter is known and has well-typed value.
224// - every required parameter is present.
Andrei Onea9445fc62019-06-27 18:11:59 +0100225bool AidlAnnotation::CheckValid() const {
Andrei Onea9445fc62019-06-27 18:11:59 +0100226 for (const auto& name_and_param : parameters_) {
227 const std::string& param_name = name_and_param.first;
228 const std::shared_ptr<AidlConstantValue>& param = name_and_param.second;
Jooyung Han690f5842020-12-04 13:02:04 +0900229
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900230 const ParamType* param_type = schema_.ParamType(param_name);
231 if (!param_type) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100232 std::ostringstream stream;
233 stream << "Parameter " << param_name << " not supported ";
Devin Mooredecaf292020-04-30 09:16:40 -0700234 stream << "for annotation " << GetName() << ". ";
Andrei Onea9445fc62019-06-27 18:11:59 +0100235 stream << "It must be one of:";
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900236 for (const auto& param : schema_.parameters) {
237 stream << " " << param.name;
Andrei Onea9445fc62019-06-27 18:11:59 +0100238 }
239 AIDL_ERROR(this) << stream.str();
240 return false;
241 }
Jooyung Han690f5842020-12-04 13:02:04 +0900242
Jooyung Hanc5688f72021-01-05 15:41:48 +0900243 const auto& found = ConstReferenceFinder::Find(*param);
244 if (found) {
245 AIDL_ERROR(found) << "Value must be a constant expression but contains reference to "
246 << found->GetFieldName() << ".";
Jooyung Han690f5842020-12-04 13:02:04 +0900247 return false;
248 }
249
250 if (!param->CheckValid()) {
251 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
252 << GetName() << ".";
253 return false;
254 }
255
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900256 const std::string param_value =
257 param->ValueString(param_type->type, AidlConstantValueDecorator);
Andrei Onea9445fc62019-06-27 18:11:59 +0100258 // Assume error on empty string.
259 if (param_value == "") {
260 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
261 << GetName() << ".";
262 return false;
263 }
264 }
Jooyung Han5721a232020-12-24 04:34:55 +0900265 bool success = true;
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900266 for (const auto& param : schema_.parameters) {
267 if (param.required && parameters_.count(param.name) == 0) {
268 AIDL_ERROR(this) << "Missing '" << param.name << "' on @" << GetName() << ".";
Jooyung Han5721a232020-12-24 04:34:55 +0900269 success = false;
270 }
271 }
Thiébaud Weksteen9ab59122021-09-20 09:37:38 +0200272 if (!success) {
273 return false;
274 }
275 // For @Enforce annotations, validates the expression.
276 if (schema_.type == AidlAnnotation::Type::ENFORCE) {
277 auto expr = EnforceExpression();
278 if (!expr.ok()) {
279 AIDL_ERROR(this) << "Unable to parse @Enforce annotation: " << expr.error();
280 return false;
281 }
282 }
283 return true;
284}
285
286Result<unique_ptr<perm::Expression>> AidlAnnotation::EnforceExpression() const {
287 auto perm_expr = ParamValue<std::string>("condition");
288 if (perm_expr.has_value()) {
289 return perm::Parser::Parse(perm_expr.value());
290 }
291 return Error() << "No condition parameter for @Enforce";
Andrei Onea9445fc62019-06-27 18:11:59 +0100292}
293
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900294// Checks if the annotation is applicable to the current context.
295// For example, annotations like @VintfStability, @FixedSize is not applicable to AidlTypeSpecifier
296// nodes.
297bool AidlAnnotation::CheckContext(TargetContext context) const {
298 if (schema_.target_context & static_cast<uint32_t>(context)) {
299 return true;
300 }
301 const static map<TargetContext, string> context_name_map{
302 {CONTEXT_TYPE_INTERFACE, "interface"},
303 {CONTEXT_TYPE_ENUM, "enum"},
304 {CONTEXT_TYPE_STRUCTURED_PARCELABLE, "structured parcelable"},
305 {CONTEXT_TYPE_UNION, "union"},
306 {CONTEXT_TYPE_UNSTRUCTURED_PARCELABLE, "parcelable"},
307 {CONTEXT_CONST, "constant"},
308 {CONTEXT_FIELD, "field"},
309 {CONTEXT_METHOD, "method"},
310 {CONTEXT_TYPE_SPECIFIER, "type"},
311 };
312 vector<string> available;
313 for (const auto& [context, name] : context_name_map) {
314 if (schema_.target_context & context) {
315 available.push_back(name);
316 }
317 }
318 AIDL_ERROR(this) << "@" << GetName() << " is not available. It can annotate {"
319 << Join(available, ", ") << "}.";
320 return false;
321}
322
Andrei Onea9445fc62019-06-27 18:11:59 +0100323std::map<std::string, std::string> AidlAnnotation::AnnotationParams(
324 const ConstantValueDecorator& decorator) const {
325 std::map<std::string, std::string> raw_params;
Andrei Onea9445fc62019-06-27 18:11:59 +0100326 for (const auto& name_and_param : parameters_) {
327 const std::string& param_name = name_and_param.first;
328 const std::shared_ptr<AidlConstantValue>& param = name_and_param.second;
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900329 const ParamType* param_type = schema_.ParamType(param_name);
330 AIDL_FATAL_IF(!param_type, this);
331 raw_params.emplace(param_name, param->ValueString(param_type->type, decorator));
Andrei Onea9445fc62019-06-27 18:11:59 +0100332 }
333 return raw_params;
334}
Steven Moreland46e9da82018-07-27 15:45:29 -0700335
Jooyung Han965e31d2020-11-27 12:30:16 +0900336std::string AidlAnnotation::ToString() const {
Daniel Norman37d43dd2019-09-09 17:22:34 -0700337 if (parameters_.empty()) {
338 return "@" + GetName();
339 } else {
340 vector<string> param_strings;
Jooyung Han965e31d2020-11-27 12:30:16 +0900341 for (const auto& [name, value] : AnnotationParams(AidlConstantValueDecorator)) {
Daniel Norman37d43dd2019-09-09 17:22:34 -0700342 param_strings.emplace_back(name + "=" + value);
343 }
344 return "@" + GetName() + "(" + Join(param_strings, ", ") + ")";
345 }
346}
347
Jooyung Hanc5688f72021-01-05 15:41:48 +0900348void AidlAnnotation::TraverseChildren(std::function<void(const AidlNode&)> traverse) const {
349 for (const auto& [name, value] : parameters_) {
350 (void)name;
351 traverse(*value);
352 }
353}
354
Andrei Onea9445fc62019-06-27 18:11:59 +0100355static const AidlAnnotation* GetAnnotation(const vector<AidlAnnotation>& annotations,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700356 AidlAnnotation::Type type) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100357 for (const auto& a : annotations) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700358 if (a.GetType() == type) {
Jooyung Hand902a972020-10-23 17:32:44 +0900359 AIDL_FATAL_IF(a.Repeatable(), a)
360 << "Trying to get a single annotation when it is repeatable.";
Andrei Onea9445fc62019-06-27 18:11:59 +0100361 return &a;
362 }
363 }
364 return nullptr;
365}
366
Jooyung Han8451a202021-01-16 03:07:06 +0900367AidlAnnotatable::AidlAnnotatable(const AidlLocation& location, const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +0900368 : AidlCommentable(location, comments) {}
Steven Moreland46e9da82018-07-27 15:45:29 -0700369
Jiyong Park68bc77a2018-07-19 19:00:45 +0900370bool AidlAnnotatable::IsNullable() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700371 return GetAnnotation(annotations_, AidlAnnotation::Type::NULLABLE);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900372}
373
Jooyung Han01720ed2021-08-13 07:46:07 +0900374bool AidlAnnotatable::IsHeapNullable() const {
375 auto annot = GetAnnotation(annotations_, AidlAnnotation::Type::NULLABLE);
376 if (annot) {
377 return annot->ParamValue<bool>("heap").value_or(false);
378 }
379 return false;
380}
381
Jiyong Park68bc77a2018-07-19 19:00:45 +0900382bool AidlAnnotatable::IsUtf8InCpp() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700383 return GetAnnotation(annotations_, AidlAnnotation::Type::UTF8_IN_CPP);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900384}
385
Steven Morelanda7764e52020-10-27 17:29:29 +0000386bool AidlAnnotatable::IsSensitiveData() const {
387 return GetAnnotation(annotations_, AidlAnnotation::Type::SENSITIVE_DATA);
388}
389
Steven Morelanda57d0a62019-07-30 09:41:14 -0700390bool AidlAnnotatable::IsVintfStability() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700391 return GetAnnotation(annotations_, AidlAnnotation::Type::VINTF_STABILITY);
Steven Morelanda57d0a62019-07-30 09:41:14 -0700392}
393
Jeongik Chad0a10272020-08-06 16:33:36 +0900394bool AidlAnnotatable::IsJavaOnlyImmutable() const {
395 return GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE);
Jeongik Cha36f76c32020-07-28 00:25:52 +0900396}
397
Devin Moorec7e47a32020-08-07 10:55:25 -0700398bool AidlAnnotatable::IsFixedSize() const {
399 return GetAnnotation(annotations_, AidlAnnotation::Type::FIXED_SIZE);
400}
401
Andrei Onea9445fc62019-06-27 18:11:59 +0100402const AidlAnnotation* AidlAnnotatable::UnsupportedAppUsage() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700403 return GetAnnotation(annotations_, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE);
Jiyong Parka6605ab2018-11-11 14:30:21 +0900404}
405
Andrei Homescue61feb52020-08-18 15:44:24 -0700406const AidlAnnotation* AidlAnnotatable::RustDerive() const {
407 return GetAnnotation(annotations_, AidlAnnotation::Type::RUST_DERIVE);
408}
409
Jooyung Han672557b2020-12-24 05:18:00 +0900410const AidlAnnotation* AidlAnnotatable::BackingType() const {
411 return GetAnnotation(annotations_, AidlAnnotation::Type::BACKING);
Daniel Norman85aed542019-08-21 12:01:14 -0700412}
413
Jooyung Hanf8dbbcc2020-12-26 03:05:55 +0900414std::vector<std::string> AidlAnnotatable::SuppressWarnings() const {
415 auto annot = GetAnnotation(annotations_, AidlAnnotation::Type::SUPPRESS_WARNINGS);
416 if (annot) {
417 auto names = annot->ParamValue<std::vector<std::string>>("value");
418 AIDL_FATAL_IF(!names.has_value(), this);
419 return std::move(names.value());
420 }
421 return {};
422}
423
Thiébaud Weksteen5a4db212021-09-02 17:09:34 +0200424// Parses the @Enforce annotation expression.
425std::unique_ptr<perm::Expression> AidlAnnotatable::EnforceExpression(
426 const AidlNode& context) const {
427 auto annot = GetAnnotation(annotations_, AidlAnnotation::Type::ENFORCE);
428 if (annot) {
Thiébaud Weksteen9ab59122021-09-20 09:37:38 +0200429 auto perm_expr = annot->EnforceExpression();
430 if (!perm_expr.ok()) {
431 // This should have been caught during validation.
432 AIDL_FATAL(context) << "Unable to parse @Enforce annotation: " << perm_expr.error();
Thiébaud Weksteen5a4db212021-09-02 17:09:34 +0200433 }
Thiébaud Weksteen9ab59122021-09-20 09:37:38 +0200434 return std::move(perm_expr.value());
Thiébaud Weksteen5a4db212021-09-02 17:09:34 +0200435 }
436 return {};
437}
438
Jeongik Cha88f95a82020-01-15 13:02:16 +0900439bool AidlAnnotatable::IsStableApiParcelable(Options::Language lang) const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700440 return lang == Options::Language::JAVA &&
441 GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_STABLE_PARCELABLE);
Jeongik Cha82317dd2019-02-27 20:26:11 +0900442}
443
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800444bool AidlAnnotatable::IsHide() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700445 return GetAnnotation(annotations_, AidlAnnotation::Type::HIDE);
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800446}
447
Jooyung Han829ec7c2020-12-02 12:07:36 +0900448bool AidlAnnotatable::JavaDerive(const std::string& method) const {
449 auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_DERIVE);
450 if (annotation != nullptr) {
Jooyung Hanb3c77ed2020-12-26 02:02:45 +0900451 return annotation->ParamValue<bool>(method).value_or(false);
Jooyung Han829ec7c2020-12-02 12:07:36 +0900452 }
453 return false;
Jiyong Park43113fb2020-07-20 16:26:19 +0900454}
455
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900456std::string AidlAnnotatable::GetDescriptor() const {
457 auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::DESCRIPTOR);
458 if (annotation != nullptr) {
Jooyung Hanb3c77ed2020-12-26 02:02:45 +0900459 return annotation->ParamValue<std::string>("value").value();
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900460 }
461 return "";
462}
463
Devin Moore24f68572020-02-26 13:20:59 -0800464bool AidlAnnotatable::CheckValid(const AidlTypenames&) const {
Andrei Onea9445fc62019-06-27 18:11:59 +0100465 for (const auto& annotation : GetAnnotations()) {
Jooyung Hand902a972020-10-23 17:32:44 +0900466 if (!annotation.CheckValid()) {
467 return false;
468 }
469 }
470
471 std::map<AidlAnnotation::Type, AidlLocation> declared;
472 for (const auto& annotation : GetAnnotations()) {
473 const auto& [iter, inserted] = declared.emplace(annotation.GetType(), annotation.GetLocation());
474 if (!inserted && !annotation.Repeatable()) {
475 AIDL_ERROR(this) << "'" << annotation.GetName()
476 << "' is repeated, but not allowed. Previous location: " << iter->second;
477 return false;
478 }
Andrei Onea9445fc62019-06-27 18:11:59 +0100479 }
Steven Morelanda57d0a62019-07-30 09:41:14 -0700480
Andrei Onea9445fc62019-06-27 18:11:59 +0100481 return true;
482}
483
Jiyong Park68bc77a2018-07-19 19:00:45 +0900484string AidlAnnotatable::ToString() const {
485 vector<string> ret;
486 for (const auto& a : annotations_) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900487 ret.emplace_back(a.ToString());
Jiyong Park68bc77a2018-07-19 19:00:45 +0900488 }
489 std::sort(ret.begin(), ret.end());
490 return Join(ret, " ");
491}
492
Steven Moreland46e9da82018-07-27 15:45:29 -0700493AidlTypeSpecifier::AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name,
494 bool is_array,
Jiyong Park1deecc32018-07-17 01:14:41 +0900495 vector<unique_ptr<AidlTypeSpecifier>>* type_params,
Jooyung Han8451a202021-01-16 03:07:06 +0900496 const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +0900497 : AidlAnnotatable(location, comments),
Jeongik Chadf76dc72019-11-28 00:08:47 +0900498 AidlParameterizable<unique_ptr<AidlTypeSpecifier>>(type_params),
Steven Moreland46e9da82018-07-27 15:45:29 -0700499 unresolved_name_(unresolved_name),
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700500 is_array_(is_array),
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900501 split_name_(Split(unresolved_name, ".")) {}
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700502
Jooyung Hand2fa0232020-10-19 02:51:41 +0900503const AidlTypeSpecifier& AidlTypeSpecifier::ArrayBase() const {
Steven Moreland3f658cf2018-08-20 13:40:54 -0700504 AIDL_FATAL_IF(!is_array_, this);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900505 // Declaring array of generic type cannot happen, it is grammar error.
506 AIDL_FATAL_IF(IsGeneric(), this);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700507
Jooyung Hand2fa0232020-10-19 02:51:41 +0900508 if (!array_base_) {
509 array_base_.reset(new AidlTypeSpecifier(*this));
510 array_base_->is_array_ = false;
511 }
512 return *array_base_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700513}
514
Jooyung Han965e31d2020-11-27 12:30:16 +0900515string AidlTypeSpecifier::Signature() const {
Jiyong Park1deecc32018-07-17 01:14:41 +0900516 string ret = GetName();
517 if (IsGeneric()) {
518 vector<string> arg_names;
519 for (const auto& ta : GetTypeParameters()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900520 arg_names.emplace_back(ta->Signature());
Jiyong Parkccf00f82018-07-17 01:39:23 +0900521 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900522 ret += "<" + Join(arg_names, ",") + ">";
Jiyong Parkccf00f82018-07-17 01:39:23 +0900523 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900524 if (IsArray()) {
525 ret += "[]";
526 }
527 return ret;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900528}
529
Jooyung Han965e31d2020-11-27 12:30:16 +0900530string AidlTypeSpecifier::ToString() const {
531 string ret = Signature();
Jiyong Park02da7422018-07-16 16:00:26 +0900532 string annotations = AidlAnnotatable::ToString();
533 if (annotations != "") {
534 ret = annotations + " " + ret;
535 }
536 return ret;
537}
538
Jooyung Han13f1fa52021-06-11 18:06:12 +0900539// When `scope` is specified, name is resolved first based on it.
540// `scope` can be null for built-in types and fully-qualified types.
541bool AidlTypeSpecifier::Resolve(const AidlTypenames& typenames, const AidlScope* scope) {
Steven Moreland21780812020-09-11 01:29:45 +0000542 AIDL_FATAL_IF(IsResolved(), this);
Jooyung Han13f1fa52021-06-11 18:06:12 +0900543 std::string name = unresolved_name_;
544 if (scope) {
545 name = scope->ResolveName(name);
546 }
547 AidlTypenames::ResolvedTypename result = typenames.ResolveTypename(name);
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700548 if (result.is_resolved) {
549 fully_qualified_name_ = result.canonical_name;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900550 split_name_ = Split(fully_qualified_name_, ".");
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900551 defined_type_ = result.defined_type;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900552 }
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700553 return result.is_resolved;
Casey Dahlin70078e62015-09-30 17:01:30 -0700554}
555
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900556const AidlDefinedType* AidlTypeSpecifier::GetDefinedType() const {
557 return defined_type_;
558}
559
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900560bool AidlTypeSpecifier::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -0800561 if (!AidlAnnotatable::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100562 return false;
563 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900564 if (IsGeneric()) {
Jooyung Hand09a21d2021-02-15 18:56:55 +0900565 const auto& types = GetTypeParameters();
566 for (const auto& arg : types) {
567 if (!arg->CheckValid(typenames)) {
568 return false;
569 }
570 }
Jeongik Chae74c86d2019-12-12 16:54:03 +0900571
Jooyung Hand09a21d2021-02-15 18:56:55 +0900572 const string& type_name = GetName();
Jeongik Chae74c86d2019-12-12 16:54:03 +0900573 // TODO(b/136048684) Disallow to use primitive types only if it is List or Map.
574 if (type_name == "List" || type_name == "Map") {
Jooyung Hane87cdd02020-12-11 16:47:35 +0900575 if (std::any_of(types.begin(), types.end(), [&](auto& type_ptr) {
Jooyung Han1f35ef32021-02-15 19:08:05 +0900576 return !type_ptr->IsArray() &&
577 (typenames.GetEnumDeclaration(*type_ptr) ||
578 AidlTypenames::IsPrimitiveTypename(type_ptr->GetName()));
Jeongik Chae74c86d2019-12-12 16:54:03 +0900579 })) {
Devin Moore7b8d5c92020-03-17 14:14:08 -0700580 AIDL_ERROR(this) << "A generic type cannot have any primitive type parameters.";
Jeongik Chae74c86d2019-12-12 16:54:03 +0900581 return false;
582 }
583 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800584 const auto defined_type = typenames.TryGetDefinedType(type_name);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900585 const auto parameterizable =
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800586 defined_type != nullptr ? defined_type->AsParameterizable() : nullptr;
587 const bool is_user_defined_generic_type =
Jeongik Chadf76dc72019-11-28 00:08:47 +0900588 parameterizable != nullptr && parameterizable->IsGeneric();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800589 const size_t num_params = GetTypeParameters().size();
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900590 if (type_name == "List") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800591 if (num_params > 1) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900592 AIDL_ERROR(this) << "List can only have one type parameter, but got: '" << Signature()
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000593 << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900594 return false;
595 }
Jooyung Han55f96ad2020-12-13 10:08:33 +0900596 const AidlTypeSpecifier& contained_type = *GetTypeParameters()[0];
Jooyung Hancea89002021-02-15 17:04:04 +0900597 if (contained_type.IsArray()) {
598 AIDL_ERROR(this)
599 << "List of arrays is not supported. List<T> supports parcelable/union, String, "
600 "IBinder, and ParcelFileDescriptor.";
601 return false;
602 }
Jooyung Han55f96ad2020-12-13 10:08:33 +0900603 const string& contained_type_name = contained_type.GetName();
604 if (AidlTypenames::IsBuiltinTypename(contained_type_name)) {
605 if (contained_type_name != "String" && contained_type_name != "IBinder" &&
606 contained_type_name != "ParcelFileDescriptor") {
607 AIDL_ERROR(this) << "List<" << contained_type_name
608 << "> is not supported. List<T> supports parcelable/union, String, "
609 "IBinder, and ParcelFileDescriptor.";
610 return false;
611 }
612 } else { // Defined types
613 if (typenames.GetInterface(contained_type)) {
614 AIDL_ERROR(this) << "List<" << contained_type_name
615 << "> is not supported. List<T> supports parcelable/union, String, "
616 "IBinder, and ParcelFileDescriptor.";
617 return false;
618 }
619 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900620 } else if (type_name == "Map") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800621 if (num_params != 0 && num_params != 2) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900622 AIDL_ERROR(this) << "Map must have 0 or 2 type parameters, but got "
Jooyung Han965e31d2020-11-27 12:30:16 +0900623 << "'" << Signature() << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900624 return false;
625 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800626 if (num_params == 2) {
Jooyung Hanaab242a2021-02-15 19:01:15 +0900627 const string& key_type = GetTypeParameters()[0]->Signature();
Jeongik Chae48d9942020-01-02 17:39:00 +0900628 if (key_type != "String") {
629 AIDL_ERROR(this) << "The type of key in map must be String, but it is "
630 << "'" << key_type << "'";
631 return false;
632 }
633 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800634 } else if (is_user_defined_generic_type) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900635 const size_t allowed = parameterizable->GetTypeParameters().size();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800636 if (num_params != allowed) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900637 AIDL_ERROR(this) << type_name << " must have " << allowed << " type parameters, but got "
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800638 << num_params;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900639 return false;
640 }
641 } else {
642 AIDL_ERROR(this) << type_name << " is not a generic type.";
643 return false;
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900644 }
645 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900646
Steven Moreland11cb9452020-01-21 16:56:58 -0800647 const bool is_generic_string_list = GetName() == "List" && IsGeneric() &&
648 GetTypeParameters().size() == 1 &&
649 GetTypeParameters()[0]->GetName() == "String";
650 if (IsUtf8InCpp() && (GetName() != "String" && !is_generic_string_list)) {
651 AIDL_ERROR(this) << "@utf8InCpp can only be used on String, String[], and List<String>.";
652 return false;
653 }
654
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900655 if (GetName() == "void") {
656 if (IsArray() || IsNullable() || IsUtf8InCpp()) {
657 AIDL_ERROR(this) << "void type cannot be an array or nullable or utf8 string";
658 return false;
659 }
660 }
661
662 if (IsArray()) {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800663 const auto defined_type = typenames.TryGetDefinedType(GetName());
664 if (defined_type != nullptr && defined_type->AsInterface() != nullptr) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900665 AIDL_ERROR(this) << "Binder type cannot be an array";
666 return false;
667 }
Steven Moreland8042d2d2020-09-30 23:31:32 +0000668 if (GetName() == "ParcelableHolder") {
669 AIDL_ERROR(this) << "Arrays of ParcelableHolder are not supported.";
670 return false;
671 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900672 }
673
674 if (IsNullable()) {
675 if (AidlTypenames::IsPrimitiveTypename(GetName()) && !IsArray()) {
676 AIDL_ERROR(this) << "Primitive type cannot get nullable annotation";
677 return false;
678 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800679 const auto defined_type = typenames.TryGetDefinedType(GetName());
680 if (defined_type != nullptr && defined_type->AsEnumDeclaration() != nullptr && !IsArray()) {
Daniel Normanee8674f2019-09-20 16:07:00 -0700681 AIDL_ERROR(this) << "Enum type cannot get nullable annotation";
682 return false;
683 }
Jeongik Chaf6ec8982020-10-15 00:10:30 +0900684 if (GetName() == "ParcelableHolder") {
685 AIDL_ERROR(this) << "ParcelableHolder cannot be nullable.";
686 return false;
687 }
Jooyung Han01720ed2021-08-13 07:46:07 +0900688 if (IsHeapNullable()) {
689 if (!defined_type || IsArray() || !defined_type->AsParcelable()) {
690 AIDL_ERROR(this) << "@nullable(heap=true) is available to parcelables.";
691 return false;
692 }
693 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900694 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900695 return true;
696}
697
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900698std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type,
Steven Moreland860b1942018-08-16 14:59:28 -0700699 const std::string& raw_value) {
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900700 if (type.IsArray()) {
701 return raw_value;
702 }
703
704 if (auto defined_type = type.GetDefinedType(); defined_type) {
705 auto enum_type = defined_type->AsEnumDeclaration();
706 AIDL_FATAL_IF(!enum_type, type) << "Invalid type for \"" << raw_value << "\"";
707 return type.GetName() + "." + raw_value.substr(raw_value.find_last_of('.') + 1);
708 }
Steven Moreland860b1942018-08-16 14:59:28 -0700709 return raw_value;
710}
711
Steven Moreland46e9da82018-07-27 15:45:29 -0700712AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
713 AidlTypeSpecifier* type, const std::string& name)
Steven Moreland541788d2020-05-21 22:05:52 +0000714 : AidlVariableDeclaration(location, type, name, AidlConstantValue::Default(*type)) {
715 default_user_specified_ = false;
716}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700717
Steven Moreland46e9da82018-07-27 15:45:29 -0700718AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
719 AidlTypeSpecifier* type, const std::string& name,
720 AidlConstantValue* default_value)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900721 : AidlMember(location, type->GetComments()),
Steven Moreland541788d2020-05-21 22:05:52 +0000722 type_(type),
723 name_(name),
724 default_user_specified_(true),
725 default_value_(default_value) {}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700726
Jooyung Han53fb4242020-12-17 16:03:49 +0900727bool AidlVariableDeclaration::HasUsefulDefaultValue() const {
728 if (GetDefaultValue()) {
729 return true;
730 }
731 // null is accepted as a valid default value in all backends
732 if (GetType().IsNullable()) {
733 return true;
734 }
735 return false;
736}
737
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900738bool AidlVariableDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700739 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900740 valid &= type_->CheckValid(typenames);
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900741
Steven Moreland54be7bd2019-12-05 11:17:53 -0800742 if (type_->GetName() == "void") {
743 AIDL_ERROR(this) << "Declaration " << name_
744 << " is void, but declarations cannot be of void type.";
745 valid = false;
746 }
747
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900748 if (default_value_ == nullptr) return valid;
Steven Moreland25294322018-08-07 18:13:55 -0700749 valid &= default_value_->CheckValid();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700750
Steven Moreland25294322018-08-07 18:13:55 -0700751 if (!valid) return false;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700752
Steven Moreland860b1942018-08-16 14:59:28 -0700753 return !ValueString(AidlConstantValueDecorator).empty();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700754}
Steven Moreland5557f1c2018-07-02 13:50:23 -0700755
Jooyung Hanacae85d2020-10-28 16:39:09 +0900756string AidlVariableDeclaration::GetCapitalizedName() const {
757 AIDL_FATAL_IF(name_.size() <= 0, *this) << "Name can't be empty.";
758 string str = name_;
759 str[0] = static_cast<char>(toupper(str[0]));
760 return str;
761}
762
Steven Moreland5557f1c2018-07-02 13:50:23 -0700763string AidlVariableDeclaration::ToString() const {
Jooyung Han965e31d2020-11-27 12:30:16 +0900764 string ret = type_->ToString() + " " + name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000765 if (default_value_ != nullptr && default_user_specified_) {
Steven Moreland860b1942018-08-16 14:59:28 -0700766 ret += " = " + ValueString(AidlConstantValueDecorator);
Steven Moreland9ea10e32018-07-19 15:26:09 -0700767 }
768 return ret;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700769}
770
Jiyong Park02da7422018-07-16 16:00:26 +0900771string AidlVariableDeclaration::Signature() const {
772 return type_->Signature() + " " + name_;
773}
774
Steven Moreland860b1942018-08-16 14:59:28 -0700775std::string AidlVariableDeclaration::ValueString(const ConstantValueDecorator& decorator) const {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900776 if (default_value_ != nullptr) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700777 return default_value_->ValueString(GetType(), decorator);
Jiyong Parka468e2a2018-08-29 21:25:18 +0900778 } else {
779 return "";
780 }
Steven Moreland25294322018-08-07 18:13:55 -0700781}
782
Jooyung Hanc5688f72021-01-05 15:41:48 +0900783void AidlVariableDeclaration::TraverseChildren(
784 std::function<void(const AidlNode&)> traverse) const {
785 traverse(GetType());
786 if (IsDefaultUserSpecified()) {
787 traverse(*GetDefaultValue());
788 }
789}
790
Steven Moreland46e9da82018-07-27 15:45:29 -0700791AidlArgument::AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
792 AidlTypeSpecifier* type, const std::string& name)
793 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700794 direction_(direction),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700795 direction_specified_(true) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700796
Steven Moreland46e9da82018-07-27 15:45:29 -0700797AidlArgument::AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type,
798 const std::string& name)
799 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700800 direction_(AidlArgument::IN_DIR),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700801 direction_specified_(false) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700802
Jooyung Han020d8d12021-02-26 17:23:02 +0900803static std::string to_string(AidlArgument::Direction direction) {
804 switch (direction) {
805 case AidlArgument::IN_DIR:
806 return "in";
807 case AidlArgument::OUT_DIR:
808 return "out";
809 case AidlArgument::INOUT_DIR:
810 return "inout";
811 }
812}
813
Jiyong Park02da7422018-07-16 16:00:26 +0900814string AidlArgument::GetDirectionSpecifier() const {
Casey Dahlinc378c992015-09-29 16:50:40 -0700815 string ret;
Casey Dahlinc378c992015-09-29 16:50:40 -0700816 if (direction_specified_) {
Jooyung Han020d8d12021-02-26 17:23:02 +0900817 ret = to_string(direction_);
Casey Dahlinc378c992015-09-29 16:50:40 -0700818 }
Casey Dahlinc378c992015-09-29 16:50:40 -0700819 return ret;
820}
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700821
Jiyong Park02da7422018-07-16 16:00:26 +0900822string AidlArgument::ToString() const {
Devin Mooreeccdb902020-03-24 16:22:40 -0700823 if (direction_specified_) {
824 return GetDirectionSpecifier() + " " + AidlVariableDeclaration::ToString();
825 } else {
826 return AidlVariableDeclaration::ToString();
827 }
Jiyong Park02da7422018-07-16 16:00:26 +0900828}
829
Jooyung Han020d8d12021-02-26 17:23:02 +0900830static std::string FormatDirections(const std::set<AidlArgument::Direction>& directions) {
831 std::vector<std::string> out;
832 for (const auto& d : directions) {
833 out.push_back(to_string(d));
834 }
835
836 if (out.size() <= 1) { // [] => "" or [A] => "A"
837 return Join(out, "");
838 } else if (out.size() == 2) { // [A,B] => "A or B"
839 return Join(out, " or ");
840 } else { // [A,B,C] => "A, B, or C"
841 out.back() = "or " + out.back();
842 return Join(out, ", ");
843 }
844}
845
846bool AidlArgument::CheckValid(const AidlTypenames& typenames) const {
847 if (!GetType().CheckValid(typenames)) {
848 return false;
849 }
850
851 const auto& aspect = typenames.GetArgumentAspect(GetType());
852
853 if (aspect.possible_directions.size() == 0) {
854 AIDL_ERROR(this) << aspect.name << " cannot be an argument type";
855 return false;
856 }
857
858 // when direction is not specified, "in" is assumed and should be the only possible direction
859 if (!DirectionWasSpecified() && aspect.possible_directions != std::set{AidlArgument::IN_DIR}) {
860 AIDL_ERROR(this) << "The direction of '" << GetName() << "' is not specified. " << aspect.name
861 << " can be an " << FormatDirections(aspect.possible_directions)
862 << " parameter.";
863 return false;
864 }
865
866 if (aspect.possible_directions.count(GetDirection()) == 0) {
867 AIDL_ERROR(this) << "'" << GetName() << "' can't be an " << GetDirectionSpecifier()
868 << " parameter because " << aspect.name << " can only be an "
869 << FormatDirections(aspect.possible_directions) << " parameter.";
870 return false;
871 }
872
873 return true;
874}
875
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900876bool AidlCommentable::IsHidden() const {
Jooyung Han24effbf2021-01-16 10:24:03 +0900877 return android::aidl::HasHideInComments(GetComments());
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900878}
879
880bool AidlCommentable::IsDeprecated() const {
Jooyung Hand4fe00e2021-01-11 16:21:53 +0900881 return android::aidl::FindDeprecated(GetComments()).has_value();
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900882}
883
Jooyung Han8451a202021-01-16 03:07:06 +0900884AidlMember::AidlMember(const AidlLocation& location, const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +0900885 : AidlCommentable(location, comments) {}
Steven Moreland46e9da82018-07-27 15:45:29 -0700886
Steven Moreland46e9da82018-07-27 15:45:29 -0700887AidlConstantDeclaration::AidlConstantDeclaration(const AidlLocation& location,
888 AidlTypeSpecifier* type, const std::string& name,
889 AidlConstantValue* value)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900890 : AidlMember(location, type->GetComments()), type_(type), name_(name), value_(value) {}
Steven Moreland693640b2018-07-19 13:46:27 -0700891
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900892bool AidlConstantDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700893 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900894 valid &= type_->CheckValid(typenames);
Steven Moreland25294322018-08-07 18:13:55 -0700895 valid &= value_->CheckValid();
896 if (!valid) return false;
Steven Moreland693640b2018-07-19 13:46:27 -0700897
Steven Morelande689da22020-11-10 02:06:30 +0000898 const static set<string> kSupportedConstTypes = {"String", "byte", "int", "long"};
Jooyung Han965e31d2020-11-27 12:30:16 +0900899 if (kSupportedConstTypes.find(type_->Signature()) == kSupportedConstTypes.end()) {
900 AIDL_ERROR(this) << "Constant of type " << type_->Signature() << " is not supported.";
Steven Moreland693640b2018-07-19 13:46:27 -0700901 return false;
902 }
903
Will McVickerd7d18df2019-09-12 13:40:50 -0700904 return true;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700905}
906
Jiyong Parka428d212018-08-29 22:26:30 +0900907string AidlConstantDeclaration::ToString() const {
Jooyung Hanb3ca6302020-11-27 14:13:27 +0900908 return "const " + type_->ToString() + " " + name_ + " = " +
909 ValueString(AidlConstantValueDecorator);
Jiyong Parka428d212018-08-29 22:26:30 +0900910}
911
912string AidlConstantDeclaration::Signature() const {
913 return type_->Signature() + " " + name_;
914}
915
Steven Moreland46e9da82018-07-27 15:45:29 -0700916AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
917 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jooyung Han8451a202021-01-16 03:07:06 +0900918 const Comments& comments)
Jiyong Parkb034bf02018-07-30 17:44:33 +0900919 : AidlMethod(location, oneway, type, name, args, comments, 0, true) {
920 has_id_ = false;
921}
922
923AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
924 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jooyung Han8451a202021-01-16 03:07:06 +0900925 const Comments& comments, int id, bool is_user_defined)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900926 : AidlMember(location, comments),
Steven Moreland46e9da82018-07-27 15:45:29 -0700927 oneway_(oneway),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700928 type_(type),
929 name_(name),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700930 arguments_(std::move(*args)),
Jiyong Parkb034bf02018-07-30 17:44:33 +0900931 id_(id),
932 is_user_defined_(is_user_defined) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700933 has_id_ = true;
934 delete args;
Christopher Wileyad339272015-10-05 19:11:58 -0700935 for (const unique_ptr<AidlArgument>& a : arguments_) {
936 if (a->IsIn()) { in_arguments_.push_back(a.get()); }
937 if (a->IsOut()) { out_arguments_.push_back(a.get()); }
938 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700939}
940
Jiyong Park02da7422018-07-16 16:00:26 +0900941string AidlMethod::Signature() const {
942 vector<string> arg_signatures;
943 for (const auto& arg : GetArguments()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900944 arg_signatures.emplace_back(arg->GetType().Signature());
Jiyong Park02da7422018-07-16 16:00:26 +0900945 }
Jiyong Park309668e2018-07-28 16:55:44 +0900946 return GetName() + "(" + Join(arg_signatures, ", ") + ")";
947}
948
949string AidlMethod::ToString() const {
950 vector<string> arg_strings;
951 for (const auto& arg : GetArguments()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900952 arg_strings.emplace_back(arg->ToString());
Jiyong Park309668e2018-07-28 16:55:44 +0900953 }
Jooyung Han965e31d2020-11-27 12:30:16 +0900954 string ret = (IsOneway() ? "oneway " : "") + GetType().ToString() + " " + GetName() + "(" +
Steven Moreland4ee68632018-12-14 15:52:46 -0800955 Join(arg_strings, ", ") + ")";
Jiyong Parked65bf42018-08-28 15:43:27 +0900956 if (HasId()) {
957 ret += " = " + std::to_string(GetId());
958 }
959 return ret;
Jiyong Park02da7422018-07-16 16:00:26 +0900960}
961
Steven Moreland46e9da82018-07-27 15:45:29 -0700962AidlDefinedType::AidlDefinedType(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +0900963 const Comments& comments, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900964 std::vector<std::unique_ptr<AidlMember>>* members)
Jooyung Han35784982021-06-29 06:26:12 +0900965 : AidlAnnotatable(location, comments), AidlScope(this), name_(name), package_(package) {
Jooyung Han93f48f02021-06-05 00:11:16 +0900966 // adjust name/package when name is fully qualified (for preprocessed files)
967 if (package_.empty() && name_.find('.') != std::string::npos) {
968 // Note that this logic is absolutely wrong. Given a parcelable
969 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
970 // the class is just Bar. However, this was the way it was done in the past.
971 //
972 // See b/17415692
973 auto pos = name.rfind('.');
974 // name is the last part.
975 name_ = name.substr(pos + 1);
976 // package is the initial parts (except the last).
977 package_ = name.substr(0, pos);
978 }
Jooyung Han829ec7c2020-12-02 12:07:36 +0900979 if (members) {
980 for (auto& m : *members) {
981 if (auto constant = m->AsConstantDeclaration(); constant) {
982 constants_.emplace_back(constant);
983 } else if (auto variable = m->AsVariableDeclaration(); variable) {
984 variables_.emplace_back(variable);
985 } else if (auto method = m->AsMethod(); method) {
986 methods_.emplace_back(method);
987 } else {
988 AIDL_FATAL(*m);
989 }
990 members_.push_back(m.release());
991 }
992 delete members;
993 }
994}
Steven Moreland787b0432018-07-03 09:00:58 -0700995
Jooyung Han808a2a02020-12-28 16:46:54 +0900996bool AidlDefinedType::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -0800997 if (!AidlAnnotatable::CheckValid(typenames)) {
998 return false;
999 }
Jooyung Han829ec7c2020-12-02 12:07:36 +09001000 if (!CheckValidWithMembers(typenames)) {
1001 return false;
1002 }
Devin Moore24f68572020-02-26 13:20:59 -08001003 return true;
1004}
1005
Steven Moreland787b0432018-07-03 09:00:58 -07001006std::string AidlDefinedType::GetCanonicalName() const {
1007 if (package_.empty()) {
1008 return GetName();
1009 }
1010 return GetPackage() + "." + GetName();
1011}
1012
Jooyung Han829ec7c2020-12-02 12:07:36 +09001013bool AidlDefinedType::CheckValidWithMembers(const AidlTypenames& typenames) const {
1014 bool success = true;
1015
1016 for (const auto& v : GetFields()) {
1017 const bool field_valid = v->CheckValid(typenames);
1018 success = success && field_valid;
1019 }
1020
1021 // field names should be unique
1022 std::set<std::string> fieldnames;
1023 for (const auto& v : GetFields()) {
1024 bool duplicated = !fieldnames.emplace(v->GetName()).second;
1025 if (duplicated) {
1026 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName() << "'";
1027 success = false;
1028 }
1029 }
1030
1031 // immutable parcelables should have immutable fields.
1032 if (IsJavaOnlyImmutable()) {
1033 for (const auto& v : GetFields()) {
1034 if (!typenames.CanBeJavaOnlyImmutable(v->GetType())) {
1035 AIDL_ERROR(v) << "The @JavaOnlyImmutable '" << GetName() << "' has a "
1036 << "non-immutable field named '" << v->GetName() << "'.";
1037 success = false;
1038 }
1039 }
1040 }
1041
1042 set<string> constant_names;
1043 for (const auto& constant : GetConstantDeclarations()) {
1044 if (constant_names.count(constant->GetName()) > 0) {
1045 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
1046 success = false;
1047 }
1048 constant_names.insert(constant->GetName());
1049 success = success && constant->CheckValid(typenames);
1050 }
1051
1052 return success;
1053}
1054
1055bool AidlDefinedType::CheckValidForGetterNames() const {
1056 bool success = true;
1057 std::set<std::string> getters;
1058 for (const auto& v : GetFields()) {
1059 bool duplicated = !getters.emplace(v->GetCapitalizedName()).second;
1060 if (duplicated) {
1061 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName()
1062 << "' after capitalizing the first letter";
1063 success = false;
1064 }
1065 }
1066 return success;
1067}
1068
Jooyung Han13f1fa52021-06-11 18:06:12 +09001069std::string AidlDefinedType::ResolveName(const std::string& name) const {
1070 // TODO(b/182508839): resolve with nested types when we support nested types
1071 //
1072 // For example, in the following (the syntax is TBD), t1's Type is x.Foo.Bar.Type
1073 // while t2's Type is y.Type.
1074 //
1075 // package x;
1076 // import y.Type;
1077 // parcelable Foo {
1078 // parcelable Bar {
1079 // enum Type { Type1, Type2 }
1080 // Type t1;
1081 // }
1082 // Type t2;
1083 // }
1084 AIDL_FATAL_IF(!GetEnclosingScope(), this)
1085 << "Type should have an enclosing scope.(e.g. AidlDocument)";
1086 return GetEnclosingScope()->ResolveName(name);
1087}
1088
Jooyung Han35784982021-06-29 06:26:12 +09001089template <typename T>
1090const T* AidlCast(const AidlNode& node) {
1091 struct CastVisitor : AidlVisitor {
1092 const T* cast = nullptr;
1093 void Visit(const T& t) override { cast = &t; }
1094 } visitor;
1095 node.DispatchVisit(visitor);
1096 return visitor.cast;
1097}
1098
1099const AidlDocument& AidlDefinedType::GetDocument() const {
1100 // TODO(b/182508839): resolve with nested types when we support nested types
1101 auto scope = GetEnclosingScope();
1102 AIDL_FATAL_IF(!scope, this) << "no scope defined.";
1103 auto doc = AidlCast<AidlDocument>(scope->GetNode());
1104 AIDL_FATAL_IF(!doc, this) << "scope is not a document.";
1105 return *doc;
1106}
1107
Jiyong Park18132182020-06-08 20:24:40 +09001108AidlParcelable::AidlParcelable(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001109 const std::string& package, const Comments& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001110 const std::string& cpp_header, std::vector<std::string>* type_params,
1111 std::vector<std::unique_ptr<AidlMember>>* members)
1112 : AidlDefinedType(location, name, comments, package, members),
Jeongik Chadf76dc72019-11-28 00:08:47 +09001113 AidlParameterizable<std::string>(type_params),
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -08001114 cpp_header_(cpp_header) {
1115 // Strip off quotation marks if we actually have a cpp header.
1116 if (cpp_header_.length() >= 2) {
1117 cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2);
1118 }
Casey Dahlin59401da2015-10-09 18:16:45 -07001119}
Jeongik Chadf76dc72019-11-28 00:08:47 +09001120template <typename T>
1121AidlParameterizable<T>::AidlParameterizable(const AidlParameterizable& other) {
1122 // Copying is not supported if it has type parameters.
1123 // It doesn't make a problem because only ArrayBase() makes a copy,
1124 // and it can be called only if a type is not generic.
Steven Moreland21780812020-09-11 01:29:45 +00001125 AIDL_FATAL_IF(other.IsGeneric(), AIDL_LOCATION_HERE);
Jeongik Chadf76dc72019-11-28 00:08:47 +09001126}
1127
1128template <typename T>
1129bool AidlParameterizable<T>::CheckValid() const {
1130 return true;
1131};
1132
1133template <>
1134bool AidlParameterizable<std::string>::CheckValid() const {
1135 if (!IsGeneric()) {
1136 return true;
1137 }
1138 std::unordered_set<std::string> set(GetTypeParameters().begin(), GetTypeParameters().end());
1139 if (set.size() != GetTypeParameters().size()) {
1140 AIDL_ERROR(this->AsAidlNode()) << "Every type parameter should be unique.";
1141 return false;
1142 }
1143 return true;
1144}
Casey Dahlin59401da2015-10-09 18:16:45 -07001145
Jooyung Han808a2a02020-12-28 16:46:54 +09001146bool AidlParcelable::CheckValid(const AidlTypenames& typenames) const {
1147 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001148 return false;
1149 }
Jeongik Chadf76dc72019-11-28 00:08:47 +09001150 if (!AidlParameterizable<std::string>::CheckValid()) {
1151 return false;
1152 }
Jeongik Cha82317dd2019-02-27 20:26:11 +09001153
1154 return true;
1155}
1156
Steven Moreland5557f1c2018-07-02 13:50:23 -07001157AidlStructuredParcelable::AidlStructuredParcelable(
Jiyong Park18132182020-06-08 20:24:40 +09001158 const AidlLocation& location, const std::string& name, const std::string& package,
Jooyung Han8451a202021-01-16 03:07:06 +09001159 const Comments& comments, std::vector<std::string>* type_params,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001160 std::vector<std::unique_ptr<AidlMember>>* members)
1161 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Steven Moreland5557f1c2018-07-02 13:50:23 -07001162
Jooyung Han808a2a02020-12-28 16:46:54 +09001163bool AidlStructuredParcelable::CheckValid(const AidlTypenames& typenames) const {
1164 if (!AidlParcelable::CheckValid(typenames)) {
Devin Moore24f68572020-02-26 13:20:59 -08001165 return false;
1166 }
Jeongik Cha13066da2020-08-06 15:43:19 +09001167
Jooyung Han59af9cc2020-10-25 21:44:14 +09001168 bool success = true;
Jeongik Cha36f76c32020-07-28 00:25:52 +09001169
Jooyung Hand4057c42020-10-23 13:28:22 +09001170 if (IsFixedSize()) {
1171 for (const auto& v : GetFields()) {
1172 if (!typenames.CanBeFixedSize(v->GetType())) {
1173 AIDL_ERROR(v) << "The @FixedSize parcelable '" << this->GetName() << "' has a "
1174 << "non-fixed size field named " << v->GetName() << ".";
1175 success = false;
1176 }
1177 }
1178 }
1179
1180 if (IsJavaOnlyImmutable()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001181 // Immutable parcelables provide getters
Jooyung Han829ec7c2020-12-02 12:07:36 +09001182 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001183 success = false;
Jooyung Hand4057c42020-10-23 13:28:22 +09001184 }
1185 }
1186
Daniel Norman85aed542019-08-21 12:01:14 -07001187 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001188}
1189
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001190// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -07001191bool AidlTypeSpecifier::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1192 Options::Language lang) const {
Jooyung Hand09a21d2021-02-15 18:56:55 +09001193 if (IsGeneric()) {
1194 const auto& types = GetTypeParameters();
1195 for (const auto& arg : types) {
1196 if (!arg->LanguageSpecificCheckValid(typenames, lang)) {
1197 return false;
1198 }
1199 }
1200 }
1201
Andrei Homescub62afd92020-05-11 19:24:59 -07001202 if ((lang == Options::Language::NDK || lang == Options::Language::RUST) && IsArray() &&
1203 GetName() == "IBinder") {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001204 AIDL_ERROR(this) << "The " << to_string(lang) << " backend does not support array of IBinder";
Steven Moreland0185d9b2020-05-15 23:21:22 +00001205 return false;
1206 }
Jeongik Cha8f02a532020-10-14 00:16:28 +09001207 if (lang == Options::Language::RUST && GetName() == "ParcelableHolder") {
1208 // TODO(b/146611855): Remove it when Rust backend supports ParcelableHolder
1209 AIDL_ERROR(this) << "The Rust backend does not support ParcelableHolder yet.";
Jeongik Cha225519b2020-08-29 01:55:32 +09001210 return false;
1211 }
Andrei Homescub62afd92020-05-11 19:24:59 -07001212 if ((lang == Options::Language::NDK || lang == Options::Language::RUST) && IsArray() &&
1213 IsNullable()) {
Steven Moreland0185d9b2020-05-15 23:21:22 +00001214 if (GetName() == "ParcelFileDescriptor") {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001215 AIDL_ERROR(this) << "The " << to_string(lang)
Andrei Homescub62afd92020-05-11 19:24:59 -07001216 << " backend does not support nullable array of ParcelFileDescriptor";
Steven Moreland0185d9b2020-05-15 23:21:22 +00001217 return false;
1218 }
1219
Steven Morelandd59e3172020-05-11 16:42:09 -07001220 const auto defined_type = typenames.TryGetDefinedType(GetName());
1221 if (defined_type != nullptr && defined_type->AsParcelable() != nullptr) {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001222 AIDL_ERROR(this) << "The " << to_string(lang)
Andrei Homescub62afd92020-05-11 19:24:59 -07001223 << " backend does not support nullable array of parcelable";
Steven Morelandd59e3172020-05-11 16:42:09 -07001224 return false;
1225 }
1226 }
Andrei Homescub62afd92020-05-11 19:24:59 -07001227 if (this->GetName() == "FileDescriptor" &&
1228 (lang == Options::Language::NDK || lang == Options::Language::RUST)) {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001229 AIDL_ERROR(this) << "FileDescriptor isn't supported by the " << to_string(lang) << " backend.";
Steven Morelandc8a4ca82020-01-21 17:50:08 -08001230 return false;
1231 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001232 if (this->IsGeneric()) {
1233 if (this->GetName() == "List") {
Jooyung Han55f96ad2020-12-13 10:08:33 +09001234 if (lang == Options::Language::NDK) {
1235 const AidlTypeSpecifier& contained_type = *GetTypeParameters()[0];
1236 const string& contained_type_name = contained_type.GetName();
Jooyung Hane87cdd02020-12-11 16:47:35 +09001237 if (typenames.GetInterface(contained_type)) {
1238 AIDL_ERROR(this) << "List<" << contained_type_name
1239 << "> is not supported. List in NDK doesn't support interface.";
1240 return false;
1241 }
1242 if (contained_type_name == "IBinder") {
1243 AIDL_ERROR(this) << "List<" << contained_type_name
1244 << "> is not supported. List in NDK doesn't support IBinder.";
1245 return false;
Jeongik Cha08ca2182019-11-21 14:01:13 +09001246 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001247 }
Jeongik Chabb55b5e2020-01-07 23:11:26 +09001248 }
1249 }
Devin Moore6a01ca12020-08-28 10:24:19 -07001250
1251 if (this->IsArray()) {
1252 if (this->GetName() == "List" || this->GetName() == "Map" ||
1253 this->GetName() == "CharSequence") {
1254 AIDL_ERROR(this) << this->GetName() << "[] is not supported.";
Jeongik Chabb55b5e2020-01-07 23:11:26 +09001255 return false;
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001256 }
1257 }
Devin Moore6a01ca12020-08-28 10:24:19 -07001258
1259 if (lang != Options::Language::JAVA) {
1260 if (this->GetName() == "List" && !this->IsGeneric()) {
1261 AIDL_ERROR(this) << "Currently, only the Java backend supports non-generic List.";
1262 return false;
1263 }
1264 if (this->GetName() == "Map" || this->GetName() == "CharSequence") {
1265 AIDL_ERROR(this) << "Currently, only Java backend supports " << this->GetName() << ".";
1266 return false;
Jeongik Cha08ca2182019-11-21 14:01:13 +09001267 }
1268 }
1269
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001270 return true;
1271}
1272
1273// TODO: we should treat every backend all the same in future.
Jooyung Han589cfb02021-09-28 17:26:04 +09001274bool AidlDefinedType::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1275 Options::Language lang) const {
1276 struct Visitor : AidlVisitor {
1277 Visitor(const AidlTypenames& typenames, Options::Language lang)
1278 : typenames(typenames), lang(lang) {}
1279 void Visit(const AidlTypeSpecifier& type) override {
1280 success = success && type.LanguageSpecificCheckValid(typenames, lang);
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001281 }
Jooyung Han589cfb02021-09-28 17:26:04 +09001282 const AidlTypenames& typenames;
1283 Options::Language lang;
1284 bool success = true;
1285 } v(typenames, lang);
1286 VisitTopDown(v, *this);
1287 return v.success;
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001288}
1289
Daniel Norman85aed542019-08-21 12:01:14 -07001290AidlEnumerator::AidlEnumerator(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001291 AidlConstantValue* value, const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +09001292 : AidlCommentable(location, comments),
Jooyung Han29813842020-12-08 01:28:03 +09001293 name_(name),
1294 value_(value),
Jooyung Han29813842020-12-08 01:28:03 +09001295 value_user_specified_(value != nullptr) {}
Daniel Norman85aed542019-08-21 12:01:14 -07001296
1297bool AidlEnumerator::CheckValid(const AidlTypeSpecifier& enum_backing_type) const {
1298 if (GetValue() == nullptr) {
1299 return false;
1300 }
1301 if (!GetValue()->CheckValid()) {
1302 return false;
1303 }
Will McVickerd7d18df2019-09-12 13:40:50 -07001304 if (GetValue()->ValueString(enum_backing_type, AidlConstantValueDecorator).empty()) {
Daniel Norman85aed542019-08-21 12:01:14 -07001305 AIDL_ERROR(this) << "Enumerator type differs from enum backing type.";
1306 return false;
1307 }
1308 return true;
1309}
1310
1311string AidlEnumerator::ValueString(const AidlTypeSpecifier& backing_type,
1312 const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -07001313 return GetValue()->ValueString(backing_type, decorator);
Daniel Norman85aed542019-08-21 12:01:14 -07001314}
1315
1316AidlEnumDeclaration::AidlEnumDeclaration(const AidlLocation& location, const std::string& name,
1317 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jooyung Han8451a202021-01-16 03:07:06 +09001318 const std::string& package, const Comments& comments)
Jooyung Han829ec7c2020-12-02 12:07:36 +09001319 : AidlDefinedType(location, name, comments, package, nullptr),
Jooyung Han29813842020-12-08 01:28:03 +09001320 enumerators_(std::move(*enumerators)) {
Jooyung Han672557b2020-12-24 05:18:00 +09001321 // Fill missing enumerator values with <prev + 1>
1322 // This can't be done in Autofill() because type/ref resolution depends on this.
1323 // For example, with enum E { A, B = A }, B's value 'A' is a reference which can't be
1324 // resolved if A has no value set.
Daniel Normanb28684e2019-10-17 15:31:39 -07001325 const AidlEnumerator* previous = nullptr;
1326 for (const auto& enumerator : enumerators_) {
1327 if (enumerator->GetValue() == nullptr) {
Jooyung Han29813842020-12-08 01:28:03 +09001328 auto loc = enumerator->GetLocation();
Daniel Normanb28684e2019-10-17 15:31:39 -07001329 if (previous == nullptr) {
Devin Mooredf93ebb2020-03-25 14:03:35 -07001330 enumerator->SetValue(
Jooyung Han29813842020-12-08 01:28:03 +09001331 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "0")));
Daniel Normanb28684e2019-10-17 15:31:39 -07001332 } else {
Jooyung Hand0c8af02021-01-06 18:08:01 +09001333 auto prev_value = std::make_unique<AidlConstantReference>(loc, previous->GetName());
Daniel Normanb28684e2019-10-17 15:31:39 -07001334 enumerator->SetValue(std::make_unique<AidlBinaryConstExpression>(
Jooyung Han29813842020-12-08 01:28:03 +09001335 loc, std::move(prev_value), "+",
1336 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "1"))));
Daniel Normanb28684e2019-10-17 15:31:39 -07001337 }
1338 }
1339 previous = enumerator.get();
1340 }
1341}
1342
Jooyung Han672557b2020-12-24 05:18:00 +09001343bool AidlEnumDeclaration::Autofill(const AidlTypenames& typenames) {
1344 if (auto annot = BackingType(); annot != nullptr) {
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001345 // Autofill() is called before the grand CheckValid(). But AidlAnnotation::ParamValue()
1346 // calls AidlConstantValue::evaluate() which requires CheckValid() to be called before. So we
Jooyung Han672557b2020-12-24 05:18:00 +09001347 // need to call CheckValid().
1348 if (!annot->CheckValid()) {
1349 return false;
1350 }
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001351 auto type = annot->ParamValue<std::string>("type").value();
1352 backing_type_ =
Jooyung Han8451a202021-01-16 03:07:06 +09001353 std::make_unique<AidlTypeSpecifier>(annot->GetLocation(), type, false, nullptr, Comments{});
Jooyung Han672557b2020-12-24 05:18:00 +09001354 } else {
1355 // Default to byte type for enums.
1356 backing_type_ =
Jooyung Han8451a202021-01-16 03:07:06 +09001357 std::make_unique<AidlTypeSpecifier>(AIDL_LOCATION_HERE, "byte", false, nullptr, Comments{});
Jooyung Han672557b2020-12-24 05:18:00 +09001358 }
1359 // Autofill() is called after type resolution, we resolve the backing type manually.
Jooyung Han13f1fa52021-06-11 18:06:12 +09001360 if (!backing_type_->Resolve(typenames, nullptr)) {
Jooyung Han672557b2020-12-24 05:18:00 +09001361 AIDL_ERROR(this) << "Invalid backing type: " << backing_type_->GetName();
1362 }
1363 return true;
1364}
1365
Jooyung Han808a2a02020-12-28 16:46:54 +09001366bool AidlEnumDeclaration::CheckValid(const AidlTypenames& typenames) const {
1367 if (!AidlDefinedType::CheckValid(typenames)) {
Devin Moore24f68572020-02-26 13:20:59 -08001368 return false;
1369 }
Jooyung Han829ec7c2020-12-02 12:07:36 +09001370 if (!GetMembers().empty()) {
1371 AIDL_ERROR(this) << "Enum doesn't support fields/constants/methods.";
1372 return false;
1373 }
Daniel Norman85aed542019-08-21 12:01:14 -07001374 if (backing_type_ == nullptr) {
1375 AIDL_ERROR(this) << "Enum declaration missing backing type.";
1376 return false;
1377 }
1378 bool success = true;
1379 for (const auto& enumerator : enumerators_) {
1380 success = success && enumerator->CheckValid(GetBackingType());
1381 }
Jooyung Han3b990182020-12-22 17:44:31 +09001382
Jooyung Han808a2a02020-12-28 16:46:54 +09001383 return success;
Daniel Norman85aed542019-08-21 12:01:14 -07001384}
1385
Jooyung Han2946afc2020-10-05 20:29:16 +09001386AidlUnionDecl::AidlUnionDecl(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001387 const std::string& package, const Comments& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001388 std::vector<std::string>* type_params,
1389 std::vector<std::unique_ptr<AidlMember>>* members)
1390 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Jooyung Han2946afc2020-10-05 20:29:16 +09001391
Jooyung Han808a2a02020-12-28 16:46:54 +09001392bool AidlUnionDecl::CheckValid(const AidlTypenames& typenames) const {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001393 // visit parents
Jooyung Han808a2a02020-12-28 16:46:54 +09001394 if (!AidlParcelable::CheckValid(typenames)) {
Jooyung Hanfe89f122020-10-14 03:49:18 +09001395 return false;
1396 }
Jooyung Han59af9cc2020-10-25 21:44:14 +09001397
1398 // unions provide getters always
Jooyung Han829ec7c2020-12-02 12:07:36 +09001399 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001400 return false;
Jooyung Hanfe89f122020-10-14 03:49:18 +09001401 }
1402
1403 // now, visit self!
1404 bool success = true;
1405
1406 // TODO(b/170807936) do we need to allow ParcelableHolder in union?
1407 for (const auto& v : GetFields()) {
1408 if (v->GetType().GetName() == "ParcelableHolder") {
1409 AIDL_ERROR(*v) << "A union can't have a member of ParcelableHolder '" << v->GetName() << "'";
1410 success = false;
1411 }
1412 }
1413
Jooyung Hanfe89f122020-10-14 03:49:18 +09001414 if (GetFields().empty()) {
1415 AIDL_ERROR(*this) << "The union '" << this->GetName() << "' has no fields.";
1416 return false;
1417 }
1418
Jooyung Han53fb4242020-12-17 16:03:49 +09001419 // first member should have useful default value (implicit or explicit)
1420 const auto& first = GetFields()[0];
1421 if (!first->HasUsefulDefaultValue()) {
1422 // Most types can be initialized without a default value. For example,
1423 // interface types are inherently nullable. But, enum types should have
1424 // an explicit default value.
1425 if (!first->GetType().IsArray() && typenames.GetEnumDeclaration(first->GetType())) {
1426 AIDL_ERROR(first)
1427 << "The union's first member should have a useful default value. Enum types can be "
1428 "initialized with a reference. (e.g. ... = MyEnum.FOO;)";
1429 return false;
1430 }
1431 // In Java, array types are initialized as null without a default value. To be sure that default
1432 // initialized unions are accepted by other backends we require arrays also have a default
1433 // value.
1434 if (first->GetType().IsArray()) {
1435 AIDL_ERROR(first)
1436 << "The union's first member should have a useful default value. Arrays can be "
1437 "initialized with values(e.g. ... = { values... };) or marked as @nullable.";
1438 return false;
1439 }
1440 }
1441
Jooyung Hanfe89f122020-10-14 03:49:18 +09001442 return success;
1443}
1444
Steven Moreland46e9da82018-07-27 15:45:29 -07001445AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001446 const Comments& comments, bool oneway, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001447 std::vector<std::unique_ptr<AidlMember>>* members)
1448 : AidlDefinedType(location, name, comments, package, members) {
1449 for (auto& m : GetMethods()) {
1450 m.get()->ApplyInterfaceOneway(oneway);
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001451 }
Casey Dahlinfb7da2e2015-10-08 17:26:09 -07001452}
1453
Jooyung Han808a2a02020-12-28 16:46:54 +09001454bool AidlInterface::CheckValid(const AidlTypenames& typenames) const {
1455 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001456 return false;
1457 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001458 // Has to be a pointer due to deleting copy constructor. No idea why.
1459 map<string, const AidlMethod*> method_names;
1460 for (const auto& m : GetMethods()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001461 if (!m->GetType().CheckValid(typenames)) {
1462 return false;
1463 }
1464
Jeongik Cha649e8a72020-03-27 17:47:40 +09001465 // TODO(b/156872582): Support it when ParcelableHolder supports every backend.
1466 if (m->GetType().GetName() == "ParcelableHolder") {
1467 AIDL_ERROR(m) << "ParcelableHolder cannot be a return type";
1468 return false;
1469 }
Steven Morelandacd53472018-12-14 10:17:26 -08001470 if (m->IsOneway() && m->GetType().GetName() != "void") {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001471 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot return a value";
1472 return false;
1473 }
1474
1475 set<string> argument_names;
1476 for (const auto& arg : m->GetArguments()) {
1477 auto it = argument_names.find(arg->GetName());
1478 if (it != argument_names.end()) {
1479 AIDL_ERROR(m) << "method '" << m->GetName() << "' has duplicate argument name '"
1480 << arg->GetName() << "'";
1481 return false;
1482 }
1483 argument_names.insert(arg->GetName());
1484
Jooyung Han020d8d12021-02-26 17:23:02 +09001485 if (!arg->CheckValid(typenames)) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001486 return false;
1487 }
1488
Steven Morelandacd53472018-12-14 10:17:26 -08001489 if (m->IsOneway() && arg->IsOut()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001490 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot have out parameters";
1491 return false;
1492 }
Jooyung Han15fd6c62020-10-23 13:54:46 +09001493
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001494 // check that the name doesn't match a keyword
Jeongik Cha997281d2020-01-16 15:23:59 +09001495 if (IsJavaKeyword(arg->GetName().c_str())) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001496 AIDL_ERROR(arg) << "Argument name is a Java or aidl keyword";
1497 return false;
1498 }
1499
1500 // Reserve a namespace for internal use
1501 if (android::base::StartsWith(arg->GetName(), "_aidl")) {
1502 AIDL_ERROR(arg) << "Argument name cannot begin with '_aidl'";
1503 return false;
1504 }
Jooyung Hanfa181932021-06-12 07:56:53 +09001505
1506 if (arg->GetType().GetName() == "void") {
1507 AIDL_ERROR(arg->GetType())
1508 << "'void' is an invalid type for the parameter '" << arg->GetName() << "'";
1509 return false;
1510 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001511 }
1512
1513 auto it = method_names.find(m->GetName());
1514 // prevent duplicate methods
1515 if (it == method_names.end()) {
1516 method_names[m->GetName()] = m.get();
1517 } else {
1518 AIDL_ERROR(m) << "attempt to redefine method " << m->GetName() << ":";
1519 AIDL_ERROR(it->second) << "previously defined here.";
1520 return false;
1521 }
1522
Paul Trautrimb77048c2020-01-21 16:39:32 +09001523 static set<string> reserved_methods{"asBinder()", "getInterfaceHash()", "getInterfaceVersion()",
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001524 "getTransactionName(int)"};
1525
1526 if (reserved_methods.find(m->Signature()) != reserved_methods.end()) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001527 AIDL_ERROR(m) << " method " << m->Signature() << " is reserved for internal use.";
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001528 return false;
1529 }
1530 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001531
1532 bool success = true;
1533 set<string> constant_names;
Jooyung Han3f347ca2020-12-01 12:41:50 +09001534 for (const auto& constant : GetConstantDeclarations()) {
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001535 if (constant_names.count(constant->GetName()) > 0) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001536 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001537 success = false;
1538 }
1539 constant_names.insert(constant->GetName());
1540 success = success && constant->CheckValid(typenames);
1541 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001542 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001543}
1544
Jiyong Park27fd7fd2020-08-27 16:25:09 +09001545std::string AidlInterface::GetDescriptor() const {
1546 std::string annotatedDescriptor = AidlAnnotatable::GetDescriptor();
1547 if (annotatedDescriptor != "") {
1548 return annotatedDescriptor;
1549 }
1550 return GetCanonicalName();
1551}
1552
Jooyung Han132cf802021-01-15 02:17:32 +09001553AidlImport::AidlImport(const AidlLocation& location, const std::string& needed_class,
Jooyung Han8451a202021-01-16 03:07:06 +09001554 const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +09001555 : AidlNode(location, comments), needed_class_(needed_class) {}
Jooyung Han29813842020-12-08 01:28:03 +09001556
Jooyung Han13f1fa52021-06-11 18:06:12 +09001557AidlDocument::AidlDocument(const AidlLocation& location, const Comments& comments,
1558 std::vector<std::unique_ptr<AidlImport>> imports,
Jooyung Han35784982021-06-29 06:26:12 +09001559 std::vector<std::unique_ptr<AidlDefinedType>> defined_types,
1560 bool is_preprocessed)
Jooyung Han13f1fa52021-06-11 18:06:12 +09001561 : AidlCommentable(location, comments),
Jooyung Han35784982021-06-29 06:26:12 +09001562 AidlScope(this),
Jooyung Han13f1fa52021-06-11 18:06:12 +09001563 imports_(std::move(imports)),
Jooyung Han35784982021-06-29 06:26:12 +09001564 defined_types_(std::move(defined_types)),
1565 is_preprocessed_(is_preprocessed) {
Jooyung Han13f1fa52021-06-11 18:06:12 +09001566 for (const auto& t : defined_types_) {
1567 t->SetEnclosingScope(this);
1568 }
1569}
1570
1571// Resolves type name in the current document.
1572// - built-in types
1573// - imported types
1574// - top-level type
1575std::string AidlDocument::ResolveName(const std::string& name) const {
1576 if (AidlTypenames::IsBuiltinTypename(name)) {
1577 return name;
1578 }
1579
1580 const auto first_dot = name.find_first_of('.');
1581 // For "Outer.Inner", we look up "Outer" in the import list.
Jooyung Han29813842020-12-08 01:28:03 +09001582 const std::string class_name =
Jooyung Han13f1fa52021-06-11 18:06:12 +09001583 (first_dot == std::string::npos) ? name : name.substr(0, first_dot);
1584 // Keep ".Inner", to make a fully-qualified name
1585 const std::string nested_type = (first_dot == std::string::npos) ? "" : name.substr(first_dot);
1586
Jooyung Han29813842020-12-08 01:28:03 +09001587 for (const auto& import : Imports()) {
Jooyung Han13f1fa52021-06-11 18:06:12 +09001588 if (import->SimpleName() == class_name) {
1589 return import->GetNeededClass() + nested_type;
Jooyung Han29813842020-12-08 01:28:03 +09001590 }
1591 }
Jooyung Han13f1fa52021-06-11 18:06:12 +09001592
1593 // check if it is a top-level type.
1594 for (const auto& type : DefinedTypes()) {
1595 if (type->GetName() == class_name) {
1596 return type->GetCanonicalName() + nested_type;
1597 }
Jooyung Han29813842020-12-08 01:28:03 +09001598 }
Jooyung Han13f1fa52021-06-11 18:06:12 +09001599
1600 // name itself might be fully-qualified name.
1601 return name;
Steven Moreland26318532020-12-23 20:08:36 +00001602}