blob: 623820caa5f1b5aee8968a47b5ebb231b1f43e32 [file] [log] [blame]
Steven Moreland860b1942018-08-16 14:59:28 -07001/*
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_language.h"
Jiyong Parkce50e262018-10-29 09:54:20 +090019#include "logging.h"
20
Daniel Norman85aed542019-08-21 12:01:14 -070021#include <android-base/stringprintf.h>
Jeongik Cha1a7ab642019-07-29 17:31:02 +090022#include <android-base/strings.h>
23
Jiyong Parkce50e262018-10-29 09:54:20 +090024#include <functional>
25#include <unordered_map>
Steven Moreland860b1942018-08-16 14:59:28 -070026
Jeongik Cha1a7ab642019-07-29 17:31:02 +090027using android::base::Join;
28using android::base::Split;
Daniel Norman85aed542019-08-21 12:01:14 -070029using android::base::StringPrintf;
Jeongik Chab5d962f2018-11-17 09:12:28 +090030using std::ostringstream;
31
Steven Moreland860b1942018-08-16 14:59:28 -070032namespace android {
33namespace aidl {
34namespace cpp {
35
Jeongik Cha1a7ab642019-07-29 17:31:02 +090036namespace {
37std::string RawParcelMethod(const AidlTypeSpecifier& raw_type, const AidlTypenames& typenames,
38 bool readMethod) {
39 static map<string, string> kBuiltin = {
40 {"byte", "Byte"},
41 {"boolean", "Bool"},
42 {"char", "Char"},
43 {"double", "Double"},
44 {"FileDescriptor", "UniqueFileDescriptor"},
45 {"float", "Float"},
46 {"IBinder", "StrongBinder"},
47 {"int", "Int32"},
48 {"long", "Int64"},
49 {"ParcelFileDescriptor", "Parcelable"},
50 {"String", "String16"},
51 };
52
53 static map<string, string> kBuiltinVector = {
54 {"FileDescriptor", "UniqueFileDescriptorVector"},
55 {"double", "DoubleVector"},
56 {"char", "CharVector"},
57 {"boolean", "BoolVector"},
58 {"byte", "ByteVector"},
59 {"float", "FloatVector"},
60 {"IBinder", "StrongBinderVector"},
61 {"String", "String16Vector"},
62 {"int", "Int32Vector"},
63 {"long", "Int64Vector"},
64 {"ParcelFileDescriptor", "ParcelableVector"},
65 };
66
67 const bool nullable = raw_type.IsNullable();
68 const bool isVector =
69 raw_type.IsArray() || (raw_type.IsGeneric() && raw_type.GetName() == "List");
70 const bool utf8 = raw_type.IsUtf8InCpp();
71 const auto& type = raw_type.IsGeneric() ? *raw_type.GetTypeParameters().at(0) : raw_type;
72 const string& aidl_name = type.GetName();
73
74 if (isVector) {
75 if (kBuiltinVector.find(aidl_name) != kBuiltinVector.end()) {
76 CHECK(AidlTypenames::IsBuiltinTypename(aidl_name));
77 if (utf8) {
78 CHECK(aidl_name == "String");
79 return readMethod ? "Utf8VectorFromUtf16Vector" : "Utf8VectorAsUtf16Vector";
80 }
81 return kBuiltinVector[aidl_name];
82 }
83 } else {
84 if (kBuiltin.find(aidl_name) != kBuiltin.end()) {
85 CHECK(AidlTypenames::IsBuiltinTypename(aidl_name));
86 if (aidl_name == "IBinder" && nullable && readMethod) {
87 return "NullableStrongBinder";
88 }
89 if (aidl_name == "ParcelFileDescriptor" && nullable && !readMethod) {
90 return "NullableParcelable";
91 }
92 if (utf8) {
93 CHECK(aidl_name == "String");
94 return readMethod ? "Utf8FromUtf16" : "Utf8AsUtf16";
95 }
96 return kBuiltin[aidl_name];
97 }
98 }
99 CHECK(!AidlTypenames::IsBuiltinTypename(aidl_name));
100 auto definedType = typenames.TryGetDefinedType(type.GetName());
101 if (definedType != nullptr && definedType->AsInterface() != nullptr) {
102 if (isVector) {
103 return "StrongBinderVector";
104 }
105 if (nullable && readMethod) {
106 return "NullableStrongBinder";
107 }
108 return "StrongBinder";
109 }
110
111 // The type must be either primitive or interface or parcelable,
112 // so it cannot be nullptr.
113 CHECK(definedType != nullptr) << type.GetName() << " is not found.";
114
115 // Parcelable
116 if (isVector) {
117 return "ParcelableVector";
118 }
119 if (nullable && !readMethod) {
120 return "NullableParcelable";
121 }
122 return "Parcelable";
123}
124
125std::string GetRawCppName(const AidlTypeSpecifier& type) {
126 return "::" + Join(type.GetSplitName(), "::");
127}
128
129std::string WrapIfNullable(const std::string type_str, const AidlTypeSpecifier& raw_type) {
130 const auto& type = raw_type.IsGeneric() ? (*raw_type.GetTypeParameters().at(0)) : raw_type;
131
132 if (raw_type.IsNullable() && !AidlTypenames::IsPrimitiveTypename(type.GetName()) &&
133 type.GetName() != "IBinder") {
134 return "::std::unique_ptr<" + type_str + ">";
135 }
136 return type_str;
137}
138
139std::string GetCppName(const AidlTypeSpecifier& raw_type, const AidlTypenames& typenames) {
140 // map from AIDL built-in type name to the corresponding Cpp type name
141 static map<string, string> m = {
142 {"boolean", "bool"},
143 {"byte", "int8_t"},
144 {"char", "char16_t"},
145 {"double", "double"},
146 {"FileDescriptor", "::android::base::unique_fd"},
147 {"float", "float"},
148 {"IBinder", "::android::sp<::android::IBinder>"},
149 {"int", "int32_t"},
150 {"long", "int64_t"},
151 {"ParcelFileDescriptor", "::android::os::ParcelFileDescriptor"},
152 {"String", "::android::String16"},
153 {"void", "void"},
154 };
155
156 CHECK(!raw_type.IsGeneric() ||
157 (raw_type.GetName() == "List" && raw_type.GetTypeParameters().size() == 1));
158 const auto& type = raw_type.IsGeneric() ? (*raw_type.GetTypeParameters().at(0)) : raw_type;
159 const string& aidl_name = type.GetName();
160 if (m.find(aidl_name) != m.end()) {
161 CHECK(AidlTypenames::IsBuiltinTypename(aidl_name));
162 if (aidl_name == "byte" && type.IsArray()) {
163 return "uint8_t";
164 } else if (raw_type.IsUtf8InCpp()) {
165 CHECK(aidl_name == "String");
166 return WrapIfNullable("::std::string", raw_type);
167 }
168 return WrapIfNullable(m[aidl_name], raw_type);
169 }
170 auto definedType = typenames.TryGetDefinedType(type.GetName());
171 if (definedType != nullptr && definedType->AsInterface() != nullptr) {
172 return "::android::sp<" + GetRawCppName(type) + ">";
173 }
174
175 return WrapIfNullable(GetRawCppName(type), raw_type);
176}
177} // namespace
Steven Moreland860b1942018-08-16 14:59:28 -0700178std::string ConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value) {
179 if (type.GetName() == "String" && !type.IsArray() && !type.IsUtf8InCpp()) {
180 return "::android::String16(" + raw_value + ")";
181 }
182
183 return raw_value;
184};
185
Jeongik Chab5d962f2018-11-17 09:12:28 +0900186std::string GetTransactionIdFor(const AidlMethod& method) {
187 ostringstream output;
188
Jeongik Chaf1470e22019-05-20 18:45:05 +0900189 output << "::android::IBinder::FIRST_CALL_TRANSACTION + ";
Jeongik Chab5d962f2018-11-17 09:12:28 +0900190 output << method.GetId() << " /* " << method.GetName() << " */";
191 return output.str();
192}
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900193
194std::string CppNameOf(const AidlTypeSpecifier& type, const AidlTypenames& typenames) {
195 if (type.IsArray() || type.IsGeneric()) {
196 std::string cpp_name = GetCppName(type, typenames);
197 if (type.IsNullable()) {
198 return "::std::unique_ptr<::std::vector<" + cpp_name + ">>";
199 }
200 return "::std::vector<" + cpp_name + ">";
201 }
202 return GetCppName(type, typenames);
203}
204
205std::string ParcelReadMethodOf(const AidlTypeSpecifier& type, const AidlTypenames& typenames) {
Daniel Norman85aed542019-08-21 12:01:14 -0700206 if (auto enum_decl = typenames.GetEnumDeclaration(type); enum_decl != nullptr) {
207 return ParcelReadMethodOf(enum_decl->GetBackingType(), typenames);
208 }
209
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900210 return "read" + RawParcelMethod(type, typenames, true /* readMethod */);
211}
212
Daniel Norman85aed542019-08-21 12:01:14 -0700213std::string ParcelReadCastOf(const AidlTypeSpecifier& type, const AidlTypenames& typenames,
214 const std::string& variable_name) {
215 if (auto enum_decl = typenames.GetEnumDeclaration(type); enum_decl != nullptr) {
216 return StringPrintf("(%s *) %s", CppNameOf(enum_decl->GetBackingType(), typenames).c_str(),
217 variable_name.c_str());
218 }
219
220 return variable_name;
221}
222
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900223std::string ParcelWriteMethodOf(const AidlTypeSpecifier& type, const AidlTypenames& typenames) {
Daniel Norman85aed542019-08-21 12:01:14 -0700224 if (auto enum_decl = typenames.GetEnumDeclaration(type); enum_decl != nullptr) {
225 return ParcelWriteMethodOf(enum_decl->GetBackingType(), typenames);
226 }
227
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900228 return "write" + RawParcelMethod(type, typenames, false /* readMethod */);
229}
230
Daniel Norman85aed542019-08-21 12:01:14 -0700231std::string ParcelWriteCastOf(const AidlTypeSpecifier& type, const AidlTypenames& typenames,
232 const std::string& variable_name) {
233 if (auto enum_decl = typenames.GetEnumDeclaration(type); enum_decl != nullptr) {
234 return StringPrintf("static_cast<%s>(%s)",
235 CppNameOf(enum_decl->GetBackingType(), typenames).c_str(),
236 variable_name.c_str());
237 }
238
239 if (typenames.GetInterface(type) != nullptr) {
240 return GetRawCppName(type) + "::asBinder(" + variable_name + ")";
241 }
242
243 return variable_name;
244}
245
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900246void AddHeaders(const AidlTypeSpecifier& raw_type, const AidlTypenames& typenames,
247 std::set<std::string>& headers) {
248 bool isVector = raw_type.IsArray() || raw_type.IsGeneric();
249 bool isNullable = raw_type.IsNullable();
250 bool utf8 = raw_type.IsUtf8InCpp();
251
252 CHECK(!raw_type.IsGeneric() ||
253 (raw_type.GetName() == "List" && raw_type.GetTypeParameters().size() == 1));
254 const auto& type = raw_type.IsGeneric() ? *raw_type.GetTypeParameters().at(0) : raw_type;
255 auto definedType = typenames.TryGetDefinedType(type.GetName());
256
257 if (isVector) {
258 headers.insert("vector");
259 }
260 if (isNullable) {
261 if (type.GetName() != "IBinder") {
262 headers.insert("memory");
263 }
264 }
265 if (type.GetName() == "String") {
266 headers.insert(utf8 ? "string" : "utils/String16.h");
267 }
268 if (type.GetName() == "IBinder") {
269 headers.insert("binder/IBinder.h");
270 }
271 if (type.GetName() == "FileDescriptor") {
272 headers.insert("android-base/unique_fd.h");
273 }
274 if (type.GetName() == "ParcelFileDescriptor") {
275 headers.insert("binder/ParcelFileDescriptor.h");
276 }
277
278 static const std::set<string> need_cstdint{"byte", "int", "long"};
279 if (need_cstdint.find(type.GetName()) != need_cstdint.end()) {
280 headers.insert("cstdint");
281 }
282
283 if (definedType == nullptr) {
284 return;
285 }
Daniel Norman85aed542019-08-21 12:01:14 -0700286 if (definedType->AsInterface() != nullptr || definedType->AsStructuredParcelable() != nullptr ||
287 definedType->AsEnumDeclaration() != nullptr) {
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900288 AddHeaders(*definedType, headers);
289 } else if (definedType->AsParcelable() != nullptr) {
290 const std::string cpp_header = definedType->AsParcelable()->GetCppHeader();
291 AIDL_FATAL_IF(cpp_header.empty(), definedType->AsParcelable())
292 << "Parcelable " << definedType->AsParcelable()->GetCanonicalName()
293 << " has no C++ header defined.";
294 headers.insert(cpp_header);
295 }
296}
297
298void AddHeaders(const AidlDefinedType& definedType, std::set<std::string>& headers) {
299 vector<string> name = definedType.GetSplitPackage();
300 name.push_back(definedType.GetName());
301 const std::string cpp_header = Join(name, '/') + ".h";
302 headers.insert(cpp_header);
303}
304
Steven Moreland860b1942018-08-16 14:59:28 -0700305} // namespace cpp
306} // namespace aidl
307} // namespace android