blob: 2678a5a2bbc89bd2feece5b6788a51d89b96bfbc [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
72void AddHideComment(CodeWriter* writer) {
73 writer->Write("/* @hide */\n");
74}
75
76inline bool HasHideComment(const std::string& comment) {
77 return std::regex_search(comment, std::regex("@hide\\b"));
78}
Jeongik Cha047c5ee2019-08-07 23:16:49 +090079} // namespace
80
Casey Dahlincdbbc8c2015-10-14 15:31:04 -070081AidlToken::AidlToken(const std::string& text, const std::string& comments)
82 : text_(text),
83 comments_(comments) {}
Casey Dahlin98a544b2015-10-14 14:22:55 -070084
Devin Mooredf93ebb2020-03-25 14:03:35 -070085AidlLocation::AidlLocation(const std::string& file, Point begin, Point end, Source source)
86 : file_(file), begin_(begin), end_(end), source_(source) {}
Steven Moreland46e9da82018-07-27 15:45:29 -070087
88std::ostream& operator<<(std::ostream& os, const AidlLocation& l) {
Devin Moore5de18ed2020-04-02 13:52:29 -070089 os << l.file_;
90 if (l.LocationKnown()) {
91 os << ":" << l.begin_.line << "." << l.begin_.column << "-";
92 if (l.begin_.line != l.end_.line) {
93 os << l.end_.line << ".";
94 }
95 os << l.end_.column;
Steven Moreland46e9da82018-07-27 15:45:29 -070096 }
Steven Moreland46e9da82018-07-27 15:45:29 -070097 return os;
98}
99
100AidlNode::AidlNode(const AidlLocation& location) : location_(location) {}
101
Mathew Inwoodadb74672019-11-29 14:01:53 +0000102std::string AidlNode::PrintLine() const {
Andrei Onea8714b022019-02-01 18:55:54 +0000103 std::stringstream ss;
104 ss << location_.file_ << ":" << location_.begin_.line;
105 return ss.str();
106}
107
Mathew Inwoodadb74672019-11-29 14:01:53 +0000108std::string AidlNode::PrintLocation() const {
109 std::stringstream ss;
110 ss << location_.file_ << ":" << location_.begin_.line << ":" << location_.begin_.column << ":"
111 << location_.end_.line << ":" << location_.end_.column;
112 return ss.str();
113}
114
Devin Moore5de18ed2020-04-02 13:52:29 -0700115AidlErrorLog::AidlErrorLog(bool fatal, const AidlLocation& location)
116 : os_(std::cerr), fatal_(fatal), location_(location) {
Steven Morelandfdb57cd2020-01-08 20:03:30 -0800117 sHadError = true;
118
Steven Moreland92c55f12018-07-31 14:08:37 -0700119 os_ << "ERROR: ";
Devin Mooredf93ebb2020-03-25 14:03:35 -0700120 os_ << location << ": ";
121}
122
Steven Morelandb0d15a52020-03-31 14:03:47 -0700123bool AidlErrorLog::sHadError = false;
Steven Morelandfdb57cd2020-01-08 20:03:30 -0800124
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700125const std::vector<AidlAnnotation::Schema>& AidlAnnotation::AllSchemas() {
126 static const std::vector<Schema> kSchemas{
127 {AidlAnnotation::Type::NULLABLE, "nullable", {}},
128 {AidlAnnotation::Type::UTF8_IN_CPP, "utf8InCpp", {}},
129 {AidlAnnotation::Type::VINTF_STABILITY, "VintfStability", {}},
130 {AidlAnnotation::Type::UNSUPPORTED_APP_USAGE,
131 "UnsupportedAppUsage",
132 {{"expectedSignature", "String"},
133 {"implicitMember", "String"},
134 {"maxTargetSdk", "int"},
135 {"publicAlternatives", "String"},
136 {"trackingBug", "long"}}},
137 {AidlAnnotation::Type::JAVA_STABLE_PARCELABLE, "JavaOnlyStableParcelable", {}},
138 {AidlAnnotation::Type::HIDE, "Hide", {}},
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900139 {AidlAnnotation::Type::BACKING, "Backing", {{"type", "String"}}},
140 {AidlAnnotation::Type::JAVA_PASSTHROUGH, "JavaPassthrough", {{"annotation", "String"}}},
141 };
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700142 return kSchemas;
143}
Jiyong Park68bc77a2018-07-19 19:00:45 +0900144
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700145std::string AidlAnnotation::TypeToString(Type type) {
146 for (const Schema& schema : AllSchemas()) {
147 if (type == schema.type) return schema.name;
148 }
149 AIDL_FATAL(AIDL_LOCATION_HERE) << "Unrecognized type: " << static_cast<size_t>(type);
150 __builtin_unreachable();
151}
Andrei Onea9445fc62019-06-27 18:11:59 +0100152
153AidlAnnotation* AidlAnnotation::Parse(
154 const AidlLocation& location, const string& name,
155 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700156 const Schema* schema = nullptr;
157 for (const Schema& a_schema : AllSchemas()) {
158 if (a_schema.name == name) {
159 schema = &a_schema;
160 }
161 }
162
163 if (schema == nullptr) {
Jiyong Park68bc77a2018-07-19 19:00:45 +0900164 std::ostringstream stream;
Steven Moreland46e9da82018-07-27 15:45:29 -0700165 stream << "'" << name << "' is not a recognized annotation. ";
Jiyong Park68bc77a2018-07-19 19:00:45 +0900166 stream << "It must be one of:";
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700167 for (const Schema& s : AllSchemas()) {
168 stream << " " << s.name;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900169 }
170 stream << ".";
Steven Moreland46e9da82018-07-27 15:45:29 -0700171 AIDL_ERROR(location) << stream.str();
172 return nullptr;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900173 }
Andrei Onea9445fc62019-06-27 18:11:59 +0100174 if (parameter_list == nullptr) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700175 return new AidlAnnotation(location, *schema, {});
Andrei Onea9445fc62019-06-27 18:11:59 +0100176 }
177
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700178 return new AidlAnnotation(location, *schema, std::move(*parameter_list));
Jiyong Park68bc77a2018-07-19 19:00:45 +0900179}
180
Andrei Onea9445fc62019-06-27 18:11:59 +0100181AidlAnnotation::AidlAnnotation(
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700182 const AidlLocation& location, const Schema& schema,
Andrei Onea9445fc62019-06-27 18:11:59 +0100183 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters)
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700184 : AidlNode(location), schema_(schema), parameters_(std::move(parameters)) {}
Andrei Onea9445fc62019-06-27 18:11:59 +0100185
186bool AidlAnnotation::CheckValid() const {
Andrei Onea9445fc62019-06-27 18:11:59 +0100187 for (const auto& name_and_param : parameters_) {
188 const std::string& param_name = name_and_param.first;
189 const std::shared_ptr<AidlConstantValue>& param = name_and_param.second;
Will McVickerd7d18df2019-09-12 13:40:50 -0700190 if (!param->CheckValid()) {
191 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
192 << GetName() << ".";
193 return false;
194 }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700195 auto parameter_mapping_it = schema_.supported_parameters.find(param_name);
196 if (parameter_mapping_it == schema_.supported_parameters.end()) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100197 std::ostringstream stream;
198 stream << "Parameter " << param_name << " not supported ";
Devin Mooredecaf292020-04-30 09:16:40 -0700199 stream << "for annotation " << GetName() << ". ";
Andrei Onea9445fc62019-06-27 18:11:59 +0100200 stream << "It must be one of:";
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700201 for (const auto& kv : schema_.supported_parameters) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100202 stream << " " << kv.first;
203 }
204 AIDL_ERROR(this) << stream.str();
205 return false;
206 }
207 AidlTypeSpecifier type{AIDL_LOCATION_HERE, parameter_mapping_it->second, false, nullptr, ""};
Will McVickerd7d18df2019-09-12 13:40:50 -0700208 const std::string param_value = param->ValueString(type, AidlConstantValueDecorator);
Andrei Onea9445fc62019-06-27 18:11:59 +0100209 // Assume error on empty string.
210 if (param_value == "") {
211 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
212 << GetName() << ".";
213 return false;
214 }
215 }
216 return true;
217}
218
219std::map<std::string, std::string> AidlAnnotation::AnnotationParams(
220 const ConstantValueDecorator& decorator) const {
221 std::map<std::string, std::string> raw_params;
Andrei Onea9445fc62019-06-27 18:11:59 +0100222 for (const auto& name_and_param : parameters_) {
223 const std::string& param_name = name_and_param.first;
224 const std::shared_ptr<AidlConstantValue>& param = name_and_param.second;
Devin Mooredecaf292020-04-30 09:16:40 -0700225 if (schema_.supported_parameters.find(param_name) == schema_.supported_parameters.end()) {
226 std::ostringstream stream;
227 stream << "Parameter " << param_name << " not supported ";
228 stream << "for annotation " << GetName() << ". ";
229 stream << "It must be one of:";
230 for (const auto& kv : schema_.supported_parameters) {
231 stream << " " << kv.first;
232 }
233 AIDL_ERROR(this) << stream.str();
234 continue;
235 }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700236 AidlTypeSpecifier type{AIDL_LOCATION_HERE, schema_.supported_parameters.at(param_name), false,
237 nullptr, ""};
Will McVickerd7d18df2019-09-12 13:40:50 -0700238 if (!param->CheckValid()) {
239 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
240 << GetName() << ".";
Devin Mooredecaf292020-04-30 09:16:40 -0700241 continue;
Will McVickerd7d18df2019-09-12 13:40:50 -0700242 }
243
244 raw_params.emplace(param_name, param->ValueString(type, decorator));
Andrei Onea9445fc62019-06-27 18:11:59 +0100245 }
246 return raw_params;
247}
Steven Moreland46e9da82018-07-27 15:45:29 -0700248
Daniel Norman37d43dd2019-09-09 17:22:34 -0700249std::string AidlAnnotation::ToString(const ConstantValueDecorator& decorator) const {
250 if (parameters_.empty()) {
251 return "@" + GetName();
252 } else {
253 vector<string> param_strings;
254 for (const auto& [name, value] : AnnotationParams(decorator)) {
255 param_strings.emplace_back(name + "=" + value);
256 }
257 return "@" + GetName() + "(" + Join(param_strings, ", ") + ")";
258 }
259}
260
Andrei Onea9445fc62019-06-27 18:11:59 +0100261static const AidlAnnotation* GetAnnotation(const vector<AidlAnnotation>& annotations,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700262 AidlAnnotation::Type type) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100263 for (const auto& a : annotations) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700264 if (a.GetType() == type) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100265 return &a;
266 }
267 }
268 return nullptr;
269}
270
Steven Moreland46e9da82018-07-27 15:45:29 -0700271AidlAnnotatable::AidlAnnotatable(const AidlLocation& location) : AidlNode(location) {}
272
Jiyong Park68bc77a2018-07-19 19:00:45 +0900273bool AidlAnnotatable::IsNullable() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700274 return GetAnnotation(annotations_, AidlAnnotation::Type::NULLABLE);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900275}
276
Jiyong Park68bc77a2018-07-19 19:00:45 +0900277bool AidlAnnotatable::IsUtf8InCpp() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700278 return GetAnnotation(annotations_, AidlAnnotation::Type::UTF8_IN_CPP);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900279}
280
Steven Morelanda57d0a62019-07-30 09:41:14 -0700281bool AidlAnnotatable::IsVintfStability() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700282 return GetAnnotation(annotations_, AidlAnnotation::Type::VINTF_STABILITY);
Steven Morelanda57d0a62019-07-30 09:41:14 -0700283}
284
Andrei Onea9445fc62019-06-27 18:11:59 +0100285const AidlAnnotation* AidlAnnotatable::UnsupportedAppUsage() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700286 return GetAnnotation(annotations_, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE);
Jiyong Parka6605ab2018-11-11 14:30:21 +0900287}
288
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900289const AidlAnnotation* AidlAnnotatable::JavaPassthrough() const {
290 return GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_PASSTHROUGH);
291}
292
Daniel Norman716d3112019-09-10 13:11:56 -0700293const AidlTypeSpecifier* AidlAnnotatable::BackingType(const AidlTypenames& typenames) const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700294 auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::BACKING);
Daniel Norman85aed542019-08-21 12:01:14 -0700295 if (annotation != nullptr) {
296 auto annotation_params = annotation->AnnotationParams(AidlConstantValueDecorator);
297 if (auto it = annotation_params.find("type"); it != annotation_params.end()) {
298 const string& type = it->second;
Steven Morelande7d5d082020-05-21 20:29:02 +0000299
300 AIDL_FATAL_IF(type.size() < 2, this) << type;
301 AIDL_FATAL_IF(type[0] != '"', this) << type;
302 AIDL_FATAL_IF(type[type.length() - 1] != '"', this) << type;
303 string unquoted_type = type.substr(1, type.length() - 2);
304
Daniel Norman716d3112019-09-10 13:11:56 -0700305 AidlTypeSpecifier* type_specifier =
Steven Morelande7d5d082020-05-21 20:29:02 +0000306 new AidlTypeSpecifier(AIDL_LOCATION_HERE, unquoted_type, false, nullptr, "");
Daniel Norman716d3112019-09-10 13:11:56 -0700307 type_specifier->Resolve(typenames);
308 return type_specifier;
Daniel Norman85aed542019-08-21 12:01:14 -0700309 }
310 }
311 return nullptr;
312}
313
Jeongik Cha88f95a82020-01-15 13:02:16 +0900314bool AidlAnnotatable::IsStableApiParcelable(Options::Language lang) const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700315 return lang == Options::Language::JAVA &&
316 GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_STABLE_PARCELABLE);
Jeongik Cha82317dd2019-02-27 20:26:11 +0900317}
318
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800319bool AidlAnnotatable::IsHide() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700320 return GetAnnotation(annotations_, AidlAnnotation::Type::HIDE);
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800321}
322
Steven Moreland7e4b9502020-02-20 18:10:42 -0800323void AidlAnnotatable::DumpAnnotations(CodeWriter* writer) const {
324 if (annotations_.empty()) return;
325
326 writer->Write("%s\n", AidlAnnotatable::ToString().c_str());
327}
328
Devin Moore24f68572020-02-26 13:20:59 -0800329bool AidlAnnotatable::CheckValid(const AidlTypenames&) const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700330 std::set<AidlAnnotation::Type> supported_annotations = GetSupportedAnnotations();
Andrei Onea9445fc62019-06-27 18:11:59 +0100331 for (const auto& annotation : GetAnnotations()) {
332 if (!annotation.CheckValid()) {
333 return false;
334 }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700335
336 std::vector<std::string> supported_annot_strings;
337 for (AidlAnnotation::Type type : supported_annotations) {
338 supported_annot_strings.push_back(AidlAnnotation::TypeToString(type));
339 }
340
341 if (supported_annotations.find(annotation.GetType()) == supported_annotations.end()) {
Devin Moore24f68572020-02-26 13:20:59 -0800342 AIDL_ERROR(this) << "'" << annotation.GetName()
343 << "' is not a supported annotation for this node. "
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700344 << "It must be one of: "
345 << android::base::Join(supported_annot_strings, ", ");
Devin Moore24f68572020-02-26 13:20:59 -0800346 return false;
347 }
Andrei Onea9445fc62019-06-27 18:11:59 +0100348 }
Steven Morelanda57d0a62019-07-30 09:41:14 -0700349
Andrei Onea9445fc62019-06-27 18:11:59 +0100350 return true;
351}
352
Jiyong Park68bc77a2018-07-19 19:00:45 +0900353string AidlAnnotatable::ToString() const {
354 vector<string> ret;
355 for (const auto& a : annotations_) {
Daniel Norman37d43dd2019-09-09 17:22:34 -0700356 ret.emplace_back(a.ToString(AidlConstantValueDecorator));
Jiyong Park68bc77a2018-07-19 19:00:45 +0900357 }
358 std::sort(ret.begin(), ret.end());
359 return Join(ret, " ");
360}
361
Steven Moreland46e9da82018-07-27 15:45:29 -0700362AidlTypeSpecifier::AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name,
363 bool is_array,
Jiyong Park1deecc32018-07-17 01:14:41 +0900364 vector<unique_ptr<AidlTypeSpecifier>>* type_params,
Steven Moreland46e9da82018-07-27 15:45:29 -0700365 const string& comments)
366 : AidlAnnotatable(location),
Jeongik Chadf76dc72019-11-28 00:08:47 +0900367 AidlParameterizable<unique_ptr<AidlTypeSpecifier>>(type_params),
Steven Moreland46e9da82018-07-27 15:45:29 -0700368 unresolved_name_(unresolved_name),
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700369 is_array_(is_array),
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900370 comments_(comments),
371 split_name_(Split(unresolved_name, ".")) {}
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700372
Steven Moreland3f658cf2018-08-20 13:40:54 -0700373AidlTypeSpecifier AidlTypeSpecifier::ArrayBase() const {
374 AIDL_FATAL_IF(!is_array_, this);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900375 // Declaring array of generic type cannot happen, it is grammar error.
376 AIDL_FATAL_IF(IsGeneric(), this);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700377
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800378 AidlTypeSpecifier array_base = *this;
379 array_base.is_array_ = false;
380 return array_base;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700381}
382
Jeongik Cha997281d2020-01-16 15:23:59 +0900383bool AidlTypeSpecifier::IsHidden() const {
384 return HasHideComment(GetComments());
385}
386
Jiyong Park1deecc32018-07-17 01:14:41 +0900387string AidlTypeSpecifier::ToString() const {
388 string ret = GetName();
389 if (IsGeneric()) {
390 vector<string> arg_names;
391 for (const auto& ta : GetTypeParameters()) {
Jiyong Parkccf00f82018-07-17 01:39:23 +0900392 arg_names.emplace_back(ta->ToString());
393 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900394 ret += "<" + Join(arg_names, ",") + ">";
Jiyong Parkccf00f82018-07-17 01:39:23 +0900395 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900396 if (IsArray()) {
397 ret += "[]";
398 }
399 return ret;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900400}
401
Jiyong Park02da7422018-07-16 16:00:26 +0900402string AidlTypeSpecifier::Signature() const {
403 string ret = ToString();
404 string annotations = AidlAnnotatable::ToString();
405 if (annotations != "") {
406 ret = annotations + " " + ret;
407 }
408 return ret;
409}
410
Daniel Norman716d3112019-09-10 13:11:56 -0700411bool AidlTypeSpecifier::Resolve(const AidlTypenames& typenames) {
Steven Moreland9731c632019-08-13 10:21:08 -0700412 CHECK(!IsResolved());
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700413 AidlTypenames::ResolvedTypename result = typenames.ResolveTypename(unresolved_name_);
414 if (result.is_resolved) {
415 fully_qualified_name_ = result.canonical_name;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900416 split_name_ = Split(fully_qualified_name_, ".");
Jiyong Parkccf00f82018-07-17 01:39:23 +0900417 }
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700418 return result.is_resolved;
Casey Dahlin70078e62015-09-30 17:01:30 -0700419}
420
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700421std::set<AidlAnnotation::Type> AidlTypeSpecifier::GetSupportedAnnotations() const {
Devin Moore24f68572020-02-26 13:20:59 -0800422 // kHide and kUnsupportedAppUsage are both method return annotations
423 // which we don't distinguish from other type specifiers.
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700424 return {AidlAnnotation::Type::NULLABLE, AidlAnnotation::Type::UTF8_IN_CPP,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900425 AidlAnnotation::Type::UNSUPPORTED_APP_USAGE, AidlAnnotation::Type::HIDE,
426 AidlAnnotation::Type::JAVA_PASSTHROUGH};
Devin Moore24f68572020-02-26 13:20:59 -0800427}
428
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900429bool AidlTypeSpecifier::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -0800430 if (!AidlAnnotatable::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100431 return false;
432 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900433 if (IsGeneric()) {
434 const string& type_name = GetName();
Jeongik Chae74c86d2019-12-12 16:54:03 +0900435
436 auto& types = GetTypeParameters();
437 // TODO(b/136048684) Disallow to use primitive types only if it is List or Map.
438 if (type_name == "List" || type_name == "Map") {
439 if (std::any_of(types.begin(), types.end(), [](auto& type_ptr) {
440 return AidlTypenames::IsPrimitiveTypename(type_ptr->GetName());
441 })) {
Devin Moore7b8d5c92020-03-17 14:14:08 -0700442 AIDL_ERROR(this) << "A generic type cannot have any primitive type parameters.";
Jeongik Chae74c86d2019-12-12 16:54:03 +0900443 return false;
444 }
445 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800446 const auto defined_type = typenames.TryGetDefinedType(type_name);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900447 const auto parameterizable =
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800448 defined_type != nullptr ? defined_type->AsParameterizable() : nullptr;
449 const bool is_user_defined_generic_type =
Jeongik Chadf76dc72019-11-28 00:08:47 +0900450 parameterizable != nullptr && parameterizable->IsGeneric();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800451 const size_t num_params = GetTypeParameters().size();
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900452 if (type_name == "List") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800453 if (num_params > 1) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900454 AIDL_ERROR(this) << " List cannot have type parameters more than one, but got "
455 << "'" << ToString() << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900456 return false;
457 }
458 } else if (type_name == "Map") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800459 if (num_params != 0 && num_params != 2) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900460 AIDL_ERROR(this) << "Map must have 0 or 2 type parameters, but got "
461 << "'" << ToString() << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900462 return false;
463 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800464 if (num_params == 2) {
Jeongik Chae48d9942020-01-02 17:39:00 +0900465 const string& key_type = GetTypeParameters()[0]->GetName();
466 if (key_type != "String") {
467 AIDL_ERROR(this) << "The type of key in map must be String, but it is "
468 << "'" << key_type << "'";
469 return false;
470 }
471 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800472 } else if (is_user_defined_generic_type) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900473 const size_t allowed = parameterizable->GetTypeParameters().size();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800474 if (num_params != allowed) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900475 AIDL_ERROR(this) << type_name << " must have " << allowed << " type parameters, but got "
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800476 << num_params;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900477 return false;
478 }
479 } else {
480 AIDL_ERROR(this) << type_name << " is not a generic type.";
481 return false;
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900482 }
483 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900484
Steven Moreland11cb9452020-01-21 16:56:58 -0800485 const bool is_generic_string_list = GetName() == "List" && IsGeneric() &&
486 GetTypeParameters().size() == 1 &&
487 GetTypeParameters()[0]->GetName() == "String";
488 if (IsUtf8InCpp() && (GetName() != "String" && !is_generic_string_list)) {
489 AIDL_ERROR(this) << "@utf8InCpp can only be used on String, String[], and List<String>.";
490 return false;
491 }
492
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900493 if (GetName() == "void") {
494 if (IsArray() || IsNullable() || IsUtf8InCpp()) {
495 AIDL_ERROR(this) << "void type cannot be an array or nullable or utf8 string";
496 return false;
497 }
498 }
499
500 if (IsArray()) {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800501 const auto defined_type = typenames.TryGetDefinedType(GetName());
502 if (defined_type != nullptr && defined_type->AsInterface() != nullptr) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900503 AIDL_ERROR(this) << "Binder type cannot be an array";
504 return false;
505 }
506 }
507
508 if (IsNullable()) {
509 if (AidlTypenames::IsPrimitiveTypename(GetName()) && !IsArray()) {
510 AIDL_ERROR(this) << "Primitive type cannot get nullable annotation";
511 return false;
512 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800513 const auto defined_type = typenames.TryGetDefinedType(GetName());
514 if (defined_type != nullptr && defined_type->AsEnumDeclaration() != nullptr && !IsArray()) {
Daniel Normanee8674f2019-09-20 16:07:00 -0700515 AIDL_ERROR(this) << "Enum type cannot get nullable annotation";
516 return false;
517 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900518 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900519 return true;
520}
521
Steven Moreland860b1942018-08-16 14:59:28 -0700522std::string AidlConstantValueDecorator(const AidlTypeSpecifier& /*type*/,
523 const std::string& raw_value) {
524 return raw_value;
525}
526
Steven Moreland46e9da82018-07-27 15:45:29 -0700527AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
528 AidlTypeSpecifier* type, const std::string& name)
529 : AidlVariableDeclaration(location, type, name, nullptr /*default_value*/) {}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700530
Steven Moreland46e9da82018-07-27 15:45:29 -0700531AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
532 AidlTypeSpecifier* type, const std::string& name,
533 AidlConstantValue* default_value)
534 : AidlNode(location), type_(type), name_(name), default_value_(default_value) {}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700535
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900536bool AidlVariableDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700537 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900538 valid &= type_->CheckValid(typenames);
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900539
Steven Moreland54be7bd2019-12-05 11:17:53 -0800540 if (type_->GetName() == "void") {
541 AIDL_ERROR(this) << "Declaration " << name_
542 << " is void, but declarations cannot be of void type.";
543 valid = false;
544 }
545
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900546 if (default_value_ == nullptr) return valid;
Steven Moreland25294322018-08-07 18:13:55 -0700547 valid &= default_value_->CheckValid();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700548
Steven Moreland25294322018-08-07 18:13:55 -0700549 if (!valid) return false;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700550
Steven Moreland860b1942018-08-16 14:59:28 -0700551 return !ValueString(AidlConstantValueDecorator).empty();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700552}
Steven Moreland5557f1c2018-07-02 13:50:23 -0700553
554string AidlVariableDeclaration::ToString() const {
Jeongik Cha3271ffa2018-12-04 15:19:20 +0900555 string ret = type_->Signature() + " " + name_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700556 if (default_value_ != nullptr) {
Steven Moreland860b1942018-08-16 14:59:28 -0700557 ret += " = " + ValueString(AidlConstantValueDecorator);
Steven Moreland9ea10e32018-07-19 15:26:09 -0700558 }
559 return ret;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700560}
561
Jiyong Park02da7422018-07-16 16:00:26 +0900562string AidlVariableDeclaration::Signature() const {
563 return type_->Signature() + " " + name_;
564}
565
Steven Moreland860b1942018-08-16 14:59:28 -0700566std::string AidlVariableDeclaration::ValueString(const ConstantValueDecorator& decorator) const {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900567 if (default_value_ != nullptr) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700568 return default_value_->ValueString(GetType(), decorator);
Jiyong Parka468e2a2018-08-29 21:25:18 +0900569 } else {
570 return "";
571 }
Steven Moreland25294322018-08-07 18:13:55 -0700572}
573
Steven Moreland46e9da82018-07-27 15:45:29 -0700574AidlArgument::AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
575 AidlTypeSpecifier* type, const std::string& name)
576 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700577 direction_(direction),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700578 direction_specified_(true) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700579
Steven Moreland46e9da82018-07-27 15:45:29 -0700580AidlArgument::AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type,
581 const std::string& name)
582 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700583 direction_(AidlArgument::IN_DIR),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700584 direction_specified_(false) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700585
Jiyong Park02da7422018-07-16 16:00:26 +0900586string AidlArgument::GetDirectionSpecifier() const {
Casey Dahlinc378c992015-09-29 16:50:40 -0700587 string ret;
Casey Dahlinc378c992015-09-29 16:50:40 -0700588 if (direction_specified_) {
589 switch(direction_) {
590 case AidlArgument::IN_DIR:
Devin Mooreeccdb902020-03-24 16:22:40 -0700591 ret += "in";
Casey Dahlinc378c992015-09-29 16:50:40 -0700592 break;
593 case AidlArgument::OUT_DIR:
Devin Mooreeccdb902020-03-24 16:22:40 -0700594 ret += "out";
Casey Dahlinc378c992015-09-29 16:50:40 -0700595 break;
596 case AidlArgument::INOUT_DIR:
Devin Mooreeccdb902020-03-24 16:22:40 -0700597 ret += "inout";
Casey Dahlinc378c992015-09-29 16:50:40 -0700598 break;
599 }
600 }
Casey Dahlinc378c992015-09-29 16:50:40 -0700601 return ret;
602}
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700603
Jiyong Park02da7422018-07-16 16:00:26 +0900604string AidlArgument::ToString() const {
Devin Mooreeccdb902020-03-24 16:22:40 -0700605 if (direction_specified_) {
606 return GetDirectionSpecifier() + " " + AidlVariableDeclaration::ToString();
607 } else {
608 return AidlVariableDeclaration::ToString();
609 }
Jiyong Park02da7422018-07-16 16:00:26 +0900610}
611
612std::string AidlArgument::Signature() const {
Steven Moreland46e9da82018-07-27 15:45:29 -0700613 class AidlInterface;
614 class AidlInterface;
615 class AidlParcelable;
616 class AidlStructuredParcelable;
617 class AidlParcelable;
618 class AidlStructuredParcelable;
Devin Mooreeccdb902020-03-24 16:22:40 -0700619 if (direction_specified_) {
620 return GetDirectionSpecifier() + " " + AidlVariableDeclaration::Signature();
621 } else {
622 return AidlVariableDeclaration::Signature();
623 }
Jiyong Park02da7422018-07-16 16:00:26 +0900624}
625
Steven Moreland46e9da82018-07-27 15:45:29 -0700626AidlMember::AidlMember(const AidlLocation& location) : AidlNode(location) {}
627
Steven Moreland46e9da82018-07-27 15:45:29 -0700628AidlConstantDeclaration::AidlConstantDeclaration(const AidlLocation& location,
629 AidlTypeSpecifier* type, const std::string& name,
630 AidlConstantValue* value)
631 : AidlMember(location), type_(type), name_(name), value_(value) {}
Steven Moreland693640b2018-07-19 13:46:27 -0700632
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900633bool AidlConstantDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700634 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900635 valid &= type_->CheckValid(typenames);
Steven Moreland25294322018-08-07 18:13:55 -0700636 valid &= value_->CheckValid();
637 if (!valid) return false;
Steven Moreland693640b2018-07-19 13:46:27 -0700638
Steven Moreland25294322018-08-07 18:13:55 -0700639 const static set<string> kSupportedConstTypes = {"String", "int"};
640 if (kSupportedConstTypes.find(type_->ToString()) == kSupportedConstTypes.end()) {
641 AIDL_ERROR(this) << "Constant of type " << type_->ToString() << " is not supported.";
Steven Moreland693640b2018-07-19 13:46:27 -0700642 return false;
643 }
644
Will McVickerd7d18df2019-09-12 13:40:50 -0700645 return true;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700646}
647
Jiyong Parka428d212018-08-29 22:26:30 +0900648string AidlConstantDeclaration::ToString() const {
649 return "const " + type_->ToString() + " " + name_ + " = " +
650 ValueString(AidlConstantValueDecorator);
651}
652
653string AidlConstantDeclaration::Signature() const {
654 return type_->Signature() + " " + name_;
655}
656
Steven Moreland46e9da82018-07-27 15:45:29 -0700657AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
658 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900659 const std::string& comments)
660 : AidlMethod(location, oneway, type, name, args, comments, 0, true) {
661 has_id_ = false;
662}
663
664AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
665 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900666 const std::string& comments, int id, bool is_user_defined)
Steven Moreland46e9da82018-07-27 15:45:29 -0700667 : AidlMember(location),
668 oneway_(oneway),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700669 comments_(comments),
670 type_(type),
671 name_(name),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700672 arguments_(std::move(*args)),
Jiyong Parkb034bf02018-07-30 17:44:33 +0900673 id_(id),
674 is_user_defined_(is_user_defined) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700675 has_id_ = true;
676 delete args;
Christopher Wileyad339272015-10-05 19:11:58 -0700677 for (const unique_ptr<AidlArgument>& a : arguments_) {
678 if (a->IsIn()) { in_arguments_.push_back(a.get()); }
679 if (a->IsOut()) { out_arguments_.push_back(a.get()); }
680 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700681}
682
Jeongik Cha997281d2020-01-16 15:23:59 +0900683bool AidlMethod::IsHidden() const {
684 return HasHideComment(GetComments());
685}
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700686
Jiyong Park02da7422018-07-16 16:00:26 +0900687string AidlMethod::Signature() const {
688 vector<string> arg_signatures;
689 for (const auto& arg : GetArguments()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900690 arg_signatures.emplace_back(arg->GetType().ToString());
Jiyong Park02da7422018-07-16 16:00:26 +0900691 }
Jiyong Park309668e2018-07-28 16:55:44 +0900692 return GetName() + "(" + Join(arg_signatures, ", ") + ")";
693}
694
695string AidlMethod::ToString() const {
696 vector<string> arg_strings;
697 for (const auto& arg : GetArguments()) {
698 arg_strings.emplace_back(arg->Signature());
699 }
Steven Moreland4ee68632018-12-14 15:52:46 -0800700 string ret = (IsOneway() ? "oneway " : "") + GetType().Signature() + " " + GetName() + "(" +
701 Join(arg_strings, ", ") + ")";
Jiyong Parked65bf42018-08-28 15:43:27 +0900702 if (HasId()) {
703 ret += " = " + std::to_string(GetId());
704 }
705 return ret;
Jiyong Park02da7422018-07-16 16:00:26 +0900706}
707
Steven Moreland46e9da82018-07-27 15:45:29 -0700708AidlDefinedType::AidlDefinedType(const AidlLocation& location, const std::string& name,
Jiyong Park18132182020-06-08 20:24:40 +0900709 const std::string& comments, const std::string& package)
710 : AidlAnnotatable(location),
711 name_(name),
712 comments_(comments),
713 package_(package),
714 split_package_(package.empty() ? std::vector<std::string>()
715 : android::base::Split(package, ".")) {}
Steven Moreland787b0432018-07-03 09:00:58 -0700716
Devin Moore24f68572020-02-26 13:20:59 -0800717bool AidlDefinedType::CheckValid(const AidlTypenames& typenames) const {
718 if (!AidlAnnotatable::CheckValid(typenames)) {
719 return false;
720 }
721
722 return true;
723}
724
Jeongik Cha997281d2020-01-16 15:23:59 +0900725bool AidlDefinedType::IsHidden() const {
726 return HasHideComment(GetComments());
727}
728
Steven Moreland787b0432018-07-03 09:00:58 -0700729std::string AidlDefinedType::GetCanonicalName() const {
730 if (package_.empty()) {
731 return GetName();
732 }
733 return GetPackage() + "." + GetName();
734}
735
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800736void AidlDefinedType::DumpHeader(CodeWriter* writer) const {
737 if (this->IsHidden()) {
738 AddHideComment(writer);
739 }
740 DumpAnnotations(writer);
741}
742
Jiyong Park18132182020-06-08 20:24:40 +0900743AidlParcelable::AidlParcelable(const AidlLocation& location, const std::string& name,
744 const std::string& package, const std::string& comments,
Jeongik Chadf76dc72019-11-28 00:08:47 +0900745 const std::string& cpp_header, std::vector<std::string>* type_params)
Jiyong Park18132182020-06-08 20:24:40 +0900746 : AidlDefinedType(location, name, comments, package),
Jeongik Chadf76dc72019-11-28 00:08:47 +0900747 AidlParameterizable<std::string>(type_params),
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800748 cpp_header_(cpp_header) {
749 // Strip off quotation marks if we actually have a cpp header.
750 if (cpp_header_.length() >= 2) {
751 cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2);
752 }
Casey Dahlin59401da2015-10-09 18:16:45 -0700753}
Jeongik Chadf76dc72019-11-28 00:08:47 +0900754template <typename T>
755AidlParameterizable<T>::AidlParameterizable(const AidlParameterizable& other) {
756 // Copying is not supported if it has type parameters.
757 // It doesn't make a problem because only ArrayBase() makes a copy,
758 // and it can be called only if a type is not generic.
759 CHECK(!other.IsGeneric());
760}
761
762template <typename T>
763bool AidlParameterizable<T>::CheckValid() const {
764 return true;
765};
766
767template <>
768bool AidlParameterizable<std::string>::CheckValid() const {
769 if (!IsGeneric()) {
770 return true;
771 }
772 std::unordered_set<std::string> set(GetTypeParameters().begin(), GetTypeParameters().end());
773 if (set.size() != GetTypeParameters().size()) {
774 AIDL_ERROR(this->AsAidlNode()) << "Every type parameter should be unique.";
775 return false;
776 }
777 return true;
778}
Casey Dahlin59401da2015-10-09 18:16:45 -0700779
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700780std::set<AidlAnnotation::Type> AidlParcelable::GetSupportedAnnotations() const {
781 return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900782 AidlAnnotation::Type::JAVA_STABLE_PARCELABLE, AidlAnnotation::Type::HIDE,
783 AidlAnnotation::Type::JAVA_PASSTHROUGH};
Devin Moore24f68572020-02-26 13:20:59 -0800784}
785
786bool AidlParcelable::CheckValid(const AidlTypenames& typenames) const {
787 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100788 return false;
789 }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900790 if (!AidlParameterizable<std::string>::CheckValid()) {
791 return false;
792 }
Jeongik Cha82317dd2019-02-27 20:26:11 +0900793
794 return true;
795}
796
Jeongik Cha997281d2020-01-16 15:23:59 +0900797void AidlParcelable::Dump(CodeWriter* writer) const {
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800798 DumpHeader(writer);
Jiyong Park02da7422018-07-16 16:00:26 +0900799 writer->Write("parcelable %s ;\n", GetName().c_str());
800}
801
Steven Moreland5557f1c2018-07-02 13:50:23 -0700802AidlStructuredParcelable::AidlStructuredParcelable(
Jiyong Park18132182020-06-08 20:24:40 +0900803 const AidlLocation& location, const std::string& name, const std::string& package,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900804 const std::string& comments, std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables)
805 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/),
Steven Moreland46e9da82018-07-27 15:45:29 -0700806 variables_(std::move(*variables)) {}
Steven Moreland5557f1c2018-07-02 13:50:23 -0700807
Jeongik Cha997281d2020-01-16 15:23:59 +0900808void AidlStructuredParcelable::Dump(CodeWriter* writer) const {
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800809 DumpHeader(writer);
Jiyong Park02da7422018-07-16 16:00:26 +0900810 writer->Write("parcelable %s {\n", GetName().c_str());
811 writer->Indent();
812 for (const auto& field : GetFields()) {
Jeongik Cha997281d2020-01-16 15:23:59 +0900813 if (field->GetType().IsHidden()) {
814 AddHideComment(writer);
815 }
Jiyong Parka468e2a2018-08-29 21:25:18 +0900816 writer->Write("%s;\n", field->ToString().c_str());
Jiyong Park02da7422018-07-16 16:00:26 +0900817 }
818 writer->Dedent();
819 writer->Write("}\n");
820}
821
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700822std::set<AidlAnnotation::Type> AidlStructuredParcelable::GetSupportedAnnotations() const {
823 return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900824 AidlAnnotation::Type::HIDE, AidlAnnotation::Type::JAVA_PASSTHROUGH};
Devin Moore24f68572020-02-26 13:20:59 -0800825}
826
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900827bool AidlStructuredParcelable::CheckValid(const AidlTypenames& typenames) const {
Daniel Norman85aed542019-08-21 12:01:14 -0700828 bool success = true;
Devin Moore24f68572020-02-26 13:20:59 -0800829 if (!AidlParcelable::CheckValid(typenames)) {
830 return false;
831 }
832
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900833 for (const auto& v : GetFields()) {
Daniel Norman85aed542019-08-21 12:01:14 -0700834 success = success && v->CheckValid(typenames);
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900835 }
Daniel Norman85aed542019-08-21 12:01:14 -0700836 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900837}
838
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900839// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -0700840bool AidlTypeSpecifier::LanguageSpecificCheckValid(const AidlTypenames& typenames,
841 Options::Language lang) const {
Steven Moreland0185d9b2020-05-15 23:21:22 +0000842 if (lang == Options::Language::NDK && IsArray() && GetName() == "IBinder") {
843 AIDL_ERROR(this) << "The NDK backend does not support array of IBinder";
844 return false;
845 }
Steven Morelandd59e3172020-05-11 16:42:09 -0700846 if (lang == Options::Language::NDK && IsArray() && IsNullable()) {
Steven Moreland0185d9b2020-05-15 23:21:22 +0000847 if (GetName() == "ParcelFileDescriptor") {
848 AIDL_ERROR(this) << "The NDK backend does not support nullable array of ParcelFileDescriptor";
849 return false;
850 }
851
Steven Morelandd59e3172020-05-11 16:42:09 -0700852 const auto defined_type = typenames.TryGetDefinedType(GetName());
853 if (defined_type != nullptr && defined_type->AsParcelable() != nullptr) {
854 AIDL_ERROR(this) << "The NDK backend does not support nullable array of parcelable";
855 return false;
856 }
857 }
Steven Moreland04b07e12020-01-21 18:11:07 -0800858 if (lang != Options::Language::JAVA) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900859 if (this->GetName() == "List" && !this->IsGeneric()) {
Steven Moreland04b07e12020-01-21 18:11:07 -0800860 AIDL_ERROR(this) << "Currently, only the Java backend supports non-generic List.";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900861 return false;
862 }
863 }
Steven Morelandc8a4ca82020-01-21 17:50:08 -0800864 if (this->GetName() == "FileDescriptor" && lang == Options::Language::NDK) {
865 AIDL_ERROR(this) << "FileDescriptor isn't supported with the NDK.";
866 return false;
867 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900868 if (this->IsGeneric()) {
869 if (this->GetName() == "List") {
870 if (this->GetTypeParameters().size() != 1) {
871 AIDL_ERROR(this) << "List must have only one type parameter.";
872 return false;
873 }
874 if (lang == Options::Language::CPP) {
Devin Moore2ac52f92020-03-23 15:39:36 -0700875 const string& contained_type = this->GetTypeParameters()[0]->GetName();
876 if (!(contained_type == "String" || contained_type == "IBinder")) {
877 AIDL_ERROR(this) << "List<" << contained_type
878 << "> is not supported. List in cpp supports only String and IBinder.";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900879 return false;
880 }
Jeongik Cha08ca2182019-11-21 14:01:13 +0900881 } else if (lang == Options::Language::JAVA) {
882 const string& contained_type = this->GetTypeParameters()[0]->GetName();
883 if (AidlTypenames::IsBuiltinTypename(contained_type)) {
884 if (contained_type != "String" && contained_type != "IBinder" &&
885 contained_type != "ParcelFileDescriptor") {
Devin Moore2ac52f92020-03-23 15:39:36 -0700886 AIDL_ERROR(this) << "List<" << contained_type
887 << "> is not supported. List in Java supports only String, IBinder, "
888 "and ParcelFileDescriptor.";
Jeongik Cha08ca2182019-11-21 14:01:13 +0900889 return false;
890 }
891 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900892 }
Jeongik Chabb55b5e2020-01-07 23:11:26 +0900893 }
894 }
Jeongik Chab75a4512020-01-10 13:37:04 +0900895 if (this->GetName() == "Map" || this->GetName() == "CharSequence") {
Jeongik Chabb55b5e2020-01-07 23:11:26 +0900896 if (lang != Options::Language::JAVA) {
Jeongik Chab75a4512020-01-10 13:37:04 +0900897 AIDL_ERROR(this) << "Currently, only Java backend supports " << this->GetName() << ".";
Jeongik Chabb55b5e2020-01-07 23:11:26 +0900898 return false;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900899 }
900 }
Jeongik Cha08ca2182019-11-21 14:01:13 +0900901 if (lang == Options::Language::JAVA) {
902 const string name = this->GetName();
903 // List[], Map[], CharSequence[] are not supported.
904 if (AidlTypenames::IsBuiltinTypename(name) && this->IsArray()) {
905 if (name == "List" || name == "Map" || name == "CharSequence") {
906 AIDL_ERROR(this) << "List[], Map[], CharSequence[] are not supported.";
907 return false;
908 }
909 }
910 }
911
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900912 return true;
913}
914
915// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -0700916bool AidlParcelable::LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/,
917 Options::Language lang) const {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900918 if (lang != Options::Language::JAVA) {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800919 const AidlParcelable* unstructured_parcelable = this->AsUnstructuredParcelable();
920 if (unstructured_parcelable != nullptr) {
921 if (unstructured_parcelable->GetCppHeader().empty()) {
922 AIDL_ERROR(unstructured_parcelable)
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900923 << "Unstructured parcelable must have C++ header defined.";
924 return false;
925 }
926 }
927 }
928 return true;
929}
930
931// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -0700932bool AidlStructuredParcelable::LanguageSpecificCheckValid(const AidlTypenames& typenames,
933 Options::Language lang) const {
934 if (!AidlParcelable::LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900935 return false;
936 }
937 for (const auto& v : this->GetFields()) {
Steven Morelandd59e3172020-05-11 16:42:09 -0700938 if (!v->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900939 return false;
940 }
941 }
942 return true;
943}
944
Daniel Norman85aed542019-08-21 12:01:14 -0700945AidlEnumerator::AidlEnumerator(const AidlLocation& location, const std::string& name,
Daniel Norman2e4112d2019-10-03 10:22:35 -0700946 AidlConstantValue* value, const std::string& comments)
947 : AidlNode(location), name_(name), value_(value), comments_(comments) {}
Daniel Norman85aed542019-08-21 12:01:14 -0700948
949bool AidlEnumerator::CheckValid(const AidlTypeSpecifier& enum_backing_type) const {
950 if (GetValue() == nullptr) {
951 return false;
952 }
953 if (!GetValue()->CheckValid()) {
954 return false;
955 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700956 if (GetValue()->ValueString(enum_backing_type, AidlConstantValueDecorator).empty()) {
Daniel Norman85aed542019-08-21 12:01:14 -0700957 AIDL_ERROR(this) << "Enumerator type differs from enum backing type.";
958 return false;
959 }
960 return true;
961}
962
963string AidlEnumerator::ValueString(const AidlTypeSpecifier& backing_type,
964 const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700965 return GetValue()->ValueString(backing_type, decorator);
Daniel Norman85aed542019-08-21 12:01:14 -0700966}
967
968AidlEnumDeclaration::AidlEnumDeclaration(const AidlLocation& location, const std::string& name,
969 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jiyong Park18132182020-06-08 20:24:40 +0900970 const std::string& package, const std::string& comments)
Daniel Norman2e4112d2019-10-03 10:22:35 -0700971 : AidlDefinedType(location, name, comments, package), enumerators_(std::move(*enumerators)) {}
Daniel Norman85aed542019-08-21 12:01:14 -0700972
973void AidlEnumDeclaration::SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type) {
974 backing_type_ = std::move(type);
975}
976
Steven Moreland59e53e42019-11-26 20:38:08 -0800977bool AidlEnumDeclaration::Autofill() {
Daniel Normanb28684e2019-10-17 15:31:39 -0700978 const AidlEnumerator* previous = nullptr;
979 for (const auto& enumerator : enumerators_) {
980 if (enumerator->GetValue() == nullptr) {
981 if (previous == nullptr) {
Devin Mooredf93ebb2020-03-25 14:03:35 -0700982 enumerator->SetValue(
983 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(GetLocation(), "0")));
Daniel Normanb28684e2019-10-17 15:31:39 -0700984 } else {
Steven Moreland59e53e42019-11-26 20:38:08 -0800985 auto prev_value = std::unique_ptr<AidlConstantValue>(
986 AidlConstantValue::ShallowIntegralCopy(*previous->GetValue()));
987 if (prev_value == nullptr) {
988 return false;
989 }
Daniel Normanb28684e2019-10-17 15:31:39 -0700990 enumerator->SetValue(std::make_unique<AidlBinaryConstExpression>(
Devin Mooredf93ebb2020-03-25 14:03:35 -0700991 GetLocation(), std::move(prev_value), "+",
992 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(GetLocation(), "1"))));
Daniel Normanb28684e2019-10-17 15:31:39 -0700993 }
994 }
995 previous = enumerator.get();
996 }
Steven Moreland59e53e42019-11-26 20:38:08 -0800997 return true;
Daniel Normanb28684e2019-10-17 15:31:39 -0700998}
999
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001000std::set<AidlAnnotation::Type> AidlEnumDeclaration::GetSupportedAnnotations() const {
1001 return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::BACKING,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +09001002 AidlAnnotation::Type::HIDE, AidlAnnotation::Type::JAVA_PASSTHROUGH};
Devin Moore24f68572020-02-26 13:20:59 -08001003}
1004
1005bool AidlEnumDeclaration::CheckValid(const AidlTypenames& typenames) const {
1006 if (!AidlDefinedType::CheckValid(typenames)) {
1007 return false;
1008 }
Daniel Norman85aed542019-08-21 12:01:14 -07001009 if (backing_type_ == nullptr) {
1010 AIDL_ERROR(this) << "Enum declaration missing backing type.";
1011 return false;
1012 }
1013 bool success = true;
1014 for (const auto& enumerator : enumerators_) {
1015 success = success && enumerator->CheckValid(GetBackingType());
1016 }
1017 return success;
1018}
1019
Jeongik Cha997281d2020-01-16 15:23:59 +09001020void AidlEnumDeclaration::Dump(CodeWriter* writer) const {
Steven Morelanda5d9c5c2020-02-21 16:01:09 -08001021 DumpHeader(writer);
Daniel Norman37d43dd2019-09-09 17:22:34 -07001022 writer->Write("enum %s {\n", GetName().c_str());
Daniel Norman85aed542019-08-21 12:01:14 -07001023 writer->Indent();
1024 for (const auto& enumerator : GetEnumerators()) {
Daniel Norman85aed542019-08-21 12:01:14 -07001025 writer->Write("%s = %s,\n", enumerator->GetName().c_str(),
1026 enumerator->ValueString(GetBackingType(), AidlConstantValueDecorator).c_str());
1027 }
1028 writer->Dedent();
1029 writer->Write("}\n");
1030}
1031
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001032// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -07001033bool AidlInterface::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1034 Options::Language lang) const {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001035 for (const auto& m : this->GetMethods()) {
Steven Morelandd59e3172020-05-11 16:42:09 -07001036 if (!m->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001037 return false;
1038 }
1039 for (const auto& arg : m->GetArguments()) {
Steven Morelandd59e3172020-05-11 16:42:09 -07001040 if (!arg->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001041 return false;
1042 }
1043 }
1044 }
1045 return true;
1046}
1047
Steven Moreland46e9da82018-07-27 15:45:29 -07001048AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name,
Casey Dahlinfb7da2e2015-10-08 17:26:09 -07001049 const std::string& comments, bool oneway,
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001050 std::vector<std::unique_ptr<AidlMember>>* members,
Jiyong Park18132182020-06-08 20:24:40 +09001051 const std::string& package)
Steven Morelandacd53472018-12-14 10:17:26 -08001052 : AidlDefinedType(location, name, comments, package) {
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001053 for (auto& member : *members) {
1054 AidlMember* local = member.release();
1055 AidlMethod* method = local->AsMethod();
Steven Moreland693640b2018-07-19 13:46:27 -07001056 AidlConstantDeclaration* constant = local->AsConstantDeclaration();
1057
1058 CHECK(method == nullptr || constant == nullptr);
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001059
1060 if (method) {
Steven Moreland8c70ba92018-12-17 10:20:31 -08001061 method->ApplyInterfaceOneway(oneway);
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001062 methods_.emplace_back(method);
Steven Moreland693640b2018-07-19 13:46:27 -07001063 } else if (constant) {
1064 constants_.emplace_back(constant);
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001065 } else {
Steven Moreland46e9da82018-07-27 15:45:29 -07001066 AIDL_FATAL(this) << "Member is neither method nor constant!";
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001067 }
1068 }
1069
1070 delete members;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -07001071}
1072
Jeongik Cha997281d2020-01-16 15:23:59 +09001073void AidlInterface::Dump(CodeWriter* writer) const {
Steven Morelanda5d9c5c2020-02-21 16:01:09 -08001074 DumpHeader(writer);
Jiyong Park02da7422018-07-16 16:00:26 +09001075 writer->Write("interface %s {\n", GetName().c_str());
1076 writer->Indent();
1077 for (const auto& method : GetMethods()) {
Jeongik Cha997281d2020-01-16 15:23:59 +09001078 if (method->IsHidden()) {
1079 AddHideComment(writer);
1080 }
Jiyong Park309668e2018-07-28 16:55:44 +09001081 writer->Write("%s;\n", method->ToString().c_str());
Jiyong Park02da7422018-07-16 16:00:26 +09001082 }
Jiyong Parka428d212018-08-29 22:26:30 +09001083 for (const auto& constdecl : GetConstantDeclarations()) {
Jeongik Cha997281d2020-01-16 15:23:59 +09001084 if (constdecl->GetType().IsHidden()) {
1085 AddHideComment(writer);
1086 }
Jiyong Parka428d212018-08-29 22:26:30 +09001087 writer->Write("%s;\n", constdecl->ToString().c_str());
1088 }
Jiyong Park02da7422018-07-16 16:00:26 +09001089 writer->Dedent();
1090 writer->Write("}\n");
1091}
1092
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001093std::set<AidlAnnotation::Type> AidlInterface::GetSupportedAnnotations() const {
1094 return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +09001095 AidlAnnotation::Type::HIDE, AidlAnnotation::Type::JAVA_PASSTHROUGH};
Devin Moore24f68572020-02-26 13:20:59 -08001096}
1097
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001098bool AidlInterface::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -08001099 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001100 return false;
1101 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001102 // Has to be a pointer due to deleting copy constructor. No idea why.
1103 map<string, const AidlMethod*> method_names;
1104 for (const auto& m : GetMethods()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001105 if (!m->GetType().CheckValid(typenames)) {
1106 return false;
1107 }
1108
Jeongik Cha649e8a72020-03-27 17:47:40 +09001109 // TODO(b/156872582): Support it when ParcelableHolder supports every backend.
1110 if (m->GetType().GetName() == "ParcelableHolder") {
1111 AIDL_ERROR(m) << "ParcelableHolder cannot be a return type";
1112 return false;
1113 }
Steven Morelandacd53472018-12-14 10:17:26 -08001114 if (m->IsOneway() && m->GetType().GetName() != "void") {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001115 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot return a value";
1116 return false;
1117 }
1118
1119 set<string> argument_names;
1120 for (const auto& arg : m->GetArguments()) {
1121 auto it = argument_names.find(arg->GetName());
1122 if (it != argument_names.end()) {
1123 AIDL_ERROR(m) << "method '" << m->GetName() << "' has duplicate argument name '"
1124 << arg->GetName() << "'";
1125 return false;
1126 }
1127 argument_names.insert(arg->GetName());
1128
1129 if (!arg->GetType().CheckValid(typenames)) {
1130 return false;
1131 }
1132
Jeongik Cha649e8a72020-03-27 17:47:40 +09001133 // TODO(b/156872582): Support it when ParcelableHolder supports every backend.
1134 if (arg->GetType().GetName() == "ParcelableHolder") {
1135 AIDL_ERROR(arg) << "ParcelableHolder cannot be an argument type";
1136 return false;
1137 }
Steven Morelandacd53472018-12-14 10:17:26 -08001138 if (m->IsOneway() && arg->IsOut()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001139 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot have out parameters";
1140 return false;
1141 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001142 const bool can_be_out = typenames.CanBeOutParameter(arg->GetType());
1143 if (!arg->DirectionWasSpecified() && can_be_out) {
1144 AIDL_ERROR(arg) << "'" << arg->GetType().ToString()
1145 << "' can be an out type, so you must declare it as in, out, or inout.";
1146 return false;
1147 }
1148
1149 if (arg->GetDirection() != AidlArgument::IN_DIR && !can_be_out) {
1150 AIDL_ERROR(arg) << "'" << arg->ToString() << "' can only be an in parameter.";
1151 return false;
1152 }
1153
1154 // check that the name doesn't match a keyword
Jeongik Cha997281d2020-01-16 15:23:59 +09001155 if (IsJavaKeyword(arg->GetName().c_str())) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001156 AIDL_ERROR(arg) << "Argument name is a Java or aidl keyword";
1157 return false;
1158 }
1159
1160 // Reserve a namespace for internal use
1161 if (android::base::StartsWith(arg->GetName(), "_aidl")) {
1162 AIDL_ERROR(arg) << "Argument name cannot begin with '_aidl'";
1163 return false;
1164 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001165 }
1166
1167 auto it = method_names.find(m->GetName());
1168 // prevent duplicate methods
1169 if (it == method_names.end()) {
1170 method_names[m->GetName()] = m.get();
1171 } else {
1172 AIDL_ERROR(m) << "attempt to redefine method " << m->GetName() << ":";
1173 AIDL_ERROR(it->second) << "previously defined here.";
1174 return false;
1175 }
1176
Paul Trautrimb77048c2020-01-21 16:39:32 +09001177 static set<string> reserved_methods{"asBinder()", "getInterfaceHash()", "getInterfaceVersion()",
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001178 "getTransactionName(int)"};
1179
1180 if (reserved_methods.find(m->Signature()) != reserved_methods.end()) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001181 AIDL_ERROR(m) << " method " << m->Signature() << " is reserved for internal use.";
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001182 return false;
1183 }
1184 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001185
1186 bool success = true;
1187 set<string> constant_names;
1188 for (const std::unique_ptr<AidlConstantDeclaration>& constant : GetConstantDeclarations()) {
1189 if (constant_names.count(constant->GetName()) > 0) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001190 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001191 success = false;
1192 }
1193 constant_names.insert(constant->GetName());
1194 success = success && constant->CheckValid(typenames);
1195 }
1196
1197 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001198}
1199
Steven Moreland46e9da82018-07-27 15:45:29 -07001200AidlImport::AidlImport(const AidlLocation& location, const std::string& needed_class)
1201 : AidlNode(location), needed_class_(needed_class) {}