Skip to main content

rustc_ast/expand/
allocator.rs

1use rustc_macros::StableHash;
2use rustc_span::{Symbol, sym};
3
4#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for AllocatorKind {
}
#[automatically_derived]impl ::core::clone::Clone for AllocatorKind {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self { *self }
}Clone, #[automatically_derived]impl ::core::fmt::Debug for AllocatorKind {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                AllocatorKind::Global => "Global",
                AllocatorKind::Default => "Default",
            })
    }
}Debug, #[automatically_derived]impl ::core::marker::Copy for AllocatorKind { }Copy, #[automatically_derived]impl ::core::cmp::Eq for AllocatorKind { }Eq, #[automatically_derived]impl ::core::marker::StructuralPartialEq for
    AllocatorKind {
}
#[automatically_derived]impl ::core::cmp::PartialEq for AllocatorKind {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        ::core::intrinsics::discriminant_value(self) ==
            ::core::intrinsics::discriminant_value(other)
    }
}PartialEq, const _: () =
    {
        impl ::rustc_data_structures::stable_hash::StableHash for
            AllocatorKind {
            #[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 {
                    AllocatorKind::Global => 0u8.stable_hash(__hcx, __hasher),
                    AllocatorKind::Default => 1u8.stable_hash(__hcx, __hasher),
                }
                match *self {
                    AllocatorKind::Global => {}
                    AllocatorKind::Default => {}
                }
            }
        }
    };StableHash)]
5pub enum AllocatorKind {
6    /// Use `#[global_allocator]` as global allocator.
7    Global,
8    /// Use the default implementation in libstd as global allocator.
9    Default,
10}
11
12pub fn global_fn_name(base: Symbol) -> String {
13    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("__rust_{0}", base))
    })format!("__rust_{base}")
14}
15
16pub fn default_fn_name(base: Symbol) -> String {
17    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("__rdl_{0}", base))
    })format!("__rdl_{base}")
18}
19
20pub const ALLOC_ERROR_HANDLER: Symbol = sym::alloc_error_handler;
21pub const NO_ALLOC_SHIM_IS_UNSTABLE: &str = "__rust_no_alloc_shim_is_unstable_v2";
22
23/// Argument or return type for methods in the allocator shim
24#[derive(#[automatically_derived]impl ::core::marker::Copy for AllocatorTy { }Copy, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for AllocatorTy {
}
#[automatically_derived]impl ::core::clone::Clone for AllocatorTy {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self { *self }
}Clone, #[automatically_derived]impl ::core::fmt::Debug for AllocatorTy {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_c_like_enum_write_str(f,
            "Layout Never Ptr ResultPtr Unit Usize",
            match self {
                AllocatorTy::Layout => 0usize,
                AllocatorTy::Never => 7usize,
                AllocatorTy::Ptr => 13usize,
                AllocatorTy::ResultPtr => 17usize,
                AllocatorTy::Unit => 27usize,
                AllocatorTy::Usize => 32usize,
            })
    }
}Debug)]
25pub enum AllocatorTy {
26    Layout,
27    Never,
28    Ptr,
29    ResultPtr,
30    Unit,
31    Usize,
32}
33
34/// Some allocator methods are known to the compiler: they act more like
35/// intrinsics/language primitives than library-defined functions.
36/// FIXME: ideally this would be derived from attributes like `#[rustc_allocator]`,
37/// so we don't have two sources of truth.
38#[derive(#[automatically_derived]impl ::core::marker::Copy for SpecialAllocatorMethod {
}Copy, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for SpecialAllocatorMethod {
}
#[automatically_derived]impl ::core::clone::Clone for SpecialAllocatorMethod {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self { *self }
}Clone, #[automatically_derived]impl ::core::fmt::Debug for SpecialAllocatorMethod {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                SpecialAllocatorMethod::Alloc => "Alloc",
                SpecialAllocatorMethod::AllocZeroed => "AllocZeroed",
                SpecialAllocatorMethod::Dealloc => "Dealloc",
                SpecialAllocatorMethod::Realloc => "Realloc",
            })
    }
}Debug)]
39pub enum SpecialAllocatorMethod {
40    Alloc,
41    AllocZeroed,
42    Dealloc,
43    Realloc,
44}
45
46/// A method that will be codegened in the allocator shim.
47#[derive(#[automatically_derived]impl ::core::marker::Copy for AllocatorMethod { }Copy, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for AllocatorMethod {
}
#[automatically_derived]impl ::core::clone::Clone for AllocatorMethod {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<Symbol>;
        let _:
                ::core::clone::AssertParamIsClone<Option<SpecialAllocatorMethod>>;
        let _:
                ::core::clone::AssertParamIsClone<&'static [AllocatorMethodInput]>;
        let _: ::core::clone::AssertParamIsClone<AllocatorTy>;
        *self
    }
}Clone, #[automatically_derived]impl ::core::fmt::Debug for AllocatorMethod {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field4_finish(f,
            "AllocatorMethod name special inputs output", &self.name,
            &self.special, &self.inputs, &&self.output)
    }
}Debug)]
48pub struct AllocatorMethod {
49    pub name: Symbol,
50    pub special: Option<SpecialAllocatorMethod>,
51    pub inputs: &'static [AllocatorMethodInput],
52    pub output: AllocatorTy,
53}
54
55#[derive(#[automatically_derived]impl ::core::fmt::Debug for AllocatorMethodInput {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "AllocatorMethodInput name ty", &self.name, &&self.ty)
    }
}Debug)]
56pub struct AllocatorMethodInput {
57    pub name: &'static str,
58    pub ty: AllocatorTy,
59}
60
61pub static ALLOCATOR_METHODS: &[AllocatorMethod] = &[
62    AllocatorMethod {
63        name: sym::alloc,
64        special: Some(SpecialAllocatorMethod::Alloc),
65        inputs: &[AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout }],
66        output: AllocatorTy::ResultPtr,
67    },
68    AllocatorMethod {
69        name: sym::dealloc,
70        special: Some(SpecialAllocatorMethod::Dealloc),
71        inputs: &[
72            AllocatorMethodInput { name: "ptr", ty: AllocatorTy::Ptr },
73            AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout },
74        ],
75        output: AllocatorTy::Unit,
76    },
77    AllocatorMethod {
78        name: sym::realloc,
79        special: Some(SpecialAllocatorMethod::Realloc),
80        inputs: &[
81            AllocatorMethodInput { name: "ptr", ty: AllocatorTy::Ptr },
82            AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout },
83            AllocatorMethodInput { name: "new_size", ty: AllocatorTy::Usize },
84        ],
85        output: AllocatorTy::ResultPtr,
86    },
87    AllocatorMethod {
88        name: sym::alloc_zeroed,
89        special: Some(SpecialAllocatorMethod::AllocZeroed),
90        inputs: &[AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout }],
91        output: AllocatorTy::ResultPtr,
92    },
93];