]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass-fulldeps/auxiliary/macro_crate_test.rs
Use `Ident` instead of `Name` in `MetaItem`
[rust.git] / src / test / run-pass-fulldeps / auxiliary / macro_crate_test.rs
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.
4 //
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.
10
11 // force-host
12
13 #![feature(plugin_registrar, quote, rustc_private)]
14
15 extern crate syntax;
16 extern crate rustc;
17 extern crate rustc_plugin;
18 extern crate syntax_pos;
19
20 use syntax::ast::{self, Item, MetaItem, ItemKind};
21 use syntax::codemap::DUMMY_SP;
22 use syntax::ext::base::*;
23 use syntax::ext::quote::rt::ToTokens;
24 use syntax::parse::{self, token};
25 use syntax::ptr::P;
26 use syntax::symbol::Symbol;
27 use syntax::tokenstream::TokenTree;
28 use syntax_pos::Span;
29 use rustc_plugin::Registry;
30
31 #[macro_export]
32 macro_rules! exported_macro { () => (2) }
33 macro_rules! unexported_macro { () => (3) }
34
35 #[plugin_registrar]
36 pub fn plugin_registrar(reg: &mut Registry) {
37     reg.register_macro("make_a_1", expand_make_a_1);
38     reg.register_macro("identity", expand_identity);
39     reg.register_syntax_extension(
40         Symbol::intern("into_multi_foo"),
41         MultiModifier(Box::new(expand_into_foo_multi)));
42     reg.register_syntax_extension(
43         Symbol::intern("duplicate"),
44         MultiDecorator(Box::new(expand_duplicate)));
45     reg.register_syntax_extension(
46         Symbol::intern("caller"),
47         MultiDecorator(Box::new(expand_caller)));
48 }
49
50 fn expand_make_a_1(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree]) -> Box<MacResult + 'static> {
51     if !tts.is_empty() {
52         cx.span_fatal(sp, "make_a_1 takes no arguments");
53     }
54     MacEager::expr(quote_expr!(cx, 1))
55 }
56
57 // See Issue #15750
58 fn expand_identity(cx: &mut ExtCtxt, _span: Span, tts: &[TokenTree]) -> 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))
63 }
64
65 fn expand_into_foo_multi(cx: &mut ExtCtxt,
66                          _sp: Span,
67                          _attr: &MetaItem,
68                          it: Annotatable)
69                          -> Vec<Annotatable> {
70     match it {
71         Annotatable::Item(it) => vec![
72             Annotatable::Item(P(Item {
73                 attrs: it.attrs.clone(),
74                 ..(*quote_item!(cx, enum Foo2 { Bar2, Baz2 }).unwrap()).clone()
75             })),
76             Annotatable::Item(quote_item!(cx, enum Foo3 { Bar }).unwrap()),
77             Annotatable::Item(quote_item!(cx, #[cfg(any())] fn foo2() {}).unwrap()),
78         ],
79         Annotatable::ImplItem(_it) => vec![
80             quote_item!(cx, impl X { fn foo(&self) -> i32 { 42 } }).unwrap().and_then(|i| {
81                 match i.node {
82                     ItemKind::Impl(.., mut items) => {
83                         Annotatable::ImplItem(P(items.pop().expect("impl method not found")))
84                     }
85                     _ => unreachable!("impl parsed to something other than impl")
86                 }
87             })
88         ],
89         Annotatable::TraitItem(_it) => vec![
90             quote_item!(cx, trait X { fn foo(&self) -> i32 { 0 } }).unwrap().and_then(|i| {
91                 match i.node {
92                     ItemKind::Trait(.., mut items) => {
93                         Annotatable::TraitItem(P(items.pop().expect("trait method not found")))
94                     }
95                     _ => unreachable!("trait parsed to something other than trait")
96                 }
97             })
98         ],
99         // covered in proc_macro/macros-in-extern.rs
100         Annotatable::ForeignItem(..) => unimplemented!(),
101         // covered in proc_macro/attr-stmt-expr.rs
102         Annotatable::Stmt(_) | Annotatable::Expr(_) => panic!("expected item"),
103     }
104 }
105
106 // Create a duplicate of the annotatable, based on the MetaItem
107 fn expand_duplicate(cx: &mut ExtCtxt,
108                     _sp: Span,
109                     mi: &MetaItem,
110                     it: &Annotatable,
111                     push: &mut FnMut(Annotatable)) {
112     let copy_name = match mi.node {
113         ast::MetaItemKind::List(ref xs) => {
114             if let Some(word) = xs[0].word() {
115                 word.ident
116             } else {
117                 cx.span_err(mi.span, "Expected word");
118                 return;
119             }
120         }
121         _ => {
122             cx.span_err(mi.span, "Expected list");
123             return;
124         }
125     };
126
127     // Duplicate the item but replace its ident by the MetaItem
128     match it.clone() {
129         Annotatable::Item(it) => {
130             let mut new_it = (*it).clone();
131             new_it.attrs.clear();
132             new_it.ident = copy_name;
133             push(Annotatable::Item(P(new_it)));
134         }
135         Annotatable::ImplItem(it) => {
136             let mut new_it = (*it).clone();
137             new_it.attrs.clear();
138             new_it.ident = copy_name;
139             push(Annotatable::ImplItem(P(new_it)));
140         }
141         Annotatable::TraitItem(tt) => {
142             let mut new_it = (*tt).clone();
143             new_it.attrs.clear();
144             new_it.ident = copy_name;
145             push(Annotatable::TraitItem(P(new_it)));
146         }
147         // covered in proc_macro/macros-in-extern.rs
148         Annotatable::ForeignItem(..) => unimplemented!(),
149         // covered in proc_macro/attr-stmt-expr.rs
150         Annotatable::Stmt(_) | Annotatable::Expr(_) => panic!("expected item")
151     }
152 }
153
154 pub fn token_separate<T: ToTokens>(ecx: &ExtCtxt, things: &[T],
155                                    token: token::Token) -> Vec<TokenTree> {
156     let mut output: Vec<TokenTree> = vec![];
157     for (i, thing) in things.iter().enumerate() {
158         output.extend(thing.to_tokens(ecx));
159         if i < things.len() - 1 {
160             output.push(TokenTree::Token(DUMMY_SP, token.clone()));
161         }
162     }
163
164     output
165 }
166
167 fn expand_caller(cx: &mut ExtCtxt,
168                  sp: Span,
169                  mi: &MetaItem,
170                  it: &Annotatable,
171                  push: &mut FnMut(Annotatable)) {
172     let (orig_fn_name, ret_type) = match *it {
173         Annotatable::Item(ref item) => match item.node {
174             ItemKind::Fn(ref decl, ..) => {
175                 (item.ident, &decl.output)
176             }
177             _ => cx.span_fatal(item.span, "Only functions with return types can be annotated.")
178         },
179         _ => cx.span_fatal(sp, "Only functions can be annotated.")
180     };
181
182     let (caller_name, arguments) = if let Some(list) = mi.meta_item_list() {
183         if list.len() < 2 {
184             cx.span_fatal(mi.span(), "Need a function name and at least one parameter.");
185         }
186
187         let fn_name = match list[0].name() {
188             Some(name) => ast::Ident::with_empty_ctxt(name),
189             None => cx.span_fatal(list[0].span(), "First parameter must be an ident.")
190         };
191
192         (fn_name, &list[1..])
193     } else {
194         cx.span_fatal(mi.span, "Expected list.");
195     };
196
197     let literals: Vec<ast::Lit> = arguments.iter().map(|arg| {
198         if let Some(lit) = arg.literal() {
199             lit.clone()
200         } else {
201             cx.span_fatal(arg.span(), "Expected literal.");
202         }
203     }).collect();
204
205     let arguments = token_separate(cx, literals.as_slice(), token::Comma);
206     if let ast::FunctionRetTy::Ty(ref rt) = *ret_type {
207         push(Annotatable::Item(quote_item!(cx,
208                                            fn $caller_name() -> $rt {
209                                                $orig_fn_name($arguments)
210                                            }).unwrap()))
211     } else {
212         push(Annotatable::Item(quote_item!(cx,
213                                            fn $caller_name() {
214                                                $orig_fn_name($arguments)
215                                            }).unwrap()))
216     }
217 }
218
219 pub fn foo() {}