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