Skip to content

Commit

Permalink
Proper declare_class!
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Aug 13, 2022
1 parent ad3e42e commit 8c4affb
Showing 1 changed file with 113 additions and 7 deletions.
120 changes: 113 additions & 7 deletions objc2/src/macros/declare_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,42 +67,75 @@ macro_rules! __inner_declare_class {
}
};

// No return type
{
@method_out
@($(#[$($m:tt)*])*)
@($($qualifiers:tt)*)
@($name:ident)
@()
@($($ret:ty)?)
@($($body:tt)*)
@($($_kind:tt)*)
@($($args_start:tt)*)
@($($args_rest:tt)*)
} => {
$crate::__fn_args! {
($crate::__inner_declare_class)
($($args_rest)*,)
()
()
@method_out_inner
@($(#[$($m)*])*)
@($($qualifiers)*)
@($name)
@($($ret)?)
@($($body)*)
@($($_kind)*)
@($($args_start)*)
// Will add @(args_converted)
// Will add @(body_prefix)
}
};

// No return type
{
@method_out_inner
@($(#[$($m:tt)*])*)
@($($qualifiers:tt)*)
@($name:ident)
@()
@($($body:tt)*)
@($($_kind:tt)*)
@($($args_start:tt)*)
@($($args_converted:tt)*)
@($($body_prefix:tt)*)
} => {
$crate::__attribute_helper! {
@strip_sel
$(@[$($m)*])*
($($qualifiers)* fn $name($($args_start)* $($args_rest)*) {
($($qualifiers)* fn $name($($args_start)* $($args_converted)*) {
$($body_prefix)*
$($body)*
})
}
};
// With return type
{
@method_out
@method_out_inner
@($(#[$($m:tt)*])*)
@($($qualifiers:tt)*)
@($name:ident)
@($ret:ty)
@($($body:tt)*)
@($($_kind:tt)*)
@($($args_start:tt)*)
@($($args_rest:tt)*)
@($($args_converted:tt)*)
@($($body_prefix:tt)*)
} => {
$crate::__attribute_helper! {
@strip_sel
$(@[$($m)*])*
($($qualifiers)* fn $name($($args_start)* $($args_rest)*) -> <$ret as $crate::encode::EncodeConvert>::Encode {
($($qualifiers)* fn $name($($args_start)* $($args_converted)*) -> <$ret as $crate::encode::EncodeConvert>::Encode {
$($body_prefix)*
<$ret as $crate::encode::EncodeConvert>::into_encode($($body)*)
})
}
Expand Down Expand Up @@ -169,13 +202,86 @@ macro_rules! __inner_declare_class {
macro_rules! __fn_ptr {
(
@($($qualifiers:tt)*)
$($($param:ident)? $(_)?: $param_ty:ty),* $(,)?
$($(mut)? $($param:ident)? $(_)?: $param_ty:ty),* $(,)?
) => {
$($qualifiers)* fn($($crate::__fn_ptr!(@__to_anonymous $param_ty)),*) -> _
};
(@__to_anonymous $param_ty:ty) => { _ }
}

#[doc(hidden)]
#[macro_export]
macro_rules! __fn_args {
// Ignore `_`
{
($out_macro:path)
(_: $param_ty:ty, $($rest:tt)*)
($($args_converted:tt)*)
($($body_prefix:tt)*)
$($macro_args:tt)*
} => {
$crate::__fn_args! {
($out_macro)
($($rest)*)
($($args_converted)* _: $param_ty,)
($($body_prefix)*)
$($macro_args)*
}
};
// Convert mut
{
($out_macro:path)
(mut $param:ident: $param_ty:ty, $($rest:tt)*)
($($args_converted:tt)*)
($($body_prefix:tt)*)
$($macro_args:tt)*
} => {
$crate::__fn_args! {
($out_macro)
($($rest)*)
($($args_converted)* $param: <$param_ty as $crate::encode::EncodeConvert>::Encode,)
(
$($body_prefix)*
let mut $param = <$param_ty as $crate::encode::EncodeConvert>::from_encode($param);
)
$($macro_args)*
}
};
// Convert
{
($out_macro:path)
($param:ident: $param_ty:ty, $($rest:tt)*)
($($args_converted:tt)*)
($($body_prefix:tt)*)
$($macro_args:tt)*
} => {
$crate::__fn_args! {
($out_macro)
($($rest)*)
($($args_converted)* $param: <$param_ty as $crate::encode::EncodeConvert>::Encode,)
(
$($body_prefix)*
let $param = <$param_ty as $crate::encode::EncodeConvert>::from_encode($param);
)
$($macro_args)*
}
};
// Output result
{
($out_macro:path)
($(,)*)
($($args_converted:tt)*)
($($body_prefix:tt)*)
$($macro_args:tt)*
} => {
$out_macro! {
$($macro_args)*
@($($args_converted)*)
@($($body_prefix)*)
}
};
}

/// Declare a new Objective-C class.
///
/// This is mostly just a convenience macro on top of [`extern_class!`] and
Expand Down

0 comments on commit 8c4affb

Please sign in to comment.