• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
IsIgnorableImport(const string & import) const84 bool AidlTypenames::IsIgnorableImport(const string& import) const {
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"};
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;
95 }
96 
AddDefinedType(unique_ptr<AidlDefinedType> type)97 bool AidlTypenames::AddDefinedType(unique_ptr<AidlDefinedType> type) {
98   const string name = type->GetCanonicalName();
99   if (defined_types_.find(name) != defined_types_.end()) {
100     return false;
101   }
102   if (!IsValidName(type->GetPackage()) || !IsValidName(type->GetName())) {
103     return false;
104   }
105   defined_types_.emplace(name, std::move(type));
106   return true;
107 }
108 
AddPreprocessedType(unique_ptr<AidlDefinedType> type)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   }
114   if (!IsValidName(type->GetPackage()) || !IsValidName(type->GetName())) {
115     return false;
116   }
117   preprocessed_types_.insert(make_pair(name, std::move(type)));
118   return true;
119 }
120 
IsBuiltinTypename(const string & type_name)121 bool AidlTypenames::IsBuiltinTypename(const string& type_name) {
122   return kBuiltinTypes.find(type_name) != kBuiltinTypes.end() ||
123       kJavaLikeTypeToAidlType.find(type_name) != kJavaLikeTypeToAidlType.end();
124 }
125 
IsPrimitiveTypename(const string & type_name)126 bool AidlTypenames::IsPrimitiveTypename(const string& type_name) {
127   return kPrimitiveTypes.find(type_name) != kPrimitiveTypes.end();
128 }
129 
TryGetDefinedType(const string & type_name) const130 const AidlDefinedType* AidlTypenames::TryGetDefinedType(const string& type_name) const {
131   return TryGetDefinedTypeImpl(type_name).type;
132 }
133 
TryGetDefinedTypeImpl(const string & type_name) const134 AidlTypenames::DefinedImplResult AidlTypenames::TryGetDefinedTypeImpl(
135     const string& type_name) const {
136   // Do the exact match first.
137   auto found_def = defined_types_.find(type_name);
138   if (found_def != defined_types_.end()) {
139     return DefinedImplResult(found_def->second.get(), false);
140   }
141 
142   auto found_prep = preprocessed_types_.find(type_name);
143   if (found_prep != preprocessed_types_.end()) {
144     return DefinedImplResult(found_prep->second.get(), true);
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) {
151       return DefinedImplResult(it->second.get(), false);
152     }
153   }
154 
155   for (auto it = preprocessed_types_.begin(); it != preprocessed_types_.end(); it++) {
156     if (it->second->GetName() == type_name) {
157       return DefinedImplResult(it->second.get(), true);
158     }
159   }
160 
161   return DefinedImplResult(nullptr, false);
162 }
163 
ResolveTypename(const string & type_name) const164 pair<string, bool> AidlTypenames::ResolveTypename(const string& type_name) const {
165   if (IsBuiltinTypename(type_name)) {
166     auto found = kJavaLikeTypeToAidlType.find(type_name);
167     if (found != kJavaLikeTypeToAidlType.end()) {
168       return make_pair(found->second, true);
169     }
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 
180 // Only T[], List, Map, ParcelFileDescriptor and Parcelable can be an out parameter.
CanBeOutParameter(const AidlTypeSpecifier & type) const181 bool AidlTypenames::CanBeOutParameter(const AidlTypeSpecifier& type) const {
182   const string& name = type.GetName();
183   if (IsBuiltinTypename(name) || GetEnumDeclaration(type)) {
184     return type.IsArray() || type.GetName() == "List" || type.GetName() == "Map" ||
185            type.GetName() == "ParcelFileDescriptor";
186   }
187   const AidlDefinedType* t = TryGetDefinedType(type.GetName());
188   CHECK(t != nullptr) << "Unrecognized type: '" << type.GetName() << "'";
189   return t->AsParcelable() != nullptr;
190 }
191 
GetEnumDeclaration(const AidlTypeSpecifier & type) const192 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 
GetInterface(const AidlTypeSpecifier & type) const201 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 
IterateTypes(const std::function<void (const AidlDefinedType &)> & body) const210 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 
Reset()219 void AidlTypenames::Reset() {
220   defined_types_.clear();
221   preprocessed_types_.clear();
222 }
223 
224 }  // namespace aidl
225 }  // namespace android
226