Skip to content

Commit

Permalink
Refactor rewrite_self_arg to allow generic output macro
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Aug 9, 2022
1 parent f287dd2 commit 2af52d2
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 125 deletions.
1 change: 1 addition & 0 deletions objc2/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod __rewrite_self_arg;
mod declare_class;
mod extern_class;

Expand Down
102 changes: 102 additions & 0 deletions objc2/src/macros/__rewrite_self_arg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#[doc(hidden)]
#[macro_export]
macro_rules! __rewrite_self_arg {
{
($out_macro:path)
($($args:tt)*)
$($macro_args:tt)*
} => {
$crate::__rewrite_self_arg! {
($out_macro)
// Duplicate args out so that we can match on `self`, while still
// use it as a function argument
@($($args)*)
@($($args)*)
$($macro_args)*
}
};

// Instance method
{
($out_macro:path)
@(&mut self $($__rest_args:tt)*)
@(&mut $self:ident $(, $($rest_args:tt)*)?)
$($macro_args:tt)*
} => {
$out_macro! {
$($macro_args)*
@(instance_method)
@(
$self: &mut Self,
_: $crate::runtime::Sel,
$($($rest_args)*)?
)
}
};
{
($out_macro:path)
@(&self $($__rest_args:tt)*)
@(&$self:ident $(, $($rest_args:tt)*)?)
$($macro_args:tt)*
} => {
$out_macro! {
$($macro_args)*
@(instance_method)
@(
$self: &Self,
_: $crate::runtime::Sel,
$($($rest_args)*)?
)
}
};
{
($out_macro:path)
@(mut self: $__self_ty:ty $(, $($__rest_args:tt)*)?)
@(mut $self:ident: $self_ty:ty $(, $($rest_args:tt)*)?)
$($macro_args:tt)*
} => {
$out_macro! {
$($macro_args)*
@(instance_method)
@(
mut $self: $self_ty,
_: $crate::runtime::Sel,
$($($rest_args)*)?
)
}
};
{
($out_macro:path)
@(self: $__self_ty:ty $(, $($__rest_args:tt)*)?)
@($self:ident: $self_ty:ty $(, $($rest_args:tt)*)?)
$($macro_args:tt)*
} => {
$out_macro! {
$($macro_args)*
@(instance_method)
@(
$self: $self_ty,
_: $crate::runtime::Sel,
$($($rest_args)*)?
)
}
};

// Class method
{
($out_macro:path)
@($($__args:tt)*)
@($($args:tt)*)
$($macro_args:tt)*
} => {
$out_macro! {
$($macro_args)*
@(class_method)
@(
_: &$crate::runtime::Class,
_: $crate::runtime::Sel,
$($args)*
)
}
};
}
141 changes: 16 additions & 125 deletions objc2/src/macros/declare_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,20 @@ macro_rules! __inner_declare_class {

$($rest:tt)*
} => {
$crate::__inner_declare_class! {
@rewrite_self_arg
$crate::__rewrite_self_arg! {
($crate::__inner_declare_class)
($($args)*)

// Duplicate args out so that we can match on `self`, while still
// use it as a function argument
@($($args)*)
// Split the function into parts, and send the arguments down to
// be used later on
@($($args)*)
@$($output)*
@($(#[$($m)*])*)
@(unsafe extern "C")
@($name)
@($($ret)?)
@($body)

#($($output)*)
// Will add @(kind)
// Will add @(args)
}

$crate::__inner_declare_class! {
Expand All @@ -47,18 +45,15 @@ macro_rules! __inner_declare_class {

$($rest:tt)*
} => {
$crate::__inner_declare_class! {
@rewrite_self_arg

@($($args)*)
@($($args)*)
$crate::__rewrite_self_arg! {
($crate::__inner_declare_class)
($($args)*)
@$($output)*
@($(#[$($m)*])*)
@(extern "C")
@($name)
@($($ret)?)
@($body)

#($($output)*)
}

$crate::__inner_declare_class! {
Expand All @@ -69,119 +64,15 @@ macro_rules! __inner_declare_class {
}
};

// Instance method
{
@rewrite_self_arg
@(&mut self $($__rest_args:tt)*)
@(&mut $self:ident $(, $($rest_args:tt)*)?)
$($rest:tt)*
} => {
$crate::__inner_declare_class! {
@dispatch
@(instance_method)
@(
$self: &mut Self,
_: $crate::runtime::Sel,
$($($rest_args)*)?
)
$($rest)*
}
};
{
@rewrite_self_arg
@(&self $($__rest_args:tt)*)
@(&$self:ident $(, $($rest_args:tt)*)?)
$($rest:tt)*
} => {
$crate::__inner_declare_class! {
@dispatch
@(instance_method)
@(
$self: &Self,
_: $crate::runtime::Sel,
$($($rest_args)*)?
)
$($rest)*
}
};
{
@rewrite_self_arg
@(mut self: $__self_ty:ty $(, $($__rest_args:tt)*)?)
@(mut $self:ident: $self_ty:ty $(, $($rest_args:tt)*)?)
$($rest:tt)*
} => {
$crate::__inner_declare_class! {
@dispatch
@(instance_method)
@(
mut $self: $self_ty,
_: $crate::runtime::Sel,
$($($rest_args)*)?
)
$($rest)*
}
};
{
@rewrite_self_arg
@(self: $__self_ty:ty $(, $($__rest_args:tt)*)?)
@($self:ident: $self_ty:ty $(, $($rest_args:tt)*)?)
$($rest:tt)*
} => {
$crate::__inner_declare_class! {
@dispatch
@(instance_method)
@(
$self: $self_ty,
_: $crate::runtime::Sel,
$($($rest_args)*)?
)
$($rest)*
}
};
// Class method
{
@rewrite_self_arg
@($($__args:tt)*)
@($($args:tt)*)
$($rest:tt)*
} => {
$crate::__inner_declare_class! {
@dispatch
@(class_method)
@(
_: &$crate::runtime::Class,
_: $crate::runtime::Sel,
$($args)*
)
$($rest)*
}
};

{
@dispatch
$(
@($($items:tt)*)
)*

#($($output:tt)*)
} => {
$crate::__inner_declare_class! {
@$($output)*
$(
@($($items)*)
)*
}
};

{
@method_out
@($($_kind:tt)*)
@($($args:tt)*)
@($(#[$($m:tt)*])*)
@($($qualifiers:tt)*)
@($name:ident)
@($($ret:ty)?)
@($($body:tt)*)
@($($_kind:tt)*)
@($($args:tt)*)
} => {
$crate::__attribute_helper! {
@strip_sel
Expand All @@ -192,13 +83,13 @@ macro_rules! __inner_declare_class {

{
@register_out($builder:ident)
@(class_method)
@($($args:tt)*)
@($(#[$($m:tt)*])*)
@($($qualifiers:tt)*)
@($name:ident)
@($($_ret:tt)*)
@($($_body:tt)*)
@(class_method)
@($($args:tt)*)
} => {
$builder.add_class_method(
$crate::__attribute_helper! {
Expand All @@ -214,13 +105,13 @@ macro_rules! __inner_declare_class {
};
{
@register_out($builder:ident)
@(instance_method)
@($($args:tt)*)
@($(#[$($m:tt)*])*)
@($($qualifiers:tt)*)
@($name:ident)
@($($_ret:tt)*)
@($($_body:tt)*)
@(instance_method)
@($($args:tt)*)
} => {
$builder.add_method(
$crate::__attribute_helper! {
Expand Down

0 comments on commit 2af52d2

Please sign in to comment.