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