diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-10-16 10:59:30 +0200 |
|---|---|---|
| committer | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-10-16 10:59:53 +0200 |
| commit | d420d719c4c44c3c6d02b5fafba4f2cf5e837dba (patch) | |
| tree | 5f5586264bf0a019ea09a20f0526337913a00b03 /src/libsyntax_expand/allocator.rs | |
| parent | d160a4e4225eb5e310554316cba78efb2cbe6fa2 (diff) | |
| download | rust-d420d719c4c44c3c6d02b5fafba4f2cf5e837dba.tar.gz rust-d420d719c4c44c3c6d02b5fafba4f2cf5e837dba.zip | |
move syntax::ext to new crate syntax_expand
Diffstat (limited to 'src/libsyntax_expand/allocator.rs')
| -rw-r--r-- | src/libsyntax_expand/allocator.rs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/libsyntax_expand/allocator.rs b/src/libsyntax_expand/allocator.rs new file mode 100644 index 00000000000..3526be17721 --- /dev/null +++ b/src/libsyntax_expand/allocator.rs @@ -0,0 +1,75 @@ +use syntax::{ast, attr, visit}; +use syntax::symbol::{sym, Symbol}; +use syntax_pos::Span; + +#[derive(Clone, Copy)] +pub enum AllocatorKind { + Global, + DefaultLib, + DefaultExe, +} + +impl AllocatorKind { + pub fn fn_name(&self, base: &str) -> String { + match *self { + AllocatorKind::Global => format!("__rg_{}", base), + AllocatorKind::DefaultLib => format!("__rdl_{}", base), + AllocatorKind::DefaultExe => format!("__rde_{}", 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 +} |
