| 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; |
| 26 | using ::android::base::Split; |
| 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 | |
| Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 72 | static std::function<void(const CodeGeneratorContext& c)> StandardRead(const std::string& name) { |
| Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 73 | return [name](const CodeGeneratorContext& c) { |
| Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 74 | c.writer << name << "(" << c.parcel << ", " << c.var << ")"; |
| Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 75 | }; |
| 76 | } |
| Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 77 | static std::function<void(const CodeGeneratorContext& c)> StandardWrite(const std::string& name) { |
| Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 78 | return [name](const CodeGeneratorContext& c) { |
| Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 79 | c.writer << name << "(" << c.parcel << ", " << c.var << ")"; |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | TypeInfo PrimitiveType(const std::string& cpp_name, const std::string& pretty_name) { |
| 84 | return TypeInfo{ |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 85 | .raw = |
| 86 | TypeInfo::Aspect{ |
| 87 | .cpp_name = cpp_name, |
| 88 | .value_is_cheap = true, |
| 89 | .read_func = StandardRead("AParcel_read" + pretty_name), |
| 90 | .write_func = StandardWrite("AParcel_write" + pretty_name), |
| 91 | }, |
| 92 | .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 93 | .cpp_name = "std::vector<" + cpp_name + ">", |
| 94 | .value_is_cheap = false, |
| 95 | .read_func = StandardRead("::ndk::AParcel_readVector"), |
| 96 | .write_func = StandardWrite("::ndk::AParcel_writeVector"), |
| 97 | }), |
| 98 | .nullable = nullptr, |
| Steven Moreland | c10ed46 | 2018-11-15 14:50:28 -0800 | [diff] [blame] | 99 | .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 100 | .cpp_name = "std::optional<std::vector<" + cpp_name + ">>", |
| 101 | .value_is_cheap = false, |
| 102 | .read_func = StandardRead("::ndk::AParcel_readVector"), |
| 103 | .write_func = StandardWrite("::ndk::AParcel_writeVector"), |
| 104 | }), |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 105 | }; |
| 106 | } |
| 107 | |
| 108 | TypeInfo InterfaceTypeInfo(const AidlInterface& type) { |
| 109 | const std::string clazz = NdkFullClassName(type, cpp::ClassNames::INTERFACE); |
| 110 | |
| 111 | return TypeInfo{ |
| 112 | .raw = |
| 113 | TypeInfo::Aspect{ |
| 114 | .cpp_name = "std::shared_ptr<" + clazz + ">", |
| 115 | .value_is_cheap = false, |
| 116 | // TODO(b/111445392): these should be non-null |
| 117 | .read_func = StandardRead(clazz + "::readFromParcel"), |
| 118 | .write_func = StandardWrite(clazz + "::writeToParcel"), |
| 119 | }, |
| 120 | .array = nullptr, |
| 121 | .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 122 | .cpp_name = "std::shared_ptr<" + clazz + ">", |
| 123 | .value_is_cheap = false, |
| 124 | .read_func = StandardRead(clazz + "::readFromParcel"), |
| 125 | .write_func = StandardWrite(clazz + "::writeToParcel"), |
| 126 | }), |
| 127 | .nullable_array = nullptr, |
| 128 | }; |
| 129 | } |
| 130 | |
| 131 | TypeInfo ParcelableTypeInfo(const AidlParcelable& type) { |
| 132 | const std::string clazz = NdkFullClassName(type, cpp::ClassNames::BASE); |
| 133 | |
| 134 | return TypeInfo{ |
| 135 | .raw = |
| 136 | TypeInfo::Aspect{ |
| 137 | .cpp_name = clazz, |
| 138 | .value_is_cheap = false, |
| 139 | .read_func = |
| 140 | [](const CodeGeneratorContext& c) { |
| 141 | c.writer << "(" << c.var << ")->readFromParcel(" << c.parcel << ")"; |
| 142 | }, |
| 143 | .write_func = |
| 144 | [](const CodeGeneratorContext& c) { |
| 145 | c.writer << "(" << c.var << ").writeToParcel(" << c.parcel << ")"; |
| 146 | }, |
| 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 | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 154 | .nullable = nullptr, |
| 155 | .nullable_array = nullptr, |
| Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 156 | }; |
| 157 | } |
| 158 | |
| 159 | // map from AIDL built-in type name to the corresponding Ndk type name |
| 160 | static map<std::string, TypeInfo> kNdkTypeInfoMap = { |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 161 | {"void", TypeInfo{{"void", true, nullptr, nullptr}, nullptr, nullptr, nullptr}}, |
| Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 162 | {"boolean", PrimitiveType("bool", "Bool")}, |
| 163 | {"byte", PrimitiveType("int8_t", "Byte")}, |
| 164 | {"char", PrimitiveType("char16_t", "Char")}, |
| 165 | {"int", PrimitiveType("int32_t", "Int32")}, |
| 166 | {"long", PrimitiveType("int64_t", "Int64")}, |
| 167 | {"float", PrimitiveType("float", "Float")}, |
| 168 | {"double", PrimitiveType("double", "Double")}, |
| Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 169 | {"String", |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 170 | TypeInfo{ |
| 171 | .raw = |
| 172 | TypeInfo::Aspect{ |
| 173 | .cpp_name = "std::string", |
| 174 | .value_is_cheap = false, |
| 175 | .read_func = StandardRead("::ndk::AParcel_readString"), |
| 176 | .write_func = StandardWrite("::ndk::AParcel_writeString"), |
| 177 | }, |
| 178 | .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 179 | .cpp_name = "std::vector<std::string>", |
| 180 | .value_is_cheap = false, |
| 181 | .read_func = StandardRead("::ndk::AParcel_readVector"), |
| 182 | .write_func = StandardWrite("::ndk::AParcel_writeVector"), |
| 183 | }), |
| Steven Moreland | 18ed978 | 2018-11-14 17:08:09 -0800 | [diff] [blame] | 184 | .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 185 | .cpp_name = "std::optional<std::string>", |
| 186 | .value_is_cheap = false, |
| 187 | .read_func = StandardRead("::ndk::AParcel_readString"), |
| 188 | .write_func = StandardWrite("::ndk::AParcel_writeString"), |
| 189 | }), |
| 190 | .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 191 | .cpp_name = "std::optional<std::vector<std::optional<std::string>>>", |
| 192 | .value_is_cheap = false, |
| 193 | .read_func = StandardRead("::ndk::AParcel_readVector"), |
| 194 | .write_func = StandardWrite("::ndk::AParcel_writeVector"), |
| 195 | }), |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 196 | }}, |
| Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 197 | // TODO(b/111445392) {"List", ""}, |
| 198 | // TODO(b/111445392) {"Map", ""}, |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 199 | {"IBinder", |
| 200 | TypeInfo{ |
| 201 | .raw = |
| 202 | TypeInfo::Aspect{ |
| 203 | .cpp_name = "::ndk::SpAIBinder", |
| 204 | .value_is_cheap = false, |
| Steven Moreland | e1048a3 | 2018-11-16 12:52:17 -0800 | [diff] [blame] | 205 | .read_func = StandardRead("::ndk::AParcel_readRequiredStrongBinder"), |
| 206 | .write_func = StandardRead("::ndk::AParcel_writeRequiredStrongBinder"), |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 207 | }, |
| 208 | .array = nullptr, |
| 209 | .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 210 | .cpp_name = "::ndk::SpAIBinder", |
| 211 | .value_is_cheap = false, |
| Steven Moreland | e1048a3 | 2018-11-16 12:52:17 -0800 | [diff] [blame] | 212 | .read_func = StandardRead("::ndk::AParcel_readNullableStrongBinder"), |
| 213 | .write_func = StandardRead("::ndk::AParcel_writeNullableStrongBinder"), |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 214 | }), |
| 215 | .nullable_array = nullptr, |
| 216 | }}, |
| Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 217 | // TODO(b/111445392) {"FileDescriptor", ""}, |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 218 | {"ParcelFileDescriptor", |
| 219 | TypeInfo{ |
| 220 | .raw = |
| 221 | TypeInfo::Aspect{ |
| 222 | .cpp_name = "::ndk::ScopedFileDescriptor", |
| 223 | .value_is_cheap = false, |
| Steven Moreland | 962c60d | 2018-11-16 15:19:27 -0800 | [diff] [blame] | 224 | .read_func = StandardRead("::ndk::AParcel_readRequiredParcelFileDescriptor"), |
| 225 | .write_func = StandardRead("::ndk::AParcel_writeRequiredParcelFileDescriptor"), |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 226 | }, |
| 227 | .array = nullptr, |
| Steven Moreland | 962c60d | 2018-11-16 15:19:27 -0800 | [diff] [blame] | 228 | .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{ |
| 229 | .cpp_name = "::ndk::ScopedFileDescriptor", |
| 230 | .value_is_cheap = false, |
| 231 | .read_func = StandardRead("::ndk::AParcel_readNullableParcelFileDescriptor"), |
| 232 | .write_func = StandardRead("::ndk::AParcel_writeNullableParcelFileDescriptor"), |
| 233 | }), |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 234 | .nullable_array = nullptr, |
| 235 | }}, |
| Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 236 | // TODO(b/111445392) {"CharSequence", ""}, |
| 237 | }; |
| 238 | |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 239 | static TypeInfo::Aspect GetTypeAspect(const AidlTypenames& types, const AidlTypeSpecifier& aidl) { |
| Steven Moreland | 1cb099e | 2018-10-17 16:31:08 -0700 | [diff] [blame] | 240 | CHECK(aidl.IsResolved()) << aidl.ToString(); |
| 241 | |
| Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 242 | const string aidl_name = aidl.GetName(); |
| 243 | |
| Steven Moreland | 1cb099e | 2018-10-17 16:31:08 -0700 | [diff] [blame] | 244 | // TODO(b/112664205): this is okay for some types |
| 245 | AIDL_FATAL_IF(aidl.IsGeneric(), aidl) << aidl.ToString(); |
| Steven Moreland | 1cb099e | 2018-10-17 16:31:08 -0700 | [diff] [blame] | 246 | |
| Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 247 | TypeInfo info; |
| 248 | if (AidlTypenames::IsBuiltinTypename(aidl_name)) { |
| 249 | auto it = kNdkTypeInfoMap.find(aidl_name); |
| 250 | CHECK(it != kNdkTypeInfoMap.end()); |
| 251 | info = it->second; |
| 252 | } else { |
| 253 | const AidlDefinedType* type = types.TryGetDefinedType(aidl_name); |
| Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 254 | AIDL_FATAL_IF(type == nullptr, aidl_name) << "Unrecognized type."; |
| 255 | |
| 256 | if (type->AsInterface() != nullptr) { |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 257 | info = InterfaceTypeInfo(*type->AsInterface()); |
| Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 258 | } else if (type->AsParcelable() != nullptr) { |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 259 | info = ParcelableTypeInfo(*type->AsParcelable()); |
| Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 260 | } else { |
| 261 | AIDL_FATAL(aidl_name) << "Unrecognized type"; |
| 262 | } |
| 263 | } |
| 264 | |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 265 | if (aidl.IsArray()) { |
| 266 | if (aidl.IsNullable()) { |
| 267 | AIDL_FATAL_IF(info.nullable_array == nullptr, aidl) << "Unsupported type in NDK Backend."; |
| 268 | return *info.nullable_array; |
| 269 | } |
| 270 | AIDL_FATAL_IF(info.array == nullptr, aidl) << "Unsupported type in NDK Backend."; |
| 271 | return *info.array; |
| 272 | } |
| Steven Moreland | 1cb099e | 2018-10-17 16:31:08 -0700 | [diff] [blame] | 273 | |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 274 | if (aidl.IsNullable()) { |
| 275 | AIDL_FATAL_IF(info.nullable == nullptr, aidl) << "Unsupported type in NDK Backend."; |
| 276 | return *info.nullable; |
| 277 | } |
| 278 | |
| 279 | return info.raw; |
| Steven Moreland | 67caf42 | 2018-10-15 12:39:12 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 282 | std::string NdkFullClassName(const AidlDefinedType& type, cpp::ClassNames name) { |
| Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 283 | std::vector<std::string> pieces = {"::aidl"}; |
| Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 284 | std::vector<std::string> package = type.GetSplitPackage(); |
| 285 | pieces.insert(pieces.end(), package.begin(), package.end()); |
| 286 | pieces.push_back(cpp::ClassName(type, name)); |
| 287 | |
| 288 | return Join(pieces, "::"); |
| 289 | } |
| 290 | |
| 291 | std::string NdkNameOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl, StorageMode mode) { |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 292 | TypeInfo::Aspect aspect = GetTypeAspect(types, aidl); |
| Steven Moreland | eb38ee7 | 2018-10-15 14:20:04 -0700 | [diff] [blame] | 293 | |
| Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 294 | switch (mode) { |
| 295 | case StorageMode::STACK: |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 296 | return aspect.cpp_name; |
| Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 297 | case StorageMode::ARGUMENT: |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 298 | if (aspect.value_is_cheap) { |
| 299 | return aspect.cpp_name; |
| Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 300 | } else { |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 301 | return "const " + aspect.cpp_name + "&"; |
| Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 302 | } |
| 303 | case StorageMode::OUT_ARGUMENT: |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 304 | return aspect.cpp_name + "*"; |
| Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 305 | default: |
| 306 | AIDL_FATAL(aidl.GetName()) << "Unrecognized mode type: " << static_cast<int>(mode); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | void WriteToParcelFor(const CodeGeneratorContext& c) { |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 311 | TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type); |
| 312 | aspect.write_func(c); |
| Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | void ReadFromParcelFor(const CodeGeneratorContext& c) { |
| Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 316 | TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type); |
| 317 | aspect.read_func(c); |
| Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 320 | std::string NdkArgList( |
| 321 | const AidlTypenames& types, const AidlMethod& method, |
| 322 | std::function<std::string(const std::string& type, const std::string& name, bool isOut)> |
| 323 | formatter) { |
| Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 324 | std::vector<std::string> method_arguments; |
| 325 | for (const auto& a : method.GetArguments()) { |
| 326 | StorageMode mode = a->IsOut() ? StorageMode::OUT_ARGUMENT : StorageMode::ARGUMENT; |
| Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 327 | std::string type = NdkNameOf(types, a->GetType(), mode); |
| Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 328 | std::string name = cpp::BuildVarName(*a); |
| Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 329 | method_arguments.emplace_back(formatter(type, name, a->IsOut())); |
| Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | if (method.GetType().GetName() != "void") { |
| Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 333 | std::string type = NdkNameOf(types, method.GetType(), StorageMode::OUT_ARGUMENT); |
| 334 | std::string name = "_aidl_return"; |
| 335 | method_arguments.emplace_back(formatter(type, name, true)); |
| Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | return Join(method_arguments, ", "); |
| 339 | } |
| 340 | |
| Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 341 | std::string NdkMethodDecl(const AidlTypenames& types, const AidlMethod& method, |
| 342 | const std::string& clazz) { |
| Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 343 | std::string class_prefix = clazz.empty() ? "" : (clazz + "::"); |
| Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 344 | return "::ndk::ScopedAStatus " + class_prefix + method.GetName() + "(" + |
| Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 345 | NdkArgList(types, method, FormatArgForDecl) + ")"; |
| Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| Steven Moreland | e8a3a19 | 2018-09-20 14:14:28 -0700 | [diff] [blame] | 348 | } // namespace ndk |
| 349 | } // namespace aidl |
| 350 | } // namespace android |