blob: 7c6705677e933cf9d447cbc438a09bddbf9872c7 [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
Dan Willemsen609ba6d2019-12-30 10:44:00 -080035#include "aidl_language_y-module.h"
Christopher Wiley4a2884b2015-10-07 11:27:45 -070036#include "logging.h"
Adam Lesinskiffa16862014-01-23 18:17:42 -080037
Will McVickerd7d18df2019-09-12 13:40:50 -070038#include "aidl.h"
39
Casey Dahlin07b9dde2015-09-10 19:13:49 -070040#ifdef _WIN32
41int isatty(int fd)
42{
43 return (fd == 0);
44}
45#endif
46
Christopher Wiley4a2884b2015-10-07 11:27:45 -070047using android::aidl::IoDelegate;
Christopher Wileyd76067c2015-10-19 17:00:13 -070048using android::base::Join;
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -080049using android::base::Split;
Casey Dahlindd691812015-09-09 17:59:06 -070050using std::cerr;
Jiyong Park1deecc32018-07-17 01:14:41 +090051using std::pair;
Jiyong Park68bc77a2018-07-19 19:00:45 +090052using std::set;
Christopher Wiley4a2884b2015-10-07 11:27:45 -070053using std::string;
54using std::unique_ptr;
Jiyong Parkccf00f82018-07-17 01:39:23 +090055using std::vector;
Adam Lesinskiffa16862014-01-23 18:17:42 -080056
Jeongik Cha047c5ee2019-08-07 23:16:49 +090057namespace {
Jeongik Cha997281d2020-01-16 15:23:59 +090058bool IsJavaKeyword(const char* str) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +090059 static const std::vector<std::string> kJavaKeywords{
60 "abstract", "assert", "boolean", "break", "byte", "case", "catch",
61 "char", "class", "const", "continue", "default", "do", "double",
62 "else", "enum", "extends", "final", "finally", "float", "for",
63 "goto", "if", "implements", "import", "instanceof", "int", "interface",
64 "long", "native", "new", "package", "private", "protected", "public",
65 "return", "short", "static", "strictfp", "super", "switch", "synchronized",
66 "this", "throw", "throws", "transient", "try", "void", "volatile",
67 "while", "true", "false", "null",
68 };
69 return std::find(kJavaKeywords.begin(), kJavaKeywords.end(), str) != kJavaKeywords.end();
70}
Jeongik Cha997281d2020-01-16 15:23:59 +090071
Jeongik Cha91180252020-07-31 15:43:11 +090072inline std::string CapitalizeFirstLetter(const std::string& str) {
73 CHECK(str.size() > 0) << "Input cannot be empty.";
74 std::ostringstream out;
75 out << static_cast<char>(toupper(str[0])) << str.substr(1);
76 return out.str();
77}
78
Jeongik Cha997281d2020-01-16 15:23:59 +090079void AddHideComment(CodeWriter* writer) {
80 writer->Write("/* @hide */\n");
81}
82
83inline bool HasHideComment(const std::string& comment) {
84 return std::regex_search(comment, std::regex("@hide\\b"));
85}
Jeongik Cha047c5ee2019-08-07 23:16:49 +090086} // namespace
87
Devin Mooredf93ebb2020-03-25 14:03:35 -070088AidlLocation::AidlLocation(const std::string& file, Point begin, Point end, Source source)
89 : file_(file), begin_(begin), end_(end), source_(source) {}
Steven Moreland46e9da82018-07-27 15:45:29 -070090
91std::ostream& operator<<(std::ostream& os, const AidlLocation& l) {
Devin Moore5de18ed2020-04-02 13:52:29 -070092 os << l.file_;
93 if (l.LocationKnown()) {
94 os << ":" << l.begin_.line << "." << l.begin_.column << "-";
95 if (l.begin_.line != l.end_.line) {
96 os << l.end_.line << ".";
97 }
98 os << l.end_.column;
Steven Moreland46e9da82018-07-27 15:45:29 -070099 }
Steven Moreland46e9da82018-07-27 15:45:29 -0700100 return os;
101}
102
103AidlNode::AidlNode(const AidlLocation& location) : location_(location) {}
104
Mathew Inwoodadb74672019-11-29 14:01:53 +0000105std::string AidlNode::PrintLine() const {
Andrei Onea8714b022019-02-01 18:55:54 +0000106 std::stringstream ss;
107 ss << location_.file_ << ":" << location_.begin_.line;
108 return ss.str();
109}
110
Mathew Inwoodadb74672019-11-29 14:01:53 +0000111std::string AidlNode::PrintLocation() const {
112 std::stringstream ss;
113 ss << location_.file_ << ":" << location_.begin_.line << ":" << location_.begin_.column << ":"
114 << location_.end_.line << ":" << location_.end_.column;
115 return ss.str();
116}
117
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700118const std::vector<AidlAnnotation::Schema>& AidlAnnotation::AllSchemas() {
119 static const std::vector<Schema> kSchemas{
120 {AidlAnnotation::Type::NULLABLE, "nullable", {}},
121 {AidlAnnotation::Type::UTF8_IN_CPP, "utf8InCpp", {}},
122 {AidlAnnotation::Type::VINTF_STABILITY, "VintfStability", {}},
123 {AidlAnnotation::Type::UNSUPPORTED_APP_USAGE,
124 "UnsupportedAppUsage",
125 {{"expectedSignature", "String"},
126 {"implicitMember", "String"},
127 {"maxTargetSdk", "int"},
128 {"publicAlternatives", "String"},
129 {"trackingBug", "long"}}},
130 {AidlAnnotation::Type::JAVA_STABLE_PARCELABLE, "JavaOnlyStableParcelable", {}},
131 {AidlAnnotation::Type::HIDE, "Hide", {}},
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900132 {AidlAnnotation::Type::BACKING, "Backing", {{"type", "String"}}},
133 {AidlAnnotation::Type::JAVA_PASSTHROUGH, "JavaPassthrough", {{"annotation", "String"}}},
Jiyong Park43113fb2020-07-20 16:26:19 +0900134 {AidlAnnotation::Type::JAVA_DEBUG, "JavaDebug", {}},
Jeongik Chad0a10272020-08-06 16:33:36 +0900135 {AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE, "JavaOnlyImmutable", {}},
Devin Moorec7e47a32020-08-07 10:55:25 -0700136 {AidlAnnotation::Type::FIXED_SIZE, "FixedSize", {}},
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900137 {AidlAnnotation::Type::DESCRIPTOR, "Descriptor", {{"value", "String"}}},
Andrei Homescue61feb52020-08-18 15:44:24 -0700138 {AidlAnnotation::Type::RUST_DERIVE,
139 "RustDerive",
140 {{"Copy", "boolean"},
141 {"Clone", "boolean"},
142 {"PartialOrd", "boolean"},
143 {"Ord", "boolean"},
144 {"PartialEq", "boolean"},
145 {"Eq", "boolean"},
146 {"Hash", "boolean"}}},
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900147 };
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700148 return kSchemas;
149}
Jiyong Park68bc77a2018-07-19 19:00:45 +0900150
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700151std::string AidlAnnotation::TypeToString(Type type) {
152 for (const Schema& schema : AllSchemas()) {
153 if (type == schema.type) return schema.name;
154 }
155 AIDL_FATAL(AIDL_LOCATION_HERE) << "Unrecognized type: " << static_cast<size_t>(type);
156 __builtin_unreachable();
157}
Andrei Onea9445fc62019-06-27 18:11:59 +0100158
159AidlAnnotation* AidlAnnotation::Parse(
160 const AidlLocation& location, const string& name,
161 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700162 const Schema* schema = nullptr;
163 for (const Schema& a_schema : AllSchemas()) {
164 if (a_schema.name == name) {
165 schema = &a_schema;
166 }
167 }
168
169 if (schema == nullptr) {
Jiyong Park68bc77a2018-07-19 19:00:45 +0900170 std::ostringstream stream;
Steven Moreland46e9da82018-07-27 15:45:29 -0700171 stream << "'" << name << "' is not a recognized annotation. ";
Jiyong Park68bc77a2018-07-19 19:00:45 +0900172 stream << "It must be one of:";
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700173 for (const Schema& s : AllSchemas()) {
174 stream << " " << s.name;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900175 }
176 stream << ".";
Steven Moreland46e9da82018-07-27 15:45:29 -0700177 AIDL_ERROR(location) << stream.str();
178 return nullptr;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900179 }
Andrei Onea9445fc62019-06-27 18:11:59 +0100180 if (parameter_list == nullptr) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700181 return new AidlAnnotation(location, *schema, {});
Andrei Onea9445fc62019-06-27 18:11:59 +0100182 }
183
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700184 return new AidlAnnotation(location, *schema, std::move(*parameter_list));
Jiyong Park68bc77a2018-07-19 19:00:45 +0900185}
186
Andrei Onea9445fc62019-06-27 18:11:59 +0100187AidlAnnotation::AidlAnnotation(
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700188 const AidlLocation& location, const Schema& schema,
Andrei Onea9445fc62019-06-27 18:11:59 +0100189 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters)
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700190 : AidlNode(location), schema_(schema), parameters_(std::move(parameters)) {}
Andrei Onea9445fc62019-06-27 18:11:59 +0100191
192bool AidlAnnotation::CheckValid() const {
Andrei Onea9445fc62019-06-27 18:11:59 +0100193 for (const auto& name_and_param : parameters_) {
194 const std::string& param_name = name_and_param.first;
195 const std::shared_ptr<AidlConstantValue>& param = name_and_param.second;
Will McVickerd7d18df2019-09-12 13:40:50 -0700196 if (!param->CheckValid()) {
197 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
198 << GetName() << ".";
199 return false;
200 }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700201 auto parameter_mapping_it = schema_.supported_parameters.find(param_name);
202 if (parameter_mapping_it == schema_.supported_parameters.end()) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100203 std::ostringstream stream;
204 stream << "Parameter " << param_name << " not supported ";
Devin Mooredecaf292020-04-30 09:16:40 -0700205 stream << "for annotation " << GetName() << ". ";
Andrei Onea9445fc62019-06-27 18:11:59 +0100206 stream << "It must be one of:";
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700207 for (const auto& kv : schema_.supported_parameters) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100208 stream << " " << kv.first;
209 }
210 AIDL_ERROR(this) << stream.str();
211 return false;
212 }
213 AidlTypeSpecifier type{AIDL_LOCATION_HERE, parameter_mapping_it->second, false, nullptr, ""};
Will McVickerd7d18df2019-09-12 13:40:50 -0700214 const std::string param_value = param->ValueString(type, AidlConstantValueDecorator);
Andrei Onea9445fc62019-06-27 18:11:59 +0100215 // Assume error on empty string.
216 if (param_value == "") {
217 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
218 << GetName() << ".";
219 return false;
220 }
221 }
222 return true;
223}
224
225std::map<std::string, std::string> AidlAnnotation::AnnotationParams(
226 const ConstantValueDecorator& decorator) const {
227 std::map<std::string, std::string> raw_params;
Andrei Onea9445fc62019-06-27 18:11:59 +0100228 for (const auto& name_and_param : parameters_) {
229 const std::string& param_name = name_and_param.first;
230 const std::shared_ptr<AidlConstantValue>& param = name_and_param.second;
Devin Mooredecaf292020-04-30 09:16:40 -0700231 if (schema_.supported_parameters.find(param_name) == schema_.supported_parameters.end()) {
232 std::ostringstream stream;
233 stream << "Parameter " << param_name << " not supported ";
234 stream << "for annotation " << GetName() << ". ";
235 stream << "It must be one of:";
236 for (const auto& kv : schema_.supported_parameters) {
237 stream << " " << kv.first;
238 }
239 AIDL_ERROR(this) << stream.str();
240 continue;
241 }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700242 AidlTypeSpecifier type{AIDL_LOCATION_HERE, schema_.supported_parameters.at(param_name), false,
243 nullptr, ""};
Will McVickerd7d18df2019-09-12 13:40:50 -0700244 if (!param->CheckValid()) {
245 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
246 << GetName() << ".";
Devin Mooredecaf292020-04-30 09:16:40 -0700247 continue;
Will McVickerd7d18df2019-09-12 13:40:50 -0700248 }
249
250 raw_params.emplace(param_name, param->ValueString(type, decorator));
Andrei Onea9445fc62019-06-27 18:11:59 +0100251 }
252 return raw_params;
253}
Steven Moreland46e9da82018-07-27 15:45:29 -0700254
Daniel Norman37d43dd2019-09-09 17:22:34 -0700255std::string AidlAnnotation::ToString(const ConstantValueDecorator& decorator) const {
256 if (parameters_.empty()) {
257 return "@" + GetName();
258 } else {
259 vector<string> param_strings;
260 for (const auto& [name, value] : AnnotationParams(decorator)) {
261 param_strings.emplace_back(name + "=" + value);
262 }
263 return "@" + GetName() + "(" + Join(param_strings, ", ") + ")";
264 }
265}
266
Andrei Onea9445fc62019-06-27 18:11:59 +0100267static const AidlAnnotation* GetAnnotation(const vector<AidlAnnotation>& annotations,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700268 AidlAnnotation::Type type) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100269 for (const auto& a : annotations) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700270 if (a.GetType() == type) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100271 return &a;
272 }
273 }
274 return nullptr;
275}
276
Steven Moreland46e9da82018-07-27 15:45:29 -0700277AidlAnnotatable::AidlAnnotatable(const AidlLocation& location) : AidlNode(location) {}
278
Jiyong Park68bc77a2018-07-19 19:00:45 +0900279bool AidlAnnotatable::IsNullable() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700280 return GetAnnotation(annotations_, AidlAnnotation::Type::NULLABLE);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900281}
282
Jiyong Park68bc77a2018-07-19 19:00:45 +0900283bool AidlAnnotatable::IsUtf8InCpp() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700284 return GetAnnotation(annotations_, AidlAnnotation::Type::UTF8_IN_CPP);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900285}
286
Steven Morelanda57d0a62019-07-30 09:41:14 -0700287bool AidlAnnotatable::IsVintfStability() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700288 return GetAnnotation(annotations_, AidlAnnotation::Type::VINTF_STABILITY);
Steven Morelanda57d0a62019-07-30 09:41:14 -0700289}
290
Jeongik Chad0a10272020-08-06 16:33:36 +0900291bool AidlAnnotatable::IsJavaOnlyImmutable() const {
292 return GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE);
Jeongik Cha36f76c32020-07-28 00:25:52 +0900293}
294
Devin Moorec7e47a32020-08-07 10:55:25 -0700295bool AidlAnnotatable::IsFixedSize() const {
296 return GetAnnotation(annotations_, AidlAnnotation::Type::FIXED_SIZE);
297}
298
Andrei Onea9445fc62019-06-27 18:11:59 +0100299const AidlAnnotation* AidlAnnotatable::UnsupportedAppUsage() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700300 return GetAnnotation(annotations_, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE);
Jiyong Parka6605ab2018-11-11 14:30:21 +0900301}
302
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900303const AidlAnnotation* AidlAnnotatable::JavaPassthrough() const {
304 return GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_PASSTHROUGH);
305}
306
Andrei Homescue61feb52020-08-18 15:44:24 -0700307const AidlAnnotation* AidlAnnotatable::RustDerive() const {
308 return GetAnnotation(annotations_, AidlAnnotation::Type::RUST_DERIVE);
309}
310
Daniel Norman716d3112019-09-10 13:11:56 -0700311const AidlTypeSpecifier* AidlAnnotatable::BackingType(const AidlTypenames& typenames) const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700312 auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::BACKING);
Daniel Norman85aed542019-08-21 12:01:14 -0700313 if (annotation != nullptr) {
314 auto annotation_params = annotation->AnnotationParams(AidlConstantValueDecorator);
315 if (auto it = annotation_params.find("type"); it != annotation_params.end()) {
316 const string& type = it->second;
Steven Morelande7d5d082020-05-21 20:29:02 +0000317
318 AIDL_FATAL_IF(type.size() < 2, this) << type;
319 AIDL_FATAL_IF(type[0] != '"', this) << type;
320 AIDL_FATAL_IF(type[type.length() - 1] != '"', this) << type;
321 string unquoted_type = type.substr(1, type.length() - 2);
322
Daniel Norman716d3112019-09-10 13:11:56 -0700323 AidlTypeSpecifier* type_specifier =
Steven Morelande7d5d082020-05-21 20:29:02 +0000324 new AidlTypeSpecifier(AIDL_LOCATION_HERE, unquoted_type, false, nullptr, "");
Daniel Norman716d3112019-09-10 13:11:56 -0700325 type_specifier->Resolve(typenames);
326 return type_specifier;
Daniel Norman85aed542019-08-21 12:01:14 -0700327 }
328 }
329 return nullptr;
330}
331
Jeongik Cha88f95a82020-01-15 13:02:16 +0900332bool AidlAnnotatable::IsStableApiParcelable(Options::Language lang) const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700333 return lang == Options::Language::JAVA &&
334 GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_STABLE_PARCELABLE);
Jeongik Cha82317dd2019-02-27 20:26:11 +0900335}
336
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800337bool AidlAnnotatable::IsHide() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700338 return GetAnnotation(annotations_, AidlAnnotation::Type::HIDE);
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800339}
340
Jiyong Park43113fb2020-07-20 16:26:19 +0900341bool AidlAnnotatable::IsJavaDebug() const {
342 return GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_DEBUG);
343}
344
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900345std::string AidlAnnotatable::GetDescriptor() const {
346 auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::DESCRIPTOR);
347 if (annotation != nullptr) {
348 auto params = annotation->AnnotationParams(AidlConstantValueDecorator);
349 if (auto it = params.find("value"); it != params.end()) {
350 const string& value = it->second;
351
352 AIDL_FATAL_IF(value.size() < 2, this) << value;
353 AIDL_FATAL_IF(value[0] != '"', this) << value;
354 AIDL_FATAL_IF(value[value.length() - 1] != '"', this) << value;
355 std::string unquoted_value = value.substr(1, value.length() - 2);
356 return unquoted_value;
357 }
358 }
359 return "";
360}
361
Steven Moreland7e4b9502020-02-20 18:10:42 -0800362void AidlAnnotatable::DumpAnnotations(CodeWriter* writer) const {
363 if (annotations_.empty()) return;
364
365 writer->Write("%s\n", AidlAnnotatable::ToString().c_str());
366}
367
Devin Moore24f68572020-02-26 13:20:59 -0800368bool AidlAnnotatable::CheckValid(const AidlTypenames&) const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700369 std::set<AidlAnnotation::Type> supported_annotations = GetSupportedAnnotations();
Andrei Onea9445fc62019-06-27 18:11:59 +0100370 for (const auto& annotation : GetAnnotations()) {
371 if (!annotation.CheckValid()) {
372 return false;
373 }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700374
375 std::vector<std::string> supported_annot_strings;
376 for (AidlAnnotation::Type type : supported_annotations) {
377 supported_annot_strings.push_back(AidlAnnotation::TypeToString(type));
378 }
379
380 if (supported_annotations.find(annotation.GetType()) == supported_annotations.end()) {
Devin Moore24f68572020-02-26 13:20:59 -0800381 AIDL_ERROR(this) << "'" << annotation.GetName()
382 << "' is not a supported annotation for this node. "
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700383 << "It must be one of: "
384 << android::base::Join(supported_annot_strings, ", ");
Devin Moore24f68572020-02-26 13:20:59 -0800385 return false;
386 }
Andrei Onea9445fc62019-06-27 18:11:59 +0100387 }
Steven Morelanda57d0a62019-07-30 09:41:14 -0700388
Andrei Onea9445fc62019-06-27 18:11:59 +0100389 return true;
390}
391
Jiyong Park68bc77a2018-07-19 19:00:45 +0900392string AidlAnnotatable::ToString() const {
393 vector<string> ret;
394 for (const auto& a : annotations_) {
Daniel Norman37d43dd2019-09-09 17:22:34 -0700395 ret.emplace_back(a.ToString(AidlConstantValueDecorator));
Jiyong Park68bc77a2018-07-19 19:00:45 +0900396 }
397 std::sort(ret.begin(), ret.end());
398 return Join(ret, " ");
399}
400
Steven Moreland46e9da82018-07-27 15:45:29 -0700401AidlTypeSpecifier::AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name,
402 bool is_array,
Jiyong Park1deecc32018-07-17 01:14:41 +0900403 vector<unique_ptr<AidlTypeSpecifier>>* type_params,
Steven Moreland46e9da82018-07-27 15:45:29 -0700404 const string& comments)
405 : AidlAnnotatable(location),
Jeongik Chadf76dc72019-11-28 00:08:47 +0900406 AidlParameterizable<unique_ptr<AidlTypeSpecifier>>(type_params),
Steven Moreland46e9da82018-07-27 15:45:29 -0700407 unresolved_name_(unresolved_name),
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700408 is_array_(is_array),
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900409 comments_(comments),
410 split_name_(Split(unresolved_name, ".")) {}
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700411
Steven Moreland3f658cf2018-08-20 13:40:54 -0700412AidlTypeSpecifier AidlTypeSpecifier::ArrayBase() const {
413 AIDL_FATAL_IF(!is_array_, this);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900414 // Declaring array of generic type cannot happen, it is grammar error.
415 AIDL_FATAL_IF(IsGeneric(), this);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700416
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800417 AidlTypeSpecifier array_base = *this;
418 array_base.is_array_ = false;
419 return array_base;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700420}
421
Jeongik Cha997281d2020-01-16 15:23:59 +0900422bool AidlTypeSpecifier::IsHidden() const {
423 return HasHideComment(GetComments());
424}
425
Jiyong Park1deecc32018-07-17 01:14:41 +0900426string AidlTypeSpecifier::ToString() const {
427 string ret = GetName();
428 if (IsGeneric()) {
429 vector<string> arg_names;
430 for (const auto& ta : GetTypeParameters()) {
Jiyong Parkccf00f82018-07-17 01:39:23 +0900431 arg_names.emplace_back(ta->ToString());
432 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900433 ret += "<" + Join(arg_names, ",") + ">";
Jiyong Parkccf00f82018-07-17 01:39:23 +0900434 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900435 if (IsArray()) {
436 ret += "[]";
437 }
438 return ret;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900439}
440
Jiyong Park02da7422018-07-16 16:00:26 +0900441string AidlTypeSpecifier::Signature() const {
442 string ret = ToString();
443 string annotations = AidlAnnotatable::ToString();
444 if (annotations != "") {
445 ret = annotations + " " + ret;
446 }
447 return ret;
448}
449
Daniel Norman716d3112019-09-10 13:11:56 -0700450bool AidlTypeSpecifier::Resolve(const AidlTypenames& typenames) {
Steven Moreland9731c632019-08-13 10:21:08 -0700451 CHECK(!IsResolved());
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700452 AidlTypenames::ResolvedTypename result = typenames.ResolveTypename(unresolved_name_);
453 if (result.is_resolved) {
454 fully_qualified_name_ = result.canonical_name;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900455 split_name_ = Split(fully_qualified_name_, ".");
Jiyong Parkccf00f82018-07-17 01:39:23 +0900456 }
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700457 return result.is_resolved;
Casey Dahlin70078e62015-09-30 17:01:30 -0700458}
459
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700460std::set<AidlAnnotation::Type> AidlTypeSpecifier::GetSupportedAnnotations() const {
Devin Moore24f68572020-02-26 13:20:59 -0800461 // kHide and kUnsupportedAppUsage are both method return annotations
462 // which we don't distinguish from other type specifiers.
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700463 return {AidlAnnotation::Type::NULLABLE, AidlAnnotation::Type::UTF8_IN_CPP,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900464 AidlAnnotation::Type::UNSUPPORTED_APP_USAGE, AidlAnnotation::Type::HIDE,
465 AidlAnnotation::Type::JAVA_PASSTHROUGH};
Devin Moore24f68572020-02-26 13:20:59 -0800466}
467
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900468bool AidlTypeSpecifier::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -0800469 if (!AidlAnnotatable::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100470 return false;
471 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900472 if (IsGeneric()) {
473 const string& type_name = GetName();
Jeongik Chae74c86d2019-12-12 16:54:03 +0900474
475 auto& types = GetTypeParameters();
476 // TODO(b/136048684) Disallow to use primitive types only if it is List or Map.
477 if (type_name == "List" || type_name == "Map") {
478 if (std::any_of(types.begin(), types.end(), [](auto& type_ptr) {
479 return AidlTypenames::IsPrimitiveTypename(type_ptr->GetName());
480 })) {
Devin Moore7b8d5c92020-03-17 14:14:08 -0700481 AIDL_ERROR(this) << "A generic type cannot have any primitive type parameters.";
Jeongik Chae74c86d2019-12-12 16:54:03 +0900482 return false;
483 }
484 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800485 const auto defined_type = typenames.TryGetDefinedType(type_name);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900486 const auto parameterizable =
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800487 defined_type != nullptr ? defined_type->AsParameterizable() : nullptr;
488 const bool is_user_defined_generic_type =
Jeongik Chadf76dc72019-11-28 00:08:47 +0900489 parameterizable != nullptr && parameterizable->IsGeneric();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800490 const size_t num_params = GetTypeParameters().size();
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900491 if (type_name == "List") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800492 if (num_params > 1) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900493 AIDL_ERROR(this) << " List cannot have type parameters more than one, but got "
494 << "'" << ToString() << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900495 return false;
496 }
497 } else if (type_name == "Map") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800498 if (num_params != 0 && num_params != 2) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900499 AIDL_ERROR(this) << "Map must have 0 or 2 type parameters, but got "
500 << "'" << ToString() << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900501 return false;
502 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800503 if (num_params == 2) {
Jeongik Chae48d9942020-01-02 17:39:00 +0900504 const string& key_type = GetTypeParameters()[0]->GetName();
505 if (key_type != "String") {
506 AIDL_ERROR(this) << "The type of key in map must be String, but it is "
507 << "'" << key_type << "'";
508 return false;
509 }
510 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800511 } else if (is_user_defined_generic_type) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900512 const size_t allowed = parameterizable->GetTypeParameters().size();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800513 if (num_params != allowed) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900514 AIDL_ERROR(this) << type_name << " must have " << allowed << " type parameters, but got "
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800515 << num_params;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900516 return false;
517 }
518 } else {
519 AIDL_ERROR(this) << type_name << " is not a generic type.";
520 return false;
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900521 }
522 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900523
Steven Moreland11cb9452020-01-21 16:56:58 -0800524 const bool is_generic_string_list = GetName() == "List" && IsGeneric() &&
525 GetTypeParameters().size() == 1 &&
526 GetTypeParameters()[0]->GetName() == "String";
527 if (IsUtf8InCpp() && (GetName() != "String" && !is_generic_string_list)) {
528 AIDL_ERROR(this) << "@utf8InCpp can only be used on String, String[], and List<String>.";
529 return false;
530 }
531
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900532 if (GetName() == "void") {
533 if (IsArray() || IsNullable() || IsUtf8InCpp()) {
534 AIDL_ERROR(this) << "void type cannot be an array or nullable or utf8 string";
535 return false;
536 }
537 }
538
539 if (IsArray()) {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800540 const auto defined_type = typenames.TryGetDefinedType(GetName());
541 if (defined_type != nullptr && defined_type->AsInterface() != nullptr) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900542 AIDL_ERROR(this) << "Binder type cannot be an array";
543 return false;
544 }
545 }
546
547 if (IsNullable()) {
548 if (AidlTypenames::IsPrimitiveTypename(GetName()) && !IsArray()) {
549 AIDL_ERROR(this) << "Primitive type cannot get nullable annotation";
550 return false;
551 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800552 const auto defined_type = typenames.TryGetDefinedType(GetName());
553 if (defined_type != nullptr && defined_type->AsEnumDeclaration() != nullptr && !IsArray()) {
Daniel Normanee8674f2019-09-20 16:07:00 -0700554 AIDL_ERROR(this) << "Enum type cannot get nullable annotation";
555 return false;
556 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900557 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900558 return true;
559}
560
Steven Moreland860b1942018-08-16 14:59:28 -0700561std::string AidlConstantValueDecorator(const AidlTypeSpecifier& /*type*/,
562 const std::string& raw_value) {
563 return raw_value;
564}
565
Steven Moreland46e9da82018-07-27 15:45:29 -0700566AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
567 AidlTypeSpecifier* type, const std::string& name)
Steven Moreland541788d2020-05-21 22:05:52 +0000568 : AidlVariableDeclaration(location, type, name, AidlConstantValue::Default(*type)) {
569 default_user_specified_ = false;
570}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700571
Steven Moreland46e9da82018-07-27 15:45:29 -0700572AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
573 AidlTypeSpecifier* type, const std::string& name,
574 AidlConstantValue* default_value)
Steven Moreland541788d2020-05-21 22:05:52 +0000575 : AidlNode(location),
576 type_(type),
577 name_(name),
578 default_user_specified_(true),
579 default_value_(default_value) {}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700580
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900581bool AidlVariableDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700582 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900583 valid &= type_->CheckValid(typenames);
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900584
Steven Moreland54be7bd2019-12-05 11:17:53 -0800585 if (type_->GetName() == "void") {
586 AIDL_ERROR(this) << "Declaration " << name_
587 << " is void, but declarations cannot be of void type.";
588 valid = false;
589 }
590
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900591 if (default_value_ == nullptr) return valid;
Steven Moreland25294322018-08-07 18:13:55 -0700592 valid &= default_value_->CheckValid();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700593
Steven Moreland25294322018-08-07 18:13:55 -0700594 if (!valid) return false;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700595
Steven Moreland860b1942018-08-16 14:59:28 -0700596 return !ValueString(AidlConstantValueDecorator).empty();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700597}
Steven Moreland5557f1c2018-07-02 13:50:23 -0700598
599string AidlVariableDeclaration::ToString() const {
Jeongik Cha3271ffa2018-12-04 15:19:20 +0900600 string ret = type_->Signature() + " " + name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000601 if (default_value_ != nullptr && default_user_specified_) {
Steven Moreland860b1942018-08-16 14:59:28 -0700602 ret += " = " + ValueString(AidlConstantValueDecorator);
Steven Moreland9ea10e32018-07-19 15:26:09 -0700603 }
604 return ret;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700605}
606
Jiyong Park02da7422018-07-16 16:00:26 +0900607string AidlVariableDeclaration::Signature() const {
608 return type_->Signature() + " " + name_;
609}
610
Steven Moreland860b1942018-08-16 14:59:28 -0700611std::string AidlVariableDeclaration::ValueString(const ConstantValueDecorator& decorator) const {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900612 if (default_value_ != nullptr) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700613 return default_value_->ValueString(GetType(), decorator);
Jiyong Parka468e2a2018-08-29 21:25:18 +0900614 } else {
615 return "";
616 }
Steven Moreland25294322018-08-07 18:13:55 -0700617}
618
Steven Moreland46e9da82018-07-27 15:45:29 -0700619AidlArgument::AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
620 AidlTypeSpecifier* type, const std::string& name)
621 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700622 direction_(direction),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700623 direction_specified_(true) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700624
Steven Moreland46e9da82018-07-27 15:45:29 -0700625AidlArgument::AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type,
626 const std::string& name)
627 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700628 direction_(AidlArgument::IN_DIR),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700629 direction_specified_(false) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700630
Jiyong Park02da7422018-07-16 16:00:26 +0900631string AidlArgument::GetDirectionSpecifier() const {
Casey Dahlinc378c992015-09-29 16:50:40 -0700632 string ret;
Casey Dahlinc378c992015-09-29 16:50:40 -0700633 if (direction_specified_) {
634 switch(direction_) {
635 case AidlArgument::IN_DIR:
Devin Mooreeccdb902020-03-24 16:22:40 -0700636 ret += "in";
Casey Dahlinc378c992015-09-29 16:50:40 -0700637 break;
638 case AidlArgument::OUT_DIR:
Devin Mooreeccdb902020-03-24 16:22:40 -0700639 ret += "out";
Casey Dahlinc378c992015-09-29 16:50:40 -0700640 break;
641 case AidlArgument::INOUT_DIR:
Devin Mooreeccdb902020-03-24 16:22:40 -0700642 ret += "inout";
Casey Dahlinc378c992015-09-29 16:50:40 -0700643 break;
644 }
645 }
Casey Dahlinc378c992015-09-29 16:50:40 -0700646 return ret;
647}
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700648
Jiyong Park02da7422018-07-16 16:00:26 +0900649string AidlArgument::ToString() const {
Devin Mooreeccdb902020-03-24 16:22:40 -0700650 if (direction_specified_) {
651 return GetDirectionSpecifier() + " " + AidlVariableDeclaration::ToString();
652 } else {
653 return AidlVariableDeclaration::ToString();
654 }
Jiyong Park02da7422018-07-16 16:00:26 +0900655}
656
657std::string AidlArgument::Signature() const {
Steven Moreland46e9da82018-07-27 15:45:29 -0700658 class AidlInterface;
659 class AidlInterface;
660 class AidlParcelable;
661 class AidlStructuredParcelable;
662 class AidlParcelable;
663 class AidlStructuredParcelable;
Devin Mooreeccdb902020-03-24 16:22:40 -0700664 if (direction_specified_) {
665 return GetDirectionSpecifier() + " " + AidlVariableDeclaration::Signature();
666 } else {
667 return AidlVariableDeclaration::Signature();
668 }
Jiyong Park02da7422018-07-16 16:00:26 +0900669}
670
Steven Moreland46e9da82018-07-27 15:45:29 -0700671AidlMember::AidlMember(const AidlLocation& location) : AidlNode(location) {}
672
Steven Moreland46e9da82018-07-27 15:45:29 -0700673AidlConstantDeclaration::AidlConstantDeclaration(const AidlLocation& location,
674 AidlTypeSpecifier* type, const std::string& name,
675 AidlConstantValue* value)
676 : AidlMember(location), type_(type), name_(name), value_(value) {}
Steven Moreland693640b2018-07-19 13:46:27 -0700677
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900678bool AidlConstantDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700679 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900680 valid &= type_->CheckValid(typenames);
Steven Moreland25294322018-08-07 18:13:55 -0700681 valid &= value_->CheckValid();
682 if (!valid) return false;
Steven Moreland693640b2018-07-19 13:46:27 -0700683
Steven Moreland25294322018-08-07 18:13:55 -0700684 const static set<string> kSupportedConstTypes = {"String", "int"};
685 if (kSupportedConstTypes.find(type_->ToString()) == kSupportedConstTypes.end()) {
686 AIDL_ERROR(this) << "Constant of type " << type_->ToString() << " is not supported.";
Steven Moreland693640b2018-07-19 13:46:27 -0700687 return false;
688 }
689
Will McVickerd7d18df2019-09-12 13:40:50 -0700690 return true;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700691}
692
Jiyong Parka428d212018-08-29 22:26:30 +0900693string AidlConstantDeclaration::ToString() const {
694 return "const " + type_->ToString() + " " + name_ + " = " +
695 ValueString(AidlConstantValueDecorator);
696}
697
698string AidlConstantDeclaration::Signature() const {
699 return type_->Signature() + " " + name_;
700}
701
Steven Moreland46e9da82018-07-27 15:45:29 -0700702AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
703 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900704 const std::string& comments)
705 : AidlMethod(location, oneway, type, name, args, comments, 0, true) {
706 has_id_ = false;
707}
708
709AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
710 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900711 const std::string& comments, int id, bool is_user_defined)
Steven Moreland46e9da82018-07-27 15:45:29 -0700712 : AidlMember(location),
713 oneway_(oneway),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700714 comments_(comments),
715 type_(type),
716 name_(name),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700717 arguments_(std::move(*args)),
Jiyong Parkb034bf02018-07-30 17:44:33 +0900718 id_(id),
719 is_user_defined_(is_user_defined) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700720 has_id_ = true;
721 delete args;
Christopher Wileyad339272015-10-05 19:11:58 -0700722 for (const unique_ptr<AidlArgument>& a : arguments_) {
723 if (a->IsIn()) { in_arguments_.push_back(a.get()); }
724 if (a->IsOut()) { out_arguments_.push_back(a.get()); }
725 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700726}
727
Jeongik Cha997281d2020-01-16 15:23:59 +0900728bool AidlMethod::IsHidden() const {
729 return HasHideComment(GetComments());
730}
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700731
Jiyong Park02da7422018-07-16 16:00:26 +0900732string AidlMethod::Signature() const {
733 vector<string> arg_signatures;
734 for (const auto& arg : GetArguments()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900735 arg_signatures.emplace_back(arg->GetType().ToString());
Jiyong Park02da7422018-07-16 16:00:26 +0900736 }
Jiyong Park309668e2018-07-28 16:55:44 +0900737 return GetName() + "(" + Join(arg_signatures, ", ") + ")";
738}
739
740string AidlMethod::ToString() const {
741 vector<string> arg_strings;
742 for (const auto& arg : GetArguments()) {
743 arg_strings.emplace_back(arg->Signature());
744 }
Steven Moreland4ee68632018-12-14 15:52:46 -0800745 string ret = (IsOneway() ? "oneway " : "") + GetType().Signature() + " " + GetName() + "(" +
746 Join(arg_strings, ", ") + ")";
Jiyong Parked65bf42018-08-28 15:43:27 +0900747 if (HasId()) {
748 ret += " = " + std::to_string(GetId());
749 }
750 return ret;
Jiyong Park02da7422018-07-16 16:00:26 +0900751}
752
Steven Moreland46e9da82018-07-27 15:45:29 -0700753AidlDefinedType::AidlDefinedType(const AidlLocation& location, const std::string& name,
Jiyong Park18132182020-06-08 20:24:40 +0900754 const std::string& comments, const std::string& package)
755 : AidlAnnotatable(location),
756 name_(name),
757 comments_(comments),
758 package_(package),
759 split_package_(package.empty() ? std::vector<std::string>()
760 : android::base::Split(package, ".")) {}
Steven Moreland787b0432018-07-03 09:00:58 -0700761
Devin Moore24f68572020-02-26 13:20:59 -0800762bool AidlDefinedType::CheckValid(const AidlTypenames& typenames) const {
763 if (!AidlAnnotatable::CheckValid(typenames)) {
764 return false;
765 }
766
767 return true;
768}
769
Jeongik Cha997281d2020-01-16 15:23:59 +0900770bool AidlDefinedType::IsHidden() const {
771 return HasHideComment(GetComments());
772}
773
Steven Moreland787b0432018-07-03 09:00:58 -0700774std::string AidlDefinedType::GetCanonicalName() const {
775 if (package_.empty()) {
776 return GetName();
777 }
778 return GetPackage() + "." + GetName();
779}
780
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800781void AidlDefinedType::DumpHeader(CodeWriter* writer) const {
782 if (this->IsHidden()) {
783 AddHideComment(writer);
784 }
785 DumpAnnotations(writer);
786}
787
Jiyong Park18132182020-06-08 20:24:40 +0900788AidlParcelable::AidlParcelable(const AidlLocation& location, const std::string& name,
789 const std::string& package, const std::string& comments,
Jeongik Chadf76dc72019-11-28 00:08:47 +0900790 const std::string& cpp_header, std::vector<std::string>* type_params)
Jiyong Park18132182020-06-08 20:24:40 +0900791 : AidlDefinedType(location, name, comments, package),
Jeongik Chadf76dc72019-11-28 00:08:47 +0900792 AidlParameterizable<std::string>(type_params),
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800793 cpp_header_(cpp_header) {
794 // Strip off quotation marks if we actually have a cpp header.
795 if (cpp_header_.length() >= 2) {
796 cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2);
797 }
Casey Dahlin59401da2015-10-09 18:16:45 -0700798}
Jeongik Chadf76dc72019-11-28 00:08:47 +0900799template <typename T>
800AidlParameterizable<T>::AidlParameterizable(const AidlParameterizable& other) {
801 // Copying is not supported if it has type parameters.
802 // It doesn't make a problem because only ArrayBase() makes a copy,
803 // and it can be called only if a type is not generic.
804 CHECK(!other.IsGeneric());
805}
806
807template <typename T>
808bool AidlParameterizable<T>::CheckValid() const {
809 return true;
810};
811
812template <>
813bool AidlParameterizable<std::string>::CheckValid() const {
814 if (!IsGeneric()) {
815 return true;
816 }
817 std::unordered_set<std::string> set(GetTypeParameters().begin(), GetTypeParameters().end());
818 if (set.size() != GetTypeParameters().size()) {
819 AIDL_ERROR(this->AsAidlNode()) << "Every type parameter should be unique.";
820 return false;
821 }
822 return true;
823}
Casey Dahlin59401da2015-10-09 18:16:45 -0700824
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700825std::set<AidlAnnotation::Type> AidlParcelable::GetSupportedAnnotations() const {
Jeongik Cha36f76c32020-07-28 00:25:52 +0900826 return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900827 AidlAnnotation::Type::JAVA_STABLE_PARCELABLE, AidlAnnotation::Type::HIDE,
Jeongik Chad0a10272020-08-06 16:33:36 +0900828 AidlAnnotation::Type::JAVA_PASSTHROUGH, AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE};
Devin Moore24f68572020-02-26 13:20:59 -0800829}
830
831bool AidlParcelable::CheckValid(const AidlTypenames& typenames) const {
832 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100833 return false;
834 }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900835 if (!AidlParameterizable<std::string>::CheckValid()) {
836 return false;
837 }
Jeongik Cha82317dd2019-02-27 20:26:11 +0900838
839 return true;
840}
841
Jeongik Cha997281d2020-01-16 15:23:59 +0900842void AidlParcelable::Dump(CodeWriter* writer) const {
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800843 DumpHeader(writer);
Jiyong Park02da7422018-07-16 16:00:26 +0900844 writer->Write("parcelable %s ;\n", GetName().c_str());
845}
846
Steven Moreland5557f1c2018-07-02 13:50:23 -0700847AidlStructuredParcelable::AidlStructuredParcelable(
Jiyong Park18132182020-06-08 20:24:40 +0900848 const AidlLocation& location, const std::string& name, const std::string& package,
Devin Moore53fc99c2020-08-12 08:07:52 -0700849 const std::string& comments, std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables,
850 std::vector<std::string>* type_params)
851 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params),
Steven Moreland46e9da82018-07-27 15:45:29 -0700852 variables_(std::move(*variables)) {}
Steven Moreland5557f1c2018-07-02 13:50:23 -0700853
Jeongik Cha997281d2020-01-16 15:23:59 +0900854void AidlStructuredParcelable::Dump(CodeWriter* writer) const {
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800855 DumpHeader(writer);
Jiyong Park02da7422018-07-16 16:00:26 +0900856 writer->Write("parcelable %s {\n", GetName().c_str());
857 writer->Indent();
858 for (const auto& field : GetFields()) {
Jeongik Cha997281d2020-01-16 15:23:59 +0900859 if (field->GetType().IsHidden()) {
860 AddHideComment(writer);
861 }
Jiyong Parka468e2a2018-08-29 21:25:18 +0900862 writer->Write("%s;\n", field->ToString().c_str());
Jiyong Park02da7422018-07-16 16:00:26 +0900863 }
864 writer->Dedent();
865 writer->Write("}\n");
866}
867
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700868std::set<AidlAnnotation::Type> AidlStructuredParcelable::GetSupportedAnnotations() const {
Jeongik Cha36f76c32020-07-28 00:25:52 +0900869 return {AidlAnnotation::Type::VINTF_STABILITY,
870 AidlAnnotation::Type::UNSUPPORTED_APP_USAGE,
871 AidlAnnotation::Type::HIDE,
872 AidlAnnotation::Type::JAVA_PASSTHROUGH,
873 AidlAnnotation::Type::JAVA_DEBUG,
Devin Moorec7e47a32020-08-07 10:55:25 -0700874 AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE,
Andrei Homescue61feb52020-08-18 15:44:24 -0700875 AidlAnnotation::Type::FIXED_SIZE,
876 AidlAnnotation::Type::RUST_DERIVE};
Devin Moore24f68572020-02-26 13:20:59 -0800877}
878
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900879bool AidlStructuredParcelable::CheckValid(const AidlTypenames& typenames) const {
Daniel Norman85aed542019-08-21 12:01:14 -0700880 bool success = true;
Devin Moore24f68572020-02-26 13:20:59 -0800881 if (!AidlParcelable::CheckValid(typenames)) {
882 return false;
883 }
Jeongik Cha13066da2020-08-06 15:43:19 +0900884 std::set<std::string> fieldnames;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900885 for (const auto& v : GetFields()) {
Daniel Norman85aed542019-08-21 12:01:14 -0700886 success = success && v->CheckValid(typenames);
Jeongik Cha91180252020-07-31 15:43:11 +0900887 bool duplicated;
Jeongik Chad0a10272020-08-06 16:33:36 +0900888 if (IsJavaOnlyImmutable()) {
889 success = success && typenames.CanBeJavaOnlyImmutable(v->GetType());
Jeongik Cha91180252020-07-31 15:43:11 +0900890 duplicated = !fieldnames.emplace(CapitalizeFirstLetter(v->GetName())).second;
891 } else {
Devin Moorec7e47a32020-08-07 10:55:25 -0700892 if (IsFixedSize()) {
893 success = success && typenames.CanBeFixedSize(v->GetType());
894 if (!success) {
895 AIDL_ERROR(v) << "The @FixedSize parcelable '" << this->GetName() << "' has a "
896 << "non-fixed size field named " << v->GetName() << ".";
897 }
898 }
Jeongik Cha91180252020-07-31 15:43:11 +0900899 duplicated = !fieldnames.emplace(v->GetName()).second;
Jeongik Cha36f76c32020-07-28 00:25:52 +0900900 }
Jeongik Cha13066da2020-08-06 15:43:19 +0900901
Jeongik Cha91180252020-07-31 15:43:11 +0900902 if (duplicated) {
Jeongik Cha13066da2020-08-06 15:43:19 +0900903 AIDL_ERROR(this) << "The parcelable '" << this->GetName() << "' has duplicate field name '"
Jeongik Cha91180252020-07-31 15:43:11 +0900904 << v->GetName() << "'"
Jeongik Chad0a10272020-08-06 16:33:36 +0900905 << (IsJavaOnlyImmutable() ? " after capitalizing the first letter" : "");
Jeongik Cha13066da2020-08-06 15:43:19 +0900906 return false;
907 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900908 }
Jeongik Cha36f76c32020-07-28 00:25:52 +0900909
Daniel Norman85aed542019-08-21 12:01:14 -0700910 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900911}
912
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900913// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -0700914bool AidlTypeSpecifier::LanguageSpecificCheckValid(const AidlTypenames& typenames,
915 Options::Language lang) const {
Andrei Homescub62afd92020-05-11 19:24:59 -0700916 if ((lang == Options::Language::NDK || lang == Options::Language::RUST) && IsArray() &&
917 GetName() == "IBinder") {
918 AIDL_ERROR(this) << "The " << Options::LanguageToString(lang)
919 << " backend does not support array of IBinder";
Steven Moreland0185d9b2020-05-15 23:21:22 +0000920 return false;
921 }
Andrei Homescub62afd92020-05-11 19:24:59 -0700922 if ((lang == Options::Language::NDK || lang == Options::Language::RUST) && IsArray() &&
923 IsNullable()) {
Steven Moreland0185d9b2020-05-15 23:21:22 +0000924 if (GetName() == "ParcelFileDescriptor") {
Andrei Homescub62afd92020-05-11 19:24:59 -0700925 AIDL_ERROR(this) << "The " << Options::LanguageToString(lang)
926 << " backend does not support nullable array of ParcelFileDescriptor";
Steven Moreland0185d9b2020-05-15 23:21:22 +0000927 return false;
928 }
929
Steven Morelandd59e3172020-05-11 16:42:09 -0700930 const auto defined_type = typenames.TryGetDefinedType(GetName());
931 if (defined_type != nullptr && defined_type->AsParcelable() != nullptr) {
Andrei Homescub62afd92020-05-11 19:24:59 -0700932 AIDL_ERROR(this) << "The " << Options::LanguageToString(lang)
933 << " backend does not support nullable array of parcelable";
Steven Morelandd59e3172020-05-11 16:42:09 -0700934 return false;
935 }
936 }
Andrei Homescub62afd92020-05-11 19:24:59 -0700937 if (this->GetName() == "FileDescriptor" &&
938 (lang == Options::Language::NDK || lang == Options::Language::RUST)) {
939 AIDL_ERROR(this) << "FileDescriptor isn't supported by the " << Options::LanguageToString(lang)
940 << " backend.";
Steven Morelandc8a4ca82020-01-21 17:50:08 -0800941 return false;
942 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900943 if (this->IsGeneric()) {
944 if (this->GetName() == "List") {
945 if (this->GetTypeParameters().size() != 1) {
946 AIDL_ERROR(this) << "List must have only one type parameter.";
947 return false;
948 }
949 if (lang == Options::Language::CPP) {
Devin Moore2ac52f92020-03-23 15:39:36 -0700950 const string& contained_type = this->GetTypeParameters()[0]->GetName();
951 if (!(contained_type == "String" || contained_type == "IBinder")) {
952 AIDL_ERROR(this) << "List<" << contained_type
953 << "> is not supported. List in cpp supports only String and IBinder.";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900954 return false;
955 }
Jeongik Cha08ca2182019-11-21 14:01:13 +0900956 } else if (lang == Options::Language::JAVA) {
957 const string& contained_type = this->GetTypeParameters()[0]->GetName();
958 if (AidlTypenames::IsBuiltinTypename(contained_type)) {
959 if (contained_type != "String" && contained_type != "IBinder" &&
960 contained_type != "ParcelFileDescriptor") {
Devin Moore2ac52f92020-03-23 15:39:36 -0700961 AIDL_ERROR(this) << "List<" << contained_type
962 << "> is not supported. List in Java supports only String, IBinder, "
963 "and ParcelFileDescriptor.";
Jeongik Cha08ca2182019-11-21 14:01:13 +0900964 return false;
965 }
966 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900967 }
Jeongik Chabb55b5e2020-01-07 23:11:26 +0900968 }
969 }
Devin Moore6a01ca12020-08-28 10:24:19 -0700970
971 if (this->IsArray()) {
972 if (this->GetName() == "List" || this->GetName() == "Map" ||
973 this->GetName() == "CharSequence") {
974 AIDL_ERROR(this) << this->GetName() << "[] is not supported.";
Jeongik Chabb55b5e2020-01-07 23:11:26 +0900975 return false;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900976 }
977 }
Devin Moore6a01ca12020-08-28 10:24:19 -0700978
979 if (lang != Options::Language::JAVA) {
980 if (this->GetName() == "List" && !this->IsGeneric()) {
981 AIDL_ERROR(this) << "Currently, only the Java backend supports non-generic List.";
982 return false;
983 }
984 if (this->GetName() == "Map" || this->GetName() == "CharSequence") {
985 AIDL_ERROR(this) << "Currently, only Java backend supports " << this->GetName() << ".";
986 return false;
Jeongik Cha08ca2182019-11-21 14:01:13 +0900987 }
988 }
989
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900990 return true;
991}
992
993// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -0700994bool AidlParcelable::LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/,
995 Options::Language lang) const {
Andrei Homescub62afd92020-05-11 19:24:59 -0700996 if (lang == Options::Language::CPP || lang == Options::Language::NDK) {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800997 const AidlParcelable* unstructured_parcelable = this->AsUnstructuredParcelable();
998 if (unstructured_parcelable != nullptr) {
999 if (unstructured_parcelable->GetCppHeader().empty()) {
1000 AIDL_ERROR(unstructured_parcelable)
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001001 << "Unstructured parcelable must have C++ header defined.";
1002 return false;
1003 }
1004 }
1005 }
1006 return true;
1007}
1008
1009// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -07001010bool AidlStructuredParcelable::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1011 Options::Language lang) const {
1012 if (!AidlParcelable::LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001013 return false;
1014 }
1015 for (const auto& v : this->GetFields()) {
Steven Morelandd59e3172020-05-11 16:42:09 -07001016 if (!v->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001017 return false;
1018 }
1019 }
1020 return true;
1021}
1022
Daniel Norman85aed542019-08-21 12:01:14 -07001023AidlEnumerator::AidlEnumerator(const AidlLocation& location, const std::string& name,
Daniel Norman2e4112d2019-10-03 10:22:35 -07001024 AidlConstantValue* value, const std::string& comments)
1025 : AidlNode(location), name_(name), value_(value), comments_(comments) {}
Daniel Norman85aed542019-08-21 12:01:14 -07001026
1027bool AidlEnumerator::CheckValid(const AidlTypeSpecifier& enum_backing_type) const {
1028 if (GetValue() == nullptr) {
1029 return false;
1030 }
1031 if (!GetValue()->CheckValid()) {
1032 return false;
1033 }
Will McVickerd7d18df2019-09-12 13:40:50 -07001034 if (GetValue()->ValueString(enum_backing_type, AidlConstantValueDecorator).empty()) {
Daniel Norman85aed542019-08-21 12:01:14 -07001035 AIDL_ERROR(this) << "Enumerator type differs from enum backing type.";
1036 return false;
1037 }
1038 return true;
1039}
1040
1041string AidlEnumerator::ValueString(const AidlTypeSpecifier& backing_type,
1042 const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -07001043 return GetValue()->ValueString(backing_type, decorator);
Daniel Norman85aed542019-08-21 12:01:14 -07001044}
1045
1046AidlEnumDeclaration::AidlEnumDeclaration(const AidlLocation& location, const std::string& name,
1047 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jiyong Park18132182020-06-08 20:24:40 +09001048 const std::string& package, const std::string& comments)
Daniel Norman2e4112d2019-10-03 10:22:35 -07001049 : AidlDefinedType(location, name, comments, package), enumerators_(std::move(*enumerators)) {}
Daniel Norman85aed542019-08-21 12:01:14 -07001050
1051void AidlEnumDeclaration::SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type) {
1052 backing_type_ = std::move(type);
1053}
1054
Steven Moreland59e53e42019-11-26 20:38:08 -08001055bool AidlEnumDeclaration::Autofill() {
Daniel Normanb28684e2019-10-17 15:31:39 -07001056 const AidlEnumerator* previous = nullptr;
1057 for (const auto& enumerator : enumerators_) {
1058 if (enumerator->GetValue() == nullptr) {
1059 if (previous == nullptr) {
Devin Mooredf93ebb2020-03-25 14:03:35 -07001060 enumerator->SetValue(
1061 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(GetLocation(), "0")));
Daniel Normanb28684e2019-10-17 15:31:39 -07001062 } else {
Steven Moreland59e53e42019-11-26 20:38:08 -08001063 auto prev_value = std::unique_ptr<AidlConstantValue>(
1064 AidlConstantValue::ShallowIntegralCopy(*previous->GetValue()));
1065 if (prev_value == nullptr) {
1066 return false;
1067 }
Daniel Normanb28684e2019-10-17 15:31:39 -07001068 enumerator->SetValue(std::make_unique<AidlBinaryConstExpression>(
Devin Mooredf93ebb2020-03-25 14:03:35 -07001069 GetLocation(), std::move(prev_value), "+",
1070 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(GetLocation(), "1"))));
Daniel Normanb28684e2019-10-17 15:31:39 -07001071 }
1072 }
1073 previous = enumerator.get();
1074 }
Steven Moreland59e53e42019-11-26 20:38:08 -08001075 return true;
Daniel Normanb28684e2019-10-17 15:31:39 -07001076}
1077
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001078std::set<AidlAnnotation::Type> AidlEnumDeclaration::GetSupportedAnnotations() const {
1079 return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::BACKING,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +09001080 AidlAnnotation::Type::HIDE, AidlAnnotation::Type::JAVA_PASSTHROUGH};
Devin Moore24f68572020-02-26 13:20:59 -08001081}
1082
1083bool AidlEnumDeclaration::CheckValid(const AidlTypenames& typenames) const {
1084 if (!AidlDefinedType::CheckValid(typenames)) {
1085 return false;
1086 }
Daniel Norman85aed542019-08-21 12:01:14 -07001087 if (backing_type_ == nullptr) {
1088 AIDL_ERROR(this) << "Enum declaration missing backing type.";
1089 return false;
1090 }
1091 bool success = true;
1092 for (const auto& enumerator : enumerators_) {
1093 success = success && enumerator->CheckValid(GetBackingType());
1094 }
1095 return success;
1096}
1097
Jeongik Cha997281d2020-01-16 15:23:59 +09001098void AidlEnumDeclaration::Dump(CodeWriter* writer) const {
Steven Morelanda5d9c5c2020-02-21 16:01:09 -08001099 DumpHeader(writer);
Daniel Norman37d43dd2019-09-09 17:22:34 -07001100 writer->Write("enum %s {\n", GetName().c_str());
Daniel Norman85aed542019-08-21 12:01:14 -07001101 writer->Indent();
1102 for (const auto& enumerator : GetEnumerators()) {
Daniel Norman85aed542019-08-21 12:01:14 -07001103 writer->Write("%s = %s,\n", enumerator->GetName().c_str(),
1104 enumerator->ValueString(GetBackingType(), AidlConstantValueDecorator).c_str());
1105 }
1106 writer->Dedent();
1107 writer->Write("}\n");
1108}
1109
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001110// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -07001111bool AidlInterface::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1112 Options::Language lang) const {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001113 for (const auto& m : this->GetMethods()) {
Steven Morelandd59e3172020-05-11 16:42:09 -07001114 if (!m->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001115 return false;
1116 }
1117 for (const auto& arg : m->GetArguments()) {
Steven Morelandd59e3172020-05-11 16:42:09 -07001118 if (!arg->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001119 return false;
1120 }
1121 }
1122 }
1123 return true;
1124}
1125
Steven Moreland46e9da82018-07-27 15:45:29 -07001126AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name,
Casey Dahlinfb7da2e2015-10-08 17:26:09 -07001127 const std::string& comments, bool oneway,
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001128 std::vector<std::unique_ptr<AidlMember>>* members,
Jiyong Park18132182020-06-08 20:24:40 +09001129 const std::string& package)
Steven Morelandacd53472018-12-14 10:17:26 -08001130 : AidlDefinedType(location, name, comments, package) {
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001131 for (auto& member : *members) {
1132 AidlMember* local = member.release();
1133 AidlMethod* method = local->AsMethod();
Steven Moreland693640b2018-07-19 13:46:27 -07001134 AidlConstantDeclaration* constant = local->AsConstantDeclaration();
1135
1136 CHECK(method == nullptr || constant == nullptr);
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001137
1138 if (method) {
Steven Moreland8c70ba92018-12-17 10:20:31 -08001139 method->ApplyInterfaceOneway(oneway);
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001140 methods_.emplace_back(method);
Steven Moreland693640b2018-07-19 13:46:27 -07001141 } else if (constant) {
1142 constants_.emplace_back(constant);
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001143 } else {
Steven Moreland46e9da82018-07-27 15:45:29 -07001144 AIDL_FATAL(this) << "Member is neither method nor constant!";
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001145 }
1146 }
1147
1148 delete members;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -07001149}
1150
Jeongik Cha997281d2020-01-16 15:23:59 +09001151void AidlInterface::Dump(CodeWriter* writer) const {
Steven Morelanda5d9c5c2020-02-21 16:01:09 -08001152 DumpHeader(writer);
Jiyong Park02da7422018-07-16 16:00:26 +09001153 writer->Write("interface %s {\n", GetName().c_str());
1154 writer->Indent();
1155 for (const auto& method : GetMethods()) {
Jeongik Cha997281d2020-01-16 15:23:59 +09001156 if (method->IsHidden()) {
1157 AddHideComment(writer);
1158 }
Jiyong Park309668e2018-07-28 16:55:44 +09001159 writer->Write("%s;\n", method->ToString().c_str());
Jiyong Park02da7422018-07-16 16:00:26 +09001160 }
Jiyong Parka428d212018-08-29 22:26:30 +09001161 for (const auto& constdecl : GetConstantDeclarations()) {
Jeongik Cha997281d2020-01-16 15:23:59 +09001162 if (constdecl->GetType().IsHidden()) {
1163 AddHideComment(writer);
1164 }
Jiyong Parka428d212018-08-29 22:26:30 +09001165 writer->Write("%s;\n", constdecl->ToString().c_str());
1166 }
Jiyong Park02da7422018-07-16 16:00:26 +09001167 writer->Dedent();
1168 writer->Write("}\n");
1169}
1170
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001171std::set<AidlAnnotation::Type> AidlInterface::GetSupportedAnnotations() const {
1172 return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE,
Jiyong Park27fd7fd2020-08-27 16:25:09 +09001173 AidlAnnotation::Type::HIDE, AidlAnnotation::Type::JAVA_PASSTHROUGH,
1174 AidlAnnotation::Type::DESCRIPTOR};
Devin Moore24f68572020-02-26 13:20:59 -08001175}
1176
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001177bool AidlInterface::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -08001178 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001179 return false;
1180 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001181 // Has to be a pointer due to deleting copy constructor. No idea why.
1182 map<string, const AidlMethod*> method_names;
1183 for (const auto& m : GetMethods()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001184 if (!m->GetType().CheckValid(typenames)) {
1185 return false;
1186 }
1187
Jeongik Cha649e8a72020-03-27 17:47:40 +09001188 // TODO(b/156872582): Support it when ParcelableHolder supports every backend.
1189 if (m->GetType().GetName() == "ParcelableHolder") {
1190 AIDL_ERROR(m) << "ParcelableHolder cannot be a return type";
1191 return false;
1192 }
Steven Morelandacd53472018-12-14 10:17:26 -08001193 if (m->IsOneway() && m->GetType().GetName() != "void") {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001194 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot return a value";
1195 return false;
1196 }
1197
1198 set<string> argument_names;
1199 for (const auto& arg : m->GetArguments()) {
1200 auto it = argument_names.find(arg->GetName());
1201 if (it != argument_names.end()) {
1202 AIDL_ERROR(m) << "method '" << m->GetName() << "' has duplicate argument name '"
1203 << arg->GetName() << "'";
1204 return false;
1205 }
1206 argument_names.insert(arg->GetName());
1207
1208 if (!arg->GetType().CheckValid(typenames)) {
1209 return false;
1210 }
1211
Jeongik Cha649e8a72020-03-27 17:47:40 +09001212 // TODO(b/156872582): Support it when ParcelableHolder supports every backend.
1213 if (arg->GetType().GetName() == "ParcelableHolder") {
1214 AIDL_ERROR(arg) << "ParcelableHolder cannot be an argument type";
1215 return false;
1216 }
Steven Morelandacd53472018-12-14 10:17:26 -08001217 if (m->IsOneway() && arg->IsOut()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001218 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot have out parameters";
1219 return false;
1220 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001221 const bool can_be_out = typenames.CanBeOutParameter(arg->GetType());
1222 if (!arg->DirectionWasSpecified() && can_be_out) {
1223 AIDL_ERROR(arg) << "'" << arg->GetType().ToString()
1224 << "' can be an out type, so you must declare it as in, out, or inout.";
1225 return false;
1226 }
1227
1228 if (arg->GetDirection() != AidlArgument::IN_DIR && !can_be_out) {
1229 AIDL_ERROR(arg) << "'" << arg->ToString() << "' can only be an in parameter.";
1230 return false;
1231 }
1232
1233 // check that the name doesn't match a keyword
Jeongik Cha997281d2020-01-16 15:23:59 +09001234 if (IsJavaKeyword(arg->GetName().c_str())) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001235 AIDL_ERROR(arg) << "Argument name is a Java or aidl keyword";
1236 return false;
1237 }
1238
1239 // Reserve a namespace for internal use
1240 if (android::base::StartsWith(arg->GetName(), "_aidl")) {
1241 AIDL_ERROR(arg) << "Argument name cannot begin with '_aidl'";
1242 return false;
1243 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001244 }
1245
1246 auto it = method_names.find(m->GetName());
1247 // prevent duplicate methods
1248 if (it == method_names.end()) {
1249 method_names[m->GetName()] = m.get();
1250 } else {
1251 AIDL_ERROR(m) << "attempt to redefine method " << m->GetName() << ":";
1252 AIDL_ERROR(it->second) << "previously defined here.";
1253 return false;
1254 }
1255
Paul Trautrimb77048c2020-01-21 16:39:32 +09001256 static set<string> reserved_methods{"asBinder()", "getInterfaceHash()", "getInterfaceVersion()",
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001257 "getTransactionName(int)"};
1258
1259 if (reserved_methods.find(m->Signature()) != reserved_methods.end()) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001260 AIDL_ERROR(m) << " method " << m->Signature() << " is reserved for internal use.";
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001261 return false;
1262 }
1263 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001264
1265 bool success = true;
1266 set<string> constant_names;
1267 for (const std::unique_ptr<AidlConstantDeclaration>& constant : GetConstantDeclarations()) {
1268 if (constant_names.count(constant->GetName()) > 0) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001269 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001270 success = false;
1271 }
1272 constant_names.insert(constant->GetName());
1273 success = success && constant->CheckValid(typenames);
1274 }
1275
1276 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001277}
1278
Jiyong Park27fd7fd2020-08-27 16:25:09 +09001279std::string AidlInterface::GetDescriptor() const {
1280 std::string annotatedDescriptor = AidlAnnotatable::GetDescriptor();
1281 if (annotatedDescriptor != "") {
1282 return annotatedDescriptor;
1283 }
1284 return GetCanonicalName();
1285}
1286
Steven Moreland46e9da82018-07-27 15:45:29 -07001287AidlImport::AidlImport(const AidlLocation& location, const std::string& needed_class)
1288 : AidlNode(location), needed_class_(needed_class) {}