about summary refs log tree commit diff
path: root/src/libsyntax/expand
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-10-09 16:47:38 +0200
committerMazdak Farrokhzad <twingoow@gmail.com>2019-10-27 17:05:57 +0100
commitfb12c708521d66b1f727ad4c1ec752a78799980d (patch)
tree45db802c6ea92c6b7bcce21b52dd596b7c2c0d17 /src/libsyntax/expand
parent16329402bff5115135804580386ffa8a9e89f1f9 (diff)
downloadrust-fb12c708521d66b1f727ad4c1ec752a78799980d.tar.gz
rust-fb12c708521d66b1f727ad4c1ec752a78799980d.zip
rustc, rustc_passes: don't depend on syntax_expand.
This is done by moving some data definitions to syntax::expand.
Diffstat (limited to 'src/libsyntax/expand')
-rw-r--r--src/libsyntax/expand/allocator.rs75
-rw-r--r--src/libsyntax/expand/mod.rs21
2 files changed, 96 insertions, 0 deletions
diff --git a/src/libsyntax/expand/allocator.rs b/src/libsyntax/expand/allocator.rs
new file mode 100644
index 00000000000..20487b9af03
--- /dev/null
+++ b/src/libsyntax/expand/allocator.rs
@@ -0,0 +1,75 @@
+use crate::{ast, attr, visit};
+use syntax_pos::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
+}
diff --git a/src/libsyntax/expand/mod.rs b/src/libsyntax/expand/mod.rs
new file mode 100644
index 00000000000..038f60287be
--- /dev/null
+++ b/src/libsyntax/expand/mod.rs
@@ -0,0 +1,21 @@
+//! Definitions shared by macros / syntax extensions and e.g. librustc.
+
+use crate::ast::Attribute;
+use syntax_pos::symbol::sym;
+
+pub mod allocator;
+
+bitflags::bitflags! {
+    /// Built-in derives that need some extra tracking beyond the usual macro functionality.
+    #[derive(Default)]
+    pub struct SpecialDerives: u8 {
+        const PARTIAL_EQ = 1 << 0;
+        const EQ         = 1 << 1;
+        const COPY       = 1 << 2;
+    }
+}
+
+pub fn is_proc_macro_attr(attr: &Attribute) -> bool {
+    [sym::proc_macro, sym::proc_macro_attribute, sym::proc_macro_derive]
+        .iter().any(|kind| attr.check_name(*kind))
+}