Skip to content

Commit 0bd840c

Browse files
committed
Create a strong_if attribute for the intrinsics! macro
This can be specified with a `cfg` predicate that disables weak linkage for specific intrinsics.
1 parent e7fd5be commit 0bd840c

File tree

1 file changed

+62
-7
lines changed

1 file changed

+62
-7
lines changed

src/macros.rs

+62-7
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,56 @@ macro_rules! intrinsics {
435435
intrinsics!($($rest)*);
436436
);
437437

438-
// This is the final catch-all rule. At this point we generate an
438+
// This is the last rule for anything that has `strong_if`, which can be
439+
// provided to opt out of the default weak linkage.
440+
//
441+
// This attribute takes a `cfg` predicate, e.g. `#![strong_if(target_arch = "mips")]`
442+
(
443+
444+
#[strong_if($($strong:tt)*)]
445+
$(#[$($attr:tt)*])*
446+
pub $(unsafe $(@ $empty:tt)?)? extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
447+
$($body:tt)*
448+
}
449+
450+
$($rest:tt)*
451+
) => (
452+
intrinsics!(
453+
@final
454+
func:
455+
$(#[$($attr)*])*
456+
pub $(unsafe $($empty)?)? extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
457+
$($body)*
458+
}
459+
strong_if: ( $($strong)* )
460+
rest: $($rest)*
461+
);
462+
);
463+
464+
// This is the final catch-all rule, used by everything that does not have
465+
// `strong_if` (above). The rule just converts matched syntax into the
466+
// struct-like syntax used by `@final`.
467+
(
468+
$(#[$($attr:tt)*])*
469+
pub $(unsafe $(@ $empty:tt)?)? extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
470+
$($body:tt)*
471+
}
472+
473+
$($rest:tt)*
474+
) => (
475+
intrinsics!(
476+
@final
477+
func:
478+
$(#[$($attr)*])*
479+
pub $(unsafe $($empty)?)? extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
480+
$($body)*
481+
}
482+
strong_if:
483+
rest: $($rest)*
484+
);
485+
);
486+
487+
// This is where everything gets instantiated. At this point we generate an
439488
// intrinsic with a conditional `#[no_mangle]` directive to avoid
440489
// interfering with duplicate symbols and whatnot during testing.
441490
//
@@ -448,12 +497,15 @@ macro_rules! intrinsics {
448497
// After the intrinsic is defined we just continue with the rest of the
449498
// input we were given.
450499
(
451-
$(#[$($attr:tt)*])*
452-
pub $(unsafe $(@ $empty:tt)?)? extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
453-
$($body:tt)*
454-
}
500+
@final
501+
func:
502+
$(#[$($attr:tt)*])*
503+
pub $(unsafe $(@ $empty:tt)?)? extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
504+
$($body:tt)*
505+
}
455506

456-
$($rest:tt)*
507+
strong_if: $( ( $($strong:tt)* ) )?
508+
rest: $($rest:tt)*
457509
) => (
458510
$(#[$($attr)*])*
459511
pub $(unsafe $($empty)?)? extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
@@ -464,7 +516,10 @@ macro_rules! intrinsics {
464516
mod $name {
465517
$(#[$($attr)*])*
466518
#[no_mangle]
467-
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), linkage = "weak")]
519+
#[cfg_attr(
520+
not(any(windows, target_vendor = "apple" $(, $($strong)* )?)),
521+
linkage = "weak"
522+
)]
468523
$(unsafe $($empty)?)? extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
469524
super::$name($($argname),*)
470525
}

0 commit comments

Comments
 (0)