blob: a32f8a288619a00549eae32178b9ef391c45e7d6 [file] [log] [blame]
Adam Lesinskiffa16862014-01-23 18:17:42 -08001#include "aidl_language.h"
Jiyong Park1deecc32018-07-17 01:14:41 +09002#include "aidl_typenames.h"
Christopher Wileyf690be52015-09-14 15:19:10 -07003
Adam Lesinskiffa16862014-01-23 18:17:42 -08004#include <stdio.h>
Adam Lesinskiffa16862014-01-23 18:17:42 -08005#include <stdlib.h>
Christopher Wiley4a2884b2015-10-07 11:27:45 -07006#include <string.h>
Jiyong Park68bc77a2018-07-19 19:00:45 +09007#include <algorithm>
Jiyong Park1deecc32018-07-17 01:14:41 +09008#include <iostream>
Jiyong Park68bc77a2018-07-19 19:00:45 +09009#include <set>
10#include <sstream>
Casey Dahlindd691812015-09-09 17:59:06 -070011#include <string>
Jiyong Park1deecc32018-07-17 01:14:41 +090012#include <utility>
Christopher Wileyf690be52015-09-14 15:19:10 -070013
Steven Moreland1c4ba202018-08-09 10:49:54 -070014#include <android-base/parsedouble.h>
Roshan Pius9d7810a2016-07-28 08:57:50 -070015#include <android-base/parseint.h>
Elliott Hughes0a620672015-12-04 13:53:18 -080016#include <android-base/strings.h>
Christopher Wileyd76067c2015-10-19 17:00:13 -070017
Ying Wang3000e752016-01-11 18:05:59 -080018#include "aidl_language_y.h"
Christopher Wiley4a2884b2015-10-07 11:27:45 -070019#include "logging.h"
Adam Lesinskiffa16862014-01-23 18:17:42 -080020
Casey Dahlin07b9dde2015-09-10 19:13:49 -070021#ifdef _WIN32
22int isatty(int fd)
23{
24 return (fd == 0);
25}
26#endif
27
Christopher Wiley4a2884b2015-10-07 11:27:45 -070028using android::aidl::IoDelegate;
Christopher Wileyd76067c2015-10-19 17:00:13 -070029using android::base::Join;
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -080030using android::base::Split;
Casey Dahlindd691812015-09-09 17:59:06 -070031using std::cerr;
32using std::endl;
Jiyong Park1deecc32018-07-17 01:14:41 +090033using std::pair;
Jiyong Park68bc77a2018-07-19 19:00:45 +090034using std::set;
Christopher Wiley4a2884b2015-10-07 11:27:45 -070035using std::string;
36using std::unique_ptr;
Jiyong Parkccf00f82018-07-17 01:39:23 +090037using std::vector;
Adam Lesinskiffa16862014-01-23 18:17:42 -080038
Jeongik Cha047c5ee2019-08-07 23:16:49 +090039namespace {
40bool is_java_keyword(const char* str) {
41 static const std::vector<std::string> kJavaKeywords{
42 "abstract", "assert", "boolean", "break", "byte", "case", "catch",
43 "char", "class", "const", "continue", "default", "do", "double",
44 "else", "enum", "extends", "final", "finally", "float", "for",
45 "goto", "if", "implements", "import", "instanceof", "int", "interface",
46 "long", "native", "new", "package", "private", "protected", "public",
47 "return", "short", "static", "strictfp", "super", "switch", "synchronized",
48 "this", "throw", "throws", "transient", "try", "void", "volatile",
49 "while", "true", "false", "null",
50 };
51 return std::find(kJavaKeywords.begin(), kJavaKeywords.end(), str) != kJavaKeywords.end();
52}
53} // namespace
54
Casey Dahlindd691812015-09-09 17:59:06 -070055void yylex_init(void **);
56void yylex_destroy(void *);
57void yyset_in(FILE *f, void *);
Casey Dahline2507492015-09-14 17:11:20 -070058int yyparse(Parser*);
Christopher Wiley4a2884b2015-10-07 11:27:45 -070059YY_BUFFER_STATE yy_scan_buffer(char *, size_t, void *);
Casey Dahlin89d44842015-09-24 18:45:54 -070060void yy_delete_buffer(YY_BUFFER_STATE, void *);
Casey Dahlindd691812015-09-09 17:59:06 -070061
Casey Dahlincdbbc8c2015-10-14 15:31:04 -070062AidlToken::AidlToken(const std::string& text, const std::string& comments)
63 : text_(text),
64 comments_(comments) {}
Casey Dahlin98a544b2015-10-14 14:22:55 -070065
Steven Moreland46e9da82018-07-27 15:45:29 -070066AidlLocation::AidlLocation(const std::string& file, Point begin, Point end)
67 : file_(file), begin_(begin), end_(end) {}
68
69std::ostream& operator<<(std::ostream& os, const AidlLocation& l) {
70 os << l.file_ << ":" << l.begin_.line << "." << l.begin_.column << "-";
71 if (l.begin_.line != l.end_.line) {
72 os << l.end_.line << ".";
73 }
74 os << l.end_.column;
75 return os;
76}
77
78AidlNode::AidlNode(const AidlLocation& location) : location_(location) {}
79
Andrei Onea8714b022019-02-01 18:55:54 +000080std::string AidlNode::PrintLocation() const {
81 std::stringstream ss;
82 ss << location_.file_ << ":" << location_.begin_.line;
83 return ss.str();
84}
85
Steven Moreland92c55f12018-07-31 14:08:37 -070086AidlError::AidlError(bool fatal) : os_(std::cerr), fatal_(fatal) {
87 os_ << "ERROR: ";
88}
89
Jiyong Park68bc77a2018-07-19 19:00:45 +090090static const string kNullable("nullable");
Jiyong Park68bc77a2018-07-19 19:00:45 +090091static const string kUtf8InCpp("utf8InCpp");
Steven Morelanda57d0a62019-07-30 09:41:14 -070092static const string kVintfStability("VintfStability");
Jiyong Parka6605ab2018-11-11 14:30:21 +090093static const string kUnsupportedAppUsage("UnsupportedAppUsage");
Jeongik Cha698e6af2018-12-12 14:39:55 +090094static const string kSystemApi("SystemApi");
Jeongik Cha82317dd2019-02-27 20:26:11 +090095static const string kStableParcelable("JavaOnlyStableParcelable");
Daniel Norman85aed542019-08-21 12:01:14 -070096static const string kBacking("Backing");
Jiyong Park68bc77a2018-07-19 19:00:45 +090097
Andrei Onea9445fc62019-06-27 18:11:59 +010098static const std::map<string, std::map<std::string, std::string>> kAnnotationParameters{
99 {kNullable, {}},
100 {kUtf8InCpp, {}},
Steven Morelanda57d0a62019-07-30 09:41:14 -0700101 {kVintfStability, {}},
Andrei Onea9445fc62019-06-27 18:11:59 +0100102 {kUnsupportedAppUsage,
103 {{"expectedSignature", "String"},
104 {"implicitMember", "String"},
105 {"maxTargetSdk", "int"},
106 {"publicAlternatives", "String"},
107 {"trackingBug", "long"}}},
108 {kSystemApi, {}},
Daniel Norman85aed542019-08-21 12:01:14 -0700109 {kStableParcelable, {}},
110 {kBacking, {{"type", "String"}}}};
Andrei Onea9445fc62019-06-27 18:11:59 +0100111
112AidlAnnotation* AidlAnnotation::Parse(
113 const AidlLocation& location, const string& name,
114 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list) {
Steven Moreland29db3022019-07-30 12:37:17 -0700115 if (kAnnotationParameters.find(name) == kAnnotationParameters.end()) {
Jiyong Park68bc77a2018-07-19 19:00:45 +0900116 std::ostringstream stream;
Steven Moreland46e9da82018-07-27 15:45:29 -0700117 stream << "'" << name << "' is not a recognized annotation. ";
Jiyong Park68bc77a2018-07-19 19:00:45 +0900118 stream << "It must be one of:";
Steven Moreland29db3022019-07-30 12:37:17 -0700119 for (const auto& kv : kAnnotationParameters) {
120 stream << " " << kv.first;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900121 }
122 stream << ".";
Steven Moreland46e9da82018-07-27 15:45:29 -0700123 AIDL_ERROR(location) << stream.str();
124 return nullptr;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900125 }
Andrei Onea9445fc62019-06-27 18:11:59 +0100126 if (parameter_list == nullptr) {
127 return new AidlAnnotation(location, name);
128 }
129
130 return new AidlAnnotation(location, name, std::move(*parameter_list));
Jiyong Park68bc77a2018-07-19 19:00:45 +0900131}
132
Steven Moreland46e9da82018-07-27 15:45:29 -0700133AidlAnnotation::AidlAnnotation(const AidlLocation& location, const string& name)
Andrei Onea9445fc62019-06-27 18:11:59 +0100134 : AidlAnnotation(location, name, {}) {}
135
136AidlAnnotation::AidlAnnotation(
137 const AidlLocation& location, const string& name,
138 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters)
139 : AidlNode(location), name_(name), parameters_(std::move(parameters)) {}
140
141bool AidlAnnotation::CheckValid() const {
142 auto supported_params_iterator = kAnnotationParameters.find(GetName());
143 if (supported_params_iterator == kAnnotationParameters.end()) {
144 AIDL_ERROR(this) << GetName() << " annotation does not have any supported parameters.";
145 return false;
146 }
147 const auto& supported_params = supported_params_iterator->second;
148 for (const auto& name_and_param : parameters_) {
149 const std::string& param_name = name_and_param.first;
150 const std::shared_ptr<AidlConstantValue>& param = name_and_param.second;
151 auto parameter_mapping_it = supported_params.find(param_name);
152 if (parameter_mapping_it == supported_params.end()) {
153 std::ostringstream stream;
154 stream << "Parameter " << param_name << " not supported ";
155 stream << "for annotation " << GetName() << ".";
156 stream << "It must be one of:";
157 for (const auto& kv : supported_params) {
158 stream << " " << kv.first;
159 }
160 AIDL_ERROR(this) << stream.str();
161 return false;
162 }
163 AidlTypeSpecifier type{AIDL_LOCATION_HERE, parameter_mapping_it->second, false, nullptr, ""};
164 const std::string param_value = param->As(type, AidlConstantValueDecorator);
165 // Assume error on empty string.
166 if (param_value == "") {
167 AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation "
168 << GetName() << ".";
169 return false;
170 }
171 }
172 return true;
173}
174
175std::map<std::string, std::string> AidlAnnotation::AnnotationParams(
176 const ConstantValueDecorator& decorator) const {
177 std::map<std::string, std::string> raw_params;
178 const auto& supported_params = kAnnotationParameters.at(GetName());
179 for (const auto& name_and_param : parameters_) {
180 const std::string& param_name = name_and_param.first;
181 const std::shared_ptr<AidlConstantValue>& param = name_and_param.second;
182 AidlTypeSpecifier type{AIDL_LOCATION_HERE, supported_params.at(param_name), false, nullptr, ""};
183 raw_params.emplace(param_name, param->As(type, decorator));
184 }
185 return raw_params;
186}
Steven Moreland46e9da82018-07-27 15:45:29 -0700187
Jiyong Parka6605ab2018-11-11 14:30:21 +0900188static bool HasAnnotation(const vector<AidlAnnotation>& annotations, const string& name) {
Jiyong Park68bc77a2018-07-19 19:00:45 +0900189 for (const auto& a : annotations) {
Steven Moreland3be75772018-08-20 13:27:43 -0700190 if (a.GetName() == name) {
Jiyong Park68bc77a2018-07-19 19:00:45 +0900191 return true;
192 }
193 }
194 return false;
195}
196
Andrei Onea9445fc62019-06-27 18:11:59 +0100197static const AidlAnnotation* GetAnnotation(const vector<AidlAnnotation>& annotations,
198 const string& name) {
199 for (const auto& a : annotations) {
200 if (a.GetName() == name) {
201 return &a;
202 }
203 }
204 return nullptr;
205}
206
Steven Moreland46e9da82018-07-27 15:45:29 -0700207AidlAnnotatable::AidlAnnotatable(const AidlLocation& location) : AidlNode(location) {}
208
Jiyong Park68bc77a2018-07-19 19:00:45 +0900209bool AidlAnnotatable::IsNullable() const {
210 return HasAnnotation(annotations_, kNullable);
211}
212
Jiyong Park68bc77a2018-07-19 19:00:45 +0900213bool AidlAnnotatable::IsUtf8InCpp() const {
214 return HasAnnotation(annotations_, kUtf8InCpp);
215}
216
Steven Morelanda57d0a62019-07-30 09:41:14 -0700217bool AidlAnnotatable::IsVintfStability() const {
218 return HasAnnotation(annotations_, kVintfStability);
219}
220
Andrei Onea9445fc62019-06-27 18:11:59 +0100221const AidlAnnotation* AidlAnnotatable::UnsupportedAppUsage() const {
222 return GetAnnotation(annotations_, kUnsupportedAppUsage);
Jiyong Parka6605ab2018-11-11 14:30:21 +0900223}
224
Daniel Norman85aed542019-08-21 12:01:14 -0700225const AidlTypeSpecifier* AidlAnnotatable::BackingType() const {
226 auto annotation = GetAnnotation(annotations_, kBacking);
227 if (annotation != nullptr) {
228 auto annotation_params = annotation->AnnotationParams(AidlConstantValueDecorator);
229 if (auto it = annotation_params.find("type"); it != annotation_params.end()) {
230 const string& type = it->second;
231 return new AidlTypeSpecifier(AIDL_LOCATION_HERE,
232 // Strip the quotes off the type String.
233 type.substr(1, type.length() - 2), false, nullptr, "");
234 }
235 }
236 return nullptr;
237}
238
Jeongik Cha698e6af2018-12-12 14:39:55 +0900239bool AidlAnnotatable::IsSystemApi() const {
240 return HasAnnotation(annotations_, kSystemApi);
241}
242
Jeongik Cha82317dd2019-02-27 20:26:11 +0900243bool AidlAnnotatable::IsStableParcelable() const {
244 return HasAnnotation(annotations_, kStableParcelable);
245}
246
Andrei Onea9445fc62019-06-27 18:11:59 +0100247bool AidlAnnotatable::CheckValidAnnotations() const {
248 for (const auto& annotation : GetAnnotations()) {
249 if (!annotation.CheckValid()) {
250 return false;
251 }
252 }
Steven Morelanda57d0a62019-07-30 09:41:14 -0700253
Andrei Onea9445fc62019-06-27 18:11:59 +0100254 return true;
255}
256
Jiyong Park68bc77a2018-07-19 19:00:45 +0900257string AidlAnnotatable::ToString() const {
258 vector<string> ret;
259 for (const auto& a : annotations_) {
Steven Moreland3be75772018-08-20 13:27:43 -0700260 ret.emplace_back(a.ToString());
Jiyong Park68bc77a2018-07-19 19:00:45 +0900261 }
262 std::sort(ret.begin(), ret.end());
263 return Join(ret, " ");
264}
265
Steven Moreland46e9da82018-07-27 15:45:29 -0700266AidlTypeSpecifier::AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name,
267 bool is_array,
Jiyong Park1deecc32018-07-17 01:14:41 +0900268 vector<unique_ptr<AidlTypeSpecifier>>* type_params,
Steven Moreland46e9da82018-07-27 15:45:29 -0700269 const string& comments)
270 : AidlAnnotatable(location),
271 unresolved_name_(unresolved_name),
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700272 is_array_(is_array),
Jiyong Park1deecc32018-07-17 01:14:41 +0900273 type_params_(type_params),
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900274 comments_(comments),
275 split_name_(Split(unresolved_name, ".")) {}
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700276
Steven Moreland3f658cf2018-08-20 13:40:54 -0700277AidlTypeSpecifier AidlTypeSpecifier::ArrayBase() const {
278 AIDL_FATAL_IF(!is_array_, this);
279
280 AidlTypeSpecifier arrayBase = *this;
281 arrayBase.is_array_ = false;
282 return arrayBase;
283}
284
Jiyong Park1deecc32018-07-17 01:14:41 +0900285string AidlTypeSpecifier::ToString() const {
286 string ret = GetName();
287 if (IsGeneric()) {
288 vector<string> arg_names;
289 for (const auto& ta : GetTypeParameters()) {
Jiyong Parkccf00f82018-07-17 01:39:23 +0900290 arg_names.emplace_back(ta->ToString());
291 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900292 ret += "<" + Join(arg_names, ",") + ">";
Jiyong Parkccf00f82018-07-17 01:39:23 +0900293 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900294 if (IsArray()) {
295 ret += "[]";
296 }
297 return ret;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900298}
299
Jiyong Park02da7422018-07-16 16:00:26 +0900300string AidlTypeSpecifier::Signature() const {
301 string ret = ToString();
302 string annotations = AidlAnnotatable::ToString();
303 if (annotations != "") {
304 ret = annotations + " " + ret;
305 }
306 return ret;
307}
308
Jiyong Park1deecc32018-07-17 01:14:41 +0900309bool AidlTypeSpecifier::Resolve(android::aidl::AidlTypenames& typenames) {
Steven Moreland9731c632019-08-13 10:21:08 -0700310 CHECK(!IsResolved());
Jiyong Park1deecc32018-07-17 01:14:41 +0900311 pair<string, bool> result = typenames.ResolveTypename(unresolved_name_);
312 if (result.second) {
313 fully_qualified_name_ = result.first;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900314 split_name_ = Split(fully_qualified_name_, ".");
Jiyong Parkccf00f82018-07-17 01:39:23 +0900315 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900316 return result.second;
Casey Dahlin70078e62015-09-30 17:01:30 -0700317}
318
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900319bool AidlTypeSpecifier::CheckValid(const AidlTypenames& typenames) const {
Andrei Onea9445fc62019-06-27 18:11:59 +0100320 if (!CheckValidAnnotations()) {
321 return false;
322 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900323 if (IsGeneric()) {
324 const string& type_name = GetName();
325 const int num = GetTypeParameters().size();
326 if (type_name == "List") {
327 if (num > 1) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900328 AIDL_ERROR(this) << " List cannot have type parameters more than one, but got "
329 << "'" << ToString() << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900330 return false;
331 }
332 } else if (type_name == "Map") {
333 if (num != 0 && num != 2) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900334 AIDL_ERROR(this) << "Map must have 0 or 2 type parameters, but got "
335 << "'" << ToString() << "'";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900336 return false;
337 }
338 }
339 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900340
341 if (GetName() == "void") {
342 if (IsArray() || IsNullable() || IsUtf8InCpp()) {
343 AIDL_ERROR(this) << "void type cannot be an array or nullable or utf8 string";
344 return false;
345 }
346 }
347
348 if (IsArray()) {
349 const auto definedType = typenames.TryGetDefinedType(GetName());
350 if (definedType != nullptr && definedType->AsInterface() != nullptr) {
351 AIDL_ERROR(this) << "Binder type cannot be an array";
352 return false;
353 }
354 }
355
356 if (IsNullable()) {
357 if (AidlTypenames::IsPrimitiveTypename(GetName()) && !IsArray()) {
358 AIDL_ERROR(this) << "Primitive type cannot get nullable annotation";
359 return false;
360 }
361 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900362 return true;
363}
364
Steven Moreland860b1942018-08-16 14:59:28 -0700365std::string AidlConstantValueDecorator(const AidlTypeSpecifier& /*type*/,
366 const std::string& raw_value) {
367 return raw_value;
368}
369
Steven Moreland46e9da82018-07-27 15:45:29 -0700370AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
371 AidlTypeSpecifier* type, const std::string& name)
372 : AidlVariableDeclaration(location, type, name, nullptr /*default_value*/) {}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700373
Steven Moreland46e9da82018-07-27 15:45:29 -0700374AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
375 AidlTypeSpecifier* type, const std::string& name,
376 AidlConstantValue* default_value)
377 : AidlNode(location), type_(type), name_(name), default_value_(default_value) {}
Steven Moreland9ea10e32018-07-19 15:26:09 -0700378
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900379bool AidlVariableDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700380 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900381 valid &= type_->CheckValid(typenames);
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900382
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900383 if (default_value_ == nullptr) return valid;
Steven Moreland25294322018-08-07 18:13:55 -0700384 valid &= default_value_->CheckValid();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700385
Steven Moreland25294322018-08-07 18:13:55 -0700386 if (!valid) return false;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700387
Steven Moreland860b1942018-08-16 14:59:28 -0700388 return !ValueString(AidlConstantValueDecorator).empty();
Steven Moreland9ea10e32018-07-19 15:26:09 -0700389}
Steven Moreland5557f1c2018-07-02 13:50:23 -0700390
391string AidlVariableDeclaration::ToString() const {
Jeongik Cha3271ffa2018-12-04 15:19:20 +0900392 string ret = type_->Signature() + " " + name_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700393 if (default_value_ != nullptr) {
Steven Moreland860b1942018-08-16 14:59:28 -0700394 ret += " = " + ValueString(AidlConstantValueDecorator);
Steven Moreland9ea10e32018-07-19 15:26:09 -0700395 }
396 return ret;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700397}
398
Jiyong Park02da7422018-07-16 16:00:26 +0900399string AidlVariableDeclaration::Signature() const {
400 return type_->Signature() + " " + name_;
401}
402
Steven Moreland860b1942018-08-16 14:59:28 -0700403std::string AidlVariableDeclaration::ValueString(const ConstantValueDecorator& decorator) const {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900404 if (default_value_ != nullptr) {
405 return GetDefaultValue()->As(GetType(), decorator);
406 } else {
407 return "";
408 }
Steven Moreland25294322018-08-07 18:13:55 -0700409}
410
Steven Moreland46e9da82018-07-27 15:45:29 -0700411AidlArgument::AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
412 AidlTypeSpecifier* type, const std::string& name)
413 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700414 direction_(direction),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700415 direction_specified_(true) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700416
Steven Moreland46e9da82018-07-27 15:45:29 -0700417AidlArgument::AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type,
418 const std::string& name)
419 : AidlVariableDeclaration(location, type, name),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700420 direction_(AidlArgument::IN_DIR),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700421 direction_specified_(false) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700422
Jiyong Park02da7422018-07-16 16:00:26 +0900423string AidlArgument::GetDirectionSpecifier() const {
Casey Dahlinc378c992015-09-29 16:50:40 -0700424 string ret;
Casey Dahlinc378c992015-09-29 16:50:40 -0700425 if (direction_specified_) {
426 switch(direction_) {
427 case AidlArgument::IN_DIR:
428 ret += "in ";
429 break;
430 case AidlArgument::OUT_DIR:
431 ret += "out ";
432 break;
433 case AidlArgument::INOUT_DIR:
434 ret += "inout ";
435 break;
436 }
437 }
Casey Dahlinc378c992015-09-29 16:50:40 -0700438 return ret;
439}
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700440
Jiyong Park02da7422018-07-16 16:00:26 +0900441string AidlArgument::ToString() const {
442 return GetDirectionSpecifier() + AidlVariableDeclaration::ToString();
443}
444
445std::string AidlArgument::Signature() const {
Steven Moreland46e9da82018-07-27 15:45:29 -0700446 class AidlInterface;
447 class AidlInterface;
448 class AidlParcelable;
449 class AidlStructuredParcelable;
450 class AidlParcelable;
451 class AidlStructuredParcelable;
Jiyong Park02da7422018-07-16 16:00:26 +0900452 return GetDirectionSpecifier() + AidlVariableDeclaration::Signature();
453}
454
Steven Moreland46e9da82018-07-27 15:45:29 -0700455AidlMember::AidlMember(const AidlLocation& location) : AidlNode(location) {}
456
Steven Moreland46e9da82018-07-27 15:45:29 -0700457AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
458 const std::string& checked_value)
Steven Moreland1c4ba202018-08-09 10:49:54 -0700459 : AidlNode(location), type_(type), value_(checked_value) {
460 CHECK(!value_.empty() || type_ == Type::ERROR);
Steven Moreland860b1942018-08-16 14:59:28 -0700461 CHECK(type_ != Type::ARRAY);
Steven Moreland1c4ba202018-08-09 10:49:54 -0700462}
Steven Moreland693640b2018-07-19 13:46:27 -0700463
Steven Moreland860b1942018-08-16 14:59:28 -0700464AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type,
465 std::vector<std::unique_ptr<AidlConstantValue>>* values)
466 : AidlNode(location), type_(type), values_(std::move(*values)) {}
467
Steven Moreland25294322018-08-07 18:13:55 -0700468static bool isValidLiteralChar(char c) {
469 return !(c <= 0x1f || // control characters are < 0x20
470 c >= 0x7f || // DEL is 0x7f
471 c == '\\'); // Disallow backslashes for future proofing.
Steven Moreland693640b2018-07-19 13:46:27 -0700472}
473
Steven Moreland25294322018-08-07 18:13:55 -0700474AidlConstantValue* AidlConstantValue::Boolean(const AidlLocation& location, bool value) {
475 return new AidlConstantValue(location, Type::BOOLEAN, value ? "true" : "false");
476}
477
478AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location, char value) {
479 if (!isValidLiteralChar(value)) {
480 AIDL_ERROR(location) << "Invalid character literal " << value;
Steven Moreland46e9da82018-07-27 15:45:29 -0700481 return new AidlConstantValue(location, Type::ERROR, "");
Steven Moreland693640b2018-07-19 13:46:27 -0700482 }
Steven Moreland25294322018-08-07 18:13:55 -0700483 return new AidlConstantValue(location, Type::CHARACTER, std::string("'") + value + "'");
Steven Moreland693640b2018-07-19 13:46:27 -0700484}
485
Steven Moreland1c4ba202018-08-09 10:49:54 -0700486AidlConstantValue* AidlConstantValue::Floating(const AidlLocation& location,
487 const std::string& value) {
488 return new AidlConstantValue(location, Type::FLOATING, value);
489}
490
Steven Moreland25294322018-08-07 18:13:55 -0700491AidlConstantValue* AidlConstantValue::Hex(const AidlLocation& location, const std::string& value) {
492 return new AidlConstantValue(location, Type::HEXIDECIMAL, value);
493}
494
495AidlConstantValue* AidlConstantValue::Integral(const AidlLocation& location,
496 const std::string& value) {
497 return new AidlConstantValue(location, Type::INTEGRAL, value);
498}
499
Steven Moreland860b1942018-08-16 14:59:28 -0700500AidlConstantValue* AidlConstantValue::Array(
501 const AidlLocation& location, std::vector<std::unique_ptr<AidlConstantValue>>* values) {
502 return new AidlConstantValue(location, Type::ARRAY, values);
503}
504
Steven Moreland25294322018-08-07 18:13:55 -0700505AidlConstantValue* AidlConstantValue::String(const AidlLocation& location,
506 const std::string& value) {
Steven Moreland693640b2018-07-19 13:46:27 -0700507 for (size_t i = 0; i < value.length(); ++i) {
Steven Moreland25294322018-08-07 18:13:55 -0700508 if (!isValidLiteralChar(value[i])) {
Steven Moreland46e9da82018-07-27 15:45:29 -0700509 AIDL_ERROR(location) << "Found invalid character at index " << i << " in string constant '"
510 << value << "'";
511 return new AidlConstantValue(location, Type::ERROR, "");
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700512 }
513 }
Steven Moreland693640b2018-07-19 13:46:27 -0700514
Steven Moreland46e9da82018-07-27 15:45:29 -0700515 return new AidlConstantValue(location, Type::STRING, value);
Steven Moreland693640b2018-07-19 13:46:27 -0700516}
517
Steven Moreland25294322018-08-07 18:13:55 -0700518bool AidlConstantValue::CheckValid() const {
519 // error always logged during creation
520 return type_ != AidlConstantValue::Type::ERROR;
521}
522
Steven Moreland1c4ba202018-08-09 10:49:54 -0700523static string TrimIfSuffix(const string& str, const string& suffix) {
524 if (str.size() > suffix.size() &&
525 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix)) {
526 return str.substr(0, str.size() - suffix.size());
527 }
528 return str;
529}
530
Steven Moreland860b1942018-08-16 14:59:28 -0700531string AidlConstantValue::As(const AidlTypeSpecifier& type,
532 const ConstantValueDecorator& decorator) const {
533 if (type.IsGeneric()) {
534 AIDL_ERROR(type) << "Generic type cannot be specified with a constant literal.";
535 return "";
536 }
537
538 const std::string& type_string = type.GetName();
539
540 if ((type_ == Type::ARRAY) != type.IsArray()) {
541 goto mismatch_error;
542 }
Steven Moreland25294322018-08-07 18:13:55 -0700543
544 switch (type_) {
Steven Moreland860b1942018-08-16 14:59:28 -0700545 case AidlConstantValue::Type::ARRAY: {
546 vector<string> raw_values;
547 raw_values.reserve(values_.size());
548
549 bool success = true;
550 for (const auto& value : values_) {
551 const AidlTypeSpecifier& array_base = type.ArrayBase();
552 const std::string raw_value = value->As(array_base, decorator);
553
554 success &= !raw_value.empty();
555 raw_values.push_back(decorator(array_base, raw_value));
556 }
557 if (!success) {
558 AIDL_ERROR(this) << "Default value must be a literal array of " << type_string << ".";
559 return "";
560 }
561 return decorator(type, "{" + Join(raw_values, ", ") + "}");
562 }
Steven Moreland1c4ba202018-08-09 10:49:54 -0700563 case AidlConstantValue::Type::BOOLEAN:
Steven Moreland860b1942018-08-16 14:59:28 -0700564 if (type_string == "boolean") return decorator(type, value_);
Steven Moreland1c4ba202018-08-09 10:49:54 -0700565 goto mismatch_error;
566 case AidlConstantValue::Type::CHARACTER:
Steven Moreland860b1942018-08-16 14:59:28 -0700567 if (type_string == "char") return decorator(type, value_);
Steven Moreland1c4ba202018-08-09 10:49:54 -0700568 goto mismatch_error;
569 case AidlConstantValue::Type::FLOATING: {
570 bool is_float_literal = value_.back() == 'f';
571 const std::string raw_value = TrimIfSuffix(value_, "f");
572
573 if (type_string == "double") {
574 double parsed_value;
575 if (!android::base::ParseDouble(raw_value, &parsed_value)) goto parse_error;
Steven Moreland860b1942018-08-16 14:59:28 -0700576 return decorator(type, std::to_string(parsed_value));
Steven Moreland25294322018-08-07 18:13:55 -0700577 }
Steven Moreland1c4ba202018-08-09 10:49:54 -0700578 if (is_float_literal && type_string == "float") {
579 float parsed_value;
580 if (!android::base::ParseFloat(raw_value, &parsed_value)) goto parse_error;
Steven Moreland860b1942018-08-16 14:59:28 -0700581 return decorator(type, std::to_string(parsed_value) + "f");
Steven Moreland25294322018-08-07 18:13:55 -0700582 }
583 goto mismatch_error;
Steven Moreland1c4ba202018-08-09 10:49:54 -0700584 }
Steven Moreland25294322018-08-07 18:13:55 -0700585 case AidlConstantValue::Type::HEXIDECIMAL:
586 // For historical reasons, a hexidecimal int needs to have the specified bits interpreted
587 // as the signed type, so the other types are made consistent with it.
588 if (type_string == "byte") {
589 uint8_t unsigned_value;
590 if (!android::base::ParseUint<uint8_t>(value_, &unsigned_value)) goto parse_error;
Steven Moreland860b1942018-08-16 14:59:28 -0700591 return decorator(type, std::to_string((int8_t)unsigned_value));
Steven Moreland25294322018-08-07 18:13:55 -0700592 }
593 if (type_string == "int") {
594 uint32_t unsigned_value;
595 if (!android::base::ParseUint<uint32_t>(value_, &unsigned_value)) goto parse_error;
Steven Moreland860b1942018-08-16 14:59:28 -0700596 return decorator(type, std::to_string((int32_t)unsigned_value));
Steven Moreland25294322018-08-07 18:13:55 -0700597 }
598 if (type_string == "long") {
599 uint64_t unsigned_value;
600 if (!android::base::ParseUint<uint64_t>(value_, &unsigned_value)) goto parse_error;
Steven Moreland860b1942018-08-16 14:59:28 -0700601 return decorator(type, std::to_string((int64_t)unsigned_value));
Steven Moreland25294322018-08-07 18:13:55 -0700602 }
603 goto mismatch_error;
Steven Moreland1c4ba202018-08-09 10:49:54 -0700604 case AidlConstantValue::Type::INTEGRAL:
605 if (type_string == "byte") {
606 if (!android::base::ParseInt<int8_t>(value_, nullptr)) goto parse_error;
Steven Moreland860b1942018-08-16 14:59:28 -0700607 return decorator(type, value_);
Steven Moreland1c4ba202018-08-09 10:49:54 -0700608 }
609 if (type_string == "int") {
610 if (!android::base::ParseInt<int32_t>(value_, nullptr)) goto parse_error;
Steven Moreland860b1942018-08-16 14:59:28 -0700611 return decorator(type, value_);
Steven Moreland1c4ba202018-08-09 10:49:54 -0700612 }
613 if (type_string == "long") {
614 if (!android::base::ParseInt<int64_t>(value_, nullptr)) goto parse_error;
Steven Moreland860b1942018-08-16 14:59:28 -0700615 return decorator(type, value_);
Steven Moreland1c4ba202018-08-09 10:49:54 -0700616 }
Steven Moreland25294322018-08-07 18:13:55 -0700617 goto mismatch_error;
618 case AidlConstantValue::Type::STRING:
Steven Moreland860b1942018-08-16 14:59:28 -0700619 if (type_string == "String") return decorator(type, value_);
Steven Moreland25294322018-08-07 18:13:55 -0700620 goto mismatch_error;
621 default:
622 AIDL_FATAL(this) << "Unrecognized constant value type";
623 }
624
625mismatch_error:
Steven Moreland860b1942018-08-16 14:59:28 -0700626 AIDL_ERROR(this) << "Expecting type " << type_string << " but constant is " << ToString(type_);
Steven Moreland25294322018-08-07 18:13:55 -0700627 return "";
628parse_error:
629 AIDL_ERROR(this) << "Could not parse " << value_ << " as " << type_string;
630 return "";
631}
632
633string AidlConstantValue::ToString(Type type) {
634 switch (type) {
Steven Moreland860b1942018-08-16 14:59:28 -0700635 case Type::ARRAY:
636 return "a literal array";
Steven Moreland25294322018-08-07 18:13:55 -0700637 case Type::BOOLEAN:
638 return "a literal boolean";
639 case Type::CHARACTER:
640 return "a literal char";
Steven Moreland1c4ba202018-08-09 10:49:54 -0700641 case Type::FLOATING:
642 return "a floating-point literal";
643 case Type::HEXIDECIMAL:
644 return "a hexidecimal literal";
Steven Moreland25294322018-08-07 18:13:55 -0700645 case Type::INTEGRAL:
646 return "an integral literal";
647 case Type::STRING:
648 return "a literal string";
649 case Type::ERROR:
650 LOG(FATAL) << "aidl internal error: error type failed to halt program";
Wei Wang35b7bb62018-10-09 13:06:12 -0700651 return "";
Steven Moreland25294322018-08-07 18:13:55 -0700652 default:
653 LOG(FATAL) << "aidl internal error: unknown constant type: " << static_cast<int>(type);
654 return ""; // not reached
655 }
Steven Moreland693640b2018-07-19 13:46:27 -0700656}
657
Steven Moreland46e9da82018-07-27 15:45:29 -0700658AidlConstantDeclaration::AidlConstantDeclaration(const AidlLocation& location,
659 AidlTypeSpecifier* type, const std::string& name,
660 AidlConstantValue* value)
661 : AidlMember(location), type_(type), name_(name), value_(value) {}
Steven Moreland693640b2018-07-19 13:46:27 -0700662
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900663bool AidlConstantDeclaration::CheckValid(const AidlTypenames& typenames) const {
Steven Moreland25294322018-08-07 18:13:55 -0700664 bool valid = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900665 valid &= type_->CheckValid(typenames);
Steven Moreland25294322018-08-07 18:13:55 -0700666 valid &= value_->CheckValid();
667 if (!valid) return false;
Steven Moreland693640b2018-07-19 13:46:27 -0700668
Steven Moreland25294322018-08-07 18:13:55 -0700669 const static set<string> kSupportedConstTypes = {"String", "int"};
670 if (kSupportedConstTypes.find(type_->ToString()) == kSupportedConstTypes.end()) {
671 AIDL_ERROR(this) << "Constant of type " << type_->ToString() << " is not supported.";
Steven Moreland693640b2018-07-19 13:46:27 -0700672 return false;
673 }
674
Steven Moreland860b1942018-08-16 14:59:28 -0700675 return !ValueString(AidlConstantValueDecorator).empty();
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700676}
677
Jiyong Parka428d212018-08-29 22:26:30 +0900678string AidlConstantDeclaration::ToString() const {
679 return "const " + type_->ToString() + " " + name_ + " = " +
680 ValueString(AidlConstantValueDecorator);
681}
682
683string AidlConstantDeclaration::Signature() const {
684 return type_->Signature() + " " + name_;
685}
686
Steven Moreland46e9da82018-07-27 15:45:29 -0700687AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
688 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900689 const std::string& comments)
690 : AidlMethod(location, oneway, type, name, args, comments, 0, true) {
691 has_id_ = false;
692}
693
694AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
695 const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
Jiyong Parkb034bf02018-07-30 17:44:33 +0900696 const std::string& comments, int id, bool is_user_defined)
Steven Moreland46e9da82018-07-27 15:45:29 -0700697 : AidlMember(location),
698 oneway_(oneway),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700699 comments_(comments),
700 type_(type),
701 name_(name),
Casey Dahlinf4a93112015-10-05 16:58:09 -0700702 arguments_(std::move(*args)),
Jiyong Parkb034bf02018-07-30 17:44:33 +0900703 id_(id),
704 is_user_defined_(is_user_defined) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700705 has_id_ = true;
706 delete args;
Christopher Wileyad339272015-10-05 19:11:58 -0700707 for (const unique_ptr<AidlArgument>& a : arguments_) {
708 if (a->IsIn()) { in_arguments_.push_back(a.get()); }
709 if (a->IsOut()) { out_arguments_.push_back(a.get()); }
710 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700711}
712
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700713
Jiyong Park02da7422018-07-16 16:00:26 +0900714string AidlMethod::Signature() const {
715 vector<string> arg_signatures;
716 for (const auto& arg : GetArguments()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900717 arg_signatures.emplace_back(arg->GetType().ToString());
Jiyong Park02da7422018-07-16 16:00:26 +0900718 }
Jiyong Park309668e2018-07-28 16:55:44 +0900719 return GetName() + "(" + Join(arg_signatures, ", ") + ")";
720}
721
722string AidlMethod::ToString() const {
723 vector<string> arg_strings;
724 for (const auto& arg : GetArguments()) {
725 arg_strings.emplace_back(arg->Signature());
726 }
Steven Moreland4ee68632018-12-14 15:52:46 -0800727 string ret = (IsOneway() ? "oneway " : "") + GetType().Signature() + " " + GetName() + "(" +
728 Join(arg_strings, ", ") + ")";
Jiyong Parked65bf42018-08-28 15:43:27 +0900729 if (HasId()) {
730 ret += " = " + std::to_string(GetId());
731 }
732 return ret;
Jiyong Park02da7422018-07-16 16:00:26 +0900733}
734
Steven Moreland46e9da82018-07-27 15:45:29 -0700735AidlDefinedType::AidlDefinedType(const AidlLocation& location, const std::string& name,
736 const std::string& comments,
Steven Moreland787b0432018-07-03 09:00:58 -0700737 const std::vector<std::string>& package)
Steven Moreland46e9da82018-07-27 15:45:29 -0700738 : AidlAnnotatable(location), name_(name), comments_(comments), package_(package) {}
Steven Moreland787b0432018-07-03 09:00:58 -0700739
740std::string AidlDefinedType::GetPackage() const {
741 return Join(package_, '.');
742}
743
744std::string AidlDefinedType::GetCanonicalName() const {
745 if (package_.empty()) {
746 return GetName();
747 }
748 return GetPackage() + "." + GetName();
749}
750
Steven Moreland46e9da82018-07-27 15:45:29 -0700751AidlParcelable::AidlParcelable(const AidlLocation& location, AidlQualifiedName* name,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900752 const std::vector<std::string>& package, const std::string& comments,
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800753 const std::string& cpp_header)
Jiyong Parka6605ab2018-11-11 14:30:21 +0900754 : AidlDefinedType(location, name->GetDotName(), comments, package),
Steven Moreland787b0432018-07-03 09:00:58 -0700755 name_(name),
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800756 cpp_header_(cpp_header) {
757 // Strip off quotation marks if we actually have a cpp header.
758 if (cpp_header_.length() >= 2) {
759 cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2);
760 }
Casey Dahlin59401da2015-10-09 18:16:45 -0700761}
762
Jeongik Cha82317dd2019-02-27 20:26:11 +0900763bool AidlParcelable::CheckValid(const AidlTypenames&) const {
764 static const std::set<string> allowed{kStableParcelable};
Andrei Onea9445fc62019-06-27 18:11:59 +0100765 if (!CheckValidAnnotations()) {
766 return false;
767 }
Jeongik Cha82317dd2019-02-27 20:26:11 +0900768 for (const auto& v : GetAnnotations()) {
769 if (allowed.find(v.GetName()) == allowed.end()) {
770 std::ostringstream stream;
771 stream << "Unstructured parcelable can contain only";
772 for (const string& kv : allowed) {
773 stream << " " << kv;
774 }
775 stream << ".";
776 AIDL_ERROR(this) << stream.str();
777 return false;
778 }
779 }
780
781 return true;
782}
783
Jiyong Park02da7422018-07-16 16:00:26 +0900784void AidlParcelable::Write(CodeWriter* writer) const {
785 writer->Write("parcelable %s ;\n", GetName().c_str());
786}
787
Steven Moreland5557f1c2018-07-02 13:50:23 -0700788AidlStructuredParcelable::AidlStructuredParcelable(
Steven Moreland46e9da82018-07-27 15:45:29 -0700789 const AidlLocation& location, AidlQualifiedName* name, const std::vector<std::string>& package,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900790 const std::string& comments, std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables)
791 : AidlParcelable(location, name, package, comments, "" /*cpp_header*/),
Steven Moreland46e9da82018-07-27 15:45:29 -0700792 variables_(std::move(*variables)) {}
Steven Moreland5557f1c2018-07-02 13:50:23 -0700793
Jiyong Park02da7422018-07-16 16:00:26 +0900794void AidlStructuredParcelable::Write(CodeWriter* writer) const {
795 writer->Write("parcelable %s {\n", GetName().c_str());
796 writer->Indent();
797 for (const auto& field : GetFields()) {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900798 writer->Write("%s;\n", field->ToString().c_str());
Jiyong Park02da7422018-07-16 16:00:26 +0900799 }
800 writer->Dedent();
801 writer->Write("}\n");
802}
803
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900804bool AidlStructuredParcelable::CheckValid(const AidlTypenames& typenames) const {
Daniel Norman85aed542019-08-21 12:01:14 -0700805 bool success = true;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900806 for (const auto& v : GetFields()) {
Daniel Norman85aed542019-08-21 12:01:14 -0700807 success = success && v->CheckValid(typenames);
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900808 }
Daniel Norman85aed542019-08-21 12:01:14 -0700809 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900810}
811
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900812// TODO: we should treat every backend all the same in future.
813bool AidlTypeSpecifier::LanguageSpecificCheckValid(Options::Language lang) const {
814 if (lang == Options::Language::CPP) {
815 if (this->GetName() == "List" && !this->IsGeneric()) {
816 AIDL_ERROR(this) << "List without type isn't supported in cpp.";
817 return false;
818 }
819 }
820 if (this->IsGeneric()) {
821 if (this->GetName() == "List") {
822 if (this->GetTypeParameters().size() != 1) {
823 AIDL_ERROR(this) << "List must have only one type parameter.";
824 return false;
825 }
826 if (lang == Options::Language::CPP) {
827 auto& name = this->GetTypeParameters()[0]->GetName();
828 if (!(name == "String" || name == "IBinder")) {
829 AIDL_ERROR(this) << "List in cpp supports only string and IBinder for now.";
830 return false;
831 }
832 } else if (lang == Options::Language::NDK) {
833 AIDL_ERROR(this) << "NDK backend does not support List yet.";
834 return false;
835 }
836
837 } else if (this->GetName() == "Map") {
838 if (lang != Options::Language::JAVA) {
839 AIDL_ERROR(this) << "Currently, only Java backend supports Map.";
840 return false;
841 }
842 }
843 }
844 return true;
845}
846
847// TODO: we should treat every backend all the same in future.
848bool AidlParcelable::LanguageSpecificCheckValid(Options::Language lang) const {
849 if (lang != Options::Language::JAVA) {
850 if (this->IsStableParcelable()) {
851 AIDL_ERROR(this) << "@JavaOnlyStableParcelable supports only Java target.";
852 return false;
853 }
854 const AidlParcelable* unstructuredParcelable = this->AsUnstructuredParcelable();
855 if (unstructuredParcelable != nullptr) {
856 if (unstructuredParcelable->GetCppHeader().empty()) {
857 AIDL_ERROR(unstructuredParcelable)
858 << "Unstructured parcelable must have C++ header defined.";
859 return false;
860 }
861 }
862 }
863 return true;
864}
865
866// TODO: we should treat every backend all the same in future.
867bool AidlStructuredParcelable::LanguageSpecificCheckValid(Options::Language lang) const {
868 if (!AidlParcelable::LanguageSpecificCheckValid(lang)) {
869 return false;
870 }
871 for (const auto& v : this->GetFields()) {
872 if (!v->GetType().LanguageSpecificCheckValid(lang)) {
873 return false;
874 }
875 }
876 return true;
877}
878
Daniel Norman85aed542019-08-21 12:01:14 -0700879AidlEnumerator::AidlEnumerator(const AidlLocation& location, const std::string& name,
880 AidlConstantValue* value)
881 : AidlNode(location), name_(name), value_(value) {}
882
883bool AidlEnumerator::CheckValid(const AidlTypeSpecifier& enum_backing_type) const {
884 if (GetValue() == nullptr) {
885 return false;
886 }
887 if (!GetValue()->CheckValid()) {
888 return false;
889 }
890 if (GetValue()->As(enum_backing_type, AidlConstantValueDecorator).empty()) {
891 AIDL_ERROR(this) << "Enumerator type differs from enum backing type.";
892 return false;
893 }
894 return true;
895}
896
897string AidlEnumerator::ValueString(const AidlTypeSpecifier& backing_type,
898 const ConstantValueDecorator& decorator) const {
899 return GetValue()->As(backing_type, decorator);
900}
901
902AidlEnumDeclaration::AidlEnumDeclaration(const AidlLocation& location, const std::string& name,
903 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
904 const std::vector<std::string>& package)
905 : AidlDefinedType(location, name, "", package), enumerators_(std::move(*enumerators)) {}
906
907void AidlEnumDeclaration::SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type) {
908 backing_type_ = std::move(type);
909}
910
911bool AidlEnumDeclaration::CheckValid(const AidlTypenames&) const {
912 if (backing_type_ == nullptr) {
913 AIDL_ERROR(this) << "Enum declaration missing backing type.";
914 return false;
915 }
916 bool success = true;
917 for (const auto& enumerator : enumerators_) {
918 success = success && enumerator->CheckValid(GetBackingType());
919 }
920 return success;
921}
922
923void AidlEnumDeclaration::Write(CodeWriter* writer) const {
924 writer->Write(AidlAnnotatable::ToString().c_str());
925 writer->Write("enum %s {", GetName().c_str());
926 writer->Indent();
927 for (const auto& enumerator : GetEnumerators()) {
928 // TODO(b/123321528): After autofilling is supported, determine if we want
929 // to leave out the assigned value for enumerators that were autofilled.
930 writer->Write("%s = %s,\n", enumerator->GetName().c_str(),
931 enumerator->ValueString(GetBackingType(), AidlConstantValueDecorator).c_str());
932 }
933 writer->Dedent();
934 writer->Write("}\n");
935}
936
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900937// TODO: we should treat every backend all the same in future.
938bool AidlInterface::LanguageSpecificCheckValid(Options::Language lang) const {
939 for (const auto& m : this->GetMethods()) {
940 if (!m->GetType().LanguageSpecificCheckValid(lang)) {
941 return false;
942 }
943 for (const auto& arg : m->GetArguments()) {
944 if (!arg->GetType().LanguageSpecificCheckValid(lang)) {
945 return false;
946 }
947 }
948 }
949 return true;
950}
951
Steven Moreland46e9da82018-07-27 15:45:29 -0700952AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name,
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700953 const std::string& comments, bool oneway,
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800954 std::vector<std::unique_ptr<AidlMember>>* members,
Christopher Wileyd76067c2015-10-19 17:00:13 -0700955 const std::vector<std::string>& package)
Steven Morelandacd53472018-12-14 10:17:26 -0800956 : AidlDefinedType(location, name, comments, package) {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800957 for (auto& member : *members) {
958 AidlMember* local = member.release();
959 AidlMethod* method = local->AsMethod();
Steven Moreland693640b2018-07-19 13:46:27 -0700960 AidlConstantDeclaration* constant = local->AsConstantDeclaration();
961
962 CHECK(method == nullptr || constant == nullptr);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800963
964 if (method) {
Steven Moreland8c70ba92018-12-17 10:20:31 -0800965 method->ApplyInterfaceOneway(oneway);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800966 methods_.emplace_back(method);
Steven Moreland693640b2018-07-19 13:46:27 -0700967 } else if (constant) {
968 constants_.emplace_back(constant);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800969 } else {
Steven Moreland46e9da82018-07-27 15:45:29 -0700970 AIDL_FATAL(this) << "Member is neither method nor constant!";
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800971 }
972 }
973
974 delete members;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700975}
976
Jiyong Park02da7422018-07-16 16:00:26 +0900977void AidlInterface::Write(CodeWriter* writer) const {
978 writer->Write("interface %s {\n", GetName().c_str());
979 writer->Indent();
980 for (const auto& method : GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +0900981 writer->Write("%s;\n", method->ToString().c_str());
Jiyong Park02da7422018-07-16 16:00:26 +0900982 }
Jiyong Parka428d212018-08-29 22:26:30 +0900983 for (const auto& constdecl : GetConstantDeclarations()) {
984 writer->Write("%s;\n", constdecl->ToString().c_str());
985 }
Jiyong Park02da7422018-07-16 16:00:26 +0900986 writer->Dedent();
987 writer->Write("}\n");
988}
989
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900990bool AidlInterface::CheckValid(const AidlTypenames& typenames) const {
Andrei Onea9445fc62019-06-27 18:11:59 +0100991 if (!CheckValidAnnotations()) {
992 return false;
993 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900994 // Has to be a pointer due to deleting copy constructor. No idea why.
995 map<string, const AidlMethod*> method_names;
996 for (const auto& m : GetMethods()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900997 if (!m->GetType().CheckValid(typenames)) {
998 return false;
999 }
1000
Steven Morelandacd53472018-12-14 10:17:26 -08001001 if (m->IsOneway() && m->GetType().GetName() != "void") {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001002 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot return a value";
1003 return false;
1004 }
1005
1006 set<string> argument_names;
1007 for (const auto& arg : m->GetArguments()) {
1008 auto it = argument_names.find(arg->GetName());
1009 if (it != argument_names.end()) {
1010 AIDL_ERROR(m) << "method '" << m->GetName() << "' has duplicate argument name '"
1011 << arg->GetName() << "'";
1012 return false;
1013 }
1014 argument_names.insert(arg->GetName());
1015
1016 if (!arg->GetType().CheckValid(typenames)) {
1017 return false;
1018 }
1019
Steven Morelandacd53472018-12-14 10:17:26 -08001020 if (m->IsOneway() && arg->IsOut()) {
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001021 AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot have out parameters";
1022 return false;
1023 }
Jeongik Cha047c5ee2019-08-07 23:16:49 +09001024 const bool can_be_out = typenames.CanBeOutParameter(arg->GetType());
1025 if (!arg->DirectionWasSpecified() && can_be_out) {
1026 AIDL_ERROR(arg) << "'" << arg->GetType().ToString()
1027 << "' can be an out type, so you must declare it as in, out, or inout.";
1028 return false;
1029 }
1030
1031 if (arg->GetDirection() != AidlArgument::IN_DIR && !can_be_out) {
1032 AIDL_ERROR(arg) << "'" << arg->ToString() << "' can only be an in parameter.";
1033 return false;
1034 }
1035
1036 // check that the name doesn't match a keyword
1037 if (is_java_keyword(arg->GetName().c_str())) {
1038 AIDL_ERROR(arg) << "Argument name is a Java or aidl keyword";
1039 return false;
1040 }
1041
1042 // Reserve a namespace for internal use
1043 if (android::base::StartsWith(arg->GetName(), "_aidl")) {
1044 AIDL_ERROR(arg) << "Argument name cannot begin with '_aidl'";
1045 return false;
1046 }
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001047 }
1048
1049 auto it = method_names.find(m->GetName());
1050 // prevent duplicate methods
1051 if (it == method_names.end()) {
1052 method_names[m->GetName()] = m.get();
1053 } else {
1054 AIDL_ERROR(m) << "attempt to redefine method " << m->GetName() << ":";
1055 AIDL_ERROR(it->second) << "previously defined here.";
1056 return false;
1057 }
1058
1059 static set<string> reserved_methods{"asBinder()", "getInterfaceVersion()",
1060 "getTransactionName(int)"};
1061
1062 if (reserved_methods.find(m->Signature()) != reserved_methods.end()) {
1063 AIDL_ERROR(m) << " method " << m->Signature() << " is reserved for internal use." << endl;
1064 return false;
1065 }
1066 }
Steven Moreland4d12f9a2018-10-31 14:30:55 -07001067
1068 bool success = true;
1069 set<string> constant_names;
1070 for (const std::unique_ptr<AidlConstantDeclaration>& constant : GetConstantDeclarations()) {
1071 if (constant_names.count(constant->GetName()) > 0) {
1072 LOG(ERROR) << "Found duplicate constant name '" << constant->GetName() << "'";
1073 success = false;
1074 }
1075 constant_names.insert(constant->GetName());
1076 success = success && constant->CheckValid(typenames);
1077 }
1078
1079 return success;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001080}
1081
Steven Moreland46e9da82018-07-27 15:45:29 -07001082AidlQualifiedName::AidlQualifiedName(const AidlLocation& location, const std::string& term,
1083 const std::string& comments)
1084 : AidlNode(location), terms_({term}), comments_(comments) {
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -08001085 if (term.find('.') != string::npos) {
1086 terms_ = Split(term, ".");
Steven Moreland46e9da82018-07-27 15:45:29 -07001087 for (const auto& subterm : terms_) {
1088 if (subterm.empty()) {
1089 AIDL_FATAL(this) << "Malformed qualified identifier: '" << term << "'";
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -08001090 }
1091 }
1092 }
Casey Dahlin2b2879b2015-10-13 16:59:44 -07001093}
1094
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -07001095void AidlQualifiedName::AddTerm(const std::string& term) {
Casey Dahlin2b2879b2015-10-13 16:59:44 -07001096 terms_.push_back(term);
1097}
1098
Steven Moreland46e9da82018-07-27 15:45:29 -07001099AidlImport::AidlImport(const AidlLocation& location, const std::string& needed_class)
1100 : AidlNode(location), needed_class_(needed_class) {}
Casey Dahlin0edf3422015-10-07 12:34:59 -07001101
Steven Moreland64e29be2018-08-08 18:52:19 -07001102std::unique_ptr<Parser> Parser::Parse(const std::string& filename,
1103 const android::aidl::IoDelegate& io_delegate,
1104 AidlTypenames& typenames) {
Christopher Wiley4a2884b2015-10-07 11:27:45 -07001105 // Make sure we can read the file first, before trashing previous state.
Steven Moreland64e29be2018-08-08 18:52:19 -07001106 unique_ptr<string> raw_buffer = io_delegate.GetFileContents(filename);
1107 if (raw_buffer == nullptr) {
Steven Moreland46e9da82018-07-27 15:45:29 -07001108 AIDL_ERROR(filename) << "Error while opening file for parsing";
Steven Moreland64e29be2018-08-08 18:52:19 -07001109 return nullptr;
Christopher Wiley4a2884b2015-10-07 11:27:45 -07001110 }
1111
Christopher Wiley4a2884b2015-10-07 11:27:45 -07001112 // We're going to scan this buffer in place, and yacc demands we put two
1113 // nulls at the end.
Steven Moreland64e29be2018-08-08 18:52:19 -07001114 raw_buffer->append(2u, '\0');
Christopher Wiley4a2884b2015-10-07 11:27:45 -07001115
Steven Moreland64e29be2018-08-08 18:52:19 -07001116 std::unique_ptr<Parser> parser(new Parser(filename, *raw_buffer, typenames));
Christopher Wiley4a2884b2015-10-07 11:27:45 -07001117
Steven Moreland64e29be2018-08-08 18:52:19 -07001118 if (yy::parser(parser.get()).parse() != 0 || parser->HasError()) return nullptr;
Christopher Wiley4a2884b2015-10-07 11:27:45 -07001119
Steven Moreland64e29be2018-08-08 18:52:19 -07001120 return parser;
Christopher Wiley4a2884b2015-10-07 11:27:45 -07001121}
1122
Christopher Wiley90be4e32015-10-20 14:55:25 -07001123std::vector<std::string> Parser::Package() const {
1124 if (!package_) {
1125 return {};
1126 }
1127 return package_->GetTerms();
1128}
1129
Steven Moreland46e9da82018-07-27 15:45:29 -07001130void Parser::AddImport(AidlImport* import) {
1131 imports_.emplace_back(import);
Casey Dahline2507492015-09-14 17:11:20 -07001132}
Jiyong Park1deecc32018-07-17 01:14:41 +09001133
1134bool Parser::Resolve() {
1135 bool success = true;
1136 for (AidlTypeSpecifier* typespec : unresolved_typespecs_) {
Jiyong Park1d2df7d2018-07-23 15:22:50 +09001137 if (!typespec->Resolve(typenames_)) {
Steven Moreland46e9da82018-07-27 15:45:29 -07001138 AIDL_ERROR(typespec) << "Failed to resolve '" << typespec->GetUnresolvedName() << "'";
Jiyong Park1deecc32018-07-17 01:14:41 +09001139 success = false;
1140 // don't stop to show more errors if any
1141 }
1142 }
1143 return success;
1144}
Steven Moreland64e29be2018-08-08 18:52:19 -07001145
1146Parser::Parser(const std::string& filename, std::string& raw_buffer,
1147 android::aidl::AidlTypenames& typenames)
1148 : filename_(filename), typenames_(typenames) {
1149 yylex_init(&scanner_);
1150 buffer_ = yy_scan_buffer(&raw_buffer[0], raw_buffer.length(), scanner_);
1151}
1152
1153Parser::~Parser() {
1154 yy_delete_buffer(buffer_, scanner_);
1155 yylex_destroy(scanner_);
1156}