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" |
Jiyong Park | e5c4529 | 2020-05-26 19:06:24 +0900 | [diff] [blame] | 19 | #include "parser.h" |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 20 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 21 | #include <stdio.h> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 22 | #include <stdlib.h> |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 23 | #include <string.h> |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 24 | #include <algorithm> |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 25 | #include <iostream> |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 26 | #include <set> |
| 27 | #include <sstream> |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 28 | #include <string> |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 29 | #include <utility> |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 30 | |
Steven Moreland | 1c4ba20 | 2018-08-09 10:49:54 -0700 | [diff] [blame] | 31 | #include <android-base/parsedouble.h> |
Roshan Pius | 9d7810a | 2016-07-28 08:57:50 -0700 | [diff] [blame] | 32 | #include <android-base/parseint.h> |
Elliott Hughes | 0a62067 | 2015-12-04 13:53:18 -0800 | [diff] [blame] | 33 | #include <android-base/strings.h> |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 34 | |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 35 | #include "aidl_language_y.h" |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 36 | #include "logging.h" |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 37 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 38 | #include "aidl.h" |
| 39 | |
Casey Dahlin | 07b9dde | 2015-09-10 19:13:49 -0700 | [diff] [blame] | 40 | #ifdef _WIN32 |
| 41 | int isatty(int fd) |
| 42 | { |
| 43 | return (fd == 0); |
| 44 | } |
| 45 | #endif |
| 46 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 47 | using android::aidl::IoDelegate; |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 48 | using android::base::Join; |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 49 | using android::base::Split; |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 50 | using std::cerr; |
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 | |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 72 | inline std::string CapitalizeFirstLetter(const AidlNode& context, const std::string& str) { |
| 73 | AIDL_FATAL_IF(str.size() <= 0, context) << "Input cannot be empty."; |
Jeongik Cha | 9118025 | 2020-07-31 15:43:11 +0900 | [diff] [blame] | 74 | std::ostringstream out; |
| 75 | out << static_cast<char>(toupper(str[0])) << str.substr(1); |
| 76 | return out.str(); |
| 77 | } |
| 78 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 79 | void AddHideComment(CodeWriter* writer) { |
| 80 | writer->Write("/* @hide */\n"); |
| 81 | } |
| 82 | |
| 83 | inline bool HasHideComment(const std::string& comment) { |
| 84 | return std::regex_search(comment, std::regex("@hide\\b")); |
| 85 | } |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 86 | } // namespace |
| 87 | |
Devin Moore | df93ebb | 2020-03-25 14:03:35 -0700 | [diff] [blame] | 88 | AidlLocation::AidlLocation(const std::string& file, Point begin, Point end, Source source) |
| 89 | : file_(file), begin_(begin), end_(end), source_(source) {} |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 90 | |
| 91 | std::ostream& operator<<(std::ostream& os, const AidlLocation& l) { |
Devin Moore | 5de18ed | 2020-04-02 13:52:29 -0700 | [diff] [blame] | 92 | os << l.file_; |
| 93 | if (l.LocationKnown()) { |
| 94 | os << ":" << l.begin_.line << "." << l.begin_.column << "-"; |
| 95 | if (l.begin_.line != l.end_.line) { |
| 96 | os << l.end_.line << "."; |
| 97 | } |
| 98 | os << l.end_.column; |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 99 | } |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 100 | return os; |
| 101 | } |
| 102 | |
| 103 | AidlNode::AidlNode(const AidlLocation& location) : location_(location) {} |
| 104 | |
Mathew Inwood | adb7467 | 2019-11-29 14:01:53 +0000 | [diff] [blame] | 105 | std::string AidlNode::PrintLine() const { |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 106 | std::stringstream ss; |
| 107 | ss << location_.file_ << ":" << location_.begin_.line; |
| 108 | return ss.str(); |
| 109 | } |
| 110 | |
Mathew Inwood | adb7467 | 2019-11-29 14:01:53 +0000 | [diff] [blame] | 111 | std::string AidlNode::PrintLocation() const { |
| 112 | std::stringstream ss; |
| 113 | ss << location_.file_ << ":" << location_.begin_.line << ":" << location_.begin_.column << ":" |
| 114 | << location_.end_.line << ":" << location_.end_.column; |
| 115 | return ss.str(); |
| 116 | } |
| 117 | |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 118 | const std::vector<AidlAnnotation::Schema>& AidlAnnotation::AllSchemas() { |
| 119 | static const std::vector<Schema> kSchemas{ |
Jooyung Han | d902a97 | 2020-10-23 17:32:44 +0900 | [diff] [blame] | 120 | {AidlAnnotation::Type::NULLABLE, "nullable", {}, false}, |
| 121 | {AidlAnnotation::Type::UTF8_IN_CPP, "utf8InCpp", {}, false}, |
| 122 | {AidlAnnotation::Type::VINTF_STABILITY, "VintfStability", {}, false}, |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 123 | {AidlAnnotation::Type::UNSUPPORTED_APP_USAGE, |
| 124 | "UnsupportedAppUsage", |
| 125 | {{"expectedSignature", "String"}, |
| 126 | {"implicitMember", "String"}, |
| 127 | {"maxTargetSdk", "int"}, |
| 128 | {"publicAlternatives", "String"}, |
Jooyung Han | d902a97 | 2020-10-23 17:32:44 +0900 | [diff] [blame] | 129 | {"trackingBug", "long"}}, |
| 130 | false}, |
| 131 | {AidlAnnotation::Type::JAVA_STABLE_PARCELABLE, "JavaOnlyStableParcelable", {}, false}, |
| 132 | {AidlAnnotation::Type::HIDE, "Hide", {}, false}, |
| 133 | {AidlAnnotation::Type::BACKING, "Backing", {{"type", "String"}}, false}, |
| 134 | {AidlAnnotation::Type::JAVA_PASSTHROUGH, "JavaPassthrough", {{"annotation", "String"}}, true}, |
| 135 | {AidlAnnotation::Type::JAVA_DEBUG, "JavaDebug", {}, false}, |
| 136 | {AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE, "JavaOnlyImmutable", {}, false}, |
| 137 | {AidlAnnotation::Type::FIXED_SIZE, "FixedSize", {}, false}, |
| 138 | {AidlAnnotation::Type::DESCRIPTOR, "Descriptor", {{"value", "String"}}, false}, |
Andrei Homescu | e61feb5 | 2020-08-18 15:44:24 -0700 | [diff] [blame] | 139 | {AidlAnnotation::Type::RUST_DERIVE, |
| 140 | "RustDerive", |
| 141 | {{"Copy", "boolean"}, |
| 142 | {"Clone", "boolean"}, |
| 143 | {"PartialOrd", "boolean"}, |
| 144 | {"Ord", "boolean"}, |
| 145 | {"PartialEq", "boolean"}, |
| 146 | {"Eq", "boolean"}, |
Jooyung Han | d902a97 | 2020-10-23 17:32:44 +0900 | [diff] [blame] | 147 | {"Hash", "boolean"}}, |
| 148 | false}, |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 149 | }; |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 150 | return kSchemas; |
| 151 | } |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 152 | |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 153 | std::string AidlAnnotation::TypeToString(Type type) { |
| 154 | for (const Schema& schema : AllSchemas()) { |
| 155 | if (type == schema.type) return schema.name; |
| 156 | } |
| 157 | AIDL_FATAL(AIDL_LOCATION_HERE) << "Unrecognized type: " << static_cast<size_t>(type); |
| 158 | __builtin_unreachable(); |
| 159 | } |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 160 | |
| 161 | AidlAnnotation* AidlAnnotation::Parse( |
| 162 | const AidlLocation& location, const string& name, |
| 163 | std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list) { |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 164 | const Schema* schema = nullptr; |
| 165 | for (const Schema& a_schema : AllSchemas()) { |
| 166 | if (a_schema.name == name) { |
| 167 | schema = &a_schema; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if (schema == nullptr) { |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 172 | std::ostringstream stream; |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 173 | stream << "'" << name << "' is not a recognized annotation. "; |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 174 | stream << "It must be one of:"; |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 175 | for (const Schema& s : AllSchemas()) { |
| 176 | stream << " " << s.name; |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 177 | } |
| 178 | stream << "."; |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 179 | AIDL_ERROR(location) << stream.str(); |
| 180 | return nullptr; |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 181 | } |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 182 | if (parameter_list == nullptr) { |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 183 | return new AidlAnnotation(location, *schema, {}); |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 186 | return new AidlAnnotation(location, *schema, std::move(*parameter_list)); |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 187 | } |
| 188 | |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 189 | AidlAnnotation::AidlAnnotation( |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 190 | const AidlLocation& location, const Schema& schema, |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 191 | std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters) |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 192 | : AidlNode(location), schema_(schema), parameters_(std::move(parameters)) {} |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 193 | |
| 194 | bool AidlAnnotation::CheckValid() const { |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 195 | for (const auto& name_and_param : parameters_) { |
| 196 | const std::string& param_name = name_and_param.first; |
| 197 | const std::shared_ptr<AidlConstantValue>& param = name_and_param.second; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 198 | if (!param->CheckValid()) { |
| 199 | AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation " |
| 200 | << GetName() << "."; |
| 201 | return false; |
| 202 | } |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 203 | auto parameter_mapping_it = schema_.supported_parameters.find(param_name); |
| 204 | if (parameter_mapping_it == schema_.supported_parameters.end()) { |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 205 | std::ostringstream stream; |
| 206 | stream << "Parameter " << param_name << " not supported "; |
Devin Moore | decaf29 | 2020-04-30 09:16:40 -0700 | [diff] [blame] | 207 | stream << "for annotation " << GetName() << ". "; |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 208 | stream << "It must be one of:"; |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 209 | for (const auto& kv : schema_.supported_parameters) { |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 210 | stream << " " << kv.first; |
| 211 | } |
| 212 | AIDL_ERROR(this) << stream.str(); |
| 213 | return false; |
| 214 | } |
| 215 | AidlTypeSpecifier type{AIDL_LOCATION_HERE, parameter_mapping_it->second, false, nullptr, ""}; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 216 | const std::string param_value = param->ValueString(type, AidlConstantValueDecorator); |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 217 | // Assume error on empty string. |
| 218 | if (param_value == "") { |
| 219 | AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation " |
| 220 | << GetName() << "."; |
| 221 | return false; |
| 222 | } |
| 223 | } |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | std::map<std::string, std::string> AidlAnnotation::AnnotationParams( |
| 228 | const ConstantValueDecorator& decorator) const { |
| 229 | std::map<std::string, std::string> raw_params; |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 230 | for (const auto& name_and_param : parameters_) { |
| 231 | const std::string& param_name = name_and_param.first; |
| 232 | const std::shared_ptr<AidlConstantValue>& param = name_and_param.second; |
Devin Moore | decaf29 | 2020-04-30 09:16:40 -0700 | [diff] [blame] | 233 | if (schema_.supported_parameters.find(param_name) == schema_.supported_parameters.end()) { |
| 234 | std::ostringstream stream; |
| 235 | stream << "Parameter " << param_name << " not supported "; |
| 236 | stream << "for annotation " << GetName() << ". "; |
| 237 | stream << "It must be one of:"; |
| 238 | for (const auto& kv : schema_.supported_parameters) { |
| 239 | stream << " " << kv.first; |
| 240 | } |
| 241 | AIDL_ERROR(this) << stream.str(); |
| 242 | continue; |
| 243 | } |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 244 | AidlTypeSpecifier type{AIDL_LOCATION_HERE, schema_.supported_parameters.at(param_name), false, |
| 245 | nullptr, ""}; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 246 | if (!param->CheckValid()) { |
| 247 | AIDL_ERROR(this) << "Invalid value for parameter " << param_name << " on annotation " |
| 248 | << GetName() << "."; |
Devin Moore | decaf29 | 2020-04-30 09:16:40 -0700 | [diff] [blame] | 249 | continue; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | raw_params.emplace(param_name, param->ValueString(type, decorator)); |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 253 | } |
| 254 | return raw_params; |
| 255 | } |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 256 | |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 257 | std::string AidlAnnotation::ToString(const ConstantValueDecorator& decorator) const { |
| 258 | if (parameters_.empty()) { |
| 259 | return "@" + GetName(); |
| 260 | } else { |
| 261 | vector<string> param_strings; |
| 262 | for (const auto& [name, value] : AnnotationParams(decorator)) { |
| 263 | param_strings.emplace_back(name + "=" + value); |
| 264 | } |
| 265 | return "@" + GetName() + "(" + Join(param_strings, ", ") + ")"; |
| 266 | } |
| 267 | } |
| 268 | |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 269 | static const AidlAnnotation* GetAnnotation(const vector<AidlAnnotation>& annotations, |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 270 | AidlAnnotation::Type type) { |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 271 | for (const auto& a : annotations) { |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 272 | if (a.GetType() == type) { |
Jooyung Han | d902a97 | 2020-10-23 17:32:44 +0900 | [diff] [blame] | 273 | AIDL_FATAL_IF(a.Repeatable(), a) |
| 274 | << "Trying to get a single annotation when it is repeatable."; |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 275 | return &a; |
| 276 | } |
| 277 | } |
| 278 | return nullptr; |
| 279 | } |
| 280 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 281 | AidlAnnotatable::AidlAnnotatable(const AidlLocation& location) : AidlNode(location) {} |
| 282 | |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 283 | bool AidlAnnotatable::IsNullable() const { |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 284 | return GetAnnotation(annotations_, AidlAnnotation::Type::NULLABLE); |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 285 | } |
| 286 | |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 287 | bool AidlAnnotatable::IsUtf8InCpp() const { |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 288 | return GetAnnotation(annotations_, AidlAnnotation::Type::UTF8_IN_CPP); |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 289 | } |
| 290 | |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 291 | bool AidlAnnotatable::IsVintfStability() const { |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 292 | return GetAnnotation(annotations_, AidlAnnotation::Type::VINTF_STABILITY); |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 295 | bool AidlAnnotatable::IsJavaOnlyImmutable() const { |
| 296 | return GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE); |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 297 | } |
| 298 | |
Devin Moore | c7e47a3 | 2020-08-07 10:55:25 -0700 | [diff] [blame] | 299 | bool AidlAnnotatable::IsFixedSize() const { |
| 300 | return GetAnnotation(annotations_, AidlAnnotation::Type::FIXED_SIZE); |
| 301 | } |
| 302 | |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 303 | const AidlAnnotation* AidlAnnotatable::UnsupportedAppUsage() const { |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 304 | return GetAnnotation(annotations_, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE); |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 305 | } |
| 306 | |
Andrei Homescu | e61feb5 | 2020-08-18 15:44:24 -0700 | [diff] [blame] | 307 | const AidlAnnotation* AidlAnnotatable::RustDerive() const { |
| 308 | return GetAnnotation(annotations_, AidlAnnotation::Type::RUST_DERIVE); |
| 309 | } |
| 310 | |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 311 | const AidlTypeSpecifier* AidlAnnotatable::BackingType(const AidlTypenames& typenames) const { |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 312 | auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::BACKING); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 313 | if (annotation != nullptr) { |
| 314 | auto annotation_params = annotation->AnnotationParams(AidlConstantValueDecorator); |
| 315 | if (auto it = annotation_params.find("type"); it != annotation_params.end()) { |
| 316 | const string& type = it->second; |
Steven Moreland | e7d5d08 | 2020-05-21 20:29:02 +0000 | [diff] [blame] | 317 | |
| 318 | AIDL_FATAL_IF(type.size() < 2, this) << type; |
| 319 | AIDL_FATAL_IF(type[0] != '"', this) << type; |
| 320 | AIDL_FATAL_IF(type[type.length() - 1] != '"', this) << type; |
| 321 | string unquoted_type = type.substr(1, type.length() - 2); |
| 322 | |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 323 | AidlTypeSpecifier* type_specifier = |
Steven Moreland | e7d5d08 | 2020-05-21 20:29:02 +0000 | [diff] [blame] | 324 | new AidlTypeSpecifier(AIDL_LOCATION_HERE, unquoted_type, false, nullptr, ""); |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 325 | type_specifier->Resolve(typenames); |
| 326 | return type_specifier; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | return nullptr; |
| 330 | } |
| 331 | |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 332 | bool AidlAnnotatable::IsStableApiParcelable(Options::Language lang) const { |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 333 | return lang == Options::Language::JAVA && |
| 334 | GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_STABLE_PARCELABLE); |
Jeongik Cha | 82317dd | 2019-02-27 20:26:11 +0900 | [diff] [blame] | 335 | } |
| 336 | |
Makoto Onuki | 78a1c1c | 2020-03-04 16:57:23 -0800 | [diff] [blame] | 337 | bool AidlAnnotatable::IsHide() const { |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 338 | return GetAnnotation(annotations_, AidlAnnotation::Type::HIDE); |
Makoto Onuki | 78a1c1c | 2020-03-04 16:57:23 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Jiyong Park | 43113fb | 2020-07-20 16:26:19 +0900 | [diff] [blame] | 341 | bool AidlAnnotatable::IsJavaDebug() const { |
| 342 | return GetAnnotation(annotations_, AidlAnnotation::Type::JAVA_DEBUG); |
| 343 | } |
| 344 | |
Jiyong Park | 27fd7fd | 2020-08-27 16:25:09 +0900 | [diff] [blame] | 345 | std::string AidlAnnotatable::GetDescriptor() const { |
| 346 | auto annotation = GetAnnotation(annotations_, AidlAnnotation::Type::DESCRIPTOR); |
| 347 | if (annotation != nullptr) { |
| 348 | auto params = annotation->AnnotationParams(AidlConstantValueDecorator); |
| 349 | if (auto it = params.find("value"); it != params.end()) { |
| 350 | const string& value = it->second; |
| 351 | |
| 352 | AIDL_FATAL_IF(value.size() < 2, this) << value; |
| 353 | AIDL_FATAL_IF(value[0] != '"', this) << value; |
| 354 | AIDL_FATAL_IF(value[value.length() - 1] != '"', this) << value; |
| 355 | std::string unquoted_value = value.substr(1, value.length() - 2); |
| 356 | return unquoted_value; |
| 357 | } |
| 358 | } |
| 359 | return ""; |
| 360 | } |
| 361 | |
Steven Moreland | 7e4b950 | 2020-02-20 18:10:42 -0800 | [diff] [blame] | 362 | void AidlAnnotatable::DumpAnnotations(CodeWriter* writer) const { |
| 363 | if (annotations_.empty()) return; |
| 364 | |
| 365 | writer->Write("%s\n", AidlAnnotatable::ToString().c_str()); |
| 366 | } |
| 367 | |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 368 | bool AidlAnnotatable::CheckValid(const AidlTypenames&) const { |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 369 | std::set<AidlAnnotation::Type> supported_annotations = GetSupportedAnnotations(); |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 370 | for (const auto& annotation : GetAnnotations()) { |
Jooyung Han | d902a97 | 2020-10-23 17:32:44 +0900 | [diff] [blame] | 371 | // check if it is allowed for this node |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 372 | if (supported_annotations.find(annotation.GetType()) == supported_annotations.end()) { |
Jooyung Han | d902a97 | 2020-10-23 17:32:44 +0900 | [diff] [blame] | 373 | std::vector<std::string> supported_annot_strings; |
| 374 | for (AidlAnnotation::Type type : supported_annotations) { |
| 375 | supported_annot_strings.push_back(AidlAnnotation::TypeToString(type)); |
| 376 | } |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 377 | AIDL_ERROR(this) << "'" << annotation.GetName() |
| 378 | << "' is not a supported annotation for this node. " |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 379 | << "It must be one of: " |
| 380 | << android::base::Join(supported_annot_strings, ", "); |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 381 | return false; |
| 382 | } |
Jooyung Han | d902a97 | 2020-10-23 17:32:44 +0900 | [diff] [blame] | 383 | // CheckValid() only if it is okay to be here |
| 384 | if (!annotation.CheckValid()) { |
| 385 | return false; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | std::map<AidlAnnotation::Type, AidlLocation> declared; |
| 390 | for (const auto& annotation : GetAnnotations()) { |
| 391 | const auto& [iter, inserted] = declared.emplace(annotation.GetType(), annotation.GetLocation()); |
| 392 | if (!inserted && !annotation.Repeatable()) { |
| 393 | AIDL_ERROR(this) << "'" << annotation.GetName() |
| 394 | << "' is repeated, but not allowed. Previous location: " << iter->second; |
| 395 | return false; |
| 396 | } |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 397 | } |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 398 | |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 399 | return true; |
| 400 | } |
| 401 | |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 402 | string AidlAnnotatable::ToString() const { |
| 403 | vector<string> ret; |
| 404 | for (const auto& a : annotations_) { |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 405 | ret.emplace_back(a.ToString(AidlConstantValueDecorator)); |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 406 | } |
| 407 | std::sort(ret.begin(), ret.end()); |
| 408 | return Join(ret, " "); |
| 409 | } |
| 410 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 411 | AidlTypeSpecifier::AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, |
| 412 | bool is_array, |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 413 | vector<unique_ptr<AidlTypeSpecifier>>* type_params, |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 414 | const string& comments) |
| 415 | : AidlAnnotatable(location), |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 416 | AidlParameterizable<unique_ptr<AidlTypeSpecifier>>(type_params), |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 417 | unresolved_name_(unresolved_name), |
Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame] | 418 | is_array_(is_array), |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 419 | comments_(comments), |
| 420 | split_name_(Split(unresolved_name, ".")) {} |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 421 | |
Jooyung Han | d2fa023 | 2020-10-19 02:51:41 +0900 | [diff] [blame] | 422 | const AidlTypeSpecifier& AidlTypeSpecifier::ArrayBase() const { |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 423 | AIDL_FATAL_IF(!is_array_, this); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 424 | // Declaring array of generic type cannot happen, it is grammar error. |
| 425 | AIDL_FATAL_IF(IsGeneric(), this); |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 426 | |
Jooyung Han | d2fa023 | 2020-10-19 02:51:41 +0900 | [diff] [blame] | 427 | if (!array_base_) { |
| 428 | array_base_.reset(new AidlTypeSpecifier(*this)); |
| 429 | array_base_->is_array_ = false; |
| 430 | } |
| 431 | return *array_base_; |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 434 | bool AidlTypeSpecifier::IsHidden() const { |
| 435 | return HasHideComment(GetComments()); |
| 436 | } |
| 437 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 438 | string AidlTypeSpecifier::ToString() const { |
| 439 | string ret = GetName(); |
| 440 | if (IsGeneric()) { |
| 441 | vector<string> arg_names; |
| 442 | for (const auto& ta : GetTypeParameters()) { |
Jiyong Park | ccf00f8 | 2018-07-17 01:39:23 +0900 | [diff] [blame] | 443 | arg_names.emplace_back(ta->ToString()); |
| 444 | } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 445 | ret += "<" + Join(arg_names, ",") + ">"; |
Jiyong Park | ccf00f8 | 2018-07-17 01:39:23 +0900 | [diff] [blame] | 446 | } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 447 | if (IsArray()) { |
| 448 | ret += "[]"; |
| 449 | } |
| 450 | return ret; |
Jiyong Park | ccf00f8 | 2018-07-17 01:39:23 +0900 | [diff] [blame] | 451 | } |
| 452 | |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 453 | string AidlTypeSpecifier::Signature() const { |
| 454 | string ret = ToString(); |
| 455 | string annotations = AidlAnnotatable::ToString(); |
| 456 | if (annotations != "") { |
| 457 | ret = annotations + " " + ret; |
| 458 | } |
| 459 | return ret; |
| 460 | } |
| 461 | |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 462 | bool AidlTypeSpecifier::Resolve(const AidlTypenames& typenames) { |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 463 | AIDL_FATAL_IF(IsResolved(), this); |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 464 | AidlTypenames::ResolvedTypename result = typenames.ResolveTypename(unresolved_name_); |
| 465 | if (result.is_resolved) { |
| 466 | fully_qualified_name_ = result.canonical_name; |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 467 | split_name_ = Split(fully_qualified_name_, "."); |
Jiyong Park | ccf00f8 | 2018-07-17 01:39:23 +0900 | [diff] [blame] | 468 | } |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 469 | return result.is_resolved; |
Casey Dahlin | 70078e6 | 2015-09-30 17:01:30 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 472 | std::set<AidlAnnotation::Type> AidlTypeSpecifier::GetSupportedAnnotations() const { |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 473 | // kHide and kUnsupportedAppUsage are both method return annotations |
| 474 | // which we don't distinguish from other type specifiers. |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 475 | return {AidlAnnotation::Type::NULLABLE, AidlAnnotation::Type::UTF8_IN_CPP, |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 476 | AidlAnnotation::Type::UNSUPPORTED_APP_USAGE, AidlAnnotation::Type::HIDE, |
| 477 | AidlAnnotation::Type::JAVA_PASSTHROUGH}; |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 480 | bool AidlTypeSpecifier::CheckValid(const AidlTypenames& typenames) const { |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 481 | if (!AidlAnnotatable::CheckValid(typenames)) { |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 482 | return false; |
| 483 | } |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 484 | if (IsGeneric()) { |
| 485 | const string& type_name = GetName(); |
Jeongik Cha | e74c86d | 2019-12-12 16:54:03 +0900 | [diff] [blame] | 486 | |
| 487 | auto& types = GetTypeParameters(); |
| 488 | // TODO(b/136048684) Disallow to use primitive types only if it is List or Map. |
| 489 | if (type_name == "List" || type_name == "Map") { |
| 490 | if (std::any_of(types.begin(), types.end(), [](auto& type_ptr) { |
| 491 | return AidlTypenames::IsPrimitiveTypename(type_ptr->GetName()); |
| 492 | })) { |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 493 | AIDL_ERROR(this) << "A generic type cannot have any primitive type parameters."; |
Jeongik Cha | e74c86d | 2019-12-12 16:54:03 +0900 | [diff] [blame] | 494 | return false; |
| 495 | } |
| 496 | } |
Steven Moreland | 0d9c26e | 2020-01-22 08:52:08 -0800 | [diff] [blame] | 497 | const auto defined_type = typenames.TryGetDefinedType(type_name); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 498 | const auto parameterizable = |
Steven Moreland | 0d9c26e | 2020-01-22 08:52:08 -0800 | [diff] [blame] | 499 | defined_type != nullptr ? defined_type->AsParameterizable() : nullptr; |
| 500 | const bool is_user_defined_generic_type = |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 501 | parameterizable != nullptr && parameterizable->IsGeneric(); |
Steven Moreland | 0d9c26e | 2020-01-22 08:52:08 -0800 | [diff] [blame] | 502 | const size_t num_params = GetTypeParameters().size(); |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 503 | if (type_name == "List") { |
Steven Moreland | 0d9c26e | 2020-01-22 08:52:08 -0800 | [diff] [blame] | 504 | if (num_params > 1) { |
Steven Moreland | ebc3c5d | 2020-09-30 23:40:33 +0000 | [diff] [blame] | 505 | AIDL_ERROR(this) << "List can only have one type parameter, but got: '" << ToString() |
| 506 | << "'"; |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 507 | return false; |
| 508 | } |
| 509 | } else if (type_name == "Map") { |
Steven Moreland | 0d9c26e | 2020-01-22 08:52:08 -0800 | [diff] [blame] | 510 | if (num_params != 0 && num_params != 2) { |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 511 | AIDL_ERROR(this) << "Map must have 0 or 2 type parameters, but got " |
| 512 | << "'" << ToString() << "'"; |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 513 | return false; |
| 514 | } |
Steven Moreland | 0d9c26e | 2020-01-22 08:52:08 -0800 | [diff] [blame] | 515 | if (num_params == 2) { |
Jeongik Cha | e48d994 | 2020-01-02 17:39:00 +0900 | [diff] [blame] | 516 | const string& key_type = GetTypeParameters()[0]->GetName(); |
| 517 | if (key_type != "String") { |
| 518 | AIDL_ERROR(this) << "The type of key in map must be String, but it is " |
| 519 | << "'" << key_type << "'"; |
| 520 | return false; |
| 521 | } |
| 522 | } |
Steven Moreland | 0d9c26e | 2020-01-22 08:52:08 -0800 | [diff] [blame] | 523 | } else if (is_user_defined_generic_type) { |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 524 | const size_t allowed = parameterizable->GetTypeParameters().size(); |
Steven Moreland | 0d9c26e | 2020-01-22 08:52:08 -0800 | [diff] [blame] | 525 | if (num_params != allowed) { |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 526 | AIDL_ERROR(this) << type_name << " must have " << allowed << " type parameters, but got " |
Steven Moreland | 0d9c26e | 2020-01-22 08:52:08 -0800 | [diff] [blame] | 527 | << num_params; |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 528 | return false; |
| 529 | } |
| 530 | } else { |
| 531 | AIDL_ERROR(this) << type_name << " is not a generic type."; |
| 532 | return false; |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 533 | } |
| 534 | } |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 535 | |
Steven Moreland | 11cb945 | 2020-01-21 16:56:58 -0800 | [diff] [blame] | 536 | const bool is_generic_string_list = GetName() == "List" && IsGeneric() && |
| 537 | GetTypeParameters().size() == 1 && |
| 538 | GetTypeParameters()[0]->GetName() == "String"; |
| 539 | if (IsUtf8InCpp() && (GetName() != "String" && !is_generic_string_list)) { |
| 540 | AIDL_ERROR(this) << "@utf8InCpp can only be used on String, String[], and List<String>."; |
| 541 | return false; |
| 542 | } |
| 543 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 544 | if (GetName() == "void") { |
| 545 | if (IsArray() || IsNullable() || IsUtf8InCpp()) { |
| 546 | AIDL_ERROR(this) << "void type cannot be an array or nullable or utf8 string"; |
| 547 | return false; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | if (IsArray()) { |
Steven Moreland | 0d9c26e | 2020-01-22 08:52:08 -0800 | [diff] [blame] | 552 | const auto defined_type = typenames.TryGetDefinedType(GetName()); |
| 553 | if (defined_type != nullptr && defined_type->AsInterface() != nullptr) { |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 554 | AIDL_ERROR(this) << "Binder type cannot be an array"; |
| 555 | return false; |
| 556 | } |
Steven Moreland | 8042d2d | 2020-09-30 23:31:32 +0000 | [diff] [blame] | 557 | if (GetName() == "ParcelableHolder") { |
| 558 | AIDL_ERROR(this) << "Arrays of ParcelableHolder are not supported."; |
| 559 | return false; |
| 560 | } |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | if (IsNullable()) { |
| 564 | if (AidlTypenames::IsPrimitiveTypename(GetName()) && !IsArray()) { |
| 565 | AIDL_ERROR(this) << "Primitive type cannot get nullable annotation"; |
| 566 | return false; |
| 567 | } |
Steven Moreland | 0d9c26e | 2020-01-22 08:52:08 -0800 | [diff] [blame] | 568 | const auto defined_type = typenames.TryGetDefinedType(GetName()); |
| 569 | if (defined_type != nullptr && defined_type->AsEnumDeclaration() != nullptr && !IsArray()) { |
Daniel Norman | ee8674f | 2019-09-20 16:07:00 -0700 | [diff] [blame] | 570 | AIDL_ERROR(this) << "Enum type cannot get nullable annotation"; |
| 571 | return false; |
| 572 | } |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 573 | } |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 574 | return true; |
| 575 | } |
| 576 | |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 577 | std::string AidlConstantValueDecorator(const AidlTypeSpecifier& /*type*/, |
| 578 | const std::string& raw_value) { |
| 579 | return raw_value; |
| 580 | } |
| 581 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 582 | AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location, |
| 583 | AidlTypeSpecifier* type, const std::string& name) |
Steven Moreland | 541788d | 2020-05-21 22:05:52 +0000 | [diff] [blame] | 584 | : AidlVariableDeclaration(location, type, name, AidlConstantValue::Default(*type)) { |
| 585 | default_user_specified_ = false; |
| 586 | } |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 587 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 588 | AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location, |
| 589 | AidlTypeSpecifier* type, const std::string& name, |
| 590 | AidlConstantValue* default_value) |
Steven Moreland | 541788d | 2020-05-21 22:05:52 +0000 | [diff] [blame] | 591 | : AidlNode(location), |
| 592 | type_(type), |
| 593 | name_(name), |
| 594 | default_user_specified_(true), |
| 595 | default_value_(default_value) {} |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 596 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 597 | bool AidlVariableDeclaration::CheckValid(const AidlTypenames& typenames) const { |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 598 | bool valid = true; |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 599 | valid &= type_->CheckValid(typenames); |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 600 | |
Steven Moreland | 54be7bd | 2019-12-05 11:17:53 -0800 | [diff] [blame] | 601 | if (type_->GetName() == "void") { |
| 602 | AIDL_ERROR(this) << "Declaration " << name_ |
| 603 | << " is void, but declarations cannot be of void type."; |
| 604 | valid = false; |
| 605 | } |
| 606 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 607 | if (default_value_ == nullptr) return valid; |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 608 | valid &= default_value_->CheckValid(); |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 609 | |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 610 | if (!valid) return false; |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 611 | |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 612 | return !ValueString(AidlConstantValueDecorator).empty(); |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 613 | } |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 614 | |
| 615 | string AidlVariableDeclaration::ToString() const { |
Jeongik Cha | 3271ffa | 2018-12-04 15:19:20 +0900 | [diff] [blame] | 616 | string ret = type_->Signature() + " " + name_; |
Steven Moreland | 541788d | 2020-05-21 22:05:52 +0000 | [diff] [blame] | 617 | if (default_value_ != nullptr && default_user_specified_) { |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 618 | ret += " = " + ValueString(AidlConstantValueDecorator); |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 619 | } |
| 620 | return ret; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 623 | string AidlVariableDeclaration::Signature() const { |
| 624 | return type_->Signature() + " " + name_; |
| 625 | } |
| 626 | |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 627 | std::string AidlVariableDeclaration::ValueString(const ConstantValueDecorator& decorator) const { |
Jiyong Park | a468e2a | 2018-08-29 21:25:18 +0900 | [diff] [blame] | 628 | if (default_value_ != nullptr) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 629 | return default_value_->ValueString(GetType(), decorator); |
Jiyong Park | a468e2a | 2018-08-29 21:25:18 +0900 | [diff] [blame] | 630 | } else { |
| 631 | return ""; |
| 632 | } |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 635 | AidlArgument::AidlArgument(const AidlLocation& location, AidlArgument::Direction direction, |
| 636 | AidlTypeSpecifier* type, const std::string& name) |
| 637 | : AidlVariableDeclaration(location, type, name), |
Casey Dahlin | fd6fb48 | 2015-09-30 14:48:18 -0700 | [diff] [blame] | 638 | direction_(direction), |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 639 | direction_specified_(true) {} |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 640 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 641 | AidlArgument::AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, |
| 642 | const std::string& name) |
| 643 | : AidlVariableDeclaration(location, type, name), |
Casey Dahlin | fd6fb48 | 2015-09-30 14:48:18 -0700 | [diff] [blame] | 644 | direction_(AidlArgument::IN_DIR), |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 645 | direction_specified_(false) {} |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 646 | |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 647 | string AidlArgument::GetDirectionSpecifier() const { |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 648 | string ret; |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 649 | if (direction_specified_) { |
| 650 | switch(direction_) { |
| 651 | case AidlArgument::IN_DIR: |
Devin Moore | eccdb90 | 2020-03-24 16:22:40 -0700 | [diff] [blame] | 652 | ret += "in"; |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 653 | break; |
| 654 | case AidlArgument::OUT_DIR: |
Devin Moore | eccdb90 | 2020-03-24 16:22:40 -0700 | [diff] [blame] | 655 | ret += "out"; |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 656 | break; |
| 657 | case AidlArgument::INOUT_DIR: |
Devin Moore | eccdb90 | 2020-03-24 16:22:40 -0700 | [diff] [blame] | 658 | ret += "inout"; |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 659 | break; |
| 660 | } |
| 661 | } |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 662 | return ret; |
| 663 | } |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 664 | |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 665 | string AidlArgument::ToString() const { |
Devin Moore | eccdb90 | 2020-03-24 16:22:40 -0700 | [diff] [blame] | 666 | if (direction_specified_) { |
| 667 | return GetDirectionSpecifier() + " " + AidlVariableDeclaration::ToString(); |
| 668 | } else { |
| 669 | return AidlVariableDeclaration::ToString(); |
| 670 | } |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | std::string AidlArgument::Signature() const { |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 674 | class AidlInterface; |
| 675 | class AidlInterface; |
| 676 | class AidlParcelable; |
| 677 | class AidlStructuredParcelable; |
| 678 | class AidlParcelable; |
| 679 | class AidlStructuredParcelable; |
Devin Moore | eccdb90 | 2020-03-24 16:22:40 -0700 | [diff] [blame] | 680 | if (direction_specified_) { |
| 681 | return GetDirectionSpecifier() + " " + AidlVariableDeclaration::Signature(); |
| 682 | } else { |
| 683 | return AidlVariableDeclaration::Signature(); |
| 684 | } |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 685 | } |
| 686 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 687 | AidlMember::AidlMember(const AidlLocation& location) : AidlNode(location) {} |
| 688 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 689 | AidlConstantDeclaration::AidlConstantDeclaration(const AidlLocation& location, |
| 690 | AidlTypeSpecifier* type, const std::string& name, |
| 691 | AidlConstantValue* value) |
| 692 | : AidlMember(location), type_(type), name_(name), value_(value) {} |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 693 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 694 | bool AidlConstantDeclaration::CheckValid(const AidlTypenames& typenames) const { |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 695 | bool valid = true; |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 696 | valid &= type_->CheckValid(typenames); |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 697 | valid &= value_->CheckValid(); |
| 698 | if (!valid) return false; |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 699 | |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 700 | const static set<string> kSupportedConstTypes = {"String", "int"}; |
| 701 | if (kSupportedConstTypes.find(type_->ToString()) == kSupportedConstTypes.end()) { |
| 702 | AIDL_ERROR(this) << "Constant of type " << type_->ToString() << " is not supported."; |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 703 | return false; |
| 704 | } |
| 705 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 706 | return true; |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 709 | string AidlConstantDeclaration::ToString() const { |
| 710 | return "const " + type_->ToString() + " " + name_ + " = " + |
| 711 | ValueString(AidlConstantValueDecorator); |
| 712 | } |
| 713 | |
| 714 | string AidlConstantDeclaration::Signature() const { |
| 715 | return type_->Signature() + " " + name_; |
| 716 | } |
| 717 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 718 | AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, |
| 719 | const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args, |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 720 | const std::string& comments) |
| 721 | : AidlMethod(location, oneway, type, name, args, comments, 0, true) { |
| 722 | has_id_ = false; |
| 723 | } |
| 724 | |
| 725 | AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, |
| 726 | const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args, |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 727 | const std::string& comments, int id, bool is_user_defined) |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 728 | : AidlMember(location), |
| 729 | oneway_(oneway), |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 730 | comments_(comments), |
| 731 | type_(type), |
| 732 | name_(name), |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 733 | arguments_(std::move(*args)), |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 734 | id_(id), |
| 735 | is_user_defined_(is_user_defined) { |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 736 | has_id_ = true; |
| 737 | delete args; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 738 | for (const unique_ptr<AidlArgument>& a : arguments_) { |
| 739 | if (a->IsIn()) { in_arguments_.push_back(a.get()); } |
| 740 | if (a->IsOut()) { out_arguments_.push_back(a.get()); } |
| 741 | } |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 742 | } |
| 743 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 744 | bool AidlMethod::IsHidden() const { |
| 745 | return HasHideComment(GetComments()); |
| 746 | } |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 747 | |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 748 | string AidlMethod::Signature() const { |
| 749 | vector<string> arg_signatures; |
| 750 | for (const auto& arg : GetArguments()) { |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 751 | arg_signatures.emplace_back(arg->GetType().ToString()); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 752 | } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 753 | return GetName() + "(" + Join(arg_signatures, ", ") + ")"; |
| 754 | } |
| 755 | |
| 756 | string AidlMethod::ToString() const { |
| 757 | vector<string> arg_strings; |
| 758 | for (const auto& arg : GetArguments()) { |
| 759 | arg_strings.emplace_back(arg->Signature()); |
| 760 | } |
Steven Moreland | 4ee6863 | 2018-12-14 15:52:46 -0800 | [diff] [blame] | 761 | string ret = (IsOneway() ? "oneway " : "") + GetType().Signature() + " " + GetName() + "(" + |
| 762 | Join(arg_strings, ", ") + ")"; |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 763 | if (HasId()) { |
| 764 | ret += " = " + std::to_string(GetId()); |
| 765 | } |
| 766 | return ret; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 767 | } |
| 768 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 769 | AidlDefinedType::AidlDefinedType(const AidlLocation& location, const std::string& name, |
Jiyong Park | 1813218 | 2020-06-08 20:24:40 +0900 | [diff] [blame] | 770 | const std::string& comments, const std::string& package) |
| 771 | : AidlAnnotatable(location), |
| 772 | name_(name), |
| 773 | comments_(comments), |
| 774 | package_(package), |
| 775 | split_package_(package.empty() ? std::vector<std::string>() |
| 776 | : android::base::Split(package, ".")) {} |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 777 | |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 778 | bool AidlDefinedType::CheckValid(const AidlTypenames& typenames) const { |
| 779 | if (!AidlAnnotatable::CheckValid(typenames)) { |
| 780 | return false; |
| 781 | } |
| 782 | |
| 783 | return true; |
| 784 | } |
| 785 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 786 | bool AidlDefinedType::IsHidden() const { |
| 787 | return HasHideComment(GetComments()); |
| 788 | } |
| 789 | |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 790 | std::string AidlDefinedType::GetCanonicalName() const { |
| 791 | if (package_.empty()) { |
| 792 | return GetName(); |
| 793 | } |
| 794 | return GetPackage() + "." + GetName(); |
| 795 | } |
| 796 | |
Steven Moreland | a5d9c5c | 2020-02-21 16:01:09 -0800 | [diff] [blame] | 797 | void AidlDefinedType::DumpHeader(CodeWriter* writer) const { |
| 798 | if (this->IsHidden()) { |
| 799 | AddHideComment(writer); |
| 800 | } |
| 801 | DumpAnnotations(writer); |
| 802 | } |
| 803 | |
Jiyong Park | 1813218 | 2020-06-08 20:24:40 +0900 | [diff] [blame] | 804 | AidlParcelable::AidlParcelable(const AidlLocation& location, const std::string& name, |
| 805 | const std::string& package, const std::string& comments, |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 806 | const std::string& cpp_header, std::vector<std::string>* type_params) |
Jiyong Park | 1813218 | 2020-06-08 20:24:40 +0900 | [diff] [blame] | 807 | : AidlDefinedType(location, name, comments, package), |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 808 | AidlParameterizable<std::string>(type_params), |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 809 | cpp_header_(cpp_header) { |
| 810 | // Strip off quotation marks if we actually have a cpp header. |
| 811 | if (cpp_header_.length() >= 2) { |
| 812 | cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2); |
| 813 | } |
Casey Dahlin | 59401da | 2015-10-09 18:16:45 -0700 | [diff] [blame] | 814 | } |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 815 | template <typename T> |
| 816 | AidlParameterizable<T>::AidlParameterizable(const AidlParameterizable& other) { |
| 817 | // Copying is not supported if it has type parameters. |
| 818 | // It doesn't make a problem because only ArrayBase() makes a copy, |
| 819 | // and it can be called only if a type is not generic. |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 820 | AIDL_FATAL_IF(other.IsGeneric(), AIDL_LOCATION_HERE); |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | template <typename T> |
| 824 | bool AidlParameterizable<T>::CheckValid() const { |
| 825 | return true; |
| 826 | }; |
| 827 | |
| 828 | template <> |
| 829 | bool AidlParameterizable<std::string>::CheckValid() const { |
| 830 | if (!IsGeneric()) { |
| 831 | return true; |
| 832 | } |
| 833 | std::unordered_set<std::string> set(GetTypeParameters().begin(), GetTypeParameters().end()); |
| 834 | if (set.size() != GetTypeParameters().size()) { |
| 835 | AIDL_ERROR(this->AsAidlNode()) << "Every type parameter should be unique."; |
| 836 | return false; |
| 837 | } |
| 838 | return true; |
| 839 | } |
Casey Dahlin | 59401da | 2015-10-09 18:16:45 -0700 | [diff] [blame] | 840 | |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 841 | std::set<AidlAnnotation::Type> AidlParcelable::GetSupportedAnnotations() const { |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 842 | return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE, |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 843 | AidlAnnotation::Type::JAVA_STABLE_PARCELABLE, AidlAnnotation::Type::HIDE, |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 844 | AidlAnnotation::Type::JAVA_PASSTHROUGH, AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE}; |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | bool AidlParcelable::CheckValid(const AidlTypenames& typenames) const { |
| 848 | if (!AidlDefinedType::CheckValid(typenames)) { |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 849 | return false; |
| 850 | } |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 851 | if (!AidlParameterizable<std::string>::CheckValid()) { |
| 852 | return false; |
| 853 | } |
Jeongik Cha | 82317dd | 2019-02-27 20:26:11 +0900 | [diff] [blame] | 854 | |
| 855 | return true; |
| 856 | } |
| 857 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 858 | void AidlParcelable::Dump(CodeWriter* writer) const { |
Steven Moreland | a5d9c5c | 2020-02-21 16:01:09 -0800 | [diff] [blame] | 859 | DumpHeader(writer); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 860 | writer->Write("parcelable %s ;\n", GetName().c_str()); |
| 861 | } |
| 862 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 863 | AidlStructuredParcelable::AidlStructuredParcelable( |
Jiyong Park | 1813218 | 2020-06-08 20:24:40 +0900 | [diff] [blame] | 864 | const AidlLocation& location, const std::string& name, const std::string& package, |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 865 | const std::string& comments, std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables, |
| 866 | std::vector<std::string>* type_params) |
| 867 | : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params), |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 868 | variables_(std::move(*variables)) {} |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 869 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 870 | void AidlStructuredParcelable::Dump(CodeWriter* writer) const { |
Steven Moreland | a5d9c5c | 2020-02-21 16:01:09 -0800 | [diff] [blame] | 871 | DumpHeader(writer); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 872 | writer->Write("parcelable %s {\n", GetName().c_str()); |
| 873 | writer->Indent(); |
| 874 | for (const auto& field : GetFields()) { |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 875 | if (field->GetType().IsHidden()) { |
| 876 | AddHideComment(writer); |
| 877 | } |
Jiyong Park | a468e2a | 2018-08-29 21:25:18 +0900 | [diff] [blame] | 878 | writer->Write("%s;\n", field->ToString().c_str()); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 879 | } |
| 880 | writer->Dedent(); |
| 881 | writer->Write("}\n"); |
| 882 | } |
| 883 | |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 884 | std::set<AidlAnnotation::Type> AidlStructuredParcelable::GetSupportedAnnotations() const { |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 885 | return {AidlAnnotation::Type::VINTF_STABILITY, |
| 886 | AidlAnnotation::Type::UNSUPPORTED_APP_USAGE, |
| 887 | AidlAnnotation::Type::HIDE, |
| 888 | AidlAnnotation::Type::JAVA_PASSTHROUGH, |
| 889 | AidlAnnotation::Type::JAVA_DEBUG, |
Devin Moore | c7e47a3 | 2020-08-07 10:55:25 -0700 | [diff] [blame] | 890 | AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE, |
Andrei Homescu | e61feb5 | 2020-08-18 15:44:24 -0700 | [diff] [blame] | 891 | AidlAnnotation::Type::FIXED_SIZE, |
| 892 | AidlAnnotation::Type::RUST_DERIVE}; |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 893 | } |
| 894 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 895 | bool AidlStructuredParcelable::CheckValid(const AidlTypenames& typenames) const { |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 896 | bool success = true; |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 897 | if (!AidlParcelable::CheckValid(typenames)) { |
| 898 | return false; |
| 899 | } |
Jooyung Han | d4057c4 | 2020-10-23 13:28:22 +0900 | [diff] [blame^] | 900 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 901 | for (const auto& v : GetFields()) { |
Steven Moreland | 265f3d4 | 2020-09-24 19:06:54 +0000 | [diff] [blame] | 902 | const bool field_valid = v->CheckValid(typenames); |
| 903 | success = success && field_valid; |
Jooyung Han | d4057c4 | 2020-10-23 13:28:22 +0900 | [diff] [blame^] | 904 | } |
Jeongik Cha | 13066da | 2020-08-06 15:43:19 +0900 | [diff] [blame] | 905 | |
Jooyung Han | d4057c4 | 2020-10-23 13:28:22 +0900 | [diff] [blame^] | 906 | std::set<std::string> fieldnames; |
| 907 | for (const auto& v : GetFields()) { |
| 908 | bool duplicated = !fieldnames.emplace(v->GetName()).second; |
Jeongik Cha | 9118025 | 2020-07-31 15:43:11 +0900 | [diff] [blame] | 909 | if (duplicated) { |
Jeongik Cha | 13066da | 2020-08-06 15:43:19 +0900 | [diff] [blame] | 910 | AIDL_ERROR(this) << "The parcelable '" << this->GetName() << "' has duplicate field name '" |
Jooyung Han | d4057c4 | 2020-10-23 13:28:22 +0900 | [diff] [blame^] | 911 | << v->GetName() << "'"; |
Jeongik Cha | 13066da | 2020-08-06 15:43:19 +0900 | [diff] [blame] | 912 | return false; |
| 913 | } |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 914 | } |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 915 | |
Jooyung Han | d4057c4 | 2020-10-23 13:28:22 +0900 | [diff] [blame^] | 916 | if (IsFixedSize()) { |
| 917 | for (const auto& v : GetFields()) { |
| 918 | if (!typenames.CanBeFixedSize(v->GetType())) { |
| 919 | AIDL_ERROR(v) << "The @FixedSize parcelable '" << this->GetName() << "' has a " |
| 920 | << "non-fixed size field named " << v->GetName() << "."; |
| 921 | success = false; |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | if (IsJavaOnlyImmutable()) { |
| 927 | std::set<std::string> getters; |
| 928 | for (const auto& v : GetFields()) { |
| 929 | if (!typenames.CanBeJavaOnlyImmutable(v->GetType())) { |
| 930 | AIDL_ERROR(v) << "The @JavaOnlyImmutable parcelable '" << this->GetName() << "' has a " |
| 931 | << "non-immutable field named '" << v->GetName() << "'."; |
| 932 | success = false; |
| 933 | } |
| 934 | bool duplicated = !getters.emplace(CapitalizeFirstLetter(*v, v->GetName())).second; |
| 935 | if (duplicated) { |
| 936 | AIDL_ERROR(this) << "The parcelable '" << this->GetName() << "' has duplicate field name '" |
| 937 | << v->GetName() << "' after capitalizing the first letter"; |
| 938 | return false; |
| 939 | } |
| 940 | } |
| 941 | } |
| 942 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 943 | return success; |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 944 | } |
| 945 | |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 946 | // TODO: we should treat every backend all the same in future. |
Steven Moreland | d59e317 | 2020-05-11 16:42:09 -0700 | [diff] [blame] | 947 | bool AidlTypeSpecifier::LanguageSpecificCheckValid(const AidlTypenames& typenames, |
| 948 | Options::Language lang) const { |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 949 | if ((lang == Options::Language::NDK || lang == Options::Language::RUST) && IsArray() && |
| 950 | GetName() == "IBinder") { |
| 951 | AIDL_ERROR(this) << "The " << Options::LanguageToString(lang) |
| 952 | << " backend does not support array of IBinder"; |
Steven Moreland | 0185d9b | 2020-05-15 23:21:22 +0000 | [diff] [blame] | 953 | return false; |
| 954 | } |
Jeongik Cha | 8f02a53 | 2020-10-14 00:16:28 +0900 | [diff] [blame] | 955 | if (lang == Options::Language::RUST && GetName() == "ParcelableHolder") { |
| 956 | // TODO(b/146611855): Remove it when Rust backend supports ParcelableHolder |
| 957 | AIDL_ERROR(this) << "The Rust backend does not support ParcelableHolder yet."; |
Jeongik Cha | 225519b | 2020-08-29 01:55:32 +0900 | [diff] [blame] | 958 | return false; |
| 959 | } |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 960 | if ((lang == Options::Language::NDK || lang == Options::Language::RUST) && IsArray() && |
| 961 | IsNullable()) { |
Steven Moreland | 0185d9b | 2020-05-15 23:21:22 +0000 | [diff] [blame] | 962 | if (GetName() == "ParcelFileDescriptor") { |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 963 | AIDL_ERROR(this) << "The " << Options::LanguageToString(lang) |
| 964 | << " backend does not support nullable array of ParcelFileDescriptor"; |
Steven Moreland | 0185d9b | 2020-05-15 23:21:22 +0000 | [diff] [blame] | 965 | return false; |
| 966 | } |
| 967 | |
Steven Moreland | d59e317 | 2020-05-11 16:42:09 -0700 | [diff] [blame] | 968 | const auto defined_type = typenames.TryGetDefinedType(GetName()); |
| 969 | if (defined_type != nullptr && defined_type->AsParcelable() != nullptr) { |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 970 | AIDL_ERROR(this) << "The " << Options::LanguageToString(lang) |
| 971 | << " backend does not support nullable array of parcelable"; |
Steven Moreland | d59e317 | 2020-05-11 16:42:09 -0700 | [diff] [blame] | 972 | return false; |
| 973 | } |
| 974 | } |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 975 | if (this->GetName() == "FileDescriptor" && |
| 976 | (lang == Options::Language::NDK || lang == Options::Language::RUST)) { |
| 977 | AIDL_ERROR(this) << "FileDescriptor isn't supported by the " << Options::LanguageToString(lang) |
| 978 | << " backend."; |
Steven Moreland | c8a4ca8 | 2020-01-21 17:50:08 -0800 | [diff] [blame] | 979 | return false; |
| 980 | } |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 981 | if (this->IsGeneric()) { |
| 982 | if (this->GetName() == "List") { |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 983 | if (lang == Options::Language::CPP) { |
Devin Moore | 2ac52f9 | 2020-03-23 15:39:36 -0700 | [diff] [blame] | 984 | const string& contained_type = this->GetTypeParameters()[0]->GetName(); |
| 985 | if (!(contained_type == "String" || contained_type == "IBinder")) { |
| 986 | AIDL_ERROR(this) << "List<" << contained_type |
| 987 | << "> is not supported. List in cpp supports only String and IBinder."; |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 988 | return false; |
| 989 | } |
Jeongik Cha | 08ca218 | 2019-11-21 14:01:13 +0900 | [diff] [blame] | 990 | } else if (lang == Options::Language::JAVA) { |
| 991 | const string& contained_type = this->GetTypeParameters()[0]->GetName(); |
| 992 | if (AidlTypenames::IsBuiltinTypename(contained_type)) { |
| 993 | if (contained_type != "String" && contained_type != "IBinder" && |
| 994 | contained_type != "ParcelFileDescriptor") { |
Devin Moore | 2ac52f9 | 2020-03-23 15:39:36 -0700 | [diff] [blame] | 995 | AIDL_ERROR(this) << "List<" << contained_type |
| 996 | << "> is not supported. List in Java supports only String, IBinder, " |
| 997 | "and ParcelFileDescriptor."; |
Jeongik Cha | 08ca218 | 2019-11-21 14:01:13 +0900 | [diff] [blame] | 998 | return false; |
| 999 | } |
| 1000 | } |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1001 | } |
Jeongik Cha | bb55b5e | 2020-01-07 23:11:26 +0900 | [diff] [blame] | 1002 | } |
| 1003 | } |
Devin Moore | 6a01ca1 | 2020-08-28 10:24:19 -0700 | [diff] [blame] | 1004 | |
| 1005 | if (this->IsArray()) { |
| 1006 | if (this->GetName() == "List" || this->GetName() == "Map" || |
| 1007 | this->GetName() == "CharSequence") { |
| 1008 | AIDL_ERROR(this) << this->GetName() << "[] is not supported."; |
Jeongik Cha | bb55b5e | 2020-01-07 23:11:26 +0900 | [diff] [blame] | 1009 | return false; |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1010 | } |
| 1011 | } |
Devin Moore | 6a01ca1 | 2020-08-28 10:24:19 -0700 | [diff] [blame] | 1012 | |
| 1013 | if (lang != Options::Language::JAVA) { |
| 1014 | if (this->GetName() == "List" && !this->IsGeneric()) { |
| 1015 | AIDL_ERROR(this) << "Currently, only the Java backend supports non-generic List."; |
| 1016 | return false; |
| 1017 | } |
| 1018 | if (this->GetName() == "Map" || this->GetName() == "CharSequence") { |
| 1019 | AIDL_ERROR(this) << "Currently, only Java backend supports " << this->GetName() << "."; |
| 1020 | return false; |
Jeongik Cha | 08ca218 | 2019-11-21 14:01:13 +0900 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1024 | return true; |
| 1025 | } |
| 1026 | |
| 1027 | // TODO: we should treat every backend all the same in future. |
Steven Moreland | d59e317 | 2020-05-11 16:42:09 -0700 | [diff] [blame] | 1028 | bool AidlParcelable::LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/, |
| 1029 | Options::Language lang) const { |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 1030 | if (lang == Options::Language::CPP || lang == Options::Language::NDK) { |
Steven Moreland | 0d9c26e | 2020-01-22 08:52:08 -0800 | [diff] [blame] | 1031 | const AidlParcelable* unstructured_parcelable = this->AsUnstructuredParcelable(); |
| 1032 | if (unstructured_parcelable != nullptr) { |
| 1033 | if (unstructured_parcelable->GetCppHeader().empty()) { |
| 1034 | AIDL_ERROR(unstructured_parcelable) |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1035 | << "Unstructured parcelable must have C++ header defined."; |
| 1036 | return false; |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | return true; |
| 1041 | } |
| 1042 | |
| 1043 | // TODO: we should treat every backend all the same in future. |
Steven Moreland | d59e317 | 2020-05-11 16:42:09 -0700 | [diff] [blame] | 1044 | bool AidlStructuredParcelable::LanguageSpecificCheckValid(const AidlTypenames& typenames, |
| 1045 | Options::Language lang) const { |
| 1046 | if (!AidlParcelable::LanguageSpecificCheckValid(typenames, lang)) { |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1047 | return false; |
| 1048 | } |
| 1049 | for (const auto& v : this->GetFields()) { |
Steven Moreland | d59e317 | 2020-05-11 16:42:09 -0700 | [diff] [blame] | 1050 | if (!v->GetType().LanguageSpecificCheckValid(typenames, lang)) { |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1051 | return false; |
| 1052 | } |
| 1053 | } |
| 1054 | return true; |
| 1055 | } |
| 1056 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1057 | AidlEnumerator::AidlEnumerator(const AidlLocation& location, const std::string& name, |
Daniel Norman | 2e4112d | 2019-10-03 10:22:35 -0700 | [diff] [blame] | 1058 | AidlConstantValue* value, const std::string& comments) |
| 1059 | : AidlNode(location), name_(name), value_(value), comments_(comments) {} |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1060 | |
| 1061 | bool AidlEnumerator::CheckValid(const AidlTypeSpecifier& enum_backing_type) const { |
| 1062 | if (GetValue() == nullptr) { |
| 1063 | return false; |
| 1064 | } |
| 1065 | if (!GetValue()->CheckValid()) { |
| 1066 | return false; |
| 1067 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1068 | if (GetValue()->ValueString(enum_backing_type, AidlConstantValueDecorator).empty()) { |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1069 | AIDL_ERROR(this) << "Enumerator type differs from enum backing type."; |
| 1070 | return false; |
| 1071 | } |
| 1072 | return true; |
| 1073 | } |
| 1074 | |
| 1075 | string AidlEnumerator::ValueString(const AidlTypeSpecifier& backing_type, |
| 1076 | const ConstantValueDecorator& decorator) const { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1077 | return GetValue()->ValueString(backing_type, decorator); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | AidlEnumDeclaration::AidlEnumDeclaration(const AidlLocation& location, const std::string& name, |
| 1081 | std::vector<std::unique_ptr<AidlEnumerator>>* enumerators, |
Jiyong Park | 1813218 | 2020-06-08 20:24:40 +0900 | [diff] [blame] | 1082 | const std::string& package, const std::string& comments) |
Daniel Norman | 2e4112d | 2019-10-03 10:22:35 -0700 | [diff] [blame] | 1083 | : AidlDefinedType(location, name, comments, package), enumerators_(std::move(*enumerators)) {} |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1084 | |
| 1085 | void AidlEnumDeclaration::SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type) { |
| 1086 | backing_type_ = std::move(type); |
| 1087 | } |
| 1088 | |
Steven Moreland | 59e53e4 | 2019-11-26 20:38:08 -0800 | [diff] [blame] | 1089 | bool AidlEnumDeclaration::Autofill() { |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 1090 | const AidlEnumerator* previous = nullptr; |
| 1091 | for (const auto& enumerator : enumerators_) { |
| 1092 | if (enumerator->GetValue() == nullptr) { |
| 1093 | if (previous == nullptr) { |
Devin Moore | df93ebb | 2020-03-25 14:03:35 -0700 | [diff] [blame] | 1094 | enumerator->SetValue( |
| 1095 | std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(GetLocation(), "0"))); |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 1096 | } else { |
Steven Moreland | 59e53e4 | 2019-11-26 20:38:08 -0800 | [diff] [blame] | 1097 | auto prev_value = std::unique_ptr<AidlConstantValue>( |
| 1098 | AidlConstantValue::ShallowIntegralCopy(*previous->GetValue())); |
| 1099 | if (prev_value == nullptr) { |
| 1100 | return false; |
| 1101 | } |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 1102 | enumerator->SetValue(std::make_unique<AidlBinaryConstExpression>( |
Devin Moore | df93ebb | 2020-03-25 14:03:35 -0700 | [diff] [blame] | 1103 | GetLocation(), std::move(prev_value), "+", |
| 1104 | std::unique_ptr<AidlConstantValue>(AidlConstantValue::Integral(GetLocation(), "1")))); |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 1105 | } |
| 1106 | } |
| 1107 | previous = enumerator.get(); |
| 1108 | } |
Steven Moreland | 59e53e4 | 2019-11-26 20:38:08 -0800 | [diff] [blame] | 1109 | return true; |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 1112 | std::set<AidlAnnotation::Type> AidlEnumDeclaration::GetSupportedAnnotations() const { |
| 1113 | return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::BACKING, |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 1114 | AidlAnnotation::Type::HIDE, AidlAnnotation::Type::JAVA_PASSTHROUGH}; |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | bool AidlEnumDeclaration::CheckValid(const AidlTypenames& typenames) const { |
| 1118 | if (!AidlDefinedType::CheckValid(typenames)) { |
| 1119 | return false; |
| 1120 | } |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1121 | if (backing_type_ == nullptr) { |
| 1122 | AIDL_ERROR(this) << "Enum declaration missing backing type."; |
| 1123 | return false; |
| 1124 | } |
| 1125 | bool success = true; |
| 1126 | for (const auto& enumerator : enumerators_) { |
| 1127 | success = success && enumerator->CheckValid(GetBackingType()); |
| 1128 | } |
| 1129 | return success; |
| 1130 | } |
| 1131 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1132 | void AidlEnumDeclaration::Dump(CodeWriter* writer) const { |
Steven Moreland | a5d9c5c | 2020-02-21 16:01:09 -0800 | [diff] [blame] | 1133 | DumpHeader(writer); |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 1134 | writer->Write("enum %s {\n", GetName().c_str()); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1135 | writer->Indent(); |
| 1136 | for (const auto& enumerator : GetEnumerators()) { |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1137 | writer->Write("%s = %s,\n", enumerator->GetName().c_str(), |
| 1138 | enumerator->ValueString(GetBackingType(), AidlConstantValueDecorator).c_str()); |
| 1139 | } |
| 1140 | writer->Dedent(); |
| 1141 | writer->Write("}\n"); |
| 1142 | } |
| 1143 | |
Jooyung Han | 2946afc | 2020-10-05 20:29:16 +0900 | [diff] [blame] | 1144 | AidlUnionDecl::AidlUnionDecl(const AidlLocation& location, const std::string& name, |
| 1145 | const std::string& package, const std::string& comments, |
| 1146 | std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables, |
| 1147 | std::vector<std::string>* type_params) |
| 1148 | : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params), |
| 1149 | variables_(std::move(*variables)) {} |
| 1150 | |
| 1151 | std::set<AidlAnnotation::Type> AidlUnionDecl::GetSupportedAnnotations() const { |
| 1152 | return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::HIDE, |
| 1153 | AidlAnnotation::Type::JAVA_PASSTHROUGH}; |
| 1154 | } |
| 1155 | |
| 1156 | void AidlUnionDecl::Dump(CodeWriter* writer) const { |
| 1157 | DumpHeader(writer); |
| 1158 | writer->Write("union %s {\n", GetName().c_str()); |
| 1159 | writer->Indent(); |
| 1160 | for (const auto& field : GetFields()) { |
| 1161 | if (field->GetType().IsHidden()) { |
| 1162 | AddHideComment(writer); |
| 1163 | } |
| 1164 | writer->Write("%s;\n", field->ToString().c_str()); |
| 1165 | } |
| 1166 | writer->Dedent(); |
| 1167 | writer->Write("}\n"); |
| 1168 | } |
| 1169 | |
Jooyung Han | fe89f12 | 2020-10-14 03:49:18 +0900 | [diff] [blame] | 1170 | bool AidlUnionDecl::CheckValid(const AidlTypenames& typenames) const { |
| 1171 | // visit parent |
| 1172 | if (!AidlParcelable::CheckValid(typenames)) { |
| 1173 | return false; |
| 1174 | } |
| 1175 | // visit members |
| 1176 | for (const auto& v : GetFields()) { |
| 1177 | if (!v->CheckValid(typenames)) { |
| 1178 | return false; |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | // now, visit self! |
| 1183 | bool success = true; |
| 1184 | |
| 1185 | // TODO(b/170807936) do we need to allow ParcelableHolder in union? |
| 1186 | for (const auto& v : GetFields()) { |
| 1187 | if (v->GetType().GetName() == "ParcelableHolder") { |
| 1188 | AIDL_ERROR(*v) << "A union can't have a member of ParcelableHolder '" << v->GetName() << "'"; |
| 1189 | success = false; |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | // first member should have default value (implicit or explicit) |
| 1194 | if (GetFields().empty()) { |
| 1195 | AIDL_ERROR(*this) << "The union '" << this->GetName() << "' has no fields."; |
| 1196 | return false; |
| 1197 | } |
| 1198 | |
| 1199 | return success; |
| 1200 | } |
| 1201 | |
| 1202 | // TODO: we should treat every backend all the same in future. |
| 1203 | bool AidlUnionDecl::LanguageSpecificCheckValid(const AidlTypenames& typenames, |
| 1204 | Options::Language lang) const { |
| 1205 | if (!AidlParcelable::LanguageSpecificCheckValid(typenames, lang)) { |
| 1206 | return false; |
| 1207 | } |
| 1208 | for (const auto& v : this->GetFields()) { |
| 1209 | if (!v->GetType().LanguageSpecificCheckValid(typenames, lang)) { |
| 1210 | return false; |
| 1211 | } |
| 1212 | } |
| 1213 | return true; |
| 1214 | } |
| 1215 | |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1216 | // TODO: we should treat every backend all the same in future. |
Steven Moreland | d59e317 | 2020-05-11 16:42:09 -0700 | [diff] [blame] | 1217 | bool AidlInterface::LanguageSpecificCheckValid(const AidlTypenames& typenames, |
| 1218 | Options::Language lang) const { |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1219 | for (const auto& m : this->GetMethods()) { |
Steven Moreland | d59e317 | 2020-05-11 16:42:09 -0700 | [diff] [blame] | 1220 | if (!m->GetType().LanguageSpecificCheckValid(typenames, lang)) { |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1221 | return false; |
| 1222 | } |
| 1223 | for (const auto& arg : m->GetArguments()) { |
Steven Moreland | d59e317 | 2020-05-11 16:42:09 -0700 | [diff] [blame] | 1224 | if (!arg->GetType().LanguageSpecificCheckValid(typenames, lang)) { |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1225 | return false; |
| 1226 | } |
| 1227 | } |
| 1228 | } |
| 1229 | return true; |
| 1230 | } |
| 1231 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1232 | AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name, |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 1233 | const std::string& comments, bool oneway, |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 1234 | std::vector<std::unique_ptr<AidlMember>>* members, |
Jiyong Park | 1813218 | 2020-06-08 20:24:40 +0900 | [diff] [blame] | 1235 | const std::string& package) |
Steven Moreland | acd5347 | 2018-12-14 10:17:26 -0800 | [diff] [blame] | 1236 | : AidlDefinedType(location, name, comments, package) { |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 1237 | for (auto& member : *members) { |
| 1238 | AidlMember* local = member.release(); |
| 1239 | AidlMethod* method = local->AsMethod(); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 1240 | AidlConstantDeclaration* constant = local->AsConstantDeclaration(); |
| 1241 | |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 1242 | AIDL_FATAL_IF(method != nullptr && constant != nullptr, member); |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 1243 | |
| 1244 | if (method) { |
Steven Moreland | 8c70ba9 | 2018-12-17 10:20:31 -0800 | [diff] [blame] | 1245 | method->ApplyInterfaceOneway(oneway); |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 1246 | methods_.emplace_back(method); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 1247 | } else if (constant) { |
| 1248 | constants_.emplace_back(constant); |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 1249 | } else { |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1250 | AIDL_FATAL(this) << "Member is neither method nor constant!"; |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | delete members; |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 1255 | } |
| 1256 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1257 | void AidlInterface::Dump(CodeWriter* writer) const { |
Steven Moreland | a5d9c5c | 2020-02-21 16:01:09 -0800 | [diff] [blame] | 1258 | DumpHeader(writer); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1259 | writer->Write("interface %s {\n", GetName().c_str()); |
| 1260 | writer->Indent(); |
| 1261 | for (const auto& method : GetMethods()) { |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1262 | if (method->IsHidden()) { |
| 1263 | AddHideComment(writer); |
| 1264 | } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1265 | writer->Write("%s;\n", method->ToString().c_str()); |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1266 | } |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 1267 | for (const auto& constdecl : GetConstantDeclarations()) { |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1268 | if (constdecl->GetType().IsHidden()) { |
| 1269 | AddHideComment(writer); |
| 1270 | } |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 1271 | writer->Write("%s;\n", constdecl->ToString().c_str()); |
| 1272 | } |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1273 | writer->Dedent(); |
| 1274 | writer->Write("}\n"); |
| 1275 | } |
| 1276 | |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 1277 | std::set<AidlAnnotation::Type> AidlInterface::GetSupportedAnnotations() const { |
| 1278 | return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::UNSUPPORTED_APP_USAGE, |
Jiyong Park | 27fd7fd | 2020-08-27 16:25:09 +0900 | [diff] [blame] | 1279 | AidlAnnotation::Type::HIDE, AidlAnnotation::Type::JAVA_PASSTHROUGH, |
| 1280 | AidlAnnotation::Type::DESCRIPTOR}; |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 1281 | } |
| 1282 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1283 | bool AidlInterface::CheckValid(const AidlTypenames& typenames) const { |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 1284 | if (!AidlDefinedType::CheckValid(typenames)) { |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 1285 | return false; |
| 1286 | } |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1287 | // Has to be a pointer due to deleting copy constructor. No idea why. |
| 1288 | map<string, const AidlMethod*> method_names; |
| 1289 | for (const auto& m : GetMethods()) { |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1290 | if (!m->GetType().CheckValid(typenames)) { |
| 1291 | return false; |
| 1292 | } |
| 1293 | |
Jeongik Cha | 649e8a7 | 2020-03-27 17:47:40 +0900 | [diff] [blame] | 1294 | // TODO(b/156872582): Support it when ParcelableHolder supports every backend. |
| 1295 | if (m->GetType().GetName() == "ParcelableHolder") { |
| 1296 | AIDL_ERROR(m) << "ParcelableHolder cannot be a return type"; |
| 1297 | return false; |
| 1298 | } |
Steven Moreland | acd5347 | 2018-12-14 10:17:26 -0800 | [diff] [blame] | 1299 | if (m->IsOneway() && m->GetType().GetName() != "void") { |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1300 | AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot return a value"; |
| 1301 | return false; |
| 1302 | } |
| 1303 | |
| 1304 | set<string> argument_names; |
| 1305 | for (const auto& arg : m->GetArguments()) { |
| 1306 | auto it = argument_names.find(arg->GetName()); |
| 1307 | if (it != argument_names.end()) { |
| 1308 | AIDL_ERROR(m) << "method '" << m->GetName() << "' has duplicate argument name '" |
| 1309 | << arg->GetName() << "'"; |
| 1310 | return false; |
| 1311 | } |
| 1312 | argument_names.insert(arg->GetName()); |
| 1313 | |
| 1314 | if (!arg->GetType().CheckValid(typenames)) { |
| 1315 | return false; |
| 1316 | } |
| 1317 | |
Jeongik Cha | 649e8a7 | 2020-03-27 17:47:40 +0900 | [diff] [blame] | 1318 | // TODO(b/156872582): Support it when ParcelableHolder supports every backend. |
| 1319 | if (arg->GetType().GetName() == "ParcelableHolder") { |
| 1320 | AIDL_ERROR(arg) << "ParcelableHolder cannot be an argument type"; |
| 1321 | return false; |
| 1322 | } |
Steven Moreland | acd5347 | 2018-12-14 10:17:26 -0800 | [diff] [blame] | 1323 | if (m->IsOneway() && arg->IsOut()) { |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1324 | AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot have out parameters"; |
| 1325 | return false; |
| 1326 | } |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1327 | const bool can_be_out = typenames.CanBeOutParameter(arg->GetType()); |
| 1328 | if (!arg->DirectionWasSpecified() && can_be_out) { |
| 1329 | AIDL_ERROR(arg) << "'" << arg->GetType().ToString() |
| 1330 | << "' can be an out type, so you must declare it as in, out, or inout."; |
| 1331 | return false; |
| 1332 | } |
| 1333 | |
| 1334 | if (arg->GetDirection() != AidlArgument::IN_DIR && !can_be_out) { |
| 1335 | AIDL_ERROR(arg) << "'" << arg->ToString() << "' can only be an in parameter."; |
| 1336 | return false; |
| 1337 | } |
| 1338 | |
| 1339 | // check that the name doesn't match a keyword |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1340 | if (IsJavaKeyword(arg->GetName().c_str())) { |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 1341 | AIDL_ERROR(arg) << "Argument name is a Java or aidl keyword"; |
| 1342 | return false; |
| 1343 | } |
| 1344 | |
| 1345 | // Reserve a namespace for internal use |
| 1346 | if (android::base::StartsWith(arg->GetName(), "_aidl")) { |
| 1347 | AIDL_ERROR(arg) << "Argument name cannot begin with '_aidl'"; |
| 1348 | return false; |
| 1349 | } |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | auto it = method_names.find(m->GetName()); |
| 1353 | // prevent duplicate methods |
| 1354 | if (it == method_names.end()) { |
| 1355 | method_names[m->GetName()] = m.get(); |
| 1356 | } else { |
| 1357 | AIDL_ERROR(m) << "attempt to redefine method " << m->GetName() << ":"; |
| 1358 | AIDL_ERROR(it->second) << "previously defined here."; |
| 1359 | return false; |
| 1360 | } |
| 1361 | |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 1362 | static set<string> reserved_methods{"asBinder()", "getInterfaceHash()", "getInterfaceVersion()", |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1363 | "getTransactionName(int)"}; |
| 1364 | |
| 1365 | if (reserved_methods.find(m->Signature()) != reserved_methods.end()) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1366 | AIDL_ERROR(m) << " method " << m->Signature() << " is reserved for internal use."; |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1367 | return false; |
| 1368 | } |
| 1369 | } |
Steven Moreland | 4d12f9a | 2018-10-31 14:30:55 -0700 | [diff] [blame] | 1370 | |
| 1371 | bool success = true; |
| 1372 | set<string> constant_names; |
| 1373 | for (const std::unique_ptr<AidlConstantDeclaration>& constant : GetConstantDeclarations()) { |
| 1374 | if (constant_names.count(constant->GetName()) > 0) { |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 1375 | AIDL_ERROR(constant) << "Found duplicate constant name '" << constant->GetName() << "'"; |
Steven Moreland | 4d12f9a | 2018-10-31 14:30:55 -0700 | [diff] [blame] | 1376 | success = false; |
| 1377 | } |
| 1378 | constant_names.insert(constant->GetName()); |
| 1379 | success = success && constant->CheckValid(typenames); |
| 1380 | } |
| 1381 | |
| 1382 | return success; |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1383 | } |
| 1384 | |
Jiyong Park | 27fd7fd | 2020-08-27 16:25:09 +0900 | [diff] [blame] | 1385 | std::string AidlInterface::GetDescriptor() const { |
| 1386 | std::string annotatedDescriptor = AidlAnnotatable::GetDescriptor(); |
| 1387 | if (annotatedDescriptor != "") { |
| 1388 | return annotatedDescriptor; |
| 1389 | } |
| 1390 | return GetCanonicalName(); |
| 1391 | } |
| 1392 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1393 | AidlImport::AidlImport(const AidlLocation& location, const std::string& needed_class) |
| 1394 | : AidlNode(location), needed_class_(needed_class) {} |