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 | 698e6af | 2018-12-12 14:39:55 +0900 | [diff] [blame] | 131 | static const string kSystemApi("SystemApi"); |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 132 | static const string kJavaStableParcelable("JavaOnlyStableParcelable"); |
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"}}}, |
| 145 | {kSystemApi, {}}, |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 146 | {kJavaStableParcelable, {}}, |
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 | 698e6af | 2018-12-12 14:39:55 +0900 | [diff] [blame] | 303 | bool AidlAnnotatable::IsSystemApi() const { |
| 304 | return HasAnnotation(annotations_, kSystemApi); |
| 305 | } |
| 306 | |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 307 | bool AidlAnnotatable::IsStableApiParcelable(Options::Language lang) const { |
| 308 | return HasAnnotation(annotations_, kJavaStableParcelable) && lang == Options::Language::JAVA; |
Jeongik Cha | 82317dd | 2019-02-27 20:26:11 +0900 | [diff] [blame] | 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 | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 686 | AidlParcelable::AidlParcelable(const AidlLocation& location, AidlQualifiedName* name, |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 687 | const std::vector<std::string>& package, const std::string& comments, |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 688 | const std::string& cpp_header, std::vector<std::string>* type_params) |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 689 | : AidlDefinedType(location, name->GetDotName(), comments, package), |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 690 | AidlParameterizable<std::string>(type_params), |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 691 | name_(name), |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 692 | cpp_header_(cpp_header) { |
| 693 | // Strip off quotation marks if we actually have a cpp header. |
| 694 | if (cpp_header_.length() >= 2) { |
| 695 | cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2); |
| 696 | } |
Casey Dahlin | 59401da | 2015-10-09 18:16:45 -0700 | [diff] [blame] | 697 | } |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 698 | template <typename T> |
| 699 | AidlParameterizable<T>::AidlParameterizable(const AidlParameterizable& other) { |
| 700 | // Copying is not supported if it has type parameters. |
| 701 | // It doesn't make a problem because only ArrayBase() makes a copy, |
| 702 | // and it can be called only if a type is not generic. |
| 703 | CHECK(!other.IsGeneric()); |
| 704 | } |
| 705 | |
| 706 | template <typename T> |
| 707 | bool AidlParameterizable<T>::CheckValid() const { |
| 708 | return true; |
| 709 | }; |
| 710 | |
| 711 | template <> |
| 712 | bool AidlParameterizable<std::string>::CheckValid() const { |
| 713 | if (!IsGeneric()) { |
| 714 | return true; |
| 715 | } |
| 716 | std::unordered_set<std::string> set(GetTypeParameters().begin(), GetTypeParameters().end()); |
| 717 | if (set.size() != GetTypeParameters().size()) { |
| 718 | AIDL_ERROR(this->AsAidlNode()) << "Every type parameter should be unique."; |
| 719 | return false; |
| 720 | } |
| 721 | return true; |
| 722 | } |
Casey Dahlin | 59401da | 2015-10-09 18:16:45 -0700 | [diff] [blame] | 723 | |
Jeongik Cha | 82317dd | 2019-02-27 20:26:11 +0900 | [diff] [blame] | 724 | bool AidlParcelable::CheckValid(const AidlTypenames&) const { |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 725 | static const std::set<string> allowed{kJavaStableParcelable}; |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 726 | if (!CheckValidAnnotations()) { |
| 727 | return false; |
| 728 | } |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 729 | if (!AidlParameterizable<std::string>::CheckValid()) { |
| 730 | return false; |
| 731 | } |
Jeongik Cha | 82317dd | 2019-02-27 20:26:11 +0900 | [diff] [blame] | 732 | for (const auto& v : GetAnnotations()) { |
| 733 | if (allowed.find(v.GetName()) == allowed.end()) { |
| 734 | std::ostringstream stream; |
| 735 | stream << "Unstructured parcelable can contain only"; |
| 736 | for (const string& kv : allowed) { |
| 737 | stream << " " << kv; |
| 738 | } |
| 739 | stream << "."; |
| 740 | AIDL_ERROR(this) << stream.str(); |
| 741 | return false; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | return true; |
| 746 | } |
| 747 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 748 | void AidlParcelable::Dump(CodeWriter* writer) const { |
Steven Moreland | 71998bf | 2020-02-20 18:10:42 -0800 | [diff] [blame^] | 749 | DumpAnnotations(writer); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 750 | writer->Write("parcelable %s ;\n", GetName().c_str()); |
| 751 | } |
| 752 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 753 | AidlStructuredParcelable::AidlStructuredParcelable( |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 754 | const AidlLocation& location, AidlQualifiedName* name, const std::vector<std::string>& package, |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 755 | const std::string& comments, std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables) |
| 756 | : AidlParcelable(location, name, package, comments, "" /*cpp_header*/), |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 757 | variables_(std::move(*variables)) {} |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 758 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 759 | void AidlStructuredParcelable::Dump(CodeWriter* writer) const { |
| 760 | if (this->IsHidden()) { |
| 761 | AddHideComment(writer); |
| 762 | } |
Steven Moreland | 71998bf | 2020-02-20 18:10:42 -0800 | [diff] [blame^] | 763 | DumpAnnotations(writer); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 764 | writer->Write("parcelable %s {\n", GetName().c_str()); |
| 765 | writer->Indent(); |
| 766 | for (const auto& field : GetFields()) { |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 767 | if (field->GetType().IsHidden()) { |
| 768 | AddHideComment(writer); |
| 769 | } |
Jiyong Park | a468e2a | 2018-08-29 21:25:18 +0900 | [diff] [blame] | 770 | writer->Write("%s;\n", field->ToString().c_str()); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 771 | } |
| 772 | writer->Dedent(); |
| 773 | writer->Write("}\n"); |
| 774 | } |
| 775 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 776 | bool AidlStructuredParcelable::CheckValid(const AidlTypenames& typenames) const { |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 777 | bool success = true; |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 778 | for (const auto& v : GetFields()) { |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 779 | success = success && v->CheckValid(typenames); |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 780 | } |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 781 | return success; |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 782 | } |
| 783 | |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 784 | // TODO: we should treat every backend all the same in future. |
| 785 | bool AidlTypeSpecifier::LanguageSpecificCheckValid(Options::Language lang) const { |
Steven Moreland | 04b07e1 | 2020-01-21 18:11:07 -0800 | [diff] [blame] | 786 | if (lang != Options::Language::JAVA) { |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 787 | if (this->GetName() == "List" && !this->IsGeneric()) { |
Steven Moreland | 04b07e1 | 2020-01-21 18:11:07 -0800 | [diff] [blame] | 788 | AIDL_ERROR(this) << "Currently, only the Java backend supports non-generic List."; |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 789 | return false; |
| 790 | } |
| 791 | } |
Steven Moreland | c8a4ca8 | 2020-01-21 17:50:08 -0800 | [diff] [blame] | 792 | if (this->GetName() == "FileDescriptor" && lang == Options::Language::NDK) { |
| 793 | AIDL_ERROR(this) << "FileDescriptor isn't supported with the NDK."; |
| 794 | return false; |
| 795 | } |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 796 | if (this->IsGeneric()) { |
| 797 | if (this->GetName() == "List") { |
| 798 | if (this->GetTypeParameters().size() != 1) { |
| 799 | AIDL_ERROR(this) << "List must have only one type parameter."; |
| 800 | return false; |
| 801 | } |
| 802 | if (lang == Options::Language::CPP) { |
| 803 | auto& name = this->GetTypeParameters()[0]->GetName(); |
| 804 | if (!(name == "String" || name == "IBinder")) { |
| 805 | AIDL_ERROR(this) << "List in cpp supports only string and IBinder for now."; |
| 806 | return false; |
| 807 | } |
Jeongik Cha | 08ca218 | 2019-11-21 14:01:13 +0900 | [diff] [blame] | 808 | } else if (lang == Options::Language::JAVA) { |
| 809 | const string& contained_type = this->GetTypeParameters()[0]->GetName(); |
| 810 | if (AidlTypenames::IsBuiltinTypename(contained_type)) { |
| 811 | if (contained_type != "String" && contained_type != "IBinder" && |
| 812 | contained_type != "ParcelFileDescriptor") { |
| 813 | AIDL_ERROR(this) << "List<" << contained_type << "> isn't supported in Java"; |
| 814 | return false; |
| 815 | } |
| 816 | } |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 817 | } |
Jeongik Cha | bb55b5e | 2020-01-07 23:11:26 +0900 | [diff] [blame] | 818 | } |
| 819 | } |
Jeongik Cha | b75a451 | 2020-01-10 13:37:04 +0900 | [diff] [blame] | 820 | if (this->GetName() == "Map" || this->GetName() == "CharSequence") { |
Jeongik Cha | bb55b5e | 2020-01-07 23:11:26 +0900 | [diff] [blame] | 821 | if (lang != Options::Language::JAVA) { |
Jeongik Cha | b75a451 | 2020-01-10 13:37:04 +0900 | [diff] [blame] | 822 | AIDL_ERROR(this) << "Currently, only Java backend supports " << this->GetName() << "."; |
Jeongik Cha | bb55b5e | 2020-01-07 23:11:26 +0900 | [diff] [blame] | 823 | return false; |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 824 | } |
| 825 | } |
Jeongik Cha | 08ca218 | 2019-11-21 14:01:13 +0900 | [diff] [blame] | 826 | if (lang == Options::Language::JAVA) { |
| 827 | const string name = this->GetName(); |
| 828 | // List[], Map[], CharSequence[] are not supported. |
| 829 | if (AidlTypenames::IsBuiltinTypename(name) && this->IsArray()) { |
| 830 | if (name == "List" || name == "Map" || name == "CharSequence") { |
| 831 | AIDL_ERROR(this) << "List[], Map[], CharSequence[] are not supported."; |
| 832 | return false; |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 837 | return true; |
| 838 | } |
| 839 | |
| 840 | // TODO: we should treat every backend all the same in future. |
| 841 | bool AidlParcelable::LanguageSpecificCheckValid(Options::Language lang) const { |
| 842 | if (lang != Options::Language::JAVA) { |
Steven Moreland | 0d9c26e | 2020-01-22 08:52:08 -0800 | [diff] [blame] | 843 | const AidlParcelable* unstructured_parcelable = this->AsUnstructuredParcelable(); |
| 844 | if (unstructured_parcelable != nullptr) { |
| 845 | if (unstructured_parcelable->GetCppHeader().empty()) { |
| 846 | AIDL_ERROR(unstructured_parcelable) |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 847 | << "Unstructured parcelable must have C++ header defined."; |
| 848 | return false; |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | return true; |
| 853 | } |
| 854 | |
| 855 | // TODO: we should treat every backend all the same in future. |
| 856 | bool AidlStructuredParcelable::LanguageSpecificCheckValid(Options::Language lang) const { |
| 857 | if (!AidlParcelable::LanguageSpecificCheckValid(lang)) { |
| 858 | return false; |
| 859 | } |
| 860 | for (const auto& v : this->GetFields()) { |
| 861 | if (!v->GetType().LanguageSpecificCheckValid(lang)) { |
| 862 | return false; |
| 863 | } |
| 864 | } |
| 865 | return true; |
| 866 | } |
| 867 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 868 | AidlEnumerator::AidlEnumerator(const AidlLocation& location, const std::string& name, |
Daniel Norman | 2e4112d | 2019-10-03 10:22:35 -0700 | [diff] [blame] | 869 | AidlConstantValue* value, const std::string& comments) |
| 870 | : AidlNode(location), name_(name), value_(value), comments_(comments) {} |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 871 | |
| 872 | bool AidlEnumerator::CheckValid(const AidlTypeSpecifier& enum_backing_type) const { |
| 873 | if (GetValue() == nullptr) { |
| 874 | return false; |
| 875 | } |
| 876 | if (!GetValue()->CheckValid()) { |
| 877 | return false; |
| 878 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 879 | if (GetValue()->ValueString(enum_backing_type, AidlConstantValueDecorator).empty()) { |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 880 | AIDL_ERROR(this) << "Enumerator type differs from enum backing type."; |
| 881 | return false; |
| 882 | } |
| 883 | return true; |
| 884 | } |
| 885 | |
| 886 | string AidlEnumerator::ValueString(const AidlTypeSpecifier& backing_type, |
| 887 | const ConstantValueDecorator& decorator) const { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 888 | return GetValue()->ValueString(backing_type, decorator); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | AidlEnumDeclaration::AidlEnumDeclaration(const AidlLocation& location, const std::string& name, |
| 892 | std::vector<std::unique_ptr<AidlEnumerator>>* enumerators, |
Daniel Norman | 2e4112d | 2019-10-03 10:22:35 -0700 | [diff] [blame] | 893 | const std::vector<std::string>& package, |
| 894 | const std::string& comments) |
| 895 | : AidlDefinedType(location, name, comments, package), enumerators_(std::move(*enumerators)) {} |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 896 | |
| 897 | void AidlEnumDeclaration::SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type) { |
| 898 | backing_type_ = std::move(type); |
| 899 | } |
| 900 | |
Steven Moreland | 59e53e4 | 2019-11-26 20:38:08 -0800 | [diff] [blame] | 901 | bool AidlEnumDeclaration::Autofill() { |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 902 | const AidlEnumerator* previous = nullptr; |
| 903 | for (const auto& enumerator : enumerators_) { |
| 904 | if (enumerator->GetValue() == nullptr) { |
| 905 | if (previous == nullptr) { |
| 906 | enumerator->SetValue(std::unique_ptr<AidlConstantValue>( |
| 907 | AidlConstantValue::Integral(AIDL_LOCATION_HERE, "0"))); |
| 908 | } else { |
Steven Moreland | 59e53e4 | 2019-11-26 20:38:08 -0800 | [diff] [blame] | 909 | auto prev_value = std::unique_ptr<AidlConstantValue>( |
| 910 | AidlConstantValue::ShallowIntegralCopy(*previous->GetValue())); |
| 911 | if (prev_value == nullptr) { |
| 912 | return false; |
| 913 | } |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 914 | enumerator->SetValue(std::make_unique<AidlBinaryConstExpression>( |
Steven Moreland | 59e53e4 | 2019-11-26 20:38:08 -0800 | [diff] [blame] | 915 | AIDL_LOCATION_HERE, std::move(prev_value), "+", |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 916 | std::unique_ptr<AidlConstantValue>( |
| 917 | AidlConstantValue::Integral(AIDL_LOCATION_HERE, "1")))); |
| 918 | } |
| 919 | } |
| 920 | previous = enumerator.get(); |
| 921 | } |
Steven Moreland | 59e53e4 | 2019-11-26 20:38:08 -0800 | [diff] [blame] | 922 | return true; |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 925 | bool AidlEnumDeclaration::CheckValid(const AidlTypenames&) const { |
| 926 | if (backing_type_ == nullptr) { |
| 927 | AIDL_ERROR(this) << "Enum declaration missing backing type."; |
| 928 | return false; |
| 929 | } |
| 930 | bool success = true; |
| 931 | for (const auto& enumerator : enumerators_) { |
| 932 | success = success && enumerator->CheckValid(GetBackingType()); |
| 933 | } |
| 934 | return success; |
| 935 | } |
| 936 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 937 | void AidlEnumDeclaration::Dump(CodeWriter* writer) const { |
Steven Moreland | 71998bf | 2020-02-20 18:10:42 -0800 | [diff] [blame^] | 938 | DumpAnnotations(writer); |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 939 | writer->Write("enum %s {\n", GetName().c_str()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 940 | writer->Indent(); |
| 941 | for (const auto& enumerator : GetEnumerators()) { |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 942 | writer->Write("%s = %s,\n", enumerator->GetName().c_str(), |
| 943 | enumerator->ValueString(GetBackingType(), AidlConstantValueDecorator).c_str()); |
| 944 | } |
| 945 | writer->Dedent(); |
| 946 | writer->Write("}\n"); |
| 947 | } |
| 948 | |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 949 | // TODO: we should treat every backend all the same in future. |
| 950 | bool AidlInterface::LanguageSpecificCheckValid(Options::Language lang) const { |
| 951 | for (const auto& m : this->GetMethods()) { |
| 952 | if (!m->GetType().LanguageSpecificCheckValid(lang)) { |
| 953 | return false; |
| 954 | } |
| 955 | for (const auto& arg : m->GetArguments()) { |
| 956 | if (!arg->GetType().LanguageSpecificCheckValid(lang)) { |
| 957 | return false; |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | return true; |
| 962 | } |
| 963 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 964 | AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name, |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 965 | const std::string& comments, bool oneway, |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 966 | std::vector<std::unique_ptr<AidlMember>>* members, |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 967 | const std::vector<std::string>& package) |
Steven Moreland | acd5347 | 2018-12-14 10:17:26 -0800 | [diff] [blame] | 968 | : AidlDefinedType(location, name, comments, package) { |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 969 | for (auto& member : *members) { |
| 970 | AidlMember* local = member.release(); |
| 971 | AidlMethod* method = local->AsMethod(); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 972 | AidlConstantDeclaration* constant = local->AsConstantDeclaration(); |
| 973 | |
| 974 | CHECK(method == nullptr || constant == nullptr); |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 975 | |
| 976 | if (method) { |
Steven Moreland | 8c70ba9 | 2018-12-17 10:20:31 -0800 | [diff] [blame] | 977 | method->ApplyInterfaceOneway(oneway); |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 978 | methods_.emplace_back(method); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 979 | } else if (constant) { |
| 980 | constants_.emplace_back(constant); |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 981 | } else { |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 982 | AIDL_FATAL(this) << "Member is neither method nor constant!"; |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 983 | } |
| 984 | } |
| 985 | |
| 986 | delete members; |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 987 | } |
| 988 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 989 | void AidlInterface::Dump(CodeWriter* writer) const { |
| 990 | if (this->IsHidden()) { |
| 991 | AddHideComment(writer); |
| 992 | } |
Steven Moreland | 71998bf | 2020-02-20 18:10:42 -0800 | [diff] [blame^] | 993 | DumpAnnotations(writer); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 994 | writer->Write("interface %s {\n", GetName().c_str()); |
| 995 | writer->Indent(); |
| 996 | for (const auto& method : GetMethods()) { |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 997 | if (method->IsHidden()) { |
| 998 | AddHideComment(writer); |
| 999 | } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1000 | writer->Write("%s;\n", method->ToString().c_str()); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1001 | } |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 1002 | for (const auto& constdecl : GetConstantDeclarations()) { |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1003 | if (constdecl->GetType().IsHidden()) { |
| 1004 | AddHideComment(writer); |
| 1005 | } |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 1006 | writer->Write("%s;\n", constdecl->ToString().c_str()); |
| 1007 | } |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1008 | writer->Dedent(); |
| 1009 | writer->Write("}\n"); |
| 1010 | } |
| 1011 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1012 | bool AidlInterface::CheckValid(const AidlTypenames& typenames) const { |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 1013 | if (!CheckValidAnnotations()) { |
| 1014 | return false; |
| 1015 | } |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1016 | // Has to be a pointer due to deleting copy constructor. No idea why. |
| 1017 | map<string, const AidlMethod*> method_names; |
| 1018 | for (const auto& m : GetMethods()) { |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1019 | if (!m->GetType().CheckValid(typenames)) { |
| 1020 | return false; |
| 1021 | } |
| 1022 | |
Steven Moreland | acd5347 | 2018-12-14 10:17:26 -0800 | [diff] [blame] | 1023 | if (m->IsOneway() && m->GetType().GetName() != "void") { |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1024 | AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot return a value"; |
| 1025 | return false; |
| 1026 | } |
| 1027 | |
| 1028 | set<string> argument_names; |
| 1029 | for (const auto& arg : m->GetArguments()) { |
| 1030 | auto it = argument_names.find(arg->GetName()); |
| 1031 | if (it != argument_names.end()) { |
| 1032 | AIDL_ERROR(m) << "method '" << m->GetName() << "' has duplicate argument name '" |
| 1033 | << arg->GetName() << "'"; |
| 1034 | return false; |
| 1035 | } |
| 1036 | argument_names.insert(arg->GetName()); |
| 1037 | |
| 1038 | if (!arg->GetType().CheckValid(typenames)) { |
| 1039 | return false; |
| 1040 | } |
| 1041 | |
Steven Moreland | acd5347 | 2018-12-14 10:17:26 -0800 | [diff] [blame] | 1042 | if (m->IsOneway() && arg->IsOut()) { |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1043 | AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot have out parameters"; |
| 1044 | return false; |
| 1045 | } |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1046 | const bool can_be_out = typenames.CanBeOutParameter(arg->GetType()); |
| 1047 | if (!arg->DirectionWasSpecified() && can_be_out) { |
| 1048 | AIDL_ERROR(arg) << "'" << arg->GetType().ToString() |
| 1049 | << "' can be an out type, so you must declare it as in, out, or inout."; |
| 1050 | return false; |
| 1051 | } |
| 1052 | |
| 1053 | if (arg->GetDirection() != AidlArgument::IN_DIR && !can_be_out) { |
| 1054 | AIDL_ERROR(arg) << "'" << arg->ToString() << "' can only be an in parameter."; |
| 1055 | return false; |
| 1056 | } |
| 1057 | |
| 1058 | // check that the name doesn't match a keyword |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1059 | if (IsJavaKeyword(arg->GetName().c_str())) { |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1060 | AIDL_ERROR(arg) << "Argument name is a Java or aidl keyword"; |
| 1061 | return false; |
| 1062 | } |
| 1063 | |
| 1064 | // Reserve a namespace for internal use |
| 1065 | if (android::base::StartsWith(arg->GetName(), "_aidl")) { |
| 1066 | AIDL_ERROR(arg) << "Argument name cannot begin with '_aidl'"; |
| 1067 | return false; |
| 1068 | } |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | auto it = method_names.find(m->GetName()); |
| 1072 | // prevent duplicate methods |
| 1073 | if (it == method_names.end()) { |
| 1074 | method_names[m->GetName()] = m.get(); |
| 1075 | } else { |
| 1076 | AIDL_ERROR(m) << "attempt to redefine method " << m->GetName() << ":"; |
| 1077 | AIDL_ERROR(it->second) << "previously defined here."; |
| 1078 | return false; |
| 1079 | } |
| 1080 | |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 1081 | static set<string> reserved_methods{"asBinder()", "getInterfaceHash()", "getInterfaceVersion()", |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1082 | "getTransactionName(int)"}; |
| 1083 | |
| 1084 | if (reserved_methods.find(m->Signature()) != reserved_methods.end()) { |
| 1085 | AIDL_ERROR(m) << " method " << m->Signature() << " is reserved for internal use." << endl; |
| 1086 | return false; |
| 1087 | } |
| 1088 | } |
Steven Moreland | 4d12f9a | 2018-10-31 14:30:55 -0700 | [diff] [blame] | 1089 | |
| 1090 | bool success = true; |
| 1091 | set<string> constant_names; |
| 1092 | for (const std::unique_ptr<AidlConstantDeclaration>& constant : GetConstantDeclarations()) { |
| 1093 | if (constant_names.count(constant->GetName()) > 0) { |
| 1094 | LOG(ERROR) << "Found duplicate constant name '" << constant->GetName() << "'"; |
| 1095 | success = false; |
| 1096 | } |
| 1097 | constant_names.insert(constant->GetName()); |
| 1098 | success = success && constant->CheckValid(typenames); |
| 1099 | } |
| 1100 | |
| 1101 | return success; |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1102 | } |
| 1103 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1104 | AidlQualifiedName::AidlQualifiedName(const AidlLocation& location, const std::string& term, |
| 1105 | const std::string& comments) |
| 1106 | : AidlNode(location), terms_({term}), comments_(comments) { |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 1107 | if (term.find('.') != string::npos) { |
| 1108 | terms_ = Split(term, "."); |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1109 | for (const auto& subterm : terms_) { |
| 1110 | if (subterm.empty()) { |
| 1111 | AIDL_FATAL(this) << "Malformed qualified identifier: '" << term << "'"; |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | } |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
Chih-Hung Hsieh | f05cc26 | 2016-07-27 11:42:51 -0700 | [diff] [blame] | 1117 | void AidlQualifiedName::AddTerm(const std::string& term) { |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 1118 | terms_.push_back(term); |
| 1119 | } |
| 1120 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1121 | AidlImport::AidlImport(const AidlLocation& location, const std::string& needed_class) |
| 1122 | : AidlNode(location), needed_class_(needed_class) {} |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 1123 | |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1124 | std::unique_ptr<Parser> Parser::Parse(const std::string& filename, |
| 1125 | const android::aidl::IoDelegate& io_delegate, |
| 1126 | AidlTypenames& typenames) { |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1127 | // Make sure we can read the file first, before trashing previous state. |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1128 | unique_ptr<string> raw_buffer = io_delegate.GetFileContents(filename); |
| 1129 | if (raw_buffer == nullptr) { |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1130 | AIDL_ERROR(filename) << "Error while opening file for parsing"; |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1131 | return nullptr; |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1134 | // We're going to scan this buffer in place, and yacc demands we put two |
| 1135 | // nulls at the end. |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1136 | raw_buffer->append(2u, '\0'); |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1137 | |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1138 | std::unique_ptr<Parser> parser(new Parser(filename, *raw_buffer, typenames)); |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1139 | |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1140 | if (yy::parser(parser.get()).parse() != 0 || parser->HasError()) return nullptr; |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1141 | |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1142 | return parser; |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 1145 | std::vector<std::string> Parser::Package() const { |
| 1146 | if (!package_) { |
| 1147 | return {}; |
| 1148 | } |
| 1149 | return package_->GetTerms(); |
| 1150 | } |
| 1151 | |
Steven Moreland | d1039a9 | 2020-01-23 09:49:43 -0800 | [diff] [blame] | 1152 | void Parser::AddImport(std::unique_ptr<AidlImport>&& import) { |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 1153 | for (const auto& i : imports_) { |
| 1154 | if (i->GetNeededClass() == import->GetNeededClass()) { |
| 1155 | return; |
| 1156 | } |
| 1157 | } |
Steven Moreland | d1039a9 | 2020-01-23 09:49:43 -0800 | [diff] [blame] | 1158 | imports_.emplace_back(std::move(import)); |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 1159 | } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 1160 | |
| 1161 | bool Parser::Resolve() { |
| 1162 | bool success = true; |
| 1163 | for (AidlTypeSpecifier* typespec : unresolved_typespecs_) { |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1164 | if (!typespec->Resolve(typenames_)) { |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1165 | AIDL_ERROR(typespec) << "Failed to resolve '" << typespec->GetUnresolvedName() << "'"; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 1166 | success = false; |
| 1167 | // don't stop to show more errors if any |
| 1168 | } |
| 1169 | } |
| 1170 | return success; |
| 1171 | } |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 1172 | |
| 1173 | Parser::Parser(const std::string& filename, std::string& raw_buffer, |
| 1174 | android::aidl::AidlTypenames& typenames) |
| 1175 | : filename_(filename), typenames_(typenames) { |
| 1176 | yylex_init(&scanner_); |
| 1177 | buffer_ = yy_scan_buffer(&raw_buffer[0], raw_buffer.length(), scanner_); |
| 1178 | } |
| 1179 | |
| 1180 | Parser::~Parser() { |
| 1181 | yy_delete_buffer(buffer_, scanner_); |
| 1182 | yylex_destroy(scanner_); |
| 1183 | } |