blob: c9ad112008ac9dc9c60ba4dfacd1e03b2c681a68 [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 Han981fc592021-11-06 20:24:45 +090074 return cpp::CppConstantValueDecorator(type, raw_value, /*is_ndk=*/true);
Daniel Norman37d43dd2019-09-09 17:22:34 -070075};
76
Steven Moreland67caf422018-10-15 12:39:12 -070077static std::function<void(const CodeGeneratorContext& c)> StandardRead(const std::string& name) {
Steven Morelande8a3a192018-09-20 14:14:28 -070078 return [name](const CodeGeneratorContext& c) {
Steven Moreland67caf422018-10-15 12:39:12 -070079 c.writer << name << "(" << c.parcel << ", " << c.var << ")";
Steven Morelande8a3a192018-09-20 14:14:28 -070080 };
81}
Steven Moreland67caf422018-10-15 12:39:12 -070082static std::function<void(const CodeGeneratorContext& c)> StandardWrite(const std::string& name) {
Steven Morelande8a3a192018-09-20 14:14:28 -070083 return [name](const CodeGeneratorContext& c) {
Steven Moreland67caf422018-10-15 12:39:12 -070084 c.writer << name << "(" << c.parcel << ", " << c.var << ")";
85 };
86}
87
Jooyung Han241fdda2020-02-21 21:17:44 +090088TypeInfo PrimitiveType(const std::string& cpp_name, const std::string& pretty_name,
89 const std::optional<std::string>& cpp_name_for_array_opt = std::nullopt) {
90 std::string cpp_name_for_array = cpp_name_for_array_opt.value_or(cpp_name);
Steven Moreland67caf422018-10-15 12:39:12 -070091 return TypeInfo{
Steven Moreland055d8792018-11-14 12:48:42 -080092 .raw =
93 TypeInfo::Aspect{
94 .cpp_name = cpp_name,
95 .value_is_cheap = true,
96 .read_func = StandardRead("AParcel_read" + pretty_name),
97 .write_func = StandardWrite("AParcel_write" + pretty_name),
98 },
99 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
Jooyung Han241fdda2020-02-21 21:17:44 +0900100 .cpp_name = "std::vector<" + cpp_name_for_array + ">",
Steven Moreland055d8792018-11-14 12:48:42 -0800101 .value_is_cheap = false,
102 .read_func = StandardRead("::ndk::AParcel_readVector"),
103 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
104 }),
105 .nullable = nullptr,
Steven Morelandc10ed462018-11-15 14:50:28 -0800106 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
Jooyung Han241fdda2020-02-21 21:17:44 +0900107 .cpp_name = "std::optional<std::vector<" + cpp_name_for_array + ">>",
Steven Morelandc10ed462018-11-15 14:50:28 -0800108 .value_is_cheap = false,
109 .read_func = StandardRead("::ndk::AParcel_readVector"),
110 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
111 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800112 };
113}
114
115TypeInfo InterfaceTypeInfo(const AidlInterface& type) {
116 const std::string clazz = NdkFullClassName(type, cpp::ClassNames::INTERFACE);
117
118 return TypeInfo{
119 .raw =
120 TypeInfo::Aspect{
121 .cpp_name = "std::shared_ptr<" + clazz + ">",
122 .value_is_cheap = false,
Steven Moreland055d8792018-11-14 12:48:42 -0800123 .read_func = StandardRead(clazz + "::readFromParcel"),
124 .write_func = StandardWrite(clazz + "::writeToParcel"),
125 },
Jooyung Han1d3ec712021-11-02 04:41:36 +0900126 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
127 .cpp_name = "std::vector<std::shared_ptr<" + clazz + ">>",
128 .value_is_cheap = false,
129 .read_func = StandardRead("::ndk::AParcel_readVector"),
130 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
131 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800132 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
133 .cpp_name = "std::shared_ptr<" + clazz + ">",
134 .value_is_cheap = false,
135 .read_func = StandardRead(clazz + "::readFromParcel"),
136 .write_func = StandardWrite(clazz + "::writeToParcel"),
137 }),
Jooyung Han1d3ec712021-11-02 04:41:36 +0900138 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
139 .cpp_name = "std::optional<std::vector<std::shared_ptr<" + clazz + ">>>",
140 .value_is_cheap = false,
141 .read_func = StandardRead("::ndk::AParcel_readVector"),
142 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
143 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800144 };
145}
146
Devin Moore53fc99c2020-08-12 08:07:52 -0700147TypeInfo ParcelableTypeInfo(const AidlParcelable& type, const AidlTypeSpecifier& typeSpec,
148 const AidlTypenames& types) {
149 std::string clazz = NdkFullClassName(type, cpp::ClassNames::RAW);
150 std::string template_params = "";
151 if (typeSpec.IsGeneric()) {
152 std::vector<std::string> type_params;
153 for (const auto& parameter : typeSpec.GetTypeParameters()) {
154 type_params.push_back(NdkNameOf(types, *parameter, StorageMode::STACK));
155 }
156 clazz += base::StringPrintf("<%s>", base::Join(type_params, ", ").c_str());
157 }
Jooyung Han01720ed2021-08-13 07:46:07 +0900158 const std::string nullable = typeSpec.IsHeapNullable() ? "std::unique_ptr" : "std::optional";
Steven Moreland055d8792018-11-14 12:48:42 -0800159 return TypeInfo{
160 .raw =
161 TypeInfo::Aspect{
162 .cpp_name = clazz,
163 .value_is_cheap = false,
Steven Moreland4348f9a2019-12-16 16:33:01 -0800164 .read_func = StandardRead("::ndk::AParcel_readParcelable"),
165 .write_func = StandardWrite("::ndk::AParcel_writeParcelable"),
Steven Moreland055d8792018-11-14 12:48:42 -0800166 },
Steven Moreland5ecec6b2018-12-11 18:56:41 -0800167 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
168 .cpp_name = "std::vector<" + clazz + ">",
169 .value_is_cheap = false,
170 .read_func = StandardRead("::ndk::AParcel_readVector"),
171 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
172 }),
Steven Moreland18f75262019-12-19 16:58:55 -0800173 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
Jooyung Han01720ed2021-08-13 07:46:07 +0900174 .cpp_name = nullable + "<" + clazz + ">",
Steven Moreland18f75262019-12-19 16:58:55 -0800175 .value_is_cheap = false,
176 .read_func = StandardRead("::ndk::AParcel_readNullableParcelable"),
177 .write_func = StandardWrite("::ndk::AParcel_writeNullableParcelable"),
178 }),
Jooyung Hanb4997aa2021-10-16 03:26:12 +0900179 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
180 .cpp_name = "std::optional<std::vector<std::optional<" + clazz + ">>>",
181 .value_is_cheap = false,
182 .read_func = StandardRead("::ndk::AParcel_readVector"),
183 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
184 }),
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 }),
Jooyung Hanb4997aa2021-10-16 03:26:12 +0900346 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
347 .cpp_name = "std::optional<std::vector<::ndk::ScopedFileDescriptor>>",
348 .value_is_cheap = false,
349 .read_func = StandardRead("::ndk::AParcel_readVector"),
350 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
351 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800352 }},
Jeongik Cha8f02a532020-10-14 00:16:28 +0900353 {"ParcelableHolder",
354 TypeInfo{
355 .raw =
356 TypeInfo::Aspect{
357 .cpp_name = "::ndk::AParcelableHolder",
358 .value_is_cheap = false,
359 .read_func = StandardRead("::ndk::AParcel_readParcelable"),
360 .write_func = StandardWrite("::ndk::AParcel_writeParcelable"),
361 },
362 .array = nullptr,
363 .nullable = nullptr,
364 .nullable_array = nullptr,
365 }},
Steven Morelande8a3a192018-09-20 14:14:28 -0700366};
367
Jooyung Han5014b3e2021-10-15 09:58:20 +0900368static TypeInfo GetTypeInfo(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
369 auto& aidl_name = aidl.GetName();
370
371 if (AidlTypenames::IsBuiltinTypename(aidl_name)) {
372 auto it = kNdkTypeInfoMap.find(aidl_name);
373 AIDL_FATAL_IF(it == kNdkTypeInfoMap.end(), aidl_name);
374 return it->second;
375 }
376 const AidlDefinedType* type = types.TryGetDefinedType(aidl_name);
377 AIDL_FATAL_IF(type == nullptr, aidl_name) << "Unrecognized type.";
378
379 if (const AidlInterface* intf = type->AsInterface(); intf != nullptr) {
380 return InterfaceTypeInfo(*intf);
381 } else if (const AidlParcelable* parcelable = type->AsParcelable(); parcelable != nullptr) {
382 return ParcelableTypeInfo(*parcelable, aidl, types);
383 } else if (const AidlEnumDeclaration* enum_decl = type->AsEnumDeclaration();
384 enum_decl != nullptr) {
385 return EnumDeclarationTypeInfo(*enum_decl);
386 } else {
387 AIDL_FATAL(aidl_name) << "Unrecognized type";
388 }
389}
390
Steven Moreland055d8792018-11-14 12:48:42 -0800391static TypeInfo::Aspect GetTypeAspect(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
Steven Moreland21780812020-09-11 01:29:45 +0000392 AIDL_FATAL_IF(!aidl.IsResolved(), aidl) << aidl.ToString();
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900393 auto& aidl_name = aidl.GetName();
Steven Moreland1cb099e2018-10-17 16:31:08 -0700394
Steven Moreland67caf422018-10-15 12:39:12 -0700395 TypeInfo info;
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900396
397 // TODO(b/136048684): For now, List<T> is converted to T[].(Both are using vector<T>)
398 if (aidl_name == "List") {
Steven Morelandfe52c9f2019-11-27 19:04:48 -0800399 AIDL_FATAL_IF(!aidl.IsGeneric(), aidl) << "List must be generic type.";
400 AIDL_FATAL_IF(aidl.GetTypeParameters().size() != 1, aidl)
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900401 << "List can accept only one type parameter.";
Jooyung Han5014b3e2021-10-15 09:58:20 +0900402 const auto& type_param = *aidl.GetTypeParameters()[0];
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900403 // TODO(b/136048684) AIDL doesn't support nested type parameter yet.
Jooyung Han5014b3e2021-10-15 09:58:20 +0900404 AIDL_FATAL_IF(type_param.IsGeneric(), aidl) << "AIDL doesn't support nested type parameter";
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900405
Jooyung Han5014b3e2021-10-15 09:58:20 +0900406 info = GetTypeInfo(types, type_param);
Steven Moreland67caf422018-10-15 12:39:12 -0700407 } else {
Jooyung Han5014b3e2021-10-15 09:58:20 +0900408 info = GetTypeInfo(types, aidl);
Steven Moreland67caf422018-10-15 12:39:12 -0700409 }
410
Jooyung Han5014b3e2021-10-15 09:58:20 +0900411 if (aidl.IsArray() || aidl_name == "List") {
Steven Moreland055d8792018-11-14 12:48:42 -0800412 if (aidl.IsNullable()) {
Steven Morelandd59e3172020-05-11 16:42:09 -0700413 AIDL_FATAL_IF(info.nullable_array == nullptr, aidl)
414 << "Unsupported type in NDK Backend: " << aidl.ToString();
Steven Moreland055d8792018-11-14 12:48:42 -0800415 return *info.nullable_array;
416 }
Steven Morelandd59e3172020-05-11 16:42:09 -0700417 AIDL_FATAL_IF(info.array == nullptr, aidl)
418 << "Unsupported type in NDK Backend: " << aidl.ToString();
Steven Moreland055d8792018-11-14 12:48:42 -0800419 return *info.array;
420 }
Steven Moreland1cb099e2018-10-17 16:31:08 -0700421
Steven Moreland055d8792018-11-14 12:48:42 -0800422 if (aidl.IsNullable()) {
Steven Morelandd59e3172020-05-11 16:42:09 -0700423 AIDL_FATAL_IF(info.nullable == nullptr, aidl)
424 << "Unsupported type in NDK Backend: " << aidl.ToString();
Steven Moreland055d8792018-11-14 12:48:42 -0800425 return *info.nullable;
426 }
427
428 return info.raw;
Steven Moreland67caf422018-10-15 12:39:12 -0700429}
430
Steven Moreland2bea13b2018-10-03 15:12:33 -0700431std::string NdkFullClassName(const AidlDefinedType& type, cpp::ClassNames name) {
Steven Moreland63404532018-10-08 14:31:00 -0700432 std::vector<std::string> pieces = {"::aidl"};
Jooyung Han4b832522021-10-06 16:08:30 +0900433 std::vector<std::string> split_name = Split(type.GetCanonicalName(), ".");
434 pieces.insert(pieces.end(), split_name.begin(), split_name.end());
435 // Override name part with cpp::ClassName(type, name)
436 pieces.back() = cpp::ClassName(type, name);
Steven Moreland2bea13b2018-10-03 15:12:33 -0700437 return Join(pieces, "::");
438}
439
440std::string NdkNameOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl, StorageMode mode) {
Steven Moreland055d8792018-11-14 12:48:42 -0800441 TypeInfo::Aspect aspect = GetTypeAspect(types, aidl);
Steven Morelandeb38ee72018-10-15 14:20:04 -0700442
Steven Morelande8a3a192018-09-20 14:14:28 -0700443 switch (mode) {
444 case StorageMode::STACK:
Steven Moreland055d8792018-11-14 12:48:42 -0800445 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700446 case StorageMode::ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800447 if (aspect.value_is_cheap) {
448 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700449 } else {
Steven Moreland055d8792018-11-14 12:48:42 -0800450 return "const " + aspect.cpp_name + "&";
Steven Morelande8a3a192018-09-20 14:14:28 -0700451 }
452 case StorageMode::OUT_ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800453 return aspect.cpp_name + "*";
Steven Morelande8a3a192018-09-20 14:14:28 -0700454 default:
455 AIDL_FATAL(aidl.GetName()) << "Unrecognized mode type: " << static_cast<int>(mode);
456 }
457}
458
459void WriteToParcelFor(const CodeGeneratorContext& c) {
Steven Moreland055d8792018-11-14 12:48:42 -0800460 TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
461 aspect.write_func(c);
Steven Morelande8a3a192018-09-20 14:14:28 -0700462}
463
464void ReadFromParcelFor(const CodeGeneratorContext& c) {
Steven Moreland055d8792018-11-14 12:48:42 -0800465 TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
466 aspect.read_func(c);
Steven Morelande8a3a192018-09-20 14:14:28 -0700467}
468
Jiyong Park965c5b92018-11-21 13:37:15 +0900469std::string NdkArgList(
470 const AidlTypenames& types, const AidlMethod& method,
471 std::function<std::string(const std::string& type, const std::string& name, bool isOut)>
472 formatter) {
Steven Morelandaada3422018-09-20 15:55:33 -0700473 std::vector<std::string> method_arguments;
474 for (const auto& a : method.GetArguments()) {
475 StorageMode mode = a->IsOut() ? StorageMode::OUT_ARGUMENT : StorageMode::ARGUMENT;
Steven Moreland2bea13b2018-10-03 15:12:33 -0700476 std::string type = NdkNameOf(types, a->GetType(), mode);
Steven Morelandaada3422018-09-20 15:55:33 -0700477 std::string name = cpp::BuildVarName(*a);
Jiyong Park965c5b92018-11-21 13:37:15 +0900478 method_arguments.emplace_back(formatter(type, name, a->IsOut()));
Steven Morelandaada3422018-09-20 15:55:33 -0700479 }
480
481 if (method.GetType().GetName() != "void") {
Jiyong Park965c5b92018-11-21 13:37:15 +0900482 std::string type = NdkNameOf(types, method.GetType(), StorageMode::OUT_ARGUMENT);
483 std::string name = "_aidl_return";
484 method_arguments.emplace_back(formatter(type, name, true));
Steven Morelandaada3422018-09-20 15:55:33 -0700485 }
486
487 return Join(method_arguments, ", ");
488}
489
Steven Moreland2bea13b2018-10-03 15:12:33 -0700490std::string NdkMethodDecl(const AidlTypenames& types, const AidlMethod& method,
491 const std::string& clazz) {
Steven Morelandaada3422018-09-20 15:55:33 -0700492 std::string class_prefix = clazz.empty() ? "" : (clazz + "::");
Steven Moreland63404532018-10-08 14:31:00 -0700493 return "::ndk::ScopedAStatus " + class_prefix + method.GetName() + "(" +
Jiyong Park965c5b92018-11-21 13:37:15 +0900494 NdkArgList(types, method, FormatArgForDecl) + ")";
Steven Morelandaada3422018-09-20 15:55:33 -0700495}
496
Steven Morelande8a3a192018-09-20 14:14:28 -0700497} // namespace ndk
498} // namespace aidl
499} // namespace android