blob: 26d2465270ba77acb4682c28a3de199e80d7c1ef [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
Steven Moreland67caf422018-10-15 12:39:12 -070071static std::function<void(const CodeGeneratorContext& c)> StandardRead(const std::string& name) {
Steven Morelande8a3a192018-09-20 14:14:28 -070072 return [name](const CodeGeneratorContext& c) {
Steven Moreland67caf422018-10-15 12:39:12 -070073 c.writer << name << "(" << c.parcel << ", " << c.var << ")";
Steven Morelande8a3a192018-09-20 14:14:28 -070074 };
75}
Steven Moreland67caf422018-10-15 12:39:12 -070076static std::function<void(const CodeGeneratorContext& c)> StandardWrite(const std::string& name) {
Steven Morelande8a3a192018-09-20 14:14:28 -070077 return [name](const CodeGeneratorContext& c) {
Steven Moreland67caf422018-10-15 12:39:12 -070078 c.writer << name << "(" << c.parcel << ", " << c.var << ")";
79 };
80}
81
82TypeInfo PrimitiveType(const std::string& cpp_name, const std::string& pretty_name) {
83 return TypeInfo{
Steven Moreland055d8792018-11-14 12:48:42 -080084 .raw =
85 TypeInfo::Aspect{
86 .cpp_name = cpp_name,
87 .value_is_cheap = true,
88 .read_func = StandardRead("AParcel_read" + pretty_name),
89 .write_func = StandardWrite("AParcel_write" + pretty_name),
90 },
91 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
92 .cpp_name = "std::vector<" + cpp_name + ">",
93 .value_is_cheap = false,
94 .read_func = StandardRead("::ndk::AParcel_readVector"),
95 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
96 }),
97 .nullable = nullptr,
Steven Morelandc10ed462018-11-15 14:50:28 -080098 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
99 .cpp_name = "std::optional<std::vector<" + cpp_name + ">>",
100 .value_is_cheap = false,
101 .read_func = StandardRead("::ndk::AParcel_readVector"),
102 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
103 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800104 };
105}
106
107TypeInfo InterfaceTypeInfo(const AidlInterface& type) {
108 const std::string clazz = NdkFullClassName(type, cpp::ClassNames::INTERFACE);
109
110 return TypeInfo{
111 .raw =
112 TypeInfo::Aspect{
113 .cpp_name = "std::shared_ptr<" + clazz + ">",
114 .value_is_cheap = false,
115 // TODO(b/111445392): these should be non-null
116 .read_func = StandardRead(clazz + "::readFromParcel"),
117 .write_func = StandardWrite(clazz + "::writeToParcel"),
118 },
119 .array = nullptr,
120 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
121 .cpp_name = "std::shared_ptr<" + clazz + ">",
122 .value_is_cheap = false,
123 .read_func = StandardRead(clazz + "::readFromParcel"),
124 .write_func = StandardWrite(clazz + "::writeToParcel"),
125 }),
126 .nullable_array = nullptr,
127 };
128}
129
130TypeInfo ParcelableTypeInfo(const AidlParcelable& type) {
131 const std::string clazz = NdkFullClassName(type, cpp::ClassNames::BASE);
132
133 return TypeInfo{
134 .raw =
135 TypeInfo::Aspect{
136 .cpp_name = clazz,
137 .value_is_cheap = false,
138 .read_func =
139 [](const CodeGeneratorContext& c) {
140 c.writer << "(" << c.var << ")->readFromParcel(" << c.parcel << ")";
141 },
142 .write_func =
143 [](const CodeGeneratorContext& c) {
144 c.writer << "(" << c.var << ").writeToParcel(" << c.parcel << ")";
145 },
146 },
Steven Moreland5ecec6b2018-12-11 18:56:41 -0800147 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
148 .cpp_name = "std::vector<" + clazz + ">",
149 .value_is_cheap = false,
150 .read_func = StandardRead("::ndk::AParcel_readVector"),
151 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
152 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800153 .nullable = nullptr,
154 .nullable_array = nullptr,
Steven Morelande8a3a192018-09-20 14:14:28 -0700155 };
156}
157
158// map from AIDL built-in type name to the corresponding Ndk type name
159static map<std::string, TypeInfo> kNdkTypeInfoMap = {
Steven Moreland055d8792018-11-14 12:48:42 -0800160 {"void", TypeInfo{{"void", true, nullptr, nullptr}, nullptr, nullptr, nullptr}},
Steven Moreland67caf422018-10-15 12:39:12 -0700161 {"boolean", PrimitiveType("bool", "Bool")},
162 {"byte", PrimitiveType("int8_t", "Byte")},
163 {"char", PrimitiveType("char16_t", "Char")},
164 {"int", PrimitiveType("int32_t", "Int32")},
165 {"long", PrimitiveType("int64_t", "Int64")},
166 {"float", PrimitiveType("float", "Float")},
167 {"double", PrimitiveType("double", "Double")},
Steven Moreland63404532018-10-08 14:31:00 -0700168 {"String",
Steven Moreland055d8792018-11-14 12:48:42 -0800169 TypeInfo{
170 .raw =
171 TypeInfo::Aspect{
172 .cpp_name = "std::string",
173 .value_is_cheap = false,
174 .read_func = StandardRead("::ndk::AParcel_readString"),
175 .write_func = StandardWrite("::ndk::AParcel_writeString"),
176 },
177 .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
178 .cpp_name = "std::vector<std::string>",
179 .value_is_cheap = false,
180 .read_func = StandardRead("::ndk::AParcel_readVector"),
181 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
182 }),
Steven Moreland18ed9782018-11-14 17:08:09 -0800183 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
184 .cpp_name = "std::optional<std::string>",
185 .value_is_cheap = false,
186 .read_func = StandardRead("::ndk::AParcel_readString"),
187 .write_func = StandardWrite("::ndk::AParcel_writeString"),
188 }),
189 .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
190 .cpp_name = "std::optional<std::vector<std::optional<std::string>>>",
191 .value_is_cheap = false,
192 .read_func = StandardRead("::ndk::AParcel_readVector"),
193 .write_func = StandardWrite("::ndk::AParcel_writeVector"),
194 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800195 }},
Steven Morelande8a3a192018-09-20 14:14:28 -0700196 // TODO(b/111445392) {"List", ""},
197 // TODO(b/111445392) {"Map", ""},
Steven Moreland055d8792018-11-14 12:48:42 -0800198 {"IBinder",
199 TypeInfo{
200 .raw =
201 TypeInfo::Aspect{
202 .cpp_name = "::ndk::SpAIBinder",
203 .value_is_cheap = false,
Steven Morelande1048a32018-11-16 12:52:17 -0800204 .read_func = StandardRead("::ndk::AParcel_readRequiredStrongBinder"),
205 .write_func = StandardRead("::ndk::AParcel_writeRequiredStrongBinder"),
Steven Moreland055d8792018-11-14 12:48:42 -0800206 },
207 .array = nullptr,
208 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
209 .cpp_name = "::ndk::SpAIBinder",
210 .value_is_cheap = false,
Steven Morelande1048a32018-11-16 12:52:17 -0800211 .read_func = StandardRead("::ndk::AParcel_readNullableStrongBinder"),
212 .write_func = StandardRead("::ndk::AParcel_writeNullableStrongBinder"),
Steven Moreland055d8792018-11-14 12:48:42 -0800213 }),
214 .nullable_array = nullptr,
215 }},
Steven Morelande8a3a192018-09-20 14:14:28 -0700216 // TODO(b/111445392) {"FileDescriptor", ""},
Steven Moreland055d8792018-11-14 12:48:42 -0800217 {"ParcelFileDescriptor",
218 TypeInfo{
219 .raw =
220 TypeInfo::Aspect{
221 .cpp_name = "::ndk::ScopedFileDescriptor",
222 .value_is_cheap = false,
Steven Moreland962c60d2018-11-16 15:19:27 -0800223 .read_func = StandardRead("::ndk::AParcel_readRequiredParcelFileDescriptor"),
224 .write_func = StandardRead("::ndk::AParcel_writeRequiredParcelFileDescriptor"),
Steven Moreland055d8792018-11-14 12:48:42 -0800225 },
226 .array = nullptr,
Steven Moreland962c60d2018-11-16 15:19:27 -0800227 .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
228 .cpp_name = "::ndk::ScopedFileDescriptor",
229 .value_is_cheap = false,
230 .read_func = StandardRead("::ndk::AParcel_readNullableParcelFileDescriptor"),
231 .write_func = StandardRead("::ndk::AParcel_writeNullableParcelFileDescriptor"),
232 }),
Steven Moreland055d8792018-11-14 12:48:42 -0800233 .nullable_array = nullptr,
234 }},
Steven Morelande8a3a192018-09-20 14:14:28 -0700235 // TODO(b/111445392) {"CharSequence", ""},
236};
237
Steven Moreland055d8792018-11-14 12:48:42 -0800238static TypeInfo::Aspect GetTypeAspect(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
Steven Moreland1cb099e2018-10-17 16:31:08 -0700239 CHECK(aidl.IsResolved()) << aidl.ToString();
240
Steven Moreland67caf422018-10-15 12:39:12 -0700241 const string aidl_name = aidl.GetName();
242
Steven Moreland1cb099e2018-10-17 16:31:08 -0700243 // TODO(b/112664205): this is okay for some types
244 AIDL_FATAL_IF(aidl.IsGeneric(), aidl) << aidl.ToString();
Steven Moreland1cb099e2018-10-17 16:31:08 -0700245
Steven Moreland67caf422018-10-15 12:39:12 -0700246 TypeInfo info;
247 if (AidlTypenames::IsBuiltinTypename(aidl_name)) {
248 auto it = kNdkTypeInfoMap.find(aidl_name);
249 CHECK(it != kNdkTypeInfoMap.end());
250 info = it->second;
251 } else {
252 const AidlDefinedType* type = types.TryGetDefinedType(aidl_name);
Steven Moreland67caf422018-10-15 12:39:12 -0700253 AIDL_FATAL_IF(type == nullptr, aidl_name) << "Unrecognized type.";
254
255 if (type->AsInterface() != nullptr) {
Steven Moreland055d8792018-11-14 12:48:42 -0800256 info = InterfaceTypeInfo(*type->AsInterface());
Steven Moreland67caf422018-10-15 12:39:12 -0700257 } else if (type->AsParcelable() != nullptr) {
Steven Moreland055d8792018-11-14 12:48:42 -0800258 info = ParcelableTypeInfo(*type->AsParcelable());
Steven Moreland67caf422018-10-15 12:39:12 -0700259 } else {
260 AIDL_FATAL(aidl_name) << "Unrecognized type";
261 }
262 }
263
Steven Moreland055d8792018-11-14 12:48:42 -0800264 if (aidl.IsArray()) {
265 if (aidl.IsNullable()) {
266 AIDL_FATAL_IF(info.nullable_array == nullptr, aidl) << "Unsupported type in NDK Backend.";
267 return *info.nullable_array;
268 }
269 AIDL_FATAL_IF(info.array == nullptr, aidl) << "Unsupported type in NDK Backend.";
270 return *info.array;
271 }
Steven Moreland1cb099e2018-10-17 16:31:08 -0700272
Steven Moreland055d8792018-11-14 12:48:42 -0800273 if (aidl.IsNullable()) {
274 AIDL_FATAL_IF(info.nullable == nullptr, aidl) << "Unsupported type in NDK Backend.";
275 return *info.nullable;
276 }
277
278 return info.raw;
Steven Moreland67caf422018-10-15 12:39:12 -0700279}
280
Steven Moreland2bea13b2018-10-03 15:12:33 -0700281std::string NdkFullClassName(const AidlDefinedType& type, cpp::ClassNames name) {
Steven Moreland63404532018-10-08 14:31:00 -0700282 std::vector<std::string> pieces = {"::aidl"};
Steven Moreland2bea13b2018-10-03 15:12:33 -0700283 std::vector<std::string> package = type.GetSplitPackage();
284 pieces.insert(pieces.end(), package.begin(), package.end());
285 pieces.push_back(cpp::ClassName(type, name));
286
287 return Join(pieces, "::");
288}
289
290std::string NdkNameOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl, StorageMode mode) {
Steven Moreland055d8792018-11-14 12:48:42 -0800291 TypeInfo::Aspect aspect = GetTypeAspect(types, aidl);
Steven Morelandeb38ee72018-10-15 14:20:04 -0700292
Steven Morelande8a3a192018-09-20 14:14:28 -0700293 switch (mode) {
294 case StorageMode::STACK:
Steven Moreland055d8792018-11-14 12:48:42 -0800295 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700296 case StorageMode::ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800297 if (aspect.value_is_cheap) {
298 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700299 } else {
Steven Moreland055d8792018-11-14 12:48:42 -0800300 return "const " + aspect.cpp_name + "&";
Steven Morelande8a3a192018-09-20 14:14:28 -0700301 }
302 case StorageMode::OUT_ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800303 return aspect.cpp_name + "*";
Steven Morelande8a3a192018-09-20 14:14:28 -0700304 default:
305 AIDL_FATAL(aidl.GetName()) << "Unrecognized mode type: " << static_cast<int>(mode);
306 }
307}
308
309void WriteToParcelFor(const CodeGeneratorContext& c) {
Steven Moreland055d8792018-11-14 12:48:42 -0800310 TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
311 aspect.write_func(c);
Steven Morelande8a3a192018-09-20 14:14:28 -0700312}
313
314void ReadFromParcelFor(const CodeGeneratorContext& c) {
Steven Moreland055d8792018-11-14 12:48:42 -0800315 TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
316 aspect.read_func(c);
Steven Morelande8a3a192018-09-20 14:14:28 -0700317}
318
Jiyong Park965c5b92018-11-21 13:37:15 +0900319std::string NdkArgList(
320 const AidlTypenames& types, const AidlMethod& method,
321 std::function<std::string(const std::string& type, const std::string& name, bool isOut)>
322 formatter) {
Steven Morelandaada3422018-09-20 15:55:33 -0700323 std::vector<std::string> method_arguments;
324 for (const auto& a : method.GetArguments()) {
325 StorageMode mode = a->IsOut() ? StorageMode::OUT_ARGUMENT : StorageMode::ARGUMENT;
Steven Moreland2bea13b2018-10-03 15:12:33 -0700326 std::string type = NdkNameOf(types, a->GetType(), mode);
Steven Morelandaada3422018-09-20 15:55:33 -0700327 std::string name = cpp::BuildVarName(*a);
Jiyong Park965c5b92018-11-21 13:37:15 +0900328 method_arguments.emplace_back(formatter(type, name, a->IsOut()));
Steven Morelandaada3422018-09-20 15:55:33 -0700329 }
330
331 if (method.GetType().GetName() != "void") {
Jiyong Park965c5b92018-11-21 13:37:15 +0900332 std::string type = NdkNameOf(types, method.GetType(), StorageMode::OUT_ARGUMENT);
333 std::string name = "_aidl_return";
334 method_arguments.emplace_back(formatter(type, name, true));
Steven Morelandaada3422018-09-20 15:55:33 -0700335 }
336
337 return Join(method_arguments, ", ");
338}
339
Steven Moreland2bea13b2018-10-03 15:12:33 -0700340std::string NdkMethodDecl(const AidlTypenames& types, const AidlMethod& method,
341 const std::string& clazz) {
Steven Morelandaada3422018-09-20 15:55:33 -0700342 std::string class_prefix = clazz.empty() ? "" : (clazz + "::");
Steven Moreland63404532018-10-08 14:31:00 -0700343 return "::ndk::ScopedAStatus " + class_prefix + method.GetName() + "(" +
Jiyong Park965c5b92018-11-21 13:37:15 +0900344 NdkArgList(types, method, FormatArgForDecl) + ")";
Steven Morelandaada3422018-09-20 15:55:33 -0700345}
346
Steven Morelande8a3a192018-09-20 14:14:28 -0700347} // namespace ndk
348} // namespace aidl
349} // namespace android