From e305994beb1347e2fcadf5c84acec60fb6902551 Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Fri, 16 Mar 2018 01:09:22 +0200 Subject: proc_macro: introduce a "bridge" between clients (proc macros) and servers (compiler front-ends). --- src/libsyntax_ext/deriving/custom.rs | 65 +++-- src/libsyntax_ext/lib.rs | 4 +- src/libsyntax_ext/proc_macro_decls.rs | 441 +++++++++++++++++++++++++++++ src/libsyntax_ext/proc_macro_impl.rs | 42 ++- src/libsyntax_ext/proc_macro_registrar.rs | 447 ------------------------------ 5 files changed, 497 insertions(+), 502 deletions(-) create mode 100644 src/libsyntax_ext/proc_macro_decls.rs delete mode 100644 src/libsyntax_ext/proc_macro_registrar.rs (limited to 'src/libsyntax_ext') diff --git a/src/libsyntax_ext/deriving/custom.rs b/src/libsyntax_ext/deriving/custom.rs index 55b3928d68e..76826dd93e7 100644 --- a/src/libsyntax_ext/deriving/custom.rs +++ b/src/libsyntax_ext/deriving/custom.rs @@ -8,15 +8,18 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::panic; - use errors::FatalError; -use proc_macro::{TokenStream, __internal}; use syntax::ast::{self, ItemKind, Attribute, Mac}; use syntax::attr::{mark_used, mark_known}; use syntax::source_map::Span; use syntax::ext::base::*; +use syntax::parse; +use syntax::parse::token::{self, Token}; +use syntax::tokenstream; use syntax::visit::Visitor; +use syntax_pos::DUMMY_SP; + +use proc_macro_impl::EXEC_STRATEGY; struct MarkAttrs<'a>(&'a [ast::Name]); @@ -32,14 +35,10 @@ impl<'a> Visitor<'a> for MarkAttrs<'a> { } pub struct ProcMacroDerive { - inner: fn(TokenStream) -> TokenStream, - attrs: Vec, -} - -impl ProcMacroDerive { - pub fn new(inner: fn(TokenStream) -> TokenStream, attrs: Vec) -> ProcMacroDerive { - ProcMacroDerive { inner: inner, attrs: attrs } - } + pub client: ::proc_macro::bridge::client::Client< + fn(::proc_macro::TokenStream) -> ::proc_macro::TokenStream, + >, + pub attrs: Vec, } impl MultiItemModifier for ProcMacroDerive { @@ -75,10 +74,12 @@ impl MultiItemModifier for ProcMacroDerive { // Mark attributes as known, and used. MarkAttrs(&self.attrs).visit_item(&item); - let input = __internal::new_token_stream(ecx.resolver.eliminate_crate_var(item)); - let res = __internal::set_sess(ecx, || { - let inner = self.inner; - panic::catch_unwind(panic::AssertUnwindSafe(|| inner(input))) + let item = ecx.resolver.eliminate_crate_var(item); + let token = Token::interpolated(token::NtItem(item)); + let input = tokenstream::TokenTree::Token(DUMMY_SP, token).into(); + let server = ::proc_macro::rustc::Rustc; + let res = ::proc_macro::__internal::set_sess(ecx, || { + self.client.run(&EXEC_STRATEGY, server, input) }); let stream = match res { @@ -86,10 +87,7 @@ impl MultiItemModifier for ProcMacroDerive { Err(e) => { let msg = "proc-macro derive panicked"; let mut err = ecx.struct_span_fatal(span, msg); - if let Some(s) = e.downcast_ref::() { - err.help(&format!("message: {}", s)); - } - if let Some(s) = e.downcast_ref::<&'static str>() { + if let Some(s) = e.as_str() { err.help(&format!("message: {}", s)); } @@ -99,21 +97,32 @@ impl MultiItemModifier for ProcMacroDerive { }; let error_count_before = ecx.parse_sess.span_diagnostic.err_count(); - __internal::set_sess(ecx, || { - let msg = "proc-macro derive produced unparseable tokens"; - match __internal::token_stream_parse_items(stream) { - // fail if there have been errors emitted - Ok(_) if ecx.parse_sess.span_diagnostic.err_count() > error_count_before => { - ecx.struct_span_fatal(span, msg).emit(); - FatalError.raise(); + let msg = "proc-macro derive produced unparseable tokens"; + + let mut parser = parse::stream_to_parser(ecx.parse_sess, stream); + let mut items = vec![]; + + loop { + match parser.parse_item().map_err(::proc_macro::__internal::parse_to_lex_err) { + Ok(None) => break, + Ok(Some(item)) => { + items.push(Annotatable::Item(item)) } - Ok(new_items) => new_items.into_iter().map(Annotatable::Item).collect(), Err(_) => { // FIXME: handle this better ecx.struct_span_fatal(span, msg).emit(); FatalError.raise(); } } - }) + } + + + // fail if there have been errors emitted + if ecx.parse_sess.span_diagnostic.err_count() > error_count_before { + ecx.struct_span_fatal(span, msg).emit(); + FatalError.raise(); + } + + items } } diff --git a/src/libsyntax_ext/lib.rs b/src/libsyntax_ext/lib.rs index 7c023fc5c9c..c5bceff4733 100644 --- a/src/libsyntax_ext/lib.rs +++ b/src/libsyntax_ext/lib.rs @@ -55,9 +55,7 @@ mod trace_macros; mod test; mod test_case; -pub mod proc_macro_registrar; - - +pub mod proc_macro_decls; pub mod proc_macro_impl; use rustc_data_structures::sync::Lrc; diff --git a/src/libsyntax_ext/proc_macro_decls.rs b/src/libsyntax_ext/proc_macro_decls.rs new file mode 100644 index 00000000000..c859275ed02 --- /dev/null +++ b/src/libsyntax_ext/proc_macro_decls.rs @@ -0,0 +1,441 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::mem; + +use errors; + +use syntax::ast::{self, Ident}; +use syntax::attr; +use syntax::source_map::{ExpnInfo, MacroAttribute, hygiene, respan}; +use syntax::ext::base::ExtCtxt; +use syntax::ext::build::AstBuilder; +use syntax::ext::expand::ExpansionConfig; +use syntax::ext::hygiene::Mark; +use syntax::fold::Folder; +use syntax::parse::ParseSess; +use syntax::ptr::P; +use syntax::symbol::Symbol; +use syntax::symbol::keywords; +use syntax::visit::{self, Visitor}; + +use syntax_pos::{Span, DUMMY_SP}; + +use deriving; + +const PROC_MACRO_KINDS: [&'static str; 3] = + ["proc_macro_derive", "proc_macro_attribute", "proc_macro"]; + +struct ProcMacroDerive { + trait_name: ast::Name, + function_name: Ident, + span: Span, + attrs: Vec, +} + +struct ProcMacroDef { + function_name: Ident, + span: Span, +} + +struct CollectProcMacros<'a> { + derives: Vec, + attr_macros: Vec, + bang_macros: Vec, + in_root: bool, + handler: &'a errors::Handler, + is_proc_macro_crate: bool, + is_test_crate: bool, +} + +pub fn modify(sess: &ParseSess, + resolver: &mut dyn (::syntax::ext::base::Resolver), + mut krate: ast::Crate, + is_proc_macro_crate: bool, + is_test_crate: bool, + num_crate_types: usize, + handler: &errors::Handler) -> ast::Crate { + let ecfg = ExpansionConfig::default("proc_macro".to_string()); + let mut cx = ExtCtxt::new(sess, ecfg, resolver); + + let (derives, attr_macros, bang_macros) = { + let mut collect = CollectProcMacros { + derives: Vec::new(), + attr_macros: Vec::new(), + bang_macros: Vec::new(), + in_root: true, + handler, + is_proc_macro_crate, + is_test_crate, + }; + visit::walk_crate(&mut collect, &krate); + (collect.derives, collect.attr_macros, collect.bang_macros) + }; + + if !is_proc_macro_crate { + return krate + } + + if num_crate_types > 1 { + handler.err("cannot mix `proc-macro` crate type with others"); + } + + if is_test_crate { + return krate; + } + + krate.module.items.push(mk_decls(&mut cx, &derives, &attr_macros, &bang_macros)); + + krate +} + +fn is_proc_macro_attr(attr: &ast::Attribute) -> bool { + PROC_MACRO_KINDS.iter().any(|kind| attr.check_name(kind)) +} + +impl<'a> CollectProcMacros<'a> { + fn check_not_pub_in_root(&self, vis: &ast::Visibility, sp: Span) { + if self.is_proc_macro_crate && self.in_root && vis.node.is_pub() { + self.handler.span_err(sp, + "`proc-macro` crate types cannot \ + export any items other than functions \ + tagged with `#[proc_macro_derive]` currently"); + } + } + + fn collect_custom_derive(&mut self, item: &'a ast::Item, attr: &'a ast::Attribute) { + // Once we've located the `#[proc_macro_derive]` attribute, verify + // that it's of the form `#[proc_macro_derive(Foo)]` or + // `#[proc_macro_derive(Foo, attributes(A, ..))]` + let list = match attr.meta_item_list() { + Some(list) => list, + None => { + self.handler.span_err(attr.span(), + "attribute must be of form: \ + #[proc_macro_derive(TraitName)]"); + return + } + }; + if list.len() != 1 && list.len() != 2 { + self.handler.span_err(attr.span(), + "attribute must have either one or two arguments"); + return + } + let trait_attr = &list[0]; + let attributes_attr = list.get(1); + let trait_name = match trait_attr.name() { + Some(name) => name, + _ => { + self.handler.span_err(trait_attr.span(), "not a meta item"); + return + } + }; + if !trait_attr.is_word() { + self.handler.span_err(trait_attr.span(), "must only be one word"); + } + + if deriving::is_builtin_trait(trait_name) { + self.handler.span_err(trait_attr.span(), + "cannot override a built-in #[derive] mode"); + } + + let proc_attrs: Vec<_> = if let Some(attr) = attributes_attr { + if !attr.check_name("attributes") { + self.handler.span_err(attr.span(), "second argument must be `attributes`") + } + attr.meta_item_list().unwrap_or_else(|| { + self.handler.span_err(attr.span(), + "attribute must be of form: \ + `attributes(foo, bar)`"); + &[] + }).into_iter().filter_map(|attr| { + let name = match attr.name() { + Some(name) => name, + _ => { + self.handler.span_err(attr.span(), "not a meta item"); + return None; + }, + }; + + if !attr.is_word() { + self.handler.span_err(attr.span(), "must only be one word"); + return None; + } + + Some(name) + }).collect() + } else { + Vec::new() + }; + + if self.in_root && item.vis.node.is_pub() { + self.derives.push(ProcMacroDerive { + span: item.span, + trait_name, + function_name: item.ident, + attrs: proc_attrs, + }); + } else { + let msg = if !self.in_root { + "functions tagged with `#[proc_macro_derive]` must \ + currently reside in the root of the crate" + } else { + "functions tagged with `#[proc_macro_derive]` must be `pub`" + }; + self.handler.span_err(item.span, msg); + } + } + + fn collect_attr_proc_macro(&mut self, item: &'a ast::Item, attr: &'a ast::Attribute) { + if !attr.is_word() { + self.handler.span_err(attr.span, "`#[proc_macro_attribute]` attribute \ + does not take any arguments"); + return; + } + + if self.in_root && item.vis.node.is_pub() { + self.attr_macros.push(ProcMacroDef { + span: item.span, + function_name: item.ident, + }); + } else { + let msg = if !self.in_root { + "functions tagged with `#[proc_macro_attribute]` must \ + currently reside in the root of the crate" + } else { + "functions tagged with `#[proc_macro_attribute]` must be `pub`" + }; + self.handler.span_err(item.span, msg); + } + } + + fn collect_bang_proc_macro(&mut self, item: &'a ast::Item, attr: &'a ast::Attribute) { + if !attr.is_word() { + self.handler.span_err(attr.span, "`#[proc_macro]` attribute \ + does not take any arguments"); + return; + } + + if self.in_root && item.vis.node.is_pub() { + self.bang_macros.push(ProcMacroDef { + span: item.span, + function_name: item.ident, + }); + } else { + let msg = if !self.in_root { + "functions tagged with `#[proc_macro]` must \ + currently reside in the root of the crate" + } else { + "functions tagged with `#[proc_macro]` must be `pub`" + }; + self.handler.span_err(item.span, msg); + } + } +} + +impl<'a> Visitor<'a> for CollectProcMacros<'a> { + fn visit_item(&mut self, item: &'a ast::Item) { + if let ast::ItemKind::MacroDef(..) = item.node { + if self.is_proc_macro_crate && attr::contains_name(&item.attrs, "macro_export") { + let msg = + "cannot export macro_rules! macros from a `proc-macro` crate type currently"; + self.handler.span_err(item.span, msg); + } + } + + // First up, make sure we're checking a bare function. If we're not then + // we're just not interested in this item. + // + // If we find one, try to locate a `#[proc_macro_derive]` attribute on + // it. + let is_fn = match item.node { + ast::ItemKind::Fn(..) => true, + _ => false, + }; + + let mut found_attr: Option<&'a ast::Attribute> = None; + + for attr in &item.attrs { + if is_proc_macro_attr(&attr) { + if let Some(prev_attr) = found_attr { + let msg = if attr.path.segments[0].ident.name == + prev_attr.path.segments[0].ident.name { + format!("Only one `#[{}]` attribute is allowed on any given function", + attr.path) + } else { + format!("`#[{}]` and `#[{}]` attributes cannot both be applied \ + to the same function", attr.path, prev_attr.path) + }; + + self.handler.struct_span_err(attr.span(), &msg) + .span_note(prev_attr.span(), "Previous attribute here") + .emit(); + + return; + } + + found_attr = Some(attr); + } + } + + let attr = match found_attr { + None => { + self.check_not_pub_in_root(&item.vis, item.span); + let prev_in_root = mem::replace(&mut self.in_root, false); + visit::walk_item(self, item); + self.in_root = prev_in_root; + return; + }, + Some(attr) => attr, + }; + + if !is_fn { + let msg = format!("the `#[{}]` attribute may only be used on bare functions", + attr.path); + + self.handler.span_err(attr.span(), &msg); + return; + } + + if self.is_test_crate { + return; + } + + if !self.is_proc_macro_crate { + let msg = format!("the `#[{}]` attribute is only usable with crates of the \ + `proc-macro` crate type", attr.path); + + self.handler.span_err(attr.span(), &msg); + return; + } + + if attr.check_name("proc_macro_derive") { + self.collect_custom_derive(item, attr); + } else if attr.check_name("proc_macro_attribute") { + self.collect_attr_proc_macro(item, attr); + } else if attr.check_name("proc_macro") { + self.collect_bang_proc_macro(item, attr); + }; + + let prev_in_root = mem::replace(&mut self.in_root, false); + visit::walk_item(self, item); + self.in_root = prev_in_root; + } + + fn visit_mac(&mut self, mac: &ast::Mac) { + visit::walk_mac(self, mac) + } +} + +// Creates a new module which looks like: +// +// mod $gensym { +// extern crate proc_macro; +// +// use proc_macro::bridge::client::ProcMacro; +// +// #[rustc_proc_macro_decls] +// static DECLS: &[ProcMacro] = &[ +// ProcMacro::custom_derive($name_trait1, &[], ::$name1); +// ProcMacro::custom_derive($name_trait2, &["attribute_name"], ::$name2); +// // ... +// ]; +// } +fn mk_decls( + cx: &mut ExtCtxt, + custom_derives: &[ProcMacroDerive], + custom_attrs: &[ProcMacroDef], + custom_macros: &[ProcMacroDef], +) -> P { + let mark = Mark::fresh(Mark::root()); + mark.set_expn_info(ExpnInfo { + call_site: DUMMY_SP, + def_site: None, + format: MacroAttribute(Symbol::intern("proc_macro")), + allow_internal_unstable: true, + allow_internal_unsafe: false, + local_inner_macros: false, + edition: hygiene::default_edition(), + }); + let span = DUMMY_SP.apply_mark(mark); + + let proc_macro = Ident::from_str("proc_macro"); + let krate = cx.item(span, + proc_macro, + Vec::new(), + ast::ItemKind::ExternCrate(None)); + + let bridge = Ident::from_str("bridge"); + let client = Ident::from_str("client"); + let proc_macro_ty = Ident::from_str("ProcMacro"); + let custom_derive = Ident::from_str("custom_derive"); + let attr = Ident::from_str("attr"); + let bang = Ident::from_str("bang"); + let crate_kw = Ident::with_empty_ctxt(keywords::Crate.name()); + + let decls = { + let local_path = |sp: Span, name| { + cx.expr_path(cx.path(sp.with_ctxt(span.ctxt()), vec![crate_kw, name])) + }; + let proc_macro_ty_method_path = |method| cx.expr_path(cx.path(span, vec![ + proc_macro, bridge, client, proc_macro_ty, method, + ])); + custom_derives.iter().map(|cd| { + cx.expr_call(span, proc_macro_ty_method_path(custom_derive), vec![ + cx.expr_str(cd.span, cd.trait_name), + cx.expr_vec_slice( + span, + cd.attrs.iter().map(|&s| cx.expr_str(cd.span, s)).collect::>() + ), + local_path(cd.span, cd.function_name), + ]) + }).chain(custom_attrs.iter().map(|ca| { + cx.expr_call(span, proc_macro_ty_method_path(attr), vec![ + cx.expr_str(ca.span, ca.function_name.name), + local_path(ca.span, ca.function_name), + ]) + })).chain(custom_macros.iter().map(|cm| { + cx.expr_call(span, proc_macro_ty_method_path(bang), vec![ + cx.expr_str(cm.span, cm.function_name.name), + local_path(cm.span, cm.function_name), + ]) + })).collect() + }; + + let decls_static = cx.item_static( + span, + Ident::from_str("_DECLS"), + cx.ty_rptr(span, + cx.ty(span, ast::TyKind::Slice( + cx.ty_path(cx.path(span, + vec![proc_macro, bridge, client, proc_macro_ty])))), + None, ast::Mutability::Immutable), + ast::Mutability::Immutable, + cx.expr_vec_slice(span, decls), + ).map(|mut i| { + let attr = cx.meta_word(span, Symbol::intern("rustc_proc_macro_decls")); + i.attrs.push(cx.attribute(span, attr)); + i.vis = respan(span, ast::VisibilityKind::Public); + i + }); + + let module = cx.item_mod( + span, + span, + ast::Ident::with_empty_ctxt(Symbol::gensym("decls")), + vec![], + vec![krate, decls_static], + ).map(|mut i| { + i.vis = respan(span, ast::VisibilityKind::Public); + i + }); + + cx.monotonic_expander().fold_item(module).pop().unwrap() +} diff --git a/src/libsyntax_ext/proc_macro_impl.rs b/src/libsyntax_ext/proc_macro_impl.rs index ff60262055b..1f111dfcaf9 100644 --- a/src/libsyntax_ext/proc_macro_impl.rs +++ b/src/libsyntax_ext/proc_macro_impl.rs @@ -8,7 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::panic; use errors::FatalError; @@ -17,11 +16,13 @@ use syntax::ext::base::*; use syntax::tokenstream::TokenStream; use syntax::ext::base; -use proc_macro::TokenStream as TsShim; -use proc_macro::__internal; +pub const EXEC_STRATEGY: ::proc_macro::bridge::server::SameThread = + ::proc_macro::bridge::server::SameThread; pub struct AttrProcMacro { - pub inner: fn(TsShim, TsShim) -> TsShim, + pub client: ::proc_macro::bridge::client::Client< + fn(::proc_macro::TokenStream, ::proc_macro::TokenStream) -> ::proc_macro::TokenStream, + >, } impl base::AttrProcMacro for AttrProcMacro { @@ -31,22 +32,17 @@ impl base::AttrProcMacro for AttrProcMacro { annotation: TokenStream, annotated: TokenStream) -> TokenStream { - let annotation = __internal::token_stream_wrap(annotation); - let annotated = __internal::token_stream_wrap(annotated); - - let res = __internal::set_sess(ecx, || { - panic::catch_unwind(panic::AssertUnwindSafe(|| (self.inner)(annotation, annotated))) + let server = ::proc_macro::rustc::Rustc; + let res = ::proc_macro::__internal::set_sess(ecx, || { + self.client.run(&EXEC_STRATEGY, server, annotation, annotated) }); match res { - Ok(stream) => __internal::token_stream_inner(stream), + Ok(stream) => stream, Err(e) => { let msg = "custom attribute panicked"; let mut err = ecx.struct_span_fatal(span, msg); - if let Some(s) = e.downcast_ref::() { - err.help(&format!("message: {}", s)); - } - if let Some(s) = e.downcast_ref::<&'static str>() { + if let Some(s) = e.as_str() { err.help(&format!("message: {}", s)); } @@ -58,7 +54,9 @@ impl base::AttrProcMacro for AttrProcMacro { } pub struct BangProcMacro { - pub inner: fn(TsShim) -> TsShim, + pub client: ::proc_macro::bridge::client::Client< + fn(::proc_macro::TokenStream) -> ::proc_macro::TokenStream, + >, } impl base::ProcMacro for BangProcMacro { @@ -67,21 +65,17 @@ impl base::ProcMacro for BangProcMacro { span: Span, input: TokenStream) -> TokenStream { - let input = __internal::token_stream_wrap(input); - - let res = __internal::set_sess(ecx, || { - panic::catch_unwind(panic::AssertUnwindSafe(|| (self.inner)(input))) + let server = ::proc_macro::rustc::Rustc; + let res = ::proc_macro::__internal::set_sess(ecx, || { + self.client.run(&EXEC_STRATEGY, server, input) }); match res { - Ok(stream) => __internal::token_stream_inner(stream), + Ok(stream) => stream, Err(e) => { let msg = "proc macro panicked"; let mut err = ecx.struct_span_fatal(span, msg); - if let Some(s) = e.downcast_ref::() { - err.help(&format!("message: {}", s)); - } - if let Some(s) = e.downcast_ref::<&'static str>() { + if let Some(s) = e.as_str() { err.help(&format!("message: {}", s)); } diff --git a/src/libsyntax_ext/proc_macro_registrar.rs b/src/libsyntax_ext/proc_macro_registrar.rs deleted file mode 100644 index 65e175f95df..00000000000 --- a/src/libsyntax_ext/proc_macro_registrar.rs +++ /dev/null @@ -1,447 +0,0 @@ -// Copyright 2016 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use std::mem; - -use errors; - -use syntax::ast::{self, Ident}; -use syntax::attr; -use syntax::source_map::{ExpnInfo, MacroAttribute, hygiene, respan}; -use syntax::ext::base::ExtCtxt; -use syntax::ext::build::AstBuilder; -use syntax::ext::expand::ExpansionConfig; -use syntax::ext::hygiene::Mark; -use syntax::fold::Folder; -use syntax::parse::ParseSess; -use syntax::ptr::P; -use syntax::symbol::Symbol; -use syntax::symbol::keywords; -use syntax::visit::{self, Visitor}; - -use syntax_pos::{Span, DUMMY_SP}; - -use deriving; - -const PROC_MACRO_KINDS: [&'static str; 3] = - ["proc_macro_derive", "proc_macro_attribute", "proc_macro"]; - -struct ProcMacroDerive { - trait_name: ast::Name, - function_name: Ident, - span: Span, - attrs: Vec, -} - -struct ProcMacroDef { - function_name: Ident, - span: Span, -} - -struct CollectProcMacros<'a> { - derives: Vec, - attr_macros: Vec, - bang_macros: Vec, - in_root: bool, - handler: &'a errors::Handler, - is_proc_macro_crate: bool, - is_test_crate: bool, -} - -pub fn modify(sess: &ParseSess, - resolver: &mut dyn (::syntax::ext::base::Resolver), - mut krate: ast::Crate, - is_proc_macro_crate: bool, - is_test_crate: bool, - num_crate_types: usize, - handler: &errors::Handler) -> ast::Crate { - let ecfg = ExpansionConfig::default("proc_macro".to_string()); - let mut cx = ExtCtxt::new(sess, ecfg, resolver); - - let (derives, attr_macros, bang_macros) = { - let mut collect = CollectProcMacros { - derives: Vec::new(), - attr_macros: Vec::new(), - bang_macros: Vec::new(), - in_root: true, - handler, - is_proc_macro_crate, - is_test_crate, - }; - visit::walk_crate(&mut collect, &krate); - (collect.derives, collect.attr_macros, collect.bang_macros) - }; - - if !is_proc_macro_crate { - return krate - } - - if num_crate_types > 1 { - handler.err("cannot mix `proc-macro` crate type with others"); - } - - if is_test_crate { - return krate; - } - - krate.module.items.push(mk_registrar(&mut cx, &derives, &attr_macros, &bang_macros)); - - krate -} - -fn is_proc_macro_attr(attr: &ast::Attribute) -> bool { - PROC_MACRO_KINDS.iter().any(|kind| attr.check_name(kind)) -} - -impl<'a> CollectProcMacros<'a> { - fn check_not_pub_in_root(&self, vis: &ast::Visibility, sp: Span) { - if self.is_proc_macro_crate && self.in_root && vis.node.is_pub() { - self.handler.span_err(sp, - "`proc-macro` crate types cannot \ - export any items other than functions \ - tagged with `#[proc_macro_derive]` currently"); - } - } - - fn collect_custom_derive(&mut self, item: &'a ast::Item, attr: &'a ast::Attribute) { - // Once we've located the `#[proc_macro_derive]` attribute, verify - // that it's of the form `#[proc_macro_derive(Foo)]` or - // `#[proc_macro_derive(Foo, attributes(A, ..))]` - let list = match attr.meta_item_list() { - Some(list) => list, - None => { - self.handler.span_err(attr.span(), - "attribute must be of form: \ - #[proc_macro_derive(TraitName)]"); - return - } - }; - if list.len() != 1 && list.len() != 2 { - self.handler.span_err(attr.span(), - "attribute must have either one or two arguments"); - return - } - let trait_attr = &list[0]; - let attributes_attr = list.get(1); - let trait_name = match trait_attr.name() { - Some(name) => name, - _ => { - self.handler.span_err(trait_attr.span(), "not a meta item"); - return - } - }; - if !trait_attr.is_word() { - self.handler.span_err(trait_attr.span(), "must only be one word"); - } - - if deriving::is_builtin_trait(trait_name) { - self.handler.span_err(trait_attr.span(), - "cannot override a built-in #[derive] mode"); - } - - let proc_attrs: Vec<_> = if let Some(attr) = attributes_attr { - if !attr.check_name("attributes") { - self.handler.span_err(attr.span(), "second argument must be `attributes`") - } - attr.meta_item_list().unwrap_or_else(|| { - self.handler.span_err(attr.span(), - "attribute must be of form: \ - `attributes(foo, bar)`"); - &[] - }).into_iter().filter_map(|attr| { - let name = match attr.name() { - Some(name) => name, - _ => { - self.handler.span_err(attr.span(), "not a meta item"); - return None; - }, - }; - - if !attr.is_word() { - self.handler.span_err(attr.span(), "must only be one word"); - return None; - } - - Some(name) - }).collect() - } else { - Vec::new() - }; - - if self.in_root && item.vis.node.is_pub() { - self.derives.push(ProcMacroDerive { - span: item.span, - trait_name, - function_name: item.ident, - attrs: proc_attrs, - }); - } else { - let msg = if !self.in_root { - "functions tagged with `#[proc_macro_derive]` must \ - currently reside in the root of the crate" - } else { - "functions tagged with `#[proc_macro_derive]` must be `pub`" - }; - self.handler.span_err(item.span, msg); - } - } - - fn collect_attr_proc_macro(&mut self, item: &'a ast::Item, attr: &'a ast::Attribute) { - if !attr.is_word() { - self.handler.span_err(attr.span, "`#[proc_macro_attribute]` attribute \ - does not take any arguments"); - return; - } - - if self.in_root && item.vis.node.is_pub() { - self.attr_macros.push(ProcMacroDef { - span: item.span, - function_name: item.ident, - }); - } else { - let msg = if !self.in_root { - "functions tagged with `#[proc_macro_attribute]` must \ - currently reside in the root of the crate" - } else { - "functions tagged with `#[proc_macro_attribute]` must be `pub`" - }; - self.handler.span_err(item.span, msg); - } - } - - fn collect_bang_proc_macro(&mut self, item: &'a ast::Item, attr: &'a ast::Attribute) { - if !attr.is_word() { - self.handler.span_err(attr.span, "`#[proc_macro]` attribute \ - does not take any arguments"); - return; - } - - if self.in_root && item.vis.node.is_pub() { - self.bang_macros.push(ProcMacroDef { - span: item.span, - function_name: item.ident, - }); - } else { - let msg = if !self.in_root { - "functions tagged with `#[proc_macro]` must \ - currently reside in the root of the crate" - } else { - "functions tagged with `#[proc_macro]` must be `pub`" - }; - self.handler.span_err(item.span, msg); - } - } -} - -impl<'a> Visitor<'a> for CollectProcMacros<'a> { - fn visit_item(&mut self, item: &'a ast::Item) { - if let ast::ItemKind::MacroDef(..) = item.node { - if self.is_proc_macro_crate && attr::contains_name(&item.attrs, "macro_export") { - let msg = - "cannot export macro_rules! macros from a `proc-macro` crate type currently"; - self.handler.span_err(item.span, msg); - } - } - - // First up, make sure we're checking a bare function. If we're not then - // we're just not interested in this item. - // - // If we find one, try to locate a `#[proc_macro_derive]` attribute on - // it. - let is_fn = match item.node { - ast::ItemKind::Fn(..) => true, - _ => false, - }; - - let mut found_attr: Option<&'a ast::Attribute> = None; - - for attr in &item.attrs { - if is_proc_macro_attr(&attr) { - if let Some(prev_attr) = found_attr { - let msg = if attr.path.segments[0].ident.name == - prev_attr.path.segments[0].ident.name { - format!("Only one `#[{}]` attribute is allowed on any given function", - attr.path) - } else { - format!("`#[{}]` and `#[{}]` attributes cannot both be applied \ - to the same function", attr.path, prev_attr.path) - }; - - self.handler.struct_span_err(attr.span(), &msg) - .span_note(prev_attr.span(), "Previous attribute here") - .emit(); - - return; - } - - found_attr = Some(attr); - } - } - - let attr = match found_attr { - None => { - self.check_not_pub_in_root(&item.vis, item.span); - let prev_in_root = mem::replace(&mut self.in_root, false); - visit::walk_item(self, item); - self.in_root = prev_in_root; - return; - }, - Some(attr) => attr, - }; - - if !is_fn { - let msg = format!("the `#[{}]` attribute may only be used on bare functions", - attr.path); - - self.handler.span_err(attr.span(), &msg); - return; - } - - if self.is_test_crate { - return; - } - - if !self.is_proc_macro_crate { - let msg = format!("the `#[{}]` attribute is only usable with crates of the \ - `proc-macro` crate type", attr.path); - - self.handler.span_err(attr.span(), &msg); - return; - } - - if attr.check_name("proc_macro_derive") { - self.collect_custom_derive(item, attr); - } else if attr.check_name("proc_macro_attribute") { - self.collect_attr_proc_macro(item, attr); - } else if attr.check_name("proc_macro") { - self.collect_bang_proc_macro(item, attr); - }; - - let prev_in_root = mem::replace(&mut self.in_root, false); - visit::walk_item(self, item); - self.in_root = prev_in_root; - } - - fn visit_mac(&mut self, mac: &ast::Mac) { - visit::walk_mac(self, mac) - } -} - -// Creates a new module which looks like: -// -// mod $gensym { -// extern crate proc_macro; -// -// use proc_macro::__internal::Registry; -// -// #[plugin_registrar] -// fn registrar(registrar: &mut Registry) { -// registrar.register_custom_derive($name_trait1, ::$name1, &[]); -// registrar.register_custom_derive($name_trait2, ::$name2, &["attribute_name"]); -// // ... -// } -// } -fn mk_registrar(cx: &mut ExtCtxt, - custom_derives: &[ProcMacroDerive], - custom_attrs: &[ProcMacroDef], - custom_macros: &[ProcMacroDef]) -> P { - let mark = Mark::fresh(Mark::root()); - mark.set_expn_info(ExpnInfo { - call_site: DUMMY_SP, - def_site: None, - format: MacroAttribute(Symbol::intern("proc_macro")), - allow_internal_unstable: true, - allow_internal_unsafe: false, - local_inner_macros: false, - edition: hygiene::default_edition(), - }); - let span = DUMMY_SP.apply_mark(mark); - - let proc_macro = Ident::from_str("proc_macro"); - let krate = cx.item(span, - proc_macro, - Vec::new(), - ast::ItemKind::ExternCrate(None)); - - let __internal = Ident::from_str("__internal"); - let registry = Ident::from_str("Registry"); - let registrar = Ident::from_str("_registrar"); - let register_custom_derive = Ident::from_str("register_custom_derive"); - let register_attr_proc_macro = Ident::from_str("register_attr_proc_macro"); - let register_bang_proc_macro = Ident::from_str("register_bang_proc_macro"); - let crate_kw = Ident::with_empty_ctxt(keywords::Crate.name()); - let local_path = |cx: &mut ExtCtxt, sp: Span, name: Ident| { - cx.path(sp.with_ctxt(span.ctxt()), vec![crate_kw, name]) - }; - - let mut stmts = custom_derives.iter().map(|cd| { - let path = local_path(cx, cd.span, cd.function_name); - let trait_name = cx.expr_str(cd.span, cd.trait_name); - let attrs = cx.expr_vec_slice( - span, - cd.attrs.iter().map(|&s| cx.expr_str(cd.span, s)).collect::>() - ); - let registrar = cx.expr_ident(span, registrar); - let ufcs_path = cx.path(span, vec![proc_macro, __internal, registry, - register_custom_derive]); - - cx.stmt_expr(cx.expr_call(span, cx.expr_path(ufcs_path), - vec![registrar, trait_name, cx.expr_path(path), attrs])) - - }).collect::>(); - - stmts.extend(custom_attrs.iter().map(|ca| { - let name = cx.expr_str(ca.span, ca.function_name.name); - let path = local_path(cx, ca.span, ca.function_name); - let registrar = cx.expr_ident(ca.span, registrar); - - let ufcs_path = cx.path(span, - vec![proc_macro, __internal, registry, register_attr_proc_macro]); - - cx.stmt_expr(cx.expr_call(span, cx.expr_path(ufcs_path), - vec![registrar, name, cx.expr_path(path)])) - })); - - stmts.extend(custom_macros.iter().map(|cm| { - let name = cx.expr_str(cm.span, cm.function_name.name); - let path = local_path(cx, cm.span, cm.function_name); - let registrar = cx.expr_ident(cm.span, registrar); - - let ufcs_path = cx.path(span, - vec![proc_macro, __internal, registry, register_bang_proc_macro]); - - cx.stmt_expr(cx.expr_call(span, cx.expr_path(ufcs_path), - vec![registrar, name, cx.expr_path(path)])) - })); - - let path = cx.path(span, vec![proc_macro, __internal, registry]); - let registrar_path = cx.ty_path(path); - let arg_ty = cx.ty_rptr(span, registrar_path, None, ast::Mutability::Mutable); - let func = cx.item_fn(span, - registrar, - vec![cx.arg(span, registrar, arg_ty)], - cx.ty(span, ast::TyKind::Tup(Vec::new())), - cx.block(span, stmts)); - - let derive_registrar = cx.meta_word(span, Symbol::intern("rustc_derive_registrar")); - let derive_registrar = cx.attribute(span, derive_registrar); - let func = func.map(|mut i| { - i.attrs.push(derive_registrar); - i.vis = respan(span, ast::VisibilityKind::Public); - i - }); - let ident = ast::Ident::with_empty_ctxt(Symbol::gensym("registrar")); - let module = cx.item_mod(span, span, ident, Vec::new(), vec![krate, func]).map(|mut i| { - i.vis = respan(span, ast::VisibilityKind::Public); - i - }); - - cx.monotonic_expander().fold_item(module).pop().unwrap() -} -- cgit 1.4.1-3-g733a5