Skip to content

Commit 5e705a7

Browse files
authored
Merge pull request #512 from madsmtm/rewrite-macro-internals
Refactor macro internals
2 parents 812a615 + 44d2c20 commit 5e705a7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1519
-1450
lines changed

crates/icrate/src/additions/Foundation/macros/ns_string.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,17 @@ macro_rules! __ns_string {
8686
macro_rules! __ns_string_inner {
8787
($inp:ident) => {{
8888
const X: &[u8] = $inp.as_bytes();
89-
$crate::__ns_string_inner!(@inner X);
89+
$crate::__ns_string_static!(X);
9090
// Return &'static NSString
9191
CFSTRING.as_nsstring()
9292
}};
93-
(@inner $inp:ident) => {
93+
}
94+
95+
#[doc(hidden)]
96+
#[cfg(all(feature = "apple", feature = "unstable-static-nsstring"))]
97+
#[macro_export]
98+
macro_rules! __ns_string_static {
99+
($inp:ident) => {
94100
// Note: We create both the ASCII + NUL and the UTF-16 + NUL versions
95101
// of the string, since we can't conditionally create a static.
96102
//

crates/icrate/src/macros.rs

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,13 @@ macro_rules! extern_enum {
7070
),* $(,)?
7171
}
7272
) => {
73-
extern_enum! {
74-
@__inner
75-
@($v)
76-
@($ty)
77-
@($(
73+
extern_enum_inner! {
74+
($v)
75+
($ty)
76+
$(
7877
$(#[$field_m])*
7978
$field = $value,
80-
)*)
79+
)*
8180
}
8281
};
8382
(
@@ -94,45 +93,42 @@ macro_rules! extern_enum {
9493
$(#[$m])*
9594
$v type $name = $ty;
9695

97-
extern_enum! {
98-
@__inner
99-
@($v)
100-
@($name)
101-
@($(
96+
extern_enum_inner! {
97+
($v)
98+
($name)
99+
$(
102100
$(#[$field_m])*
103101
$field = $value,
104-
)*)
102+
)*
105103
}
106104
};
105+
}
107106

108-
// tt-munch each field
107+
// tt-munch each enum field
108+
macro_rules! extern_enum_inner {
109+
// Base case
109110
(
110-
@__inner
111-
@($v:vis)
112-
@($ty:ty)
113-
@()
114-
) => {
115-
// Base case
116-
};
111+
($v:vis)
112+
($ty:ty)
113+
) => {};
114+
115+
// Parse each field
117116
(
118-
@__inner
119-
@($v:vis)
120-
@($ty:ty)
121-
@(
122-
$(#[$field_m:meta])*
123-
$field:ident = $value:expr,
117+
($v:vis)
118+
($ty:ty)
124119

125-
$($rest:tt)*
126-
)
120+
$(#[$field_m:meta])*
121+
$field:ident = $value:expr,
122+
123+
$($rest:tt)*
127124
) => {
128125
$(#[$field_m])*
129126
$v const $field: $ty = $value;
130127

131-
extern_enum! {
132-
@__inner
133-
@($v)
134-
@($ty)
135-
@($($rest)*)
128+
extern_enum_inner! {
129+
($v)
130+
($ty)
131+
$($rest)*
136132
}
137133
};
138134
}
@@ -309,11 +305,11 @@ macro_rules! extern_static {
309305
macro_rules! extern_fn {
310306
(
311307
$(#[$m:meta])*
312-
$v:vis unsafe fn $name:ident($($args:tt)*) $(-> $res:ty)?;
308+
$v:vis unsafe fn $name:ident($($params:tt)*) $(-> $res:ty)?;
313309
) => {
314310
$(#[$m])*
315311
extern "C" {
316-
$v fn $name($($args)*) $(-> $res)?;
312+
$v fn $name($($params)*) $(-> $res)?;
317313
}
318314
};
319315
(
@@ -336,7 +332,7 @@ macro_rules! extern_fn {
336332
macro_rules! inline_fn {
337333
(
338334
$(#[$m:meta])*
339-
$v:vis unsafe fn $name:ident($($args:tt)*) $(-> $res:ty)? $body:block
335+
$v:vis unsafe fn $name:ident($($params:tt)*) $(-> $res:ty)? $body:block
340336
) => {
341337
// TODO
342338
};

crates/objc2/src/declare/mod.rs

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ pub trait MethodImplementation: private::Sealed + Sized {
166166
fn __imp(self) -> Imp;
167167
}
168168

169-
macro_rules! method_decl_impl {
170-
(@<$($l:lifetime),*> T: $t_bound:ident $(+ $t_bound2:ident)?, $r:ident, $f:ty, $($t:ident),*) => {
169+
macro_rules! method_impl_generic {
170+
(<$($l:lifetime),*> T: $t_bound:ident $(+ $t_bound2:ident)?, $r:ident, $f:ty, $($t:ident),*) => {
171171
impl<$($l,)* T, $r, $($t),*> private::Sealed for $f
172172
where
173173
T: ?Sized + $t_bound $(+ $t_bound2)?,
@@ -190,7 +190,10 @@ macro_rules! method_decl_impl {
190190
}
191191
}
192192
};
193-
(@<$($l:lifetime),*> $callee:ident, $r:ident, $f:ty, $($t:ident),*) => {
193+
}
194+
195+
macro_rules! method_impl_concrete {
196+
(<$($l:lifetime),*> $callee:ident, $r:ident, $f:ty, $($t:ident),*) => {
194197
impl<$($l,)* $r, $($t),*> private::Sealed for $f
195198
where
196199
$r: EncodeReturn,
@@ -211,7 +214,10 @@ macro_rules! method_decl_impl {
211214
}
212215
}
213216
};
214-
(@<> Allocated<T>, $f:ty, $($t:ident),*) => {
217+
}
218+
219+
macro_rules! method_impl_allocated {
220+
(<> Allocated<T>, $f:ty, $($t:ident),*) => {
215221
#[doc(hidden)]
216222
impl<T, $($t),*> private::Sealed for $f
217223
where
@@ -242,48 +248,54 @@ macro_rules! method_decl_impl {
242248
}
243249
}
244250
};
245-
(# $abi:literal; $($t:ident),*) => {
246-
method_decl_impl!(@<'a> T: Message, R, extern $abi fn(&'a T, Sel $(, $t)*) -> R, $($t),*);
247-
method_decl_impl!(@<'a> T: Message + IsMutable, R, extern $abi fn(&'a mut T, Sel $(, $t)*) -> R, $($t),*);
248-
method_decl_impl!(@<> T: Message, R, unsafe extern $abi fn(*const T, Sel $(, $t)*) -> R, $($t),*);
249-
method_decl_impl!(@<> T: Message, R, unsafe extern $abi fn(*mut T, Sel $(, $t)*) -> R, $($t),*);
250-
method_decl_impl!(@<'a> T: Message, R, unsafe extern $abi fn(&'a T, Sel $(, $t)*) -> R, $($t),*);
251-
method_decl_impl!(@<'a> T: Message + IsMutable, R, unsafe extern $abi fn(&'a mut T, Sel $(, $t)*) -> R, $($t),*);
252-
253-
method_decl_impl!(@<'a> AnyObject, R, extern $abi fn(&'a mut AnyObject, Sel $(, $t)*) -> R, $($t),*);
254-
method_decl_impl!(@<'a> AnyObject, R, unsafe extern $abi fn(&'a mut AnyObject, Sel $(, $t)*) -> R, $($t),*);
255-
256-
method_decl_impl!(@<'a> AnyClass, R, extern $abi fn(&'a AnyClass, Sel $(, $t)*) -> R, $($t),*);
257-
method_decl_impl!(@<> AnyClass, R, unsafe extern $abi fn(*const AnyClass, Sel $(, $t)*) -> R, $($t),*);
258-
method_decl_impl!(@<'a> AnyClass, R, unsafe extern $abi fn(&'a AnyClass, Sel $(, $t)*) -> R, $($t),*);
259-
260-
method_decl_impl!(@<> Allocated<T>, extern $abi fn(Allocated<T>, Sel $(, $t)*) -> __IdReturnValue, $($t),*);
261-
method_decl_impl!(@<> Allocated<T>, unsafe extern $abi fn(Allocated<T>, Sel $(, $t)*) -> __IdReturnValue, $($t),*);
251+
}
252+
253+
macro_rules! method_impl_abi {
254+
($abi:literal; $($t:ident),*) => {
255+
method_impl_generic!(<'a> T: Message, R, extern $abi fn(&'a T, Sel $(, $t)*) -> R, $($t),*);
256+
method_impl_generic!(<'a> T: Message + IsMutable, R, extern $abi fn(&'a mut T, Sel $(, $t)*) -> R, $($t),*);
257+
method_impl_generic!(<> T: Message, R, unsafe extern $abi fn(*const T, Sel $(, $t)*) -> R, $($t),*);
258+
method_impl_generic!(<> T: Message, R, unsafe extern $abi fn(*mut T, Sel $(, $t)*) -> R, $($t),*);
259+
method_impl_generic!(<'a> T: Message, R, unsafe extern $abi fn(&'a T, Sel $(, $t)*) -> R, $($t),*);
260+
method_impl_generic!(<'a> T: Message + IsMutable, R, unsafe extern $abi fn(&'a mut T, Sel $(, $t)*) -> R, $($t),*);
261+
262+
method_impl_concrete!(<'a> AnyObject, R, extern $abi fn(&'a mut AnyObject, Sel $(, $t)*) -> R, $($t),*);
263+
method_impl_concrete!(<'a> AnyObject, R, unsafe extern $abi fn(&'a mut AnyObject, Sel $(, $t)*) -> R, $($t),*);
264+
265+
method_impl_concrete!(<'a> AnyClass, R, extern $abi fn(&'a AnyClass, Sel $(, $t)*) -> R, $($t),*);
266+
method_impl_concrete!(<> AnyClass, R, unsafe extern $abi fn(*const AnyClass, Sel $(, $t)*) -> R, $($t),*);
267+
method_impl_concrete!(<'a> AnyClass, R, unsafe extern $abi fn(&'a AnyClass, Sel $(, $t)*) -> R, $($t),*);
268+
269+
method_impl_allocated!(<> Allocated<T>, extern $abi fn(Allocated<T>, Sel $(, $t)*) -> __IdReturnValue, $($t),*);
270+
method_impl_allocated!(<> Allocated<T>, unsafe extern $abi fn(Allocated<T>, Sel $(, $t)*) -> __IdReturnValue, $($t),*);
262271
};
272+
}
273+
274+
macro_rules! method_impl {
263275
($($t:ident),*) => {
264-
method_decl_impl!(# "C"; $($t),*);
276+
method_impl_abi!("C"; $($t),*);
265277
#[cfg(feature = "unstable-c-unwind")]
266-
method_decl_impl!(# "C-unwind"; $($t),*);
278+
method_impl_abi!("C-unwind"; $($t),*);
267279
};
268280
}
269281

270-
method_decl_impl!();
271-
method_decl_impl!(A);
272-
method_decl_impl!(A, B);
273-
method_decl_impl!(A, B, C);
274-
method_decl_impl!(A, B, C, D);
275-
method_decl_impl!(A, B, C, D, E);
276-
method_decl_impl!(A, B, C, D, E, F);
277-
method_decl_impl!(A, B, C, D, E, F, G);
278-
method_decl_impl!(A, B, C, D, E, F, G, H);
279-
method_decl_impl!(A, B, C, D, E, F, G, H, I);
280-
method_decl_impl!(A, B, C, D, E, F, G, H, I, J);
281-
method_decl_impl!(A, B, C, D, E, F, G, H, I, J, K);
282-
method_decl_impl!(A, B, C, D, E, F, G, H, I, J, K, L);
283-
method_decl_impl!(A, B, C, D, E, F, G, H, I, J, K, L, M);
284-
method_decl_impl!(A, B, C, D, E, F, G, H, I, J, K, L, M, N);
285-
method_decl_impl!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
286-
method_decl_impl!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);
282+
method_impl!();
283+
method_impl!(A);
284+
method_impl!(A, B);
285+
method_impl!(A, B, C);
286+
method_impl!(A, B, C, D);
287+
method_impl!(A, B, C, D, E);
288+
method_impl!(A, B, C, D, E, F);
289+
method_impl!(A, B, C, D, E, F, G);
290+
method_impl!(A, B, C, D, E, F, G, H);
291+
method_impl!(A, B, C, D, E, F, G, H, I);
292+
method_impl!(A, B, C, D, E, F, G, H, I, J);
293+
method_impl!(A, B, C, D, E, F, G, H, I, J, K);
294+
method_impl!(A, B, C, D, E, F, G, H, I, J, K, L);
295+
method_impl!(A, B, C, D, E, F, G, H, I, J, K, L, M);
296+
method_impl!(A, B, C, D, E, F, G, H, I, J, K, L, M, N);
297+
method_impl!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
298+
method_impl!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);
287299

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

0 commit comments

Comments
 (0)