2019-10-09 16:47:38 +02:00
|
|
|
use crate::{ast, attr, visit};
|
|
|
|
use syntax_pos::symbol::{sym, Symbol};
|
2019-07-19 00:24:58 +03:00
|
|
|
use syntax_pos::Span;
|
|
|
|
|
2019-07-16 21:06:17 +03:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum AllocatorKind {
|
|
|
|
Global,
|
2019-11-24 14:37:46 +03:00
|
|
|
Default,
|
2019-07-16 21:06:17 +03:00
|
|
|
}
|
2017-06-03 14:54:08 -07:00
|
|
|
|
2019-07-16 21:06:17 +03:00
|
|
|
impl AllocatorKind {
|
|
|
|
pub fn fn_name(&self, base: &str) -> String {
|
|
|
|
match *self {
|
|
|
|
AllocatorKind::Global => format!("__rg_{}", base),
|
2019-11-24 14:37:46 +03:00
|
|
|
AllocatorKind::Default => format!("__rdl_{}", base),
|
2019-07-16 21:06:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-03 14:54:08 -07:00
|
|
|
|
2019-07-16 21:06:17 +03:00
|
|
|
pub enum AllocatorTy {
|
|
|
|
Layout,
|
|
|
|
Ptr,
|
|
|
|
ResultPtr,
|
|
|
|
Unit,
|
|
|
|
Usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AllocatorMethod {
|
|
|
|
pub name: &'static str,
|
|
|
|
pub inputs: &'static [AllocatorTy],
|
|
|
|
pub output: AllocatorTy,
|
|
|
|
}
|
2017-06-03 14:54:08 -07:00
|
|
|
|
|
|
|
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",
|
2018-04-03 17:12:57 +02:00
|
|
|
inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout, AllocatorTy::Usize],
|
2017-06-03 14:54:08 -07:00
|
|
|
output: AllocatorTy::ResultPtr,
|
|
|
|
},
|
|
|
|
AllocatorMethod {
|
|
|
|
name: "alloc_zeroed",
|
|
|
|
inputs: &[AllocatorTy::Layout],
|
|
|
|
output: AllocatorTy::ResultPtr,
|
|
|
|
},
|
|
|
|
];
|
2019-07-19 00:24:58 +03:00
|
|
|
|
|
|
|
pub fn global_allocator_spans(krate: &ast::Crate) -> Vec<Span> {
|
2019-12-22 17:42:04 -05:00
|
|
|
struct Finder {
|
|
|
|
name: Symbol,
|
|
|
|
spans: Vec<Span>,
|
|
|
|
}
|
2019-07-19 00:24:58 +03:00
|
|
|
impl<'ast> visit::Visitor<'ast> for Finder {
|
|
|
|
fn visit_item(&mut self, item: &'ast ast::Item) {
|
2019-12-22 17:42:04 -05:00
|
|
|
if item.ident.name == self.name
|
|
|
|
&& attr::contains_name(&item.attrs, sym::rustc_std_internal_symbol)
|
|
|
|
{
|
2019-07-19 00:24:58 +03:00
|
|
|
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
|
|
|
|
}
|