blob: 6884e899e542207abb6797e880ba5153e58d31e1 [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;
Steven Morelande8a3a192018-09-20 14:14:28 -070027
28namespace android {
29namespace aidl {
30namespace ndk {
31
Steven Moreland7c933372018-10-11 15:20:04 -070032std::string NdkHeaderFile(const AidlDefinedType& defined_type, cpp::ClassNames name,
33 bool use_os_sep) {
34 char seperator = (use_os_sep) ? OS_PATH_SEPARATOR : '/';
35 return std::string("aidl") + seperator + cpp::HeaderFile(defined_type, name, use_os_sep);
36}
37
Steven Moreland055d8792018-11-14 12:48:42 -080038// This represents a type in AIDL (e.g. 'String' which can be referenced in multiple ways)
Steven Morelande8a3a192018-09-20 14:14:28 -070039struct TypeInfo {
Steven Moreland055d8792018-11-14 12:48:42 -080040 struct Aspect {
41 // name of the type in C++ output
42 std::string cpp_name;
43 // whether to prefer 'value type' over 'const&'
44 bool value_is_cheap;
Steven Morelande8a3a192018-09-20 14:14:28 -070045
Steven Moreland055d8792018-11-14 12:48:42 -080046 std::function<void(const CodeGeneratorContext& c)> read_func;
47 std::function<void(const CodeGeneratorContext& c)> write_func;
48 };
Steven Morelandeb38ee72018-10-15 14:20:04 -070049
Steven Moreland055d8792018-11-14 12:48:42 -080050 // e.g. 'String'
51 Aspect raw;
52
53 // e.g. 'String[]'
54 std::shared_ptr<Aspect> array;
55
56 // note: Nullable types do not exist in Java. For most Java types, the type is split into a
57 // nullable and non-nullable variant. This is because C++ types are more usually non-nullable, but
58 // everything in Java is non-nullable. This does mean that some Java interfaces may have to have
59 // '@nullable' added to them in order to function as expected w/ the NDK. It also means that some
60 // transactions will be allowed in Java which are not allowed in C++. However, in Java, if a null
61 // is ignored, it will just result in a NullPointerException and be delivered to the other side.
62 // C++ does not have this same capacity (in Android), and so instead, we distinguish nullability
63 // in the type system.
64
65 // e.g. '@nullable String'
66 std::shared_ptr<Aspect> nullable;
67
68 // e.g. '@nullable String[]'
69 std::shared_ptr<Aspect> nullable_array;
Steven Morelande8a3a192018-09-20 14:14:28 -070070};
71
Daniel Norman37d43dd2019-09-09 17:22:34 -070072std::string ConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value) {
Will McVickerd7d18df2019-09-12 13:40:50 -070073 if (type.GetName() == "long" && !type.IsArray()) {
Daniel Norman37d43dd2019-09-09 17:22:34 -070074 return raw_value + "L";
75 }
76
77 return raw_value;
78};
79
Steven Moreland67caf422018-10-15 12:39:12 -070080static std::function<void(const CodeGeneratorContext& c)> StandardRead(const std::string& name) {
Steven Morelande8a3a192018-09-20 14:14:28 -070081 return [name](const CodeGeneratorContext& c) {
Steven Moreland67caf422018-10-15 12:39:12 -070082 c.writer << name << "(" << c.parcel << ", " << c.var << ")";
Steven Morelande8a3a192018-09-20 14:14:28 -070083 };
84}
Steven Moreland67caf422018-10-15 12:39:12 -070085static std::function<void(const CodeGeneratorContext& c)> StandardWrite(const std::string& name) {
Steven Morelande8a3a192018-09-20 14:14:28 -070086 return [name](const CodeGeneratorContext& c) {
Steven Moreland67caf422018-10-15 12:39:12 -070087 c.writer << name << "(" << c.parcel << ", " << c.var << ")";
88 };
89}
90
Jooyung Han241fdda2020-02-21 21:17:44 +090091TypeInfo PrimitiveType(const std::string& cpp_name, const std::string& pretty_name,
92 const std::optional<std::string>& cpp_name_for_array_opt = std::nullopt) {
93 std::string cpp_name_for_array = cpp_name_for_array_opt.value_or(cpp_name);
Steven Moreland67caf422018-10-15 12:39:12 -070094 return TypeInfo{
Steven Moreland055d8792018-11-14 12:48:42 -080095 .raw =
96 TypeInfo::Aspect{
97 .cpp_name = cpp_name,
98 .value_is_cheap = true,
99 .read_func = StandardRead("AParcel_read" + pretty_name),
100 .write_func = StandardWrite("AParcel_write" + pretty_name),
101 },
102 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
Jooyung Han241fdda2020-02-21 21:17:44 +0900103 .cpp_name = "std::vector<" + cpp_name_for_array + ">",
Steven Moreland055d8792018-11-14 12:48:42 -0800104 .value_is_cheap = false,
105 .read_func = StandardRead("::ndk::AParcel_readVector"),
106 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
107 }),
108 .nullable = nullptr,
Steven Morelandc10ed462018-11-15 14:50:28 -0800109 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
Jooyung Han241fdda2020-02-21 21:17:44 +0900110 .cpp_name = "std::optional<std::vector<" + cpp_name_for_array + ">>",
Steven Morelandc10ed462018-11-15 14:50:28 -0800111 .value_is_cheap = false,
112 .read_func = StandardRead("::ndk::AParcel_readVector"),
113 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
114 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800115 };
116}
117
118TypeInfo InterfaceTypeInfo(const AidlInterface& type) {
119 const std::string clazz = NdkFullClassName(type, cpp::ClassNames::INTERFACE);
120
121 return TypeInfo{
122 .raw =
123 TypeInfo::Aspect{
124 .cpp_name = "std::shared_ptr<" + clazz + ">",
125 .value_is_cheap = false,
Steven Moreland055d8792018-11-14 12:48:42 -0800126 .read_func = StandardRead(clazz + "::readFromParcel"),
127 .write_func = StandardWrite(clazz + "::writeToParcel"),
128 },
129 .array = nullptr,
130 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
131 .cpp_name = "std::shared_ptr<" + clazz + ">",
132 .value_is_cheap = false,
133 .read_func = StandardRead(clazz + "::readFromParcel"),
134 .write_func = StandardWrite(clazz + "::writeToParcel"),
135 }),
136 .nullable_array = nullptr,
137 };
138}
139
Devin Moore53fc99c2020-08-12 08:07:52 -0700140TypeInfo ParcelableTypeInfo(const AidlParcelable& type, const AidlTypeSpecifier& typeSpec,
141 const AidlTypenames& types) {
142 std::string clazz = NdkFullClassName(type, cpp::ClassNames::RAW);
143 std::string template_params = "";
144 if (typeSpec.IsGeneric()) {
145 std::vector<std::string> type_params;
146 for (const auto& parameter : typeSpec.GetTypeParameters()) {
147 type_params.push_back(NdkNameOf(types, *parameter, StorageMode::STACK));
148 }
149 clazz += base::StringPrintf("<%s>", base::Join(type_params, ", ").c_str());
150 }
Steven Moreland055d8792018-11-14 12:48:42 -0800151 return TypeInfo{
152 .raw =
153 TypeInfo::Aspect{
154 .cpp_name = clazz,
155 .value_is_cheap = false,
Steven Moreland4348f9a2019-12-16 16:33:01 -0800156 .read_func = StandardRead("::ndk::AParcel_readParcelable"),
157 .write_func = StandardWrite("::ndk::AParcel_writeParcelable"),
Steven Moreland055d8792018-11-14 12:48:42 -0800158 },
Steven Moreland5ecec6b2018-12-11 18:56:41 -0800159 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
160 .cpp_name = "std::vector<" + clazz + ">",
161 .value_is_cheap = false,
162 .read_func = StandardRead("::ndk::AParcel_readVector"),
163 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
164 }),
Steven Moreland18f75262019-12-19 16:58:55 -0800165 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
166 .cpp_name = "std::optional<" + clazz + ">",
167 .value_is_cheap = false,
168 .read_func = StandardRead("::ndk::AParcel_readNullableParcelable"),
169 .write_func = StandardWrite("::ndk::AParcel_writeNullableParcelable"),
170 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800171 .nullable_array = nullptr,
Steven Morelande8a3a192018-09-20 14:14:28 -0700172 };
173}
174
Daniel Norman37d43dd2019-09-09 17:22:34 -0700175TypeInfo EnumDeclarationTypeInfo(const AidlEnumDeclaration& enum_decl) {
Steven Morelandb8df37d2019-11-21 12:33:24 -0800176 const std::string clazz = NdkFullClassName(enum_decl, cpp::ClassNames::RAW);
Daniel Norman37d43dd2019-09-09 17:22:34 -0700177
178 static map<std::string, std::string> kAParcelTypeNameMap = {
179 {"byte", "Byte"},
180 {"int", "Int32"},
181 {"long", "Int64"},
182 };
183 auto aparcel_name_it = kAParcelTypeNameMap.find(enum_decl.GetBackingType().GetName());
Steven Moreland21780812020-09-11 01:29:45 +0000184 AIDL_FATAL_IF(aparcel_name_it == kAParcelTypeNameMap.end(), enum_decl);
Daniel Norman37d43dd2019-09-09 17:22:34 -0700185 const std::string aparcel_name = aparcel_name_it->second;
186
187 const std::string backing_type_name =
188 NdkNameOf(AidlTypenames(), enum_decl.GetBackingType(), StorageMode::STACK);
189
190 return TypeInfo{
191 .raw = TypeInfo::Aspect{
192 .cpp_name = clazz,
193 .value_is_cheap = true,
194 .read_func =
195 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
196 c.writer << "AParcel_read" << aparcel_name << "(" << c.parcel
197 << ", reinterpret_cast<" << backing_type_name << "*>(" << c.var << "))";
198 },
199 .write_func =
200 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
201 c.writer << "AParcel_write" << aparcel_name << "(" << c.parcel << ", static_cast<"
202 << backing_type_name << ">(" << c.var << "))";
203 },
204 },
Daniel Normanee8674f2019-09-20 16:07:00 -0700205 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
206 .cpp_name = "std::vector<" + clazz + ">",
207 .value_is_cheap = false,
208 .read_func =
209 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
210 c.writer << "AParcel_read" << aparcel_name << "Array(" << c.parcel
211 << ", static_cast<void*>(" << c.var
212 << "), ndk::AParcel_stdVectorAllocator<" << backing_type_name << ">)";
213 },
214 .write_func =
215 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
216 c.writer << "AParcel_write" << aparcel_name << "Array(" << c.parcel
217 << ", reinterpret_cast<const " << backing_type_name << "*>(" << c.var
218 << ".data()), " << c.var << ".size())";
219 },
220 }),
Daniel Norman37d43dd2019-09-09 17:22:34 -0700221 .nullable = nullptr,
Daniel Normanee8674f2019-09-20 16:07:00 -0700222 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
223 .cpp_name = "std::optional<std::vector<" + clazz + ">>",
224 .value_is_cheap = false,
225 .read_func =
226 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
227 c.writer << "AParcel_read" << aparcel_name << "Array(" << c.parcel
228 << ", static_cast<void*>(" << c.var
229 << "), ndk::AParcel_nullableStdVectorAllocator<" << backing_type_name
230 << ">)";
231 },
232 .write_func =
233 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
234 // If the var exists, use writeArray with data() and size().
235 // Otherwise, use nullptr and -1.
236 c.writer << "AParcel_write" << aparcel_name << "Array(" << c.parcel << ", ("
237 << c.var << " ? reinterpret_cast<const " << backing_type_name << "*>("
238 << c.var << "->data()) : nullptr)"
239 << ", (" << c.var << " ? " << c.var << "->size() : -1))";
240 },
241 }),
Daniel Norman37d43dd2019-09-09 17:22:34 -0700242 };
243}
244
245// map from AIDL built-in type name to the corresponding Ndk type info
Steven Morelande8a3a192018-09-20 14:14:28 -0700246static map<std::string, TypeInfo> kNdkTypeInfoMap = {
Steven Moreland055d8792018-11-14 12:48:42 -0800247 {"void", TypeInfo{{"void", true, nullptr, nullptr}, nullptr, nullptr, nullptr}},
Steven Moreland67caf422018-10-15 12:39:12 -0700248 {"boolean", PrimitiveType("bool", "Bool")},
Jooyung Han241fdda2020-02-21 21:17:44 +0900249 {"byte", PrimitiveType("int8_t", "Byte", "uint8_t")},
Steven Moreland67caf422018-10-15 12:39:12 -0700250 {"char", PrimitiveType("char16_t", "Char")},
251 {"int", PrimitiveType("int32_t", "Int32")},
252 {"long", PrimitiveType("int64_t", "Int64")},
253 {"float", PrimitiveType("float", "Float")},
254 {"double", PrimitiveType("double", "Double")},
Steven Moreland63404532018-10-08 14:31:00 -0700255 {"String",
Steven Moreland055d8792018-11-14 12:48:42 -0800256 TypeInfo{
257 .raw =
258 TypeInfo::Aspect{
259 .cpp_name = "std::string",
260 .value_is_cheap = false,
261 .read_func = StandardRead("::ndk::AParcel_readString"),
262 .write_func = StandardWrite("::ndk::AParcel_writeString"),
263 },
264 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
265 .cpp_name = "std::vector<std::string>",
266 .value_is_cheap = false,
267 .read_func = StandardRead("::ndk::AParcel_readVector"),
268 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
269 }),
Steven Moreland18ed9782018-11-14 17:08:09 -0800270 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
271 .cpp_name = "std::optional<std::string>",
272 .value_is_cheap = false,
273 .read_func = StandardRead("::ndk::AParcel_readString"),
274 .write_func = StandardWrite("::ndk::AParcel_writeString"),
275 }),
276 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
277 .cpp_name = "std::optional<std::vector<std::optional<std::string>>>",
278 .value_is_cheap = false,
279 .read_func = StandardRead("::ndk::AParcel_readVector"),
280 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
281 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800282 }},
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900283 // TODO(b/136048684) {"Map", ""},
Steven Moreland055d8792018-11-14 12:48:42 -0800284 {"IBinder",
285 TypeInfo{
286 .raw =
287 TypeInfo::Aspect{
288 .cpp_name = "::ndk::SpAIBinder",
289 .value_is_cheap = false,
Steven Morelande1048a32018-11-16 12:52:17 -0800290 .read_func = StandardRead("::ndk::AParcel_readRequiredStrongBinder"),
291 .write_func = StandardRead("::ndk::AParcel_writeRequiredStrongBinder"),
Steven Moreland055d8792018-11-14 12:48:42 -0800292 },
293 .array = nullptr,
294 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
295 .cpp_name = "::ndk::SpAIBinder",
296 .value_is_cheap = false,
Steven Morelande1048a32018-11-16 12:52:17 -0800297 .read_func = StandardRead("::ndk::AParcel_readNullableStrongBinder"),
298 .write_func = StandardRead("::ndk::AParcel_writeNullableStrongBinder"),
Steven Moreland055d8792018-11-14 12:48:42 -0800299 }),
300 .nullable_array = nullptr,
301 }},
Steven Moreland055d8792018-11-14 12:48:42 -0800302 {"ParcelFileDescriptor",
303 TypeInfo{
304 .raw =
305 TypeInfo::Aspect{
306 .cpp_name = "::ndk::ScopedFileDescriptor",
307 .value_is_cheap = false,
Steven Moreland962c60d2018-11-16 15:19:27 -0800308 .read_func = StandardRead("::ndk::AParcel_readRequiredParcelFileDescriptor"),
309 .write_func = StandardRead("::ndk::AParcel_writeRequiredParcelFileDescriptor"),
Steven Moreland055d8792018-11-14 12:48:42 -0800310 },
Jeongik Chacc8dd1d2019-11-14 10:22:57 +0900311 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
312 .cpp_name = "std::vector<::ndk::ScopedFileDescriptor>",
313 .value_is_cheap = false,
314 .read_func = StandardRead("::ndk::AParcel_readVector"),
315 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
316 }),
Steven Moreland962c60d2018-11-16 15:19:27 -0800317 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
318 .cpp_name = "::ndk::ScopedFileDescriptor",
319 .value_is_cheap = false,
320 .read_func = StandardRead("::ndk::AParcel_readNullableParcelFileDescriptor"),
321 .write_func = StandardRead("::ndk::AParcel_writeNullableParcelFileDescriptor"),
322 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800323 .nullable_array = nullptr,
324 }},
Jeongik Cha8f02a532020-10-14 00:16:28 +0900325 {"ParcelableHolder",
326 TypeInfo{
327 .raw =
328 TypeInfo::Aspect{
329 .cpp_name = "::ndk::AParcelableHolder",
330 .value_is_cheap = false,
331 .read_func = StandardRead("::ndk::AParcel_readParcelable"),
332 .write_func = StandardWrite("::ndk::AParcel_writeParcelable"),
333 },
334 .array = nullptr,
335 .nullable = nullptr,
336 .nullable_array = nullptr,
337 }},
Steven Morelande8a3a192018-09-20 14:14:28 -0700338};
339
Steven Moreland055d8792018-11-14 12:48:42 -0800340static TypeInfo::Aspect GetTypeAspect(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
Steven Moreland21780812020-09-11 01:29:45 +0000341 AIDL_FATAL_IF(!aidl.IsResolved(), aidl) << aidl.ToString();
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900342 auto& aidl_name = aidl.GetName();
Steven Moreland1cb099e2018-10-17 16:31:08 -0700343
Steven Moreland67caf422018-10-15 12:39:12 -0700344 TypeInfo info;
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900345
346 // TODO(b/136048684): For now, List<T> is converted to T[].(Both are using vector<T>)
347 if (aidl_name == "List") {
Steven Morelandfe52c9f2019-11-27 19:04:48 -0800348 AIDL_FATAL_IF(!aidl.IsGeneric(), aidl) << "List must be generic type.";
349 AIDL_FATAL_IF(aidl.GetTypeParameters().size() != 1, aidl)
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900350 << "List can accept only one type parameter.";
Devin Moore53fc99c2020-08-12 08:07:52 -0700351 const auto& type_param = aidl.GetTypeParameters()[0];
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900352 // TODO(b/136048684) AIDL doesn't support nested type parameter yet.
353 AIDL_FATAL_IF(type_param->IsGeneric(), aidl) << "AIDL doesn't support nested type parameter";
354
355 AidlTypeSpecifier array_type =
356 AidlTypeSpecifier(AIDL_LOCATION_HERE, type_param->GetUnresolvedName(), true /* isArray */,
357 nullptr /* type_params */, aidl.GetComments());
358 if (!(array_type.Resolve(types) && array_type.CheckValid(types))) {
359 AIDL_FATAL(aidl) << "The type parameter is wrong.";
360 }
361 return GetTypeAspect(types, array_type);
362 }
363
Steven Moreland67caf422018-10-15 12:39:12 -0700364 if (AidlTypenames::IsBuiltinTypename(aidl_name)) {
365 auto it = kNdkTypeInfoMap.find(aidl_name);
Steven Moreland21780812020-09-11 01:29:45 +0000366 AIDL_FATAL_IF(it == kNdkTypeInfoMap.end(), aidl_name);
Steven Moreland67caf422018-10-15 12:39:12 -0700367 info = it->second;
368 } else {
369 const AidlDefinedType* type = types.TryGetDefinedType(aidl_name);
Steven Moreland67caf422018-10-15 12:39:12 -0700370 AIDL_FATAL_IF(type == nullptr, aidl_name) << "Unrecognized type.";
371
Daniel Norman37d43dd2019-09-09 17:22:34 -0700372 if (const AidlInterface* intf = type->AsInterface(); intf != nullptr) {
373 info = InterfaceTypeInfo(*intf);
374 } else if (const AidlParcelable* parcelable = type->AsParcelable(); parcelable != nullptr) {
Devin Moore53fc99c2020-08-12 08:07:52 -0700375 info = ParcelableTypeInfo(*parcelable, aidl, types);
Daniel Norman37d43dd2019-09-09 17:22:34 -0700376 } else if (const AidlEnumDeclaration* enum_decl = type->AsEnumDeclaration();
377 enum_decl != nullptr) {
378 info = EnumDeclarationTypeInfo(*enum_decl);
Steven Moreland67caf422018-10-15 12:39:12 -0700379 } else {
380 AIDL_FATAL(aidl_name) << "Unrecognized type";
381 }
382 }
383
Steven Moreland055d8792018-11-14 12:48:42 -0800384 if (aidl.IsArray()) {
385 if (aidl.IsNullable()) {
Steven Morelandd59e3172020-05-11 16:42:09 -0700386 AIDL_FATAL_IF(info.nullable_array == nullptr, aidl)
387 << "Unsupported type in NDK Backend: " << aidl.ToString();
Steven Moreland055d8792018-11-14 12:48:42 -0800388 return *info.nullable_array;
389 }
Steven Morelandd59e3172020-05-11 16:42:09 -0700390 AIDL_FATAL_IF(info.array == nullptr, aidl)
391 << "Unsupported type in NDK Backend: " << aidl.ToString();
Steven Moreland055d8792018-11-14 12:48:42 -0800392 return *info.array;
393 }
Steven Moreland1cb099e2018-10-17 16:31:08 -0700394
Steven Moreland055d8792018-11-14 12:48:42 -0800395 if (aidl.IsNullable()) {
Steven Morelandd59e3172020-05-11 16:42:09 -0700396 AIDL_FATAL_IF(info.nullable == nullptr, aidl)
397 << "Unsupported type in NDK Backend: " << aidl.ToString();
Steven Moreland055d8792018-11-14 12:48:42 -0800398 return *info.nullable;
399 }
400
401 return info.raw;
Steven Moreland67caf422018-10-15 12:39:12 -0700402}
403
Steven Moreland2bea13b2018-10-03 15:12:33 -0700404std::string NdkFullClassName(const AidlDefinedType& type, cpp::ClassNames name) {
Steven Moreland63404532018-10-08 14:31:00 -0700405 std::vector<std::string> pieces = {"::aidl"};
Steven Moreland2bea13b2018-10-03 15:12:33 -0700406 std::vector<std::string> package = type.GetSplitPackage();
407 pieces.insert(pieces.end(), package.begin(), package.end());
408 pieces.push_back(cpp::ClassName(type, name));
409
410 return Join(pieces, "::");
411}
412
413std::string NdkNameOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl, StorageMode mode) {
Steven Moreland055d8792018-11-14 12:48:42 -0800414 TypeInfo::Aspect aspect = GetTypeAspect(types, aidl);
Steven Morelandeb38ee72018-10-15 14:20:04 -0700415
Steven Morelande8a3a192018-09-20 14:14:28 -0700416 switch (mode) {
417 case StorageMode::STACK:
Steven Moreland055d8792018-11-14 12:48:42 -0800418 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700419 case StorageMode::ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800420 if (aspect.value_is_cheap) {
421 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700422 } else {
Steven Moreland055d8792018-11-14 12:48:42 -0800423 return "const " + aspect.cpp_name + "&";
Steven Morelande8a3a192018-09-20 14:14:28 -0700424 }
425 case StorageMode::OUT_ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800426 return aspect.cpp_name + "*";
Steven Morelande8a3a192018-09-20 14:14:28 -0700427 default:
428 AIDL_FATAL(aidl.GetName()) << "Unrecognized mode type: " << static_cast<int>(mode);
429 }
430}
431
Devin Moore83e76982020-09-30 09:32:35 -0700432size_t NdkAlignmentOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
433 // map from NDK type name to the corresponding alignment size
434 static map<string, int> alignment = {
435 {"bool", 1}, {"int8_t", 1}, {"char16_t", 2}, {"double", 8},
436 {"float", 4}, {"int32_t", 4}, {"int64_t", 8},
437 };
438
439 const string& name = NdkNameOf(types, aidl, StorageMode::STACK);
440 if (alignment.find(name) != alignment.end()) {
441 return alignment[name];
442 } else {
443 const auto& definedType = types.TryGetDefinedType(aidl.GetName());
444 AIDL_FATAL_IF(definedType == nullptr, aidl) << "Failed to resolve type.";
445 if (const auto& enumType = definedType->AsEnumDeclaration(); enumType != nullptr) {
446 return NdkAlignmentOf(types, enumType->GetBackingType());
447 }
448 }
449 return 0;
450}
451
Steven Morelande8a3a192018-09-20 14:14:28 -0700452void WriteToParcelFor(const CodeGeneratorContext& c) {
Steven Moreland055d8792018-11-14 12:48:42 -0800453 TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
454 aspect.write_func(c);
Steven Morelande8a3a192018-09-20 14:14:28 -0700455}
456
457void ReadFromParcelFor(const CodeGeneratorContext& c) {
Steven Moreland055d8792018-11-14 12:48:42 -0800458 TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
459 aspect.read_func(c);
Steven Morelande8a3a192018-09-20 14:14:28 -0700460}
461
Jiyong Park965c5b92018-11-21 13:37:15 +0900462std::string NdkArgList(
463 const AidlTypenames& types, const AidlMethod& method,
464 std::function<std::string(const std::string& type, const std::string& name, bool isOut)>
465 formatter) {
Steven Morelandaada3422018-09-20 15:55:33 -0700466 std::vector<std::string> method_arguments;
467 for (const auto& a : method.GetArguments()) {
468 StorageMode mode = a->IsOut() ? StorageMode::OUT_ARGUMENT : StorageMode::ARGUMENT;
Steven Moreland2bea13b2018-10-03 15:12:33 -0700469 std::string type = NdkNameOf(types, a->GetType(), mode);
Steven Morelandaada3422018-09-20 15:55:33 -0700470 std::string name = cpp::BuildVarName(*a);
Jiyong Park965c5b92018-11-21 13:37:15 +0900471 method_arguments.emplace_back(formatter(type, name, a->IsOut()));
Steven Morelandaada3422018-09-20 15:55:33 -0700472 }
473
474 if (method.GetType().GetName() != "void") {
Jiyong Park965c5b92018-11-21 13:37:15 +0900475 std::string type = NdkNameOf(types, method.GetType(), StorageMode::OUT_ARGUMENT);
476 std::string name = "_aidl_return";
477 method_arguments.emplace_back(formatter(type, name, true));
Steven Morelandaada3422018-09-20 15:55:33 -0700478 }
479
480 return Join(method_arguments, ", ");
481}
482
Steven Moreland2bea13b2018-10-03 15:12:33 -0700483std::string NdkMethodDecl(const AidlTypenames& types, const AidlMethod& method,
484 const std::string& clazz) {
Steven Morelandaada3422018-09-20 15:55:33 -0700485 std::string class_prefix = clazz.empty() ? "" : (clazz + "::");
Steven Moreland63404532018-10-08 14:31:00 -0700486 return "::ndk::ScopedAStatus " + class_prefix + method.GetName() + "(" +
Jiyong Park965c5b92018-11-21 13:37:15 +0900487 NdkArgList(types, method, FormatArgForDecl) + ")";
Steven Morelandaada3422018-09-20 15:55:33 -0700488}
489
Steven Morelande8a3a192018-09-20 14:14:28 -0700490} // namespace ndk
491} // namespace aidl
492} // namespace android