blob: 0b36ff605b2bf8ea95571a0f1e9d53c22c962c10 [file] [log] [blame]
Andrei Homescub62afd92020-05-11 19:24:59 -07001/*
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 Homescu71bd1c32020-12-17 18:21:05 -080019#include <android-base/stringprintf.h>
20#include <android-base/strings.h>
Andrei Homescub62afd92020-05-11 19:24:59 -070021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
Andrei Homescu71bd1c32020-12-17 18:21:05 -080024
Andrei Homescub62afd92020-05-11 19:24:59 -070025#include <map>
26#include <memory>
27#include <sstream>
28
Andrei Homescub62afd92020-05-11 19:24:59 -070029#include "aidl_to_cpp_common.h"
30#include "aidl_to_rust.h"
31#include "code_writer.h"
Jooyung Hand4fe00e2021-01-11 16:21:53 +090032#include "comments.h"
Andrei Homescub62afd92020-05-11 19:24:59 -070033#include "logging.h"
34
Andrei Homescue61feb52020-08-18 15:44:24 -070035using android::base::Join;
Andrei Homescub62afd92020-05-11 19:24:59 -070036using std::ostringstream;
37using std::shared_ptr;
38using std::string;
39using std::unique_ptr;
40using std::vector;
41
42namespace android {
43namespace aidl {
44namespace rust {
45
46static constexpr const char kArgumentPrefix[] = "_arg_";
47static constexpr const char kGetInterfaceVersion[] = "getInterfaceVersion";
48static constexpr const char kGetInterfaceHash[] = "getInterfaceHash";
49
50void 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
60string 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
68string 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 Morelanda7764e52020-10-27 17:29:29 +000079void GenerateClientMethod(CodeWriter& out, const AidlInterface& iface, const AidlMethod& method,
80 const AidlTypenames& typenames, const Options& options,
81 const std::string& trait_name) {
Andrei Homescub62afd92020-05-11 19:24:59 -070082 // 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 Morelanda7764e52020-10-27 17:29:29 +0000105 vector<string> flags;
Andrew Walbran7a1563f2021-03-09 12:17:13 +0000106 if (method.IsOneway()) flags.push_back("binder::FLAG_ONEWAY");
107 if (iface.IsSensitiveData()) flags.push_back("binder::FLAG_CLEAR_BUF");
Stephen Crane53734a22021-02-25 17:09:39 -0800108 flags.push_back("binder::FLAG_PRIVATE_LOCAL");
Steven Morelanda7764e52020-10-27 17:29:29 +0000109
110 string transact_flags = flags.empty() ? "0" : Join(flags, " | ");
Andrei Homescub62afd92020-05-11 19:24:59 -0700111 out << "let _aidl_reply = self.binder.transact("
112 << "transactions::" << method.GetName() << ", " << transact_flags << ", |_aidl_data| {\n";
113 out.Indent();
114
Steven Morelanda7764e52020-10-27 17:29:29 +0000115 if (iface.IsSensitiveData()) {
116 out << "_aidl_data.mark_sensitive();\n";
117 }
118
Andrei Homescub62afd92020-05-11 19:24:59 -0700119 // Arguments
120 for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) {
121 auto arg_name = kArgumentPrefix + arg->GetName();
122 if (arg->IsIn()) {
123 // If the argument is already a reference, don't reference it again
124 // (unless we turned it into an Option<&T>)
125 auto ref_mode = ArgumentReferenceMode(*arg, typenames);
126 if (IsReference(ref_mode)) {
127 out << "_aidl_data.write(" << arg_name << ")?;\n";
128 } else {
129 out << "_aidl_data.write(&" << arg_name << ")?;\n";
130 }
131 } else if (arg->GetType().IsArray()) {
132 // For out-only arrays, send the array size
133 if (arg->GetType().IsNullable()) {
134 out << "_aidl_data.write_slice_size(" << arg_name << ".as_deref())?;\n";
135 } else {
136 out << "_aidl_data.write_slice_size(Some(" << arg_name << "))?;\n";
137 }
138 }
139 }
140
141 // Return Ok(()) if all the `_aidl_data.write(...)?;` calls pass
142 out << "Ok(())\n";
143 out.Dedent();
144 out << "});\n";
145
146 // Check for UNKNOWN_TRANSACTION and call the default impl
Andrei Homescu71bd1c32020-12-17 18:21:05 -0800147 if (method.IsUserDefined()) {
148 string default_args;
149 for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) {
150 if (!default_args.empty()) {
151 default_args += ", ";
152 }
153 default_args += kArgumentPrefix;
154 default_args += arg->GetName();
Andrei Homescub62afd92020-05-11 19:24:59 -0700155 }
Andrei Homescu71bd1c32020-12-17 18:21:05 -0800156 out << "if let Err(binder::StatusCode::UNKNOWN_TRANSACTION) = _aidl_reply {\n";
157 out << " if let Some(_aidl_default_impl) = <Self as " << trait_name
158 << ">::getDefaultImpl() {\n";
159 out << " return _aidl_default_impl." << method.GetName() << "(" << default_args << ");\n";
160 out << " }\n";
161 out << "}\n";
Andrei Homescub62afd92020-05-11 19:24:59 -0700162 }
Andrei Homescub62afd92020-05-11 19:24:59 -0700163
164 // Return all other errors
165 out << "let _aidl_reply = _aidl_reply?;\n";
166
167 string return_val = "()";
168 if (!method.IsOneway()) {
169 // Check for errors
170 out << "let _aidl_status: binder::Status = _aidl_reply.read()?;\n";
171 out << "if !_aidl_status.is_ok() { return Err(_aidl_status); }\n";
172
173 // Return reply value
174 if (method.GetType().GetName() != "void") {
175 auto return_type = RustNameOf(method.GetType(), typenames, StorageMode::VALUE);
176 out << "let _aidl_return: " << return_type << " = _aidl_reply.read()?;\n";
177 return_val = "_aidl_return";
178
179 if (!method.IsUserDefined()) {
180 if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) {
181 out << "self.cached_version.store(_aidl_return, std::sync::atomic::Ordering::Relaxed);\n";
182 }
183 if (method.GetName() == kGetInterfaceHash && !options.Hash().empty()) {
184 out << "*self.cached_hash.lock().unwrap() = Some(_aidl_return.clone());\n";
185 }
186 }
187 }
188
189 for (const AidlArgument* arg : method.GetOutArguments()) {
190 out << "*" << kArgumentPrefix << arg->GetName() << " = _aidl_reply.read()?;\n";
191 }
192 }
193
194 // Return the result
195 out << "Ok(" << return_val << ")\n";
196 out.Dedent();
197 out << "}\n";
198}
199
200void GenerateServerTransaction(CodeWriter& out, const AidlMethod& method,
201 const AidlTypenames& typenames) {
202 out << "transactions::" << method.GetName() << " => {\n";
203 out.Indent();
204
205 string args;
206 for (const auto& arg : method.GetArguments()) {
207 string arg_name = kArgumentPrefix + arg->GetName();
208 StorageMode arg_mode;
209 if (arg->IsIn()) {
210 arg_mode = StorageMode::VALUE;
211 } else {
212 // We need a value we can call Default::default() on
213 arg_mode = StorageMode::DEFAULT_VALUE;
214 }
215 auto arg_type = RustNameOf(arg->GetType(), typenames, arg_mode);
216
217 string arg_mut = arg->IsOut() ? "mut " : "";
218 string arg_init = arg->IsIn() ? "_aidl_data.read()?" : "Default::default()";
219 out << "let " << arg_mut << arg_name << ": " << arg_type << " = " << arg_init << ";\n";
220 if (!arg->IsIn() && arg->GetType().IsArray()) {
221 // _aidl_data.resize_[nullable_]out_vec(&mut _arg_foo)?;
222 auto resize_name = arg->GetType().IsNullable() ? "resize_nullable_out_vec" : "resize_out_vec";
223 out << "_aidl_data." << resize_name << "(&mut " << arg_name << ")?;\n";
224 }
225
226 auto ref_mode = ArgumentReferenceMode(*arg, typenames);
227 if (!args.empty()) {
228 args += ", ";
229 }
230 args += TakeReference(ref_mode, arg_name);
231 }
232 out << "let _aidl_return = _aidl_service." << method.GetName() << "(" << args << ");\n";
233
234 if (!method.IsOneway()) {
235 out << "match &_aidl_return {\n";
236 out.Indent();
237 out << "Ok(_aidl_return) => {\n";
238 out.Indent();
239 out << "_aidl_reply.write(&binder::Status::from(binder::StatusCode::OK))?;\n";
240 if (method.GetType().GetName() != "void") {
241 out << "_aidl_reply.write(_aidl_return)?;\n";
242 }
243
244 // Serialize out arguments
245 for (const AidlArgument* arg : method.GetOutArguments()) {
246 string arg_name = kArgumentPrefix + arg->GetName();
247
248 auto& arg_type = arg->GetType();
249 if (!arg->IsIn() && arg_type.IsArray() && arg_type.GetName() == "ParcelFileDescriptor") {
250 // We represent arrays of ParcelFileDescriptor as
251 // Vec<Option<ParcelFileDescriptor>> when they're out-arguments,
252 // but we need all of them to be initialized to Some; if there's
253 // any None, return UNEXPECTED_NULL (this is what libbinder_ndk does)
254 out << "if " << arg_name << ".iter().any(Option::is_none) { "
255 << "return Err(binder::StatusCode::UNEXPECTED_NULL); }\n";
256 } else if (!arg->IsIn() && !TypeHasDefault(arg_type, typenames)) {
257 // Unwrap out-only arguments that we wrapped in Option<T>
258 out << "let " << arg_name << " = " << arg_name
259 << ".ok_or(binder::StatusCode::UNEXPECTED_NULL)?;\n";
260 }
261
262 out << "_aidl_reply.write(&" << arg_name << ")?;\n";
263 }
264 out.Dedent();
265 out << "}\n";
266 out << "Err(_aidl_status) => _aidl_reply.write(_aidl_status)?\n";
267 out.Dedent();
268 out << "}\n";
269 }
270 out << "Ok(())\n";
271 out.Dedent();
272 out << "}\n";
273}
274
275void GenerateServerItems(CodeWriter& out, const AidlInterface* iface,
276 const AidlTypenames& typenames) {
277 auto trait_name = ClassName(*iface, cpp::ClassNames::INTERFACE);
278 auto server_name = ClassName(*iface, cpp::ClassNames::SERVER);
279
280 // Forward all IFoo functions from Binder to the inner object
281 out << "impl " << trait_name << " for binder::Binder<" << server_name << "> {\n";
282 out.Indent();
283 for (const auto& method : iface->GetMethods()) {
284 string args;
285 for (const std::unique_ptr<AidlArgument>& arg : method->GetArguments()) {
286 if (!args.empty()) {
287 args += ", ";
288 }
289 args += kArgumentPrefix;
290 args += arg->GetName();
291 }
292 out << BuildMethod(*method, typenames) << " { "
293 << "self.0." << method->GetName() << "(" << args << ") }\n";
294 }
295 out.Dedent();
296 out << "}\n";
297
298 out << "fn on_transact("
299 "_aidl_service: &dyn "
300 << trait_name
301 << ", "
302 "_aidl_code: binder::TransactionCode, "
303 "_aidl_data: &binder::parcel::Parcel, "
304 "_aidl_reply: &mut binder::parcel::Parcel) -> binder::Result<()> {\n";
305 out.Indent();
306 out << "match _aidl_code {\n";
307 out.Indent();
308 for (const auto& method : iface->GetMethods()) {
309 GenerateServerTransaction(out, *method, typenames);
310 }
311 out << "_ => Err(binder::StatusCode::UNKNOWN_TRANSACTION)\n";
312 out.Dedent();
313 out << "}\n";
314 out.Dedent();
315 out << "}\n";
316}
317
Jooyung Hand4fe00e2021-01-11 16:21:53 +0900318void GenerateDeprecated(CodeWriter& out, const AidlCommentable& type) {
319 if (auto deprecated = FindDeprecated(type.GetComments()); deprecated.has_value()) {
320 if (deprecated->note.empty()) {
321 out << "#[deprecated]\n";
322 } else {
323 out << "#[deprecated = " << QuotedEscape(deprecated->note) << "]\n";
324 }
Jooyung Han720253d2021-01-05 19:13:17 +0900325 }
326}
327
Jooyung Han3f347ca2020-12-01 12:41:50 +0900328template <typename TypeWithConstants>
329void GenerateConstantDeclarations(CodeWriter& out, const TypeWithConstants& type,
330 const AidlTypenames& typenames) {
331 for (const auto& constant : type.GetConstantDeclarations()) {
332 const AidlTypeSpecifier& type = constant->GetType();
333 const AidlConstantValue& value = constant->GetValue();
334
335 string const_type;
336 if (type.Signature() == "String") {
337 const_type = "&str";
338 } else if (type.Signature() == "byte" || type.Signature() == "int" ||
339 type.Signature() == "long") {
340 const_type = RustNameOf(type, typenames, StorageMode::VALUE);
341 } else {
342 AIDL_FATAL(value) << "Unrecognized constant type: " << type.Signature();
343 }
344
Jooyung Han720253d2021-01-05 19:13:17 +0900345 GenerateDeprecated(out, *constant);
Jooyung Han3f347ca2020-12-01 12:41:50 +0900346 out << "pub const " << constant->GetName() << ": " << const_type << " = "
347 << constant->ValueString(ConstantValueDecoratorRef) << ";\n";
348 }
349}
350
Andrei Homescub62afd92020-05-11 19:24:59 -0700351bool GenerateRustInterface(const string& filename, const AidlInterface* iface,
352 const AidlTypenames& typenames, const IoDelegate& io_delegate,
353 const Options& options) {
354 CodeWriterPtr code_writer = io_delegate.GetCodeWriter(filename);
355
356 *code_writer << "#![allow(non_upper_case_globals)]\n";
357 *code_writer << "#![allow(non_snake_case)]\n";
Andrew Walbran7a1563f2021-03-09 12:17:13 +0000358 // Import IBinderInternal for transact()
359 *code_writer << "#[allow(unused_imports)] use binder::IBinderInternal;\n";
Andrei Homescub62afd92020-05-11 19:24:59 -0700360
361 auto trait_name = ClassName(*iface, cpp::ClassNames::INTERFACE);
362 auto client_name = ClassName(*iface, cpp::ClassNames::CLIENT);
363 auto server_name = ClassName(*iface, cpp::ClassNames::SERVER);
364 *code_writer << "use binder::declare_binder_interface;\n";
365 *code_writer << "declare_binder_interface! {\n";
366 code_writer->Indent();
367 *code_writer << trait_name << "[\"" << iface->GetDescriptor() << "\"] {\n";
368 code_writer->Indent();
369 *code_writer << "native: " << server_name << "(on_transact),\n";
370 *code_writer << "proxy: " << client_name << " {\n";
371 code_writer->Indent();
372 if (options.Version() > 0) {
373 string comma = options.Hash().empty() ? "" : ",";
374 *code_writer << "cached_version: "
375 "std::sync::atomic::AtomicI32 = "
376 "std::sync::atomic::AtomicI32::new(-1)"
377 << comma << "\n";
378 }
379 if (!options.Hash().empty()) {
380 *code_writer << "cached_hash: "
381 "std::sync::Mutex<Option<String>> = "
382 "std::sync::Mutex::new(None)\n";
383 }
384 code_writer->Dedent();
385 *code_writer << "},\n";
386 code_writer->Dedent();
Stephen Crane53734a22021-02-25 17:09:39 -0800387 if (iface->IsVintfStability()) {
388 *code_writer << "stability: binder::Stability::Vintf,\n";
389 }
Andrei Homescub62afd92020-05-11 19:24:59 -0700390 *code_writer << "}\n";
391 code_writer->Dedent();
392 *code_writer << "}\n";
393
Jooyung Han720253d2021-01-05 19:13:17 +0900394 GenerateDeprecated(*code_writer, *iface);
Andrei Homescub62afd92020-05-11 19:24:59 -0700395 *code_writer << "pub trait " << trait_name << ": binder::Interface + Send {\n";
396 code_writer->Indent();
397 *code_writer << "fn get_descriptor() -> &'static str where Self: Sized { \""
398 << iface->GetDescriptor() << "\" }\n";
399
400 for (const auto& method : iface->GetMethods()) {
401 // Generate the method
Jooyung Han720253d2021-01-05 19:13:17 +0900402 GenerateDeprecated(*code_writer, *method);
Andrei Homescub62afd92020-05-11 19:24:59 -0700403 if (method->IsUserDefined()) {
Andrei Homescu71bd1c32020-12-17 18:21:05 -0800404 *code_writer << BuildMethod(*method, typenames) << ";\n";
Andrei Homescub62afd92020-05-11 19:24:59 -0700405 } else {
406 // Generate default implementations for meta methods
Andrei Homescu71bd1c32020-12-17 18:21:05 -0800407 *code_writer << BuildMethod(*method, typenames) << " {\n";
408 code_writer->Indent();
Andrei Homescub62afd92020-05-11 19:24:59 -0700409 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
410 *code_writer << "Ok(VERSION)\n";
411 } else if (method->GetName() == kGetInterfaceHash && !options.Hash().empty()) {
412 *code_writer << "Ok(HASH.into())\n";
413 }
Andrei Homescu71bd1c32020-12-17 18:21:05 -0800414 code_writer->Dedent();
415 *code_writer << "}\n";
Andrei Homescub62afd92020-05-11 19:24:59 -0700416 }
Andrei Homescub62afd92020-05-11 19:24:59 -0700417 }
418
419 // Emit the default implementation code inside the trait
Andrei Homescu71bd1c32020-12-17 18:21:05 -0800420 auto default_trait_name = ClassName(*iface, cpp::ClassNames::DEFAULT_IMPL);
421 auto default_ref_name = default_trait_name + "Ref";
Andrei Homescub62afd92020-05-11 19:24:59 -0700422 *code_writer << "fn getDefaultImpl()"
Andrei Homescu71bd1c32020-12-17 18:21:05 -0800423 << " -> " << default_ref_name << " where Self: Sized {\n";
Andrei Homescub62afd92020-05-11 19:24:59 -0700424 *code_writer << " DEFAULT_IMPL.lock().unwrap().clone()\n";
425 *code_writer << "}\n";
Andrei Homescu71bd1c32020-12-17 18:21:05 -0800426 *code_writer << "fn setDefaultImpl(d: " << default_ref_name << ")"
427 << " -> " << default_ref_name << " where Self: Sized {\n";
Andrei Homescub62afd92020-05-11 19:24:59 -0700428 *code_writer << " std::mem::replace(&mut *DEFAULT_IMPL.lock().unwrap(), d)\n";
429 *code_writer << "}\n";
430 code_writer->Dedent();
431 *code_writer << "}\n";
432
Andrei Homescu71bd1c32020-12-17 18:21:05 -0800433 // Emit the default trait
434 *code_writer << "pub trait " << default_trait_name << ": Send + Sync {\n";
435 code_writer->Indent();
436 for (const auto& method : iface->GetMethods()) {
437 if (!method->IsUserDefined()) {
438 continue;
439 }
440
441 // Generate the default method
442 *code_writer << BuildMethod(*method, typenames) << " {\n";
443 code_writer->Indent();
444 *code_writer << "Err(binder::StatusCode::UNKNOWN_TRANSACTION.into())\n";
445 code_writer->Dedent();
446 *code_writer << "}\n";
447 }
448 code_writer->Dedent();
449 *code_writer << "}\n";
450
Andrei Homescub62afd92020-05-11 19:24:59 -0700451 // Generate the transaction code constants
452 // The constants get their own sub-module to avoid conflicts
453 *code_writer << "pub mod transactions {\n";
454 code_writer->Indent();
Andrei Homescub62afd92020-05-11 19:24:59 -0700455 for (const auto& method : iface->GetMethods()) {
456 // Generate the transaction code constant
457 *code_writer << "pub const " << method->GetName()
458 << ": binder::TransactionCode = "
Andrew Walbran7a1563f2021-03-09 12:17:13 +0000459 "binder::FIRST_CALL_TRANSACTION + " +
Andrei Homescub62afd92020-05-11 19:24:59 -0700460 std::to_string(method->GetId()) + ";\n";
461 }
462 code_writer->Dedent();
463 *code_writer << "}\n";
464
465 // Emit the default implementation code outside the trait
Andrei Homescu71bd1c32020-12-17 18:21:05 -0800466 *code_writer << "pub type " << default_ref_name << " = Option<std::sync::Arc<dyn "
467 << default_trait_name << ">>;\n";
Andrei Homescub62afd92020-05-11 19:24:59 -0700468 *code_writer << "use lazy_static::lazy_static;\n";
469 *code_writer << "lazy_static! {\n";
Andrei Homescu71bd1c32020-12-17 18:21:05 -0800470 *code_writer << " static ref DEFAULT_IMPL: std::sync::Mutex<" << default_ref_name
Andrei Homescub62afd92020-05-11 19:24:59 -0700471 << "> = std::sync::Mutex::new(None);\n";
472 *code_writer << "}\n";
473
474 // Emit the interface constants
Jooyung Han3f347ca2020-12-01 12:41:50 +0900475 GenerateConstantDeclarations(*code_writer, *iface, typenames);
Andrei Homescub62afd92020-05-11 19:24:59 -0700476
477 GenerateMangledAlias(*code_writer, iface);
478
479 // Emit VERSION and HASH
480 // These need to be top-level item constants instead of associated consts
481 // because the latter are incompatible with trait objects, see
482 // https://doc.rust-lang.org/reference/items/traits.html#object-safety
483 if (options.Version() > 0) {
484 *code_writer << "pub const VERSION: i32 = " << std::to_string(options.Version()) << ";\n";
485 }
486 if (!options.Hash().empty()) {
487 *code_writer << "pub const HASH: &str = \"" << options.Hash() << "\";\n";
488 }
489
490 // Generate the client-side methods
491 *code_writer << "impl " << trait_name << " for " << client_name << " {\n";
492 code_writer->Indent();
493 for (const auto& method : iface->GetMethods()) {
Steven Morelanda7764e52020-10-27 17:29:29 +0000494 GenerateClientMethod(*code_writer, *iface, *method, typenames, options, trait_name);
Andrei Homescub62afd92020-05-11 19:24:59 -0700495 }
496 code_writer->Dedent();
497 *code_writer << "}\n";
498
499 // Generate the server-side methods
500 GenerateServerItems(*code_writer, iface, typenames);
501
502 return true;
503}
504
Jooyung Hanf93b5bd2020-10-28 15:12:08 +0900505void GenerateParcelBody(CodeWriter& out, const AidlStructuredParcelable* parcel,
506 const AidlTypenames& typenames) {
Jooyung Han720253d2021-01-05 19:13:17 +0900507 GenerateDeprecated(out, *parcel);
Jooyung Hanf93b5bd2020-10-28 15:12:08 +0900508 out << "pub struct " << parcel->GetName() << " {\n";
509 out.Indent();
510 for (const auto& variable : parcel->GetFields()) {
Jooyung Han720253d2021-01-05 19:13:17 +0900511 GenerateDeprecated(out, *variable);
Jooyung Hanf93b5bd2020-10-28 15:12:08 +0900512 auto field_type = RustNameOf(variable->GetType(), typenames, StorageMode::PARCELABLE_FIELD);
513 out << "pub " << variable->GetName() << ": " << field_type << ",\n";
514 }
515 out.Dedent();
516 out << "}\n";
517}
518
Andrei Homescub62afd92020-05-11 19:24:59 -0700519void GenerateParcelDefault(CodeWriter& out, const AidlStructuredParcelable* parcel) {
520 out << "impl Default for " << parcel->GetName() << " {\n";
521 out.Indent();
522 out << "fn default() -> Self {\n";
523 out.Indent();
524 out << "Self {\n";
525 out.Indent();
526 for (const auto& variable : parcel->GetFields()) {
527 if (variable->GetDefaultValue()) {
528 out << variable->GetName() << ": " << variable->ValueString(ConstantValueDecorator) << ",\n";
529 } else {
530 out << variable->GetName() << ": Default::default(),\n";
531 }
532 }
533 out.Dedent();
534 out << "}\n";
535 out.Dedent();
536 out << "}\n";
537 out.Dedent();
538 out << "}\n";
539}
540
Jooyung Hanf93b5bd2020-10-28 15:12:08 +0900541void GenerateParcelSerializeBody(CodeWriter& out, const AidlStructuredParcelable* parcel,
542 const AidlTypenames& typenames) {
543 out << "parcel.sized_write(|subparcel| {\n";
544 out.Indent();
545 for (const auto& variable : parcel->GetFields()) {
546 if (!TypeHasDefault(variable->GetType(), typenames)) {
547 out << "let __field_ref = this." << variable->GetName()
548 << ".as_ref().ok_or(binder::StatusCode::UNEXPECTED_NULL)?;\n";
549 out << "subparcel.write(__field_ref)?;\n";
550 } else {
551 out << "subparcel.write(&this." << variable->GetName() << ")?;\n";
552 }
553 }
554 out << "Ok(())\n";
555 out.Dedent();
556 out << "})\n";
557}
558
559void GenerateParcelDeserializeBody(CodeWriter& out, const AidlStructuredParcelable* parcel,
560 const AidlTypenames& typenames) {
561 out << "let start_pos = parcel.get_data_position();\n";
562 out << "let parcelable_size: i32 = parcel.read()?;\n";
563 out << "if parcelable_size < 0 { return Err(binder::StatusCode::BAD_VALUE); }\n";
564
565 // Pre-emit the common field epilogue code, shared between all fields:
566 ostringstream epilogue;
567 epilogue << "if (parcel.get_data_position() - start_pos) == parcelable_size {\n";
568 // We assume the lhs can never be > parcelable_size, because then the read
569 // immediately preceding this check would have returned NOT_ENOUGH_DATA
570 epilogue << " return Ok(Some(result));\n";
571 epilogue << "}\n";
572 string epilogue_str = epilogue.str();
573
574 out << "let mut result = Self::default();\n";
575 for (const auto& variable : parcel->GetFields()) {
576 if (!TypeHasDefault(variable->GetType(), typenames)) {
577 out << "result." << variable->GetName() << " = Some(parcel.read()?);\n";
578 } else {
579 out << "result." << variable->GetName() << " = parcel.read()?;\n";
580 }
581 out << epilogue_str;
582 }
583
584 out << "Ok(Some(result))\n";
585}
586
587void GenerateParcelBody(CodeWriter& out, const AidlUnionDecl* parcel,
588 const AidlTypenames& typenames) {
Jooyung Han720253d2021-01-05 19:13:17 +0900589 GenerateDeprecated(out, *parcel);
Jooyung Hanf93b5bd2020-10-28 15:12:08 +0900590 out << "pub enum " << parcel->GetName() << " {\n";
591 out.Indent();
592 for (const auto& variable : parcel->GetFields()) {
Jooyung Han720253d2021-01-05 19:13:17 +0900593 GenerateDeprecated(out, *variable);
Jooyung Hanf93b5bd2020-10-28 15:12:08 +0900594 auto field_type = RustNameOf(variable->GetType(), typenames, StorageMode::PARCELABLE_FIELD);
595 out << variable->GetCapitalizedName() << "(" << field_type << "),\n";
596 }
597 out.Dedent();
598 out << "}\n";
599}
600
601void GenerateParcelDefault(CodeWriter& out, const AidlUnionDecl* parcel) {
602 out << "impl Default for " << parcel->GetName() << " {\n";
603 out.Indent();
604 out << "fn default() -> Self {\n";
605 out.Indent();
606
607 AIDL_FATAL_IF(parcel->GetFields().empty(), *parcel)
608 << "Union '" << parcel->GetName() << "' is empty.";
609 const auto& first_field = parcel->GetFields()[0];
610 const auto& first_value = first_field->ValueString(ConstantValueDecorator);
611
612 out << "Self::";
613 if (first_field->GetDefaultValue()) {
614 out << first_field->GetCapitalizedName() << "(" << first_value << ")\n";
615 } else {
616 out << first_field->GetCapitalizedName() << "(Default::default())\n";
617 }
618
619 out.Dedent();
620 out << "}\n";
621 out.Dedent();
622 out << "}\n";
623}
624
625void GenerateParcelSerializeBody(CodeWriter& out, const AidlUnionDecl* parcel,
626 const AidlTypenames& typenames) {
627 out << "match this {\n";
628 out.Indent();
629 int tag = 0;
630 for (const auto& variable : parcel->GetFields()) {
631 out << "Self::" << variable->GetCapitalizedName() << "(v) => {\n";
632 out.Indent();
633 out << "parcel.write(&" << std::to_string(tag++) << "i32)?;\n";
634 if (!TypeHasDefault(variable->GetType(), typenames)) {
635 out << "let __field_ref = v.as_ref().ok_or(binder::StatusCode::UNEXPECTED_NULL)?;\n";
636 out << "parcel.write(__field_ref)\n";
637 } else {
638 out << "parcel.write(v)\n";
639 }
640 out.Dedent();
641 out << "}\n";
642 }
643 out.Dedent();
644 out << "}\n";
645}
646
647void GenerateParcelDeserializeBody(CodeWriter& out, const AidlUnionDecl* parcel,
648 const AidlTypenames& typenames) {
649 out << "let tag: i32 = parcel.read()?;\n";
650 out << "match tag {\n";
651 out.Indent();
652 int tag = 0;
653 for (const auto& variable : parcel->GetFields()) {
654 auto field_type = RustNameOf(variable->GetType(), typenames, StorageMode::PARCELABLE_FIELD);
655
656 out << std::to_string(tag++) << " => {\n";
657 out.Indent();
658 out << "let value: " << field_type << " = ";
659 if (!TypeHasDefault(variable->GetType(), typenames)) {
660 out << "Some(parcel.read()?);\n";
661 } else {
662 out << "parcel.read()?;\n";
663 }
664 out << "Ok(Some(Self::" << variable->GetCapitalizedName() << "(value)))\n";
665 out.Dedent();
666 out << "}\n";
667 }
668 out << "_ => {\n";
669 out << " Err(binder::StatusCode::BAD_VALUE)\n";
670 out << "}\n";
671 out.Dedent();
672 out << "}\n";
673}
674
675template <typename ParcelableType>
676void GenerateParcelSerialize(CodeWriter& out, const ParcelableType* parcel,
Andrei Homescub62afd92020-05-11 19:24:59 -0700677 const AidlTypenames& typenames) {
678 out << "impl binder::parcel::Serialize for " << parcel->GetName() << " {\n";
679 out << " fn serialize(&self, parcel: &mut binder::parcel::Parcel) -> binder::Result<()> {\n";
680 out << " <Self as binder::parcel::SerializeOption>::serialize_option(Some(self), parcel)\n";
681 out << " }\n";
682 out << "}\n";
683
684 out << "impl binder::parcel::SerializeArray for " << parcel->GetName() << " {}\n";
685
686 out << "impl binder::parcel::SerializeOption for " << parcel->GetName() << " {\n";
687 out.Indent();
688 out << "fn serialize_option(this: Option<&Self>, parcel: &mut binder::parcel::Parcel) -> "
689 "binder::Result<()> {\n";
690 out.Indent();
691 out << "let this = if let Some(this) = this {\n";
692 out << " parcel.write(&1i32)?;\n";
693 out << " this\n";
694 out << "} else {\n";
695 out << " return parcel.write(&0i32);\n";
696 out << "};\n";
Jooyung Hanf93b5bd2020-10-28 15:12:08 +0900697
698 GenerateParcelSerializeBody(out, parcel, typenames);
699
Andrei Homescuad9d0da2020-08-06 18:43:40 -0700700 out.Dedent();
Andrei Homescub62afd92020-05-11 19:24:59 -0700701 out << "}\n";
702 out.Dedent();
703 out << "}\n";
704}
705
Jooyung Hanf93b5bd2020-10-28 15:12:08 +0900706template <typename ParcelableType>
707void GenerateParcelDeserialize(CodeWriter& out, const ParcelableType* parcel,
Andrei Homescub62afd92020-05-11 19:24:59 -0700708 const AidlTypenames& typenames) {
709 out << "impl binder::parcel::Deserialize for " << parcel->GetName() << " {\n";
710 out << " fn deserialize(parcel: &binder::parcel::Parcel) -> binder::Result<Self> {\n";
711 out << " <Self as binder::parcel::DeserializeOption>::deserialize_option(parcel)\n";
712 out << " .transpose()\n";
713 out << " .unwrap_or(Err(binder::StatusCode::UNEXPECTED_NULL))\n";
714 out << " }\n";
715 out << "}\n";
716
717 out << "impl binder::parcel::DeserializeArray for " << parcel->GetName() << " {}\n";
718
719 out << "impl binder::parcel::DeserializeOption for " << parcel->GetName() << " {\n";
Jooyung Hanf93b5bd2020-10-28 15:12:08 +0900720 out.Indent();
721 out << "fn deserialize_option(parcel: &binder::parcel::Parcel) -> binder::Result<Option<Self>> "
Andrei Homescub62afd92020-05-11 19:24:59 -0700722 "{\n";
Jooyung Hanf93b5bd2020-10-28 15:12:08 +0900723 out.Indent();
724 out << "let status: i32 = parcel.read()?;\n";
725 out << "if status == 0 { return Ok(None); }\n";
Andrei Homescub62afd92020-05-11 19:24:59 -0700726
Jooyung Hanf93b5bd2020-10-28 15:12:08 +0900727 GenerateParcelDeserializeBody(out, parcel, typenames);
Andrei Homescub62afd92020-05-11 19:24:59 -0700728
Jooyung Hanf93b5bd2020-10-28 15:12:08 +0900729 out.Dedent();
730 out << "}\n";
731 out.Dedent();
Andrei Homescub62afd92020-05-11 19:24:59 -0700732 out << "}\n";
733}
734
Jooyung Hanf93b5bd2020-10-28 15:12:08 +0900735template <typename ParcelableType>
736bool GenerateRustParcel(const string& filename, const ParcelableType* parcel,
Andrei Homescub62afd92020-05-11 19:24:59 -0700737 const AidlTypenames& typenames, const IoDelegate& io_delegate) {
738 CodeWriterPtr code_writer = io_delegate.GetCodeWriter(filename);
739
Andrei Homescue61feb52020-08-18 15:44:24 -0700740 // Debug is always derived because all Rust AIDL types implement it
741 // ParcelFileDescriptor doesn't support any of the others because
742 // it's a newtype over std::fs::File which only implements Debug
743 vector<string> derives{"Debug"};
744 const AidlAnnotation* derive_annotation = parcel->RustDerive();
745 if (derive_annotation != nullptr) {
746 for (const auto& name_and_param : derive_annotation->AnnotationParams(ConstantValueDecorator)) {
747 if (name_and_param.second == "true") {
748 derives.push_back(name_and_param.first);
749 }
750 }
751 }
752
753 *code_writer << "#[derive(" << Join(derives, ", ") << ")]\n";
Jooyung Hanf93b5bd2020-10-28 15:12:08 +0900754 GenerateParcelBody(*code_writer, parcel, typenames);
Jooyung Han3f347ca2020-12-01 12:41:50 +0900755 GenerateConstantDeclarations(*code_writer, *parcel, typenames);
Andrei Homescub62afd92020-05-11 19:24:59 -0700756 GenerateMangledAlias(*code_writer, parcel);
757 GenerateParcelDefault(*code_writer, parcel);
758 GenerateParcelSerialize(*code_writer, parcel, typenames);
759 GenerateParcelDeserialize(*code_writer, parcel, typenames);
Andrei Homescub62afd92020-05-11 19:24:59 -0700760 return true;
761}
762
763bool GenerateRustEnumDeclaration(const string& filename, const AidlEnumDeclaration* enum_decl,
764 const AidlTypenames& typenames, const IoDelegate& io_delegate) {
765 CodeWriterPtr code_writer = io_delegate.GetCodeWriter(filename);
766
767 const auto& aidl_backing_type = enum_decl->GetBackingType();
768 auto backing_type = RustNameOf(aidl_backing_type, typenames, StorageMode::VALUE);
769
Jooyung Hanb7a60ec2021-01-21 13:47:59 +0900770 // TODO(b/177860423) support "deprecated" for enum types
Andrei Homescub62afd92020-05-11 19:24:59 -0700771 *code_writer << "#![allow(non_upper_case_globals)]\n";
Andrei Homescu0717c432020-09-04 17:41:00 -0700772 *code_writer << "use binder::declare_binder_enum;\n";
773 *code_writer << "declare_binder_enum! { " << enum_decl->GetName() << " : " << backing_type
774 << " {\n";
775 code_writer->Indent();
Andrei Homescub62afd92020-05-11 19:24:59 -0700776 for (const auto& enumerator : enum_decl->GetEnumerators()) {
777 auto value = enumerator->GetValue()->ValueString(aidl_backing_type, ConstantValueDecorator);
Andrei Homescu0717c432020-09-04 17:41:00 -0700778 *code_writer << enumerator->GetName() << " = " << value << ",\n";
Andrei Homescub62afd92020-05-11 19:24:59 -0700779 }
Andrei Homescu0717c432020-09-04 17:41:00 -0700780 code_writer->Dedent();
781 *code_writer << "} }\n";
Andrei Homescub62afd92020-05-11 19:24:59 -0700782
783 GenerateMangledAlias(*code_writer, enum_decl);
784
785 return true;
786}
787
788bool GenerateRust(const string& filename, const AidlDefinedType* defined_type,
789 const AidlTypenames& typenames, const IoDelegate& io_delegate,
790 const Options& options) {
791 if (const AidlStructuredParcelable* parcelable = defined_type->AsStructuredParcelable();
792 parcelable != nullptr) {
793 return GenerateRustParcel(filename, parcelable, typenames, io_delegate);
794 }
795
Jooyung Hanf93b5bd2020-10-28 15:12:08 +0900796 if (const AidlUnionDecl* parcelable = defined_type->AsUnionDeclaration(); parcelable != nullptr) {
797 return GenerateRustParcel(filename, parcelable, typenames, io_delegate);
798 }
799
Andrei Homescub62afd92020-05-11 19:24:59 -0700800 if (const AidlEnumDeclaration* enum_decl = defined_type->AsEnumDeclaration();
801 enum_decl != nullptr) {
802 return GenerateRustEnumDeclaration(filename, enum_decl, typenames, io_delegate);
803 }
804
805 if (const AidlInterface* interface = defined_type->AsInterface(); interface != nullptr) {
806 return GenerateRustInterface(filename, interface, typenames, io_delegate, options);
807 }
808
Steven Moreland21780812020-09-11 01:29:45 +0000809 AIDL_FATAL(filename) << "Unrecognized type sent for Rust generation.";
Andrei Homescub62afd92020-05-11 19:24:59 -0700810 return false;
811}
812
813} // namespace rust
814} // namespace aidl
815} // namespace android