blob: abf54ac97386f6d152b8c9789d138587a120f907 [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());
184 CHECK(aparcel_name_it != kAParcelTypeNameMap.end());
185 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 }},
Steven Morelande8a3a192018-09-20 14:14:28 -0700325};
326
Steven Moreland055d8792018-11-14 12:48:42 -0800327static TypeInfo::Aspect GetTypeAspect(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
Steven Moreland1cb099e2018-10-17 16:31:08 -0700328 CHECK(aidl.IsResolved()) << aidl.ToString();
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900329 auto& aidl_name = aidl.GetName();
Steven Moreland1cb099e2018-10-17 16:31:08 -0700330
Steven Moreland67caf422018-10-15 12:39:12 -0700331 TypeInfo info;
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900332
333 // TODO(b/136048684): For now, List<T> is converted to T[].(Both are using vector<T>)
334 if (aidl_name == "List") {
Steven Morelandfe52c9f2019-11-27 19:04:48 -0800335 AIDL_FATAL_IF(!aidl.IsGeneric(), aidl) << "List must be generic type.";
336 AIDL_FATAL_IF(aidl.GetTypeParameters().size() != 1, aidl)
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900337 << "List can accept only one type parameter.";
Devin Moore53fc99c2020-08-12 08:07:52 -0700338 const auto& type_param = aidl.GetTypeParameters()[0];
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900339 // TODO(b/136048684) AIDL doesn't support nested type parameter yet.
340 AIDL_FATAL_IF(type_param->IsGeneric(), aidl) << "AIDL doesn't support nested type parameter";
341
342 AidlTypeSpecifier array_type =
343 AidlTypeSpecifier(AIDL_LOCATION_HERE, type_param->GetUnresolvedName(), true /* isArray */,
344 nullptr /* type_params */, aidl.GetComments());
345 if (!(array_type.Resolve(types) && array_type.CheckValid(types))) {
346 AIDL_FATAL(aidl) << "The type parameter is wrong.";
347 }
348 return GetTypeAspect(types, array_type);
349 }
350
Steven Moreland67caf422018-10-15 12:39:12 -0700351 if (AidlTypenames::IsBuiltinTypename(aidl_name)) {
352 auto it = kNdkTypeInfoMap.find(aidl_name);
353 CHECK(it != kNdkTypeInfoMap.end());
354 info = it->second;
355 } else {
356 const AidlDefinedType* type = types.TryGetDefinedType(aidl_name);
Steven Moreland67caf422018-10-15 12:39:12 -0700357 AIDL_FATAL_IF(type == nullptr, aidl_name) << "Unrecognized type.";
358
Daniel Norman37d43dd2019-09-09 17:22:34 -0700359 if (const AidlInterface* intf = type->AsInterface(); intf != nullptr) {
360 info = InterfaceTypeInfo(*intf);
361 } else if (const AidlParcelable* parcelable = type->AsParcelable(); parcelable != nullptr) {
Devin Moore53fc99c2020-08-12 08:07:52 -0700362 info = ParcelableTypeInfo(*parcelable, aidl, types);
Daniel Norman37d43dd2019-09-09 17:22:34 -0700363 } else if (const AidlEnumDeclaration* enum_decl = type->AsEnumDeclaration();
364 enum_decl != nullptr) {
365 info = EnumDeclarationTypeInfo(*enum_decl);
Steven Moreland67caf422018-10-15 12:39:12 -0700366 } else {
367 AIDL_FATAL(aidl_name) << "Unrecognized type";
368 }
369 }
370
Steven Moreland055d8792018-11-14 12:48:42 -0800371 if (aidl.IsArray()) {
372 if (aidl.IsNullable()) {
Steven Morelandd59e3172020-05-11 16:42:09 -0700373 AIDL_FATAL_IF(info.nullable_array == nullptr, aidl)
374 << "Unsupported type in NDK Backend: " << aidl.ToString();
Steven Moreland055d8792018-11-14 12:48:42 -0800375 return *info.nullable_array;
376 }
Steven Morelandd59e3172020-05-11 16:42:09 -0700377 AIDL_FATAL_IF(info.array == nullptr, aidl)
378 << "Unsupported type in NDK Backend: " << aidl.ToString();
Steven Moreland055d8792018-11-14 12:48:42 -0800379 return *info.array;
380 }
Steven Moreland1cb099e2018-10-17 16:31:08 -0700381
Steven Moreland055d8792018-11-14 12:48:42 -0800382 if (aidl.IsNullable()) {
Steven Morelandd59e3172020-05-11 16:42:09 -0700383 AIDL_FATAL_IF(info.nullable == nullptr, aidl)
384 << "Unsupported type in NDK Backend: " << aidl.ToString();
Steven Moreland055d8792018-11-14 12:48:42 -0800385 return *info.nullable;
386 }
387
388 return info.raw;
Steven Moreland67caf422018-10-15 12:39:12 -0700389}
390
Steven Moreland2bea13b2018-10-03 15:12:33 -0700391std::string NdkFullClassName(const AidlDefinedType& type, cpp::ClassNames name) {
Steven Moreland63404532018-10-08 14:31:00 -0700392 std::vector<std::string> pieces = {"::aidl"};
Steven Moreland2bea13b2018-10-03 15:12:33 -0700393 std::vector<std::string> package = type.GetSplitPackage();
394 pieces.insert(pieces.end(), package.begin(), package.end());
395 pieces.push_back(cpp::ClassName(type, name));
396
397 return Join(pieces, "::");
398}
399
400std::string NdkNameOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl, StorageMode mode) {
Steven Moreland055d8792018-11-14 12:48:42 -0800401 TypeInfo::Aspect aspect = GetTypeAspect(types, aidl);
Steven Morelandeb38ee72018-10-15 14:20:04 -0700402
Steven Morelande8a3a192018-09-20 14:14:28 -0700403 switch (mode) {
404 case StorageMode::STACK:
Steven Moreland055d8792018-11-14 12:48:42 -0800405 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700406 case StorageMode::ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800407 if (aspect.value_is_cheap) {
408 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700409 } else {
Steven Moreland055d8792018-11-14 12:48:42 -0800410 return "const " + aspect.cpp_name + "&";
Steven Morelande8a3a192018-09-20 14:14:28 -0700411 }
412 case StorageMode::OUT_ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800413 return aspect.cpp_name + "*";
Steven Morelande8a3a192018-09-20 14:14:28 -0700414 default:
415 AIDL_FATAL(aidl.GetName()) << "Unrecognized mode type: " << static_cast<int>(mode);
416 }
417}
418
419void WriteToParcelFor(const CodeGeneratorContext& c) {
Steven Moreland055d8792018-11-14 12:48:42 -0800420 TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
421 aspect.write_func(c);
Steven Morelande8a3a192018-09-20 14:14:28 -0700422}
423
424void ReadFromParcelFor(const CodeGeneratorContext& c) {
Steven Moreland055d8792018-11-14 12:48:42 -0800425 TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
426 aspect.read_func(c);
Steven Morelande8a3a192018-09-20 14:14:28 -0700427}
428
Jiyong Park965c5b92018-11-21 13:37:15 +0900429std::string NdkArgList(
430 const AidlTypenames& types, const AidlMethod& method,
431 std::function<std::string(const std::string& type, const std::string& name, bool isOut)>
432 formatter) {
Steven Morelandaada3422018-09-20 15:55:33 -0700433 std::vector<std::string> method_arguments;
434 for (const auto& a : method.GetArguments()) {
435 StorageMode mode = a->IsOut() ? StorageMode::OUT_ARGUMENT : StorageMode::ARGUMENT;
Steven Moreland2bea13b2018-10-03 15:12:33 -0700436 std::string type = NdkNameOf(types, a->GetType(), mode);
Steven Morelandaada3422018-09-20 15:55:33 -0700437 std::string name = cpp::BuildVarName(*a);
Jiyong Park965c5b92018-11-21 13:37:15 +0900438 method_arguments.emplace_back(formatter(type, name, a->IsOut()));
Steven Morelandaada3422018-09-20 15:55:33 -0700439 }
440
441 if (method.GetType().GetName() != "void") {
Jiyong Park965c5b92018-11-21 13:37:15 +0900442 std::string type = NdkNameOf(types, method.GetType(), StorageMode::OUT_ARGUMENT);
443 std::string name = "_aidl_return";
444 method_arguments.emplace_back(formatter(type, name, true));
Steven Morelandaada3422018-09-20 15:55:33 -0700445 }
446
447 return Join(method_arguments, ", ");
448}
449
Steven Moreland2bea13b2018-10-03 15:12:33 -0700450std::string NdkMethodDecl(const AidlTypenames& types, const AidlMethod& method,
451 const std::string& clazz) {
Steven Morelandaada3422018-09-20 15:55:33 -0700452 std::string class_prefix = clazz.empty() ? "" : (clazz + "::");
Steven Moreland63404532018-10-08 14:31:00 -0700453 return "::ndk::ScopedAStatus " + class_prefix + method.GetName() + "(" +
Jiyong Park965c5b92018-11-21 13:37:15 +0900454 NdkArgList(types, method, FormatArgForDecl) + ")";
Steven Morelandaada3422018-09-20 15:55:33 -0700455}
456
Steven Morelande8a3a192018-09-20 14:14:28 -0700457} // namespace ndk
458} // namespace aidl
459} // namespace android