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)]
16 extern crate syntax_pos;
18 extern crate rustc_plugin;
20 use syntax::ast::{self, Item, MetaItem, ItemKind};
21 use syntax::ext::base::*;
24 use syntax::symbol::Symbol;
25 use syntax::tokenstream::TokenTree;
27 use rustc_plugin::Registry;
30 macro_rules! exported_macro { () => (2) }
31 macro_rules! unexported_macro { () => (3) }
34 pub fn plugin_registrar(reg: &mut Registry) {
35 reg.register_macro("make_a_1", expand_make_a_1);
36 reg.register_macro("identity", expand_identity);
37 reg.register_syntax_extension(
38 Symbol::intern("into_multi_foo"),
39 MultiModifier(Box::new(expand_into_foo_multi)));
40 reg.register_syntax_extension(
41 Symbol::intern("noop_attribute"),
42 MultiModifier(Box::new(expand_noop_attribute)));
43 reg.register_syntax_extension(
44 Symbol::intern("duplicate"),
45 MultiDecorator(Box::new(expand_duplicate)));
48 fn expand_make_a_1(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
49 -> Box<MacResult+'static> {
51 cx.span_fatal(sp, "make_a_1 takes no arguments");
53 MacEager::expr(quote_expr!(cx, 1))
57 fn expand_identity(cx: &mut ExtCtxt, _span: Span, tts: &[TokenTree])
58 -> Box<MacResult+'static> {
59 // Parse an expression and emit it unchanged.
60 let mut parser = parse::new_parser_from_tts(cx.parse_sess(), tts.to_vec());
61 let expr = parser.parse_expr().unwrap();
62 MacEager::expr(quote_expr!(&mut *cx, $expr))
65 fn expand_into_foo_multi(cx: &mut ExtCtxt,
68 it: Annotatable) -> Annotatable {
70 Annotatable::Item(it) => {
71 Annotatable::Item(P(Item {
72 attrs: it.attrs.clone(),
73 ..(*quote_item!(cx, enum Foo2 { Bar2, Baz2 }).unwrap()).clone()
76 Annotatable::ImplItem(_) => {
77 quote_item!(cx, impl X { fn foo(&self) -> i32 { 42 } }).unwrap().and_then(|i| {
79 ItemKind::Impl(.., mut items) => {
80 Annotatable::ImplItem(P(items.pop().expect("impl method not found")))
82 _ => unreachable!("impl parsed to something other than impl")
86 Annotatable::TraitItem(_) => {
87 quote_item!(cx, trait X { fn foo(&self) -> i32 { 0 } }).unwrap().and_then(|i| {
89 ItemKind::Trait(.., mut items) => {
90 Annotatable::TraitItem(P(items.pop().expect("trait method not found")))
92 _ => unreachable!("trait parsed to something other than trait")
96 // covered in proc_macro/macros-in-extern.rs
97 Annotatable::ForeignItem(_) => unimplemented!(),
98 // covered in proc_macro/attr-stmt-expr.rs
99 Annotatable::Stmt(_) | Annotatable::Expr(_) => panic!("expected item")
103 fn expand_noop_attribute(_cx: &mut ExtCtxt,
106 it: Annotatable) -> Annotatable {
110 // Create a duplicate of the annotatable, based on the MetaItem
111 fn expand_duplicate(cx: &mut ExtCtxt,
115 push: &mut FnMut(Annotatable))
117 let copy_name = match mi.node {
118 ast::MetaItemKind::List(ref xs) => {
119 if let Some(word) = xs[0].word() {
120 word.ident.segments.last().unwrap().ident
122 cx.span_err(mi.span, "Expected word");
127 cx.span_err(mi.span, "Expected list");
132 // Duplicate the item but replace its ident by the MetaItem
134 Annotatable::Item(it) => {
135 let mut new_it = (*it).clone();
136 new_it.attrs.clear();
137 new_it.ident = copy_name;
138 push(Annotatable::Item(P(new_it)));
140 Annotatable::ImplItem(it) => {
141 let mut new_it = (*it).clone();
142 new_it.attrs.clear();
143 new_it.ident = copy_name;
144 push(Annotatable::ImplItem(P(new_it)));
146 Annotatable::TraitItem(tt) => {
147 let mut new_it = (*tt).clone();
148 new_it.attrs.clear();
149 new_it.ident = copy_name;
150 push(Annotatable::TraitItem(P(new_it)));
152 // covered in proc_macro/macros-in-extern.rs
153 Annotatable::ForeignItem(_) => unimplemented!(),
154 // covered in proc_macro/attr-stmt-expr.rs
155 Annotatable::Stmt(_) | Annotatable::Expr(_) => panic!("expected item")