Skip to main content

rustc_ast/expand/
allocator.rs

1use rustc_macros::HashStable_Generic;
2use rustc_span::{Symbol, sym};
3
4#[derive(#[automatically_derived]
impl ::core::clone::Clone for AllocatorKind {
    #[inline]
    fn clone(&self) -> AllocatorKind { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for AllocatorKind {
    #[inline]
    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 {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::cmp::PartialEq for AllocatorKind {
    #[inline]
    fn eq(&self, other: &AllocatorKind) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, const _: () =
    {
        impl<__CTX> ::rustc_data_structures::stable_hasher::HashStable<__CTX>
            for AllocatorKind where __CTX: crate::HashStableContext {
            #[inline]
            fn hash_stable(&self, __hcx: &mut __CTX,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                ::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
                match *self {
                    AllocatorKind::Global => {}
                    AllocatorKind::Default => {}
                }
            }
        }
    };HashStable_Generic)]
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]
impl ::core::clone::Clone for AllocatorTy {
    #[inline]
    fn clone(&self) -> AllocatorTy { *self }
}Clone)]
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]
impl ::core::clone::Clone for SpecialAllocatorMethod {
    #[inline]
    fn clone(&self) -> SpecialAllocatorMethod { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for SpecialAllocatorMethod {
    #[inline]
    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]
impl ::core::clone::Clone for AllocatorMethod {
    #[inline]
    fn clone(&self) -> AllocatorMethod {
        let _: ::core::clone::AssertParamIsClone<Symbol>;
        let _:
                ::core::clone::AssertParamIsClone<Option<SpecialAllocatorMethod>>;
        let _:
                ::core::clone::AssertParamIsClone<&'static [AllocatorMethodInput]>;
        let _: ::core::clone::AssertParamIsClone<AllocatorTy>;
        *self
    }
}Clone)]
48pub struct AllocatorMethod {
49    pub name: Symbol,
50    pub special: Option<SpecialAllocatorMethod>,
51    pub inputs: &'static [AllocatorMethodInput],
52    pub output: AllocatorTy,
53}
54
55pub struct AllocatorMethodInput {
56    pub name: &'static str,
57    pub ty: AllocatorTy,
58}
59
60pub static ALLOCATOR_METHODS: &[AllocatorMethod] = &[
61    AllocatorMethod {
62        name: sym::alloc,
63        special: Some(SpecialAllocatorMethod::Alloc),
64        inputs: &[AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout }],
65        output: AllocatorTy::ResultPtr,
66    },
67    AllocatorMethod {
68        name: sym::dealloc,
69        special: Some(SpecialAllocatorMethod::Dealloc),
70        inputs: &[
71            AllocatorMethodInput { name: "ptr", ty: AllocatorTy::Ptr },
72            AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout },
73        ],
74        output: AllocatorTy::Unit,
75    },
76    AllocatorMethod {
77        name: sym::realloc,
78        special: Some(SpecialAllocatorMethod::Realloc),
79        inputs: &[
80            AllocatorMethodInput { name: "ptr", ty: AllocatorTy::Ptr },
81            AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout },
82            AllocatorMethodInput { name: "new_size", ty: AllocatorTy::Usize },
83        ],
84        output: AllocatorTy::ResultPtr,
85    },
86    AllocatorMethod {
87        name: sym::alloc_zeroed,
88        special: Some(SpecialAllocatorMethod::AllocZeroed),
89        inputs: &[AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout }],
90        output: AllocatorTy::ResultPtr,
91    },
92];