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