1use super::method_context::{
9 TraitMethodContext, class_ref, s7_trait_member_access, trait_namespace_env,
10 trait_namespace_env_var, trait_namespace_target,
11};
12use super::{TraitConst, TraitMethod};
13use crate::miniextendr_impl::ClassSystem;
14use crate::r_class_formatter::emit_s3_generic_guard;
15
16pub(super) struct TraitWrapperOpts {
18 pub(super) class_system: ClassSystem,
20 pub(super) class_has_no_rd: bool,
23 pub(super) internal: bool,
26 pub(super) noexport: bool,
29 pub(super) impl_preconditions: Option<bool>,
32}
33
34pub(super) fn binds_trait_namespace(
41 class_system: ClassSystem,
42 methods: &[TraitMethod],
43 consts: &[TraitConst],
44) -> bool {
45 match class_system {
46 ClassSystem::Env => true,
47 ClassSystem::R6 => !methods.is_empty() || !consts.is_empty(),
48 ClassSystem::S3 | ClassSystem::Vctrs => {
49 methods.iter().any(|m| !m.has_self) || !consts.is_empty()
50 }
51 ClassSystem::S4 | ClassSystem::S7 => false,
52 }
53}
54
55pub(super) fn generate_trait_r_wrapper(
66 type_ident: &syn::Ident,
67 trait_name: &syn::Ident,
68 methods: &[TraitMethod],
69 consts: &[TraitConst],
70 opts: TraitWrapperOpts,
71) -> syn::Result<String> {
72 let TraitWrapperOpts {
73 class_system,
74 class_has_no_rd,
75 internal,
76 noexport,
77 impl_preconditions,
78 } = opts;
79 reject_unsupported_describe_in(methods, class_system)?;
80 check_trait_method_formals(methods, class_system)?;
81 let generate = match class_system {
82 ClassSystem::Env => generate_trait_env_r_wrapper,
83 ClassSystem::S3 | ClassSystem::Vctrs => generate_trait_s3_r_wrapper,
85 ClassSystem::S4 => generate_trait_s4_r_wrapper,
86 ClassSystem::S7 => generate_trait_s7_r_wrapper,
87 ClassSystem::R6 => generate_trait_r6_r_wrapper,
88 };
89 let result = generate(type_ident, trait_name, methods, consts, impl_preconditions);
90
91 let suppress_all_rd = class_has_no_rd || (noexport && !internal);
99 if suppress_all_rd {
100 if #[allow(non_exhaustive_omitted_patterns)] match class_system {
ClassSystem::S3 | ClassSystem::Vctrs => true,
_ => false,
}matches!(class_system, ClassSystem::S3 | ClassSystem::Vctrs) {
101 let keep_export = class_has_no_rd;
108 let mut filtered = Vec::new();
109 let mut roxygen_block: Vec<&str> = Vec::new();
110
111 let flush_block = |block: &mut Vec<&str>, out: &mut Vec<String>| {
112 if block.iter().any(|line| line.contains("@method ")) {
113 out.push("#' @noRd".to_string());
114 for &line in block.iter() {
115 if line.contains("@method ")
116 || line.contains("@param ")
117 || (keep_export && line.contains("@export"))
118 {
119 out.push(line.to_string());
120 }
121 }
122 }
123 block.clear();
124 };
125
126 for line in result.lines() {
127 if line.starts_with("#'") {
128 roxygen_block.push(line);
129 continue;
130 }
131
132 if !roxygen_block.is_empty() {
133 flush_block(&mut roxygen_block, &mut filtered);
134 }
135 filtered.push(line.to_string());
136 }
137
138 if !roxygen_block.is_empty() {
139 flush_block(&mut roxygen_block, &mut filtered);
140 }
141
142 Ok(filtered.join("\n"))
143 } else {
144 Ok(result
145 .lines()
146 .filter(|line| !line.starts_with("#'"))
147 .collect::<Vec<_>>()
148 .join("\n"))
149 }
150 } else if internal {
151 let has_export = result.lines().any(|line| line.contains("@export"));
158 let lines: Vec<&str> = result.lines().collect();
159 let mut processed: Vec<String> = Vec::with_capacity(lines.len());
160 let mut start = 0;
161 while start < lines.len() {
162 let roxygen = lines[start].starts_with("#'");
163 let end = lines[start..]
164 .iter()
165 .position(|line| line.starts_with("#'") != roxygen)
166 .map_or(lines.len(), |len| start + len);
167 let block = &lines[start..end];
168 let registers_s3 = roxygen && block.iter().any(|line| line.contains("@method "));
169 for &line in block {
170 if line.contains("@export") && !(registers_s3 && line.trim_end() == "#' @export") {
171 processed.push("#' @keywords internal".to_string());
172 } else {
173 processed.push(line.to_string());
174 }
175 }
176 start = end;
177 }
178 if !has_export && let Some(pos) = processed.iter().position(|l| l.starts_with("#'")) {
181 processed.insert(pos, "#' @keywords internal".to_string());
182 }
183 Ok(processed.join("\n"))
184 } else {
185 Ok(result)
186 }
187}
188
189const PAGE_TAGS: &[&str] = &[
201 "describeIn",
202 "rdname",
203 "name",
204 "order",
205 "inheritParams",
206 "inherit",
207 "inheritDotParams",
208];
209
210fn forwarded_body_tag(tag: &str) -> bool {
219 crate::roxygen::forwarded_member_tag(tag)
220 && crate::roxygen::roxygen_tag_name(tag)
221 .is_none_or(|name| name != "title" && !PAGE_TAGS.contains(&name))
222}
223
224fn namespace_member_body_lines(
232 ctx: &TraitMethodContext<'_>,
233 type_str: &str,
234 label: &str,
235) -> Vec<String> {
236 let choice_docs = ctx.choice_param_docs();
237 crate::r_class_formatter::describe_params_lines(
238 &ctx.method.doc_tags,
239 forwarded_body_tag,
240 &ctx.params,
241 type_str,
242 Some(&choice_docs),
243 label,
244 )
245}
246
247fn param_doc_tags(ctx: &TraitMethodContext<'_>, type_str: &str) -> Vec<String> {
255 let method = ctx.method;
256 let mut tags: Vec<String> = param_tags(method).cloned().collect();
257 if params_documented_elsewhere(method, type_str) {
258 return tags;
259 }
260 let choice_docs = ctx.choice_param_docs();
261 for formal in crate::roxygen::split_r_formals(&ctx.params) {
262 let pname = crate::roxygen::formal_name(formal);
263 if pname == "..." || crate::roxygen::param_documented(&method.doc_tags, pname) {
264 continue;
265 }
266 let body = choice_docs
267 .get(pname)
268 .map_or("(undocumented)", String::as_str);
269 tags.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("@param {0} {1}", pname, body))
})format!("@param {pname} {body}"));
270 }
271 tags
272}
273
274fn param_tags(method: &TraitMethod) -> impl Iterator<Item = &String> {
276 method
277 .doc_tags
278 .iter()
279 .filter(|tag| crate::roxygen::roxygen_tag_name(tag) == Some("param"))
280}
281
282fn method_page<'a>(method: &'a TraitMethod, type_str: &'a str) -> &'a str {
287 crate::roxygen::method_page(&method.doc_tags, type_str)
288}
289
290fn joins_author_topic(method: &TraitMethod, type_str: &str) -> bool {
296 crate::roxygen::joins_author_topic(&method.doc_tags, Some(type_str))
297}
298
299fn params_documented_elsewhere(method: &TraitMethod, type_str: &str) -> bool {
303 crate::roxygen::params_documented_elsewhere(&method.doc_tags, Some(type_str))
304}
305
306fn describes_in(method: &TraitMethod) -> bool {
309 crate::roxygen::describe_in_topic(&method.doc_tags).is_some()
310}
311
312fn own_block_page_lines(
326 method: &TraitMethod,
327 type_str: &str,
328 default_name: Option<&str>,
329 split_title: Option<&str>,
330) -> Vec<String> {
331 let tags = &method.doc_tags;
332 let mut lines = Vec::new();
333 if !describes_in(method) {
334 let has = |tag: &str| crate::roxygen::has_roxygen_tag(tags, tag);
335 if has("rdname")
336 && let Some(title) = split_title
337 {
338 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("#\' @title {0}", title))
})format!("#' @title {title}"));
339 }
340 if !has("name")
341 && let Some(name) = default_name
342 {
343 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("#\' @name {0}", name))
})format!("#' @name {name}"));
344 }
345 if !has("rdname") {
346 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("#\' @rdname {0}", type_str))
})format!("#' @rdname {type_str}"));
347 }
348 }
349 let page_tags: Vec<String> = tags
350 .iter()
351 .filter(|tag| {
352 crate::roxygen::roxygen_tag_name(tag).is_some_and(|name| PAGE_TAGS.contains(&name))
353 })
354 .cloned()
355 .collect();
356 crate::roxygen::push_roxygen_tags(&mut lines, &page_tags);
357 crate::roxygen::push_order_after_topic_blocks(&mut lines, tags, type_str);
358 lines
359}
360
361fn push_param_tags(lines: &mut Vec<String>, method: &TraitMethod) {
364 let params: Vec<String> = param_tags(method).cloned().collect();
365 crate::roxygen::push_roxygen_tags(lines, ¶ms);
366}
367
368fn check_trait_method_formals(
375 methods: &[TraitMethod],
376 class_system: ClassSystem,
377) -> syn::Result<()> {
378 let system = match class_system {
379 ClassSystem::Env => "Env-class",
380 ClassSystem::R6 => "R6",
381 ClassSystem::S3 => "S3",
382 ClassSystem::S4 => "S4",
383 ClassSystem::S7 => "S7",
384 ClassSystem::Vctrs => "vctrs",
385 };
386 for method in methods {
387 if (method.generic.is_some() || method.receiver.is_some() || method.no_dispatch_dots)
388 && (!method.has_self || !#[allow(non_exhaustive_omitted_patterns)] match class_system {
ClassSystem::S3 | ClassSystem::S4 => true,
_ => false,
}matches!(class_system, ClassSystem::S3 | ClassSystem::S4))
389 {
390 return Err(syn::Error::new_spanned(
391 &method.sig,
392 "`generic`, `receiver` and `no_dots` apply only to S3/S4 instance methods",
393 ));
394 }
395 if (method.receiver.is_some() || method.no_dispatch_dots) && method.generic.is_none() {
396 return Err(syn::Error::new_spanned(
397 &method.sig,
398 "`receiver` and `no_dots` require an explicit S3/S4 `generic = \"...\"`",
399 ));
400 }
401 let mut reserved = Vec::new();
402 if method.has_self && !#[allow(non_exhaustive_omitted_patterns)] match class_system {
ClassSystem::Env | ClassSystem::R6 => true,
_ => false,
}matches!(class_system, ClassSystem::Env | ClassSystem::R6) {
403 reserved.push((
404 method.receiver.as_deref().unwrap_or("x").to_string(),
405 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("is the receiver of the generated {0} trait method",
system))
})format!("is the receiver of the generated {system} trait method"),
406 ));
407 }
408 crate::r_wrapper_builder::check_r_formals(
409 &method.sig.inputs,
410 &Default::default(),
411 &reserved,
412 )?;
413 }
414 Ok(())
415}
416
417fn reject_unsupported_describe_in(
438 methods: &[TraitMethod],
439 class_system: ClassSystem,
440) -> syn::Result<()> {
441 let system = match class_system {
442 ClassSystem::S3 => "S3",
443 ClassSystem::Vctrs => "vctrs",
444 ClassSystem::S4 => "S4",
445 ClassSystem::S7 => "S7",
446 ClassSystem::Env => "Env",
447 ClassSystem::R6 => "R6",
448 };
449 let mut errors: Option<syn::Error> = None;
450 for method in methods.iter().filter(|m| describes_in(m)) {
451 let supported = match class_system {
452 ClassSystem::S3 | ClassSystem::Vctrs => method.has_self,
453 ClassSystem::S4 => true,
454 ClassSystem::S7 => method.has_self && !method.no_shortcut,
455 ClassSystem::Env | ClassSystem::R6 => false,
456 };
457 if supported {
458 continue;
459 }
460 let (kind, qualifier) = if !method.has_self {
461 ("static method", "")
462 } else if #[allow(non_exhaustive_omitted_patterns)] match class_system {
ClassSystem::S7 => true,
_ => false,
}matches!(class_system, ClassSystem::S7) {
463 ("instance method", " without a fast-path shortcut")
464 } else {
465 ("instance method", "")
466 };
467 let error = syn::Error::new_spanned(
468 &method.ident,
469 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`@describeIn` is not supported on the {1} trait {2} `{0}`{3}: its R wrapper is not an R function roxygen2 can list in the destination\'s \"Functions\" section, so roxygen2 would drop the block. Use `@rdname <topic>` to document it on that page instead. On trait impls `@describeIn` works on S3, vctrs and S4 instance methods, on S4 static methods, and on S7 instance methods with a fast-path shortcut.",
method.ident, system, kind, qualifier))
})format!(
470 "`@describeIn` is not supported on the {system} trait {kind} `{}`{qualifier}: \
471 its R wrapper is not an R function roxygen2 can list in the destination's \
472 \"Functions\" section, so roxygen2 would drop the block. Use `@rdname <topic>` \
473 to document it on that page instead. On trait impls `@describeIn` works on \
474 S3, vctrs and S4 instance methods, on S4 static methods, and on S7 instance \
475 methods with a fast-path shortcut.",
476 method.ident,
477 ),
478 );
479 match errors.as_mut() {
480 Some(all) => all.combine(error),
481 None => errors = Some(error),
482 }
483 }
484 errors.map_or(Ok(()), Err)
485}
486
487fn generate_trait_env_r_wrapper(
501 type_ident: &syn::Ident,
502 trait_name: &syn::Ident,
503 methods: &[TraitMethod],
504 consts: &[TraitConst],
505 impl_preconditions: Option<bool>,
506) -> String {
507 use crate::r_wrapper_builder::{DotCallBuilder, RoxygenBuilder};
508
509 let mut lines = Vec::new();
510 let type_str = type_ident.to_string();
511
512 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("# Trait methods and consts for {0} implementing {1}",
type_ident, trait_name))
})format!(
514 "# Trait methods and consts for {} implementing {}",
515 type_ident, trait_name
516 ));
517 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("# Generated by #[miniextendr] impl {0} for {1}",
trait_name, type_ident))
})format!(
518 "# Generated by #[miniextendr] impl {} for {}",
519 trait_name, type_ident
520 ));
521 lines.push(String::new());
522
523 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} <- new.env(parent = emptyenv())",
trait_namespace_env(ClassSystem::Env, type_ident,
trait_name)))
})format!(
525 "{} <- new.env(parent = emptyenv())",
526 trait_namespace_env(ClassSystem::Env, type_ident, trait_name)
527 ));
528 lines.push(String::new());
529
530 for method in methods {
531 let r_name = method.r_method_name();
532 let ctx = TraitMethodContext::new(method, type_ident, trait_name)
533 .with_impl_preconditions(impl_preconditions);
534
535 let target = ctx.namespace_target(ClassSystem::Env);
538 let symbol = ctx.namespace_symbol(ClassSystem::Env);
539
540 lines.extend(namespace_member_body_lines(&ctx, &type_str, &target));
544 lines.extend(own_block_page_lines(
545 method,
546 &type_str,
547 Some(&target),
548 Some(&target),
549 ));
550
551 let (full_params, call) = if method.has_self {
553 let fp = if ctx.params.is_empty() {
554 ".mx_self".to_string()
555 } else {
556 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(".mx_self, {0}", ctx.params))
})format!(".mx_self, {}", ctx.params)
557 };
558 (fp, ctx.instance_call(".mx_self"))
559 } else {
560 (ctx.params.clone(), ctx.static_call())
561 };
562
563 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} <- function({1}) {{", symbol,
full_params))
})format!("{symbol} <- function({full_params}) {{"));
565 ctx.emit_method_prelude(&mut lines, " ", &r_name);
566 lines.extend(ctx.method_body_lines(&call, ClassSystem::Env));
567 if method.has_self {
568 finish_instance_body(&mut lines, method, ".mx_self");
569 }
570 lines.push("}".to_string());
571
572 if method.has_self {
574 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("attr({0}, \".__mx_instance__\") <- TRUE",
symbol))
})format!("attr({symbol}, \".__mx_instance__\") <- TRUE"));
575 }
576
577 lines.push(String::new());
578 }
579
580 for trait_const in consts {
582 let const_name = &trait_const.ident;
583 let const_str = const_name.to_string();
584 let target = trait_namespace_target(ClassSystem::Env, type_ident, trait_name, &const_str);
585
586 let roxygen = RoxygenBuilder::new()
588 .name(target.clone())
589 .rdname(&type_str)
590 .build();
591 lines.extend(roxygen);
592
593 let c_ident = trait_const.c_wrapper_ident_string(type_ident, trait_name);
595 let call = DotCallBuilder::new(&c_ident).build();
596
597 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} <- function() {{", target))
})format!("{target} <- function() {{"));
599 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" {0}", call))
})format!(" {}", call));
600 lines.push("}".to_string());
601 lines.push(String::new());
602 }
603
604 lines.join("\n")
605}
606
607fn generate_trait_s3_r_wrapper(
620 type_ident: &syn::Ident,
621 trait_name: &syn::Ident,
622 methods: &[TraitMethod],
623 consts: &[TraitConst],
624 impl_preconditions: Option<bool>,
625) -> String {
626 use crate::r_wrapper_builder::{DotCallBuilder, RoxygenBuilder};
627
628 let mut lines = Vec::new();
629 let type_str = type_ident.to_string();
630 let class_str = class_ref(ClassSystem::S3, type_ident);
633
634 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("# S3 trait methods for {0} implementing {1}",
type_ident, trait_name))
})format!(
636 "# S3 trait methods for {} implementing {}",
637 type_ident, trait_name
638 ));
639 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("# Generated by #[miniextendr(s3)] impl {0} for {1}",
trait_name, type_ident))
})format!(
640 "# Generated by #[miniextendr(s3)] impl {} for {}",
641 trait_name, type_ident
642 ));
643 lines.push(String::new());
644
645 let instance_methods: Vec<_> = methods.iter().filter(|m| m.has_self).collect();
647 let static_methods: Vec<_> = methods.iter().filter(|m| !m.has_self).collect();
648
649 for method in &instance_methods {
653 let qualified_generic = method
654 .generic
655 .clone()
656 .unwrap_or_else(|| method.r_method_name());
657 let (generic_package, generic_name) = crate::naming::split_generic(&qualified_generic);
658 let receiver = method.receiver.as_deref().unwrap_or("x");
659 let s3_method_name = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}.{1}", generic_name, class_str))
})format!("{generic_name}.{class_str}");
660 let ctx = TraitMethodContext::new(method, type_ident, trait_name)
661 .with_impl_preconditions(impl_preconditions)
662 .with_call_attribution(crate::r_wrapper_builder::CallAttribution::Generic);
663
664 if method.generic.is_none() {
665 let tags = &method.doc_tags;
677 let generic_roxygen = RoxygenBuilder::new();
678 let generic_roxygen =
679 if crate::roxygen::has_roxygen_tag(tags, "rdname") || describes_in(method) {
680 generic_roxygen
681 } else {
682 generic_roxygen.title(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("S3 generic for `{0}`",
generic_name))
})format!("S3 generic for `{}`", generic_name))
683 };
684 let generic_roxygen = generic_roxygen
685 .name(&s3_method_name)
686 .rdname(method_page(method, &type_str));
687 let generic_roxygen = if joins_author_topic(method, &type_str) {
690 generic_roxygen
691 } else {
692 generic_roxygen
693 .description(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("S3 generic for `{0}`",
generic_name))
})format!("S3 generic for `{}`", generic_name))
694 .custom("@param x An object")
695 .custom("@param ... Additional arguments passed to methods")
696 };
697 let generic_roxygen = if crate::roxygen::orders_after_topic_blocks(tags, &type_str) {
698 generic_roxygen.custom(crate::roxygen::ORDER_AFTER_TOPIC_BLOCKS)
699 } else {
700 generic_roxygen
701 };
702 let generic_roxygen = generic_roxygen
703 .source(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("Generated by miniextendr from `impl {0} for {1}`",
trait_name, type_ident))
})format!(
704 "Generated by miniextendr from `impl {} for {}`",
705 trait_name, type_ident
706 ))
707 .custom(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("@export {0}", generic_name))
})format!("@export {}", generic_name))
712 .build();
713 lines.extend(generic_roxygen);
714
715 lines.push(emit_s3_generic_guard(generic_name));
717 lines.push(String::new());
718 }
719
720 let full_params = crate::r_class_formatter::instance_formals(
722 &ctx.params,
723 receiver,
724 !method.no_dispatch_dots,
725 );
726
727 lines.extend(own_block_page_lines(
736 method,
737 &type_str,
738 None,
739 Some(&s3_method_name),
740 ));
741 let mut method_roxygen = match generic_package {
744 Some(package) => RoxygenBuilder::new().custom(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("@exportS3Method {0}::{1} {2}",
package, generic_name, class_str))
})format!(
745 "@exportS3Method {package}::{generic_name} {class_str}"
746 )),
747 None => RoxygenBuilder::new().export(),
748 }
749 .method(generic_name, &class_str);
750 if method.generic.is_some() && !joins_author_topic(method, &type_str) {
751 method_roxygen = method_roxygen.custom(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("@param {0} An object", receiver))
})format!("@param {receiver} An object"));
752 if !method.no_dispatch_dots {
753 method_roxygen =
754 method_roxygen.custom("@param ... Additional arguments passed to methods");
755 }
756 }
757 for tag in param_doc_tags(&ctx, &type_str) {
758 method_roxygen = method_roxygen.custom(tag);
759 }
760 lines.extend(method_roxygen.build());
761
762 let call = ctx.instance_call(receiver);
764
765 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} <- function({1}) {{",
crate::naming::r_def_name(&s3_method_name), full_params))
})format!(
767 "{} <- function({}) {{",
768 crate::naming::r_def_name(&s3_method_name),
769 full_params
770 ));
771 ctx.emit_method_prelude(&mut lines, " ", generic_name);
772 lines.extend(ctx.method_body_lines(&call, ClassSystem::S3));
773 finish_instance_body(&mut lines, method, receiver);
774 lines.push("}".to_string());
775
776 if generic_package.is_none() {
781 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("if (inherits(get0(\"{0}\", mode = \"function\"), \"S7_generic\")) {{",
generic_name))
})format!(
782 "if (inherits(get0(\"{generic_name}\", mode = \"function\"), \"S7_generic\")) {{"
783 ));
784 let generic_symbol = crate::naming::r_def_name(generic_name);
787 let method_symbol = crate::naming::r_def_name(&s3_method_name);
788 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" S7::method({0}, S7::new_S3_class(\"{1}\")) <- {2}",
generic_symbol, class_str, method_symbol))
})format!(
789 " S7::method({generic_symbol}, S7::new_S3_class(\"{class_str}\")) <- {method_symbol}"
790 ));
791 lines.push("}".to_string());
792 }
793 lines.push(String::new());
794 }
795
796 if binds_trait_namespace(ClassSystem::S3, methods, consts) {
798 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} <- new.env(parent = emptyenv())",
trait_namespace_env(ClassSystem::S3, type_ident, trait_name)))
})format!(
799 "{} <- new.env(parent = emptyenv())",
800 trait_namespace_env(ClassSystem::S3, type_ident, trait_name)
801 ));
802 lines.push(String::new());
803 }
804
805 for method in &static_methods {
807 let r_name = method.r_method_name();
808 let ctx = TraitMethodContext::new(method, type_ident, trait_name)
809 .with_impl_preconditions(impl_preconditions);
810 let target = ctx.namespace_target(ClassSystem::S3);
811
812 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("#\' Static trait method {0}::{1}()",
trait_name, r_name))
})format!(
815 "#' Static trait method {}::{}()",
816 trait_name, r_name
817 ));
818 lines.extend(namespace_member_body_lines(&ctx, &type_str, &target));
819 lines.extend(own_block_page_lines(method, &type_str, Some(&target), None));
820
821 let call = ctx.static_call();
822
823 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} <- function({1}) {{",
ctx.namespace_symbol(ClassSystem::S3), ctx.params))
})format!(
824 "{} <- function({}) {{",
825 ctx.namespace_symbol(ClassSystem::S3),
826 ctx.params
827 ));
828 ctx.emit_method_prelude(&mut lines, " ", &r_name);
829 lines.extend(ctx.method_body_lines(&call, ClassSystem::S3));
830 lines.push("}".to_string());
831 lines.push(String::new());
832 }
833
834 for trait_const in consts {
836 let const_name = &trait_const.ident;
837 let const_str = const_name.to_string();
838 let target = trait_namespace_target(ClassSystem::S3, type_ident, trait_name, &const_str);
839
840 let roxygen = RoxygenBuilder::new()
841 .name(target.clone())
842 .rdname(&type_str)
843 .build();
844 lines.extend(roxygen);
845
846 let c_ident = trait_const.c_wrapper_ident_string(type_ident, trait_name);
847 let call = DotCallBuilder::new(&c_ident).build();
848
849 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} <- function() {{", target))
})format!("{target} <- function() {{"));
850 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" {0}", call))
})format!(" {}", call));
851 lines.push("}".to_string());
852 lines.push(String::new());
853 }
854
855 lines.join("\n")
856}
857
858fn generate_trait_s4_r_wrapper(
869 type_ident: &syn::Ident,
870 trait_name: &syn::Ident,
871 methods: &[TraitMethod],
872 consts: &[TraitConst],
873 impl_preconditions: Option<bool>,
874) -> String {
875 use crate::r_wrapper_builder::{DotCallBuilder, RoxygenBuilder};
876
877 let mut lines = Vec::new();
878 let type_str = type_ident.to_string();
879
880 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("# S4 trait methods for {0} implementing {1}",
type_ident, trait_name))
})format!(
882 "# S4 trait methods for {} implementing {}",
883 type_ident, trait_name
884 ));
885 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("# Generated by #[miniextendr(s4)] impl {0} for {1}",
trait_name, type_ident))
})format!(
886 "# Generated by #[miniextendr(s4)] impl {} for {}",
887 trait_name, type_ident
888 ));
889 lines.push(String::new());
890
891 let instance_methods: Vec<_> = methods.iter().filter(|m| m.has_self).collect();
893 let static_methods: Vec<_> = methods.iter().filter(|m| !m.has_self).collect();
894
895 if !instance_methods.is_empty() {
904 lines.push("#' @importFrom methods setGeneric setMethod".to_string());
905 lines.push(String::new());
906 }
907
908 for method in &instance_methods {
910 let generic_name = method
911 .generic
912 .clone()
913 .unwrap_or_else(|| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("s4_trait_{0}_{1}", trait_name,
method.r_method_name()))
})format!("s4_trait_{}_{}", trait_name, method.r_method_name()));
914 let receiver = method.receiver.as_deref().unwrap_or("x");
915 let ctx = TraitMethodContext::new(method, type_ident, trait_name)
916 .with_impl_preconditions(impl_preconditions);
917
918 let full_params = crate::r_class_formatter::instance_formals(
920 &ctx.params,
921 receiver,
922 !method.no_dispatch_dots,
923 );
924
925 if method.generic.is_none() {
926 let mut generic_roxygen = RoxygenBuilder::new()
931 .description(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("S4 generic for trait method `{0}::{1}`",
trait_name, method.r_method_name()))
})format!(
932 "S4 generic for trait method `{}::{}`",
933 trait_name,
934 method.r_method_name()
935 ))
936 .name(&generic_name)
937 .rdname(&type_str)
938 .source(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("Generated by miniextendr from `impl {0} for {1}`",
trait_name, type_ident))
})format!(
939 "Generated by miniextendr from `impl {} for {}`",
940 trait_name, type_ident
941 ))
942 .custom(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("@param x A `{0}` object",
type_str))
})format!("@param x A `{}` object", type_str))
943 .custom("@param ... Additional arguments passed to methods");
944 for tag in param_tags(method) {
945 generic_roxygen = generic_roxygen.custom(tag.clone());
946 }
947 lines.extend(generic_roxygen.export().build());
948 }
949 lines.push(crate::miniextendr_impl::s4_class::s4_generic_guard(
952 &generic_name,
953 method.generic.is_some(),
954 receiver,
955 ));
956 lines.push(String::new());
957
958 lines.extend(own_block_page_lines(
963 method,
964 &type_str,
965 None,
966 Some(&generic_name),
967 ));
968 if method.generic.is_some() && !joins_author_topic(method, &type_str) {
969 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("#\' @param {0} A `{1}` object",
receiver, type_str))
})format!("#' @param {receiver} A `{type_str}` object"));
970 if !method.no_dispatch_dots {
971 lines.push("#' @param ... Additional arguments passed to methods".to_string());
972 }
973 }
974 push_param_tags(&mut lines, method);
975 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("#\' @exportMethod {0}",
generic_name))
})format!("#' @exportMethod {}", generic_name));
976
977 let class_str = class_ref(ClassSystem::S4, type_ident);
978 let signature = if method.receiver.is_some() {
979 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("c({0} = \"{1}\")", receiver,
class_str))
})format!("c({receiver} = \"{class_str}\")")
980 } else {
981 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("\"{0}\"", class_str))
})format!("\"{class_str}\"")
982 };
983 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("methods::setMethod(\"{0}\", {1}, function({2}) {{",
generic_name, signature, full_params))
})format!(
984 "methods::setMethod(\"{generic_name}\", {signature}, function({full_params}) {{"
985 ));
986 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" .ptr <- {0}@ptr", receiver))
})format!(" .ptr <- {receiver}@ptr"));
988 let s4_call = ctx.instance_call(".ptr");
989 ctx.emit_method_prelude(&mut lines, " ", &method.r_method_name());
990 lines.extend(ctx.method_body_lines(&s4_call, ClassSystem::S4));
991 finish_instance_body(&mut lines, method, receiver);
992 lines.push("})".to_string());
993 lines.push(String::new());
994 }
995
996 for method in &static_methods {
1000 let r_name = method.r_method_name();
1001 let ctx = TraitMethodContext::new(method, type_ident, trait_name)
1002 .with_impl_preconditions(impl_preconditions);
1003 let fn_name = ctx.namespace_target(ClassSystem::S4);
1004
1005 if !describes_in(method) {
1009 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("#\' Static trait method {0}::{1}() for {2}",
trait_name, r_name, type_str))
})format!(
1010 "#' Static trait method {}::{}() for {}",
1011 trait_name, r_name, type_str
1012 ));
1013 }
1014 lines.extend(own_block_page_lines(
1015 method,
1016 &type_str,
1017 Some(&fn_name),
1018 None,
1019 ));
1020 crate::roxygen::push_roxygen_tags(&mut lines, ¶m_doc_tags(&ctx, &type_str));
1024 lines.push("#' @export".to_string());
1025
1026 let call = ctx.static_call();
1027
1028 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} <- function({1}) {{",
ctx.namespace_symbol(ClassSystem::S4), ctx.params))
})format!(
1029 "{} <- function({}) {{",
1030 ctx.namespace_symbol(ClassSystem::S4),
1031 ctx.params
1032 ));
1033 ctx.emit_method_prelude(&mut lines, " ", &r_name);
1034 lines.extend(ctx.method_body_lines(&call, ClassSystem::S4));
1035 lines.push("}".to_string());
1036 lines.push(String::new());
1037 }
1038
1039 for trait_const in consts {
1041 let const_name = &trait_const.ident;
1042 let const_str = const_name.to_string();
1043 let fn_name = trait_namespace_target(ClassSystem::S4, type_ident, trait_name, &const_str);
1044
1045 let roxygen = RoxygenBuilder::new()
1046 .name(&fn_name)
1047 .rdname(&type_str)
1048 .export()
1049 .build();
1050 lines.extend(roxygen);
1051
1052 let c_ident = trait_const.c_wrapper_ident_string(type_ident, trait_name);
1053 let call = DotCallBuilder::new(&c_ident).build();
1054
1055 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} <- function() {{", fn_name))
})format!("{} <- function() {{", fn_name));
1056 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" {0}", call))
})format!(" {}", call));
1057 lines.push("}".to_string());
1058 lines.push(String::new());
1059 }
1060
1061 lines.join("\n")
1062}
1063
1064fn generate_trait_s7_r_wrapper(
1075 type_ident: &syn::Ident,
1076 trait_name: &syn::Ident,
1077 methods: &[TraitMethod],
1078 consts: &[TraitConst],
1079 impl_preconditions: Option<bool>,
1080) -> String {
1081 use crate::r_wrapper_builder::{DotCallBuilder, RoxygenBuilder};
1082
1083 let mut lines = Vec::new();
1084 let type_str = type_ident.to_string();
1085 let s7_class_var = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(".s7_class_{0}", type_str))
})format!(".s7_class_{}", type_str);
1086
1087 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("# S7 trait methods for {0} implementing {1}",
type_ident, trait_name))
})format!(
1089 "# S7 trait methods for {} implementing {}",
1090 type_ident, trait_name
1091 ));
1092 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("# Generated by #[miniextendr(s7)] impl {0} for {1}",
trait_name, type_ident))
})format!(
1093 "# Generated by #[miniextendr(s7)] impl {} for {}",
1094 trait_name, type_ident
1095 ));
1096 lines.push(String::new());
1097
1098 lines.push("#' @importFrom S7 new_generic method S7_dispatch".to_string());
1103 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1} <- {0}",
class_ref(ClassSystem::S7, type_ident), s7_class_var))
})format!(
1104 "{s7_class_var} <- {}",
1105 class_ref(ClassSystem::S7, type_ident)
1106 ));
1107 lines.push(String::new());
1108
1109 let instance_methods: Vec<_> = methods.iter().filter(|m| m.has_self).collect();
1111 let static_methods: Vec<_> = methods.iter().filter(|m| !m.has_self).collect();
1112
1113 for method in &instance_methods {
1115 let generic_name = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("s7_trait_{0}_{1}", trait_name,
method.r_method_name()))
})format!("s7_trait_{}_{}", trait_name, method.r_method_name());
1116 let ctx = TraitMethodContext::new(method, type_ident, trait_name)
1117 .with_impl_preconditions(impl_preconditions);
1118
1119 let full_params = if ctx.params.is_empty() {
1121 "x, ...".to_string()
1122 } else {
1123 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("x, {0}, ...", ctx.params))
})format!("x, {}, ...", ctx.params)
1124 };
1125
1126 let generic_roxygen = RoxygenBuilder::new()
1132 .description(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("S7 generic for trait method `{0}::{1}`",
trait_name, method.r_method_name()))
})format!(
1133 "S7 generic for trait method `{}::{}`",
1134 trait_name,
1135 method.r_method_name()
1136 ))
1137 .name(&generic_name)
1138 .rdname(&type_str)
1139 .source(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("Generated by miniextendr from `impl {0} for {1}`",
trait_name, type_ident))
})format!(
1140 "Generated by miniextendr from `impl {} for {}`",
1141 trait_name, type_ident
1142 ))
1143 .export()
1144 .build();
1145 lines.extend(generic_roxygen);
1146
1147 let generic_symbol = crate::naming::r_def_name(&generic_name);
1151 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("if (!exists(\"{0}\", mode = \"function\")) {{",
generic_name))
})format!(
1152 "if (!exists(\"{generic_name}\", mode = \"function\")) {{"
1153 ));
1154 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" {0} <- S7::new_generic(\"{1}\", \"x\", function(x, ...) S7::S7_dispatch())",
generic_symbol, generic_name))
})format!(
1155 " {generic_symbol} <- S7::new_generic(\"{generic_name}\", \"x\", function(x, ...) S7::S7_dispatch())"
1156 ));
1157 lines.push("}".to_string());
1158 lines.push(String::new());
1159
1160 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("S7::method({0}, {1}) <- function({2}) {{",
generic_symbol, s7_class_var, full_params))
})format!(
1162 "S7::method({}, {}) <- function({}) {{",
1163 generic_symbol, s7_class_var, full_params
1164 ));
1165 lines.push(" .ptr <- x@.ptr".to_string());
1167 let s7_call = ctx.instance_call(".ptr");
1168 ctx.emit_method_prelude(&mut lines, " ", &method.r_method_name());
1169 lines.extend(ctx.method_body_lines(&s7_call, ClassSystem::S7));
1170 finish_instance_body(&mut lines, method, "x");
1171 lines.push("}".to_string());
1172 lines.push(String::new());
1173
1174 let shortcut_name = if method.no_shortcut {
1184 None
1185 } else {
1186 crate::miniextendr_impl::s7_class::s7_shortcut_name(&type_str, &method.r_method_name())
1187 };
1188 if let Some(shortcut_name) = shortcut_name {
1189 let shortcut_formals = if ctx.params.is_empty() {
1190 ".mx_self, ...".to_string()
1191 } else {
1192 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(".mx_self, {0}, ...", ctx.params))
})format!(".mx_self, {}, ...", ctx.params)
1193 };
1194 let shortcut_call = ctx.instance_call(".mx_self@.ptr");
1195
1196 let joined = joins_author_topic(method, &type_str);
1209 let advisory = crate::miniextendr_impl::s7_class::shortcut_advisory_lines(
1210 &method.r_method_name(),
1211 &type_str,
1212 );
1213 let advisory_len = if joined { 1 } else { advisory.len() };
1214 lines.extend(advisory.into_iter().take(advisory_len));
1215 if !joined {
1216 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("#\' @param .mx_self A `{0}` object.",
type_str))
})format!("#' @param .mx_self A `{}` object.", type_str));
1217 }
1218 push_param_tags(&mut lines, method);
1219 let fillers = !params_documented_elsewhere(method, &type_str);
1228 let choice_docs = ctx.choice_param_docs();
1229 for formal in crate::roxygen::split_r_formals(&shortcut_formals) {
1230 let pname = crate::roxygen::formal_name(formal);
1231 if joined || pname == ".mx_self" {
1232 continue;
1233 }
1234 let documented = crate::roxygen::param_documented(&method.doc_tags, pname);
1235 if documented {
1236 continue;
1237 }
1238 if pname == "..." {
1239 lines.push(
1240 "#' @param ... Additional arguments; ignored by the fast-path shortcut."
1241 .to_string(),
1242 );
1243 } else if fillers {
1244 let body = choice_docs
1245 .get(pname)
1246 .map_or("(undocumented)", String::as_str);
1247 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("#\' @param {0} {1}", pname, body))
})format!("#' @param {pname} {body}"));
1248 }
1249 }
1250 lines.extend(own_block_page_lines(
1251 method,
1252 &type_str,
1253 Some(&shortcut_name),
1254 None,
1255 ));
1256 lines.extend(crate::roxygen::source_tag(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("Generated by miniextendr from `impl {0} for {1}` (`{2}` shortcut)",
trait_name, type_ident, method.r_method_name()))
})format!(
1257 "Generated by miniextendr from `impl {} for {}` (`{}` shortcut)",
1258 trait_name,
1259 type_ident,
1260 method.r_method_name()
1261 )));
1262 lines.push("#' @export".to_string());
1263
1264 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} <- function({1}) {{",
shortcut_name, shortcut_formals))
})format!(
1265 "{} <- function({}) {{",
1266 shortcut_name, shortcut_formals
1267 ));
1268 ctx.emit_method_prelude(&mut lines, " ", &method.r_method_name());
1269 lines.extend(ctx.method_body_lines(&shortcut_call, ClassSystem::S7));
1270 finish_instance_body(&mut lines, method, ".mx_self");
1271 lines.push("}".to_string());
1272 lines.push(String::new());
1273 }
1274 }
1275
1276 let trait_env_var = trait_namespace_env_var(type_ident, trait_name);
1279 if !static_methods.is_empty() || !consts.is_empty() {
1280 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} <- new.env(parent = emptyenv())",
trait_env_var))
})format!("{} <- new.env(parent = emptyenv())", trait_env_var));
1281 lines.push(String::new());
1282 }
1283
1284 for method in &static_methods {
1289 let r_name = method.r_method_name();
1290 let ctx = TraitMethodContext::new(method, type_ident, trait_name)
1291 .with_impl_preconditions(impl_preconditions);
1292
1293 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("#\' Static trait method {0}::{1}()",
trait_name, r_name))
})format!(
1294 "#' Static trait method {}::{}()",
1295 trait_name, r_name
1296 ));
1297 let name = s7_trait_member_access(type_ident, trait_name, &r_name);
1298 lines.extend(namespace_member_body_lines(&ctx, &type_str, &name));
1299 lines.extend(own_block_page_lines(method, &type_str, Some(&name), None));
1300
1301 let call = ctx.static_call();
1302
1303 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} <- function({1}) {{",
ctx.namespace_symbol(ClassSystem::S7), ctx.params))
})format!(
1304 "{} <- function({}) {{",
1305 ctx.namespace_symbol(ClassSystem::S7),
1306 ctx.params
1307 ));
1308 ctx.emit_method_prelude(&mut lines, " ", &r_name);
1309 lines.extend(ctx.method_body_lines(&call, ClassSystem::S7));
1310 lines.push("}".to_string());
1311 lines.push(String::new());
1312 }
1313
1314 for trait_const in consts {
1317 let const_name = &trait_const.ident;
1318 let const_str = const_name.to_string();
1319
1320 let roxygen = RoxygenBuilder::new()
1321 .name(s7_trait_member_access(type_ident, trait_name, &const_str))
1322 .rdname(&type_str)
1323 .build();
1324 lines.extend(roxygen);
1325
1326 let c_ident = trait_const.c_wrapper_ident_string(type_ident, trait_name);
1327 let call = DotCallBuilder::new(&c_ident).build();
1328
1329 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} <- function() {{",
trait_namespace_target(ClassSystem::S7, type_ident,
trait_name, &const_str)))
})format!(
1330 "{} <- function() {{",
1331 trait_namespace_target(ClassSystem::S7, type_ident, trait_name, &const_str)
1332 ));
1333 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" {0}", call))
})format!(" {}", call));
1334 lines.push("}".to_string());
1335 lines.push(String::new());
1336 }
1337
1338 if !static_methods.is_empty() || !consts.is_empty() {
1342 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("attr({0}, \"{1}\") <- {2}",
class_ref(ClassSystem::S7, type_ident), trait_name,
trait_env_var))
})format!(
1343 "attr({}, \"{}\") <- {}",
1344 class_ref(ClassSystem::S7, type_ident),
1345 trait_name,
1346 trait_env_var
1347 ));
1348 lines.push(String::new());
1349 }
1350
1351 lines.join("\n")
1352}
1353
1354fn generate_trait_r6_r_wrapper(
1373 type_ident: &syn::Ident,
1374 trait_name: &syn::Ident,
1375 methods: &[TraitMethod],
1376 consts: &[TraitConst],
1377 impl_preconditions: Option<bool>,
1378) -> String {
1379 use crate::r_wrapper_builder::{DotCallBuilder, RoxygenBuilder};
1380
1381 let mut lines = Vec::new();
1382 let type_str = type_ident.to_string();
1383
1384 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("# R6 trait methods for {0} implementing {1}",
type_ident, trait_name))
})format!(
1386 "# R6 trait methods for {} implementing {}",
1387 type_ident, trait_name
1388 ));
1389 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("# Generated by #[miniextendr(r6)] impl {0} for {1}",
trait_name, type_ident))
})format!(
1390 "# Generated by #[miniextendr(r6)] impl {} for {}",
1391 trait_name, type_ident
1392 ));
1393 lines.push("# Note: R6 trait methods live in the Type$Trait$method namespace".to_string());
1394 lines.push(String::new());
1395
1396 let instance_methods: Vec<_> = methods.iter().filter(|m| m.has_self).collect();
1398 let static_methods: Vec<_> = methods.iter().filter(|m| !m.has_self).collect();
1399
1400 if binds_trait_namespace(ClassSystem::R6, methods, consts) {
1403 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} <- new.env(parent = emptyenv())",
trait_namespace_env(ClassSystem::R6, type_ident, trait_name)))
})format!(
1404 "{} <- new.env(parent = emptyenv())",
1405 trait_namespace_env(ClassSystem::R6, type_ident, trait_name)
1406 ));
1407 lines.push(String::new());
1408 }
1409
1410 for method in &instance_methods {
1412 let ctx = TraitMethodContext::new(method, type_ident, trait_name)
1413 .with_impl_preconditions(impl_preconditions);
1414 let target = ctx.namespace_target(ClassSystem::R6);
1415
1416 let full_params = if ctx.params.is_empty() {
1418 ".mx_self".to_string()
1419 } else {
1420 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(".mx_self, {0}", ctx.params))
})format!(".mx_self, {}", ctx.params)
1421 };
1422
1423 lines.extend(own_block_page_lines(
1435 method,
1436 &type_str,
1437 Some(&target),
1438 Some(&target),
1439 ));
1440
1441 let call = ctx.instance_call(".ptr");
1442
1443 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} <- function({1}) {{",
ctx.namespace_symbol(ClassSystem::R6), full_params))
})format!(
1444 "{} <- function({full_params}) {{",
1445 ctx.namespace_symbol(ClassSystem::R6)
1446 ));
1447 lines.push(" .ptr <- .mx_self$.__enclos_env__$private$.ptr".to_string());
1449 ctx.emit_method_prelude(&mut lines, " ", &method.r_method_name());
1450 lines.extend(ctx.method_body_lines(&call, ClassSystem::R6));
1451 finish_instance_body(&mut lines, method, ".mx_self");
1452 lines.push("}".to_string());
1453 lines.push(String::new());
1454 }
1455
1456 for method in &static_methods {
1458 let r_name = method.r_method_name();
1459 let ctx = TraitMethodContext::new(method, type_ident, trait_name)
1460 .with_impl_preconditions(impl_preconditions);
1461 let target = ctx.namespace_target(ClassSystem::R6);
1462
1463 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("#\' Static trait method {0}::{1}()",
trait_name, r_name))
})format!(
1464 "#' Static trait method {}::{}()",
1465 trait_name, r_name
1466 ));
1467 lines.extend(own_block_page_lines(method, &type_str, Some(&target), None));
1468
1469 let call = ctx.static_call();
1470
1471 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} <- function({1}) {{",
ctx.namespace_symbol(ClassSystem::R6), ctx.params))
})format!(
1472 "{} <- function({}) {{",
1473 ctx.namespace_symbol(ClassSystem::R6),
1474 ctx.params
1475 ));
1476 ctx.emit_method_prelude(&mut lines, " ", &r_name);
1477 lines.extend(ctx.method_body_lines(&call, ClassSystem::R6));
1478 lines.push("}".to_string());
1479 lines.push(String::new());
1480 }
1481
1482 for trait_const in consts {
1484 let const_name = &trait_const.ident;
1485 let const_str = const_name.to_string();
1486 let target = trait_namespace_target(ClassSystem::R6, type_ident, trait_name, &const_str);
1487
1488 let roxygen = RoxygenBuilder::new()
1489 .name(target.clone())
1490 .rdname(&type_str)
1491 .build();
1492 lines.extend(roxygen);
1493
1494 let c_ident = trait_const.c_wrapper_ident_string(type_ident, trait_name);
1495 let call = DotCallBuilder::new(&c_ident).build();
1496
1497 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} <- function() {{", target))
})format!("{target} <- function() {{"));
1498 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" {0}", call))
})format!(" {}", call));
1499 lines.push("}".to_string());
1500 lines.push(String::new());
1501 }
1502
1503 lines.join("\n")
1504}
1505
1506fn finish_instance_body(lines: &mut Vec<String>, method: &super::TraitMethod, receiver: &str) {
1513 if method.returns_unit() {
1514 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" {0}", receiver))
})format!(" {receiver}"));
1515 }
1516 if method.is_invisible()
1517 && let Some(last) = lines.last_mut()
1518 {
1519 let expr = last.trim_start();
1520 let indent = &last[..last.len() - expr.len()];
1521 *last = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}invisible({1})", indent, expr))
})format!("{indent}invisible({expr})");
1522 }
1523}