3 #![feature(plugin_registrar, quote, rustc_private)]
6 extern crate syntax_pos;
8 extern crate rustc_plugin;
10 use syntax::ast::{self, Item, MetaItem, ItemKind};
11 use syntax::ext::base::*;
14 use syntax::symbol::Symbol;
15 use syntax::tokenstream::TokenTree;
17 use rustc_plugin::Registry;
20 macro_rules! exported_macro { () => (2) }
21 macro_rules! unexported_macro { () => (3) }
24 pub fn plugin_registrar(reg: &mut Registry) {
25 reg.register_macro("make_a_1", expand_make_a_1);
26 reg.register_macro("identity", expand_identity);
27 reg.register_syntax_extension(
28 Symbol::intern("into_multi_foo"),
29 MultiModifier(Box::new(expand_into_foo_multi)));
30 reg.register_syntax_extension(
31 Symbol::intern("noop_attribute"),
32 MultiModifier(Box::new(expand_noop_attribute)));
33 reg.register_syntax_extension(
34 Symbol::intern("duplicate"),
35 MultiDecorator(Box::new(expand_duplicate)));
38 fn expand_make_a_1(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
39 -> Box<MacResult+'static> {
41 cx.span_fatal(sp, "make_a_1 takes no arguments");
43 MacEager::expr(quote_expr!(cx, 1))
47 fn expand_identity(cx: &mut ExtCtxt, _span: Span, tts: &[TokenTree])
48 -> Box<MacResult+'static> {
49 // Parse an expression and emit it unchanged.
50 let mut parser = parse::new_parser_from_tts(cx.parse_sess(), tts.to_vec());
51 let expr = parser.parse_expr().unwrap();
52 MacEager::expr(quote_expr!(&mut *cx, $expr))
55 fn expand_into_foo_multi(cx: &mut ExtCtxt,
58 it: Annotatable) -> Annotatable {
60 Annotatable::Item(it) => {
61 Annotatable::Item(P(Item {
62 attrs: it.attrs.clone(),
63 ..(*quote_item!(cx, enum Foo2 { Bar2, Baz2 }).unwrap()).clone()
66 Annotatable::ImplItem(_) => {
67 quote_item!(cx, impl X { fn foo(&self) -> i32 { 42 } }).unwrap().and_then(|i| {
69 ItemKind::Impl(.., mut items) => {
70 Annotatable::ImplItem(P(items.pop().expect("impl method not found")))
72 _ => unreachable!("impl parsed to something other than impl")
76 Annotatable::TraitItem(_) => {
77 quote_item!(cx, trait X { fn foo(&self) -> i32 { 0 } }).unwrap().and_then(|i| {
79 ItemKind::Trait(.., mut items) => {
80 Annotatable::TraitItem(P(items.pop().expect("trait method not found")))
82 _ => unreachable!("trait parsed to something other than trait")
86 // covered in proc_macro/macros-in-extern.rs
87 Annotatable::ForeignItem(_) => unimplemented!(),
88 // covered in proc_macro/attr-stmt-expr.rs
89 Annotatable::Stmt(_) | Annotatable::Expr(_) => panic!("expected item")
93 fn expand_noop_attribute(_cx: &mut ExtCtxt,
96 it: Annotatable) -> Annotatable {
100 // Create a duplicate of the annotatable, based on the MetaItem
101 fn expand_duplicate(cx: &mut ExtCtxt,
105 push: &mut FnMut(Annotatable))
107 let copy_name = match mi.node {
108 ast::MetaItemKind::List(ref xs) => {
109 if let Some(word) = xs[0].word() {
110 word.ident.segments.last().unwrap().ident
112 cx.span_err(mi.span, "Expected word");
117 cx.span_err(mi.span, "Expected list");
122 // Duplicate the item but replace its ident by the MetaItem
124 Annotatable::Item(it) => {
125 let mut new_it = (*it).clone();
126 new_it.attrs.clear();
127 new_it.ident = copy_name;
128 push(Annotatable::Item(P(new_it)));
130 Annotatable::ImplItem(it) => {
131 let mut new_it = (*it).clone();
132 new_it.attrs.clear();
133 new_it.ident = copy_name;
134 push(Annotatable::ImplItem(P(new_it)));
136 Annotatable::TraitItem(tt) => {
137 let mut new_it = (*tt).clone();
138 new_it.attrs.clear();
139 new_it.ident = copy_name;
140 push(Annotatable::TraitItem(P(new_it)));
142 // covered in proc_macro/macros-in-extern.rs
143 Annotatable::ForeignItem(_) => unimplemented!(),
144 // covered in proc_macro/attr-stmt-expr.rs
145 Annotatable::Stmt(_) | Annotatable::Expr(_) => panic!("expected item")