blob: e694212c39021ea9d3c0f2b0ffd69a84bc3fb34e [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
Steven Moreland0cac8662021-10-08 16:43:29 -0700503void AidlTypeSpecifier::ViewAsArrayBase(std::function<void(const AidlTypeSpecifier&)> func) 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
Steven Moreland0cac8662021-10-08 16:43:29 -0700508 is_array_ = false;
509 func(*this);
510 is_array_ = true;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700511}
512
Jooyung Han965e31d2020-11-27 12:30:16 +0900513string AidlTypeSpecifier::Signature() const {
Jiyong Park1deecc32018-07-17 01:14:41 +0900514 string ret = GetName();
515 if (IsGeneric()) {
516 vector<string> arg_names;
517 for (const auto& ta : GetTypeParameters()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900518 arg_names.emplace_back(ta->Signature());
Jiyong Parkccf00f82018-07-17 01:39:23 +0900519 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900520 ret += "<" + Join(arg_names, ",") + ">";
Jiyong Parkccf00f82018-07-17 01:39:23 +0900521 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900522 if (IsArray()) {
523 ret += "[]";
524 }
525 return ret;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900526}
527
Jooyung Han965e31d2020-11-27 12:30:16 +0900528string AidlTypeSpecifier::ToString() const {
529 string ret = Signature();
Jiyong Park02da7422018-07-16 16:00:26 +0900530 string annotations = AidlAnnotatable::ToString();
531 if (annotations != "") {
532 ret = annotations + " " + ret;
533 }
534 return ret;
535}
536
Jooyung Han13f1fa52021-06-11 18:06:12 +0900537// When `scope` is specified, name is resolved first based on it.
538// `scope` can be null for built-in types and fully-qualified types.
539bool AidlTypeSpecifier::Resolve(const AidlTypenames& typenames, const AidlScope* scope) {
Steven Moreland21780812020-09-11 01:29:45 +0000540 AIDL_FATAL_IF(IsResolved(), this);
Jooyung Han13f1fa52021-06-11 18:06:12 +0900541 std::string name = unresolved_name_;
542 if (scope) {
543 name = scope->ResolveName(name);
544 }
545 AidlTypenames::ResolvedTypename result = typenames.ResolveTypename(name);
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700546 if (result.is_resolved) {
547 fully_qualified_name_ = result.canonical_name;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900548 split_name_ = Split(fully_qualified_name_, ".");
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900549 defined_type_ = result.defined_type;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900550 }
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700551 return result.is_resolved;
Casey Dahlin70078e62015-09-30 17:01:30 -0700552}
553
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900554const AidlDefinedType* AidlTypeSpecifier::GetDefinedType() const {
555 return defined_type_;
556}
557
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900558bool AidlTypeSpecifier::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -0800559 if (!AidlAnnotatable::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100560 return false;
561 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900562 if (IsGeneric()) {
Jooyung Hand09a21d2021-02-15 18:56:55 +0900563 const auto& types = GetTypeParameters();
564 for (const auto& arg : types) {
565 if (!arg->CheckValid(typenames)) {
566 return false;
567 }
568 }
Jeongik Chae74c86d2019-12-12 16:54:03 +0900569
Jooyung Hand09a21d2021-02-15 18:56:55 +0900570 const string& type_name = GetName();
Jeongik Chae74c86d2019-12-12 16:54:03 +0900571 // TODO(b/136048684) Disallow to use primitive types only if it is List or Map.
572 if (type_name == "List" || type_name == "Map") {
Jooyung Hane87cdd02020-12-11 16:47:35 +0900573 if (std::any_of(types.begin(), types.end(), [&](auto& type_ptr) {
Jooyung Han1f35ef32021-02-15 19:08:05 +0900574 return !type_ptr->IsArray() &&
575 (typenames.GetEnumDeclaration(*type_ptr) ||
576 AidlTypenames::IsPrimitiveTypename(type_ptr->GetName()));
Jeongik Chae74c86d2019-12-12 16:54:03 +0900577 })) {
Devin Moore7b8d5c92020-03-17 14:14:08 -0700578 AIDL_ERROR(this) << "A generic type cannot have any primitive type parameters.";
Jeongik Chae74c86d2019-12-12 16:54:03 +0900579 return false;
580 }
581 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800582 const auto defined_type = typenames.TryGetDefinedType(type_name);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900583 const auto parameterizable =
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800584 defined_type != nullptr ? defined_type->AsParameterizable() : nullptr;
585 const bool is_user_defined_generic_type =
Jeongik Chadf76dc72019-11-28 00:08:47 +0900586 parameterizable != nullptr && parameterizable->IsGeneric();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800587 const size_t num_params = GetTypeParameters().size();
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900588 if (type_name == "List") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800589 if (num_params > 1) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900590 AIDL_ERROR(this) << "List can only have one type parameter, but got: '" << Signature()
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000591 << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900592 return false;
593 }
Jooyung Han55f96ad2020-12-13 10:08:33 +0900594 const AidlTypeSpecifier& contained_type = *GetTypeParameters()[0];
Jooyung Hancea89002021-02-15 17:04:04 +0900595 if (contained_type.IsArray()) {
596 AIDL_ERROR(this)
597 << "List of arrays is not supported. List<T> supports parcelable/union, String, "
598 "IBinder, and ParcelFileDescriptor.";
599 return false;
600 }
Jooyung Han55f96ad2020-12-13 10:08:33 +0900601 const string& contained_type_name = contained_type.GetName();
602 if (AidlTypenames::IsBuiltinTypename(contained_type_name)) {
603 if (contained_type_name != "String" && contained_type_name != "IBinder" &&
604 contained_type_name != "ParcelFileDescriptor") {
605 AIDL_ERROR(this) << "List<" << contained_type_name
606 << "> is not supported. List<T> supports parcelable/union, String, "
607 "IBinder, and ParcelFileDescriptor.";
608 return false;
609 }
610 } else { // Defined types
611 if (typenames.GetInterface(contained_type)) {
612 AIDL_ERROR(this) << "List<" << contained_type_name
613 << "> is not supported. List<T> supports parcelable/union, String, "
614 "IBinder, and ParcelFileDescriptor.";
615 return false;
616 }
617 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900618 } else if (type_name == "Map") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800619 if (num_params != 0 && num_params != 2) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900620 AIDL_ERROR(this) << "Map must have 0 or 2 type parameters, but got "
Jooyung Han965e31d2020-11-27 12:30:16 +0900621 << "'" << Signature() << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900622 return false;
623 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800624 if (num_params == 2) {
Jooyung Hanaab242a2021-02-15 19:01:15 +0900625 const string& key_type = GetTypeParameters()[0]->Signature();
Jeongik Chae48d9942020-01-02 17:39:00 +0900626 if (key_type != "String") {
627 AIDL_ERROR(this) << "The type of key in map must be String, but it is "
628 << "'" << key_type << "'";
629 return false;
630 }
631 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800632 } else if (is_user_defined_generic_type) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900633 const size_t allowed = parameterizable->GetTypeParameters().size();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800634 if (num_params != allowed) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900635 AIDL_ERROR(this) << type_name << " must have " << allowed << " type parameters, but got "
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800636 << num_params;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900637 return false;
638 }
639 } else {
640 AIDL_ERROR(this) << type_name << " is not a generic type.";
641 return false;
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900642 }
643 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900644
Steven Moreland11cb9452020-01-21 16:56:58 -0800645 const bool is_generic_string_list = GetName() == "List" && IsGeneric() &&
646 GetTypeParameters().size() == 1 &&
647 GetTypeParameters()[0]->GetName() == "String";
648 if (IsUtf8InCpp() && (GetName() != "String" && !is_generic_string_list)) {
649 AIDL_ERROR(this) << "@utf8InCpp can only be used on String, String[], and List<String>.";
650 return false;
651 }
652
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900653 if (GetName() == "void") {
654 if (IsArray() || IsNullable() || IsUtf8InCpp()) {
655 AIDL_ERROR(this) << "void type cannot be an array or nullable or utf8 string";
656 return false;
657 }
658 }
659
660 if (IsArray()) {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800661 const auto defined_type = typenames.TryGetDefinedType(GetName());
662 if (defined_type != nullptr && defined_type->AsInterface() != nullptr) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900663 AIDL_ERROR(this) << "Binder type cannot be an array";
664 return false;
665 }
Steven Moreland8042d2d2020-09-30 23:31:32 +0000666 if (GetName() == "ParcelableHolder") {
667 AIDL_ERROR(this) << "Arrays of ParcelableHolder are not supported.";
668 return false;
669 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900670 }
671
672 if (IsNullable()) {
673 if (AidlTypenames::IsPrimitiveTypename(GetName()) && !IsArray()) {
674 AIDL_ERROR(this) << "Primitive type cannot get nullable annotation";
675 return false;
676 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800677 const auto defined_type = typenames.TryGetDefinedType(GetName());
678 if (defined_type != nullptr && defined_type->AsEnumDeclaration() != nullptr && !IsArray()) {
Daniel Normanee8674f2019-09-20 16:07:00 -0700679 AIDL_ERROR(this) << "Enum type cannot get nullable annotation";
680 return false;
681 }
Jeongik Chaf6ec8982020-10-15 00:10:30 +0900682 if (GetName() == "ParcelableHolder") {
683 AIDL_ERROR(this) << "ParcelableHolder cannot be nullable.";
684 return false;
685 }
Jooyung Han01720ed2021-08-13 07:46:07 +0900686 if (IsHeapNullable()) {
687 if (!defined_type || IsArray() || !defined_type->AsParcelable()) {
688 AIDL_ERROR(this) << "@nullable(heap=true) is available to parcelables.";
689 return false;
690 }
691 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900692 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900693 return true;
694}
695
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900696std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type,
Steven Moreland860b1942018-08-16 14:59:28 -0700697 const std::string& raw_value) {
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900698 if (type.IsArray()) {
699 return raw_value;
700 }
701
702 if (auto defined_type = type.GetDefinedType(); defined_type) {
703 auto enum_type = defined_type->AsEnumDeclaration();
704 AIDL_FATAL_IF(!enum_type, type) << "Invalid type for \"" << raw_value << "\"";
705 return type.GetName() + "." + raw_value.substr(raw_value.find_last_of('.') + 1);
706 }
Steven Moreland860b1942018-08-16 14:59:28 -0700707 return raw_value;
708}
709
Steven Moreland46e9da82018-07-27 15:45:29 -0700710AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
711 AidlTypeSpecifier* type, const std::string& name)
Steven Moreland541788d2020-05-21 22:05:52 +0000712 : AidlVariableDeclaration(location, type, name, AidlConstantValue::Default(*type)) {
713 default_user_specified_ = false;
714}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700715
Steven Moreland46e9da82018-07-27 15:45:29 -0700716AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
717 AidlTypeSpecifier* type, const std::string& name,
718 AidlConstantValue* default_value)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900719 : AidlMember(location, type->GetComments()),
Steven Moreland541788d2020-05-21 22:05:52 +0000720 type_(type),
721 name_(name),
722 default_user_specified_(true),
723 default_value_(default_value) {}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700724
Jooyung Han53fb4242020-12-17 16:03:49 +0900725bool AidlVariableDeclaration::HasUsefulDefaultValue() const {
726 if (GetDefaultValue()) {
727 return true;
728 }
729 // null is accepted as a valid default value in all backends
730 if (GetType().IsNullable()) {
731 return true;
732 }
733 return false;
734}
735
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900736bool AidlVariableDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700737 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900738 valid &= type_->CheckValid(typenames);
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900739
Steven Moreland54be7bd2019-12-05 11:17:53 -0800740 if (type_->GetName() == "void") {
741 AIDL_ERROR(this) << "Declaration " << name_
742 << " is void, but declarations cannot be of void type.";
743 valid = false;
744 }
745
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900746 if (default_value_ == nullptr) return valid;
Steven Moreland25294322018-08-07 18:13:55 -0700747 valid &= default_value_->CheckValid();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700748
Steven Moreland25294322018-08-07 18:13:55 -0700749 if (!valid) return false;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700750
Steven Moreland860b1942018-08-16 14:59:28 -0700751 return !ValueString(AidlConstantValueDecorator).empty();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700752}
Steven Moreland5557f1c2018-07-02 13:50:23 -0700753
Jooyung Hanacae85d2020-10-28 16:39:09 +0900754string AidlVariableDeclaration::GetCapitalizedName() const {
755 AIDL_FATAL_IF(name_.size() <= 0, *this) << "Name can't be empty.";
756 string str = name_;
757 str[0] = static_cast<char>(toupper(str[0]));
758 return str;
759}
760
Steven Moreland5557f1c2018-07-02 13:50:23 -0700761string AidlVariableDeclaration::ToString() const {
Jooyung Han965e31d2020-11-27 12:30:16 +0900762 string ret = type_->ToString() + " " + name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000763 if (default_value_ != nullptr && default_user_specified_) {
Steven Moreland860b1942018-08-16 14:59:28 -0700764 ret += " = " + ValueString(AidlConstantValueDecorator);
Steven Moreland9ea10e32018-07-19 15:26:09 -0700765 }
766 return ret;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700767}
768
Jiyong Park02da7422018-07-16 16:00:26 +0900769string AidlVariableDeclaration::Signature() const {
770 return type_->Signature() + " " + name_;
771}
772
Steven Moreland860b1942018-08-16 14:59:28 -0700773std::string AidlVariableDeclaration::ValueString(const ConstantValueDecorator& decorator) const {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900774 if (default_value_ != nullptr) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700775 return default_value_->ValueString(GetType(), decorator);
Jiyong Parka468e2a2018-08-29 21:25:18 +0900776 } else {
777 return "";
778 }
Steven Moreland25294322018-08-07 18:13:55 -0700779}
780
Jooyung Hanc5688f72021-01-05 15:41:48 +0900781void AidlVariableDeclaration::TraverseChildren(
782 std::function<void(const AidlNode&)> traverse) const {
783 traverse(GetType());
784 if (IsDefaultUserSpecified()) {
785 traverse(*GetDefaultValue());
786 }
787}
788
Steven Moreland46e9da82018-07-27 15:45:29 -0700789AidlArgument::AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
790 AidlTypeSpecifier* type, const std::string& name)
791 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700792 direction_(direction),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700793 direction_specified_(true) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700794
Steven Moreland46e9da82018-07-27 15:45:29 -0700795AidlArgument::AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type,
796 const std::string& name)
797 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700798 direction_(AidlArgument::IN_DIR),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700799 direction_specified_(false) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700800
Jooyung Han020d8d12021-02-26 17:23:02 +0900801static std::string to_string(AidlArgument::Direction direction) {
802 switch (direction) {
803 case AidlArgument::IN_DIR:
804 return "in";
805 case AidlArgument::OUT_DIR:
806 return "out";
807 case AidlArgument::INOUT_DIR:
808 return "inout";
809 }
810}
811
Jiyong Park02da7422018-07-16 16:00:26 +0900812string AidlArgument::GetDirectionSpecifier() const {
Casey Dahlinc378c992015-09-29 16:50:40 -0700813 string ret;
Casey Dahlinc378c992015-09-29 16:50:40 -0700814 if (direction_specified_) {
Jooyung Han020d8d12021-02-26 17:23:02 +0900815 ret = to_string(direction_);
Casey Dahlinc378c992015-09-29 16:50:40 -0700816 }
Casey Dahlinc378c992015-09-29 16:50:40 -0700817 return ret;
818}
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700819
Jiyong Park02da7422018-07-16 16:00:26 +0900820string AidlArgument::ToString() const {
Devin Mooreeccdb902020-03-24 16:22:40 -0700821 if (direction_specified_) {
822 return GetDirectionSpecifier() + " " + AidlVariableDeclaration::ToString();
823 } else {
824 return AidlVariableDeclaration::ToString();
825 }
Jiyong Park02da7422018-07-16 16:00:26 +0900826}
827
Jooyung Han020d8d12021-02-26 17:23:02 +0900828static std::string FormatDirections(const std::set<AidlArgument::Direction>& directions) {
829 std::vector<std::string> out;
830 for (const auto& d : directions) {
831 out.push_back(to_string(d));
832 }
833
834 if (out.size() <= 1) { // [] => "" or [A] => "A"
835 return Join(out, "");
836 } else if (out.size() == 2) { // [A,B] => "A or B"
837 return Join(out, " or ");
838 } else { // [A,B,C] => "A, B, or C"
839 out.back() = "or " + out.back();
840 return Join(out, ", ");
841 }
842}
843
844bool AidlArgument::CheckValid(const AidlTypenames& typenames) const {
845 if (!GetType().CheckValid(typenames)) {
846 return false;
847 }
848
849 const auto& aspect = typenames.GetArgumentAspect(GetType());
850
851 if (aspect.possible_directions.size() == 0) {
852 AIDL_ERROR(this) << aspect.name << " cannot be an argument type";
853 return false;
854 }
855
856 // when direction is not specified, "in" is assumed and should be the only possible direction
857 if (!DirectionWasSpecified() && aspect.possible_directions != std::set{AidlArgument::IN_DIR}) {
858 AIDL_ERROR(this) << "The direction of '" << GetName() << "' is not specified. " << aspect.name
859 << " can be an " << FormatDirections(aspect.possible_directions)
860 << " parameter.";
861 return false;
862 }
863
864 if (aspect.possible_directions.count(GetDirection()) == 0) {
865 AIDL_ERROR(this) << "'" << GetName() << "' can't be an " << GetDirectionSpecifier()
866 << " parameter because " << aspect.name << " can only be an "
867 << FormatDirections(aspect.possible_directions) << " parameter.";
868 return false;
869 }
870
871 return true;
872}
873
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900874bool AidlCommentable::IsHidden() const {
Jooyung Han24effbf2021-01-16 10:24:03 +0900875 return android::aidl::HasHideInComments(GetComments());
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900876}
877
878bool AidlCommentable::IsDeprecated() const {
Jooyung Hand4fe00e2021-01-11 16:21:53 +0900879 return android::aidl::FindDeprecated(GetComments()).has_value();
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900880}
881
Jooyung Han8451a202021-01-16 03:07:06 +0900882AidlMember::AidlMember(const AidlLocation& location, const Comments& comments)
Jooyung Han2aedb112021-09-29 09:37:59 +0900883 : AidlAnnotatable(location, comments) {}
Steven Moreland46e9da82018-07-27 15:45:29 -0700884
Steven Moreland46e9da82018-07-27 15:45:29 -0700885AidlConstantDeclaration::AidlConstantDeclaration(const AidlLocation& location,
886 AidlTypeSpecifier* type, const std::string& name,
887 AidlConstantValue* value)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900888 : AidlMember(location, type->GetComments()), type_(type), name_(name), value_(value) {}
Steven Moreland693640b2018-07-19 13:46:27 -0700889
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900890bool AidlConstantDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700891 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900892 valid &= type_->CheckValid(typenames);
Steven Moreland25294322018-08-07 18:13:55 -0700893 valid &= value_->CheckValid();
894 if (!valid) return false;
Steven Moreland693640b2018-07-19 13:46:27 -0700895
Steven Morelande689da22020-11-10 02:06:30 +0000896 const static set<string> kSupportedConstTypes = {"String", "byte", "int", "long"};
Jooyung Han965e31d2020-11-27 12:30:16 +0900897 if (kSupportedConstTypes.find(type_->Signature()) == kSupportedConstTypes.end()) {
898 AIDL_ERROR(this) << "Constant of type " << type_->Signature() << " is not supported.";
Steven Moreland693640b2018-07-19 13:46:27 -0700899 return false;
900 }
901
Will McVickerd7d18df2019-09-12 13:40:50 -0700902 return true;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700903}
904
Jiyong Parka428d212018-08-29 22:26:30 +0900905string AidlConstantDeclaration::ToString() const {
Jooyung Hanb3ca6302020-11-27 14:13:27 +0900906 return "const " + type_->ToString() + " " + name_ + " = " +
907 ValueString(AidlConstantValueDecorator);
Jiyong Parka428d212018-08-29 22:26:30 +0900908}
909
910string AidlConstantDeclaration::Signature() const {
911 return type_->Signature() + " " + name_;
912}
913
Steven Moreland46e9da82018-07-27 15:45:29 -0700914AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
915 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jooyung Han8451a202021-01-16 03:07:06 +0900916 const Comments& comments)
Jiyong Parkb034bf02018-07-30 17:44:33 +0900917 : AidlMethod(location, oneway, type, name, args, comments, 0, true) {
918 has_id_ = false;
919}
920
921AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
922 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jooyung Han8451a202021-01-16 03:07:06 +0900923 const Comments& comments, int id, bool is_user_defined)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900924 : AidlMember(location, comments),
Steven Moreland46e9da82018-07-27 15:45:29 -0700925 oneway_(oneway),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700926 type_(type),
927 name_(name),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700928 arguments_(std::move(*args)),
Jiyong Parkb034bf02018-07-30 17:44:33 +0900929 id_(id),
930 is_user_defined_(is_user_defined) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700931 has_id_ = true;
932 delete args;
Christopher Wileyad339272015-10-05 19:11:58 -0700933 for (const unique_ptr<AidlArgument>& a : arguments_) {
934 if (a->IsIn()) { in_arguments_.push_back(a.get()); }
935 if (a->IsOut()) { out_arguments_.push_back(a.get()); }
936 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700937}
938
Jiyong Park02da7422018-07-16 16:00:26 +0900939string AidlMethod::Signature() const {
940 vector<string> arg_signatures;
941 for (const auto& arg : GetArguments()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900942 arg_signatures.emplace_back(arg->GetType().Signature());
Jiyong Park02da7422018-07-16 16:00:26 +0900943 }
Jiyong Park309668e2018-07-28 16:55:44 +0900944 return GetName() + "(" + Join(arg_signatures, ", ") + ")";
945}
946
947string AidlMethod::ToString() const {
948 vector<string> arg_strings;
949 for (const auto& arg : GetArguments()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900950 arg_strings.emplace_back(arg->ToString());
Jiyong Park309668e2018-07-28 16:55:44 +0900951 }
Jooyung Han965e31d2020-11-27 12:30:16 +0900952 string ret = (IsOneway() ? "oneway " : "") + GetType().ToString() + " " + GetName() + "(" +
Steven Moreland4ee68632018-12-14 15:52:46 -0800953 Join(arg_strings, ", ") + ")";
Jiyong Parked65bf42018-08-28 15:43:27 +0900954 if (HasId()) {
955 ret += " = " + std::to_string(GetId());
956 }
957 return ret;
Jiyong Park02da7422018-07-16 16:00:26 +0900958}
959
Steven Moreland46e9da82018-07-27 15:45:29 -0700960AidlDefinedType::AidlDefinedType(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +0900961 const Comments& comments, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900962 std::vector<std::unique_ptr<AidlMember>>* members)
Jooyung Han2aedb112021-09-29 09:37:59 +0900963 : AidlMember(location, comments), AidlScope(this), name_(name), package_(package) {
Jooyung Han93f48f02021-06-05 00:11:16 +0900964 // adjust name/package when name is fully qualified (for preprocessed files)
965 if (package_.empty() && name_.find('.') != std::string::npos) {
966 // Note that this logic is absolutely wrong. Given a parcelable
967 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
968 // the class is just Bar. However, this was the way it was done in the past.
969 //
970 // See b/17415692
971 auto pos = name.rfind('.');
972 // name is the last part.
973 name_ = name.substr(pos + 1);
974 // package is the initial parts (except the last).
975 package_ = name.substr(0, pos);
976 }
Jooyung Han829ec7c2020-12-02 12:07:36 +0900977 if (members) {
978 for (auto& m : *members) {
Jooyung Hanbaa71062021-09-29 09:06:03 +0900979 if (auto constant = AidlCast<AidlConstantDeclaration>(*m); constant) {
Jooyung Han829ec7c2020-12-02 12:07:36 +0900980 constants_.emplace_back(constant);
Jooyung Hanbaa71062021-09-29 09:06:03 +0900981 } else if (auto variable = AidlCast<AidlVariableDeclaration>(*m); variable) {
Jooyung Han829ec7c2020-12-02 12:07:36 +0900982 variables_.emplace_back(variable);
Jooyung Hanbaa71062021-09-29 09:06:03 +0900983 } else if (auto method = AidlCast<AidlMethod>(*m); method) {
Jooyung Han829ec7c2020-12-02 12:07:36 +0900984 methods_.emplace_back(method);
Jooyung Han2aedb112021-09-29 09:37:59 +0900985 } else if (auto type = AidlCast<AidlDefinedType>(*m); type) {
986 type->SetEnclosingScope(this);
987 types_.emplace_back(type);
Jooyung Han829ec7c2020-12-02 12:07:36 +0900988 } else {
Jooyung Hanbaa71062021-09-29 09:06:03 +0900989 AIDL_FATAL(*m) << "Unknown member type.";
Jooyung Han829ec7c2020-12-02 12:07:36 +0900990 }
991 members_.push_back(m.release());
992 }
993 delete members;
994 }
995}
Steven Moreland787b0432018-07-03 09:00:58 -0700996
Jooyung Han808a2a02020-12-28 16:46:54 +0900997bool AidlDefinedType::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -0800998 if (!AidlAnnotatable::CheckValid(typenames)) {
999 return false;
1000 }
Jooyung Han829ec7c2020-12-02 12:07:36 +09001001 if (!CheckValidWithMembers(typenames)) {
1002 return false;
1003 }
Devin Moore24f68572020-02-26 13:20:59 -08001004 return true;
1005}
1006
Steven Moreland787b0432018-07-03 09:00:58 -07001007std::string AidlDefinedType::GetCanonicalName() const {
1008 if (package_.empty()) {
1009 return GetName();
1010 }
Jooyung Han2aedb112021-09-29 09:37:59 +09001011 if (auto parent = GetParentType(); parent) {
1012 return parent->GetCanonicalName() + "." + GetName();
1013 }
Steven Moreland787b0432018-07-03 09:00:58 -07001014 return GetPackage() + "." + GetName();
1015}
1016
Jooyung Han829ec7c2020-12-02 12:07:36 +09001017bool AidlDefinedType::CheckValidWithMembers(const AidlTypenames& typenames) const {
1018 bool success = true;
1019
Jooyung Han2aedb112021-09-29 09:37:59 +09001020 for (const auto& t : GetNestedTypes()) {
1021 success = success && t->CheckValid(typenames);
1022 }
1023
Jooyung Han7fc5de02021-09-30 22:26:27 +09001024 if (auto parameterizable = AsParameterizable();
1025 parameterizable && parameterizable->IsGeneric() && !GetNestedTypes().empty()) {
1026 AIDL_ERROR(this) << "Generic types can't have nested types.";
1027 return false;
1028 }
1029
Jooyung Han2aedb112021-09-29 09:37:59 +09001030 std::set<std::string> nested_type_names;
1031 for (const auto& t : GetNestedTypes()) {
1032 bool duplicated = !nested_type_names.emplace(t->GetName()).second;
1033 if (duplicated) {
1034 AIDL_ERROR(t) << "Redefinition of '" << t->GetName() << "'.";
1035 success = false;
1036 }
1037 // nested type can't have a parent name
1038 if (t->GetName() == GetName()) {
1039 AIDL_ERROR(t) << "Nested type '" << GetName() << "' has the same name as its parent.";
1040 success = false;
1041 }
Jooyung Han2b1487d2021-09-30 09:57:01 +09001042 // Having unstructured parcelables as nested types doesn't make sense because they are defined
1043 // somewhere else in native languages (e.g. C++, Java...).
1044 if (AidlCast<AidlParcelable>(*t)) {
1045 AIDL_ERROR(t) << "'" << t->GetName()
1046 << "' is nested. Unstructured parcelables should be at the root scope.";
1047 return false;
1048 }
Jooyung Han2aedb112021-09-29 09:37:59 +09001049 // For now we don't allow "interface" to be nested
1050 if (AidlCast<AidlInterface>(*t)) {
1051 AIDL_ERROR(t) << "'" << t->GetName()
1052 << "' is nested. Interfaces should be at the root scope.";
1053 return false;
1054 }
1055 }
1056
Jooyung Han8e9ae872021-10-13 02:52:25 +09001057 if (!TopologicalVisit(GetNestedTypes(), [](auto&) {})) {
1058 AIDL_ERROR(this) << GetName()
1059 << " has nested types with cyclic references. C++ and NDK backends don't "
1060 "support cyclic references.";
1061 return false;
1062 }
1063
Jooyung Han829ec7c2020-12-02 12:07:36 +09001064 for (const auto& v : GetFields()) {
1065 const bool field_valid = v->CheckValid(typenames);
1066 success = success && field_valid;
1067 }
1068
1069 // field names should be unique
1070 std::set<std::string> fieldnames;
1071 for (const auto& v : GetFields()) {
1072 bool duplicated = !fieldnames.emplace(v->GetName()).second;
1073 if (duplicated) {
1074 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName() << "'";
1075 success = false;
1076 }
1077 }
1078
1079 // immutable parcelables should have immutable fields.
1080 if (IsJavaOnlyImmutable()) {
1081 for (const auto& v : GetFields()) {
1082 if (!typenames.CanBeJavaOnlyImmutable(v->GetType())) {
1083 AIDL_ERROR(v) << "The @JavaOnlyImmutable '" << GetName() << "' has a "
1084 << "non-immutable field named '" << v->GetName() << "'.";
1085 success = false;
1086 }
1087 }
1088 }
1089
1090 set<string> constant_names;
1091 for (const auto& constant : GetConstantDeclarations()) {
1092 if (constant_names.count(constant->GetName()) > 0) {
1093 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
1094 success = false;
1095 }
1096 constant_names.insert(constant->GetName());
1097 success = success && constant->CheckValid(typenames);
1098 }
1099
1100 return success;
1101}
1102
1103bool AidlDefinedType::CheckValidForGetterNames() const {
1104 bool success = true;
1105 std::set<std::string> getters;
1106 for (const auto& v : GetFields()) {
1107 bool duplicated = !getters.emplace(v->GetCapitalizedName()).second;
1108 if (duplicated) {
1109 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName()
1110 << "' after capitalizing the first letter";
1111 success = false;
1112 }
1113 }
1114 return success;
1115}
1116
Jooyung Han2aedb112021-09-29 09:37:59 +09001117const AidlDefinedType* AidlDefinedType::GetParentType() const {
1118 AIDL_FATAL_IF(GetEnclosingScope() == nullptr, this) << "Scope is not set.";
1119 return AidlCast<AidlDefinedType>(GetEnclosingScope()->GetNode());
1120}
1121
Jooyung Hanf8c39632021-10-05 09:56:29 +09001122const AidlDefinedType* AidlDefinedType::GetRootType() const {
1123 const AidlDefinedType* root = this;
1124 for (auto parent = root->GetParentType(); parent; parent = parent->GetParentType()) {
1125 root = parent;
1126 }
1127 return root;
1128}
1129
Jooyung Han2aedb112021-09-29 09:37:59 +09001130// Resolve `name` in the current scope. If not found, delegate to the parent
Jooyung Han13f1fa52021-06-11 18:06:12 +09001131std::string AidlDefinedType::ResolveName(const std::string& name) const {
Jooyung Han2aedb112021-09-29 09:37:59 +09001132 // For example, in the following, t1's type Baz means x.Foo.Bar.Baz
1133 // while t2's type is y.Baz.
Jooyung Han13f1fa52021-06-11 18:06:12 +09001134 // package x;
Jooyung Han2aedb112021-09-29 09:37:59 +09001135 // import y.Baz;
Jooyung Han13f1fa52021-06-11 18:06:12 +09001136 // parcelable Foo {
1137 // parcelable Bar {
Jooyung Han2aedb112021-09-29 09:37:59 +09001138 // enum Baz { ... }
1139 // Baz t1; // -> should be x.Foo.Bar.Baz
Jooyung Han13f1fa52021-06-11 18:06:12 +09001140 // }
Jooyung Han2aedb112021-09-29 09:37:59 +09001141 // Baz t2; // -> should be y.Baz
1142 // Bar.Baz t3; // -> should be x.Foo.Bar.Baz
Jooyung Han13f1fa52021-06-11 18:06:12 +09001143 // }
1144 AIDL_FATAL_IF(!GetEnclosingScope(), this)
1145 << "Type should have an enclosing scope.(e.g. AidlDocument)";
Jooyung Han2aedb112021-09-29 09:37:59 +09001146 if (AidlTypenames::IsBuiltinTypename(name)) {
1147 return name;
1148 }
1149
1150 const auto first_dot = name.find_first_of('.');
1151 // For "Outer.Inner", we look up "Outer" in the import list.
1152 const std::string class_name =
1153 (first_dot == std::string::npos) ? name : name.substr(0, first_dot);
1154 // Keep ".Inner", to make a fully-qualified name
1155 const std::string nested_type = (first_dot == std::string::npos) ? "" : name.substr(first_dot);
1156
1157 // check if it is a nested type
1158 for (const auto& type : GetNestedTypes()) {
1159 if (type->GetName() == class_name) {
1160 return type->GetCanonicalName() + nested_type;
1161 }
1162 }
1163
Jooyung Han13f1fa52021-06-11 18:06:12 +09001164 return GetEnclosingScope()->ResolveName(name);
1165}
1166
Jooyung Hanbaa71062021-09-29 09:06:03 +09001167template <>
1168const AidlDefinedType* AidlCast<AidlDefinedType>(const AidlNode& node) {
1169 struct Visitor : AidlVisitor {
1170 const AidlDefinedType* defined_type = nullptr;
1171 void Visit(const AidlInterface& t) override { defined_type = &t; }
1172 void Visit(const AidlEnumDeclaration& t) override { defined_type = &t; }
1173 void Visit(const AidlStructuredParcelable& t) override { defined_type = &t; }
1174 void Visit(const AidlUnionDecl& t) override { defined_type = &t; }
1175 void Visit(const AidlParcelable& t) override { defined_type = &t; }
1176 } v;
1177 node.DispatchVisit(v);
1178 return v.defined_type;
Jooyung Han35784982021-06-29 06:26:12 +09001179}
1180
1181const AidlDocument& AidlDefinedType::GetDocument() const {
Jooyung Hanf8c39632021-10-05 09:56:29 +09001182 const AidlDefinedType* root = GetRootType();
1183 auto scope = root->GetEnclosingScope();
Jooyung Han35784982021-06-29 06:26:12 +09001184 AIDL_FATAL_IF(!scope, this) << "no scope defined.";
1185 auto doc = AidlCast<AidlDocument>(scope->GetNode());
Jooyung Hanf8c39632021-10-05 09:56:29 +09001186 AIDL_FATAL_IF(!doc, this) << "root scope is not a document.";
Jooyung Han35784982021-06-29 06:26:12 +09001187 return *doc;
1188}
1189
Jiyong Park18132182020-06-08 20:24:40 +09001190AidlParcelable::AidlParcelable(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001191 const std::string& package, const Comments& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001192 const std::string& cpp_header, std::vector<std::string>* type_params,
1193 std::vector<std::unique_ptr<AidlMember>>* members)
1194 : AidlDefinedType(location, name, comments, package, members),
Jeongik Chadf76dc72019-11-28 00:08:47 +09001195 AidlParameterizable<std::string>(type_params),
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -08001196 cpp_header_(cpp_header) {
1197 // Strip off quotation marks if we actually have a cpp header.
1198 if (cpp_header_.length() >= 2) {
1199 cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2);
1200 }
Casey Dahlin59401da2015-10-09 18:16:45 -07001201}
Jeongik Chadf76dc72019-11-28 00:08:47 +09001202template <typename T>
1203AidlParameterizable<T>::AidlParameterizable(const AidlParameterizable& other) {
1204 // Copying is not supported if it has type parameters.
Steven Moreland21780812020-09-11 01:29:45 +00001205 AIDL_FATAL_IF(other.IsGeneric(), AIDL_LOCATION_HERE);
Jeongik Chadf76dc72019-11-28 00:08:47 +09001206}
1207
1208template <typename T>
1209bool AidlParameterizable<T>::CheckValid() const {
1210 return true;
1211};
1212
1213template <>
1214bool AidlParameterizable<std::string>::CheckValid() const {
1215 if (!IsGeneric()) {
1216 return true;
1217 }
1218 std::unordered_set<std::string> set(GetTypeParameters().begin(), GetTypeParameters().end());
1219 if (set.size() != GetTypeParameters().size()) {
1220 AIDL_ERROR(this->AsAidlNode()) << "Every type parameter should be unique.";
1221 return false;
1222 }
1223 return true;
1224}
Casey Dahlin59401da2015-10-09 18:16:45 -07001225
Jooyung Han808a2a02020-12-28 16:46:54 +09001226bool AidlParcelable::CheckValid(const AidlTypenames& typenames) const {
1227 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001228 return false;
1229 }
Jeongik Chadf76dc72019-11-28 00:08:47 +09001230 if (!AidlParameterizable<std::string>::CheckValid()) {
1231 return false;
1232 }
Jeongik Cha82317dd2019-02-27 20:26:11 +09001233
1234 return true;
1235}
1236
Steven Moreland5557f1c2018-07-02 13:50:23 -07001237AidlStructuredParcelable::AidlStructuredParcelable(
Jiyong Park18132182020-06-08 20:24:40 +09001238 const AidlLocation& location, const std::string& name, const std::string& package,
Jooyung Han8451a202021-01-16 03:07:06 +09001239 const Comments& comments, std::vector<std::string>* type_params,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001240 std::vector<std::unique_ptr<AidlMember>>* members)
1241 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Steven Moreland5557f1c2018-07-02 13:50:23 -07001242
Jooyung Han808a2a02020-12-28 16:46:54 +09001243bool AidlStructuredParcelable::CheckValid(const AidlTypenames& typenames) const {
1244 if (!AidlParcelable::CheckValid(typenames)) {
Devin Moore24f68572020-02-26 13:20:59 -08001245 return false;
1246 }
Jeongik Cha13066da2020-08-06 15:43:19 +09001247
Jooyung Han59af9cc2020-10-25 21:44:14 +09001248 bool success = true;
Jeongik Cha36f76c32020-07-28 00:25:52 +09001249
Jooyung Hand4057c42020-10-23 13:28:22 +09001250 if (IsFixedSize()) {
1251 for (const auto& v : GetFields()) {
1252 if (!typenames.CanBeFixedSize(v->GetType())) {
1253 AIDL_ERROR(v) << "The @FixedSize parcelable '" << this->GetName() << "' has a "
1254 << "non-fixed size field named " << v->GetName() << ".";
1255 success = false;
1256 }
1257 }
1258 }
1259
1260 if (IsJavaOnlyImmutable()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001261 // Immutable parcelables provide getters
Jooyung Han829ec7c2020-12-02 12:07:36 +09001262 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001263 success = false;
Jooyung Hand4057c42020-10-23 13:28:22 +09001264 }
1265 }
1266
Daniel Norman85aed542019-08-21 12:01:14 -07001267 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001268}
1269
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001270// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -07001271bool AidlTypeSpecifier::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1272 Options::Language lang) const {
Jooyung Hand09a21d2021-02-15 18:56:55 +09001273 if (IsGeneric()) {
1274 const auto& types = GetTypeParameters();
1275 for (const auto& arg : types) {
1276 if (!arg->LanguageSpecificCheckValid(typenames, lang)) {
1277 return false;
1278 }
1279 }
1280 }
1281
Andrei Homescub62afd92020-05-11 19:24:59 -07001282 if ((lang == Options::Language::NDK || lang == Options::Language::RUST) && IsArray() &&
1283 GetName() == "IBinder") {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001284 AIDL_ERROR(this) << "The " << to_string(lang) << " backend does not support array of IBinder";
Steven Moreland0185d9b2020-05-15 23:21:22 +00001285 return false;
1286 }
Andrei Homescub62afd92020-05-11 19:24:59 -07001287 if ((lang == Options::Language::NDK || lang == Options::Language::RUST) && IsArray() &&
1288 IsNullable()) {
Steven Moreland0185d9b2020-05-15 23:21:22 +00001289 if (GetName() == "ParcelFileDescriptor") {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001290 AIDL_ERROR(this) << "The " << to_string(lang)
Andrei Homescub62afd92020-05-11 19:24:59 -07001291 << " backend does not support nullable array of ParcelFileDescriptor";
Steven Moreland0185d9b2020-05-15 23:21:22 +00001292 return false;
1293 }
1294
Steven Morelandd59e3172020-05-11 16:42:09 -07001295 const auto defined_type = typenames.TryGetDefinedType(GetName());
1296 if (defined_type != nullptr && defined_type->AsParcelable() != nullptr) {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001297 AIDL_ERROR(this) << "The " << to_string(lang)
Andrei Homescub62afd92020-05-11 19:24:59 -07001298 << " backend does not support nullable array of parcelable";
Steven Morelandd59e3172020-05-11 16:42:09 -07001299 return false;
1300 }
1301 }
Andrei Homescub62afd92020-05-11 19:24:59 -07001302 if (this->GetName() == "FileDescriptor" &&
1303 (lang == Options::Language::NDK || lang == Options::Language::RUST)) {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001304 AIDL_ERROR(this) << "FileDescriptor isn't supported by the " << to_string(lang) << " backend.";
Steven Morelandc8a4ca82020-01-21 17:50:08 -08001305 return false;
1306 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001307 if (this->IsGeneric()) {
1308 if (this->GetName() == "List") {
Jooyung Han55f96ad2020-12-13 10:08:33 +09001309 if (lang == Options::Language::NDK) {
1310 const AidlTypeSpecifier& contained_type = *GetTypeParameters()[0];
1311 const string& contained_type_name = contained_type.GetName();
Jooyung Hane87cdd02020-12-11 16:47:35 +09001312 if (typenames.GetInterface(contained_type)) {
1313 AIDL_ERROR(this) << "List<" << contained_type_name
1314 << "> is not supported. List in NDK doesn't support interface.";
1315 return false;
1316 }
1317 if (contained_type_name == "IBinder") {
1318 AIDL_ERROR(this) << "List<" << contained_type_name
1319 << "> is not supported. List in NDK doesn't support IBinder.";
1320 return false;
Jeongik Cha08ca2182019-11-21 14:01:13 +09001321 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001322 }
Jeongik Chabb55b5e2020-01-07 23:11:26 +09001323 }
1324 }
Devin Moore6a01ca12020-08-28 10:24:19 -07001325
1326 if (this->IsArray()) {
1327 if (this->GetName() == "List" || this->GetName() == "Map" ||
1328 this->GetName() == "CharSequence") {
1329 AIDL_ERROR(this) << this->GetName() << "[] is not supported.";
Jeongik Chabb55b5e2020-01-07 23:11:26 +09001330 return false;
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001331 }
1332 }
Devin Moore6a01ca12020-08-28 10:24:19 -07001333
1334 if (lang != Options::Language::JAVA) {
1335 if (this->GetName() == "List" && !this->IsGeneric()) {
1336 AIDL_ERROR(this) << "Currently, only the Java backend supports non-generic List.";
1337 return false;
1338 }
1339 if (this->GetName() == "Map" || this->GetName() == "CharSequence") {
1340 AIDL_ERROR(this) << "Currently, only Java backend supports " << this->GetName() << ".";
1341 return false;
Jeongik Cha08ca2182019-11-21 14:01:13 +09001342 }
1343 }
1344
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001345 return true;
1346}
1347
1348// TODO: we should treat every backend all the same in future.
Jooyung Han589cfb02021-09-28 17:26:04 +09001349bool AidlDefinedType::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1350 Options::Language lang) const {
1351 struct Visitor : AidlVisitor {
1352 Visitor(const AidlTypenames& typenames, Options::Language lang)
1353 : typenames(typenames), lang(lang) {}
1354 void Visit(const AidlTypeSpecifier& type) override {
1355 success = success && type.LanguageSpecificCheckValid(typenames, lang);
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001356 }
Jooyung Han589cfb02021-09-28 17:26:04 +09001357 const AidlTypenames& typenames;
1358 Options::Language lang;
1359 bool success = true;
1360 } v(typenames, lang);
1361 VisitTopDown(v, *this);
1362 return v.success;
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001363}
1364
Daniel Norman85aed542019-08-21 12:01:14 -07001365AidlEnumerator::AidlEnumerator(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001366 AidlConstantValue* value, const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +09001367 : AidlCommentable(location, comments),
Jooyung Han29813842020-12-08 01:28:03 +09001368 name_(name),
1369 value_(value),
Jooyung Han29813842020-12-08 01:28:03 +09001370 value_user_specified_(value != nullptr) {}
Daniel Norman85aed542019-08-21 12:01:14 -07001371
1372bool AidlEnumerator::CheckValid(const AidlTypeSpecifier& enum_backing_type) const {
1373 if (GetValue() == nullptr) {
1374 return false;
1375 }
1376 if (!GetValue()->CheckValid()) {
1377 return false;
1378 }
Will McVickerd7d18df2019-09-12 13:40:50 -07001379 if (GetValue()->ValueString(enum_backing_type, AidlConstantValueDecorator).empty()) {
Daniel Norman85aed542019-08-21 12:01:14 -07001380 AIDL_ERROR(this) << "Enumerator type differs from enum backing type.";
1381 return false;
1382 }
1383 return true;
1384}
1385
1386string AidlEnumerator::ValueString(const AidlTypeSpecifier& backing_type,
1387 const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -07001388 return GetValue()->ValueString(backing_type, decorator);
Daniel Norman85aed542019-08-21 12:01:14 -07001389}
1390
1391AidlEnumDeclaration::AidlEnumDeclaration(const AidlLocation& location, const std::string& name,
1392 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jooyung Han8451a202021-01-16 03:07:06 +09001393 const std::string& package, const Comments& comments)
Jooyung Han829ec7c2020-12-02 12:07:36 +09001394 : AidlDefinedType(location, name, comments, package, nullptr),
Jooyung Han29813842020-12-08 01:28:03 +09001395 enumerators_(std::move(*enumerators)) {
Jooyung Han672557b2020-12-24 05:18:00 +09001396 // Fill missing enumerator values with <prev + 1>
1397 // This can't be done in Autofill() because type/ref resolution depends on this.
1398 // For example, with enum E { A, B = A }, B's value 'A' is a reference which can't be
1399 // resolved if A has no value set.
Daniel Normanb28684e2019-10-17 15:31:39 -07001400 const AidlEnumerator* previous = nullptr;
1401 for (const auto& enumerator : enumerators_) {
1402 if (enumerator->GetValue() == nullptr) {
Jooyung Han29813842020-12-08 01:28:03 +09001403 auto loc = enumerator->GetLocation();
Daniel Normanb28684e2019-10-17 15:31:39 -07001404 if (previous == nullptr) {
Devin Mooredf93ebb2020-03-25 14:03:35 -07001405 enumerator->SetValue(
Jooyung Han29813842020-12-08 01:28:03 +09001406 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "0")));
Daniel Normanb28684e2019-10-17 15:31:39 -07001407 } else {
Jooyung Hand0c8af02021-01-06 18:08:01 +09001408 auto prev_value = std::make_unique<AidlConstantReference>(loc, previous->GetName());
Daniel Normanb28684e2019-10-17 15:31:39 -07001409 enumerator->SetValue(std::make_unique<AidlBinaryConstExpression>(
Jooyung Han29813842020-12-08 01:28:03 +09001410 loc, std::move(prev_value), "+",
1411 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "1"))));
Daniel Normanb28684e2019-10-17 15:31:39 -07001412 }
1413 }
1414 previous = enumerator.get();
1415 }
1416}
1417
Jooyung Han672557b2020-12-24 05:18:00 +09001418bool AidlEnumDeclaration::Autofill(const AidlTypenames& typenames) {
1419 if (auto annot = BackingType(); annot != nullptr) {
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001420 // Autofill() is called before the grand CheckValid(). But AidlAnnotation::ParamValue()
1421 // calls AidlConstantValue::evaluate() which requires CheckValid() to be called before. So we
Jooyung Han672557b2020-12-24 05:18:00 +09001422 // need to call CheckValid().
1423 if (!annot->CheckValid()) {
1424 return false;
1425 }
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001426 auto type = annot->ParamValue<std::string>("type").value();
1427 backing_type_ =
Jooyung Han8451a202021-01-16 03:07:06 +09001428 std::make_unique<AidlTypeSpecifier>(annot->GetLocation(), type, false, nullptr, Comments{});
Jooyung Han672557b2020-12-24 05:18:00 +09001429 } else {
1430 // Default to byte type for enums.
1431 backing_type_ =
Jooyung Han8451a202021-01-16 03:07:06 +09001432 std::make_unique<AidlTypeSpecifier>(AIDL_LOCATION_HERE, "byte", false, nullptr, Comments{});
Jooyung Han672557b2020-12-24 05:18:00 +09001433 }
Steven Morelandb248d072021-09-29 19:07:17 -07001434
1435 // we only support/test a few backing types, so make sure this is a supported
1436 // one (otherwise boolean might work, which isn't supported/tested in all
1437 // backends)
1438 static std::set<string> kBackingTypes = {"byte", "int", "long"};
1439 if (kBackingTypes.find(backing_type_->GetName()) == kBackingTypes.end()) {
1440 AIDL_ERROR(this) << "Invalid backing type: " << backing_type_->GetName()
1441 << ". Backing type must be one of: " << Join(kBackingTypes, ", ");
1442 return false;
Jooyung Han672557b2020-12-24 05:18:00 +09001443 }
Steven Morelandb248d072021-09-29 19:07:17 -07001444
1445 // Autofill() is called before type resolution, we resolve the backing type manually.
1446 AIDL_FATAL_IF(!backing_type_->Resolve(typenames, nullptr),
1447 "supporting backing types must resolve");
1448
Jooyung Han672557b2020-12-24 05:18:00 +09001449 return true;
1450}
1451
Jooyung Han808a2a02020-12-28 16:46:54 +09001452bool AidlEnumDeclaration::CheckValid(const AidlTypenames& typenames) const {
1453 if (!AidlDefinedType::CheckValid(typenames)) {
Devin Moore24f68572020-02-26 13:20:59 -08001454 return false;
1455 }
Jooyung Han829ec7c2020-12-02 12:07:36 +09001456 if (!GetMembers().empty()) {
1457 AIDL_ERROR(this) << "Enum doesn't support fields/constants/methods.";
1458 return false;
1459 }
Daniel Norman85aed542019-08-21 12:01:14 -07001460 if (backing_type_ == nullptr) {
1461 AIDL_ERROR(this) << "Enum declaration missing backing type.";
1462 return false;
1463 }
1464 bool success = true;
1465 for (const auto& enumerator : enumerators_) {
1466 success = success && enumerator->CheckValid(GetBackingType());
1467 }
Jooyung Han3b990182020-12-22 17:44:31 +09001468
Jooyung Han808a2a02020-12-28 16:46:54 +09001469 return success;
Daniel Norman85aed542019-08-21 12:01:14 -07001470}
1471
Jooyung Han2946afc2020-10-05 20:29:16 +09001472AidlUnionDecl::AidlUnionDecl(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001473 const std::string& package, const Comments& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001474 std::vector<std::string>* type_params,
1475 std::vector<std::unique_ptr<AidlMember>>* members)
1476 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Jooyung Han2946afc2020-10-05 20:29:16 +09001477
Jooyung Han808a2a02020-12-28 16:46:54 +09001478bool AidlUnionDecl::CheckValid(const AidlTypenames& typenames) const {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001479 // visit parents
Jooyung Han808a2a02020-12-28 16:46:54 +09001480 if (!AidlParcelable::CheckValid(typenames)) {
Jooyung Hanfe89f122020-10-14 03:49:18 +09001481 return false;
1482 }
Jooyung Han59af9cc2020-10-25 21:44:14 +09001483
1484 // unions provide getters always
Jooyung Han829ec7c2020-12-02 12:07:36 +09001485 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001486 return false;
Jooyung Hanfe89f122020-10-14 03:49:18 +09001487 }
1488
1489 // now, visit self!
1490 bool success = true;
1491
1492 // TODO(b/170807936) do we need to allow ParcelableHolder in union?
1493 for (const auto& v : GetFields()) {
1494 if (v->GetType().GetName() == "ParcelableHolder") {
1495 AIDL_ERROR(*v) << "A union can't have a member of ParcelableHolder '" << v->GetName() << "'";
1496 success = false;
1497 }
1498 }
1499
Jooyung Hanfe89f122020-10-14 03:49:18 +09001500 if (GetFields().empty()) {
1501 AIDL_ERROR(*this) << "The union '" << this->GetName() << "' has no fields.";
1502 return false;
1503 }
1504
Jooyung Han53fb4242020-12-17 16:03:49 +09001505 // first member should have useful default value (implicit or explicit)
1506 const auto& first = GetFields()[0];
1507 if (!first->HasUsefulDefaultValue()) {
1508 // Most types can be initialized without a default value. For example,
1509 // interface types are inherently nullable. But, enum types should have
1510 // an explicit default value.
1511 if (!first->GetType().IsArray() && typenames.GetEnumDeclaration(first->GetType())) {
1512 AIDL_ERROR(first)
1513 << "The union's first member should have a useful default value. Enum types can be "
1514 "initialized with a reference. (e.g. ... = MyEnum.FOO;)";
1515 return false;
1516 }
1517 // In Java, array types are initialized as null without a default value. To be sure that default
1518 // initialized unions are accepted by other backends we require arrays also have a default
1519 // value.
1520 if (first->GetType().IsArray()) {
1521 AIDL_ERROR(first)
1522 << "The union's first member should have a useful default value. Arrays can be "
1523 "initialized with values(e.g. ... = { values... };) or marked as @nullable.";
1524 return false;
1525 }
1526 }
1527
Jooyung Hanfe89f122020-10-14 03:49:18 +09001528 return success;
1529}
1530
Steven Moreland46e9da82018-07-27 15:45:29 -07001531AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001532 const Comments& comments, bool oneway, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001533 std::vector<std::unique_ptr<AidlMember>>* members)
1534 : AidlDefinedType(location, name, comments, package, members) {
1535 for (auto& m : GetMethods()) {
1536 m.get()->ApplyInterfaceOneway(oneway);
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001537 }
Casey Dahlinfb7da2e2015-10-08 17:26:09 -07001538}
1539
Jooyung Han808a2a02020-12-28 16:46:54 +09001540bool AidlInterface::CheckValid(const AidlTypenames& typenames) const {
1541 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001542 return false;
1543 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001544 // Has to be a pointer due to deleting copy constructor. No idea why.
1545 map<string, const AidlMethod*> method_names;
1546 for (const auto& m : GetMethods()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001547 if (!m->GetType().CheckValid(typenames)) {
1548 return false;
1549 }
1550
Jeongik Cha649e8a72020-03-27 17:47:40 +09001551 // TODO(b/156872582): Support it when ParcelableHolder supports every backend.
1552 if (m->GetType().GetName() == "ParcelableHolder") {
1553 AIDL_ERROR(m) << "ParcelableHolder cannot be a return type";
1554 return false;
1555 }
Steven Morelandacd53472018-12-14 10:17:26 -08001556 if (m->IsOneway() && m->GetType().GetName() != "void") {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001557 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot return a value";
1558 return false;
1559 }
1560
1561 set<string> argument_names;
1562 for (const auto& arg : m->GetArguments()) {
1563 auto it = argument_names.find(arg->GetName());
1564 if (it != argument_names.end()) {
1565 AIDL_ERROR(m) << "method '" << m->GetName() << "' has duplicate argument name '"
1566 << arg->GetName() << "'";
1567 return false;
1568 }
1569 argument_names.insert(arg->GetName());
1570
Jooyung Han020d8d12021-02-26 17:23:02 +09001571 if (!arg->CheckValid(typenames)) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001572 return false;
1573 }
1574
Steven Morelandacd53472018-12-14 10:17:26 -08001575 if (m->IsOneway() && arg->IsOut()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001576 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot have out parameters";
1577 return false;
1578 }
Jooyung Han15fd6c62020-10-23 13:54:46 +09001579
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001580 // check that the name doesn't match a keyword
Jeongik Cha997281d2020-01-16 15:23:59 +09001581 if (IsJavaKeyword(arg->GetName().c_str())) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001582 AIDL_ERROR(arg) << "Argument name is a Java or aidl keyword";
1583 return false;
1584 }
1585
1586 // Reserve a namespace for internal use
1587 if (android::base::StartsWith(arg->GetName(), "_aidl")) {
1588 AIDL_ERROR(arg) << "Argument name cannot begin with '_aidl'";
1589 return false;
1590 }
Jooyung Hanfa181932021-06-12 07:56:53 +09001591
1592 if (arg->GetType().GetName() == "void") {
1593 AIDL_ERROR(arg->GetType())
1594 << "'void' is an invalid type for the parameter '" << arg->GetName() << "'";
1595 return false;
1596 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001597 }
1598
1599 auto it = method_names.find(m->GetName());
1600 // prevent duplicate methods
1601 if (it == method_names.end()) {
1602 method_names[m->GetName()] = m.get();
1603 } else {
1604 AIDL_ERROR(m) << "attempt to redefine method " << m->GetName() << ":";
1605 AIDL_ERROR(it->second) << "previously defined here.";
1606 return false;
1607 }
1608
Paul Trautrimb77048c2020-01-21 16:39:32 +09001609 static set<string> reserved_methods{"asBinder()", "getInterfaceHash()", "getInterfaceVersion()",
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001610 "getTransactionName(int)"};
1611
1612 if (reserved_methods.find(m->Signature()) != reserved_methods.end()) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001613 AIDL_ERROR(m) << " method " << m->Signature() << " is reserved for internal use.";
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001614 return false;
1615 }
1616 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001617
1618 bool success = true;
1619 set<string> constant_names;
Jooyung Han3f347ca2020-12-01 12:41:50 +09001620 for (const auto& constant : GetConstantDeclarations()) {
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001621 if (constant_names.count(constant->GetName()) > 0) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001622 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001623 success = false;
1624 }
1625 constant_names.insert(constant->GetName());
1626 success = success && constant->CheckValid(typenames);
1627 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001628 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001629}
1630
Jiyong Park27fd7fd2020-08-27 16:25:09 +09001631std::string AidlInterface::GetDescriptor() const {
1632 std::string annotatedDescriptor = AidlAnnotatable::GetDescriptor();
1633 if (annotatedDescriptor != "") {
1634 return annotatedDescriptor;
1635 }
1636 return GetCanonicalName();
1637}
1638
Jooyung Han132cf802021-01-15 02:17:32 +09001639AidlImport::AidlImport(const AidlLocation& location, const std::string& needed_class,
Jooyung Han8451a202021-01-16 03:07:06 +09001640 const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +09001641 : AidlNode(location, comments), needed_class_(needed_class) {}
Jooyung Han29813842020-12-08 01:28:03 +09001642
Jooyung Han13f1fa52021-06-11 18:06:12 +09001643AidlDocument::AidlDocument(const AidlLocation& location, const Comments& comments,
1644 std::vector<std::unique_ptr<AidlImport>> imports,
Jooyung Han35784982021-06-29 06:26:12 +09001645 std::vector<std::unique_ptr<AidlDefinedType>> defined_types,
1646 bool is_preprocessed)
Jooyung Han13f1fa52021-06-11 18:06:12 +09001647 : AidlCommentable(location, comments),
Jooyung Han35784982021-06-29 06:26:12 +09001648 AidlScope(this),
Jooyung Han13f1fa52021-06-11 18:06:12 +09001649 imports_(std::move(imports)),
Jooyung Han35784982021-06-29 06:26:12 +09001650 defined_types_(std::move(defined_types)),
1651 is_preprocessed_(is_preprocessed) {
Jooyung Han13f1fa52021-06-11 18:06:12 +09001652 for (const auto& t : defined_types_) {
1653 t->SetEnclosingScope(this);
1654 }
1655}
1656
1657// Resolves type name in the current document.
1658// - built-in types
1659// - imported types
1660// - top-level type
1661std::string AidlDocument::ResolveName(const std::string& name) const {
1662 if (AidlTypenames::IsBuiltinTypename(name)) {
1663 return name;
1664 }
1665
1666 const auto first_dot = name.find_first_of('.');
1667 // For "Outer.Inner", we look up "Outer" in the import list.
Jooyung Han29813842020-12-08 01:28:03 +09001668 const std::string class_name =
Jooyung Han13f1fa52021-06-11 18:06:12 +09001669 (first_dot == std::string::npos) ? name : name.substr(0, first_dot);
1670 // Keep ".Inner", to make a fully-qualified name
1671 const std::string nested_type = (first_dot == std::string::npos) ? "" : name.substr(first_dot);
1672
Jooyung Han29813842020-12-08 01:28:03 +09001673 for (const auto& import : Imports()) {
Jooyung Han13f1fa52021-06-11 18:06:12 +09001674 if (import->SimpleName() == class_name) {
1675 return import->GetNeededClass() + nested_type;
Jooyung Han29813842020-12-08 01:28:03 +09001676 }
1677 }
Jooyung Han13f1fa52021-06-11 18:06:12 +09001678
1679 // check if it is a top-level type.
1680 for (const auto& type : DefinedTypes()) {
1681 if (type->GetName() == class_name) {
1682 return type->GetCanonicalName() + nested_type;
1683 }
Jooyung Han29813842020-12-08 01:28:03 +09001684 }
Jooyung Han13f1fa52021-06-11 18:06:12 +09001685
1686 // name itself might be fully-qualified name.
1687 return name;
Steven Moreland26318532020-12-23 20:08:36 +00001688}