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