Skip to content

Commit

Permalink
Merge pull request #512 from madsmtm/rewrite-macro-internals
Browse files Browse the repository at this point in the history
Refactor macro internals
  • Loading branch information
madsmtm authored Sep 12, 2023
2 parents 812a615 + 44d2c20 commit 5e705a7
Show file tree
Hide file tree
Showing 52 changed files with 1,519 additions and 1,450 deletions.
10 changes: 8 additions & 2 deletions crates/icrate/src/additions/Foundation/macros/ns_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,17 @@ macro_rules! __ns_string {
macro_rules! __ns_string_inner {
($inp:ident) => {{
const X: &[u8] = $inp.as_bytes();
$crate::__ns_string_inner!(@inner X);
$crate::__ns_string_static!(X);
// Return &'static NSString
CFSTRING.as_nsstring()
}};
(@inner $inp:ident) => {
}

#[doc(hidden)]
#[cfg(all(feature = "apple", feature = "unstable-static-nsstring"))]
#[macro_export]
macro_rules! __ns_string_static {
($inp:ident) => {
// Note: We create both the ASCII + NUL and the UTF-16 + NUL versions
// of the string, since we can't conditionally create a static.
//
Expand Down
68 changes: 32 additions & 36 deletions crates/icrate/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,13 @@ macro_rules! extern_enum {
),* $(,)?
}
) => {
extern_enum! {
@__inner
@($v)
@($ty)
@($(
extern_enum_inner! {
($v)
($ty)
$(
$(#[$field_m])*
$field = $value,
)*)
)*
}
};
(
Expand All @@ -94,45 +93,42 @@ macro_rules! extern_enum {
$(#[$m])*
$v type $name = $ty;

extern_enum! {
@__inner
@($v)
@($name)
@($(
extern_enum_inner! {
($v)
($name)
$(
$(#[$field_m])*
$field = $value,
)*)
)*
}
};
}

// tt-munch each field
// tt-munch each enum field
macro_rules! extern_enum_inner {
// Base case
(
@__inner
@($v:vis)
@($ty:ty)
@()
) => {
// Base case
};
($v:vis)
($ty:ty)
) => {};

// Parse each field
(
@__inner
@($v:vis)
@($ty:ty)
@(
$(#[$field_m:meta])*
$field:ident = $value:expr,
($v:vis)
($ty:ty)

$($rest:tt)*
)
$(#[$field_m:meta])*
$field:ident = $value:expr,

$($rest:tt)*
) => {
$(#[$field_m])*
$v const $field: $ty = $value;

extern_enum! {
@__inner
@($v)
@($ty)
@($($rest)*)
extern_enum_inner! {
($v)
($ty)
$($rest)*
}
};
}
Expand Down Expand Up @@ -309,11 +305,11 @@ macro_rules! extern_static {
macro_rules! extern_fn {
(
$(#[$m:meta])*
$v:vis unsafe fn $name:ident($($args:tt)*) $(-> $res:ty)?;
$v:vis unsafe fn $name:ident($($params:tt)*) $(-> $res:ty)?;
) => {
$(#[$m])*
extern "C" {
$v fn $name($($args)*) $(-> $res)?;
$v fn $name($($params)*) $(-> $res)?;
}
};
(
Expand All @@ -336,7 +332,7 @@ macro_rules! extern_fn {
macro_rules! inline_fn {
(
$(#[$m:meta])*
$v:vis unsafe fn $name:ident($($args:tt)*) $(-> $res:ty)? $body:block
$v:vis unsafe fn $name:ident($($params:tt)*) $(-> $res:ty)? $body:block
) => {
// TODO
};
Expand Down
92 changes: 52 additions & 40 deletions crates/objc2/src/declare/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ pub trait MethodImplementation: private::Sealed + Sized {
fn __imp(self) -> Imp;
}

macro_rules! method_decl_impl {
(@<$($l:lifetime),*> T: $t_bound:ident $(+ $t_bound2:ident)?, $r:ident, $f:ty, $($t:ident),*) => {
macro_rules! method_impl_generic {
(<$($l:lifetime),*> T: $t_bound:ident $(+ $t_bound2:ident)?, $r:ident, $f:ty, $($t:ident),*) => {
impl<$($l,)* T, $r, $($t),*> private::Sealed for $f
where
T: ?Sized + $t_bound $(+ $t_bound2)?,
Expand All @@ -190,7 +190,10 @@ macro_rules! method_decl_impl {
}
}
};
(@<$($l:lifetime),*> $callee:ident, $r:ident, $f:ty, $($t:ident),*) => {
}

macro_rules! method_impl_concrete {
(<$($l:lifetime),*> $callee:ident, $r:ident, $f:ty, $($t:ident),*) => {
impl<$($l,)* $r, $($t),*> private::Sealed for $f
where
$r: EncodeReturn,
Expand All @@ -211,7 +214,10 @@ macro_rules! method_decl_impl {
}
}
};
(@<> Allocated<T>, $f:ty, $($t:ident),*) => {
}

macro_rules! method_impl_allocated {
(<> Allocated<T>, $f:ty, $($t:ident),*) => {
#[doc(hidden)]
impl<T, $($t),*> private::Sealed for $f
where
Expand Down Expand Up @@ -242,48 +248,54 @@ macro_rules! method_decl_impl {
}
}
};
(# $abi:literal; $($t:ident),*) => {
method_decl_impl!(@<'a> T: Message, R, extern $abi fn(&'a T, Sel $(, $t)*) -> R, $($t),*);
method_decl_impl!(@<'a> T: Message + IsMutable, R, extern $abi fn(&'a mut T, Sel $(, $t)*) -> R, $($t),*);
method_decl_impl!(@<> T: Message, R, unsafe extern $abi fn(*const T, Sel $(, $t)*) -> R, $($t),*);
method_decl_impl!(@<> T: Message, R, unsafe extern $abi fn(*mut T, Sel $(, $t)*) -> R, $($t),*);
method_decl_impl!(@<'a> T: Message, R, unsafe extern $abi fn(&'a T, Sel $(, $t)*) -> R, $($t),*);
method_decl_impl!(@<'a> T: Message + IsMutable, R, unsafe extern $abi fn(&'a mut T, Sel $(, $t)*) -> R, $($t),*);

method_decl_impl!(@<'a> AnyObject, R, extern $abi fn(&'a mut AnyObject, Sel $(, $t)*) -> R, $($t),*);
method_decl_impl!(@<'a> AnyObject, R, unsafe extern $abi fn(&'a mut AnyObject, Sel $(, $t)*) -> R, $($t),*);

method_decl_impl!(@<'a> AnyClass, R, extern $abi fn(&'a AnyClass, Sel $(, $t)*) -> R, $($t),*);
method_decl_impl!(@<> AnyClass, R, unsafe extern $abi fn(*const AnyClass, Sel $(, $t)*) -> R, $($t),*);
method_decl_impl!(@<'a> AnyClass, R, unsafe extern $abi fn(&'a AnyClass, Sel $(, $t)*) -> R, $($t),*);

method_decl_impl!(@<> Allocated<T>, extern $abi fn(Allocated<T>, Sel $(, $t)*) -> __IdReturnValue, $($t),*);
method_decl_impl!(@<> Allocated<T>, unsafe extern $abi fn(Allocated<T>, Sel $(, $t)*) -> __IdReturnValue, $($t),*);
}

macro_rules! method_impl_abi {
($abi:literal; $($t:ident),*) => {
method_impl_generic!(<'a> T: Message, R, extern $abi fn(&'a T, Sel $(, $t)*) -> R, $($t),*);
method_impl_generic!(<'a> T: Message + IsMutable, R, extern $abi fn(&'a mut T, Sel $(, $t)*) -> R, $($t),*);
method_impl_generic!(<> T: Message, R, unsafe extern $abi fn(*const T, Sel $(, $t)*) -> R, $($t),*);
method_impl_generic!(<> T: Message, R, unsafe extern $abi fn(*mut T, Sel $(, $t)*) -> R, $($t),*);
method_impl_generic!(<'a> T: Message, R, unsafe extern $abi fn(&'a T, Sel $(, $t)*) -> R, $($t),*);
method_impl_generic!(<'a> T: Message + IsMutable, R, unsafe extern $abi fn(&'a mut T, Sel $(, $t)*) -> R, $($t),*);

method_impl_concrete!(<'a> AnyObject, R, extern $abi fn(&'a mut AnyObject, Sel $(, $t)*) -> R, $($t),*);
method_impl_concrete!(<'a> AnyObject, R, unsafe extern $abi fn(&'a mut AnyObject, Sel $(, $t)*) -> R, $($t),*);

method_impl_concrete!(<'a> AnyClass, R, extern $abi fn(&'a AnyClass, Sel $(, $t)*) -> R, $($t),*);
method_impl_concrete!(<> AnyClass, R, unsafe extern $abi fn(*const AnyClass, Sel $(, $t)*) -> R, $($t),*);
method_impl_concrete!(<'a> AnyClass, R, unsafe extern $abi fn(&'a AnyClass, Sel $(, $t)*) -> R, $($t),*);

method_impl_allocated!(<> Allocated<T>, extern $abi fn(Allocated<T>, Sel $(, $t)*) -> __IdReturnValue, $($t),*);
method_impl_allocated!(<> Allocated<T>, unsafe extern $abi fn(Allocated<T>, Sel $(, $t)*) -> __IdReturnValue, $($t),*);
};
}

macro_rules! method_impl {
($($t:ident),*) => {
method_decl_impl!(# "C"; $($t),*);
method_impl_abi!("C"; $($t),*);
#[cfg(feature = "unstable-c-unwind")]
method_decl_impl!(# "C-unwind"; $($t),*);
method_impl_abi!("C-unwind"; $($t),*);
};
}

method_decl_impl!();
method_decl_impl!(A);
method_decl_impl!(A, B);
method_decl_impl!(A, B, C);
method_decl_impl!(A, B, C, D);
method_decl_impl!(A, B, C, D, E);
method_decl_impl!(A, B, C, D, E, F);
method_decl_impl!(A, B, C, D, E, F, G);
method_decl_impl!(A, B, C, D, E, F, G, H);
method_decl_impl!(A, B, C, D, E, F, G, H, I);
method_decl_impl!(A, B, C, D, E, F, G, H, I, J);
method_decl_impl!(A, B, C, D, E, F, G, H, I, J, K);
method_decl_impl!(A, B, C, D, E, F, G, H, I, J, K, L);
method_decl_impl!(A, B, C, D, E, F, G, H, I, J, K, L, M);
method_decl_impl!(A, B, C, D, E, F, G, H, I, J, K, L, M, N);
method_decl_impl!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
method_decl_impl!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);
method_impl!();
method_impl!(A);
method_impl!(A, B);
method_impl!(A, B, C);
method_impl!(A, B, C, D);
method_impl!(A, B, C, D, E);
method_impl!(A, B, C, D, E, F);
method_impl!(A, B, C, D, E, F, G);
method_impl!(A, B, C, D, E, F, G, H);
method_impl!(A, B, C, D, E, F, G, H, I);
method_impl!(A, B, C, D, E, F, G, H, I, J);
method_impl!(A, B, C, D, E, F, G, H, I, J, K);
method_impl!(A, B, C, D, E, F, G, H, I, J, K, L);
method_impl!(A, B, C, D, E, F, G, H, I, J, K, L, M);
method_impl!(A, B, C, D, E, F, G, H, I, J, K, L, M, N);
method_impl!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
method_impl!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);

/// Helper type for implementing `MethodImplementation` with a receiver of
/// `Allocated<T>`, without exposing that implementation to users.
Expand Down
Loading

0 comments on commit 5e705a7

Please sign in to comment.