blob: adcd45124f0a0cc72376bd20709b12faa56cfb66 [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;
Jooyung Han4b832522021-10-06 16:08:30 +090027using ::android::base::Split;
Steven Morelande8a3a192018-09-20 14:14:28 -070028
29namespace android {
30namespace aidl {
31namespace ndk {
32
Jooyung Han729630b2021-12-01 17:32:54 +090033static const AidlTypeSpecifier kIntType{AIDL_LOCATION_HERE, "int", /*array=*/std::nullopt, nullptr,
34 Comments{}};
35
Steven Moreland7c933372018-10-11 15:20:04 -070036std::string NdkHeaderFile(const AidlDefinedType& defined_type, cpp::ClassNames name,
37 bool use_os_sep) {
38 char seperator = (use_os_sep) ? OS_PATH_SEPARATOR : '/';
39 return std::string("aidl") + seperator + cpp::HeaderFile(defined_type, name, use_os_sep);
40}
41
Steven Moreland055d8792018-11-14 12:48:42 -080042// This represents a type in AIDL (e.g. 'String' which can be referenced in multiple ways)
Steven Morelande8a3a192018-09-20 14:14:28 -070043struct TypeInfo {
Jooyung Han719cdce2021-11-24 05:18:43 +090044 // name of the type in C++ output
45 std::string cpp_name;
46 // whether to prefer 'value type' over 'const&'
47 bool value_is_cheap = false;
Steven Morelande8a3a192018-09-20 14:14:28 -070048};
49
Jooyung Hanaeb01672021-11-30 17:29:22 +090050std::string ConstantValueDecorator(
51 const AidlTypeSpecifier& type,
52 const std::variant<std::string, std::vector<std::string>>& raw_value) {
Jooyung Han981fc592021-11-06 20:24:45 +090053 return cpp::CppConstantValueDecorator(type, raw_value, /*is_ndk=*/true);
Daniel Norman37d43dd2019-09-09 17:22:34 -070054};
55
Daniel Norman37d43dd2019-09-09 17:22:34 -070056// map from AIDL built-in type name to the corresponding Ndk type info
Steven Morelande8a3a192018-09-20 14:14:28 -070057static map<std::string, TypeInfo> kNdkTypeInfoMap = {
Jooyung Han719cdce2021-11-24 05:18:43 +090058 {"void", {"void", true}},
59 {"boolean", {"bool", true}},
60 {"byte", {"int8_t", true}},
61 {"char", {"char16_t", true}},
62 {"int", {"int32_t", true}},
63 {"long", {"int64_t", true}},
64 {"float", {"float", true}},
65 {"double", {"double", true}},
66 {"String", {"std::string"}},
Jeongik Cha1a0f22d2019-11-18 23:22:23 +090067 // TODO(b/136048684) {"Map", ""},
Jooyung Han719cdce2021-11-24 05:18:43 +090068 {"IBinder", {"::ndk::SpAIBinder"}},
69 {"ParcelFileDescriptor", {"::ndk::ScopedFileDescriptor"}},
70 {"ParcelableHolder", {"::ndk::AParcelableHolder"}},
Steven Morelande8a3a192018-09-20 14:14:28 -070071};
72
Jooyung Han719cdce2021-11-24 05:18:43 +090073static TypeInfo GetBaseTypeInfo(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
Jooyung Han5014b3e2021-10-15 09:58:20 +090074 auto& aidl_name = aidl.GetName();
75
76 if (AidlTypenames::IsBuiltinTypename(aidl_name)) {
77 auto it = kNdkTypeInfoMap.find(aidl_name);
78 AIDL_FATAL_IF(it == kNdkTypeInfoMap.end(), aidl_name);
79 return it->second;
80 }
81 const AidlDefinedType* type = types.TryGetDefinedType(aidl_name);
82 AIDL_FATAL_IF(type == nullptr, aidl_name) << "Unrecognized type.";
83
84 if (const AidlInterface* intf = type->AsInterface(); intf != nullptr) {
Jooyung Han719cdce2021-11-24 05:18:43 +090085 const std::string clazz = NdkFullClassName(*intf, cpp::ClassNames::INTERFACE);
86 return TypeInfo{"std::shared_ptr<" + clazz + ">"};
Jooyung Han5014b3e2021-10-15 09:58:20 +090087 } else if (const AidlParcelable* parcelable = type->AsParcelable(); parcelable != nullptr) {
Jooyung Han719cdce2021-11-24 05:18:43 +090088 std::string clazz = NdkFullClassName(*parcelable, cpp::ClassNames::RAW);
89 std::string template_params = "";
90 if (aidl.IsGeneric()) {
91 std::vector<std::string> type_params;
92 for (const auto& parameter : aidl.GetTypeParameters()) {
93 type_params.push_back(NdkNameOf(types, *parameter, StorageMode::STACK));
94 }
95 clazz += base::StringPrintf("<%s>", base::Join(type_params, ", ").c_str());
96 }
97 return TypeInfo{clazz};
Jooyung Han5014b3e2021-10-15 09:58:20 +090098 } else if (const AidlEnumDeclaration* enum_decl = type->AsEnumDeclaration();
99 enum_decl != nullptr) {
Jooyung Han719cdce2021-11-24 05:18:43 +0900100 const std::string clazz = NdkFullClassName(*enum_decl, cpp::ClassNames::RAW);
101 return TypeInfo{clazz, true};
Jooyung Han5014b3e2021-10-15 09:58:20 +0900102 } else {
103 AIDL_FATAL(aidl_name) << "Unrecognized type";
104 }
105}
106
Jooyung Han719cdce2021-11-24 05:18:43 +0900107static TypeInfo WrapNullableType(TypeInfo info, bool is_heap) {
108 if (is_heap) {
109 info.cpp_name = "std::unique_ptr<" + info.cpp_name + ">";
110 } else {
111 info.cpp_name = "std::optional<" + info.cpp_name + ">";
112 }
113 info.value_is_cheap = false;
114 return info;
115}
116
Jooyung Han729630b2021-12-01 17:32:54 +0900117static TypeInfo WrapArrayType(TypeInfo info, const ArrayType* array) {
118 AIDL_FATAL_IF(!array, AIDL_LOCATION_HERE) << "not an array";
119 // When "byte"(AIDL) is used in an array, use "uint8_t" because it's more C++ idiomatic.
Jooyung Han719cdce2021-11-24 05:18:43 +0900120 if (info.cpp_name == "int8_t") {
Jooyung Han729630b2021-12-01 17:32:54 +0900121 info.cpp_name = "uint8_t";
122 }
123 if (std::get_if<DynamicArray>(array)) {
Jooyung Han719cdce2021-11-24 05:18:43 +0900124 info.cpp_name = "std::vector<" + info.cpp_name + ">";
Jooyung Han729630b2021-12-01 17:32:54 +0900125 } else {
Jooyung Hanca4f3192021-12-15 11:28:06 +0900126 const auto& dimensions = std::get<FixedSizeArray>(*array).dimensions;
127 for (auto it = rbegin(dimensions), end = rend(dimensions); it != end; it++) {
Jooyung Han729630b2021-12-01 17:32:54 +0900128 info.cpp_name = "std::array<" + info.cpp_name + ", " +
Jooyung Hanca4f3192021-12-15 11:28:06 +0900129 (*it)->ValueString(kIntType, ConstantValueDecorator) + ">";
Jooyung Han729630b2021-12-01 17:32:54 +0900130 }
Jooyung Han719cdce2021-11-24 05:18:43 +0900131 }
132 info.value_is_cheap = false;
133 return info;
134}
135
136static bool ShouldWrapNullable(const AidlTypenames& types, const std::string& aidl_name) {
137 if (AidlTypenames::IsPrimitiveTypename(aidl_name) || aidl_name == "ParcelableHolder" ||
138 aidl_name == "IBinder" || aidl_name == "ParcelFileDescriptor") {
139 return false;
140 }
141 if (auto defined_type = types.TryGetDefinedType(aidl_name); defined_type) {
142 if (defined_type->AsEnumDeclaration() || defined_type->AsInterface()) {
143 return false;
144 }
145 }
146 return true;
147}
148
149static TypeInfo GetTypeInfo(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
Steven Moreland21780812020-09-11 01:29:45 +0000150 AIDL_FATAL_IF(!aidl.IsResolved(), aidl) << aidl.ToString();
Jooyung Han719cdce2021-11-24 05:18:43 +0900151 // Keep original @nullable to handle the case of List<T>. "@nullable" is attached to "List" not
152 // "T"
153 bool is_nullable = aidl.IsNullable();
Jooyung Han729630b2021-12-01 17:32:54 +0900154 const ArrayType* array = nullptr;
Jooyung Han719cdce2021-11-24 05:18:43 +0900155 const AidlTypeSpecifier* element_type = &aidl;
Steven Moreland1cb099e2018-10-17 16:31:08 -0700156
Jooyung Han719cdce2021-11-24 05:18:43 +0900157 // List<T> is converted to T[].
158 if (aidl.GetName() == "List") {
Jooyung Han729630b2021-12-01 17:32:54 +0900159 static const ArrayType kDynamicArray{DynamicArray{}};
160
Steven Morelandfe52c9f2019-11-27 19:04:48 -0800161 AIDL_FATAL_IF(!aidl.IsGeneric(), aidl) << "List must be generic type.";
162 AIDL_FATAL_IF(aidl.GetTypeParameters().size() != 1, aidl)
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900163 << "List can accept only one type parameter.";
Jooyung Han5014b3e2021-10-15 09:58:20 +0900164 const auto& type_param = *aidl.GetTypeParameters()[0];
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900165 // TODO(b/136048684) AIDL doesn't support nested type parameter yet.
Jooyung Han5014b3e2021-10-15 09:58:20 +0900166 AIDL_FATAL_IF(type_param.IsGeneric(), aidl) << "AIDL doesn't support nested type parameter";
Jooyung Han719cdce2021-11-24 05:18:43 +0900167 // Treat "List<T>" as an array of T.
Jooyung Han729630b2021-12-01 17:32:54 +0900168 array = &kDynamicArray;
Jooyung Han719cdce2021-11-24 05:18:43 +0900169 element_type = &type_param;
Jooyung Han729630b2021-12-01 17:32:54 +0900170 } else if (aidl.IsArray()) {
171 array = &aidl.GetArray();
Steven Moreland67caf422018-10-15 12:39:12 -0700172 }
173
Jooyung Han719cdce2021-11-24 05:18:43 +0900174 TypeInfo info = GetBaseTypeInfo(types, *element_type);
175
176 if (is_nullable && ShouldWrapNullable(types, element_type->GetName())) {
177 info = WrapNullableType(info, aidl.IsHeapNullable());
178 }
Jooyung Han729630b2021-12-01 17:32:54 +0900179 if (array) {
180 info = WrapArrayType(info, array);
Jooyung Han719cdce2021-11-24 05:18:43 +0900181 if (is_nullable) {
182 AIDL_FATAL_IF(aidl.IsHeapNullable(), aidl) << "Array/List can't be @nullable(heap=true)";
183 info = WrapNullableType(info, /*is_heap=*/false);
Steven Moreland055d8792018-11-14 12:48:42 -0800184 }
Steven Moreland055d8792018-11-14 12:48:42 -0800185 }
Jooyung Han719cdce2021-11-24 05:18:43 +0900186 return info;
Steven Moreland67caf422018-10-15 12:39:12 -0700187}
188
Steven Moreland2bea13b2018-10-03 15:12:33 -0700189std::string NdkFullClassName(const AidlDefinedType& type, cpp::ClassNames name) {
Steven Moreland63404532018-10-08 14:31:00 -0700190 std::vector<std::string> pieces = {"::aidl"};
Jooyung Han4b832522021-10-06 16:08:30 +0900191 std::vector<std::string> split_name = Split(type.GetCanonicalName(), ".");
192 pieces.insert(pieces.end(), split_name.begin(), split_name.end());
193 // Override name part with cpp::ClassName(type, name)
194 pieces.back() = cpp::ClassName(type, name);
Steven Moreland2bea13b2018-10-03 15:12:33 -0700195 return Join(pieces, "::");
196}
197
198std::string NdkNameOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl, StorageMode mode) {
Jooyung Han719cdce2021-11-24 05:18:43 +0900199 TypeInfo aspect = GetTypeInfo(types, aidl);
Steven Morelandeb38ee72018-10-15 14:20:04 -0700200
Steven Morelande8a3a192018-09-20 14:14:28 -0700201 switch (mode) {
202 case StorageMode::STACK:
Steven Moreland055d8792018-11-14 12:48:42 -0800203 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700204 case StorageMode::ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800205 if (aspect.value_is_cheap) {
206 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700207 } else {
Steven Moreland055d8792018-11-14 12:48:42 -0800208 return "const " + aspect.cpp_name + "&";
Steven Morelande8a3a192018-09-20 14:14:28 -0700209 }
210 case StorageMode::OUT_ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800211 return aspect.cpp_name + "*";
Steven Morelande8a3a192018-09-20 14:14:28 -0700212 default:
213 AIDL_FATAL(aidl.GetName()) << "Unrecognized mode type: " << static_cast<int>(mode);
214 }
215}
216
217void WriteToParcelFor(const CodeGeneratorContext& c) {
Jooyung Han719cdce2021-11-24 05:18:43 +0900218 if (c.type.IsNullable()) {
219 c.writer << "::ndk::AParcel_writeNullableData(" << c.parcel << ", " << c.var << ")";
220 } else {
221 c.writer << "::ndk::AParcel_writeData(" << c.parcel << ", " << c.var << ")";
222 }
Steven Morelande8a3a192018-09-20 14:14:28 -0700223}
224
225void ReadFromParcelFor(const CodeGeneratorContext& c) {
Jooyung Han719cdce2021-11-24 05:18:43 +0900226 if (c.type.IsNullable()) {
227 c.writer << "::ndk::AParcel_readNullableData(" << c.parcel << ", " << c.var << ")";
228 } else {
229 c.writer << "::ndk::AParcel_readData(" << c.parcel << ", " << c.var << ")";
230 }
Steven Morelande8a3a192018-09-20 14:14:28 -0700231}
232
Jiyong Park965c5b92018-11-21 13:37:15 +0900233std::string NdkArgList(
234 const AidlTypenames& types, const AidlMethod& method,
235 std::function<std::string(const std::string& type, const std::string& name, bool isOut)>
236 formatter) {
Steven Morelandaada3422018-09-20 15:55:33 -0700237 std::vector<std::string> method_arguments;
238 for (const auto& a : method.GetArguments()) {
239 StorageMode mode = a->IsOut() ? StorageMode::OUT_ARGUMENT : StorageMode::ARGUMENT;
Steven Moreland2bea13b2018-10-03 15:12:33 -0700240 std::string type = NdkNameOf(types, a->GetType(), mode);
Steven Morelandaada3422018-09-20 15:55:33 -0700241 std::string name = cpp::BuildVarName(*a);
Jiyong Park965c5b92018-11-21 13:37:15 +0900242 method_arguments.emplace_back(formatter(type, name, a->IsOut()));
Steven Morelandaada3422018-09-20 15:55:33 -0700243 }
244
245 if (method.GetType().GetName() != "void") {
Jiyong Park965c5b92018-11-21 13:37:15 +0900246 std::string type = NdkNameOf(types, method.GetType(), StorageMode::OUT_ARGUMENT);
247 std::string name = "_aidl_return";
248 method_arguments.emplace_back(formatter(type, name, true));
Steven Morelandaada3422018-09-20 15:55:33 -0700249 }
250
251 return Join(method_arguments, ", ");
252}
253
Steven Moreland2bea13b2018-10-03 15:12:33 -0700254std::string NdkMethodDecl(const AidlTypenames& types, const AidlMethod& method,
255 const std::string& clazz) {
Steven Morelandaada3422018-09-20 15:55:33 -0700256 std::string class_prefix = clazz.empty() ? "" : (clazz + "::");
Steven Moreland63404532018-10-08 14:31:00 -0700257 return "::ndk::ScopedAStatus " + class_prefix + method.GetName() + "(" +
Jiyong Park965c5b92018-11-21 13:37:15 +0900258 NdkArgList(types, method, FormatArgForDecl) + ")";
Steven Morelandaada3422018-09-20 15:55:33 -0700259}
260
Steven Morelande8a3a192018-09-20 14:14:28 -0700261} // namespace ndk
262} // namespace aidl
263} // namespace android