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 | |
| 21 | #include <android-base/strings.h> |
| 22 | |
| 23 | #include <functional> |
| 24 | |
| 25 | using ::android::base::Join; |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace aidl { |
| 29 | namespace ndk { |
| 30 | |
Steven Moreland | 7c93337 | 2018-10-11 15:20:04 -0700 | [diff] [blame] | 31 | std::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 Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 37 | // 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] | 38 | struct TypeInfo { |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 39 | 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 Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 44 | |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 45 | std::function<void(const CodeGeneratorContext& c)> read_func; |
| 46 | std::function<void(const CodeGeneratorContext& c)> write_func; |
| 47 | }; |
Steven Moreland | eb38ee7 | 2018-10-15 14:20:04 -0700 | [diff] [blame] | 48 | |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 49 | // 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 Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 71 | std::string ConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value) { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 72 | if (type.GetName() == "long" && !type.IsArray()) { |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 73 | return raw_value + "L"; |
| 74 | } |
| 75 | |
| 76 | return raw_value; |
| 77 | }; |
| 78 | |
Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 79 | static std::function<void(const CodeGeneratorContext& c)> StandardRead(const std::string& name) { |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 80 | return [name](const CodeGeneratorContext& c) { |
Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 81 | c.writer << name << "(" << c.parcel << ", " << c.var << ")"; |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 82 | }; |
| 83 | } |
Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 84 | static std::function<void(const CodeGeneratorContext& c)> StandardWrite(const std::string& name) { |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 85 | return [name](const CodeGeneratorContext& c) { |
Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 86 | c.writer << name << "(" << c.parcel << ", " << c.var << ")"; |
| 87 | }; |
| 88 | } |
| 89 | |
| 90 | TypeInfo PrimitiveType(const std::string& cpp_name, const std::string& pretty_name) { |
| 91 | return TypeInfo{ |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 92 | .raw = |
| 93 | TypeInfo::Aspect{ |
| 94 | .cpp_name = cpp_name, |
| 95 | .value_is_cheap = true, |
| 96 | .read_func = StandardRead("AParcel_read" + pretty_name), |
| 97 | .write_func = StandardWrite("AParcel_write" + pretty_name), |
| 98 | }, |
| 99 | .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 100 | .cpp_name = "std::vector<" + cpp_name + ">", |
| 101 | .value_is_cheap = false, |
| 102 | .read_func = StandardRead("::ndk::AParcel_readVector"), |
| 103 | .write_func = StandardWrite("::ndk::AParcel_writeVector"), |
| 104 | }), |
| 105 | .nullable = nullptr, |
Steven Moreland | c10ed46 | 2018-11-15 14:50:28 -0800 | [diff] [blame] | 106 | .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 107 | .cpp_name = "std::optional<std::vector<" + cpp_name + ">>", |
| 108 | .value_is_cheap = false, |
| 109 | .read_func = StandardRead("::ndk::AParcel_readVector"), |
| 110 | .write_func = StandardWrite("::ndk::AParcel_writeVector"), |
| 111 | }), |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 112 | }; |
| 113 | } |
| 114 | |
| 115 | TypeInfo InterfaceTypeInfo(const AidlInterface& type) { |
| 116 | const std::string clazz = NdkFullClassName(type, cpp::ClassNames::INTERFACE); |
| 117 | |
| 118 | return TypeInfo{ |
| 119 | .raw = |
| 120 | TypeInfo::Aspect{ |
| 121 | .cpp_name = "std::shared_ptr<" + clazz + ">", |
| 122 | .value_is_cheap = false, |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 123 | .read_func = StandardRead(clazz + "::readFromParcel"), |
| 124 | .write_func = StandardWrite(clazz + "::writeToParcel"), |
| 125 | }, |
| 126 | .array = nullptr, |
| 127 | .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 128 | .cpp_name = "std::shared_ptr<" + clazz + ">", |
| 129 | .value_is_cheap = false, |
| 130 | .read_func = StandardRead(clazz + "::readFromParcel"), |
| 131 | .write_func = StandardWrite(clazz + "::writeToParcel"), |
| 132 | }), |
| 133 | .nullable_array = nullptr, |
| 134 | }; |
| 135 | } |
| 136 | |
| 137 | TypeInfo ParcelableTypeInfo(const AidlParcelable& type) { |
Steven Moreland | b8df37d | 2019-11-21 12:33:24 -0800 | [diff] [blame] | 138 | const std::string clazz = NdkFullClassName(type, cpp::ClassNames::RAW); |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 139 | |
| 140 | return TypeInfo{ |
| 141 | .raw = |
| 142 | TypeInfo::Aspect{ |
| 143 | .cpp_name = clazz, |
| 144 | .value_is_cheap = false, |
Steven Moreland | 4348f9a | 2019-12-16 16:33:01 -0800 | [diff] [blame] | 145 | .read_func = StandardRead("::ndk::AParcel_readParcelable"), |
| 146 | .write_func = StandardWrite("::ndk::AParcel_writeParcelable"), |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 147 | }, |
Steven Moreland | 5ecec6b | 2018-12-11 18:56:41 -0800 | [diff] [blame] | 148 | .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 Moreland | 18f7526 | 2019-12-19 16:58:55 -0800 | [diff] [blame] | 154 | .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 155 | .cpp_name = "std::optional<" + clazz + ">", |
| 156 | .value_is_cheap = false, |
| 157 | .read_func = StandardRead("::ndk::AParcel_readNullableParcelable"), |
| 158 | .write_func = StandardWrite("::ndk::AParcel_writeNullableParcelable"), |
| 159 | }), |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 160 | .nullable_array = nullptr, |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 161 | }; |
| 162 | } |
| 163 | |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 164 | TypeInfo EnumDeclarationTypeInfo(const AidlEnumDeclaration& enum_decl) { |
Steven Moreland | b8df37d | 2019-11-21 12:33:24 -0800 | [diff] [blame] | 165 | const std::string clazz = NdkFullClassName(enum_decl, cpp::ClassNames::RAW); |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 166 | |
| 167 | static map<std::string, std::string> kAParcelTypeNameMap = { |
| 168 | {"byte", "Byte"}, |
| 169 | {"int", "Int32"}, |
| 170 | {"long", "Int64"}, |
| 171 | }; |
| 172 | auto aparcel_name_it = kAParcelTypeNameMap.find(enum_decl.GetBackingType().GetName()); |
| 173 | CHECK(aparcel_name_it != kAParcelTypeNameMap.end()); |
| 174 | const std::string aparcel_name = aparcel_name_it->second; |
| 175 | |
| 176 | const std::string backing_type_name = |
| 177 | NdkNameOf(AidlTypenames(), enum_decl.GetBackingType(), StorageMode::STACK); |
| 178 | |
| 179 | return TypeInfo{ |
| 180 | .raw = TypeInfo::Aspect{ |
| 181 | .cpp_name = clazz, |
| 182 | .value_is_cheap = true, |
| 183 | .read_func = |
| 184 | [aparcel_name, backing_type_name](const CodeGeneratorContext& c) { |
| 185 | c.writer << "AParcel_read" << aparcel_name << "(" << c.parcel |
| 186 | << ", reinterpret_cast<" << backing_type_name << "*>(" << c.var << "))"; |
| 187 | }, |
| 188 | .write_func = |
| 189 | [aparcel_name, backing_type_name](const CodeGeneratorContext& c) { |
| 190 | c.writer << "AParcel_write" << aparcel_name << "(" << c.parcel << ", static_cast<" |
| 191 | << backing_type_name << ">(" << c.var << "))"; |
| 192 | }, |
| 193 | }, |
Daniel Norman | ee8674f | 2019-09-20 16:07:00 -0700 | [diff] [blame] | 194 | .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 195 | .cpp_name = "std::vector<" + clazz + ">", |
| 196 | .value_is_cheap = false, |
| 197 | .read_func = |
| 198 | [aparcel_name, backing_type_name](const CodeGeneratorContext& c) { |
| 199 | c.writer << "AParcel_read" << aparcel_name << "Array(" << c.parcel |
| 200 | << ", static_cast<void*>(" << c.var |
| 201 | << "), ndk::AParcel_stdVectorAllocator<" << backing_type_name << ">)"; |
| 202 | }, |
| 203 | .write_func = |
| 204 | [aparcel_name, backing_type_name](const CodeGeneratorContext& c) { |
| 205 | c.writer << "AParcel_write" << aparcel_name << "Array(" << c.parcel |
| 206 | << ", reinterpret_cast<const " << backing_type_name << "*>(" << c.var |
| 207 | << ".data()), " << c.var << ".size())"; |
| 208 | }, |
| 209 | }), |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 210 | .nullable = nullptr, |
Daniel Norman | ee8674f | 2019-09-20 16:07:00 -0700 | [diff] [blame] | 211 | .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 212 | .cpp_name = "std::optional<std::vector<" + clazz + ">>", |
| 213 | .value_is_cheap = false, |
| 214 | .read_func = |
| 215 | [aparcel_name, backing_type_name](const CodeGeneratorContext& c) { |
| 216 | c.writer << "AParcel_read" << aparcel_name << "Array(" << c.parcel |
| 217 | << ", static_cast<void*>(" << c.var |
| 218 | << "), ndk::AParcel_nullableStdVectorAllocator<" << backing_type_name |
| 219 | << ">)"; |
| 220 | }, |
| 221 | .write_func = |
| 222 | [aparcel_name, backing_type_name](const CodeGeneratorContext& c) { |
| 223 | // If the var exists, use writeArray with data() and size(). |
| 224 | // Otherwise, use nullptr and -1. |
| 225 | c.writer << "AParcel_write" << aparcel_name << "Array(" << c.parcel << ", (" |
| 226 | << c.var << " ? reinterpret_cast<const " << backing_type_name << "*>(" |
| 227 | << c.var << "->data()) : nullptr)" |
| 228 | << ", (" << c.var << " ? " << c.var << "->size() : -1))"; |
| 229 | }, |
| 230 | }), |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 231 | }; |
| 232 | } |
| 233 | |
| 234 | // 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] | 235 | static map<std::string, TypeInfo> kNdkTypeInfoMap = { |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 236 | {"void", TypeInfo{{"void", true, nullptr, nullptr}, nullptr, nullptr, nullptr}}, |
Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 237 | {"boolean", PrimitiveType("bool", "Bool")}, |
| 238 | {"byte", PrimitiveType("int8_t", "Byte")}, |
| 239 | {"char", PrimitiveType("char16_t", "Char")}, |
| 240 | {"int", PrimitiveType("int32_t", "Int32")}, |
| 241 | {"long", PrimitiveType("int64_t", "Int64")}, |
| 242 | {"float", PrimitiveType("float", "Float")}, |
| 243 | {"double", PrimitiveType("double", "Double")}, |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 244 | {"String", |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 245 | TypeInfo{ |
| 246 | .raw = |
| 247 | TypeInfo::Aspect{ |
| 248 | .cpp_name = "std::string", |
| 249 | .value_is_cheap = false, |
| 250 | .read_func = StandardRead("::ndk::AParcel_readString"), |
| 251 | .write_func = StandardWrite("::ndk::AParcel_writeString"), |
| 252 | }, |
| 253 | .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 254 | .cpp_name = "std::vector<std::string>", |
| 255 | .value_is_cheap = false, |
| 256 | .read_func = StandardRead("::ndk::AParcel_readVector"), |
| 257 | .write_func = StandardWrite("::ndk::AParcel_writeVector"), |
| 258 | }), |
Steven Moreland | 18ed978 | 2018-11-14 17:08:09 -0800 | [diff] [blame] | 259 | .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 260 | .cpp_name = "std::optional<std::string>", |
| 261 | .value_is_cheap = false, |
| 262 | .read_func = StandardRead("::ndk::AParcel_readString"), |
| 263 | .write_func = StandardWrite("::ndk::AParcel_writeString"), |
| 264 | }), |
| 265 | .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 266 | .cpp_name = "std::optional<std::vector<std::optional<std::string>>>", |
| 267 | .value_is_cheap = false, |
| 268 | .read_func = StandardRead("::ndk::AParcel_readVector"), |
| 269 | .write_func = StandardWrite("::ndk::AParcel_writeVector"), |
| 270 | }), |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 271 | }}, |
Jeongik Cha | 1a0f22d | 2019-11-18 23:22:23 +0900 | [diff] [blame] | 272 | // TODO(b/136048684) {"Map", ""}, |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 273 | {"IBinder", |
| 274 | TypeInfo{ |
| 275 | .raw = |
| 276 | TypeInfo::Aspect{ |
| 277 | .cpp_name = "::ndk::SpAIBinder", |
| 278 | .value_is_cheap = false, |
Steven Moreland | e1048a3 | 2018-11-16 12:52:17 -0800 | [diff] [blame] | 279 | .read_func = StandardRead("::ndk::AParcel_readRequiredStrongBinder"), |
| 280 | .write_func = StandardRead("::ndk::AParcel_writeRequiredStrongBinder"), |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 281 | }, |
| 282 | .array = nullptr, |
| 283 | .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 284 | .cpp_name = "::ndk::SpAIBinder", |
| 285 | .value_is_cheap = false, |
Steven Moreland | e1048a3 | 2018-11-16 12:52:17 -0800 | [diff] [blame] | 286 | .read_func = StandardRead("::ndk::AParcel_readNullableStrongBinder"), |
| 287 | .write_func = StandardRead("::ndk::AParcel_writeNullableStrongBinder"), |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 288 | }), |
| 289 | .nullable_array = nullptr, |
| 290 | }}, |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 291 | {"ParcelFileDescriptor", |
| 292 | TypeInfo{ |
| 293 | .raw = |
| 294 | TypeInfo::Aspect{ |
| 295 | .cpp_name = "::ndk::ScopedFileDescriptor", |
| 296 | .value_is_cheap = false, |
Steven Moreland | 962c60d | 2018-11-16 15:19:27 -0800 | [diff] [blame] | 297 | .read_func = StandardRead("::ndk::AParcel_readRequiredParcelFileDescriptor"), |
| 298 | .write_func = StandardRead("::ndk::AParcel_writeRequiredParcelFileDescriptor"), |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 299 | }, |
Jeongik Cha | cc8dd1d | 2019-11-14 10:22:57 +0900 | [diff] [blame] | 300 | .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 301 | .cpp_name = "std::vector<::ndk::ScopedFileDescriptor>", |
| 302 | .value_is_cheap = false, |
| 303 | .read_func = StandardRead("::ndk::AParcel_readVector"), |
| 304 | .write_func = StandardWrite("::ndk::AParcel_writeVector"), |
| 305 | }), |
Steven Moreland | 962c60d | 2018-11-16 15:19:27 -0800 | [diff] [blame] | 306 | .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 307 | .cpp_name = "::ndk::ScopedFileDescriptor", |
| 308 | .value_is_cheap = false, |
| 309 | .read_func = StandardRead("::ndk::AParcel_readNullableParcelFileDescriptor"), |
| 310 | .write_func = StandardRead("::ndk::AParcel_writeNullableParcelFileDescriptor"), |
| 311 | }), |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 312 | .nullable_array = nullptr, |
| 313 | }}, |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 314 | }; |
| 315 | |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 316 | static TypeInfo::Aspect GetTypeAspect(const AidlTypenames& types, const AidlTypeSpecifier& aidl) { |
Steven Moreland | 1cb099e | 2018-10-17 16:31:08 -0700 | [diff] [blame] | 317 | CHECK(aidl.IsResolved()) << aidl.ToString(); |
Jeongik Cha | 1a0f22d | 2019-11-18 23:22:23 +0900 | [diff] [blame] | 318 | auto& aidl_name = aidl.GetName(); |
Steven Moreland | 1cb099e | 2018-10-17 16:31:08 -0700 | [diff] [blame] | 319 | |
Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 320 | TypeInfo info; |
Jeongik Cha | 1a0f22d | 2019-11-18 23:22:23 +0900 | [diff] [blame] | 321 | |
| 322 | // TODO(b/136048684): For now, List<T> is converted to T[].(Both are using vector<T>) |
| 323 | if (aidl_name == "List") { |
Steven Moreland | fe52c9f | 2019-11-27 19:04:48 -0800 | [diff] [blame] | 324 | AIDL_FATAL_IF(!aidl.IsGeneric(), aidl) << "List must be generic type."; |
| 325 | AIDL_FATAL_IF(aidl.GetTypeParameters().size() != 1, aidl) |
Jeongik Cha | 1a0f22d | 2019-11-18 23:22:23 +0900 | [diff] [blame] | 326 | << "List can accept only one type parameter."; |
| 327 | auto& type_param = aidl.GetTypeParameters()[0]; |
| 328 | // TODO(b/136048684) AIDL doesn't support nested type parameter yet. |
| 329 | AIDL_FATAL_IF(type_param->IsGeneric(), aidl) << "AIDL doesn't support nested type parameter"; |
| 330 | |
| 331 | AidlTypeSpecifier array_type = |
| 332 | AidlTypeSpecifier(AIDL_LOCATION_HERE, type_param->GetUnresolvedName(), true /* isArray */, |
| 333 | nullptr /* type_params */, aidl.GetComments()); |
| 334 | if (!(array_type.Resolve(types) && array_type.CheckValid(types))) { |
| 335 | AIDL_FATAL(aidl) << "The type parameter is wrong."; |
| 336 | } |
| 337 | return GetTypeAspect(types, array_type); |
| 338 | } |
| 339 | |
Steven Moreland | a6a0b75 | 2020-01-16 15:01:09 -0800 | [diff] [blame] | 340 | // All generic types should be handled above. |
Jeongik Cha | 1a0f22d | 2019-11-18 23:22:23 +0900 | [diff] [blame] | 341 | AIDL_FATAL_IF(aidl.IsGeneric(), aidl); |
| 342 | |
Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 343 | if (AidlTypenames::IsBuiltinTypename(aidl_name)) { |
| 344 | auto it = kNdkTypeInfoMap.find(aidl_name); |
| 345 | CHECK(it != kNdkTypeInfoMap.end()); |
| 346 | info = it->second; |
| 347 | } else { |
| 348 | const AidlDefinedType* type = types.TryGetDefinedType(aidl_name); |
Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 349 | AIDL_FATAL_IF(type == nullptr, aidl_name) << "Unrecognized type."; |
| 350 | |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 351 | if (const AidlInterface* intf = type->AsInterface(); intf != nullptr) { |
| 352 | info = InterfaceTypeInfo(*intf); |
| 353 | } else if (const AidlParcelable* parcelable = type->AsParcelable(); parcelable != nullptr) { |
| 354 | info = ParcelableTypeInfo(*parcelable); |
| 355 | } else if (const AidlEnumDeclaration* enum_decl = type->AsEnumDeclaration(); |
| 356 | enum_decl != nullptr) { |
| 357 | info = EnumDeclarationTypeInfo(*enum_decl); |
Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 358 | } else { |
| 359 | AIDL_FATAL(aidl_name) << "Unrecognized type"; |
| 360 | } |
| 361 | } |
| 362 | |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 363 | if (aidl.IsArray()) { |
| 364 | if (aidl.IsNullable()) { |
| 365 | AIDL_FATAL_IF(info.nullable_array == nullptr, aidl) << "Unsupported type in NDK Backend."; |
| 366 | return *info.nullable_array; |
| 367 | } |
| 368 | AIDL_FATAL_IF(info.array == nullptr, aidl) << "Unsupported type in NDK Backend."; |
| 369 | return *info.array; |
| 370 | } |
Steven Moreland | 1cb099e | 2018-10-17 16:31:08 -0700 | [diff] [blame] | 371 | |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 372 | if (aidl.IsNullable()) { |
| 373 | AIDL_FATAL_IF(info.nullable == nullptr, aidl) << "Unsupported type in NDK Backend."; |
| 374 | return *info.nullable; |
| 375 | } |
| 376 | |
| 377 | return info.raw; |
Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 380 | std::string NdkFullClassName(const AidlDefinedType& type, cpp::ClassNames name) { |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 381 | std::vector<std::string> pieces = {"::aidl"}; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 382 | std::vector<std::string> package = type.GetSplitPackage(); |
| 383 | pieces.insert(pieces.end(), package.begin(), package.end()); |
| 384 | pieces.push_back(cpp::ClassName(type, name)); |
| 385 | |
| 386 | return Join(pieces, "::"); |
| 387 | } |
| 388 | |
| 389 | std::string NdkNameOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl, StorageMode mode) { |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 390 | TypeInfo::Aspect aspect = GetTypeAspect(types, aidl); |
Steven Moreland | eb38ee7 | 2018-10-15 14:20:04 -0700 | [diff] [blame] | 391 | |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 392 | switch (mode) { |
| 393 | case StorageMode::STACK: |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 394 | return aspect.cpp_name; |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 395 | case StorageMode::ARGUMENT: |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 396 | if (aspect.value_is_cheap) { |
| 397 | return aspect.cpp_name; |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 398 | } else { |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 399 | return "const " + aspect.cpp_name + "&"; |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 400 | } |
| 401 | case StorageMode::OUT_ARGUMENT: |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 402 | return aspect.cpp_name + "*"; |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 403 | default: |
| 404 | AIDL_FATAL(aidl.GetName()) << "Unrecognized mode type: " << static_cast<int>(mode); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | void WriteToParcelFor(const CodeGeneratorContext& c) { |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 409 | TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type); |
| 410 | aspect.write_func(c); |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | void ReadFromParcelFor(const CodeGeneratorContext& c) { |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 414 | TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type); |
| 415 | aspect.read_func(c); |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 418 | std::string NdkArgList( |
| 419 | const AidlTypenames& types, const AidlMethod& method, |
| 420 | std::function<std::string(const std::string& type, const std::string& name, bool isOut)> |
| 421 | formatter) { |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 422 | std::vector<std::string> method_arguments; |
| 423 | for (const auto& a : method.GetArguments()) { |
| 424 | StorageMode mode = a->IsOut() ? StorageMode::OUT_ARGUMENT : StorageMode::ARGUMENT; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 425 | std::string type = NdkNameOf(types, a->GetType(), mode); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 426 | std::string name = cpp::BuildVarName(*a); |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 427 | method_arguments.emplace_back(formatter(type, name, a->IsOut())); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | if (method.GetType().GetName() != "void") { |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 431 | std::string type = NdkNameOf(types, method.GetType(), StorageMode::OUT_ARGUMENT); |
| 432 | std::string name = "_aidl_return"; |
| 433 | method_arguments.emplace_back(formatter(type, name, true)); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | return Join(method_arguments, ", "); |
| 437 | } |
| 438 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 439 | std::string NdkMethodDecl(const AidlTypenames& types, const AidlMethod& method, |
| 440 | const std::string& clazz) { |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 441 | std::string class_prefix = clazz.empty() ? "" : (clazz + "::"); |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 442 | return "::ndk::ScopedAStatus " + class_prefix + method.GetName() + "(" + |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 443 | NdkArgList(types, method, FormatArgForDecl) + ")"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 446 | } // namespace ndk |
| 447 | } // namespace aidl |
| 448 | } // namespace android |