]>
Commit | Line | Data |
---|---|---|
416331ca | 1 | use crate::{ast, attr, visit}; |
e74abb32 | 2 | use syntax_pos::symbol::{sym, Symbol}; |
416331ca XL |
3 | use syntax_pos::Span; |
4 | ||
5 | #[derive(Clone, Copy)] | |
6 | pub enum AllocatorKind { | |
7 | Global, | |
60c5eb7d | 8 | Default, |
416331ca XL |
9 | } |
10 | ||
11 | impl AllocatorKind { | |
12 | pub fn fn_name(&self, base: &str) -> String { | |
13 | match *self { | |
14 | AllocatorKind::Global => format!("__rg_{}", base), | |
60c5eb7d | 15 | AllocatorKind::Default => format!("__rdl_{}", base), |
416331ca XL |
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: &'static str, | |
30 | pub inputs: &'static [AllocatorTy], | |
31 | pub output: AllocatorTy, | |
32 | } | |
33 | ||
34 | pub static ALLOCATOR_METHODS: &[AllocatorMethod] = &[ | |
35 | AllocatorMethod { | |
36 | name: "alloc", | |
37 | inputs: &[AllocatorTy::Layout], | |
38 | output: AllocatorTy::ResultPtr, | |
39 | }, | |
40 | AllocatorMethod { | |
41 | name: "dealloc", | |
42 | inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout], | |
43 | output: AllocatorTy::Unit, | |
44 | }, | |
45 | AllocatorMethod { | |
46 | name: "realloc", | |
47 | inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout, AllocatorTy::Usize], | |
48 | output: AllocatorTy::ResultPtr, | |
49 | }, | |
50 | AllocatorMethod { | |
51 | name: "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 { name: Symbol, spans: Vec<Span> } | |
59 | impl<'ast> visit::Visitor<'ast> for Finder { | |
60 | fn visit_item(&mut self, item: &'ast ast::Item) { | |
61 | if item.ident.name == self.name && | |
62 | attr::contains_name(&item.attrs, sym::rustc_std_internal_symbol) { | |
63 | self.spans.push(item.span); | |
64 | } | |
65 | visit::walk_item(self, item) | |
66 | } | |
67 | } | |
68 | ||
69 | let name = Symbol::intern(&AllocatorKind::Global.fn_name("alloc")); | |
70 | let mut f = Finder { name, spans: Vec::new() }; | |
71 | visit::walk_crate(&mut f, krate); | |
72 | f.spans | |
73 | } |