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_to_cpp.h"
18 #include "aidl_to_cpp_common.h"
19 #include "aidl_language.h"
20 #include "logging.h"
21
22 #include <android-base/stringprintf.h>
23 #include <android-base/strings.h>
24
25 #include <functional>
26 #include <unordered_map>
27
28 using android::base::Join;
29 using android::base::Split;
30 using android::base::StringPrintf;
31 using std::ostringstream;
32
33 namespace android {
34 namespace aidl {
35 namespace cpp {
36
37 namespace {
RawParcelMethod(const AidlTypeSpecifier & type,const AidlTypenames & typenames,bool readMethod)38 std::string RawParcelMethod(const AidlTypeSpecifier& type, const AidlTypenames& typenames,
39 bool readMethod) {
40 static map<string, string> kBuiltin = {
41 {"byte", "Byte"},
42 {"boolean", "Bool"},
43 {"char", "Char"},
44 {"double", "Double"},
45 {"FileDescriptor", "UniqueFileDescriptor"},
46 {"float", "Float"},
47 {"IBinder", "StrongBinder"},
48 {"int", "Int32"},
49 {"long", "Int64"},
50 {"ParcelFileDescriptor", "Parcelable"},
51 {"String", "String16"},
52 {"ParcelableHolder", "Parcelable"},
53 };
54
55 static map<string, string> kBuiltinVector = {
56 {"FileDescriptor", "UniqueFileDescriptorVector"},
57 {"double", "DoubleVector"},
58 {"char", "CharVector"},
59 {"boolean", "BoolVector"},
60 {"byte", "ByteVector"},
61 {"float", "FloatVector"},
62 {"IBinder", "StrongBinderVector"},
63 {"String", "String16Vector"},
64 {"int", "Int32Vector"},
65 {"long", "Int64Vector"},
66 {"ParcelFileDescriptor", "ParcelableVector"},
67 };
68
69 const bool nullable = type.IsNullable();
70 const bool isVector = type.IsArray() || typenames.IsList(type);
71 const bool utf8 = type.IsUtf8InCpp();
72
73 if (auto enum_decl = typenames.GetEnumDeclaration(type); enum_decl != nullptr) {
74 if (isVector) {
75 return "EnumVector";
76 } else {
77 return RawParcelMethod(enum_decl->GetBackingType(), typenames, readMethod);
78 }
79 }
80
81 if (isVector) {
82 string element_name;
83 if (typenames.IsList(type)) {
84 AIDL_FATAL_IF(type.GetTypeParameters().size() != 1, type);
85 element_name = type.GetTypeParameters().at(0)->GetName();
86 } else {
87 element_name = type.GetName();
88 }
89 if (kBuiltinVector.find(element_name) != kBuiltinVector.end()) {
90 AIDL_FATAL_IF(!AidlTypenames::IsBuiltinTypename(element_name), type);
91 if (utf8) {
92 AIDL_FATAL_IF(element_name != "String", type);
93 return readMethod ? "Utf8VectorFromUtf16Vector" : "Utf8VectorAsUtf16Vector";
94 }
95 return kBuiltinVector[element_name];
96 }
97 auto definedType = typenames.TryGetDefinedType(element_name);
98 if (definedType != nullptr && definedType->AsInterface() != nullptr) {
99 return "StrongBinderVector";
100 }
101 return "ParcelableVector";
102 }
103
104 const string& type_name = type.GetName();
105 if (kBuiltin.find(type_name) != kBuiltin.end()) {
106 AIDL_FATAL_IF(!AidlTypenames::IsBuiltinTypename(type_name), type);
107 if (type_name == "IBinder" && nullable && readMethod) {
108 return "NullableStrongBinder";
109 }
110 if (type_name == "ParcelFileDescriptor" && nullable && !readMethod) {
111 return "NullableParcelable";
112 }
113 if (utf8) {
114 AIDL_FATAL_IF(type_name != "String", type);
115 return readMethod ? "Utf8FromUtf16" : "Utf8AsUtf16";
116 }
117 return kBuiltin[type_name];
118 }
119
120 AIDL_FATAL_IF(AidlTypenames::IsBuiltinTypename(type.GetName()), type);
121 auto definedType = typenames.TryGetDefinedType(type.GetName());
122 // The type must be either primitive or interface or parcelable,
123 // so it cannot be nullptr.
124 AIDL_FATAL_IF(definedType == nullptr, type) << type.GetName() << " is not found.";
125
126 if (definedType->AsInterface() != nullptr) {
127 if (nullable && readMethod) {
128 return "NullableStrongBinder";
129 }
130 return "StrongBinder";
131 }
132
133 // Parcelable
134 if (nullable && !readMethod) {
135 return "NullableParcelable";
136 }
137 return "Parcelable";
138 }
139
GetRawCppName(const AidlTypeSpecifier & type)140 std::string GetRawCppName(const AidlTypeSpecifier& type) {
141 return "::" + Join(type.GetSplitName(), "::");
142 }
143
WrapIfNullable(const std::string type_str,const AidlTypeSpecifier & raw_type,const AidlTypenames & typenames)144 std::string WrapIfNullable(const std::string type_str, const AidlTypeSpecifier& raw_type,
145 const AidlTypenames& typenames) {
146 const auto& type = typenames.IsList(raw_type) ? (*raw_type.GetTypeParameters().at(0)) : raw_type;
147
148 if (raw_type.IsNullable() && !AidlTypenames::IsPrimitiveTypename(type.GetName()) &&
149 type.GetName() != "IBinder" && typenames.GetEnumDeclaration(type) == nullptr) {
150 return "::std::optional<" + type_str + ">";
151 }
152 return type_str;
153 }
154
GetCppName(const AidlTypeSpecifier & raw_type,const AidlTypenames & typenames)155 std::string GetCppName(const AidlTypeSpecifier& raw_type, const AidlTypenames& typenames) {
156 // map from AIDL built-in type name to the corresponding Cpp type name
157 static map<string, string> m = {
158 {"boolean", "bool"},
159 {"byte", "int8_t"},
160 {"char", "char16_t"},
161 {"double", "double"},
162 {"FileDescriptor", "::android::base::unique_fd"},
163 {"float", "float"},
164 {"IBinder", "::android::sp<::android::IBinder>"},
165 {"int", "int32_t"},
166 {"long", "int64_t"},
167 {"ParcelFileDescriptor", "::android::os::ParcelFileDescriptor"},
168 {"String", "::android::String16"},
169 {"void", "void"},
170 {"ParcelableHolder", "::android::os::ParcelableHolder"},
171 };
172 AIDL_FATAL_IF(typenames.IsList(raw_type) && raw_type.GetTypeParameters().size() != 1, raw_type);
173 const auto& type = typenames.IsList(raw_type) ? (*raw_type.GetTypeParameters().at(0)) : raw_type;
174 const string& aidl_name = type.GetName();
175 if (m.find(aidl_name) != m.end()) {
176 AIDL_FATAL_IF(!AidlTypenames::IsBuiltinTypename(aidl_name), raw_type);
177 if (aidl_name == "byte" && type.IsArray()) {
178 return "uint8_t";
179 } else if (raw_type.IsUtf8InCpp()) {
180 AIDL_FATAL_IF(aidl_name != "String", type);
181 return WrapIfNullable("::std::string", raw_type, typenames);
182 }
183 return WrapIfNullable(m[aidl_name], raw_type, typenames);
184 }
185 auto definedType = typenames.TryGetDefinedType(type.GetName());
186 if (definedType != nullptr && definedType->AsInterface() != nullptr) {
187 return "::android::sp<" + GetRawCppName(type) + ">";
188 }
189
190 return WrapIfNullable(GetRawCppName(type), raw_type, typenames);
191 }
192 } // namespace
ConstantValueDecorator(const AidlTypeSpecifier & type,const std::string & raw_value)193 std::string ConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value) {
194 if (type.IsArray()) {
195 return raw_value;
196 }
197
198 if (type.GetName() == "long") {
199 return raw_value + "L";
200 }
201
202 if (type.GetName() == "String" && !type.IsUtf8InCpp()) {
203 return "::android::String16(" + raw_value + ")";
204 }
205
206 if (auto defined_type = type.GetDefinedType(); defined_type) {
207 auto enum_type = defined_type->AsEnumDeclaration();
208 AIDL_FATAL_IF(!enum_type, type) << "Invalid type for \"" << raw_value << "\"";
209 return GetRawCppName(type) + "::" + raw_value.substr(raw_value.find_last_of('.') + 1);
210 }
211
212 return raw_value;
213 };
214
GetTransactionIdFor(const AidlInterface & iface,const AidlMethod & method)215 std::string GetTransactionIdFor(const AidlInterface& iface, const AidlMethod& method) {
216 return ClassName(iface, ClassNames::SERVER) + "::TRANSACTION_" + method.GetName();
217 }
218
CppNameOf(const AidlTypeSpecifier & type,const AidlTypenames & typenames)219 std::string CppNameOf(const AidlTypeSpecifier& type, const AidlTypenames& typenames) {
220 if (type.IsArray() || typenames.IsList(type)) {
221 std::string cpp_name = GetCppName(type, typenames);
222 if (type.IsNullable()) {
223 return "::std::optional<::std::vector<" + cpp_name + ">>";
224 }
225 return "::std::vector<" + cpp_name + ">";
226 } else if (type.IsGeneric()) {
227 std::vector<std::string> type_params;
228 for (const auto& parameter : type.GetTypeParameters()) {
229 type_params.push_back(CppNameOf(*parameter, typenames));
230 }
231 return StringPrintf("%s<%s>", GetCppName(type, typenames).c_str(),
232 base::Join(type_params, ", ").c_str());
233 }
234 return GetCppName(type, typenames);
235 }
236
IsNonCopyableType(const AidlTypeSpecifier & type,const AidlTypenames & typenames)237 bool IsNonCopyableType(const AidlTypeSpecifier& type, const AidlTypenames& typenames) {
238 if (type.IsArray() || typenames.IsList(type)) {
239 return false;
240 }
241
242 const std::string cpp_name = GetCppName(type, typenames);
243 if (cpp_name == "::android::base::unique_fd") {
244 return true;
245 }
246 return false;
247 }
248
ParcelReadMethodOf(const AidlTypeSpecifier & type,const AidlTypenames & typenames)249 std::string ParcelReadMethodOf(const AidlTypeSpecifier& type, const AidlTypenames& typenames) {
250 return "read" + RawParcelMethod(type, typenames, true /* readMethod */);
251 }
252
ParcelReadCastOf(const AidlTypeSpecifier & type,const AidlTypenames & typenames,const std::string & variable_name)253 std::string ParcelReadCastOf(const AidlTypeSpecifier& type, const AidlTypenames& typenames,
254 const std::string& variable_name) {
255 if (auto enum_decl = typenames.GetEnumDeclaration(type);
256 enum_decl != nullptr && !type.IsArray()) {
257 return StringPrintf("reinterpret_cast<%s *>(%s)",
258 CppNameOf(enum_decl->GetBackingType(), typenames).c_str(),
259 variable_name.c_str());
260 }
261
262 return variable_name;
263 }
264
ParcelWriteMethodOf(const AidlTypeSpecifier & type,const AidlTypenames & typenames)265 std::string ParcelWriteMethodOf(const AidlTypeSpecifier& type, const AidlTypenames& typenames) {
266 return "write" + RawParcelMethod(type, typenames, false /* readMethod */);
267 }
268
ParcelWriteCastOf(const AidlTypeSpecifier & type,const AidlTypenames & typenames,const std::string & variable_name)269 std::string ParcelWriteCastOf(const AidlTypeSpecifier& type, const AidlTypenames& typenames,
270 const std::string& variable_name) {
271 if (auto enum_decl = typenames.GetEnumDeclaration(type);
272 enum_decl != nullptr && !type.IsArray()) {
273 return StringPrintf("static_cast<%s>(%s)",
274 CppNameOf(enum_decl->GetBackingType(), typenames).c_str(),
275 variable_name.c_str());
276 }
277
278 if (typenames.GetInterface(type) != nullptr) {
279 return GetRawCppName(type) + "::asBinder(" + variable_name + ")";
280 }
281
282 return variable_name;
283 }
284
AddHeaders(const AidlTypeSpecifier & type,const AidlTypenames & typenames,std::set<std::string> * headers)285 void AddHeaders(const AidlTypeSpecifier& type, const AidlTypenames& typenames,
286 std::set<std::string>* headers) {
287 AIDL_FATAL_IF(typenames.IsList(type) && type.GetTypeParameters().size() != 1, type);
288 bool isVector = type.IsArray() || typenames.IsList(type);
289 bool isNullable = type.IsNullable();
290 bool utf8 = type.IsUtf8InCpp();
291
292 if (isVector) {
293 headers->insert("vector");
294 }
295 if (type.IsGeneric()) {
296 for (const auto& parameter : type.GetTypeParameters()) {
297 AddHeaders(*parameter, typenames, headers);
298 }
299 }
300 if (isNullable) {
301 if (type.GetName() != "IBinder") {
302 headers->insert("optional");
303 }
304 }
305 if (typenames.IsList(type)) {
306 // Nothing else to do for List.
307 return;
308 }
309 if (type.GetName() == "String") {
310 headers->insert(utf8 ? "string" : "utils/String16.h");
311 return;
312 }
313 if (type.GetName() == "IBinder") {
314 headers->insert("binder/IBinder.h");
315 return;
316 }
317 if (type.GetName() == "FileDescriptor") {
318 headers->insert("android-base/unique_fd.h");
319 return;
320 }
321 if (type.GetName() == "ParcelFileDescriptor") {
322 headers->insert("binder/ParcelFileDescriptor.h");
323 return;
324 }
325 if (type.GetName() == "ParcelableHolder") {
326 headers->insert("binder/ParcelableHolder.h");
327 return;
328 }
329
330 static const std::set<string> need_cstdint{"byte", "int", "long"};
331 if (need_cstdint.find(type.GetName()) != need_cstdint.end()) {
332 headers->insert("cstdint");
333 return;
334 }
335
336 if (AidlTypenames::IsPrimitiveTypename(type.GetName())) {
337 return;
338 }
339
340 auto definedType = typenames.TryGetDefinedType(type.GetName());
341 AIDL_FATAL_IF(definedType == nullptr, type) << "Unexpected type: " << type.GetName();
342
343 if (definedType->AsInterface() != nullptr || definedType->AsStructuredParcelable() != nullptr ||
344 definedType->AsEnumDeclaration() != nullptr || definedType->AsUnionDeclaration() != nullptr) {
345 AddHeaders(*definedType, headers);
346 } else if (definedType->AsParcelable() != nullptr) {
347 const std::string cpp_header = definedType->AsParcelable()->GetCppHeader();
348 AIDL_FATAL_IF(cpp_header.empty(), definedType->AsParcelable())
349 << "Parcelable " << definedType->AsParcelable()->GetCanonicalName()
350 << " has no C++ header defined.";
351 headers->insert(cpp_header);
352 }
353 }
354
AddHeaders(const AidlDefinedType & definedType,std::set<std::string> * headers)355 void AddHeaders(const AidlDefinedType& definedType, std::set<std::string>* headers) {
356 vector<string> name = definedType.GetSplitPackage();
357 name.push_back(definedType.GetName());
358 const std::string cpp_header = Join(name, '/') + ".h";
359 headers->insert(cpp_header);
360 }
361
362 } // namespace cpp
363 } // namespace aidl
364 } // namespace android
365