Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1 | #include "aidl_language.h" |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 2 | #include "aidl_typenames.h" |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 3 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4 | #include <stdio.h> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 5 | #include <stdlib.h> |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 6 | #include <string.h> |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 7 | #include <algorithm> |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 8 | #include <iostream> |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 9 | #include <set> |
| 10 | #include <sstream> |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 11 | #include <string> |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 12 | #include <utility> |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 13 | |
Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 14 | #include <android-base/parsedouble.h> |
Roshan Pius | 9d7810a | 2016-07-28 08:57:50 -0700 | [diff] [blame] | 15 | #include <android-base/parseint.h> |
Elliott Hughes | 0a62067 | 2015-12-04 13:53:18 -0800 | [diff] [blame] | 16 | #include <android-base/strings.h> |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 17 | |
Ying Wang | 3000e75 | 2016-01-11 18:05:59 -0800 | [diff] [blame] | 18 | #include "aidl_language_y.h" |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 19 | #include "logging.h" |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 20 | |
Casey Dahlin | 07b9dde | 2015-09-10 19:13:49 -0700 | [diff] [blame] | 21 | #ifdef _WIN32 |
| 22 | int isatty(int fd) |
| 23 | { |
| 24 | return (fd == 0); |
| 25 | } |
| 26 | #endif |
| 27 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 28 | using android::aidl::IoDelegate; |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 29 | using android::base::Join; |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 30 | using android::base::Split; |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 31 | using std::cerr; |
| 32 | using std::endl; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 33 | using std::pair; |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 34 | using std::set; |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 35 | using std::string; |
| 36 | using std::unique_ptr; |
Jiyong Park | ccf00f8 | 2018-07-17 01:39:23 +0900 | [diff] [blame] | 37 | using std::vector; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 38 | |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 39 | namespace { |
| 40 | bool 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 Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 55 | void yylex_init(void **); |
| 56 | void yylex_destroy(void *); |
| 57 | void yyset_in(FILE *f, void *); |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 58 | int yyparse(Parser*); |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 59 | YY_BUFFER_STATE yy_scan_buffer(char *, size_t, void *); |
Casey Dahlin | 89d4484 | 2015-09-24 18:45:54 -0700 | [diff] [blame] | 60 | void yy_delete_buffer(YY_BUFFER_STATE, void *); |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 61 | |
Casey Dahlin | cdbbc8c | 2015-10-14 15:31:04 -0700 | [diff] [blame] | 62 | AidlToken::AidlToken(const std::string& text, const std::string& comments) |
| 63 | : text_(text), |
| 64 | comments_(comments) {} |
Casey Dahlin | 98a544b | 2015-10-14 14:22:55 -0700 | [diff] [blame] | 65 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 66 | AidlLocation::AidlLocation(const std::string& file, Point begin, Point end) |
| 67 | : file_(file), begin_(begin), end_(end) {} |
| 68 | |
| 69 | std::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 | |
| 78 | AidlNode::AidlNode(const AidlLocation& location) : location_(location) {} |
| 79 | |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 80 | std::string AidlNode::PrintLocation() const { |
| 81 | std::stringstream ss; |
| 82 | ss << location_.file_ << ":" << location_.begin_.line; |
| 83 | return ss.str(); |
| 84 | } |
| 85 | |
Steven Moreland | 92c55f1 | 2018-07-31 14:08:37 -0700 | [diff] [blame] | 86 | AidlError::AidlError(bool fatal) : os_(std::cerr), fatal_(fatal) { |
| 87 | os_ << "ERROR: "; |
| 88 | } |
| 89 | |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 90 | static const string kNullable("nullable"); |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 91 | static const string kUtf8InCpp("utf8InCpp"); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 92 | static const string kVintfStability("VintfStability"); |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 93 | static const string kUnsupportedAppUsage("UnsupportedAppUsage"); |
Jeongik Cha | 698e6af | 2018-12-12 14:39:55 +0900 | [diff] [blame] | 94 | static const string kSystemApi("SystemApi"); |
Jeongik Cha | 82317dd | 2019-02-27 20:26:11 +0900 | [diff] [blame] | 95 | static const string kStableParcelable("JavaOnlyStableParcelable"); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 96 | static const string kBacking("Backing"); |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 97 | |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 98 | static const std::map<string, std::map<std::string, std::string>> kAnnotationParameters{ |
| 99 | {kNullable, {}}, |
| 100 | {kUtf8InCpp, {}}, |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 101 | {kVintfStability, {}}, |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 102 | {kUnsupportedAppUsage, |
| 103 | {{"expectedSignature", "String"}, |
| 104 | {"implicitMember", "String"}, |
| 105 | {"maxTargetSdk", "int"}, |
| 106 | {"publicAlternatives", "String"}, |
| 107 | {"trackingBug", "long"}}}, |
| 108 | {kSystemApi, {}}, |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 109 | {kStableParcelable, {}}, |
| 110 | {kBacking, {{"type", "String"}}}}; |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 111 | |
| 112 | AidlAnnotation* AidlAnnotation::Parse( |
| 113 | const AidlLocation& location, const string& name, |
| 114 | std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list) { |
Steven Moreland | 29db302 | 2019-07-30 12:37:17 -0700 | [diff] [blame] | 115 | if (kAnnotationParameters.find(name) == kAnnotationParameters.end()) { |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 116 | std::ostringstream stream; |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 117 | stream << "'" << name << "' is not a recognized annotation. "; |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 118 | stream << "It must be one of:"; |
Steven Moreland | 29db302 | 2019-07-30 12:37:17 -0700 | [diff] [blame] | 119 | for (const auto& kv : kAnnotationParameters) { |
| 120 | stream << " " << kv.first; |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 121 | } |
| 122 | stream << "."; |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 123 | AIDL_ERROR(location) << stream.str(); |
| 124 | return nullptr; |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 125 | } |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 126 | if (parameter_list == nullptr) { |
| 127 | return new AidlAnnotation(location, name); |
| 128 | } |
| 129 | |
| 130 | return new AidlAnnotation(location, name, std::move(*parameter_list)); |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 131 | } |
| 132 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 133 | AidlAnnotation::AidlAnnotation(const AidlLocation& location, const string& name) |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 134 | : AidlAnnotation(location, name, {}) {} |
| 135 | |
| 136 | AidlAnnotation::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 | |
| 141 | bool 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 | |
| 175 | std::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 Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 187 | |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame^] | 188 | std::string AidlAnnotation::ToString(const ConstantValueDecorator& decorator) const { |
| 189 | if (parameters_.empty()) { |
| 190 | return "@" + GetName(); |
| 191 | } else { |
| 192 | vector<string> param_strings; |
| 193 | for (const auto& [name, value] : AnnotationParams(decorator)) { |
| 194 | param_strings.emplace_back(name + "=" + value); |
| 195 | } |
| 196 | return "@" + GetName() + "(" + Join(param_strings, ", ") + ")"; |
| 197 | } |
| 198 | } |
| 199 | |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 200 | static bool HasAnnotation(const vector<AidlAnnotation>& annotations, const string& name) { |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 201 | for (const auto& a : annotations) { |
Steven Moreland | 3be7577 | 2018-08-20 13:27:43 -0700 | [diff] [blame] | 202 | if (a.GetName() == name) { |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 203 | return true; |
| 204 | } |
| 205 | } |
| 206 | return false; |
| 207 | } |
| 208 | |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 209 | static const AidlAnnotation* GetAnnotation(const vector<AidlAnnotation>& annotations, |
| 210 | const string& name) { |
| 211 | for (const auto& a : annotations) { |
| 212 | if (a.GetName() == name) { |
| 213 | return &a; |
| 214 | } |
| 215 | } |
| 216 | return nullptr; |
| 217 | } |
| 218 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 219 | AidlAnnotatable::AidlAnnotatable(const AidlLocation& location) : AidlNode(location) {} |
| 220 | |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 221 | bool AidlAnnotatable::IsNullable() const { |
| 222 | return HasAnnotation(annotations_, kNullable); |
| 223 | } |
| 224 | |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 225 | bool AidlAnnotatable::IsUtf8InCpp() const { |
| 226 | return HasAnnotation(annotations_, kUtf8InCpp); |
| 227 | } |
| 228 | |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 229 | bool AidlAnnotatable::IsVintfStability() const { |
| 230 | return HasAnnotation(annotations_, kVintfStability); |
| 231 | } |
| 232 | |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 233 | const AidlAnnotation* AidlAnnotatable::UnsupportedAppUsage() const { |
| 234 | return GetAnnotation(annotations_, kUnsupportedAppUsage); |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 235 | } |
| 236 | |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 237 | const AidlTypeSpecifier* AidlAnnotatable::BackingType(const AidlTypenames& typenames) const { |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 238 | auto annotation = GetAnnotation(annotations_, kBacking); |
| 239 | if (annotation != nullptr) { |
| 240 | auto annotation_params = annotation->AnnotationParams(AidlConstantValueDecorator); |
| 241 | if (auto it = annotation_params.find("type"); it != annotation_params.end()) { |
| 242 | const string& type = it->second; |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 243 | AidlTypeSpecifier* type_specifier = |
| 244 | new AidlTypeSpecifier(AIDL_LOCATION_HERE, |
| 245 | // Strip the quotes off the type String. |
| 246 | type.substr(1, type.length() - 2), false, nullptr, ""); |
| 247 | type_specifier->Resolve(typenames); |
| 248 | return type_specifier; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | return nullptr; |
| 252 | } |
| 253 | |
Jeongik Cha | 698e6af | 2018-12-12 14:39:55 +0900 | [diff] [blame] | 254 | bool AidlAnnotatable::IsSystemApi() const { |
| 255 | return HasAnnotation(annotations_, kSystemApi); |
| 256 | } |
| 257 | |
Jeongik Cha | 82317dd | 2019-02-27 20:26:11 +0900 | [diff] [blame] | 258 | bool AidlAnnotatable::IsStableParcelable() const { |
| 259 | return HasAnnotation(annotations_, kStableParcelable); |
| 260 | } |
| 261 | |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 262 | bool AidlAnnotatable::CheckValidAnnotations() const { |
| 263 | for (const auto& annotation : GetAnnotations()) { |
| 264 | if (!annotation.CheckValid()) { |
| 265 | return false; |
| 266 | } |
| 267 | } |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 268 | |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 269 | return true; |
| 270 | } |
| 271 | |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 272 | string AidlAnnotatable::ToString() const { |
| 273 | vector<string> ret; |
| 274 | for (const auto& a : annotations_) { |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame^] | 275 | ret.emplace_back(a.ToString(AidlConstantValueDecorator)); |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 276 | } |
| 277 | std::sort(ret.begin(), ret.end()); |
| 278 | return Join(ret, " "); |
| 279 | } |
| 280 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 281 | AidlTypeSpecifier::AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, |
| 282 | bool is_array, |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 283 | vector<unique_ptr<AidlTypeSpecifier>>* type_params, |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 284 | const string& comments) |
| 285 | : AidlAnnotatable(location), |
| 286 | unresolved_name_(unresolved_name), |
Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame] | 287 | is_array_(is_array), |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 288 | type_params_(type_params), |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 289 | comments_(comments), |
| 290 | split_name_(Split(unresolved_name, ".")) {} |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 291 | |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 292 | AidlTypeSpecifier AidlTypeSpecifier::ArrayBase() const { |
| 293 | AIDL_FATAL_IF(!is_array_, this); |
| 294 | |
| 295 | AidlTypeSpecifier arrayBase = *this; |
| 296 | arrayBase.is_array_ = false; |
| 297 | return arrayBase; |
| 298 | } |
| 299 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 300 | string AidlTypeSpecifier::ToString() const { |
| 301 | string ret = GetName(); |
| 302 | if (IsGeneric()) { |
| 303 | vector<string> arg_names; |
| 304 | for (const auto& ta : GetTypeParameters()) { |
Jiyong Park | ccf00f8 | 2018-07-17 01:39:23 +0900 | [diff] [blame] | 305 | arg_names.emplace_back(ta->ToString()); |
| 306 | } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 307 | ret += "<" + Join(arg_names, ",") + ">"; |
Jiyong Park | ccf00f8 | 2018-07-17 01:39:23 +0900 | [diff] [blame] | 308 | } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 309 | if (IsArray()) { |
| 310 | ret += "[]"; |
| 311 | } |
| 312 | return ret; |
Jiyong Park | ccf00f8 | 2018-07-17 01:39:23 +0900 | [diff] [blame] | 313 | } |
| 314 | |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 315 | string AidlTypeSpecifier::Signature() const { |
| 316 | string ret = ToString(); |
| 317 | string annotations = AidlAnnotatable::ToString(); |
| 318 | if (annotations != "") { |
| 319 | ret = annotations + " " + ret; |
| 320 | } |
| 321 | return ret; |
| 322 | } |
| 323 | |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 324 | bool AidlTypeSpecifier::Resolve(const AidlTypenames& typenames) { |
Steven Moreland | 9731c63 | 2019-08-13 10:21:08 -0700 | [diff] [blame] | 325 | CHECK(!IsResolved()); |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 326 | pair<string, bool> result = typenames.ResolveTypename(unresolved_name_); |
| 327 | if (result.second) { |
| 328 | fully_qualified_name_ = result.first; |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 329 | split_name_ = Split(fully_qualified_name_, "."); |
Jiyong Park | ccf00f8 | 2018-07-17 01:39:23 +0900 | [diff] [blame] | 330 | } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 331 | return result.second; |
Casey Dahlin | 70078e6 | 2015-09-30 17:01:30 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 334 | bool AidlTypeSpecifier::CheckValid(const AidlTypenames& typenames) const { |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 335 | if (!CheckValidAnnotations()) { |
| 336 | return false; |
| 337 | } |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 338 | if (IsGeneric()) { |
| 339 | const string& type_name = GetName(); |
| 340 | const int num = GetTypeParameters().size(); |
| 341 | if (type_name == "List") { |
| 342 | if (num > 1) { |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 343 | AIDL_ERROR(this) << " List cannot have type parameters more than one, but got " |
| 344 | << "'" << ToString() << "'"; |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 345 | return false; |
| 346 | } |
| 347 | } else if (type_name == "Map") { |
| 348 | if (num != 0 && num != 2) { |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 349 | AIDL_ERROR(this) << "Map must have 0 or 2 type parameters, but got " |
| 350 | << "'" << ToString() << "'"; |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 351 | return false; |
| 352 | } |
| 353 | } |
| 354 | } |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 355 | |
| 356 | if (GetName() == "void") { |
| 357 | if (IsArray() || IsNullable() || IsUtf8InCpp()) { |
| 358 | AIDL_ERROR(this) << "void type cannot be an array or nullable or utf8 string"; |
| 359 | return false; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | if (IsArray()) { |
| 364 | const auto definedType = typenames.TryGetDefinedType(GetName()); |
| 365 | if (definedType != nullptr && definedType->AsInterface() != nullptr) { |
| 366 | AIDL_ERROR(this) << "Binder type cannot be an array"; |
| 367 | return false; |
| 368 | } |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 369 | if (definedType != nullptr && definedType->AsEnumDeclaration() != nullptr) { |
| 370 | // TODO(b/123321528): Support arrays of enums. |
| 371 | AIDL_ERROR(this) << "Enum type cannot be an array"; |
| 372 | return false; |
| 373 | } |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | if (IsNullable()) { |
| 377 | if (AidlTypenames::IsPrimitiveTypename(GetName()) && !IsArray()) { |
| 378 | AIDL_ERROR(this) << "Primitive type cannot get nullable annotation"; |
| 379 | return false; |
| 380 | } |
| 381 | } |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 382 | return true; |
| 383 | } |
| 384 | |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 385 | std::string AidlConstantValueDecorator(const AidlTypeSpecifier& /*type*/, |
| 386 | const std::string& raw_value) { |
| 387 | return raw_value; |
| 388 | } |
| 389 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 390 | AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location, |
| 391 | AidlTypeSpecifier* type, const std::string& name) |
| 392 | : AidlVariableDeclaration(location, type, name, nullptr /*default_value*/) {} |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 393 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 394 | AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location, |
| 395 | AidlTypeSpecifier* type, const std::string& name, |
| 396 | AidlConstantValue* default_value) |
| 397 | : AidlNode(location), type_(type), name_(name), default_value_(default_value) {} |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 398 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 399 | bool AidlVariableDeclaration::CheckValid(const AidlTypenames& typenames) const { |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 400 | bool valid = true; |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 401 | valid &= type_->CheckValid(typenames); |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 402 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 403 | if (default_value_ == nullptr) return valid; |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 404 | valid &= default_value_->CheckValid(); |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 405 | |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 406 | if (!valid) return false; |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 407 | |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 408 | return !ValueString(AidlConstantValueDecorator).empty(); |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 409 | } |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 410 | |
| 411 | string AidlVariableDeclaration::ToString() const { |
Jeongik Cha | 3271ffa | 2018-12-04 15:19:20 +0900 | [diff] [blame] | 412 | string ret = type_->Signature() + " " + name_; |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 413 | if (default_value_ != nullptr) { |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 414 | ret += " = " + ValueString(AidlConstantValueDecorator); |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 415 | } |
| 416 | return ret; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 419 | string AidlVariableDeclaration::Signature() const { |
| 420 | return type_->Signature() + " " + name_; |
| 421 | } |
| 422 | |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 423 | std::string AidlVariableDeclaration::ValueString(const ConstantValueDecorator& decorator) const { |
Jiyong Park | a468e2a | 2018-08-29 21:25:18 +0900 | [diff] [blame] | 424 | if (default_value_ != nullptr) { |
| 425 | return GetDefaultValue()->As(GetType(), decorator); |
| 426 | } else { |
| 427 | return ""; |
| 428 | } |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 431 | AidlArgument::AidlArgument(const AidlLocation& location, AidlArgument::Direction direction, |
| 432 | AidlTypeSpecifier* type, const std::string& name) |
| 433 | : AidlVariableDeclaration(location, type, name), |
Casey Dahlin | fd6fb48 | 2015-09-30 14:48:18 -0700 | [diff] [blame] | 434 | direction_(direction), |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 435 | direction_specified_(true) {} |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 436 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 437 | AidlArgument::AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, |
| 438 | const std::string& name) |
| 439 | : AidlVariableDeclaration(location, type, name), |
Casey Dahlin | fd6fb48 | 2015-09-30 14:48:18 -0700 | [diff] [blame] | 440 | direction_(AidlArgument::IN_DIR), |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 441 | direction_specified_(false) {} |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 442 | |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 443 | string AidlArgument::GetDirectionSpecifier() const { |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 444 | string ret; |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 445 | if (direction_specified_) { |
| 446 | switch(direction_) { |
| 447 | case AidlArgument::IN_DIR: |
| 448 | ret += "in "; |
| 449 | break; |
| 450 | case AidlArgument::OUT_DIR: |
| 451 | ret += "out "; |
| 452 | break; |
| 453 | case AidlArgument::INOUT_DIR: |
| 454 | ret += "inout "; |
| 455 | break; |
| 456 | } |
| 457 | } |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 458 | return ret; |
| 459 | } |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 460 | |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 461 | string AidlArgument::ToString() const { |
| 462 | return GetDirectionSpecifier() + AidlVariableDeclaration::ToString(); |
| 463 | } |
| 464 | |
| 465 | std::string AidlArgument::Signature() const { |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 466 | class AidlInterface; |
| 467 | class AidlInterface; |
| 468 | class AidlParcelable; |
| 469 | class AidlStructuredParcelable; |
| 470 | class AidlParcelable; |
| 471 | class AidlStructuredParcelable; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 472 | return GetDirectionSpecifier() + AidlVariableDeclaration::Signature(); |
| 473 | } |
| 474 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 475 | AidlMember::AidlMember(const AidlLocation& location) : AidlNode(location) {} |
| 476 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 477 | AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type, |
| 478 | const std::string& checked_value) |
Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 479 | : AidlNode(location), type_(type), value_(checked_value) { |
| 480 | CHECK(!value_.empty() || type_ == Type::ERROR); |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 481 | CHECK(type_ != Type::ARRAY); |
Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 482 | } |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 483 | |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 484 | AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type, |
| 485 | std::vector<std::unique_ptr<AidlConstantValue>>* values) |
| 486 | : AidlNode(location), type_(type), values_(std::move(*values)) {} |
| 487 | |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 488 | static bool isValidLiteralChar(char c) { |
| 489 | return !(c <= 0x1f || // control characters are < 0x20 |
| 490 | c >= 0x7f || // DEL is 0x7f |
| 491 | c == '\\'); // Disallow backslashes for future proofing. |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 494 | AidlConstantValue* AidlConstantValue::Boolean(const AidlLocation& location, bool value) { |
| 495 | return new AidlConstantValue(location, Type::BOOLEAN, value ? "true" : "false"); |
| 496 | } |
| 497 | |
| 498 | AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location, char value) { |
| 499 | if (!isValidLiteralChar(value)) { |
| 500 | AIDL_ERROR(location) << "Invalid character literal " << value; |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 501 | return new AidlConstantValue(location, Type::ERROR, ""); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 502 | } |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 503 | return new AidlConstantValue(location, Type::CHARACTER, std::string("'") + value + "'"); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 506 | AidlConstantValue* AidlConstantValue::Floating(const AidlLocation& location, |
| 507 | const std::string& value) { |
| 508 | return new AidlConstantValue(location, Type::FLOATING, value); |
| 509 | } |
| 510 | |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 511 | AidlConstantValue* AidlConstantValue::Hex(const AidlLocation& location, const std::string& value) { |
| 512 | return new AidlConstantValue(location, Type::HEXIDECIMAL, value); |
| 513 | } |
| 514 | |
| 515 | AidlConstantValue* AidlConstantValue::Integral(const AidlLocation& location, |
| 516 | const std::string& value) { |
| 517 | return new AidlConstantValue(location, Type::INTEGRAL, value); |
| 518 | } |
| 519 | |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 520 | AidlConstantValue* AidlConstantValue::Array( |
| 521 | const AidlLocation& location, std::vector<std::unique_ptr<AidlConstantValue>>* values) { |
| 522 | return new AidlConstantValue(location, Type::ARRAY, values); |
| 523 | } |
| 524 | |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 525 | AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, |
| 526 | const std::string& value) { |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 527 | for (size_t i = 0; i < value.length(); ++i) { |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 528 | if (!isValidLiteralChar(value[i])) { |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 529 | AIDL_ERROR(location) << "Found invalid character at index " << i << " in string constant '" |
| 530 | << value << "'"; |
| 531 | return new AidlConstantValue(location, Type::ERROR, ""); |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 532 | } |
| 533 | } |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 534 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 535 | return new AidlConstantValue(location, Type::STRING, value); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 538 | bool AidlConstantValue::CheckValid() const { |
| 539 | // error always logged during creation |
| 540 | return type_ != AidlConstantValue::Type::ERROR; |
| 541 | } |
| 542 | |
Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 543 | static string TrimIfSuffix(const string& str, const string& suffix) { |
| 544 | if (str.size() > suffix.size() && |
| 545 | 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix)) { |
| 546 | return str.substr(0, str.size() - suffix.size()); |
| 547 | } |
| 548 | return str; |
| 549 | } |
| 550 | |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 551 | string AidlConstantValue::As(const AidlTypeSpecifier& type, |
| 552 | const ConstantValueDecorator& decorator) const { |
| 553 | if (type.IsGeneric()) { |
| 554 | AIDL_ERROR(type) << "Generic type cannot be specified with a constant literal."; |
| 555 | return ""; |
| 556 | } |
| 557 | |
| 558 | const std::string& type_string = type.GetName(); |
| 559 | |
| 560 | if ((type_ == Type::ARRAY) != type.IsArray()) { |
| 561 | goto mismatch_error; |
| 562 | } |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 563 | |
| 564 | switch (type_) { |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 565 | case AidlConstantValue::Type::ARRAY: { |
| 566 | vector<string> raw_values; |
| 567 | raw_values.reserve(values_.size()); |
| 568 | |
| 569 | bool success = true; |
| 570 | for (const auto& value : values_) { |
| 571 | const AidlTypeSpecifier& array_base = type.ArrayBase(); |
| 572 | const std::string raw_value = value->As(array_base, decorator); |
| 573 | |
| 574 | success &= !raw_value.empty(); |
| 575 | raw_values.push_back(decorator(array_base, raw_value)); |
| 576 | } |
| 577 | if (!success) { |
| 578 | AIDL_ERROR(this) << "Default value must be a literal array of " << type_string << "."; |
| 579 | return ""; |
| 580 | } |
| 581 | return decorator(type, "{" + Join(raw_values, ", ") + "}"); |
| 582 | } |
Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 583 | case AidlConstantValue::Type::BOOLEAN: |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 584 | if (type_string == "boolean") return decorator(type, value_); |
Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 585 | goto mismatch_error; |
| 586 | case AidlConstantValue::Type::CHARACTER: |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 587 | if (type_string == "char") return decorator(type, value_); |
Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 588 | goto mismatch_error; |
| 589 | case AidlConstantValue::Type::FLOATING: { |
| 590 | bool is_float_literal = value_.back() == 'f'; |
| 591 | const std::string raw_value = TrimIfSuffix(value_, "f"); |
| 592 | |
| 593 | if (type_string == "double") { |
| 594 | double parsed_value; |
| 595 | if (!android::base::ParseDouble(raw_value, &parsed_value)) goto parse_error; |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 596 | return decorator(type, std::to_string(parsed_value)); |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 597 | } |
Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 598 | if (is_float_literal && type_string == "float") { |
| 599 | float parsed_value; |
| 600 | if (!android::base::ParseFloat(raw_value, &parsed_value)) goto parse_error; |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 601 | return decorator(type, std::to_string(parsed_value) + "f"); |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 602 | } |
| 603 | goto mismatch_error; |
Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 604 | } |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 605 | case AidlConstantValue::Type::HEXIDECIMAL: |
| 606 | // For historical reasons, a hexidecimal int needs to have the specified bits interpreted |
| 607 | // as the signed type, so the other types are made consistent with it. |
| 608 | if (type_string == "byte") { |
| 609 | uint8_t unsigned_value; |
| 610 | if (!android::base::ParseUint<uint8_t>(value_, &unsigned_value)) goto parse_error; |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 611 | return decorator(type, std::to_string((int8_t)unsigned_value)); |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 612 | } |
| 613 | if (type_string == "int") { |
| 614 | uint32_t unsigned_value; |
| 615 | if (!android::base::ParseUint<uint32_t>(value_, &unsigned_value)) goto parse_error; |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 616 | return decorator(type, std::to_string((int32_t)unsigned_value)); |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 617 | } |
| 618 | if (type_string == "long") { |
| 619 | uint64_t unsigned_value; |
| 620 | if (!android::base::ParseUint<uint64_t>(value_, &unsigned_value)) goto parse_error; |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 621 | return decorator(type, std::to_string((int64_t)unsigned_value)); |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 622 | } |
| 623 | goto mismatch_error; |
Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 624 | case AidlConstantValue::Type::INTEGRAL: |
| 625 | if (type_string == "byte") { |
| 626 | if (!android::base::ParseInt<int8_t>(value_, nullptr)) goto parse_error; |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 627 | return decorator(type, value_); |
Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 628 | } |
| 629 | if (type_string == "int") { |
| 630 | if (!android::base::ParseInt<int32_t>(value_, nullptr)) goto parse_error; |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 631 | return decorator(type, value_); |
Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 632 | } |
| 633 | if (type_string == "long") { |
| 634 | if (!android::base::ParseInt<int64_t>(value_, nullptr)) goto parse_error; |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame^] | 635 | return decorator(type, value_); |
Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 636 | } |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 637 | goto mismatch_error; |
| 638 | case AidlConstantValue::Type::STRING: |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 639 | if (type_string == "String") return decorator(type, value_); |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 640 | goto mismatch_error; |
| 641 | default: |
| 642 | AIDL_FATAL(this) << "Unrecognized constant value type"; |
| 643 | } |
| 644 | |
| 645 | mismatch_error: |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 646 | AIDL_ERROR(this) << "Expecting type " << type_string << " but constant is " << ToString(type_); |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 647 | return ""; |
| 648 | parse_error: |
| 649 | AIDL_ERROR(this) << "Could not parse " << value_ << " as " << type_string; |
| 650 | return ""; |
| 651 | } |
| 652 | |
| 653 | string AidlConstantValue::ToString(Type type) { |
| 654 | switch (type) { |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 655 | case Type::ARRAY: |
| 656 | return "a literal array"; |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 657 | case Type::BOOLEAN: |
| 658 | return "a literal boolean"; |
| 659 | case Type::CHARACTER: |
| 660 | return "a literal char"; |
Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 661 | case Type::FLOATING: |
| 662 | return "a floating-point literal"; |
| 663 | case Type::HEXIDECIMAL: |
| 664 | return "a hexidecimal literal"; |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 665 | case Type::INTEGRAL: |
| 666 | return "an integral literal"; |
| 667 | case Type::STRING: |
| 668 | return "a literal string"; |
| 669 | case Type::ERROR: |
| 670 | LOG(FATAL) << "aidl internal error: error type failed to halt program"; |
Wei Wang | 35b7bb6 | 2018-10-09 13:06:12 -0700 | [diff] [blame] | 671 | return ""; |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 672 | default: |
| 673 | LOG(FATAL) << "aidl internal error: unknown constant type: " << static_cast<int>(type); |
| 674 | return ""; // not reached |
| 675 | } |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 678 | AidlConstantDeclaration::AidlConstantDeclaration(const AidlLocation& location, |
| 679 | AidlTypeSpecifier* type, const std::string& name, |
| 680 | AidlConstantValue* value) |
| 681 | : AidlMember(location), type_(type), name_(name), value_(value) {} |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 682 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 683 | bool AidlConstantDeclaration::CheckValid(const AidlTypenames& typenames) const { |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 684 | bool valid = true; |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 685 | valid &= type_->CheckValid(typenames); |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 686 | valid &= value_->CheckValid(); |
| 687 | if (!valid) return false; |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 688 | |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 689 | const static set<string> kSupportedConstTypes = {"String", "int"}; |
| 690 | if (kSupportedConstTypes.find(type_->ToString()) == kSupportedConstTypes.end()) { |
| 691 | AIDL_ERROR(this) << "Constant of type " << type_->ToString() << " is not supported."; |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 692 | return false; |
| 693 | } |
| 694 | |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 695 | return !ValueString(AidlConstantValueDecorator).empty(); |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 698 | string AidlConstantDeclaration::ToString() const { |
| 699 | return "const " + type_->ToString() + " " + name_ + " = " + |
| 700 | ValueString(AidlConstantValueDecorator); |
| 701 | } |
| 702 | |
| 703 | string AidlConstantDeclaration::Signature() const { |
| 704 | return type_->Signature() + " " + name_; |
| 705 | } |
| 706 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 707 | AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, |
| 708 | const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args, |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 709 | const std::string& comments) |
| 710 | : AidlMethod(location, oneway, type, name, args, comments, 0, true) { |
| 711 | has_id_ = false; |
| 712 | } |
| 713 | |
| 714 | AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, |
| 715 | const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args, |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 716 | const std::string& comments, int id, bool is_user_defined) |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 717 | : AidlMember(location), |
| 718 | oneway_(oneway), |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 719 | comments_(comments), |
| 720 | type_(type), |
| 721 | name_(name), |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 722 | arguments_(std::move(*args)), |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 723 | id_(id), |
| 724 | is_user_defined_(is_user_defined) { |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 725 | has_id_ = true; |
| 726 | delete args; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 727 | for (const unique_ptr<AidlArgument>& a : arguments_) { |
| 728 | if (a->IsIn()) { in_arguments_.push_back(a.get()); } |
| 729 | if (a->IsOut()) { out_arguments_.push_back(a.get()); } |
| 730 | } |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 733 | |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 734 | string AidlMethod::Signature() const { |
| 735 | vector<string> arg_signatures; |
| 736 | for (const auto& arg : GetArguments()) { |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 737 | arg_signatures.emplace_back(arg->GetType().ToString()); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 738 | } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 739 | return GetName() + "(" + Join(arg_signatures, ", ") + ")"; |
| 740 | } |
| 741 | |
| 742 | string AidlMethod::ToString() const { |
| 743 | vector<string> arg_strings; |
| 744 | for (const auto& arg : GetArguments()) { |
| 745 | arg_strings.emplace_back(arg->Signature()); |
| 746 | } |
Steven Moreland | 4ee6863 | 2018-12-14 15:52:46 -0800 | [diff] [blame] | 747 | string ret = (IsOneway() ? "oneway " : "") + GetType().Signature() + " " + GetName() + "(" + |
| 748 | Join(arg_strings, ", ") + ")"; |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 749 | if (HasId()) { |
| 750 | ret += " = " + std::to_string(GetId()); |
| 751 | } |
| 752 | return ret; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 753 | } |
| 754 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 755 | AidlDefinedType::AidlDefinedType(const AidlLocation& location, const std::string& name, |
| 756 | const std::string& comments, |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 757 | const std::vector<std::string>& package) |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 758 | : AidlAnnotatable(location), name_(name), comments_(comments), package_(package) {} |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 759 | |
| 760 | std::string AidlDefinedType::GetPackage() const { |
| 761 | return Join(package_, '.'); |
| 762 | } |
| 763 | |
| 764 | std::string AidlDefinedType::GetCanonicalName() const { |
| 765 | if (package_.empty()) { |
| 766 | return GetName(); |
| 767 | } |
| 768 | return GetPackage() + "." + GetName(); |
| 769 | } |
| 770 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 771 | AidlParcelable::AidlParcelable(const AidlLocation& location, AidlQualifiedName* name, |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 772 | const std::vector<std::string>& package, const std::string& comments, |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 773 | const std::string& cpp_header) |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 774 | : AidlDefinedType(location, name->GetDotName(), comments, package), |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 775 | name_(name), |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 776 | cpp_header_(cpp_header) { |
| 777 | // Strip off quotation marks if we actually have a cpp header. |
| 778 | if (cpp_header_.length() >= 2) { |
| 779 | cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2); |
| 780 | } |
Casey Dahlin | 59401da | 2015-10-09 18:16:45 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Jeongik Cha | 82317dd | 2019-02-27 20:26:11 +0900 | [diff] [blame] | 783 | bool AidlParcelable::CheckValid(const AidlTypenames&) const { |
| 784 | static const std::set<string> allowed{kStableParcelable}; |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 785 | if (!CheckValidAnnotations()) { |
| 786 | return false; |
| 787 | } |
Jeongik Cha | 82317dd | 2019-02-27 20:26:11 +0900 | [diff] [blame] | 788 | for (const auto& v : GetAnnotations()) { |
| 789 | if (allowed.find(v.GetName()) == allowed.end()) { |
| 790 | std::ostringstream stream; |
| 791 | stream << "Unstructured parcelable can contain only"; |
| 792 | for (const string& kv : allowed) { |
| 793 | stream << " " << kv; |
| 794 | } |
| 795 | stream << "."; |
| 796 | AIDL_ERROR(this) << stream.str(); |
| 797 | return false; |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | return true; |
| 802 | } |
| 803 | |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 804 | void AidlParcelable::Write(CodeWriter* writer) const { |
| 805 | writer->Write("parcelable %s ;\n", GetName().c_str()); |
| 806 | } |
| 807 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 808 | AidlStructuredParcelable::AidlStructuredParcelable( |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 809 | const AidlLocation& location, AidlQualifiedName* name, const std::vector<std::string>& package, |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 810 | const std::string& comments, std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables) |
| 811 | : AidlParcelable(location, name, package, comments, "" /*cpp_header*/), |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 812 | variables_(std::move(*variables)) {} |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 813 | |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 814 | void AidlStructuredParcelable::Write(CodeWriter* writer) const { |
| 815 | writer->Write("parcelable %s {\n", GetName().c_str()); |
| 816 | writer->Indent(); |
| 817 | for (const auto& field : GetFields()) { |
Jiyong Park | a468e2a | 2018-08-29 21:25:18 +0900 | [diff] [blame] | 818 | writer->Write("%s;\n", field->ToString().c_str()); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 819 | } |
| 820 | writer->Dedent(); |
| 821 | writer->Write("}\n"); |
| 822 | } |
| 823 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 824 | bool AidlStructuredParcelable::CheckValid(const AidlTypenames& typenames) const { |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 825 | bool success = true; |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 826 | for (const auto& v : GetFields()) { |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 827 | success = success && v->CheckValid(typenames); |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 828 | } |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 829 | return success; |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 830 | } |
| 831 | |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 832 | // TODO: we should treat every backend all the same in future. |
| 833 | bool AidlTypeSpecifier::LanguageSpecificCheckValid(Options::Language lang) const { |
| 834 | if (lang == Options::Language::CPP) { |
| 835 | if (this->GetName() == "List" && !this->IsGeneric()) { |
| 836 | AIDL_ERROR(this) << "List without type isn't supported in cpp."; |
| 837 | return false; |
| 838 | } |
| 839 | } |
| 840 | if (this->IsGeneric()) { |
| 841 | if (this->GetName() == "List") { |
| 842 | if (this->GetTypeParameters().size() != 1) { |
| 843 | AIDL_ERROR(this) << "List must have only one type parameter."; |
| 844 | return false; |
| 845 | } |
| 846 | if (lang == Options::Language::CPP) { |
| 847 | auto& name = this->GetTypeParameters()[0]->GetName(); |
| 848 | if (!(name == "String" || name == "IBinder")) { |
| 849 | AIDL_ERROR(this) << "List in cpp supports only string and IBinder for now."; |
| 850 | return false; |
| 851 | } |
| 852 | } else if (lang == Options::Language::NDK) { |
| 853 | AIDL_ERROR(this) << "NDK backend does not support List yet."; |
| 854 | return false; |
| 855 | } |
| 856 | |
| 857 | } else if (this->GetName() == "Map") { |
| 858 | if (lang != Options::Language::JAVA) { |
| 859 | AIDL_ERROR(this) << "Currently, only Java backend supports Map."; |
| 860 | return false; |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | return true; |
| 865 | } |
| 866 | |
| 867 | // TODO: we should treat every backend all the same in future. |
| 868 | bool AidlParcelable::LanguageSpecificCheckValid(Options::Language lang) const { |
| 869 | if (lang != Options::Language::JAVA) { |
| 870 | if (this->IsStableParcelable()) { |
| 871 | AIDL_ERROR(this) << "@JavaOnlyStableParcelable supports only Java target."; |
| 872 | return false; |
| 873 | } |
| 874 | const AidlParcelable* unstructuredParcelable = this->AsUnstructuredParcelable(); |
| 875 | if (unstructuredParcelable != nullptr) { |
| 876 | if (unstructuredParcelable->GetCppHeader().empty()) { |
| 877 | AIDL_ERROR(unstructuredParcelable) |
| 878 | << "Unstructured parcelable must have C++ header defined."; |
| 879 | return false; |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | return true; |
| 884 | } |
| 885 | |
| 886 | // TODO: we should treat every backend all the same in future. |
| 887 | bool AidlStructuredParcelable::LanguageSpecificCheckValid(Options::Language lang) const { |
| 888 | if (!AidlParcelable::LanguageSpecificCheckValid(lang)) { |
| 889 | return false; |
| 890 | } |
| 891 | for (const auto& v : this->GetFields()) { |
| 892 | if (!v->GetType().LanguageSpecificCheckValid(lang)) { |
| 893 | return false; |
| 894 | } |
| 895 | } |
| 896 | return true; |
| 897 | } |
| 898 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 899 | AidlEnumerator::AidlEnumerator(const AidlLocation& location, const std::string& name, |
| 900 | AidlConstantValue* value) |
| 901 | : AidlNode(location), name_(name), value_(value) {} |
| 902 | |
| 903 | bool AidlEnumerator::CheckValid(const AidlTypeSpecifier& enum_backing_type) const { |
| 904 | if (GetValue() == nullptr) { |
| 905 | return false; |
| 906 | } |
| 907 | if (!GetValue()->CheckValid()) { |
| 908 | return false; |
| 909 | } |
| 910 | if (GetValue()->As(enum_backing_type, AidlConstantValueDecorator).empty()) { |
| 911 | AIDL_ERROR(this) << "Enumerator type differs from enum backing type."; |
| 912 | return false; |
| 913 | } |
| 914 | return true; |
| 915 | } |
| 916 | |
| 917 | string AidlEnumerator::ValueString(const AidlTypeSpecifier& backing_type, |
| 918 | const ConstantValueDecorator& decorator) const { |
| 919 | return GetValue()->As(backing_type, decorator); |
| 920 | } |
| 921 | |
| 922 | AidlEnumDeclaration::AidlEnumDeclaration(const AidlLocation& location, const std::string& name, |
| 923 | std::vector<std::unique_ptr<AidlEnumerator>>* enumerators, |
| 924 | const std::vector<std::string>& package) |
| 925 | : AidlDefinedType(location, name, "", package), enumerators_(std::move(*enumerators)) {} |
| 926 | |
| 927 | void AidlEnumDeclaration::SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type) { |
| 928 | backing_type_ = std::move(type); |
| 929 | } |
| 930 | |
| 931 | bool AidlEnumDeclaration::CheckValid(const AidlTypenames&) const { |
| 932 | if (backing_type_ == nullptr) { |
| 933 | AIDL_ERROR(this) << "Enum declaration missing backing type."; |
| 934 | return false; |
| 935 | } |
| 936 | bool success = true; |
| 937 | for (const auto& enumerator : enumerators_) { |
| 938 | success = success && enumerator->CheckValid(GetBackingType()); |
| 939 | } |
| 940 | return success; |
| 941 | } |
| 942 | |
| 943 | void AidlEnumDeclaration::Write(CodeWriter* writer) const { |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame^] | 944 | writer->Write("%s\n", AidlAnnotatable::ToString().c_str()); |
| 945 | writer->Write("enum %s {\n", GetName().c_str()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 946 | writer->Indent(); |
| 947 | for (const auto& enumerator : GetEnumerators()) { |
| 948 | // TODO(b/123321528): After autofilling is supported, determine if we want |
| 949 | // to leave out the assigned value for enumerators that were autofilled. |
| 950 | writer->Write("%s = %s,\n", enumerator->GetName().c_str(), |
| 951 | enumerator->ValueString(GetBackingType(), AidlConstantValueDecorator).c_str()); |
| 952 | } |
| 953 | writer->Dedent(); |
| 954 | writer->Write("}\n"); |
| 955 | } |
| 956 | |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 957 | // TODO: we should treat every backend all the same in future. |
| 958 | bool AidlInterface::LanguageSpecificCheckValid(Options::Language lang) const { |
| 959 | for (const auto& m : this->GetMethods()) { |
| 960 | if (!m->GetType().LanguageSpecificCheckValid(lang)) { |
| 961 | return false; |
| 962 | } |
| 963 | for (const auto& arg : m->GetArguments()) { |
| 964 | if (!arg->GetType().LanguageSpecificCheckValid(lang)) { |
| 965 | return false; |
| 966 | } |
| 967 | } |
| 968 | } |
| 969 | return true; |
| 970 | } |
| 971 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 972 | AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name, |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 973 | const std::string& comments, bool oneway, |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 974 | std::vector<std::unique_ptr<AidlMember>>* members, |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 975 | const std::vector<std::string>& package) |
Steven Moreland | acd5347 | 2018-12-14 10:17:26 -0800 | [diff] [blame] | 976 | : AidlDefinedType(location, name, comments, package) { |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 977 | for (auto& member : *members) { |
| 978 | AidlMember* local = member.release(); |
| 979 | AidlMethod* method = local->AsMethod(); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 980 | AidlConstantDeclaration* constant = local->AsConstantDeclaration(); |
| 981 | |
| 982 | CHECK(method == nullptr || constant == nullptr); |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 983 | |
| 984 | if (method) { |
Steven Moreland | 8c70ba9 | 2018-12-17 10:20:31 -0800 | [diff] [blame] | 985 | method->ApplyInterfaceOneway(oneway); |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 986 | methods_.emplace_back(method); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 987 | } else if (constant) { |
| 988 | constants_.emplace_back(constant); |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 989 | } else { |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 990 | AIDL_FATAL(this) << "Member is neither method nor constant!"; |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | |
| 994 | delete members; |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 995 | } |
| 996 | |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 997 | void AidlInterface::Write(CodeWriter* writer) const { |
| 998 | writer->Write("interface %s {\n", GetName().c_str()); |
| 999 | writer->Indent(); |
| 1000 | for (const auto& method : GetMethods()) { |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1001 | writer->Write("%s;\n", method->ToString().c_str()); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1002 | } |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 1003 | for (const auto& constdecl : GetConstantDeclarations()) { |
| 1004 | writer->Write("%s;\n", constdecl->ToString().c_str()); |
| 1005 | } |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1006 | writer->Dedent(); |
| 1007 | writer->Write("}\n"); |
| 1008 | } |
| 1009 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1010 | bool AidlInterface::CheckValid(const AidlTypenames& typenames) const { |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 1011 | if (!CheckValidAnnotations()) { |
| 1012 | return false; |
| 1013 | } |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1014 | // Has to be a pointer due to deleting copy constructor. No idea why. |
| 1015 | map<string, const AidlMethod*> method_names; |
| 1016 | for (const auto& m : GetMethods()) { |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1017 | if (!m->GetType().CheckValid(typenames)) { |
| 1018 | return false; |
| 1019 | } |
| 1020 | |
Steven Moreland | acd5347 | 2018-12-14 10:17:26 -0800 | [diff] [blame] | 1021 | if (m->IsOneway() && m->GetType().GetName() != "void") { |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1022 | AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot return a value"; |
| 1023 | return false; |
| 1024 | } |
| 1025 | |
| 1026 | set<string> argument_names; |
| 1027 | for (const auto& arg : m->GetArguments()) { |
| 1028 | auto it = argument_names.find(arg->GetName()); |
| 1029 | if (it != argument_names.end()) { |
| 1030 | AIDL_ERROR(m) << "method '" << m->GetName() << "' has duplicate argument name '" |
| 1031 | << arg->GetName() << "'"; |
| 1032 | return false; |
| 1033 | } |
| 1034 | argument_names.insert(arg->GetName()); |
| 1035 | |
| 1036 | if (!arg->GetType().CheckValid(typenames)) { |
| 1037 | return false; |
| 1038 | } |
| 1039 | |
Steven Moreland | acd5347 | 2018-12-14 10:17:26 -0800 | [diff] [blame] | 1040 | if (m->IsOneway() && arg->IsOut()) { |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1041 | AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot have out parameters"; |
| 1042 | return false; |
| 1043 | } |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1044 | const bool can_be_out = typenames.CanBeOutParameter(arg->GetType()); |
| 1045 | if (!arg->DirectionWasSpecified() && can_be_out) { |
| 1046 | AIDL_ERROR(arg) << "'" << arg->GetType().ToString() |
| 1047 | << "' can be an out type, so you must declare it as in, out, or inout."; |
| 1048 | return false; |
| 1049 | } |
| 1050 | |
| 1051 | if (arg->GetDirection() != AidlArgument::IN_DIR && !can_be_out) { |
| 1052 | AIDL_ERROR(arg) << "'" << arg->ToString() << "' can only be an in parameter."; |
| 1053 | return false; |
| 1054 | } |
| 1055 | |
| 1056 | // check that the name doesn't match a keyword |
| 1057 | if (is_java_keyword(arg->GetName().c_str())) { |
| 1058 | AIDL_ERROR(arg) << "Argument name is a Java or aidl keyword"; |
| 1059 | return false; |
| 1060 | } |
| 1061 | |
| 1062 | // Reserve a namespace for internal use |
| 1063 | if (android::base::StartsWith(arg->GetName(), "_aidl")) { |
| 1064 | AIDL_ERROR(arg) << "Argument name cannot begin with '_aidl'"; |
| 1065 | return false; |
| 1066 | } |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | auto it = method_names.find(m->GetName()); |
| 1070 | // prevent duplicate methods |
| 1071 | if (it == method_names.end()) { |
| 1072 | method_names[m->GetName()] = m.get(); |
| 1073 | } else { |
| 1074 | AIDL_ERROR(m) << "attempt to redefine method " << m->GetName() << ":"; |
| 1075 | AIDL_ERROR(it->second) << "previously defined here."; |
| 1076 | return false; |
| 1077 | } |
| 1078 | |
| 1079 | static set<string> reserved_methods{"asBinder()", "getInterfaceVersion()", |
| 1080 | "getTransactionName(int)"}; |
| 1081 | |
| 1082 | if (reserved_methods.find(m->Signature()) != reserved_methods.end()) { |
| 1083 | AIDL_ERROR(m) << " method " << m->Signature() << " is reserved for internal use." << endl; |
| 1084 | return false; |
| 1085 | } |
| 1086 | } |
Steven Moreland | 4d12f9a | 2018-10-31 14:30:55 -0700 | [diff] [blame] | 1087 | |
| 1088 | bool success = true; |
| 1089 | set<string> constant_names; |
| 1090 | for (const std::unique_ptr<AidlConstantDeclaration>& constant : GetConstantDeclarations()) { |
| 1091 | if (constant_names.count(constant->GetName()) > 0) { |
| 1092 | LOG(ERROR) << "Found duplicate constant name '" << constant->GetName() << "'"; |
| 1093 | success = false; |
| 1094 | } |
| 1095 | constant_names.insert(constant->GetName()); |
| 1096 | success = success && constant->CheckValid(typenames); |
| 1097 | } |
| 1098 | |
| 1099 | return success; |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1100 | } |
| 1101 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1102 | AidlQualifiedName::AidlQualifiedName(const AidlLocation& location, const std::string& term, |
| 1103 | const std::string& comments) |
| 1104 | : AidlNode(location), terms_({term}), comments_(comments) { |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 1105 | if (term.find('.') != string::npos) { |
| 1106 | terms_ = Split(term, "."); |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1107 | for (const auto& subterm : terms_) { |
| 1108 | if (subterm.empty()) { |
| 1109 | AIDL_FATAL(this) << "Malformed qualified identifier: '" << term << "'"; |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | } |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
Chih-Hung Hsieh | f05cc26 | 2016-07-27 11:42:51 -0700 | [diff] [blame] | 1115 | void AidlQualifiedName::AddTerm(const std::string& term) { |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 1116 | terms_.push_back(term); |
| 1117 | } |
| 1118 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1119 | AidlImport::AidlImport(const AidlLocation& location, const std::string& needed_class) |
| 1120 | : AidlNode(location), needed_class_(needed_class) {} |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 1121 | |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1122 | std::unique_ptr<Parser> Parser::Parse(const std::string& filename, |
| 1123 | const android::aidl::IoDelegate& io_delegate, |
| 1124 | AidlTypenames& typenames) { |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1125 | // Make sure we can read the file first, before trashing previous state. |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1126 | unique_ptr<string> raw_buffer = io_delegate.GetFileContents(filename); |
| 1127 | if (raw_buffer == nullptr) { |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1128 | AIDL_ERROR(filename) << "Error while opening file for parsing"; |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1129 | return nullptr; |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1132 | // We're going to scan this buffer in place, and yacc demands we put two |
| 1133 | // nulls at the end. |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1134 | raw_buffer->append(2u, '\0'); |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1135 | |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1136 | std::unique_ptr<Parser> parser(new Parser(filename, *raw_buffer, typenames)); |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1137 | |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1138 | if (yy::parser(parser.get()).parse() != 0 || parser->HasError()) return nullptr; |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1139 | |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1140 | return parser; |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 1143 | std::vector<std::string> Parser::Package() const { |
| 1144 | if (!package_) { |
| 1145 | return {}; |
| 1146 | } |
| 1147 | return package_->GetTerms(); |
| 1148 | } |
| 1149 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1150 | void Parser::AddImport(AidlImport* import) { |
| 1151 | imports_.emplace_back(import); |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 1152 | } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 1153 | |
| 1154 | bool Parser::Resolve() { |
| 1155 | bool success = true; |
| 1156 | for (AidlTypeSpecifier* typespec : unresolved_typespecs_) { |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1157 | if (!typespec->Resolve(typenames_)) { |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1158 | AIDL_ERROR(typespec) << "Failed to resolve '" << typespec->GetUnresolvedName() << "'"; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 1159 | success = false; |
| 1160 | // don't stop to show more errors if any |
| 1161 | } |
| 1162 | } |
| 1163 | return success; |
| 1164 | } |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1165 | |
| 1166 | Parser::Parser(const std::string& filename, std::string& raw_buffer, |
| 1167 | android::aidl::AidlTypenames& typenames) |
| 1168 | : filename_(filename), typenames_(typenames) { |
| 1169 | yylex_init(&scanner_); |
| 1170 | buffer_ = yy_scan_buffer(&raw_buffer[0], raw_buffer.length(), scanner_); |
| 1171 | } |
| 1172 | |
| 1173 | Parser::~Parser() { |
| 1174 | yy_delete_buffer(buffer_, scanner_); |
| 1175 | yylex_destroy(scanner_); |
| 1176 | } |