blob: c2c36110af0a05d8a7d375adcef5b33c2aa22246 [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", {}},
139 {AidlAnnotation::Type::BACKING, "Backing", {{"type", "String"}}}};
140 return kSchemas;
141}
Jiyong Park68bc77a2018-07-19 19:00:45 +0900142
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700143std::string AidlAnnotation::TypeToString(Type type) {
144 for (const Schema& schema : AllSchemas()) {
145 if (type == schema.type) return schema.name;
146 }
147 AIDL_FATAL(AIDL_LOCATION_HERE) << "Unrecognized type: " << static_cast<size_t>(type);
148 __builtin_unreachable();
149}
Andrei Onea9445fc62019-06-27 18:11:59 +0100150
151AidlAnnotation* AidlAnnotation::Parse(
152 const AidlLocation& location, const string& name,
153 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700154 const Schema* schema = nullptr;
155 for (const Schema& a_schema : AllSchemas()) {
156 if (a_schema.name == name) {
157 schema = &a_schema;
158 }
159 }
160
161 if (schema == nullptr) {
Jiyong Park68bc77a2018-07-19 19:00:45 +0900162 std::ostringstream stream;
Steven Moreland46e9da82018-07-27 15:45:29 -0700163 stream << "'" << name << "' is not a recognized annotation. ";
Jiyong Park68bc77a2018-07-19 19:00:45 +0900164 stream << "It must be one of:";
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700165 for (const Schema& s : AllSchemas()) {
166 stream << " " << s.name;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900167 }
168 stream << ".";
Steven Moreland46e9da82018-07-27 15:45:29 -0700169 AIDL_ERROR(location) << stream.str();
170 return nullptr;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900171 }
Andrei Onea9445fc62019-06-27 18:11:59 +0100172 if (parameter_list == nullptr) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700173 return new AidlAnnotation(location, *schema, {});
Andrei Onea9445fc62019-06-27 18:11:59 +0100174 }
175
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700176 return new AidlAnnotation(location, *schema, std::move(*parameter_list));
Jiyong Park68bc77a2018-07-19 19:00:45 +0900177}
178
Andrei Onea9445fc62019-06-27 18:11:59 +0100179AidlAnnotation::AidlAnnotation(
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700180 const AidlLocation& location, const Schema& schema,
Andrei Onea9445fc62019-06-27 18:11:59 +0100181 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters)
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700182 : AidlNode(location), schema_(schema), parameters_(std::move(parameters)) {}
Andrei Onea9445fc62019-06-27 18:11:59 +0100183
184bool AidlAnnotation::CheckValid() const {
Andrei Onea9445fc62019-06-27 18:11:59 +0100185 for (const auto& name_and_param : parameters_) {
186 const std::string& param_name = name_and_param.first;
187 const std::shared_ptr<AidlConstantValue>& param = name_and_param.second;
Will McVickerd7d18df2019-09-12 13:40:50 -0700188 if (!param->CheckValid()) {
189 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
190 << GetName() << ".";
191 return false;
192 }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700193 auto parameter_mapping_it = schema_.supported_parameters.find(param_name);
194 if (parameter_mapping_it == schema_.supported_parameters.end()) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100195 std::ostringstream stream;
196 stream << "Parameter " << param_name << " not supported ";
Devin Mooredecaf292020-04-30 09:16:40 -0700197 stream << "for annotation " << GetName() << ". ";
Andrei Onea9445fc62019-06-27 18:11:59 +0100198 stream << "It must be one of:";
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700199 for (const auto& kv : schema_.supported_parameters) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100200 stream << " " << kv.first;
201 }
202 AIDL_ERROR(this) << stream.str();
203 return false;
204 }
205 AidlTypeSpecifier type{AIDL_LOCATION_HERE, parameter_mapping_it->second, false, nullptr, ""};
Will McVickerd7d18df2019-09-12 13:40:50 -0700206 const std::string param_value = param->ValueString(type, AidlConstantValueDecorator);
Andrei Onea9445fc62019-06-27 18:11:59 +0100207 // Assume error on empty string.
208 if (param_value == "") {
209 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
210 << GetName() << ".";
211 return false;
212 }
213 }
214 return true;
215}
216
217std::map<std::string, std::string> AidlAnnotation::AnnotationParams(
218 const ConstantValueDecorator& decorator) const {
219 std::map<std::string, std::string> raw_params;
Andrei Onea9445fc62019-06-27 18:11:59 +0100220 for (const auto& name_and_param : parameters_) {
221 const std::string& param_name = name_and_param.first;
222 const std::shared_ptr<AidlConstantValue>& param = name_and_param.second;
Devin Mooredecaf292020-04-30 09:16:40 -0700223 if (schema_.supported_parameters.find(param_name) == schema_.supported_parameters.end()) {
224 std::ostringstream stream;
225 stream << "Parameter " << param_name << " not supported ";
226 stream << "for annotation " << GetName() << ". ";
227 stream << "It must be one of:";
228 for (const auto& kv : schema_.supported_parameters) {
229 stream << " " << kv.first;
230 }
231 AIDL_ERROR(this) << stream.str();
232 continue;
233 }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700234 AidlTypeSpecifier type{AIDL_LOCATION_HERE, schema_.supported_parameters.at(param_name), false,
235 nullptr, ""};
Will McVickerd7d18df2019-09-12 13:40:50 -0700236 if (!param->CheckValid()) {
237 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
238 << GetName() << ".";
Devin Mooredecaf292020-04-30 09:16:40 -0700239 continue;
Will McVickerd7d18df2019-09-12 13:40:50 -0700240 }
241
242 raw_params.emplace(param_name, param->ValueString(type, decorator));
Andrei Onea9445fc62019-06-27 18:11:59 +0100243 }
244 return raw_params;
245}
Steven Moreland46e9da82018-07-27 15:45:29 -0700246
Daniel Norman37d43dd2019-09-09 17:22:34 -0700247std::string AidlAnnotation::ToString(const ConstantValueDecorator& decorator) const {
248 if (parameters_.empty()) {
249 return "@" + GetName();
250 } else {
251 vector<string> param_strings;
252 for (const auto& [name, value] : AnnotationParams(decorator)) {
253 param_strings.emplace_back(name + "=" + value);
254 }
255 return "@" + GetName() + "(" + Join(param_strings, ", ") + ")";
256 }
257}
258
Andrei Onea9445fc62019-06-27 18:11:59 +0100259static const AidlAnnotation* GetAnnotation(const vector<AidlAnnotation>& annotations,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700260 AidlAnnotation::Type type) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100261 for (const auto& a : annotations) {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700262 if (a.GetType() == type) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100263 return &a;
264 }
265 }
266 return nullptr;
267}
268
Steven Moreland46e9da82018-07-27 15:45:29 -0700269AidlAnnotatable::AidlAnnotatable(const AidlLocation& location) : AidlNode(location) {}
270
Jiyong Park68bc77a2018-07-19 19:00:45 +0900271bool AidlAnnotatable::IsNullable() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700272 return GetAnnotation(annotations_, AidlAnnotation::Type::NULLABLE);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900273}
274
Jiyong Park68bc77a2018-07-19 19:00:45 +0900275bool AidlAnnotatable::IsUtf8InCpp() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700276 return GetAnnotation(annotations_, AidlAnnotation::Type::UTF8_IN_CPP);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900277}
278
Steven Morelanda57d0a62019-07-30 09:41:14 -0700279bool AidlAnnotatable::IsVintfStability() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700280 return GetAnnotation(annotations_, AidlAnnotation::Type::VINTF_STABILITY);
Steven Morelanda57d0a62019-07-30 09:41:14 -0700281}
282
Andrei Onea9445fc62019-06-27 18:11:59 +0100283const AidlAnnotation* AidlAnnotatable::UnsupportedAppUsage() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700284 return GetAnnotation(annotations_, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE);
Jiyong Parka6605ab2018-11-11 14:30:21 +0900285}
286
Daniel Norman716d3112019-09-10 13:11:56 -0700287const AidlTypeSpecifier* AidlAnnotatable::BackingType(const AidlTypenames& typenames) const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700288 auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::BACKING);
Daniel Norman85aed542019-08-21 12:01:14 -0700289 if (annotation != nullptr) {
290 auto annotation_params = annotation->AnnotationParams(AidlConstantValueDecorator);
291 if (auto it = annotation_params.find("type"); it != annotation_params.end()) {
292 const string& type = it->second;
Steven Morelande7d5d082020-05-21 20:29:02 +0000293
294 AIDL_FATAL_IF(type.size() < 2, this) << type;
295 AIDL_FATAL_IF(type[0] != '"', this) << type;
296 AIDL_FATAL_IF(type[type.length() - 1] != '"', this) << type;
297 string unquoted_type = type.substr(1, type.length() - 2);
298
Daniel Norman716d3112019-09-10 13:11:56 -0700299 AidlTypeSpecifier* type_specifier =
Steven Morelande7d5d082020-05-21 20:29:02 +0000300 new AidlTypeSpecifier(AIDL_LOCATION_HERE, unquoted_type, false, nullptr, "");
Daniel Norman716d3112019-09-10 13:11:56 -0700301 type_specifier->Resolve(typenames);
302 return type_specifier;
Daniel Norman85aed542019-08-21 12:01:14 -0700303 }
304 }
305 return nullptr;
306}
307
Jeongik Cha88f95a82020-01-15 13:02:16 +0900308bool AidlAnnotatable::IsStableApiParcelable(Options::Language lang) const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700309 return lang == Options::Language::JAVA &&
310 GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_STABLE_PARCELABLE);
Jeongik Cha82317dd2019-02-27 20:26:11 +0900311}
312
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800313bool AidlAnnotatable::IsHide() const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700314 return GetAnnotation(annotations_, AidlAnnotation::Type::HIDE);
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800315}
316
Steven Moreland7e4b9502020-02-20 18:10:42 -0800317void AidlAnnotatable::DumpAnnotations(CodeWriter* writer) const {
318 if (annotations_.empty()) return;
319
320 writer->Write("%s\n", AidlAnnotatable::ToString().c_str());
321}
322
Devin Moore24f68572020-02-26 13:20:59 -0800323bool AidlAnnotatable::CheckValid(const AidlTypenames&) const {
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700324 std::set<AidlAnnotation::Type> supported_annotations = GetSupportedAnnotations();
Andrei Onea9445fc62019-06-27 18:11:59 +0100325 for (const auto& annotation : GetAnnotations()) {
326 if (!annotation.CheckValid()) {
327 return false;
328 }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700329
330 std::vector<std::string> supported_annot_strings;
331 for (AidlAnnotation::Type type : supported_annotations) {
332 supported_annot_strings.push_back(AidlAnnotation::TypeToString(type));
333 }
334
335 if (supported_annotations.find(annotation.GetType()) == supported_annotations.end()) {
Devin Moore24f68572020-02-26 13:20:59 -0800336 AIDL_ERROR(this) << "'" << annotation.GetName()
337 << "' is not a supported annotation for this node. "
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700338 << "It must be one of: "
339 << android::base::Join(supported_annot_strings, ", ");
Devin Moore24f68572020-02-26 13:20:59 -0800340 return false;
341 }
Andrei Onea9445fc62019-06-27 18:11:59 +0100342 }
Steven Morelanda57d0a62019-07-30 09:41:14 -0700343
Andrei Onea9445fc62019-06-27 18:11:59 +0100344 return true;
345}
346
Jiyong Park68bc77a2018-07-19 19:00:45 +0900347string AidlAnnotatable::ToString() const {
348 vector<string> ret;
349 for (const auto& a : annotations_) {
Daniel Norman37d43dd2019-09-09 17:22:34 -0700350 ret.emplace_back(a.ToString(AidlConstantValueDecorator));
Jiyong Park68bc77a2018-07-19 19:00:45 +0900351 }
352 std::sort(ret.begin(), ret.end());
353 return Join(ret, " ");
354}
355
Steven Moreland46e9da82018-07-27 15:45:29 -0700356AidlTypeSpecifier::AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name,
357 bool is_array,
Jiyong Park1deecc32018-07-17 01:14:41 +0900358 vector<unique_ptr<AidlTypeSpecifier>>* type_params,
Steven Moreland46e9da82018-07-27 15:45:29 -0700359 const string& comments)
360 : AidlAnnotatable(location),
Jeongik Chadf76dc72019-11-28 00:08:47 +0900361 AidlParameterizable<unique_ptr<AidlTypeSpecifier>>(type_params),
Steven Moreland46e9da82018-07-27 15:45:29 -0700362 unresolved_name_(unresolved_name),
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700363 is_array_(is_array),
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900364 comments_(comments),
365 split_name_(Split(unresolved_name, ".")) {}
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700366
Steven Moreland3f658cf2018-08-20 13:40:54 -0700367AidlTypeSpecifier AidlTypeSpecifier::ArrayBase() const {
368 AIDL_FATAL_IF(!is_array_, this);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900369 // Declaring array of generic type cannot happen, it is grammar error.
370 AIDL_FATAL_IF(IsGeneric(), this);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700371
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800372 AidlTypeSpecifier array_base = *this;
373 array_base.is_array_ = false;
374 return array_base;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700375}
376
Jeongik Cha997281d2020-01-16 15:23:59 +0900377bool AidlTypeSpecifier::IsHidden() const {
378 return HasHideComment(GetComments());
379}
380
Jiyong Park1deecc32018-07-17 01:14:41 +0900381string AidlTypeSpecifier::ToString() const {
382 string ret = GetName();
383 if (IsGeneric()) {
384 vector<string> arg_names;
385 for (const auto& ta : GetTypeParameters()) {
Jiyong Parkccf00f82018-07-17 01:39:23 +0900386 arg_names.emplace_back(ta->ToString());
387 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900388 ret += "<" + Join(arg_names, ",") + ">";
Jiyong Parkccf00f82018-07-17 01:39:23 +0900389 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900390 if (IsArray()) {
391 ret += "[]";
392 }
393 return ret;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900394}
395
Jiyong Park02da7422018-07-16 16:00:26 +0900396string AidlTypeSpecifier::Signature() const {
397 string ret = ToString();
398 string annotations = AidlAnnotatable::ToString();
399 if (annotations != "") {
400 ret = annotations + " " + ret;
401 }
402 return ret;
403}
404
Daniel Norman716d3112019-09-10 13:11:56 -0700405bool AidlTypeSpecifier::Resolve(const AidlTypenames& typenames) {
Steven Moreland9731c632019-08-13 10:21:08 -0700406 CHECK(!IsResolved());
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700407 AidlTypenames::ResolvedTypename result = typenames.ResolveTypename(unresolved_name_);
408 if (result.is_resolved) {
409 fully_qualified_name_ = result.canonical_name;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900410 split_name_ = Split(fully_qualified_name_, ".");
Jiyong Parkccf00f82018-07-17 01:39:23 +0900411 }
Steven Morelandcb1bcd72020-04-29 16:30:35 -0700412 return result.is_resolved;
Casey Dahlin70078e62015-09-30 17:01:30 -0700413}
414
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700415std::set<AidlAnnotation::Type> AidlTypeSpecifier::GetSupportedAnnotations() const {
Devin Moore24f68572020-02-26 13:20:59 -0800416 // kHide and kUnsupportedAppUsage are both method return annotations
417 // which we don't distinguish from other type specifiers.
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700418 return {AidlAnnotation::Type::NULLABLE, AidlAnnotation::Type::UTF8_IN_CPP,
419 AidlAnnotation::Type::UNSUPPORTED_APP_USAGE, AidlAnnotation::Type::HIDE};
Devin Moore24f68572020-02-26 13:20:59 -0800420}
421
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900422bool AidlTypeSpecifier::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -0800423 if (!AidlAnnotatable::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100424 return false;
425 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900426 if (IsGeneric()) {
427 const string& type_name = GetName();
Jeongik Chae74c86d2019-12-12 16:54:03 +0900428
429 auto& types = GetTypeParameters();
430 // TODO(b/136048684) Disallow to use primitive types only if it is List or Map.
431 if (type_name == "List" || type_name == "Map") {
432 if (std::any_of(types.begin(), types.end(), [](auto& type_ptr) {
433 return AidlTypenames::IsPrimitiveTypename(type_ptr->GetName());
434 })) {
Devin Moore7b8d5c92020-03-17 14:14:08 -0700435 AIDL_ERROR(this) << "A generic type cannot have any primitive type parameters.";
Jeongik Chae74c86d2019-12-12 16:54:03 +0900436 return false;
437 }
438 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800439 const auto defined_type = typenames.TryGetDefinedType(type_name);
Jeongik Chadf76dc72019-11-28 00:08:47 +0900440 const auto parameterizable =
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800441 defined_type != nullptr ? defined_type->AsParameterizable() : nullptr;
442 const bool is_user_defined_generic_type =
Jeongik Chadf76dc72019-11-28 00:08:47 +0900443 parameterizable != nullptr && parameterizable->IsGeneric();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800444 const size_t num_params = GetTypeParameters().size();
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900445 if (type_name == "List") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800446 if (num_params > 1) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900447 AIDL_ERROR(this) << " List cannot have type parameters more than one, but got "
448 << "'" << ToString() << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900449 return false;
450 }
451 } else if (type_name == "Map") {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800452 if (num_params != 0 && num_params != 2) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900453 AIDL_ERROR(this) << "Map must have 0 or 2 type parameters, but got "
454 << "'" << ToString() << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900455 return false;
456 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800457 if (num_params == 2) {
Jeongik Chae48d9942020-01-02 17:39:00 +0900458 const string& key_type = GetTypeParameters()[0]->GetName();
459 if (key_type != "String") {
460 AIDL_ERROR(this) << "The type of key in map must be String, but it is "
461 << "'" << key_type << "'";
462 return false;
463 }
464 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800465 } else if (is_user_defined_generic_type) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900466 const size_t allowed = parameterizable->GetTypeParameters().size();
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800467 if (num_params != allowed) {
Jeongik Chadf76dc72019-11-28 00:08:47 +0900468 AIDL_ERROR(this) << type_name << " must have " << allowed << " type parameters, but got "
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800469 << num_params;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900470 return false;
471 }
472 } else {
473 AIDL_ERROR(this) << type_name << " is not a generic type.";
474 return false;
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900475 }
476 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900477
Steven Moreland11cb9452020-01-21 16:56:58 -0800478 const bool is_generic_string_list = GetName() == "List" && IsGeneric() &&
479 GetTypeParameters().size() == 1 &&
480 GetTypeParameters()[0]->GetName() == "String";
481 if (IsUtf8InCpp() && (GetName() != "String" && !is_generic_string_list)) {
482 AIDL_ERROR(this) << "@utf8InCpp can only be used on String, String[], and List<String>.";
483 return false;
484 }
485
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900486 if (GetName() == "void") {
487 if (IsArray() || IsNullable() || IsUtf8InCpp()) {
488 AIDL_ERROR(this) << "void type cannot be an array or nullable or utf8 string";
489 return false;
490 }
491 }
492
493 if (IsArray()) {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800494 const auto defined_type = typenames.TryGetDefinedType(GetName());
495 if (defined_type != nullptr && defined_type->AsInterface() != nullptr) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900496 AIDL_ERROR(this) << "Binder type cannot be an array";
497 return false;
498 }
499 }
500
501 if (IsNullable()) {
502 if (AidlTypenames::IsPrimitiveTypename(GetName()) && !IsArray()) {
503 AIDL_ERROR(this) << "Primitive type cannot get nullable annotation";
504 return false;
505 }
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800506 const auto defined_type = typenames.TryGetDefinedType(GetName());
507 if (defined_type != nullptr && defined_type->AsEnumDeclaration() != nullptr && !IsArray()) {
Daniel Normanee8674f2019-09-20 16:07:00 -0700508 AIDL_ERROR(this) << "Enum type cannot get nullable annotation";
509 return false;
510 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900511 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900512 return true;
513}
514
Steven Moreland860b1942018-08-16 14:59:28 -0700515std::string AidlConstantValueDecorator(const AidlTypeSpecifier& /*type*/,
516 const std::string& raw_value) {
517 return raw_value;
518}
519
Steven Moreland46e9da82018-07-27 15:45:29 -0700520AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
521 AidlTypeSpecifier* type, const std::string& name)
522 : AidlVariableDeclaration(location, type, name, nullptr /*default_value*/) {}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700523
Steven Moreland46e9da82018-07-27 15:45:29 -0700524AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
525 AidlTypeSpecifier* type, const std::string& name,
526 AidlConstantValue* default_value)
527 : AidlNode(location), type_(type), name_(name), default_value_(default_value) {}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700528
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900529bool AidlVariableDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700530 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900531 valid &= type_->CheckValid(typenames);
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900532
Steven Moreland54be7bd2019-12-05 11:17:53 -0800533 if (type_->GetName() == "void") {
534 AIDL_ERROR(this) << "Declaration " << name_
535 << " is void, but declarations cannot be of void type.";
536 valid = false;
537 }
538
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900539 if (default_value_ == nullptr) return valid;
Steven Moreland25294322018-08-07 18:13:55 -0700540 valid &= default_value_->CheckValid();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700541
Steven Moreland25294322018-08-07 18:13:55 -0700542 if (!valid) return false;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700543
Steven Moreland860b1942018-08-16 14:59:28 -0700544 return !ValueString(AidlConstantValueDecorator).empty();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700545}
Steven Moreland5557f1c2018-07-02 13:50:23 -0700546
547string AidlVariableDeclaration::ToString() const {
Jeongik Cha3271ffa2018-12-04 15:19:20 +0900548 string ret = type_->Signature() + " " + name_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700549 if (default_value_ != nullptr) {
Steven Moreland860b1942018-08-16 14:59:28 -0700550 ret += " = " + ValueString(AidlConstantValueDecorator);
Steven Moreland9ea10e32018-07-19 15:26:09 -0700551 }
552 return ret;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700553}
554
Jiyong Park02da7422018-07-16 16:00:26 +0900555string AidlVariableDeclaration::Signature() const {
556 return type_->Signature() + " " + name_;
557}
558
Steven Moreland860b1942018-08-16 14:59:28 -0700559std::string AidlVariableDeclaration::ValueString(const ConstantValueDecorator& decorator) const {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900560 if (default_value_ != nullptr) {
Will McVickerd7d18df2019-09-12 13:40:50 -0700561 return default_value_->ValueString(GetType(), decorator);
Jiyong Parka468e2a2018-08-29 21:25:18 +0900562 } else {
563 return "";
564 }
Steven Moreland25294322018-08-07 18:13:55 -0700565}
566
Steven Moreland46e9da82018-07-27 15:45:29 -0700567AidlArgument::AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
568 AidlTypeSpecifier* type, const std::string& name)
569 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700570 direction_(direction),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700571 direction_specified_(true) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700572
Steven Moreland46e9da82018-07-27 15:45:29 -0700573AidlArgument::AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type,
574 const std::string& name)
575 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700576 direction_(AidlArgument::IN_DIR),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700577 direction_specified_(false) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700578
Jiyong Park02da7422018-07-16 16:00:26 +0900579string AidlArgument::GetDirectionSpecifier() const {
Casey Dahlinc378c992015-09-29 16:50:40 -0700580 string ret;
Casey Dahlinc378c992015-09-29 16:50:40 -0700581 if (direction_specified_) {
582 switch(direction_) {
583 case AidlArgument::IN_DIR:
Devin Mooreeccdb902020-03-24 16:22:40 -0700584 ret += "in";
Casey Dahlinc378c992015-09-29 16:50:40 -0700585 break;
586 case AidlArgument::OUT_DIR:
Devin Mooreeccdb902020-03-24 16:22:40 -0700587 ret += "out";
Casey Dahlinc378c992015-09-29 16:50:40 -0700588 break;
589 case AidlArgument::INOUT_DIR:
Devin Mooreeccdb902020-03-24 16:22:40 -0700590 ret += "inout";
Casey Dahlinc378c992015-09-29 16:50:40 -0700591 break;
592 }
593 }
Casey Dahlinc378c992015-09-29 16:50:40 -0700594 return ret;
595}
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700596
Jiyong Park02da7422018-07-16 16:00:26 +0900597string AidlArgument::ToString() const {
Devin Mooreeccdb902020-03-24 16:22:40 -0700598 if (direction_specified_) {
599 return GetDirectionSpecifier() + " " + AidlVariableDeclaration::ToString();
600 } else {
601 return AidlVariableDeclaration::ToString();
602 }
Jiyong Park02da7422018-07-16 16:00:26 +0900603}
604
605std::string AidlArgument::Signature() const {
Steven Moreland46e9da82018-07-27 15:45:29 -0700606 class AidlInterface;
607 class AidlInterface;
608 class AidlParcelable;
609 class AidlStructuredParcelable;
610 class AidlParcelable;
611 class AidlStructuredParcelable;
Devin Mooreeccdb902020-03-24 16:22:40 -0700612 if (direction_specified_) {
613 return GetDirectionSpecifier() + " " + AidlVariableDeclaration::Signature();
614 } else {
615 return AidlVariableDeclaration::Signature();
616 }
Jiyong Park02da7422018-07-16 16:00:26 +0900617}
618
Steven Moreland46e9da82018-07-27 15:45:29 -0700619AidlMember::AidlMember(const AidlLocation& location) : AidlNode(location) {}
620
Steven Moreland46e9da82018-07-27 15:45:29 -0700621AidlConstantDeclaration::AidlConstantDeclaration(const AidlLocation& location,
622 AidlTypeSpecifier* type, const std::string& name,
623 AidlConstantValue* value)
624 : AidlMember(location), type_(type), name_(name), value_(value) {}
Steven Moreland693640b2018-07-19 13:46:27 -0700625
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900626bool AidlConstantDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700627 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900628 valid &= type_->CheckValid(typenames);
Steven Moreland25294322018-08-07 18:13:55 -0700629 valid &= value_->CheckValid();
630 if (!valid) return false;
Steven Moreland693640b2018-07-19 13:46:27 -0700631
Steven Moreland25294322018-08-07 18:13:55 -0700632 const static set<string> kSupportedConstTypes = {"String", "int"};
633 if (kSupportedConstTypes.find(type_->ToString()) == kSupportedConstTypes.end()) {
634 AIDL_ERROR(this) << "Constant of type " << type_->ToString() << " is not supported.";
Steven Moreland693640b2018-07-19 13:46:27 -0700635 return false;
636 }
637
Will McVickerd7d18df2019-09-12 13:40:50 -0700638 return true;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700639}
640
Jiyong Parka428d212018-08-29 22:26:30 +0900641string AidlConstantDeclaration::ToString() const {
642 return "const " + type_->ToString() + " " + name_ + " = " +
643 ValueString(AidlConstantValueDecorator);
644}
645
646string AidlConstantDeclaration::Signature() const {
647 return type_->Signature() + " " + name_;
648}
649
Steven Moreland46e9da82018-07-27 15:45:29 -0700650AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
651 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900652 const std::string& comments)
653 : AidlMethod(location, oneway, type, name, args, comments, 0, true) {
654 has_id_ = false;
655}
656
657AidlMethod::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, int id, bool is_user_defined)
Steven Moreland46e9da82018-07-27 15:45:29 -0700660 : AidlMember(location),
661 oneway_(oneway),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700662 comments_(comments),
663 type_(type),
664 name_(name),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700665 arguments_(std::move(*args)),
Jiyong Parkb034bf02018-07-30 17:44:33 +0900666 id_(id),
667 is_user_defined_(is_user_defined) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700668 has_id_ = true;
669 delete args;
Christopher Wileyad339272015-10-05 19:11:58 -0700670 for (const unique_ptr<AidlArgument>& a : arguments_) {
671 if (a->IsIn()) { in_arguments_.push_back(a.get()); }
672 if (a->IsOut()) { out_arguments_.push_back(a.get()); }
673 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700674}
675
Jeongik Cha997281d2020-01-16 15:23:59 +0900676bool AidlMethod::IsHidden() const {
677 return HasHideComment(GetComments());
678}
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700679
Jiyong Park02da7422018-07-16 16:00:26 +0900680string AidlMethod::Signature() const {
681 vector<string> arg_signatures;
682 for (const auto& arg : GetArguments()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900683 arg_signatures.emplace_back(arg->GetType().ToString());
Jiyong Park02da7422018-07-16 16:00:26 +0900684 }
Jiyong Park309668e2018-07-28 16:55:44 +0900685 return GetName() + "(" + Join(arg_signatures, ", ") + ")";
686}
687
688string AidlMethod::ToString() const {
689 vector<string> arg_strings;
690 for (const auto& arg : GetArguments()) {
691 arg_strings.emplace_back(arg->Signature());
692 }
Steven Moreland4ee68632018-12-14 15:52:46 -0800693 string ret = (IsOneway() ? "oneway " : "") + GetType().Signature() + " " + GetName() + "(" +
694 Join(arg_strings, ", ") + ")";
Jiyong Parked65bf42018-08-28 15:43:27 +0900695 if (HasId()) {
696 ret += " = " + std::to_string(GetId());
697 }
698 return ret;
Jiyong Park02da7422018-07-16 16:00:26 +0900699}
700
Steven Moreland46e9da82018-07-27 15:45:29 -0700701AidlDefinedType::AidlDefinedType(const AidlLocation& location, const std::string& name,
702 const std::string& comments,
Steven Moreland787b0432018-07-03 09:00:58 -0700703 const std::vector<std::string>& package)
Steven Moreland46e9da82018-07-27 15:45:29 -0700704 : AidlAnnotatable(location), name_(name), comments_(comments), package_(package) {}
Steven Moreland787b0432018-07-03 09:00:58 -0700705
706std::string AidlDefinedType::GetPackage() const {
707 return Join(package_, '.');
708}
709
Devin Moore24f68572020-02-26 13:20:59 -0800710bool AidlDefinedType::CheckValid(const AidlTypenames& typenames) const {
711 if (!AidlAnnotatable::CheckValid(typenames)) {
712 return false;
713 }
714
715 return true;
716}
717
Jeongik Cha997281d2020-01-16 15:23:59 +0900718bool AidlDefinedType::IsHidden() const {
719 return HasHideComment(GetComments());
720}
721
Steven Moreland787b0432018-07-03 09:00:58 -0700722std::string AidlDefinedType::GetCanonicalName() const {
723 if (package_.empty()) {
724 return GetName();
725 }
726 return GetPackage() + "." + GetName();
727}
728
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800729void AidlDefinedType::DumpHeader(CodeWriter* writer) const {
730 if (this->IsHidden()) {
731 AddHideComment(writer);
732 }
733 DumpAnnotations(writer);
734}
735
Steven Moreland46e9da82018-07-27 15:45:29 -0700736AidlParcelable::AidlParcelable(const AidlLocation& location, AidlQualifiedName* name,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900737 const std::vector<std::string>& package, const std::string& comments,
Jeongik Chadf76dc72019-11-28 00:08:47 +0900738 const std::string& cpp_header, std::vector<std::string>* type_params)
Jiyong Parka6605ab2018-11-11 14:30:21 +0900739 : AidlDefinedType(location, name->GetDotName(), comments, package),
Jeongik Chadf76dc72019-11-28 00:08:47 +0900740 AidlParameterizable<std::string>(type_params),
Steven Moreland787b0432018-07-03 09:00:58 -0700741 name_(name),
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800742 cpp_header_(cpp_header) {
743 // Strip off quotation marks if we actually have a cpp header.
744 if (cpp_header_.length() >= 2) {
745 cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2);
746 }
Casey Dahlin59401da2015-10-09 18:16:45 -0700747}
Jeongik Chadf76dc72019-11-28 00:08:47 +0900748template <typename T>
749AidlParameterizable<T>::AidlParameterizable(const AidlParameterizable& other) {
750 // Copying is not supported if it has type parameters.
751 // It doesn't make a problem because only ArrayBase() makes a copy,
752 // and it can be called only if a type is not generic.
753 CHECK(!other.IsGeneric());
754}
755
756template <typename T>
757bool AidlParameterizable<T>::CheckValid() const {
758 return true;
759};
760
761template <>
762bool AidlParameterizable<std::string>::CheckValid() const {
763 if (!IsGeneric()) {
764 return true;
765 }
766 std::unordered_set<std::string> set(GetTypeParameters().begin(), GetTypeParameters().end());
767 if (set.size() != GetTypeParameters().size()) {
768 AIDL_ERROR(this->AsAidlNode()) << "Every type parameter should be unique.";
769 return false;
770 }
771 return true;
772}
Casey Dahlin59401da2015-10-09 18:16:45 -0700773
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700774std::set<AidlAnnotation::Type> AidlParcelable::GetSupportedAnnotations() const {
775 return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE,
776 AidlAnnotation::Type::JAVA_STABLE_PARCELABLE, AidlAnnotation::Type::HIDE};
Devin Moore24f68572020-02-26 13:20:59 -0800777}
778
779bool AidlParcelable::CheckValid(const AidlTypenames& typenames) const {
780 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100781 return false;
782 }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900783 if (!AidlParameterizable<std::string>::CheckValid()) {
784 return false;
785 }
Jeongik Cha82317dd2019-02-27 20:26:11 +0900786
787 return true;
788}
789
Jeongik Cha997281d2020-01-16 15:23:59 +0900790void AidlParcelable::Dump(CodeWriter* writer) const {
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800791 DumpHeader(writer);
Jiyong Park02da7422018-07-16 16:00:26 +0900792 writer->Write("parcelable %s ;\n", GetName().c_str());
793}
794
Steven Moreland5557f1c2018-07-02 13:50:23 -0700795AidlStructuredParcelable::AidlStructuredParcelable(
Steven Moreland46e9da82018-07-27 15:45:29 -0700796 const AidlLocation& location, AidlQualifiedName* name, const std::vector<std::string>& package,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900797 const std::string& comments, std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables)
798 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/),
Steven Moreland46e9da82018-07-27 15:45:29 -0700799 variables_(std::move(*variables)) {}
Steven Moreland5557f1c2018-07-02 13:50:23 -0700800
Jeongik Cha997281d2020-01-16 15:23:59 +0900801void AidlStructuredParcelable::Dump(CodeWriter* writer) const {
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800802 DumpHeader(writer);
Jiyong Park02da7422018-07-16 16:00:26 +0900803 writer->Write("parcelable %s {\n", GetName().c_str());
804 writer->Indent();
805 for (const auto& field : GetFields()) {
Jeongik Cha997281d2020-01-16 15:23:59 +0900806 if (field->GetType().IsHidden()) {
807 AddHideComment(writer);
808 }
Jiyong Parka468e2a2018-08-29 21:25:18 +0900809 writer->Write("%s;\n", field->ToString().c_str());
Jiyong Park02da7422018-07-16 16:00:26 +0900810 }
811 writer->Dedent();
812 writer->Write("}\n");
813}
814
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700815std::set<AidlAnnotation::Type> AidlStructuredParcelable::GetSupportedAnnotations() const {
816 return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE,
817 AidlAnnotation::Type::HIDE};
Devin Moore24f68572020-02-26 13:20:59 -0800818}
819
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900820bool AidlStructuredParcelable::CheckValid(const AidlTypenames& typenames) const {
Daniel Norman85aed542019-08-21 12:01:14 -0700821 bool success = true;
Devin Moore24f68572020-02-26 13:20:59 -0800822 if (!AidlParcelable::CheckValid(typenames)) {
823 return false;
824 }
825
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900826 for (const auto& v : GetFields()) {
Daniel Norman85aed542019-08-21 12:01:14 -0700827 success = success && v->CheckValid(typenames);
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900828 }
Daniel Norman85aed542019-08-21 12:01:14 -0700829 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900830}
831
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900832// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -0700833bool AidlTypeSpecifier::LanguageSpecificCheckValid(const AidlTypenames& typenames,
834 Options::Language lang) const {
Steven Moreland0185d9b2020-05-15 23:21:22 +0000835 if (lang == Options::Language::NDK && IsArray() && GetName() == "IBinder") {
836 AIDL_ERROR(this) << "The NDK backend does not support array of IBinder";
837 return false;
838 }
Steven Morelandd59e3172020-05-11 16:42:09 -0700839 if (lang == Options::Language::NDK && IsArray() && IsNullable()) {
Steven Moreland0185d9b2020-05-15 23:21:22 +0000840 if (GetName() == "ParcelFileDescriptor") {
841 AIDL_ERROR(this) << "The NDK backend does not support nullable array of ParcelFileDescriptor";
842 return false;
843 }
844
Steven Morelandd59e3172020-05-11 16:42:09 -0700845 const auto defined_type = typenames.TryGetDefinedType(GetName());
846 if (defined_type != nullptr && defined_type->AsParcelable() != nullptr) {
847 AIDL_ERROR(this) << "The NDK backend does not support nullable array of parcelable";
848 return false;
849 }
850 }
Steven Moreland04b07e12020-01-21 18:11:07 -0800851 if (lang != Options::Language::JAVA) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900852 if (this->GetName() == "List" && !this->IsGeneric()) {
Steven Moreland04b07e12020-01-21 18:11:07 -0800853 AIDL_ERROR(this) << "Currently, only the Java backend supports non-generic List.";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900854 return false;
855 }
856 }
Steven Morelandc8a4ca82020-01-21 17:50:08 -0800857 if (this->GetName() == "FileDescriptor" && lang == Options::Language::NDK) {
858 AIDL_ERROR(this) << "FileDescriptor isn't supported with the NDK.";
859 return false;
860 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900861 if (this->IsGeneric()) {
862 if (this->GetName() == "List") {
863 if (this->GetTypeParameters().size() != 1) {
864 AIDL_ERROR(this) << "List must have only one type parameter.";
865 return false;
866 }
867 if (lang == Options::Language::CPP) {
Devin Moore2ac52f92020-03-23 15:39:36 -0700868 const string& contained_type = this->GetTypeParameters()[0]->GetName();
869 if (!(contained_type == "String" || contained_type == "IBinder")) {
870 AIDL_ERROR(this) << "List<" << contained_type
871 << "> is not supported. List in cpp supports only String and IBinder.";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900872 return false;
873 }
Jeongik Cha08ca2182019-11-21 14:01:13 +0900874 } else if (lang == Options::Language::JAVA) {
875 const string& contained_type = this->GetTypeParameters()[0]->GetName();
876 if (AidlTypenames::IsBuiltinTypename(contained_type)) {
877 if (contained_type != "String" && contained_type != "IBinder" &&
878 contained_type != "ParcelFileDescriptor") {
Devin Moore2ac52f92020-03-23 15:39:36 -0700879 AIDL_ERROR(this) << "List<" << contained_type
880 << "> is not supported. List in Java supports only String, IBinder, "
881 "and ParcelFileDescriptor.";
Jeongik Cha08ca2182019-11-21 14:01:13 +0900882 return false;
883 }
884 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900885 }
Jeongik Chabb55b5e2020-01-07 23:11:26 +0900886 }
887 }
Jeongik Chab75a4512020-01-10 13:37:04 +0900888 if (this->GetName() == "Map" || this->GetName() == "CharSequence") {
Jeongik Chabb55b5e2020-01-07 23:11:26 +0900889 if (lang != Options::Language::JAVA) {
Jeongik Chab75a4512020-01-10 13:37:04 +0900890 AIDL_ERROR(this) << "Currently, only Java backend supports " << this->GetName() << ".";
Jeongik Chabb55b5e2020-01-07 23:11:26 +0900891 return false;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900892 }
893 }
Jeongik Cha08ca2182019-11-21 14:01:13 +0900894 if (lang == Options::Language::JAVA) {
895 const string name = this->GetName();
896 // List[], Map[], CharSequence[] are not supported.
897 if (AidlTypenames::IsBuiltinTypename(name) && this->IsArray()) {
898 if (name == "List" || name == "Map" || name == "CharSequence") {
899 AIDL_ERROR(this) << "List[], Map[], CharSequence[] are not supported.";
900 return false;
901 }
902 }
903 }
904
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900905 return true;
906}
907
908// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -0700909bool AidlParcelable::LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/,
910 Options::Language lang) const {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900911 if (lang != Options::Language::JAVA) {
Steven Moreland0d9c26e2020-01-22 08:52:08 -0800912 const AidlParcelable* unstructured_parcelable = this->AsUnstructuredParcelable();
913 if (unstructured_parcelable != nullptr) {
914 if (unstructured_parcelable->GetCppHeader().empty()) {
915 AIDL_ERROR(unstructured_parcelable)
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900916 << "Unstructured parcelable must have C++ header defined.";
917 return false;
918 }
919 }
920 }
921 return true;
922}
923
924// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -0700925bool AidlStructuredParcelable::LanguageSpecificCheckValid(const AidlTypenames& typenames,
926 Options::Language lang) const {
927 if (!AidlParcelable::LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900928 return false;
929 }
930 for (const auto& v : this->GetFields()) {
Steven Morelandd59e3172020-05-11 16:42:09 -0700931 if (!v->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900932 return false;
933 }
934 }
935 return true;
936}
937
Daniel Norman85aed542019-08-21 12:01:14 -0700938AidlEnumerator::AidlEnumerator(const AidlLocation& location, const std::string& name,
Daniel Norman2e4112d2019-10-03 10:22:35 -0700939 AidlConstantValue* value, const std::string& comments)
940 : AidlNode(location), name_(name), value_(value), comments_(comments) {}
Daniel Norman85aed542019-08-21 12:01:14 -0700941
942bool AidlEnumerator::CheckValid(const AidlTypeSpecifier& enum_backing_type) const {
943 if (GetValue() == nullptr) {
944 return false;
945 }
946 if (!GetValue()->CheckValid()) {
947 return false;
948 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700949 if (GetValue()->ValueString(enum_backing_type, AidlConstantValueDecorator).empty()) {
Daniel Norman85aed542019-08-21 12:01:14 -0700950 AIDL_ERROR(this) << "Enumerator type differs from enum backing type.";
951 return false;
952 }
953 return true;
954}
955
956string AidlEnumerator::ValueString(const AidlTypeSpecifier& backing_type,
957 const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700958 return GetValue()->ValueString(backing_type, decorator);
Daniel Norman85aed542019-08-21 12:01:14 -0700959}
960
961AidlEnumDeclaration::AidlEnumDeclaration(const AidlLocation& location, const std::string& name,
962 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Daniel Norman2e4112d2019-10-03 10:22:35 -0700963 const std::vector<std::string>& package,
964 const std::string& comments)
965 : AidlDefinedType(location, name, comments, package), enumerators_(std::move(*enumerators)) {}
Daniel Norman85aed542019-08-21 12:01:14 -0700966
967void AidlEnumDeclaration::SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type) {
968 backing_type_ = std::move(type);
969}
970
Steven Moreland59e53e42019-11-26 20:38:08 -0800971bool AidlEnumDeclaration::Autofill() {
Daniel Normanb28684e2019-10-17 15:31:39 -0700972 const AidlEnumerator* previous = nullptr;
973 for (const auto& enumerator : enumerators_) {
974 if (enumerator->GetValue() == nullptr) {
975 if (previous == nullptr) {
Devin Mooredf93ebb2020-03-25 14:03:35 -0700976 enumerator->SetValue(
977 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(GetLocation(), "0")));
Daniel Normanb28684e2019-10-17 15:31:39 -0700978 } else {
Steven Moreland59e53e42019-11-26 20:38:08 -0800979 auto prev_value = std::unique_ptr<AidlConstantValue>(
980 AidlConstantValue::ShallowIntegralCopy(*previous->GetValue()));
981 if (prev_value == nullptr) {
982 return false;
983 }
Daniel Normanb28684e2019-10-17 15:31:39 -0700984 enumerator->SetValue(std::make_unique<AidlBinaryConstExpression>(
Devin Mooredf93ebb2020-03-25 14:03:35 -0700985 GetLocation(), std::move(prev_value), "+",
986 std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(GetLocation(), "1"))));
Daniel Normanb28684e2019-10-17 15:31:39 -0700987 }
988 }
989 previous = enumerator.get();
990 }
Steven Moreland59e53e42019-11-26 20:38:08 -0800991 return true;
Daniel Normanb28684e2019-10-17 15:31:39 -0700992}
993
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700994std::set<AidlAnnotation::Type> AidlEnumDeclaration::GetSupportedAnnotations() const {
995 return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::BACKING,
996 AidlAnnotation::Type::HIDE};
Devin Moore24f68572020-02-26 13:20:59 -0800997}
998
999bool AidlEnumDeclaration::CheckValid(const AidlTypenames& typenames) const {
1000 if (!AidlDefinedType::CheckValid(typenames)) {
1001 return false;
1002 }
Daniel Norman85aed542019-08-21 12:01:14 -07001003 if (backing_type_ == nullptr) {
1004 AIDL_ERROR(this) << "Enum declaration missing backing type.";
1005 return false;
1006 }
1007 bool success = true;
1008 for (const auto& enumerator : enumerators_) {
1009 success = success && enumerator->CheckValid(GetBackingType());
1010 }
1011 return success;
1012}
1013
Jeongik Cha997281d2020-01-16 15:23:59 +09001014void AidlEnumDeclaration::Dump(CodeWriter* writer) const {
Steven Morelanda5d9c5c2020-02-21 16:01:09 -08001015 DumpHeader(writer);
Daniel Norman37d43dd2019-09-09 17:22:34 -07001016 writer->Write("enum %s {\n", GetName().c_str());
Daniel Norman85aed542019-08-21 12:01:14 -07001017 writer->Indent();
1018 for (const auto& enumerator : GetEnumerators()) {
Daniel Norman85aed542019-08-21 12:01:14 -07001019 writer->Write("%s = %s,\n", enumerator->GetName().c_str(),
1020 enumerator->ValueString(GetBackingType(), AidlConstantValueDecorator).c_str());
1021 }
1022 writer->Dedent();
1023 writer->Write("}\n");
1024}
1025
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001026// TODO: we should treat every backend all the same in future.
Steven Morelandd59e3172020-05-11 16:42:09 -07001027bool AidlInterface::LanguageSpecificCheckValid(const AidlTypenames& typenames,
1028 Options::Language lang) const {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001029 for (const auto& m : this->GetMethods()) {
Steven Morelandd59e3172020-05-11 16:42:09 -07001030 if (!m->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001031 return false;
1032 }
1033 for (const auto& arg : m->GetArguments()) {
Steven Morelandd59e3172020-05-11 16:42:09 -07001034 if (!arg->GetType().LanguageSpecificCheckValid(typenames, lang)) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001035 return false;
1036 }
1037 }
1038 }
1039 return true;
1040}
1041
Steven Moreland46e9da82018-07-27 15:45:29 -07001042AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name,
Casey Dahlinfb7da2e2015-10-08 17:26:09 -07001043 const std::string& comments, bool oneway,
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001044 std::vector<std::unique_ptr<AidlMember>>* members,
Christopher Wileyd76067c2015-10-19 17:00:13 -07001045 const std::vector<std::string>& package)
Steven Morelandacd53472018-12-14 10:17:26 -08001046 : AidlDefinedType(location, name, comments, package) {
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001047 for (auto& member : *members) {
1048 AidlMember* local = member.release();
1049 AidlMethod* method = local->AsMethod();
Steven Moreland693640b2018-07-19 13:46:27 -07001050 AidlConstantDeclaration* constant = local->AsConstantDeclaration();
1051
1052 CHECK(method == nullptr || constant == nullptr);
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001053
1054 if (method) {
Steven Moreland8c70ba92018-12-17 10:20:31 -08001055 method->ApplyInterfaceOneway(oneway);
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001056 methods_.emplace_back(method);
Steven Moreland693640b2018-07-19 13:46:27 -07001057 } else if (constant) {
1058 constants_.emplace_back(constant);
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001059 } else {
Steven Moreland46e9da82018-07-27 15:45:29 -07001060 AIDL_FATAL(this) << "Member is neither method nor constant!";
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001061 }
1062 }
1063
1064 delete members;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -07001065}
1066
Jeongik Cha997281d2020-01-16 15:23:59 +09001067void AidlInterface::Dump(CodeWriter* writer) const {
Steven Morelanda5d9c5c2020-02-21 16:01:09 -08001068 DumpHeader(writer);
Jiyong Park02da7422018-07-16 16:00:26 +09001069 writer->Write("interface %s {\n", GetName().c_str());
1070 writer->Indent();
1071 for (const auto& method : GetMethods()) {
Jeongik Cha997281d2020-01-16 15:23:59 +09001072 if (method->IsHidden()) {
1073 AddHideComment(writer);
1074 }
Jiyong Park309668e2018-07-28 16:55:44 +09001075 writer->Write("%s;\n", method->ToString().c_str());
Jiyong Park02da7422018-07-16 16:00:26 +09001076 }
Jiyong Parka428d212018-08-29 22:26:30 +09001077 for (const auto& constdecl : GetConstantDeclarations()) {
Jeongik Cha997281d2020-01-16 15:23:59 +09001078 if (constdecl->GetType().IsHidden()) {
1079 AddHideComment(writer);
1080 }
Jiyong Parka428d212018-08-29 22:26:30 +09001081 writer->Write("%s;\n", constdecl->ToString().c_str());
1082 }
Jiyong Park02da7422018-07-16 16:00:26 +09001083 writer->Dedent();
1084 writer->Write("}\n");
1085}
1086
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001087std::set<AidlAnnotation::Type> AidlInterface::GetSupportedAnnotations() const {
1088 return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE,
1089 AidlAnnotation::Type::HIDE};
Devin Moore24f68572020-02-26 13:20:59 -08001090}
1091
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001092bool AidlInterface::CheckValid(const AidlTypenames& typenames) const {
Devin Moore24f68572020-02-26 13:20:59 -08001093 if (!AidlDefinedType::CheckValid(typenames)) {
Andrei Onea9445fc62019-06-27 18:11:59 +01001094 return false;
1095 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001096 // Has to be a pointer due to deleting copy constructor. No idea why.
1097 map<string, const AidlMethod*> method_names;
1098 for (const auto& m : GetMethods()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001099 if (!m->GetType().CheckValid(typenames)) {
1100 return false;
1101 }
1102
Jeongik Cha649e8a72020-03-27 17:47:40 +09001103 // TODO(b/156872582): Support it when ParcelableHolder supports every backend.
1104 if (m->GetType().GetName() == "ParcelableHolder") {
1105 AIDL_ERROR(m) << "ParcelableHolder cannot be a return type";
1106 return false;
1107 }
Steven Morelandacd53472018-12-14 10:17:26 -08001108 if (m->IsOneway() && m->GetType().GetName() != "void") {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001109 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot return a value";
1110 return false;
1111 }
1112
1113 set<string> argument_names;
1114 for (const auto& arg : m->GetArguments()) {
1115 auto it = argument_names.find(arg->GetName());
1116 if (it != argument_names.end()) {
1117 AIDL_ERROR(m) << "method '" << m->GetName() << "' has duplicate argument name '"
1118 << arg->GetName() << "'";
1119 return false;
1120 }
1121 argument_names.insert(arg->GetName());
1122
1123 if (!arg->GetType().CheckValid(typenames)) {
1124 return false;
1125 }
1126
Jeongik Cha649e8a72020-03-27 17:47:40 +09001127 // TODO(b/156872582): Support it when ParcelableHolder supports every backend.
1128 if (arg->GetType().GetName() == "ParcelableHolder") {
1129 AIDL_ERROR(arg) << "ParcelableHolder cannot be an argument type";
1130 return false;
1131 }
Steven Morelandacd53472018-12-14 10:17:26 -08001132 if (m->IsOneway() && arg->IsOut()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001133 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot have out parameters";
1134 return false;
1135 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001136 const bool can_be_out = typenames.CanBeOutParameter(arg->GetType());
1137 if (!arg->DirectionWasSpecified() && can_be_out) {
1138 AIDL_ERROR(arg) << "'" << arg->GetType().ToString()
1139 << "' can be an out type, so you must declare it as in, out, or inout.";
1140 return false;
1141 }
1142
1143 if (arg->GetDirection() != AidlArgument::IN_DIR && !can_be_out) {
1144 AIDL_ERROR(arg) << "'" << arg->ToString() << "' can only be an in parameter.";
1145 return false;
1146 }
1147
1148 // check that the name doesn't match a keyword
Jeongik Cha997281d2020-01-16 15:23:59 +09001149 if (IsJavaKeyword(arg->GetName().c_str())) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001150 AIDL_ERROR(arg) << "Argument name is a Java or aidl keyword";
1151 return false;
1152 }
1153
1154 // Reserve a namespace for internal use
1155 if (android::base::StartsWith(arg->GetName(), "_aidl")) {
1156 AIDL_ERROR(arg) << "Argument name cannot begin with '_aidl'";
1157 return false;
1158 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001159 }
1160
1161 auto it = method_names.find(m->GetName());
1162 // prevent duplicate methods
1163 if (it == method_names.end()) {
1164 method_names[m->GetName()] = m.get();
1165 } else {
1166 AIDL_ERROR(m) << "attempt to redefine method " << m->GetName() << ":";
1167 AIDL_ERROR(it->second) << "previously defined here.";
1168 return false;
1169 }
1170
Paul Trautrimb77048c2020-01-21 16:39:32 +09001171 static set<string> reserved_methods{"asBinder()", "getInterfaceHash()", "getInterfaceVersion()",
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001172 "getTransactionName(int)"};
1173
1174 if (reserved_methods.find(m->Signature()) != reserved_methods.end()) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001175 AIDL_ERROR(m) << " method " << m->Signature() << " is reserved for internal use.";
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001176 return false;
1177 }
1178 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001179
1180 bool success = true;
1181 set<string> constant_names;
1182 for (const std::unique_ptr<AidlConstantDeclaration>& constant : GetConstantDeclarations()) {
1183 if (constant_names.count(constant->GetName()) > 0) {
Devin Moore097a3ab2020-03-11 16:08:44 -07001184 AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'";
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001185 success = false;
1186 }
1187 constant_names.insert(constant->GetName());
1188 success = success && constant->CheckValid(typenames);
1189 }
1190
1191 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001192}
1193
Steven Moreland46e9da82018-07-27 15:45:29 -07001194AidlQualifiedName::AidlQualifiedName(const AidlLocation& location, const std::string& term,
1195 const std::string& comments)
1196 : AidlNode(location), terms_({term}), comments_(comments) {
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -08001197 if (term.find('.') != string::npos) {
1198 terms_ = Split(term, ".");
Steven Moreland46e9da82018-07-27 15:45:29 -07001199 for (const auto& subterm : terms_) {
1200 if (subterm.empty()) {
1201 AIDL_FATAL(this) << "Malformed qualified identifier: '" << term << "'";
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -08001202 }
1203 }
1204 }
Casey Dahlin2b2879b2015-10-13 16:59:44 -07001205}
1206
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -07001207void AidlQualifiedName::AddTerm(const std::string& term) {
Casey Dahlin2b2879b2015-10-13 16:59:44 -07001208 terms_.push_back(term);
1209}
1210
Steven Moreland46e9da82018-07-27 15:45:29 -07001211AidlImport::AidlImport(const AidlLocation& location, const std::string& needed_class)
1212 : AidlNode(location), needed_class_(needed_class) {}