Skip to content

Commit

Permalink
Avoid @ in macros in favor of explicit internal macros
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Sep 12, 2023
1 parent 15646bf commit d2737da
Show file tree
Hide file tree
Showing 13 changed files with 393 additions and 362 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
62 changes: 29 additions & 33 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
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
20 changes: 10 additions & 10 deletions crates/objc2/src/macros/__attribute_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,40 @@
macro_rules! __extract_and_apply_cfg_attributes {
// Base case
{
@() // No attributes left to process
@($($output:tt)*)
() // No attributes left to process
$($output:tt)*
} => {
$($output)*
};
// `cfg` attribute
{
@(
(
#[cfg $($args:tt)*]
$($m_rest:tt)*
)
@($($output:tt)*)
$($output:tt)*
} => {
// Apply the attribute and continue
#[cfg $($args)*]
{
$crate::__extract_and_apply_cfg_attributes! {
@($($m_rest)*)
@($($output)*)
($($m_rest)*)
$($output)*
}
}
};
// Other attributes
{
@(
(
#[$($m_ignored:tt)*]
$($m_rest:tt)*
)
@($($output:tt)*)
$($output:tt)*
} => {
// Ignore the attribute, and continue parsing the rest
$crate::__extract_and_apply_cfg_attributes! {
@($($m_rest)*)
@($($output)*)
($($m_rest)*)
$($output)*
}
};
}
Expand Down
54 changes: 27 additions & 27 deletions crates/objc2/src/macros/__method_msg_send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ macro_rules! __method_msg_send {
()
) => {
$crate::__msg_send_helper! {
@(send_message)
@($receiver)
@($sel)
@()
($receiver)
(send_message)
($sel)
()
}
};

Expand Down Expand Up @@ -95,10 +95,10 @@ macro_rules! __method_msg_send {
($($arg_parsed:tt)*)
) => {
$crate::__msg_send_helper! {
@(send_message)
@($receiver)
@($($sel_parsed)*)
@($($arg_parsed)*)
($receiver)
(send_message)
($($sel_parsed)*)
($($arg_parsed)*)
}
};

Expand All @@ -113,11 +113,11 @@ macro_rules! __method_msg_send {
($($arg_parsed:tt)*)
) => {
$crate::__msg_send_helper! {
($receiver)
// Use error method
@(__send_message_error)
@($receiver)
@($($sel_parsed)* $sel :)
@($($arg_parsed)*)
(__send_message_error)
($($sel_parsed)* $sel :)
($($arg_parsed)*)
}
};

Expand Down Expand Up @@ -165,11 +165,11 @@ macro_rules! __method_msg_send_id {
($($retain_semantics:ident)?)
) => {
$crate::__msg_send_id_helper! {
@(send_message_id)
@($receiver)
@($($retain_semantics)?)
@($sel)
@()
($receiver)
($($retain_semantics)?)
(send_message_id)
($sel)
()
}
};

Expand Down Expand Up @@ -253,11 +253,11 @@ macro_rules! __method_msg_send_id {
($($retain_semantics:ident)?)
) => {
$crate::__msg_send_id_helper! {
@(send_message_id)
@($receiver)
@($($retain_semantics)?)
@($($sel_parsed)*)
@($($arg_parsed)*)
($receiver)
($($retain_semantics)?)
(send_message_id)
($($sel_parsed)*)
($($arg_parsed)*)
}
};

Expand All @@ -273,12 +273,12 @@ macro_rules! __method_msg_send_id {
($($retain_semantics:ident)?)
) => {
$crate::__msg_send_id_helper! {
($receiver)
($($retain_semantics)?)
// Use error method
@(send_message_id_error)
@($receiver)
@($($retain_semantics)?)
@($($sel_parsed)* $sel :)
@($($arg_parsed)*)
(send_message_id_error)
($($sel_parsed)* $sel :)
($($arg_parsed)*)
}
};

Expand Down
Loading

0 comments on commit d2737da

Please sign in to comment.