blob: 0cbef0bfdfb2dce033bf424fe7b6bcd62342fa6c [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>
Jiyong Park68bc77a2018-07-19 19:00:45 +090024#include <algorithm>
Jiyong Park1deecc32018-07-17 01:14:41 +090025#include <iostream>
Jiyong Park68bc77a2018-07-19 19:00:45 +090026#include <set>
27#include <sstream>
Casey Dahlindd691812015-09-09 17:59:06 -070028#include <string>
Jiyong Park1deecc32018-07-17 01:14:41 +090029#include <utility>
Christopher Wileyf690be52015-09-14 15:19:10 -070030
Steven Moreland1c4ba202018-08-09 10:49:54 -070031#include <android-base/parsedouble.h>
Roshan Pius9d7810a2016-07-28 08:57:50 -070032#include <android-base/parseint.h>
Elliott Hughes0a620672015-12-04 13:53:18 -080033#include <android-base/strings.h>
Christopher Wileyd76067c2015-10-19 17:00:13 -070034
Steven Moreland21780812020-09-11 01:29:45 +000035#include "aidl_language_y.h"
Jooyung Hand4fe00e2021-01-11 16:21:53 +090036#include "comments.h"
Christopher Wiley4a2884b2015-10-07 11:27:45 -070037#include "logging.h"
Adam Lesinskiffa16862014-01-23 18:17:42 -080038
Will McVickerd7d18df2019-09-12 13:40:50 -070039#include "aidl.h"
40
Casey Dahlin07b9dde2015-09-10 19:13:49 -070041#ifdef _WIN32
42int isatty(int fd)
43{
44 return (fd == 0);
45}
46#endif
47
Christopher Wiley4a2884b2015-10-07 11:27:45 -070048using android::aidl::IoDelegate;
Christopher Wileyd76067c2015-10-19 17:00:13 -070049using android::base::Join;
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -080050using android::base::Split;
Casey Dahlindd691812015-09-09 17:59:06 -070051using std::cerr;
Jiyong Park1deecc32018-07-17 01:14:41 +090052using std::pair;
Jiyong Park68bc77a2018-07-19 19:00:45 +090053using std::set;
Christopher Wiley4a2884b2015-10-07 11:27:45 -070054using std::string;
55using std::unique_ptr;
Jiyong Parkccf00f82018-07-17 01:39:23 +090056using std::vector;
Adam Lesinskiffa16862014-01-23 18:17:42 -080057
Jeongik Cha047c5ee2019-08-07 23:16:49 +090058namespace {
Jeongik Cha997281d2020-01-16 15:23:59 +090059bool IsJavaKeyword(const char* str) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +090060 static const std::vector<std::string> kJavaKeywords{
61 "abstract", "assert", "boolean", "break", "byte", "case", "catch",
62 "char", "class", "const", "continue", "default", "do", "double",
63 "else", "enum", "extends", "final", "finally", "float", "for",
64 "goto", "if", "implements", "import", "instanceof", "int", "interface",
65 "long", "native", "new", "package", "private", "protected", "public",
66 "return", "short", "static", "strictfp", "super", "switch", "synchronized",
67 "this", "throw", "throws", "transient", "try", "void", "volatile",
68 "while", "true", "false", "null",
69 };
70 return std::find(kJavaKeywords.begin(), kJavaKeywords.end(), str) != kJavaKeywords.end();
71}
72} // namespace
73
Jooyung Han8451a202021-01-16 03:07:06 +090074AidlNode::AidlNode(const AidlLocation& location, const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +090075 : location_(location), comments_(comments) {}
Steven Moreland46e9da82018-07-27 15:45:29 -070076
Mathew Inwoodadb74672019-11-29 14:01:53 +000077std::string AidlNode::PrintLine() const {
Andrei Onea8714b022019-02-01 18:55:54 +000078 std::stringstream ss;
79 ss << location_.file_ << ":" << location_.begin_.line;
80 return ss.str();
81}
82
Mathew Inwoodadb74672019-11-29 14:01:53 +000083std::string AidlNode::PrintLocation() const {
84 std::stringstream ss;
85 ss << location_.file_ << ":" << location_.begin_.line << ":" << location_.begin_.column << ":"
86 << location_.end_.line << ":" << location_.end_.column;
87 return ss.str();
88}
89
Jooyung Han8451a202021-01-16 03:07:06 +090090static const AidlTypeSpecifier kStringType{AIDL_LOCATION_HERE, "String", false, nullptr,
91 Comments{}};
92static const AidlTypeSpecifier kStringArrayType{AIDL_LOCATION_HERE, "String", true, nullptr,
93 Comments{}};
94static const AidlTypeSpecifier kIntType{AIDL_LOCATION_HERE, "int", false, nullptr, Comments{}};
95static const AidlTypeSpecifier kLongType{AIDL_LOCATION_HERE, "long", false, nullptr, Comments{}};
96static const AidlTypeSpecifier kBooleanType{AIDL_LOCATION_HERE, "boolean", false, nullptr,
97 Comments{}};
Jooyung Han5c2fcae2020-12-26 00:04:39 +090098
Steven Moreland0cea4aa2020-04-20 21:06:02 -070099const std::vector<AidlAnnotation::Schema>& AidlAnnotation::AllSchemas() {
100 static const std::vector<Schema> kSchemas{
Jooyung Han01720ed2021-08-13 07:46:07 +0900101 {AidlAnnotation::Type::NULLABLE,
102 "nullable",
103 CONTEXT_TYPE_SPECIFIER,
104 {{"heap", kBooleanType}}},
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900105 {AidlAnnotation::Type::UTF8_IN_CPP, "utf8InCpp", CONTEXT_TYPE_SPECIFIER, {}},
106 {AidlAnnotation::Type::SENSITIVE_DATA, "SensitiveData", CONTEXT_TYPE_INTERFACE, {}},
107 {AidlAnnotation::Type::VINTF_STABILITY, "VintfStability", CONTEXT_TYPE, {}},
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700108 {AidlAnnotation::Type::UNSUPPORTED_APP_USAGE,
109 "UnsupportedAppUsage",
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900110 CONTEXT_TYPE | CONTEXT_MEMBER,
Jooyung Han5c2fcae2020-12-26 00:04:39 +0900111 {{"expectedSignature", kStringType},
112 {"implicitMember", kStringType},
113 {"maxTargetSdk", kIntType},
114 {"publicAlternatives", kStringType},
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900115 {"trackingBug", kLongType}}},
116 {AidlAnnotation::Type::JAVA_STABLE_PARCELABLE,
117 "JavaOnlyStableParcelable",
118 CONTEXT_TYPE_UNSTRUCTURED_PARCELABLE,
119 {}},
120 {AidlAnnotation::Type::HIDE, "Hide", CONTEXT_TYPE | CONTEXT_MEMBER, {}},
121 {AidlAnnotation::Type::BACKING,
122 "Backing",
123 CONTEXT_TYPE_ENUM,
124 {{"type", kStringType, /* required= */ true}}},
Jooyung Han5721a232020-12-24 04:34:55 +0900125 {AidlAnnotation::Type::JAVA_PASSTHROUGH,
126 "JavaPassthrough",
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900127 CONTEXT_ALL,
128 {{"annotation", kStringType, /* required= */ true}},
129 /* repeatable= */ true},
Jiyong Park9aa3d042020-12-04 23:30:02 +0900130 {AidlAnnotation::Type::JAVA_DERIVE,
Jooyung Han5721a232020-12-24 04:34:55 +0900131 "JavaDerive",
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900132 CONTEXT_TYPE_STRUCTURED_PARCELABLE | CONTEXT_TYPE_UNION,
133 {{"toString", kBooleanType}, {"equals", kBooleanType}}},
134 {AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE,
135 "JavaOnlyImmutable",
136 CONTEXT_TYPE_STRUCTURED_PARCELABLE | CONTEXT_TYPE_UNION |
137 CONTEXT_TYPE_UNSTRUCTURED_PARCELABLE,
138 {}},
139 {AidlAnnotation::Type::FIXED_SIZE, "FixedSize", CONTEXT_TYPE_STRUCTURED_PARCELABLE, {}},
140 {AidlAnnotation::Type::DESCRIPTOR,
141 "Descriptor",
142 CONTEXT_TYPE_INTERFACE,
143 {{"value", kStringType, /* required= */ true}}},
Andrei Homescue61feb52020-08-18 15:44:24 -0700144 {AidlAnnotation::Type::RUST_DERIVE,
145 "RustDerive",
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900146 CONTEXT_TYPE_STRUCTURED_PARCELABLE | CONTEXT_TYPE_UNION,
Jooyung Han5c2fcae2020-12-26 00:04:39 +0900147 {{"Copy", kBooleanType},
148 {"Clone", kBooleanType},
149 {"PartialOrd", kBooleanType},
150 {"Ord", kBooleanType},
151 {"PartialEq", kBooleanType},
152 {"Eq", kBooleanType},
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900153 {"Hash", kBooleanType}}},
Jooyung Hanf8dbbcc2020-12-26 03:05:55 +0900154 {AidlAnnotation::Type::SUPPRESS_WARNINGS,
155 "SuppressWarnings",
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900156 CONTEXT_TYPE | CONTEXT_MEMBER,
157 {{"value", kStringArrayType, /* required= */ true}}},
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900158 };
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700159 return kSchemas;
160}
Jiyong Park68bc77a2018-07-19 19:00:45 +0900161
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700162std::string AidlAnnotation::TypeToString(Type type) {
163 for (const Schema& schema : AllSchemas()) {
164 if (type == schema.type) return schema.name;
165 }
166 AIDL_FATAL(AIDL_LOCATION_HERE) << "Unrecognized type: " << static_cast<size_t>(type);
167 __builtin_unreachable();
168}
Andrei Onea9445fc62019-06-27 18:11:59 +0100169
170AidlAnnotation* AidlAnnotation::Parse(
171 const AidlLocation& location, const string& name,
Jooyung Han6736ffb2021-01-16 10:13:40 +0900172 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list,
Jooyung Han8451a202021-01-16 03:07:06 +0900173 const Comments& comments) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700174 const Schema* schema = nullptr;
175 for (const Schema& a_schema : AllSchemas()) {
176 if (a_schema.name == name) {
177 schema = &a_schema;
178 }
179 }
180
181 if (schema == nullptr) {
Jiyong Park68bc77a2018-07-19 19:00:45 +0900182 std::ostringstream stream;
Steven Moreland46e9da82018-07-27 15:45:29 -0700183 stream << "'" << name << "' is not a recognized annotation. ";
Jiyong Park68bc77a2018-07-19 19:00:45 +0900184 stream << "It must be one of:";
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700185 for (const Schema& s : AllSchemas()) {
186 stream << " " << s.name;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900187 }
188 stream << ".";
Steven Moreland46e9da82018-07-27 15:45:29 -0700189 AIDL_ERROR(location) << stream.str();
190 return nullptr;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900191 }
Andrei Onea9445fc62019-06-27 18:11:59 +0100192 if (parameter_list == nullptr) {
Jooyung Han6736ffb2021-01-16 10:13:40 +0900193 return new AidlAnnotation(location, *schema, {}, comments);
Andrei Onea9445fc62019-06-27 18:11:59 +0100194 }
195
Jooyung Han6736ffb2021-01-16 10:13:40 +0900196 return new AidlAnnotation(location, *schema, std::move(*parameter_list), comments);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900197}
198
Andrei Onea9445fc62019-06-27 18:11:59 +0100199AidlAnnotation::AidlAnnotation(
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700200 const AidlLocation& location, const Schema& schema,
Jooyung Han6736ffb2021-01-16 10:13:40 +0900201 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters,
Jooyung Han8451a202021-01-16 03:07:06 +0900202 const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +0900203 : AidlNode(location, comments), schema_(schema), parameters_(std::move(parameters)) {}
Andrei Onea9445fc62019-06-27 18:11:59 +0100204
Jooyung Hanc5688f72021-01-05 15:41:48 +0900205struct ConstReferenceFinder : AidlVisitor {
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900206 const AidlConstantReference* found;
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900207 void Visit(const AidlConstantReference& ref) override {
Jooyung Han690f5842020-12-04 13:02:04 +0900208 if (!found) found = &ref;
209 }
Jooyung Hanc5688f72021-01-05 15:41:48 +0900210 static const AidlConstantReference* Find(const AidlConstantValue& c) {
211 ConstReferenceFinder finder;
212 VisitTopDown(finder, c);
213 return finder.found;
214 }
Jooyung Han690f5842020-12-04 13:02:04 +0900215};
216
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900217// Checks if annotation complies with the schema
218// - every parameter is known and has well-typed value.
219// - every required parameter is present.
Andrei Onea9445fc62019-06-27 18:11:59 +0100220bool AidlAnnotation::CheckValid() const {
Andrei Onea9445fc62019-06-27 18:11:59 +0100221 for (const auto& name_and_param : parameters_) {
222 const std::string& param_name = name_and_param.first;
223 const std::shared_ptr<AidlConstantValue>& param = name_and_param.second;
Jooyung Han690f5842020-12-04 13:02:04 +0900224
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900225 const ParamType* param_type = schema_.ParamType(param_name);
226 if (!param_type) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100227 std::ostringstream stream;
228 stream << "Parameter " << param_name << " not supported ";
Devin Mooredecaf292020-04-30 09:16:40 -0700229 stream << "for annotation " << GetName() << ". ";
Andrei Onea9445fc62019-06-27 18:11:59 +0100230 stream << "It must be one of:";
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900231 for (const auto& param : schema_.parameters) {
232 stream << " " << param.name;
Andrei Onea9445fc62019-06-27 18:11:59 +0100233 }
234 AIDL_ERROR(this) << stream.str();
235 return false;
236 }
Jooyung Han690f5842020-12-04 13:02:04 +0900237
Jooyung Hanc5688f72021-01-05 15:41:48 +0900238 const auto& found = ConstReferenceFinder::Find(*param);
239 if (found) {
240 AIDL_ERROR(found) << "Value must be a constant expression but contains reference to "
241 << found->GetFieldName() << ".";
Jooyung Han690f5842020-12-04 13:02:04 +0900242 return false;
243 }
244
245 if (!param->CheckValid()) {
246 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
247 << GetName() << ".";
248 return false;
249 }
250
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900251 const std::string param_value =
252 param->ValueString(param_type->type, AidlConstantValueDecorator);
Andrei Onea9445fc62019-06-27 18:11:59 +0100253 // Assume error on empty string.
254 if (param_value == "") {
255 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
256 << GetName() << ".";
257 return false;
258 }
259 }
Jooyung Han5721a232020-12-24 04:34:55 +0900260 bool success = true;
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900261 for (const auto& param : schema_.parameters) {
262 if (param.required && parameters_.count(param.name) == 0) {
263 AIDL_ERROR(this) << "Missing '" << param.name << "' on @" << GetName() << ".";
Jooyung Han5721a232020-12-24 04:34:55 +0900264 success = false;
265 }
266 }
267 return success;
Andrei Onea9445fc62019-06-27 18:11:59 +0100268}
269
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900270// Checks if the annotation is applicable to the current context.
271// For example, annotations like @VintfStability, @FixedSize is not applicable to AidlTypeSpecifier
272// nodes.
273bool AidlAnnotation::CheckContext(TargetContext context) const {
274 if (schema_.target_context & static_cast<uint32_t>(context)) {
275 return true;
276 }
277 const static map<TargetContext, string> context_name_map{
278 {CONTEXT_TYPE_INTERFACE, "interface"},
279 {CONTEXT_TYPE_ENUM, "enum"},
280 {CONTEXT_TYPE_STRUCTURED_PARCELABLE, "structured parcelable"},
281 {CONTEXT_TYPE_UNION, "union"},
282 {CONTEXT_TYPE_UNSTRUCTURED_PARCELABLE, "parcelable"},
283 {CONTEXT_CONST, "constant"},
284 {CONTEXT_FIELD, "field"},
285 {CONTEXT_METHOD, "method"},
286 {CONTEXT_TYPE_SPECIFIER, "type"},
287 };
288 vector<string> available;
289 for (const auto& [context, name] : context_name_map) {
290 if (schema_.target_context & context) {
291 available.push_back(name);
292 }
293 }
294 AIDL_ERROR(this) << "@" << GetName() << " is not available. It can annotate {"
295 << Join(available, ", ") << "}.";
296 return false;
297}
298
Andrei Onea9445fc62019-06-27 18:11:59 +0100299std::map<std::string, std::string> AidlAnnotation::AnnotationParams(
300 const ConstantValueDecorator& decorator) const {
301 std::map<std::string, std::string> raw_params;
Andrei Onea9445fc62019-06-27 18:11:59 +0100302 for (const auto& name_and_param : parameters_) {
303 const std::string& param_name = name_and_param.first;
304 const std::shared_ptr<AidlConstantValue>& param = name_and_param.second;
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900305 const ParamType* param_type = schema_.ParamType(param_name);
306 AIDL_FATAL_IF(!param_type, this);
307 raw_params.emplace(param_name, param->ValueString(param_type->type, decorator));
Andrei Onea9445fc62019-06-27 18:11:59 +0100308 }
309 return raw_params;
310}
Steven Moreland46e9da82018-07-27 15:45:29 -0700311
Jooyung Han965e31d2020-11-27 12:30:16 +0900312std::string AidlAnnotation::ToString() const {
Daniel Norman37d43dd2019-09-09 17:22:34 -0700313 if (parameters_.empty()) {
314 return "@" + GetName();
315 } else {
316 vector<string> param_strings;
Jooyung Han965e31d2020-11-27 12:30:16 +0900317 for (const auto& [name, value] : AnnotationParams(AidlConstantValueDecorator)) {
Daniel Norman37d43dd2019-09-09 17:22:34 -0700318 param_strings.emplace_back(name + "=" + value);
319 }
320 return "@" + GetName() + "(" + Join(param_strings, ", ") + ")";
321 }
322}
323
Jooyung Hanc5688f72021-01-05 15:41:48 +0900324void AidlAnnotation::TraverseChildren(std::function<void(const AidlNode&)> traverse) const {
325 for (const auto& [name, value] : parameters_) {
326 (void)name;
327 traverse(*value);
328 }
329}
330
Andrei Onea9445fc62019-06-27 18:11:59 +0100331static const AidlAnnotation* GetAnnotation(const vector<AidlAnnotation>& annotations,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700332 AidlAnnotation::Type type) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100333 for (const auto& a : annotations) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700334 if (a.GetType() == type) {
Jooyung Hand902a972020-10-23 17:32:44 +0900335 AIDL_FATAL_IF(a.Repeatable(), a)
336 << "Trying to get a single annotation when it is repeatable.";
Andrei Onea9445fc62019-06-27 18:11:59 +0100337 return &a;
338 }
339 }
340 return nullptr;
341}
342
Jooyung Han8451a202021-01-16 03:07:06 +0900343AidlAnnotatable::AidlAnnotatable(const AidlLocation& location, const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +0900344 : AidlCommentable(location, comments) {}
Steven Moreland46e9da82018-07-27 15:45:29 -0700345
Jiyong Park68bc77a2018-07-19 19:00:45 +0900346bool AidlAnnotatable::IsNullable() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700347 return GetAnnotation(annotations_, AidlAnnotation::Type::NULLABLE);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900348}
349
Jooyung Han01720ed2021-08-13 07:46:07 +0900350bool AidlAnnotatable::IsHeapNullable() const {
351 auto annot = GetAnnotation(annotations_, AidlAnnotation::Type::NULLABLE);
352 if (annot) {
353 return annot->ParamValue<bool>("heap").value_or(false);
354 }
355 return false;
356}
357
Jiyong Park68bc77a2018-07-19 19:00:45 +0900358bool AidlAnnotatable::IsUtf8InCpp() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700359 return GetAnnotation(annotations_, AidlAnnotation::Type::UTF8_IN_CPP);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900360}
361
Steven Morelanda7764e52020-10-27 17:29:29 +0000362bool AidlAnnotatable::IsSensitiveData() const {
363 return GetAnnotation(annotations_, AidlAnnotation::Type::SENSITIVE_DATA);
364}
365
Steven Morelanda57d0a62019-07-30 09:41:14 -0700366bool AidlAnnotatable::IsVintfStability() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700367 return GetAnnotation(annotations_, AidlAnnotation::Type::VINTF_STABILITY);
Steven Morelanda57d0a62019-07-30 09:41:14 -0700368}
369
Jeongik Chad0a10272020-08-06 16:33:36 +0900370bool AidlAnnotatable::IsJavaOnlyImmutable() const {
371 return GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE);
Jeongik Cha36f76c32020-07-28 00:25:52 +0900372}
373
Devin Moorec7e47a32020-08-07 10:55:25 -0700374bool AidlAnnotatable::IsFixedSize() const {
375 return GetAnnotation(annotations_, AidlAnnotation::Type::FIXED_SIZE);
376}
377
Andrei Onea9445fc62019-06-27 18:11:59 +0100378const AidlAnnotation* AidlAnnotatable::UnsupportedAppUsage() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700379 return GetAnnotation(annotations_, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE);
Jiyong Parka6605ab2018-11-11 14:30:21 +0900380}
381
Andrei Homescue61feb52020-08-18 15:44:24 -0700382const AidlAnnotation* AidlAnnotatable::RustDerive() const {
383 return GetAnnotation(annotations_, AidlAnnotation::Type::RUST_DERIVE);
384}
385
Jooyung Han672557b2020-12-24 05:18:00 +0900386const AidlAnnotation* AidlAnnotatable::BackingType() const {
387 return GetAnnotation(annotations_, AidlAnnotation::Type::BACKING);
Daniel Norman85aed542019-08-21 12:01:14 -0700388}
389
Jooyung Hanf8dbbcc2020-12-26 03:05:55 +0900390std::vector<std::string> AidlAnnotatable::SuppressWarnings() const {
391 auto annot = GetAnnotation(annotations_, AidlAnnotation::Type::SUPPRESS_WARNINGS);
392 if (annot) {
393 auto names = annot->ParamValue<std::vector<std::string>>("value");
394 AIDL_FATAL_IF(!names.has_value(), this);
395 return std::move(names.value());
396 }
397 return {};
398}
399
Jeongik Cha88f95a82020-01-15 13:02:16 +0900400bool AidlAnnotatable::IsStableApiParcelable(Options::Language lang) const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700401 return lang == Options::Language::JAVA &&
402 GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_STABLE_PARCELABLE);
Jeongik Cha82317dd2019-02-27 20:26:11 +0900403}
404
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800405bool AidlAnnotatable::IsHide() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700406 return GetAnnotation(annotations_, AidlAnnotation::Type::HIDE);
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800407}
408
Jooyung Han829ec7c2020-12-02 12:07:36 +0900409bool AidlAnnotatable::JavaDerive(const std::string& method) const {
410 auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_DERIVE);
411 if (annotation != nullptr) {
Jooyung Hanb3c77ed2020-12-26 02:02:45 +0900412 return annotation->ParamValue<bool>(method).value_or(false);
Jooyung Han829ec7c2020-12-02 12:07:36 +0900413 }
414 return false;
Jiyong Park43113fb2020-07-20 16:26:19 +0900415}
416
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900417std::string AidlAnnotatable::GetDescriptor() const {
418 auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::DESCRIPTOR);
419 if (annotation != nullptr) {
Jooyung Hanb3c77ed2020-12-26 02:02:45 +0900420 return annotation->ParamValue<std::string>("value").value();
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900421 }
422 return "";
423}
424
Devin Moore24f68572020-02-26 13:20:59 -0800425bool AidlAnnotatable::CheckValid(const AidlTypenames&) const {
Andrei Onea9445fc62019-06-27 18:11:59 +0100426 for (const auto& annotation : GetAnnotations()) {
Jooyung Hand902a972020-10-23 17:32:44 +0900427 if (!annotation.CheckValid()) {
428 return false;
429 }
430 }
431
432 std::map<AidlAnnotation::Type, AidlLocation> declared;
433 for (const auto& annotation : GetAnnotations()) {
434 const auto& [iter, inserted] = declared.emplace(annotation.GetType(), annotation.GetLocation());
435 if (!inserted && !annotation.Repeatable()) {
436 AIDL_ERROR(this) << "'" << annotation.GetName()
437 << "' is repeated, but not allowed. Previous location: " << iter->second;
438 return false;
439 }
Andrei Onea9445fc62019-06-27 18:11:59 +0100440 }
Steven Morelanda57d0a62019-07-30 09:41:14 -0700441
Andrei Onea9445fc62019-06-27 18:11:59 +0100442 return true;
443}
444
Jiyong Park68bc77a2018-07-19 19:00:45 +0900445string AidlAnnotatable::ToString() const {
446 vector<string> ret;
447 for (const auto& a : annotations_) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900448 ret.emplace_back(a.ToString());
Jiyong Park68bc77a2018-07-19 19:00:45 +0900449 }
450 std::sort(ret.begin(), ret.end());
451 return Join(ret, " ");
452}
453
Steven Moreland46e9da82018-07-27 15:45:29 -0700454AidlTypeSpecifier::AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name,
455 bool is_array,
Jiyong Park1deecc32018-07-17 01:14:41 +0900456 vector<unique_ptr<AidlTypeSpecifier>>* type_params,
Jooyung Han8451a202021-01-16 03:07:06 +0900457 const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +0900458 : AidlAnnotatable(location, comments),
Jeongik Chadf76dc72019-11-28 00:08:47 +0900459 AidlParameterizable<unique_ptr<AidlTypeSpecifier>>(type_params),
Steven Moreland46e9da82018-07-27 15:45:29 -0700460 unresolved_name_(unresolved_name),
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700461 is_array_(is_array),
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900462 split_name_(Split(unresolved_name, ".")) {}
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700463
Jooyung Hand2fa0232020-10-19 02:51:41 +0900464const AidlTypeSpecifier& AidlTypeSpecifier::ArrayBase() const {
Steven Moreland3f658cf2018-08-20 13:40:54 -0700465 AIDL_FATAL_IF(!is_array_, this);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900466 // Declaring array of generic type cannot happen, it is grammar error.
467 AIDL_FATAL_IF(IsGeneric(), this);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700468
Jooyung Hand2fa0232020-10-19 02:51:41 +0900469 if (!array_base_) {
470 array_base_.reset(new AidlTypeSpecifier(*this));
471 array_base_->is_array_ = false;
472 }
473 return *array_base_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700474}
475
Jooyung Han965e31d2020-11-27 12:30:16 +0900476string AidlTypeSpecifier::Signature() const {
Jiyong Park1deecc32018-07-17 01:14:41 +0900477 string ret = GetName();
478 if (IsGeneric()) {
479 vector<string> arg_names;
480 for (const auto& ta : GetTypeParameters()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900481 arg_names.emplace_back(ta->Signature());
Jiyong Parkccf00f82018-07-17 01:39:23 +0900482 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900483 ret += "<" + Join(arg_names, ",") + ">";
Jiyong Parkccf00f82018-07-17 01:39:23 +0900484 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900485 if (IsArray()) {
486 ret += "[]";
487 }
488 return ret;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900489}
490
Jooyung Han965e31d2020-11-27 12:30:16 +0900491string AidlTypeSpecifier::ToString() const {
492 string ret = Signature();
Jiyong Park02da7422018-07-16 16:00:26 +0900493 string annotations = AidlAnnotatable::ToString();
494 if (annotations != "") {
495 ret = annotations + " " + ret;
496 }
497 return ret;
498}
499
Jooyung Han13f1fa52021-06-11 18:06:12 +0900500// When `scope` is specified, name is resolved first based on it.
501// `scope` can be null for built-in types and fully-qualified types.
502bool AidlTypeSpecifier::Resolve(const AidlTypenames& typenames, const AidlScope* scope) {
Steven Moreland21780812020-09-11 01:29:45 +0000503 AIDL_FATAL_IF(IsResolved(), this);
Jooyung Han13f1fa52021-06-11 18:06:12 +0900504 std::string name = unresolved_name_;
505 if (scope) {
506 name = scope->ResolveName(name);
507 }
508 AidlTypenames::ResolvedTypename result = typenames.ResolveTypename(name);
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700509 if (result.is_resolved) {
510 fully_qualified_name_ = result.canonical_name;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900511 split_name_ = Split(fully_qualified_name_, ".");
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900512 defined_type_ = result.defined_type;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900513 }
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700514 return result.is_resolved;
Casey Dahlin70078e62015-09-30 17:01:30 -0700515}
516
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900517const AidlDefinedType* AidlTypeSpecifier::GetDefinedType() const {
518 return defined_type_;
519}
520
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900521bool AidlTypeSpecifier::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -0800522 if (!AidlAnnotatable::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100523 return false;
524 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900525 if (IsGeneric()) {
Jooyung Hand09a21d2021-02-15 18:56:55 +0900526 const auto& types = GetTypeParameters();
527 for (const auto& arg : types) {
528 if (!arg->CheckValid(typenames)) {
529 return false;
530 }
531 }
Jeongik Chae74c86d2019-12-12 16:54:03 +0900532
Jooyung Hand09a21d2021-02-15 18:56:55 +0900533 const string& type_name = GetName();
Jeongik Chae74c86d2019-12-12 16:54:03 +0900534 // TODO(b/136048684) Disallow to use primitive types only if it is List or Map.
535 if (type_name == "List" || type_name == "Map") {
Jooyung Hane87cdd02020-12-11 16:47:35 +0900536 if (std::any_of(types.begin(), types.end(), [&](auto& type_ptr) {
Jooyung Han1f35ef32021-02-15 19:08:05 +0900537 return !type_ptr->IsArray() &&
538 (typenames.GetEnumDeclaration(*type_ptr) ||
539 AidlTypenames::IsPrimitiveTypename(type_ptr->GetName()));
Jeongik Chae74c86d2019-12-12 16:54:03 +0900540 })) {
Devin Moore7b8d5c92020-03-17 14:14:08 -0700541 AIDL_ERROR(this) << "A generic type cannot have any primitive type parameters.";
Jeongik Chae74c86d2019-12-12 16:54:03 +0900542 return false;
543 }
544 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800545 const auto defined_type = typenames.TryGetDefinedType(type_name);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900546 const auto parameterizable =
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800547 defined_type != nullptr ? defined_type->AsParameterizable() : nullptr;
548 const bool is_user_defined_generic_type =
Jeongik Chadf76dc72019-11-28 00:08:47 +0900549 parameterizable != nullptr && parameterizable->IsGeneric();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800550 const size_t num_params = GetTypeParameters().size();
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900551 if (type_name == "List") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800552 if (num_params > 1) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900553 AIDL_ERROR(this) << "List can only have one type parameter, but got: '" << Signature()
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000554 << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900555 return false;
556 }
Jooyung Han55f96ad2020-12-13 10:08:33 +0900557 const AidlTypeSpecifier& contained_type = *GetTypeParameters()[0];
Jooyung Hancea89002021-02-15 17:04:04 +0900558 if (contained_type.IsArray()) {
559 AIDL_ERROR(this)
560 << "List of arrays is not supported. List<T> supports parcelable/union, String, "
561 "IBinder, and ParcelFileDescriptor.";
562 return false;
563 }
Jooyung Han55f96ad2020-12-13 10:08:33 +0900564 const string& contained_type_name = contained_type.GetName();
565 if (AidlTypenames::IsBuiltinTypename(contained_type_name)) {
566 if (contained_type_name != "String" && contained_type_name != "IBinder" &&
567 contained_type_name != "ParcelFileDescriptor") {
568 AIDL_ERROR(this) << "List<" << contained_type_name
569 << "> is not supported. List<T> supports parcelable/union, String, "
570 "IBinder, and ParcelFileDescriptor.";
571 return false;
572 }
573 } else { // Defined types
574 if (typenames.GetInterface(contained_type)) {
575 AIDL_ERROR(this) << "List<" << contained_type_name
576 << "> is not supported. List<T> supports parcelable/union, String, "
577 "IBinder, and ParcelFileDescriptor.";
578 return false;
579 }
580 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900581 } else if (type_name == "Map") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800582 if (num_params != 0 && num_params != 2) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900583 AIDL_ERROR(this) << "Map must have 0 or 2 type parameters, but got "
Jooyung Han965e31d2020-11-27 12:30:16 +0900584 << "'" << Signature() << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900585 return false;
586 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800587 if (num_params == 2) {
Jooyung Hanaab242a2021-02-15 19:01:15 +0900588 const string& key_type = GetTypeParameters()[0]->Signature();
Jeongik Chae48d9942020-01-02 17:39:00 +0900589 if (key_type != "String") {
590 AIDL_ERROR(this) << "The type of key in map must be String, but it is "
591 << "'" << key_type << "'";
592 return false;
593 }
594 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800595 } else if (is_user_defined_generic_type) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900596 const size_t allowed = parameterizable->GetTypeParameters().size();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800597 if (num_params != allowed) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900598 AIDL_ERROR(this) << type_name << " must have " << allowed << " type parameters, but got "
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800599 << num_params;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900600 return false;
601 }
602 } else {
603 AIDL_ERROR(this) << type_name << " is not a generic type.";
604 return false;
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900605 }
606 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900607
Steven Moreland11cb9452020-01-21 16:56:58 -0800608 const bool is_generic_string_list = GetName() == "List" && IsGeneric() &&
609 GetTypeParameters().size() == 1 &&
610 GetTypeParameters()[0]->GetName() == "String";
611 if (IsUtf8InCpp() && (GetName() != "String" && !is_generic_string_list)) {
612 AIDL_ERROR(this) << "@utf8InCpp can only be used on String, String[], and List<String>.";
613 return false;
614 }
615
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900616 if (GetName() == "void") {
617 if (IsArray() || IsNullable() || IsUtf8InCpp()) {
618 AIDL_ERROR(this) << "void type cannot be an array or nullable or utf8 string";
619 return false;
620 }
621 }
622
623 if (IsArray()) {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800624 const auto defined_type = typenames.TryGetDefinedType(GetName());
625 if (defined_type != nullptr && defined_type->AsInterface() != nullptr) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900626 AIDL_ERROR(this) << "Binder type cannot be an array";
627 return false;
628 }
Steven Moreland8042d2d2020-09-30 23:31:32 +0000629 if (GetName() == "ParcelableHolder") {
630 AIDL_ERROR(this) << "Arrays of ParcelableHolder are not supported.";
631 return false;
632 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900633 }
634
635 if (IsNullable()) {
636 if (AidlTypenames::IsPrimitiveTypename(GetName()) && !IsArray()) {
637 AIDL_ERROR(this) << "Primitive type cannot get nullable annotation";
638 return false;
639 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800640 const auto defined_type = typenames.TryGetDefinedType(GetName());
641 if (defined_type != nullptr && defined_type->AsEnumDeclaration() != nullptr && !IsArray()) {
Daniel Normanee8674f2019-09-20 16:07:00 -0700642 AIDL_ERROR(this) << "Enum type cannot get nullable annotation";
643 return false;
644 }
Jeongik Chaf6ec8982020-10-15 00:10:30 +0900645 if (GetName() == "ParcelableHolder") {
646 AIDL_ERROR(this) << "ParcelableHolder cannot be nullable.";
647 return false;
648 }
Jooyung Han01720ed2021-08-13 07:46:07 +0900649 if (IsHeapNullable()) {
650 if (!defined_type || IsArray() || !defined_type->AsParcelable()) {
651 AIDL_ERROR(this) << "@nullable(heap=true) is available to parcelables.";
652 return false;
653 }
654 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900655 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900656 return true;
657}
658
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900659std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type,
Steven Moreland860b1942018-08-16 14:59:28 -0700660 const std::string& raw_value) {
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900661 if (type.IsArray()) {
662 return raw_value;
663 }
664
665 if (auto defined_type = type.GetDefinedType(); defined_type) {
666 auto enum_type = defined_type->AsEnumDeclaration();
667 AIDL_FATAL_IF(!enum_type, type) << "Invalid type for \"" << raw_value << "\"";
668 return type.GetName() + "." + raw_value.substr(raw_value.find_last_of('.') + 1);
669 }
Steven Moreland860b1942018-08-16 14:59:28 -0700670 return raw_value;
671}
672
Steven Moreland46e9da82018-07-27 15:45:29 -0700673AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
674 AidlTypeSpecifier* type, const std::string& name)
Steven Moreland541788d2020-05-21 22:05:52 +0000675 : AidlVariableDeclaration(location, type, name, AidlConstantValue::Default(*type)) {
676 default_user_specified_ = false;
677}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700678
Steven Moreland46e9da82018-07-27 15:45:29 -0700679AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
680 AidlTypeSpecifier* type, const std::string& name,
681 AidlConstantValue* default_value)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900682 : AidlMember(location, type->GetComments()),
Steven Moreland541788d2020-05-21 22:05:52 +0000683 type_(type),
684 name_(name),
685 default_user_specified_(true),
686 default_value_(default_value) {}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700687
Jooyung Han53fb4242020-12-17 16:03:49 +0900688bool AidlVariableDeclaration::HasUsefulDefaultValue() const {
689 if (GetDefaultValue()) {
690 return true;
691 }
692 // null is accepted as a valid default value in all backends
693 if (GetType().IsNullable()) {
694 return true;
695 }
696 return false;
697}
698
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900699bool AidlVariableDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700700 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900701 valid &= type_->CheckValid(typenames);
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900702
Steven Moreland54be7bd2019-12-05 11:17:53 -0800703 if (type_->GetName() == "void") {
704 AIDL_ERROR(this) << "Declaration " << name_
705 << " is void, but declarations cannot be of void type.";
706 valid = false;
707 }
708
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900709 if (default_value_ == nullptr) return valid;
Steven Moreland25294322018-08-07 18:13:55 -0700710 valid &= default_value_->CheckValid();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700711
Steven Moreland25294322018-08-07 18:13:55 -0700712 if (!valid) return false;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700713
Steven Moreland860b1942018-08-16 14:59:28 -0700714 return !ValueString(AidlConstantValueDecorator).empty();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700715}
Steven Moreland5557f1c2018-07-02 13:50:23 -0700716
Jooyung Hanacae85d2020-10-28 16:39:09 +0900717string AidlVariableDeclaration::GetCapitalizedName() const {
718 AIDL_FATAL_IF(name_.size() <= 0, *this) << "Name can't be empty.";
719 string str = name_;
720 str[0] = static_cast<char>(toupper(str[0]));
721 return str;
722}
723
Steven Moreland5557f1c2018-07-02 13:50:23 -0700724string AidlVariableDeclaration::ToString() const {
Jooyung Han965e31d2020-11-27 12:30:16 +0900725 string ret = type_->ToString() + " " + name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000726 if (default_value_ != nullptr && default_user_specified_) {
Steven Moreland860b1942018-08-16 14:59:28 -0700727 ret += " = " + ValueString(AidlConstantValueDecorator);
Steven Moreland9ea10e32018-07-19 15:26:09 -0700728 }
729 return ret;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700730}
731
Jiyong Park02da7422018-07-16 16:00:26 +0900732string AidlVariableDeclaration::Signature() const {
733 return type_->Signature() + " " + name_;
734}
735
Steven Moreland860b1942018-08-16 14:59:28 -0700736std::string AidlVariableDeclaration::ValueString(const ConstantValueDecorator& decorator) const {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900737 if (default_value_ != nullptr) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700738 return default_value_->ValueString(GetType(), decorator);
Jiyong Parka468e2a2018-08-29 21:25:18 +0900739 } else {
740 return "";
741 }
Steven Moreland25294322018-08-07 18:13:55 -0700742}
743
Jooyung Hanc5688f72021-01-05 15:41:48 +0900744void AidlVariableDeclaration::TraverseChildren(
745 std::function<void(const AidlNode&)> traverse) const {
746 traverse(GetType());
747 if (IsDefaultUserSpecified()) {
748 traverse(*GetDefaultValue());
749 }
750}
751
Steven Moreland46e9da82018-07-27 15:45:29 -0700752AidlArgument::AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
753 AidlTypeSpecifier* type, const std::string& name)
754 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700755 direction_(direction),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700756 direction_specified_(true) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700757
Steven Moreland46e9da82018-07-27 15:45:29 -0700758AidlArgument::AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type,
759 const std::string& name)
760 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700761 direction_(AidlArgument::IN_DIR),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700762 direction_specified_(false) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700763
Jooyung Han020d8d12021-02-26 17:23:02 +0900764static std::string to_string(AidlArgument::Direction direction) {
765 switch (direction) {
766 case AidlArgument::IN_DIR:
767 return "in";
768 case AidlArgument::OUT_DIR:
769 return "out";
770 case AidlArgument::INOUT_DIR:
771 return "inout";
772 }
773}
774
Jiyong Park02da7422018-07-16 16:00:26 +0900775string AidlArgument::GetDirectionSpecifier() const {
Casey Dahlinc378c992015-09-29 16:50:40 -0700776 string ret;
Casey Dahlinc378c992015-09-29 16:50:40 -0700777 if (direction_specified_) {
Jooyung Han020d8d12021-02-26 17:23:02 +0900778 ret = to_string(direction_);
Casey Dahlinc378c992015-09-29 16:50:40 -0700779 }
Casey Dahlinc378c992015-09-29 16:50:40 -0700780 return ret;
781}
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700782
Jiyong Park02da7422018-07-16 16:00:26 +0900783string AidlArgument::ToString() const {
Devin Mooreeccdb902020-03-24 16:22:40 -0700784 if (direction_specified_) {
785 return GetDirectionSpecifier() + " " + AidlVariableDeclaration::ToString();
786 } else {
787 return AidlVariableDeclaration::ToString();
788 }
Jiyong Park02da7422018-07-16 16:00:26 +0900789}
790
Jooyung Han020d8d12021-02-26 17:23:02 +0900791static std::string FormatDirections(const std::set<AidlArgument::Direction>& directions) {
792 std::vector<std::string> out;
793 for (const auto& d : directions) {
794 out.push_back(to_string(d));
795 }
796
797 if (out.size() <= 1) { // [] => "" or [A] => "A"
798 return Join(out, "");
799 } else if (out.size() == 2) { // [A,B] => "A or B"
800 return Join(out, " or ");
801 } else { // [A,B,C] => "A, B, or C"
802 out.back() = "or " + out.back();
803 return Join(out, ", ");
804 }
805}
806
807bool AidlArgument::CheckValid(const AidlTypenames& typenames) const {
808 if (!GetType().CheckValid(typenames)) {
809 return false;
810 }
811
812 const auto& aspect = typenames.GetArgumentAspect(GetType());
813
814 if (aspect.possible_directions.size() == 0) {
815 AIDL_ERROR(this) << aspect.name << " cannot be an argument type";
816 return false;
817 }
818
819 // when direction is not specified, "in" is assumed and should be the only possible direction
820 if (!DirectionWasSpecified() && aspect.possible_directions != std::set{AidlArgument::IN_DIR}) {
821 AIDL_ERROR(this) << "The direction of '" << GetName() << "' is not specified. " << aspect.name
822 << " can be an " << FormatDirections(aspect.possible_directions)
823 << " parameter.";
824 return false;
825 }
826
827 if (aspect.possible_directions.count(GetDirection()) == 0) {
828 AIDL_ERROR(this) << "'" << GetName() << "' can't be an " << GetDirectionSpecifier()
829 << " parameter because " << aspect.name << " can only be an "
830 << FormatDirections(aspect.possible_directions) << " parameter.";
831 return false;
832 }
833
834 return true;
835}
836
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900837bool AidlCommentable::IsHidden() const {
Jooyung Han24effbf2021-01-16 10:24:03 +0900838 return android::aidl::HasHideInComments(GetComments());
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900839}
840
841bool AidlCommentable::IsDeprecated() const {
Jooyung Hand4fe00e2021-01-11 16:21:53 +0900842 return android::aidl::FindDeprecated(GetComments()).has_value();
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900843}
844
Jooyung Han8451a202021-01-16 03:07:06 +0900845AidlMember::AidlMember(const AidlLocation& location, const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +0900846 : AidlCommentable(location, comments) {}
Steven Moreland46e9da82018-07-27 15:45:29 -0700847
Steven Moreland46e9da82018-07-27 15:45:29 -0700848AidlConstantDeclaration::AidlConstantDeclaration(const AidlLocation& location,
849 AidlTypeSpecifier* type, const std::string& name,
850 AidlConstantValue* value)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900851 : AidlMember(location, type->GetComments()), type_(type), name_(name), value_(value) {}
Steven Moreland693640b2018-07-19 13:46:27 -0700852
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900853bool AidlConstantDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700854 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900855 valid &= type_->CheckValid(typenames);
Steven Moreland25294322018-08-07 18:13:55 -0700856 valid &= value_->CheckValid();
857 if (!valid) return false;
Steven Moreland693640b2018-07-19 13:46:27 -0700858
Steven Morelande689da22020-11-10 02:06:30 +0000859 const static set<string> kSupportedConstTypes = {"String", "byte", "int", "long"};
Jooyung Han965e31d2020-11-27 12:30:16 +0900860 if (kSupportedConstTypes.find(type_->Signature()) == kSupportedConstTypes.end()) {
861 AIDL_ERROR(this) << "Constant of type " << type_->Signature() << " is not supported.";
Steven Moreland693640b2018-07-19 13:46:27 -0700862 return false;
863 }
864
Will McVickerd7d18df2019-09-12 13:40:50 -0700865 return true;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700866}
867
Jiyong Parka428d212018-08-29 22:26:30 +0900868string AidlConstantDeclaration::ToString() const {
Jooyung Hanb3ca6302020-11-27 14:13:27 +0900869 return "const " + type_->ToString() + " " + name_ + " = " +
870 ValueString(AidlConstantValueDecorator);
Jiyong Parka428d212018-08-29 22:26:30 +0900871}
872
873string AidlConstantDeclaration::Signature() const {
874 return type_->Signature() + " " + name_;
875}
876
Steven Moreland46e9da82018-07-27 15:45:29 -0700877AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
878 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jooyung Han8451a202021-01-16 03:07:06 +0900879 const Comments& comments)
Jiyong Parkb034bf02018-07-30 17:44:33 +0900880 : AidlMethod(location, oneway, type, name, args, comments, 0, true) {
881 has_id_ = false;
882}
883
884AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
885 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jooyung Han8451a202021-01-16 03:07:06 +0900886 const Comments& comments, int id, bool is_user_defined)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900887 : AidlMember(location, comments),
Steven Moreland46e9da82018-07-27 15:45:29 -0700888 oneway_(oneway),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700889 type_(type),
890 name_(name),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700891 arguments_(std::move(*args)),
Jiyong Parkb034bf02018-07-30 17:44:33 +0900892 id_(id),
893 is_user_defined_(is_user_defined) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700894 has_id_ = true;
895 delete args;
Christopher Wileyad339272015-10-05 19:11:58 -0700896 for (const unique_ptr<AidlArgument>& a : arguments_) {
897 if (a->IsIn()) { in_arguments_.push_back(a.get()); }
898 if (a->IsOut()) { out_arguments_.push_back(a.get()); }
899 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700900}
901
Jiyong Park02da7422018-07-16 16:00:26 +0900902string AidlMethod::Signature() const {
903 vector<string> arg_signatures;
904 for (const auto& arg : GetArguments()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900905 arg_signatures.emplace_back(arg->GetType().Signature());
Jiyong Park02da7422018-07-16 16:00:26 +0900906 }
Jiyong Park309668e2018-07-28 16:55:44 +0900907 return GetName() + "(" + Join(arg_signatures, ", ") + ")";
908}
909
910string AidlMethod::ToString() const {
911 vector<string> arg_strings;
912 for (const auto& arg : GetArguments()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900913 arg_strings.emplace_back(arg->ToString());
Jiyong Park309668e2018-07-28 16:55:44 +0900914 }
Jooyung Han965e31d2020-11-27 12:30:16 +0900915 string ret = (IsOneway() ? "oneway " : "") + GetType().ToString() + " " + GetName() + "(" +
Steven Moreland4ee68632018-12-14 15:52:46 -0800916 Join(arg_strings, ", ") + ")";
Jiyong Parked65bf42018-08-28 15:43:27 +0900917 if (HasId()) {
918 ret += " = " + std::to_string(GetId());
919 }
920 return ret;
Jiyong Park02da7422018-07-16 16:00:26 +0900921}
922
Steven Moreland46e9da82018-07-27 15:45:29 -0700923AidlDefinedType::AidlDefinedType(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +0900924 const Comments& comments, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900925 std::vector<std::unique_ptr<AidlMember>>* members)
Jooyung Han35784982021-06-29 06:26:12 +0900926 : AidlAnnotatable(location, comments), AidlScope(this), name_(name), package_(package) {
Jooyung Han93f48f02021-06-05 00:11:16 +0900927 // adjust name/package when name is fully qualified (for preprocessed files)
928 if (package_.empty() && name_.find('.') != std::string::npos) {
929 // Note that this logic is absolutely wrong. Given a parcelable
930 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
931 // the class is just Bar. However, this was the way it was done in the past.
932 //
933 // See b/17415692
934 auto pos = name.rfind('.');
935 // name is the last part.
936 name_ = name.substr(pos + 1);
937 // package is the initial parts (except the last).
938 package_ = name.substr(0, pos);
939 }
Jooyung Han829ec7c2020-12-02 12:07:36 +0900940 if (members) {
941 for (auto& m : *members) {
942 if (auto constant = m->AsConstantDeclaration(); constant) {
943 constants_.emplace_back(constant);
944 } else if (auto variable = m->AsVariableDeclaration(); variable) {
945 variables_.emplace_back(variable);
946 } else if (auto method = m->AsMethod(); method) {
947 methods_.emplace_back(method);
948 } else {
949 AIDL_FATAL(*m);
950 }
951 members_.push_back(m.release());
952 }
953 delete members;
954 }
955}
Steven Moreland787b0432018-07-03 09:00:58 -0700956
Jooyung Han808a2a02020-12-28 16:46:54 +0900957bool AidlDefinedType::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -0800958 if (!AidlAnnotatable::CheckValid(typenames)) {
959 return false;
960 }
Jooyung Han829ec7c2020-12-02 12:07:36 +0900961 if (!CheckValidWithMembers(typenames)) {
962 return false;
963 }
Devin Moore24f68572020-02-26 13:20:59 -0800964 return true;
965}
966
Steven Moreland787b0432018-07-03 09:00:58 -0700967std::string AidlDefinedType::GetCanonicalName() const {
968 if (package_.empty()) {
969 return GetName();
970 }
971 return GetPackage() + "." + GetName();
972}
973
Jooyung Han829ec7c2020-12-02 12:07:36 +0900974bool AidlDefinedType::CheckValidWithMembers(const AidlTypenames& typenames) const {
975 bool success = true;
976
977 for (const auto& v : GetFields()) {
978 const bool field_valid = v->CheckValid(typenames);
979 success = success && field_valid;
980 }
981
982 // field names should be unique
983 std::set<std::string> fieldnames;
984 for (const auto& v : GetFields()) {
985 bool duplicated = !fieldnames.emplace(v->GetName()).second;
986 if (duplicated) {
987 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName() << "'";
988 success = false;
989 }
990 }
991
992 // immutable parcelables should have immutable fields.
993 if (IsJavaOnlyImmutable()) {
994 for (const auto& v : GetFields()) {
995 if (!typenames.CanBeJavaOnlyImmutable(v->GetType())) {
996 AIDL_ERROR(v) << "The @JavaOnlyImmutable '" << GetName() << "' has a "
997 << "non-immutable field named '" << v->GetName() << "'.";
998 success = false;
999 }
1000 }
1001 }
1002
1003 set<string> constant_names;
1004 for (const auto& constant : GetConstantDeclarations()) {
1005 if (constant_names.count(constant->GetName()) > 0) {
1006 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
1007 success = false;
1008 }
1009 constant_names.insert(constant->GetName());
1010 success = success && constant->CheckValid(typenames);
1011 }
1012
1013 return success;
1014}
1015
1016bool AidlDefinedType::CheckValidForGetterNames() const {
1017 bool success = true;
1018 std::set<std::string> getters;
1019 for (const auto& v : GetFields()) {
1020 bool duplicated = !getters.emplace(v->GetCapitalizedName()).second;
1021 if (duplicated) {
1022 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName()
1023 << "' after capitalizing the first letter";
1024 success = false;
1025 }
1026 }
1027 return success;
1028}
1029
Jooyung Han13f1fa52021-06-11 18:06:12 +09001030std::string AidlDefinedType::ResolveName(const std::string& name) const {
1031 // TODO(b/182508839): resolve with nested types when we support nested types
1032 //
1033 // For example, in the following (the syntax is TBD), t1's Type is x.Foo.Bar.Type
1034 // while t2's Type is y.Type.
1035 //
1036 // package x;
1037 // import y.Type;
1038 // parcelable Foo {
1039 // parcelable Bar {
1040 // enum Type { Type1, Type2 }
1041 // Type t1;
1042 // }
1043 // Type t2;
1044 // }
1045 AIDL_FATAL_IF(!GetEnclosingScope(), this)
1046 << "Type should have an enclosing scope.(e.g. AidlDocument)";
1047 return GetEnclosingScope()->ResolveName(name);
1048}
1049
Jooyung Han35784982021-06-29 06:26:12 +09001050template <typename T>
1051const T* AidlCast(const AidlNode& node) {
1052 struct CastVisitor : AidlVisitor {
1053 const T* cast = nullptr;
1054 void Visit(const T& t) override { cast = &t; }
1055 } visitor;
1056 node.DispatchVisit(visitor);
1057 return visitor.cast;
1058}
1059
1060const AidlDocument& AidlDefinedType::GetDocument() const {
1061 // TODO(b/182508839): resolve with nested types when we support nested types
1062 auto scope = GetEnclosingScope();
1063 AIDL_FATAL_IF(!scope, this) << "no scope defined.";
1064 auto doc = AidlCast<AidlDocument>(scope->GetNode());
1065 AIDL_FATAL_IF(!doc, this) << "scope is not a document.";
1066 return *doc;
1067}
1068
Jiyong Park18132182020-06-08 20:24:40 +09001069AidlParcelable::AidlParcelable(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001070 const std::string& package, const Comments& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001071 const std::string& cpp_header, std::vector<std::string>* type_params,
1072 std::vector<std::unique_ptr<AidlMember>>* members)
1073 : AidlDefinedType(location, name, comments, package, members),
Jeongik Chadf76dc72019-11-28 00:08:47 +09001074 AidlParameterizable<std::string>(type_params),
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -08001075 cpp_header_(cpp_header) {
1076 // Strip off quotation marks if we actually have a cpp header.
1077 if (cpp_header_.length() >= 2) {
1078 cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2);
1079 }
Casey Dahlin59401da2015-10-09 18:16:45 -07001080}
Jeongik Chadf76dc72019-11-28 00:08:47 +09001081template <typename T>
1082AidlParameterizable<T>::AidlParameterizable(const AidlParameterizable& other) {
1083 // Copying is not supported if it has type parameters.
1084 // It doesn't make a problem because only ArrayBase() makes a copy,
1085 // and it can be called only if a type is not generic.
Steven Moreland21780812020-09-11 01:29:45 +00001086 AIDL_FATAL_IF(other.IsGeneric(), AIDL_LOCATION_HERE);
Jeongik Chadf76dc72019-11-28 00:08:47 +09001087}
1088
1089template <typename T>
1090bool AidlParameterizable<T>::CheckValid() const {
1091 return true;
1092};
1093
1094template <>
1095bool AidlParameterizable<std::string>::CheckValid() const {
1096 if (!IsGeneric()) {
1097 return true;
1098 }
1099 std::unordered_set<std::string> set(GetTypeParameters().begin(), GetTypeParameters().end());
1100 if (set.size() != GetTypeParameters().size()) {
1101 AIDL_ERROR(this->AsAidlNode()) << "Every type parameter should be unique.";
1102 return false;
1103 }
1104 return true;
1105}
Casey Dahlin59401da2015-10-09 18:16:45 -07001106
Jooyung Han808a2a02020-12-28 16:46:54 +09001107bool AidlParcelable::CheckValid(const AidlTypenames& typenames) const {
1108 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001109 return false;
1110 }
Jeongik Chadf76dc72019-11-28 00:08:47 +09001111 if (!AidlParameterizable<std::string>::CheckValid()) {
1112 return false;
1113 }
Jeongik Cha82317dd2019-02-27 20:26:11 +09001114
1115 return true;
1116}
1117
Steven Moreland5557f1c2018-07-02 13:50:23 -07001118AidlStructuredParcelable::AidlStructuredParcelable(
Jiyong Park18132182020-06-08 20:24:40 +09001119 const AidlLocation& location, const std::string& name, const std::string& package,
Jooyung Han8451a202021-01-16 03:07:06 +09001120 const Comments& comments, std::vector<std::string>* type_params,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001121 std::vector<std::unique_ptr<AidlMember>>* members)
1122 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Steven Moreland5557f1c2018-07-02 13:50:23 -07001123
Jooyung Han808a2a02020-12-28 16:46:54 +09001124bool AidlStructuredParcelable::CheckValid(const AidlTypenames& typenames) const {
1125 if (!AidlParcelable::CheckValid(typenames)) {
Devin Moore24f68572020-02-26 13:20:59 -08001126 return false;
1127 }
Jeongik Cha13066da2020-08-06 15:43:19 +09001128
Jooyung Han59af9cc2020-10-25 21:44:14 +09001129 bool success = true;
Jeongik Cha36f76c32020-07-28 00:25:52 +09001130
Jooyung Hand4057c42020-10-23 13:28:22 +09001131 if (IsFixedSize()) {
1132 for (const auto& v : GetFields()) {
1133 if (!typenames.CanBeFixedSize(v->GetType())) {
1134 AIDL_ERROR(v) << "The @FixedSize parcelable '" << this->GetName() << "' has a "
1135 << "non-fixed size field named " << v->GetName() << ".";
1136 success = false;
1137 }
1138 }
1139 }
1140
1141 if (IsJavaOnlyImmutable()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001142 // Immutable parcelables provide getters
Jooyung Han829ec7c2020-12-02 12:07:36 +09001143 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001144 success = false;
Jooyung Hand4057c42020-10-23 13:28:22 +09001145 }
1146 }
1147
Daniel Norman85aed542019-08-21 12:01:14 -07001148 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001149}
1150
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001151// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -07001152bool AidlTypeSpecifier::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1153 Options::Language lang) const {
Jooyung Hand09a21d2021-02-15 18:56:55 +09001154 if (IsGeneric()) {
1155 const auto& types = GetTypeParameters();
1156 for (const auto& arg : types) {
1157 if (!arg->LanguageSpecificCheckValid(typenames, lang)) {
1158 return false;
1159 }
1160 }
1161 }
1162
Andrei Homescub62afd92020-05-11 19:24:59 -07001163 if ((lang == Options::Language::NDK || lang == Options::Language::RUST) && IsArray() &&
1164 GetName() == "IBinder") {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001165 AIDL_ERROR(this) << "The " << to_string(lang) << " backend does not support array of IBinder";
Steven Moreland0185d9b2020-05-15 23:21:22 +00001166 return false;
1167 }
Jeongik Cha8f02a532020-10-14 00:16:28 +09001168 if (lang == Options::Language::RUST && GetName() == "ParcelableHolder") {
1169 // TODO(b/146611855): Remove it when Rust backend supports ParcelableHolder
1170 AIDL_ERROR(this) << "The Rust backend does not support ParcelableHolder yet.";
Jeongik Cha225519b2020-08-29 01:55:32 +09001171 return false;
1172 }
Andrei Homescub62afd92020-05-11 19:24:59 -07001173 if ((lang == Options::Language::NDK || lang == Options::Language::RUST) && IsArray() &&
1174 IsNullable()) {
Steven Moreland0185d9b2020-05-15 23:21:22 +00001175 if (GetName() == "ParcelFileDescriptor") {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001176 AIDL_ERROR(this) << "The " << to_string(lang)
Andrei Homescub62afd92020-05-11 19:24:59 -07001177 << " backend does not support nullable array of ParcelFileDescriptor";
Steven Moreland0185d9b2020-05-15 23:21:22 +00001178 return false;
1179 }
1180
Steven Morelandd59e3172020-05-11 16:42:09 -07001181 const auto defined_type = typenames.TryGetDefinedType(GetName());
1182 if (defined_type != nullptr && defined_type->AsParcelable() != nullptr) {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001183 AIDL_ERROR(this) << "The " << to_string(lang)
Andrei Homescub62afd92020-05-11 19:24:59 -07001184 << " backend does not support nullable array of parcelable";
Steven Morelandd59e3172020-05-11 16:42:09 -07001185 return false;
1186 }
1187 }
Andrei Homescub62afd92020-05-11 19:24:59 -07001188 if (this->GetName() == "FileDescriptor" &&
1189 (lang == Options::Language::NDK || lang == Options::Language::RUST)) {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001190 AIDL_ERROR(this) << "FileDescriptor isn't supported by the " << to_string(lang) << " backend.";
Steven Morelandc8a4ca82020-01-21 17:50:08 -08001191 return false;
1192 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001193 if (this->IsGeneric()) {
1194 if (this->GetName() == "List") {
Jooyung Han55f96ad2020-12-13 10:08:33 +09001195 if (lang == Options::Language::NDK) {
1196 const AidlTypeSpecifier& contained_type = *GetTypeParameters()[0];
1197 const string& contained_type_name = contained_type.GetName();
Jooyung Hane87cdd02020-12-11 16:47:35 +09001198 if (typenames.GetInterface(contained_type)) {
1199 AIDL_ERROR(this) << "List<" << contained_type_name
1200 << "> is not supported. List in NDK doesn't support interface.";
1201 return false;
1202 }
1203 if (contained_type_name == "IBinder") {
1204 AIDL_ERROR(this) << "List<" << contained_type_name
1205 << "> is not supported. List in NDK doesn't support IBinder.";
1206 return false;
Jeongik Cha08ca2182019-11-21 14:01:13 +09001207 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001208 }
Jeongik Chabb55b5e2020-01-07 23:11:26 +09001209 }
1210 }
Devin Moore6a01ca12020-08-28 10:24:19 -07001211
1212 if (this->IsArray()) {
1213 if (this->GetName() == "List" || this->GetName() == "Map" ||
1214 this->GetName() == "CharSequence") {
1215 AIDL_ERROR(this) << this->GetName() << "[] is not supported.";
Jeongik Chabb55b5e2020-01-07 23:11:26 +09001216 return false;
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001217 }
1218 }
Devin Moore6a01ca12020-08-28 10:24:19 -07001219
1220 if (lang != Options::Language::JAVA) {
1221 if (this->GetName() == "List" && !this->IsGeneric()) {
1222 AIDL_ERROR(this) << "Currently, only the Java backend supports non-generic List.";
1223 return false;
1224 }
1225 if (this->GetName() == "Map" || this->GetName() == "CharSequence") {
1226 AIDL_ERROR(this) << "Currently, only Java backend supports " << this->GetName() << ".";
1227 return false;
Jeongik Cha08ca2182019-11-21 14:01:13 +09001228 }
1229 }
1230
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001231 return true;
1232}
1233
1234// TODO: we should treat every backend all the same in future.
Jooyung Han2cb8ecd2021-07-28 18:41:30 +09001235bool AidlParcelable::LanguageSpecificCheckValid(const AidlTypenames& typenames,
Steven Morelandd59e3172020-05-11 16:42:09 -07001236 Options::Language lang) const {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001237 for (const auto& v : this->GetFields()) {
Steven Morelandd59e3172020-05-11 16:42:09 -07001238 if (!v->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001239 return false;
1240 }
1241 }
1242 return true;
1243}
1244
Daniel Norman85aed542019-08-21 12:01:14 -07001245AidlEnumerator::AidlEnumerator(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001246 AidlConstantValue* value, const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +09001247 : AidlCommentable(location, comments),
Jooyung Han29813842020-12-08 01:28:03 +09001248 name_(name),
1249 value_(value),
Jooyung Han29813842020-12-08 01:28:03 +09001250 value_user_specified_(value != nullptr) {}
Daniel Norman85aed542019-08-21 12:01:14 -07001251
1252bool AidlEnumerator::CheckValid(const AidlTypeSpecifier& enum_backing_type) const {
1253 if (GetValue() == nullptr) {
1254 return false;
1255 }
1256 if (!GetValue()->CheckValid()) {
1257 return false;
1258 }
Will McVickerd7d18df2019-09-12 13:40:50 -07001259 if (GetValue()->ValueString(enum_backing_type, AidlConstantValueDecorator).empty()) {
Daniel Norman85aed542019-08-21 12:01:14 -07001260 AIDL_ERROR(this) << "Enumerator type differs from enum backing type.";
1261 return false;
1262 }
1263 return true;
1264}
1265
1266string AidlEnumerator::ValueString(const AidlTypeSpecifier& backing_type,
1267 const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -07001268 return GetValue()->ValueString(backing_type, decorator);
Daniel Norman85aed542019-08-21 12:01:14 -07001269}
1270
1271AidlEnumDeclaration::AidlEnumDeclaration(const AidlLocation& location, const std::string& name,
1272 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jooyung Han8451a202021-01-16 03:07:06 +09001273 const std::string& package, const Comments& comments)
Jooyung Han829ec7c2020-12-02 12:07:36 +09001274 : AidlDefinedType(location, name, comments, package, nullptr),
Jooyung Han29813842020-12-08 01:28:03 +09001275 enumerators_(std::move(*enumerators)) {
Jooyung Han672557b2020-12-24 05:18:00 +09001276 // Fill missing enumerator values with <prev + 1>
1277 // This can't be done in Autofill() because type/ref resolution depends on this.
1278 // For example, with enum E { A, B = A }, B's value 'A' is a reference which can't be
1279 // resolved if A has no value set.
Daniel Normanb28684e2019-10-17 15:31:39 -07001280 const AidlEnumerator* previous = nullptr;
1281 for (const auto& enumerator : enumerators_) {
1282 if (enumerator->GetValue() == nullptr) {
Jooyung Han29813842020-12-08 01:28:03 +09001283 auto loc = enumerator->GetLocation();
Daniel Normanb28684e2019-10-17 15:31:39 -07001284 if (previous == nullptr) {
Devin Mooredf93ebb2020-03-25 14:03:35 -07001285 enumerator->SetValue(
Jooyung Han29813842020-12-08 01:28:03 +09001286 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "0")));
Daniel Normanb28684e2019-10-17 15:31:39 -07001287 } else {
Jooyung Hand0c8af02021-01-06 18:08:01 +09001288 auto prev_value = std::make_unique<AidlConstantReference>(loc, previous->GetName());
Daniel Normanb28684e2019-10-17 15:31:39 -07001289 enumerator->SetValue(std::make_unique<AidlBinaryConstExpression>(
Jooyung Han29813842020-12-08 01:28:03 +09001290 loc, std::move(prev_value), "+",
1291 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "1"))));
Daniel Normanb28684e2019-10-17 15:31:39 -07001292 }
1293 }
1294 previous = enumerator.get();
1295 }
1296}
1297
Jooyung Han672557b2020-12-24 05:18:00 +09001298bool AidlEnumDeclaration::Autofill(const AidlTypenames& typenames) {
1299 if (auto annot = BackingType(); annot != nullptr) {
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001300 // Autofill() is called before the grand CheckValid(). But AidlAnnotation::ParamValue()
1301 // calls AidlConstantValue::evaluate() which requires CheckValid() to be called before. So we
Jooyung Han672557b2020-12-24 05:18:00 +09001302 // need to call CheckValid().
1303 if (!annot->CheckValid()) {
1304 return false;
1305 }
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001306 auto type = annot->ParamValue<std::string>("type").value();
1307 backing_type_ =
Jooyung Han8451a202021-01-16 03:07:06 +09001308 std::make_unique<AidlTypeSpecifier>(annot->GetLocation(), type, false, nullptr, Comments{});
Jooyung Han672557b2020-12-24 05:18:00 +09001309 } else {
1310 // Default to byte type for enums.
1311 backing_type_ =
Jooyung Han8451a202021-01-16 03:07:06 +09001312 std::make_unique<AidlTypeSpecifier>(AIDL_LOCATION_HERE, "byte", false, nullptr, Comments{});
Jooyung Han672557b2020-12-24 05:18:00 +09001313 }
1314 // Autofill() is called after type resolution, we resolve the backing type manually.
Jooyung Han13f1fa52021-06-11 18:06:12 +09001315 if (!backing_type_->Resolve(typenames, nullptr)) {
Jooyung Han672557b2020-12-24 05:18:00 +09001316 AIDL_ERROR(this) << "Invalid backing type: " << backing_type_->GetName();
1317 }
1318 return true;
1319}
1320
Jooyung Han808a2a02020-12-28 16:46:54 +09001321bool AidlEnumDeclaration::CheckValid(const AidlTypenames& typenames) const {
1322 if (!AidlDefinedType::CheckValid(typenames)) {
Devin Moore24f68572020-02-26 13:20:59 -08001323 return false;
1324 }
Jooyung Han829ec7c2020-12-02 12:07:36 +09001325 if (!GetMembers().empty()) {
1326 AIDL_ERROR(this) << "Enum doesn't support fields/constants/methods.";
1327 return false;
1328 }
Daniel Norman85aed542019-08-21 12:01:14 -07001329 if (backing_type_ == nullptr) {
1330 AIDL_ERROR(this) << "Enum declaration missing backing type.";
1331 return false;
1332 }
1333 bool success = true;
1334 for (const auto& enumerator : enumerators_) {
1335 success = success && enumerator->CheckValid(GetBackingType());
1336 }
Jooyung Han3b990182020-12-22 17:44:31 +09001337
Jooyung Han808a2a02020-12-28 16:46:54 +09001338 return success;
Daniel Norman85aed542019-08-21 12:01:14 -07001339}
1340
Jooyung Han2946afc2020-10-05 20:29:16 +09001341AidlUnionDecl::AidlUnionDecl(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001342 const std::string& package, const Comments& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001343 std::vector<std::string>* type_params,
1344 std::vector<std::unique_ptr<AidlMember>>* members)
1345 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Jooyung Han2946afc2020-10-05 20:29:16 +09001346
Jooyung Han808a2a02020-12-28 16:46:54 +09001347bool AidlUnionDecl::CheckValid(const AidlTypenames& typenames) const {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001348 // visit parents
Jooyung Han808a2a02020-12-28 16:46:54 +09001349 if (!AidlParcelable::CheckValid(typenames)) {
Jooyung Hanfe89f122020-10-14 03:49:18 +09001350 return false;
1351 }
Jooyung Han59af9cc2020-10-25 21:44:14 +09001352
1353 // unions provide getters always
Jooyung Han829ec7c2020-12-02 12:07:36 +09001354 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001355 return false;
Jooyung Hanfe89f122020-10-14 03:49:18 +09001356 }
1357
1358 // now, visit self!
1359 bool success = true;
1360
1361 // TODO(b/170807936) do we need to allow ParcelableHolder in union?
1362 for (const auto& v : GetFields()) {
1363 if (v->GetType().GetName() == "ParcelableHolder") {
1364 AIDL_ERROR(*v) << "A union can't have a member of ParcelableHolder '" << v->GetName() << "'";
1365 success = false;
1366 }
1367 }
1368
Jooyung Hanfe89f122020-10-14 03:49:18 +09001369 if (GetFields().empty()) {
1370 AIDL_ERROR(*this) << "The union '" << this->GetName() << "' has no fields.";
1371 return false;
1372 }
1373
Jooyung Han53fb4242020-12-17 16:03:49 +09001374 // first member should have useful default value (implicit or explicit)
1375 const auto& first = GetFields()[0];
1376 if (!first->HasUsefulDefaultValue()) {
1377 // Most types can be initialized without a default value. For example,
1378 // interface types are inherently nullable. But, enum types should have
1379 // an explicit default value.
1380 if (!first->GetType().IsArray() && typenames.GetEnumDeclaration(first->GetType())) {
1381 AIDL_ERROR(first)
1382 << "The union's first member should have a useful default value. Enum types can be "
1383 "initialized with a reference. (e.g. ... = MyEnum.FOO;)";
1384 return false;
1385 }
1386 // In Java, array types are initialized as null without a default value. To be sure that default
1387 // initialized unions are accepted by other backends we require arrays also have a default
1388 // value.
1389 if (first->GetType().IsArray()) {
1390 AIDL_ERROR(first)
1391 << "The union's first member should have a useful default value. Arrays can be "
1392 "initialized with values(e.g. ... = { values... };) or marked as @nullable.";
1393 return false;
1394 }
1395 }
1396
Jooyung Hanfe89f122020-10-14 03:49:18 +09001397 return success;
1398}
1399
1400// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -07001401bool AidlInterface::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1402 Options::Language lang) const {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001403 for (const auto& m : this->GetMethods()) {
Steven Morelandd59e3172020-05-11 16:42:09 -07001404 if (!m->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001405 return false;
1406 }
1407 for (const auto& arg : m->GetArguments()) {
Steven Morelandd59e3172020-05-11 16:42:09 -07001408 if (!arg->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001409 return false;
1410 }
1411 }
1412 }
1413 return true;
1414}
1415
Steven Moreland46e9da82018-07-27 15:45:29 -07001416AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001417 const Comments& comments, bool oneway, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001418 std::vector<std::unique_ptr<AidlMember>>* members)
1419 : AidlDefinedType(location, name, comments, package, members) {
1420 for (auto& m : GetMethods()) {
1421 m.get()->ApplyInterfaceOneway(oneway);
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001422 }
Casey Dahlinfb7da2e2015-10-08 17:26:09 -07001423}
1424
Jooyung Han808a2a02020-12-28 16:46:54 +09001425bool AidlInterface::CheckValid(const AidlTypenames& typenames) const {
1426 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001427 return false;
1428 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001429 // Has to be a pointer due to deleting copy constructor. No idea why.
1430 map<string, const AidlMethod*> method_names;
1431 for (const auto& m : GetMethods()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001432 if (!m->GetType().CheckValid(typenames)) {
1433 return false;
1434 }
1435
Jeongik Cha649e8a72020-03-27 17:47:40 +09001436 // TODO(b/156872582): Support it when ParcelableHolder supports every backend.
1437 if (m->GetType().GetName() == "ParcelableHolder") {
1438 AIDL_ERROR(m) << "ParcelableHolder cannot be a return type";
1439 return false;
1440 }
Steven Morelandacd53472018-12-14 10:17:26 -08001441 if (m->IsOneway() && m->GetType().GetName() != "void") {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001442 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot return a value";
1443 return false;
1444 }
1445
1446 set<string> argument_names;
1447 for (const auto& arg : m->GetArguments()) {
1448 auto it = argument_names.find(arg->GetName());
1449 if (it != argument_names.end()) {
1450 AIDL_ERROR(m) << "method '" << m->GetName() << "' has duplicate argument name '"
1451 << arg->GetName() << "'";
1452 return false;
1453 }
1454 argument_names.insert(arg->GetName());
1455
Jooyung Han020d8d12021-02-26 17:23:02 +09001456 if (!arg->CheckValid(typenames)) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001457 return false;
1458 }
1459
Steven Morelandacd53472018-12-14 10:17:26 -08001460 if (m->IsOneway() && arg->IsOut()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001461 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot have out parameters";
1462 return false;
1463 }
Jooyung Han15fd6c62020-10-23 13:54:46 +09001464
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001465 // check that the name doesn't match a keyword
Jeongik Cha997281d2020-01-16 15:23:59 +09001466 if (IsJavaKeyword(arg->GetName().c_str())) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001467 AIDL_ERROR(arg) << "Argument name is a Java or aidl keyword";
1468 return false;
1469 }
1470
1471 // Reserve a namespace for internal use
1472 if (android::base::StartsWith(arg->GetName(), "_aidl")) {
1473 AIDL_ERROR(arg) << "Argument name cannot begin with '_aidl'";
1474 return false;
1475 }
Jooyung Hanfa181932021-06-12 07:56:53 +09001476
1477 if (arg->GetType().GetName() == "void") {
1478 AIDL_ERROR(arg->GetType())
1479 << "'void' is an invalid type for the parameter '" << arg->GetName() << "'";
1480 return false;
1481 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001482 }
1483
1484 auto it = method_names.find(m->GetName());
1485 // prevent duplicate methods
1486 if (it == method_names.end()) {
1487 method_names[m->GetName()] = m.get();
1488 } else {
1489 AIDL_ERROR(m) << "attempt to redefine method " << m->GetName() << ":";
1490 AIDL_ERROR(it->second) << "previously defined here.";
1491 return false;
1492 }
1493
Paul Trautrimb77048c2020-01-21 16:39:32 +09001494 static set<string> reserved_methods{"asBinder()", "getInterfaceHash()", "getInterfaceVersion()",
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001495 "getTransactionName(int)"};
1496
1497 if (reserved_methods.find(m->Signature()) != reserved_methods.end()) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001498 AIDL_ERROR(m) << " method " << m->Signature() << " is reserved for internal use.";
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001499 return false;
1500 }
1501 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001502
1503 bool success = true;
1504 set<string> constant_names;
Jooyung Han3f347ca2020-12-01 12:41:50 +09001505 for (const auto& constant : GetConstantDeclarations()) {
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001506 if (constant_names.count(constant->GetName()) > 0) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001507 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001508 success = false;
1509 }
1510 constant_names.insert(constant->GetName());
1511 success = success && constant->CheckValid(typenames);
1512 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001513 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001514}
1515
Jiyong Park27fd7fd2020-08-27 16:25:09 +09001516std::string AidlInterface::GetDescriptor() const {
1517 std::string annotatedDescriptor = AidlAnnotatable::GetDescriptor();
1518 if (annotatedDescriptor != "") {
1519 return annotatedDescriptor;
1520 }
1521 return GetCanonicalName();
1522}
1523
Jooyung Han132cf802021-01-15 02:17:32 +09001524AidlImport::AidlImport(const AidlLocation& location, const std::string& needed_class,
Jooyung Han8451a202021-01-16 03:07:06 +09001525 const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +09001526 : AidlNode(location, comments), needed_class_(needed_class) {}
Jooyung Han29813842020-12-08 01:28:03 +09001527
Jooyung Han13f1fa52021-06-11 18:06:12 +09001528AidlDocument::AidlDocument(const AidlLocation& location, const Comments& comments,
1529 std::vector<std::unique_ptr<AidlImport>> imports,
Jooyung Han35784982021-06-29 06:26:12 +09001530 std::vector<std::unique_ptr<AidlDefinedType>> defined_types,
1531 bool is_preprocessed)
Jooyung Han13f1fa52021-06-11 18:06:12 +09001532 : AidlCommentable(location, comments),
Jooyung Han35784982021-06-29 06:26:12 +09001533 AidlScope(this),
Jooyung Han13f1fa52021-06-11 18:06:12 +09001534 imports_(std::move(imports)),
Jooyung Han35784982021-06-29 06:26:12 +09001535 defined_types_(std::move(defined_types)),
1536 is_preprocessed_(is_preprocessed) {
Jooyung Han13f1fa52021-06-11 18:06:12 +09001537 for (const auto& t : defined_types_) {
1538 t->SetEnclosingScope(this);
1539 }
1540}
1541
1542// Resolves type name in the current document.
1543// - built-in types
1544// - imported types
1545// - top-level type
1546std::string AidlDocument::ResolveName(const std::string& name) const {
1547 if (AidlTypenames::IsBuiltinTypename(name)) {
1548 return name;
1549 }
1550
1551 const auto first_dot = name.find_first_of('.');
1552 // For "Outer.Inner", we look up "Outer" in the import list.
Jooyung Han29813842020-12-08 01:28:03 +09001553 const std::string class_name =
Jooyung Han13f1fa52021-06-11 18:06:12 +09001554 (first_dot == std::string::npos) ? name : name.substr(0, first_dot);
1555 // Keep ".Inner", to make a fully-qualified name
1556 const std::string nested_type = (first_dot == std::string::npos) ? "" : name.substr(first_dot);
1557
Jooyung Han29813842020-12-08 01:28:03 +09001558 for (const auto& import : Imports()) {
Jooyung Han13f1fa52021-06-11 18:06:12 +09001559 if (import->SimpleName() == class_name) {
1560 return import->GetNeededClass() + nested_type;
Jooyung Han29813842020-12-08 01:28:03 +09001561 }
1562 }
Jooyung Han13f1fa52021-06-11 18:06:12 +09001563
1564 // check if it is a top-level type.
1565 for (const auto& type : DefinedTypes()) {
1566 if (type->GetName() == class_name) {
1567 return type->GetCanonicalName() + nested_type;
1568 }
Jooyung Han29813842020-12-08 01:28:03 +09001569 }
Jooyung Han13f1fa52021-06-11 18:06:12 +09001570
1571 // name itself might be fully-qualified name.
1572 return name;
Steven Moreland26318532020-12-23 20:08:36 +00001573}