blob: 199689dadce6ae4c449fec93f0f7cfc9a176e60f [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",
Thiébaud Weksteen133da842021-10-29 16:49:58 +1100184 CONTEXT_TYPE_INTERFACE | CONTEXT_METHOD,
Thiébaud Weksteen9ab59122021-09-20 09:37:38 +0200185 {{"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.
Thiébaud Weksteen133da842021-10-29 16:49:58 +1100445std::unique_ptr<perm::Expression> AidlAnnotatable::EnforceExpression() const {
Thiébaud Weksteen5a4db212021-09-02 17:09:34 +0200446 auto annot = GetAnnotation(annotations_, AidlAnnotation::Type::ENFORCE);
447 if (annot) {
Thiébaud Weksteen9ab59122021-09-20 09:37:38 +0200448 auto perm_expr = annot->EnforceExpression();
449 if (!perm_expr.ok()) {
450 // This should have been caught during validation.
Thiébaud Weksteen133da842021-10-29 16:49:58 +1100451 AIDL_FATAL(this) << "Unable to parse @Enforce annotation: " << perm_expr.error();
Thiébaud Weksteen5a4db212021-09-02 17:09:34 +0200452 }
Thiébaud Weksteen9ab59122021-09-20 09:37:38 +0200453 return std::move(perm_expr.value());
Thiébaud Weksteen5a4db212021-09-02 17:09:34 +0200454 }
455 return {};
456}
457
Jeongik Cha88f95a82020-01-15 13:02:16 +0900458bool AidlAnnotatable::IsStableApiParcelable(Options::Language lang) const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700459 return lang == Options::Language::JAVA &&
460 GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_STABLE_PARCELABLE);
Jeongik Cha82317dd2019-02-27 20:26:11 +0900461}
462
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800463bool AidlAnnotatable::IsHide() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700464 return GetAnnotation(annotations_, AidlAnnotation::Type::HIDE);
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800465}
466
Jooyung Han829ec7c2020-12-02 12:07:36 +0900467bool AidlAnnotatable::JavaDerive(const std::string& method) const {
468 auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_DERIVE);
469 if (annotation != nullptr) {
Jooyung Hanb3c77ed2020-12-26 02:02:45 +0900470 return annotation->ParamValue<bool>(method).value_or(false);
Jooyung Han829ec7c2020-12-02 12:07:36 +0900471 }
472 return false;
Jiyong Park43113fb2020-07-20 16:26:19 +0900473}
474
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900475std::string AidlAnnotatable::GetDescriptor() const {
476 auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::DESCRIPTOR);
477 if (annotation != nullptr) {
Jooyung Hanb3c77ed2020-12-26 02:02:45 +0900478 return annotation->ParamValue<std::string>("value").value();
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900479 }
480 return "";
481}
482
Devin Moore24f68572020-02-26 13:20:59 -0800483bool AidlAnnotatable::CheckValid(const AidlTypenames&) const {
Andrei Onea9445fc62019-06-27 18:11:59 +0100484 for (const auto& annotation : GetAnnotations()) {
Steven Morelanda7560e82021-10-08 16:24:39 -0700485 if (!annotation->CheckValid()) {
Jooyung Hand902a972020-10-23 17:32:44 +0900486 return false;
487 }
488 }
489
490 std::map<AidlAnnotation::Type, AidlLocation> declared;
491 for (const auto& annotation : GetAnnotations()) {
Steven Morelanda7560e82021-10-08 16:24:39 -0700492 const auto& [iter, inserted] =
493 declared.emplace(annotation->GetType(), annotation->GetLocation());
494 if (!inserted && !annotation->Repeatable()) {
495 AIDL_ERROR(this) << "'" << annotation->GetName()
Jooyung Hand902a972020-10-23 17:32:44 +0900496 << "' is repeated, but not allowed. Previous location: " << iter->second;
497 return false;
498 }
Andrei Onea9445fc62019-06-27 18:11:59 +0100499 }
Steven Morelanda57d0a62019-07-30 09:41:14 -0700500
Andrei Onea9445fc62019-06-27 18:11:59 +0100501 return true;
502}
503
Jiyong Park68bc77a2018-07-19 19:00:45 +0900504string AidlAnnotatable::ToString() const {
505 vector<string> ret;
506 for (const auto& a : annotations_) {
Steven Morelanda7560e82021-10-08 16:24:39 -0700507 ret.emplace_back(a->ToString());
Jiyong Park68bc77a2018-07-19 19:00:45 +0900508 }
509 std::sort(ret.begin(), ret.end());
510 return Join(ret, " ");
511}
512
Steven Moreland46e9da82018-07-27 15:45:29 -0700513AidlTypeSpecifier::AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name,
514 bool is_array,
Jiyong Park1deecc32018-07-17 01:14:41 +0900515 vector<unique_ptr<AidlTypeSpecifier>>* type_params,
Jooyung Han8451a202021-01-16 03:07:06 +0900516 const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +0900517 : AidlAnnotatable(location, comments),
Jeongik Chadf76dc72019-11-28 00:08:47 +0900518 AidlParameterizable<unique_ptr<AidlTypeSpecifier>>(type_params),
Steven Moreland46e9da82018-07-27 15:45:29 -0700519 unresolved_name_(unresolved_name),
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700520 is_array_(is_array),
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900521 split_name_(Split(unresolved_name, ".")) {}
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700522
Steven Moreland0cac8662021-10-08 16:43:29 -0700523void AidlTypeSpecifier::ViewAsArrayBase(std::function<void(const AidlTypeSpecifier&)> func) const {
Steven Moreland3f658cf2018-08-20 13:40:54 -0700524 AIDL_FATAL_IF(!is_array_, this);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900525 // Declaring array of generic type cannot happen, it is grammar error.
526 AIDL_FATAL_IF(IsGeneric(), this);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700527
Steven Moreland0cac8662021-10-08 16:43:29 -0700528 is_array_ = false;
529 func(*this);
530 is_array_ = true;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700531}
532
Jooyung Han965e31d2020-11-27 12:30:16 +0900533string AidlTypeSpecifier::Signature() const {
Jiyong Park1deecc32018-07-17 01:14:41 +0900534 string ret = GetName();
535 if (IsGeneric()) {
536 vector<string> arg_names;
537 for (const auto& ta : GetTypeParameters()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900538 arg_names.emplace_back(ta->Signature());
Jiyong Parkccf00f82018-07-17 01:39:23 +0900539 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900540 ret += "<" + Join(arg_names, ",") + ">";
Jiyong Parkccf00f82018-07-17 01:39:23 +0900541 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900542 if (IsArray()) {
543 ret += "[]";
544 }
545 return ret;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900546}
547
Jooyung Han965e31d2020-11-27 12:30:16 +0900548string AidlTypeSpecifier::ToString() const {
549 string ret = Signature();
Jiyong Park02da7422018-07-16 16:00:26 +0900550 string annotations = AidlAnnotatable::ToString();
551 if (annotations != "") {
552 ret = annotations + " " + ret;
553 }
554 return ret;
555}
556
Jooyung Han13f1fa52021-06-11 18:06:12 +0900557// When `scope` is specified, name is resolved first based on it.
558// `scope` can be null for built-in types and fully-qualified types.
559bool AidlTypeSpecifier::Resolve(const AidlTypenames& typenames, const AidlScope* scope) {
Steven Moreland21780812020-09-11 01:29:45 +0000560 AIDL_FATAL_IF(IsResolved(), this);
Jooyung Han13f1fa52021-06-11 18:06:12 +0900561 std::string name = unresolved_name_;
562 if (scope) {
563 name = scope->ResolveName(name);
564 }
565 AidlTypenames::ResolvedTypename result = typenames.ResolveTypename(name);
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700566 if (result.is_resolved) {
567 fully_qualified_name_ = result.canonical_name;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900568 split_name_ = Split(fully_qualified_name_, ".");
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900569 defined_type_ = result.defined_type;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900570 }
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700571 return result.is_resolved;
Casey Dahlin70078e62015-09-30 17:01:30 -0700572}
573
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900574const AidlDefinedType* AidlTypeSpecifier::GetDefinedType() const {
575 return defined_type_;
576}
577
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900578bool AidlTypeSpecifier::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -0800579 if (!AidlAnnotatable::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100580 return false;
581 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900582 if (IsGeneric()) {
Jooyung Hand09a21d2021-02-15 18:56:55 +0900583 const auto& types = GetTypeParameters();
584 for (const auto& arg : types) {
585 if (!arg->CheckValid(typenames)) {
586 return false;
587 }
588 }
Jeongik Chae74c86d2019-12-12 16:54:03 +0900589
Jooyung Hand09a21d2021-02-15 18:56:55 +0900590 const string& type_name = GetName();
Jeongik Chae74c86d2019-12-12 16:54:03 +0900591 // TODO(b/136048684) Disallow to use primitive types only if it is List or Map.
592 if (type_name == "List" || type_name == "Map") {
Jooyung Hane87cdd02020-12-11 16:47:35 +0900593 if (std::any_of(types.begin(), types.end(), [&](auto& type_ptr) {
Jooyung Han1f35ef32021-02-15 19:08:05 +0900594 return !type_ptr->IsArray() &&
595 (typenames.GetEnumDeclaration(*type_ptr) ||
596 AidlTypenames::IsPrimitiveTypename(type_ptr->GetName()));
Jeongik Chae74c86d2019-12-12 16:54:03 +0900597 })) {
Devin Moore7b8d5c92020-03-17 14:14:08 -0700598 AIDL_ERROR(this) << "A generic type cannot have any primitive type parameters.";
Jeongik Chae74c86d2019-12-12 16:54:03 +0900599 return false;
600 }
601 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800602 const auto defined_type = typenames.TryGetDefinedType(type_name);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900603 const auto parameterizable =
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800604 defined_type != nullptr ? defined_type->AsParameterizable() : nullptr;
605 const bool is_user_defined_generic_type =
Jeongik Chadf76dc72019-11-28 00:08:47 +0900606 parameterizable != nullptr && parameterizable->IsGeneric();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800607 const size_t num_params = GetTypeParameters().size();
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900608 if (type_name == "List") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800609 if (num_params > 1) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900610 AIDL_ERROR(this) << "List can only have one type parameter, but got: '" << Signature()
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000611 << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900612 return false;
613 }
Jooyung Han55f96ad2020-12-13 10:08:33 +0900614 const AidlTypeSpecifier& contained_type = *GetTypeParameters()[0];
Jooyung Hancea89002021-02-15 17:04:04 +0900615 if (contained_type.IsArray()) {
616 AIDL_ERROR(this)
617 << "List of arrays is not supported. List<T> supports parcelable/union, String, "
618 "IBinder, and ParcelFileDescriptor.";
619 return false;
620 }
Jooyung Han55f96ad2020-12-13 10:08:33 +0900621 const string& contained_type_name = contained_type.GetName();
622 if (AidlTypenames::IsBuiltinTypename(contained_type_name)) {
623 if (contained_type_name != "String" && contained_type_name != "IBinder" &&
624 contained_type_name != "ParcelFileDescriptor") {
625 AIDL_ERROR(this) << "List<" << contained_type_name
626 << "> is not supported. List<T> supports parcelable/union, String, "
627 "IBinder, and ParcelFileDescriptor.";
628 return false;
629 }
630 } else { // Defined types
631 if (typenames.GetInterface(contained_type)) {
632 AIDL_ERROR(this) << "List<" << contained_type_name
633 << "> is not supported. List<T> supports parcelable/union, String, "
634 "IBinder, and ParcelFileDescriptor.";
635 return false;
636 }
637 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900638 } else if (type_name == "Map") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800639 if (num_params != 0 && num_params != 2) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900640 AIDL_ERROR(this) << "Map must have 0 or 2 type parameters, but got "
Jooyung Han965e31d2020-11-27 12:30:16 +0900641 << "'" << Signature() << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900642 return false;
643 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800644 if (num_params == 2) {
Jooyung Hanaab242a2021-02-15 19:01:15 +0900645 const string& key_type = GetTypeParameters()[0]->Signature();
Jeongik Chae48d9942020-01-02 17:39:00 +0900646 if (key_type != "String") {
647 AIDL_ERROR(this) << "The type of key in map must be String, but it is "
648 << "'" << key_type << "'";
649 return false;
650 }
651 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800652 } else if (is_user_defined_generic_type) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900653 const size_t allowed = parameterizable->GetTypeParameters().size();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800654 if (num_params != allowed) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900655 AIDL_ERROR(this) << type_name << " must have " << allowed << " type parameters, but got "
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800656 << num_params;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900657 return false;
658 }
659 } else {
660 AIDL_ERROR(this) << type_name << " is not a generic type.";
661 return false;
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900662 }
663 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900664
Steven Moreland11cb9452020-01-21 16:56:58 -0800665 const bool is_generic_string_list = GetName() == "List" && IsGeneric() &&
666 GetTypeParameters().size() == 1 &&
667 GetTypeParameters()[0]->GetName() == "String";
668 if (IsUtf8InCpp() && (GetName() != "String" && !is_generic_string_list)) {
669 AIDL_ERROR(this) << "@utf8InCpp can only be used on String, String[], and List<String>.";
670 return false;
671 }
672
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900673 if (GetName() == "void") {
674 if (IsArray() || IsNullable() || IsUtf8InCpp()) {
675 AIDL_ERROR(this) << "void type cannot be an array or nullable or utf8 string";
676 return false;
677 }
678 }
679
680 if (IsArray()) {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800681 const auto defined_type = typenames.TryGetDefinedType(GetName());
682 if (defined_type != nullptr && defined_type->AsInterface() != nullptr) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900683 AIDL_ERROR(this) << "Binder type cannot be an array";
684 return false;
685 }
Jooyung Han49b8f362021-10-15 10:58:02 +0900686 if (GetName() == "ParcelableHolder" || GetName() == "List" || GetName() == "Map" ||
687 GetName() == "CharSequence") {
688 AIDL_ERROR(this) << "Arrays of " << GetName() << " are not supported.";
Steven Moreland8042d2d2020-09-30 23:31:32 +0000689 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
Thiébaud Weksteenff6dafa2021-09-21 11:53:40 +0200981bool AidlMethod::CheckValid(const AidlTypenames& typenames) const {
982 if (!GetType().CheckValid(typenames)) {
983 return false;
984 }
985
986 // TODO(b/156872582): Support it when ParcelableHolder supports every backend.
987 if (GetType().GetName() == "ParcelableHolder") {
988 AIDL_ERROR(this) << "ParcelableHolder cannot be a return type";
989 return false;
990 }
991 if (IsOneway() && GetType().GetName() != "void") {
992 AIDL_ERROR(this) << "oneway method '" << GetName() << "' cannot return a value";
993 return false;
994 }
995
996 set<string> argument_names;
997 for (const auto& arg : GetArguments()) {
998 auto it = argument_names.find(arg->GetName());
999 if (it != argument_names.end()) {
1000 AIDL_ERROR(this) << "method '" << GetName() << "' has duplicate argument name '"
1001 << arg->GetName() << "'";
1002 return false;
1003 }
1004 argument_names.insert(arg->GetName());
1005
1006 if (!arg->CheckValid(typenames)) {
1007 return false;
1008 }
1009
1010 if (IsOneway() && arg->IsOut()) {
1011 AIDL_ERROR(this) << "oneway method '" << this->GetName() << "' cannot have out parameters";
1012 return false;
1013 }
1014
1015 // check that the name doesn't match a keyword
1016 if (IsJavaKeyword(arg->GetName().c_str())) {
1017 AIDL_ERROR(arg) << "Argument name is a Java or aidl keyword";
1018 return false;
1019 }
1020
1021 // Reserve a namespace for internal use
1022 if (android::base::StartsWith(arg->GetName(), "_aidl")) {
1023 AIDL_ERROR(arg) << "Argument name cannot begin with '_aidl'";
1024 return false;
1025 }
1026
1027 if (arg->GetType().GetName() == "void") {
1028 AIDL_ERROR(arg->GetType()) << "'void' is an invalid type for the parameter '"
1029 << arg->GetName() << "'";
1030 return false;
1031 }
1032 }
1033 return true;
1034}
1035
Steven Moreland46e9da82018-07-27 15:45:29 -07001036AidlDefinedType::AidlDefinedType(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001037 const Comments& comments, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001038 std::vector<std::unique_ptr<AidlMember>>* members)
Jooyung Han2aedb112021-09-29 09:37:59 +09001039 : AidlMember(location, comments), AidlScope(this), name_(name), package_(package) {
Jooyung Han93f48f02021-06-05 00:11:16 +09001040 // adjust name/package when name is fully qualified (for preprocessed files)
1041 if (package_.empty() && name_.find('.') != std::string::npos) {
1042 // Note that this logic is absolutely wrong. Given a parcelable
1043 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
1044 // the class is just Bar. However, this was the way it was done in the past.
1045 //
1046 // See b/17415692
1047 auto pos = name.rfind('.');
1048 // name is the last part.
1049 name_ = name.substr(pos + 1);
1050 // package is the initial parts (except the last).
1051 package_ = name.substr(0, pos);
1052 }
Jooyung Han829ec7c2020-12-02 12:07:36 +09001053 if (members) {
1054 for (auto& m : *members) {
Jooyung Hanbaa71062021-09-29 09:06:03 +09001055 if (auto constant = AidlCast<AidlConstantDeclaration>(*m); constant) {
Jooyung Han829ec7c2020-12-02 12:07:36 +09001056 constants_.emplace_back(constant);
Jooyung Hanbaa71062021-09-29 09:06:03 +09001057 } else if (auto variable = AidlCast<AidlVariableDeclaration>(*m); variable) {
Jooyung Han829ec7c2020-12-02 12:07:36 +09001058 variables_.emplace_back(variable);
Jooyung Hanbaa71062021-09-29 09:06:03 +09001059 } else if (auto method = AidlCast<AidlMethod>(*m); method) {
Jooyung Han829ec7c2020-12-02 12:07:36 +09001060 methods_.emplace_back(method);
Jooyung Han2aedb112021-09-29 09:37:59 +09001061 } else if (auto type = AidlCast<AidlDefinedType>(*m); type) {
1062 type->SetEnclosingScope(this);
1063 types_.emplace_back(type);
Jooyung Han829ec7c2020-12-02 12:07:36 +09001064 } else {
Jooyung Hanbaa71062021-09-29 09:06:03 +09001065 AIDL_FATAL(*m) << "Unknown member type.";
Jooyung Han829ec7c2020-12-02 12:07:36 +09001066 }
1067 members_.push_back(m.release());
1068 }
1069 delete members;
1070 }
1071}
Steven Moreland787b0432018-07-03 09:00:58 -07001072
Jooyung Han808a2a02020-12-28 16:46:54 +09001073bool AidlDefinedType::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -08001074 if (!AidlAnnotatable::CheckValid(typenames)) {
1075 return false;
1076 }
Jooyung Han829ec7c2020-12-02 12:07:36 +09001077 if (!CheckValidWithMembers(typenames)) {
1078 return false;
1079 }
Devin Moore24f68572020-02-26 13:20:59 -08001080 return true;
1081}
1082
Steven Moreland787b0432018-07-03 09:00:58 -07001083std::string AidlDefinedType::GetCanonicalName() const {
1084 if (package_.empty()) {
1085 return GetName();
1086 }
Jooyung Han2aedb112021-09-29 09:37:59 +09001087 if (auto parent = GetParentType(); parent) {
1088 return parent->GetCanonicalName() + "." + GetName();
1089 }
Steven Moreland787b0432018-07-03 09:00:58 -07001090 return GetPackage() + "." + GetName();
1091}
1092
Jooyung Han829ec7c2020-12-02 12:07:36 +09001093bool AidlDefinedType::CheckValidWithMembers(const AidlTypenames& typenames) const {
1094 bool success = true;
1095
Jooyung Han2aedb112021-09-29 09:37:59 +09001096 for (const auto& t : GetNestedTypes()) {
1097 success = success && t->CheckValid(typenames);
1098 }
1099
Jooyung Han7fc5de02021-09-30 22:26:27 +09001100 if (auto parameterizable = AsParameterizable();
1101 parameterizable && parameterizable->IsGeneric() && !GetNestedTypes().empty()) {
1102 AIDL_ERROR(this) << "Generic types can't have nested types.";
1103 return false;
1104 }
1105
Jooyung Han2aedb112021-09-29 09:37:59 +09001106 std::set<std::string> nested_type_names;
1107 for (const auto& t : GetNestedTypes()) {
1108 bool duplicated = !nested_type_names.emplace(t->GetName()).second;
1109 if (duplicated) {
1110 AIDL_ERROR(t) << "Redefinition of '" << t->GetName() << "'.";
1111 success = false;
1112 }
1113 // nested type can't have a parent name
1114 if (t->GetName() == GetName()) {
1115 AIDL_ERROR(t) << "Nested type '" << GetName() << "' has the same name as its parent.";
1116 success = false;
1117 }
Jooyung Han2b1487d2021-09-30 09:57:01 +09001118 // Having unstructured parcelables as nested types doesn't make sense because they are defined
1119 // somewhere else in native languages (e.g. C++, Java...).
1120 if (AidlCast<AidlParcelable>(*t)) {
1121 AIDL_ERROR(t) << "'" << t->GetName()
1122 << "' is nested. Unstructured parcelables should be at the root scope.";
1123 return false;
1124 }
Jooyung Han2aedb112021-09-29 09:37:59 +09001125 // For now we don't allow "interface" to be nested
1126 if (AidlCast<AidlInterface>(*t)) {
1127 AIDL_ERROR(t) << "'" << t->GetName()
1128 << "' is nested. Interfaces should be at the root scope.";
1129 return false;
1130 }
1131 }
1132
Jooyung Han8e9ae872021-10-13 02:52:25 +09001133 if (!TopologicalVisit(GetNestedTypes(), [](auto&) {})) {
1134 AIDL_ERROR(this) << GetName()
1135 << " has nested types with cyclic references. C++ and NDK backends don't "
1136 "support cyclic references.";
1137 return false;
1138 }
1139
Jooyung Han829ec7c2020-12-02 12:07:36 +09001140 for (const auto& v : GetFields()) {
1141 const bool field_valid = v->CheckValid(typenames);
1142 success = success && field_valid;
1143 }
1144
1145 // field names should be unique
1146 std::set<std::string> fieldnames;
1147 for (const auto& v : GetFields()) {
1148 bool duplicated = !fieldnames.emplace(v->GetName()).second;
1149 if (duplicated) {
1150 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName() << "'";
1151 success = false;
1152 }
1153 }
1154
1155 // immutable parcelables should have immutable fields.
1156 if (IsJavaOnlyImmutable()) {
1157 for (const auto& v : GetFields()) {
1158 if (!typenames.CanBeJavaOnlyImmutable(v->GetType())) {
1159 AIDL_ERROR(v) << "The @JavaOnlyImmutable '" << GetName() << "' has a "
1160 << "non-immutable field named '" << v->GetName() << "'.";
1161 success = false;
1162 }
1163 }
1164 }
1165
1166 set<string> constant_names;
1167 for (const auto& constant : GetConstantDeclarations()) {
1168 if (constant_names.count(constant->GetName()) > 0) {
1169 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
1170 success = false;
1171 }
1172 constant_names.insert(constant->GetName());
1173 success = success && constant->CheckValid(typenames);
1174 }
1175
1176 return success;
1177}
1178
1179bool AidlDefinedType::CheckValidForGetterNames() const {
1180 bool success = true;
1181 std::set<std::string> getters;
1182 for (const auto& v : GetFields()) {
1183 bool duplicated = !getters.emplace(v->GetCapitalizedName()).second;
1184 if (duplicated) {
1185 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName()
1186 << "' after capitalizing the first letter";
1187 success = false;
1188 }
1189 }
1190 return success;
1191}
1192
Jooyung Han2aedb112021-09-29 09:37:59 +09001193const AidlDefinedType* AidlDefinedType::GetParentType() const {
1194 AIDL_FATAL_IF(GetEnclosingScope() == nullptr, this) << "Scope is not set.";
1195 return AidlCast<AidlDefinedType>(GetEnclosingScope()->GetNode());
1196}
1197
Jooyung Hanf8c39632021-10-05 09:56:29 +09001198const AidlDefinedType* AidlDefinedType::GetRootType() const {
1199 const AidlDefinedType* root = this;
1200 for (auto parent = root->GetParentType(); parent; parent = parent->GetParentType()) {
1201 root = parent;
1202 }
1203 return root;
1204}
1205
Jooyung Han2aedb112021-09-29 09:37:59 +09001206// Resolve `name` in the current scope. If not found, delegate to the parent
Jooyung Han13f1fa52021-06-11 18:06:12 +09001207std::string AidlDefinedType::ResolveName(const std::string& name) const {
Jooyung Han2aedb112021-09-29 09:37:59 +09001208 // For example, in the following, t1's type Baz means x.Foo.Bar.Baz
1209 // while t2's type is y.Baz.
Jooyung Han13f1fa52021-06-11 18:06:12 +09001210 // package x;
Jooyung Han2aedb112021-09-29 09:37:59 +09001211 // import y.Baz;
Jooyung Han13f1fa52021-06-11 18:06:12 +09001212 // parcelable Foo {
1213 // parcelable Bar {
Jooyung Han2aedb112021-09-29 09:37:59 +09001214 // enum Baz { ... }
1215 // Baz t1; // -> should be x.Foo.Bar.Baz
Jooyung Han13f1fa52021-06-11 18:06:12 +09001216 // }
Jooyung Han2aedb112021-09-29 09:37:59 +09001217 // Baz t2; // -> should be y.Baz
1218 // Bar.Baz t3; // -> should be x.Foo.Bar.Baz
Jooyung Han13f1fa52021-06-11 18:06:12 +09001219 // }
1220 AIDL_FATAL_IF(!GetEnclosingScope(), this)
1221 << "Type should have an enclosing scope.(e.g. AidlDocument)";
Jooyung Han2aedb112021-09-29 09:37:59 +09001222 if (AidlTypenames::IsBuiltinTypename(name)) {
1223 return name;
1224 }
1225
1226 const auto first_dot = name.find_first_of('.');
1227 // For "Outer.Inner", we look up "Outer" in the import list.
1228 const std::string class_name =
1229 (first_dot == std::string::npos) ? name : name.substr(0, first_dot);
1230 // Keep ".Inner", to make a fully-qualified name
1231 const std::string nested_type = (first_dot == std::string::npos) ? "" : name.substr(first_dot);
1232
1233 // check if it is a nested type
1234 for (const auto& type : GetNestedTypes()) {
1235 if (type->GetName() == class_name) {
1236 return type->GetCanonicalName() + nested_type;
1237 }
1238 }
1239
Jooyung Han13f1fa52021-06-11 18:06:12 +09001240 return GetEnclosingScope()->ResolveName(name);
1241}
1242
Jooyung Hanbaa71062021-09-29 09:06:03 +09001243template <>
1244const AidlDefinedType* AidlCast<AidlDefinedType>(const AidlNode& node) {
1245 struct Visitor : AidlVisitor {
1246 const AidlDefinedType* defined_type = nullptr;
1247 void Visit(const AidlInterface& t) override { defined_type = &t; }
1248 void Visit(const AidlEnumDeclaration& t) override { defined_type = &t; }
1249 void Visit(const AidlStructuredParcelable& t) override { defined_type = &t; }
1250 void Visit(const AidlUnionDecl& t) override { defined_type = &t; }
1251 void Visit(const AidlParcelable& t) override { defined_type = &t; }
1252 } v;
1253 node.DispatchVisit(v);
1254 return v.defined_type;
Jooyung Han35784982021-06-29 06:26:12 +09001255}
1256
1257const AidlDocument& AidlDefinedType::GetDocument() const {
Jooyung Hanf8c39632021-10-05 09:56:29 +09001258 const AidlDefinedType* root = GetRootType();
1259 auto scope = root->GetEnclosingScope();
Jooyung Han35784982021-06-29 06:26:12 +09001260 AIDL_FATAL_IF(!scope, this) << "no scope defined.";
1261 auto doc = AidlCast<AidlDocument>(scope->GetNode());
Jooyung Hanf8c39632021-10-05 09:56:29 +09001262 AIDL_FATAL_IF(!doc, this) << "root scope is not a document.";
Jooyung Han35784982021-06-29 06:26:12 +09001263 return *doc;
1264}
1265
Jiyong Park18132182020-06-08 20:24:40 +09001266AidlParcelable::AidlParcelable(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001267 const std::string& package, const Comments& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001268 const std::string& cpp_header, std::vector<std::string>* type_params,
1269 std::vector<std::unique_ptr<AidlMember>>* members)
1270 : AidlDefinedType(location, name, comments, package, members),
Jeongik Chadf76dc72019-11-28 00:08:47 +09001271 AidlParameterizable<std::string>(type_params),
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -08001272 cpp_header_(cpp_header) {
1273 // Strip off quotation marks if we actually have a cpp header.
1274 if (cpp_header_.length() >= 2) {
1275 cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2);
1276 }
Casey Dahlin59401da2015-10-09 18:16:45 -07001277}
Jeongik Chadf76dc72019-11-28 00:08:47 +09001278
1279template <typename T>
1280bool AidlParameterizable<T>::CheckValid() const {
1281 return true;
1282};
1283
1284template <>
1285bool AidlParameterizable<std::string>::CheckValid() const {
1286 if (!IsGeneric()) {
1287 return true;
1288 }
1289 std::unordered_set<std::string> set(GetTypeParameters().begin(), GetTypeParameters().end());
1290 if (set.size() != GetTypeParameters().size()) {
1291 AIDL_ERROR(this->AsAidlNode()) << "Every type parameter should be unique.";
1292 return false;
1293 }
1294 return true;
1295}
Casey Dahlin59401da2015-10-09 18:16:45 -07001296
Jooyung Han808a2a02020-12-28 16:46:54 +09001297bool AidlParcelable::CheckValid(const AidlTypenames& typenames) const {
1298 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001299 return false;
1300 }
Jeongik Chadf76dc72019-11-28 00:08:47 +09001301 if (!AidlParameterizable<std::string>::CheckValid()) {
1302 return false;
1303 }
Jeongik Cha82317dd2019-02-27 20:26:11 +09001304
1305 return true;
1306}
1307
Steven Moreland5557f1c2018-07-02 13:50:23 -07001308AidlStructuredParcelable::AidlStructuredParcelable(
Jiyong Park18132182020-06-08 20:24:40 +09001309 const AidlLocation& location, const std::string& name, const std::string& package,
Jooyung Han8451a202021-01-16 03:07:06 +09001310 const Comments& comments, std::vector<std::string>* type_params,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001311 std::vector<std::unique_ptr<AidlMember>>* members)
1312 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Steven Moreland5557f1c2018-07-02 13:50:23 -07001313
Jooyung Han808a2a02020-12-28 16:46:54 +09001314bool AidlStructuredParcelable::CheckValid(const AidlTypenames& typenames) const {
1315 if (!AidlParcelable::CheckValid(typenames)) {
Devin Moore24f68572020-02-26 13:20:59 -08001316 return false;
1317 }
Jeongik Cha13066da2020-08-06 15:43:19 +09001318
Jooyung Han59af9cc2020-10-25 21:44:14 +09001319 bool success = true;
Jeongik Cha36f76c32020-07-28 00:25:52 +09001320
Jooyung Hand4057c42020-10-23 13:28:22 +09001321 if (IsFixedSize()) {
1322 for (const auto& v : GetFields()) {
1323 if (!typenames.CanBeFixedSize(v->GetType())) {
1324 AIDL_ERROR(v) << "The @FixedSize parcelable '" << this->GetName() << "' has a "
1325 << "non-fixed size field named " << v->GetName() << ".";
1326 success = false;
1327 }
1328 }
1329 }
1330
1331 if (IsJavaOnlyImmutable()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001332 // Immutable parcelables provide getters
Jooyung Han829ec7c2020-12-02 12:07:36 +09001333 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001334 success = false;
Jooyung Hand4057c42020-10-23 13:28:22 +09001335 }
1336 }
1337
Daniel Norman85aed542019-08-21 12:01:14 -07001338 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001339}
1340
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001341// TODO: we should treat every backend all the same in future.
Jooyung Hanb4997aa2021-10-16 03:26:12 +09001342bool AidlTypeSpecifier::LanguageSpecificCheckValid(Options::Language lang) const {
Andrei Homescub62afd92020-05-11 19:24:59 -07001343 if (this->GetName() == "FileDescriptor" &&
1344 (lang == Options::Language::NDK || lang == Options::Language::RUST)) {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001345 AIDL_ERROR(this) << "FileDescriptor isn't supported by the " << to_string(lang) << " backend.";
Steven Morelandc8a4ca82020-01-21 17:50:08 -08001346 return false;
1347 }
Devin Moore6a01ca12020-08-28 10:24:19 -07001348
Devin Moore6a01ca12020-08-28 10:24:19 -07001349 if (lang != Options::Language::JAVA) {
1350 if (this->GetName() == "List" && !this->IsGeneric()) {
1351 AIDL_ERROR(this) << "Currently, only the Java backend supports non-generic List.";
1352 return false;
1353 }
1354 if (this->GetName() == "Map" || this->GetName() == "CharSequence") {
1355 AIDL_ERROR(this) << "Currently, only Java backend supports " << this->GetName() << ".";
1356 return false;
Jeongik Cha08ca2182019-11-21 14:01:13 +09001357 }
1358 }
1359
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001360 return true;
1361}
1362
1363// TODO: we should treat every backend all the same in future.
Jooyung Hanb4997aa2021-10-16 03:26:12 +09001364bool AidlDefinedType::LanguageSpecificCheckValid(Options::Language lang) const {
Jooyung Han589cfb02021-09-28 17:26:04 +09001365 struct Visitor : AidlVisitor {
Jooyung Hanb4997aa2021-10-16 03:26:12 +09001366 Visitor(Options::Language lang) : lang(lang) {}
Jooyung Han589cfb02021-09-28 17:26:04 +09001367 void Visit(const AidlTypeSpecifier& type) override {
Jooyung Hanb4997aa2021-10-16 03:26:12 +09001368 success = success && type.LanguageSpecificCheckValid(lang);
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001369 }
Jooyung Han589cfb02021-09-28 17:26:04 +09001370 Options::Language lang;
1371 bool success = true;
Jooyung Hanb4997aa2021-10-16 03:26:12 +09001372 } v(lang);
Jooyung Han589cfb02021-09-28 17:26:04 +09001373 VisitTopDown(v, *this);
1374 return v.success;
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001375}
1376
Daniel Norman85aed542019-08-21 12:01:14 -07001377AidlEnumerator::AidlEnumerator(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001378 AidlConstantValue* value, const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +09001379 : AidlCommentable(location, comments),
Jooyung Han29813842020-12-08 01:28:03 +09001380 name_(name),
1381 value_(value),
Jooyung Han29813842020-12-08 01:28:03 +09001382 value_user_specified_(value != nullptr) {}
Daniel Norman85aed542019-08-21 12:01:14 -07001383
1384bool AidlEnumerator::CheckValid(const AidlTypeSpecifier& enum_backing_type) const {
1385 if (GetValue() == nullptr) {
1386 return false;
1387 }
1388 if (!GetValue()->CheckValid()) {
1389 return false;
1390 }
Will McVickerd7d18df2019-09-12 13:40:50 -07001391 if (GetValue()->ValueString(enum_backing_type, AidlConstantValueDecorator).empty()) {
Daniel Norman85aed542019-08-21 12:01:14 -07001392 AIDL_ERROR(this) << "Enumerator type differs from enum backing type.";
1393 return false;
1394 }
1395 return true;
1396}
1397
1398string AidlEnumerator::ValueString(const AidlTypeSpecifier& backing_type,
1399 const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -07001400 return GetValue()->ValueString(backing_type, decorator);
Daniel Norman85aed542019-08-21 12:01:14 -07001401}
1402
1403AidlEnumDeclaration::AidlEnumDeclaration(const AidlLocation& location, const std::string& name,
1404 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jooyung Han8451a202021-01-16 03:07:06 +09001405 const std::string& package, const Comments& comments)
Jooyung Han829ec7c2020-12-02 12:07:36 +09001406 : AidlDefinedType(location, name, comments, package, nullptr),
Jooyung Han29813842020-12-08 01:28:03 +09001407 enumerators_(std::move(*enumerators)) {
Jooyung Han672557b2020-12-24 05:18:00 +09001408 // Fill missing enumerator values with <prev + 1>
1409 // This can't be done in Autofill() because type/ref resolution depends on this.
1410 // For example, with enum E { A, B = A }, B's value 'A' is a reference which can't be
1411 // resolved if A has no value set.
Daniel Normanb28684e2019-10-17 15:31:39 -07001412 const AidlEnumerator* previous = nullptr;
1413 for (const auto& enumerator : enumerators_) {
1414 if (enumerator->GetValue() == nullptr) {
Jooyung Han29813842020-12-08 01:28:03 +09001415 auto loc = enumerator->GetLocation();
Daniel Normanb28684e2019-10-17 15:31:39 -07001416 if (previous == nullptr) {
Devin Mooredf93ebb2020-03-25 14:03:35 -07001417 enumerator->SetValue(
Jooyung Han29813842020-12-08 01:28:03 +09001418 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "0")));
Daniel Normanb28684e2019-10-17 15:31:39 -07001419 } else {
Jooyung Hand0c8af02021-01-06 18:08:01 +09001420 auto prev_value = std::make_unique<AidlConstantReference>(loc, previous->GetName());
Daniel Normanb28684e2019-10-17 15:31:39 -07001421 enumerator->SetValue(std::make_unique<AidlBinaryConstExpression>(
Jooyung Han29813842020-12-08 01:28:03 +09001422 loc, std::move(prev_value), "+",
1423 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "1"))));
Daniel Normanb28684e2019-10-17 15:31:39 -07001424 }
1425 }
1426 previous = enumerator.get();
1427 }
1428}
1429
Jooyung Han672557b2020-12-24 05:18:00 +09001430bool AidlEnumDeclaration::Autofill(const AidlTypenames& typenames) {
1431 if (auto annot = BackingType(); annot != nullptr) {
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001432 // Autofill() is called before the grand CheckValid(). But AidlAnnotation::ParamValue()
1433 // calls AidlConstantValue::evaluate() which requires CheckValid() to be called before. So we
Jooyung Han672557b2020-12-24 05:18:00 +09001434 // need to call CheckValid().
1435 if (!annot->CheckValid()) {
1436 return false;
1437 }
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001438 auto type = annot->ParamValue<std::string>("type").value();
Jooyung Hanaccd9192021-10-14 15:57:28 +09001439 backing_type_ = typenames.MakeResolvedType(annot->GetLocation(), type, false);
Jooyung Han672557b2020-12-24 05:18:00 +09001440 } else {
1441 // Default to byte type for enums.
Jooyung Hanaccd9192021-10-14 15:57:28 +09001442 backing_type_ = typenames.MakeResolvedType(GetLocation(), "byte", false);
Jooyung Han672557b2020-12-24 05:18:00 +09001443 }
Steven Morelandb248d072021-09-29 19:07:17 -07001444
1445 // we only support/test a few backing types, so make sure this is a supported
1446 // one (otherwise boolean might work, which isn't supported/tested in all
1447 // backends)
1448 static std::set<string> kBackingTypes = {"byte", "int", "long"};
1449 if (kBackingTypes.find(backing_type_->GetName()) == kBackingTypes.end()) {
1450 AIDL_ERROR(this) << "Invalid backing type: " << backing_type_->GetName()
1451 << ". Backing type must be one of: " << Join(kBackingTypes, ", ");
1452 return false;
Jooyung Han672557b2020-12-24 05:18:00 +09001453 }
1454 return true;
1455}
1456
Jooyung Han808a2a02020-12-28 16:46:54 +09001457bool AidlEnumDeclaration::CheckValid(const AidlTypenames& typenames) const {
1458 if (!AidlDefinedType::CheckValid(typenames)) {
Devin Moore24f68572020-02-26 13:20:59 -08001459 return false;
1460 }
Jooyung Han829ec7c2020-12-02 12:07:36 +09001461 if (!GetMembers().empty()) {
1462 AIDL_ERROR(this) << "Enum doesn't support fields/constants/methods.";
1463 return false;
1464 }
Daniel Norman85aed542019-08-21 12:01:14 -07001465 if (backing_type_ == nullptr) {
1466 AIDL_ERROR(this) << "Enum declaration missing backing type.";
1467 return false;
1468 }
1469 bool success = true;
1470 for (const auto& enumerator : enumerators_) {
1471 success = success && enumerator->CheckValid(GetBackingType());
1472 }
Jooyung Han3b990182020-12-22 17:44:31 +09001473
Jooyung Han808a2a02020-12-28 16:46:54 +09001474 return success;
Daniel Norman85aed542019-08-21 12:01:14 -07001475}
1476
Jooyung Han2946afc2020-10-05 20:29:16 +09001477AidlUnionDecl::AidlUnionDecl(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001478 const std::string& package, const Comments& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001479 std::vector<std::string>* type_params,
1480 std::vector<std::unique_ptr<AidlMember>>* members)
1481 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Jooyung Han2946afc2020-10-05 20:29:16 +09001482
Jooyung Han808a2a02020-12-28 16:46:54 +09001483bool AidlUnionDecl::CheckValid(const AidlTypenames& typenames) const {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001484 // visit parents
Jooyung Han808a2a02020-12-28 16:46:54 +09001485 if (!AidlParcelable::CheckValid(typenames)) {
Jooyung Hanfe89f122020-10-14 03:49:18 +09001486 return false;
1487 }
Jooyung Han59af9cc2020-10-25 21:44:14 +09001488
1489 // unions provide getters always
Jooyung Han829ec7c2020-12-02 12:07:36 +09001490 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001491 return false;
Jooyung Hanfe89f122020-10-14 03:49:18 +09001492 }
1493
1494 // now, visit self!
1495 bool success = true;
1496
1497 // TODO(b/170807936) do we need to allow ParcelableHolder in union?
1498 for (const auto& v : GetFields()) {
1499 if (v->GetType().GetName() == "ParcelableHolder") {
1500 AIDL_ERROR(*v) << "A union can't have a member of ParcelableHolder '" << v->GetName() << "'";
1501 success = false;
1502 }
1503 }
1504
Jooyung Hanfe89f122020-10-14 03:49:18 +09001505 if (GetFields().empty()) {
1506 AIDL_ERROR(*this) << "The union '" << this->GetName() << "' has no fields.";
1507 return false;
1508 }
1509
Jooyung Han53fb4242020-12-17 16:03:49 +09001510 // first member should have useful default value (implicit or explicit)
1511 const auto& first = GetFields()[0];
1512 if (!first->HasUsefulDefaultValue()) {
1513 // Most types can be initialized without a default value. For example,
1514 // interface types are inherently nullable. But, enum types should have
1515 // an explicit default value.
1516 if (!first->GetType().IsArray() && typenames.GetEnumDeclaration(first->GetType())) {
1517 AIDL_ERROR(first)
1518 << "The union's first member should have a useful default value. Enum types can be "
1519 "initialized with a reference. (e.g. ... = MyEnum.FOO;)";
1520 return false;
1521 }
1522 // In Java, array types are initialized as null without a default value. To be sure that default
1523 // initialized unions are accepted by other backends we require arrays also have a default
1524 // value.
1525 if (first->GetType().IsArray()) {
1526 AIDL_ERROR(first)
1527 << "The union's first member should have a useful default value. Arrays can be "
1528 "initialized with values(e.g. ... = { values... };) or marked as @nullable.";
1529 return false;
1530 }
1531 }
1532
Jooyung Hanfe89f122020-10-14 03:49:18 +09001533 return success;
1534}
1535
Steven Moreland46e9da82018-07-27 15:45:29 -07001536AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001537 const Comments& comments, bool oneway, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001538 std::vector<std::unique_ptr<AidlMember>>* members)
1539 : AidlDefinedType(location, name, comments, package, members) {
1540 for (auto& m : GetMethods()) {
1541 m.get()->ApplyInterfaceOneway(oneway);
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001542 }
Casey Dahlinfb7da2e2015-10-08 17:26:09 -07001543}
1544
Jooyung Han808a2a02020-12-28 16:46:54 +09001545bool AidlInterface::CheckValid(const AidlTypenames& typenames) const {
1546 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001547 return false;
1548 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001549 // Has to be a pointer due to deleting copy constructor. No idea why.
1550 map<string, const AidlMethod*> method_names;
1551 for (const auto& m : GetMethods()) {
Thiébaud Weksteenff6dafa2021-09-21 11:53:40 +02001552 if (!m->CheckValid(typenames)) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001553 return false;
1554 }
1555
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001556 auto it = method_names.find(m->GetName());
1557 // prevent duplicate methods
1558 if (it == method_names.end()) {
1559 method_names[m->GetName()] = m.get();
1560 } else {
1561 AIDL_ERROR(m) << "attempt to redefine method " << m->GetName() << ":";
1562 AIDL_ERROR(it->second) << "previously defined here.";
1563 return false;
1564 }
1565
Paul Trautrimb77048c2020-01-21 16:39:32 +09001566 static set<string> reserved_methods{"asBinder()", "getInterfaceHash()", "getInterfaceVersion()",
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001567 "getTransactionName(int)"};
1568
1569 if (reserved_methods.find(m->Signature()) != reserved_methods.end()) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001570 AIDL_ERROR(m) << " method " << m->Signature() << " is reserved for internal use.";
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001571 return false;
1572 }
1573 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001574
1575 bool success = true;
1576 set<string> constant_names;
Jooyung Han3f347ca2020-12-01 12:41:50 +09001577 for (const auto& constant : GetConstantDeclarations()) {
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001578 if (constant_names.count(constant->GetName()) > 0) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001579 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001580 success = false;
1581 }
1582 constant_names.insert(constant->GetName());
1583 success = success && constant->CheckValid(typenames);
1584 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001585 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001586}
1587
Jiyong Park27fd7fd2020-08-27 16:25:09 +09001588std::string AidlInterface::GetDescriptor() const {
1589 std::string annotatedDescriptor = AidlAnnotatable::GetDescriptor();
1590 if (annotatedDescriptor != "") {
1591 return annotatedDescriptor;
1592 }
1593 return GetCanonicalName();
1594}
1595
Jooyung Han13f1fa52021-06-11 18:06:12 +09001596AidlDocument::AidlDocument(const AidlLocation& location, const Comments& comments,
Jooyung Hancdf89ec2021-10-29 14:08:30 +09001597 std::set<string> imports,
Jooyung Han35784982021-06-29 06:26:12 +09001598 std::vector<std::unique_ptr<AidlDefinedType>> defined_types,
1599 bool is_preprocessed)
Jooyung Han13f1fa52021-06-11 18:06:12 +09001600 : AidlCommentable(location, comments),
Jooyung Han35784982021-06-29 06:26:12 +09001601 AidlScope(this),
Jooyung Han13f1fa52021-06-11 18:06:12 +09001602 imports_(std::move(imports)),
Jooyung Han35784982021-06-29 06:26:12 +09001603 defined_types_(std::move(defined_types)),
1604 is_preprocessed_(is_preprocessed) {
Jooyung Han13f1fa52021-06-11 18:06:12 +09001605 for (const auto& t : defined_types_) {
1606 t->SetEnclosingScope(this);
1607 }
1608}
1609
1610// Resolves type name in the current document.
1611// - built-in types
1612// - imported types
1613// - top-level type
1614std::string AidlDocument::ResolveName(const std::string& name) const {
1615 if (AidlTypenames::IsBuiltinTypename(name)) {
1616 return name;
1617 }
1618
1619 const auto first_dot = name.find_first_of('.');
1620 // For "Outer.Inner", we look up "Outer" in the import list.
Jooyung Han29813842020-12-08 01:28:03 +09001621 const std::string class_name =
Jooyung Han13f1fa52021-06-11 18:06:12 +09001622 (first_dot == std::string::npos) ? name : name.substr(0, first_dot);
1623 // Keep ".Inner", to make a fully-qualified name
1624 const std::string nested_type = (first_dot == std::string::npos) ? "" : name.substr(first_dot);
1625
Jooyung Han29813842020-12-08 01:28:03 +09001626 for (const auto& import : Imports()) {
Jooyung Hancdf89ec2021-10-29 14:08:30 +09001627 if (SimpleName(import) == class_name) {
1628 return import + nested_type;
Jooyung Han29813842020-12-08 01:28:03 +09001629 }
1630 }
Jooyung Han13f1fa52021-06-11 18:06:12 +09001631
1632 // check if it is a top-level type.
1633 for (const auto& type : DefinedTypes()) {
1634 if (type->GetName() == class_name) {
1635 return type->GetCanonicalName() + nested_type;
1636 }
Jooyung Han29813842020-12-08 01:28:03 +09001637 }
Jooyung Han13f1fa52021-06-11 18:06:12 +09001638
1639 // name itself might be fully-qualified name.
1640 return name;
Steven Moreland26318532020-12-23 20:08:36 +00001641}