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 | |
| 30 | using android::base::EndsWith; |
| 31 | using android::base::Join; |
| 32 | using android::base::Split; |
| 33 | using android::base::Trim; |
| 34 | |
| 35 | using std::make_pair; |
| 36 | using std::map; |
| 37 | using std::pair; |
| 38 | using std::set; |
| 39 | using std::string; |
| 40 | using std::unique_ptr; |
| 41 | using std::vector; |
| 42 | |
| 43 | namespace android { |
| 44 | namespace aidl { |
| 45 | |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 46 | // The built-in AIDL types.. |
| 47 | static const set<string> kBuiltinTypes = { |
| 48 | "void", "boolean", "byte", "char", "int", "long", |
| 49 | "float", "double", "String", "List", "Map", "IBinder", |
| 50 | "FileDescriptor", "CharSequence"}; |
| 51 | |
| 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 = { |
| 58 | {"java.util.List", "List"}, |
| 59 | {"java.util.Map", "Map"}, |
| 60 | }; |
| 61 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame^] | 62 | // Package name and type name can't be one of these as they are keywords |
| 63 | // in Java and C++. Using these names will eventually cause compilation error, |
| 64 | // so checking this here is not a must have, but early detection of errors |
| 65 | // is always better. |
| 66 | static const set<string> kInvalidNames = { |
| 67 | "break", "case", "catch", "char", "class", "continue", "default", |
| 68 | "do", "double", "else", "enum", "false", "float", "for", |
| 69 | "goto", "if", "int", "long", "new", "private", "protected", |
| 70 | "public", "return", "short", "static", "switch", "this", "throw", |
| 71 | "true", "try", "void", "volatile", "while"}; |
| 72 | |
| 73 | static bool IsValidName(const string& name) { |
| 74 | vector<string> pieces = Split(name, "."); |
| 75 | for (const auto& piece : pieces) { |
| 76 | if (kInvalidNames.find(piece) != kInvalidNames.end()) { |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | bool AidlTypenames::AddDefinedType(unique_ptr<AidlDefinedType> type) { |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 84 | const string name = type->GetCanonicalName(); |
| 85 | if (defined_types_.find(name) != defined_types_.end()) { |
| 86 | return false; |
| 87 | } |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame^] | 88 | if (!IsValidName(type->GetPackage()) || !IsValidName(type->GetName())) { |
| 89 | return false; |
| 90 | } |
| 91 | defined_types_.emplace(name, std::move(type)); |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | |
| 95 | bool AidlTypenames::AddPreprocessedType(unique_ptr<AidlDefinedType> type) { |
| 96 | const string name = type->GetCanonicalName(); |
| 97 | if (preprocessed_types_.find(name) != preprocessed_types_.end()) { |
| 98 | return false; |
| 99 | } |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame^] | 100 | if (!IsValidName(type->GetPackage()) || !IsValidName(type->GetName())) { |
| 101 | return false; |
| 102 | } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 103 | preprocessed_types_.insert(make_pair(name, std::move(type))); |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | bool AidlTypenames::IsBuiltinTypename(const string& type_name) { |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 108 | return kBuiltinTypes.find(type_name) != kBuiltinTypes.end() || |
| 109 | kJavaLikeTypeToAidlType.find(type_name) != kJavaLikeTypeToAidlType.end(); |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 110 | } |
| 111 | |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 112 | const AidlDefinedType* AidlTypenames::TryGetDefinedType(const string& type_name) const { |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 113 | // Do the exact match first. |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 114 | auto found_def = defined_types_.find(type_name); |
| 115 | if (found_def != defined_types_.end()) { |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame^] | 116 | return found_def->second.get(); |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 117 | } |
| 118 | |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 119 | auto found_prep = preprocessed_types_.find(type_name); |
| 120 | if (found_prep != preprocessed_types_.end()) { |
| 121 | return found_prep->second.get(); |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // Then match with the class name. Defined types has higher priority than |
| 125 | // types from the preprocessed file. |
| 126 | for (auto it = defined_types_.begin(); it != defined_types_.end(); it++) { |
| 127 | if (it->second->GetName() == type_name) { |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame^] | 128 | return it->second.get(); |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | for (auto it = preprocessed_types_.begin(); it != preprocessed_types_.end(); it++) { |
| 133 | if (it->second->GetName() == type_name) { |
| 134 | return it->second.get(); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return nullptr; |
| 139 | } |
| 140 | |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 141 | pair<string, bool> AidlTypenames::ResolveTypename(const string& type_name) const { |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 142 | if (IsBuiltinTypename(type_name)) { |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 143 | auto found = kJavaLikeTypeToAidlType.find(type_name); |
| 144 | if (found != kJavaLikeTypeToAidlType.end()) { |
| 145 | return make_pair(found->second, true); |
| 146 | } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 147 | return make_pair(type_name, true); |
| 148 | } |
| 149 | const AidlDefinedType* defined_type = TryGetDefinedType(type_name); |
| 150 | if (defined_type != nullptr) { |
| 151 | return make_pair(defined_type->GetCanonicalName(), true); |
| 152 | } else { |
| 153 | return make_pair(type_name, false); |
| 154 | } |
| 155 | } |
| 156 | |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 157 | // Only T[], List, Map, and Parcelable can be an out parameter. |
| 158 | bool AidlTypenames::CanBeOutParameter(const AidlTypeSpecifier& type) const { |
| 159 | const string& name = type.GetName(); |
| 160 | if (IsBuiltinTypename(name)) { |
| 161 | return type.IsArray() || type.GetName() == "List" || type.GetName() == "Map"; |
| 162 | } |
| 163 | const AidlDefinedType* t = TryGetDefinedType(type.GetName()); |
| 164 | CHECK(t != nullptr) << "Unrecognized type: '" << type.GetName() << "'"; |
| 165 | return t->AsParcelable() != nullptr; |
| 166 | } |
| 167 | |
Steven Moreland | 6cee348 | 2018-07-18 14:39:58 -0700 | [diff] [blame] | 168 | void AidlTypenames::IterateTypes(const std::function<void(const AidlDefinedType&)>& body) const { |
| 169 | for (const auto& kv : defined_types_) { |
| 170 | body(*kv.second); |
| 171 | } |
| 172 | for (const auto& kv : preprocessed_types_) { |
| 173 | body(*kv.second); |
| 174 | } |
| 175 | } |
| 176 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame^] | 177 | void AidlTypenames::Reset() { |
| 178 | defined_types_.clear(); |
| 179 | preprocessed_types_.clear(); |
| 180 | } |
| 181 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 182 | } // namespace aidl |
| 183 | } // namespace android |