blob: 5941f35ff9e95ab071771968ad06cc757cc0a546 [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
Jooyung Han442cacf2021-09-13 17:44:56 +0900170std::unique_ptr<AidlAnnotation> AidlAnnotation::Parse(
Andrei Onea9445fc62019-06-27 18:11:59 +0100171 const AidlLocation& location, const string& name,
Jooyung Han442cacf2021-09-13 17:44:56 +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();
Jooyung Han442cacf2021-09-13 17:44:56 +0900190 return {};
Andrei Onea9445fc62019-06-27 18:11:59 +0100191 }
192
Jooyung Han442cacf2021-09-13 17:44:56 +0900193 return std::unique_ptr<AidlAnnotation>(
194 new AidlAnnotation(location, *schema, std::move(parameter_list), comments));
Jiyong Park68bc77a2018-07-19 19:00:45 +0900195}
196
Jooyung Han442cacf2021-09-13 17:44:56 +0900197AidlAnnotation::AidlAnnotation(const AidlLocation& location, const Schema& schema,
198 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters,
199 const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +0900200 : AidlNode(location, comments), schema_(schema), parameters_(std::move(parameters)) {}
Andrei Onea9445fc62019-06-27 18:11:59 +0100201
Jooyung Hanc5688f72021-01-05 15:41:48 +0900202struct ConstReferenceFinder : AidlVisitor {
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900203 const AidlConstantReference* found;
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900204 void Visit(const AidlConstantReference& ref) override {
Jooyung Han690f5842020-12-04 13:02:04 +0900205 if (!found) found = &ref;
206 }
Jooyung Hanc5688f72021-01-05 15:41:48 +0900207 static const AidlConstantReference* Find(const AidlConstantValue& c) {
208 ConstReferenceFinder finder;
209 VisitTopDown(finder, c);
210 return finder.found;
211 }
Jooyung Han690f5842020-12-04 13:02:04 +0900212};
213
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900214// Checks if annotation complies with the schema
215// - every parameter is known and has well-typed value.
216// - every required parameter is present.
Andrei Onea9445fc62019-06-27 18:11:59 +0100217bool AidlAnnotation::CheckValid() const {
Andrei Onea9445fc62019-06-27 18:11:59 +0100218 for (const auto& name_and_param : parameters_) {
219 const std::string& param_name = name_and_param.first;
220 const std::shared_ptr<AidlConstantValue>& param = name_and_param.second;
Jooyung Han690f5842020-12-04 13:02:04 +0900221
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900222 const ParamType* param_type = schema_.ParamType(param_name);
223 if (!param_type) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100224 std::ostringstream stream;
225 stream << "Parameter " << param_name << " not supported ";
Devin Mooredecaf292020-04-30 09:16:40 -0700226 stream << "for annotation " << GetName() << ". ";
Andrei Onea9445fc62019-06-27 18:11:59 +0100227 stream << "It must be one of:";
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900228 for (const auto& param : schema_.parameters) {
229 stream << " " << param.name;
Andrei Onea9445fc62019-06-27 18:11:59 +0100230 }
231 AIDL_ERROR(this) << stream.str();
232 return false;
233 }
Jooyung Han690f5842020-12-04 13:02:04 +0900234
Jooyung Hanc5688f72021-01-05 15:41:48 +0900235 const auto& found = ConstReferenceFinder::Find(*param);
236 if (found) {
237 AIDL_ERROR(found) << "Value must be a constant expression but contains reference to "
238 << found->GetFieldName() << ".";
Jooyung Han690f5842020-12-04 13:02:04 +0900239 return false;
240 }
241
242 if (!param->CheckValid()) {
243 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
244 << GetName() << ".";
245 return false;
246 }
247
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900248 const std::string param_value =
249 param->ValueString(param_type->type, AidlConstantValueDecorator);
Andrei Onea9445fc62019-06-27 18:11:59 +0100250 // Assume error on empty string.
251 if (param_value == "") {
252 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
253 << GetName() << ".";
254 return false;
255 }
256 }
Jooyung Han5721a232020-12-24 04:34:55 +0900257 bool success = true;
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900258 for (const auto& param : schema_.parameters) {
259 if (param.required && parameters_.count(param.name) == 0) {
260 AIDL_ERROR(this) << "Missing '" << param.name << "' on @" << GetName() << ".";
Jooyung Han5721a232020-12-24 04:34:55 +0900261 success = false;
262 }
263 }
264 return success;
Andrei Onea9445fc62019-06-27 18:11:59 +0100265}
266
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900267// Checks if the annotation is applicable to the current context.
268// For example, annotations like @VintfStability, @FixedSize is not applicable to AidlTypeSpecifier
269// nodes.
270bool AidlAnnotation::CheckContext(TargetContext context) const {
271 if (schema_.target_context & static_cast<uint32_t>(context)) {
272 return true;
273 }
274 const static map<TargetContext, string> context_name_map{
275 {CONTEXT_TYPE_INTERFACE, "interface"},
276 {CONTEXT_TYPE_ENUM, "enum"},
277 {CONTEXT_TYPE_STRUCTURED_PARCELABLE, "structured parcelable"},
278 {CONTEXT_TYPE_UNION, "union"},
279 {CONTEXT_TYPE_UNSTRUCTURED_PARCELABLE, "parcelable"},
280 {CONTEXT_CONST, "constant"},
281 {CONTEXT_FIELD, "field"},
282 {CONTEXT_METHOD, "method"},
283 {CONTEXT_TYPE_SPECIFIER, "type"},
284 };
285 vector<string> available;
286 for (const auto& [context, name] : context_name_map) {
287 if (schema_.target_context & context) {
288 available.push_back(name);
289 }
290 }
291 AIDL_ERROR(this) << "@" << GetName() << " is not available. It can annotate {"
292 << Join(available, ", ") << "}.";
293 return false;
294}
295
Andrei Onea9445fc62019-06-27 18:11:59 +0100296std::map<std::string, std::string> AidlAnnotation::AnnotationParams(
297 const ConstantValueDecorator& decorator) const {
298 std::map<std::string, std::string> raw_params;
Andrei Onea9445fc62019-06-27 18:11:59 +0100299 for (const auto& name_and_param : parameters_) {
300 const std::string& param_name = name_and_param.first;
301 const std::shared_ptr<AidlConstantValue>& param = name_and_param.second;
Jooyung Han2d6b5c42021-01-09 01:01:06 +0900302 const ParamType* param_type = schema_.ParamType(param_name);
303 AIDL_FATAL_IF(!param_type, this);
304 raw_params.emplace(param_name, param->ValueString(param_type->type, decorator));
Andrei Onea9445fc62019-06-27 18:11:59 +0100305 }
306 return raw_params;
307}
Steven Moreland46e9da82018-07-27 15:45:29 -0700308
Jooyung Han965e31d2020-11-27 12:30:16 +0900309std::string AidlAnnotation::ToString() const {
Daniel Norman37d43dd2019-09-09 17:22:34 -0700310 if (parameters_.empty()) {
311 return "@" + GetName();
312 } else {
313 vector<string> param_strings;
Jooyung Han965e31d2020-11-27 12:30:16 +0900314 for (const auto& [name, value] : AnnotationParams(AidlConstantValueDecorator)) {
Daniel Norman37d43dd2019-09-09 17:22:34 -0700315 param_strings.emplace_back(name + "=" + value);
316 }
317 return "@" + GetName() + "(" + Join(param_strings, ", ") + ")";
318 }
319}
320
Jooyung Hanc5688f72021-01-05 15:41:48 +0900321void AidlAnnotation::TraverseChildren(std::function<void(const AidlNode&)> traverse) const {
322 for (const auto& [name, value] : parameters_) {
323 (void)name;
324 traverse(*value);
325 }
326}
327
Andrei Onea9445fc62019-06-27 18:11:59 +0100328static const AidlAnnotation* GetAnnotation(const vector<AidlAnnotation>& annotations,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700329 AidlAnnotation::Type type) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100330 for (const auto& a : annotations) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700331 if (a.GetType() == type) {
Jooyung Hand902a972020-10-23 17:32:44 +0900332 AIDL_FATAL_IF(a.Repeatable(), a)
333 << "Trying to get a single annotation when it is repeatable.";
Andrei Onea9445fc62019-06-27 18:11:59 +0100334 return &a;
335 }
336 }
337 return nullptr;
338}
339
Jooyung Han8451a202021-01-16 03:07:06 +0900340AidlAnnotatable::AidlAnnotatable(const AidlLocation& location, const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +0900341 : AidlCommentable(location, comments) {}
Steven Moreland46e9da82018-07-27 15:45:29 -0700342
Jiyong Park68bc77a2018-07-19 19:00:45 +0900343bool AidlAnnotatable::IsNullable() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700344 return GetAnnotation(annotations_, AidlAnnotation::Type::NULLABLE);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900345}
346
Jooyung Han01720ed2021-08-13 07:46:07 +0900347bool AidlAnnotatable::IsHeapNullable() const {
348 auto annot = GetAnnotation(annotations_, AidlAnnotation::Type::NULLABLE);
349 if (annot) {
350 return annot->ParamValue<bool>("heap").value_or(false);
351 }
352 return false;
353}
354
Jiyong Park68bc77a2018-07-19 19:00:45 +0900355bool AidlAnnotatable::IsUtf8InCpp() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700356 return GetAnnotation(annotations_, AidlAnnotation::Type::UTF8_IN_CPP);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900357}
358
Steven Morelanda7764e52020-10-27 17:29:29 +0000359bool AidlAnnotatable::IsSensitiveData() const {
360 return GetAnnotation(annotations_, AidlAnnotation::Type::SENSITIVE_DATA);
361}
362
Steven Morelanda57d0a62019-07-30 09:41:14 -0700363bool AidlAnnotatable::IsVintfStability() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700364 return GetAnnotation(annotations_, AidlAnnotation::Type::VINTF_STABILITY);
Steven Morelanda57d0a62019-07-30 09:41:14 -0700365}
366
Jeongik Chad0a10272020-08-06 16:33:36 +0900367bool AidlAnnotatable::IsJavaOnlyImmutable() const {
368 return GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE);
Jeongik Cha36f76c32020-07-28 00:25:52 +0900369}
370
Devin Moorec7e47a32020-08-07 10:55:25 -0700371bool AidlAnnotatable::IsFixedSize() const {
372 return GetAnnotation(annotations_, AidlAnnotation::Type::FIXED_SIZE);
373}
374
Andrei Onea9445fc62019-06-27 18:11:59 +0100375const AidlAnnotation* AidlAnnotatable::UnsupportedAppUsage() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700376 return GetAnnotation(annotations_, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE);
Jiyong Parka6605ab2018-11-11 14:30:21 +0900377}
378
Andrei Homescue61feb52020-08-18 15:44:24 -0700379const AidlAnnotation* AidlAnnotatable::RustDerive() const {
380 return GetAnnotation(annotations_, AidlAnnotation::Type::RUST_DERIVE);
381}
382
Jooyung Han672557b2020-12-24 05:18:00 +0900383const AidlAnnotation* AidlAnnotatable::BackingType() const {
384 return GetAnnotation(annotations_, AidlAnnotation::Type::BACKING);
Daniel Norman85aed542019-08-21 12:01:14 -0700385}
386
Jooyung Hanf8dbbcc2020-12-26 03:05:55 +0900387std::vector<std::string> AidlAnnotatable::SuppressWarnings() const {
388 auto annot = GetAnnotation(annotations_, AidlAnnotation::Type::SUPPRESS_WARNINGS);
389 if (annot) {
390 auto names = annot->ParamValue<std::vector<std::string>>("value");
391 AIDL_FATAL_IF(!names.has_value(), this);
392 return std::move(names.value());
393 }
394 return {};
395}
396
Jeongik Cha88f95a82020-01-15 13:02:16 +0900397bool AidlAnnotatable::IsStableApiParcelable(Options::Language lang) const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700398 return lang == Options::Language::JAVA &&
399 GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_STABLE_PARCELABLE);
Jeongik Cha82317dd2019-02-27 20:26:11 +0900400}
401
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800402bool AidlAnnotatable::IsHide() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700403 return GetAnnotation(annotations_, AidlAnnotation::Type::HIDE);
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800404}
405
Jooyung Han829ec7c2020-12-02 12:07:36 +0900406bool AidlAnnotatable::JavaDerive(const std::string& method) const {
407 auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_DERIVE);
408 if (annotation != nullptr) {
Jooyung Hanb3c77ed2020-12-26 02:02:45 +0900409 return annotation->ParamValue<bool>(method).value_or(false);
Jooyung Han829ec7c2020-12-02 12:07:36 +0900410 }
411 return false;
Jiyong Park43113fb2020-07-20 16:26:19 +0900412}
413
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900414std::string AidlAnnotatable::GetDescriptor() const {
415 auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::DESCRIPTOR);
416 if (annotation != nullptr) {
Jooyung Hanb3c77ed2020-12-26 02:02:45 +0900417 return annotation->ParamValue<std::string>("value").value();
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900418 }
419 return "";
420}
421
Devin Moore24f68572020-02-26 13:20:59 -0800422bool AidlAnnotatable::CheckValid(const AidlTypenames&) const {
Andrei Onea9445fc62019-06-27 18:11:59 +0100423 for (const auto& annotation : GetAnnotations()) {
Jooyung Hand902a972020-10-23 17:32:44 +0900424 if (!annotation.CheckValid()) {
425 return false;
426 }
427 }
428
429 std::map<AidlAnnotation::Type, AidlLocation> declared;
430 for (const auto& annotation : GetAnnotations()) {
431 const auto& [iter, inserted] = declared.emplace(annotation.GetType(), annotation.GetLocation());
432 if (!inserted && !annotation.Repeatable()) {
433 AIDL_ERROR(this) << "'" << annotation.GetName()
434 << "' is repeated, but not allowed. Previous location: " << iter->second;
435 return false;
436 }
Andrei Onea9445fc62019-06-27 18:11:59 +0100437 }
Steven Morelanda57d0a62019-07-30 09:41:14 -0700438
Andrei Onea9445fc62019-06-27 18:11:59 +0100439 return true;
440}
441
Jiyong Park68bc77a2018-07-19 19:00:45 +0900442string AidlAnnotatable::ToString() const {
443 vector<string> ret;
444 for (const auto& a : annotations_) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900445 ret.emplace_back(a.ToString());
Jiyong Park68bc77a2018-07-19 19:00:45 +0900446 }
447 std::sort(ret.begin(), ret.end());
448 return Join(ret, " ");
449}
450
Steven Moreland46e9da82018-07-27 15:45:29 -0700451AidlTypeSpecifier::AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name,
452 bool is_array,
Jiyong Park1deecc32018-07-17 01:14:41 +0900453 vector<unique_ptr<AidlTypeSpecifier>>* type_params,
Jooyung Han8451a202021-01-16 03:07:06 +0900454 const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +0900455 : AidlAnnotatable(location, comments),
Jeongik Chadf76dc72019-11-28 00:08:47 +0900456 AidlParameterizable<unique_ptr<AidlTypeSpecifier>>(type_params),
Steven Moreland46e9da82018-07-27 15:45:29 -0700457 unresolved_name_(unresolved_name),
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700458 is_array_(is_array),
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900459 split_name_(Split(unresolved_name, ".")) {}
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700460
Jooyung Hand2fa0232020-10-19 02:51:41 +0900461const AidlTypeSpecifier& AidlTypeSpecifier::ArrayBase() const {
Steven Moreland3f658cf2018-08-20 13:40:54 -0700462 AIDL_FATAL_IF(!is_array_, this);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900463 // Declaring array of generic type cannot happen, it is grammar error.
464 AIDL_FATAL_IF(IsGeneric(), this);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700465
Jooyung Hand2fa0232020-10-19 02:51:41 +0900466 if (!array_base_) {
467 array_base_.reset(new AidlTypeSpecifier(*this));
468 array_base_->is_array_ = false;
469 }
470 return *array_base_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700471}
472
Jooyung Han965e31d2020-11-27 12:30:16 +0900473string AidlTypeSpecifier::Signature() const {
Jiyong Park1deecc32018-07-17 01:14:41 +0900474 string ret = GetName();
475 if (IsGeneric()) {
476 vector<string> arg_names;
477 for (const auto& ta : GetTypeParameters()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900478 arg_names.emplace_back(ta->Signature());
Jiyong Parkccf00f82018-07-17 01:39:23 +0900479 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900480 ret += "<" + Join(arg_names, ",") + ">";
Jiyong Parkccf00f82018-07-17 01:39:23 +0900481 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900482 if (IsArray()) {
483 ret += "[]";
484 }
485 return ret;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900486}
487
Jooyung Han965e31d2020-11-27 12:30:16 +0900488string AidlTypeSpecifier::ToString() const {
489 string ret = Signature();
Jiyong Park02da7422018-07-16 16:00:26 +0900490 string annotations = AidlAnnotatable::ToString();
491 if (annotations != "") {
492 ret = annotations + " " + ret;
493 }
494 return ret;
495}
496
Jooyung Han13f1fa52021-06-11 18:06:12 +0900497// When `scope` is specified, name is resolved first based on it.
498// `scope` can be null for built-in types and fully-qualified types.
499bool AidlTypeSpecifier::Resolve(const AidlTypenames& typenames, const AidlScope* scope) {
Steven Moreland21780812020-09-11 01:29:45 +0000500 AIDL_FATAL_IF(IsResolved(), this);
Jooyung Han13f1fa52021-06-11 18:06:12 +0900501 std::string name = unresolved_name_;
502 if (scope) {
503 name = scope->ResolveName(name);
504 }
505 AidlTypenames::ResolvedTypename result = typenames.ResolveTypename(name);
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700506 if (result.is_resolved) {
507 fully_qualified_name_ = result.canonical_name;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900508 split_name_ = Split(fully_qualified_name_, ".");
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900509 defined_type_ = result.defined_type;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900510 }
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700511 return result.is_resolved;
Casey Dahlin70078e62015-09-30 17:01:30 -0700512}
513
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900514const AidlDefinedType* AidlTypeSpecifier::GetDefinedType() const {
515 return defined_type_;
516}
517
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900518bool AidlTypeSpecifier::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -0800519 if (!AidlAnnotatable::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100520 return false;
521 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900522 if (IsGeneric()) {
Jooyung Hand09a21d2021-02-15 18:56:55 +0900523 const auto& types = GetTypeParameters();
524 for (const auto& arg : types) {
525 if (!arg->CheckValid(typenames)) {
526 return false;
527 }
528 }
Jeongik Chae74c86d2019-12-12 16:54:03 +0900529
Jooyung Hand09a21d2021-02-15 18:56:55 +0900530 const string& type_name = GetName();
Jeongik Chae74c86d2019-12-12 16:54:03 +0900531 // TODO(b/136048684) Disallow to use primitive types only if it is List or Map.
532 if (type_name == "List" || type_name == "Map") {
Jooyung Hane87cdd02020-12-11 16:47:35 +0900533 if (std::any_of(types.begin(), types.end(), [&](auto& type_ptr) {
Jooyung Han1f35ef32021-02-15 19:08:05 +0900534 return !type_ptr->IsArray() &&
535 (typenames.GetEnumDeclaration(*type_ptr) ||
536 AidlTypenames::IsPrimitiveTypename(type_ptr->GetName()));
Jeongik Chae74c86d2019-12-12 16:54:03 +0900537 })) {
Devin Moore7b8d5c92020-03-17 14:14:08 -0700538 AIDL_ERROR(this) << "A generic type cannot have any primitive type parameters.";
Jeongik Chae74c86d2019-12-12 16:54:03 +0900539 return false;
540 }
541 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800542 const auto defined_type = typenames.TryGetDefinedType(type_name);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900543 const auto parameterizable =
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800544 defined_type != nullptr ? defined_type->AsParameterizable() : nullptr;
545 const bool is_user_defined_generic_type =
Jeongik Chadf76dc72019-11-28 00:08:47 +0900546 parameterizable != nullptr && parameterizable->IsGeneric();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800547 const size_t num_params = GetTypeParameters().size();
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900548 if (type_name == "List") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800549 if (num_params > 1) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900550 AIDL_ERROR(this) << "List can only have one type parameter, but got: '" << Signature()
Steven Morelandebc3c5d2020-09-30 23:40:33 +0000551 << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900552 return false;
553 }
Jooyung Han55f96ad2020-12-13 10:08:33 +0900554 const AidlTypeSpecifier& contained_type = *GetTypeParameters()[0];
Jooyung Hancea89002021-02-15 17:04:04 +0900555 if (contained_type.IsArray()) {
556 AIDL_ERROR(this)
557 << "List of arrays is not supported. List<T> supports parcelable/union, String, "
558 "IBinder, and ParcelFileDescriptor.";
559 return false;
560 }
Jooyung Han55f96ad2020-12-13 10:08:33 +0900561 const string& contained_type_name = contained_type.GetName();
562 if (AidlTypenames::IsBuiltinTypename(contained_type_name)) {
563 if (contained_type_name != "String" && contained_type_name != "IBinder" &&
564 contained_type_name != "ParcelFileDescriptor") {
565 AIDL_ERROR(this) << "List<" << contained_type_name
566 << "> is not supported. List<T> supports parcelable/union, String, "
567 "IBinder, and ParcelFileDescriptor.";
568 return false;
569 }
570 } else { // Defined types
571 if (typenames.GetInterface(contained_type)) {
572 AIDL_ERROR(this) << "List<" << contained_type_name
573 << "> is not supported. List<T> supports parcelable/union, String, "
574 "IBinder, and ParcelFileDescriptor.";
575 return false;
576 }
577 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900578 } else if (type_name == "Map") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800579 if (num_params != 0 && num_params != 2) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900580 AIDL_ERROR(this) << "Map must have 0 or 2 type parameters, but got "
Jooyung Han965e31d2020-11-27 12:30:16 +0900581 << "'" << Signature() << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900582 return false;
583 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800584 if (num_params == 2) {
Jooyung Hanaab242a2021-02-15 19:01:15 +0900585 const string& key_type = GetTypeParameters()[0]->Signature();
Jeongik Chae48d9942020-01-02 17:39:00 +0900586 if (key_type != "String") {
587 AIDL_ERROR(this) << "The type of key in map must be String, but it is "
588 << "'" << key_type << "'";
589 return false;
590 }
591 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800592 } else if (is_user_defined_generic_type) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900593 const size_t allowed = parameterizable->GetTypeParameters().size();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800594 if (num_params != allowed) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900595 AIDL_ERROR(this) << type_name << " must have " << allowed << " type parameters, but got "
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800596 << num_params;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900597 return false;
598 }
599 } else {
600 AIDL_ERROR(this) << type_name << " is not a generic type.";
601 return false;
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900602 }
603 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900604
Steven Moreland11cb9452020-01-21 16:56:58 -0800605 const bool is_generic_string_list = GetName() == "List" && IsGeneric() &&
606 GetTypeParameters().size() == 1 &&
607 GetTypeParameters()[0]->GetName() == "String";
608 if (IsUtf8InCpp() && (GetName() != "String" && !is_generic_string_list)) {
609 AIDL_ERROR(this) << "@utf8InCpp can only be used on String, String[], and List<String>.";
610 return false;
611 }
612
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900613 if (GetName() == "void") {
614 if (IsArray() || IsNullable() || IsUtf8InCpp()) {
615 AIDL_ERROR(this) << "void type cannot be an array or nullable or utf8 string";
616 return false;
617 }
618 }
619
620 if (IsArray()) {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800621 const auto defined_type = typenames.TryGetDefinedType(GetName());
622 if (defined_type != nullptr && defined_type->AsInterface() != nullptr) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900623 AIDL_ERROR(this) << "Binder type cannot be an array";
624 return false;
625 }
Steven Moreland8042d2d2020-09-30 23:31:32 +0000626 if (GetName() == "ParcelableHolder") {
627 AIDL_ERROR(this) << "Arrays of ParcelableHolder are not supported.";
628 return false;
629 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900630 }
631
632 if (IsNullable()) {
633 if (AidlTypenames::IsPrimitiveTypename(GetName()) && !IsArray()) {
634 AIDL_ERROR(this) << "Primitive type cannot get nullable annotation";
635 return false;
636 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800637 const auto defined_type = typenames.TryGetDefinedType(GetName());
638 if (defined_type != nullptr && defined_type->AsEnumDeclaration() != nullptr && !IsArray()) {
Daniel Normanee8674f2019-09-20 16:07:00 -0700639 AIDL_ERROR(this) << "Enum type cannot get nullable annotation";
640 return false;
641 }
Jeongik Chaf6ec8982020-10-15 00:10:30 +0900642 if (GetName() == "ParcelableHolder") {
643 AIDL_ERROR(this) << "ParcelableHolder cannot be nullable.";
644 return false;
645 }
Jooyung Han01720ed2021-08-13 07:46:07 +0900646 if (IsHeapNullable()) {
647 if (!defined_type || IsArray() || !defined_type->AsParcelable()) {
648 AIDL_ERROR(this) << "@nullable(heap=true) is available to parcelables.";
649 return false;
650 }
651 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900652 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900653 return true;
654}
655
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900656std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type,
Steven Moreland860b1942018-08-16 14:59:28 -0700657 const std::string& raw_value) {
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900658 if (type.IsArray()) {
659 return raw_value;
660 }
661
662 if (auto defined_type = type.GetDefinedType(); defined_type) {
663 auto enum_type = defined_type->AsEnumDeclaration();
664 AIDL_FATAL_IF(!enum_type, type) << "Invalid type for \"" << raw_value << "\"";
665 return type.GetName() + "." + raw_value.substr(raw_value.find_last_of('.') + 1);
666 }
Steven Moreland860b1942018-08-16 14:59:28 -0700667 return raw_value;
668}
669
Steven Moreland46e9da82018-07-27 15:45:29 -0700670AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
671 AidlTypeSpecifier* type, const std::string& name)
Steven Moreland541788d2020-05-21 22:05:52 +0000672 : AidlVariableDeclaration(location, type, name, AidlConstantValue::Default(*type)) {
673 default_user_specified_ = false;
674}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700675
Steven Moreland46e9da82018-07-27 15:45:29 -0700676AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
677 AidlTypeSpecifier* type, const std::string& name,
678 AidlConstantValue* default_value)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900679 : AidlMember(location, type->GetComments()),
Steven Moreland541788d2020-05-21 22:05:52 +0000680 type_(type),
681 name_(name),
682 default_user_specified_(true),
683 default_value_(default_value) {}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700684
Jooyung Han53fb4242020-12-17 16:03:49 +0900685bool AidlVariableDeclaration::HasUsefulDefaultValue() const {
686 if (GetDefaultValue()) {
687 return true;
688 }
689 // null is accepted as a valid default value in all backends
690 if (GetType().IsNullable()) {
691 return true;
692 }
693 return false;
694}
695
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900696bool AidlVariableDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700697 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900698 valid &= type_->CheckValid(typenames);
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900699
Steven Moreland54be7bd2019-12-05 11:17:53 -0800700 if (type_->GetName() == "void") {
701 AIDL_ERROR(this) << "Declaration " << name_
702 << " is void, but declarations cannot be of void type.";
703 valid = false;
704 }
705
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900706 if (default_value_ == nullptr) return valid;
Steven Moreland25294322018-08-07 18:13:55 -0700707 valid &= default_value_->CheckValid();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700708
Steven Moreland25294322018-08-07 18:13:55 -0700709 if (!valid) return false;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700710
Steven Moreland860b1942018-08-16 14:59:28 -0700711 return !ValueString(AidlConstantValueDecorator).empty();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700712}
Steven Moreland5557f1c2018-07-02 13:50:23 -0700713
Jooyung Hanacae85d2020-10-28 16:39:09 +0900714string AidlVariableDeclaration::GetCapitalizedName() const {
715 AIDL_FATAL_IF(name_.size() <= 0, *this) << "Name can't be empty.";
716 string str = name_;
717 str[0] = static_cast<char>(toupper(str[0]));
718 return str;
719}
720
Steven Moreland5557f1c2018-07-02 13:50:23 -0700721string AidlVariableDeclaration::ToString() const {
Jooyung Han965e31d2020-11-27 12:30:16 +0900722 string ret = type_->ToString() + " " + name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000723 if (default_value_ != nullptr && default_user_specified_) {
Steven Moreland860b1942018-08-16 14:59:28 -0700724 ret += " = " + ValueString(AidlConstantValueDecorator);
Steven Moreland9ea10e32018-07-19 15:26:09 -0700725 }
726 return ret;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700727}
728
Jiyong Park02da7422018-07-16 16:00:26 +0900729string AidlVariableDeclaration::Signature() const {
730 return type_->Signature() + " " + name_;
731}
732
Steven Moreland860b1942018-08-16 14:59:28 -0700733std::string AidlVariableDeclaration::ValueString(const ConstantValueDecorator& decorator) const {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900734 if (default_value_ != nullptr) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700735 return default_value_->ValueString(GetType(), decorator);
Jiyong Parka468e2a2018-08-29 21:25:18 +0900736 } else {
737 return "";
738 }
Steven Moreland25294322018-08-07 18:13:55 -0700739}
740
Jooyung Hanc5688f72021-01-05 15:41:48 +0900741void AidlVariableDeclaration::TraverseChildren(
742 std::function<void(const AidlNode&)> traverse) const {
743 traverse(GetType());
744 if (IsDefaultUserSpecified()) {
745 traverse(*GetDefaultValue());
746 }
747}
748
Steven Moreland46e9da82018-07-27 15:45:29 -0700749AidlArgument::AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
750 AidlTypeSpecifier* type, const std::string& name)
751 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700752 direction_(direction),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700753 direction_specified_(true) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700754
Steven Moreland46e9da82018-07-27 15:45:29 -0700755AidlArgument::AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type,
756 const std::string& name)
757 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700758 direction_(AidlArgument::IN_DIR),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700759 direction_specified_(false) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700760
Jooyung Han020d8d12021-02-26 17:23:02 +0900761static std::string to_string(AidlArgument::Direction direction) {
762 switch (direction) {
763 case AidlArgument::IN_DIR:
764 return "in";
765 case AidlArgument::OUT_DIR:
766 return "out";
767 case AidlArgument::INOUT_DIR:
768 return "inout";
769 }
770}
771
Jiyong Park02da7422018-07-16 16:00:26 +0900772string AidlArgument::GetDirectionSpecifier() const {
Casey Dahlinc378c992015-09-29 16:50:40 -0700773 string ret;
Casey Dahlinc378c992015-09-29 16:50:40 -0700774 if (direction_specified_) {
Jooyung Han020d8d12021-02-26 17:23:02 +0900775 ret = to_string(direction_);
Casey Dahlinc378c992015-09-29 16:50:40 -0700776 }
Casey Dahlinc378c992015-09-29 16:50:40 -0700777 return ret;
778}
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700779
Jiyong Park02da7422018-07-16 16:00:26 +0900780string AidlArgument::ToString() const {
Devin Mooreeccdb902020-03-24 16:22:40 -0700781 if (direction_specified_) {
782 return GetDirectionSpecifier() + " " + AidlVariableDeclaration::ToString();
783 } else {
784 return AidlVariableDeclaration::ToString();
785 }
Jiyong Park02da7422018-07-16 16:00:26 +0900786}
787
Jooyung Han020d8d12021-02-26 17:23:02 +0900788static std::string FormatDirections(const std::set<AidlArgument::Direction>& directions) {
789 std::vector<std::string> out;
790 for (const auto& d : directions) {
791 out.push_back(to_string(d));
792 }
793
794 if (out.size() <= 1) { // [] => "" or [A] => "A"
795 return Join(out, "");
796 } else if (out.size() == 2) { // [A,B] => "A or B"
797 return Join(out, " or ");
798 } else { // [A,B,C] => "A, B, or C"
799 out.back() = "or " + out.back();
800 return Join(out, ", ");
801 }
802}
803
804bool AidlArgument::CheckValid(const AidlTypenames& typenames) const {
805 if (!GetType().CheckValid(typenames)) {
806 return false;
807 }
808
809 const auto& aspect = typenames.GetArgumentAspect(GetType());
810
811 if (aspect.possible_directions.size() == 0) {
812 AIDL_ERROR(this) << aspect.name << " cannot be an argument type";
813 return false;
814 }
815
816 // when direction is not specified, "in" is assumed and should be the only possible direction
817 if (!DirectionWasSpecified() && aspect.possible_directions != std::set{AidlArgument::IN_DIR}) {
818 AIDL_ERROR(this) << "The direction of '" << GetName() << "' is not specified. " << aspect.name
819 << " can be an " << FormatDirections(aspect.possible_directions)
820 << " parameter.";
821 return false;
822 }
823
824 if (aspect.possible_directions.count(GetDirection()) == 0) {
825 AIDL_ERROR(this) << "'" << GetName() << "' can't be an " << GetDirectionSpecifier()
826 << " parameter because " << aspect.name << " can only be an "
827 << FormatDirections(aspect.possible_directions) << " parameter.";
828 return false;
829 }
830
831 return true;
832}
833
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900834bool AidlCommentable::IsHidden() const {
Jooyung Han24effbf2021-01-16 10:24:03 +0900835 return android::aidl::HasHideInComments(GetComments());
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900836}
837
838bool AidlCommentable::IsDeprecated() const {
Jooyung Hand4fe00e2021-01-11 16:21:53 +0900839 return android::aidl::FindDeprecated(GetComments()).has_value();
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900840}
841
Jooyung Han8451a202021-01-16 03:07:06 +0900842AidlMember::AidlMember(const AidlLocation& location, const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +0900843 : AidlCommentable(location, comments) {}
Steven Moreland46e9da82018-07-27 15:45:29 -0700844
Steven Moreland46e9da82018-07-27 15:45:29 -0700845AidlConstantDeclaration::AidlConstantDeclaration(const AidlLocation& location,
846 AidlTypeSpecifier* type, const std::string& name,
847 AidlConstantValue* value)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900848 : AidlMember(location, type->GetComments()), type_(type), name_(name), value_(value) {}
Steven Moreland693640b2018-07-19 13:46:27 -0700849
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900850bool AidlConstantDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700851 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900852 valid &= type_->CheckValid(typenames);
Steven Moreland25294322018-08-07 18:13:55 -0700853 valid &= value_->CheckValid();
854 if (!valid) return false;
Steven Moreland693640b2018-07-19 13:46:27 -0700855
Steven Morelande689da22020-11-10 02:06:30 +0000856 const static set<string> kSupportedConstTypes = {"String", "byte", "int", "long"};
Jooyung Han965e31d2020-11-27 12:30:16 +0900857 if (kSupportedConstTypes.find(type_->Signature()) == kSupportedConstTypes.end()) {
858 AIDL_ERROR(this) << "Constant of type " << type_->Signature() << " is not supported.";
Steven Moreland693640b2018-07-19 13:46:27 -0700859 return false;
860 }
861
Will McVickerd7d18df2019-09-12 13:40:50 -0700862 return true;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700863}
864
Jiyong Parka428d212018-08-29 22:26:30 +0900865string AidlConstantDeclaration::ToString() const {
Jooyung Hanb3ca6302020-11-27 14:13:27 +0900866 return "const " + type_->ToString() + " " + name_ + " = " +
867 ValueString(AidlConstantValueDecorator);
Jiyong Parka428d212018-08-29 22:26:30 +0900868}
869
870string AidlConstantDeclaration::Signature() const {
871 return type_->Signature() + " " + name_;
872}
873
Steven Moreland46e9da82018-07-27 15:45:29 -0700874AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
875 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jooyung Han8451a202021-01-16 03:07:06 +0900876 const Comments& comments)
Jiyong Parkb034bf02018-07-30 17:44:33 +0900877 : AidlMethod(location, oneway, type, name, args, comments, 0, true) {
878 has_id_ = false;
879}
880
881AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
882 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jooyung Han8451a202021-01-16 03:07:06 +0900883 const Comments& comments, int id, bool is_user_defined)
Jooyung Han8aeef8c2021-01-11 12:16:19 +0900884 : AidlMember(location, comments),
Steven Moreland46e9da82018-07-27 15:45:29 -0700885 oneway_(oneway),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700886 type_(type),
887 name_(name),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700888 arguments_(std::move(*args)),
Jiyong Parkb034bf02018-07-30 17:44:33 +0900889 id_(id),
890 is_user_defined_(is_user_defined) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700891 has_id_ = true;
892 delete args;
Christopher Wileyad339272015-10-05 19:11:58 -0700893 for (const unique_ptr<AidlArgument>& a : arguments_) {
894 if (a->IsIn()) { in_arguments_.push_back(a.get()); }
895 if (a->IsOut()) { out_arguments_.push_back(a.get()); }
896 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700897}
898
Jiyong Park02da7422018-07-16 16:00:26 +0900899string AidlMethod::Signature() const {
900 vector<string> arg_signatures;
901 for (const auto& arg : GetArguments()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900902 arg_signatures.emplace_back(arg->GetType().Signature());
Jiyong Park02da7422018-07-16 16:00:26 +0900903 }
Jiyong Park309668e2018-07-28 16:55:44 +0900904 return GetName() + "(" + Join(arg_signatures, ", ") + ")";
905}
906
907string AidlMethod::ToString() const {
908 vector<string> arg_strings;
909 for (const auto& arg : GetArguments()) {
Jooyung Han965e31d2020-11-27 12:30:16 +0900910 arg_strings.emplace_back(arg->ToString());
Jiyong Park309668e2018-07-28 16:55:44 +0900911 }
Jooyung Han965e31d2020-11-27 12:30:16 +0900912 string ret = (IsOneway() ? "oneway " : "") + GetType().ToString() + " " + GetName() + "(" +
Steven Moreland4ee68632018-12-14 15:52:46 -0800913 Join(arg_strings, ", ") + ")";
Jiyong Parked65bf42018-08-28 15:43:27 +0900914 if (HasId()) {
915 ret += " = " + std::to_string(GetId());
916 }
917 return ret;
Jiyong Park02da7422018-07-16 16:00:26 +0900918}
919
Steven Moreland46e9da82018-07-27 15:45:29 -0700920AidlDefinedType::AidlDefinedType(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +0900921 const Comments& comments, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900922 std::vector<std::unique_ptr<AidlMember>>* members)
Jooyung Han35784982021-06-29 06:26:12 +0900923 : AidlAnnotatable(location, comments), AidlScope(this), name_(name), package_(package) {
Jooyung Han93f48f02021-06-05 00:11:16 +0900924 // adjust name/package when name is fully qualified (for preprocessed files)
925 if (package_.empty() && name_.find('.') != std::string::npos) {
926 // Note that this logic is absolutely wrong. Given a parcelable
927 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
928 // the class is just Bar. However, this was the way it was done in the past.
929 //
930 // See b/17415692
931 auto pos = name.rfind('.');
932 // name is the last part.
933 name_ = name.substr(pos + 1);
934 // package is the initial parts (except the last).
935 package_ = name.substr(0, pos);
936 }
Jooyung Han829ec7c2020-12-02 12:07:36 +0900937 if (members) {
938 for (auto& m : *members) {
939 if (auto constant = m->AsConstantDeclaration(); constant) {
940 constants_.emplace_back(constant);
941 } else if (auto variable = m->AsVariableDeclaration(); variable) {
942 variables_.emplace_back(variable);
943 } else if (auto method = m->AsMethod(); method) {
944 methods_.emplace_back(method);
945 } else {
946 AIDL_FATAL(*m);
947 }
948 members_.push_back(m.release());
949 }
950 delete members;
951 }
952}
Steven Moreland787b0432018-07-03 09:00:58 -0700953
Jooyung Han808a2a02020-12-28 16:46:54 +0900954bool AidlDefinedType::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -0800955 if (!AidlAnnotatable::CheckValid(typenames)) {
956 return false;
957 }
Jooyung Han829ec7c2020-12-02 12:07:36 +0900958 if (!CheckValidWithMembers(typenames)) {
959 return false;
960 }
Devin Moore24f68572020-02-26 13:20:59 -0800961 return true;
962}
963
Steven Moreland787b0432018-07-03 09:00:58 -0700964std::string AidlDefinedType::GetCanonicalName() const {
965 if (package_.empty()) {
966 return GetName();
967 }
968 return GetPackage() + "." + GetName();
969}
970
Jooyung Han829ec7c2020-12-02 12:07:36 +0900971bool AidlDefinedType::CheckValidWithMembers(const AidlTypenames& typenames) const {
972 bool success = true;
973
974 for (const auto& v : GetFields()) {
975 const bool field_valid = v->CheckValid(typenames);
976 success = success && field_valid;
977 }
978
979 // field names should be unique
980 std::set<std::string> fieldnames;
981 for (const auto& v : GetFields()) {
982 bool duplicated = !fieldnames.emplace(v->GetName()).second;
983 if (duplicated) {
984 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName() << "'";
985 success = false;
986 }
987 }
988
989 // immutable parcelables should have immutable fields.
990 if (IsJavaOnlyImmutable()) {
991 for (const auto& v : GetFields()) {
992 if (!typenames.CanBeJavaOnlyImmutable(v->GetType())) {
993 AIDL_ERROR(v) << "The @JavaOnlyImmutable '" << GetName() << "' has a "
994 << "non-immutable field named '" << v->GetName() << "'.";
995 success = false;
996 }
997 }
998 }
999
1000 set<string> constant_names;
1001 for (const auto& constant : GetConstantDeclarations()) {
1002 if (constant_names.count(constant->GetName()) > 0) {
1003 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
1004 success = false;
1005 }
1006 constant_names.insert(constant->GetName());
1007 success = success && constant->CheckValid(typenames);
1008 }
1009
1010 return success;
1011}
1012
1013bool AidlDefinedType::CheckValidForGetterNames() const {
1014 bool success = true;
1015 std::set<std::string> getters;
1016 for (const auto& v : GetFields()) {
1017 bool duplicated = !getters.emplace(v->GetCapitalizedName()).second;
1018 if (duplicated) {
1019 AIDL_ERROR(v) << "'" << GetName() << "' has duplicate field name '" << v->GetName()
1020 << "' after capitalizing the first letter";
1021 success = false;
1022 }
1023 }
1024 return success;
1025}
1026
Jooyung Han13f1fa52021-06-11 18:06:12 +09001027std::string AidlDefinedType::ResolveName(const std::string& name) const {
1028 // TODO(b/182508839): resolve with nested types when we support nested types
1029 //
1030 // For example, in the following (the syntax is TBD), t1's Type is x.Foo.Bar.Type
1031 // while t2's Type is y.Type.
1032 //
1033 // package x;
1034 // import y.Type;
1035 // parcelable Foo {
1036 // parcelable Bar {
1037 // enum Type { Type1, Type2 }
1038 // Type t1;
1039 // }
1040 // Type t2;
1041 // }
1042 AIDL_FATAL_IF(!GetEnclosingScope(), this)
1043 << "Type should have an enclosing scope.(e.g. AidlDocument)";
1044 return GetEnclosingScope()->ResolveName(name);
1045}
1046
Jooyung Han35784982021-06-29 06:26:12 +09001047template <typename T>
1048const T* AidlCast(const AidlNode& node) {
1049 struct CastVisitor : AidlVisitor {
1050 const T* cast = nullptr;
1051 void Visit(const T& t) override { cast = &t; }
1052 } visitor;
1053 node.DispatchVisit(visitor);
1054 return visitor.cast;
1055}
1056
1057const AidlDocument& AidlDefinedType::GetDocument() const {
1058 // TODO(b/182508839): resolve with nested types when we support nested types
1059 auto scope = GetEnclosingScope();
1060 AIDL_FATAL_IF(!scope, this) << "no scope defined.";
1061 auto doc = AidlCast<AidlDocument>(scope->GetNode());
1062 AIDL_FATAL_IF(!doc, this) << "scope is not a document.";
1063 return *doc;
1064}
1065
Jiyong Park18132182020-06-08 20:24:40 +09001066AidlParcelable::AidlParcelable(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001067 const std::string& package, const Comments& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001068 const std::string& cpp_header, std::vector<std::string>* type_params,
1069 std::vector<std::unique_ptr<AidlMember>>* members)
1070 : AidlDefinedType(location, name, comments, package, members),
Jeongik Chadf76dc72019-11-28 00:08:47 +09001071 AidlParameterizable<std::string>(type_params),
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -08001072 cpp_header_(cpp_header) {
1073 // Strip off quotation marks if we actually have a cpp header.
1074 if (cpp_header_.length() >= 2) {
1075 cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2);
1076 }
Casey Dahlin59401da2015-10-09 18:16:45 -07001077}
Jeongik Chadf76dc72019-11-28 00:08:47 +09001078template <typename T>
1079AidlParameterizable<T>::AidlParameterizable(const AidlParameterizable& other) {
1080 // Copying is not supported if it has type parameters.
1081 // It doesn't make a problem because only ArrayBase() makes a copy,
1082 // and it can be called only if a type is not generic.
Steven Moreland21780812020-09-11 01:29:45 +00001083 AIDL_FATAL_IF(other.IsGeneric(), AIDL_LOCATION_HERE);
Jeongik Chadf76dc72019-11-28 00:08:47 +09001084}
1085
1086template <typename T>
1087bool AidlParameterizable<T>::CheckValid() const {
1088 return true;
1089};
1090
1091template <>
1092bool AidlParameterizable<std::string>::CheckValid() const {
1093 if (!IsGeneric()) {
1094 return true;
1095 }
1096 std::unordered_set<std::string> set(GetTypeParameters().begin(), GetTypeParameters().end());
1097 if (set.size() != GetTypeParameters().size()) {
1098 AIDL_ERROR(this->AsAidlNode()) << "Every type parameter should be unique.";
1099 return false;
1100 }
1101 return true;
1102}
Casey Dahlin59401da2015-10-09 18:16:45 -07001103
Jooyung Han808a2a02020-12-28 16:46:54 +09001104bool AidlParcelable::CheckValid(const AidlTypenames& typenames) const {
1105 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001106 return false;
1107 }
Jeongik Chadf76dc72019-11-28 00:08:47 +09001108 if (!AidlParameterizable<std::string>::CheckValid()) {
1109 return false;
1110 }
Jeongik Cha82317dd2019-02-27 20:26:11 +09001111
1112 return true;
1113}
1114
Steven Moreland5557f1c2018-07-02 13:50:23 -07001115AidlStructuredParcelable::AidlStructuredParcelable(
Jiyong Park18132182020-06-08 20:24:40 +09001116 const AidlLocation& location, const std::string& name, const std::string& package,
Jooyung Han8451a202021-01-16 03:07:06 +09001117 const Comments& comments, std::vector<std::string>* type_params,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001118 std::vector<std::unique_ptr<AidlMember>>* members)
1119 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Steven Moreland5557f1c2018-07-02 13:50:23 -07001120
Jooyung Han808a2a02020-12-28 16:46:54 +09001121bool AidlStructuredParcelable::CheckValid(const AidlTypenames& typenames) const {
1122 if (!AidlParcelable::CheckValid(typenames)) {
Devin Moore24f68572020-02-26 13:20:59 -08001123 return false;
1124 }
Jeongik Cha13066da2020-08-06 15:43:19 +09001125
Jooyung Han59af9cc2020-10-25 21:44:14 +09001126 bool success = true;
Jeongik Cha36f76c32020-07-28 00:25:52 +09001127
Jooyung Hand4057c42020-10-23 13:28:22 +09001128 if (IsFixedSize()) {
1129 for (const auto& v : GetFields()) {
1130 if (!typenames.CanBeFixedSize(v->GetType())) {
1131 AIDL_ERROR(v) << "The @FixedSize parcelable '" << this->GetName() << "' has a "
1132 << "non-fixed size field named " << v->GetName() << ".";
1133 success = false;
1134 }
1135 }
1136 }
1137
1138 if (IsJavaOnlyImmutable()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001139 // Immutable parcelables provide getters
Jooyung Han829ec7c2020-12-02 12:07:36 +09001140 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001141 success = false;
Jooyung Hand4057c42020-10-23 13:28:22 +09001142 }
1143 }
1144
Daniel Norman85aed542019-08-21 12:01:14 -07001145 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001146}
1147
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001148// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -07001149bool AidlTypeSpecifier::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1150 Options::Language lang) const {
Jooyung Hand09a21d2021-02-15 18:56:55 +09001151 if (IsGeneric()) {
1152 const auto& types = GetTypeParameters();
1153 for (const auto& arg : types) {
1154 if (!arg->LanguageSpecificCheckValid(typenames, lang)) {
1155 return false;
1156 }
1157 }
1158 }
1159
Andrei Homescub62afd92020-05-11 19:24:59 -07001160 if ((lang == Options::Language::NDK || lang == Options::Language::RUST) && IsArray() &&
1161 GetName() == "IBinder") {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001162 AIDL_ERROR(this) << "The " << to_string(lang) << " backend does not support array of IBinder";
Steven Moreland0185d9b2020-05-15 23:21:22 +00001163 return false;
1164 }
Jeongik Cha8f02a532020-10-14 00:16:28 +09001165 if (lang == Options::Language::RUST && GetName() == "ParcelableHolder") {
1166 // TODO(b/146611855): Remove it when Rust backend supports ParcelableHolder
1167 AIDL_ERROR(this) << "The Rust backend does not support ParcelableHolder yet.";
Jeongik Cha225519b2020-08-29 01:55:32 +09001168 return false;
1169 }
Andrei Homescub62afd92020-05-11 19:24:59 -07001170 if ((lang == Options::Language::NDK || lang == Options::Language::RUST) && IsArray() &&
1171 IsNullable()) {
Steven Moreland0185d9b2020-05-15 23:21:22 +00001172 if (GetName() == "ParcelFileDescriptor") {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001173 AIDL_ERROR(this) << "The " << to_string(lang)
Andrei Homescub62afd92020-05-11 19:24:59 -07001174 << " backend does not support nullable array of ParcelFileDescriptor";
Steven Moreland0185d9b2020-05-15 23:21:22 +00001175 return false;
1176 }
1177
Steven Morelandd59e3172020-05-11 16:42:09 -07001178 const auto defined_type = typenames.TryGetDefinedType(GetName());
1179 if (defined_type != nullptr && defined_type->AsParcelable() != nullptr) {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001180 AIDL_ERROR(this) << "The " << to_string(lang)
Andrei Homescub62afd92020-05-11 19:24:59 -07001181 << " backend does not support nullable array of parcelable";
Steven Morelandd59e3172020-05-11 16:42:09 -07001182 return false;
1183 }
1184 }
Andrei Homescub62afd92020-05-11 19:24:59 -07001185 if (this->GetName() == "FileDescriptor" &&
1186 (lang == Options::Language::NDK || lang == Options::Language::RUST)) {
Jooyung Han9435e9a2021-01-06 10:16:31 +09001187 AIDL_ERROR(this) << "FileDescriptor isn't supported by the " << to_string(lang) << " backend.";
Steven Morelandc8a4ca82020-01-21 17:50:08 -08001188 return false;
1189 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001190 if (this->IsGeneric()) {
1191 if (this->GetName() == "List") {
Jooyung Han55f96ad2020-12-13 10:08:33 +09001192 if (lang == Options::Language::NDK) {
1193 const AidlTypeSpecifier& contained_type = *GetTypeParameters()[0];
1194 const string& contained_type_name = contained_type.GetName();
Jooyung Hane87cdd02020-12-11 16:47:35 +09001195 if (typenames.GetInterface(contained_type)) {
1196 AIDL_ERROR(this) << "List<" << contained_type_name
1197 << "> is not supported. List in NDK doesn't support interface.";
1198 return false;
1199 }
1200 if (contained_type_name == "IBinder") {
1201 AIDL_ERROR(this) << "List<" << contained_type_name
1202 << "> is not supported. List in NDK doesn't support IBinder.";
1203 return false;
Jeongik Cha08ca2182019-11-21 14:01:13 +09001204 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001205 }
Jeongik Chabb55b5e2020-01-07 23:11:26 +09001206 }
1207 }
Devin Moore6a01ca12020-08-28 10:24:19 -07001208
1209 if (this->IsArray()) {
1210 if (this->GetName() == "List" || this->GetName() == "Map" ||
1211 this->GetName() == "CharSequence") {
1212 AIDL_ERROR(this) << this->GetName() << "[] is not supported.";
Jeongik Chabb55b5e2020-01-07 23:11:26 +09001213 return false;
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001214 }
1215 }
Devin Moore6a01ca12020-08-28 10:24:19 -07001216
1217 if (lang != Options::Language::JAVA) {
1218 if (this->GetName() == "List" && !this->IsGeneric()) {
1219 AIDL_ERROR(this) << "Currently, only the Java backend supports non-generic List.";
1220 return false;
1221 }
1222 if (this->GetName() == "Map" || this->GetName() == "CharSequence") {
1223 AIDL_ERROR(this) << "Currently, only Java backend supports " << this->GetName() << ".";
1224 return false;
Jeongik Cha08ca2182019-11-21 14:01:13 +09001225 }
1226 }
1227
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001228 return true;
1229}
1230
1231// TODO: we should treat every backend all the same in future.
Jooyung Han2cb8ecd2021-07-28 18:41:30 +09001232bool AidlParcelable::LanguageSpecificCheckValid(const AidlTypenames& typenames,
Steven Morelandd59e3172020-05-11 16:42:09 -07001233 Options::Language lang) const {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001234 for (const auto& v : this->GetFields()) {
Steven Morelandd59e3172020-05-11 16:42:09 -07001235 if (!v->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001236 return false;
1237 }
1238 }
1239 return true;
1240}
1241
Daniel Norman85aed542019-08-21 12:01:14 -07001242AidlEnumerator::AidlEnumerator(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001243 AidlConstantValue* value, const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +09001244 : AidlCommentable(location, comments),
Jooyung Han29813842020-12-08 01:28:03 +09001245 name_(name),
1246 value_(value),
Jooyung Han29813842020-12-08 01:28:03 +09001247 value_user_specified_(value != nullptr) {}
Daniel Norman85aed542019-08-21 12:01:14 -07001248
1249bool AidlEnumerator::CheckValid(const AidlTypeSpecifier& enum_backing_type) const {
1250 if (GetValue() == nullptr) {
1251 return false;
1252 }
1253 if (!GetValue()->CheckValid()) {
1254 return false;
1255 }
Will McVickerd7d18df2019-09-12 13:40:50 -07001256 if (GetValue()->ValueString(enum_backing_type, AidlConstantValueDecorator).empty()) {
Daniel Norman85aed542019-08-21 12:01:14 -07001257 AIDL_ERROR(this) << "Enumerator type differs from enum backing type.";
1258 return false;
1259 }
1260 return true;
1261}
1262
1263string AidlEnumerator::ValueString(const AidlTypeSpecifier& backing_type,
1264 const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -07001265 return GetValue()->ValueString(backing_type, decorator);
Daniel Norman85aed542019-08-21 12:01:14 -07001266}
1267
1268AidlEnumDeclaration::AidlEnumDeclaration(const AidlLocation& location, const std::string& name,
1269 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jooyung Han8451a202021-01-16 03:07:06 +09001270 const std::string& package, const Comments& comments)
Jooyung Han829ec7c2020-12-02 12:07:36 +09001271 : AidlDefinedType(location, name, comments, package, nullptr),
Jooyung Han29813842020-12-08 01:28:03 +09001272 enumerators_(std::move(*enumerators)) {
Jooyung Han672557b2020-12-24 05:18:00 +09001273 // Fill missing enumerator values with <prev + 1>
1274 // This can't be done in Autofill() because type/ref resolution depends on this.
1275 // For example, with enum E { A, B = A }, B's value 'A' is a reference which can't be
1276 // resolved if A has no value set.
Daniel Normanb28684e2019-10-17 15:31:39 -07001277 const AidlEnumerator* previous = nullptr;
1278 for (const auto& enumerator : enumerators_) {
1279 if (enumerator->GetValue() == nullptr) {
Jooyung Han29813842020-12-08 01:28:03 +09001280 auto loc = enumerator->GetLocation();
Daniel Normanb28684e2019-10-17 15:31:39 -07001281 if (previous == nullptr) {
Devin Mooredf93ebb2020-03-25 14:03:35 -07001282 enumerator->SetValue(
Jooyung Han29813842020-12-08 01:28:03 +09001283 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "0")));
Daniel Normanb28684e2019-10-17 15:31:39 -07001284 } else {
Jooyung Hand0c8af02021-01-06 18:08:01 +09001285 auto prev_value = std::make_unique<AidlConstantReference>(loc, previous->GetName());
Daniel Normanb28684e2019-10-17 15:31:39 -07001286 enumerator->SetValue(std::make_unique<AidlBinaryConstExpression>(
Jooyung Han29813842020-12-08 01:28:03 +09001287 loc, std::move(prev_value), "+",
1288 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(loc, "1"))));
Daniel Normanb28684e2019-10-17 15:31:39 -07001289 }
1290 }
1291 previous = enumerator.get();
1292 }
1293}
1294
Jooyung Han672557b2020-12-24 05:18:00 +09001295bool AidlEnumDeclaration::Autofill(const AidlTypenames& typenames) {
1296 if (auto annot = BackingType(); annot != nullptr) {
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001297 // Autofill() is called before the grand CheckValid(). But AidlAnnotation::ParamValue()
1298 // calls AidlConstantValue::evaluate() which requires CheckValid() to be called before. So we
Jooyung Han672557b2020-12-24 05:18:00 +09001299 // need to call CheckValid().
1300 if (!annot->CheckValid()) {
1301 return false;
1302 }
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001303 auto type = annot->ParamValue<std::string>("type").value();
1304 backing_type_ =
Jooyung Han8451a202021-01-16 03:07:06 +09001305 std::make_unique<AidlTypeSpecifier>(annot->GetLocation(), type, false, nullptr, Comments{});
Jooyung Han672557b2020-12-24 05:18:00 +09001306 } else {
1307 // Default to byte type for enums.
1308 backing_type_ =
Jooyung Han8451a202021-01-16 03:07:06 +09001309 std::make_unique<AidlTypeSpecifier>(AIDL_LOCATION_HERE, "byte", false, nullptr, Comments{});
Jooyung Han672557b2020-12-24 05:18:00 +09001310 }
1311 // Autofill() is called after type resolution, we resolve the backing type manually.
Jooyung Han13f1fa52021-06-11 18:06:12 +09001312 if (!backing_type_->Resolve(typenames, nullptr)) {
Jooyung Han672557b2020-12-24 05:18:00 +09001313 AIDL_ERROR(this) << "Invalid backing type: " << backing_type_->GetName();
1314 }
1315 return true;
1316}
1317
Jooyung Han808a2a02020-12-28 16:46:54 +09001318bool AidlEnumDeclaration::CheckValid(const AidlTypenames& typenames) const {
1319 if (!AidlDefinedType::CheckValid(typenames)) {
Devin Moore24f68572020-02-26 13:20:59 -08001320 return false;
1321 }
Jooyung Han829ec7c2020-12-02 12:07:36 +09001322 if (!GetMembers().empty()) {
1323 AIDL_ERROR(this) << "Enum doesn't support fields/constants/methods.";
1324 return false;
1325 }
Daniel Norman85aed542019-08-21 12:01:14 -07001326 if (backing_type_ == nullptr) {
1327 AIDL_ERROR(this) << "Enum declaration missing backing type.";
1328 return false;
1329 }
1330 bool success = true;
1331 for (const auto& enumerator : enumerators_) {
1332 success = success && enumerator->CheckValid(GetBackingType());
1333 }
Jooyung Han3b990182020-12-22 17:44:31 +09001334
Jooyung Han808a2a02020-12-28 16:46:54 +09001335 return success;
Daniel Norman85aed542019-08-21 12:01:14 -07001336}
1337
Jooyung Han2946afc2020-10-05 20:29:16 +09001338AidlUnionDecl::AidlUnionDecl(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001339 const std::string& package, const Comments& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001340 std::vector<std::string>* type_params,
1341 std::vector<std::unique_ptr<AidlMember>>* members)
1342 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params, members) {}
Jooyung Han2946afc2020-10-05 20:29:16 +09001343
Jooyung Han808a2a02020-12-28 16:46:54 +09001344bool AidlUnionDecl::CheckValid(const AidlTypenames& typenames) const {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001345 // visit parents
Jooyung Han808a2a02020-12-28 16:46:54 +09001346 if (!AidlParcelable::CheckValid(typenames)) {
Jooyung Hanfe89f122020-10-14 03:49:18 +09001347 return false;
1348 }
Jooyung Han59af9cc2020-10-25 21:44:14 +09001349
1350 // unions provide getters always
Jooyung Han829ec7c2020-12-02 12:07:36 +09001351 if (!CheckValidForGetterNames()) {
Jooyung Han59af9cc2020-10-25 21:44:14 +09001352 return false;
Jooyung Hanfe89f122020-10-14 03:49:18 +09001353 }
1354
1355 // now, visit self!
1356 bool success = true;
1357
1358 // TODO(b/170807936) do we need to allow ParcelableHolder in union?
1359 for (const auto& v : GetFields()) {
1360 if (v->GetType().GetName() == "ParcelableHolder") {
1361 AIDL_ERROR(*v) << "A union can't have a member of ParcelableHolder '" << v->GetName() << "'";
1362 success = false;
1363 }
1364 }
1365
Jooyung Hanfe89f122020-10-14 03:49:18 +09001366 if (GetFields().empty()) {
1367 AIDL_ERROR(*this) << "The union '" << this->GetName() << "' has no fields.";
1368 return false;
1369 }
1370
Jooyung Han53fb4242020-12-17 16:03:49 +09001371 // first member should have useful default value (implicit or explicit)
1372 const auto& first = GetFields()[0];
1373 if (!first->HasUsefulDefaultValue()) {
1374 // Most types can be initialized without a default value. For example,
1375 // interface types are inherently nullable. But, enum types should have
1376 // an explicit default value.
1377 if (!first->GetType().IsArray() && typenames.GetEnumDeclaration(first->GetType())) {
1378 AIDL_ERROR(first)
1379 << "The union's first member should have a useful default value. Enum types can be "
1380 "initialized with a reference. (e.g. ... = MyEnum.FOO;)";
1381 return false;
1382 }
1383 // In Java, array types are initialized as null without a default value. To be sure that default
1384 // initialized unions are accepted by other backends we require arrays also have a default
1385 // value.
1386 if (first->GetType().IsArray()) {
1387 AIDL_ERROR(first)
1388 << "The union's first member should have a useful default value. Arrays can be "
1389 "initialized with values(e.g. ... = { values... };) or marked as @nullable.";
1390 return false;
1391 }
1392 }
1393
Jooyung Hanfe89f122020-10-14 03:49:18 +09001394 return success;
1395}
1396
1397// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -07001398bool AidlInterface::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1399 Options::Language lang) const {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001400 for (const auto& m : this->GetMethods()) {
Steven Morelandd59e3172020-05-11 16:42:09 -07001401 if (!m->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001402 return false;
1403 }
1404 for (const auto& arg : m->GetArguments()) {
Steven Morelandd59e3172020-05-11 16:42:09 -07001405 if (!arg->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001406 return false;
1407 }
1408 }
1409 }
1410 return true;
1411}
1412
Steven Moreland46e9da82018-07-27 15:45:29 -07001413AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name,
Jooyung Han8451a202021-01-16 03:07:06 +09001414 const Comments& comments, bool oneway, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001415 std::vector<std::unique_ptr<AidlMember>>* members)
1416 : AidlDefinedType(location, name, comments, package, members) {
1417 for (auto& m : GetMethods()) {
1418 m.get()->ApplyInterfaceOneway(oneway);
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001419 }
Casey Dahlinfb7da2e2015-10-08 17:26:09 -07001420}
1421
Jooyung Han808a2a02020-12-28 16:46:54 +09001422bool AidlInterface::CheckValid(const AidlTypenames& typenames) const {
1423 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001424 return false;
1425 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001426 // Has to be a pointer due to deleting copy constructor. No idea why.
1427 map<string, const AidlMethod*> method_names;
1428 for (const auto& m : GetMethods()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001429 if (!m->GetType().CheckValid(typenames)) {
1430 return false;
1431 }
1432
Jeongik Cha649e8a72020-03-27 17:47:40 +09001433 // TODO(b/156872582): Support it when ParcelableHolder supports every backend.
1434 if (m->GetType().GetName() == "ParcelableHolder") {
1435 AIDL_ERROR(m) << "ParcelableHolder cannot be a return type";
1436 return false;
1437 }
Steven Morelandacd53472018-12-14 10:17:26 -08001438 if (m->IsOneway() && m->GetType().GetName() != "void") {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001439 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot return a value";
1440 return false;
1441 }
1442
1443 set<string> argument_names;
1444 for (const auto& arg : m->GetArguments()) {
1445 auto it = argument_names.find(arg->GetName());
1446 if (it != argument_names.end()) {
1447 AIDL_ERROR(m) << "method '" << m->GetName() << "' has duplicate argument name '"
1448 << arg->GetName() << "'";
1449 return false;
1450 }
1451 argument_names.insert(arg->GetName());
1452
Jooyung Han020d8d12021-02-26 17:23:02 +09001453 if (!arg->CheckValid(typenames)) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001454 return false;
1455 }
1456
Steven Morelandacd53472018-12-14 10:17:26 -08001457 if (m->IsOneway() && arg->IsOut()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001458 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot have out parameters";
1459 return false;
1460 }
Jooyung Han15fd6c62020-10-23 13:54:46 +09001461
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001462 // check that the name doesn't match a keyword
Jeongik Cha997281d2020-01-16 15:23:59 +09001463 if (IsJavaKeyword(arg->GetName().c_str())) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001464 AIDL_ERROR(arg) << "Argument name is a Java or aidl keyword";
1465 return false;
1466 }
1467
1468 // Reserve a namespace for internal use
1469 if (android::base::StartsWith(arg->GetName(), "_aidl")) {
1470 AIDL_ERROR(arg) << "Argument name cannot begin with '_aidl'";
1471 return false;
1472 }
Jooyung Hanfa181932021-06-12 07:56:53 +09001473
1474 if (arg->GetType().GetName() == "void") {
1475 AIDL_ERROR(arg->GetType())
1476 << "'void' is an invalid type for the parameter '" << arg->GetName() << "'";
1477 return false;
1478 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001479 }
1480
1481 auto it = method_names.find(m->GetName());
1482 // prevent duplicate methods
1483 if (it == method_names.end()) {
1484 method_names[m->GetName()] = m.get();
1485 } else {
1486 AIDL_ERROR(m) << "attempt to redefine method " << m->GetName() << ":";
1487 AIDL_ERROR(it->second) << "previously defined here.";
1488 return false;
1489 }
1490
Paul Trautrimb77048c2020-01-21 16:39:32 +09001491 static set<string> reserved_methods{"asBinder()", "getInterfaceHash()", "getInterfaceVersion()",
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001492 "getTransactionName(int)"};
1493
1494 if (reserved_methods.find(m->Signature()) != reserved_methods.end()) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001495 AIDL_ERROR(m) << " method " << m->Signature() << " is reserved for internal use.";
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001496 return false;
1497 }
1498 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001499
1500 bool success = true;
1501 set<string> constant_names;
Jooyung Han3f347ca2020-12-01 12:41:50 +09001502 for (const auto& constant : GetConstantDeclarations()) {
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001503 if (constant_names.count(constant->GetName()) > 0) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001504 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001505 success = false;
1506 }
1507 constant_names.insert(constant->GetName());
1508 success = success && constant->CheckValid(typenames);
1509 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001510 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001511}
1512
Jiyong Park27fd7fd2020-08-27 16:25:09 +09001513std::string AidlInterface::GetDescriptor() const {
1514 std::string annotatedDescriptor = AidlAnnotatable::GetDescriptor();
1515 if (annotatedDescriptor != "") {
1516 return annotatedDescriptor;
1517 }
1518 return GetCanonicalName();
1519}
1520
Jooyung Han132cf802021-01-15 02:17:32 +09001521AidlImport::AidlImport(const AidlLocation& location, const std::string& needed_class,
Jooyung Han8451a202021-01-16 03:07:06 +09001522 const Comments& comments)
Jooyung Han5c7e77c2021-01-20 16:00:29 +09001523 : AidlNode(location, comments), needed_class_(needed_class) {}
Jooyung Han29813842020-12-08 01:28:03 +09001524
Jooyung Han13f1fa52021-06-11 18:06:12 +09001525AidlDocument::AidlDocument(const AidlLocation& location, const Comments& comments,
1526 std::vector<std::unique_ptr<AidlImport>> imports,
Jooyung Han35784982021-06-29 06:26:12 +09001527 std::vector<std::unique_ptr<AidlDefinedType>> defined_types,
1528 bool is_preprocessed)
Jooyung Han13f1fa52021-06-11 18:06:12 +09001529 : AidlCommentable(location, comments),
Jooyung Han35784982021-06-29 06:26:12 +09001530 AidlScope(this),
Jooyung Han13f1fa52021-06-11 18:06:12 +09001531 imports_(std::move(imports)),
Jooyung Han35784982021-06-29 06:26:12 +09001532 defined_types_(std::move(defined_types)),
1533 is_preprocessed_(is_preprocessed) {
Jooyung Han13f1fa52021-06-11 18:06:12 +09001534 for (const auto& t : defined_types_) {
1535 t->SetEnclosingScope(this);
1536 }
1537}
1538
1539// Resolves type name in the current document.
1540// - built-in types
1541// - imported types
1542// - top-level type
1543std::string AidlDocument::ResolveName(const std::string& name) const {
1544 if (AidlTypenames::IsBuiltinTypename(name)) {
1545 return name;
1546 }
1547
1548 const auto first_dot = name.find_first_of('.');
1549 // For "Outer.Inner", we look up "Outer" in the import list.
Jooyung Han29813842020-12-08 01:28:03 +09001550 const std::string class_name =
Jooyung Han13f1fa52021-06-11 18:06:12 +09001551 (first_dot == std::string::npos) ? name : name.substr(0, first_dot);
1552 // Keep ".Inner", to make a fully-qualified name
1553 const std::string nested_type = (first_dot == std::string::npos) ? "" : name.substr(first_dot);
1554
Jooyung Han29813842020-12-08 01:28:03 +09001555 for (const auto& import : Imports()) {
Jooyung Han13f1fa52021-06-11 18:06:12 +09001556 if (import->SimpleName() == class_name) {
1557 return import->GetNeededClass() + nested_type;
Jooyung Han29813842020-12-08 01:28:03 +09001558 }
1559 }
Jooyung Han13f1fa52021-06-11 18:06:12 +09001560
1561 // check if it is a top-level type.
1562 for (const auto& type : DefinedTypes()) {
1563 if (type->GetName() == class_name) {
1564 return type->GetCanonicalName() + nested_type;
1565 }
Jooyung Han29813842020-12-08 01:28:03 +09001566 }
Jooyung Han13f1fa52021-06-11 18:06:12 +09001567
1568 // name itself might be fully-qualified name.
1569 return name;
Steven Moreland26318532020-12-23 20:08:36 +00001570}