Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 1 | /* |
| 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 Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 17 | #include "aidl_to_cpp_common.h" |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 18 | #include "logging.h" |
Steven Moreland | 7c93337 | 2018-10-11 15:20:04 -0700 | [diff] [blame] | 19 | #include "os.h" |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 20 | |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 21 | #include <android-base/stringprintf.h> |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 22 | #include <android-base/strings.h> |
| 23 | |
| 24 | #include <functional> |
| 25 | |
| 26 | using ::android::base::Join; |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace aidl { |
| 30 | namespace ndk { |
| 31 | |
Steven Moreland | 7c93337 | 2018-10-11 15:20:04 -0700 | [diff] [blame] | 32 | std::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 Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 38 | // This represents a type in AIDL (e.g. 'String' which can be referenced in multiple ways) |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 39 | struct TypeInfo { |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 40 | 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 Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 45 | |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 46 | std::function<void(const CodeGeneratorContext& c)> read_func; |
| 47 | std::function<void(const CodeGeneratorContext& c)> write_func; |
| 48 | }; |
Steven Moreland | eb38ee7 | 2018-10-15 14:20:04 -0700 | [diff] [blame] | 49 | |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 50 | // 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 Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 72 | std::string ConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 73 | if (type.GetName() == "long" && !type.IsArray()) { |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 74 | return raw_value + "L"; |
| 75 | } |
| 76 | |
| 77 | return raw_value; |
| 78 | }; |
| 79 | |
Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 80 | static std::function<void(const CodeGeneratorContext& c)> StandardRead(const std::string& name) { |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 81 | return [name](const CodeGeneratorContext& c) { |
Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 82 | c.writer << name << "(" << c.parcel << ", " << c.var << ")"; |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 83 | }; |
| 84 | } |
Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 85 | static std::function<void(const CodeGeneratorContext& c)> StandardWrite(const std::string& name) { |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 86 | return [name](const CodeGeneratorContext& c) { |
Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 87 | c.writer << name << "(" << c.parcel << ", " << c.var << ")"; |
| 88 | }; |
| 89 | } |
| 90 | |
Jooyung Han | 241fdda | 2020-02-21 21:17:44 +0900 | [diff] [blame] | 91 | TypeInfo 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 Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 94 | return TypeInfo{ |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 95 | .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 Han | 241fdda | 2020-02-21 21:17:44 +0900 | [diff] [blame] | 103 | .cpp_name = "std::vector<" + cpp_name_for_array + ">", |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 104 | .value_is_cheap = false, |
| 105 | .read_func = StandardRead("::ndk::AParcel_readVector"), |
| 106 | .write_func = StandardWrite("::ndk::AParcel_writeVector"), |
| 107 | }), |
| 108 | .nullable = nullptr, |
Steven Moreland | c10ed46 | 2018-11-15 14:50:28 -0800 | [diff] [blame] | 109 | .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
Jooyung Han | 241fdda | 2020-02-21 21:17:44 +0900 | [diff] [blame] | 110 | .cpp_name = "std::optional<std::vector<" + cpp_name_for_array + ">>", |
Steven Moreland | c10ed46 | 2018-11-15 14:50:28 -0800 | [diff] [blame] | 111 | .value_is_cheap = false, |
| 112 | .read_func = StandardRead("::ndk::AParcel_readVector"), |
| 113 | .write_func = StandardWrite("::ndk::AParcel_writeVector"), |
| 114 | }), |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 115 | }; |
| 116 | } |
| 117 | |
| 118 | TypeInfo 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 Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 126 | .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 Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 140 | TypeInfo 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 Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 151 | return TypeInfo{ |
| 152 | .raw = |
| 153 | TypeInfo::Aspect{ |
| 154 | .cpp_name = clazz, |
| 155 | .value_is_cheap = false, |
Steven Moreland | 4348f9a | 2019-12-16 16:33:01 -0800 | [diff] [blame] | 156 | .read_func = StandardRead("::ndk::AParcel_readParcelable"), |
| 157 | .write_func = StandardWrite("::ndk::AParcel_writeParcelable"), |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 158 | }, |
Steven Moreland | 5ecec6b | 2018-12-11 18:56:41 -0800 | [diff] [blame] | 159 | .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 Moreland | 18f7526 | 2019-12-19 16:58:55 -0800 | [diff] [blame] | 165 | .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 Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 171 | .nullable_array = nullptr, |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 172 | }; |
| 173 | } |
| 174 | |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 175 | TypeInfo EnumDeclarationTypeInfo(const AidlEnumDeclaration& enum_decl) { |
Steven Moreland | b8df37d | 2019-11-21 12:33:24 -0800 | [diff] [blame] | 176 | const std::string clazz = NdkFullClassName(enum_decl, cpp::ClassNames::RAW); |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 177 | |
| 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 Norman | ee8674f | 2019-09-20 16:07:00 -0700 | [diff] [blame] | 205 | .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 Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 221 | .nullable = nullptr, |
Daniel Norman | ee8674f | 2019-09-20 16:07:00 -0700 | [diff] [blame] | 222 | .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 Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 242 | }; |
| 243 | } |
| 244 | |
| 245 | // map from AIDL built-in type name to the corresponding Ndk type info |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 246 | static map<std::string, TypeInfo> kNdkTypeInfoMap = { |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 247 | {"void", TypeInfo{{"void", true, nullptr, nullptr}, nullptr, nullptr, nullptr}}, |
Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 248 | {"boolean", PrimitiveType("bool", "Bool")}, |
Jooyung Han | 241fdda | 2020-02-21 21:17:44 +0900 | [diff] [blame] | 249 | {"byte", PrimitiveType("int8_t", "Byte", "uint8_t")}, |
Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 250 | {"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 Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 255 | {"String", |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 256 | 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 Moreland | 18ed978 | 2018-11-14 17:08:09 -0800 | [diff] [blame] | 270 | .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 Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 282 | }}, |
Jeongik Cha | 1a0f22d | 2019-11-18 23:22:23 +0900 | [diff] [blame] | 283 | // TODO(b/136048684) {"Map", ""}, |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 284 | {"IBinder", |
| 285 | TypeInfo{ |
| 286 | .raw = |
| 287 | TypeInfo::Aspect{ |
| 288 | .cpp_name = "::ndk::SpAIBinder", |
| 289 | .value_is_cheap = false, |
Steven Moreland | e1048a3 | 2018-11-16 12:52:17 -0800 | [diff] [blame] | 290 | .read_func = StandardRead("::ndk::AParcel_readRequiredStrongBinder"), |
| 291 | .write_func = StandardRead("::ndk::AParcel_writeRequiredStrongBinder"), |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 292 | }, |
| 293 | .array = nullptr, |
| 294 | .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 295 | .cpp_name = "::ndk::SpAIBinder", |
| 296 | .value_is_cheap = false, |
Steven Moreland | e1048a3 | 2018-11-16 12:52:17 -0800 | [diff] [blame] | 297 | .read_func = StandardRead("::ndk::AParcel_readNullableStrongBinder"), |
| 298 | .write_func = StandardRead("::ndk::AParcel_writeNullableStrongBinder"), |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 299 | }), |
| 300 | .nullable_array = nullptr, |
| 301 | }}, |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 302 | {"ParcelFileDescriptor", |
| 303 | TypeInfo{ |
| 304 | .raw = |
| 305 | TypeInfo::Aspect{ |
| 306 | .cpp_name = "::ndk::ScopedFileDescriptor", |
| 307 | .value_is_cheap = false, |
Steven Moreland | 962c60d | 2018-11-16 15:19:27 -0800 | [diff] [blame] | 308 | .read_func = StandardRead("::ndk::AParcel_readRequiredParcelFileDescriptor"), |
| 309 | .write_func = StandardRead("::ndk::AParcel_writeRequiredParcelFileDescriptor"), |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 310 | }, |
Jeongik Cha | cc8dd1d | 2019-11-14 10:22:57 +0900 | [diff] [blame] | 311 | .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 Moreland | 962c60d | 2018-11-16 15:19:27 -0800 | [diff] [blame] | 317 | .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 Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 323 | .nullable_array = nullptr, |
| 324 | }}, |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 325 | }; |
| 326 | |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 327 | static TypeInfo::Aspect GetTypeAspect(const AidlTypenames& types, const AidlTypeSpecifier& aidl) { |
Steven Moreland | 1cb099e | 2018-10-17 16:31:08 -0700 | [diff] [blame] | 328 | CHECK(aidl.IsResolved()) << aidl.ToString(); |
Jeongik Cha | 1a0f22d | 2019-11-18 23:22:23 +0900 | [diff] [blame] | 329 | auto& aidl_name = aidl.GetName(); |
Steven Moreland | 1cb099e | 2018-10-17 16:31:08 -0700 | [diff] [blame] | 330 | |
Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 331 | TypeInfo info; |
Jeongik Cha | 1a0f22d | 2019-11-18 23:22:23 +0900 | [diff] [blame] | 332 | |
| 333 | // TODO(b/136048684): For now, List<T> is converted to T[].(Both are using vector<T>) |
| 334 | if (aidl_name == "List") { |
Steven Moreland | fe52c9f | 2019-11-27 19:04:48 -0800 | [diff] [blame] | 335 | AIDL_FATAL_IF(!aidl.IsGeneric(), aidl) << "List must be generic type."; |
| 336 | AIDL_FATAL_IF(aidl.GetTypeParameters().size() != 1, aidl) |
Jeongik Cha | 1a0f22d | 2019-11-18 23:22:23 +0900 | [diff] [blame] | 337 | << "List can accept only one type parameter."; |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 338 | const auto& type_param = aidl.GetTypeParameters()[0]; |
Jeongik Cha | 1a0f22d | 2019-11-18 23:22:23 +0900 | [diff] [blame] | 339 | // 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 Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 351 | 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 Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 357 | AIDL_FATAL_IF(type == nullptr, aidl_name) << "Unrecognized type."; |
| 358 | |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 359 | if (const AidlInterface* intf = type->AsInterface(); intf != nullptr) { |
| 360 | info = InterfaceTypeInfo(*intf); |
| 361 | } else if (const AidlParcelable* parcelable = type->AsParcelable(); parcelable != nullptr) { |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 362 | info = ParcelableTypeInfo(*parcelable, aidl, types); |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 363 | } else if (const AidlEnumDeclaration* enum_decl = type->AsEnumDeclaration(); |
| 364 | enum_decl != nullptr) { |
| 365 | info = EnumDeclarationTypeInfo(*enum_decl); |
Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 366 | } else { |
| 367 | AIDL_FATAL(aidl_name) << "Unrecognized type"; |
| 368 | } |
| 369 | } |
| 370 | |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 371 | if (aidl.IsArray()) { |
| 372 | if (aidl.IsNullable()) { |
Steven Moreland | d59e317 | 2020-05-11 16:42:09 -0700 | [diff] [blame] | 373 | AIDL_FATAL_IF(info.nullable_array == nullptr, aidl) |
| 374 | << "Unsupported type in NDK Backend: " << aidl.ToString(); |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 375 | return *info.nullable_array; |
| 376 | } |
Steven Moreland | d59e317 | 2020-05-11 16:42:09 -0700 | [diff] [blame] | 377 | AIDL_FATAL_IF(info.array == nullptr, aidl) |
| 378 | << "Unsupported type in NDK Backend: " << aidl.ToString(); |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 379 | return *info.array; |
| 380 | } |
Steven Moreland | 1cb099e | 2018-10-17 16:31:08 -0700 | [diff] [blame] | 381 | |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 382 | if (aidl.IsNullable()) { |
Steven Moreland | d59e317 | 2020-05-11 16:42:09 -0700 | [diff] [blame] | 383 | AIDL_FATAL_IF(info.nullable == nullptr, aidl) |
| 384 | << "Unsupported type in NDK Backend: " << aidl.ToString(); |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 385 | return *info.nullable; |
| 386 | } |
| 387 | |
| 388 | return info.raw; |
Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 391 | std::string NdkFullClassName(const AidlDefinedType& type, cpp::ClassNames name) { |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 392 | std::vector<std::string> pieces = {"::aidl"}; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 393 | 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 | |
| 400 | std::string NdkNameOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl, StorageMode mode) { |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 401 | TypeInfo::Aspect aspect = GetTypeAspect(types, aidl); |
Steven Moreland | eb38ee7 | 2018-10-15 14:20:04 -0700 | [diff] [blame] | 402 | |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 403 | switch (mode) { |
| 404 | case StorageMode::STACK: |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 405 | return aspect.cpp_name; |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 406 | case StorageMode::ARGUMENT: |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 407 | if (aspect.value_is_cheap) { |
| 408 | return aspect.cpp_name; |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 409 | } else { |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 410 | return "const " + aspect.cpp_name + "&"; |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 411 | } |
| 412 | case StorageMode::OUT_ARGUMENT: |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 413 | return aspect.cpp_name + "*"; |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 414 | default: |
| 415 | AIDL_FATAL(aidl.GetName()) << "Unrecognized mode type: " << static_cast<int>(mode); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | void WriteToParcelFor(const CodeGeneratorContext& c) { |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 420 | TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type); |
| 421 | aspect.write_func(c); |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | void ReadFromParcelFor(const CodeGeneratorContext& c) { |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 425 | TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type); |
| 426 | aspect.read_func(c); |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 429 | std::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 Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 433 | std::vector<std::string> method_arguments; |
| 434 | for (const auto& a : method.GetArguments()) { |
| 435 | StorageMode mode = a->IsOut() ? StorageMode::OUT_ARGUMENT : StorageMode::ARGUMENT; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 436 | std::string type = NdkNameOf(types, a->GetType(), mode); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 437 | std::string name = cpp::BuildVarName(*a); |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 438 | method_arguments.emplace_back(formatter(type, name, a->IsOut())); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | if (method.GetType().GetName() != "void") { |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 442 | 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 Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | return Join(method_arguments, ", "); |
| 448 | } |
| 449 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 450 | std::string NdkMethodDecl(const AidlTypenames& types, const AidlMethod& method, |
| 451 | const std::string& clazz) { |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 452 | std::string class_prefix = clazz.empty() ? "" : (clazz + "::"); |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 453 | return "::ndk::ScopedAStatus " + class_prefix + method.GetName() + "(" + |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 454 | NdkArgList(types, method, FormatArgForDecl) + ")"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 457 | } // namespace ndk |
| 458 | } // namespace aidl |
| 459 | } // namespace android |