blob: da626291125e47c7765bbd9d0b6dd68ee3aac3e2 [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 Han2b3cd2a2021-10-15 06:54:55 +090078AidlNode::~AidlNode() {
79 if (!visited_) {
80 unvisited_locations_.push_back(location_);
81 }
82}
83
84void AidlNode::ClearUnvisitedNodes() {
85 unvisited_locations_.clear();
86}
87
88const std::vector<AidlLocation>& AidlNode::GetLocationsOfUnvisitedNodes() {
89 return unvisited_locations_;
90}
91
92void AidlNode::MarkVisited() const {
93 visited_ = true;
94}
95
Jooyung Han8451a202021-01-16 03:07:06 +090096AidlNode::AidlNode(const AidlLocation& location, const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +090097 : location_(location), comments_(comments) {}
Steven Moreland46e9da82018-07-27 15:45:29 -070098
Mathew Inwoodadb74672019-11-29 14:01:53 +000099std::string AidlNode::PrintLine() const {
Andrei Onea8714b022019-02-01 18:55:54 +0000100 std::stringstream ss;
101 ss << location_.file_ << ":" << location_.begin_.line;
102 return ss.str();
103}
104
Mathew Inwoodadb74672019-11-29 14:01:53 +0000105std::string AidlNode::PrintLocation() const {
106 std::stringstream ss;
107 ss << location_.file_ << ":" << location_.begin_.line << ":" << location_.begin_.column << ":"
108 << location_.end_.line << ":" << location_.end_.column;
109 return ss.str();
110}
111
Jooyung Han2b3cd2a2021-10-15 06:54:55 +0900112std::vector<AidlLocation> AidlNode::unvisited_locations_;
113
Jooyung Han8451a202021-01-16 03:07:06 +0900114static const AidlTypeSpecifier kStringType{AIDL_LOCATION_HERE, "String", false, nullptr,
115 Comments{}};
116static const AidlTypeSpecifier kStringArrayType{AIDL_LOCATION_HERE, "String", true, nullptr,
117 Comments{}};
118static const AidlTypeSpecifier kIntType{AIDL_LOCATION_HERE, "int", false, nullptr, Comments{}};
119static const AidlTypeSpecifier kLongType{AIDL_LOCATION_HERE, "long", false, nullptr, Comments{}};
120static const AidlTypeSpecifier kBooleanType{AIDL_LOCATION_HERE, "boolean", false, nullptr,
121 Comments{}};
Jooyung Han5c2fcae2020-12-26 00:04:39 +0900122
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700123const std::vector<AidlAnnotation::Schema>& AidlAnnotation::AllSchemas() {
124 static const std::vector<Schema> kSchemas{
Jooyung Han01720ed2021-08-13 07:46:07 +0900125 {AidlAnnotation::Type::NULLABLE,
126 "nullable",
127 CONTEXT_TYPE_SPECIFIER,
128 {{"heap", kBooleanType}}},
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900129 {AidlAnnotation::Type::UTF8_IN_CPP, "utf8InCpp", CONTEXT_TYPE_SPECIFIER, {}},
130 {AidlAnnotation::Type::SENSITIVE_DATA, "SensitiveData", CONTEXT_TYPE_INTERFACE, {}},
131 {AidlAnnotation::Type::VINTF_STABILITY, "VintfStability", CONTEXT_TYPE, {}},
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700132 {AidlAnnotation::Type::UNSUPPORTED_APP_USAGE,
133 "UnsupportedAppUsage",
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900134 CONTEXT_TYPE | CONTEXT_MEMBER,
Jooyung Han5c2fcae2020-12-26 00:04:39 +0900135 {{"expectedSignature", kStringType},
136 {"implicitMember", kStringType},
137 {"maxTargetSdk", kIntType},
138 {"publicAlternatives", kStringType},
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900139 {"trackingBug", kLongType}}},
140 {AidlAnnotation::Type::JAVA_STABLE_PARCELABLE,
141 "JavaOnlyStableParcelable",
142 CONTEXT_TYPE_UNSTRUCTURED_PARCELABLE,
143 {}},
144 {AidlAnnotation::Type::HIDE, "Hide", CONTEXT_TYPE | CONTEXT_MEMBER, {}},
145 {AidlAnnotation::Type::BACKING,
146 "Backing",
147 CONTEXT_TYPE_ENUM,
148 {{"type", kStringType, /* required= */ true}}},
Jooyung Han5721a232020-12-24 04:34:55 +0900149 {AidlAnnotation::Type::JAVA_PASSTHROUGH,
150 "JavaPassthrough",
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900151 CONTEXT_ALL,
152 {{"annotation", kStringType, /* required= */ true}},
153 /* repeatable= */ true},
Jiyong Park9aa3d042020-12-04 23:30:02 +0900154 {AidlAnnotation::Type::JAVA_DERIVE,
Jooyung Han5721a232020-12-24 04:34:55 +0900155 "JavaDerive",
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900156 CONTEXT_TYPE_STRUCTURED_PARCELABLE | CONTEXT_TYPE_UNION,
157 {{"toString", kBooleanType}, {"equals", kBooleanType}}},
158 {AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE,
159 "JavaOnlyImmutable",
160 CONTEXT_TYPE_STRUCTURED_PARCELABLE | CONTEXT_TYPE_UNION |
161 CONTEXT_TYPE_UNSTRUCTURED_PARCELABLE,
162 {}},
163 {AidlAnnotation::Type::FIXED_SIZE, "FixedSize", CONTEXT_TYPE_STRUCTURED_PARCELABLE, {}},
164 {AidlAnnotation::Type::DESCRIPTOR,
165 "Descriptor",
166 CONTEXT_TYPE_INTERFACE,
167 {{"value", kStringType, /* required= */ true}}},
Andrei Homescue61feb52020-08-18 15:44:24 -0700168 {AidlAnnotation::Type::RUST_DERIVE,
169 "RustDerive",
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900170 CONTEXT_TYPE_STRUCTURED_PARCELABLE | CONTEXT_TYPE_UNION,
Jooyung Han5c2fcae2020-12-26 00:04:39 +0900171 {{"Copy", kBooleanType},
172 {"Clone", kBooleanType},
173 {"PartialOrd", kBooleanType},
174 {"Ord", kBooleanType},
175 {"PartialEq", kBooleanType},
176 {"Eq", kBooleanType},
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900177 {"Hash", kBooleanType}}},
Jooyung Hanf8dbbcc2020-12-26 03:05:55 +0900178 {AidlAnnotation::Type::SUPPRESS_WARNINGS,
179 "SuppressWarnings",
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900180 CONTEXT_TYPE | CONTEXT_MEMBER,
181 {{"value", kStringArrayType, /* required= */ true}}},
Thiébaud Weksteen9ab59122021-09-20 09:37:38 +0200182 {AidlAnnotation::Type::ENFORCE,
183 "Enforce",
184 CONTEXT_METHOD,
185 {{"condition", kStringType, /* required= */ true}}},
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900186 };
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700187 return kSchemas;
188}
Jiyong Park68bc77a2018-07-19 19:00:45 +0900189
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700190std::string AidlAnnotation::TypeToString(Type type) {
191 for (const Schema& schema : AllSchemas()) {
192 if (type == schema.type) return schema.name;
193 }
194 AIDL_FATAL(AIDL_LOCATION_HERE) << "Unrecognized type: " << static_cast<size_t>(type);
195 __builtin_unreachable();
196}
Andrei Onea9445fc62019-06-27 18:11:59 +0100197
Jooyung Han442cacf2021-09-13 17:44:56 +0900198std::unique_ptr<AidlAnnotation> AidlAnnotation::Parse(
Andrei Onea9445fc62019-06-27 18:11:59 +0100199 const AidlLocation& location, const string& name,
Jooyung Han442cacf2021-09-13 17:44:56 +0900200 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameter_list,
Jooyung Han8451a202021-01-16 03:07:06 +0900201 const Comments& comments) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700202 const Schema* schema = nullptr;
203 for (const Schema& a_schema : AllSchemas()) {
204 if (a_schema.name == name) {
205 schema = &a_schema;
206 }
207 }
208
209 if (schema == nullptr) {
Jiyong Park68bc77a2018-07-19 19:00:45 +0900210 std::ostringstream stream;
Steven Moreland46e9da82018-07-27 15:45:29 -0700211 stream << "'" << name << "' is not a recognized annotation. ";
Jiyong Park68bc77a2018-07-19 19:00:45 +0900212 stream << "It must be one of:";
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700213 for (const Schema& s : AllSchemas()) {
214 stream << " " << s.name;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900215 }
216 stream << ".";
Steven Moreland46e9da82018-07-27 15:45:29 -0700217 AIDL_ERROR(location) << stream.str();
Jooyung Han442cacf2021-09-13 17:44:56 +0900218 return {};
Andrei Onea9445fc62019-06-27 18:11:59 +0100219 }
220
Jooyung Han442cacf2021-09-13 17:44:56 +0900221 return std::unique_ptr<AidlAnnotation>(
222 new AidlAnnotation(location, *schema, std::move(parameter_list), comments));
Jiyong Park68bc77a2018-07-19 19:00:45 +0900223}
224
Jooyung Han442cacf2021-09-13 17:44:56 +0900225AidlAnnotation::AidlAnnotation(const AidlLocation& location, const Schema& schema,
226 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters,
227 const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +0900228 : AidlNode(location, comments), schema_(schema), parameters_(std::move(parameters)) {}
Andrei Onea9445fc62019-06-27 18:11:59 +0100229
Jooyung Hanc5688f72021-01-05 15:41:48 +0900230struct ConstReferenceFinder : AidlVisitor {
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900231 const AidlConstantReference* found;
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900232 void Visit(const AidlConstantReference& ref) override {
Jooyung Han690f5842020-12-04 13:02:04 +0900233 if (!found) found = &ref;
234 }
Jooyung Hanc5688f72021-01-05 15:41:48 +0900235 static const AidlConstantReference* Find(const AidlConstantValue& c) {
236 ConstReferenceFinder finder;
237 VisitTopDown(finder, c);
238 return finder.found;
239 }
Jooyung Han690f5842020-12-04 13:02:04 +0900240};
241
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900242// Checks if annotation complies with the schema
243// - every parameter is known and has well-typed value.
244// - every required parameter is present.
Andrei Onea9445fc62019-06-27 18:11:59 +0100245bool AidlAnnotation::CheckValid() const {
Andrei Onea9445fc62019-06-27 18:11:59 +0100246 for (const auto& name_and_param : parameters_) {
247 const std::string& param_name = name_and_param.first;
248 const std::shared_ptr<AidlConstantValue>& param = name_and_param.second;
Jooyung Han690f5842020-12-04 13:02:04 +0900249
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900250 const ParamType* param_type = schema_.ParamType(param_name);
251 if (!param_type) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100252 std::ostringstream stream;
253 stream << "Parameter " << param_name << " not supported ";
Devin Mooredecaf292020-04-30 09:16:40 -0700254 stream << "for annotation " << GetName() << ". ";
Andrei Onea9445fc62019-06-27 18:11:59 +0100255 stream << "It must be one of:";
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900256 for (const auto& param : schema_.parameters) {
257 stream << " " << param.name;
Andrei Onea9445fc62019-06-27 18:11:59 +0100258 }
259 AIDL_ERROR(this) << stream.str();
260 return false;
261 }
Jooyung Han690f5842020-12-04 13:02:04 +0900262
Jooyung Hanc5688f72021-01-05 15:41:48 +0900263 const auto& found = ConstReferenceFinder::Find(*param);
264 if (found) {
265 AIDL_ERROR(found) << "Value must be a constant expression but contains reference to "
266 << found->GetFieldName() << ".";
Jooyung Han690f5842020-12-04 13:02:04 +0900267 return false;
268 }
269
270 if (!param->CheckValid()) {
271 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
272 << GetName() << ".";
273 return false;
274 }
275
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900276 const std::string param_value =
277 param->ValueString(param_type->type, AidlConstantValueDecorator);
Andrei Onea9445fc62019-06-27 18:11:59 +0100278 // Assume error on empty string.
279 if (param_value == "") {
280 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
281 << GetName() << ".";
282 return false;
283 }
284 }
Jooyung Han5721a232020-12-24 04:34:55 +0900285 bool success = true;
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900286 for (const auto& param : schema_.parameters) {
287 if (param.required && parameters_.count(param.name) == 0) {
288 AIDL_ERROR(this) << "Missing '" << param.name << "' on @" << GetName() << ".";
Jooyung Han5721a232020-12-24 04:34:55 +0900289 success = false;
290 }
291 }
Thiébaud Weksteen9ab59122021-09-20 09:37:38 +0200292 if (!success) {
293 return false;
294 }
295 // For @Enforce annotations, validates the expression.
296 if (schema_.type == AidlAnnotation::Type::ENFORCE) {
297 auto expr = EnforceExpression();
298 if (!expr.ok()) {
299 AIDL_ERROR(this) << "Unable to parse @Enforce annotation: " << expr.error();
300 return false;
301 }
302 }
303 return true;
304}
305
306Result<unique_ptr<perm::Expression>> AidlAnnotation::EnforceExpression() const {
307 auto perm_expr = ParamValue<std::string>("condition");
308 if (perm_expr.has_value()) {
309 return perm::Parser::Parse(perm_expr.value());
310 }
311 return Error() << "No condition parameter for @Enforce";
Andrei Onea9445fc62019-06-27 18:11:59 +0100312}
313
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900314// Checks if the annotation is applicable to the current context.
315// For example, annotations like @VintfStability, @FixedSize is not applicable to AidlTypeSpecifier
316// nodes.
317bool AidlAnnotation::CheckContext(TargetContext context) const {
318 if (schema_.target_context & static_cast<uint32_t>(context)) {
319 return true;
320 }
321 const static map<TargetContext, string> context_name_map{
322 {CONTEXT_TYPE_INTERFACE, "interface"},
323 {CONTEXT_TYPE_ENUM, "enum"},
324 {CONTEXT_TYPE_STRUCTURED_PARCELABLE, "structured parcelable"},
325 {CONTEXT_TYPE_UNION, "union"},
326 {CONTEXT_TYPE_UNSTRUCTURED_PARCELABLE, "parcelable"},
327 {CONTEXT_CONST, "constant"},
328 {CONTEXT_FIELD, "field"},
329 {CONTEXT_METHOD, "method"},
330 {CONTEXT_TYPE_SPECIFIER, "type"},
331 };
332 vector<string> available;
333 for (const auto& [context, name] : context_name_map) {
334 if (schema_.target_context & context) {
335 available.push_back(name);
336 }
337 }
338 AIDL_ERROR(this) << "@" << GetName() << " is not available. It can annotate {"
339 << Join(available, ", ") << "}.";
340 return false;
341}
342
Andrei Onea9445fc62019-06-27 18:11:59 +0100343std::map<std::string, std::string> AidlAnnotation::AnnotationParams(
344 const ConstantValueDecorator& decorator) const {
345 std::map<std::string, std::string> raw_params;
Andrei Onea9445fc62019-06-27 18:11:59 +0100346 for (const auto& name_and_param : parameters_) {
347 const std::string& param_name = name_and_param.first;
348 const std::shared_ptr<AidlConstantValue>& param = name_and_param.second;
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900349 const ParamType* param_type = schema_.ParamType(param_name);
350 AIDL_FATAL_IF(!param_type, this);
351 raw_params.emplace(param_name, param->ValueString(param_type->type, decorator));
Andrei Onea9445fc62019-06-27 18:11:59 +0100352 }
353 return raw_params;
354}
Steven Moreland46e9da82018-07-27 15:45:29 -0700355
Jooyung Han965e31d2020-11-27 12:30:16 +0900356std::string AidlAnnotation::ToString() const {
Daniel Norman37d43dd2019-09-09 17:22:34 -0700357 if (parameters_.empty()) {
358 return "@" + GetName();
359 } else {
360 vector<string> param_strings;
Jooyung Han965e31d2020-11-27 12:30:16 +0900361 for (const auto& [name, value] : AnnotationParams(AidlConstantValueDecorator)) {
Daniel Norman37d43dd2019-09-09 17:22:34 -0700362 param_strings.emplace_back(name + "=" + value);
363 }
364 return "@" + GetName() + "(" + Join(param_strings, ", ") + ")";
365 }
366}
367
Jooyung Hanc5688f72021-01-05 15:41:48 +0900368void AidlAnnotation::TraverseChildren(std::function<void(const AidlNode&)> traverse) const {
369 for (const auto& [name, value] : parameters_) {
370 (void)name;
371 traverse(*value);
372 }
373}
374
Steven Morelanda7560e82021-10-08 16:24:39 -0700375static const AidlAnnotation* GetAnnotation(
376 const vector<std::unique_ptr<AidlAnnotation>>& annotations, AidlAnnotation::Type type) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100377 for (const auto& a : annotations) {
Steven Morelanda7560e82021-10-08 16:24:39 -0700378 if (a->GetType() == type) {
379 AIDL_FATAL_IF(a->Repeatable(), a)
Jooyung Hand902a972020-10-23 17:32:44 +0900380 << "Trying to get a single annotation when it is repeatable.";
Steven Morelanda7560e82021-10-08 16:24:39 -0700381 return a.get();
Andrei Onea9445fc62019-06-27 18:11:59 +0100382 }
383 }
384 return nullptr;
385}
386
Jooyung Han8451a202021-01-16 03:07:06 +0900387AidlAnnotatable::AidlAnnotatable(const AidlLocation& location, const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +0900388 : AidlCommentable(location, comments) {}
Steven Moreland46e9da82018-07-27 15:45:29 -0700389
Jiyong Park68bc77a2018-07-19 19:00:45 +0900390bool AidlAnnotatable::IsNullable() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700391 return GetAnnotation(annotations_, AidlAnnotation::Type::NULLABLE);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900392}
393
Jooyung Han01720ed2021-08-13 07:46:07 +0900394bool AidlAnnotatable::IsHeapNullable() const {
395 auto annot = GetAnnotation(annotations_, AidlAnnotation::Type::NULLABLE);
396 if (annot) {
397 return annot->ParamValue<bool>("heap").value_or(false);
398 }
399 return false;
400}
401
Jiyong Park68bc77a2018-07-19 19:00:45 +0900402bool AidlAnnotatable::IsUtf8InCpp() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700403 return GetAnnotation(annotations_, AidlAnnotation::Type::UTF8_IN_CPP);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900404}
405
Steven Morelanda7764e52020-10-27 17:29:29 +0000406bool AidlAnnotatable::IsSensitiveData() const {
407 return GetAnnotation(annotations_, AidlAnnotation::Type::SENSITIVE_DATA);
408}
409
Steven Morelanda57d0a62019-07-30 09:41:14 -0700410bool AidlAnnotatable::IsVintfStability() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700411 return GetAnnotation(annotations_, AidlAnnotation::Type::VINTF_STABILITY);
Steven Morelanda57d0a62019-07-30 09:41:14 -0700412}
413
Jeongik Chad0a10272020-08-06 16:33:36 +0900414bool AidlAnnotatable::IsJavaOnlyImmutable() const {
415 return GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE);
Jeongik Cha36f76c32020-07-28 00:25:52 +0900416}
417
Devin Moorec7e47a32020-08-07 10:55:25 -0700418bool AidlAnnotatable::IsFixedSize() const {
419 return GetAnnotation(annotations_, AidlAnnotation::Type::FIXED_SIZE);
420}
421
Andrei Onea9445fc62019-06-27 18:11:59 +0100422const AidlAnnotation* AidlAnnotatable::UnsupportedAppUsage() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700423 return GetAnnotation(annotations_, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE);
Jiyong Parka6605ab2018-11-11 14:30:21 +0900424}
425
Andrei Homescue61feb52020-08-18 15:44:24 -0700426const AidlAnnotation* AidlAnnotatable::RustDerive() const {
427 return GetAnnotation(annotations_, AidlAnnotation::Type::RUST_DERIVE);
428}
429
Jooyung Han672557b2020-12-24 05:18:00 +0900430const AidlAnnotation* AidlAnnotatable::BackingType() const {
431 return GetAnnotation(annotations_, AidlAnnotation::Type::BACKING);
Daniel Norman85aed542019-08-21 12:01:14 -0700432}
433
Jooyung Hanf8dbbcc2020-12-26 03:05:55 +0900434std::vector<std::string> AidlAnnotatable::SuppressWarnings() const {
435 auto annot = GetAnnotation(annotations_, AidlAnnotation::Type::SUPPRESS_WARNINGS);
436 if (annot) {
437 auto names = annot->ParamValue<std::vector<std::string>>("value");
438 AIDL_FATAL_IF(!names.has_value(), this);
439 return std::move(names.value());
440 }
441 return {};
442}
443
Thiébaud Weksteen5a4db212021-09-02 17:09:34 +0200444// Parses the @Enforce annotation expression.
445std::unique_ptr<perm::Expression> AidlAnnotatable::EnforceExpression(
446 const AidlNode& context) const {
447 auto annot = GetAnnotation(annotations_, AidlAnnotation::Type::ENFORCE);
448 if (annot) {
Thiébaud Weksteen9ab59122021-09-20 09:37:38 +0200449 auto perm_expr = annot->EnforceExpression();
450 if (!perm_expr.ok()) {
451 // This should have been caught during validation.
452 AIDL_FATAL(context) << "Unable to parse @Enforce annotation: " << perm_expr.error();
Thiébaud Weksteen5a4db212021-09-02 17:09:34 +0200453 }
Thiébaud Weksteen9ab59122021-09-20 09:37:38 +0200454 return std::move(perm_expr.value());
Thiébaud Weksteen5a4db212021-09-02 17:09:34 +0200455 }
456 return {};
457}
458
Jeongik Cha88f95a82020-01-15 13:02:16 +0900459bool AidlAnnotatable::IsStableApiParcelable(Options::Language lang) const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700460 return lang == Options::Language::JAVA &&
461 GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_STABLE_PARCELABLE);
Jeongik Cha82317dd2019-02-27 20:26:11 +0900462}
463
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800464bool AidlAnnotatable::IsHide() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700465 return GetAnnotation(annotations_, AidlAnnotation::Type::HIDE);
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800466}
467
Jooyung Han829ec7c2020-12-02 12:07:36 +0900468bool AidlAnnotatable::JavaDerive(const std::string& method) const {
469 auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_DERIVE);
470 if (annotation != nullptr) {
Jooyung Hanb3c77ed2020-12-26 02:02:45 +0900471 return annotation->ParamValue<bool>(method).value_or(false);
Jooyung Han829ec7c2020-12-02 12:07:36 +0900472 }
473 return false;
Jiyong Park43113fb2020-07-20 16:26:19 +0900474}
475
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900476std::string AidlAnnotatable::GetDescriptor() const {
477 auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::DESCRIPTOR);
478 if (annotation != nullptr) {
Jooyung Hanb3c77ed2020-12-26 02:02:45 +0900479 return annotation->ParamValue<std::string>("value").value();
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900480 }
481 return "";
482}
483
Devin Moore24f68572020-02-26 13:20:59 -0800484bool AidlAnnotatable::CheckValid(const AidlTypenames&) const {
Andrei Onea9445fc62019-06-27 18:11:59 +0100485 for (const auto& annotation : GetAnnotations()) {
Steven Morelanda7560e82021-10-08 16:24:39 -0700486 if (!annotation->CheckValid()) {
Jooyung Hand902a972020-10-23 17:32:44 +0900487 return false;
488 }
489 }
490
491 std::map<AidlAnnotation::Type, AidlLocation> declared;
492 for (const auto& annotation : GetAnnotations()) {
Steven Morelanda7560e82021-10-08 16:24:39 -0700493 const auto& [iter, inserted] =
494 declared.emplace(annotation->GetType(), annotation->GetLocation());
495 if (!inserted && !annotation->Repeatable()) {
496 AIDL_ERROR(this) << "'" << annotation->GetName()
Jooyung Hand902a972020-10-23 17:32:44 +0900497 << "' is repeated, but not allowed. Previous location: " << iter->second;
498 return false;
499 }
Andrei Onea9445fc62019-06-27 18:11:59 +0100500 }
Steven Morelanda57d0a62019-07-30 09:41:14 -0700501
Andrei Onea9445fc62019-06-27 18:11:59 +0100502 return true;
503}
504
Jiyong Park68bc77a2018-07-19 19:00:45 +0900505string AidlAnnotatable::ToString() const {
506 vector<string> ret;
507 for (const auto& a : annotations_) {
Steven Morelanda7560e82021-10-08 16:24:39 -0700508 ret.emplace_back(a->ToString());
Jiyong Park68bc77a2018-07-19 19:00:45 +0900509 }
510 std::sort(ret.begin(), ret.end());
511 return Join(ret, " ");
512}
513
Steven Moreland46e9da82018-07-27 15:45:29 -0700514AidlTypeSpecifier::AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name,
515 bool is_array,
Jiyong Park1deecc32018-07-17 01:14:41 +0900516 vector<unique_ptr<AidlTypeSpecifier>>* type_params,
Jooyung Han8451a202021-01-16 03:07:06 +0900517 const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +0900518 : AidlAnnotatable(location, comments),
Jeongik Chadf76dc72019-11-28 00:08:47 +0900519 AidlParameterizable<unique_ptr<AidlTypeSpecifier>>(type_params),
Steven Moreland46e9da82018-07-27 15:45:29 -0700520 unresolved_name_(unresolved_name),
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700521 is_array_(is_array),
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900522 split_name_(Split(unresolved_name, ".")) {}
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700523
Steven Moreland0cac8662021-10-08 16:43:29 -0700524void AidlTypeSpecifier::ViewAsArrayBase(std::function<void(const AidlTypeSpecifier&)> func) const {
Steven Moreland3f658cf2018-08-20 13:40:54 -0700525 AIDL_FATAL_IF(!is_array_, this);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900526 // Declaring array of generic type cannot happen, it is grammar error.
527 AIDL_FATAL_IF(IsGeneric(), this);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700528
Steven Moreland0cac8662021-10-08 16:43:29 -0700529 is_array_ = false;
530 func(*this);
531 is_array_ = true;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700532}
533
Jooyung Han965e31d2020-11-27 12:30:16 +0900534string AidlTypeSpecifier::Signature() const {
Jiyong Park1deecc32018-07-17 01:14:41 +0900535 string ret = GetName();
536 if (IsGeneric()) {
537 vector<string> arg_names;
538 for (const auto& ta : GetTypeParameters()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900539 arg_names.emplace_back(ta->Signature());
Jiyong Parkccf00f82018-07-17 01:39:23 +0900540 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900541 ret += "<" + Join(arg_names, ",") + ">";
Jiyong Parkccf00f82018-07-17 01:39:23 +0900542 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900543 if (IsArray()) {
544 ret += "[]";
545 }
546 return ret;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900547}
548
Jooyung Han965e31d2020-11-27 12:30:16 +0900549string AidlTypeSpecifier::ToString() const {
550 string ret = Signature();
Jiyong Park02da7422018-07-16 16:00:26 +0900551 string annotations = AidlAnnotatable::ToString();
552 if (annotations != "") {
553 ret = annotations + " " + ret;
554 }
555 return ret;
556}
557
Jooyung Han13f1fa52021-06-11 18:06:12 +0900558// When `scope` is specified, name is resolved first based on it.
559// `scope` can be null for built-in types and fully-qualified types.
560bool AidlTypeSpecifier::Resolve(const AidlTypenames& typenames, const AidlScope* scope) {
Steven Moreland21780812020-09-11 01:29:45 +0000561 AIDL_FATAL_IF(IsResolved(), this);
Jooyung Han13f1fa52021-06-11 18:06:12 +0900562 std::string name = unresolved_name_;
563 if (scope) {
564 name = scope->ResolveName(name);
565 }
566 AidlTypenames::ResolvedTypename result = typenames.ResolveTypename(name);
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700567 if (result.is_resolved) {
568 fully_qualified_name_ = result.canonical_name;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900569 split_name_ = Split(fully_qualified_name_, ".");
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900570 defined_type_ = result.defined_type;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900571 }
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700572 return result.is_resolved;
Casey Dahlin70078e62015-09-30 17:01:30 -0700573}
574
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900575const AidlDefinedType* AidlTypeSpecifier::GetDefinedType() const {
576 return defined_type_;
577}
578
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900579bool AidlTypeSpecifier::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -0800580 if (!AidlAnnotatable::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100581 return false;
582 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900583 if (IsGeneric()) {
Jooyung Hand09a21d2021-02-15 18:56:55 +0900584 const auto& types = GetTypeParameters();
585 for (const auto& arg : types) {
586 if (!arg->CheckValid(typenames)) {
587 return false;
588 }
589 }
Jeongik Chae74c86d2019-12-12 16:54:03 +0900590
Jooyung Hand09a21d2021-02-15 18:56:55 +0900591 const string& type_name = GetName();
Jeongik Chae74c86d2019-12-12 16:54:03 +0900592 // TODO(b/136048684) Disallow to use primitive types only if it is List or Map.
593 if (type_name == "List" || type_name == "Map") {
Jooyung Hane87cdd02020-12-11 16:47:35 +0900594 if (std::any_of(types.begin(), types.end(), [&](auto& type_ptr) {
Jooyung Han1f35ef32021-02-15 19:08:05 +0900595 return !type_ptr->IsArray() &&
596 (typenames.GetEnumDeclaration(*type_ptr) ||
597 AidlTypenames::IsPrimitiveTypename(type_ptr->GetName()));
Jeongik Chae74c86d2019-12-12 16:54:03 +0900598 })) {
Devin Moore7b8d5c92020-03-17 14:14:08 -0700599 AIDL_ERROR(this) << "A generic type cannot have any primitive type parameters.";
Jeongik Chae74c86d2019-12-12 16:54:03 +0900600 return false;
601 }
602 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800603 const auto defined_type = typenames.TryGetDefinedType(type_name);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900604 const auto parameterizable =
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800605 defined_type != nullptr ? defined_type->AsParameterizable() : nullptr;
606 const bool is_user_defined_generic_type =
Jeongik Chadf76dc72019-11-28 00:08:47 +0900607 parameterizable != nullptr && parameterizable->IsGeneric();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800608 const size_t num_params = GetTypeParameters().size();
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900609 if (type_name == "List") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800610 if (num_params > 1) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900611 AIDL_ERROR(this) << "List can only have one type parameter, but got: '" << Signature()
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000612 << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900613 return false;
614 }
Jooyung Han55f96ad2020-12-13 10:08:33 +0900615 const AidlTypeSpecifier& contained_type = *GetTypeParameters()[0];
Jooyung Hancea89002021-02-15 17:04:04 +0900616 if (contained_type.IsArray()) {
617 AIDL_ERROR(this)
618 << "List of arrays is not supported. List<T> supports parcelable/union, String, "
619 "IBinder, and ParcelFileDescriptor.";
620 return false;
621 }
Jooyung Han55f96ad2020-12-13 10:08:33 +0900622 const string& contained_type_name = contained_type.GetName();
623 if (AidlTypenames::IsBuiltinTypename(contained_type_name)) {
624 if (contained_type_name != "String" && contained_type_name != "IBinder" &&
625 contained_type_name != "ParcelFileDescriptor") {
626 AIDL_ERROR(this) << "List<" << contained_type_name
627 << "> is not supported. List<T> supports parcelable/union, String, "
628 "IBinder, and ParcelFileDescriptor.";
629 return false;
630 }
631 } else { // Defined types
632 if (typenames.GetInterface(contained_type)) {
633 AIDL_ERROR(this) << "List<" << contained_type_name
634 << "> is not supported. List<T> supports parcelable/union, String, "
635 "IBinder, and ParcelFileDescriptor.";
636 return false;
637 }
638 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900639 } else if (type_name == "Map") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800640 if (num_params != 0 && num_params != 2) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900641 AIDL_ERROR(this) << "Map must have 0 or 2 type parameters, but got "
Jooyung Han965e31d2020-11-27 12:30:16 +0900642 << "'" << Signature() << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900643 return false;
644 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800645 if (num_params == 2) {
Jooyung Hanaab242a2021-02-15 19:01:15 +0900646 const string& key_type = GetTypeParameters()[0]->Signature();
Jeongik Chae48d9942020-01-02 17:39:00 +0900647 if (key_type != "String") {
648 AIDL_ERROR(this) << "The type of key in map must be String, but it is "
649 << "'" << key_type << "'";
650 return false;
651 }
652 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800653 } else if (is_user_defined_generic_type) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900654 const size_t allowed = parameterizable->GetTypeParameters().size();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800655 if (num_params != allowed) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900656 AIDL_ERROR(this) << type_name << " must have " << allowed << " type parameters, but got "
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800657 << num_params;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900658 return false;
659 }
660 } else {
661 AIDL_ERROR(this) << type_name << " is not a generic type.";
662 return false;
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900663 }
664 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900665
Steven Moreland11cb9452020-01-21 16:56:58 -0800666 const bool is_generic_string_list = GetName() == "List" && IsGeneric() &&
667 GetTypeParameters().size() == 1 &&
668 GetTypeParameters()[0]->GetName() == "String";
669 if (IsUtf8InCpp() && (GetName() != "String" && !is_generic_string_list)) {
670 AIDL_ERROR(this) << "@utf8InCpp can only be used on String, String[], and List<String>.";
671 return false;
672 }
673
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900674 if (GetName() == "void") {
675 if (IsArray() || IsNullable() || IsUtf8InCpp()) {
676 AIDL_ERROR(this) << "void type cannot be an array or nullable or utf8 string";
677 return false;
678 }
679 }
680
681 if (IsArray()) {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800682 const auto defined_type = typenames.TryGetDefinedType(GetName());
683 if (defined_type != nullptr && defined_type->AsInterface() != nullptr) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900684 AIDL_ERROR(this) << "Binder type cannot be an array";
685 return false;
686 }
Steven Moreland8042d2d2020-09-30 23:31:32 +0000687 if (GetName() == "ParcelableHolder") {
688 AIDL_ERROR(this) << "Arrays of ParcelableHolder are not supported.";
689 return false;
690 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900691 }
692
693 if (IsNullable()) {
694 if (AidlTypenames::IsPrimitiveTypename(GetName()) && !IsArray()) {
695 AIDL_ERROR(this) << "Primitive type cannot get nullable annotation";
696 return false;
697 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800698 const auto defined_type = typenames.TryGetDefinedType(GetName());
699 if (defined_type != nullptr && defined_type->AsEnumDeclaration() != nullptr && !IsArray()) {
Daniel Normanee8674f2019-09-20 16:07:00 -0700700 AIDL_ERROR(this) << "Enum type cannot get nullable annotation";
701 return false;
702 }
Jeongik Chaf6ec8982020-10-15 00:10:30 +0900703 if (GetName() == "ParcelableHolder") {
704 AIDL_ERROR(this) << "ParcelableHolder cannot be nullable.";
705 return false;
706 }
Jooyung Han01720ed2021-08-13 07:46:07 +0900707 if (IsHeapNullable()) {
708 if (!defined_type || IsArray() || !defined_type->AsParcelable()) {
709 AIDL_ERROR(this) << "@nullable(heap=true) is available to parcelables.";
710 return false;
711 }
712 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900713 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900714 return true;
715}
716
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900717std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type,
Steven Moreland860b1942018-08-16 14:59:28 -0700718 const std::string& raw_value) {
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900719 if (type.IsArray()) {
720 return raw_value;
721 }
722
723 if (auto defined_type = type.GetDefinedType(); defined_type) {
724 auto enum_type = defined_type->AsEnumDeclaration();
725 AIDL_FATAL_IF(!enum_type, type) << "Invalid type for \"" << raw_value << "\"";
726 return type.GetName() + "." + raw_value.substr(raw_value.find_last_of('.') + 1);
727 }
Steven Moreland860b1942018-08-16 14:59:28 -0700728 return raw_value;
729}
730
Steven Moreland46e9da82018-07-27 15:45:29 -0700731AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
732 AidlTypeSpecifier* type, const std::string& name)
Steven Moreland541788d2020-05-21 22:05:52 +0000733 : AidlVariableDeclaration(location, type, name, AidlConstantValue::Default(*type)) {
734 default_user_specified_ = false;
735}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700736
Steven Moreland46e9da82018-07-27 15:45:29 -0700737AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
738 AidlTypeSpecifier* type, const std::string& name,
739 AidlConstantValue* default_value)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900740 : AidlMember(location, type->GetComments()),
Steven Moreland541788d2020-05-21 22:05:52 +0000741 type_(type),
742 name_(name),
743 default_user_specified_(true),
744 default_value_(default_value) {}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700745
Jooyung Han53fb4242020-12-17 16:03:49 +0900746bool AidlVariableDeclaration::HasUsefulDefaultValue() const {
747 if (GetDefaultValue()) {
748 return true;
749 }
750 // null is accepted as a valid default value in all backends
751 if (GetType().IsNullable()) {
752 return true;
753 }
754 return false;
755}
756
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900757bool AidlVariableDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700758 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900759 valid &= type_->CheckValid(typenames);
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900760
Steven Moreland54be7bd2019-12-05 11:17:53 -0800761 if (type_->GetName() == "void") {
762 AIDL_ERROR(this) << "Declaration " << name_
763 << " is void, but declarations cannot be of void type.";
764 valid = false;
765 }
766
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900767 if (default_value_ == nullptr) return valid;
Steven Moreland25294322018-08-07 18:13:55 -0700768 valid &= default_value_->CheckValid();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700769
Steven Moreland25294322018-08-07 18:13:55 -0700770 if (!valid) return false;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700771
Steven Moreland860b1942018-08-16 14:59:28 -0700772 return !ValueString(AidlConstantValueDecorator).empty();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700773}
Steven Moreland5557f1c2018-07-02 13:50:23 -0700774
Jooyung Hanacae85d2020-10-28 16:39:09 +0900775string AidlVariableDeclaration::GetCapitalizedName() const {
776 AIDL_FATAL_IF(name_.size() <= 0, *this) << "Name can't be empty.";
777 string str = name_;
778 str[0] = static_cast<char>(toupper(str[0]));
779 return str;
780}
781
Steven Moreland5557f1c2018-07-02 13:50:23 -0700782string AidlVariableDeclaration::ToString() const {
Jooyung Han965e31d2020-11-27 12:30:16 +0900783 string ret = type_->ToString() + " " + name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000784 if (default_value_ != nullptr && default_user_specified_) {
Steven Moreland860b1942018-08-16 14:59:28 -0700785 ret += " = " + ValueString(AidlConstantValueDecorator);
Steven Moreland9ea10e32018-07-19 15:26:09 -0700786 }
787 return ret;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700788}
789
Jiyong Park02da7422018-07-16 16:00:26 +0900790string AidlVariableDeclaration::Signature() const {
791 return type_->Signature() + " " + name_;
792}
793
Steven Moreland860b1942018-08-16 14:59:28 -0700794std::string AidlVariableDeclaration::ValueString(const ConstantValueDecorator& decorator) const {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900795 if (default_value_ != nullptr) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700796 return default_value_->ValueString(GetType(), decorator);
Jiyong Parka468e2a2018-08-29 21:25:18 +0900797 } else {
798 return "";
799 }
Steven Moreland25294322018-08-07 18:13:55 -0700800}
801
Jooyung Hanc5688f72021-01-05 15:41:48 +0900802void AidlVariableDeclaration::TraverseChildren(
803 std::function<void(const AidlNode&)> traverse) const {
804 traverse(GetType());
Jooyung Hanc3c739a2021-10-14 11:33:14 +0900805 if (auto default_value = GetDefaultValue(); default_value) {
806 traverse(*default_value);
Jooyung Hanc5688f72021-01-05 15:41:48 +0900807 }
808}
809
Steven Moreland46e9da82018-07-27 15:45:29 -0700810AidlArgument::AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
811 AidlTypeSpecifier* type, const std::string& name)
812 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700813 direction_(direction),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700814 direction_specified_(true) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700815
Steven Moreland46e9da82018-07-27 15:45:29 -0700816AidlArgument::AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type,
817 const std::string& name)
818 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700819 direction_(AidlArgument::IN_DIR),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700820 direction_specified_(false) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700821
Jooyung Han020d8d12021-02-26 17:23:02 +0900822static std::string to_string(AidlArgument::Direction direction) {
823 switch (direction) {
824 case AidlArgument::IN_DIR:
825 return "in";
826 case AidlArgument::OUT_DIR:
827 return "out";
828 case AidlArgument::INOUT_DIR:
829 return "inout";
830 }
831}
832
Jiyong Park02da7422018-07-16 16:00:26 +0900833string AidlArgument::GetDirectionSpecifier() const {
Casey Dahlinc378c992015-09-29 16:50:40 -0700834 string ret;
Casey Dahlinc378c992015-09-29 16:50:40 -0700835 if (direction_specified_) {
Jooyung Han020d8d12021-02-26 17:23:02 +0900836 ret = to_string(direction_);
Casey Dahlinc378c992015-09-29 16:50:40 -0700837 }
Casey Dahlinc378c992015-09-29 16:50:40 -0700838 return ret;
839}
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700840
Jiyong Park02da7422018-07-16 16:00:26 +0900841string AidlArgument::ToString() const {
Devin Mooreeccdb902020-03-24 16:22:40 -0700842 if (direction_specified_) {
843 return GetDirectionSpecifier() + " " + AidlVariableDeclaration::ToString();
844 } else {
845 return AidlVariableDeclaration::ToString();
846 }
Jiyong Park02da7422018-07-16 16:00:26 +0900847}
848
Jooyung Han020d8d12021-02-26 17:23:02 +0900849static std::string FormatDirections(const std::set<AidlArgument::Direction>& directions) {
850 std::vector<std::string> out;
851 for (const auto& d : directions) {
852 out.push_back(to_string(d));
853 }
854
855 if (out.size() <= 1) { // [] => "" or [A] => "A"
856 return Join(out, "");
857 } else if (out.size() == 2) { // [A,B] => "A or B"
858 return Join(out, " or ");
859 } else { // [A,B,C] => "A, B, or C"
860 out.back() = "or " + out.back();
861 return Join(out, ", ");
862 }
863}
864
865bool AidlArgument::CheckValid(const AidlTypenames& typenames) const {
866 if (!GetType().CheckValid(typenames)) {
867 return false;
868 }
869
870 const auto& aspect = typenames.GetArgumentAspect(GetType());
871
872 if (aspect.possible_directions.size() == 0) {
873 AIDL_ERROR(this) << aspect.name << " cannot be an argument type";
874 return false;
875 }
876
877 // when direction is not specified, "in" is assumed and should be the only possible direction
878 if (!DirectionWasSpecified() && aspect.possible_directions != std::set{AidlArgument::IN_DIR}) {
879 AIDL_ERROR(this) << "The direction of '" << GetName() << "' is not specified. " << aspect.name
880 << " can be an " << FormatDirections(aspect.possible_directions)
881 << " parameter.";
882 return false;
883 }
884
885 if (aspect.possible_directions.count(GetDirection()) == 0) {
886 AIDL_ERROR(this) << "'" << GetName() << "' can't be an " << GetDirectionSpecifier()
887 << " parameter because " << aspect.name << " can only be an "
888 << FormatDirections(aspect.possible_directions) << " parameter.";
889 return false;
890 }
891
892 return true;
893}
894
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900895bool AidlCommentable::IsHidden() const {
Jooyung Han24effbf2021-01-16 10:24:03 +0900896 return android::aidl::HasHideInComments(GetComments());
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900897}
898
899bool AidlCommentable::IsDeprecated() const {
Jooyung Hand4fe00e2021-01-11 16:21:53 +0900900 return android::aidl::FindDeprecated(GetComments()).has_value();
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900901}
902
Jooyung Han8451a202021-01-16 03:07:06 +0900903AidlMember::AidlMember(const AidlLocation& location, const Comments& comments)
Jooyung Han2aedb112021-09-29 09:37:59 +0900904 : AidlAnnotatable(location, comments) {}
Steven Moreland46e9da82018-07-27 15:45:29 -0700905
Steven Moreland46e9da82018-07-27 15:45:29 -0700906AidlConstantDeclaration::AidlConstantDeclaration(const AidlLocation& location,
907 AidlTypeSpecifier* type, const std::string& name,
908 AidlConstantValue* value)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900909 : AidlMember(location, type->GetComments()), type_(type), name_(name), value_(value) {}
Steven Moreland693640b2018-07-19 13:46:27 -0700910
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900911bool AidlConstantDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700912 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900913 valid &= type_->CheckValid(typenames);
Steven Moreland25294322018-08-07 18:13:55 -0700914 valid &= value_->CheckValid();
915 if (!valid) return false;
Steven Moreland693640b2018-07-19 13:46:27 -0700916
Steven Morelande689da22020-11-10 02:06:30 +0000917 const static set<string> kSupportedConstTypes = {"String", "byte", "int", "long"};
Jooyung Han965e31d2020-11-27 12:30:16 +0900918 if (kSupportedConstTypes.find(type_->Signature()) == kSupportedConstTypes.end()) {
919 AIDL_ERROR(this) << "Constant of type " << type_->Signature() << " is not supported.";
Steven Moreland693640b2018-07-19 13:46:27 -0700920 return false;
921 }
922
Will McVickerd7d18df2019-09-12 13:40:50 -0700923 return true;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700924}
925
Jiyong Parka428d212018-08-29 22:26:30 +0900926string AidlConstantDeclaration::ToString() const {
Jooyung Hanb3ca6302020-11-27 14:13:27 +0900927 return "const " + type_->ToString() + " " + name_ + " = " +
928 ValueString(AidlConstantValueDecorator);
Jiyong Parka428d212018-08-29 22:26:30 +0900929}
930
931string AidlConstantDeclaration::Signature() const {
932 return type_->Signature() + " " + name_;
933}
934
Steven Moreland46e9da82018-07-27 15:45:29 -0700935AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
936 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jooyung Han8451a202021-01-16 03:07:06 +0900937 const Comments& comments)
Jiyong Parkb034bf02018-07-30 17:44:33 +0900938 : AidlMethod(location, oneway, type, name, args, comments, 0, true) {
939 has_id_ = false;
940}
941
942AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
943 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jooyung Han8451a202021-01-16 03:07:06 +0900944 const Comments& comments, int id, bool is_user_defined)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900945 : AidlMember(location, comments),
Steven Moreland46e9da82018-07-27 15:45:29 -0700946 oneway_(oneway),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700947 type_(type),
948 name_(name),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700949 arguments_(std::move(*args)),
Jiyong Parkb034bf02018-07-30 17:44:33 +0900950 id_(id),
951 is_user_defined_(is_user_defined) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700952 has_id_ = true;
953 delete args;
Christopher Wileyad339272015-10-05 19:11:58 -0700954 for (const unique_ptr<AidlArgument>& a : arguments_) {
955 if (a->IsIn()) { in_arguments_.push_back(a.get()); }
956 if (a->IsOut()) { out_arguments_.push_back(a.get()); }
957 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700958}
959
Jiyong Park02da7422018-07-16 16:00:26 +0900960string AidlMethod::Signature() const {
961 vector<string> arg_signatures;
962 for (const auto& arg : GetArguments()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900963 arg_signatures.emplace_back(arg->GetType().Signature());
Jiyong Park02da7422018-07-16 16:00:26 +0900964 }
Jiyong Park309668e2018-07-28 16:55:44 +0900965 return GetName() + "(" + Join(arg_signatures, ", ") + ")";
966}
967
968string AidlMethod::ToString() const {
969 vector<string> arg_strings;
970 for (const auto& arg : GetArguments()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900971 arg_strings.emplace_back(arg->ToString());
Jiyong Park309668e2018-07-28 16:55:44 +0900972 }
Jooyung Han965e31d2020-11-27 12:30:16 +0900973 string ret = (IsOneway() ? "oneway " : "") + GetType().ToString() + " " + GetName() + "(" +
Steven Moreland4ee68632018-12-14 15:52:46 -0800974 Join(arg_strings, ", ") + ")";
Jiyong Parked65bf42018-08-28 15:43:27 +0900975 if (HasId()) {
976 ret += " = " + std::to_string(GetId());
977 }
978 return ret;
Jiyong Park02da7422018-07-16 16:00:26 +0900979}
980
Steven Moreland46e9da82018-07-27 15:45:29 -0700981AidlDefinedType::AidlDefinedType(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +0900982 const Comments& comments, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900983 std::vector<std::unique_ptr<AidlMember>>* members)
Jooyung Han2aedb112021-09-29 09:37:59 +0900984 : AidlMember(location, comments), AidlScope(this), name_(name), package_(package) {
Jooyung Han93f48f02021-06-05 00:11:16 +0900985 // adjust name/package when name is fully qualified (for preprocessed files)
986 if (package_.empty() && name_.find('.') != std::string::npos) {
987 // Note that this logic is absolutely wrong. Given a parcelable
988 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
989 // the class is just Bar. However, this was the way it was done in the past.
990 //
991 // See b/17415692
992 auto pos = name.rfind('.');
993 // name is the last part.
994 name_ = name.substr(pos + 1);
995 // package is the initial parts (except the last).
996 package_ = name.substr(0, pos);
997 }
Jooyung Han829ec7c2020-12-02 12:07:36 +0900998 if (members) {
999 for (auto& m : *members) {
Jooyung Hanbaa71062021-09-29 09:06:03 +09001000 if (auto constant = AidlCast<AidlConstantDeclaration>(*m); constant) {
Jooyung Han829ec7c2020-12-02 12:07:36 +09001001 constants_.emplace_back(constant);
Jooyung Hanbaa71062021-09-29 09:06:03 +09001002 } else if (auto variable = AidlCast<AidlVariableDeclaration>(*m); variable) {
Jooyung Han829ec7c2020-12-02 12:07:36 +09001003 variables_.emplace_back(variable);
Jooyung Hanbaa71062021-09-29 09:06:03 +09001004 } else if (auto method = AidlCast<AidlMethod>(*m); method) {
Jooyung Han829ec7c2020-12-02 12:07:36 +09001005 methods_.emplace_back(method);
Jooyung Han2aedb112021-09-29 09:37:59 +09001006 } else if (auto type = AidlCast<AidlDefinedType>(*m); type) {
1007 type->SetEnclosingScope(this);
1008 types_.emplace_back(type);
Jooyung Han829ec7c2020-12-02 12:07:36 +09001009 } else {
Jooyung Hanbaa71062021-09-29 09:06:03 +09001010 AIDL_FATAL(*m) << "Unknown member type.";
Jooyung Han829ec7c2020-12-02 12:07:36 +09001011 }
1012 members_.push_back(m.release());
1013 }
1014 delete members;
1015 }
1016}
Steven Moreland787b0432018-07-03 09:00:58 -07001017
Jooyung Han808a2a02020-12-28 16:46:54 +09001018bool AidlDefinedType::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -08001019 if (!AidlAnnotatable::CheckValid(typenames)) {
1020 return false;
1021 }
Jooyung Han829ec7c2020-12-02 12:07:36 +09001022 if (!CheckValidWithMembers(typenames)) {
1023 return false;
1024 }
Devin Moore24f68572020-02-26 13:20:59 -08001025 return true;
1026}
1027
Steven Moreland787b0432018-07-03 09:00:58 -07001028std::string AidlDefinedType::GetCanonicalName() const {
1029 if (package_.empty()) {
1030 return GetName();
1031 }
Jooyung Han2aedb112021-09-29 09:37:59 +09001032 if (auto parent = GetParentType(); parent) {
1033 return parent->GetCanonicalName() + "." + GetName();
1034 }
Steven Moreland787b0432018-07-03 09:00:58 -07001035 return GetPackage() + "." + GetName();
1036}
1037
Jooyung Han829ec7c2020-12-02 12:07:36 +09001038bool AidlDefinedType::CheckValidWithMembers(const AidlTypenames& typenames) const {
1039 bool success = true;
1040
Jooyung Han2aedb112021-09-29 09:37:59 +09001041 for (const auto& t : GetNestedTypes()) {
1042 success = success && t->CheckValid(typenames);
1043 }
1044
Jooyung Han7fc5de02021-09-30 22:26:27 +09001045 if (auto parameterizable = AsParameterizable();
1046 parameterizable && parameterizable->IsGeneric() && !GetNestedTypes().empty()) {
1047 AIDL_ERROR(this) << "Generic types can't have nested types.";
1048 return false;
1049 }
1050
Jooyung Han2aedb112021-09-29 09:37:59 +09001051 std::set<std::string> nested_type_names;
1052 for (const auto& t : GetNestedTypes()) {
1053 bool duplicated = !nested_type_names.emplace(t->GetName()).second;
1054 if (duplicated) {
1055 AIDL_ERROR(t) << "Redefinition of '" << t->GetName() << "'.";
1056 success = false;
1057 }
1058 // nested type can't have a parent name
1059 if (t->GetName() == GetName()) {
1060 AIDL_ERROR(t) << "Nested type '" << GetName() << "' has the same name as its parent.";
1061 success = false;
1062 }
Jooyung Han2b1487d2021-09-30 09:57:01 +09001063 // Having unstructured parcelables as nested types doesn't make sense because they are defined
1064 // somewhere else in native languages (e.g. C++, Java...).
1065 if (AidlCast<AidlParcelable>(*t)) {
1066 AIDL_ERROR(t) << "'" << t->GetName()
1067 << "' is nested. Unstructured parcelables should be at the root scope.";
1068 return false;
1069 }
Jooyung Han2aedb112021-09-29 09:37:59 +09001070 // For now we don't allow "interface" to be nested
1071 if (AidlCast<AidlInterface>(*t)) {
1072 AIDL_ERROR(t) << "'" << t->GetName()
1073 << "' is nested. Interfaces should be at the root scope.";
1074 return false;
1075 }
1076 }
1077
Jooyung Han8e9ae872021-10-13 02:52:25 +09001078 if (!TopologicalVisit(GetNestedTypes(), [](auto&) {})) {
1079 AIDL_ERROR(this) << GetName()
1080 << " has nested types with cyclic references. C++ and NDK backends don't "
1081 "support cyclic references.";
1082 return false;
1083 }
1084
Jooyung Han829ec7c2020-12-02 12:07:36 +09001085 for (const auto& v : GetFields()) {
1086 const bool field_valid = v->CheckValid(typenames);
1087 success = success && field_valid;
1088 }
1089
1090 // field names should be unique
1091 std::set<std::string> fieldnames;
1092 for (const auto& v : GetFields()) {
1093 bool duplicated = !fieldnames.emplace(v->GetName()).second;
1094 if (duplicated) {
1095 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName() << "'";
1096 success = false;
1097 }
1098 }
1099
1100 // immutable parcelables should have immutable fields.
1101 if (IsJavaOnlyImmutable()) {
1102 for (const auto& v : GetFields()) {
1103 if (!typenames.CanBeJavaOnlyImmutable(v->GetType())) {
1104 AIDL_ERROR(v) << "The @JavaOnlyImmutable '" << GetName() << "' has a "
1105 << "non-immutable field named '" << v->GetName() << "'.";
1106 success = false;
1107 }
1108 }
1109 }
1110
1111 set<string> constant_names;
1112 for (const auto& constant : GetConstantDeclarations()) {
1113 if (constant_names.count(constant->GetName()) > 0) {
1114 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
1115 success = false;
1116 }
1117 constant_names.insert(constant->GetName());
1118 success = success && constant->CheckValid(typenames);
1119 }
1120
1121 return success;
1122}
1123
1124bool AidlDefinedType::CheckValidForGetterNames() const {
1125 bool success = true;
1126 std::set<std::string> getters;
1127 for (const auto& v : GetFields()) {
1128 bool duplicated = !getters.emplace(v->GetCapitalizedName()).second;
1129 if (duplicated) {
1130 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName()
1131 << "' after capitalizing the first letter";
1132 success = false;
1133 }
1134 }
1135 return success;
1136}
1137
Jooyung Han2aedb112021-09-29 09:37:59 +09001138const AidlDefinedType* AidlDefinedType::GetParentType() const {
1139 AIDL_FATAL_IF(GetEnclosingScope() == nullptr, this) << "Scope is not set.";
1140 return AidlCast<AidlDefinedType>(GetEnclosingScope()->GetNode());
1141}
1142
Jooyung Hanf8c39632021-10-05 09:56:29 +09001143const AidlDefinedType* AidlDefinedType::GetRootType() const {
1144 const AidlDefinedType* root = this;
1145 for (auto parent = root->GetParentType(); parent; parent = parent->GetParentType()) {
1146 root = parent;
1147 }
1148 return root;
1149}
1150
Jooyung Han2aedb112021-09-29 09:37:59 +09001151// Resolve `name` in the current scope. If not found, delegate to the parent
Jooyung Han13f1fa52021-06-11 18:06:12 +09001152std::string AidlDefinedType::ResolveName(const std::string& name) const {
Jooyung Han2aedb112021-09-29 09:37:59 +09001153 // For example, in the following, t1's type Baz means x.Foo.Bar.Baz
1154 // while t2's type is y.Baz.
Jooyung Han13f1fa52021-06-11 18:06:12 +09001155 // package x;
Jooyung Han2aedb112021-09-29 09:37:59 +09001156 // import y.Baz;
Jooyung Han13f1fa52021-06-11 18:06:12 +09001157 // parcelable Foo {
1158 // parcelable Bar {
Jooyung Han2aedb112021-09-29 09:37:59 +09001159 // enum Baz { ... }
1160 // Baz t1; // -> should be x.Foo.Bar.Baz
Jooyung Han13f1fa52021-06-11 18:06:12 +09001161 // }
Jooyung Han2aedb112021-09-29 09:37:59 +09001162 // Baz t2; // -> should be y.Baz
1163 // Bar.Baz t3; // -> should be x.Foo.Bar.Baz
Jooyung Han13f1fa52021-06-11 18:06:12 +09001164 // }
1165 AIDL_FATAL_IF(!GetEnclosingScope(), this)
1166 << "Type should have an enclosing scope.(e.g. AidlDocument)";
Jooyung Han2aedb112021-09-29 09:37:59 +09001167 if (AidlTypenames::IsBuiltinTypename(name)) {
1168 return name;
1169 }
1170
1171 const auto first_dot = name.find_first_of('.');
1172 // For "Outer.Inner", we look up "Outer" in the import list.
1173 const std::string class_name =
1174 (first_dot == std::string::npos) ? name : name.substr(0, first_dot);
1175 // Keep ".Inner", to make a fully-qualified name
1176 const std::string nested_type = (first_dot == std::string::npos) ? "" : name.substr(first_dot);
1177
1178 // check if it is a nested type
1179 for (const auto& type : GetNestedTypes()) {
1180 if (type->GetName() == class_name) {
1181 return type->GetCanonicalName() + nested_type;
1182 }
1183 }
1184
Jooyung Han13f1fa52021-06-11 18:06:12 +09001185 return GetEnclosingScope()->ResolveName(name);
1186}
1187
Jooyung Hanbaa71062021-09-29 09:06:03 +09001188template <>
1189const AidlDefinedType* AidlCast<AidlDefinedType>(const AidlNode& node) {
1190 struct Visitor : AidlVisitor {
1191 const AidlDefinedType* defined_type = nullptr;
1192 void Visit(const AidlInterface& t) override { defined_type = &t; }
1193 void Visit(const AidlEnumDeclaration& t) override { defined_type = &t; }
1194 void Visit(const AidlStructuredParcelable& t) override { defined_type = &t; }
1195 void Visit(const AidlUnionDecl& t) override { defined_type = &t; }
1196 void Visit(const AidlParcelable& t) override { defined_type = &t; }
1197 } v;
1198 node.DispatchVisit(v);
1199 return v.defined_type;
Jooyung Han35784982021-06-29 06:26:12 +09001200}
1201
1202const AidlDocument& AidlDefinedType::GetDocument() const {
Jooyung Hanf8c39632021-10-05 09:56:29 +09001203 const AidlDefinedType* root = GetRootType();
1204 auto scope = root->GetEnclosingScope();
Jooyung Han35784982021-06-29 06:26:12 +09001205 AIDL_FATAL_IF(!scope, this) << "no scope defined.";
1206 auto doc = AidlCast<AidlDocument>(scope->GetNode());
Jooyung Hanf8c39632021-10-05 09:56:29 +09001207 AIDL_FATAL_IF(!doc, this) << "root scope is not a document.";
Jooyung Han35784982021-06-29 06:26:12 +09001208 return *doc;
1209}
1210
Jiyong Park18132182020-06-08 20:24:40 +09001211AidlParcelable::AidlParcelable(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001212 const std::string& package, const Comments& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001213 const std::string& cpp_header, std::vector<std::string>* type_params,
1214 std::vector<std::unique_ptr<AidlMember>>* members)
1215 : AidlDefinedType(location, name, comments, package, members),
Jeongik Chadf76dc72019-11-28 00:08:47 +09001216 AidlParameterizable<std::string>(type_params),
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -08001217 cpp_header_(cpp_header) {
1218 // Strip off quotation marks if we actually have a cpp header.
1219 if (cpp_header_.length() >= 2) {
1220 cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2);
1221 }
Casey Dahlin59401da2015-10-09 18:16:45 -07001222}
Jeongik Chadf76dc72019-11-28 00:08:47 +09001223
1224template <typename T>
1225bool AidlParameterizable<T>::CheckValid() const {
1226 return true;
1227};
1228
1229template <>
1230bool AidlParameterizable<std::string>::CheckValid() const {
1231 if (!IsGeneric()) {
1232 return true;
1233 }
1234 std::unordered_set<std::string> set(GetTypeParameters().begin(), GetTypeParameters().end());
1235 if (set.size() != GetTypeParameters().size()) {
1236 AIDL_ERROR(this->AsAidlNode()) << "Every type parameter should be unique.";
1237 return false;
1238 }
1239 return true;
1240}
Casey Dahlin59401da2015-10-09 18:16:45 -07001241
Jooyung Han808a2a02020-12-28 16:46:54 +09001242bool AidlParcelable::CheckValid(const AidlTypenames& typenames) const {
1243 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001244 return false;
1245 }
Jeongik Chadf76dc72019-11-28 00:08:47 +09001246 if (!AidlParameterizable<std::string>::CheckValid()) {
1247 return false;
1248 }
Jeongik Cha82317dd2019-02-27 20:26:11 +09001249
1250 return true;
1251}
1252
Steven Moreland5557f1c2018-07-02 13:50:23 -07001253AidlStructuredParcelable::AidlStructuredParcelable(
Jiyong Park18132182020-06-08 20:24:40 +09001254 const AidlLocation& location, const std::string& name, const std::string& package,
Jooyung Han8451a202021-01-16 03:07:06 +09001255 const Comments& comments, std::vector<std::string>* type_params,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001256 std::vector<std::unique_ptr<AidlMember>>* members)
1257 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Steven Moreland5557f1c2018-07-02 13:50:23 -07001258
Jooyung Han808a2a02020-12-28 16:46:54 +09001259bool AidlStructuredParcelable::CheckValid(const AidlTypenames& typenames) const {
1260 if (!AidlParcelable::CheckValid(typenames)) {
Devin Moore24f68572020-02-26 13:20:59 -08001261 return false;
1262 }
Jeongik Cha13066da2020-08-06 15:43:19 +09001263
Jooyung Han59af9cc2020-10-25 21:44:14 +09001264 bool success = true;
Jeongik Cha36f76c32020-07-28 00:25:52 +09001265
Jooyung Hand4057c42020-10-23 13:28:22 +09001266 if (IsFixedSize()) {
1267 for (const auto& v : GetFields()) {
1268 if (!typenames.CanBeFixedSize(v->GetType())) {
1269 AIDL_ERROR(v) << "The @FixedSize parcelable '" << this->GetName() << "' has a "
1270 << "non-fixed size field named " << v->GetName() << ".";
1271 success = false;
1272 }
1273 }
1274 }
1275
1276 if (IsJavaOnlyImmutable()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001277 // Immutable parcelables provide getters
Jooyung Han829ec7c2020-12-02 12:07:36 +09001278 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001279 success = false;
Jooyung Hand4057c42020-10-23 13:28:22 +09001280 }
1281 }
1282
Daniel Norman85aed542019-08-21 12:01:14 -07001283 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001284}
1285
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001286// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -07001287bool AidlTypeSpecifier::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1288 Options::Language lang) const {
Jooyung Hand09a21d2021-02-15 18:56:55 +09001289 if (IsGeneric()) {
1290 const auto& types = GetTypeParameters();
1291 for (const auto& arg : types) {
1292 if (!arg->LanguageSpecificCheckValid(typenames, lang)) {
1293 return false;
1294 }
1295 }
1296 }
1297
Andrei Homescub62afd92020-05-11 19:24:59 -07001298 if ((lang == Options::Language::NDK || lang == Options::Language::RUST) && IsArray() &&
Andrei Homescub62afd92020-05-11 19:24:59 -07001299 IsNullable()) {
Steven Moreland0185d9b2020-05-15 23:21:22 +00001300 if (GetName() == "ParcelFileDescriptor") {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001301 AIDL_ERROR(this) << "The " << to_string(lang)
Andrei Homescub62afd92020-05-11 19:24:59 -07001302 << " backend does not support nullable array of ParcelFileDescriptor";
Steven Moreland0185d9b2020-05-15 23:21:22 +00001303 return false;
1304 }
1305
Steven Morelandd59e3172020-05-11 16:42:09 -07001306 const auto defined_type = typenames.TryGetDefinedType(GetName());
1307 if (defined_type != nullptr && defined_type->AsParcelable() != nullptr) {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001308 AIDL_ERROR(this) << "The " << to_string(lang)
Andrei Homescub62afd92020-05-11 19:24:59 -07001309 << " backend does not support nullable array of parcelable";
Steven Morelandd59e3172020-05-11 16:42:09 -07001310 return false;
1311 }
1312 }
Andrei Homescub62afd92020-05-11 19:24:59 -07001313 if (this->GetName() == "FileDescriptor" &&
1314 (lang == Options::Language::NDK || lang == Options::Language::RUST)) {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001315 AIDL_ERROR(this) << "FileDescriptor isn't supported by the " << to_string(lang) << " backend.";
Steven Morelandc8a4ca82020-01-21 17:50:08 -08001316 return false;
1317 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001318 if (this->IsGeneric()) {
1319 if (this->GetName() == "List") {
Jooyung Han55f96ad2020-12-13 10:08:33 +09001320 if (lang == Options::Language::NDK) {
1321 const AidlTypeSpecifier& contained_type = *GetTypeParameters()[0];
1322 const string& contained_type_name = contained_type.GetName();
Jooyung Hane87cdd02020-12-11 16:47:35 +09001323 if (typenames.GetInterface(contained_type)) {
1324 AIDL_ERROR(this) << "List<" << contained_type_name
1325 << "> is not supported. List in NDK doesn't support interface.";
1326 return false;
1327 }
1328 if (contained_type_name == "IBinder") {
1329 AIDL_ERROR(this) << "List<" << contained_type_name
1330 << "> is not supported. List in NDK doesn't support IBinder.";
1331 return false;
Jeongik Cha08ca2182019-11-21 14:01:13 +09001332 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001333 }
Jeongik Chabb55b5e2020-01-07 23:11:26 +09001334 }
1335 }
Devin Moore6a01ca12020-08-28 10:24:19 -07001336
1337 if (this->IsArray()) {
1338 if (this->GetName() == "List" || this->GetName() == "Map" ||
1339 this->GetName() == "CharSequence") {
1340 AIDL_ERROR(this) << this->GetName() << "[] is not supported.";
Jeongik Chabb55b5e2020-01-07 23:11:26 +09001341 return false;
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001342 }
1343 }
Devin Moore6a01ca12020-08-28 10:24:19 -07001344
1345 if (lang != Options::Language::JAVA) {
1346 if (this->GetName() == "List" && !this->IsGeneric()) {
1347 AIDL_ERROR(this) << "Currently, only the Java backend supports non-generic List.";
1348 return false;
1349 }
1350 if (this->GetName() == "Map" || this->GetName() == "CharSequence") {
1351 AIDL_ERROR(this) << "Currently, only Java backend supports " << this->GetName() << ".";
1352 return false;
Jeongik Cha08ca2182019-11-21 14:01:13 +09001353 }
1354 }
1355
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001356 return true;
1357}
1358
1359// TODO: we should treat every backend all the same in future.
Jooyung Han589cfb02021-09-28 17:26:04 +09001360bool AidlDefinedType::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1361 Options::Language lang) const {
1362 struct Visitor : AidlVisitor {
1363 Visitor(const AidlTypenames& typenames, Options::Language lang)
1364 : typenames(typenames), lang(lang) {}
1365 void Visit(const AidlTypeSpecifier& type) override {
1366 success = success && type.LanguageSpecificCheckValid(typenames, lang);
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001367 }
Jooyung Han589cfb02021-09-28 17:26:04 +09001368 const AidlTypenames& typenames;
1369 Options::Language lang;
1370 bool success = true;
1371 } v(typenames, lang);
1372 VisitTopDown(v, *this);
1373 return v.success;
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001374}
1375
Daniel Norman85aed542019-08-21 12:01:14 -07001376AidlEnumerator::AidlEnumerator(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001377 AidlConstantValue* value, const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +09001378 : AidlCommentable(location, comments),
Jooyung Han29813842020-12-08 01:28:03 +09001379 name_(name),
1380 value_(value),
Jooyung Han29813842020-12-08 01:28:03 +09001381 value_user_specified_(value != nullptr) {}
Daniel Norman85aed542019-08-21 12:01:14 -07001382
1383bool AidlEnumerator::CheckValid(const AidlTypeSpecifier& enum_backing_type) const {
1384 if (GetValue() == nullptr) {
1385 return false;
1386 }
1387 if (!GetValue()->CheckValid()) {
1388 return false;
1389 }
Will McVickerd7d18df2019-09-12 13:40:50 -07001390 if (GetValue()->ValueString(enum_backing_type, AidlConstantValueDecorator).empty()) {
Daniel Norman85aed542019-08-21 12:01:14 -07001391 AIDL_ERROR(this) << "Enumerator type differs from enum backing type.";
1392 return false;
1393 }
1394 return true;
1395}
1396
1397string AidlEnumerator::ValueString(const AidlTypeSpecifier& backing_type,
1398 const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -07001399 return GetValue()->ValueString(backing_type, decorator);
Daniel Norman85aed542019-08-21 12:01:14 -07001400}
1401
1402AidlEnumDeclaration::AidlEnumDeclaration(const AidlLocation& location, const std::string& name,
1403 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jooyung Han8451a202021-01-16 03:07:06 +09001404 const std::string& package, const Comments& comments)
Jooyung Han829ec7c2020-12-02 12:07:36 +09001405 : AidlDefinedType(location, name, comments, package, nullptr),
Jooyung Han29813842020-12-08 01:28:03 +09001406 enumerators_(std::move(*enumerators)) {
Jooyung Han672557b2020-12-24 05:18:00 +09001407 // Fill missing enumerator values with <prev + 1>
1408 // This can't be done in Autofill() because type/ref resolution depends on this.
1409 // For example, with enum E { A, B = A }, B's value 'A' is a reference which can't be
1410 // resolved if A has no value set.
Daniel Normanb28684e2019-10-17 15:31:39 -07001411 const AidlEnumerator* previous = nullptr;
1412 for (const auto& enumerator : enumerators_) {
1413 if (enumerator->GetValue() == nullptr) {
Jooyung Han29813842020-12-08 01:28:03 +09001414 auto loc = enumerator->GetLocation();
Daniel Normanb28684e2019-10-17 15:31:39 -07001415 if (previous == nullptr) {
Devin Mooredf93ebb2020-03-25 14:03:35 -07001416 enumerator->SetValue(
Jooyung Han29813842020-12-08 01:28:03 +09001417 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "0")));
Daniel Normanb28684e2019-10-17 15:31:39 -07001418 } else {
Jooyung Hand0c8af02021-01-06 18:08:01 +09001419 auto prev_value = std::make_unique<AidlConstantReference>(loc, previous->GetName());
Daniel Normanb28684e2019-10-17 15:31:39 -07001420 enumerator->SetValue(std::make_unique<AidlBinaryConstExpression>(
Jooyung Han29813842020-12-08 01:28:03 +09001421 loc, std::move(prev_value), "+",
1422 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "1"))));
Daniel Normanb28684e2019-10-17 15:31:39 -07001423 }
1424 }
1425 previous = enumerator.get();
1426 }
1427}
1428
Jooyung Han672557b2020-12-24 05:18:00 +09001429bool AidlEnumDeclaration::Autofill(const AidlTypenames& typenames) {
1430 if (auto annot = BackingType(); annot != nullptr) {
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001431 // Autofill() is called before the grand CheckValid(). But AidlAnnotation::ParamValue()
1432 // calls AidlConstantValue::evaluate() which requires CheckValid() to be called before. So we
Jooyung Han672557b2020-12-24 05:18:00 +09001433 // need to call CheckValid().
1434 if (!annot->CheckValid()) {
1435 return false;
1436 }
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001437 auto type = annot->ParamValue<std::string>("type").value();
Jooyung Hanaccd9192021-10-14 15:57:28 +09001438 backing_type_ = typenames.MakeResolvedType(annot->GetLocation(), type, false);
Jooyung Han672557b2020-12-24 05:18:00 +09001439 } else {
1440 // Default to byte type for enums.
Jooyung Hanaccd9192021-10-14 15:57:28 +09001441 backing_type_ = typenames.MakeResolvedType(GetLocation(), "byte", false);
Jooyung Han672557b2020-12-24 05:18:00 +09001442 }
Steven Morelandb248d072021-09-29 19:07:17 -07001443
1444 // we only support/test a few backing types, so make sure this is a supported
1445 // one (otherwise boolean might work, which isn't supported/tested in all
1446 // backends)
1447 static std::set<string> kBackingTypes = {"byte", "int", "long"};
1448 if (kBackingTypes.find(backing_type_->GetName()) == kBackingTypes.end()) {
1449 AIDL_ERROR(this) << "Invalid backing type: " << backing_type_->GetName()
1450 << ". Backing type must be one of: " << Join(kBackingTypes, ", ");
1451 return false;
Jooyung Han672557b2020-12-24 05:18:00 +09001452 }
1453 return true;
1454}
1455
Jooyung Han808a2a02020-12-28 16:46:54 +09001456bool AidlEnumDeclaration::CheckValid(const AidlTypenames& typenames) const {
1457 if (!AidlDefinedType::CheckValid(typenames)) {
Devin Moore24f68572020-02-26 13:20:59 -08001458 return false;
1459 }
Jooyung Han829ec7c2020-12-02 12:07:36 +09001460 if (!GetMembers().empty()) {
1461 AIDL_ERROR(this) << "Enum doesn't support fields/constants/methods.";
1462 return false;
1463 }
Daniel Norman85aed542019-08-21 12:01:14 -07001464 if (backing_type_ == nullptr) {
1465 AIDL_ERROR(this) << "Enum declaration missing backing type.";
1466 return false;
1467 }
1468 bool success = true;
1469 for (const auto& enumerator : enumerators_) {
1470 success = success && enumerator->CheckValid(GetBackingType());
1471 }
Jooyung Han3b990182020-12-22 17:44:31 +09001472
Jooyung Han808a2a02020-12-28 16:46:54 +09001473 return success;
Daniel Norman85aed542019-08-21 12:01:14 -07001474}
1475
Jooyung Han2946afc2020-10-05 20:29:16 +09001476AidlUnionDecl::AidlUnionDecl(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001477 const std::string& package, const Comments& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001478 std::vector<std::string>* type_params,
1479 std::vector<std::unique_ptr<AidlMember>>* members)
1480 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Jooyung Han2946afc2020-10-05 20:29:16 +09001481
Jooyung Han808a2a02020-12-28 16:46:54 +09001482bool AidlUnionDecl::CheckValid(const AidlTypenames& typenames) const {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001483 // visit parents
Jooyung Han808a2a02020-12-28 16:46:54 +09001484 if (!AidlParcelable::CheckValid(typenames)) {
Jooyung Hanfe89f122020-10-14 03:49:18 +09001485 return false;
1486 }
Jooyung Han59af9cc2020-10-25 21:44:14 +09001487
1488 // unions provide getters always
Jooyung Han829ec7c2020-12-02 12:07:36 +09001489 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001490 return false;
Jooyung Hanfe89f122020-10-14 03:49:18 +09001491 }
1492
1493 // now, visit self!
1494 bool success = true;
1495
1496 // TODO(b/170807936) do we need to allow ParcelableHolder in union?
1497 for (const auto& v : GetFields()) {
1498 if (v->GetType().GetName() == "ParcelableHolder") {
1499 AIDL_ERROR(*v) << "A union can't have a member of ParcelableHolder '" << v->GetName() << "'";
1500 success = false;
1501 }
1502 }
1503
Jooyung Hanfe89f122020-10-14 03:49:18 +09001504 if (GetFields().empty()) {
1505 AIDL_ERROR(*this) << "The union '" << this->GetName() << "' has no fields.";
1506 return false;
1507 }
1508
Jooyung Han53fb4242020-12-17 16:03:49 +09001509 // first member should have useful default value (implicit or explicit)
1510 const auto& first = GetFields()[0];
1511 if (!first->HasUsefulDefaultValue()) {
1512 // Most types can be initialized without a default value. For example,
1513 // interface types are inherently nullable. But, enum types should have
1514 // an explicit default value.
1515 if (!first->GetType().IsArray() && typenames.GetEnumDeclaration(first->GetType())) {
1516 AIDL_ERROR(first)
1517 << "The union's first member should have a useful default value. Enum types can be "
1518 "initialized with a reference. (e.g. ... = MyEnum.FOO;)";
1519 return false;
1520 }
1521 // In Java, array types are initialized as null without a default value. To be sure that default
1522 // initialized unions are accepted by other backends we require arrays also have a default
1523 // value.
1524 if (first->GetType().IsArray()) {
1525 AIDL_ERROR(first)
1526 << "The union's first member should have a useful default value. Arrays can be "
1527 "initialized with values(e.g. ... = { values... };) or marked as @nullable.";
1528 return false;
1529 }
1530 }
1531
Jooyung Hanfe89f122020-10-14 03:49:18 +09001532 return success;
1533}
1534
Steven Moreland46e9da82018-07-27 15:45:29 -07001535AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001536 const Comments& comments, bool oneway, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001537 std::vector<std::unique_ptr<AidlMember>>* members)
1538 : AidlDefinedType(location, name, comments, package, members) {
1539 for (auto& m : GetMethods()) {
1540 m.get()->ApplyInterfaceOneway(oneway);
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001541 }
Casey Dahlinfb7da2e2015-10-08 17:26:09 -07001542}
1543
Jooyung Han808a2a02020-12-28 16:46:54 +09001544bool AidlInterface::CheckValid(const AidlTypenames& typenames) const {
1545 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001546 return false;
1547 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001548 // Has to be a pointer due to deleting copy constructor. No idea why.
1549 map<string, const AidlMethod*> method_names;
1550 for (const auto& m : GetMethods()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001551 if (!m->GetType().CheckValid(typenames)) {
1552 return false;
1553 }
1554
Jeongik Cha649e8a72020-03-27 17:47:40 +09001555 // TODO(b/156872582): Support it when ParcelableHolder supports every backend.
1556 if (m->GetType().GetName() == "ParcelableHolder") {
1557 AIDL_ERROR(m) << "ParcelableHolder cannot be a return type";
1558 return false;
1559 }
Steven Morelandacd53472018-12-14 10:17:26 -08001560 if (m->IsOneway() && m->GetType().GetName() != "void") {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001561 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot return a value";
1562 return false;
1563 }
1564
1565 set<string> argument_names;
1566 for (const auto& arg : m->GetArguments()) {
1567 auto it = argument_names.find(arg->GetName());
1568 if (it != argument_names.end()) {
1569 AIDL_ERROR(m) << "method '" << m->GetName() << "' has duplicate argument name '"
1570 << arg->GetName() << "'";
1571 return false;
1572 }
1573 argument_names.insert(arg->GetName());
1574
Jooyung Han020d8d12021-02-26 17:23:02 +09001575 if (!arg->CheckValid(typenames)) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001576 return false;
1577 }
1578
Steven Morelandacd53472018-12-14 10:17:26 -08001579 if (m->IsOneway() && arg->IsOut()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001580 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot have out parameters";
1581 return false;
1582 }
Jooyung Han15fd6c62020-10-23 13:54:46 +09001583
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001584 // check that the name doesn't match a keyword
Jeongik Cha997281d2020-01-16 15:23:59 +09001585 if (IsJavaKeyword(arg->GetName().c_str())) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001586 AIDL_ERROR(arg) << "Argument name is a Java or aidl keyword";
1587 return false;
1588 }
1589
1590 // Reserve a namespace for internal use
1591 if (android::base::StartsWith(arg->GetName(), "_aidl")) {
1592 AIDL_ERROR(arg) << "Argument name cannot begin with '_aidl'";
1593 return false;
1594 }
Jooyung Hanfa181932021-06-12 07:56:53 +09001595
1596 if (arg->GetType().GetName() == "void") {
1597 AIDL_ERROR(arg->GetType())
1598 << "'void' is an invalid type for the parameter '" << arg->GetName() << "'";
1599 return false;
1600 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001601 }
1602
1603 auto it = method_names.find(m->GetName());
1604 // prevent duplicate methods
1605 if (it == method_names.end()) {
1606 method_names[m->GetName()] = m.get();
1607 } else {
1608 AIDL_ERROR(m) << "attempt to redefine method " << m->GetName() << ":";
1609 AIDL_ERROR(it->second) << "previously defined here.";
1610 return false;
1611 }
1612
Paul Trautrimb77048c2020-01-21 16:39:32 +09001613 static set<string> reserved_methods{"asBinder()", "getInterfaceHash()", "getInterfaceVersion()",
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001614 "getTransactionName(int)"};
1615
1616 if (reserved_methods.find(m->Signature()) != reserved_methods.end()) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001617 AIDL_ERROR(m) << " method " << m->Signature() << " is reserved for internal use.";
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001618 return false;
1619 }
1620 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001621
1622 bool success = true;
1623 set<string> constant_names;
Jooyung Han3f347ca2020-12-01 12:41:50 +09001624 for (const auto& constant : GetConstantDeclarations()) {
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001625 if (constant_names.count(constant->GetName()) > 0) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001626 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001627 success = false;
1628 }
1629 constant_names.insert(constant->GetName());
1630 success = success && constant->CheckValid(typenames);
1631 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001632 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001633}
1634
Jiyong Park27fd7fd2020-08-27 16:25:09 +09001635std::string AidlInterface::GetDescriptor() const {
1636 std::string annotatedDescriptor = AidlAnnotatable::GetDescriptor();
1637 if (annotatedDescriptor != "") {
1638 return annotatedDescriptor;
1639 }
1640 return GetCanonicalName();
1641}
1642
Jooyung Han132cf802021-01-15 02:17:32 +09001643AidlImport::AidlImport(const AidlLocation& location, const std::string& needed_class,
Jooyung Han8451a202021-01-16 03:07:06 +09001644 const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +09001645 : AidlNode(location, comments), needed_class_(needed_class) {}
Jooyung Han29813842020-12-08 01:28:03 +09001646
Jooyung Han13f1fa52021-06-11 18:06:12 +09001647AidlDocument::AidlDocument(const AidlLocation& location, const Comments& comments,
1648 std::vector<std::unique_ptr<AidlImport>> imports,
Jooyung Han35784982021-06-29 06:26:12 +09001649 std::vector<std::unique_ptr<AidlDefinedType>> defined_types,
1650 bool is_preprocessed)
Jooyung Han13f1fa52021-06-11 18:06:12 +09001651 : AidlCommentable(location, comments),
Jooyung Han35784982021-06-29 06:26:12 +09001652 AidlScope(this),
Jooyung Han13f1fa52021-06-11 18:06:12 +09001653 imports_(std::move(imports)),
Jooyung Han35784982021-06-29 06:26:12 +09001654 defined_types_(std::move(defined_types)),
1655 is_preprocessed_(is_preprocessed) {
Jooyung Han13f1fa52021-06-11 18:06:12 +09001656 for (const auto& t : defined_types_) {
1657 t->SetEnclosingScope(this);
1658 }
1659}
1660
1661// Resolves type name in the current document.
1662// - built-in types
1663// - imported types
1664// - top-level type
1665std::string AidlDocument::ResolveName(const std::string& name) const {
1666 if (AidlTypenames::IsBuiltinTypename(name)) {
1667 return name;
1668 }
1669
1670 const auto first_dot = name.find_first_of('.');
1671 // For "Outer.Inner", we look up "Outer" in the import list.
Jooyung Han29813842020-12-08 01:28:03 +09001672 const std::string class_name =
Jooyung Han13f1fa52021-06-11 18:06:12 +09001673 (first_dot == std::string::npos) ? name : name.substr(0, first_dot);
1674 // Keep ".Inner", to make a fully-qualified name
1675 const std::string nested_type = (first_dot == std::string::npos) ? "" : name.substr(first_dot);
1676
Jooyung Han29813842020-12-08 01:28:03 +09001677 for (const auto& import : Imports()) {
Jooyung Han13f1fa52021-06-11 18:06:12 +09001678 if (import->SimpleName() == class_name) {
1679 return import->GetNeededClass() + nested_type;
Jooyung Han29813842020-12-08 01:28:03 +09001680 }
1681 }
Jooyung Han13f1fa52021-06-11 18:06:12 +09001682
1683 // check if it is a top-level type.
1684 for (const auto& type : DefinedTypes()) {
1685 if (type->GetName() == class_name) {
1686 return type->GetCanonicalName() + nested_type;
1687 }
Jooyung Han29813842020-12-08 01:28:03 +09001688 }
Jooyung Han13f1fa52021-06-11 18:06:12 +09001689
1690 // name itself might be fully-qualified name.
1691 return name;
Steven Moreland26318532020-12-23 20:08:36 +00001692}