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"
19 #include "logging.h"
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::Split;
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
43 // The built-in AIDL types..
44 static const set<string> kBuiltinTypes = {
45 "void", "boolean", "byte", "char", "int",
46 "long", "float", "double", "String", "List",
47 "Map", "IBinder", "FileDescriptor", "CharSequence", "ParcelFileDescriptor"};
48
49 static const set<string> kPrimitiveTypes = {"void", "boolean", "byte", "char",
50 "int", "long", "float", "double"};
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 {"android.os.ParcelFileDescriptor", "ParcelFileDescriptor"},
61 };
62
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
IsValidName(const string & name)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
AddDefinedType(unique_ptr<AidlDefinedType> type)84 bool AidlTypenames::AddDefinedType(unique_ptr<AidlDefinedType> type) {
85 const string name = type->GetCanonicalName();
86 if (defined_types_.find(name) != defined_types_.end()) {
87 return false;
88 }
89 if (!IsValidName(type->GetPackage()) || !IsValidName(type->GetName())) {
90 return false;
91 }
92 defined_types_.emplace(name, std::move(type));
93 return true;
94 }
95
AddPreprocessedType(unique_ptr<AidlDefinedType> type)96 bool AidlTypenames::AddPreprocessedType(unique_ptr<AidlDefinedType> type) {
97 const string name = type->GetCanonicalName();
98 if (preprocessed_types_.find(name) != preprocessed_types_.end()) {
99 return false;
100 }
101 if (!IsValidName(type->GetPackage()) || !IsValidName(type->GetName())) {
102 return false;
103 }
104 preprocessed_types_.insert(make_pair(name, std::move(type)));
105 return true;
106 }
107
IsBuiltinTypename(const string & type_name)108 bool AidlTypenames::IsBuiltinTypename(const string& type_name) {
109 return kBuiltinTypes.find(type_name) != kBuiltinTypes.end() ||
110 kJavaLikeTypeToAidlType.find(type_name) != kJavaLikeTypeToAidlType.end();
111 }
112
IsPrimitiveTypename(const string & type_name)113 bool AidlTypenames::IsPrimitiveTypename(const string& type_name) {
114 return kPrimitiveTypes.find(type_name) != kPrimitiveTypes.end();
115 }
116
TryGetDefinedType(const string & type_name) const117 const AidlDefinedType* AidlTypenames::TryGetDefinedType(const string& type_name) const {
118 // Do the exact match first.
119 auto found_def = defined_types_.find(type_name);
120 if (found_def != defined_types_.end()) {
121 return found_def->second.get();
122 }
123
124 auto found_prep = preprocessed_types_.find(type_name);
125 if (found_prep != preprocessed_types_.end()) {
126 return found_prep->second.get();
127 }
128
129 // Then match with the class name. Defined types has higher priority than
130 // types from the preprocessed file.
131 for (auto it = defined_types_.begin(); it != defined_types_.end(); it++) {
132 if (it->second->GetName() == type_name) {
133 return it->second.get();
134 }
135 }
136
137 for (auto it = preprocessed_types_.begin(); it != preprocessed_types_.end(); it++) {
138 if (it->second->GetName() == type_name) {
139 return it->second.get();
140 }
141 }
142
143 return nullptr;
144 }
145
ResolveTypename(const string & type_name) const146 pair<string, bool> AidlTypenames::ResolveTypename(const string& type_name) const {
147 if (IsBuiltinTypename(type_name)) {
148 auto found = kJavaLikeTypeToAidlType.find(type_name);
149 if (found != kJavaLikeTypeToAidlType.end()) {
150 return make_pair(found->second, true);
151 }
152 return make_pair(type_name, true);
153 }
154 const AidlDefinedType* defined_type = TryGetDefinedType(type_name);
155 if (defined_type != nullptr) {
156 return make_pair(defined_type->GetCanonicalName(), true);
157 } else {
158 return make_pair(type_name, false);
159 }
160 }
161
162 // Only T[], List, Map, ParcelFileDescriptor and Parcelable can be an out parameter.
CanBeOutParameter(const AidlTypeSpecifier & type) const163 bool AidlTypenames::CanBeOutParameter(const AidlTypeSpecifier& type) const {
164 const string& name = type.GetName();
165 if (IsBuiltinTypename(name)) {
166 return type.IsArray() || type.GetName() == "List" || type.GetName() == "Map" ||
167 type.GetName() == "ParcelFileDescriptor";
168 }
169 const AidlDefinedType* t = TryGetDefinedType(type.GetName());
170 CHECK(t != nullptr) << "Unrecognized type: '" << type.GetName() << "'";
171 return t->AsParcelable() != nullptr;
172 }
173
IterateTypes(const std::function<void (const AidlDefinedType &)> & body) const174 void AidlTypenames::IterateTypes(const std::function<void(const AidlDefinedType&)>& body) const {
175 for (const auto& kv : defined_types_) {
176 body(*kv.second);
177 }
178 for (const auto& kv : preprocessed_types_) {
179 body(*kv.second);
180 }
181 }
182
Reset()183 void AidlTypenames::Reset() {
184 defined_types_.clear();
185 preprocessed_types_.clear();
186 }
187
188 } // namespace aidl
189 } // namespace android
190