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