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