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