rustc_ast/expand/
allocator.rs

1use rustc_macros::HashStable_Generic;
2use rustc_span::{Symbol, sym};
3
4#[derive(Clone, Debug, Copy, Eq, PartialEq, 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    format!("__rust_{base}")
14}
15
16pub fn default_fn_name(base: Symbol) -> String {
17    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(Copy, 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(Copy, Clone, 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(Copy, 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];