#[macro_export]
macro_rules! dotnet_class {
(
$module:expr =>
static class $ns:literal . $tyname:ident {
$($methods:tt)+
}
) => {{
$crate::dotnet_class!(@bind $($methods)+);
let __module = $module;
(move || -> $crate::error::Result<
$crate::dotnet::entries::Entry<$crate::dotnet::entries::TypeDef>,
> {
let __class = __module.add_static_class($ns, stringify!($tyname))?;
$crate::dotnet_class!(@register __module, __class, $($methods)+);
Ok(__class)
})()
}};
(@bind) => {};
(@bind
$( #[$attr:ident] )?
fn $mname:ident ( $($p:ident : $pk:tt),* $(,)? ) -> $ret:tt
= chain { $($cc:tt)* } ; $($rest:tt)*
) => {
#[allow(non_snake_case)]
let $mname = $crate::callchain!(@from_class params( $($p : $pk),* ), $($cc)*);
$crate::dotnet_class!(@bind $($rest)*);
};
(@bind
$( #[$attr:ident] )?
fn $mname:ident ( $($p:ident : $pk:tt),* $(,)? ) -> $ret:tt
= managed { $($mb:tt)* } ; $($rest:tt)*
) => {
#[allow(non_snake_case)]
let $mname = $crate::managed_body!(@from_class params( $($p : $pk),* ), $($mb)*);
$crate::dotnet_class!(@bind $($rest)*);
};
(@bind
$( #[$attr:ident] )?
fn $mname:ident ( $($p:ident : $pk:tt),* $(,)? ) -> $ret:tt
= $body:expr ; $($rest:tt)*
) => {
#[allow(non_snake_case)]
let $mname = $body;
$crate::dotnet_class!(@bind $($rest)*);
};
(@register $module:ident, $class:ident,) => {};
(@register $module:ident, $class:ident,
$( #[$attr:ident] )?
fn $mname:ident ( $($p:ident : $pk:tt),* $(,)? ) -> $ret:tt
= chain { $($cc:tt)* } ; $($rest:tt)*
) => {
$crate::dotnet_class!(@add $module, $class,
$( #[$attr] )? fn $mname ( $($p : $pk),* ) -> $ret);
$crate::dotnet_class!(@register $module, $class, $($rest)*);
};
(@register $module:ident, $class:ident,
$( #[$attr:ident] )?
fn $mname:ident ( $($p:ident : $pk:tt),* $(,)? ) -> $ret:tt
= managed { $($mb:tt)* } ; $($rest:tt)*
) => {
$crate::dotnet_class!(@add $module, $class,
$( #[$attr] )? fn $mname ( $($p : $pk),* ) -> $ret);
$crate::dotnet_class!(@register $module, $class, $($rest)*);
};
(@register $module:ident, $class:ident,
$( #[$attr:ident] )?
fn $mname:ident ( $($p:ident : $pk:tt),* $(,)? ) -> $ret:tt
= $body:expr ; $($rest:tt)*
) => {
$crate::dotnet_class!(@add $module, $class,
$( #[$attr] )? fn $mname ( $($p : $pk),* ) -> $ret);
$crate::dotnet_class!(@register $module, $class, $($rest)*);
};
(@add $module:ident, $class:ident,
$( #[$attr:ident] )?
fn $mname:ident ( $($p:ident : $pk:tt),* ) -> $ret:tt
) => {
#[allow(unused_mut)]
let mut __method = $crate::dotnet::entries::Method::public_static(
stringify!($mname),
$crate::dotnet::signature::MethodSig::new_static(
$crate::dotnet_class!(@ty $ret),
::std::vec![ $( $crate::dotnet_class!(@ty $pk) ),* ],
),
)
.with_body($mname);
$( __method = __method.with_attribute($crate::dotnet_class!(@attr $module, $attr)); )?
$class.add_method(__method);
};
(@attr $module:ident, unmanaged_callers_only) => {
$module.corlib_attribute(
$crate::dotnet::entries::well_known::SystemRuntimeInteropServicesType::UnmanagedCallersOnlyAttribute,
)?
};
(@ty i64) => { $crate::dotnet::signature::TypeSig::INT64 };
(@ty i32) => { $crate::dotnet::signature::TypeSig::INT32 };
(@ty nint) => { $crate::dotnet::signature::TypeSig::NATIVE_INT };
(@ty f64) => { $crate::dotnet::signature::TypeSig::FLOAT64 };
(@ty bool) => { $crate::dotnet::signature::TypeSig::BOOL };
(@ty object) => { $crate::dotnet::signature::TypeSig::OBJECT };
(@ty string) => { $crate::dotnet::signature::TypeSig::STRING };
(@ty [ $elem:tt ]) => {
$crate::dotnet::signature::TypeSig::sz_array($crate::dotnet_class!(@ty $elem))
};
(@ty ()) => { $crate::dotnet::signature::TypeSig::VOID };
}