blob: 4bf1c1fabf430ecdb9ea05128496b8621ba8c210 [file] [log] [blame]
Steven Morelande8a3a192018-09-20 14:14: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 * limitations under the License.
13 */
14
15#include "aidl_to_ndk.h"
16#include "aidl_language.h"
Steven Morelandaada3422018-09-20 15:55:33 -070017#include "aidl_to_cpp_common.h"
Steven Morelande8a3a192018-09-20 14:14:28 -070018#include "logging.h"
Steven Moreland7c933372018-10-11 15:20:04 -070019#include "os.h"
Steven Morelande8a3a192018-09-20 14:14:28 -070020
Devin Moore53fc99c2020-08-12 08:07:52 -070021#include <android-base/stringprintf.h>
Steven Morelande8a3a192018-09-20 14:14:28 -070022#include <android-base/strings.h>
23
24#include <functional>
25
26using ::android::base::Join;
Jooyung Han4b832522021-10-06 16:08:30 +090027using ::android::base::Split;
Steven Morelande8a3a192018-09-20 14:14:28 -070028
29namespace android {
30namespace aidl {
31namespace ndk {
32
Steven Moreland7c933372018-10-11 15:20:04 -070033std::string NdkHeaderFile(const AidlDefinedType& defined_type, cpp::ClassNames name,
34 bool use_os_sep) {
35 char seperator = (use_os_sep) ? OS_PATH_SEPARATOR : '/';
36 return std::string("aidl") + seperator + cpp::HeaderFile(defined_type, name, use_os_sep);
37}
38
Steven Moreland055d8792018-11-14 12:48:42 -080039// This represents a type in AIDL (e.g. 'String' which can be referenced in multiple ways)
Steven Morelande8a3a192018-09-20 14:14:28 -070040struct TypeInfo {
Steven Moreland055d8792018-11-14 12:48:42 -080041 struct Aspect {
42 // name of the type in C++ output
43 std::string cpp_name;
44 // whether to prefer 'value type' over 'const&'
45 bool value_is_cheap;
Steven Morelande8a3a192018-09-20 14:14:28 -070046
Steven Moreland055d8792018-11-14 12:48:42 -080047 std::function<void(const CodeGeneratorContext& c)> read_func;
48 std::function<void(const CodeGeneratorContext& c)> write_func;
49 };
Steven Morelandeb38ee72018-10-15 14:20:04 -070050
Steven Moreland055d8792018-11-14 12:48:42 -080051 // e.g. 'String'
52 Aspect raw;
53
54 // e.g. 'String[]'
55 std::shared_ptr<Aspect> array;
56
57 // note: Nullable types do not exist in Java. For most Java types, the type is split into a
58 // nullable and non-nullable variant. This is because C++ types are more usually non-nullable, but
59 // everything in Java is non-nullable. This does mean that some Java interfaces may have to have
60 // '@nullable' added to them in order to function as expected w/ the NDK. It also means that some
61 // transactions will be allowed in Java which are not allowed in C++. However, in Java, if a null
62 // is ignored, it will just result in a NullPointerException and be delivered to the other side.
63 // C++ does not have this same capacity (in Android), and so instead, we distinguish nullability
64 // in the type system.
65
66 // e.g. '@nullable String'
67 std::shared_ptr<Aspect> nullable;
68
69 // e.g. '@nullable String[]'
70 std::shared_ptr<Aspect> nullable_array;
Steven Morelande8a3a192018-09-20 14:14:28 -070071};
72
Daniel Norman37d43dd2019-09-09 17:22:34 -070073std::string ConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value) {
Jooyung Han690f5842020-12-04 13:02:04 +090074 if (type.IsArray()) {
75 return raw_value;
76 }
77
Will McVickerd7d18df2019-09-12 13:40:50 -070078 if (type.GetName() == "long" && !type.IsArray()) {
Daniel Norman37d43dd2019-09-09 17:22:34 -070079 return raw_value + "L";
80 }
81
Jooyung Han690f5842020-12-04 13:02:04 +090082 if (auto defined_type = type.GetDefinedType(); defined_type) {
83 auto enum_type = defined_type->AsEnumDeclaration();
84 AIDL_FATAL_IF(!enum_type, type) << "Invalid type for \"" << raw_value << "\"";
85 return NdkFullClassName(*enum_type, cpp::ClassNames::RAW) +
86 "::" + raw_value.substr(raw_value.find_last_of('.') + 1);
87 }
88
Daniel Norman37d43dd2019-09-09 17:22:34 -070089 return raw_value;
90};
91
Steven Moreland67caf422018-10-15 12:39:12 -070092static std::function<void(const CodeGeneratorContext& c)> StandardRead(const std::string& name) {
Steven Morelande8a3a192018-09-20 14:14:28 -070093 return [name](const CodeGeneratorContext& c) {
Steven Moreland67caf422018-10-15 12:39:12 -070094 c.writer << name << "(" << c.parcel << ", " << c.var << ")";
Steven Morelande8a3a192018-09-20 14:14:28 -070095 };
96}
Steven Moreland67caf422018-10-15 12:39:12 -070097static std::function<void(const CodeGeneratorContext& c)> StandardWrite(const std::string& name) {
Steven Morelande8a3a192018-09-20 14:14:28 -070098 return [name](const CodeGeneratorContext& c) {
Steven Moreland67caf422018-10-15 12:39:12 -070099 c.writer << name << "(" << c.parcel << ", " << c.var << ")";
100 };
101}
102
Jooyung Han241fdda2020-02-21 21:17:44 +0900103TypeInfo PrimitiveType(const std::string& cpp_name, const std::string& pretty_name,
104 const std::optional<std::string>& cpp_name_for_array_opt = std::nullopt) {
105 std::string cpp_name_for_array = cpp_name_for_array_opt.value_or(cpp_name);
Steven Moreland67caf422018-10-15 12:39:12 -0700106 return TypeInfo{
Steven Moreland055d8792018-11-14 12:48:42 -0800107 .raw =
108 TypeInfo::Aspect{
109 .cpp_name = cpp_name,
110 .value_is_cheap = true,
111 .read_func = StandardRead("AParcel_read" + pretty_name),
112 .write_func = StandardWrite("AParcel_write" + pretty_name),
113 },
114 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
Jooyung Han241fdda2020-02-21 21:17:44 +0900115 .cpp_name = "std::vector<" + cpp_name_for_array + ">",
Steven Moreland055d8792018-11-14 12:48:42 -0800116 .value_is_cheap = false,
117 .read_func = StandardRead("::ndk::AParcel_readVector"),
118 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
119 }),
120 .nullable = nullptr,
Steven Morelandc10ed462018-11-15 14:50:28 -0800121 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
Jooyung Han241fdda2020-02-21 21:17:44 +0900122 .cpp_name = "std::optional<std::vector<" + cpp_name_for_array + ">>",
Steven Morelandc10ed462018-11-15 14:50:28 -0800123 .value_is_cheap = false,
124 .read_func = StandardRead("::ndk::AParcel_readVector"),
125 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
126 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800127 };
128}
129
130TypeInfo InterfaceTypeInfo(const AidlInterface& type) {
131 const std::string clazz = NdkFullClassName(type, cpp::ClassNames::INTERFACE);
132
133 return TypeInfo{
134 .raw =
135 TypeInfo::Aspect{
136 .cpp_name = "std::shared_ptr<" + clazz + ">",
137 .value_is_cheap = false,
Steven Moreland055d8792018-11-14 12:48:42 -0800138 .read_func = StandardRead(clazz + "::readFromParcel"),
139 .write_func = StandardWrite(clazz + "::writeToParcel"),
140 },
141 .array = nullptr,
142 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
143 .cpp_name = "std::shared_ptr<" + clazz + ">",
144 .value_is_cheap = false,
145 .read_func = StandardRead(clazz + "::readFromParcel"),
146 .write_func = StandardWrite(clazz + "::writeToParcel"),
147 }),
148 .nullable_array = nullptr,
149 };
150}
151
Devin Moore53fc99c2020-08-12 08:07:52 -0700152TypeInfo ParcelableTypeInfo(const AidlParcelable& type, const AidlTypeSpecifier& typeSpec,
153 const AidlTypenames& types) {
154 std::string clazz = NdkFullClassName(type, cpp::ClassNames::RAW);
155 std::string template_params = "";
156 if (typeSpec.IsGeneric()) {
157 std::vector<std::string> type_params;
158 for (const auto& parameter : typeSpec.GetTypeParameters()) {
159 type_params.push_back(NdkNameOf(types, *parameter, StorageMode::STACK));
160 }
161 clazz += base::StringPrintf("<%s>", base::Join(type_params, ", ").c_str());
162 }
Jooyung Han01720ed2021-08-13 07:46:07 +0900163 const std::string nullable = typeSpec.IsHeapNullable() ? "std::unique_ptr" : "std::optional";
Steven Moreland055d8792018-11-14 12:48:42 -0800164 return TypeInfo{
165 .raw =
166 TypeInfo::Aspect{
167 .cpp_name = clazz,
168 .value_is_cheap = false,
Steven Moreland4348f9a2019-12-16 16:33:01 -0800169 .read_func = StandardRead("::ndk::AParcel_readParcelable"),
170 .write_func = StandardWrite("::ndk::AParcel_writeParcelable"),
Steven Moreland055d8792018-11-14 12:48:42 -0800171 },
Steven Moreland5ecec6b2018-12-11 18:56:41 -0800172 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
173 .cpp_name = "std::vector<" + clazz + ">",
174 .value_is_cheap = false,
175 .read_func = StandardRead("::ndk::AParcel_readVector"),
176 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
177 }),
Steven Moreland18f75262019-12-19 16:58:55 -0800178 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
Jooyung Han01720ed2021-08-13 07:46:07 +0900179 .cpp_name = nullable + "<" + clazz + ">",
Steven Moreland18f75262019-12-19 16:58:55 -0800180 .value_is_cheap = false,
181 .read_func = StandardRead("::ndk::AParcel_readNullableParcelable"),
182 .write_func = StandardWrite("::ndk::AParcel_writeNullableParcelable"),
183 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800184 .nullable_array = nullptr,
Steven Morelande8a3a192018-09-20 14:14:28 -0700185 };
186}
187
Daniel Norman37d43dd2019-09-09 17:22:34 -0700188TypeInfo EnumDeclarationTypeInfo(const AidlEnumDeclaration& enum_decl) {
Steven Morelandb8df37d2019-11-21 12:33:24 -0800189 const std::string clazz = NdkFullClassName(enum_decl, cpp::ClassNames::RAW);
Daniel Norman37d43dd2019-09-09 17:22:34 -0700190
191 static map<std::string, std::string> kAParcelTypeNameMap = {
192 {"byte", "Byte"},
193 {"int", "Int32"},
194 {"long", "Int64"},
195 };
196 auto aparcel_name_it = kAParcelTypeNameMap.find(enum_decl.GetBackingType().GetName());
Steven Moreland21780812020-09-11 01:29:45 +0000197 AIDL_FATAL_IF(aparcel_name_it == kAParcelTypeNameMap.end(), enum_decl);
Daniel Norman37d43dd2019-09-09 17:22:34 -0700198 const std::string aparcel_name = aparcel_name_it->second;
199
200 const std::string backing_type_name =
201 NdkNameOf(AidlTypenames(), enum_decl.GetBackingType(), StorageMode::STACK);
202
203 return TypeInfo{
204 .raw = TypeInfo::Aspect{
205 .cpp_name = clazz,
206 .value_is_cheap = true,
207 .read_func =
208 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
209 c.writer << "AParcel_read" << aparcel_name << "(" << c.parcel
210 << ", reinterpret_cast<" << backing_type_name << "*>(" << c.var << "))";
211 },
212 .write_func =
213 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
214 c.writer << "AParcel_write" << aparcel_name << "(" << c.parcel << ", static_cast<"
215 << backing_type_name << ">(" << c.var << "))";
216 },
217 },
Daniel Normanee8674f2019-09-20 16:07:00 -0700218 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
219 .cpp_name = "std::vector<" + clazz + ">",
220 .value_is_cheap = false,
221 .read_func =
222 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
223 c.writer << "AParcel_read" << aparcel_name << "Array(" << c.parcel
224 << ", static_cast<void*>(" << c.var
225 << "), ndk::AParcel_stdVectorAllocator<" << backing_type_name << ">)";
226 },
227 .write_func =
228 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
229 c.writer << "AParcel_write" << aparcel_name << "Array(" << c.parcel
230 << ", reinterpret_cast<const " << backing_type_name << "*>(" << c.var
231 << ".data()), " << c.var << ".size())";
232 },
233 }),
Daniel Norman37d43dd2019-09-09 17:22:34 -0700234 .nullable = nullptr,
Daniel Normanee8674f2019-09-20 16:07:00 -0700235 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
236 .cpp_name = "std::optional<std::vector<" + clazz + ">>",
237 .value_is_cheap = false,
238 .read_func =
239 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
240 c.writer << "AParcel_read" << aparcel_name << "Array(" << c.parcel
241 << ", static_cast<void*>(" << c.var
242 << "), ndk::AParcel_nullableStdVectorAllocator<" << backing_type_name
243 << ">)";
244 },
245 .write_func =
246 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
247 // If the var exists, use writeArray with data() and size().
248 // Otherwise, use nullptr and -1.
249 c.writer << "AParcel_write" << aparcel_name << "Array(" << c.parcel << ", ("
250 << c.var << " ? reinterpret_cast<const " << backing_type_name << "*>("
251 << c.var << "->data()) : nullptr)"
252 << ", (" << c.var << " ? " << c.var << "->size() : -1))";
253 },
254 }),
Daniel Norman37d43dd2019-09-09 17:22:34 -0700255 };
256}
257
258// map from AIDL built-in type name to the corresponding Ndk type info
Steven Morelande8a3a192018-09-20 14:14:28 -0700259static map<std::string, TypeInfo> kNdkTypeInfoMap = {
Steven Moreland055d8792018-11-14 12:48:42 -0800260 {"void", TypeInfo{{"void", true, nullptr, nullptr}, nullptr, nullptr, nullptr}},
Steven Moreland67caf422018-10-15 12:39:12 -0700261 {"boolean", PrimitiveType("bool", "Bool")},
Jooyung Han241fdda2020-02-21 21:17:44 +0900262 {"byte", PrimitiveType("int8_t", "Byte", "uint8_t")},
Steven Moreland67caf422018-10-15 12:39:12 -0700263 {"char", PrimitiveType("char16_t", "Char")},
264 {"int", PrimitiveType("int32_t", "Int32")},
265 {"long", PrimitiveType("int64_t", "Int64")},
266 {"float", PrimitiveType("float", "Float")},
267 {"double", PrimitiveType("double", "Double")},
Steven Moreland63404532018-10-08 14:31:00 -0700268 {"String",
Steven Moreland055d8792018-11-14 12:48:42 -0800269 TypeInfo{
270 .raw =
271 TypeInfo::Aspect{
272 .cpp_name = "std::string",
273 .value_is_cheap = false,
274 .read_func = StandardRead("::ndk::AParcel_readString"),
275 .write_func = StandardWrite("::ndk::AParcel_writeString"),
276 },
277 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
278 .cpp_name = "std::vector<std::string>",
279 .value_is_cheap = false,
280 .read_func = StandardRead("::ndk::AParcel_readVector"),
281 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
282 }),
Steven Moreland18ed9782018-11-14 17:08:09 -0800283 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
284 .cpp_name = "std::optional<std::string>",
285 .value_is_cheap = false,
286 .read_func = StandardRead("::ndk::AParcel_readString"),
287 .write_func = StandardWrite("::ndk::AParcel_writeString"),
288 }),
289 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
290 .cpp_name = "std::optional<std::vector<std::optional<std::string>>>",
291 .value_is_cheap = false,
292 .read_func = StandardRead("::ndk::AParcel_readVector"),
293 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
294 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800295 }},
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900296 // TODO(b/136048684) {"Map", ""},
Steven Moreland055d8792018-11-14 12:48:42 -0800297 {"IBinder",
298 TypeInfo{
299 .raw =
300 TypeInfo::Aspect{
301 .cpp_name = "::ndk::SpAIBinder",
302 .value_is_cheap = false,
Steven Morelande1048a32018-11-16 12:52:17 -0800303 .read_func = StandardRead("::ndk::AParcel_readRequiredStrongBinder"),
304 .write_func = StandardRead("::ndk::AParcel_writeRequiredStrongBinder"),
Steven Moreland055d8792018-11-14 12:48:42 -0800305 },
Jooyung Hanbc7ab902021-10-14 07:09:49 +0900306 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
307 .cpp_name = "std::vector<::ndk::SpAIBinder>",
308 .value_is_cheap = false,
309 .read_func = StandardRead("::ndk::AParcel_readVector"),
310 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
311 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800312 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
313 .cpp_name = "::ndk::SpAIBinder",
314 .value_is_cheap = false,
Steven Morelande1048a32018-11-16 12:52:17 -0800315 .read_func = StandardRead("::ndk::AParcel_readNullableStrongBinder"),
316 .write_func = StandardRead("::ndk::AParcel_writeNullableStrongBinder"),
Steven Moreland055d8792018-11-14 12:48:42 -0800317 }),
Jooyung Hanbc7ab902021-10-14 07:09:49 +0900318 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
319 .cpp_name = "std::optional<std::vector<::ndk::SpAIBinder>>",
320 .value_is_cheap = false,
321 .read_func = StandardRead("::ndk::AParcel_readVector"),
322 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
323 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800324 }},
Steven Moreland055d8792018-11-14 12:48:42 -0800325 {"ParcelFileDescriptor",
326 TypeInfo{
327 .raw =
328 TypeInfo::Aspect{
329 .cpp_name = "::ndk::ScopedFileDescriptor",
330 .value_is_cheap = false,
Steven Moreland962c60d2018-11-16 15:19:27 -0800331 .read_func = StandardRead("::ndk::AParcel_readRequiredParcelFileDescriptor"),
332 .write_func = StandardRead("::ndk::AParcel_writeRequiredParcelFileDescriptor"),
Steven Moreland055d8792018-11-14 12:48:42 -0800333 },
Jeongik Chacc8dd1d2019-11-14 10:22:57 +0900334 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
335 .cpp_name = "std::vector<::ndk::ScopedFileDescriptor>",
336 .value_is_cheap = false,
337 .read_func = StandardRead("::ndk::AParcel_readVector"),
338 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
339 }),
Steven Moreland962c60d2018-11-16 15:19:27 -0800340 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
341 .cpp_name = "::ndk::ScopedFileDescriptor",
342 .value_is_cheap = false,
343 .read_func = StandardRead("::ndk::AParcel_readNullableParcelFileDescriptor"),
344 .write_func = StandardRead("::ndk::AParcel_writeNullableParcelFileDescriptor"),
345 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800346 .nullable_array = nullptr,
347 }},
Jeongik Cha8f02a532020-10-14 00:16:28 +0900348 {"ParcelableHolder",
349 TypeInfo{
350 .raw =
351 TypeInfo::Aspect{
352 .cpp_name = "::ndk::AParcelableHolder",
353 .value_is_cheap = false,
354 .read_func = StandardRead("::ndk::AParcel_readParcelable"),
355 .write_func = StandardWrite("::ndk::AParcel_writeParcelable"),
356 },
357 .array = nullptr,
358 .nullable = nullptr,
359 .nullable_array = nullptr,
360 }},
Steven Morelande8a3a192018-09-20 14:14:28 -0700361};
362
Jooyung Han5014b3e2021-10-15 09:58:20 +0900363static TypeInfo GetTypeInfo(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
364 auto& aidl_name = aidl.GetName();
365
366 if (AidlTypenames::IsBuiltinTypename(aidl_name)) {
367 auto it = kNdkTypeInfoMap.find(aidl_name);
368 AIDL_FATAL_IF(it == kNdkTypeInfoMap.end(), aidl_name);
369 return it->second;
370 }
371 const AidlDefinedType* type = types.TryGetDefinedType(aidl_name);
372 AIDL_FATAL_IF(type == nullptr, aidl_name) << "Unrecognized type.";
373
374 if (const AidlInterface* intf = type->AsInterface(); intf != nullptr) {
375 return InterfaceTypeInfo(*intf);
376 } else if (const AidlParcelable* parcelable = type->AsParcelable(); parcelable != nullptr) {
377 return ParcelableTypeInfo(*parcelable, aidl, types);
378 } else if (const AidlEnumDeclaration* enum_decl = type->AsEnumDeclaration();
379 enum_decl != nullptr) {
380 return EnumDeclarationTypeInfo(*enum_decl);
381 } else {
382 AIDL_FATAL(aidl_name) << "Unrecognized type";
383 }
384}
385
Steven Moreland055d8792018-11-14 12:48:42 -0800386static TypeInfo::Aspect GetTypeAspect(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
Steven Moreland21780812020-09-11 01:29:45 +0000387 AIDL_FATAL_IF(!aidl.IsResolved(), aidl) << aidl.ToString();
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900388 auto& aidl_name = aidl.GetName();
Steven Moreland1cb099e2018-10-17 16:31:08 -0700389
Steven Moreland67caf422018-10-15 12:39:12 -0700390 TypeInfo info;
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900391
392 // TODO(b/136048684): For now, List<T> is converted to T[].(Both are using vector<T>)
393 if (aidl_name == "List") {
Steven Morelandfe52c9f2019-11-27 19:04:48 -0800394 AIDL_FATAL_IF(!aidl.IsGeneric(), aidl) << "List must be generic type.";
395 AIDL_FATAL_IF(aidl.GetTypeParameters().size() != 1, aidl)
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900396 << "List can accept only one type parameter.";
Jooyung Han5014b3e2021-10-15 09:58:20 +0900397 const auto& type_param = *aidl.GetTypeParameters()[0];
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900398 // TODO(b/136048684) AIDL doesn't support nested type parameter yet.
Jooyung Han5014b3e2021-10-15 09:58:20 +0900399 AIDL_FATAL_IF(type_param.IsGeneric(), aidl) << "AIDL doesn't support nested type parameter";
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900400
Jooyung Han5014b3e2021-10-15 09:58:20 +0900401 info = GetTypeInfo(types, type_param);
Steven Moreland67caf422018-10-15 12:39:12 -0700402 } else {
Jooyung Han5014b3e2021-10-15 09:58:20 +0900403 info = GetTypeInfo(types, aidl);
Steven Moreland67caf422018-10-15 12:39:12 -0700404 }
405
Jooyung Han5014b3e2021-10-15 09:58:20 +0900406 if (aidl.IsArray() || aidl_name == "List") {
Steven Moreland055d8792018-11-14 12:48:42 -0800407 if (aidl.IsNullable()) {
Steven Morelandd59e3172020-05-11 16:42:09 -0700408 AIDL_FATAL_IF(info.nullable_array == nullptr, aidl)
409 << "Unsupported type in NDK Backend: " << aidl.ToString();
Steven Moreland055d8792018-11-14 12:48:42 -0800410 return *info.nullable_array;
411 }
Steven Morelandd59e3172020-05-11 16:42:09 -0700412 AIDL_FATAL_IF(info.array == nullptr, aidl)
413 << "Unsupported type in NDK Backend: " << aidl.ToString();
Steven Moreland055d8792018-11-14 12:48:42 -0800414 return *info.array;
415 }
Steven Moreland1cb099e2018-10-17 16:31:08 -0700416
Steven Moreland055d8792018-11-14 12:48:42 -0800417 if (aidl.IsNullable()) {
Steven Morelandd59e3172020-05-11 16:42:09 -0700418 AIDL_FATAL_IF(info.nullable == nullptr, aidl)
419 << "Unsupported type in NDK Backend: " << aidl.ToString();
Steven Moreland055d8792018-11-14 12:48:42 -0800420 return *info.nullable;
421 }
422
423 return info.raw;
Steven Moreland67caf422018-10-15 12:39:12 -0700424}
425
Steven Moreland2bea13b2018-10-03 15:12:33 -0700426std::string NdkFullClassName(const AidlDefinedType& type, cpp::ClassNames name) {
Steven Moreland63404532018-10-08 14:31:00 -0700427 std::vector<std::string> pieces = {"::aidl"};
Jooyung Han4b832522021-10-06 16:08:30 +0900428 std::vector<std::string> split_name = Split(type.GetCanonicalName(), ".");
429 pieces.insert(pieces.end(), split_name.begin(), split_name.end());
430 // Override name part with cpp::ClassName(type, name)
431 pieces.back() = cpp::ClassName(type, name);
Steven Moreland2bea13b2018-10-03 15:12:33 -0700432 return Join(pieces, "::");
433}
434
435std::string NdkNameOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl, StorageMode mode) {
Steven Moreland055d8792018-11-14 12:48:42 -0800436 TypeInfo::Aspect aspect = GetTypeAspect(types, aidl);
Steven Morelandeb38ee72018-10-15 14:20:04 -0700437
Steven Morelande8a3a192018-09-20 14:14:28 -0700438 switch (mode) {
439 case StorageMode::STACK:
Steven Moreland055d8792018-11-14 12:48:42 -0800440 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700441 case StorageMode::ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800442 if (aspect.value_is_cheap) {
443 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700444 } else {
Steven Moreland055d8792018-11-14 12:48:42 -0800445 return "const " + aspect.cpp_name + "&";
Steven Morelande8a3a192018-09-20 14:14:28 -0700446 }
447 case StorageMode::OUT_ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800448 return aspect.cpp_name + "*";
Steven Morelande8a3a192018-09-20 14:14:28 -0700449 default:
450 AIDL_FATAL(aidl.GetName()) << "Unrecognized mode type: " << static_cast<int>(mode);
451 }
452}
453
Devin Moore83e76982020-09-30 09:32:35 -0700454size_t NdkAlignmentOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
455 // map from NDK type name to the corresponding alignment size
456 static map<string, int> alignment = {
457 {"bool", 1}, {"int8_t", 1}, {"char16_t", 2}, {"double", 8},
458 {"float", 4}, {"int32_t", 4}, {"int64_t", 8},
459 };
460
461 const string& name = NdkNameOf(types, aidl, StorageMode::STACK);
462 if (alignment.find(name) != alignment.end()) {
463 return alignment[name];
464 } else {
465 const auto& definedType = types.TryGetDefinedType(aidl.GetName());
466 AIDL_FATAL_IF(definedType == nullptr, aidl) << "Failed to resolve type.";
467 if (const auto& enumType = definedType->AsEnumDeclaration(); enumType != nullptr) {
468 return NdkAlignmentOf(types, enumType->GetBackingType());
469 }
470 }
471 return 0;
472}
473
Steven Morelande8a3a192018-09-20 14:14:28 -0700474void WriteToParcelFor(const CodeGeneratorContext& c) {
Steven Moreland055d8792018-11-14 12:48:42 -0800475 TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
476 aspect.write_func(c);
Steven Morelande8a3a192018-09-20 14:14:28 -0700477}
478
479void ReadFromParcelFor(const CodeGeneratorContext& c) {
Steven Moreland055d8792018-11-14 12:48:42 -0800480 TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
481 aspect.read_func(c);
Steven Morelande8a3a192018-09-20 14:14:28 -0700482}
483
Jiyong Park965c5b92018-11-21 13:37:15 +0900484std::string NdkArgList(
485 const AidlTypenames& types, const AidlMethod& method,
486 std::function<std::string(const std::string& type, const std::string& name, bool isOut)>
487 formatter) {
Steven Morelandaada3422018-09-20 15:55:33 -0700488 std::vector<std::string> method_arguments;
489 for (const auto& a : method.GetArguments()) {
490 StorageMode mode = a->IsOut() ? StorageMode::OUT_ARGUMENT : StorageMode::ARGUMENT;
Steven Moreland2bea13b2018-10-03 15:12:33 -0700491 std::string type = NdkNameOf(types, a->GetType(), mode);
Steven Morelandaada3422018-09-20 15:55:33 -0700492 std::string name = cpp::BuildVarName(*a);
Jiyong Park965c5b92018-11-21 13:37:15 +0900493 method_arguments.emplace_back(formatter(type, name, a->IsOut()));
Steven Morelandaada3422018-09-20 15:55:33 -0700494 }
495
496 if (method.GetType().GetName() != "void") {
Jiyong Park965c5b92018-11-21 13:37:15 +0900497 std::string type = NdkNameOf(types, method.GetType(), StorageMode::OUT_ARGUMENT);
498 std::string name = "_aidl_return";
499 method_arguments.emplace_back(formatter(type, name, true));
Steven Morelandaada3422018-09-20 15:55:33 -0700500 }
501
502 return Join(method_arguments, ", ");
503}
504
Steven Moreland2bea13b2018-10-03 15:12:33 -0700505std::string NdkMethodDecl(const AidlTypenames& types, const AidlMethod& method,
506 const std::string& clazz) {
Steven Morelandaada3422018-09-20 15:55:33 -0700507 std::string class_prefix = clazz.empty() ? "" : (clazz + "::");
Steven Moreland63404532018-10-08 14:31:00 -0700508 return "::ndk::ScopedAStatus " + class_prefix + method.GetName() + "(" +
Jiyong Park965c5b92018-11-21 13:37:15 +0900509 NdkArgList(types, method, FormatArgForDecl) + ")";
Steven Morelandaada3422018-09-20 15:55:33 -0700510}
511
Steven Morelande8a3a192018-09-20 14:14:28 -0700512} // namespace ndk
513} // namespace aidl
514} // namespace android