blob: 10862853d3eb47c38ddd032f972e536b1cda0a76 [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
30using android::base::EndsWith;
31using android::base::Join;
32using android::base::Split;
33using android::base::Trim;
34
35using std::make_pair;
36using std::map;
37using std::pair;
38using std::set;
39using std::string;
40using std::unique_ptr;
41using std::vector;
42
43namespace android {
44namespace aidl {
45
Jiyong Parkac896042018-07-23 21:23:05 +090046// The built-in AIDL types..
47static 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.
57static const map<string, string> kJavaLikeTypeToAidlType = {
58 {"java.util.List", "List"},
59 {"java.util.Map", "Map"},
60};
61
Jiyong Parkb034bf02018-07-30 17:44:33 +090062// 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.
66static 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
73static 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
83bool AidlTypenames::AddDefinedType(unique_ptr<AidlDefinedType> type) {
Jiyong Park1deecc32018-07-17 01:14:41 +090084 const string name = type->GetCanonicalName();
85 if (defined_types_.find(name) != defined_types_.end()) {
86 return false;
87 }
Jiyong Parkb034bf02018-07-30 17:44:33 +090088 if (!IsValidName(type->GetPackage()) || !IsValidName(type->GetName())) {
89 return false;
90 }
91 defined_types_.emplace(name, std::move(type));
Jiyong Park1deecc32018-07-17 01:14:41 +090092 return true;
93}
94
95bool 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 Parkb034bf02018-07-30 17:44:33 +0900100 if (!IsValidName(type->GetPackage()) || !IsValidName(type->GetName())) {
101 return false;
102 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900103 preprocessed_types_.insert(make_pair(name, std::move(type)));
104 return true;
105}
106
107bool AidlTypenames::IsBuiltinTypename(const string& type_name) {
Jiyong Parkac896042018-07-23 21:23:05 +0900108 return kBuiltinTypes.find(type_name) != kBuiltinTypes.end() ||
109 kJavaLikeTypeToAidlType.find(type_name) != kJavaLikeTypeToAidlType.end();
Jiyong Park1deecc32018-07-17 01:14:41 +0900110}
111
Jiyong Parkac896042018-07-23 21:23:05 +0900112const AidlDefinedType* AidlTypenames::TryGetDefinedType(const string& type_name) const {
Jiyong Park1deecc32018-07-17 01:14:41 +0900113 // Do the exact match first.
Jiyong Parkac896042018-07-23 21:23:05 +0900114 auto found_def = defined_types_.find(type_name);
115 if (found_def != defined_types_.end()) {
Jiyong Parkb034bf02018-07-30 17:44:33 +0900116 return found_def->second.get();
Jiyong Park1deecc32018-07-17 01:14:41 +0900117 }
118
Jiyong Parkac896042018-07-23 21:23:05 +0900119 auto found_prep = preprocessed_types_.find(type_name);
120 if (found_prep != preprocessed_types_.end()) {
121 return found_prep->second.get();
Jiyong Park1deecc32018-07-17 01:14:41 +0900122 }
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 Parkb034bf02018-07-30 17:44:33 +0900128 return it->second.get();
Jiyong Park1deecc32018-07-17 01:14:41 +0900129 }
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 Parkac896042018-07-23 21:23:05 +0900141pair<string, bool> AidlTypenames::ResolveTypename(const string& type_name) const {
Jiyong Park1deecc32018-07-17 01:14:41 +0900142 if (IsBuiltinTypename(type_name)) {
Jiyong Parkac896042018-07-23 21:23:05 +0900143 auto found = kJavaLikeTypeToAidlType.find(type_name);
144 if (found != kJavaLikeTypeToAidlType.end()) {
145 return make_pair(found->second, true);
146 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900147 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 Park1d2df7d2018-07-23 15:22:50 +0900157// Only T[], List, Map, and Parcelable can be an out parameter.
158bool 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 Moreland6cee3482018-07-18 14:39:58 -0700168void 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 Parkb034bf02018-07-30 17:44:33 +0900177void AidlTypenames::Reset() {
178 defined_types_.clear();
179 preprocessed_types_.clear();
180}
181
Jiyong Park1deecc32018-07-17 01:14:41 +0900182} // namespace aidl
183} // namespace android