]> git.proxmox.com Git - rustc.git/blame - src/libsyntax/ext/allocator.rs
New upstream version 1.38.0+dfsg1
[rustc.git] / src / libsyntax / ext / allocator.rs
CommitLineData
416331ca
XL
1use crate::{ast, attr, visit};
2use crate::symbol::{sym, Symbol};
3use syntax_pos::Span;
4
5#[derive(Clone, Copy)]
6pub enum AllocatorKind {
7 Global,
8 DefaultLib,
9 DefaultExe,
10}
11
12impl 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
22pub enum AllocatorTy {
23 Layout,
24 Ptr,
25 ResultPtr,
26 Unit,
27 Usize,
28}
29
30pub struct AllocatorMethod {
31 pub name: &'static str,
32 pub inputs: &'static [AllocatorTy],
33 pub output: AllocatorTy,
34}
35
36pub 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
59pub 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}