blob: 639730d21a31a045efb2e1dc5fd5e2e436d7e595 [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;
26using ::android::base::Split;
27
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
Steven Moreland67caf422018-10-15 12:39:12 -070072static std::function<void(const CodeGeneratorContext& c)> StandardRead(const std::string& name) {
Steven Morelande8a3a192018-09-20 14:14:28 -070073 return [name](const CodeGeneratorContext& c) {
Steven Moreland67caf422018-10-15 12:39:12 -070074 c.writer << name << "(" << c.parcel << ", " << c.var << ")";
Steven Morelande8a3a192018-09-20 14:14:28 -070075 };
76}
Steven Moreland67caf422018-10-15 12:39:12 -070077static std::function<void(const CodeGeneratorContext& c)> StandardWrite(const std::string& name) {
Steven Morelande8a3a192018-09-20 14:14:28 -070078 return [name](const CodeGeneratorContext& c) {
Steven Moreland67caf422018-10-15 12:39:12 -070079 c.writer << name << "(" << c.parcel << ", " << c.var << ")";
80 };
81}
82
83TypeInfo PrimitiveType(const std::string& cpp_name, const std::string& pretty_name) {
84 return TypeInfo{
Steven Moreland055d8792018-11-14 12:48:42 -080085 .raw =
86 TypeInfo::Aspect{
87 .cpp_name = cpp_name,
88 .value_is_cheap = true,
89 .read_func = StandardRead("AParcel_read" + pretty_name),
90 .write_func = StandardWrite("AParcel_write" + pretty_name),
91 },
92 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
93 .cpp_name = "std::vector<" + cpp_name + ">",
94 .value_is_cheap = false,
95 .read_func = StandardRead("::ndk::AParcel_readVector"),
96 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
97 }),
98 .nullable = nullptr,
Steven Morelandc10ed462018-11-15 14:50:28 -080099 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
100 .cpp_name = "std::optional<std::vector<" + cpp_name + ">>",
101 .value_is_cheap = false,
102 .read_func = StandardRead("::ndk::AParcel_readVector"),
103 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
104 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800105 };
106}
107
108TypeInfo InterfaceTypeInfo(const AidlInterface& type) {
109 const std::string clazz = NdkFullClassName(type, cpp::ClassNames::INTERFACE);
110
111 return TypeInfo{
112 .raw =
113 TypeInfo::Aspect{
114 .cpp_name = "std::shared_ptr<" + clazz + ">",
115 .value_is_cheap = false,
116 // TODO(b/111445392): these should be non-null
117 .read_func = StandardRead(clazz + "::readFromParcel"),
118 .write_func = StandardWrite(clazz + "::writeToParcel"),
119 },
120 .array = nullptr,
121 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
122 .cpp_name = "std::shared_ptr<" + clazz + ">",
123 .value_is_cheap = false,
124 .read_func = StandardRead(clazz + "::readFromParcel"),
125 .write_func = StandardWrite(clazz + "::writeToParcel"),
126 }),
127 .nullable_array = nullptr,
128 };
129}
130
131TypeInfo ParcelableTypeInfo(const AidlParcelable& type) {
132 const std::string clazz = NdkFullClassName(type, cpp::ClassNames::BASE);
133
134 return TypeInfo{
135 .raw =
136 TypeInfo::Aspect{
137 .cpp_name = clazz,
138 .value_is_cheap = false,
139 .read_func =
140 [](const CodeGeneratorContext& c) {
141 c.writer << "(" << c.var << ")->readFromParcel(" << c.parcel << ")";
142 },
143 .write_func =
144 [](const CodeGeneratorContext& c) {
145 c.writer << "(" << c.var << ").writeToParcel(" << c.parcel << ")";
146 },
147 },
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 Moreland055d8792018-11-14 12:48:42 -0800154 .nullable = nullptr,
155 .nullable_array = nullptr,
Steven Morelande8a3a192018-09-20 14:14:28 -0700156 };
157}
158
159// map from AIDL built-in type name to the corresponding Ndk type name
160static map<std::string, TypeInfo> kNdkTypeInfoMap = {
Steven Moreland055d8792018-11-14 12:48:42 -0800161 {"void", TypeInfo{{"void", true, nullptr, nullptr}, nullptr, nullptr, nullptr}},
Steven Moreland67caf422018-10-15 12:39:12 -0700162 {"boolean", PrimitiveType("bool", "Bool")},
163 {"byte", PrimitiveType("int8_t", "Byte")},
164 {"char", PrimitiveType("char16_t", "Char")},
165 {"int", PrimitiveType("int32_t", "Int32")},
166 {"long", PrimitiveType("int64_t", "Int64")},
167 {"float", PrimitiveType("float", "Float")},
168 {"double", PrimitiveType("double", "Double")},
Steven Moreland63404532018-10-08 14:31:00 -0700169 {"String",
Steven Moreland055d8792018-11-14 12:48:42 -0800170 TypeInfo{
171 .raw =
172 TypeInfo::Aspect{
173 .cpp_name = "std::string",
174 .value_is_cheap = false,
175 .read_func = StandardRead("::ndk::AParcel_readString"),
176 .write_func = StandardWrite("::ndk::AParcel_writeString"),
177 },
178 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
179 .cpp_name = "std::vector<std::string>",
180 .value_is_cheap = false,
181 .read_func = StandardRead("::ndk::AParcel_readVector"),
182 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
183 }),
Steven Moreland18ed9782018-11-14 17:08:09 -0800184 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
185 .cpp_name = "std::optional<std::string>",
186 .value_is_cheap = false,
187 .read_func = StandardRead("::ndk::AParcel_readString"),
188 .write_func = StandardWrite("::ndk::AParcel_writeString"),
189 }),
190 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
191 .cpp_name = "std::optional<std::vector<std::optional<std::string>>>",
192 .value_is_cheap = false,
193 .read_func = StandardRead("::ndk::AParcel_readVector"),
194 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
195 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800196 }},
Steven Morelande8a3a192018-09-20 14:14:28 -0700197 // TODO(b/111445392) {"List", ""},
198 // TODO(b/111445392) {"Map", ""},
Steven Moreland055d8792018-11-14 12:48:42 -0800199 {"IBinder",
200 TypeInfo{
201 .raw =
202 TypeInfo::Aspect{
203 .cpp_name = "::ndk::SpAIBinder",
204 .value_is_cheap = false,
Steven Morelande1048a32018-11-16 12:52:17 -0800205 .read_func = StandardRead("::ndk::AParcel_readRequiredStrongBinder"),
206 .write_func = StandardRead("::ndk::AParcel_writeRequiredStrongBinder"),
Steven Moreland055d8792018-11-14 12:48:42 -0800207 },
208 .array = nullptr,
209 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
210 .cpp_name = "::ndk::SpAIBinder",
211 .value_is_cheap = false,
Steven Morelande1048a32018-11-16 12:52:17 -0800212 .read_func = StandardRead("::ndk::AParcel_readNullableStrongBinder"),
213 .write_func = StandardRead("::ndk::AParcel_writeNullableStrongBinder"),
Steven Moreland055d8792018-11-14 12:48:42 -0800214 }),
215 .nullable_array = nullptr,
216 }},
Steven Morelande8a3a192018-09-20 14:14:28 -0700217 // TODO(b/111445392) {"FileDescriptor", ""},
Steven Moreland055d8792018-11-14 12:48:42 -0800218 {"ParcelFileDescriptor",
219 TypeInfo{
220 .raw =
221 TypeInfo::Aspect{
222 .cpp_name = "::ndk::ScopedFileDescriptor",
223 .value_is_cheap = false,
Steven Moreland962c60d2018-11-16 15:19:27 -0800224 .read_func = StandardRead("::ndk::AParcel_readRequiredParcelFileDescriptor"),
225 .write_func = StandardRead("::ndk::AParcel_writeRequiredParcelFileDescriptor"),
Steven Moreland055d8792018-11-14 12:48:42 -0800226 },
227 .array = nullptr,
Steven Moreland962c60d2018-11-16 15:19:27 -0800228 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
229 .cpp_name = "::ndk::ScopedFileDescriptor",
230 .value_is_cheap = false,
231 .read_func = StandardRead("::ndk::AParcel_readNullableParcelFileDescriptor"),
232 .write_func = StandardRead("::ndk::AParcel_writeNullableParcelFileDescriptor"),
233 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800234 .nullable_array = nullptr,
235 }},
Steven Morelande8a3a192018-09-20 14:14:28 -0700236 // TODO(b/111445392) {"CharSequence", ""},
237};
238
Steven Moreland055d8792018-11-14 12:48:42 -0800239static TypeInfo::Aspect GetTypeAspect(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
Steven Moreland1cb099e2018-10-17 16:31:08 -0700240 CHECK(aidl.IsResolved()) << aidl.ToString();
241
Steven Moreland67caf422018-10-15 12:39:12 -0700242 const string aidl_name = aidl.GetName();
243
Steven Moreland1cb099e2018-10-17 16:31:08 -0700244 // TODO(b/112664205): this is okay for some types
245 AIDL_FATAL_IF(aidl.IsGeneric(), aidl) << aidl.ToString();
Steven Moreland1cb099e2018-10-17 16:31:08 -0700246
Steven Moreland67caf422018-10-15 12:39:12 -0700247 TypeInfo info;
248 if (AidlTypenames::IsBuiltinTypename(aidl_name)) {
249 auto it = kNdkTypeInfoMap.find(aidl_name);
250 CHECK(it != kNdkTypeInfoMap.end());
251 info = it->second;
252 } else {
253 const AidlDefinedType* type = types.TryGetDefinedType(aidl_name);
Steven Moreland67caf422018-10-15 12:39:12 -0700254 AIDL_FATAL_IF(type == nullptr, aidl_name) << "Unrecognized type.";
255
256 if (type->AsInterface() != nullptr) {
Steven Moreland055d8792018-11-14 12:48:42 -0800257 info = InterfaceTypeInfo(*type->AsInterface());
Steven Moreland67caf422018-10-15 12:39:12 -0700258 } else if (type->AsParcelable() != nullptr) {
Steven Moreland055d8792018-11-14 12:48:42 -0800259 info = ParcelableTypeInfo(*type->AsParcelable());
Steven Moreland67caf422018-10-15 12:39:12 -0700260 } else {
261 AIDL_FATAL(aidl_name) << "Unrecognized type";
262 }
263 }
264
Steven Moreland055d8792018-11-14 12:48:42 -0800265 if (aidl.IsArray()) {
266 if (aidl.IsNullable()) {
267 AIDL_FATAL_IF(info.nullable_array == nullptr, aidl) << "Unsupported type in NDK Backend.";
268 return *info.nullable_array;
269 }
270 AIDL_FATAL_IF(info.array == nullptr, aidl) << "Unsupported type in NDK Backend.";
271 return *info.array;
272 }
Steven Moreland1cb099e2018-10-17 16:31:08 -0700273
Steven Moreland055d8792018-11-14 12:48:42 -0800274 if (aidl.IsNullable()) {
275 AIDL_FATAL_IF(info.nullable == nullptr, aidl) << "Unsupported type in NDK Backend.";
276 return *info.nullable;
277 }
278
279 return info.raw;
Steven Moreland67caf422018-10-15 12:39:12 -0700280}
281
Steven Moreland2bea13b2018-10-03 15:12:33 -0700282std::string NdkFullClassName(const AidlDefinedType& type, cpp::ClassNames name) {
Steven Moreland63404532018-10-08 14:31:00 -0700283 std::vector<std::string> pieces = {"::aidl"};
Steven Moreland2bea13b2018-10-03 15:12:33 -0700284 std::vector<std::string> package = type.GetSplitPackage();
285 pieces.insert(pieces.end(), package.begin(), package.end());
286 pieces.push_back(cpp::ClassName(type, name));
287
288 return Join(pieces, "::");
289}
290
291std::string NdkNameOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl, StorageMode mode) {
Steven Moreland055d8792018-11-14 12:48:42 -0800292 TypeInfo::Aspect aspect = GetTypeAspect(types, aidl);
Steven Morelandeb38ee72018-10-15 14:20:04 -0700293
Steven Morelande8a3a192018-09-20 14:14:28 -0700294 switch (mode) {
295 case StorageMode::STACK:
Steven Moreland055d8792018-11-14 12:48:42 -0800296 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700297 case StorageMode::ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800298 if (aspect.value_is_cheap) {
299 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700300 } else {
Steven Moreland055d8792018-11-14 12:48:42 -0800301 return "const " + aspect.cpp_name + "&";
Steven Morelande8a3a192018-09-20 14:14:28 -0700302 }
303 case StorageMode::OUT_ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800304 return aspect.cpp_name + "*";
Steven Morelande8a3a192018-09-20 14:14:28 -0700305 default:
306 AIDL_FATAL(aidl.GetName()) << "Unrecognized mode type: " << static_cast<int>(mode);
307 }
308}
309
310void WriteToParcelFor(const CodeGeneratorContext& c) {
Steven Moreland055d8792018-11-14 12:48:42 -0800311 TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
312 aspect.write_func(c);
Steven Morelande8a3a192018-09-20 14:14:28 -0700313}
314
315void ReadFromParcelFor(const CodeGeneratorContext& c) {
Steven Moreland055d8792018-11-14 12:48:42 -0800316 TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
317 aspect.read_func(c);
Steven Morelande8a3a192018-09-20 14:14:28 -0700318}
319
Jiyong Park965c5b92018-11-21 13:37:15 +0900320std::string NdkArgList(
321 const AidlTypenames& types, const AidlMethod& method,
322 std::function<std::string(const std::string& type, const std::string& name, bool isOut)>
323 formatter) {
Steven Morelandaada3422018-09-20 15:55:33 -0700324 std::vector<std::string> method_arguments;
325 for (const auto& a : method.GetArguments()) {
326 StorageMode mode = a->IsOut() ? StorageMode::OUT_ARGUMENT : StorageMode::ARGUMENT;
Steven Moreland2bea13b2018-10-03 15:12:33 -0700327 std::string type = NdkNameOf(types, a->GetType(), mode);
Steven Morelandaada3422018-09-20 15:55:33 -0700328 std::string name = cpp::BuildVarName(*a);
Jiyong Park965c5b92018-11-21 13:37:15 +0900329 method_arguments.emplace_back(formatter(type, name, a->IsOut()));
Steven Morelandaada3422018-09-20 15:55:33 -0700330 }
331
332 if (method.GetType().GetName() != "void") {
Jiyong Park965c5b92018-11-21 13:37:15 +0900333 std::string type = NdkNameOf(types, method.GetType(), StorageMode::OUT_ARGUMENT);
334 std::string name = "_aidl_return";
335 method_arguments.emplace_back(formatter(type, name, true));
Steven Morelandaada3422018-09-20 15:55:33 -0700336 }
337
338 return Join(method_arguments, ", ");
339}
340
Steven Moreland2bea13b2018-10-03 15:12:33 -0700341std::string NdkMethodDecl(const AidlTypenames& types, const AidlMethod& method,
342 const std::string& clazz) {
Steven Morelandaada3422018-09-20 15:55:33 -0700343 std::string class_prefix = clazz.empty() ? "" : (clazz + "::");
Steven Moreland63404532018-10-08 14:31:00 -0700344 return "::ndk::ScopedAStatus " + class_prefix + method.GetName() + "(" +
Jiyong Park965c5b92018-11-21 13:37:15 +0900345 NdkArgList(types, method, FormatArgForDecl) + ")";
Steven Morelandaada3422018-09-20 15:55:33 -0700346}
347
Steven Morelande8a3a192018-09-20 14:14:28 -0700348} // namespace ndk
349} // namespace aidl
350} // namespace android