]> git.lizzy.rs Git - rust.git/blob - src/librustc_ast/expand/allocator.rs
Merge branch 'master' into feature/incorporate-tracing
[rust.git] / src / librustc_ast / expand / allocator.rs
1 use crate::{ast, attr, visit};
2 use rustc_span::symbol::{sym, Symbol};
3 use rustc_span::Span;
4
5 #[derive(Clone, Copy)]
6 pub enum AllocatorKind {
7     Global,
8     Default,
9 }
10
11 impl AllocatorKind {
12     pub fn fn_name(&self, base: Symbol) -> String {
13         match *self {
14             AllocatorKind::Global => format!("__rg_{}", base),
15             AllocatorKind::Default => format!("__rdl_{}", base),
16         }
17     }
18 }
19
20 pub enum AllocatorTy {
21     Layout,
22     Ptr,
23     ResultPtr,
24     Unit,
25     Usize,
26 }
27
28 pub struct AllocatorMethod {
29     pub name: Symbol,
30     pub inputs: &'static [AllocatorTy],
31     pub output: AllocatorTy,
32 }
33
34 pub static ALLOCATOR_METHODS: &[AllocatorMethod] = &[
35     AllocatorMethod {
36         name: sym::alloc,
37         inputs: &[AllocatorTy::Layout],
38         output: AllocatorTy::ResultPtr,
39     },
40     AllocatorMethod {
41         name: sym::dealloc,
42         inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout],
43         output: AllocatorTy::Unit,
44     },
45     AllocatorMethod {
46         name: sym::realloc,
47         inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout, AllocatorTy::Usize],
48         output: AllocatorTy::ResultPtr,
49     },
50     AllocatorMethod {
51         name: sym::alloc_zeroed,
52         inputs: &[AllocatorTy::Layout],
53         output: AllocatorTy::ResultPtr,
54     },
55 ];
56
57 pub fn global_allocator_spans(krate: &ast::Crate) -> Vec<Span> {
58     struct Finder {
59         name: Symbol,
60         spans: Vec<Span>,
61     }
62     impl<'ast> visit::Visitor<'ast> for Finder {
63         fn visit_item(&mut self, item: &'ast ast::Item) {
64             if item.ident.name == self.name
65                 && attr::contains_name(&item.attrs, sym::rustc_std_internal_symbol)
66             {
67                 self.spans.push(item.span);
68             }
69             visit::walk_item(self, item)
70         }
71     }
72
73     let name = Symbol::intern(&AllocatorKind::Global.fn_name(sym::alloc));
74     let mut f = Finder { name, spans: Vec::new() };
75     visit::walk_crate(&mut f, krate);
76     f.spans
77 }