blob: 441db231e1836dfe876fea70d96a1656bf85367d [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
Thiébaud Weksteenff6dafa2021-09-21 11:53:40 +0200982bool AidlMethod::CheckValid(const AidlTypenames& typenames) const {
983 if (!GetType().CheckValid(typenames)) {
984 return false;
985 }
986
987 // TODO(b/156872582): Support it when ParcelableHolder supports every backend.
988 if (GetType().GetName() == "ParcelableHolder") {
989 AIDL_ERROR(this) << "ParcelableHolder cannot be a return type";
990 return false;
991 }
992 if (IsOneway() && GetType().GetName() != "void") {
993 AIDL_ERROR(this) << "oneway method '" << GetName() << "' cannot return a value";
994 return false;
995 }
996
997 set<string> argument_names;
998 for (const auto& arg : GetArguments()) {
999 auto it = argument_names.find(arg->GetName());
1000 if (it != argument_names.end()) {
1001 AIDL_ERROR(this) << "method '" << GetName() << "' has duplicate argument name '"
1002 << arg->GetName() << "'";
1003 return false;
1004 }
1005 argument_names.insert(arg->GetName());
1006
1007 if (!arg->CheckValid(typenames)) {
1008 return false;
1009 }
1010
1011 if (IsOneway() && arg->IsOut()) {
1012 AIDL_ERROR(this) << "oneway method '" << this->GetName() << "' cannot have out parameters";
1013 return false;
1014 }
1015
1016 // check that the name doesn't match a keyword
1017 if (IsJavaKeyword(arg->GetName().c_str())) {
1018 AIDL_ERROR(arg) << "Argument name is a Java or aidl keyword";
1019 return false;
1020 }
1021
1022 // Reserve a namespace for internal use
1023 if (android::base::StartsWith(arg->GetName(), "_aidl")) {
1024 AIDL_ERROR(arg) << "Argument name cannot begin with '_aidl'";
1025 return false;
1026 }
1027
1028 if (arg->GetType().GetName() == "void") {
1029 AIDL_ERROR(arg->GetType()) << "'void' is an invalid type for the parameter '"
1030 << arg->GetName() << "'";
1031 return false;
1032 }
1033 }
1034 return true;
1035}
1036
Steven Moreland46e9da82018-07-27 15:45:29 -07001037AidlDefinedType::AidlDefinedType(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001038 const Comments& comments, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001039 std::vector<std::unique_ptr<AidlMember>>* members)
Jooyung Han2aedb112021-09-29 09:37:59 +09001040 : AidlMember(location, comments), AidlScope(this), name_(name), package_(package) {
Jooyung Han93f48f02021-06-05 00:11:16 +09001041 // adjust name/package when name is fully qualified (for preprocessed files)
1042 if (package_.empty() && name_.find('.') != std::string::npos) {
1043 // Note that this logic is absolutely wrong. Given a parcelable
1044 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
1045 // the class is just Bar. However, this was the way it was done in the past.
1046 //
1047 // See b/17415692
1048 auto pos = name.rfind('.');
1049 // name is the last part.
1050 name_ = name.substr(pos + 1);
1051 // package is the initial parts (except the last).
1052 package_ = name.substr(0, pos);
1053 }
Jooyung Han829ec7c2020-12-02 12:07:36 +09001054 if (members) {
1055 for (auto& m : *members) {
Jooyung Hanbaa71062021-09-29 09:06:03 +09001056 if (auto constant = AidlCast<AidlConstantDeclaration>(*m); constant) {
Jooyung Han829ec7c2020-12-02 12:07:36 +09001057 constants_.emplace_back(constant);
Jooyung Hanbaa71062021-09-29 09:06:03 +09001058 } else if (auto variable = AidlCast<AidlVariableDeclaration>(*m); variable) {
Jooyung Han829ec7c2020-12-02 12:07:36 +09001059 variables_.emplace_back(variable);
Jooyung Hanbaa71062021-09-29 09:06:03 +09001060 } else if (auto method = AidlCast<AidlMethod>(*m); method) {
Jooyung Han829ec7c2020-12-02 12:07:36 +09001061 methods_.emplace_back(method);
Jooyung Han2aedb112021-09-29 09:37:59 +09001062 } else if (auto type = AidlCast<AidlDefinedType>(*m); type) {
1063 type->SetEnclosingScope(this);
1064 types_.emplace_back(type);
Jooyung Han829ec7c2020-12-02 12:07:36 +09001065 } else {
Jooyung Hanbaa71062021-09-29 09:06:03 +09001066 AIDL_FATAL(*m) << "Unknown member type.";
Jooyung Han829ec7c2020-12-02 12:07:36 +09001067 }
1068 members_.push_back(m.release());
1069 }
1070 delete members;
1071 }
1072}
Steven Moreland787b0432018-07-03 09:00:58 -07001073
Jooyung Han808a2a02020-12-28 16:46:54 +09001074bool AidlDefinedType::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -08001075 if (!AidlAnnotatable::CheckValid(typenames)) {
1076 return false;
1077 }
Jooyung Han829ec7c2020-12-02 12:07:36 +09001078 if (!CheckValidWithMembers(typenames)) {
1079 return false;
1080 }
Devin Moore24f68572020-02-26 13:20:59 -08001081 return true;
1082}
1083
Steven Moreland787b0432018-07-03 09:00:58 -07001084std::string AidlDefinedType::GetCanonicalName() const {
1085 if (package_.empty()) {
1086 return GetName();
1087 }
Jooyung Han2aedb112021-09-29 09:37:59 +09001088 if (auto parent = GetParentType(); parent) {
1089 return parent->GetCanonicalName() + "." + GetName();
1090 }
Steven Moreland787b0432018-07-03 09:00:58 -07001091 return GetPackage() + "." + GetName();
1092}
1093
Jooyung Han829ec7c2020-12-02 12:07:36 +09001094bool AidlDefinedType::CheckValidWithMembers(const AidlTypenames& typenames) const {
1095 bool success = true;
1096
Jooyung Han2aedb112021-09-29 09:37:59 +09001097 for (const auto& t : GetNestedTypes()) {
1098 success = success && t->CheckValid(typenames);
1099 }
1100
Jooyung Han7fc5de02021-09-30 22:26:27 +09001101 if (auto parameterizable = AsParameterizable();
1102 parameterizable && parameterizable->IsGeneric() && !GetNestedTypes().empty()) {
1103 AIDL_ERROR(this) << "Generic types can't have nested types.";
1104 return false;
1105 }
1106
Jooyung Han2aedb112021-09-29 09:37:59 +09001107 std::set<std::string> nested_type_names;
1108 for (const auto& t : GetNestedTypes()) {
1109 bool duplicated = !nested_type_names.emplace(t->GetName()).second;
1110 if (duplicated) {
1111 AIDL_ERROR(t) << "Redefinition of '" << t->GetName() << "'.";
1112 success = false;
1113 }
1114 // nested type can't have a parent name
1115 if (t->GetName() == GetName()) {
1116 AIDL_ERROR(t) << "Nested type '" << GetName() << "' has the same name as its parent.";
1117 success = false;
1118 }
Jooyung Han2b1487d2021-09-30 09:57:01 +09001119 // Having unstructured parcelables as nested types doesn't make sense because they are defined
1120 // somewhere else in native languages (e.g. C++, Java...).
1121 if (AidlCast<AidlParcelable>(*t)) {
1122 AIDL_ERROR(t) << "'" << t->GetName()
1123 << "' is nested. Unstructured parcelables should be at the root scope.";
1124 return false;
1125 }
Jooyung Han2aedb112021-09-29 09:37:59 +09001126 // For now we don't allow "interface" to be nested
1127 if (AidlCast<AidlInterface>(*t)) {
1128 AIDL_ERROR(t) << "'" << t->GetName()
1129 << "' is nested. Interfaces should be at the root scope.";
1130 return false;
1131 }
1132 }
1133
Jooyung Han8e9ae872021-10-13 02:52:25 +09001134 if (!TopologicalVisit(GetNestedTypes(), [](auto&) {})) {
1135 AIDL_ERROR(this) << GetName()
1136 << " has nested types with cyclic references. C++ and NDK backends don't "
1137 "support cyclic references.";
1138 return false;
1139 }
1140
Jooyung Han829ec7c2020-12-02 12:07:36 +09001141 for (const auto& v : GetFields()) {
1142 const bool field_valid = v->CheckValid(typenames);
1143 success = success && field_valid;
1144 }
1145
1146 // field names should be unique
1147 std::set<std::string> fieldnames;
1148 for (const auto& v : GetFields()) {
1149 bool duplicated = !fieldnames.emplace(v->GetName()).second;
1150 if (duplicated) {
1151 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName() << "'";
1152 success = false;
1153 }
1154 }
1155
1156 // immutable parcelables should have immutable fields.
1157 if (IsJavaOnlyImmutable()) {
1158 for (const auto& v : GetFields()) {
1159 if (!typenames.CanBeJavaOnlyImmutable(v->GetType())) {
1160 AIDL_ERROR(v) << "The @JavaOnlyImmutable '" << GetName() << "' has a "
1161 << "non-immutable field named '" << v->GetName() << "'.";
1162 success = false;
1163 }
1164 }
1165 }
1166
1167 set<string> constant_names;
1168 for (const auto& constant : GetConstantDeclarations()) {
1169 if (constant_names.count(constant->GetName()) > 0) {
1170 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
1171 success = false;
1172 }
1173 constant_names.insert(constant->GetName());
1174 success = success && constant->CheckValid(typenames);
1175 }
1176
1177 return success;
1178}
1179
1180bool AidlDefinedType::CheckValidForGetterNames() const {
1181 bool success = true;
1182 std::set<std::string> getters;
1183 for (const auto& v : GetFields()) {
1184 bool duplicated = !getters.emplace(v->GetCapitalizedName()).second;
1185 if (duplicated) {
1186 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName()
1187 << "' after capitalizing the first letter";
1188 success = false;
1189 }
1190 }
1191 return success;
1192}
1193
Jooyung Han2aedb112021-09-29 09:37:59 +09001194const AidlDefinedType* AidlDefinedType::GetParentType() const {
1195 AIDL_FATAL_IF(GetEnclosingScope() == nullptr, this) << "Scope is not set.";
1196 return AidlCast<AidlDefinedType>(GetEnclosingScope()->GetNode());
1197}
1198
Jooyung Hanf8c39632021-10-05 09:56:29 +09001199const AidlDefinedType* AidlDefinedType::GetRootType() const {
1200 const AidlDefinedType* root = this;
1201 for (auto parent = root->GetParentType(); parent; parent = parent->GetParentType()) {
1202 root = parent;
1203 }
1204 return root;
1205}
1206
Jooyung Han2aedb112021-09-29 09:37:59 +09001207// Resolve `name` in the current scope. If not found, delegate to the parent
Jooyung Han13f1fa52021-06-11 18:06:12 +09001208std::string AidlDefinedType::ResolveName(const std::string& name) const {
Jooyung Han2aedb112021-09-29 09:37:59 +09001209 // For example, in the following, t1's type Baz means x.Foo.Bar.Baz
1210 // while t2's type is y.Baz.
Jooyung Han13f1fa52021-06-11 18:06:12 +09001211 // package x;
Jooyung Han2aedb112021-09-29 09:37:59 +09001212 // import y.Baz;
Jooyung Han13f1fa52021-06-11 18:06:12 +09001213 // parcelable Foo {
1214 // parcelable Bar {
Jooyung Han2aedb112021-09-29 09:37:59 +09001215 // enum Baz { ... }
1216 // Baz t1; // -> should be x.Foo.Bar.Baz
Jooyung Han13f1fa52021-06-11 18:06:12 +09001217 // }
Jooyung Han2aedb112021-09-29 09:37:59 +09001218 // Baz t2; // -> should be y.Baz
1219 // Bar.Baz t3; // -> should be x.Foo.Bar.Baz
Jooyung Han13f1fa52021-06-11 18:06:12 +09001220 // }
1221 AIDL_FATAL_IF(!GetEnclosingScope(), this)
1222 << "Type should have an enclosing scope.(e.g. AidlDocument)";
Jooyung Han2aedb112021-09-29 09:37:59 +09001223 if (AidlTypenames::IsBuiltinTypename(name)) {
1224 return name;
1225 }
1226
1227 const auto first_dot = name.find_first_of('.');
1228 // For "Outer.Inner", we look up "Outer" in the import list.
1229 const std::string class_name =
1230 (first_dot == std::string::npos) ? name : name.substr(0, first_dot);
1231 // Keep ".Inner", to make a fully-qualified name
1232 const std::string nested_type = (first_dot == std::string::npos) ? "" : name.substr(first_dot);
1233
1234 // check if it is a nested type
1235 for (const auto& type : GetNestedTypes()) {
1236 if (type->GetName() == class_name) {
1237 return type->GetCanonicalName() + nested_type;
1238 }
1239 }
1240
Jooyung Han13f1fa52021-06-11 18:06:12 +09001241 return GetEnclosingScope()->ResolveName(name);
1242}
1243
Jooyung Hanbaa71062021-09-29 09:06:03 +09001244template <>
1245const AidlDefinedType* AidlCast<AidlDefinedType>(const AidlNode& node) {
1246 struct Visitor : AidlVisitor {
1247 const AidlDefinedType* defined_type = nullptr;
1248 void Visit(const AidlInterface& t) override { defined_type = &t; }
1249 void Visit(const AidlEnumDeclaration& t) override { defined_type = &t; }
1250 void Visit(const AidlStructuredParcelable& t) override { defined_type = &t; }
1251 void Visit(const AidlUnionDecl& t) override { defined_type = &t; }
1252 void Visit(const AidlParcelable& t) override { defined_type = &t; }
1253 } v;
1254 node.DispatchVisit(v);
1255 return v.defined_type;
Jooyung Han35784982021-06-29 06:26:12 +09001256}
1257
1258const AidlDocument& AidlDefinedType::GetDocument() const {
Jooyung Hanf8c39632021-10-05 09:56:29 +09001259 const AidlDefinedType* root = GetRootType();
1260 auto scope = root->GetEnclosingScope();
Jooyung Han35784982021-06-29 06:26:12 +09001261 AIDL_FATAL_IF(!scope, this) << "no scope defined.";
1262 auto doc = AidlCast<AidlDocument>(scope->GetNode());
Jooyung Hanf8c39632021-10-05 09:56:29 +09001263 AIDL_FATAL_IF(!doc, this) << "root scope is not a document.";
Jooyung Han35784982021-06-29 06:26:12 +09001264 return *doc;
1265}
1266
Jiyong Park18132182020-06-08 20:24:40 +09001267AidlParcelable::AidlParcelable(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001268 const std::string& package, const Comments& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001269 const std::string& cpp_header, std::vector<std::string>* type_params,
1270 std::vector<std::unique_ptr<AidlMember>>* members)
1271 : AidlDefinedType(location, name, comments, package, members),
Jeongik Chadf76dc72019-11-28 00:08:47 +09001272 AidlParameterizable<std::string>(type_params),
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -08001273 cpp_header_(cpp_header) {
1274 // Strip off quotation marks if we actually have a cpp header.
1275 if (cpp_header_.length() >= 2) {
1276 cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2);
1277 }
Casey Dahlin59401da2015-10-09 18:16:45 -07001278}
Jeongik Chadf76dc72019-11-28 00:08:47 +09001279
1280template <typename T>
1281bool AidlParameterizable<T>::CheckValid() const {
1282 return true;
1283};
1284
1285template <>
1286bool AidlParameterizable<std::string>::CheckValid() const {
1287 if (!IsGeneric()) {
1288 return true;
1289 }
1290 std::unordered_set<std::string> set(GetTypeParameters().begin(), GetTypeParameters().end());
1291 if (set.size() != GetTypeParameters().size()) {
1292 AIDL_ERROR(this->AsAidlNode()) << "Every type parameter should be unique.";
1293 return false;
1294 }
1295 return true;
1296}
Casey Dahlin59401da2015-10-09 18:16:45 -07001297
Jooyung Han808a2a02020-12-28 16:46:54 +09001298bool AidlParcelable::CheckValid(const AidlTypenames& typenames) const {
1299 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001300 return false;
1301 }
Jeongik Chadf76dc72019-11-28 00:08:47 +09001302 if (!AidlParameterizable<std::string>::CheckValid()) {
1303 return false;
1304 }
Jeongik Cha82317dd2019-02-27 20:26:11 +09001305
1306 return true;
1307}
1308
Steven Moreland5557f1c2018-07-02 13:50:23 -07001309AidlStructuredParcelable::AidlStructuredParcelable(
Jiyong Park18132182020-06-08 20:24:40 +09001310 const AidlLocation& location, const std::string& name, const std::string& package,
Jooyung Han8451a202021-01-16 03:07:06 +09001311 const Comments& comments, std::vector<std::string>* type_params,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001312 std::vector<std::unique_ptr<AidlMember>>* members)
1313 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Steven Moreland5557f1c2018-07-02 13:50:23 -07001314
Jooyung Han808a2a02020-12-28 16:46:54 +09001315bool AidlStructuredParcelable::CheckValid(const AidlTypenames& typenames) const {
1316 if (!AidlParcelable::CheckValid(typenames)) {
Devin Moore24f68572020-02-26 13:20:59 -08001317 return false;
1318 }
Jeongik Cha13066da2020-08-06 15:43:19 +09001319
Jooyung Han59af9cc2020-10-25 21:44:14 +09001320 bool success = true;
Jeongik Cha36f76c32020-07-28 00:25:52 +09001321
Jooyung Hand4057c42020-10-23 13:28:22 +09001322 if (IsFixedSize()) {
1323 for (const auto& v : GetFields()) {
1324 if (!typenames.CanBeFixedSize(v->GetType())) {
1325 AIDL_ERROR(v) << "The @FixedSize parcelable '" << this->GetName() << "' has a "
1326 << "non-fixed size field named " << v->GetName() << ".";
1327 success = false;
1328 }
1329 }
1330 }
1331
1332 if (IsJavaOnlyImmutable()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001333 // Immutable parcelables provide getters
Jooyung Han829ec7c2020-12-02 12:07:36 +09001334 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001335 success = false;
Jooyung Hand4057c42020-10-23 13:28:22 +09001336 }
1337 }
1338
Daniel Norman85aed542019-08-21 12:01:14 -07001339 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001340}
1341
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001342// TODO: we should treat every backend all the same in future.
Jooyung Hanb4997aa2021-10-16 03:26:12 +09001343bool AidlTypeSpecifier::LanguageSpecificCheckValid(Options::Language lang) const {
Andrei Homescub62afd92020-05-11 19:24:59 -07001344 if (this->GetName() == "FileDescriptor" &&
1345 (lang == Options::Language::NDK || lang == Options::Language::RUST)) {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001346 AIDL_ERROR(this) << "FileDescriptor isn't supported by the " << to_string(lang) << " backend.";
Steven Morelandc8a4ca82020-01-21 17:50:08 -08001347 return false;
1348 }
Devin Moore6a01ca12020-08-28 10:24:19 -07001349
Devin Moore6a01ca12020-08-28 10:24:19 -07001350 if (lang != Options::Language::JAVA) {
1351 if (this->GetName() == "List" && !this->IsGeneric()) {
1352 AIDL_ERROR(this) << "Currently, only the Java backend supports non-generic List.";
1353 return false;
1354 }
1355 if (this->GetName() == "Map" || this->GetName() == "CharSequence") {
1356 AIDL_ERROR(this) << "Currently, only Java backend supports " << this->GetName() << ".";
1357 return false;
Jeongik Cha08ca2182019-11-21 14:01:13 +09001358 }
1359 }
1360
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001361 return true;
1362}
1363
1364// TODO: we should treat every backend all the same in future.
Jooyung Hanb4997aa2021-10-16 03:26:12 +09001365bool AidlDefinedType::LanguageSpecificCheckValid(Options::Language lang) const {
Jooyung Han589cfb02021-09-28 17:26:04 +09001366 struct Visitor : AidlVisitor {
Jooyung Hanb4997aa2021-10-16 03:26:12 +09001367 Visitor(Options::Language lang) : lang(lang) {}
Jooyung Han589cfb02021-09-28 17:26:04 +09001368 void Visit(const AidlTypeSpecifier& type) override {
Jooyung Hanb4997aa2021-10-16 03:26:12 +09001369 success = success && type.LanguageSpecificCheckValid(lang);
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001370 }
Jooyung Han589cfb02021-09-28 17:26:04 +09001371 Options::Language lang;
1372 bool success = true;
Jooyung Hanb4997aa2021-10-16 03:26:12 +09001373 } v(lang);
Jooyung Han589cfb02021-09-28 17:26:04 +09001374 VisitTopDown(v, *this);
1375 return v.success;
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001376}
1377
Daniel Norman85aed542019-08-21 12:01:14 -07001378AidlEnumerator::AidlEnumerator(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001379 AidlConstantValue* value, const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +09001380 : AidlCommentable(location, comments),
Jooyung Han29813842020-12-08 01:28:03 +09001381 name_(name),
1382 value_(value),
Jooyung Han29813842020-12-08 01:28:03 +09001383 value_user_specified_(value != nullptr) {}
Daniel Norman85aed542019-08-21 12:01:14 -07001384
1385bool AidlEnumerator::CheckValid(const AidlTypeSpecifier& enum_backing_type) const {
1386 if (GetValue() == nullptr) {
1387 return false;
1388 }
1389 if (!GetValue()->CheckValid()) {
1390 return false;
1391 }
Will McVickerd7d18df2019-09-12 13:40:50 -07001392 if (GetValue()->ValueString(enum_backing_type, AidlConstantValueDecorator).empty()) {
Daniel Norman85aed542019-08-21 12:01:14 -07001393 AIDL_ERROR(this) << "Enumerator type differs from enum backing type.";
1394 return false;
1395 }
1396 return true;
1397}
1398
1399string AidlEnumerator::ValueString(const AidlTypeSpecifier& backing_type,
1400 const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -07001401 return GetValue()->ValueString(backing_type, decorator);
Daniel Norman85aed542019-08-21 12:01:14 -07001402}
1403
1404AidlEnumDeclaration::AidlEnumDeclaration(const AidlLocation& location, const std::string& name,
1405 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jooyung Han8451a202021-01-16 03:07:06 +09001406 const std::string& package, const Comments& comments)
Jooyung Han829ec7c2020-12-02 12:07:36 +09001407 : AidlDefinedType(location, name, comments, package, nullptr),
Jooyung Han29813842020-12-08 01:28:03 +09001408 enumerators_(std::move(*enumerators)) {
Jooyung Han672557b2020-12-24 05:18:00 +09001409 // Fill missing enumerator values with <prev + 1>
1410 // This can't be done in Autofill() because type/ref resolution depends on this.
1411 // For example, with enum E { A, B = A }, B's value 'A' is a reference which can't be
1412 // resolved if A has no value set.
Daniel Normanb28684e2019-10-17 15:31:39 -07001413 const AidlEnumerator* previous = nullptr;
1414 for (const auto& enumerator : enumerators_) {
1415 if (enumerator->GetValue() == nullptr) {
Jooyung Han29813842020-12-08 01:28:03 +09001416 auto loc = enumerator->GetLocation();
Daniel Normanb28684e2019-10-17 15:31:39 -07001417 if (previous == nullptr) {
Devin Mooredf93ebb2020-03-25 14:03:35 -07001418 enumerator->SetValue(
Jooyung Han29813842020-12-08 01:28:03 +09001419 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "0")));
Daniel Normanb28684e2019-10-17 15:31:39 -07001420 } else {
Jooyung Hand0c8af02021-01-06 18:08:01 +09001421 auto prev_value = std::make_unique<AidlConstantReference>(loc, previous->GetName());
Daniel Normanb28684e2019-10-17 15:31:39 -07001422 enumerator->SetValue(std::make_unique<AidlBinaryConstExpression>(
Jooyung Han29813842020-12-08 01:28:03 +09001423 loc, std::move(prev_value), "+",
1424 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "1"))));
Daniel Normanb28684e2019-10-17 15:31:39 -07001425 }
1426 }
1427 previous = enumerator.get();
1428 }
1429}
1430
Jooyung Han672557b2020-12-24 05:18:00 +09001431bool AidlEnumDeclaration::Autofill(const AidlTypenames& typenames) {
1432 if (auto annot = BackingType(); annot != nullptr) {
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001433 // Autofill() is called before the grand CheckValid(). But AidlAnnotation::ParamValue()
1434 // calls AidlConstantValue::evaluate() which requires CheckValid() to be called before. So we
Jooyung Han672557b2020-12-24 05:18:00 +09001435 // need to call CheckValid().
1436 if (!annot->CheckValid()) {
1437 return false;
1438 }
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001439 auto type = annot->ParamValue<std::string>("type").value();
Jooyung Hanaccd9192021-10-14 15:57:28 +09001440 backing_type_ = typenames.MakeResolvedType(annot->GetLocation(), type, false);
Jooyung Han672557b2020-12-24 05:18:00 +09001441 } else {
1442 // Default to byte type for enums.
Jooyung Hanaccd9192021-10-14 15:57:28 +09001443 backing_type_ = typenames.MakeResolvedType(GetLocation(), "byte", false);
Jooyung Han672557b2020-12-24 05:18:00 +09001444 }
Steven Morelandb248d072021-09-29 19:07:17 -07001445
1446 // we only support/test a few backing types, so make sure this is a supported
1447 // one (otherwise boolean might work, which isn't supported/tested in all
1448 // backends)
1449 static std::set<string> kBackingTypes = {"byte", "int", "long"};
1450 if (kBackingTypes.find(backing_type_->GetName()) == kBackingTypes.end()) {
1451 AIDL_ERROR(this) << "Invalid backing type: " << backing_type_->GetName()
1452 << ". Backing type must be one of: " << Join(kBackingTypes, ", ");
1453 return false;
Jooyung Han672557b2020-12-24 05:18:00 +09001454 }
1455 return true;
1456}
1457
Jooyung Han808a2a02020-12-28 16:46:54 +09001458bool AidlEnumDeclaration::CheckValid(const AidlTypenames& typenames) const {
1459 if (!AidlDefinedType::CheckValid(typenames)) {
Devin Moore24f68572020-02-26 13:20:59 -08001460 return false;
1461 }
Jooyung Han829ec7c2020-12-02 12:07:36 +09001462 if (!GetMembers().empty()) {
1463 AIDL_ERROR(this) << "Enum doesn't support fields/constants/methods.";
1464 return false;
1465 }
Daniel Norman85aed542019-08-21 12:01:14 -07001466 if (backing_type_ == nullptr) {
1467 AIDL_ERROR(this) << "Enum declaration missing backing type.";
1468 return false;
1469 }
1470 bool success = true;
1471 for (const auto& enumerator : enumerators_) {
1472 success = success && enumerator->CheckValid(GetBackingType());
1473 }
Jooyung Han3b990182020-12-22 17:44:31 +09001474
Jooyung Han808a2a02020-12-28 16:46:54 +09001475 return success;
Daniel Norman85aed542019-08-21 12:01:14 -07001476}
1477
Jooyung Han2946afc2020-10-05 20:29:16 +09001478AidlUnionDecl::AidlUnionDecl(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001479 const std::string& package, const Comments& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001480 std::vector<std::string>* type_params,
1481 std::vector<std::unique_ptr<AidlMember>>* members)
1482 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Jooyung Han2946afc2020-10-05 20:29:16 +09001483
Jooyung Han808a2a02020-12-28 16:46:54 +09001484bool AidlUnionDecl::CheckValid(const AidlTypenames& typenames) const {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001485 // visit parents
Jooyung Han808a2a02020-12-28 16:46:54 +09001486 if (!AidlParcelable::CheckValid(typenames)) {
Jooyung Hanfe89f122020-10-14 03:49:18 +09001487 return false;
1488 }
Jooyung Han59af9cc2020-10-25 21:44:14 +09001489
1490 // unions provide getters always
Jooyung Han829ec7c2020-12-02 12:07:36 +09001491 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001492 return false;
Jooyung Hanfe89f122020-10-14 03:49:18 +09001493 }
1494
1495 // now, visit self!
1496 bool success = true;
1497
1498 // TODO(b/170807936) do we need to allow ParcelableHolder in union?
1499 for (const auto& v : GetFields()) {
1500 if (v->GetType().GetName() == "ParcelableHolder") {
1501 AIDL_ERROR(*v) << "A union can't have a member of ParcelableHolder '" << v->GetName() << "'";
1502 success = false;
1503 }
1504 }
1505
Jooyung Hanfe89f122020-10-14 03:49:18 +09001506 if (GetFields().empty()) {
1507 AIDL_ERROR(*this) << "The union '" << this->GetName() << "' has no fields.";
1508 return false;
1509 }
1510
Jooyung Han53fb4242020-12-17 16:03:49 +09001511 // first member should have useful default value (implicit or explicit)
1512 const auto& first = GetFields()[0];
1513 if (!first->HasUsefulDefaultValue()) {
1514 // Most types can be initialized without a default value. For example,
1515 // interface types are inherently nullable. But, enum types should have
1516 // an explicit default value.
1517 if (!first->GetType().IsArray() && typenames.GetEnumDeclaration(first->GetType())) {
1518 AIDL_ERROR(first)
1519 << "The union's first member should have a useful default value. Enum types can be "
1520 "initialized with a reference. (e.g. ... = MyEnum.FOO;)";
1521 return false;
1522 }
1523 // In Java, array types are initialized as null without a default value. To be sure that default
1524 // initialized unions are accepted by other backends we require arrays also have a default
1525 // value.
1526 if (first->GetType().IsArray()) {
1527 AIDL_ERROR(first)
1528 << "The union's first member should have a useful default value. Arrays can be "
1529 "initialized with values(e.g. ... = { values... };) or marked as @nullable.";
1530 return false;
1531 }
1532 }
1533
Jooyung Hanfe89f122020-10-14 03:49:18 +09001534 return success;
1535}
1536
Steven Moreland46e9da82018-07-27 15:45:29 -07001537AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001538 const Comments& comments, bool oneway, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001539 std::vector<std::unique_ptr<AidlMember>>* members)
1540 : AidlDefinedType(location, name, comments, package, members) {
1541 for (auto& m : GetMethods()) {
1542 m.get()->ApplyInterfaceOneway(oneway);
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001543 }
Casey Dahlinfb7da2e2015-10-08 17:26:09 -07001544}
1545
Jooyung Han808a2a02020-12-28 16:46:54 +09001546bool AidlInterface::CheckValid(const AidlTypenames& typenames) const {
1547 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001548 return false;
1549 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001550 // Has to be a pointer due to deleting copy constructor. No idea why.
1551 map<string, const AidlMethod*> method_names;
1552 for (const auto& m : GetMethods()) {
Thiébaud Weksteenff6dafa2021-09-21 11:53:40 +02001553 if (!m->CheckValid(typenames)) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001554 return false;
1555 }
1556
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001557 auto it = method_names.find(m->GetName());
1558 // prevent duplicate methods
1559 if (it == method_names.end()) {
1560 method_names[m->GetName()] = m.get();
1561 } else {
1562 AIDL_ERROR(m) << "attempt to redefine method " << m->GetName() << ":";
1563 AIDL_ERROR(it->second) << "previously defined here.";
1564 return false;
1565 }
1566
Paul Trautrimb77048c2020-01-21 16:39:32 +09001567 static set<string> reserved_methods{"asBinder()", "getInterfaceHash()", "getInterfaceVersion()",
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001568 "getTransactionName(int)"};
1569
1570 if (reserved_methods.find(m->Signature()) != reserved_methods.end()) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001571 AIDL_ERROR(m) << " method " << m->Signature() << " is reserved for internal use.";
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001572 return false;
1573 }
1574 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001575
1576 bool success = true;
1577 set<string> constant_names;
Jooyung Han3f347ca2020-12-01 12:41:50 +09001578 for (const auto& constant : GetConstantDeclarations()) {
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001579 if (constant_names.count(constant->GetName()) > 0) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001580 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001581 success = false;
1582 }
1583 constant_names.insert(constant->GetName());
1584 success = success && constant->CheckValid(typenames);
1585 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001586 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001587}
1588
Jiyong Park27fd7fd2020-08-27 16:25:09 +09001589std::string AidlInterface::GetDescriptor() const {
1590 std::string annotatedDescriptor = AidlAnnotatable::GetDescriptor();
1591 if (annotatedDescriptor != "") {
1592 return annotatedDescriptor;
1593 }
1594 return GetCanonicalName();
1595}
1596
Jooyung Han132cf802021-01-15 02:17:32 +09001597AidlImport::AidlImport(const AidlLocation& location, const std::string& needed_class,
Jooyung Han8451a202021-01-16 03:07:06 +09001598 const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +09001599 : AidlNode(location, comments), needed_class_(needed_class) {}
Jooyung Han29813842020-12-08 01:28:03 +09001600
Jooyung Han13f1fa52021-06-11 18:06:12 +09001601AidlDocument::AidlDocument(const AidlLocation& location, const Comments& comments,
1602 std::vector<std::unique_ptr<AidlImport>> imports,
Jooyung Han35784982021-06-29 06:26:12 +09001603 std::vector<std::unique_ptr<AidlDefinedType>> defined_types,
1604 bool is_preprocessed)
Jooyung Han13f1fa52021-06-11 18:06:12 +09001605 : AidlCommentable(location, comments),
Jooyung Han35784982021-06-29 06:26:12 +09001606 AidlScope(this),
Jooyung Han13f1fa52021-06-11 18:06:12 +09001607 imports_(std::move(imports)),
Jooyung Han35784982021-06-29 06:26:12 +09001608 defined_types_(std::move(defined_types)),
1609 is_preprocessed_(is_preprocessed) {
Jooyung Han13f1fa52021-06-11 18:06:12 +09001610 for (const auto& t : defined_types_) {
1611 t->SetEnclosingScope(this);
1612 }
1613}
1614
1615// Resolves type name in the current document.
1616// - built-in types
1617// - imported types
1618// - top-level type
1619std::string AidlDocument::ResolveName(const std::string& name) const {
1620 if (AidlTypenames::IsBuiltinTypename(name)) {
1621 return name;
1622 }
1623
1624 const auto first_dot = name.find_first_of('.');
1625 // For "Outer.Inner", we look up "Outer" in the import list.
Jooyung Han29813842020-12-08 01:28:03 +09001626 const std::string class_name =
Jooyung Han13f1fa52021-06-11 18:06:12 +09001627 (first_dot == std::string::npos) ? name : name.substr(0, first_dot);
1628 // Keep ".Inner", to make a fully-qualified name
1629 const std::string nested_type = (first_dot == std::string::npos) ? "" : name.substr(first_dot);
1630
Jooyung Han29813842020-12-08 01:28:03 +09001631 for (const auto& import : Imports()) {
Jooyung Han13f1fa52021-06-11 18:06:12 +09001632 if (import->SimpleName() == class_name) {
1633 return import->GetNeededClass() + nested_type;
Jooyung Han29813842020-12-08 01:28:03 +09001634 }
1635 }
Jooyung Han13f1fa52021-06-11 18:06:12 +09001636
1637 // check if it is a top-level type.
1638 for (const auto& type : DefinedTypes()) {
1639 if (type->GetName() == class_name) {
1640 return type->GetCanonicalName() + nested_type;
1641 }
Jooyung Han29813842020-12-08 01:28:03 +09001642 }
Jooyung Han13f1fa52021-06-11 18:06:12 +09001643
1644 // name itself might be fully-qualified name.
1645 return name;
Steven Moreland26318532020-12-23 20:08:36 +00001646}