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.. |
| 44 | static const set<string> kBuiltinTypes = { |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 45 | "void", "boolean", "byte", "char", "int", |
| 46 | "long", "float", "double", "String", "List", |
| 47 | "Map", "IBinder", "FileDescriptor", "CharSequence", "ParcelFileDescriptor"}; |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 48 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 49 | static const set<string> kPrimitiveTypes = {"void", "boolean", "byte", "char", |
| 50 | "int", "long", "float", "double"}; |
| 51 | |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 52 | // Note: these types may look wrong because they look like Java |
| 53 | // types, but they have long been supported from the time when Java |
| 54 | // was the only target language of this compiler. They are added here for |
| 55 | // backwards compatibility, but we internally treat them as List and Map, |
| 56 | // respectively. |
| 57 | static const map<string, string> kJavaLikeTypeToAidlType = { |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 58 | {"java.util.List", "List"}, |
| 59 | {"java.util.Map", "Map"}, |
| 60 | {"android.os.ParcelFileDescriptor", "ParcelFileDescriptor"}, |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 61 | }; |
| 62 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 63 | // Package name and type name can't be one of these as they are keywords |
| 64 | // in Java and C++. Using these names will eventually cause compilation error, |
| 65 | // so checking this here is not a must have, but early detection of errors |
| 66 | // is always better. |
| 67 | static const set<string> kInvalidNames = { |
| 68 | "break", "case", "catch", "char", "class", "continue", "default", |
| 69 | "do", "double", "else", "enum", "false", "float", "for", |
| 70 | "goto", "if", "int", "long", "new", "private", "protected", |
| 71 | "public", "return", "short", "static", "switch", "this", "throw", |
| 72 | "true", "try", "void", "volatile", "while"}; |
| 73 | |
| 74 | static bool IsValidName(const string& name) { |
| 75 | vector<string> pieces = Split(name, "."); |
| 76 | for (const auto& piece : pieces) { |
| 77 | if (kInvalidNames.find(piece) != kInvalidNames.end()) { |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | return true; |
| 82 | } |
| 83 | |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 84 | bool AidlTypenames::IsIgnorableImport(const string& import) const { |
Raju Kulkarni | 7c27de3 | 2020-10-30 12:55:11 -0700 | [diff] [blame] | 85 | static set<string> ignore_import = { |
| 86 | "android.os.IInterface", "android.os.IBinder", "android.os.Parcelable", "android.os.Parcel", |
| 87 | "android.content.Context", "java.lang.String", "java.lang.CharSequence"}; |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 88 | // these known built-in types don't need to be imported |
| 89 | const bool in_ignore_import = ignore_import.find(import) != ignore_import.end(); |
| 90 | // an already defined type doesn't need to be imported again unless it is from |
| 91 | // the preprocessed file |
| 92 | auto ret = TryGetDefinedTypeImpl(import); |
| 93 | const bool defined_type_not_from_preprocessed = ret.type != nullptr && !ret.from_preprocessed; |
| 94 | return in_ignore_import || defined_type_not_from_preprocessed; |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 95 | } |
| 96 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 97 | bool AidlTypenames::AddDefinedType(unique_ptr<AidlDefinedType> type) { |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 98 | const string name = type->GetCanonicalName(); |
| 99 | if (defined_types_.find(name) != defined_types_.end()) { |
| 100 | return false; |
| 101 | } |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 102 | if (!IsValidName(type->GetPackage()) || !IsValidName(type->GetName())) { |
| 103 | return false; |
| 104 | } |
| 105 | defined_types_.emplace(name, std::move(type)); |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | |
| 109 | bool AidlTypenames::AddPreprocessedType(unique_ptr<AidlDefinedType> type) { |
| 110 | const string name = type->GetCanonicalName(); |
| 111 | if (preprocessed_types_.find(name) != preprocessed_types_.end()) { |
| 112 | return false; |
| 113 | } |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 114 | if (!IsValidName(type->GetPackage()) || !IsValidName(type->GetName())) { |
| 115 | return false; |
| 116 | } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 117 | preprocessed_types_.insert(make_pair(name, std::move(type))); |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | bool AidlTypenames::IsBuiltinTypename(const string& type_name) { |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 122 | return kBuiltinTypes.find(type_name) != kBuiltinTypes.end() || |
| 123 | kJavaLikeTypeToAidlType.find(type_name) != kJavaLikeTypeToAidlType.end(); |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 124 | } |
| 125 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 126 | bool AidlTypenames::IsPrimitiveTypename(const string& type_name) { |
| 127 | return kPrimitiveTypes.find(type_name) != kPrimitiveTypes.end(); |
| 128 | } |
| 129 | |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 130 | const AidlDefinedType* AidlTypenames::TryGetDefinedType(const string& type_name) const { |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 131 | return TryGetDefinedTypeImpl(type_name).type; |
| 132 | } |
| 133 | |
| 134 | AidlTypenames::DefinedImplResult AidlTypenames::TryGetDefinedTypeImpl( |
| 135 | const string& type_name) const { |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 136 | // Do the exact match first. |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 137 | auto found_def = defined_types_.find(type_name); |
| 138 | if (found_def != defined_types_.end()) { |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 139 | return DefinedImplResult(found_def->second.get(), false); |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 140 | } |
| 141 | |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 142 | auto found_prep = preprocessed_types_.find(type_name); |
| 143 | if (found_prep != preprocessed_types_.end()) { |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 144 | return DefinedImplResult(found_prep->second.get(), true); |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // Then match with the class name. Defined types has higher priority than |
| 148 | // types from the preprocessed file. |
| 149 | for (auto it = defined_types_.begin(); it != defined_types_.end(); it++) { |
| 150 | if (it->second->GetName() == type_name) { |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 151 | return DefinedImplResult(it->second.get(), false); |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | for (auto it = preprocessed_types_.begin(); it != preprocessed_types_.end(); it++) { |
| 156 | if (it->second->GetName() == type_name) { |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 157 | return DefinedImplResult(it->second.get(), true); |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 161 | return DefinedImplResult(nullptr, false); |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 162 | } |
| 163 | |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 164 | pair<string, bool> AidlTypenames::ResolveTypename(const string& type_name) const { |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 165 | if (IsBuiltinTypename(type_name)) { |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 166 | auto found = kJavaLikeTypeToAidlType.find(type_name); |
| 167 | if (found != kJavaLikeTypeToAidlType.end()) { |
| 168 | return make_pair(found->second, true); |
| 169 | } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 170 | return make_pair(type_name, true); |
| 171 | } |
| 172 | const AidlDefinedType* defined_type = TryGetDefinedType(type_name); |
| 173 | if (defined_type != nullptr) { |
| 174 | return make_pair(defined_type->GetCanonicalName(), true); |
| 175 | } else { |
| 176 | return make_pair(type_name, false); |
| 177 | } |
| 178 | } |
| 179 | |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 180 | // Only T[], List, Map, ParcelFileDescriptor and Parcelable can be an out parameter. |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 181 | bool AidlTypenames::CanBeOutParameter(const AidlTypeSpecifier& type) const { |
| 182 | const string& name = type.GetName(); |
Daniel Norman | ee8674f | 2019-09-20 16:07:00 -0700 | [diff] [blame] | 183 | if (IsBuiltinTypename(name) || GetEnumDeclaration(type)) { |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 184 | return type.IsArray() || type.GetName() == "List" || type.GetName() == "Map" || |
| 185 | type.GetName() == "ParcelFileDescriptor"; |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 186 | } |
| 187 | const AidlDefinedType* t = TryGetDefinedType(type.GetName()); |
| 188 | CHECK(t != nullptr) << "Unrecognized type: '" << type.GetName() << "'"; |
| 189 | return t->AsParcelable() != nullptr; |
| 190 | } |
| 191 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 192 | const AidlEnumDeclaration* AidlTypenames::GetEnumDeclaration(const AidlTypeSpecifier& type) const { |
| 193 | if (auto defined_type = TryGetDefinedType(type.GetName()); defined_type != nullptr) { |
| 194 | if (auto enum_decl = defined_type->AsEnumDeclaration(); enum_decl != nullptr) { |
| 195 | return enum_decl; |
| 196 | } |
| 197 | } |
| 198 | return nullptr; |
| 199 | } |
| 200 | |
| 201 | const AidlInterface* AidlTypenames::GetInterface(const AidlTypeSpecifier& type) const { |
| 202 | if (auto defined_type = TryGetDefinedType(type.GetName()); defined_type != nullptr) { |
| 203 | if (auto intf = defined_type->AsInterface(); intf != nullptr) { |
| 204 | return intf; |
| 205 | } |
| 206 | } |
| 207 | return nullptr; |
| 208 | } |
| 209 | |
Steven Moreland | 6cee348 | 2018-07-18 14:39:58 -0700 | [diff] [blame] | 210 | void AidlTypenames::IterateTypes(const std::function<void(const AidlDefinedType&)>& body) const { |
| 211 | for (const auto& kv : defined_types_) { |
| 212 | body(*kv.second); |
| 213 | } |
| 214 | for (const auto& kv : preprocessed_types_) { |
| 215 | body(*kv.second); |
| 216 | } |
| 217 | } |
| 218 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 219 | void AidlTypenames::Reset() { |
| 220 | defined_types_.clear(); |
| 221 | preprocessed_types_.clear(); |
| 222 | } |
| 223 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 224 | } // namespace aidl |
| 225 | } // namespace android |