blob: f535d10049d83b5737c4eb3477a153cc2c4fa360 [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) {
Jooyung Han690f5842020-12-04 13:02:04 +090073 if (type.IsArray()) {
74 return raw_value;
75 }
76
Will McVickerd7d18df2019-09-12 13:40:50 -070077 if (type.GetName() == "long" && !type.IsArray()) {
Daniel Norman37d43dd2019-09-09 17:22:34 -070078 return raw_value + "L";
79 }
80
Jooyung Han690f5842020-12-04 13:02:04 +090081 if (auto defined_type = type.GetDefinedType(); defined_type) {
82 auto enum_type = defined_type->AsEnumDeclaration();
83 AIDL_FATAL_IF(!enum_type, type) << "Invalid type for \"" << raw_value << "\"";
84 return NdkFullClassName(*enum_type, cpp::ClassNames::RAW) +
85 "::" + raw_value.substr(raw_value.find_last_of('.') + 1);
86 }
87
Daniel Norman37d43dd2019-09-09 17:22:34 -070088 return raw_value;
89};
90
Steven Moreland67caf422018-10-15 12:39:12 -070091static std::function<void(const CodeGeneratorContext& c)> StandardRead(const std::string& name) {
Steven Morelande8a3a192018-09-20 14:14:28 -070092 return [name](const CodeGeneratorContext& c) {
Steven Moreland67caf422018-10-15 12:39:12 -070093 c.writer << name << "(" << c.parcel << ", " << c.var << ")";
Steven Morelande8a3a192018-09-20 14:14:28 -070094 };
95}
Steven Moreland67caf422018-10-15 12:39:12 -070096static std::function<void(const CodeGeneratorContext& c)> StandardWrite(const std::string& name) {
Steven Morelande8a3a192018-09-20 14:14:28 -070097 return [name](const CodeGeneratorContext& c) {
Steven Moreland67caf422018-10-15 12:39:12 -070098 c.writer << name << "(" << c.parcel << ", " << c.var << ")";
99 };
100}
101
Jooyung Han241fdda2020-02-21 21:17:44 +0900102TypeInfo PrimitiveType(const std::string& cpp_name, const std::string& pretty_name,
103 const std::optional<std::string>& cpp_name_for_array_opt = std::nullopt) {
104 std::string cpp_name_for_array = cpp_name_for_array_opt.value_or(cpp_name);
Steven Moreland67caf422018-10-15 12:39:12 -0700105 return TypeInfo{
Steven Moreland055d8792018-11-14 12:48:42 -0800106 .raw =
107 TypeInfo::Aspect{
108 .cpp_name = cpp_name,
109 .value_is_cheap = true,
110 .read_func = StandardRead("AParcel_read" + pretty_name),
111 .write_func = StandardWrite("AParcel_write" + pretty_name),
112 },
113 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
Jooyung Han241fdda2020-02-21 21:17:44 +0900114 .cpp_name = "std::vector<" + cpp_name_for_array + ">",
Steven Moreland055d8792018-11-14 12:48:42 -0800115 .value_is_cheap = false,
116 .read_func = StandardRead("::ndk::AParcel_readVector"),
117 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
118 }),
119 .nullable = nullptr,
Steven Morelandc10ed462018-11-15 14:50:28 -0800120 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
Jooyung Han241fdda2020-02-21 21:17:44 +0900121 .cpp_name = "std::optional<std::vector<" + cpp_name_for_array + ">>",
Steven Morelandc10ed462018-11-15 14:50:28 -0800122 .value_is_cheap = false,
123 .read_func = StandardRead("::ndk::AParcel_readVector"),
124 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
125 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800126 };
127}
128
129TypeInfo InterfaceTypeInfo(const AidlInterface& type) {
130 const std::string clazz = NdkFullClassName(type, cpp::ClassNames::INTERFACE);
131
132 return TypeInfo{
133 .raw =
134 TypeInfo::Aspect{
135 .cpp_name = "std::shared_ptr<" + clazz + ">",
136 .value_is_cheap = false,
Steven Moreland055d8792018-11-14 12:48:42 -0800137 .read_func = StandardRead(clazz + "::readFromParcel"),
138 .write_func = StandardWrite(clazz + "::writeToParcel"),
139 },
140 .array = nullptr,
141 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
142 .cpp_name = "std::shared_ptr<" + clazz + ">",
143 .value_is_cheap = false,
144 .read_func = StandardRead(clazz + "::readFromParcel"),
145 .write_func = StandardWrite(clazz + "::writeToParcel"),
146 }),
147 .nullable_array = nullptr,
148 };
149}
150
Devin Moore53fc99c2020-08-12 08:07:52 -0700151TypeInfo ParcelableTypeInfo(const AidlParcelable& type, const AidlTypeSpecifier& typeSpec,
152 const AidlTypenames& types) {
153 std::string clazz = NdkFullClassName(type, cpp::ClassNames::RAW);
154 std::string template_params = "";
155 if (typeSpec.IsGeneric()) {
156 std::vector<std::string> type_params;
157 for (const auto& parameter : typeSpec.GetTypeParameters()) {
158 type_params.push_back(NdkNameOf(types, *parameter, StorageMode::STACK));
159 }
160 clazz += base::StringPrintf("<%s>", base::Join(type_params, ", ").c_str());
161 }
Jooyung Han01720ed2021-08-13 07:46:07 +0900162 const std::string nullable = typeSpec.IsHeapNullable() ? "std::unique_ptr" : "std::optional";
Steven Moreland055d8792018-11-14 12:48:42 -0800163 return TypeInfo{
164 .raw =
165 TypeInfo::Aspect{
166 .cpp_name = clazz,
167 .value_is_cheap = false,
Steven Moreland4348f9a2019-12-16 16:33:01 -0800168 .read_func = StandardRead("::ndk::AParcel_readParcelable"),
169 .write_func = StandardWrite("::ndk::AParcel_writeParcelable"),
Steven Moreland055d8792018-11-14 12:48:42 -0800170 },
Steven Moreland5ecec6b2018-12-11 18:56:41 -0800171 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
172 .cpp_name = "std::vector<" + clazz + ">",
173 .value_is_cheap = false,
174 .read_func = StandardRead("::ndk::AParcel_readVector"),
175 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
176 }),
Steven Moreland18f75262019-12-19 16:58:55 -0800177 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
Jooyung Han01720ed2021-08-13 07:46:07 +0900178 .cpp_name = nullable + "<" + clazz + ">",
Steven Moreland18f75262019-12-19 16:58:55 -0800179 .value_is_cheap = false,
180 .read_func = StandardRead("::ndk::AParcel_readNullableParcelable"),
181 .write_func = StandardWrite("::ndk::AParcel_writeNullableParcelable"),
182 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800183 .nullable_array = nullptr,
Steven Morelande8a3a192018-09-20 14:14:28 -0700184 };
185}
186
Daniel Norman37d43dd2019-09-09 17:22:34 -0700187TypeInfo EnumDeclarationTypeInfo(const AidlEnumDeclaration& enum_decl) {
Steven Morelandb8df37d2019-11-21 12:33:24 -0800188 const std::string clazz = NdkFullClassName(enum_decl, cpp::ClassNames::RAW);
Daniel Norman37d43dd2019-09-09 17:22:34 -0700189
190 static map<std::string, std::string> kAParcelTypeNameMap = {
191 {"byte", "Byte"},
192 {"int", "Int32"},
193 {"long", "Int64"},
194 };
195 auto aparcel_name_it = kAParcelTypeNameMap.find(enum_decl.GetBackingType().GetName());
Steven Moreland21780812020-09-11 01:29:45 +0000196 AIDL_FATAL_IF(aparcel_name_it == kAParcelTypeNameMap.end(), enum_decl);
Daniel Norman37d43dd2019-09-09 17:22:34 -0700197 const std::string aparcel_name = aparcel_name_it->second;
198
199 const std::string backing_type_name =
200 NdkNameOf(AidlTypenames(), enum_decl.GetBackingType(), StorageMode::STACK);
201
202 return TypeInfo{
203 .raw = TypeInfo::Aspect{
204 .cpp_name = clazz,
205 .value_is_cheap = true,
206 .read_func =
207 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
208 c.writer << "AParcel_read" << aparcel_name << "(" << c.parcel
209 << ", reinterpret_cast<" << backing_type_name << "*>(" << c.var << "))";
210 },
211 .write_func =
212 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
213 c.writer << "AParcel_write" << aparcel_name << "(" << c.parcel << ", static_cast<"
214 << backing_type_name << ">(" << c.var << "))";
215 },
216 },
Daniel Normanee8674f2019-09-20 16:07:00 -0700217 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
218 .cpp_name = "std::vector<" + clazz + ">",
219 .value_is_cheap = false,
220 .read_func =
221 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
222 c.writer << "AParcel_read" << aparcel_name << "Array(" << c.parcel
223 << ", static_cast<void*>(" << c.var
224 << "), ndk::AParcel_stdVectorAllocator<" << backing_type_name << ">)";
225 },
226 .write_func =
227 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
228 c.writer << "AParcel_write" << aparcel_name << "Array(" << c.parcel
229 << ", reinterpret_cast<const " << backing_type_name << "*>(" << c.var
230 << ".data()), " << c.var << ".size())";
231 },
232 }),
Daniel Norman37d43dd2019-09-09 17:22:34 -0700233 .nullable = nullptr,
Daniel Normanee8674f2019-09-20 16:07:00 -0700234 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
235 .cpp_name = "std::optional<std::vector<" + clazz + ">>",
236 .value_is_cheap = false,
237 .read_func =
238 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
239 c.writer << "AParcel_read" << aparcel_name << "Array(" << c.parcel
240 << ", static_cast<void*>(" << c.var
241 << "), ndk::AParcel_nullableStdVectorAllocator<" << backing_type_name
242 << ">)";
243 },
244 .write_func =
245 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
246 // If the var exists, use writeArray with data() and size().
247 // Otherwise, use nullptr and -1.
248 c.writer << "AParcel_write" << aparcel_name << "Array(" << c.parcel << ", ("
249 << c.var << " ? reinterpret_cast<const " << backing_type_name << "*>("
250 << c.var << "->data()) : nullptr)"
251 << ", (" << c.var << " ? " << c.var << "->size() : -1))";
252 },
253 }),
Daniel Norman37d43dd2019-09-09 17:22:34 -0700254 };
255}
256
257// map from AIDL built-in type name to the corresponding Ndk type info
Steven Morelande8a3a192018-09-20 14:14:28 -0700258static map<std::string, TypeInfo> kNdkTypeInfoMap = {
Steven Moreland055d8792018-11-14 12:48:42 -0800259 {"void", TypeInfo{{"void", true, nullptr, nullptr}, nullptr, nullptr, nullptr}},
Steven Moreland67caf422018-10-15 12:39:12 -0700260 {"boolean", PrimitiveType("bool", "Bool")},
Jooyung Han241fdda2020-02-21 21:17:44 +0900261 {"byte", PrimitiveType("int8_t", "Byte", "uint8_t")},
Steven Moreland67caf422018-10-15 12:39:12 -0700262 {"char", PrimitiveType("char16_t", "Char")},
263 {"int", PrimitiveType("int32_t", "Int32")},
264 {"long", PrimitiveType("int64_t", "Int64")},
265 {"float", PrimitiveType("float", "Float")},
266 {"double", PrimitiveType("double", "Double")},
Steven Moreland63404532018-10-08 14:31:00 -0700267 {"String",
Steven Moreland055d8792018-11-14 12:48:42 -0800268 TypeInfo{
269 .raw =
270 TypeInfo::Aspect{
271 .cpp_name = "std::string",
272 .value_is_cheap = false,
273 .read_func = StandardRead("::ndk::AParcel_readString"),
274 .write_func = StandardWrite("::ndk::AParcel_writeString"),
275 },
276 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
277 .cpp_name = "std::vector<std::string>",
278 .value_is_cheap = false,
279 .read_func = StandardRead("::ndk::AParcel_readVector"),
280 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
281 }),
Steven Moreland18ed9782018-11-14 17:08:09 -0800282 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
283 .cpp_name = "std::optional<std::string>",
284 .value_is_cheap = false,
285 .read_func = StandardRead("::ndk::AParcel_readString"),
286 .write_func = StandardWrite("::ndk::AParcel_writeString"),
287 }),
288 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
289 .cpp_name = "std::optional<std::vector<std::optional<std::string>>>",
290 .value_is_cheap = false,
291 .read_func = StandardRead("::ndk::AParcel_readVector"),
292 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
293 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800294 }},
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900295 // TODO(b/136048684) {"Map", ""},
Steven Moreland055d8792018-11-14 12:48:42 -0800296 {"IBinder",
297 TypeInfo{
298 .raw =
299 TypeInfo::Aspect{
300 .cpp_name = "::ndk::SpAIBinder",
301 .value_is_cheap = false,
Steven Morelande1048a32018-11-16 12:52:17 -0800302 .read_func = StandardRead("::ndk::AParcel_readRequiredStrongBinder"),
303 .write_func = StandardRead("::ndk::AParcel_writeRequiredStrongBinder"),
Steven Moreland055d8792018-11-14 12:48:42 -0800304 },
305 .array = nullptr,
306 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
307 .cpp_name = "::ndk::SpAIBinder",
308 .value_is_cheap = false,
Steven Morelande1048a32018-11-16 12:52:17 -0800309 .read_func = StandardRead("::ndk::AParcel_readNullableStrongBinder"),
310 .write_func = StandardRead("::ndk::AParcel_writeNullableStrongBinder"),
Steven Moreland055d8792018-11-14 12:48:42 -0800311 }),
312 .nullable_array = nullptr,
313 }},
Steven Moreland055d8792018-11-14 12:48:42 -0800314 {"ParcelFileDescriptor",
315 TypeInfo{
316 .raw =
317 TypeInfo::Aspect{
318 .cpp_name = "::ndk::ScopedFileDescriptor",
319 .value_is_cheap = false,
Steven Moreland962c60d2018-11-16 15:19:27 -0800320 .read_func = StandardRead("::ndk::AParcel_readRequiredParcelFileDescriptor"),
321 .write_func = StandardRead("::ndk::AParcel_writeRequiredParcelFileDescriptor"),
Steven Moreland055d8792018-11-14 12:48:42 -0800322 },
Jeongik Chacc8dd1d2019-11-14 10:22:57 +0900323 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
324 .cpp_name = "std::vector<::ndk::ScopedFileDescriptor>",
325 .value_is_cheap = false,
326 .read_func = StandardRead("::ndk::AParcel_readVector"),
327 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
328 }),
Steven Moreland962c60d2018-11-16 15:19:27 -0800329 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
330 .cpp_name = "::ndk::ScopedFileDescriptor",
331 .value_is_cheap = false,
332 .read_func = StandardRead("::ndk::AParcel_readNullableParcelFileDescriptor"),
333 .write_func = StandardRead("::ndk::AParcel_writeNullableParcelFileDescriptor"),
334 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800335 .nullable_array = nullptr,
336 }},
Jeongik Cha8f02a532020-10-14 00:16:28 +0900337 {"ParcelableHolder",
338 TypeInfo{
339 .raw =
340 TypeInfo::Aspect{
341 .cpp_name = "::ndk::AParcelableHolder",
342 .value_is_cheap = false,
343 .read_func = StandardRead("::ndk::AParcel_readParcelable"),
344 .write_func = StandardWrite("::ndk::AParcel_writeParcelable"),
345 },
346 .array = nullptr,
347 .nullable = nullptr,
348 .nullable_array = nullptr,
349 }},
Steven Morelande8a3a192018-09-20 14:14:28 -0700350};
351
Steven Moreland055d8792018-11-14 12:48:42 -0800352static TypeInfo::Aspect GetTypeAspect(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
Steven Moreland21780812020-09-11 01:29:45 +0000353 AIDL_FATAL_IF(!aidl.IsResolved(), aidl) << aidl.ToString();
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900354 auto& aidl_name = aidl.GetName();
Steven Moreland1cb099e2018-10-17 16:31:08 -0700355
Steven Moreland67caf422018-10-15 12:39:12 -0700356 TypeInfo info;
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900357
358 // TODO(b/136048684): For now, List<T> is converted to T[].(Both are using vector<T>)
359 if (aidl_name == "List") {
Steven Morelandfe52c9f2019-11-27 19:04:48 -0800360 AIDL_FATAL_IF(!aidl.IsGeneric(), aidl) << "List must be generic type.";
361 AIDL_FATAL_IF(aidl.GetTypeParameters().size() != 1, aidl)
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900362 << "List can accept only one type parameter.";
Devin Moore53fc99c2020-08-12 08:07:52 -0700363 const auto& type_param = aidl.GetTypeParameters()[0];
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900364 // TODO(b/136048684) AIDL doesn't support nested type parameter yet.
365 AIDL_FATAL_IF(type_param->IsGeneric(), aidl) << "AIDL doesn't support nested type parameter";
366
367 AidlTypeSpecifier array_type =
Jooyung Han13f1fa52021-06-11 18:06:12 +0900368 AidlTypeSpecifier(AIDL_LOCATION_HERE, type_param->GetName(), true /* isArray */,
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900369 nullptr /* type_params */, aidl.GetComments());
Jooyung Han13f1fa52021-06-11 18:06:12 +0900370 if (!(array_type.Resolve(types, nullptr) && array_type.CheckValid(types))) {
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900371 AIDL_FATAL(aidl) << "The type parameter is wrong.";
372 }
373 return GetTypeAspect(types, array_type);
374 }
375
Steven Moreland67caf422018-10-15 12:39:12 -0700376 if (AidlTypenames::IsBuiltinTypename(aidl_name)) {
377 auto it = kNdkTypeInfoMap.find(aidl_name);
Steven Moreland21780812020-09-11 01:29:45 +0000378 AIDL_FATAL_IF(it == kNdkTypeInfoMap.end(), aidl_name);
Steven Moreland67caf422018-10-15 12:39:12 -0700379 info = it->second;
380 } else {
381 const AidlDefinedType* type = types.TryGetDefinedType(aidl_name);
Steven Moreland67caf422018-10-15 12:39:12 -0700382 AIDL_FATAL_IF(type == nullptr, aidl_name) << "Unrecognized type.";
383
Daniel Norman37d43dd2019-09-09 17:22:34 -0700384 if (const AidlInterface* intf = type->AsInterface(); intf != nullptr) {
385 info = InterfaceTypeInfo(*intf);
386 } else if (const AidlParcelable* parcelable = type->AsParcelable(); parcelable != nullptr) {
Devin Moore53fc99c2020-08-12 08:07:52 -0700387 info = ParcelableTypeInfo(*parcelable, aidl, types);
Daniel Norman37d43dd2019-09-09 17:22:34 -0700388 } else if (const AidlEnumDeclaration* enum_decl = type->AsEnumDeclaration();
389 enum_decl != nullptr) {
390 info = EnumDeclarationTypeInfo(*enum_decl);
Steven Moreland67caf422018-10-15 12:39:12 -0700391 } else {
392 AIDL_FATAL(aidl_name) << "Unrecognized type";
393 }
394 }
395
Steven Moreland055d8792018-11-14 12:48:42 -0800396 if (aidl.IsArray()) {
397 if (aidl.IsNullable()) {
Steven Morelandd59e3172020-05-11 16:42:09 -0700398 AIDL_FATAL_IF(info.nullable_array == nullptr, aidl)
399 << "Unsupported type in NDK Backend: " << aidl.ToString();
Steven Moreland055d8792018-11-14 12:48:42 -0800400 return *info.nullable_array;
401 }
Steven Morelandd59e3172020-05-11 16:42:09 -0700402 AIDL_FATAL_IF(info.array == nullptr, aidl)
403 << "Unsupported type in NDK Backend: " << aidl.ToString();
Steven Moreland055d8792018-11-14 12:48:42 -0800404 return *info.array;
405 }
Steven Moreland1cb099e2018-10-17 16:31:08 -0700406
Steven Moreland055d8792018-11-14 12:48:42 -0800407 if (aidl.IsNullable()) {
Steven Morelandd59e3172020-05-11 16:42:09 -0700408 AIDL_FATAL_IF(info.nullable == nullptr, aidl)
409 << "Unsupported type in NDK Backend: " << aidl.ToString();
Steven Moreland055d8792018-11-14 12:48:42 -0800410 return *info.nullable;
411 }
412
413 return info.raw;
Steven Moreland67caf422018-10-15 12:39:12 -0700414}
415
Steven Moreland2bea13b2018-10-03 15:12:33 -0700416std::string NdkFullClassName(const AidlDefinedType& type, cpp::ClassNames name) {
Steven Moreland63404532018-10-08 14:31:00 -0700417 std::vector<std::string> pieces = {"::aidl"};
Steven Moreland2bea13b2018-10-03 15:12:33 -0700418 std::vector<std::string> package = type.GetSplitPackage();
419 pieces.insert(pieces.end(), package.begin(), package.end());
420 pieces.push_back(cpp::ClassName(type, name));
421
422 return Join(pieces, "::");
423}
424
425std::string NdkNameOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl, StorageMode mode) {
Steven Moreland055d8792018-11-14 12:48:42 -0800426 TypeInfo::Aspect aspect = GetTypeAspect(types, aidl);
Steven Morelandeb38ee72018-10-15 14:20:04 -0700427
Steven Morelande8a3a192018-09-20 14:14:28 -0700428 switch (mode) {
429 case StorageMode::STACK:
Steven Moreland055d8792018-11-14 12:48:42 -0800430 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700431 case StorageMode::ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800432 if (aspect.value_is_cheap) {
433 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700434 } else {
Steven Moreland055d8792018-11-14 12:48:42 -0800435 return "const " + aspect.cpp_name + "&";
Steven Morelande8a3a192018-09-20 14:14:28 -0700436 }
437 case StorageMode::OUT_ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800438 return aspect.cpp_name + "*";
Steven Morelande8a3a192018-09-20 14:14:28 -0700439 default:
440 AIDL_FATAL(aidl.GetName()) << "Unrecognized mode type: " << static_cast<int>(mode);
441 }
442}
443
Devin Moore83e76982020-09-30 09:32:35 -0700444size_t NdkAlignmentOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
445 // map from NDK type name to the corresponding alignment size
446 static map<string, int> alignment = {
447 {"bool", 1}, {"int8_t", 1}, {"char16_t", 2}, {"double", 8},
448 {"float", 4}, {"int32_t", 4}, {"int64_t", 8},
449 };
450
451 const string& name = NdkNameOf(types, aidl, StorageMode::STACK);
452 if (alignment.find(name) != alignment.end()) {
453 return alignment[name];
454 } else {
455 const auto& definedType = types.TryGetDefinedType(aidl.GetName());
456 AIDL_FATAL_IF(definedType == nullptr, aidl) << "Failed to resolve type.";
457 if (const auto& enumType = definedType->AsEnumDeclaration(); enumType != nullptr) {
458 return NdkAlignmentOf(types, enumType->GetBackingType());
459 }
460 }
461 return 0;
462}
463
Steven Morelande8a3a192018-09-20 14:14:28 -0700464void WriteToParcelFor(const CodeGeneratorContext& c) {
Steven Moreland055d8792018-11-14 12:48:42 -0800465 TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
466 aspect.write_func(c);
Steven Morelande8a3a192018-09-20 14:14:28 -0700467}
468
469void ReadFromParcelFor(const CodeGeneratorContext& c) {
Steven Moreland055d8792018-11-14 12:48:42 -0800470 TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
471 aspect.read_func(c);
Steven Morelande8a3a192018-09-20 14:14:28 -0700472}
473
Jiyong Park965c5b92018-11-21 13:37:15 +0900474std::string NdkArgList(
475 const AidlTypenames& types, const AidlMethod& method,
476 std::function<std::string(const std::string& type, const std::string& name, bool isOut)>
477 formatter) {
Steven Morelandaada3422018-09-20 15:55:33 -0700478 std::vector<std::string> method_arguments;
479 for (const auto& a : method.GetArguments()) {
480 StorageMode mode = a->IsOut() ? StorageMode::OUT_ARGUMENT : StorageMode::ARGUMENT;
Steven Moreland2bea13b2018-10-03 15:12:33 -0700481 std::string type = NdkNameOf(types, a->GetType(), mode);
Steven Morelandaada3422018-09-20 15:55:33 -0700482 std::string name = cpp::BuildVarName(*a);
Jiyong Park965c5b92018-11-21 13:37:15 +0900483 method_arguments.emplace_back(formatter(type, name, a->IsOut()));
Steven Morelandaada3422018-09-20 15:55:33 -0700484 }
485
486 if (method.GetType().GetName() != "void") {
Jiyong Park965c5b92018-11-21 13:37:15 +0900487 std::string type = NdkNameOf(types, method.GetType(), StorageMode::OUT_ARGUMENT);
488 std::string name = "_aidl_return";
489 method_arguments.emplace_back(formatter(type, name, true));
Steven Morelandaada3422018-09-20 15:55:33 -0700490 }
491
492 return Join(method_arguments, ", ");
493}
494
Steven Moreland2bea13b2018-10-03 15:12:33 -0700495std::string NdkMethodDecl(const AidlTypenames& types, const AidlMethod& method,
496 const std::string& clazz) {
Steven Morelandaada3422018-09-20 15:55:33 -0700497 std::string class_prefix = clazz.empty() ? "" : (clazz + "::");
Steven Moreland63404532018-10-08 14:31:00 -0700498 return "::ndk::ScopedAStatus " + class_prefix + method.GetName() + "(" +
Jiyong Park965c5b92018-11-21 13:37:15 +0900499 NdkArgList(types, method, FormatArgForDecl) + ")";
Steven Morelandaada3422018-09-20 15:55:33 -0700500}
501
Steven Morelande8a3a192018-09-20 14:14:28 -0700502} // namespace ndk
503} // namespace aidl
504} // namespace android