Skip to main content

rustc_middle/ty/
intrinsic.rs

1use std::fmt::Display;
2
3use rustc_errors::IntoDiagArg;
4use rustc_macros::{Decodable, Encodable, StableHash};
5use rustc_span::Symbol;
6use rustc_span::def_id::DefId;
7
8use super::TyCtxt;
9
10/// The name of an intrinsic.
11#[derive(#[automatically_derived]impl ::core::marker::Copy for Intrinsic { }Copy, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for Intrinsic {
}
#[automatically_derived]impl ::core::clone::Clone for Intrinsic {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<Symbol>;
        *self
    }
}Clone, #[automatically_derived]impl ::core::fmt::Debug for Intrinsic {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Self::Rustc(__self_0) =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f, "Rustc",
                    &__self_0),
            Self::Backend { llvm: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "Backend llvm", &__self_0),
        }
    }
}Debug, const _: () =
    {
        impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
            for Intrinsic {
            fn decode(__decoder: &mut __D) -> Self {
                match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
                    {
                    0usize => {
                        Intrinsic::Rustc(::rustc_serialize::Decodable::decode(__decoder))
                    }
                    1usize => {
                        Intrinsic::Backend {
                            llvm: ::rustc_serialize::Decodable::decode(__decoder),
                        }
                    }
                    n => {
                        ::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `Intrinsic`, expected 0..2, actual {0}",
                                n));
                    }
                }
            }
        }
    };Decodable, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for Intrinsic {
            fn encode(&self, __encoder: &mut __E) {
                let disc =
                    match *self {
                        Intrinsic::Rustc(ref __binding_0) => { 0usize }
                        Intrinsic::Backend { llvm: ref __binding_0 } => { 1usize }
                    };
                ::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
                match *self {
                    Intrinsic::Rustc(ref __binding_0) => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                    Intrinsic::Backend { llvm: ref __binding_0 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                    }
                }
            }
        }
    };Encodable, const _: () =
    {
        impl ::rustc_data_structures::stable_hash::StableHash for Intrinsic {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hash::StableHasher) {
                match *self {
                    Intrinsic::Rustc(ref __binding_0) =>
                        0u8.stable_hash(__hcx, __hasher),
                    Intrinsic::Backend { llvm: ref __binding_0 } =>
                        1u8.stable_hash(__hcx, __hasher),
                }
                match *self {
                    Intrinsic::Rustc(ref __binding_0) => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                    }
                    Intrinsic::Backend { llvm: ref __binding_0 } => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                    }
                }
            }
        }
    };StableHash, #[automatically_derived]impl ::core::marker::StructuralPartialEq for Intrinsic
    {
}
#[automatically_derived]impl ::core::cmp::PartialEq for Intrinsic {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        ::core::intrinsics::discriminant_value(self) ==
                ::core::intrinsics::discriminant_value(other) &&
            match (self, other) {
                (Self::Rustc(__self_0), Self::Rustc(__arg1_0)) =>
                    __self_0 == __arg1_0,
                (Self::Backend { llvm: __self_0 }, Self::Backend {
                    llvm: __arg1_0 }) => __self_0 == __arg1_0,
                _ => unsafe { ::core::intrinsics::unreachable() }
            }
    }
}PartialEq, #[automatically_derived]impl ::core::cmp::Eq for Intrinsic {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Symbol>;
    }
}Eq)]
12pub enum Intrinsic {
13    /// A Rust intrinsic (common to all backends).
14    Rustc(Symbol),
15    /// A backend-specific intrinsic. For now, we only support llvm-specific intrinsics.
16    Backend { llvm: Symbol },
17}
18
19impl Display for Intrinsic {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        match self {
22            Intrinsic::Rustc(i) => f.write_fmt(format_args!("{0}", i))write!(f, "{i}"),
23            Intrinsic::Backend { llvm } => f.write_fmt(format_args!("{0}", llvm))write!(f, "{llvm}"),
24        }
25    }
26}
27
28impl IntoDiagArg for Intrinsic {
29    fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
30        self.to_string().into_diag_arg(path)
31    }
32}
33
34#[derive(#[automatically_derived]impl ::core::marker::Copy for IntrinsicDef { }Copy, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for IntrinsicDef {
}
#[automatically_derived]impl ::core::clone::Clone for IntrinsicDef {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<Intrinsic>;
        let _: ::core::clone::AssertParamIsClone<bool>;
        *self
    }
}Clone, #[automatically_derived]impl ::core::fmt::Debug for IntrinsicDef {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f,
            "IntrinsicDef name must_be_overridden const_stable_indirect",
            &self.name, &self.must_be_overridden,
            &&self.const_stable_indirect)
    }
}Debug, const _: () =
    {
        impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
            for IntrinsicDef {
            fn decode(__decoder: &mut __D) -> Self {
                IntrinsicDef {
                    name: ::rustc_serialize::Decodable::decode(__decoder),
                    must_be_overridden: ::rustc_serialize::Decodable::decode(__decoder),
                    const_stable_indirect: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };Decodable, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for IntrinsicDef {
            fn encode(&self, __encoder: &mut __E) {
                let IntrinsicDef {
                        name: ref __binding_0,
                        must_be_overridden: ref __binding_1,
                        const_stable_indirect: ref __binding_2 } = *self;
                ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                    __encoder);
                ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                    __encoder);
                ::rustc_serialize::Encodable::<__E>::encode(__binding_2,
                    __encoder);
            }
        }
    };Encodable, const _: () =
    {
        impl ::rustc_data_structures::stable_hash::StableHash for IntrinsicDef
            {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hash::StableHasher) {
                match *self {
                    IntrinsicDef {
                        name: ref __binding_0,
                        must_be_overridden: ref __binding_1,
                        const_stable_indirect: ref __binding_2 } => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                        { __binding_1.stable_hash(__hcx, __hasher); }
                        { __binding_2.stable_hash(__hcx, __hasher); }
                    }
                }
            }
        }
    };StableHash)]
35pub struct IntrinsicDef {
36    pub name: Intrinsic,
37    /// Whether the intrinsic has no meaningful body and all backends need to shim all calls to it.
38    pub must_be_overridden: bool,
39    /// Whether the intrinsic can be invoked from stable const fn (`#[rustc_intrinsic_const_stable_indirect]`).
40    pub const_stable_indirect: bool,
41}
42
43impl TyCtxt<'_> {
44    pub fn is_rustc_intrinsic(self, def_id: DefId, name: Symbol) -> bool {
45        let Some(i) = self.intrinsic(def_id) else { return false };
46        #[allow(non_exhaustive_omitted_patterns)] match i.name {
    Intrinsic::Rustc(n) if n == name => true,
    _ => false,
}matches!(i.name, Intrinsic::Rustc(n) if n == name)
47    }
48}