blob: b8e8d125d8964e2106c78b7269e380a1cf1f48ae [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 }),
Jooyung Hanb4997aa2021-10-16 03:26:12 +0900184 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
185 .cpp_name = "std::optional<std::vector<std::optional<" + clazz + ">>>",
186 .value_is_cheap = false,
187 .read_func = StandardRead("::ndk::AParcel_readVector"),
188 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
189 }),
Steven Morelande8a3a192018-09-20 14:14:28 -0700190 };
191}
192
Daniel Norman37d43dd2019-09-09 17:22:34 -0700193TypeInfo EnumDeclarationTypeInfo(const AidlEnumDeclaration& enum_decl) {
Steven Morelandb8df37d2019-11-21 12:33:24 -0800194 const std::string clazz = NdkFullClassName(enum_decl, cpp::ClassNames::RAW);
Daniel Norman37d43dd2019-09-09 17:22:34 -0700195
196 static map<std::string, std::string> kAParcelTypeNameMap = {
197 {"byte", "Byte"},
198 {"int", "Int32"},
199 {"long", "Int64"},
200 };
201 auto aparcel_name_it = kAParcelTypeNameMap.find(enum_decl.GetBackingType().GetName());
Steven Moreland21780812020-09-11 01:29:45 +0000202 AIDL_FATAL_IF(aparcel_name_it == kAParcelTypeNameMap.end(), enum_decl);
Daniel Norman37d43dd2019-09-09 17:22:34 -0700203 const std::string aparcel_name = aparcel_name_it->second;
204
205 const std::string backing_type_name =
206 NdkNameOf(AidlTypenames(), enum_decl.GetBackingType(), StorageMode::STACK);
207
208 return TypeInfo{
209 .raw = TypeInfo::Aspect{
210 .cpp_name = clazz,
211 .value_is_cheap = true,
212 .read_func =
213 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
214 c.writer << "AParcel_read" << aparcel_name << "(" << c.parcel
215 << ", reinterpret_cast<" << backing_type_name << "*>(" << c.var << "))";
216 },
217 .write_func =
218 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
219 c.writer << "AParcel_write" << aparcel_name << "(" << c.parcel << ", static_cast<"
220 << backing_type_name << ">(" << c.var << "))";
221 },
222 },
Daniel Normanee8674f2019-09-20 16:07:00 -0700223 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
224 .cpp_name = "std::vector<" + clazz + ">",
225 .value_is_cheap = false,
226 .read_func =
227 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
228 c.writer << "AParcel_read" << aparcel_name << "Array(" << c.parcel
229 << ", static_cast<void*>(" << c.var
230 << "), ndk::AParcel_stdVectorAllocator<" << backing_type_name << ">)";
231 },
232 .write_func =
233 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
234 c.writer << "AParcel_write" << aparcel_name << "Array(" << c.parcel
235 << ", reinterpret_cast<const " << backing_type_name << "*>(" << c.var
236 << ".data()), " << c.var << ".size())";
237 },
238 }),
Daniel Norman37d43dd2019-09-09 17:22:34 -0700239 .nullable = nullptr,
Daniel Normanee8674f2019-09-20 16:07:00 -0700240 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
241 .cpp_name = "std::optional<std::vector<" + clazz + ">>",
242 .value_is_cheap = false,
243 .read_func =
244 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
245 c.writer << "AParcel_read" << aparcel_name << "Array(" << c.parcel
246 << ", static_cast<void*>(" << c.var
247 << "), ndk::AParcel_nullableStdVectorAllocator<" << backing_type_name
248 << ">)";
249 },
250 .write_func =
251 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
252 // If the var exists, use writeArray with data() and size().
253 // Otherwise, use nullptr and -1.
254 c.writer << "AParcel_write" << aparcel_name << "Array(" << c.parcel << ", ("
255 << c.var << " ? reinterpret_cast<const " << backing_type_name << "*>("
256 << c.var << "->data()) : nullptr)"
257 << ", (" << c.var << " ? " << c.var << "->size() : -1))";
258 },
259 }),
Daniel Norman37d43dd2019-09-09 17:22:34 -0700260 };
261}
262
263// map from AIDL built-in type name to the corresponding Ndk type info
Steven Morelande8a3a192018-09-20 14:14:28 -0700264static map<std::string, TypeInfo> kNdkTypeInfoMap = {
Steven Moreland055d8792018-11-14 12:48:42 -0800265 {"void", TypeInfo{{"void", true, nullptr, nullptr}, nullptr, nullptr, nullptr}},
Steven Moreland67caf422018-10-15 12:39:12 -0700266 {"boolean", PrimitiveType("bool", "Bool")},
Jooyung Han241fdda2020-02-21 21:17:44 +0900267 {"byte", PrimitiveType("int8_t", "Byte", "uint8_t")},
Steven Moreland67caf422018-10-15 12:39:12 -0700268 {"char", PrimitiveType("char16_t", "Char")},
269 {"int", PrimitiveType("int32_t", "Int32")},
270 {"long", PrimitiveType("int64_t", "Int64")},
271 {"float", PrimitiveType("float", "Float")},
272 {"double", PrimitiveType("double", "Double")},
Steven Moreland63404532018-10-08 14:31:00 -0700273 {"String",
Steven Moreland055d8792018-11-14 12:48:42 -0800274 TypeInfo{
275 .raw =
276 TypeInfo::Aspect{
277 .cpp_name = "std::string",
278 .value_is_cheap = false,
279 .read_func = StandardRead("::ndk::AParcel_readString"),
280 .write_func = StandardWrite("::ndk::AParcel_writeString"),
281 },
282 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
283 .cpp_name = "std::vector<std::string>",
284 .value_is_cheap = false,
285 .read_func = StandardRead("::ndk::AParcel_readVector"),
286 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
287 }),
Steven Moreland18ed9782018-11-14 17:08:09 -0800288 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
289 .cpp_name = "std::optional<std::string>",
290 .value_is_cheap = false,
291 .read_func = StandardRead("::ndk::AParcel_readString"),
292 .write_func = StandardWrite("::ndk::AParcel_writeString"),
293 }),
294 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
295 .cpp_name = "std::optional<std::vector<std::optional<std::string>>>",
296 .value_is_cheap = false,
297 .read_func = StandardRead("::ndk::AParcel_readVector"),
298 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
299 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800300 }},
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900301 // TODO(b/136048684) {"Map", ""},
Steven Moreland055d8792018-11-14 12:48:42 -0800302 {"IBinder",
303 TypeInfo{
304 .raw =
305 TypeInfo::Aspect{
306 .cpp_name = "::ndk::SpAIBinder",
307 .value_is_cheap = false,
Steven Morelande1048a32018-11-16 12:52:17 -0800308 .read_func = StandardRead("::ndk::AParcel_readRequiredStrongBinder"),
309 .write_func = StandardRead("::ndk::AParcel_writeRequiredStrongBinder"),
Steven Moreland055d8792018-11-14 12:48:42 -0800310 },
Jooyung Hanbc7ab902021-10-14 07:09:49 +0900311 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
312 .cpp_name = "std::vector<::ndk::SpAIBinder>",
313 .value_is_cheap = false,
314 .read_func = StandardRead("::ndk::AParcel_readVector"),
315 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
316 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800317 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
318 .cpp_name = "::ndk::SpAIBinder",
319 .value_is_cheap = false,
Steven Morelande1048a32018-11-16 12:52:17 -0800320 .read_func = StandardRead("::ndk::AParcel_readNullableStrongBinder"),
321 .write_func = StandardRead("::ndk::AParcel_writeNullableStrongBinder"),
Steven Moreland055d8792018-11-14 12:48:42 -0800322 }),
Jooyung Hanbc7ab902021-10-14 07:09:49 +0900323 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
324 .cpp_name = "std::optional<std::vector<::ndk::SpAIBinder>>",
325 .value_is_cheap = false,
326 .read_func = StandardRead("::ndk::AParcel_readVector"),
327 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
328 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800329 }},
Steven Moreland055d8792018-11-14 12:48:42 -0800330 {"ParcelFileDescriptor",
331 TypeInfo{
332 .raw =
333 TypeInfo::Aspect{
334 .cpp_name = "::ndk::ScopedFileDescriptor",
335 .value_is_cheap = false,
Steven Moreland962c60d2018-11-16 15:19:27 -0800336 .read_func = StandardRead("::ndk::AParcel_readRequiredParcelFileDescriptor"),
337 .write_func = StandardRead("::ndk::AParcel_writeRequiredParcelFileDescriptor"),
Steven Moreland055d8792018-11-14 12:48:42 -0800338 },
Jeongik Chacc8dd1d2019-11-14 10:22:57 +0900339 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
340 .cpp_name = "std::vector<::ndk::ScopedFileDescriptor>",
341 .value_is_cheap = false,
342 .read_func = StandardRead("::ndk::AParcel_readVector"),
343 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
344 }),
Steven Moreland962c60d2018-11-16 15:19:27 -0800345 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
346 .cpp_name = "::ndk::ScopedFileDescriptor",
347 .value_is_cheap = false,
348 .read_func = StandardRead("::ndk::AParcel_readNullableParcelFileDescriptor"),
349 .write_func = StandardRead("::ndk::AParcel_writeNullableParcelFileDescriptor"),
350 }),
Jooyung Hanb4997aa2021-10-16 03:26:12 +0900351 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
352 .cpp_name = "std::optional<std::vector<::ndk::ScopedFileDescriptor>>",
353 .value_is_cheap = false,
354 .read_func = StandardRead("::ndk::AParcel_readVector"),
355 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
356 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800357 }},
Jeongik Cha8f02a532020-10-14 00:16:28 +0900358 {"ParcelableHolder",
359 TypeInfo{
360 .raw =
361 TypeInfo::Aspect{
362 .cpp_name = "::ndk::AParcelableHolder",
363 .value_is_cheap = false,
364 .read_func = StandardRead("::ndk::AParcel_readParcelable"),
365 .write_func = StandardWrite("::ndk::AParcel_writeParcelable"),
366 },
367 .array = nullptr,
368 .nullable = nullptr,
369 .nullable_array = nullptr,
370 }},
Steven Morelande8a3a192018-09-20 14:14:28 -0700371};
372
Jooyung Han5014b3e2021-10-15 09:58:20 +0900373static TypeInfo GetTypeInfo(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
374 auto& aidl_name = aidl.GetName();
375
376 if (AidlTypenames::IsBuiltinTypename(aidl_name)) {
377 auto it = kNdkTypeInfoMap.find(aidl_name);
378 AIDL_FATAL_IF(it == kNdkTypeInfoMap.end(), aidl_name);
379 return it->second;
380 }
381 const AidlDefinedType* type = types.TryGetDefinedType(aidl_name);
382 AIDL_FATAL_IF(type == nullptr, aidl_name) << "Unrecognized type.";
383
384 if (const AidlInterface* intf = type->AsInterface(); intf != nullptr) {
385 return InterfaceTypeInfo(*intf);
386 } else if (const AidlParcelable* parcelable = type->AsParcelable(); parcelable != nullptr) {
387 return ParcelableTypeInfo(*parcelable, aidl, types);
388 } else if (const AidlEnumDeclaration* enum_decl = type->AsEnumDeclaration();
389 enum_decl != nullptr) {
390 return EnumDeclarationTypeInfo(*enum_decl);
391 } else {
392 AIDL_FATAL(aidl_name) << "Unrecognized type";
393 }
394}
395
Steven Moreland055d8792018-11-14 12:48:42 -0800396static TypeInfo::Aspect GetTypeAspect(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
Steven Moreland21780812020-09-11 01:29:45 +0000397 AIDL_FATAL_IF(!aidl.IsResolved(), aidl) << aidl.ToString();
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900398 auto& aidl_name = aidl.GetName();
Steven Moreland1cb099e2018-10-17 16:31:08 -0700399
Steven Moreland67caf422018-10-15 12:39:12 -0700400 TypeInfo info;
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900401
402 // TODO(b/136048684): For now, List<T> is converted to T[].(Both are using vector<T>)
403 if (aidl_name == "List") {
Steven Morelandfe52c9f2019-11-27 19:04:48 -0800404 AIDL_FATAL_IF(!aidl.IsGeneric(), aidl) << "List must be generic type.";
405 AIDL_FATAL_IF(aidl.GetTypeParameters().size() != 1, aidl)
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900406 << "List can accept only one type parameter.";
Jooyung Han5014b3e2021-10-15 09:58:20 +0900407 const auto& type_param = *aidl.GetTypeParameters()[0];
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900408 // TODO(b/136048684) AIDL doesn't support nested type parameter yet.
Jooyung Han5014b3e2021-10-15 09:58:20 +0900409 AIDL_FATAL_IF(type_param.IsGeneric(), aidl) << "AIDL doesn't support nested type parameter";
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900410
Jooyung Han5014b3e2021-10-15 09:58:20 +0900411 info = GetTypeInfo(types, type_param);
Steven Moreland67caf422018-10-15 12:39:12 -0700412 } else {
Jooyung Han5014b3e2021-10-15 09:58:20 +0900413 info = GetTypeInfo(types, aidl);
Steven Moreland67caf422018-10-15 12:39:12 -0700414 }
415
Jooyung Han5014b3e2021-10-15 09:58:20 +0900416 if (aidl.IsArray() || aidl_name == "List") {
Steven Moreland055d8792018-11-14 12:48:42 -0800417 if (aidl.IsNullable()) {
Steven Morelandd59e3172020-05-11 16:42:09 -0700418 AIDL_FATAL_IF(info.nullable_array == nullptr, aidl)
419 << "Unsupported type in NDK Backend: " << aidl.ToString();
Steven Moreland055d8792018-11-14 12:48:42 -0800420 return *info.nullable_array;
421 }
Steven Morelandd59e3172020-05-11 16:42:09 -0700422 AIDL_FATAL_IF(info.array == nullptr, aidl)
423 << "Unsupported type in NDK Backend: " << aidl.ToString();
Steven Moreland055d8792018-11-14 12:48:42 -0800424 return *info.array;
425 }
Steven Moreland1cb099e2018-10-17 16:31:08 -0700426
Steven Moreland055d8792018-11-14 12:48:42 -0800427 if (aidl.IsNullable()) {
Steven Morelandd59e3172020-05-11 16:42:09 -0700428 AIDL_FATAL_IF(info.nullable == nullptr, aidl)
429 << "Unsupported type in NDK Backend: " << aidl.ToString();
Steven Moreland055d8792018-11-14 12:48:42 -0800430 return *info.nullable;
431 }
432
433 return info.raw;
Steven Moreland67caf422018-10-15 12:39:12 -0700434}
435
Steven Moreland2bea13b2018-10-03 15:12:33 -0700436std::string NdkFullClassName(const AidlDefinedType& type, cpp::ClassNames name) {
Steven Moreland63404532018-10-08 14:31:00 -0700437 std::vector<std::string> pieces = {"::aidl"};
Jooyung Han4b832522021-10-06 16:08:30 +0900438 std::vector<std::string> split_name = Split(type.GetCanonicalName(), ".");
439 pieces.insert(pieces.end(), split_name.begin(), split_name.end());
440 // Override name part with cpp::ClassName(type, name)
441 pieces.back() = cpp::ClassName(type, name);
Steven Moreland2bea13b2018-10-03 15:12:33 -0700442 return Join(pieces, "::");
443}
444
445std::string NdkNameOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl, StorageMode mode) {
Steven Moreland055d8792018-11-14 12:48:42 -0800446 TypeInfo::Aspect aspect = GetTypeAspect(types, aidl);
Steven Morelandeb38ee72018-10-15 14:20:04 -0700447
Steven Morelande8a3a192018-09-20 14:14:28 -0700448 switch (mode) {
449 case StorageMode::STACK:
Steven Moreland055d8792018-11-14 12:48:42 -0800450 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700451 case StorageMode::ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800452 if (aspect.value_is_cheap) {
453 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700454 } else {
Steven Moreland055d8792018-11-14 12:48:42 -0800455 return "const " + aspect.cpp_name + "&";
Steven Morelande8a3a192018-09-20 14:14:28 -0700456 }
457 case StorageMode::OUT_ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800458 return aspect.cpp_name + "*";
Steven Morelande8a3a192018-09-20 14:14:28 -0700459 default:
460 AIDL_FATAL(aidl.GetName()) << "Unrecognized mode type: " << static_cast<int>(mode);
461 }
462}
463
Devin Moore83e76982020-09-30 09:32:35 -0700464size_t NdkAlignmentOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
465 // map from NDK type name to the corresponding alignment size
466 static map<string, int> alignment = {
467 {"bool", 1}, {"int8_t", 1}, {"char16_t", 2}, {"double", 8},
468 {"float", 4}, {"int32_t", 4}, {"int64_t", 8},
469 };
470
471 const string& name = NdkNameOf(types, aidl, StorageMode::STACK);
472 if (alignment.find(name) != alignment.end()) {
473 return alignment[name];
474 } else {
475 const auto& definedType = types.TryGetDefinedType(aidl.GetName());
476 AIDL_FATAL_IF(definedType == nullptr, aidl) << "Failed to resolve type.";
477 if (const auto& enumType = definedType->AsEnumDeclaration(); enumType != nullptr) {
478 return NdkAlignmentOf(types, enumType->GetBackingType());
479 }
480 }
481 return 0;
482}
483
Steven Morelande8a3a192018-09-20 14:14:28 -0700484void WriteToParcelFor(const CodeGeneratorContext& c) {
Steven Moreland055d8792018-11-14 12:48:42 -0800485 TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
486 aspect.write_func(c);
Steven Morelande8a3a192018-09-20 14:14:28 -0700487}
488
489void ReadFromParcelFor(const CodeGeneratorContext& c) {
Steven Moreland055d8792018-11-14 12:48:42 -0800490 TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
491 aspect.read_func(c);
Steven Morelande8a3a192018-09-20 14:14:28 -0700492}
493
Jiyong Park965c5b92018-11-21 13:37:15 +0900494std::string NdkArgList(
495 const AidlTypenames& types, const AidlMethod& method,
496 std::function<std::string(const std::string& type, const std::string& name, bool isOut)>
497 formatter) {
Steven Morelandaada3422018-09-20 15:55:33 -0700498 std::vector<std::string> method_arguments;
499 for (const auto& a : method.GetArguments()) {
500 StorageMode mode = a->IsOut() ? StorageMode::OUT_ARGUMENT : StorageMode::ARGUMENT;
Steven Moreland2bea13b2018-10-03 15:12:33 -0700501 std::string type = NdkNameOf(types, a->GetType(), mode);
Steven Morelandaada3422018-09-20 15:55:33 -0700502 std::string name = cpp::BuildVarName(*a);
Jiyong Park965c5b92018-11-21 13:37:15 +0900503 method_arguments.emplace_back(formatter(type, name, a->IsOut()));
Steven Morelandaada3422018-09-20 15:55:33 -0700504 }
505
506 if (method.GetType().GetName() != "void") {
Jiyong Park965c5b92018-11-21 13:37:15 +0900507 std::string type = NdkNameOf(types, method.GetType(), StorageMode::OUT_ARGUMENT);
508 std::string name = "_aidl_return";
509 method_arguments.emplace_back(formatter(type, name, true));
Steven Morelandaada3422018-09-20 15:55:33 -0700510 }
511
512 return Join(method_arguments, ", ");
513}
514
Steven Moreland2bea13b2018-10-03 15:12:33 -0700515std::string NdkMethodDecl(const AidlTypenames& types, const AidlMethod& method,
516 const std::string& clazz) {
Steven Morelandaada3422018-09-20 15:55:33 -0700517 std::string class_prefix = clazz.empty() ? "" : (clazz + "::");
Steven Moreland63404532018-10-08 14:31:00 -0700518 return "::ndk::ScopedAStatus " + class_prefix + method.GetName() + "(" +
Jiyong Park965c5b92018-11-21 13:37:15 +0900519 NdkArgList(types, method, FormatArgForDecl) + ")";
Steven Morelandaada3422018-09-20 15:55:33 -0700520}
521
Steven Morelande8a3a192018-09-20 14:14:28 -0700522} // namespace ndk
523} // namespace aidl
524} // namespace android