blob: 51c26d7e565af08d7ffa07ed743fc43aab12b7a2 [file] [log] [blame]
Jiyong Park1deecc32018-07-17 01:14:41 +09001/*
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 Park1d2df7d2018-07-23 15:22:50 +090019#include "logging.h"
Jiyong Park1deecc32018-07-17 01:14:41 +090020
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 Park1deecc32018-07-17 01:14:41 +090030using android::base::Split;
Jiyong Park1deecc32018-07-17 01:14:41 +090031
32using std::make_pair;
33using std::map;
34using std::pair;
35using std::set;
36using std::string;
37using std::unique_ptr;
38using std::vector;
39
40namespace android {
41namespace aidl {
42
Jiyong Parkac896042018-07-23 21:23:05 +090043// The built-in AIDL types..
44static const set<string> kBuiltinTypes = {
Jiyong Parke05195e2018-10-08 18:24:23 +090045 "void", "boolean", "byte", "char", "int",
46 "long", "float", "double", "String", "List",
47 "Map", "IBinder", "FileDescriptor", "CharSequence", "ParcelFileDescriptor"};
Jiyong Parkac896042018-07-23 21:23:05 +090048
Jeongik Chadb0f59e2018-11-01 18:11:21 +090049static const set<string> kPrimitiveTypes = {"void", "boolean", "byte", "char",
50 "int", "long", "float", "double"};
51
Jiyong Parkac896042018-07-23 21:23:05 +090052// 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.
57static const map<string, string> kJavaLikeTypeToAidlType = {
Jiyong Parke05195e2018-10-08 18:24:23 +090058 {"java.util.List", "List"},
59 {"java.util.Map", "Map"},
60 {"android.os.ParcelFileDescriptor", "ParcelFileDescriptor"},
Jiyong Parkac896042018-07-23 21:23:05 +090061};
62
Jiyong Parkb034bf02018-07-30 17:44:33 +090063// 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.
67static 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
74static 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 Cha047c5ee2019-08-07 23:16:49 +090084bool AidlTypenames::IsIgnorableImport(const string& import) const {
Raju Kulkarni7c27de32020-10-30 12:55:11 -070085 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 Park8f6ec462020-01-19 20:52:47 +090088 // 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 Cha047c5ee2019-08-07 23:16:49 +090095}
96
Jiyong Parkb034bf02018-07-30 17:44:33 +090097bool AidlTypenames::AddDefinedType(unique_ptr<AidlDefinedType> type) {
Jiyong Park1deecc32018-07-17 01:14:41 +090098 const string name = type->GetCanonicalName();
99 if (defined_types_.find(name) != defined_types_.end()) {
100 return false;
101 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900102 if (!IsValidName(type->GetPackage()) || !IsValidName(type->GetName())) {
103 return false;
104 }
105 defined_types_.emplace(name, std::move(type));
Jiyong Park1deecc32018-07-17 01:14:41 +0900106 return true;
107}
108
109bool 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 Parkb034bf02018-07-30 17:44:33 +0900114 if (!IsValidName(type->GetPackage()) || !IsValidName(type->GetName())) {
115 return false;
116 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900117 preprocessed_types_.insert(make_pair(name, std::move(type)));
118 return true;
119}
120
121bool AidlTypenames::IsBuiltinTypename(const string& type_name) {
Jiyong Parkac896042018-07-23 21:23:05 +0900122 return kBuiltinTypes.find(type_name) != kBuiltinTypes.end() ||
123 kJavaLikeTypeToAidlType.find(type_name) != kJavaLikeTypeToAidlType.end();
Jiyong Park1deecc32018-07-17 01:14:41 +0900124}
125
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900126bool AidlTypenames::IsPrimitiveTypename(const string& type_name) {
127 return kPrimitiveTypes.find(type_name) != kPrimitiveTypes.end();
128}
129
Jiyong Parkac896042018-07-23 21:23:05 +0900130const AidlDefinedType* AidlTypenames::TryGetDefinedType(const string& type_name) const {
Jiyong Park8f6ec462020-01-19 20:52:47 +0900131 return TryGetDefinedTypeImpl(type_name).type;
132}
133
134AidlTypenames::DefinedImplResult AidlTypenames::TryGetDefinedTypeImpl(
135 const string& type_name) const {
Jiyong Park1deecc32018-07-17 01:14:41 +0900136 // Do the exact match first.
Jiyong Parkac896042018-07-23 21:23:05 +0900137 auto found_def = defined_types_.find(type_name);
138 if (found_def != defined_types_.end()) {
Jiyong Park8f6ec462020-01-19 20:52:47 +0900139 return DefinedImplResult(found_def->second.get(), false);
Jiyong Park1deecc32018-07-17 01:14:41 +0900140 }
141
Jiyong Parkac896042018-07-23 21:23:05 +0900142 auto found_prep = preprocessed_types_.find(type_name);
143 if (found_prep != preprocessed_types_.end()) {
Jiyong Park8f6ec462020-01-19 20:52:47 +0900144 return DefinedImplResult(found_prep->second.get(), true);
Jiyong Park1deecc32018-07-17 01:14:41 +0900145 }
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 Park8f6ec462020-01-19 20:52:47 +0900151 return DefinedImplResult(it->second.get(), false);
Jiyong Park1deecc32018-07-17 01:14:41 +0900152 }
153 }
154
155 for (auto it = preprocessed_types_.begin(); it != preprocessed_types_.end(); it++) {
156 if (it->second->GetName() == type_name) {
Jiyong Park8f6ec462020-01-19 20:52:47 +0900157 return DefinedImplResult(it->second.get(), true);
Jiyong Park1deecc32018-07-17 01:14:41 +0900158 }
159 }
160
Jiyong Park8f6ec462020-01-19 20:52:47 +0900161 return DefinedImplResult(nullptr, false);
Jiyong Park1deecc32018-07-17 01:14:41 +0900162}
163
Jiyong Parkac896042018-07-23 21:23:05 +0900164pair<string, bool> AidlTypenames::ResolveTypename(const string& type_name) const {
Jiyong Park1deecc32018-07-17 01:14:41 +0900165 if (IsBuiltinTypename(type_name)) {
Jiyong Parkac896042018-07-23 21:23:05 +0900166 auto found = kJavaLikeTypeToAidlType.find(type_name);
167 if (found != kJavaLikeTypeToAidlType.end()) {
168 return make_pair(found->second, true);
169 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900170 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 Parke05195e2018-10-08 18:24:23 +0900180// Only T[], List, Map, ParcelFileDescriptor and Parcelable can be an out parameter.
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900181bool AidlTypenames::CanBeOutParameter(const AidlTypeSpecifier& type) const {
182 const string& name = type.GetName();
Daniel Normanee8674f2019-09-20 16:07:00 -0700183 if (IsBuiltinTypename(name) || GetEnumDeclaration(type)) {
Jiyong Parke05195e2018-10-08 18:24:23 +0900184 return type.IsArray() || type.GetName() == "List" || type.GetName() == "Map" ||
185 type.GetName() == "ParcelFileDescriptor";
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900186 }
187 const AidlDefinedType* t = TryGetDefinedType(type.GetName());
188 CHECK(t != nullptr) << "Unrecognized type: '" << type.GetName() << "'";
189 return t->AsParcelable() != nullptr;
190}
191
Daniel Norman85aed542019-08-21 12:01:14 -0700192const 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
201const 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 Moreland6cee3482018-07-18 14:39:58 -0700210void 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 Parkb034bf02018-07-30 17:44:33 +0900219void AidlTypenames::Reset() {
220 defined_types_.clear();
221 preprocessed_types_.clear();
222}
223
Jiyong Park1deecc32018-07-17 01:14:41 +0900224} // namespace aidl
225} // namespace android