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