Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018, 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 | |
| 17 | #include "aidl_typenames.h" |
| 18 | #include "aidl_language.h" |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 19 | #include "logging.h" |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 20 | |
| 21 | #include <android-base/strings.h> |
| 22 | |
| 23 | #include <map> |
| 24 | #include <memory> |
| 25 | #include <set> |
| 26 | #include <string> |
| 27 | #include <utility> |
| 28 | #include <vector> |
| 29 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 30 | using android::base::Split; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 31 | |
| 32 | using std::make_pair; |
| 33 | using std::map; |
| 34 | using std::pair; |
| 35 | using std::set; |
| 36 | using std::string; |
| 37 | using std::unique_ptr; |
| 38 | using std::vector; |
| 39 | |
| 40 | namespace android { |
| 41 | namespace aidl { |
| 42 | |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 43 | // The built-in AIDL types.. |
Jeongik Cha | 649e8a7 | 2020-03-27 17:47:40 +0900 | [diff] [blame] | 44 | static const set<string> kBuiltinTypes = {"void", |
| 45 | "boolean", |
| 46 | "byte", |
| 47 | "char", |
| 48 | "int", |
| 49 | "long", |
| 50 | "float", |
| 51 | "double", |
| 52 | "String", |
| 53 | "List", |
| 54 | "Map", |
| 55 | "IBinder", |
| 56 | "FileDescriptor", |
| 57 | "CharSequence", |
| 58 | "ParcelFileDescriptor", |
| 59 | "ParcelableHolder"}; |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 60 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 61 | static const set<string> kPrimitiveTypes = {"void", "boolean", "byte", "char", |
| 62 | "int", "long", "float", "double"}; |
| 63 | |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 64 | // Note: these types may look wrong because they look like Java |
| 65 | // types, but they have long been supported from the time when Java |
| 66 | // was the only target language of this compiler. They are added here for |
| 67 | // backwards compatibility, but we internally treat them as List and Map, |
| 68 | // respectively. |
| 69 | static const map<string, string> kJavaLikeTypeToAidlType = { |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 70 | {"java.util.List", "List"}, |
| 71 | {"java.util.Map", "Map"}, |
| 72 | {"android.os.ParcelFileDescriptor", "ParcelFileDescriptor"}, |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 73 | }; |
| 74 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 75 | // Package name and type name can't be one of these as they are keywords |
| 76 | // in Java and C++. Using these names will eventually cause compilation error, |
| 77 | // so checking this here is not a must have, but early detection of errors |
| 78 | // is always better. |
Steven Moreland | 79f0ed1 | 2020-04-27 19:43:11 -0700 | [diff] [blame] | 79 | static const set<string> kCppOrJavaReservedWord = { |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 80 | "break", "case", "catch", "char", "class", "continue", "default", |
| 81 | "do", "double", "else", "enum", "false", "float", "for", |
| 82 | "goto", "if", "int", "long", "new", "private", "protected", |
| 83 | "public", "return", "short", "static", "switch", "this", "throw", |
| 84 | "true", "try", "void", "volatile", "while"}; |
| 85 | |
Steven Moreland | 79f0ed1 | 2020-04-27 19:43:11 -0700 | [diff] [blame] | 86 | static bool HasValidNameComponents(const AidlDefinedType& defined) { |
| 87 | bool success = true; |
| 88 | vector<string> pieces = Split(defined.GetCanonicalName(), "."); |
| 89 | for (const string& piece : pieces) { |
| 90 | if (kCppOrJavaReservedWord.find(piece) != kCppOrJavaReservedWord.end()) { |
| 91 | AIDL_ERROR(defined) << defined.GetCanonicalName() << " is an invalid name because '" << piece |
| 92 | << "' is a Java or C++ identifier."; |
| 93 | success = false; |
| 94 | } |
| 95 | // not checking kJavaLikeTypeToAidl, since that wouldn't make sense here |
| 96 | if (kBuiltinTypes.find(piece) != kBuiltinTypes.end()) { |
| 97 | AIDL_ERROR(defined) << defined.GetCanonicalName() << " is an invalid name because '" << piece |
| 98 | << "' is a built-in AIDL type."; |
| 99 | success = false; |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 100 | } |
| 101 | } |
Steven Moreland | 79f0ed1 | 2020-04-27 19:43:11 -0700 | [diff] [blame] | 102 | return success; |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 103 | } |
| 104 | |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 105 | bool AidlTypenames::IsIgnorableImport(const string& import) const { |
| 106 | static set<string> ignore_import = {"android.os.IInterface", "android.os.IBinder", |
| 107 | "android.os.Parcelable", "android.os.Parcel", |
| 108 | "android.content.Context", "java.lang.String"}; |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 109 | // these known built-in types don't need to be imported |
| 110 | const bool in_ignore_import = ignore_import.find(import) != ignore_import.end(); |
| 111 | // an already defined type doesn't need to be imported again unless it is from |
| 112 | // the preprocessed file |
| 113 | auto ret = TryGetDefinedTypeImpl(import); |
| 114 | const bool defined_type_not_from_preprocessed = ret.type != nullptr && !ret.from_preprocessed; |
| 115 | return in_ignore_import || defined_type_not_from_preprocessed; |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 116 | } |
| 117 | |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 118 | bool AidlTypenames::AddDocument(std::unique_ptr<AidlDocument> doc) { |
| 119 | for (const auto& type : doc->DefinedTypes()) { |
| 120 | if (defined_types_.find(type->GetCanonicalName()) != defined_types_.end()) { |
| 121 | return false; |
| 122 | } |
| 123 | if (!HasValidNameComponents(*type)) { |
| 124 | return false; |
| 125 | } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 126 | } |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 127 | documents_.push_back(std::move(doc)); |
| 128 | for (const auto& type : documents_.back()->DefinedTypes()) { |
| 129 | defined_types_.emplace(type->GetCanonicalName(), type.get()); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 130 | } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 131 | return true; |
| 132 | } |
| 133 | |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 134 | const AidlDocument& AidlTypenames::MainDocument() const { |
| 135 | CHECK(documents_.size() != 0) << "Main document doesn't exist"; |
| 136 | return *(documents_[0]); |
| 137 | } |
| 138 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 139 | bool AidlTypenames::AddPreprocessedType(unique_ptr<AidlDefinedType> type) { |
| 140 | const string name = type->GetCanonicalName(); |
| 141 | if (preprocessed_types_.find(name) != preprocessed_types_.end()) { |
| 142 | return false; |
| 143 | } |
Steven Moreland | 79f0ed1 | 2020-04-27 19:43:11 -0700 | [diff] [blame] | 144 | if (!HasValidNameComponents(*type)) { |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 145 | return false; |
| 146 | } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 147 | preprocessed_types_.insert(make_pair(name, std::move(type))); |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | bool AidlTypenames::IsBuiltinTypename(const string& type_name) { |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 152 | return kBuiltinTypes.find(type_name) != kBuiltinTypes.end() || |
| 153 | kJavaLikeTypeToAidlType.find(type_name) != kJavaLikeTypeToAidlType.end(); |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 154 | } |
| 155 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 156 | bool AidlTypenames::IsPrimitiveTypename(const string& type_name) { |
| 157 | return kPrimitiveTypes.find(type_name) != kPrimitiveTypes.end(); |
| 158 | } |
| 159 | |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 160 | const AidlDefinedType* AidlTypenames::TryGetDefinedType(const string& type_name) const { |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 161 | return TryGetDefinedTypeImpl(type_name).type; |
| 162 | } |
| 163 | |
| 164 | AidlTypenames::DefinedImplResult AidlTypenames::TryGetDefinedTypeImpl( |
| 165 | const string& type_name) const { |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 166 | // Do the exact match first. |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 167 | auto found_def = defined_types_.find(type_name); |
| 168 | if (found_def != defined_types_.end()) { |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 169 | return DefinedImplResult(found_def->second, false); |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 170 | } |
| 171 | |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 172 | auto found_prep = preprocessed_types_.find(type_name); |
| 173 | if (found_prep != preprocessed_types_.end()) { |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 174 | return DefinedImplResult(found_prep->second.get(), true); |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // Then match with the class name. Defined types has higher priority than |
| 178 | // types from the preprocessed file. |
| 179 | for (auto it = defined_types_.begin(); it != defined_types_.end(); it++) { |
| 180 | if (it->second->GetName() == type_name) { |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 181 | return DefinedImplResult(it->second, false); |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | for (auto it = preprocessed_types_.begin(); it != preprocessed_types_.end(); it++) { |
| 186 | if (it->second->GetName() == type_name) { |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 187 | return DefinedImplResult(it->second.get(), true); |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 191 | return DefinedImplResult(nullptr, false); |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 192 | } |
| 193 | |
Jiyong Park | 0cf03b1 | 2020-07-22 19:36:34 +0900 | [diff] [blame] | 194 | std::vector<AidlDefinedType*> AidlTypenames::AllDefinedTypes() const { |
| 195 | std::vector<AidlDefinedType*> res; |
| 196 | for (const auto& d : AllDocuments()) { |
| 197 | for (const auto& t : d->DefinedTypes()) { |
| 198 | res.push_back(t.get()); |
| 199 | } |
| 200 | } |
| 201 | return res; |
| 202 | } |
| 203 | |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 204 | AidlTypenames::ResolvedTypename AidlTypenames::ResolveTypename(const string& type_name) const { |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 205 | if (IsBuiltinTypename(type_name)) { |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 206 | auto found = kJavaLikeTypeToAidlType.find(type_name); |
| 207 | if (found != kJavaLikeTypeToAidlType.end()) { |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 208 | return {found->second, true}; |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 209 | } |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 210 | return {type_name, true}; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 211 | } |
| 212 | const AidlDefinedType* defined_type = TryGetDefinedType(type_name); |
| 213 | if (defined_type != nullptr) { |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 214 | return {defined_type->GetCanonicalName(), true}; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 215 | } else { |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 216 | return {type_name, false}; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 220 | // Only immutable Parcelable, primitive type, and String, and List, Map, array of the types can be |
| 221 | // immutable. |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 222 | bool AidlTypenames::CanBeJavaOnlyImmutable(const AidlTypeSpecifier& type) const { |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 223 | const string& name = type.GetName(); |
| 224 | if (type.IsGeneric()) { |
| 225 | if (type.GetName() == "List" || type.GetName() == "Map") { |
| 226 | const auto& types = type.GetTypeParameters(); |
| 227 | return std::all_of(types.begin(), types.end(), |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 228 | [this](const auto& t) { return CanBeJavaOnlyImmutable(*t); }); |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 229 | } |
| 230 | AIDL_ERROR(type) << "For a generic type, an immutable parcelable can contain only List or Map."; |
| 231 | return false; |
| 232 | } |
| 233 | if (IsPrimitiveTypename(name) || name == "String") { |
| 234 | return true; |
| 235 | } |
| 236 | const AidlDefinedType* t = TryGetDefinedType(type.GetName()); |
| 237 | if (t == nullptr) { |
| 238 | AIDL_ERROR(type) << "An immutable parcelable can contain only immutable Parcelable, primitive " |
| 239 | "type, and String."; |
| 240 | return false; |
| 241 | } |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 242 | return t->IsJavaOnlyImmutable(); |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 243 | } |
| 244 | |
Devin Moore | c7e47a3 | 2020-08-07 10:55:25 -0700 | [diff] [blame] | 245 | // Only FixedSize Parcelable, primitive types, and enum types can be FixedSize. |
| 246 | bool AidlTypenames::CanBeFixedSize(const AidlTypeSpecifier& type) const { |
| 247 | const string& name = type.GetName(); |
| 248 | if (type.IsGeneric() || type.IsArray()) { |
| 249 | return false; |
| 250 | } |
| 251 | if (IsPrimitiveTypename(name)) { |
| 252 | return true; |
| 253 | } |
| 254 | const AidlDefinedType* t = TryGetDefinedType(type.GetName()); |
| 255 | AIDL_FATAL_IF(t == nullptr, type) |
| 256 | << "Failed to look up type. Cannot determine if it can be fixed size."; |
| 257 | |
| 258 | if (t->AsEnumDeclaration()) { |
| 259 | return true; |
| 260 | } |
| 261 | return t->IsFixedSize(); |
| 262 | } |
| 263 | |
Devin Moore | 693e673 | 2020-08-28 11:07:38 -0700 | [diff] [blame] | 264 | bool AidlTypenames::IsList(const AidlTypeSpecifier& type) { |
Devin Moore | f12cc11 | 2020-08-26 16:25:00 -0700 | [diff] [blame] | 265 | return type.GetName() == "List"; |
| 266 | } |
| 267 | |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 268 | // Only T[], List, Map, ParcelFileDescriptor and mutable Parcelable can be an out parameter. |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 269 | bool AidlTypenames::CanBeOutParameter(const AidlTypeSpecifier& type) const { |
| 270 | const string& name = type.GetName(); |
Daniel Norman | ee8674f | 2019-09-20 16:07:00 -0700 | [diff] [blame] | 271 | if (IsBuiltinTypename(name) || GetEnumDeclaration(type)) { |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 272 | return type.IsArray() || type.GetName() == "List" || type.GetName() == "Map" || |
| 273 | type.GetName() == "ParcelFileDescriptor"; |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 274 | } |
| 275 | const AidlDefinedType* t = TryGetDefinedType(type.GetName()); |
| 276 | CHECK(t != nullptr) << "Unrecognized type: '" << type.GetName() << "'"; |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 277 | // An 'out' field is passed as an argument, so it doesn't make sense if it is immutable. |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 278 | return t->AsParcelable() != nullptr && !t->IsJavaOnlyImmutable(); |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 279 | } |
| 280 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 281 | const AidlEnumDeclaration* AidlTypenames::GetEnumDeclaration(const AidlTypeSpecifier& type) const { |
| 282 | if (auto defined_type = TryGetDefinedType(type.GetName()); defined_type != nullptr) { |
| 283 | if (auto enum_decl = defined_type->AsEnumDeclaration(); enum_decl != nullptr) { |
| 284 | return enum_decl; |
| 285 | } |
| 286 | } |
| 287 | return nullptr; |
| 288 | } |
| 289 | |
| 290 | const AidlInterface* AidlTypenames::GetInterface(const AidlTypeSpecifier& type) const { |
| 291 | if (auto defined_type = TryGetDefinedType(type.GetName()); defined_type != nullptr) { |
| 292 | if (auto intf = defined_type->AsInterface(); intf != nullptr) { |
| 293 | return intf; |
| 294 | } |
| 295 | } |
| 296 | return nullptr; |
| 297 | } |
| 298 | |
Steven Moreland | 6cee348 | 2018-07-18 14:39:58 -0700 | [diff] [blame] | 299 | void AidlTypenames::IterateTypes(const std::function<void(const AidlDefinedType&)>& body) const { |
| 300 | for (const auto& kv : defined_types_) { |
| 301 | body(*kv.second); |
| 302 | } |
| 303 | for (const auto& kv : preprocessed_types_) { |
| 304 | body(*kv.second); |
| 305 | } |
| 306 | } |
| 307 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 308 | } // namespace aidl |
| 309 | } // namespace android |