blob: 663a17e86bd627897be82b4d66ed6289fd171d44 [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
21#include <android-base/strings.h>
22
23#include <functional>
24
25using ::android::base::Join;
Steven Morelande8a3a192018-09-20 14:14:28 -070026
27namespace android {
28namespace aidl {
29namespace ndk {
30
Steven Moreland7c933372018-10-11 15:20:04 -070031std::string NdkHeaderFile(const AidlDefinedType& defined_type, cpp::ClassNames name,
32 bool use_os_sep) {
33 char seperator = (use_os_sep) ? OS_PATH_SEPARATOR : '/';
34 return std::string("aidl") + seperator + cpp::HeaderFile(defined_type, name, use_os_sep);
35}
36
Steven Moreland055d8792018-11-14 12:48:42 -080037// This represents a type in AIDL (e.g. 'String' which can be referenced in multiple ways)
Steven Morelande8a3a192018-09-20 14:14:28 -070038struct TypeInfo {
Steven Moreland055d8792018-11-14 12:48:42 -080039 struct Aspect {
40 // name of the type in C++ output
41 std::string cpp_name;
42 // whether to prefer 'value type' over 'const&'
43 bool value_is_cheap;
Steven Morelande8a3a192018-09-20 14:14:28 -070044
Steven Moreland055d8792018-11-14 12:48:42 -080045 std::function<void(const CodeGeneratorContext& c)> read_func;
46 std::function<void(const CodeGeneratorContext& c)> write_func;
47 };
Steven Morelandeb38ee72018-10-15 14:20:04 -070048
Steven Moreland055d8792018-11-14 12:48:42 -080049 // e.g. 'String'
50 Aspect raw;
51
52 // e.g. 'String[]'
53 std::shared_ptr<Aspect> array;
54
55 // note: Nullable types do not exist in Java. For most Java types, the type is split into a
56 // nullable and non-nullable variant. This is because C++ types are more usually non-nullable, but
57 // everything in Java is non-nullable. This does mean that some Java interfaces may have to have
58 // '@nullable' added to them in order to function as expected w/ the NDK. It also means that some
59 // transactions will be allowed in Java which are not allowed in C++. However, in Java, if a null
60 // is ignored, it will just result in a NullPointerException and be delivered to the other side.
61 // C++ does not have this same capacity (in Android), and so instead, we distinguish nullability
62 // in the type system.
63
64 // e.g. '@nullable String'
65 std::shared_ptr<Aspect> nullable;
66
67 // e.g. '@nullable String[]'
68 std::shared_ptr<Aspect> nullable_array;
Steven Morelande8a3a192018-09-20 14:14:28 -070069};
70
Daniel Norman37d43dd2019-09-09 17:22:34 -070071std::string ConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value) {
Will McVickerd7d18df2019-09-12 13:40:50 -070072 if (type.GetName() == "long" && !type.IsArray()) {
Daniel Norman37d43dd2019-09-09 17:22:34 -070073 return raw_value + "L";
74 }
75
76 return raw_value;
77};
78
Steven Moreland67caf422018-10-15 12:39:12 -070079static std::function<void(const CodeGeneratorContext& c)> StandardRead(const std::string& name) {
Steven Morelande8a3a192018-09-20 14:14:28 -070080 return [name](const CodeGeneratorContext& c) {
Steven Moreland67caf422018-10-15 12:39:12 -070081 c.writer << name << "(" << c.parcel << ", " << c.var << ")";
Steven Morelande8a3a192018-09-20 14:14:28 -070082 };
83}
Steven Moreland67caf422018-10-15 12:39:12 -070084static std::function<void(const CodeGeneratorContext& c)> StandardWrite(const std::string& name) {
Steven Morelande8a3a192018-09-20 14:14:28 -070085 return [name](const CodeGeneratorContext& c) {
Steven Moreland67caf422018-10-15 12:39:12 -070086 c.writer << name << "(" << c.parcel << ", " << c.var << ")";
87 };
88}
89
90TypeInfo PrimitiveType(const std::string& cpp_name, const std::string& pretty_name) {
91 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{
100 .cpp_name = "std::vector<" + cpp_name + ">",
101 .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{
107 .cpp_name = "std::optional<std::vector<" + cpp_name + ">>",
108 .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 },
126 .array = nullptr,
127 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
128 .cpp_name = "std::shared_ptr<" + clazz + ">",
129 .value_is_cheap = false,
130 .read_func = StandardRead(clazz + "::readFromParcel"),
131 .write_func = StandardWrite(clazz + "::writeToParcel"),
132 }),
133 .nullable_array = nullptr,
134 };
135}
136
137TypeInfo ParcelableTypeInfo(const AidlParcelable& type) {
Steven Morelandb8df37d2019-11-21 12:33:24 -0800138 const std::string clazz = NdkFullClassName(type, cpp::ClassNames::RAW);
Steven Moreland055d8792018-11-14 12:48:42 -0800139
140 return TypeInfo{
141 .raw =
142 TypeInfo::Aspect{
143 .cpp_name = clazz,
144 .value_is_cheap = false,
Steven Moreland4348f9a2019-12-16 16:33:01 -0800145 .read_func = StandardRead("::ndk::AParcel_readParcelable"),
146 .write_func = StandardWrite("::ndk::AParcel_writeParcelable"),
Steven Moreland055d8792018-11-14 12:48:42 -0800147 },
Steven Moreland5ecec6b2018-12-11 18:56:41 -0800148 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
149 .cpp_name = "std::vector<" + clazz + ">",
150 .value_is_cheap = false,
151 .read_func = StandardRead("::ndk::AParcel_readVector"),
152 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
153 }),
Steven Moreland18f75262019-12-19 16:58:55 -0800154 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
155 .cpp_name = "std::optional<" + clazz + ">",
156 .value_is_cheap = false,
157 .read_func = StandardRead("::ndk::AParcel_readNullableParcelable"),
158 .write_func = StandardWrite("::ndk::AParcel_writeNullableParcelable"),
159 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800160 .nullable_array = nullptr,
Steven Morelande8a3a192018-09-20 14:14:28 -0700161 };
162}
163
Daniel Norman37d43dd2019-09-09 17:22:34 -0700164TypeInfo EnumDeclarationTypeInfo(const AidlEnumDeclaration& enum_decl) {
Steven Morelandb8df37d2019-11-21 12:33:24 -0800165 const std::string clazz = NdkFullClassName(enum_decl, cpp::ClassNames::RAW);
Daniel Norman37d43dd2019-09-09 17:22:34 -0700166
167 static map<std::string, std::string> kAParcelTypeNameMap = {
168 {"byte", "Byte"},
169 {"int", "Int32"},
170 {"long", "Int64"},
171 };
172 auto aparcel_name_it = kAParcelTypeNameMap.find(enum_decl.GetBackingType().GetName());
173 CHECK(aparcel_name_it != kAParcelTypeNameMap.end());
174 const std::string aparcel_name = aparcel_name_it->second;
175
176 const std::string backing_type_name =
177 NdkNameOf(AidlTypenames(), enum_decl.GetBackingType(), StorageMode::STACK);
178
179 return TypeInfo{
180 .raw = TypeInfo::Aspect{
181 .cpp_name = clazz,
182 .value_is_cheap = true,
183 .read_func =
184 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
185 c.writer << "AParcel_read" << aparcel_name << "(" << c.parcel
186 << ", reinterpret_cast<" << backing_type_name << "*>(" << c.var << "))";
187 },
188 .write_func =
189 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
190 c.writer << "AParcel_write" << aparcel_name << "(" << c.parcel << ", static_cast<"
191 << backing_type_name << ">(" << c.var << "))";
192 },
193 },
Daniel Normanee8674f2019-09-20 16:07:00 -0700194 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
195 .cpp_name = "std::vector<" + clazz + ">",
196 .value_is_cheap = false,
197 .read_func =
198 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
199 c.writer << "AParcel_read" << aparcel_name << "Array(" << c.parcel
200 << ", static_cast<void*>(" << c.var
201 << "), ndk::AParcel_stdVectorAllocator<" << backing_type_name << ">)";
202 },
203 .write_func =
204 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
205 c.writer << "AParcel_write" << aparcel_name << "Array(" << c.parcel
206 << ", reinterpret_cast<const " << backing_type_name << "*>(" << c.var
207 << ".data()), " << c.var << ".size())";
208 },
209 }),
Daniel Norman37d43dd2019-09-09 17:22:34 -0700210 .nullable = nullptr,
Daniel Normanee8674f2019-09-20 16:07:00 -0700211 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
212 .cpp_name = "std::optional<std::vector<" + clazz + ">>",
213 .value_is_cheap = false,
214 .read_func =
215 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
216 c.writer << "AParcel_read" << aparcel_name << "Array(" << c.parcel
217 << ", static_cast<void*>(" << c.var
218 << "), ndk::AParcel_nullableStdVectorAllocator<" << backing_type_name
219 << ">)";
220 },
221 .write_func =
222 [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
223 // If the var exists, use writeArray with data() and size().
224 // Otherwise, use nullptr and -1.
225 c.writer << "AParcel_write" << aparcel_name << "Array(" << c.parcel << ", ("
226 << c.var << " ? reinterpret_cast<const " << backing_type_name << "*>("
227 << c.var << "->data()) : nullptr)"
228 << ", (" << c.var << " ? " << c.var << "->size() : -1))";
229 },
230 }),
Daniel Norman37d43dd2019-09-09 17:22:34 -0700231 };
232}
233
234// map from AIDL built-in type name to the corresponding Ndk type info
Steven Morelande8a3a192018-09-20 14:14:28 -0700235static map<std::string, TypeInfo> kNdkTypeInfoMap = {
Steven Moreland055d8792018-11-14 12:48:42 -0800236 {"void", TypeInfo{{"void", true, nullptr, nullptr}, nullptr, nullptr, nullptr}},
Steven Moreland67caf422018-10-15 12:39:12 -0700237 {"boolean", PrimitiveType("bool", "Bool")},
238 {"byte", PrimitiveType("int8_t", "Byte")},
239 {"char", PrimitiveType("char16_t", "Char")},
240 {"int", PrimitiveType("int32_t", "Int32")},
241 {"long", PrimitiveType("int64_t", "Int64")},
242 {"float", PrimitiveType("float", "Float")},
243 {"double", PrimitiveType("double", "Double")},
Steven Moreland63404532018-10-08 14:31:00 -0700244 {"String",
Steven Moreland055d8792018-11-14 12:48:42 -0800245 TypeInfo{
246 .raw =
247 TypeInfo::Aspect{
248 .cpp_name = "std::string",
249 .value_is_cheap = false,
250 .read_func = StandardRead("::ndk::AParcel_readString"),
251 .write_func = StandardWrite("::ndk::AParcel_writeString"),
252 },
253 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
254 .cpp_name = "std::vector<std::string>",
255 .value_is_cheap = false,
256 .read_func = StandardRead("::ndk::AParcel_readVector"),
257 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
258 }),
Steven Moreland18ed9782018-11-14 17:08:09 -0800259 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
260 .cpp_name = "std::optional<std::string>",
261 .value_is_cheap = false,
262 .read_func = StandardRead("::ndk::AParcel_readString"),
263 .write_func = StandardWrite("::ndk::AParcel_writeString"),
264 }),
265 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
266 .cpp_name = "std::optional<std::vector<std::optional<std::string>>>",
267 .value_is_cheap = false,
268 .read_func = StandardRead("::ndk::AParcel_readVector"),
269 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
270 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800271 }},
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900272 // TODO(b/136048684) {"Map", ""},
Steven Moreland055d8792018-11-14 12:48:42 -0800273 {"IBinder",
274 TypeInfo{
275 .raw =
276 TypeInfo::Aspect{
277 .cpp_name = "::ndk::SpAIBinder",
278 .value_is_cheap = false,
Steven Morelande1048a32018-11-16 12:52:17 -0800279 .read_func = StandardRead("::ndk::AParcel_readRequiredStrongBinder"),
280 .write_func = StandardRead("::ndk::AParcel_writeRequiredStrongBinder"),
Steven Moreland055d8792018-11-14 12:48:42 -0800281 },
282 .array = nullptr,
283 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
284 .cpp_name = "::ndk::SpAIBinder",
285 .value_is_cheap = false,
Steven Morelande1048a32018-11-16 12:52:17 -0800286 .read_func = StandardRead("::ndk::AParcel_readNullableStrongBinder"),
287 .write_func = StandardRead("::ndk::AParcel_writeNullableStrongBinder"),
Steven Moreland055d8792018-11-14 12:48:42 -0800288 }),
289 .nullable_array = nullptr,
290 }},
Steven Moreland055d8792018-11-14 12:48:42 -0800291 {"ParcelFileDescriptor",
292 TypeInfo{
293 .raw =
294 TypeInfo::Aspect{
295 .cpp_name = "::ndk::ScopedFileDescriptor",
296 .value_is_cheap = false,
Steven Moreland962c60d2018-11-16 15:19:27 -0800297 .read_func = StandardRead("::ndk::AParcel_readRequiredParcelFileDescriptor"),
298 .write_func = StandardRead("::ndk::AParcel_writeRequiredParcelFileDescriptor"),
Steven Moreland055d8792018-11-14 12:48:42 -0800299 },
Jeongik Chacc8dd1d2019-11-14 10:22:57 +0900300 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
301 .cpp_name = "std::vector<::ndk::ScopedFileDescriptor>",
302 .value_is_cheap = false,
303 .read_func = StandardRead("::ndk::AParcel_readVector"),
304 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
305 }),
Steven Moreland962c60d2018-11-16 15:19:27 -0800306 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
307 .cpp_name = "::ndk::ScopedFileDescriptor",
308 .value_is_cheap = false,
309 .read_func = StandardRead("::ndk::AParcel_readNullableParcelFileDescriptor"),
310 .write_func = StandardRead("::ndk::AParcel_writeNullableParcelFileDescriptor"),
311 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800312 .nullable_array = nullptr,
313 }},
Steven Morelande8a3a192018-09-20 14:14:28 -0700314};
315
Steven Moreland055d8792018-11-14 12:48:42 -0800316static TypeInfo::Aspect GetTypeAspect(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
Steven Moreland1cb099e2018-10-17 16:31:08 -0700317 CHECK(aidl.IsResolved()) << aidl.ToString();
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900318 auto& aidl_name = aidl.GetName();
Steven Moreland1cb099e2018-10-17 16:31:08 -0700319
Steven Moreland67caf422018-10-15 12:39:12 -0700320 TypeInfo info;
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900321
322 // TODO(b/136048684): For now, List<T> is converted to T[].(Both are using vector<T>)
323 if (aidl_name == "List") {
Steven Morelandfe52c9f2019-11-27 19:04:48 -0800324 AIDL_FATAL_IF(!aidl.IsGeneric(), aidl) << "List must be generic type.";
325 AIDL_FATAL_IF(aidl.GetTypeParameters().size() != 1, aidl)
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900326 << "List can accept only one type parameter.";
327 auto& type_param = aidl.GetTypeParameters()[0];
328 // TODO(b/136048684) AIDL doesn't support nested type parameter yet.
329 AIDL_FATAL_IF(type_param->IsGeneric(), aidl) << "AIDL doesn't support nested type parameter";
330
331 AidlTypeSpecifier array_type =
332 AidlTypeSpecifier(AIDL_LOCATION_HERE, type_param->GetUnresolvedName(), true /* isArray */,
333 nullptr /* type_params */, aidl.GetComments());
334 if (!(array_type.Resolve(types) && array_type.CheckValid(types))) {
335 AIDL_FATAL(aidl) << "The type parameter is wrong.";
336 }
337 return GetTypeAspect(types, array_type);
338 }
339
Steven Morelanda6a0b752020-01-16 15:01:09 -0800340 // All generic types should be handled above.
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900341 AIDL_FATAL_IF(aidl.IsGeneric(), aidl);
342
Steven Moreland67caf422018-10-15 12:39:12 -0700343 if (AidlTypenames::IsBuiltinTypename(aidl_name)) {
344 auto it = kNdkTypeInfoMap.find(aidl_name);
345 CHECK(it != kNdkTypeInfoMap.end());
346 info = it->second;
347 } else {
348 const AidlDefinedType* type = types.TryGetDefinedType(aidl_name);
Steven Moreland67caf422018-10-15 12:39:12 -0700349 AIDL_FATAL_IF(type == nullptr, aidl_name) << "Unrecognized type.";
350
Daniel Norman37d43dd2019-09-09 17:22:34 -0700351 if (const AidlInterface* intf = type->AsInterface(); intf != nullptr) {
352 info = InterfaceTypeInfo(*intf);
353 } else if (const AidlParcelable* parcelable = type->AsParcelable(); parcelable != nullptr) {
354 info = ParcelableTypeInfo(*parcelable);
355 } else if (const AidlEnumDeclaration* enum_decl = type->AsEnumDeclaration();
356 enum_decl != nullptr) {
357 info = EnumDeclarationTypeInfo(*enum_decl);
Steven Moreland67caf422018-10-15 12:39:12 -0700358 } else {
359 AIDL_FATAL(aidl_name) << "Unrecognized type";
360 }
361 }
362
Steven Moreland055d8792018-11-14 12:48:42 -0800363 if (aidl.IsArray()) {
364 if (aidl.IsNullable()) {
365 AIDL_FATAL_IF(info.nullable_array == nullptr, aidl) << "Unsupported type in NDK Backend.";
366 return *info.nullable_array;
367 }
368 AIDL_FATAL_IF(info.array == nullptr, aidl) << "Unsupported type in NDK Backend.";
369 return *info.array;
370 }
Steven Moreland1cb099e2018-10-17 16:31:08 -0700371
Steven Moreland055d8792018-11-14 12:48:42 -0800372 if (aidl.IsNullable()) {
373 AIDL_FATAL_IF(info.nullable == nullptr, aidl) << "Unsupported type in NDK Backend.";
374 return *info.nullable;
375 }
376
377 return info.raw;
Steven Moreland67caf422018-10-15 12:39:12 -0700378}
379
Steven Moreland2bea13b2018-10-03 15:12:33 -0700380std::string NdkFullClassName(const AidlDefinedType& type, cpp::ClassNames name) {
Steven Moreland63404532018-10-08 14:31:00 -0700381 std::vector<std::string> pieces = {"::aidl"};
Steven Moreland2bea13b2018-10-03 15:12:33 -0700382 std::vector<std::string> package = type.GetSplitPackage();
383 pieces.insert(pieces.end(), package.begin(), package.end());
384 pieces.push_back(cpp::ClassName(type, name));
385
386 return Join(pieces, "::");
387}
388
389std::string NdkNameOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl, StorageMode mode) {
Steven Moreland055d8792018-11-14 12:48:42 -0800390 TypeInfo::Aspect aspect = GetTypeAspect(types, aidl);
Steven Morelandeb38ee72018-10-15 14:20:04 -0700391
Steven Morelande8a3a192018-09-20 14:14:28 -0700392 switch (mode) {
393 case StorageMode::STACK:
Steven Moreland055d8792018-11-14 12:48:42 -0800394 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700395 case StorageMode::ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800396 if (aspect.value_is_cheap) {
397 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700398 } else {
Steven Moreland055d8792018-11-14 12:48:42 -0800399 return "const " + aspect.cpp_name + "&";
Steven Morelande8a3a192018-09-20 14:14:28 -0700400 }
401 case StorageMode::OUT_ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800402 return aspect.cpp_name + "*";
Steven Morelande8a3a192018-09-20 14:14:28 -0700403 default:
404 AIDL_FATAL(aidl.GetName()) << "Unrecognized mode type: " << static_cast<int>(mode);
405 }
406}
407
408void WriteToParcelFor(const CodeGeneratorContext& c) {
Steven Moreland055d8792018-11-14 12:48:42 -0800409 TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
410 aspect.write_func(c);
Steven Morelande8a3a192018-09-20 14:14:28 -0700411}
412
413void ReadFromParcelFor(const CodeGeneratorContext& c) {
Steven Moreland055d8792018-11-14 12:48:42 -0800414 TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
415 aspect.read_func(c);
Steven Morelande8a3a192018-09-20 14:14:28 -0700416}
417
Jiyong Park965c5b92018-11-21 13:37:15 +0900418std::string NdkArgList(
419 const AidlTypenames& types, const AidlMethod& method,
420 std::function<std::string(const std::string& type, const std::string& name, bool isOut)>
421 formatter) {
Steven Morelandaada3422018-09-20 15:55:33 -0700422 std::vector<std::string> method_arguments;
423 for (const auto& a : method.GetArguments()) {
424 StorageMode mode = a->IsOut() ? StorageMode::OUT_ARGUMENT : StorageMode::ARGUMENT;
Steven Moreland2bea13b2018-10-03 15:12:33 -0700425 std::string type = NdkNameOf(types, a->GetType(), mode);
Steven Morelandaada3422018-09-20 15:55:33 -0700426 std::string name = cpp::BuildVarName(*a);
Jiyong Park965c5b92018-11-21 13:37:15 +0900427 method_arguments.emplace_back(formatter(type, name, a->IsOut()));
Steven Morelandaada3422018-09-20 15:55:33 -0700428 }
429
430 if (method.GetType().GetName() != "void") {
Jiyong Park965c5b92018-11-21 13:37:15 +0900431 std::string type = NdkNameOf(types, method.GetType(), StorageMode::OUT_ARGUMENT);
432 std::string name = "_aidl_return";
433 method_arguments.emplace_back(formatter(type, name, true));
Steven Morelandaada3422018-09-20 15:55:33 -0700434 }
435
436 return Join(method_arguments, ", ");
437}
438
Steven Moreland2bea13b2018-10-03 15:12:33 -0700439std::string NdkMethodDecl(const AidlTypenames& types, const AidlMethod& method,
440 const std::string& clazz) {
Steven Morelandaada3422018-09-20 15:55:33 -0700441 std::string class_prefix = clazz.empty() ? "" : (clazz + "::");
Steven Moreland63404532018-10-08 14:31:00 -0700442 return "::ndk::ScopedAStatus " + class_prefix + method.GetName() + "(" +
Jiyong Park965c5b92018-11-21 13:37:15 +0900443 NdkArgList(types, method, FormatArgForDecl) + ")";
Steven Morelandaada3422018-09-20 15:55:33 -0700444}
445
Steven Morelande8a3a192018-09-20 14:14:28 -0700446} // namespace ndk
447} // namespace aidl
448} // namespace android