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