| 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 | |
| Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 188 | static bool HasAnnotation(const vector<AidlAnnotation>& annotations, const string& name) { |
| Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 189 | for (const auto& a : annotations) { |
| Steven Moreland | 3be7577 | 2018-08-20 13:27:43 -0700 | [diff] [blame] | 190 | if (a.GetName() == name) { |
| Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 191 | return true; |
| 192 | } |
| 193 | } |
| 194 | return false; |
| 195 | } |
| 196 | |
| Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 197 | static 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 Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 207 | AidlAnnotatable::AidlAnnotatable(const AidlLocation& location) : AidlNode(location) {} |
| 208 | |
| Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 209 | bool AidlAnnotatable::IsNullable() const { |
| 210 | return HasAnnotation(annotations_, kNullable); |
| 211 | } |
| 212 | |
| Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 213 | bool AidlAnnotatable::IsUtf8InCpp() const { |
| 214 | return HasAnnotation(annotations_, kUtf8InCpp); |
| 215 | } |
| 216 | |
| Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 217 | bool AidlAnnotatable::IsVintfStability() const { |
| 218 | return HasAnnotation(annotations_, kVintfStability); |
| 219 | } |
| 220 | |
| Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 221 | const AidlAnnotation* AidlAnnotatable::UnsupportedAppUsage() const { |
| 222 | return GetAnnotation(annotations_, kUnsupportedAppUsage); |
| Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 223 | } |
| 224 | |
| Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 225 | const 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 Cha | 698e6af | 2018-12-12 14:39:55 +0900 | [diff] [blame] | 239 | bool AidlAnnotatable::IsSystemApi() const { |
| 240 | return HasAnnotation(annotations_, kSystemApi); |
| 241 | } |
| 242 | |
| Jeongik Cha | 82317dd | 2019-02-27 20:26:11 +0900 | [diff] [blame] | 243 | bool AidlAnnotatable::IsStableParcelable() const { |
| 244 | return HasAnnotation(annotations_, kStableParcelable); |
| 245 | } |
| 246 | |
| Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 247 | bool AidlAnnotatable::CheckValidAnnotations() const { |
| 248 | for (const auto& annotation : GetAnnotations()) { |
| 249 | if (!annotation.CheckValid()) { |
| 250 | return false; |
| 251 | } |
| 252 | } |
| Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 253 | |
| Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 254 | return true; |
| 255 | } |
| 256 | |
| Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 257 | string AidlAnnotatable::ToString() const { |
| 258 | vector<string> ret; |
| 259 | for (const auto& a : annotations_) { |
| Steven Moreland | 3be7577 | 2018-08-20 13:27:43 -0700 | [diff] [blame] | 260 | ret.emplace_back(a.ToString()); |
| Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 261 | } |
| 262 | std::sort(ret.begin(), ret.end()); |
| 263 | return Join(ret, " "); |
| 264 | } |
| 265 | |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 266 | AidlTypeSpecifier::AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, |
| 267 | bool is_array, |
| Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 268 | vector<unique_ptr<AidlTypeSpecifier>>* type_params, |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 269 | const string& comments) |
| 270 | : AidlAnnotatable(location), |
| 271 | unresolved_name_(unresolved_name), |
| Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame] | 272 | is_array_(is_array), |
| Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 273 | type_params_(type_params), |
| Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 274 | comments_(comments), |
| 275 | split_name_(Split(unresolved_name, ".")) {} |
| Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 276 | |
| Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 277 | AidlTypeSpecifier 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 Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 285 | string AidlTypeSpecifier::ToString() const { |
| 286 | string ret = GetName(); |
| 287 | if (IsGeneric()) { |
| 288 | vector<string> arg_names; |
| 289 | for (const auto& ta : GetTypeParameters()) { |
| Jiyong Park | ccf00f8 | 2018-07-17 01:39:23 +0900 | [diff] [blame] | 290 | arg_names.emplace_back(ta->ToString()); |
| 291 | } |
| Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 292 | ret += "<" + Join(arg_names, ",") + ">"; |
| Jiyong Park | ccf00f8 | 2018-07-17 01:39:23 +0900 | [diff] [blame] | 293 | } |
| Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 294 | if (IsArray()) { |
| 295 | ret += "[]"; |
| 296 | } |
| 297 | return ret; |
| Jiyong Park | ccf00f8 | 2018-07-17 01:39:23 +0900 | [diff] [blame] | 298 | } |
| 299 | |
| Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 300 | string 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 Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 309 | bool AidlTypeSpecifier::Resolve(android::aidl::AidlTypenames& typenames) { |
| Steven Moreland | 9731c63 | 2019-08-13 10:21:08 -0700 | [diff] [blame] | 310 | CHECK(!IsResolved()); |
| Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 311 | pair<string, bool> result = typenames.ResolveTypename(unresolved_name_); |
| 312 | if (result.second) { |
| 313 | fully_qualified_name_ = result.first; |
| Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 314 | split_name_ = Split(fully_qualified_name_, "."); |
| Jiyong Park | ccf00f8 | 2018-07-17 01:39:23 +0900 | [diff] [blame] | 315 | } |
| Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 316 | return result.second; |
| Casey Dahlin | 70078e6 | 2015-09-30 17:01:30 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 319 | bool AidlTypeSpecifier::CheckValid(const AidlTypenames& typenames) const { |
| Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 320 | if (!CheckValidAnnotations()) { |
| 321 | return false; |
| 322 | } |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 323 | if (IsGeneric()) { |
| 324 | const string& type_name = GetName(); |
| 325 | const int num = GetTypeParameters().size(); |
| 326 | if (type_name == "List") { |
| 327 | if (num > 1) { |
| Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 328 | AIDL_ERROR(this) << " List cannot have type parameters more than one, but got " |
| 329 | << "'" << ToString() << "'"; |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 330 | return false; |
| 331 | } |
| 332 | } else if (type_name == "Map") { |
| 333 | if (num != 0 && num != 2) { |
| Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 334 | AIDL_ERROR(this) << "Map must have 0 or 2 type parameters, but got " |
| 335 | << "'" << ToString() << "'"; |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 336 | return false; |
| 337 | } |
| 338 | } |
| 339 | } |
| Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 340 | |
| 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 Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 362 | return true; |
| 363 | } |
| 364 | |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 365 | std::string AidlConstantValueDecorator(const AidlTypeSpecifier& /*type*/, |
| 366 | const std::string& raw_value) { |
| 367 | return raw_value; |
| 368 | } |
| 369 | |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 370 | AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location, |
| 371 | AidlTypeSpecifier* type, const std::string& name) |
| 372 | : AidlVariableDeclaration(location, type, name, nullptr /*default_value*/) {} |
| Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 373 | |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 374 | AidlVariableDeclaration::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 Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 378 | |
| Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 379 | bool AidlVariableDeclaration::CheckValid(const AidlTypenames& typenames) const { |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 380 | bool valid = true; |
| Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 381 | valid &= type_->CheckValid(typenames); |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 382 | |
| Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 383 | if (default_value_ == nullptr) return valid; |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 384 | valid &= default_value_->CheckValid(); |
| Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 385 | |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 386 | if (!valid) return false; |
| Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 387 | |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 388 | return !ValueString(AidlConstantValueDecorator).empty(); |
| Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 389 | } |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 390 | |
| 391 | string AidlVariableDeclaration::ToString() const { |
| Jeongik Cha | 3271ffa | 2018-12-04 15:19:20 +0900 | [diff] [blame] | 392 | string ret = type_->Signature() + " " + name_; |
| Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 393 | if (default_value_ != nullptr) { |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 394 | ret += " = " + ValueString(AidlConstantValueDecorator); |
| Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 395 | } |
| 396 | return ret; |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 399 | string AidlVariableDeclaration::Signature() const { |
| 400 | return type_->Signature() + " " + name_; |
| 401 | } |
| 402 | |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 403 | std::string AidlVariableDeclaration::ValueString(const ConstantValueDecorator& decorator) const { |
| Jiyong Park | a468e2a | 2018-08-29 21:25:18 +0900 | [diff] [blame] | 404 | if (default_value_ != nullptr) { |
| 405 | return GetDefaultValue()->As(GetType(), decorator); |
| 406 | } else { |
| 407 | return ""; |
| 408 | } |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 411 | AidlArgument::AidlArgument(const AidlLocation& location, AidlArgument::Direction direction, |
| 412 | AidlTypeSpecifier* type, const std::string& name) |
| 413 | : AidlVariableDeclaration(location, type, name), |
| Casey Dahlin | fd6fb48 | 2015-09-30 14:48:18 -0700 | [diff] [blame] | 414 | direction_(direction), |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 415 | direction_specified_(true) {} |
| Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 416 | |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 417 | AidlArgument::AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, |
| 418 | const std::string& name) |
| 419 | : AidlVariableDeclaration(location, type, name), |
| Casey Dahlin | fd6fb48 | 2015-09-30 14:48:18 -0700 | [diff] [blame] | 420 | direction_(AidlArgument::IN_DIR), |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 421 | direction_specified_(false) {} |
| Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 422 | |
| Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 423 | string AidlArgument::GetDirectionSpecifier() const { |
| Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 424 | string ret; |
| Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 425 | 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 Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 438 | return ret; |
| 439 | } |
| Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 440 | |
| Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 441 | string AidlArgument::ToString() const { |
| 442 | return GetDirectionSpecifier() + AidlVariableDeclaration::ToString(); |
| 443 | } |
| 444 | |
| 445 | std::string AidlArgument::Signature() const { |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 446 | class AidlInterface; |
| 447 | class AidlInterface; |
| 448 | class AidlParcelable; |
| 449 | class AidlStructuredParcelable; |
| 450 | class AidlParcelable; |
| 451 | class AidlStructuredParcelable; |
| Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 452 | return GetDirectionSpecifier() + AidlVariableDeclaration::Signature(); |
| 453 | } |
| 454 | |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 455 | AidlMember::AidlMember(const AidlLocation& location) : AidlNode(location) {} |
| 456 | |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 457 | AidlConstantValue::AidlConstantValue(const AidlLocation& location, Type type, |
| 458 | const std::string& checked_value) |
| Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 459 | : AidlNode(location), type_(type), value_(checked_value) { |
| 460 | CHECK(!value_.empty() || type_ == Type::ERROR); |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 461 | CHECK(type_ != Type::ARRAY); |
| Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 462 | } |
| Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 463 | |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 464 | AidlConstantValue::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 Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 468 | static 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 Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 474 | AidlConstantValue* AidlConstantValue::Boolean(const AidlLocation& location, bool value) { |
| 475 | return new AidlConstantValue(location, Type::BOOLEAN, value ? "true" : "false"); |
| 476 | } |
| 477 | |
| 478 | AidlConstantValue* AidlConstantValue::Character(const AidlLocation& location, char value) { |
| 479 | if (!isValidLiteralChar(value)) { |
| 480 | AIDL_ERROR(location) << "Invalid character literal " << value; |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 481 | return new AidlConstantValue(location, Type::ERROR, ""); |
| Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 482 | } |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 483 | return new AidlConstantValue(location, Type::CHARACTER, std::string("'") + value + "'"); |
| Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 486 | AidlConstantValue* AidlConstantValue::Floating(const AidlLocation& location, |
| 487 | const std::string& value) { |
| 488 | return new AidlConstantValue(location, Type::FLOATING, value); |
| 489 | } |
| 490 | |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 491 | AidlConstantValue* AidlConstantValue::Hex(const AidlLocation& location, const std::string& value) { |
| 492 | return new AidlConstantValue(location, Type::HEXIDECIMAL, value); |
| 493 | } |
| 494 | |
| 495 | AidlConstantValue* AidlConstantValue::Integral(const AidlLocation& location, |
| 496 | const std::string& value) { |
| 497 | return new AidlConstantValue(location, Type::INTEGRAL, value); |
| 498 | } |
| 499 | |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 500 | AidlConstantValue* AidlConstantValue::Array( |
| 501 | const AidlLocation& location, std::vector<std::unique_ptr<AidlConstantValue>>* values) { |
| 502 | return new AidlConstantValue(location, Type::ARRAY, values); |
| 503 | } |
| 504 | |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 505 | AidlConstantValue* AidlConstantValue::String(const AidlLocation& location, |
| 506 | const std::string& value) { |
| Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 507 | for (size_t i = 0; i < value.length(); ++i) { |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 508 | if (!isValidLiteralChar(value[i])) { |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 509 | AIDL_ERROR(location) << "Found invalid character at index " << i << " in string constant '" |
| 510 | << value << "'"; |
| 511 | return new AidlConstantValue(location, Type::ERROR, ""); |
| Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
| Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 514 | |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 515 | return new AidlConstantValue(location, Type::STRING, value); |
| Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 518 | bool AidlConstantValue::CheckValid() const { |
| 519 | // error always logged during creation |
| 520 | return type_ != AidlConstantValue::Type::ERROR; |
| 521 | } |
| 522 | |
| Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 523 | static 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 Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 531 | string 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 Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 543 | |
| 544 | switch (type_) { |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 545 | 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 Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 563 | case AidlConstantValue::Type::BOOLEAN: |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 564 | if (type_string == "boolean") return decorator(type, value_); |
| Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 565 | goto mismatch_error; |
| 566 | case AidlConstantValue::Type::CHARACTER: |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 567 | if (type_string == "char") return decorator(type, value_); |
| Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 568 | 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 Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 576 | return decorator(type, std::to_string(parsed_value)); |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 577 | } |
| Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 578 | if (is_float_literal && type_string == "float") { |
| 579 | float parsed_value; |
| 580 | if (!android::base::ParseFloat(raw_value, &parsed_value)) goto parse_error; |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 581 | return decorator(type, std::to_string(parsed_value) + "f"); |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 582 | } |
| 583 | goto mismatch_error; |
| Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 584 | } |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 585 | 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 Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 591 | return decorator(type, std::to_string((int8_t)unsigned_value)); |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 592 | } |
| 593 | if (type_string == "int") { |
| 594 | uint32_t unsigned_value; |
| 595 | if (!android::base::ParseUint<uint32_t>(value_, &unsigned_value)) goto parse_error; |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 596 | return decorator(type, std::to_string((int32_t)unsigned_value)); |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 597 | } |
| 598 | if (type_string == "long") { |
| 599 | uint64_t unsigned_value; |
| 600 | if (!android::base::ParseUint<uint64_t>(value_, &unsigned_value)) goto parse_error; |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 601 | return decorator(type, std::to_string((int64_t)unsigned_value)); |
| 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 | case AidlConstantValue::Type::INTEGRAL: |
| 605 | if (type_string == "byte") { |
| 606 | if (!android::base::ParseInt<int8_t>(value_, nullptr)) goto parse_error; |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 607 | return decorator(type, value_); |
| Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 608 | } |
| 609 | if (type_string == "int") { |
| 610 | if (!android::base::ParseInt<int32_t>(value_, nullptr)) goto parse_error; |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 611 | return decorator(type, value_); |
| Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 612 | } |
| 613 | if (type_string == "long") { |
| 614 | if (!android::base::ParseInt<int64_t>(value_, nullptr)) goto parse_error; |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 615 | return decorator(type, value_); |
| Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 616 | } |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 617 | goto mismatch_error; |
| 618 | case AidlConstantValue::Type::STRING: |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 619 | if (type_string == "String") return decorator(type, value_); |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 620 | goto mismatch_error; |
| 621 | default: |
| 622 | AIDL_FATAL(this) << "Unrecognized constant value type"; |
| 623 | } |
| 624 | |
| 625 | mismatch_error: |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 626 | AIDL_ERROR(this) << "Expecting type " << type_string << " but constant is " << ToString(type_); |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 627 | return ""; |
| 628 | parse_error: |
| 629 | AIDL_ERROR(this) << "Could not parse " << value_ << " as " << type_string; |
| 630 | return ""; |
| 631 | } |
| 632 | |
| 633 | string AidlConstantValue::ToString(Type type) { |
| 634 | switch (type) { |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 635 | case Type::ARRAY: |
| 636 | return "a literal array"; |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 637 | case Type::BOOLEAN: |
| 638 | return "a literal boolean"; |
| 639 | case Type::CHARACTER: |
| 640 | return "a literal char"; |
| Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 641 | case Type::FLOATING: |
| 642 | return "a floating-point literal"; |
| 643 | case Type::HEXIDECIMAL: |
| 644 | return "a hexidecimal literal"; |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 645 | 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 Wang | 35b7bb6 | 2018-10-09 13:06:12 -0700 | [diff] [blame] | 651 | return ""; |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 652 | default: |
| 653 | LOG(FATAL) << "aidl internal error: unknown constant type: " << static_cast<int>(type); |
| 654 | return ""; // not reached |
| 655 | } |
| Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 656 | } |
| 657 | |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 658 | AidlConstantDeclaration::AidlConstantDeclaration(const AidlLocation& location, |
| 659 | AidlTypeSpecifier* type, const std::string& name, |
| 660 | AidlConstantValue* value) |
| 661 | : AidlMember(location), type_(type), name_(name), value_(value) {} |
| Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 662 | |
| Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 663 | bool AidlConstantDeclaration::CheckValid(const AidlTypenames& typenames) const { |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 664 | bool valid = true; |
| Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 665 | valid &= type_->CheckValid(typenames); |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 666 | valid &= value_->CheckValid(); |
| 667 | if (!valid) return false; |
| Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 668 | |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 669 | 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 Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 672 | return false; |
| 673 | } |
| 674 | |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 675 | return !ValueString(AidlConstantValueDecorator).empty(); |
| Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 678 | string AidlConstantDeclaration::ToString() const { |
| 679 | return "const " + type_->ToString() + " " + name_ + " = " + |
| 680 | ValueString(AidlConstantValueDecorator); |
| 681 | } |
| 682 | |
| 683 | string AidlConstantDeclaration::Signature() const { |
| 684 | return type_->Signature() + " " + name_; |
| 685 | } |
| 686 | |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 687 | AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, |
| 688 | const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args, |
| Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 689 | const std::string& comments) |
| 690 | : AidlMethod(location, oneway, type, name, args, comments, 0, true) { |
| 691 | has_id_ = false; |
| 692 | } |
| 693 | |
| 694 | AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, |
| 695 | const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args, |
| Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 696 | const std::string& comments, int id, bool is_user_defined) |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 697 | : AidlMember(location), |
| 698 | oneway_(oneway), |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 699 | comments_(comments), |
| 700 | type_(type), |
| 701 | name_(name), |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 702 | arguments_(std::move(*args)), |
| Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 703 | id_(id), |
| 704 | is_user_defined_(is_user_defined) { |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 705 | has_id_ = true; |
| 706 | delete args; |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 707 | 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 Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 713 | |
| Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 714 | string AidlMethod::Signature() const { |
| 715 | vector<string> arg_signatures; |
| 716 | for (const auto& arg : GetArguments()) { |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 717 | arg_signatures.emplace_back(arg->GetType().ToString()); |
| Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 718 | } |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 719 | return GetName() + "(" + Join(arg_signatures, ", ") + ")"; |
| 720 | } |
| 721 | |
| 722 | string AidlMethod::ToString() const { |
| 723 | vector<string> arg_strings; |
| 724 | for (const auto& arg : GetArguments()) { |
| 725 | arg_strings.emplace_back(arg->Signature()); |
| 726 | } |
| Steven Moreland | 4ee6863 | 2018-12-14 15:52:46 -0800 | [diff] [blame] | 727 | string ret = (IsOneway() ? "oneway " : "") + GetType().Signature() + " " + GetName() + "(" + |
| 728 | Join(arg_strings, ", ") + ")"; |
| Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 729 | if (HasId()) { |
| 730 | ret += " = " + std::to_string(GetId()); |
| 731 | } |
| 732 | return ret; |
| Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 733 | } |
| 734 | |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 735 | AidlDefinedType::AidlDefinedType(const AidlLocation& location, const std::string& name, |
| 736 | const std::string& comments, |
| Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 737 | const std::vector<std::string>& package) |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 738 | : AidlAnnotatable(location), name_(name), comments_(comments), package_(package) {} |
| Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 739 | |
| 740 | std::string AidlDefinedType::GetPackage() const { |
| 741 | return Join(package_, '.'); |
| 742 | } |
| 743 | |
| 744 | std::string AidlDefinedType::GetCanonicalName() const { |
| 745 | if (package_.empty()) { |
| 746 | return GetName(); |
| 747 | } |
| 748 | return GetPackage() + "." + GetName(); |
| 749 | } |
| 750 | |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 751 | AidlParcelable::AidlParcelable(const AidlLocation& location, AidlQualifiedName* name, |
| Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 752 | const std::vector<std::string>& package, const std::string& comments, |
| Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 753 | const std::string& cpp_header) |
| Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 754 | : AidlDefinedType(location, name->GetDotName(), comments, package), |
| Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 755 | name_(name), |
| Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 756 | 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 Dahlin | 59401da | 2015-10-09 18:16:45 -0700 | [diff] [blame] | 761 | } |
| 762 | |
| Jeongik Cha | 82317dd | 2019-02-27 20:26:11 +0900 | [diff] [blame] | 763 | bool AidlParcelable::CheckValid(const AidlTypenames&) const { |
| 764 | static const std::set<string> allowed{kStableParcelable}; |
| Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 765 | if (!CheckValidAnnotations()) { |
| 766 | return false; |
| 767 | } |
| Jeongik Cha | 82317dd | 2019-02-27 20:26:11 +0900 | [diff] [blame] | 768 | 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 Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 784 | void AidlParcelable::Write(CodeWriter* writer) const { |
| 785 | writer->Write("parcelable %s ;\n", GetName().c_str()); |
| 786 | } |
| 787 | |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 788 | AidlStructuredParcelable::AidlStructuredParcelable( |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 789 | const AidlLocation& location, AidlQualifiedName* name, const std::vector<std::string>& package, |
| Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 790 | const std::string& comments, std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables) |
| 791 | : AidlParcelable(location, name, package, comments, "" /*cpp_header*/), |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 792 | variables_(std::move(*variables)) {} |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 793 | |
| Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 794 | void AidlStructuredParcelable::Write(CodeWriter* writer) const { |
| 795 | writer->Write("parcelable %s {\n", GetName().c_str()); |
| 796 | writer->Indent(); |
| 797 | for (const auto& field : GetFields()) { |
| Jiyong Park | a468e2a | 2018-08-29 21:25:18 +0900 | [diff] [blame] | 798 | writer->Write("%s;\n", field->ToString().c_str()); |
| Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 799 | } |
| 800 | writer->Dedent(); |
| 801 | writer->Write("}\n"); |
| 802 | } |
| 803 | |
| Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 804 | bool AidlStructuredParcelable::CheckValid(const AidlTypenames& typenames) const { |
| Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 805 | bool success = true; |
| Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 806 | for (const auto& v : GetFields()) { |
| Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 807 | success = success && v->CheckValid(typenames); |
| Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 808 | } |
| Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 809 | return success; |
| Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 810 | } |
| 811 | |
| Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 812 | // TODO: we should treat every backend all the same in future. |
| 813 | bool 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. |
| 848 | bool 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. |
| 867 | bool 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 Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 879 | AidlEnumerator::AidlEnumerator(const AidlLocation& location, const std::string& name, |
| 880 | AidlConstantValue* value) |
| 881 | : AidlNode(location), name_(name), value_(value) {} |
| 882 | |
| 883 | bool 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 | |
| 897 | string AidlEnumerator::ValueString(const AidlTypeSpecifier& backing_type, |
| 898 | const ConstantValueDecorator& decorator) const { |
| 899 | return GetValue()->As(backing_type, decorator); |
| 900 | } |
| 901 | |
| 902 | AidlEnumDeclaration::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 | |
| 907 | void AidlEnumDeclaration::SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type) { |
| 908 | backing_type_ = std::move(type); |
| 909 | } |
| 910 | |
| 911 | bool 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 | |
| 923 | void 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 Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 937 | // TODO: we should treat every backend all the same in future. |
| 938 | bool 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 Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 952 | AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name, |
| Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 953 | const std::string& comments, bool oneway, |
| Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 954 | std::vector<std::unique_ptr<AidlMember>>* members, |
| Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 955 | const std::vector<std::string>& package) |
| Steven Moreland | acd5347 | 2018-12-14 10:17:26 -0800 | [diff] [blame] | 956 | : AidlDefinedType(location, name, comments, package) { |
| Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 957 | for (auto& member : *members) { |
| 958 | AidlMember* local = member.release(); |
| 959 | AidlMethod* method = local->AsMethod(); |
| Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 960 | AidlConstantDeclaration* constant = local->AsConstantDeclaration(); |
| 961 | |
| 962 | CHECK(method == nullptr || constant == nullptr); |
| Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 963 | |
| 964 | if (method) { |
| Steven Moreland | 8c70ba9 | 2018-12-17 10:20:31 -0800 | [diff] [blame] | 965 | method->ApplyInterfaceOneway(oneway); |
| Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 966 | methods_.emplace_back(method); |
| Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 967 | } else if (constant) { |
| 968 | constants_.emplace_back(constant); |
| Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 969 | } else { |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 970 | AIDL_FATAL(this) << "Member is neither method nor constant!"; |
| Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | |
| 974 | delete members; |
| Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 975 | } |
| 976 | |
| Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 977 | void AidlInterface::Write(CodeWriter* writer) const { |
| 978 | writer->Write("interface %s {\n", GetName().c_str()); |
| 979 | writer->Indent(); |
| 980 | for (const auto& method : GetMethods()) { |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 981 | writer->Write("%s;\n", method->ToString().c_str()); |
| Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 982 | } |
| Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 983 | for (const auto& constdecl : GetConstantDeclarations()) { |
| 984 | writer->Write("%s;\n", constdecl->ToString().c_str()); |
| 985 | } |
| Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 986 | writer->Dedent(); |
| 987 | writer->Write("}\n"); |
| 988 | } |
| 989 | |
| Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 990 | bool AidlInterface::CheckValid(const AidlTypenames& typenames) const { |
| Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 991 | if (!CheckValidAnnotations()) { |
| 992 | return false; |
| 993 | } |
| Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 994 | // 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 Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 997 | if (!m->GetType().CheckValid(typenames)) { |
| 998 | return false; |
| 999 | } |
| 1000 | |
| Steven Moreland | acd5347 | 2018-12-14 10:17:26 -0800 | [diff] [blame] | 1001 | if (m->IsOneway() && m->GetType().GetName() != "void") { |
| Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1002 | 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 Moreland | acd5347 | 2018-12-14 10:17:26 -0800 | [diff] [blame] | 1020 | if (m->IsOneway() && arg->IsOut()) { |
| Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1021 | AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot have out parameters"; |
| 1022 | return false; |
| 1023 | } |
| Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1024 | 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 Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1047 | } |
| 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 Moreland | 4d12f9a | 2018-10-31 14:30:55 -0700 | [diff] [blame] | 1067 | |
| 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 Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1080 | } |
| 1081 | |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1082 | AidlQualifiedName::AidlQualifiedName(const AidlLocation& location, const std::string& term, |
| 1083 | const std::string& comments) |
| 1084 | : AidlNode(location), terms_({term}), comments_(comments) { |
| Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 1085 | if (term.find('.') != string::npos) { |
| 1086 | terms_ = Split(term, "."); |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1087 | for (const auto& subterm : terms_) { |
| 1088 | if (subterm.empty()) { |
| 1089 | AIDL_FATAL(this) << "Malformed qualified identifier: '" << term << "'"; |
| Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 1090 | } |
| 1091 | } |
| 1092 | } |
| Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
| Chih-Hung Hsieh | f05cc26 | 2016-07-27 11:42:51 -0700 | [diff] [blame] | 1095 | void AidlQualifiedName::AddTerm(const std::string& term) { |
| Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 1096 | terms_.push_back(term); |
| 1097 | } |
| 1098 | |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1099 | AidlImport::AidlImport(const AidlLocation& location, const std::string& needed_class) |
| 1100 | : AidlNode(location), needed_class_(needed_class) {} |
| Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 1101 | |
| Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1102 | std::unique_ptr<Parser> Parser::Parse(const std::string& filename, |
| 1103 | const android::aidl::IoDelegate& io_delegate, |
| 1104 | AidlTypenames& typenames) { |
| Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1105 | // Make sure we can read the file first, before trashing previous state. |
| Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1106 | unique_ptr<string> raw_buffer = io_delegate.GetFileContents(filename); |
| 1107 | if (raw_buffer == nullptr) { |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1108 | AIDL_ERROR(filename) << "Error while opening file for parsing"; |
| Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1109 | return nullptr; |
| Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
| Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1112 | // We're going to scan this buffer in place, and yacc demands we put two |
| 1113 | // nulls at the end. |
| Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1114 | raw_buffer->append(2u, '\0'); |
| Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1115 | |
| Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1116 | std::unique_ptr<Parser> parser(new Parser(filename, *raw_buffer, typenames)); |
| Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1117 | |
| Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1118 | if (yy::parser(parser.get()).parse() != 0 || parser->HasError()) return nullptr; |
| Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1119 | |
| Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1120 | return parser; |
| Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
| Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 1123 | std::vector<std::string> Parser::Package() const { |
| 1124 | if (!package_) { |
| 1125 | return {}; |
| 1126 | } |
| 1127 | return package_->GetTerms(); |
| 1128 | } |
| 1129 | |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1130 | void Parser::AddImport(AidlImport* import) { |
| 1131 | imports_.emplace_back(import); |
| Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 1132 | } |
| Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 1133 | |
| 1134 | bool Parser::Resolve() { |
| 1135 | bool success = true; |
| 1136 | for (AidlTypeSpecifier* typespec : unresolved_typespecs_) { |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1137 | if (!typespec->Resolve(typenames_)) { |
| Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1138 | AIDL_ERROR(typespec) << "Failed to resolve '" << typespec->GetUnresolvedName() << "'"; |
| Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 1139 | success = false; |
| 1140 | // don't stop to show more errors if any |
| 1141 | } |
| 1142 | } |
| 1143 | return success; |
| 1144 | } |
| Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1145 | |
| 1146 | Parser::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 | |
| 1153 | Parser::~Parser() { |
| 1154 | yy_delete_buffer(buffer_, scanner_); |
| 1155 | yylex_destroy(scanner_); |
| 1156 | } |