blob: 4040217b90df021b970b91b7b1c849e0df41d907 [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 }
Jooyung Han49b8f362021-10-15 10:58:02 +0900687 if (GetName() == "ParcelableHolder" || GetName() == "List" || GetName() == "Map" ||
688 GetName() == "CharSequence") {
689 AIDL_ERROR(this) << "Arrays of " << GetName() << " are not supported.";
Steven Moreland8042d2d2020-09-30 23:31:32 +0000690 return false;
691 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900692 }
693
694 if (IsNullable()) {
695 if (AidlTypenames::IsPrimitiveTypename(GetName()) && !IsArray()) {
696 AIDL_ERROR(this) << "Primitive type cannot get nullable annotation";
697 return false;
698 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800699 const auto defined_type = typenames.TryGetDefinedType(GetName());
700 if (defined_type != nullptr && defined_type->AsEnumDeclaration() != nullptr && !IsArray()) {
Daniel Normanee8674f2019-09-20 16:07:00 -0700701 AIDL_ERROR(this) << "Enum type cannot get nullable annotation";
702 return false;
703 }
Jeongik Chaf6ec8982020-10-15 00:10:30 +0900704 if (GetName() == "ParcelableHolder") {
705 AIDL_ERROR(this) << "ParcelableHolder cannot be nullable.";
706 return false;
707 }
Jooyung Han01720ed2021-08-13 07:46:07 +0900708 if (IsHeapNullable()) {
709 if (!defined_type || IsArray() || !defined_type->AsParcelable()) {
710 AIDL_ERROR(this) << "@nullable(heap=true) is available to parcelables.";
711 return false;
712 }
713 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900714 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900715 return true;
716}
717
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900718std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type,
Steven Moreland860b1942018-08-16 14:59:28 -0700719 const std::string& raw_value) {
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900720 if (type.IsArray()) {
721 return raw_value;
722 }
723
724 if (auto defined_type = type.GetDefinedType(); defined_type) {
725 auto enum_type = defined_type->AsEnumDeclaration();
726 AIDL_FATAL_IF(!enum_type, type) << "Invalid type for \"" << raw_value << "\"";
727 return type.GetName() + "." + raw_value.substr(raw_value.find_last_of('.') + 1);
728 }
Steven Moreland860b1942018-08-16 14:59:28 -0700729 return raw_value;
730}
731
Steven Moreland46e9da82018-07-27 15:45:29 -0700732AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
733 AidlTypeSpecifier* type, const std::string& name)
Steven Moreland541788d2020-05-21 22:05:52 +0000734 : AidlVariableDeclaration(location, type, name, AidlConstantValue::Default(*type)) {
735 default_user_specified_ = false;
736}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700737
Steven Moreland46e9da82018-07-27 15:45:29 -0700738AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
739 AidlTypeSpecifier* type, const std::string& name,
740 AidlConstantValue* default_value)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900741 : AidlMember(location, type->GetComments()),
Steven Moreland541788d2020-05-21 22:05:52 +0000742 type_(type),
743 name_(name),
744 default_user_specified_(true),
745 default_value_(default_value) {}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700746
Jooyung Han53fb4242020-12-17 16:03:49 +0900747bool AidlVariableDeclaration::HasUsefulDefaultValue() const {
748 if (GetDefaultValue()) {
749 return true;
750 }
751 // null is accepted as a valid default value in all backends
752 if (GetType().IsNullable()) {
753 return true;
754 }
755 return false;
756}
757
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900758bool AidlVariableDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700759 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900760 valid &= type_->CheckValid(typenames);
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900761
Steven Moreland54be7bd2019-12-05 11:17:53 -0800762 if (type_->GetName() == "void") {
763 AIDL_ERROR(this) << "Declaration " << name_
764 << " is void, but declarations cannot be of void type.";
765 valid = false;
766 }
767
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900768 if (default_value_ == nullptr) return valid;
Steven Moreland25294322018-08-07 18:13:55 -0700769 valid &= default_value_->CheckValid();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700770
Steven Moreland25294322018-08-07 18:13:55 -0700771 if (!valid) return false;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700772
Steven Moreland860b1942018-08-16 14:59:28 -0700773 return !ValueString(AidlConstantValueDecorator).empty();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700774}
Steven Moreland5557f1c2018-07-02 13:50:23 -0700775
Jooyung Hanacae85d2020-10-28 16:39:09 +0900776string AidlVariableDeclaration::GetCapitalizedName() const {
777 AIDL_FATAL_IF(name_.size() <= 0, *this) << "Name can't be empty.";
778 string str = name_;
779 str[0] = static_cast<char>(toupper(str[0]));
780 return str;
781}
782
Steven Moreland5557f1c2018-07-02 13:50:23 -0700783string AidlVariableDeclaration::ToString() const {
Jooyung Han965e31d2020-11-27 12:30:16 +0900784 string ret = type_->ToString() + " " + name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000785 if (default_value_ != nullptr && default_user_specified_) {
Steven Moreland860b1942018-08-16 14:59:28 -0700786 ret += " = " + ValueString(AidlConstantValueDecorator);
Steven Moreland9ea10e32018-07-19 15:26:09 -0700787 }
788 return ret;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700789}
790
Jiyong Park02da7422018-07-16 16:00:26 +0900791string AidlVariableDeclaration::Signature() const {
792 return type_->Signature() + " " + name_;
793}
794
Steven Moreland860b1942018-08-16 14:59:28 -0700795std::string AidlVariableDeclaration::ValueString(const ConstantValueDecorator& decorator) const {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900796 if (default_value_ != nullptr) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700797 return default_value_->ValueString(GetType(), decorator);
Jiyong Parka468e2a2018-08-29 21:25:18 +0900798 } else {
799 return "";
800 }
Steven Moreland25294322018-08-07 18:13:55 -0700801}
802
Jooyung Hanc5688f72021-01-05 15:41:48 +0900803void AidlVariableDeclaration::TraverseChildren(
804 std::function<void(const AidlNode&)> traverse) const {
805 traverse(GetType());
Jooyung Hanc3c739a2021-10-14 11:33:14 +0900806 if (auto default_value = GetDefaultValue(); default_value) {
807 traverse(*default_value);
Jooyung Hanc5688f72021-01-05 15:41:48 +0900808 }
809}
810
Steven Moreland46e9da82018-07-27 15:45:29 -0700811AidlArgument::AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
812 AidlTypeSpecifier* type, const std::string& name)
813 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700814 direction_(direction),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700815 direction_specified_(true) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700816
Steven Moreland46e9da82018-07-27 15:45:29 -0700817AidlArgument::AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type,
818 const std::string& name)
819 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700820 direction_(AidlArgument::IN_DIR),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700821 direction_specified_(false) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700822
Jooyung Han020d8d12021-02-26 17:23:02 +0900823static std::string to_string(AidlArgument::Direction direction) {
824 switch (direction) {
825 case AidlArgument::IN_DIR:
826 return "in";
827 case AidlArgument::OUT_DIR:
828 return "out";
829 case AidlArgument::INOUT_DIR:
830 return "inout";
831 }
832}
833
Jiyong Park02da7422018-07-16 16:00:26 +0900834string AidlArgument::GetDirectionSpecifier() const {
Casey Dahlinc378c992015-09-29 16:50:40 -0700835 string ret;
Casey Dahlinc378c992015-09-29 16:50:40 -0700836 if (direction_specified_) {
Jooyung Han020d8d12021-02-26 17:23:02 +0900837 ret = to_string(direction_);
Casey Dahlinc378c992015-09-29 16:50:40 -0700838 }
Casey Dahlinc378c992015-09-29 16:50:40 -0700839 return ret;
840}
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700841
Jiyong Park02da7422018-07-16 16:00:26 +0900842string AidlArgument::ToString() const {
Devin Mooreeccdb902020-03-24 16:22:40 -0700843 if (direction_specified_) {
844 return GetDirectionSpecifier() + " " + AidlVariableDeclaration::ToString();
845 } else {
846 return AidlVariableDeclaration::ToString();
847 }
Jiyong Park02da7422018-07-16 16:00:26 +0900848}
849
Jooyung Han020d8d12021-02-26 17:23:02 +0900850static std::string FormatDirections(const std::set<AidlArgument::Direction>& directions) {
851 std::vector<std::string> out;
852 for (const auto& d : directions) {
853 out.push_back(to_string(d));
854 }
855
856 if (out.size() <= 1) { // [] => "" or [A] => "A"
857 return Join(out, "");
858 } else if (out.size() == 2) { // [A,B] => "A or B"
859 return Join(out, " or ");
860 } else { // [A,B,C] => "A, B, or C"
861 out.back() = "or " + out.back();
862 return Join(out, ", ");
863 }
864}
865
866bool AidlArgument::CheckValid(const AidlTypenames& typenames) const {
867 if (!GetType().CheckValid(typenames)) {
868 return false;
869 }
870
871 const auto& aspect = typenames.GetArgumentAspect(GetType());
872
873 if (aspect.possible_directions.size() == 0) {
874 AIDL_ERROR(this) << aspect.name << " cannot be an argument type";
875 return false;
876 }
877
878 // when direction is not specified, "in" is assumed and should be the only possible direction
879 if (!DirectionWasSpecified() && aspect.possible_directions != std::set{AidlArgument::IN_DIR}) {
880 AIDL_ERROR(this) << "The direction of '" << GetName() << "' is not specified. " << aspect.name
881 << " can be an " << FormatDirections(aspect.possible_directions)
882 << " parameter.";
883 return false;
884 }
885
886 if (aspect.possible_directions.count(GetDirection()) == 0) {
887 AIDL_ERROR(this) << "'" << GetName() << "' can't be an " << GetDirectionSpecifier()
888 << " parameter because " << aspect.name << " can only be an "
889 << FormatDirections(aspect.possible_directions) << " parameter.";
890 return false;
891 }
892
893 return true;
894}
895
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900896bool AidlCommentable::IsHidden() const {
Jooyung Han24effbf2021-01-16 10:24:03 +0900897 return android::aidl::HasHideInComments(GetComments());
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900898}
899
900bool AidlCommentable::IsDeprecated() const {
Jooyung Hand4fe00e2021-01-11 16:21:53 +0900901 return android::aidl::FindDeprecated(GetComments()).has_value();
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900902}
903
Jooyung Han8451a202021-01-16 03:07:06 +0900904AidlMember::AidlMember(const AidlLocation& location, const Comments& comments)
Jooyung Han2aedb112021-09-29 09:37:59 +0900905 : AidlAnnotatable(location, comments) {}
Steven Moreland46e9da82018-07-27 15:45:29 -0700906
Steven Moreland46e9da82018-07-27 15:45:29 -0700907AidlConstantDeclaration::AidlConstantDeclaration(const AidlLocation& location,
908 AidlTypeSpecifier* type, const std::string& name,
909 AidlConstantValue* value)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900910 : AidlMember(location, type->GetComments()), type_(type), name_(name), value_(value) {}
Steven Moreland693640b2018-07-19 13:46:27 -0700911
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900912bool AidlConstantDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700913 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900914 valid &= type_->CheckValid(typenames);
Steven Moreland25294322018-08-07 18:13:55 -0700915 valid &= value_->CheckValid();
916 if (!valid) return false;
Steven Moreland693640b2018-07-19 13:46:27 -0700917
Steven Morelande689da22020-11-10 02:06:30 +0000918 const static set<string> kSupportedConstTypes = {"String", "byte", "int", "long"};
Jooyung Han965e31d2020-11-27 12:30:16 +0900919 if (kSupportedConstTypes.find(type_->Signature()) == kSupportedConstTypes.end()) {
920 AIDL_ERROR(this) << "Constant of type " << type_->Signature() << " is not supported.";
Steven Moreland693640b2018-07-19 13:46:27 -0700921 return false;
922 }
923
Will McVickerd7d18df2019-09-12 13:40:50 -0700924 return true;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700925}
926
Jiyong Parka428d212018-08-29 22:26:30 +0900927string AidlConstantDeclaration::ToString() const {
Jooyung Hanb3ca6302020-11-27 14:13:27 +0900928 return "const " + type_->ToString() + " " + name_ + " = " +
929 ValueString(AidlConstantValueDecorator);
Jiyong Parka428d212018-08-29 22:26:30 +0900930}
931
932string AidlConstantDeclaration::Signature() const {
933 return type_->Signature() + " " + name_;
934}
935
Steven Moreland46e9da82018-07-27 15:45:29 -0700936AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
937 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jooyung Han8451a202021-01-16 03:07:06 +0900938 const Comments& comments)
Jiyong Parkb034bf02018-07-30 17:44:33 +0900939 : AidlMethod(location, oneway, type, name, args, comments, 0, true) {
940 has_id_ = false;
941}
942
943AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
944 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jooyung Han8451a202021-01-16 03:07:06 +0900945 const Comments& comments, int id, bool is_user_defined)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900946 : AidlMember(location, comments),
Steven Moreland46e9da82018-07-27 15:45:29 -0700947 oneway_(oneway),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700948 type_(type),
949 name_(name),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700950 arguments_(std::move(*args)),
Jiyong Parkb034bf02018-07-30 17:44:33 +0900951 id_(id),
952 is_user_defined_(is_user_defined) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700953 has_id_ = true;
954 delete args;
Christopher Wileyad339272015-10-05 19:11:58 -0700955 for (const unique_ptr<AidlArgument>& a : arguments_) {
956 if (a->IsIn()) { in_arguments_.push_back(a.get()); }
957 if (a->IsOut()) { out_arguments_.push_back(a.get()); }
958 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700959}
960
Jiyong Park02da7422018-07-16 16:00:26 +0900961string AidlMethod::Signature() const {
962 vector<string> arg_signatures;
963 for (const auto& arg : GetArguments()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900964 arg_signatures.emplace_back(arg->GetType().Signature());
Jiyong Park02da7422018-07-16 16:00:26 +0900965 }
Jiyong Park309668e2018-07-28 16:55:44 +0900966 return GetName() + "(" + Join(arg_signatures, ", ") + ")";
967}
968
969string AidlMethod::ToString() const {
970 vector<string> arg_strings;
971 for (const auto& arg : GetArguments()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900972 arg_strings.emplace_back(arg->ToString());
Jiyong Park309668e2018-07-28 16:55:44 +0900973 }
Jooyung Han965e31d2020-11-27 12:30:16 +0900974 string ret = (IsOneway() ? "oneway " : "") + GetType().ToString() + " " + GetName() + "(" +
Steven Moreland4ee68632018-12-14 15:52:46 -0800975 Join(arg_strings, ", ") + ")";
Jiyong Parked65bf42018-08-28 15:43:27 +0900976 if (HasId()) {
977 ret += " = " + std::to_string(GetId());
978 }
979 return ret;
Jiyong Park02da7422018-07-16 16:00:26 +0900980}
981
Steven Moreland46e9da82018-07-27 15:45:29 -0700982AidlDefinedType::AidlDefinedType(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +0900983 const Comments& comments, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900984 std::vector<std::unique_ptr<AidlMember>>* members)
Jooyung Han2aedb112021-09-29 09:37:59 +0900985 : AidlMember(location, comments), AidlScope(this), name_(name), package_(package) {
Jooyung Han93f48f02021-06-05 00:11:16 +0900986 // adjust name/package when name is fully qualified (for preprocessed files)
987 if (package_.empty() && name_.find('.') != std::string::npos) {
988 // Note that this logic is absolutely wrong. Given a parcelable
989 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
990 // the class is just Bar. However, this was the way it was done in the past.
991 //
992 // See b/17415692
993 auto pos = name.rfind('.');
994 // name is the last part.
995 name_ = name.substr(pos + 1);
996 // package is the initial parts (except the last).
997 package_ = name.substr(0, pos);
998 }
Jooyung Han829ec7c2020-12-02 12:07:36 +0900999 if (members) {
1000 for (auto& m : *members) {
Jooyung Hanbaa71062021-09-29 09:06:03 +09001001 if (auto constant = AidlCast<AidlConstantDeclaration>(*m); constant) {
Jooyung Han829ec7c2020-12-02 12:07:36 +09001002 constants_.emplace_back(constant);
Jooyung Hanbaa71062021-09-29 09:06:03 +09001003 } else if (auto variable = AidlCast<AidlVariableDeclaration>(*m); variable) {
Jooyung Han829ec7c2020-12-02 12:07:36 +09001004 variables_.emplace_back(variable);
Jooyung Hanbaa71062021-09-29 09:06:03 +09001005 } else if (auto method = AidlCast<AidlMethod>(*m); method) {
Jooyung Han829ec7c2020-12-02 12:07:36 +09001006 methods_.emplace_back(method);
Jooyung Han2aedb112021-09-29 09:37:59 +09001007 } else if (auto type = AidlCast<AidlDefinedType>(*m); type) {
1008 type->SetEnclosingScope(this);
1009 types_.emplace_back(type);
Jooyung Han829ec7c2020-12-02 12:07:36 +09001010 } else {
Jooyung Hanbaa71062021-09-29 09:06:03 +09001011 AIDL_FATAL(*m) << "Unknown member type.";
Jooyung Han829ec7c2020-12-02 12:07:36 +09001012 }
1013 members_.push_back(m.release());
1014 }
1015 delete members;
1016 }
1017}
Steven Moreland787b0432018-07-03 09:00:58 -07001018
Jooyung Han808a2a02020-12-28 16:46:54 +09001019bool AidlDefinedType::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -08001020 if (!AidlAnnotatable::CheckValid(typenames)) {
1021 return false;
1022 }
Jooyung Han829ec7c2020-12-02 12:07:36 +09001023 if (!CheckValidWithMembers(typenames)) {
1024 return false;
1025 }
Devin Moore24f68572020-02-26 13:20:59 -08001026 return true;
1027}
1028
Steven Moreland787b0432018-07-03 09:00:58 -07001029std::string AidlDefinedType::GetCanonicalName() const {
1030 if (package_.empty()) {
1031 return GetName();
1032 }
Jooyung Han2aedb112021-09-29 09:37:59 +09001033 if (auto parent = GetParentType(); parent) {
1034 return parent->GetCanonicalName() + "." + GetName();
1035 }
Steven Moreland787b0432018-07-03 09:00:58 -07001036 return GetPackage() + "." + GetName();
1037}
1038
Jooyung Han829ec7c2020-12-02 12:07:36 +09001039bool AidlDefinedType::CheckValidWithMembers(const AidlTypenames& typenames) const {
1040 bool success = true;
1041
Jooyung Han2aedb112021-09-29 09:37:59 +09001042 for (const auto& t : GetNestedTypes()) {
1043 success = success && t->CheckValid(typenames);
1044 }
1045
Jooyung Han7fc5de02021-09-30 22:26:27 +09001046 if (auto parameterizable = AsParameterizable();
1047 parameterizable && parameterizable->IsGeneric() && !GetNestedTypes().empty()) {
1048 AIDL_ERROR(this) << "Generic types can't have nested types.";
1049 return false;
1050 }
1051
Jooyung Han2aedb112021-09-29 09:37:59 +09001052 std::set<std::string> nested_type_names;
1053 for (const auto& t : GetNestedTypes()) {
1054 bool duplicated = !nested_type_names.emplace(t->GetName()).second;
1055 if (duplicated) {
1056 AIDL_ERROR(t) << "Redefinition of '" << t->GetName() << "'.";
1057 success = false;
1058 }
1059 // nested type can't have a parent name
1060 if (t->GetName() == GetName()) {
1061 AIDL_ERROR(t) << "Nested type '" << GetName() << "' has the same name as its parent.";
1062 success = false;
1063 }
Jooyung Han2b1487d2021-09-30 09:57:01 +09001064 // Having unstructured parcelables as nested types doesn't make sense because they are defined
1065 // somewhere else in native languages (e.g. C++, Java...).
1066 if (AidlCast<AidlParcelable>(*t)) {
1067 AIDL_ERROR(t) << "'" << t->GetName()
1068 << "' is nested. Unstructured parcelables should be at the root scope.";
1069 return false;
1070 }
Jooyung Han2aedb112021-09-29 09:37:59 +09001071 // For now we don't allow "interface" to be nested
1072 if (AidlCast<AidlInterface>(*t)) {
1073 AIDL_ERROR(t) << "'" << t->GetName()
1074 << "' is nested. Interfaces should be at the root scope.";
1075 return false;
1076 }
1077 }
1078
Jooyung Han8e9ae872021-10-13 02:52:25 +09001079 if (!TopologicalVisit(GetNestedTypes(), [](auto&) {})) {
1080 AIDL_ERROR(this) << GetName()
1081 << " has nested types with cyclic references. C++ and NDK backends don't "
1082 "support cyclic references.";
1083 return false;
1084 }
1085
Jooyung Han829ec7c2020-12-02 12:07:36 +09001086 for (const auto& v : GetFields()) {
1087 const bool field_valid = v->CheckValid(typenames);
1088 success = success && field_valid;
1089 }
1090
1091 // field names should be unique
1092 std::set<std::string> fieldnames;
1093 for (const auto& v : GetFields()) {
1094 bool duplicated = !fieldnames.emplace(v->GetName()).second;
1095 if (duplicated) {
1096 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName() << "'";
1097 success = false;
1098 }
1099 }
1100
1101 // immutable parcelables should have immutable fields.
1102 if (IsJavaOnlyImmutable()) {
1103 for (const auto& v : GetFields()) {
1104 if (!typenames.CanBeJavaOnlyImmutable(v->GetType())) {
1105 AIDL_ERROR(v) << "The @JavaOnlyImmutable '" << GetName() << "' has a "
1106 << "non-immutable field named '" << v->GetName() << "'.";
1107 success = false;
1108 }
1109 }
1110 }
1111
1112 set<string> constant_names;
1113 for (const auto& constant : GetConstantDeclarations()) {
1114 if (constant_names.count(constant->GetName()) > 0) {
1115 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
1116 success = false;
1117 }
1118 constant_names.insert(constant->GetName());
1119 success = success && constant->CheckValid(typenames);
1120 }
1121
1122 return success;
1123}
1124
1125bool AidlDefinedType::CheckValidForGetterNames() const {
1126 bool success = true;
1127 std::set<std::string> getters;
1128 for (const auto& v : GetFields()) {
1129 bool duplicated = !getters.emplace(v->GetCapitalizedName()).second;
1130 if (duplicated) {
1131 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName()
1132 << "' after capitalizing the first letter";
1133 success = false;
1134 }
1135 }
1136 return success;
1137}
1138
Jooyung Han2aedb112021-09-29 09:37:59 +09001139const AidlDefinedType* AidlDefinedType::GetParentType() const {
1140 AIDL_FATAL_IF(GetEnclosingScope() == nullptr, this) << "Scope is not set.";
1141 return AidlCast<AidlDefinedType>(GetEnclosingScope()->GetNode());
1142}
1143
Jooyung Hanf8c39632021-10-05 09:56:29 +09001144const AidlDefinedType* AidlDefinedType::GetRootType() const {
1145 const AidlDefinedType* root = this;
1146 for (auto parent = root->GetParentType(); parent; parent = parent->GetParentType()) {
1147 root = parent;
1148 }
1149 return root;
1150}
1151
Jooyung Han2aedb112021-09-29 09:37:59 +09001152// Resolve `name` in the current scope. If not found, delegate to the parent
Jooyung Han13f1fa52021-06-11 18:06:12 +09001153std::string AidlDefinedType::ResolveName(const std::string& name) const {
Jooyung Han2aedb112021-09-29 09:37:59 +09001154 // For example, in the following, t1's type Baz means x.Foo.Bar.Baz
1155 // while t2's type is y.Baz.
Jooyung Han13f1fa52021-06-11 18:06:12 +09001156 // package x;
Jooyung Han2aedb112021-09-29 09:37:59 +09001157 // import y.Baz;
Jooyung Han13f1fa52021-06-11 18:06:12 +09001158 // parcelable Foo {
1159 // parcelable Bar {
Jooyung Han2aedb112021-09-29 09:37:59 +09001160 // enum Baz { ... }
1161 // Baz t1; // -> should be x.Foo.Bar.Baz
Jooyung Han13f1fa52021-06-11 18:06:12 +09001162 // }
Jooyung Han2aedb112021-09-29 09:37:59 +09001163 // Baz t2; // -> should be y.Baz
1164 // Bar.Baz t3; // -> should be x.Foo.Bar.Baz
Jooyung Han13f1fa52021-06-11 18:06:12 +09001165 // }
1166 AIDL_FATAL_IF(!GetEnclosingScope(), this)
1167 << "Type should have an enclosing scope.(e.g. AidlDocument)";
Jooyung Han2aedb112021-09-29 09:37:59 +09001168 if (AidlTypenames::IsBuiltinTypename(name)) {
1169 return name;
1170 }
1171
1172 const auto first_dot = name.find_first_of('.');
1173 // For "Outer.Inner", we look up "Outer" in the import list.
1174 const std::string class_name =
1175 (first_dot == std::string::npos) ? name : name.substr(0, first_dot);
1176 // Keep ".Inner", to make a fully-qualified name
1177 const std::string nested_type = (first_dot == std::string::npos) ? "" : name.substr(first_dot);
1178
1179 // check if it is a nested type
1180 for (const auto& type : GetNestedTypes()) {
1181 if (type->GetName() == class_name) {
1182 return type->GetCanonicalName() + nested_type;
1183 }
1184 }
1185
Jooyung Han13f1fa52021-06-11 18:06:12 +09001186 return GetEnclosingScope()->ResolveName(name);
1187}
1188
Jooyung Hanbaa71062021-09-29 09:06:03 +09001189template <>
1190const AidlDefinedType* AidlCast<AidlDefinedType>(const AidlNode& node) {
1191 struct Visitor : AidlVisitor {
1192 const AidlDefinedType* defined_type = nullptr;
1193 void Visit(const AidlInterface& t) override { defined_type = &t; }
1194 void Visit(const AidlEnumDeclaration& t) override { defined_type = &t; }
1195 void Visit(const AidlStructuredParcelable& t) override { defined_type = &t; }
1196 void Visit(const AidlUnionDecl& t) override { defined_type = &t; }
1197 void Visit(const AidlParcelable& t) override { defined_type = &t; }
1198 } v;
1199 node.DispatchVisit(v);
1200 return v.defined_type;
Jooyung Han35784982021-06-29 06:26:12 +09001201}
1202
1203const AidlDocument& AidlDefinedType::GetDocument() const {
Jooyung Hanf8c39632021-10-05 09:56:29 +09001204 const AidlDefinedType* root = GetRootType();
1205 auto scope = root->GetEnclosingScope();
Jooyung Han35784982021-06-29 06:26:12 +09001206 AIDL_FATAL_IF(!scope, this) << "no scope defined.";
1207 auto doc = AidlCast<AidlDocument>(scope->GetNode());
Jooyung Hanf8c39632021-10-05 09:56:29 +09001208 AIDL_FATAL_IF(!doc, this) << "root scope is not a document.";
Jooyung Han35784982021-06-29 06:26:12 +09001209 return *doc;
1210}
1211
Jiyong Park18132182020-06-08 20:24:40 +09001212AidlParcelable::AidlParcelable(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001213 const std::string& package, const Comments& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001214 const std::string& cpp_header, std::vector<std::string>* type_params,
1215 std::vector<std::unique_ptr<AidlMember>>* members)
1216 : AidlDefinedType(location, name, comments, package, members),
Jeongik Chadf76dc72019-11-28 00:08:47 +09001217 AidlParameterizable<std::string>(type_params),
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -08001218 cpp_header_(cpp_header) {
1219 // Strip off quotation marks if we actually have a cpp header.
1220 if (cpp_header_.length() >= 2) {
1221 cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2);
1222 }
Casey Dahlin59401da2015-10-09 18:16:45 -07001223}
Jeongik Chadf76dc72019-11-28 00:08:47 +09001224
1225template <typename T>
1226bool AidlParameterizable<T>::CheckValid() const {
1227 return true;
1228};
1229
1230template <>
1231bool AidlParameterizable<std::string>::CheckValid() const {
1232 if (!IsGeneric()) {
1233 return true;
1234 }
1235 std::unordered_set<std::string> set(GetTypeParameters().begin(), GetTypeParameters().end());
1236 if (set.size() != GetTypeParameters().size()) {
1237 AIDL_ERROR(this->AsAidlNode()) << "Every type parameter should be unique.";
1238 return false;
1239 }
1240 return true;
1241}
Casey Dahlin59401da2015-10-09 18:16:45 -07001242
Jooyung Han808a2a02020-12-28 16:46:54 +09001243bool AidlParcelable::CheckValid(const AidlTypenames& typenames) const {
1244 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001245 return false;
1246 }
Jeongik Chadf76dc72019-11-28 00:08:47 +09001247 if (!AidlParameterizable<std::string>::CheckValid()) {
1248 return false;
1249 }
Jeongik Cha82317dd2019-02-27 20:26:11 +09001250
1251 return true;
1252}
1253
Steven Moreland5557f1c2018-07-02 13:50:23 -07001254AidlStructuredParcelable::AidlStructuredParcelable(
Jiyong Park18132182020-06-08 20:24:40 +09001255 const AidlLocation& location, const std::string& name, const std::string& package,
Jooyung Han8451a202021-01-16 03:07:06 +09001256 const Comments& comments, std::vector<std::string>* type_params,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001257 std::vector<std::unique_ptr<AidlMember>>* members)
1258 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Steven Moreland5557f1c2018-07-02 13:50:23 -07001259
Jooyung Han808a2a02020-12-28 16:46:54 +09001260bool AidlStructuredParcelable::CheckValid(const AidlTypenames& typenames) const {
1261 if (!AidlParcelable::CheckValid(typenames)) {
Devin Moore24f68572020-02-26 13:20:59 -08001262 return false;
1263 }
Jeongik Cha13066da2020-08-06 15:43:19 +09001264
Jooyung Han59af9cc2020-10-25 21:44:14 +09001265 bool success = true;
Jeongik Cha36f76c32020-07-28 00:25:52 +09001266
Jooyung Hand4057c42020-10-23 13:28:22 +09001267 if (IsFixedSize()) {
1268 for (const auto& v : GetFields()) {
1269 if (!typenames.CanBeFixedSize(v->GetType())) {
1270 AIDL_ERROR(v) << "The @FixedSize parcelable '" << this->GetName() << "' has a "
1271 << "non-fixed size field named " << v->GetName() << ".";
1272 success = false;
1273 }
1274 }
1275 }
1276
1277 if (IsJavaOnlyImmutable()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001278 // Immutable parcelables provide getters
Jooyung Han829ec7c2020-12-02 12:07:36 +09001279 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001280 success = false;
Jooyung Hand4057c42020-10-23 13:28:22 +09001281 }
1282 }
1283
Daniel Norman85aed542019-08-21 12:01:14 -07001284 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001285}
1286
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001287// TODO: we should treat every backend all the same in future.
Jooyung Hanb4997aa2021-10-16 03:26:12 +09001288bool AidlTypeSpecifier::LanguageSpecificCheckValid(Options::Language lang) const {
Andrei Homescub62afd92020-05-11 19:24:59 -07001289 if (this->GetName() == "FileDescriptor" &&
1290 (lang == Options::Language::NDK || lang == Options::Language::RUST)) {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001291 AIDL_ERROR(this) << "FileDescriptor isn't supported by the " << to_string(lang) << " backend.";
Steven Morelandc8a4ca82020-01-21 17:50:08 -08001292 return false;
1293 }
Devin Moore6a01ca12020-08-28 10:24:19 -07001294
Devin Moore6a01ca12020-08-28 10:24:19 -07001295 if (lang != Options::Language::JAVA) {
1296 if (this->GetName() == "List" && !this->IsGeneric()) {
1297 AIDL_ERROR(this) << "Currently, only the Java backend supports non-generic List.";
1298 return false;
1299 }
1300 if (this->GetName() == "Map" || this->GetName() == "CharSequence") {
1301 AIDL_ERROR(this) << "Currently, only Java backend supports " << this->GetName() << ".";
1302 return false;
Jeongik Cha08ca2182019-11-21 14:01:13 +09001303 }
1304 }
1305
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001306 return true;
1307}
1308
1309// TODO: we should treat every backend all the same in future.
Jooyung Hanb4997aa2021-10-16 03:26:12 +09001310bool AidlDefinedType::LanguageSpecificCheckValid(Options::Language lang) const {
Jooyung Han589cfb02021-09-28 17:26:04 +09001311 struct Visitor : AidlVisitor {
Jooyung Hanb4997aa2021-10-16 03:26:12 +09001312 Visitor(Options::Language lang) : lang(lang) {}
Jooyung Han589cfb02021-09-28 17:26:04 +09001313 void Visit(const AidlTypeSpecifier& type) override {
Jooyung Hanb4997aa2021-10-16 03:26:12 +09001314 success = success && type.LanguageSpecificCheckValid(lang);
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001315 }
Jooyung Han589cfb02021-09-28 17:26:04 +09001316 Options::Language lang;
1317 bool success = true;
Jooyung Hanb4997aa2021-10-16 03:26:12 +09001318 } v(lang);
Jooyung Han589cfb02021-09-28 17:26:04 +09001319 VisitTopDown(v, *this);
1320 return v.success;
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001321}
1322
Daniel Norman85aed542019-08-21 12:01:14 -07001323AidlEnumerator::AidlEnumerator(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001324 AidlConstantValue* value, const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +09001325 : AidlCommentable(location, comments),
Jooyung Han29813842020-12-08 01:28:03 +09001326 name_(name),
1327 value_(value),
Jooyung Han29813842020-12-08 01:28:03 +09001328 value_user_specified_(value != nullptr) {}
Daniel Norman85aed542019-08-21 12:01:14 -07001329
1330bool AidlEnumerator::CheckValid(const AidlTypeSpecifier& enum_backing_type) const {
1331 if (GetValue() == nullptr) {
1332 return false;
1333 }
1334 if (!GetValue()->CheckValid()) {
1335 return false;
1336 }
Will McVickerd7d18df2019-09-12 13:40:50 -07001337 if (GetValue()->ValueString(enum_backing_type, AidlConstantValueDecorator).empty()) {
Daniel Norman85aed542019-08-21 12:01:14 -07001338 AIDL_ERROR(this) << "Enumerator type differs from enum backing type.";
1339 return false;
1340 }
1341 return true;
1342}
1343
1344string AidlEnumerator::ValueString(const AidlTypeSpecifier& backing_type,
1345 const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -07001346 return GetValue()->ValueString(backing_type, decorator);
Daniel Norman85aed542019-08-21 12:01:14 -07001347}
1348
1349AidlEnumDeclaration::AidlEnumDeclaration(const AidlLocation& location, const std::string& name,
1350 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jooyung Han8451a202021-01-16 03:07:06 +09001351 const std::string& package, const Comments& comments)
Jooyung Han829ec7c2020-12-02 12:07:36 +09001352 : AidlDefinedType(location, name, comments, package, nullptr),
Jooyung Han29813842020-12-08 01:28:03 +09001353 enumerators_(std::move(*enumerators)) {
Jooyung Han672557b2020-12-24 05:18:00 +09001354 // Fill missing enumerator values with <prev + 1>
1355 // This can't be done in Autofill() because type/ref resolution depends on this.
1356 // For example, with enum E { A, B = A }, B's value 'A' is a reference which can't be
1357 // resolved if A has no value set.
Daniel Normanb28684e2019-10-17 15:31:39 -07001358 const AidlEnumerator* previous = nullptr;
1359 for (const auto& enumerator : enumerators_) {
1360 if (enumerator->GetValue() == nullptr) {
Jooyung Han29813842020-12-08 01:28:03 +09001361 auto loc = enumerator->GetLocation();
Daniel Normanb28684e2019-10-17 15:31:39 -07001362 if (previous == nullptr) {
Devin Mooredf93ebb2020-03-25 14:03:35 -07001363 enumerator->SetValue(
Jooyung Han29813842020-12-08 01:28:03 +09001364 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "0")));
Daniel Normanb28684e2019-10-17 15:31:39 -07001365 } else {
Jooyung Hand0c8af02021-01-06 18:08:01 +09001366 auto prev_value = std::make_unique<AidlConstantReference>(loc, previous->GetName());
Daniel Normanb28684e2019-10-17 15:31:39 -07001367 enumerator->SetValue(std::make_unique<AidlBinaryConstExpression>(
Jooyung Han29813842020-12-08 01:28:03 +09001368 loc, std::move(prev_value), "+",
1369 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "1"))));
Daniel Normanb28684e2019-10-17 15:31:39 -07001370 }
1371 }
1372 previous = enumerator.get();
1373 }
1374}
1375
Jooyung Han672557b2020-12-24 05:18:00 +09001376bool AidlEnumDeclaration::Autofill(const AidlTypenames& typenames) {
1377 if (auto annot = BackingType(); annot != nullptr) {
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001378 // Autofill() is called before the grand CheckValid(). But AidlAnnotation::ParamValue()
1379 // calls AidlConstantValue::evaluate() which requires CheckValid() to be called before. So we
Jooyung Han672557b2020-12-24 05:18:00 +09001380 // need to call CheckValid().
1381 if (!annot->CheckValid()) {
1382 return false;
1383 }
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001384 auto type = annot->ParamValue<std::string>("type").value();
Jooyung Hanaccd9192021-10-14 15:57:28 +09001385 backing_type_ = typenames.MakeResolvedType(annot->GetLocation(), type, false);
Jooyung Han672557b2020-12-24 05:18:00 +09001386 } else {
1387 // Default to byte type for enums.
Jooyung Hanaccd9192021-10-14 15:57:28 +09001388 backing_type_ = typenames.MakeResolvedType(GetLocation(), "byte", false);
Jooyung Han672557b2020-12-24 05:18:00 +09001389 }
Steven Morelandb248d072021-09-29 19:07:17 -07001390
1391 // we only support/test a few backing types, so make sure this is a supported
1392 // one (otherwise boolean might work, which isn't supported/tested in all
1393 // backends)
1394 static std::set<string> kBackingTypes = {"byte", "int", "long"};
1395 if (kBackingTypes.find(backing_type_->GetName()) == kBackingTypes.end()) {
1396 AIDL_ERROR(this) << "Invalid backing type: " << backing_type_->GetName()
1397 << ". Backing type must be one of: " << Join(kBackingTypes, ", ");
1398 return false;
Jooyung Han672557b2020-12-24 05:18:00 +09001399 }
1400 return true;
1401}
1402
Jooyung Han808a2a02020-12-28 16:46:54 +09001403bool AidlEnumDeclaration::CheckValid(const AidlTypenames& typenames) const {
1404 if (!AidlDefinedType::CheckValid(typenames)) {
Devin Moore24f68572020-02-26 13:20:59 -08001405 return false;
1406 }
Jooyung Han829ec7c2020-12-02 12:07:36 +09001407 if (!GetMembers().empty()) {
1408 AIDL_ERROR(this) << "Enum doesn't support fields/constants/methods.";
1409 return false;
1410 }
Daniel Norman85aed542019-08-21 12:01:14 -07001411 if (backing_type_ == nullptr) {
1412 AIDL_ERROR(this) << "Enum declaration missing backing type.";
1413 return false;
1414 }
1415 bool success = true;
1416 for (const auto& enumerator : enumerators_) {
1417 success = success && enumerator->CheckValid(GetBackingType());
1418 }
Jooyung Han3b990182020-12-22 17:44:31 +09001419
Jooyung Han808a2a02020-12-28 16:46:54 +09001420 return success;
Daniel Norman85aed542019-08-21 12:01:14 -07001421}
1422
Jooyung Han2946afc2020-10-05 20:29:16 +09001423AidlUnionDecl::AidlUnionDecl(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001424 const std::string& package, const Comments& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001425 std::vector<std::string>* type_params,
1426 std::vector<std::unique_ptr<AidlMember>>* members)
1427 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Jooyung Han2946afc2020-10-05 20:29:16 +09001428
Jooyung Han808a2a02020-12-28 16:46:54 +09001429bool AidlUnionDecl::CheckValid(const AidlTypenames& typenames) const {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001430 // visit parents
Jooyung Han808a2a02020-12-28 16:46:54 +09001431 if (!AidlParcelable::CheckValid(typenames)) {
Jooyung Hanfe89f122020-10-14 03:49:18 +09001432 return false;
1433 }
Jooyung Han59af9cc2020-10-25 21:44:14 +09001434
1435 // unions provide getters always
Jooyung Han829ec7c2020-12-02 12:07:36 +09001436 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001437 return false;
Jooyung Hanfe89f122020-10-14 03:49:18 +09001438 }
1439
1440 // now, visit self!
1441 bool success = true;
1442
1443 // TODO(b/170807936) do we need to allow ParcelableHolder in union?
1444 for (const auto& v : GetFields()) {
1445 if (v->GetType().GetName() == "ParcelableHolder") {
1446 AIDL_ERROR(*v) << "A union can't have a member of ParcelableHolder '" << v->GetName() << "'";
1447 success = false;
1448 }
1449 }
1450
Jooyung Hanfe89f122020-10-14 03:49:18 +09001451 if (GetFields().empty()) {
1452 AIDL_ERROR(*this) << "The union '" << this->GetName() << "' has no fields.";
1453 return false;
1454 }
1455
Jooyung Han53fb4242020-12-17 16:03:49 +09001456 // first member should have useful default value (implicit or explicit)
1457 const auto& first = GetFields()[0];
1458 if (!first->HasUsefulDefaultValue()) {
1459 // Most types can be initialized without a default value. For example,
1460 // interface types are inherently nullable. But, enum types should have
1461 // an explicit default value.
1462 if (!first->GetType().IsArray() && typenames.GetEnumDeclaration(first->GetType())) {
1463 AIDL_ERROR(first)
1464 << "The union's first member should have a useful default value. Enum types can be "
1465 "initialized with a reference. (e.g. ... = MyEnum.FOO;)";
1466 return false;
1467 }
1468 // In Java, array types are initialized as null without a default value. To be sure that default
1469 // initialized unions are accepted by other backends we require arrays also have a default
1470 // value.
1471 if (first->GetType().IsArray()) {
1472 AIDL_ERROR(first)
1473 << "The union's first member should have a useful default value. Arrays can be "
1474 "initialized with values(e.g. ... = { values... };) or marked as @nullable.";
1475 return false;
1476 }
1477 }
1478
Jooyung Hanfe89f122020-10-14 03:49:18 +09001479 return success;
1480}
1481
Steven Moreland46e9da82018-07-27 15:45:29 -07001482AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001483 const Comments& comments, bool oneway, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001484 std::vector<std::unique_ptr<AidlMember>>* members)
1485 : AidlDefinedType(location, name, comments, package, members) {
1486 for (auto& m : GetMethods()) {
1487 m.get()->ApplyInterfaceOneway(oneway);
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001488 }
Casey Dahlinfb7da2e2015-10-08 17:26:09 -07001489}
1490
Jooyung Han808a2a02020-12-28 16:46:54 +09001491bool AidlInterface::CheckValid(const AidlTypenames& typenames) const {
1492 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001493 return false;
1494 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001495 // Has to be a pointer due to deleting copy constructor. No idea why.
1496 map<string, const AidlMethod*> method_names;
1497 for (const auto& m : GetMethods()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001498 if (!m->GetType().CheckValid(typenames)) {
1499 return false;
1500 }
1501
Jeongik Cha649e8a72020-03-27 17:47:40 +09001502 // TODO(b/156872582): Support it when ParcelableHolder supports every backend.
1503 if (m->GetType().GetName() == "ParcelableHolder") {
1504 AIDL_ERROR(m) << "ParcelableHolder cannot be a return type";
1505 return false;
1506 }
Steven Morelandacd53472018-12-14 10:17:26 -08001507 if (m->IsOneway() && m->GetType().GetName() != "void") {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001508 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot return a value";
1509 return false;
1510 }
1511
1512 set<string> argument_names;
1513 for (const auto& arg : m->GetArguments()) {
1514 auto it = argument_names.find(arg->GetName());
1515 if (it != argument_names.end()) {
1516 AIDL_ERROR(m) << "method '" << m->GetName() << "' has duplicate argument name '"
1517 << arg->GetName() << "'";
1518 return false;
1519 }
1520 argument_names.insert(arg->GetName());
1521
Jooyung Han020d8d12021-02-26 17:23:02 +09001522 if (!arg->CheckValid(typenames)) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001523 return false;
1524 }
1525
Steven Morelandacd53472018-12-14 10:17:26 -08001526 if (m->IsOneway() && arg->IsOut()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001527 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot have out parameters";
1528 return false;
1529 }
Jooyung Han15fd6c62020-10-23 13:54:46 +09001530
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001531 // check that the name doesn't match a keyword
Jeongik Cha997281d2020-01-16 15:23:59 +09001532 if (IsJavaKeyword(arg->GetName().c_str())) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001533 AIDL_ERROR(arg) << "Argument name is a Java or aidl keyword";
1534 return false;
1535 }
1536
1537 // Reserve a namespace for internal use
1538 if (android::base::StartsWith(arg->GetName(), "_aidl")) {
1539 AIDL_ERROR(arg) << "Argument name cannot begin with '_aidl'";
1540 return false;
1541 }
Jooyung Hanfa181932021-06-12 07:56:53 +09001542
1543 if (arg->GetType().GetName() == "void") {
1544 AIDL_ERROR(arg->GetType())
1545 << "'void' is an invalid type for the parameter '" << arg->GetName() << "'";
1546 return false;
1547 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001548 }
1549
1550 auto it = method_names.find(m->GetName());
1551 // prevent duplicate methods
1552 if (it == method_names.end()) {
1553 method_names[m->GetName()] = m.get();
1554 } else {
1555 AIDL_ERROR(m) << "attempt to redefine method " << m->GetName() << ":";
1556 AIDL_ERROR(it->second) << "previously defined here.";
1557 return false;
1558 }
1559
Paul Trautrimb77048c2020-01-21 16:39:32 +09001560 static set<string> reserved_methods{"asBinder()", "getInterfaceHash()", "getInterfaceVersion()",
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001561 "getTransactionName(int)"};
1562
1563 if (reserved_methods.find(m->Signature()) != reserved_methods.end()) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001564 AIDL_ERROR(m) << " method " << m->Signature() << " is reserved for internal use.";
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001565 return false;
1566 }
1567 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001568
1569 bool success = true;
1570 set<string> constant_names;
Jooyung Han3f347ca2020-12-01 12:41:50 +09001571 for (const auto& constant : GetConstantDeclarations()) {
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001572 if (constant_names.count(constant->GetName()) > 0) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001573 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001574 success = false;
1575 }
1576 constant_names.insert(constant->GetName());
1577 success = success && constant->CheckValid(typenames);
1578 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001579 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001580}
1581
Jiyong Park27fd7fd2020-08-27 16:25:09 +09001582std::string AidlInterface::GetDescriptor() const {
1583 std::string annotatedDescriptor = AidlAnnotatable::GetDescriptor();
1584 if (annotatedDescriptor != "") {
1585 return annotatedDescriptor;
1586 }
1587 return GetCanonicalName();
1588}
1589
Jooyung Han132cf802021-01-15 02:17:32 +09001590AidlImport::AidlImport(const AidlLocation& location, const std::string& needed_class,
Jooyung Han8451a202021-01-16 03:07:06 +09001591 const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +09001592 : AidlNode(location, comments), needed_class_(needed_class) {}
Jooyung Han29813842020-12-08 01:28:03 +09001593
Jooyung Han13f1fa52021-06-11 18:06:12 +09001594AidlDocument::AidlDocument(const AidlLocation& location, const Comments& comments,
1595 std::vector<std::unique_ptr<AidlImport>> imports,
Jooyung Han35784982021-06-29 06:26:12 +09001596 std::vector<std::unique_ptr<AidlDefinedType>> defined_types,
1597 bool is_preprocessed)
Jooyung Han13f1fa52021-06-11 18:06:12 +09001598 : AidlCommentable(location, comments),
Jooyung Han35784982021-06-29 06:26:12 +09001599 AidlScope(this),
Jooyung Han13f1fa52021-06-11 18:06:12 +09001600 imports_(std::move(imports)),
Jooyung Han35784982021-06-29 06:26:12 +09001601 defined_types_(std::move(defined_types)),
1602 is_preprocessed_(is_preprocessed) {
Jooyung Han13f1fa52021-06-11 18:06:12 +09001603 for (const auto& t : defined_types_) {
1604 t->SetEnclosingScope(this);
1605 }
1606}
1607
1608// Resolves type name in the current document.
1609// - built-in types
1610// - imported types
1611// - top-level type
1612std::string AidlDocument::ResolveName(const std::string& name) const {
1613 if (AidlTypenames::IsBuiltinTypename(name)) {
1614 return name;
1615 }
1616
1617 const auto first_dot = name.find_first_of('.');
1618 // For "Outer.Inner", we look up "Outer" in the import list.
Jooyung Han29813842020-12-08 01:28:03 +09001619 const std::string class_name =
Jooyung Han13f1fa52021-06-11 18:06:12 +09001620 (first_dot == std::string::npos) ? name : name.substr(0, first_dot);
1621 // Keep ".Inner", to make a fully-qualified name
1622 const std::string nested_type = (first_dot == std::string::npos) ? "" : name.substr(first_dot);
1623
Jooyung Han29813842020-12-08 01:28:03 +09001624 for (const auto& import : Imports()) {
Jooyung Han13f1fa52021-06-11 18:06:12 +09001625 if (import->SimpleName() == class_name) {
1626 return import->GetNeededClass() + nested_type;
Jooyung Han29813842020-12-08 01:28:03 +09001627 }
1628 }
Jooyung Han13f1fa52021-06-11 18:06:12 +09001629
1630 // check if it is a top-level type.
1631 for (const auto& type : DefinedTypes()) {
1632 if (type->GetName() == class_name) {
1633 return type->GetCanonicalName() + nested_type;
1634 }
Jooyung Han29813842020-12-08 01:28:03 +09001635 }
Jooyung Han13f1fa52021-06-11 18:06:12 +09001636
1637 // name itself might be fully-qualified name.
1638 return name;
Steven Moreland26318532020-12-23 20:08:36 +00001639}