1 // Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 #![feature(plugin_registrar, quote, rustc_private)]
18 use syntax::ast::{self, TokenTree, Item, MetaItem, ImplItem, TraitItem};
19 use syntax::codemap::Span;
20 use syntax::ext::base::*;
21 use syntax::parse::{self, token};
23 use rustc::plugin::Registry;
26 macro_rules! exported_macro { () => (2) }
27 macro_rules! unexported_macro { () => (3) }
30 pub fn plugin_registrar(reg: &mut Registry) {
31 reg.register_macro("make_a_1", expand_make_a_1);
32 reg.register_macro("identity", expand_identity);
33 reg.register_syntax_extension(
34 token::intern("into_multi_foo"),
35 // FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
36 MultiModifier(Box::new(expand_into_foo_multi)));
37 reg.register_syntax_extension(
38 token::intern("duplicate"),
39 // FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
40 MultiDecorator(Box::new(expand_duplicate)));
43 fn expand_make_a_1(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
44 -> Box<MacResult+'static> {
46 cx.span_fatal(sp, "make_a_1 takes no arguments");
48 MacEager::expr(quote_expr!(cx, 1))
52 fn expand_identity(cx: &mut ExtCtxt, _span: Span, tts: &[TokenTree])
53 -> Box<MacResult+'static> {
54 // Parse an expression and emit it unchanged.
55 let mut parser = parse::new_parser_from_tts(cx.parse_sess(),
56 cx.cfg(), tts.to_vec());
57 let expr = parser.parse_expr();
58 MacEager::expr(quote_expr!(&mut *cx, $expr))
61 fn expand_into_foo_multi(cx: &mut ExtCtxt,
64 it: Annotatable) -> Annotatable {
66 Annotatable::Item(it) => {
67 Annotatable::Item(P(Item {
68 attrs: it.attrs.clone(),
69 ..(*quote_item!(cx, enum Foo2 { Bar2, Baz2 }).unwrap()).clone()
72 Annotatable::ImplItem(it) => {
73 quote_item!(cx, impl X { fn foo(&self) -> i32 { 42 } }).unwrap().and_then(|i| {
75 ast::ItemImpl(_, _, _, _, _, mut items) => {
76 Annotatable::ImplItem(items.pop().expect("impl method not found"))
78 _ => unreachable!("impl parsed to something other than impl")
82 Annotatable::TraitItem(it) => {
83 quote_item!(cx, trait X { fn foo(&self) -> i32 { 0 } }).unwrap().and_then(|i| {
85 ast::ItemTrait(_, _, _, mut items) => {
86 Annotatable::TraitItem(items.pop().expect("trait method not found"))
88 _ => unreachable!("trait parsed to something other than trait")
95 // Create a duplicate of the annotatable, based on the MetaItem
96 fn expand_duplicate(cx: &mut ExtCtxt,
100 push: &mut FnMut(Annotatable))
102 let copy_name = match mi.node {
103 ast::MetaItem_::MetaList(_, ref xs) => {
104 if let ast::MetaItem_::MetaWord(ref w) = xs[0].node {
105 token::str_to_ident(&w)
107 cx.span_err(mi.span, "Expected word");
112 cx.span_err(mi.span, "Expected list");
117 // Duplicate the item but replace its ident by the MetaItem
119 Annotatable::Item(it) => {
120 let mut new_it = (*it).clone();
121 new_it.attrs.clear();
122 new_it.ident = copy_name;
123 push(Annotatable::Item(P(new_it)));
125 Annotatable::ImplItem(it) => {
126 let mut new_it = (*it).clone();
127 new_it.attrs.clear();
128 new_it.ident = copy_name;
129 push(Annotatable::ImplItem(P(new_it)));
131 Annotatable::TraitItem(tt) => {
132 let mut new_it = (*tt).clone();
133 new_it.attrs.clear();
134 new_it.ident = copy_name;
135 push(Annotatable::TraitItem(P(new_it)));