]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_allocator/expand.rs
Attempt to fix hygiene for global_allocator
[rust.git] / src / librustc_allocator / expand.rs
index 497d5fdcac70288b94d5f92df52692676537506d..cea1807cfba63081d79d18a673b66a2def7c4d9d 100644 (file)
@@ -8,15 +8,16 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#![allow(unused_imports, unused_variables, dead_code)]
+
 use rustc::middle::allocator::AllocatorKind;
 use rustc_errors;
-use rustc_target::spec::abi::Abi;
 use syntax::ast::{Attribute, Crate, LitKind, StrStyle};
-use syntax::ast::{Arg, Constness, Generics, Mac, Mutability, Ty, Unsafety};
+use syntax::ast::{Arg, FnHeader, Generics, Mac, Mutability, Ty, Unsafety};
 use syntax::ast::{self, Expr, Ident, Item, ItemKind, TyKind, VisibilityKind};
 use syntax::attr;
-use syntax::codemap::{dummy_spanned, respan};
-use syntax::codemap::{ExpnInfo, MacroAttribute, NameAndSpan};
+use syntax::codemap::respan;
+use syntax::codemap::{ExpnInfo, MacroAttribute};
 use syntax::ext::base::ExtCtxt;
 use syntax::ext::base::Resolver;
 use syntax::ext::build::AstBuilder;
@@ -35,6 +36,7 @@ pub fn modify(
     sess: &ParseSess,
     resolver: &mut Resolver,
     krate: Crate,
+    crate_name: String,
     handler: &rustc_errors::Handler,
 ) -> ast::Crate {
     ExpandAllocatorDirectives {
@@ -42,6 +44,7 @@ pub fn modify(
         sess,
         resolver,
         found: false,
+        crate_name: Some(crate_name),
     }.fold_crate(krate)
 }
 
@@ -50,6 +53,7 @@ struct ExpandAllocatorDirectives<'a> {
     handler: &'a rustc_errors::Handler,
     sess: &'a ParseSess,
     resolver: &'a mut Resolver,
+    crate_name: Option<String>,
 }
 
 impl<'a> Folder for ExpandAllocatorDirectives<'a> {
@@ -78,46 +82,44 @@ fn fold_item(&mut self, item: P<Item>) -> SmallVector<P<Item>> {
         }
         self.found = true;
 
+        // Create a fresh Mark for the new macro expansion we are about to do
         let mark = Mark::fresh(Mark::root());
         mark.set_expn_info(ExpnInfo {
-            call_site: DUMMY_SP,
-            callee: NameAndSpan {
-                format: MacroAttribute(Symbol::intern(name)),
-                span: None,
-                allow_internal_unstable: true,
-                allow_internal_unsafe: false,
-                edition: hygiene::default_edition(),
-            },
+            call_site: item.span,
+            def_site: None,
+            format: MacroAttribute(Symbol::intern(name)),
+            allow_internal_unstable: true,
+            allow_internal_unsafe: false,
+            edition: hygiene::default_edition(),
         });
+
+        // Tie the span to the macro expansion info we just created
         let span = item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark));
-        let ecfg = ExpansionConfig::default(name.to_string());
+
+        // Create an expansion config
+        let ecfg = ExpansionConfig::default(self.crate_name.take().unwrap());
+
+        // Generate a bunch of new items using the AllocFnFactory
         let mut f = AllocFnFactory {
             span,
             kind: AllocatorKind::Global,
             global: item.ident,
-            core: Ident::from_str("core"),
+            core: Ident::with_empty_ctxt(Symbol::gensym("core")),
             cx: ExtCtxt::new(self.sess, ecfg, self.resolver),
         };
-        let super_path = f.cx.path(f.span, vec![Ident::from_str("super"), f.global]);
-        let mut items = vec![
-            f.cx.item_extern_crate(f.span, f.core),
-            f.cx.item_use_simple(
-                f.span,
-                respan(f.span.shrink_to_lo(), VisibilityKind::Inherited),
-                super_path,
-            ),
-        ];
-        for method in ALLOCATOR_METHODS {
-            items.push(f.allocator_fn(method));
-        }
-        let name = f.kind.fn_name("allocator_abi");
-        let allocator_abi = Ident::with_empty_ctxt(Symbol::gensym(&name));
-        let module = f.cx.item_mod(span, span, allocator_abi, Vec::new(), items);
-        let module = f.cx.monotonic_expander().fold_item(module).pop().unwrap();
+
+        let extcore = {
+            let extcore = f.cx.item_extern_crate(item.span, f.core);
+            f.cx.monotonic_expander().fold_item(extcore).pop().unwrap()
+        };
 
         let mut ret = SmallVector::new();
         ret.push(item);
-        ret.push(module);
+        ret.push(extcore);
+        ret.extend(ALLOCATOR_METHODS.iter().map(|method| {
+            let method = f.allocator_fn(method);
+            f.cx.monotonic_expander().fold_item(method).pop().unwrap()
+        }));
         return ret;
     }
 
@@ -152,9 +154,10 @@ fn allocator_fn(&self, method: &AllocatorMethod) -> P<Item> {
         let (output_ty, output_expr) = self.ret_ty(&method.output, result);
         let kind = ItemKind::Fn(
             self.cx.fn_decl(abi_args, ast::FunctionRetTy::Ty(output_ty)),
-            Unsafety::Unsafe,
-            dummy_spanned(Constness::NotConst),
-            Abi::Rust,
+            FnHeader {
+                unsafety: Unsafety::Unsafe,
+                ..FnHeader::default()
+            },
             Generics::default(),
             self.cx.block_expr(output_expr),
         );
@@ -170,6 +173,7 @@ fn call_allocator(&self, method: &str, mut args: Vec<P<Expr>>) -> P<Expr> {
         let method = self.cx.path(
             self.span,
             vec![
+                Ident::from_str("self"),
                 self.core,
                 Ident::from_str("alloc"),
                 Ident::from_str("GlobalAlloc"),
@@ -220,6 +224,7 @@ fn arg_ty(
                 let layout_new = self.cx.path(
                     self.span,
                     vec![
+                        Ident::from_str("self"),
                         self.core,
                         Ident::from_str("alloc"),
                         Ident::from_str("Layout"),
@@ -237,7 +242,7 @@ fn arg_ty(
                 let ident = ident();
                 args.push(self.cx.arg(self.span, ident, self.ptr_u8()));
                 let arg = self.cx.expr_ident(self.span, ident);
-                self.cx.expr_cast(self.span, arg, self.ptr_opaque())
+                self.cx.expr_cast(self.span, arg, self.ptr_u8())
             }
 
             AllocatorTy::Usize => {
@@ -281,17 +286,4 @@ fn ptr_u8(&self) -> P<Ty> {
         let ty_u8 = self.cx.ty_path(u8);
         self.cx.ty_ptr(self.span, ty_u8, Mutability::Mutable)
     }
-
-    fn ptr_opaque(&self) -> P<Ty> {
-        let opaque = self.cx.path(
-            self.span,
-            vec![
-                self.core,
-                Ident::from_str("alloc"),
-                Ident::from_str("Opaque"),
-            ],
-        );
-        let ty_opaque = self.cx.ty_path(opaque);
-        self.cx.ty_ptr(self.span, ty_opaque, Mutability::Mutable)
-    }
 }