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