blob: b5592d773009f6482d9e29f0e7e0a42bbb6cadd2 [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 {
126 for (const auto& dim : std::get<FixedSizeArray>(*array).dimensions) {
127 info.cpp_name = "std::array<" + info.cpp_name + ", " +
128 dim->ValueString(kIntType, ConstantValueDecorator) + ">";
129 }
Jooyung Han719cdce2021-11-24 05:18:43 +0900130 }
131 info.value_is_cheap = false;
132 return info;
133}
134
135static bool ShouldWrapNullable(const AidlTypenames& types, const std::string& aidl_name) {
136 if (AidlTypenames::IsPrimitiveTypename(aidl_name) || aidl_name == "ParcelableHolder" ||
137 aidl_name == "IBinder" || aidl_name == "ParcelFileDescriptor") {
138 return false;
139 }
140 if (auto defined_type = types.TryGetDefinedType(aidl_name); defined_type) {
141 if (defined_type->AsEnumDeclaration() || defined_type->AsInterface()) {
142 return false;
143 }
144 }
145 return true;
146}
147
148static TypeInfo GetTypeInfo(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
Steven Moreland21780812020-09-11 01:29:45 +0000149 AIDL_FATAL_IF(!aidl.IsResolved(), aidl) << aidl.ToString();
Jooyung Han719cdce2021-11-24 05:18:43 +0900150 // Keep original @nullable to handle the case of List<T>. "@nullable" is attached to "List" not
151 // "T"
152 bool is_nullable = aidl.IsNullable();
Jooyung Han729630b2021-12-01 17:32:54 +0900153 const ArrayType* array = nullptr;
Jooyung Han719cdce2021-11-24 05:18:43 +0900154 const AidlTypeSpecifier* element_type = &aidl;
Steven Moreland1cb099e2018-10-17 16:31:08 -0700155
Jooyung Han719cdce2021-11-24 05:18:43 +0900156 // List<T> is converted to T[].
157 if (aidl.GetName() == "List") {
Jooyung Han729630b2021-12-01 17:32:54 +0900158 static const ArrayType kDynamicArray{DynamicArray{}};
159
Steven Morelandfe52c9f2019-11-27 19:04:48 -0800160 AIDL_FATAL_IF(!aidl.IsGeneric(), aidl) << "List must be generic type.";
161 AIDL_FATAL_IF(aidl.GetTypeParameters().size() != 1, aidl)
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900162 << "List can accept only one type parameter.";
Jooyung Han5014b3e2021-10-15 09:58:20 +0900163 const auto& type_param = *aidl.GetTypeParameters()[0];
Jeongik Cha1a0f22d2019-11-18 23:22:23 +0900164 // TODO(b/136048684) AIDL doesn't support nested type parameter yet.
Jooyung Han5014b3e2021-10-15 09:58:20 +0900165 AIDL_FATAL_IF(type_param.IsGeneric(), aidl) << "AIDL doesn't support nested type parameter";
Jooyung Han719cdce2021-11-24 05:18:43 +0900166 // Treat "List<T>" as an array of T.
Jooyung Han729630b2021-12-01 17:32:54 +0900167 array = &kDynamicArray;
Jooyung Han719cdce2021-11-24 05:18:43 +0900168 element_type = &type_param;
Jooyung Han729630b2021-12-01 17:32:54 +0900169 } else if (aidl.IsArray()) {
170 array = &aidl.GetArray();
Steven Moreland67caf422018-10-15 12:39:12 -0700171 }
172
Jooyung Han719cdce2021-11-24 05:18:43 +0900173 TypeInfo info = GetBaseTypeInfo(types, *element_type);
174
175 if (is_nullable && ShouldWrapNullable(types, element_type->GetName())) {
176 info = WrapNullableType(info, aidl.IsHeapNullable());
177 }
Jooyung Han729630b2021-12-01 17:32:54 +0900178 if (array) {
179 info = WrapArrayType(info, array);
Jooyung Han719cdce2021-11-24 05:18:43 +0900180 if (is_nullable) {
181 AIDL_FATAL_IF(aidl.IsHeapNullable(), aidl) << "Array/List can't be @nullable(heap=true)";
182 info = WrapNullableType(info, /*is_heap=*/false);
Steven Moreland055d8792018-11-14 12:48:42 -0800183 }
Steven Moreland055d8792018-11-14 12:48:42 -0800184 }
Jooyung Han719cdce2021-11-24 05:18:43 +0900185 return info;
Steven Moreland67caf422018-10-15 12:39:12 -0700186}
187
Steven Moreland2bea13b2018-10-03 15:12:33 -0700188std::string NdkFullClassName(const AidlDefinedType& type, cpp::ClassNames name) {
Steven Moreland63404532018-10-08 14:31:00 -0700189 std::vector<std::string> pieces = {"::aidl"};
Jooyung Han4b832522021-10-06 16:08:30 +0900190 std::vector<std::string> split_name = Split(type.GetCanonicalName(), ".");
191 pieces.insert(pieces.end(), split_name.begin(), split_name.end());
192 // Override name part with cpp::ClassName(type, name)
193 pieces.back() = cpp::ClassName(type, name);
Steven Moreland2bea13b2018-10-03 15:12:33 -0700194 return Join(pieces, "::");
195}
196
197std::string NdkNameOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl, StorageMode mode) {
Jooyung Han719cdce2021-11-24 05:18:43 +0900198 TypeInfo aspect = GetTypeInfo(types, aidl);
Steven Morelandeb38ee72018-10-15 14:20:04 -0700199
Steven Morelande8a3a192018-09-20 14:14:28 -0700200 switch (mode) {
201 case StorageMode::STACK:
Steven Moreland055d8792018-11-14 12:48:42 -0800202 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700203 case StorageMode::ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800204 if (aspect.value_is_cheap) {
205 return aspect.cpp_name;
Steven Morelande8a3a192018-09-20 14:14:28 -0700206 } else {
Steven Moreland055d8792018-11-14 12:48:42 -0800207 return "const " + aspect.cpp_name + "&";
Steven Morelande8a3a192018-09-20 14:14:28 -0700208 }
209 case StorageMode::OUT_ARGUMENT:
Steven Moreland055d8792018-11-14 12:48:42 -0800210 return aspect.cpp_name + "*";
Steven Morelande8a3a192018-09-20 14:14:28 -0700211 default:
212 AIDL_FATAL(aidl.GetName()) << "Unrecognized mode type: " << static_cast<int>(mode);
213 }
214}
215
216void WriteToParcelFor(const CodeGeneratorContext& c) {
Jooyung Han719cdce2021-11-24 05:18:43 +0900217 if (c.type.IsNullable()) {
218 c.writer << "::ndk::AParcel_writeNullableData(" << c.parcel << ", " << c.var << ")";
219 } else {
220 c.writer << "::ndk::AParcel_writeData(" << c.parcel << ", " << c.var << ")";
221 }
Steven Morelande8a3a192018-09-20 14:14:28 -0700222}
223
224void ReadFromParcelFor(const CodeGeneratorContext& c) {
Jooyung Han719cdce2021-11-24 05:18:43 +0900225 if (c.type.IsNullable()) {
226 c.writer << "::ndk::AParcel_readNullableData(" << c.parcel << ", " << c.var << ")";
227 } else {
228 c.writer << "::ndk::AParcel_readData(" << c.parcel << ", " << c.var << ")";
229 }
Steven Morelande8a3a192018-09-20 14:14:28 -0700230}
231
Jiyong Park965c5b92018-11-21 13:37:15 +0900232std::string NdkArgList(
233 const AidlTypenames& types, const AidlMethod& method,
234 std::function<std::string(const std::string& type, const std::string& name, bool isOut)>
235 formatter) {
Steven Morelandaada3422018-09-20 15:55:33 -0700236 std::vector<std::string> method_arguments;
237 for (const auto& a : method.GetArguments()) {
238 StorageMode mode = a->IsOut() ? StorageMode::OUT_ARGUMENT : StorageMode::ARGUMENT;
Steven Moreland2bea13b2018-10-03 15:12:33 -0700239 std::string type = NdkNameOf(types, a->GetType(), mode);
Steven Morelandaada3422018-09-20 15:55:33 -0700240 std::string name = cpp::BuildVarName(*a);
Jiyong Park965c5b92018-11-21 13:37:15 +0900241 method_arguments.emplace_back(formatter(type, name, a->IsOut()));
Steven Morelandaada3422018-09-20 15:55:33 -0700242 }
243
244 if (method.GetType().GetName() != "void") {
Jiyong Park965c5b92018-11-21 13:37:15 +0900245 std::string type = NdkNameOf(types, method.GetType(), StorageMode::OUT_ARGUMENT);
246 std::string name = "_aidl_return";
247 method_arguments.emplace_back(formatter(type, name, true));
Steven Morelandaada3422018-09-20 15:55:33 -0700248 }
249
250 return Join(method_arguments, ", ");
251}
252
Steven Moreland2bea13b2018-10-03 15:12:33 -0700253std::string NdkMethodDecl(const AidlTypenames& types, const AidlMethod& method,
254 const std::string& clazz) {
Steven Morelandaada3422018-09-20 15:55:33 -0700255 std::string class_prefix = clazz.empty() ? "" : (clazz + "::");
Steven Moreland63404532018-10-08 14:31:00 -0700256 return "::ndk::ScopedAStatus " + class_prefix + method.GetName() + "(" +
Jiyong Park965c5b92018-11-21 13:37:15 +0900257 NdkArgList(types, method, FormatArgForDecl) + ")";
Steven Morelandaada3422018-09-20 15:55:33 -0700258}
259
Steven Morelande8a3a192018-09-20 14:14:28 -0700260} // namespace ndk
261} // namespace aidl
262} // namespace android