|
1 | | -use syn::{spanned::Spanned, FnArg, Generics, ImplItem, ItemImpl, Pat, PatIdent, Signature, Type}; |
| 1 | +use syn::{ |
| 2 | + spanned::Spanned, visit::Visit, FnArg, Generics, ImplItem, ItemImpl, Meta, NestedMeta, Pat, |
| 3 | + PatIdent, Signature, Type, |
| 4 | +}; |
2 | 5 |
|
3 | 6 | use proc_macro2::TokenStream as TokenStream2; |
4 | 7 | use quote::{quote, ToTokens}; |
5 | 8 | use std::boxed::Box; |
6 | 9 |
|
| 10 | +use crate::utils::find_non_concrete; |
| 11 | + |
| 12 | +use self::mixin_args::{MixinArgsBuilder, MixinKind}; |
| 13 | + |
| 14 | +mod mixin_args; |
| 15 | + |
7 | 16 | #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] |
8 | 17 | pub enum RpcMode { |
9 | 18 | Disabled, |
@@ -322,15 +331,57 @@ pub(crate) struct ExportArgs { |
322 | 331 | pub(crate) is_async: bool, |
323 | 332 | } |
324 | 333 |
|
325 | | -pub(crate) fn derive_methods(item_impl: ItemImpl) -> TokenStream2 { |
| 334 | +pub(crate) fn derive_methods( |
| 335 | + args: Vec<NestedMeta>, |
| 336 | + item_impl: ItemImpl, |
| 337 | +) -> Result<TokenStream2, syn::Error> { |
326 | 338 | let derived = crate::automatically_derived(); |
| 339 | + let gdnative_core = crate::crate_gdnative_core(); |
327 | 340 | let (impl_block, export) = impl_gdnative_expose(item_impl); |
328 | 341 | let (impl_generics, _, where_clause) = impl_block.generics.split_for_impl(); |
329 | 342 |
|
330 | 343 | let class_name = export.class_ty; |
331 | 344 |
|
332 | 345 | let builder = syn::Ident::new("builder", proc_macro2::Span::call_site()); |
333 | 346 |
|
| 347 | + let args = { |
| 348 | + let mut attr_args_builder = MixinArgsBuilder::new(); |
| 349 | + |
| 350 | + for arg in args { |
| 351 | + if let NestedMeta::Meta(Meta::NameValue(ref pair)) = arg { |
| 352 | + attr_args_builder.add_pair(pair)?; |
| 353 | + } else if let NestedMeta::Meta(Meta::Path(ref path)) = arg { |
| 354 | + attr_args_builder.add_path(path)?; |
| 355 | + } else { |
| 356 | + let msg = format!("Unexpected argument: {arg:?}"); |
| 357 | + return Err(syn::Error::new(arg.span(), msg)); |
| 358 | + } |
| 359 | + } |
| 360 | + |
| 361 | + attr_args_builder.done()? |
| 362 | + }; |
| 363 | + |
| 364 | + let non_concrete = find_non_concrete::with_visitor(&impl_block.generics, |v| { |
| 365 | + v.visit_type(&impl_block.self_ty) |
| 366 | + }); |
| 367 | + |
| 368 | + let non_concrete = if non_concrete.is_empty() { |
| 369 | + None |
| 370 | + } else if non_concrete.len() == 1 { |
| 371 | + Some(non_concrete[0]) |
| 372 | + } else { |
| 373 | + Some(impl_block.self_ty.span()) |
| 374 | + }; |
| 375 | + |
| 376 | + if let Some(span) = non_concrete { |
| 377 | + if matches!(args.mixin, Some(MixinKind::Auto(_))) { |
| 378 | + return Err(syn::Error::new( |
| 379 | + span, |
| 380 | + "non-concrete mixins must be named and manually registered", |
| 381 | + )); |
| 382 | + } |
| 383 | + } |
| 384 | + |
334 | 385 | let methods = export |
335 | 386 | .methods |
336 | 387 | .into_iter() |
@@ -402,19 +453,66 @@ pub(crate) fn derive_methods(item_impl: ItemImpl) -> TokenStream2 { |
402 | 453 | }) |
403 | 454 | .collect::<Vec<_>>(); |
404 | 455 |
|
405 | | - quote::quote!( |
406 | | - #impl_block |
| 456 | + match args.mixin { |
| 457 | + Some(mixin_kind) => { |
| 458 | + let vis = args.pub_.then(|| quote!(pub)); |
| 459 | + |
| 460 | + let mixin_name = match &mixin_kind { |
| 461 | + MixinKind::Named(ident) => ident.clone(), |
| 462 | + MixinKind::Auto(span) => { |
| 463 | + return Err(syn::Error::new( |
| 464 | + *span, |
| 465 | + "mixins must be named in gdnative v0.11.x", |
| 466 | + )) |
| 467 | + } |
| 468 | + }; |
407 | 469 |
|
408 | | - #derived |
409 | | - impl #impl_generics gdnative::export::NativeClassMethods for #class_name #where_clause { |
410 | | - fn nativeclass_register(#builder: &::gdnative::export::ClassBuilder<Self>) { |
411 | | - use gdnative::export::*; |
| 470 | + let body = quote! { |
| 471 | + #derived |
| 472 | + #vis struct #mixin_name { |
| 473 | + _opaque: #gdnative_core::private::mixin::Opaque, |
| 474 | + } |
412 | 475 |
|
413 | | - #(#methods)* |
414 | | - } |
| 476 | + #derived |
| 477 | + impl #gdnative_core::private::mixin::Sealed for #mixin_name {} |
| 478 | + #derived |
| 479 | + impl #impl_generics #gdnative_core::export::Mixin<#class_name> for #mixin_name #where_clause { |
| 480 | + fn register(#builder: &#gdnative_core::export::ClassBuilder<#class_name>) { |
| 481 | + use #gdnative_core::export::*; |
| 482 | + |
| 483 | + #(#methods)* |
| 484 | + } |
| 485 | + } |
| 486 | + }; |
| 487 | + |
| 488 | + let body = match &mixin_kind { |
| 489 | + MixinKind::Named(_) => body, |
| 490 | + MixinKind::Auto(_) => quote! { |
| 491 | + const _: () = { |
| 492 | + #body |
| 493 | + } |
| 494 | + }, |
| 495 | + }; |
| 496 | + |
| 497 | + Ok(quote::quote!( |
| 498 | + #impl_block |
| 499 | + #body |
| 500 | + )) |
415 | 501 | } |
| 502 | + None => Ok(quote::quote!( |
| 503 | + #impl_block |
416 | 504 |
|
417 | | - ) |
| 505 | + #derived |
| 506 | + impl #impl_generics #gdnative_core::export::NativeClassMethods for #class_name #where_clause { |
| 507 | + fn nativeclass_register(#builder: &#gdnative_core::export::ClassBuilder<Self>) { |
| 508 | + use #gdnative_core::export::*; |
| 509 | + |
| 510 | + #(#methods)* |
| 511 | + } |
| 512 | + } |
| 513 | + |
| 514 | + )), |
| 515 | + } |
418 | 516 | } |
419 | 517 |
|
420 | 518 | /// Extract the data to export from the impl block. |
@@ -464,7 +562,7 @@ fn impl_gdnative_expose(ast: ItemImpl) -> (ItemImpl, ClassMethodExport) { |
464 | 562 | }; |
465 | 563 |
|
466 | 564 | if is_export { |
467 | | - use syn::{punctuated::Punctuated, Lit, Meta, NestedMeta}; |
| 565 | + use syn::{punctuated::Punctuated, Lit}; |
468 | 566 | let mut export_args = |
469 | 567 | export_args.get_or_insert_with(ExportArgs::default); |
470 | 568 | export_args.is_old_syntax = is_old_syntax; |
|
0 commit comments