blob: 39db473732ce325e22e823bcff4c8d81b99ff77d [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
Steven Morelanda7560e82021-10-08 16:24:39 -0700355static const AidlAnnotation* GetAnnotation(
356 const vector<std::unique_ptr<AidlAnnotation>>& annotations, AidlAnnotation::Type type) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100357 for (const auto& a : annotations) {
Steven Morelanda7560e82021-10-08 16:24:39 -0700358 if (a->GetType() == type) {
359 AIDL_FATAL_IF(a->Repeatable(), a)
Jooyung Hand902a972020-10-23 17:32:44 +0900360 << "Trying to get a single annotation when it is repeatable.";
Steven Morelanda7560e82021-10-08 16:24:39 -0700361 return a.get();
Andrei Onea9445fc62019-06-27 18:11:59 +0100362 }
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()) {
Steven Morelanda7560e82021-10-08 16:24:39 -0700466 if (!annotation->CheckValid()) {
Jooyung Hand902a972020-10-23 17:32:44 +0900467 return false;
468 }
469 }
470
471 std::map<AidlAnnotation::Type, AidlLocation> declared;
472 for (const auto& annotation : GetAnnotations()) {
Steven Morelanda7560e82021-10-08 16:24:39 -0700473 const auto& [iter, inserted] =
474 declared.emplace(annotation->GetType(), annotation->GetLocation());
475 if (!inserted && !annotation->Repeatable()) {
476 AIDL_ERROR(this) << "'" << annotation->GetName()
Jooyung Hand902a972020-10-23 17:32:44 +0900477 << "' is repeated, but not allowed. Previous location: " << iter->second;
478 return false;
479 }
Andrei Onea9445fc62019-06-27 18:11:59 +0100480 }
Steven Morelanda57d0a62019-07-30 09:41:14 -0700481
Andrei Onea9445fc62019-06-27 18:11:59 +0100482 return true;
483}
484
Jiyong Park68bc77a2018-07-19 19:00:45 +0900485string AidlAnnotatable::ToString() const {
486 vector<string> ret;
487 for (const auto& a : annotations_) {
Steven Morelanda7560e82021-10-08 16:24:39 -0700488 ret.emplace_back(a->ToString());
Jiyong Park68bc77a2018-07-19 19:00:45 +0900489 }
490 std::sort(ret.begin(), ret.end());
491 return Join(ret, " ");
492}
493
Steven Moreland46e9da82018-07-27 15:45:29 -0700494AidlTypeSpecifier::AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name,
495 bool is_array,
Jiyong Park1deecc32018-07-17 01:14:41 +0900496 vector<unique_ptr<AidlTypeSpecifier>>* type_params,
Jooyung Han8451a202021-01-16 03:07:06 +0900497 const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +0900498 : AidlAnnotatable(location, comments),
Jeongik Chadf76dc72019-11-28 00:08:47 +0900499 AidlParameterizable<unique_ptr<AidlTypeSpecifier>>(type_params),
Steven Moreland46e9da82018-07-27 15:45:29 -0700500 unresolved_name_(unresolved_name),
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700501 is_array_(is_array),
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900502 split_name_(Split(unresolved_name, ".")) {}
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700503
Steven Moreland0cac8662021-10-08 16:43:29 -0700504void AidlTypeSpecifier::ViewAsArrayBase(std::function<void(const AidlTypeSpecifier&)> func) const {
Steven Moreland3f658cf2018-08-20 13:40:54 -0700505 AIDL_FATAL_IF(!is_array_, this);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900506 // Declaring array of generic type cannot happen, it is grammar error.
507 AIDL_FATAL_IF(IsGeneric(), this);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700508
Steven Moreland0cac8662021-10-08 16:43:29 -0700509 is_array_ = false;
510 func(*this);
511 is_array_ = true;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700512}
513
Jooyung Han965e31d2020-11-27 12:30:16 +0900514string AidlTypeSpecifier::Signature() const {
Jiyong Park1deecc32018-07-17 01:14:41 +0900515 string ret = GetName();
516 if (IsGeneric()) {
517 vector<string> arg_names;
518 for (const auto& ta : GetTypeParameters()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900519 arg_names.emplace_back(ta->Signature());
Jiyong Parkccf00f82018-07-17 01:39:23 +0900520 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900521 ret += "<" + Join(arg_names, ",") + ">";
Jiyong Parkccf00f82018-07-17 01:39:23 +0900522 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900523 if (IsArray()) {
524 ret += "[]";
525 }
526 return ret;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900527}
528
Jooyung Han965e31d2020-11-27 12:30:16 +0900529string AidlTypeSpecifier::ToString() const {
530 string ret = Signature();
Jiyong Park02da7422018-07-16 16:00:26 +0900531 string annotations = AidlAnnotatable::ToString();
532 if (annotations != "") {
533 ret = annotations + " " + ret;
534 }
535 return ret;
536}
537
Jooyung Han13f1fa52021-06-11 18:06:12 +0900538// When `scope` is specified, name is resolved first based on it.
539// `scope` can be null for built-in types and fully-qualified types.
540bool AidlTypeSpecifier::Resolve(const AidlTypenames& typenames, const AidlScope* scope) {
Steven Moreland21780812020-09-11 01:29:45 +0000541 AIDL_FATAL_IF(IsResolved(), this);
Jooyung Han13f1fa52021-06-11 18:06:12 +0900542 std::string name = unresolved_name_;
543 if (scope) {
544 name = scope->ResolveName(name);
545 }
546 AidlTypenames::ResolvedTypename result = typenames.ResolveTypename(name);
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700547 if (result.is_resolved) {
548 fully_qualified_name_ = result.canonical_name;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900549 split_name_ = Split(fully_qualified_name_, ".");
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900550 defined_type_ = result.defined_type;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900551 }
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700552 return result.is_resolved;
Casey Dahlin70078e62015-09-30 17:01:30 -0700553}
554
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900555const AidlDefinedType* AidlTypeSpecifier::GetDefinedType() const {
556 return defined_type_;
557}
558
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900559bool AidlTypeSpecifier::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -0800560 if (!AidlAnnotatable::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100561 return false;
562 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900563 if (IsGeneric()) {
Jooyung Hand09a21d2021-02-15 18:56:55 +0900564 const auto& types = GetTypeParameters();
565 for (const auto& arg : types) {
566 if (!arg->CheckValid(typenames)) {
567 return false;
568 }
569 }
Jeongik Chae74c86d2019-12-12 16:54:03 +0900570
Jooyung Hand09a21d2021-02-15 18:56:55 +0900571 const string& type_name = GetName();
Jeongik Chae74c86d2019-12-12 16:54:03 +0900572 // TODO(b/136048684) Disallow to use primitive types only if it is List or Map.
573 if (type_name == "List" || type_name == "Map") {
Jooyung Hane87cdd02020-12-11 16:47:35 +0900574 if (std::any_of(types.begin(), types.end(), [&](auto& type_ptr) {
Jooyung Han1f35ef32021-02-15 19:08:05 +0900575 return !type_ptr->IsArray() &&
576 (typenames.GetEnumDeclaration(*type_ptr) ||
577 AidlTypenames::IsPrimitiveTypename(type_ptr->GetName()));
Jeongik Chae74c86d2019-12-12 16:54:03 +0900578 })) {
Devin Moore7b8d5c92020-03-17 14:14:08 -0700579 AIDL_ERROR(this) << "A generic type cannot have any primitive type parameters.";
Jeongik Chae74c86d2019-12-12 16:54:03 +0900580 return false;
581 }
582 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800583 const auto defined_type = typenames.TryGetDefinedType(type_name);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900584 const auto parameterizable =
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800585 defined_type != nullptr ? defined_type->AsParameterizable() : nullptr;
586 const bool is_user_defined_generic_type =
Jeongik Chadf76dc72019-11-28 00:08:47 +0900587 parameterizable != nullptr && parameterizable->IsGeneric();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800588 const size_t num_params = GetTypeParameters().size();
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900589 if (type_name == "List") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800590 if (num_params > 1) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900591 AIDL_ERROR(this) << "List can only have one type parameter, but got: '" << Signature()
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000592 << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900593 return false;
594 }
Jooyung Han55f96ad2020-12-13 10:08:33 +0900595 const AidlTypeSpecifier& contained_type = *GetTypeParameters()[0];
Jooyung Hancea89002021-02-15 17:04:04 +0900596 if (contained_type.IsArray()) {
597 AIDL_ERROR(this)
598 << "List of arrays is not supported. List<T> supports parcelable/union, String, "
599 "IBinder, and ParcelFileDescriptor.";
600 return false;
601 }
Jooyung Han55f96ad2020-12-13 10:08:33 +0900602 const string& contained_type_name = contained_type.GetName();
603 if (AidlTypenames::IsBuiltinTypename(contained_type_name)) {
604 if (contained_type_name != "String" && contained_type_name != "IBinder" &&
605 contained_type_name != "ParcelFileDescriptor") {
606 AIDL_ERROR(this) << "List<" << contained_type_name
607 << "> is not supported. List<T> supports parcelable/union, String, "
608 "IBinder, and ParcelFileDescriptor.";
609 return false;
610 }
611 } else { // Defined types
612 if (typenames.GetInterface(contained_type)) {
613 AIDL_ERROR(this) << "List<" << contained_type_name
614 << "> is not supported. List<T> supports parcelable/union, String, "
615 "IBinder, and ParcelFileDescriptor.";
616 return false;
617 }
618 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900619 } else if (type_name == "Map") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800620 if (num_params != 0 && num_params != 2) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900621 AIDL_ERROR(this) << "Map must have 0 or 2 type parameters, but got "
Jooyung Han965e31d2020-11-27 12:30:16 +0900622 << "'" << Signature() << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900623 return false;
624 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800625 if (num_params == 2) {
Jooyung Hanaab242a2021-02-15 19:01:15 +0900626 const string& key_type = GetTypeParameters()[0]->Signature();
Jeongik Chae48d9942020-01-02 17:39:00 +0900627 if (key_type != "String") {
628 AIDL_ERROR(this) << "The type of key in map must be String, but it is "
629 << "'" << key_type << "'";
630 return false;
631 }
632 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800633 } else if (is_user_defined_generic_type) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900634 const size_t allowed = parameterizable->GetTypeParameters().size();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800635 if (num_params != allowed) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900636 AIDL_ERROR(this) << type_name << " must have " << allowed << " type parameters, but got "
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800637 << num_params;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900638 return false;
639 }
640 } else {
641 AIDL_ERROR(this) << type_name << " is not a generic type.";
642 return false;
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900643 }
644 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900645
Steven Moreland11cb9452020-01-21 16:56:58 -0800646 const bool is_generic_string_list = GetName() == "List" && IsGeneric() &&
647 GetTypeParameters().size() == 1 &&
648 GetTypeParameters()[0]->GetName() == "String";
649 if (IsUtf8InCpp() && (GetName() != "String" && !is_generic_string_list)) {
650 AIDL_ERROR(this) << "@utf8InCpp can only be used on String, String[], and List<String>.";
651 return false;
652 }
653
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900654 if (GetName() == "void") {
655 if (IsArray() || IsNullable() || IsUtf8InCpp()) {
656 AIDL_ERROR(this) << "void type cannot be an array or nullable or utf8 string";
657 return false;
658 }
659 }
660
661 if (IsArray()) {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800662 const auto defined_type = typenames.TryGetDefinedType(GetName());
663 if (defined_type != nullptr && defined_type->AsInterface() != nullptr) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900664 AIDL_ERROR(this) << "Binder type cannot be an array";
665 return false;
666 }
Steven Moreland8042d2d2020-09-30 23:31:32 +0000667 if (GetName() == "ParcelableHolder") {
668 AIDL_ERROR(this) << "Arrays of ParcelableHolder are not supported.";
669 return false;
670 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900671 }
672
673 if (IsNullable()) {
674 if (AidlTypenames::IsPrimitiveTypename(GetName()) && !IsArray()) {
675 AIDL_ERROR(this) << "Primitive type cannot get nullable annotation";
676 return false;
677 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800678 const auto defined_type = typenames.TryGetDefinedType(GetName());
679 if (defined_type != nullptr && defined_type->AsEnumDeclaration() != nullptr && !IsArray()) {
Daniel Normanee8674f2019-09-20 16:07:00 -0700680 AIDL_ERROR(this) << "Enum type cannot get nullable annotation";
681 return false;
682 }
Jeongik Chaf6ec8982020-10-15 00:10:30 +0900683 if (GetName() == "ParcelableHolder") {
684 AIDL_ERROR(this) << "ParcelableHolder cannot be nullable.";
685 return false;
686 }
Jooyung Han01720ed2021-08-13 07:46:07 +0900687 if (IsHeapNullable()) {
688 if (!defined_type || IsArray() || !defined_type->AsParcelable()) {
689 AIDL_ERROR(this) << "@nullable(heap=true) is available to parcelables.";
690 return false;
691 }
692 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900693 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900694 return true;
695}
696
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900697std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type,
Steven Moreland860b1942018-08-16 14:59:28 -0700698 const std::string& raw_value) {
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900699 if (type.IsArray()) {
700 return raw_value;
701 }
702
703 if (auto defined_type = type.GetDefinedType(); defined_type) {
704 auto enum_type = defined_type->AsEnumDeclaration();
705 AIDL_FATAL_IF(!enum_type, type) << "Invalid type for \"" << raw_value << "\"";
706 return type.GetName() + "." + raw_value.substr(raw_value.find_last_of('.') + 1);
707 }
Steven Moreland860b1942018-08-16 14:59:28 -0700708 return raw_value;
709}
710
Steven Moreland46e9da82018-07-27 15:45:29 -0700711AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
712 AidlTypeSpecifier* type, const std::string& name)
Steven Moreland541788d2020-05-21 22:05:52 +0000713 : AidlVariableDeclaration(location, type, name, AidlConstantValue::Default(*type)) {
714 default_user_specified_ = false;
715}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700716
Steven Moreland46e9da82018-07-27 15:45:29 -0700717AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
718 AidlTypeSpecifier* type, const std::string& name,
719 AidlConstantValue* default_value)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900720 : AidlMember(location, type->GetComments()),
Steven Moreland541788d2020-05-21 22:05:52 +0000721 type_(type),
722 name_(name),
723 default_user_specified_(true),
724 default_value_(default_value) {}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700725
Jooyung Han53fb4242020-12-17 16:03:49 +0900726bool AidlVariableDeclaration::HasUsefulDefaultValue() const {
727 if (GetDefaultValue()) {
728 return true;
729 }
730 // null is accepted as a valid default value in all backends
731 if (GetType().IsNullable()) {
732 return true;
733 }
734 return false;
735}
736
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900737bool AidlVariableDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700738 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900739 valid &= type_->CheckValid(typenames);
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900740
Steven Moreland54be7bd2019-12-05 11:17:53 -0800741 if (type_->GetName() == "void") {
742 AIDL_ERROR(this) << "Declaration " << name_
743 << " is void, but declarations cannot be of void type.";
744 valid = false;
745 }
746
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900747 if (default_value_ == nullptr) return valid;
Steven Moreland25294322018-08-07 18:13:55 -0700748 valid &= default_value_->CheckValid();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700749
Steven Moreland25294322018-08-07 18:13:55 -0700750 if (!valid) return false;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700751
Steven Moreland860b1942018-08-16 14:59:28 -0700752 return !ValueString(AidlConstantValueDecorator).empty();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700753}
Steven Moreland5557f1c2018-07-02 13:50:23 -0700754
Jooyung Hanacae85d2020-10-28 16:39:09 +0900755string AidlVariableDeclaration::GetCapitalizedName() const {
756 AIDL_FATAL_IF(name_.size() <= 0, *this) << "Name can't be empty.";
757 string str = name_;
758 str[0] = static_cast<char>(toupper(str[0]));
759 return str;
760}
761
Steven Moreland5557f1c2018-07-02 13:50:23 -0700762string AidlVariableDeclaration::ToString() const {
Jooyung Han965e31d2020-11-27 12:30:16 +0900763 string ret = type_->ToString() + " " + name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000764 if (default_value_ != nullptr && default_user_specified_) {
Steven Moreland860b1942018-08-16 14:59:28 -0700765 ret += " = " + ValueString(AidlConstantValueDecorator);
Steven Moreland9ea10e32018-07-19 15:26:09 -0700766 }
767 return ret;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700768}
769
Jiyong Park02da7422018-07-16 16:00:26 +0900770string AidlVariableDeclaration::Signature() const {
771 return type_->Signature() + " " + name_;
772}
773
Steven Moreland860b1942018-08-16 14:59:28 -0700774std::string AidlVariableDeclaration::ValueString(const ConstantValueDecorator& decorator) const {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900775 if (default_value_ != nullptr) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700776 return default_value_->ValueString(GetType(), decorator);
Jiyong Parka468e2a2018-08-29 21:25:18 +0900777 } else {
778 return "";
779 }
Steven Moreland25294322018-08-07 18:13:55 -0700780}
781
Jooyung Hanc5688f72021-01-05 15:41:48 +0900782void AidlVariableDeclaration::TraverseChildren(
783 std::function<void(const AidlNode&)> traverse) const {
784 traverse(GetType());
Jooyung Hanc3c739a2021-10-14 11:33:14 +0900785 if (auto default_value = GetDefaultValue(); default_value) {
786 traverse(*default_value);
Jooyung Hanc5688f72021-01-05 15:41:48 +0900787 }
788}
789
Steven Moreland46e9da82018-07-27 15:45:29 -0700790AidlArgument::AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
791 AidlTypeSpecifier* type, const std::string& name)
792 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700793 direction_(direction),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700794 direction_specified_(true) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700795
Steven Moreland46e9da82018-07-27 15:45:29 -0700796AidlArgument::AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type,
797 const std::string& name)
798 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700799 direction_(AidlArgument::IN_DIR),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700800 direction_specified_(false) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700801
Jooyung Han020d8d12021-02-26 17:23:02 +0900802static std::string to_string(AidlArgument::Direction direction) {
803 switch (direction) {
804 case AidlArgument::IN_DIR:
805 return "in";
806 case AidlArgument::OUT_DIR:
807 return "out";
808 case AidlArgument::INOUT_DIR:
809 return "inout";
810 }
811}
812
Jiyong Park02da7422018-07-16 16:00:26 +0900813string AidlArgument::GetDirectionSpecifier() const {
Casey Dahlinc378c992015-09-29 16:50:40 -0700814 string ret;
Casey Dahlinc378c992015-09-29 16:50:40 -0700815 if (direction_specified_) {
Jooyung Han020d8d12021-02-26 17:23:02 +0900816 ret = to_string(direction_);
Casey Dahlinc378c992015-09-29 16:50:40 -0700817 }
Casey Dahlinc378c992015-09-29 16:50:40 -0700818 return ret;
819}
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700820
Jiyong Park02da7422018-07-16 16:00:26 +0900821string AidlArgument::ToString() const {
Devin Mooreeccdb902020-03-24 16:22:40 -0700822 if (direction_specified_) {
823 return GetDirectionSpecifier() + " " + AidlVariableDeclaration::ToString();
824 } else {
825 return AidlVariableDeclaration::ToString();
826 }
Jiyong Park02da7422018-07-16 16:00:26 +0900827}
828
Jooyung Han020d8d12021-02-26 17:23:02 +0900829static std::string FormatDirections(const std::set<AidlArgument::Direction>& directions) {
830 std::vector<std::string> out;
831 for (const auto& d : directions) {
832 out.push_back(to_string(d));
833 }
834
835 if (out.size() <= 1) { // [] => "" or [A] => "A"
836 return Join(out, "");
837 } else if (out.size() == 2) { // [A,B] => "A or B"
838 return Join(out, " or ");
839 } else { // [A,B,C] => "A, B, or C"
840 out.back() = "or " + out.back();
841 return Join(out, ", ");
842 }
843}
844
845bool AidlArgument::CheckValid(const AidlTypenames& typenames) const {
846 if (!GetType().CheckValid(typenames)) {
847 return false;
848 }
849
850 const auto& aspect = typenames.GetArgumentAspect(GetType());
851
852 if (aspect.possible_directions.size() == 0) {
853 AIDL_ERROR(this) << aspect.name << " cannot be an argument type";
854 return false;
855 }
856
857 // when direction is not specified, "in" is assumed and should be the only possible direction
858 if (!DirectionWasSpecified() && aspect.possible_directions != std::set{AidlArgument::IN_DIR}) {
859 AIDL_ERROR(this) << "The direction of '" << GetName() << "' is not specified. " << aspect.name
860 << " can be an " << FormatDirections(aspect.possible_directions)
861 << " parameter.";
862 return false;
863 }
864
865 if (aspect.possible_directions.count(GetDirection()) == 0) {
866 AIDL_ERROR(this) << "'" << GetName() << "' can't be an " << GetDirectionSpecifier()
867 << " parameter because " << aspect.name << " can only be an "
868 << FormatDirections(aspect.possible_directions) << " parameter.";
869 return false;
870 }
871
872 return true;
873}
874
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900875bool AidlCommentable::IsHidden() const {
Jooyung Han24effbf2021-01-16 10:24:03 +0900876 return android::aidl::HasHideInComments(GetComments());
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900877}
878
879bool AidlCommentable::IsDeprecated() const {
Jooyung Hand4fe00e2021-01-11 16:21:53 +0900880 return android::aidl::FindDeprecated(GetComments()).has_value();
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900881}
882
Jooyung Han8451a202021-01-16 03:07:06 +0900883AidlMember::AidlMember(const AidlLocation& location, const Comments& comments)
Jooyung Han2aedb112021-09-29 09:37:59 +0900884 : AidlAnnotatable(location, comments) {}
Steven Moreland46e9da82018-07-27 15:45:29 -0700885
Steven Moreland46e9da82018-07-27 15:45:29 -0700886AidlConstantDeclaration::AidlConstantDeclaration(const AidlLocation& location,
887 AidlTypeSpecifier* type, const std::string& name,
888 AidlConstantValue* value)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900889 : AidlMember(location, type->GetComments()), type_(type), name_(name), value_(value) {}
Steven Moreland693640b2018-07-19 13:46:27 -0700890
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900891bool AidlConstantDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700892 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900893 valid &= type_->CheckValid(typenames);
Steven Moreland25294322018-08-07 18:13:55 -0700894 valid &= value_->CheckValid();
895 if (!valid) return false;
Steven Moreland693640b2018-07-19 13:46:27 -0700896
Steven Morelande689da22020-11-10 02:06:30 +0000897 const static set<string> kSupportedConstTypes = {"String", "byte", "int", "long"};
Jooyung Han965e31d2020-11-27 12:30:16 +0900898 if (kSupportedConstTypes.find(type_->Signature()) == kSupportedConstTypes.end()) {
899 AIDL_ERROR(this) << "Constant of type " << type_->Signature() << " is not supported.";
Steven Moreland693640b2018-07-19 13:46:27 -0700900 return false;
901 }
902
Will McVickerd7d18df2019-09-12 13:40:50 -0700903 return true;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700904}
905
Jiyong Parka428d212018-08-29 22:26:30 +0900906string AidlConstantDeclaration::ToString() const {
Jooyung Hanb3ca6302020-11-27 14:13:27 +0900907 return "const " + type_->ToString() + " " + name_ + " = " +
908 ValueString(AidlConstantValueDecorator);
Jiyong Parka428d212018-08-29 22:26:30 +0900909}
910
911string AidlConstantDeclaration::Signature() const {
912 return type_->Signature() + " " + name_;
913}
914
Steven Moreland46e9da82018-07-27 15:45:29 -0700915AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
916 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jooyung Han8451a202021-01-16 03:07:06 +0900917 const Comments& comments)
Jiyong Parkb034bf02018-07-30 17:44:33 +0900918 : AidlMethod(location, oneway, type, name, args, comments, 0, true) {
919 has_id_ = false;
920}
921
922AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
923 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jooyung Han8451a202021-01-16 03:07:06 +0900924 const Comments& comments, int id, bool is_user_defined)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900925 : AidlMember(location, comments),
Steven Moreland46e9da82018-07-27 15:45:29 -0700926 oneway_(oneway),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700927 type_(type),
928 name_(name),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700929 arguments_(std::move(*args)),
Jiyong Parkb034bf02018-07-30 17:44:33 +0900930 id_(id),
931 is_user_defined_(is_user_defined) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700932 has_id_ = true;
933 delete args;
Christopher Wileyad339272015-10-05 19:11:58 -0700934 for (const unique_ptr<AidlArgument>& a : arguments_) {
935 if (a->IsIn()) { in_arguments_.push_back(a.get()); }
936 if (a->IsOut()) { out_arguments_.push_back(a.get()); }
937 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700938}
939
Jiyong Park02da7422018-07-16 16:00:26 +0900940string AidlMethod::Signature() const {
941 vector<string> arg_signatures;
942 for (const auto& arg : GetArguments()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900943 arg_signatures.emplace_back(arg->GetType().Signature());
Jiyong Park02da7422018-07-16 16:00:26 +0900944 }
Jiyong Park309668e2018-07-28 16:55:44 +0900945 return GetName() + "(" + Join(arg_signatures, ", ") + ")";
946}
947
948string AidlMethod::ToString() const {
949 vector<string> arg_strings;
950 for (const auto& arg : GetArguments()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900951 arg_strings.emplace_back(arg->ToString());
Jiyong Park309668e2018-07-28 16:55:44 +0900952 }
Jooyung Han965e31d2020-11-27 12:30:16 +0900953 string ret = (IsOneway() ? "oneway " : "") + GetType().ToString() + " " + GetName() + "(" +
Steven Moreland4ee68632018-12-14 15:52:46 -0800954 Join(arg_strings, ", ") + ")";
Jiyong Parked65bf42018-08-28 15:43:27 +0900955 if (HasId()) {
956 ret += " = " + std::to_string(GetId());
957 }
958 return ret;
Jiyong Park02da7422018-07-16 16:00:26 +0900959}
960
Steven Moreland46e9da82018-07-27 15:45:29 -0700961AidlDefinedType::AidlDefinedType(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +0900962 const Comments& comments, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900963 std::vector<std::unique_ptr<AidlMember>>* members)
Jooyung Han2aedb112021-09-29 09:37:59 +0900964 : AidlMember(location, comments), AidlScope(this), name_(name), package_(package) {
Jooyung Han93f48f02021-06-05 00:11:16 +0900965 // adjust name/package when name is fully qualified (for preprocessed files)
966 if (package_.empty() && name_.find('.') != std::string::npos) {
967 // Note that this logic is absolutely wrong. Given a parcelable
968 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
969 // the class is just Bar. However, this was the way it was done in the past.
970 //
971 // See b/17415692
972 auto pos = name.rfind('.');
973 // name is the last part.
974 name_ = name.substr(pos + 1);
975 // package is the initial parts (except the last).
976 package_ = name.substr(0, pos);
977 }
Jooyung Han829ec7c2020-12-02 12:07:36 +0900978 if (members) {
979 for (auto& m : *members) {
Jooyung Hanbaa71062021-09-29 09:06:03 +0900980 if (auto constant = AidlCast<AidlConstantDeclaration>(*m); constant) {
Jooyung Han829ec7c2020-12-02 12:07:36 +0900981 constants_.emplace_back(constant);
Jooyung Hanbaa71062021-09-29 09:06:03 +0900982 } else if (auto variable = AidlCast<AidlVariableDeclaration>(*m); variable) {
Jooyung Han829ec7c2020-12-02 12:07:36 +0900983 variables_.emplace_back(variable);
Jooyung Hanbaa71062021-09-29 09:06:03 +0900984 } else if (auto method = AidlCast<AidlMethod>(*m); method) {
Jooyung Han829ec7c2020-12-02 12:07:36 +0900985 methods_.emplace_back(method);
Jooyung Han2aedb112021-09-29 09:37:59 +0900986 } else if (auto type = AidlCast<AidlDefinedType>(*m); type) {
987 type->SetEnclosingScope(this);
988 types_.emplace_back(type);
Jooyung Han829ec7c2020-12-02 12:07:36 +0900989 } else {
Jooyung Hanbaa71062021-09-29 09:06:03 +0900990 AIDL_FATAL(*m) << "Unknown member type.";
Jooyung Han829ec7c2020-12-02 12:07:36 +0900991 }
992 members_.push_back(m.release());
993 }
994 delete members;
995 }
996}
Steven Moreland787b0432018-07-03 09:00:58 -0700997
Jooyung Han808a2a02020-12-28 16:46:54 +0900998bool AidlDefinedType::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -0800999 if (!AidlAnnotatable::CheckValid(typenames)) {
1000 return false;
1001 }
Jooyung Han829ec7c2020-12-02 12:07:36 +09001002 if (!CheckValidWithMembers(typenames)) {
1003 return false;
1004 }
Devin Moore24f68572020-02-26 13:20:59 -08001005 return true;
1006}
1007
Steven Moreland787b0432018-07-03 09:00:58 -07001008std::string AidlDefinedType::GetCanonicalName() const {
1009 if (package_.empty()) {
1010 return GetName();
1011 }
Jooyung Han2aedb112021-09-29 09:37:59 +09001012 if (auto parent = GetParentType(); parent) {
1013 return parent->GetCanonicalName() + "." + GetName();
1014 }
Steven Moreland787b0432018-07-03 09:00:58 -07001015 return GetPackage() + "." + GetName();
1016}
1017
Jooyung Han829ec7c2020-12-02 12:07:36 +09001018bool AidlDefinedType::CheckValidWithMembers(const AidlTypenames& typenames) const {
1019 bool success = true;
1020
Jooyung Han2aedb112021-09-29 09:37:59 +09001021 for (const auto& t : GetNestedTypes()) {
1022 success = success && t->CheckValid(typenames);
1023 }
1024
Jooyung Han7fc5de02021-09-30 22:26:27 +09001025 if (auto parameterizable = AsParameterizable();
1026 parameterizable && parameterizable->IsGeneric() && !GetNestedTypes().empty()) {
1027 AIDL_ERROR(this) << "Generic types can't have nested types.";
1028 return false;
1029 }
1030
Jooyung Han2aedb112021-09-29 09:37:59 +09001031 std::set<std::string> nested_type_names;
1032 for (const auto& t : GetNestedTypes()) {
1033 bool duplicated = !nested_type_names.emplace(t->GetName()).second;
1034 if (duplicated) {
1035 AIDL_ERROR(t) << "Redefinition of '" << t->GetName() << "'.";
1036 success = false;
1037 }
1038 // nested type can't have a parent name
1039 if (t->GetName() == GetName()) {
1040 AIDL_ERROR(t) << "Nested type '" << GetName() << "' has the same name as its parent.";
1041 success = false;
1042 }
Jooyung Han2b1487d2021-09-30 09:57:01 +09001043 // Having unstructured parcelables as nested types doesn't make sense because they are defined
1044 // somewhere else in native languages (e.g. C++, Java...).
1045 if (AidlCast<AidlParcelable>(*t)) {
1046 AIDL_ERROR(t) << "'" << t->GetName()
1047 << "' is nested. Unstructured parcelables should be at the root scope.";
1048 return false;
1049 }
Jooyung Han2aedb112021-09-29 09:37:59 +09001050 // For now we don't allow "interface" to be nested
1051 if (AidlCast<AidlInterface>(*t)) {
1052 AIDL_ERROR(t) << "'" << t->GetName()
1053 << "' is nested. Interfaces should be at the root scope.";
1054 return false;
1055 }
1056 }
1057
Jooyung Han8e9ae872021-10-13 02:52:25 +09001058 if (!TopologicalVisit(GetNestedTypes(), [](auto&) {})) {
1059 AIDL_ERROR(this) << GetName()
1060 << " has nested types with cyclic references. C++ and NDK backends don't "
1061 "support cyclic references.";
1062 return false;
1063 }
1064
Jooyung Han829ec7c2020-12-02 12:07:36 +09001065 for (const auto& v : GetFields()) {
1066 const bool field_valid = v->CheckValid(typenames);
1067 success = success && field_valid;
1068 }
1069
1070 // field names should be unique
1071 std::set<std::string> fieldnames;
1072 for (const auto& v : GetFields()) {
1073 bool duplicated = !fieldnames.emplace(v->GetName()).second;
1074 if (duplicated) {
1075 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName() << "'";
1076 success = false;
1077 }
1078 }
1079
1080 // immutable parcelables should have immutable fields.
1081 if (IsJavaOnlyImmutable()) {
1082 for (const auto& v : GetFields()) {
1083 if (!typenames.CanBeJavaOnlyImmutable(v->GetType())) {
1084 AIDL_ERROR(v) << "The @JavaOnlyImmutable '" << GetName() << "' has a "
1085 << "non-immutable field named '" << v->GetName() << "'.";
1086 success = false;
1087 }
1088 }
1089 }
1090
1091 set<string> constant_names;
1092 for (const auto& constant : GetConstantDeclarations()) {
1093 if (constant_names.count(constant->GetName()) > 0) {
1094 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
1095 success = false;
1096 }
1097 constant_names.insert(constant->GetName());
1098 success = success && constant->CheckValid(typenames);
1099 }
1100
1101 return success;
1102}
1103
1104bool AidlDefinedType::CheckValidForGetterNames() const {
1105 bool success = true;
1106 std::set<std::string> getters;
1107 for (const auto& v : GetFields()) {
1108 bool duplicated = !getters.emplace(v->GetCapitalizedName()).second;
1109 if (duplicated) {
1110 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName()
1111 << "' after capitalizing the first letter";
1112 success = false;
1113 }
1114 }
1115 return success;
1116}
1117
Jooyung Han2aedb112021-09-29 09:37:59 +09001118const AidlDefinedType* AidlDefinedType::GetParentType() const {
1119 AIDL_FATAL_IF(GetEnclosingScope() == nullptr, this) << "Scope is not set.";
1120 return AidlCast<AidlDefinedType>(GetEnclosingScope()->GetNode());
1121}
1122
Jooyung Hanf8c39632021-10-05 09:56:29 +09001123const AidlDefinedType* AidlDefinedType::GetRootType() const {
1124 const AidlDefinedType* root = this;
1125 for (auto parent = root->GetParentType(); parent; parent = parent->GetParentType()) {
1126 root = parent;
1127 }
1128 return root;
1129}
1130
Jooyung Han2aedb112021-09-29 09:37:59 +09001131// Resolve `name` in the current scope. If not found, delegate to the parent
Jooyung Han13f1fa52021-06-11 18:06:12 +09001132std::string AidlDefinedType::ResolveName(const std::string& name) const {
Jooyung Han2aedb112021-09-29 09:37:59 +09001133 // For example, in the following, t1's type Baz means x.Foo.Bar.Baz
1134 // while t2's type is y.Baz.
Jooyung Han13f1fa52021-06-11 18:06:12 +09001135 // package x;
Jooyung Han2aedb112021-09-29 09:37:59 +09001136 // import y.Baz;
Jooyung Han13f1fa52021-06-11 18:06:12 +09001137 // parcelable Foo {
1138 // parcelable Bar {
Jooyung Han2aedb112021-09-29 09:37:59 +09001139 // enum Baz { ... }
1140 // Baz t1; // -> should be x.Foo.Bar.Baz
Jooyung Han13f1fa52021-06-11 18:06:12 +09001141 // }
Jooyung Han2aedb112021-09-29 09:37:59 +09001142 // Baz t2; // -> should be y.Baz
1143 // Bar.Baz t3; // -> should be x.Foo.Bar.Baz
Jooyung Han13f1fa52021-06-11 18:06:12 +09001144 // }
1145 AIDL_FATAL_IF(!GetEnclosingScope(), this)
1146 << "Type should have an enclosing scope.(e.g. AidlDocument)";
Jooyung Han2aedb112021-09-29 09:37:59 +09001147 if (AidlTypenames::IsBuiltinTypename(name)) {
1148 return name;
1149 }
1150
1151 const auto first_dot = name.find_first_of('.');
1152 // For "Outer.Inner", we look up "Outer" in the import list.
1153 const std::string class_name =
1154 (first_dot == std::string::npos) ? name : name.substr(0, first_dot);
1155 // Keep ".Inner", to make a fully-qualified name
1156 const std::string nested_type = (first_dot == std::string::npos) ? "" : name.substr(first_dot);
1157
1158 // check if it is a nested type
1159 for (const auto& type : GetNestedTypes()) {
1160 if (type->GetName() == class_name) {
1161 return type->GetCanonicalName() + nested_type;
1162 }
1163 }
1164
Jooyung Han13f1fa52021-06-11 18:06:12 +09001165 return GetEnclosingScope()->ResolveName(name);
1166}
1167
Jooyung Hanbaa71062021-09-29 09:06:03 +09001168template <>
1169const AidlDefinedType* AidlCast<AidlDefinedType>(const AidlNode& node) {
1170 struct Visitor : AidlVisitor {
1171 const AidlDefinedType* defined_type = nullptr;
1172 void Visit(const AidlInterface& t) override { defined_type = &t; }
1173 void Visit(const AidlEnumDeclaration& t) override { defined_type = &t; }
1174 void Visit(const AidlStructuredParcelable& t) override { defined_type = &t; }
1175 void Visit(const AidlUnionDecl& t) override { defined_type = &t; }
1176 void Visit(const AidlParcelable& t) override { defined_type = &t; }
1177 } v;
1178 node.DispatchVisit(v);
1179 return v.defined_type;
Jooyung Han35784982021-06-29 06:26:12 +09001180}
1181
1182const AidlDocument& AidlDefinedType::GetDocument() const {
Jooyung Hanf8c39632021-10-05 09:56:29 +09001183 const AidlDefinedType* root = GetRootType();
1184 auto scope = root->GetEnclosingScope();
Jooyung Han35784982021-06-29 06:26:12 +09001185 AIDL_FATAL_IF(!scope, this) << "no scope defined.";
1186 auto doc = AidlCast<AidlDocument>(scope->GetNode());
Jooyung Hanf8c39632021-10-05 09:56:29 +09001187 AIDL_FATAL_IF(!doc, this) << "root scope is not a document.";
Jooyung Han35784982021-06-29 06:26:12 +09001188 return *doc;
1189}
1190
Jiyong Park18132182020-06-08 20:24:40 +09001191AidlParcelable::AidlParcelable(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001192 const std::string& package, const Comments& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001193 const std::string& cpp_header, std::vector<std::string>* type_params,
1194 std::vector<std::unique_ptr<AidlMember>>* members)
1195 : AidlDefinedType(location, name, comments, package, members),
Jeongik Chadf76dc72019-11-28 00:08:47 +09001196 AidlParameterizable<std::string>(type_params),
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -08001197 cpp_header_(cpp_header) {
1198 // Strip off quotation marks if we actually have a cpp header.
1199 if (cpp_header_.length() >= 2) {
1200 cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2);
1201 }
Casey Dahlin59401da2015-10-09 18:16:45 -07001202}
Jeongik Chadf76dc72019-11-28 00:08:47 +09001203
1204template <typename T>
1205bool AidlParameterizable<T>::CheckValid() const {
1206 return true;
1207};
1208
1209template <>
1210bool AidlParameterizable<std::string>::CheckValid() const {
1211 if (!IsGeneric()) {
1212 return true;
1213 }
1214 std::unordered_set<std::string> set(GetTypeParameters().begin(), GetTypeParameters().end());
1215 if (set.size() != GetTypeParameters().size()) {
1216 AIDL_ERROR(this->AsAidlNode()) << "Every type parameter should be unique.";
1217 return false;
1218 }
1219 return true;
1220}
Casey Dahlin59401da2015-10-09 18:16:45 -07001221
Jooyung Han808a2a02020-12-28 16:46:54 +09001222bool AidlParcelable::CheckValid(const AidlTypenames& typenames) const {
1223 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001224 return false;
1225 }
Jeongik Chadf76dc72019-11-28 00:08:47 +09001226 if (!AidlParameterizable<std::string>::CheckValid()) {
1227 return false;
1228 }
Jeongik Cha82317dd2019-02-27 20:26:11 +09001229
1230 return true;
1231}
1232
Steven Moreland5557f1c2018-07-02 13:50:23 -07001233AidlStructuredParcelable::AidlStructuredParcelable(
Jiyong Park18132182020-06-08 20:24:40 +09001234 const AidlLocation& location, const std::string& name, const std::string& package,
Jooyung Han8451a202021-01-16 03:07:06 +09001235 const Comments& comments, std::vector<std::string>* type_params,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001236 std::vector<std::unique_ptr<AidlMember>>* members)
1237 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Steven Moreland5557f1c2018-07-02 13:50:23 -07001238
Jooyung Han808a2a02020-12-28 16:46:54 +09001239bool AidlStructuredParcelable::CheckValid(const AidlTypenames& typenames) const {
1240 if (!AidlParcelable::CheckValid(typenames)) {
Devin Moore24f68572020-02-26 13:20:59 -08001241 return false;
1242 }
Jeongik Cha13066da2020-08-06 15:43:19 +09001243
Jooyung Han59af9cc2020-10-25 21:44:14 +09001244 bool success = true;
Jeongik Cha36f76c32020-07-28 00:25:52 +09001245
Jooyung Hand4057c42020-10-23 13:28:22 +09001246 if (IsFixedSize()) {
1247 for (const auto& v : GetFields()) {
1248 if (!typenames.CanBeFixedSize(v->GetType())) {
1249 AIDL_ERROR(v) << "The @FixedSize parcelable '" << this->GetName() << "' has a "
1250 << "non-fixed size field named " << v->GetName() << ".";
1251 success = false;
1252 }
1253 }
1254 }
1255
1256 if (IsJavaOnlyImmutable()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001257 // Immutable parcelables provide getters
Jooyung Han829ec7c2020-12-02 12:07:36 +09001258 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001259 success = false;
Jooyung Hand4057c42020-10-23 13:28:22 +09001260 }
1261 }
1262
Daniel Norman85aed542019-08-21 12:01:14 -07001263 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001264}
1265
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001266// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -07001267bool AidlTypeSpecifier::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1268 Options::Language lang) const {
Jooyung Hand09a21d2021-02-15 18:56:55 +09001269 if (IsGeneric()) {
1270 const auto& types = GetTypeParameters();
1271 for (const auto& arg : types) {
1272 if (!arg->LanguageSpecificCheckValid(typenames, lang)) {
1273 return false;
1274 }
1275 }
1276 }
1277
Andrei Homescub62afd92020-05-11 19:24:59 -07001278 if ((lang == Options::Language::NDK || lang == Options::Language::RUST) && IsArray() &&
1279 GetName() == "IBinder") {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001280 AIDL_ERROR(this) << "The " << to_string(lang) << " backend does not support array of IBinder";
Steven Moreland0185d9b2020-05-15 23:21:22 +00001281 return false;
1282 }
Andrei Homescub62afd92020-05-11 19:24:59 -07001283 if ((lang == Options::Language::NDK || lang == Options::Language::RUST) && IsArray() &&
1284 IsNullable()) {
Steven Moreland0185d9b2020-05-15 23:21:22 +00001285 if (GetName() == "ParcelFileDescriptor") {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001286 AIDL_ERROR(this) << "The " << to_string(lang)
Andrei Homescub62afd92020-05-11 19:24:59 -07001287 << " backend does not support nullable array of ParcelFileDescriptor";
Steven Moreland0185d9b2020-05-15 23:21:22 +00001288 return false;
1289 }
1290
Steven Morelandd59e3172020-05-11 16:42:09 -07001291 const auto defined_type = typenames.TryGetDefinedType(GetName());
1292 if (defined_type != nullptr && defined_type->AsParcelable() != nullptr) {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001293 AIDL_ERROR(this) << "The " << to_string(lang)
Andrei Homescub62afd92020-05-11 19:24:59 -07001294 << " backend does not support nullable array of parcelable";
Steven Morelandd59e3172020-05-11 16:42:09 -07001295 return false;
1296 }
1297 }
Andrei Homescub62afd92020-05-11 19:24:59 -07001298 if (this->GetName() == "FileDescriptor" &&
1299 (lang == Options::Language::NDK || lang == Options::Language::RUST)) {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001300 AIDL_ERROR(this) << "FileDescriptor isn't supported by the " << to_string(lang) << " backend.";
Steven Morelandc8a4ca82020-01-21 17:50:08 -08001301 return false;
1302 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001303 if (this->IsGeneric()) {
1304 if (this->GetName() == "List") {
Jooyung Han55f96ad2020-12-13 10:08:33 +09001305 if (lang == Options::Language::NDK) {
1306 const AidlTypeSpecifier& contained_type = *GetTypeParameters()[0];
1307 const string& contained_type_name = contained_type.GetName();
Jooyung Hane87cdd02020-12-11 16:47:35 +09001308 if (typenames.GetInterface(contained_type)) {
1309 AIDL_ERROR(this) << "List<" << contained_type_name
1310 << "> is not supported. List in NDK doesn't support interface.";
1311 return false;
1312 }
1313 if (contained_type_name == "IBinder") {
1314 AIDL_ERROR(this) << "List<" << contained_type_name
1315 << "> is not supported. List in NDK doesn't support IBinder.";
1316 return false;
Jeongik Cha08ca2182019-11-21 14:01:13 +09001317 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001318 }
Jeongik Chabb55b5e2020-01-07 23:11:26 +09001319 }
1320 }
Devin Moore6a01ca12020-08-28 10:24:19 -07001321
1322 if (this->IsArray()) {
1323 if (this->GetName() == "List" || this->GetName() == "Map" ||
1324 this->GetName() == "CharSequence") {
1325 AIDL_ERROR(this) << this->GetName() << "[] is not supported.";
Jeongik Chabb55b5e2020-01-07 23:11:26 +09001326 return false;
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001327 }
1328 }
Devin Moore6a01ca12020-08-28 10:24:19 -07001329
1330 if (lang != Options::Language::JAVA) {
1331 if (this->GetName() == "List" && !this->IsGeneric()) {
1332 AIDL_ERROR(this) << "Currently, only the Java backend supports non-generic List.";
1333 return false;
1334 }
1335 if (this->GetName() == "Map" || this->GetName() == "CharSequence") {
1336 AIDL_ERROR(this) << "Currently, only Java backend supports " << this->GetName() << ".";
1337 return false;
Jeongik Cha08ca2182019-11-21 14:01:13 +09001338 }
1339 }
1340
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001341 return true;
1342}
1343
1344// TODO: we should treat every backend all the same in future.
Jooyung Han589cfb02021-09-28 17:26:04 +09001345bool AidlDefinedType::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1346 Options::Language lang) const {
1347 struct Visitor : AidlVisitor {
1348 Visitor(const AidlTypenames& typenames, Options::Language lang)
1349 : typenames(typenames), lang(lang) {}
1350 void Visit(const AidlTypeSpecifier& type) override {
1351 success = success && type.LanguageSpecificCheckValid(typenames, lang);
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001352 }
Jooyung Han589cfb02021-09-28 17:26:04 +09001353 const AidlTypenames& typenames;
1354 Options::Language lang;
1355 bool success = true;
1356 } v(typenames, lang);
1357 VisitTopDown(v, *this);
1358 return v.success;
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001359}
1360
Daniel Norman85aed542019-08-21 12:01:14 -07001361AidlEnumerator::AidlEnumerator(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001362 AidlConstantValue* value, const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +09001363 : AidlCommentable(location, comments),
Jooyung Han29813842020-12-08 01:28:03 +09001364 name_(name),
1365 value_(value),
Jooyung Han29813842020-12-08 01:28:03 +09001366 value_user_specified_(value != nullptr) {}
Daniel Norman85aed542019-08-21 12:01:14 -07001367
1368bool AidlEnumerator::CheckValid(const AidlTypeSpecifier& enum_backing_type) const {
1369 if (GetValue() == nullptr) {
1370 return false;
1371 }
1372 if (!GetValue()->CheckValid()) {
1373 return false;
1374 }
Will McVickerd7d18df2019-09-12 13:40:50 -07001375 if (GetValue()->ValueString(enum_backing_type, AidlConstantValueDecorator).empty()) {
Daniel Norman85aed542019-08-21 12:01:14 -07001376 AIDL_ERROR(this) << "Enumerator type differs from enum backing type.";
1377 return false;
1378 }
1379 return true;
1380}
1381
1382string AidlEnumerator::ValueString(const AidlTypeSpecifier& backing_type,
1383 const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -07001384 return GetValue()->ValueString(backing_type, decorator);
Daniel Norman85aed542019-08-21 12:01:14 -07001385}
1386
1387AidlEnumDeclaration::AidlEnumDeclaration(const AidlLocation& location, const std::string& name,
1388 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jooyung Han8451a202021-01-16 03:07:06 +09001389 const std::string& package, const Comments& comments)
Jooyung Han829ec7c2020-12-02 12:07:36 +09001390 : AidlDefinedType(location, name, comments, package, nullptr),
Jooyung Han29813842020-12-08 01:28:03 +09001391 enumerators_(std::move(*enumerators)) {
Jooyung Han672557b2020-12-24 05:18:00 +09001392 // Fill missing enumerator values with <prev + 1>
1393 // This can't be done in Autofill() because type/ref resolution depends on this.
1394 // For example, with enum E { A, B = A }, B's value 'A' is a reference which can't be
1395 // resolved if A has no value set.
Daniel Normanb28684e2019-10-17 15:31:39 -07001396 const AidlEnumerator* previous = nullptr;
1397 for (const auto& enumerator : enumerators_) {
1398 if (enumerator->GetValue() == nullptr) {
Jooyung Han29813842020-12-08 01:28:03 +09001399 auto loc = enumerator->GetLocation();
Daniel Normanb28684e2019-10-17 15:31:39 -07001400 if (previous == nullptr) {
Devin Mooredf93ebb2020-03-25 14:03:35 -07001401 enumerator->SetValue(
Jooyung Han29813842020-12-08 01:28:03 +09001402 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "0")));
Daniel Normanb28684e2019-10-17 15:31:39 -07001403 } else {
Jooyung Hand0c8af02021-01-06 18:08:01 +09001404 auto prev_value = std::make_unique<AidlConstantReference>(loc, previous->GetName());
Daniel Normanb28684e2019-10-17 15:31:39 -07001405 enumerator->SetValue(std::make_unique<AidlBinaryConstExpression>(
Jooyung Han29813842020-12-08 01:28:03 +09001406 loc, std::move(prev_value), "+",
1407 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "1"))));
Daniel Normanb28684e2019-10-17 15:31:39 -07001408 }
1409 }
1410 previous = enumerator.get();
1411 }
1412}
1413
Jooyung Han672557b2020-12-24 05:18:00 +09001414bool AidlEnumDeclaration::Autofill(const AidlTypenames& typenames) {
1415 if (auto annot = BackingType(); annot != nullptr) {
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001416 // Autofill() is called before the grand CheckValid(). But AidlAnnotation::ParamValue()
1417 // calls AidlConstantValue::evaluate() which requires CheckValid() to be called before. So we
Jooyung Han672557b2020-12-24 05:18:00 +09001418 // need to call CheckValid().
1419 if (!annot->CheckValid()) {
1420 return false;
1421 }
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001422 auto type = annot->ParamValue<std::string>("type").value();
1423 backing_type_ =
Jooyung Han8451a202021-01-16 03:07:06 +09001424 std::make_unique<AidlTypeSpecifier>(annot->GetLocation(), type, false, nullptr, Comments{});
Jooyung Han672557b2020-12-24 05:18:00 +09001425 } else {
1426 // Default to byte type for enums.
1427 backing_type_ =
Jooyung Han8451a202021-01-16 03:07:06 +09001428 std::make_unique<AidlTypeSpecifier>(AIDL_LOCATION_HERE, "byte", false, nullptr, Comments{});
Jooyung Han672557b2020-12-24 05:18:00 +09001429 }
Steven Morelandb248d072021-09-29 19:07:17 -07001430
1431 // we only support/test a few backing types, so make sure this is a supported
1432 // one (otherwise boolean might work, which isn't supported/tested in all
1433 // backends)
1434 static std::set<string> kBackingTypes = {"byte", "int", "long"};
1435 if (kBackingTypes.find(backing_type_->GetName()) == kBackingTypes.end()) {
1436 AIDL_ERROR(this) << "Invalid backing type: " << backing_type_->GetName()
1437 << ". Backing type must be one of: " << Join(kBackingTypes, ", ");
1438 return false;
Jooyung Han672557b2020-12-24 05:18:00 +09001439 }
Steven Morelandb248d072021-09-29 19:07:17 -07001440
1441 // Autofill() is called before type resolution, we resolve the backing type manually.
1442 AIDL_FATAL_IF(!backing_type_->Resolve(typenames, nullptr),
1443 "supporting backing types must resolve");
1444
Jooyung Han672557b2020-12-24 05:18:00 +09001445 return true;
1446}
1447
Jooyung Han808a2a02020-12-28 16:46:54 +09001448bool AidlEnumDeclaration::CheckValid(const AidlTypenames& typenames) const {
1449 if (!AidlDefinedType::CheckValid(typenames)) {
Devin Moore24f68572020-02-26 13:20:59 -08001450 return false;
1451 }
Jooyung Han829ec7c2020-12-02 12:07:36 +09001452 if (!GetMembers().empty()) {
1453 AIDL_ERROR(this) << "Enum doesn't support fields/constants/methods.";
1454 return false;
1455 }
Daniel Norman85aed542019-08-21 12:01:14 -07001456 if (backing_type_ == nullptr) {
1457 AIDL_ERROR(this) << "Enum declaration missing backing type.";
1458 return false;
1459 }
1460 bool success = true;
1461 for (const auto& enumerator : enumerators_) {
1462 success = success && enumerator->CheckValid(GetBackingType());
1463 }
Jooyung Han3b990182020-12-22 17:44:31 +09001464
Jooyung Han808a2a02020-12-28 16:46:54 +09001465 return success;
Daniel Norman85aed542019-08-21 12:01:14 -07001466}
1467
Jooyung Han2946afc2020-10-05 20:29:16 +09001468AidlUnionDecl::AidlUnionDecl(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001469 const std::string& package, const Comments& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001470 std::vector<std::string>* type_params,
1471 std::vector<std::unique_ptr<AidlMember>>* members)
1472 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Jooyung Han2946afc2020-10-05 20:29:16 +09001473
Jooyung Han808a2a02020-12-28 16:46:54 +09001474bool AidlUnionDecl::CheckValid(const AidlTypenames& typenames) const {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001475 // visit parents
Jooyung Han808a2a02020-12-28 16:46:54 +09001476 if (!AidlParcelable::CheckValid(typenames)) {
Jooyung Hanfe89f122020-10-14 03:49:18 +09001477 return false;
1478 }
Jooyung Han59af9cc2020-10-25 21:44:14 +09001479
1480 // unions provide getters always
Jooyung Han829ec7c2020-12-02 12:07:36 +09001481 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001482 return false;
Jooyung Hanfe89f122020-10-14 03:49:18 +09001483 }
1484
1485 // now, visit self!
1486 bool success = true;
1487
1488 // TODO(b/170807936) do we need to allow ParcelableHolder in union?
1489 for (const auto& v : GetFields()) {
1490 if (v->GetType().GetName() == "ParcelableHolder") {
1491 AIDL_ERROR(*v) << "A union can't have a member of ParcelableHolder '" << v->GetName() << "'";
1492 success = false;
1493 }
1494 }
1495
Jooyung Hanfe89f122020-10-14 03:49:18 +09001496 if (GetFields().empty()) {
1497 AIDL_ERROR(*this) << "The union '" << this->GetName() << "' has no fields.";
1498 return false;
1499 }
1500
Jooyung Han53fb4242020-12-17 16:03:49 +09001501 // first member should have useful default value (implicit or explicit)
1502 const auto& first = GetFields()[0];
1503 if (!first->HasUsefulDefaultValue()) {
1504 // Most types can be initialized without a default value. For example,
1505 // interface types are inherently nullable. But, enum types should have
1506 // an explicit default value.
1507 if (!first->GetType().IsArray() && typenames.GetEnumDeclaration(first->GetType())) {
1508 AIDL_ERROR(first)
1509 << "The union's first member should have a useful default value. Enum types can be "
1510 "initialized with a reference. (e.g. ... = MyEnum.FOO;)";
1511 return false;
1512 }
1513 // In Java, array types are initialized as null without a default value. To be sure that default
1514 // initialized unions are accepted by other backends we require arrays also have a default
1515 // value.
1516 if (first->GetType().IsArray()) {
1517 AIDL_ERROR(first)
1518 << "The union's first member should have a useful default value. Arrays can be "
1519 "initialized with values(e.g. ... = { values... };) or marked as @nullable.";
1520 return false;
1521 }
1522 }
1523
Jooyung Hanfe89f122020-10-14 03:49:18 +09001524 return success;
1525}
1526
Steven Moreland46e9da82018-07-27 15:45:29 -07001527AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001528 const Comments& comments, bool oneway, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001529 std::vector<std::unique_ptr<AidlMember>>* members)
1530 : AidlDefinedType(location, name, comments, package, members) {
1531 for (auto& m : GetMethods()) {
1532 m.get()->ApplyInterfaceOneway(oneway);
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001533 }
Casey Dahlinfb7da2e2015-10-08 17:26:09 -07001534}
1535
Jooyung Han808a2a02020-12-28 16:46:54 +09001536bool AidlInterface::CheckValid(const AidlTypenames& typenames) const {
1537 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001538 return false;
1539 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001540 // Has to be a pointer due to deleting copy constructor. No idea why.
1541 map<string, const AidlMethod*> method_names;
1542 for (const auto& m : GetMethods()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001543 if (!m->GetType().CheckValid(typenames)) {
1544 return false;
1545 }
1546
Jeongik Cha649e8a72020-03-27 17:47:40 +09001547 // TODO(b/156872582): Support it when ParcelableHolder supports every backend.
1548 if (m->GetType().GetName() == "ParcelableHolder") {
1549 AIDL_ERROR(m) << "ParcelableHolder cannot be a return type";
1550 return false;
1551 }
Steven Morelandacd53472018-12-14 10:17:26 -08001552 if (m->IsOneway() && m->GetType().GetName() != "void") {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001553 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot return a value";
1554 return false;
1555 }
1556
1557 set<string> argument_names;
1558 for (const auto& arg : m->GetArguments()) {
1559 auto it = argument_names.find(arg->GetName());
1560 if (it != argument_names.end()) {
1561 AIDL_ERROR(m) << "method '" << m->GetName() << "' has duplicate argument name '"
1562 << arg->GetName() << "'";
1563 return false;
1564 }
1565 argument_names.insert(arg->GetName());
1566
Jooyung Han020d8d12021-02-26 17:23:02 +09001567 if (!arg->CheckValid(typenames)) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001568 return false;
1569 }
1570
Steven Morelandacd53472018-12-14 10:17:26 -08001571 if (m->IsOneway() && arg->IsOut()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001572 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot have out parameters";
1573 return false;
1574 }
Jooyung Han15fd6c62020-10-23 13:54:46 +09001575
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001576 // check that the name doesn't match a keyword
Jeongik Cha997281d2020-01-16 15:23:59 +09001577 if (IsJavaKeyword(arg->GetName().c_str())) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001578 AIDL_ERROR(arg) << "Argument name is a Java or aidl keyword";
1579 return false;
1580 }
1581
1582 // Reserve a namespace for internal use
1583 if (android::base::StartsWith(arg->GetName(), "_aidl")) {
1584 AIDL_ERROR(arg) << "Argument name cannot begin with '_aidl'";
1585 return false;
1586 }
Jooyung Hanfa181932021-06-12 07:56:53 +09001587
1588 if (arg->GetType().GetName() == "void") {
1589 AIDL_ERROR(arg->GetType())
1590 << "'void' is an invalid type for the parameter '" << arg->GetName() << "'";
1591 return false;
1592 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001593 }
1594
1595 auto it = method_names.find(m->GetName());
1596 // prevent duplicate methods
1597 if (it == method_names.end()) {
1598 method_names[m->GetName()] = m.get();
1599 } else {
1600 AIDL_ERROR(m) << "attempt to redefine method " << m->GetName() << ":";
1601 AIDL_ERROR(it->second) << "previously defined here.";
1602 return false;
1603 }
1604
Paul Trautrimb77048c2020-01-21 16:39:32 +09001605 static set<string> reserved_methods{"asBinder()", "getInterfaceHash()", "getInterfaceVersion()",
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001606 "getTransactionName(int)"};
1607
1608 if (reserved_methods.find(m->Signature()) != reserved_methods.end()) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001609 AIDL_ERROR(m) << " method " << m->Signature() << " is reserved for internal use.";
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001610 return false;
1611 }
1612 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001613
1614 bool success = true;
1615 set<string> constant_names;
Jooyung Han3f347ca2020-12-01 12:41:50 +09001616 for (const auto& constant : GetConstantDeclarations()) {
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001617 if (constant_names.count(constant->GetName()) > 0) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001618 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001619 success = false;
1620 }
1621 constant_names.insert(constant->GetName());
1622 success = success && constant->CheckValid(typenames);
1623 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001624 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001625}
1626
Jiyong Park27fd7fd2020-08-27 16:25:09 +09001627std::string AidlInterface::GetDescriptor() const {
1628 std::string annotatedDescriptor = AidlAnnotatable::GetDescriptor();
1629 if (annotatedDescriptor != "") {
1630 return annotatedDescriptor;
1631 }
1632 return GetCanonicalName();
1633}
1634
Jooyung Han132cf802021-01-15 02:17:32 +09001635AidlImport::AidlImport(const AidlLocation& location, const std::string& needed_class,
Jooyung Han8451a202021-01-16 03:07:06 +09001636 const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +09001637 : AidlNode(location, comments), needed_class_(needed_class) {}
Jooyung Han29813842020-12-08 01:28:03 +09001638
Jooyung Han13f1fa52021-06-11 18:06:12 +09001639AidlDocument::AidlDocument(const AidlLocation& location, const Comments& comments,
1640 std::vector<std::unique_ptr<AidlImport>> imports,
Jooyung Han35784982021-06-29 06:26:12 +09001641 std::vector<std::unique_ptr<AidlDefinedType>> defined_types,
1642 bool is_preprocessed)
Jooyung Han13f1fa52021-06-11 18:06:12 +09001643 : AidlCommentable(location, comments),
Jooyung Han35784982021-06-29 06:26:12 +09001644 AidlScope(this),
Jooyung Han13f1fa52021-06-11 18:06:12 +09001645 imports_(std::move(imports)),
Jooyung Han35784982021-06-29 06:26:12 +09001646 defined_types_(std::move(defined_types)),
1647 is_preprocessed_(is_preprocessed) {
Jooyung Han13f1fa52021-06-11 18:06:12 +09001648 for (const auto& t : defined_types_) {
1649 t->SetEnclosingScope(this);
1650 }
1651}
1652
1653// Resolves type name in the current document.
1654// - built-in types
1655// - imported types
1656// - top-level type
1657std::string AidlDocument::ResolveName(const std::string& name) const {
1658 if (AidlTypenames::IsBuiltinTypename(name)) {
1659 return name;
1660 }
1661
1662 const auto first_dot = name.find_first_of('.');
1663 // For "Outer.Inner", we look up "Outer" in the import list.
Jooyung Han29813842020-12-08 01:28:03 +09001664 const std::string class_name =
Jooyung Han13f1fa52021-06-11 18:06:12 +09001665 (first_dot == std::string::npos) ? name : name.substr(0, first_dot);
1666 // Keep ".Inner", to make a fully-qualified name
1667 const std::string nested_type = (first_dot == std::string::npos) ? "" : name.substr(first_dot);
1668
Jooyung Han29813842020-12-08 01:28:03 +09001669 for (const auto& import : Imports()) {
Jooyung Han13f1fa52021-06-11 18:06:12 +09001670 if (import->SimpleName() == class_name) {
1671 return import->GetNeededClass() + nested_type;
Jooyung Han29813842020-12-08 01:28:03 +09001672 }
1673 }
Jooyung Han13f1fa52021-06-11 18:06:12 +09001674
1675 // check if it is a top-level type.
1676 for (const auto& type : DefinedTypes()) {
1677 if (type->GetName() == class_name) {
1678 return type->GetCanonicalName() + nested_type;
1679 }
Jooyung Han29813842020-12-08 01:28:03 +09001680 }
Jooyung Han13f1fa52021-06-11 18:06:12 +09001681
1682 // name itself might be fully-qualified name.
1683 return name;
Steven Moreland26318532020-12-23 20:08:36 +00001684}