Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020, 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 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "generate_rust.h" |
| 18 | |
Andrei Homescu | 71bd1c3 | 2020-12-17 18:21:05 -0800 | [diff] [blame] | 19 | #include <android-base/stringprintf.h> |
| 20 | #include <android-base/strings.h> |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Andrei Homescu | 71bd1c3 | 2020-12-17 18:21:05 -0800 | [diff] [blame] | 24 | |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 25 | #include <map> |
| 26 | #include <memory> |
| 27 | #include <sstream> |
| 28 | |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 29 | #include "aidl_to_cpp_common.h" |
| 30 | #include "aidl_to_rust.h" |
| 31 | #include "code_writer.h" |
Jooyung Han | d4fe00e | 2021-01-11 16:21:53 +0900 | [diff] [blame] | 32 | #include "comments.h" |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 33 | #include "logging.h" |
| 34 | |
Andrei Homescu | e61feb5 | 2020-08-18 15:44:24 -0700 | [diff] [blame] | 35 | using android::base::Join; |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 36 | using std::ostringstream; |
| 37 | using std::shared_ptr; |
| 38 | using std::string; |
| 39 | using std::unique_ptr; |
| 40 | using std::vector; |
| 41 | |
| 42 | namespace android { |
| 43 | namespace aidl { |
| 44 | namespace rust { |
| 45 | |
| 46 | static constexpr const char kArgumentPrefix[] = "_arg_"; |
| 47 | static constexpr const char kGetInterfaceVersion[] = "getInterfaceVersion"; |
| 48 | static constexpr const char kGetInterfaceHash[] = "getInterfaceHash"; |
| 49 | |
| 50 | void GenerateMangledAlias(CodeWriter& out, const AidlDefinedType* type) { |
| 51 | ostringstream alias; |
| 52 | for (const auto& component : type->GetSplitPackage()) { |
| 53 | alias << "_" << component.size() << "_" << component; |
| 54 | } |
| 55 | alias << "_" << type->GetName().size() << "_" << type->GetName(); |
| 56 | out << "pub(crate) mod mangled { pub use super::" << type->GetName() << " as " << alias.str() |
| 57 | << "; }\n"; |
| 58 | } |
| 59 | |
| 60 | string BuildArg(const AidlArgument& arg, const AidlTypenames& typenames) { |
| 61 | // We pass in parameters that are not primitives by const reference. |
| 62 | // Arrays get passed in as slices, which is handled in RustNameOf. |
| 63 | auto arg_mode = ArgumentStorageMode(arg, typenames); |
| 64 | auto arg_type = RustNameOf(arg.GetType(), typenames, arg_mode); |
| 65 | return kArgumentPrefix + arg.GetName() + ": " + arg_type; |
| 66 | } |
| 67 | |
| 68 | string BuildMethod(const AidlMethod& method, const AidlTypenames& typenames) { |
| 69 | auto method_type = RustNameOf(method.GetType(), typenames, StorageMode::VALUE); |
| 70 | auto return_type = string{"binder::public_api::Result<"} + method_type + ">"; |
| 71 | string parameters = "&self"; |
| 72 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
| 73 | parameters += ", "; |
| 74 | parameters += BuildArg(*arg, typenames); |
| 75 | } |
| 76 | return "fn " + method.GetName() + "(" + parameters + ") -> " + return_type; |
| 77 | } |
| 78 | |
Steven Moreland | a7764e5 | 2020-10-27 17:29:29 +0000 | [diff] [blame] | 79 | void GenerateClientMethod(CodeWriter& out, const AidlInterface& iface, const AidlMethod& method, |
| 80 | const AidlTypenames& typenames, const Options& options, |
| 81 | const std::string& trait_name) { |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 82 | // Generate the method |
| 83 | out << BuildMethod(method, typenames) << " {\n"; |
| 84 | out.Indent(); |
| 85 | |
| 86 | if (!method.IsUserDefined()) { |
| 87 | if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 88 | // Check if the version is in the cache |
| 89 | out << "let _aidl_version = " |
| 90 | "self.cached_version.load(std::sync::atomic::Ordering::Relaxed);\n"; |
| 91 | out << "if _aidl_version != -1 { return Ok(_aidl_version); }\n"; |
| 92 | } |
| 93 | |
| 94 | if (method.GetName() == kGetInterfaceHash && !options.Hash().empty()) { |
| 95 | out << "{\n"; |
| 96 | out << " let _aidl_hash_lock = self.cached_hash.lock().unwrap();\n"; |
| 97 | out << " if let Some(ref _aidl_hash) = *_aidl_hash_lock {\n"; |
| 98 | out << " return Ok(_aidl_hash.clone());\n"; |
| 99 | out << " }\n"; |
| 100 | out << "}\n"; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Call transact() |
Steven Moreland | a7764e5 | 2020-10-27 17:29:29 +0000 | [diff] [blame] | 105 | vector<string> flags; |
| 106 | if (method.IsOneway()) flags.push_back("binder::SpIBinder::FLAG_ONEWAY"); |
| 107 | if (iface.IsSensitiveData()) flags.push_back("binder::SpIBinder::FLAG_CLEAR_BUF"); |
| 108 | |
| 109 | string transact_flags = flags.empty() ? "0" : Join(flags, " | "); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 110 | out << "let _aidl_reply = self.binder.transact(" |
| 111 | << "transactions::" << method.GetName() << ", " << transact_flags << ", |_aidl_data| {\n"; |
| 112 | out.Indent(); |
| 113 | |
Steven Moreland | a7764e5 | 2020-10-27 17:29:29 +0000 | [diff] [blame] | 114 | if (iface.IsSensitiveData()) { |
| 115 | out << "_aidl_data.mark_sensitive();\n"; |
| 116 | } |
| 117 | |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 118 | // Arguments |
| 119 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
| 120 | auto arg_name = kArgumentPrefix + arg->GetName(); |
| 121 | if (arg->IsIn()) { |
| 122 | // If the argument is already a reference, don't reference it again |
| 123 | // (unless we turned it into an Option<&T>) |
| 124 | auto ref_mode = ArgumentReferenceMode(*arg, typenames); |
| 125 | if (IsReference(ref_mode)) { |
| 126 | out << "_aidl_data.write(" << arg_name << ")?;\n"; |
| 127 | } else { |
| 128 | out << "_aidl_data.write(&" << arg_name << ")?;\n"; |
| 129 | } |
| 130 | } else if (arg->GetType().IsArray()) { |
| 131 | // For out-only arrays, send the array size |
| 132 | if (arg->GetType().IsNullable()) { |
| 133 | out << "_aidl_data.write_slice_size(" << arg_name << ".as_deref())?;\n"; |
| 134 | } else { |
| 135 | out << "_aidl_data.write_slice_size(Some(" << arg_name << "))?;\n"; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // Return Ok(()) if all the `_aidl_data.write(...)?;` calls pass |
| 141 | out << "Ok(())\n"; |
| 142 | out.Dedent(); |
| 143 | out << "});\n"; |
| 144 | |
| 145 | // Check for UNKNOWN_TRANSACTION and call the default impl |
Andrei Homescu | 71bd1c3 | 2020-12-17 18:21:05 -0800 | [diff] [blame] | 146 | if (method.IsUserDefined()) { |
| 147 | string default_args; |
| 148 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
| 149 | if (!default_args.empty()) { |
| 150 | default_args += ", "; |
| 151 | } |
| 152 | default_args += kArgumentPrefix; |
| 153 | default_args += arg->GetName(); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 154 | } |
Andrei Homescu | 71bd1c3 | 2020-12-17 18:21:05 -0800 | [diff] [blame] | 155 | out << "if let Err(binder::StatusCode::UNKNOWN_TRANSACTION) = _aidl_reply {\n"; |
| 156 | out << " if let Some(_aidl_default_impl) = <Self as " << trait_name |
| 157 | << ">::getDefaultImpl() {\n"; |
| 158 | out << " return _aidl_default_impl." << method.GetName() << "(" << default_args << ");\n"; |
| 159 | out << " }\n"; |
| 160 | out << "}\n"; |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 161 | } |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 162 | |
| 163 | // Return all other errors |
| 164 | out << "let _aidl_reply = _aidl_reply?;\n"; |
| 165 | |
| 166 | string return_val = "()"; |
| 167 | if (!method.IsOneway()) { |
| 168 | // Check for errors |
| 169 | out << "let _aidl_status: binder::Status = _aidl_reply.read()?;\n"; |
| 170 | out << "if !_aidl_status.is_ok() { return Err(_aidl_status); }\n"; |
| 171 | |
| 172 | // Return reply value |
| 173 | if (method.GetType().GetName() != "void") { |
| 174 | auto return_type = RustNameOf(method.GetType(), typenames, StorageMode::VALUE); |
| 175 | out << "let _aidl_return: " << return_type << " = _aidl_reply.read()?;\n"; |
| 176 | return_val = "_aidl_return"; |
| 177 | |
| 178 | if (!method.IsUserDefined()) { |
| 179 | if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 180 | out << "self.cached_version.store(_aidl_return, std::sync::atomic::Ordering::Relaxed);\n"; |
| 181 | } |
| 182 | if (method.GetName() == kGetInterfaceHash && !options.Hash().empty()) { |
| 183 | out << "*self.cached_hash.lock().unwrap() = Some(_aidl_return.clone());\n"; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | for (const AidlArgument* arg : method.GetOutArguments()) { |
| 189 | out << "*" << kArgumentPrefix << arg->GetName() << " = _aidl_reply.read()?;\n"; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // Return the result |
| 194 | out << "Ok(" << return_val << ")\n"; |
| 195 | out.Dedent(); |
| 196 | out << "}\n"; |
| 197 | } |
| 198 | |
| 199 | void GenerateServerTransaction(CodeWriter& out, const AidlMethod& method, |
| 200 | const AidlTypenames& typenames) { |
| 201 | out << "transactions::" << method.GetName() << " => {\n"; |
| 202 | out.Indent(); |
| 203 | |
| 204 | string args; |
| 205 | for (const auto& arg : method.GetArguments()) { |
| 206 | string arg_name = kArgumentPrefix + arg->GetName(); |
| 207 | StorageMode arg_mode; |
| 208 | if (arg->IsIn()) { |
| 209 | arg_mode = StorageMode::VALUE; |
| 210 | } else { |
| 211 | // We need a value we can call Default::default() on |
| 212 | arg_mode = StorageMode::DEFAULT_VALUE; |
| 213 | } |
| 214 | auto arg_type = RustNameOf(arg->GetType(), typenames, arg_mode); |
| 215 | |
| 216 | string arg_mut = arg->IsOut() ? "mut " : ""; |
| 217 | string arg_init = arg->IsIn() ? "_aidl_data.read()?" : "Default::default()"; |
| 218 | out << "let " << arg_mut << arg_name << ": " << arg_type << " = " << arg_init << ";\n"; |
| 219 | if (!arg->IsIn() && arg->GetType().IsArray()) { |
| 220 | // _aidl_data.resize_[nullable_]out_vec(&mut _arg_foo)?; |
| 221 | auto resize_name = arg->GetType().IsNullable() ? "resize_nullable_out_vec" : "resize_out_vec"; |
| 222 | out << "_aidl_data." << resize_name << "(&mut " << arg_name << ")?;\n"; |
| 223 | } |
| 224 | |
| 225 | auto ref_mode = ArgumentReferenceMode(*arg, typenames); |
| 226 | if (!args.empty()) { |
| 227 | args += ", "; |
| 228 | } |
| 229 | args += TakeReference(ref_mode, arg_name); |
| 230 | } |
| 231 | out << "let _aidl_return = _aidl_service." << method.GetName() << "(" << args << ");\n"; |
| 232 | |
| 233 | if (!method.IsOneway()) { |
| 234 | out << "match &_aidl_return {\n"; |
| 235 | out.Indent(); |
| 236 | out << "Ok(_aidl_return) => {\n"; |
| 237 | out.Indent(); |
| 238 | out << "_aidl_reply.write(&binder::Status::from(binder::StatusCode::OK))?;\n"; |
| 239 | if (method.GetType().GetName() != "void") { |
| 240 | out << "_aidl_reply.write(_aidl_return)?;\n"; |
| 241 | } |
| 242 | |
| 243 | // Serialize out arguments |
| 244 | for (const AidlArgument* arg : method.GetOutArguments()) { |
| 245 | string arg_name = kArgumentPrefix + arg->GetName(); |
| 246 | |
| 247 | auto& arg_type = arg->GetType(); |
| 248 | if (!arg->IsIn() && arg_type.IsArray() && arg_type.GetName() == "ParcelFileDescriptor") { |
| 249 | // We represent arrays of ParcelFileDescriptor as |
| 250 | // Vec<Option<ParcelFileDescriptor>> when they're out-arguments, |
| 251 | // but we need all of them to be initialized to Some; if there's |
| 252 | // any None, return UNEXPECTED_NULL (this is what libbinder_ndk does) |
| 253 | out << "if " << arg_name << ".iter().any(Option::is_none) { " |
| 254 | << "return Err(binder::StatusCode::UNEXPECTED_NULL); }\n"; |
| 255 | } else if (!arg->IsIn() && !TypeHasDefault(arg_type, typenames)) { |
| 256 | // Unwrap out-only arguments that we wrapped in Option<T> |
| 257 | out << "let " << arg_name << " = " << arg_name |
| 258 | << ".ok_or(binder::StatusCode::UNEXPECTED_NULL)?;\n"; |
| 259 | } |
| 260 | |
| 261 | out << "_aidl_reply.write(&" << arg_name << ")?;\n"; |
| 262 | } |
| 263 | out.Dedent(); |
| 264 | out << "}\n"; |
| 265 | out << "Err(_aidl_status) => _aidl_reply.write(_aidl_status)?\n"; |
| 266 | out.Dedent(); |
| 267 | out << "}\n"; |
| 268 | } |
| 269 | out << "Ok(())\n"; |
| 270 | out.Dedent(); |
| 271 | out << "}\n"; |
| 272 | } |
| 273 | |
| 274 | void GenerateServerItems(CodeWriter& out, const AidlInterface* iface, |
| 275 | const AidlTypenames& typenames) { |
| 276 | auto trait_name = ClassName(*iface, cpp::ClassNames::INTERFACE); |
| 277 | auto server_name = ClassName(*iface, cpp::ClassNames::SERVER); |
| 278 | |
| 279 | // Forward all IFoo functions from Binder to the inner object |
| 280 | out << "impl " << trait_name << " for binder::Binder<" << server_name << "> {\n"; |
| 281 | out.Indent(); |
| 282 | for (const auto& method : iface->GetMethods()) { |
| 283 | string args; |
| 284 | for (const std::unique_ptr<AidlArgument>& arg : method->GetArguments()) { |
| 285 | if (!args.empty()) { |
| 286 | args += ", "; |
| 287 | } |
| 288 | args += kArgumentPrefix; |
| 289 | args += arg->GetName(); |
| 290 | } |
| 291 | out << BuildMethod(*method, typenames) << " { " |
| 292 | << "self.0." << method->GetName() << "(" << args << ") }\n"; |
| 293 | } |
| 294 | out.Dedent(); |
| 295 | out << "}\n"; |
| 296 | |
| 297 | out << "fn on_transact(" |
| 298 | "_aidl_service: &dyn " |
| 299 | << trait_name |
| 300 | << ", " |
| 301 | "_aidl_code: binder::TransactionCode, " |
| 302 | "_aidl_data: &binder::parcel::Parcel, " |
| 303 | "_aidl_reply: &mut binder::parcel::Parcel) -> binder::Result<()> {\n"; |
| 304 | out.Indent(); |
| 305 | out << "match _aidl_code {\n"; |
| 306 | out.Indent(); |
| 307 | for (const auto& method : iface->GetMethods()) { |
| 308 | GenerateServerTransaction(out, *method, typenames); |
| 309 | } |
| 310 | out << "_ => Err(binder::StatusCode::UNKNOWN_TRANSACTION)\n"; |
| 311 | out.Dedent(); |
| 312 | out << "}\n"; |
| 313 | out.Dedent(); |
| 314 | out << "}\n"; |
| 315 | } |
| 316 | |
Jooyung Han | d4fe00e | 2021-01-11 16:21:53 +0900 | [diff] [blame] | 317 | void GenerateDeprecated(CodeWriter& out, const AidlCommentable& type) { |
| 318 | if (auto deprecated = FindDeprecated(type.GetComments()); deprecated.has_value()) { |
| 319 | if (deprecated->note.empty()) { |
| 320 | out << "#[deprecated]\n"; |
| 321 | } else { |
| 322 | out << "#[deprecated = " << QuotedEscape(deprecated->note) << "]\n"; |
| 323 | } |
Jooyung Han | 720253d | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 327 | template <typename TypeWithConstants> |
| 328 | void GenerateConstantDeclarations(CodeWriter& out, const TypeWithConstants& type, |
| 329 | const AidlTypenames& typenames) { |
| 330 | for (const auto& constant : type.GetConstantDeclarations()) { |
| 331 | const AidlTypeSpecifier& type = constant->GetType(); |
| 332 | const AidlConstantValue& value = constant->GetValue(); |
| 333 | |
| 334 | string const_type; |
| 335 | if (type.Signature() == "String") { |
| 336 | const_type = "&str"; |
| 337 | } else if (type.Signature() == "byte" || type.Signature() == "int" || |
| 338 | type.Signature() == "long") { |
| 339 | const_type = RustNameOf(type, typenames, StorageMode::VALUE); |
| 340 | } else { |
| 341 | AIDL_FATAL(value) << "Unrecognized constant type: " << type.Signature(); |
| 342 | } |
| 343 | |
Jooyung Han | 720253d | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 344 | GenerateDeprecated(out, *constant); |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 345 | out << "pub const " << constant->GetName() << ": " << const_type << " = " |
| 346 | << constant->ValueString(ConstantValueDecoratorRef) << ";\n"; |
| 347 | } |
| 348 | } |
| 349 | |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 350 | bool GenerateRustInterface(const string& filename, const AidlInterface* iface, |
| 351 | const AidlTypenames& typenames, const IoDelegate& io_delegate, |
| 352 | const Options& options) { |
| 353 | CodeWriterPtr code_writer = io_delegate.GetCodeWriter(filename); |
| 354 | |
| 355 | *code_writer << "#![allow(non_upper_case_globals)]\n"; |
| 356 | *code_writer << "#![allow(non_snake_case)]\n"; |
| 357 | // Import IBinder for transact() |
| 358 | *code_writer << "#[allow(unused_imports)] use binder::IBinder;\n"; |
| 359 | |
| 360 | auto trait_name = ClassName(*iface, cpp::ClassNames::INTERFACE); |
| 361 | auto client_name = ClassName(*iface, cpp::ClassNames::CLIENT); |
| 362 | auto server_name = ClassName(*iface, cpp::ClassNames::SERVER); |
| 363 | *code_writer << "use binder::declare_binder_interface;\n"; |
| 364 | *code_writer << "declare_binder_interface! {\n"; |
| 365 | code_writer->Indent(); |
| 366 | *code_writer << trait_name << "[\"" << iface->GetDescriptor() << "\"] {\n"; |
| 367 | code_writer->Indent(); |
| 368 | *code_writer << "native: " << server_name << "(on_transact),\n"; |
| 369 | *code_writer << "proxy: " << client_name << " {\n"; |
| 370 | code_writer->Indent(); |
| 371 | if (options.Version() > 0) { |
| 372 | string comma = options.Hash().empty() ? "" : ","; |
| 373 | *code_writer << "cached_version: " |
| 374 | "std::sync::atomic::AtomicI32 = " |
| 375 | "std::sync::atomic::AtomicI32::new(-1)" |
| 376 | << comma << "\n"; |
| 377 | } |
| 378 | if (!options.Hash().empty()) { |
| 379 | *code_writer << "cached_hash: " |
| 380 | "std::sync::Mutex<Option<String>> = " |
| 381 | "std::sync::Mutex::new(None)\n"; |
| 382 | } |
| 383 | code_writer->Dedent(); |
| 384 | *code_writer << "},\n"; |
| 385 | code_writer->Dedent(); |
| 386 | *code_writer << "}\n"; |
| 387 | code_writer->Dedent(); |
| 388 | *code_writer << "}\n"; |
| 389 | |
Jooyung Han | 720253d | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 390 | GenerateDeprecated(*code_writer, *iface); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 391 | *code_writer << "pub trait " << trait_name << ": binder::Interface + Send {\n"; |
| 392 | code_writer->Indent(); |
| 393 | *code_writer << "fn get_descriptor() -> &'static str where Self: Sized { \"" |
| 394 | << iface->GetDescriptor() << "\" }\n"; |
| 395 | |
| 396 | for (const auto& method : iface->GetMethods()) { |
| 397 | // Generate the method |
Jooyung Han | 720253d | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 398 | GenerateDeprecated(*code_writer, *method); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 399 | if (method->IsUserDefined()) { |
Andrei Homescu | 71bd1c3 | 2020-12-17 18:21:05 -0800 | [diff] [blame] | 400 | *code_writer << BuildMethod(*method, typenames) << ";\n"; |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 401 | } else { |
| 402 | // Generate default implementations for meta methods |
Andrei Homescu | 71bd1c3 | 2020-12-17 18:21:05 -0800 | [diff] [blame] | 403 | *code_writer << BuildMethod(*method, typenames) << " {\n"; |
| 404 | code_writer->Indent(); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 405 | if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 406 | *code_writer << "Ok(VERSION)\n"; |
| 407 | } else if (method->GetName() == kGetInterfaceHash && !options.Hash().empty()) { |
| 408 | *code_writer << "Ok(HASH.into())\n"; |
| 409 | } |
Andrei Homescu | 71bd1c3 | 2020-12-17 18:21:05 -0800 | [diff] [blame] | 410 | code_writer->Dedent(); |
| 411 | *code_writer << "}\n"; |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 412 | } |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | // Emit the default implementation code inside the trait |
Andrei Homescu | 71bd1c3 | 2020-12-17 18:21:05 -0800 | [diff] [blame] | 416 | auto default_trait_name = ClassName(*iface, cpp::ClassNames::DEFAULT_IMPL); |
| 417 | auto default_ref_name = default_trait_name + "Ref"; |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 418 | *code_writer << "fn getDefaultImpl()" |
Andrei Homescu | 71bd1c3 | 2020-12-17 18:21:05 -0800 | [diff] [blame] | 419 | << " -> " << default_ref_name << " where Self: Sized {\n"; |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 420 | *code_writer << " DEFAULT_IMPL.lock().unwrap().clone()\n"; |
| 421 | *code_writer << "}\n"; |
Andrei Homescu | 71bd1c3 | 2020-12-17 18:21:05 -0800 | [diff] [blame] | 422 | *code_writer << "fn setDefaultImpl(d: " << default_ref_name << ")" |
| 423 | << " -> " << default_ref_name << " where Self: Sized {\n"; |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 424 | *code_writer << " std::mem::replace(&mut *DEFAULT_IMPL.lock().unwrap(), d)\n"; |
| 425 | *code_writer << "}\n"; |
| 426 | code_writer->Dedent(); |
| 427 | *code_writer << "}\n"; |
| 428 | |
Andrei Homescu | 71bd1c3 | 2020-12-17 18:21:05 -0800 | [diff] [blame] | 429 | // Emit the default trait |
| 430 | *code_writer << "pub trait " << default_trait_name << ": Send + Sync {\n"; |
| 431 | code_writer->Indent(); |
| 432 | for (const auto& method : iface->GetMethods()) { |
| 433 | if (!method->IsUserDefined()) { |
| 434 | continue; |
| 435 | } |
| 436 | |
| 437 | // Generate the default method |
| 438 | *code_writer << BuildMethod(*method, typenames) << " {\n"; |
| 439 | code_writer->Indent(); |
| 440 | *code_writer << "Err(binder::StatusCode::UNKNOWN_TRANSACTION.into())\n"; |
| 441 | code_writer->Dedent(); |
| 442 | *code_writer << "}\n"; |
| 443 | } |
| 444 | code_writer->Dedent(); |
| 445 | *code_writer << "}\n"; |
| 446 | |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 447 | // Generate the transaction code constants |
| 448 | // The constants get their own sub-module to avoid conflicts |
| 449 | *code_writer << "pub mod transactions {\n"; |
| 450 | code_writer->Indent(); |
| 451 | // Import IBinder so we can access FIRST_CALL_TRANSACTION |
| 452 | *code_writer << "#[allow(unused_imports)] use binder::IBinder;\n"; |
| 453 | for (const auto& method : iface->GetMethods()) { |
| 454 | // Generate the transaction code constant |
| 455 | *code_writer << "pub const " << method->GetName() |
| 456 | << ": binder::TransactionCode = " |
| 457 | "binder::SpIBinder::FIRST_CALL_TRANSACTION + " + |
| 458 | std::to_string(method->GetId()) + ";\n"; |
| 459 | } |
| 460 | code_writer->Dedent(); |
| 461 | *code_writer << "}\n"; |
| 462 | |
| 463 | // Emit the default implementation code outside the trait |
Andrei Homescu | 71bd1c3 | 2020-12-17 18:21:05 -0800 | [diff] [blame] | 464 | *code_writer << "pub type " << default_ref_name << " = Option<std::sync::Arc<dyn " |
| 465 | << default_trait_name << ">>;\n"; |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 466 | *code_writer << "use lazy_static::lazy_static;\n"; |
| 467 | *code_writer << "lazy_static! {\n"; |
Andrei Homescu | 71bd1c3 | 2020-12-17 18:21:05 -0800 | [diff] [blame] | 468 | *code_writer << " static ref DEFAULT_IMPL: std::sync::Mutex<" << default_ref_name |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 469 | << "> = std::sync::Mutex::new(None);\n"; |
| 470 | *code_writer << "}\n"; |
| 471 | |
| 472 | // Emit the interface constants |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 473 | GenerateConstantDeclarations(*code_writer, *iface, typenames); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 474 | |
| 475 | GenerateMangledAlias(*code_writer, iface); |
| 476 | |
| 477 | // Emit VERSION and HASH |
| 478 | // These need to be top-level item constants instead of associated consts |
| 479 | // because the latter are incompatible with trait objects, see |
| 480 | // https://doc.rust-lang.org/reference/items/traits.html#object-safety |
| 481 | if (options.Version() > 0) { |
| 482 | *code_writer << "pub const VERSION: i32 = " << std::to_string(options.Version()) << ";\n"; |
| 483 | } |
| 484 | if (!options.Hash().empty()) { |
| 485 | *code_writer << "pub const HASH: &str = \"" << options.Hash() << "\";\n"; |
| 486 | } |
| 487 | |
| 488 | // Generate the client-side methods |
| 489 | *code_writer << "impl " << trait_name << " for " << client_name << " {\n"; |
| 490 | code_writer->Indent(); |
| 491 | for (const auto& method : iface->GetMethods()) { |
Steven Moreland | a7764e5 | 2020-10-27 17:29:29 +0000 | [diff] [blame] | 492 | GenerateClientMethod(*code_writer, *iface, *method, typenames, options, trait_name); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 493 | } |
| 494 | code_writer->Dedent(); |
| 495 | *code_writer << "}\n"; |
| 496 | |
| 497 | // Generate the server-side methods |
| 498 | GenerateServerItems(*code_writer, iface, typenames); |
| 499 | |
| 500 | return true; |
| 501 | } |
| 502 | |
Jooyung Han | f93b5bd | 2020-10-28 15:12:08 +0900 | [diff] [blame] | 503 | void GenerateParcelBody(CodeWriter& out, const AidlStructuredParcelable* parcel, |
| 504 | const AidlTypenames& typenames) { |
Jooyung Han | 720253d | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 505 | GenerateDeprecated(out, *parcel); |
Jooyung Han | f93b5bd | 2020-10-28 15:12:08 +0900 | [diff] [blame] | 506 | out << "pub struct " << parcel->GetName() << " {\n"; |
| 507 | out.Indent(); |
| 508 | for (const auto& variable : parcel->GetFields()) { |
Jooyung Han | 720253d | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 509 | GenerateDeprecated(out, *variable); |
Jooyung Han | f93b5bd | 2020-10-28 15:12:08 +0900 | [diff] [blame] | 510 | auto field_type = RustNameOf(variable->GetType(), typenames, StorageMode::PARCELABLE_FIELD); |
| 511 | out << "pub " << variable->GetName() << ": " << field_type << ",\n"; |
| 512 | } |
| 513 | out.Dedent(); |
| 514 | out << "}\n"; |
| 515 | } |
| 516 | |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 517 | void GenerateParcelDefault(CodeWriter& out, const AidlStructuredParcelable* parcel) { |
| 518 | out << "impl Default for " << parcel->GetName() << " {\n"; |
| 519 | out.Indent(); |
| 520 | out << "fn default() -> Self {\n"; |
| 521 | out.Indent(); |
| 522 | out << "Self {\n"; |
| 523 | out.Indent(); |
| 524 | for (const auto& variable : parcel->GetFields()) { |
| 525 | if (variable->GetDefaultValue()) { |
| 526 | out << variable->GetName() << ": " << variable->ValueString(ConstantValueDecorator) << ",\n"; |
| 527 | } else { |
| 528 | out << variable->GetName() << ": Default::default(),\n"; |
| 529 | } |
| 530 | } |
| 531 | out.Dedent(); |
| 532 | out << "}\n"; |
| 533 | out.Dedent(); |
| 534 | out << "}\n"; |
| 535 | out.Dedent(); |
| 536 | out << "}\n"; |
| 537 | } |
| 538 | |
Jooyung Han | f93b5bd | 2020-10-28 15:12:08 +0900 | [diff] [blame] | 539 | void GenerateParcelSerializeBody(CodeWriter& out, const AidlStructuredParcelable* parcel, |
| 540 | const AidlTypenames& typenames) { |
| 541 | out << "parcel.sized_write(|subparcel| {\n"; |
| 542 | out.Indent(); |
| 543 | for (const auto& variable : parcel->GetFields()) { |
| 544 | if (!TypeHasDefault(variable->GetType(), typenames)) { |
| 545 | out << "let __field_ref = this." << variable->GetName() |
| 546 | << ".as_ref().ok_or(binder::StatusCode::UNEXPECTED_NULL)?;\n"; |
| 547 | out << "subparcel.write(__field_ref)?;\n"; |
| 548 | } else { |
| 549 | out << "subparcel.write(&this." << variable->GetName() << ")?;\n"; |
| 550 | } |
| 551 | } |
| 552 | out << "Ok(())\n"; |
| 553 | out.Dedent(); |
| 554 | out << "})\n"; |
| 555 | } |
| 556 | |
| 557 | void GenerateParcelDeserializeBody(CodeWriter& out, const AidlStructuredParcelable* parcel, |
| 558 | const AidlTypenames& typenames) { |
| 559 | out << "let start_pos = parcel.get_data_position();\n"; |
| 560 | out << "let parcelable_size: i32 = parcel.read()?;\n"; |
| 561 | out << "if parcelable_size < 0 { return Err(binder::StatusCode::BAD_VALUE); }\n"; |
| 562 | |
| 563 | // Pre-emit the common field epilogue code, shared between all fields: |
| 564 | ostringstream epilogue; |
| 565 | epilogue << "if (parcel.get_data_position() - start_pos) == parcelable_size {\n"; |
| 566 | // We assume the lhs can never be > parcelable_size, because then the read |
| 567 | // immediately preceding this check would have returned NOT_ENOUGH_DATA |
| 568 | epilogue << " return Ok(Some(result));\n"; |
| 569 | epilogue << "}\n"; |
| 570 | string epilogue_str = epilogue.str(); |
| 571 | |
| 572 | out << "let mut result = Self::default();\n"; |
| 573 | for (const auto& variable : parcel->GetFields()) { |
| 574 | if (!TypeHasDefault(variable->GetType(), typenames)) { |
| 575 | out << "result." << variable->GetName() << " = Some(parcel.read()?);\n"; |
| 576 | } else { |
| 577 | out << "result." << variable->GetName() << " = parcel.read()?;\n"; |
| 578 | } |
| 579 | out << epilogue_str; |
| 580 | } |
| 581 | |
| 582 | out << "Ok(Some(result))\n"; |
| 583 | } |
| 584 | |
| 585 | void GenerateParcelBody(CodeWriter& out, const AidlUnionDecl* parcel, |
| 586 | const AidlTypenames& typenames) { |
Jooyung Han | 720253d | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 587 | GenerateDeprecated(out, *parcel); |
Jooyung Han | f93b5bd | 2020-10-28 15:12:08 +0900 | [diff] [blame] | 588 | out << "pub enum " << parcel->GetName() << " {\n"; |
| 589 | out.Indent(); |
| 590 | for (const auto& variable : parcel->GetFields()) { |
Jooyung Han | 720253d | 2021-01-05 19:13:17 +0900 | [diff] [blame] | 591 | GenerateDeprecated(out, *variable); |
Jooyung Han | f93b5bd | 2020-10-28 15:12:08 +0900 | [diff] [blame] | 592 | auto field_type = RustNameOf(variable->GetType(), typenames, StorageMode::PARCELABLE_FIELD); |
| 593 | out << variable->GetCapitalizedName() << "(" << field_type << "),\n"; |
| 594 | } |
| 595 | out.Dedent(); |
| 596 | out << "}\n"; |
| 597 | } |
| 598 | |
| 599 | void GenerateParcelDefault(CodeWriter& out, const AidlUnionDecl* parcel) { |
| 600 | out << "impl Default for " << parcel->GetName() << " {\n"; |
| 601 | out.Indent(); |
| 602 | out << "fn default() -> Self {\n"; |
| 603 | out.Indent(); |
| 604 | |
| 605 | AIDL_FATAL_IF(parcel->GetFields().empty(), *parcel) |
| 606 | << "Union '" << parcel->GetName() << "' is empty."; |
| 607 | const auto& first_field = parcel->GetFields()[0]; |
| 608 | const auto& first_value = first_field->ValueString(ConstantValueDecorator); |
| 609 | |
| 610 | out << "Self::"; |
| 611 | if (first_field->GetDefaultValue()) { |
| 612 | out << first_field->GetCapitalizedName() << "(" << first_value << ")\n"; |
| 613 | } else { |
| 614 | out << first_field->GetCapitalizedName() << "(Default::default())\n"; |
| 615 | } |
| 616 | |
| 617 | out.Dedent(); |
| 618 | out << "}\n"; |
| 619 | out.Dedent(); |
| 620 | out << "}\n"; |
| 621 | } |
| 622 | |
| 623 | void GenerateParcelSerializeBody(CodeWriter& out, const AidlUnionDecl* parcel, |
| 624 | const AidlTypenames& typenames) { |
| 625 | out << "match this {\n"; |
| 626 | out.Indent(); |
| 627 | int tag = 0; |
| 628 | for (const auto& variable : parcel->GetFields()) { |
| 629 | out << "Self::" << variable->GetCapitalizedName() << "(v) => {\n"; |
| 630 | out.Indent(); |
| 631 | out << "parcel.write(&" << std::to_string(tag++) << "i32)?;\n"; |
| 632 | if (!TypeHasDefault(variable->GetType(), typenames)) { |
| 633 | out << "let __field_ref = v.as_ref().ok_or(binder::StatusCode::UNEXPECTED_NULL)?;\n"; |
| 634 | out << "parcel.write(__field_ref)\n"; |
| 635 | } else { |
| 636 | out << "parcel.write(v)\n"; |
| 637 | } |
| 638 | out.Dedent(); |
| 639 | out << "}\n"; |
| 640 | } |
| 641 | out.Dedent(); |
| 642 | out << "}\n"; |
| 643 | } |
| 644 | |
| 645 | void GenerateParcelDeserializeBody(CodeWriter& out, const AidlUnionDecl* parcel, |
| 646 | const AidlTypenames& typenames) { |
| 647 | out << "let tag: i32 = parcel.read()?;\n"; |
| 648 | out << "match tag {\n"; |
| 649 | out.Indent(); |
| 650 | int tag = 0; |
| 651 | for (const auto& variable : parcel->GetFields()) { |
| 652 | auto field_type = RustNameOf(variable->GetType(), typenames, StorageMode::PARCELABLE_FIELD); |
| 653 | |
| 654 | out << std::to_string(tag++) << " => {\n"; |
| 655 | out.Indent(); |
| 656 | out << "let value: " << field_type << " = "; |
| 657 | if (!TypeHasDefault(variable->GetType(), typenames)) { |
| 658 | out << "Some(parcel.read()?);\n"; |
| 659 | } else { |
| 660 | out << "parcel.read()?;\n"; |
| 661 | } |
| 662 | out << "Ok(Some(Self::" << variable->GetCapitalizedName() << "(value)))\n"; |
| 663 | out.Dedent(); |
| 664 | out << "}\n"; |
| 665 | } |
| 666 | out << "_ => {\n"; |
| 667 | out << " Err(binder::StatusCode::BAD_VALUE)\n"; |
| 668 | out << "}\n"; |
| 669 | out.Dedent(); |
| 670 | out << "}\n"; |
| 671 | } |
| 672 | |
| 673 | template <typename ParcelableType> |
| 674 | void GenerateParcelSerialize(CodeWriter& out, const ParcelableType* parcel, |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 675 | const AidlTypenames& typenames) { |
| 676 | out << "impl binder::parcel::Serialize for " << parcel->GetName() << " {\n"; |
| 677 | out << " fn serialize(&self, parcel: &mut binder::parcel::Parcel) -> binder::Result<()> {\n"; |
| 678 | out << " <Self as binder::parcel::SerializeOption>::serialize_option(Some(self), parcel)\n"; |
| 679 | out << " }\n"; |
| 680 | out << "}\n"; |
| 681 | |
| 682 | out << "impl binder::parcel::SerializeArray for " << parcel->GetName() << " {}\n"; |
| 683 | |
| 684 | out << "impl binder::parcel::SerializeOption for " << parcel->GetName() << " {\n"; |
| 685 | out.Indent(); |
| 686 | out << "fn serialize_option(this: Option<&Self>, parcel: &mut binder::parcel::Parcel) -> " |
| 687 | "binder::Result<()> {\n"; |
| 688 | out.Indent(); |
| 689 | out << "let this = if let Some(this) = this {\n"; |
| 690 | out << " parcel.write(&1i32)?;\n"; |
| 691 | out << " this\n"; |
| 692 | out << "} else {\n"; |
| 693 | out << " return parcel.write(&0i32);\n"; |
| 694 | out << "};\n"; |
Jooyung Han | f93b5bd | 2020-10-28 15:12:08 +0900 | [diff] [blame] | 695 | |
| 696 | GenerateParcelSerializeBody(out, parcel, typenames); |
| 697 | |
Andrei Homescu | ad9d0da | 2020-08-06 18:43:40 -0700 | [diff] [blame] | 698 | out.Dedent(); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 699 | out << "}\n"; |
| 700 | out.Dedent(); |
| 701 | out << "}\n"; |
| 702 | } |
| 703 | |
Jooyung Han | f93b5bd | 2020-10-28 15:12:08 +0900 | [diff] [blame] | 704 | template <typename ParcelableType> |
| 705 | void GenerateParcelDeserialize(CodeWriter& out, const ParcelableType* parcel, |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 706 | const AidlTypenames& typenames) { |
| 707 | out << "impl binder::parcel::Deserialize for " << parcel->GetName() << " {\n"; |
| 708 | out << " fn deserialize(parcel: &binder::parcel::Parcel) -> binder::Result<Self> {\n"; |
| 709 | out << " <Self as binder::parcel::DeserializeOption>::deserialize_option(parcel)\n"; |
| 710 | out << " .transpose()\n"; |
| 711 | out << " .unwrap_or(Err(binder::StatusCode::UNEXPECTED_NULL))\n"; |
| 712 | out << " }\n"; |
| 713 | out << "}\n"; |
| 714 | |
| 715 | out << "impl binder::parcel::DeserializeArray for " << parcel->GetName() << " {}\n"; |
| 716 | |
| 717 | out << "impl binder::parcel::DeserializeOption for " << parcel->GetName() << " {\n"; |
Jooyung Han | f93b5bd | 2020-10-28 15:12:08 +0900 | [diff] [blame] | 718 | out.Indent(); |
| 719 | out << "fn deserialize_option(parcel: &binder::parcel::Parcel) -> binder::Result<Option<Self>> " |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 720 | "{\n"; |
Jooyung Han | f93b5bd | 2020-10-28 15:12:08 +0900 | [diff] [blame] | 721 | out.Indent(); |
| 722 | out << "let status: i32 = parcel.read()?;\n"; |
| 723 | out << "if status == 0 { return Ok(None); }\n"; |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 724 | |
Jooyung Han | f93b5bd | 2020-10-28 15:12:08 +0900 | [diff] [blame] | 725 | GenerateParcelDeserializeBody(out, parcel, typenames); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 726 | |
Jooyung Han | f93b5bd | 2020-10-28 15:12:08 +0900 | [diff] [blame] | 727 | out.Dedent(); |
| 728 | out << "}\n"; |
| 729 | out.Dedent(); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 730 | out << "}\n"; |
| 731 | } |
| 732 | |
Jooyung Han | f93b5bd | 2020-10-28 15:12:08 +0900 | [diff] [blame] | 733 | template <typename ParcelableType> |
| 734 | bool GenerateRustParcel(const string& filename, const ParcelableType* parcel, |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 735 | const AidlTypenames& typenames, const IoDelegate& io_delegate) { |
| 736 | CodeWriterPtr code_writer = io_delegate.GetCodeWriter(filename); |
| 737 | |
Andrei Homescu | e61feb5 | 2020-08-18 15:44:24 -0700 | [diff] [blame] | 738 | // Debug is always derived because all Rust AIDL types implement it |
| 739 | // ParcelFileDescriptor doesn't support any of the others because |
| 740 | // it's a newtype over std::fs::File which only implements Debug |
| 741 | vector<string> derives{"Debug"}; |
| 742 | const AidlAnnotation* derive_annotation = parcel->RustDerive(); |
| 743 | if (derive_annotation != nullptr) { |
| 744 | for (const auto& name_and_param : derive_annotation->AnnotationParams(ConstantValueDecorator)) { |
| 745 | if (name_and_param.second == "true") { |
| 746 | derives.push_back(name_and_param.first); |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | *code_writer << "#[derive(" << Join(derives, ", ") << ")]\n"; |
Jooyung Han | f93b5bd | 2020-10-28 15:12:08 +0900 | [diff] [blame] | 752 | GenerateParcelBody(*code_writer, parcel, typenames); |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 753 | GenerateConstantDeclarations(*code_writer, *parcel, typenames); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 754 | GenerateMangledAlias(*code_writer, parcel); |
| 755 | GenerateParcelDefault(*code_writer, parcel); |
| 756 | GenerateParcelSerialize(*code_writer, parcel, typenames); |
| 757 | GenerateParcelDeserialize(*code_writer, parcel, typenames); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 758 | return true; |
| 759 | } |
| 760 | |
| 761 | bool GenerateRustEnumDeclaration(const string& filename, const AidlEnumDeclaration* enum_decl, |
| 762 | const AidlTypenames& typenames, const IoDelegate& io_delegate) { |
| 763 | CodeWriterPtr code_writer = io_delegate.GetCodeWriter(filename); |
| 764 | |
| 765 | const auto& aidl_backing_type = enum_decl->GetBackingType(); |
| 766 | auto backing_type = RustNameOf(aidl_backing_type, typenames, StorageMode::VALUE); |
| 767 | |
Jooyung Han | b7a60ec | 2021-01-21 13:47:59 +0900 | [diff] [blame] | 768 | // TODO(b/177860423) support "deprecated" for enum types |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 769 | *code_writer << "#![allow(non_upper_case_globals)]\n"; |
Andrei Homescu | 0717c43 | 2020-09-04 17:41:00 -0700 | [diff] [blame] | 770 | *code_writer << "use binder::declare_binder_enum;\n"; |
| 771 | *code_writer << "declare_binder_enum! { " << enum_decl->GetName() << " : " << backing_type |
| 772 | << " {\n"; |
| 773 | code_writer->Indent(); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 774 | for (const auto& enumerator : enum_decl->GetEnumerators()) { |
| 775 | auto value = enumerator->GetValue()->ValueString(aidl_backing_type, ConstantValueDecorator); |
Andrei Homescu | 0717c43 | 2020-09-04 17:41:00 -0700 | [diff] [blame] | 776 | *code_writer << enumerator->GetName() << " = " << value << ",\n"; |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 777 | } |
Andrei Homescu | 0717c43 | 2020-09-04 17:41:00 -0700 | [diff] [blame] | 778 | code_writer->Dedent(); |
| 779 | *code_writer << "} }\n"; |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 780 | |
| 781 | GenerateMangledAlias(*code_writer, enum_decl); |
| 782 | |
| 783 | return true; |
| 784 | } |
| 785 | |
| 786 | bool GenerateRust(const string& filename, const AidlDefinedType* defined_type, |
| 787 | const AidlTypenames& typenames, const IoDelegate& io_delegate, |
| 788 | const Options& options) { |
| 789 | if (const AidlStructuredParcelable* parcelable = defined_type->AsStructuredParcelable(); |
| 790 | parcelable != nullptr) { |
| 791 | return GenerateRustParcel(filename, parcelable, typenames, io_delegate); |
| 792 | } |
| 793 | |
Jooyung Han | f93b5bd | 2020-10-28 15:12:08 +0900 | [diff] [blame] | 794 | if (const AidlUnionDecl* parcelable = defined_type->AsUnionDeclaration(); parcelable != nullptr) { |
| 795 | return GenerateRustParcel(filename, parcelable, typenames, io_delegate); |
| 796 | } |
| 797 | |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 798 | if (const AidlEnumDeclaration* enum_decl = defined_type->AsEnumDeclaration(); |
| 799 | enum_decl != nullptr) { |
| 800 | return GenerateRustEnumDeclaration(filename, enum_decl, typenames, io_delegate); |
| 801 | } |
| 802 | |
| 803 | if (const AidlInterface* interface = defined_type->AsInterface(); interface != nullptr) { |
| 804 | return GenerateRustInterface(filename, interface, typenames, io_delegate, options); |
| 805 | } |
| 806 | |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 807 | AIDL_FATAL(filename) << "Unrecognized type sent for Rust generation."; |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 808 | return false; |
| 809 | } |
| 810 | |
| 811 | } // namespace rust |
| 812 | } // namespace aidl |
| 813 | } // namespace android |