]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/expand.rs
3459caecb260a21c5c9aa06e7e806f28f989e116
[rust.git] / src / libsyntax / ext / expand.rs
1 // Copyright 2012-2014 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 use ast::{Block, Crate, DeclLocal, ExprMac, PatMac};
12 use ast::{Local, Ident, MacInvocTT};
13 use ast::{ItemMac, MacStmtWithSemicolon, Mrk, Stmt, StmtDecl, StmtMac};
14 use ast::{StmtExpr, StmtSemi};
15 use ast::TokenTree;
16 use ast;
17 use ext::mtwt;
18 use ext::build::AstBuilder;
19 use attr;
20 use attr::AttrMetaMethods;
21 use codemap;
22 use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
23 use codemap::{CompilerExpansion, CompilerExpansionFormat};
24 use ext::base::*;
25 use feature_gate::{self, Features, GatedCfg};
26 use fold;
27 use fold::*;
28 use parse;
29 use parse::token::{fresh_mark, fresh_name, intern};
30 use parse::token;
31 use ptr::P;
32 use util::small_vector::SmallVector;
33 use visit;
34 use visit::Visitor;
35 use std_inject;
36
37 // Given suffix ["b","c","d"], returns path `::std::b::c::d` when
38 // `fld.cx.use_std`, and `::core::b::c::d` otherwise.
39 fn mk_core_path(fld: &mut MacroExpander,
40                 span: Span,
41                 suffix: &[&'static str]) -> ast::Path {
42     let idents = fld.cx.std_path(suffix);
43     fld.cx.path_global(span, idents)
44 }
45
46 pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
47     fn push_compiler_expansion(fld: &mut MacroExpander, span: Span,
48                                expansion_type: CompilerExpansionFormat) {
49         fld.cx.bt_push(ExpnInfo {
50             call_site: span,
51             callee: NameAndSpan {
52                 format: CompilerExpansion(expansion_type),
53
54                 // This does *not* mean code generated after
55                 // `push_compiler_expansion` is automatically exempt
56                 // from stability lints; must also tag such code with
57                 // an appropriate span from `fld.cx.backtrace()`.
58                 allow_internal_unstable: true,
59
60                 span: None,
61             },
62         });
63     }
64
65     // Sets the expn_id so that we can use unstable methods.
66     fn allow_unstable(fld: &mut MacroExpander, span: Span) -> Span {
67         Span { expn_id: fld.cx.backtrace(), ..span }
68     }
69
70     let expr_span = e.span;
71     return e.and_then(|ast::Expr {id, node, span}| match node {
72
73         // expr_mac should really be expr_ext or something; it's the
74         // entry-point for all syntax extensions.
75         ast::ExprMac(mac) => {
76             let expanded_expr = match expand_mac_invoc(mac, span,
77                                                        |r| r.make_expr(),
78                                                        mark_expr, fld) {
79                 Some(expr) => expr,
80                 None => {
81                     return DummyResult::raw_expr(span);
82                 }
83             };
84
85             // Keep going, outside-in.
86             let fully_expanded = fld.fold_expr(expanded_expr);
87             let span = fld.new_span(span);
88             fld.cx.bt_pop();
89
90             fully_expanded.map(|e| ast::Expr {
91                 id: ast::DUMMY_NODE_ID,
92                 node: e.node,
93                 span: span,
94             })
95         }
96
97         // Desugar ExprBox: `in (PLACE) EXPR`
98         ast::ExprBox(Some(placer), value_expr) => {
99             // to:
100             //
101             // let p = PLACE;
102             // let mut place = Placer::make_place(p);
103             // let raw_place = Place::pointer(&mut place);
104             // push_unsafe!({
105             //     std::intrinsics::move_val_init(raw_place, pop_unsafe!( EXPR ));
106             //     InPlace::finalize(place)
107             // })
108
109             // Ensure feature-gate is enabled
110             feature_gate::check_for_placement_in(
111                 fld.cx.ecfg.features,
112                 &fld.cx.parse_sess.span_diagnostic,
113                 expr_span);
114
115             push_compiler_expansion(fld, expr_span, CompilerExpansionFormat::PlacementIn);
116
117             let value_span = value_expr.span;
118             let placer_span = placer.span;
119
120             let placer_expr = fld.fold_expr(placer);
121             let value_expr = fld.fold_expr(value_expr);
122
123             let placer_ident = token::gensym_ident("placer");
124             let agent_ident = token::gensym_ident("place");
125             let p_ptr_ident = token::gensym_ident("p_ptr");
126
127             let placer = fld.cx.expr_ident(span, placer_ident);
128             let agent = fld.cx.expr_ident(span, agent_ident);
129             let p_ptr = fld.cx.expr_ident(span, p_ptr_ident);
130
131             let make_place = ["ops", "Placer", "make_place"];
132             let place_pointer = ["ops", "Place", "pointer"];
133             let move_val_init = ["intrinsics", "move_val_init"];
134             let inplace_finalize = ["ops", "InPlace", "finalize"];
135
136             let make_call = |fld: &mut MacroExpander, p, args| {
137                 // We feed in the `expr_span` because codemap's span_allows_unstable
138                 // allows the call_site span to inherit the `allow_internal_unstable`
139                 // setting.
140                 let span_unstable = allow_unstable(fld, expr_span);
141                 let path = mk_core_path(fld, span_unstable, p);
142                 let path = fld.cx.expr_path(path);
143                 let expr_span_unstable = allow_unstable(fld, span);
144                 fld.cx.expr_call(expr_span_unstable, path, args)
145             };
146
147             let stmt_let = |fld: &mut MacroExpander, bind, expr| {
148                 fld.cx.stmt_let(placer_span, false, bind, expr)
149             };
150             let stmt_let_mut = |fld: &mut MacroExpander, bind, expr| {
151                 fld.cx.stmt_let(placer_span, true, bind, expr)
152             };
153
154             // let placer = <placer_expr> ;
155             let s1 = stmt_let(fld, placer_ident, placer_expr);
156
157             // let mut place = Placer::make_place(placer);
158             let s2 = {
159                 let call = make_call(fld, &make_place, vec![placer]);
160                 stmt_let_mut(fld, agent_ident, call)
161             };
162
163             // let p_ptr = Place::pointer(&mut place);
164             let s3 = {
165                 let args = vec![fld.cx.expr_mut_addr_of(placer_span, agent.clone())];
166                 let call = make_call(fld, &place_pointer, args);
167                 stmt_let(fld, p_ptr_ident, call)
168             };
169
170             // pop_unsafe!(EXPR));
171             let pop_unsafe_expr = pop_unsafe_expr(fld.cx, value_expr, value_span);
172
173             // push_unsafe!({
174             //     ptr::write(p_ptr, pop_unsafe!(<value_expr>));
175             //     InPlace::finalize(place)
176             // })
177             let expr = {
178                 let call_move_val_init = StmtSemi(make_call(
179                     fld, &move_val_init, vec![p_ptr, pop_unsafe_expr]), ast::DUMMY_NODE_ID);
180                 let call_move_val_init = codemap::respan(value_span, call_move_val_init);
181
182                 let call = make_call(fld, &inplace_finalize, vec![agent]);
183                 Some(push_unsafe_expr(fld.cx, vec![P(call_move_val_init)], call, span))
184             };
185
186             let block = fld.cx.block_all(span, vec![s1, s2, s3], expr);
187             let result = fld.cx.expr_block(block);
188             fld.cx.bt_pop();
189             result
190         }
191
192         // Issue #22181:
193         // Eventually a desugaring for `box EXPR`
194         // (similar to the desugaring above for `in PLACE BLOCK`)
195         // should go here, desugaring
196         //
197         // to:
198         //
199         // let mut place = BoxPlace::make_place();
200         // let raw_place = Place::pointer(&mut place);
201         // let value = $value;
202         // unsafe {
203         //     ::std::ptr::write(raw_place, value);
204         //     Boxed::finalize(place)
205         // }
206         //
207         // But for now there are type-inference issues doing that.
208
209         ast::ExprWhile(cond, body, opt_ident) => {
210             let cond = fld.fold_expr(cond);
211             let (body, opt_ident) = expand_loop_block(body, opt_ident, fld);
212             fld.cx.expr(span, ast::ExprWhile(cond, body, opt_ident))
213         }
214
215         // Desugar ExprWhileLet
216         // From: `[opt_ident]: while let <pat> = <expr> <body>`
217         ast::ExprWhileLet(pat, expr, body, opt_ident) => {
218             // to:
219             //
220             //   [opt_ident]: loop {
221             //     match <expr> {
222             //       <pat> => <body>,
223             //       _ => break
224             //     }
225             //   }
226
227             push_compiler_expansion(fld, span, CompilerExpansionFormat::WhileLet);
228
229             // `<pat> => <body>`
230             let pat_arm = {
231                 let body_expr = fld.cx.expr_block(body);
232                 fld.cx.arm(pat.span, vec![pat], body_expr)
233             };
234
235             // `_ => break`
236             let break_arm = {
237                 let pat_under = fld.cx.pat_wild(span);
238                 let break_expr = fld.cx.expr_break(span);
239                 fld.cx.arm(span, vec![pat_under], break_expr)
240             };
241
242             // `match <expr> { ... }`
243             let arms = vec![pat_arm, break_arm];
244             let match_expr = fld.cx.expr(span,
245                                     ast::ExprMatch(expr, arms, ast::MatchSource::WhileLetDesugar));
246
247             // `[opt_ident]: loop { ... }`
248             let loop_block = fld.cx.block_expr(match_expr);
249             let (loop_block, opt_ident) = expand_loop_block(loop_block, opt_ident, fld);
250             let result = fld.cx.expr(span, ast::ExprLoop(loop_block, opt_ident));
251             fld.cx.bt_pop();
252             result
253         }
254
255         // Desugar ExprIfLet
256         // From: `if let <pat> = <expr> <body> [<elseopt>]`
257         ast::ExprIfLet(pat, expr, body, mut elseopt) => {
258             // to:
259             //
260             //   match <expr> {
261             //     <pat> => <body>,
262             //     [_ if <elseopt_if_cond> => <elseopt_if_body>,]
263             //     _ => [<elseopt> | ()]
264             //   }
265
266             push_compiler_expansion(fld, span, CompilerExpansionFormat::IfLet);
267
268             // `<pat> => <body>`
269             let pat_arm = {
270                 let body_expr = fld.cx.expr_block(body);
271                 fld.cx.arm(pat.span, vec![pat], body_expr)
272             };
273
274             // `[_ if <elseopt_if_cond> => <elseopt_if_body>,]`
275             let else_if_arms = {
276                 let mut arms = vec![];
277                 loop {
278                     let elseopt_continue = elseopt
279                         .and_then(|els| els.and_then(|els| match els.node {
280                         // else if
281                         ast::ExprIf(cond, then, elseopt) => {
282                             let pat_under = fld.cx.pat_wild(span);
283                             arms.push(ast::Arm {
284                                 attrs: vec![],
285                                 pats: vec![pat_under],
286                                 guard: Some(cond),
287                                 body: fld.cx.expr_block(then)
288                             });
289                             elseopt.map(|elseopt| (elseopt, true))
290                         }
291                         _ => Some((P(els), false))
292                     }));
293                     match elseopt_continue {
294                         Some((e, true)) => {
295                             elseopt = Some(e);
296                         }
297                         Some((e, false)) => {
298                             elseopt = Some(e);
299                             break;
300                         }
301                         None => {
302                             elseopt = None;
303                             break;
304                         }
305                     }
306                 }
307                 arms
308             };
309
310             let contains_else_clause = elseopt.is_some();
311
312             // `_ => [<elseopt> | ()]`
313             let else_arm = {
314                 let pat_under = fld.cx.pat_wild(span);
315                 let else_expr = elseopt.unwrap_or_else(|| fld.cx.expr_tuple(span, vec![]));
316                 fld.cx.arm(span, vec![pat_under], else_expr)
317             };
318
319             let mut arms = Vec::with_capacity(else_if_arms.len() + 2);
320             arms.push(pat_arm);
321             arms.extend(else_if_arms);
322             arms.push(else_arm);
323
324             let match_expr = fld.cx.expr(span,
325                                          ast::ExprMatch(expr, arms,
326                                                 ast::MatchSource::IfLetDesugar {
327                                                     contains_else_clause: contains_else_clause,
328                                                 }));
329             let result = fld.fold_expr(match_expr);
330             fld.cx.bt_pop();
331             result
332         }
333
334         // Desugar support for ExprIfLet in the ExprIf else position
335         ast::ExprIf(cond, blk, elseopt) => {
336             let elseopt = elseopt.map(|els| els.and_then(|els| match els.node {
337                 ast::ExprIfLet(..) => {
338                     push_compiler_expansion(fld, span, CompilerExpansionFormat::IfLet);
339                     // wrap the if-let expr in a block
340                     let span = els.span;
341                     let blk = P(ast::Block {
342                         stmts: vec![],
343                         expr: Some(P(els)),
344                         id: ast::DUMMY_NODE_ID,
345                         rules: ast::DefaultBlock,
346                         span: span
347                     });
348                     let result = fld.cx.expr_block(blk);
349                     fld.cx.bt_pop();
350                     result
351                 }
352                 _ => P(els)
353             }));
354             let if_expr = fld.cx.expr(span, ast::ExprIf(cond, blk, elseopt));
355             if_expr.map(|e| noop_fold_expr(e, fld))
356         }
357
358         ast::ExprLoop(loop_block, opt_ident) => {
359             let (loop_block, opt_ident) = expand_loop_block(loop_block, opt_ident, fld);
360             fld.cx.expr(span, ast::ExprLoop(loop_block, opt_ident))
361         }
362
363         // Desugar ExprForLoop
364         // From: `[opt_ident]: for <pat> in <head> <body>`
365         ast::ExprForLoop(pat, head, body, opt_ident) => {
366             // to:
367             //
368             //   {
369             //     let result = match ::std::iter::IntoIterator::into_iter(<head>) {
370             //       mut iter => {
371             //         [opt_ident]: loop {
372             //           match ::std::iter::Iterator::next(&mut iter) {
373             //             ::std::option::Option::Some(<pat>) => <body>,
374             //             ::std::option::Option::None => break
375             //           }
376             //         }
377             //       }
378             //     };
379             //     result
380             //   }
381
382             push_compiler_expansion(fld, span, CompilerExpansionFormat::ForLoop);
383
384             let span = fld.new_span(span);
385
386             // expand <head>
387             let head = fld.fold_expr(head);
388
389             let iter = token::gensym_ident("iter");
390
391             let pat_span = fld.new_span(pat.span);
392             // `::std::option::Option::Some(<pat>) => <body>`
393             let pat_arm = {
394                 let body_expr = fld.cx.expr_block(body);
395                 let pat = fld.fold_pat(pat);
396                 let some_pat = fld.cx.pat_some(pat_span, pat);
397
398                 fld.cx.arm(pat_span, vec![some_pat], body_expr)
399             };
400
401             // `::std::option::Option::None => break`
402             let break_arm = {
403                 let break_expr = fld.cx.expr_break(span);
404
405                 fld.cx.arm(span, vec![fld.cx.pat_none(span)], break_expr)
406             };
407
408             // `match ::std::iter::Iterator::next(&mut iter) { ... }`
409             let match_expr = {
410                 let next_path = {
411                     let strs = fld.cx.std_path(&["iter", "Iterator", "next"]);
412
413                     fld.cx.path_global(span, strs)
414                 };
415                 let ref_mut_iter = fld.cx.expr_mut_addr_of(span, fld.cx.expr_ident(span, iter));
416                 let next_expr =
417                     fld.cx.expr_call(span, fld.cx.expr_path(next_path), vec![ref_mut_iter]);
418                 let arms = vec![pat_arm, break_arm];
419
420                 fld.cx.expr(pat_span,
421                             ast::ExprMatch(next_expr, arms, ast::MatchSource::ForLoopDesugar))
422             };
423
424             // `[opt_ident]: loop { ... }`
425             let loop_block = fld.cx.block_expr(match_expr);
426             let (loop_block, opt_ident) = expand_loop_block(loop_block, opt_ident, fld);
427             let loop_expr = fld.cx.expr(span, ast::ExprLoop(loop_block, opt_ident));
428
429             // `mut iter => { ... }`
430             let iter_arm = {
431                 let iter_pat =
432                     fld.cx.pat_ident_binding_mode(span, iter, ast::BindByValue(ast::MutMutable));
433                 fld.cx.arm(span, vec![iter_pat], loop_expr)
434             };
435
436             // `match ::std::iter::IntoIterator::into_iter(<head>) { ... }`
437             let into_iter_expr = {
438                 let into_iter_path = {
439                     let strs = fld.cx.std_path(&["iter", "IntoIterator",
440                                                  "into_iter"]);
441
442                     fld.cx.path_global(span, strs)
443                 };
444
445                 fld.cx.expr_call(span, fld.cx.expr_path(into_iter_path), vec![head])
446             };
447
448             let match_expr = fld.cx.expr_match(span, into_iter_expr, vec![iter_arm]);
449
450             // `{ let result = ...; result }`
451             let result_ident = token::gensym_ident("result");
452             let result = fld.cx.expr_block(
453                 fld.cx.block_all(
454                     span,
455                     vec![fld.cx.stmt_let(span, false, result_ident, match_expr)],
456                     Some(fld.cx.expr_ident(span, result_ident))));
457             fld.cx.bt_pop();
458             result
459         }
460
461         ast::ExprClosure(capture_clause, fn_decl, block) => {
462             push_compiler_expansion(fld, span, CompilerExpansionFormat::Closure);
463             let (rewritten_fn_decl, rewritten_block)
464                 = expand_and_rename_fn_decl_and_block(fn_decl, block, fld);
465             let new_node = ast::ExprClosure(capture_clause,
466                                             rewritten_fn_decl,
467                                             rewritten_block);
468             let result = P(ast::Expr{id:id, node: new_node, span: fld.new_span(span)});
469             fld.cx.bt_pop();
470             result
471         }
472
473         _ => {
474             P(noop_fold_expr(ast::Expr {
475                 id: id,
476                 node: node,
477                 span: span
478             }, fld))
479         }
480     });
481
482     fn push_unsafe_expr(cx: &mut ExtCtxt, stmts: Vec<P<ast::Stmt>>,
483                         expr: P<ast::Expr>, span: Span)
484                         -> P<ast::Expr> {
485         let rules = ast::PushUnsafeBlock(ast::CompilerGenerated);
486         cx.expr_block(P(ast::Block {
487             rules: rules, span: span, id: ast::DUMMY_NODE_ID,
488             stmts: stmts, expr: Some(expr),
489         }))
490     }
491
492     fn pop_unsafe_expr(cx: &mut ExtCtxt, expr: P<ast::Expr>, span: Span)
493                        -> P<ast::Expr> {
494         let rules = ast::PopUnsafeBlock(ast::CompilerGenerated);
495         cx.expr_block(P(ast::Block {
496             rules: rules, span: span, id: ast::DUMMY_NODE_ID,
497             stmts: vec![], expr: Some(expr),
498         }))
499     }
500 }
501
502 /// Expand a (not-ident-style) macro invocation. Returns the result
503 /// of expansion and the mark which must be applied to the result.
504 /// Our current interface doesn't allow us to apply the mark to the
505 /// result until after calling make_expr, make_items, etc.
506 fn expand_mac_invoc<T, F, G>(mac: ast::Mac,
507                              span: codemap::Span,
508                              parse_thunk: F,
509                              mark_thunk: G,
510                              fld: &mut MacroExpander)
511                              -> Option<T> where
512     F: for<'a> FnOnce(Box<MacResult+'a>) -> Option<T>,
513     G: FnOnce(T, Mrk) -> T,
514 {
515     match mac.node {
516         // it would almost certainly be cleaner to pass the whole
517         // macro invocation in, rather than pulling it apart and
518         // marking the tts and the ctxt separately. This also goes
519         // for the other three macro invocation chunks of code
520         // in this file.
521         // Token-tree macros:
522         MacInvocTT(pth, tts, _) => {
523             if pth.segments.len() > 1 {
524                 fld.cx.span_err(pth.span,
525                                 "expected macro name without module \
526                                 separators");
527                 // let compilation continue
528                 return None;
529             }
530             let extname = pth.segments[0].identifier.name;
531             match fld.cx.syntax_env.find(&extname) {
532                 None => {
533                     fld.cx.span_err(
534                         pth.span,
535                         &format!("macro undefined: '{}!'",
536                                 &extname));
537
538                     // let compilation continue
539                     None
540                 }
541                 Some(rc) => match *rc {
542                     NormalTT(ref expandfun, exp_span, allow_internal_unstable) => {
543                         fld.cx.bt_push(ExpnInfo {
544                                 call_site: span,
545                                 callee: NameAndSpan {
546                                     format: MacroBang(extname),
547                                     span: exp_span,
548                                     allow_internal_unstable: allow_internal_unstable,
549                                 },
550                             });
551                         let fm = fresh_mark();
552                         let marked_before = mark_tts(&tts[..], fm);
553
554                         // The span that we pass to the expanders we want to
555                         // be the root of the call stack. That's the most
556                         // relevant span and it's the actual invocation of
557                         // the macro.
558                         let mac_span = fld.cx.original_span();
559
560                         let opt_parsed = {
561                             let expanded = expandfun.expand(fld.cx,
562                                                             mac_span,
563                                                             &marked_before[..]);
564                             parse_thunk(expanded)
565                         };
566                         let parsed = match opt_parsed {
567                             Some(e) => e,
568                             None => {
569                                 fld.cx.span_err(
570                                     pth.span,
571                                     &format!("non-expression macro in expression position: {}",
572                                             extname
573                                             ));
574                                 return None;
575                             }
576                         };
577                         Some(mark_thunk(parsed,fm))
578                     }
579                     _ => {
580                         fld.cx.span_err(
581                             pth.span,
582                             &format!("'{}' is not a tt-style macro",
583                                     extname));
584                         None
585                     }
586                 }
587             }
588         }
589     }
590 }
591
592 /// Rename loop label and expand its loop body
593 ///
594 /// The renaming procedure for loop is different in the sense that the loop
595 /// body is in a block enclosed by loop head so the renaming of loop label
596 /// must be propagated to the enclosed context.
597 fn expand_loop_block(loop_block: P<Block>,
598                      opt_ident: Option<Ident>,
599                      fld: &mut MacroExpander) -> (P<Block>, Option<Ident>) {
600     match opt_ident {
601         Some(label) => {
602             let new_label = fresh_name(&label);
603             let rename = (label, new_label);
604
605             // The rename *must not* be added to the pending list of current
606             // syntax context otherwise an unrelated `break` or `continue` in
607             // the same context will pick that up in the deferred renaming pass
608             // and be renamed incorrectly.
609             let mut rename_list = vec!(rename);
610             let mut rename_fld = IdentRenamer{renames: &mut rename_list};
611             let renamed_ident = rename_fld.fold_ident(label);
612
613             // The rename *must* be added to the enclosed syntax context for
614             // `break` or `continue` to pick up because by definition they are
615             // in a block enclosed by loop head.
616             fld.cx.syntax_env.push_frame();
617             fld.cx.syntax_env.info().pending_renames.push(rename);
618             let expanded_block = expand_block_elts(loop_block, fld);
619             fld.cx.syntax_env.pop_frame();
620
621             (expanded_block, Some(renamed_ident))
622         }
623         None => (fld.fold_block(loop_block), opt_ident)
624     }
625 }
626
627 // eval $e with a new exts frame.
628 // must be a macro so that $e isn't evaluated too early.
629 macro_rules! with_exts_frame {
630     ($extsboxexpr:expr,$macros_escape:expr,$e:expr) =>
631     ({$extsboxexpr.push_frame();
632       $extsboxexpr.info().macros_escape = $macros_escape;
633       let result = $e;
634       $extsboxexpr.pop_frame();
635       result
636      })
637 }
638
639 // When we enter a module, record it, for the sake of `module!`
640 pub fn expand_item(it: P<ast::Item>, fld: &mut MacroExpander)
641                    -> SmallVector<P<ast::Item>> {
642     let it = expand_item_modifiers(it, fld);
643
644     expand_annotatable(Annotatable::Item(it), fld)
645         .into_iter().map(|i| i.expect_item()).collect()
646 }
647
648 /// Expand item_underscore
649 fn expand_item_underscore(item: ast::Item_, fld: &mut MacroExpander) -> ast::Item_ {
650     match item {
651         ast::ItemFn(decl, unsafety, constness, abi, generics, body) => {
652             let (rewritten_fn_decl, rewritten_body)
653                 = expand_and_rename_fn_decl_and_block(decl, body, fld);
654             let expanded_generics = fold::noop_fold_generics(generics,fld);
655             ast::ItemFn(rewritten_fn_decl, unsafety, constness, abi,
656                         expanded_generics, rewritten_body)
657         }
658         _ => noop_fold_item_underscore(item, fld)
659     }
660 }
661
662 // does this attribute list contain "macro_use" ?
663 fn contains_macro_use(fld: &mut MacroExpander, attrs: &[ast::Attribute]) -> bool {
664     for attr in attrs {
665         let mut is_use = attr.check_name("macro_use");
666         if attr.check_name("macro_escape") {
667             fld.cx.span_warn(attr.span, "macro_escape is a deprecated synonym for macro_use");
668             is_use = true;
669             if let ast::AttrInner = attr.node.style {
670                 fld.cx.fileline_help(attr.span, "consider an outer attribute, \
671                                              #[macro_use] mod ...");
672             }
673         };
674
675         if is_use {
676             match attr.node.value.node {
677                 ast::MetaWord(..) => (),
678                 _ => fld.cx.span_err(attr.span, "arguments to macro_use are not allowed here"),
679             }
680             return true;
681         }
682     }
683     false
684 }
685
686 // Support for item-position macro invocations, exactly the same
687 // logic as for expression-position macro invocations.
688 pub fn expand_item_mac(it: P<ast::Item>,
689                        fld: &mut MacroExpander) -> SmallVector<P<ast::Item>> {
690     let (extname, path_span, tts) = match it.node {
691         ItemMac(codemap::Spanned {
692             node: MacInvocTT(ref pth, ref tts, _),
693             ..
694         }) => {
695             (pth.segments[0].identifier.name, pth.span, (*tts).clone())
696         }
697         _ => fld.cx.span_bug(it.span, "invalid item macro invocation")
698     };
699
700     let fm = fresh_mark();
701     let items = {
702         let expanded = match fld.cx.syntax_env.find(&extname) {
703             None => {
704                 fld.cx.span_err(path_span,
705                                 &format!("macro undefined: '{}!'",
706                                         extname));
707                 // let compilation continue
708                 return SmallVector::zero();
709             }
710
711             Some(rc) => match *rc {
712                 NormalTT(ref expander, span, allow_internal_unstable) => {
713                     if it.ident.name != parse::token::special_idents::invalid.name {
714                         fld.cx
715                             .span_err(path_span,
716                                       &format!("macro {}! expects no ident argument, given '{}'",
717                                                extname,
718                                                it.ident));
719                         return SmallVector::zero();
720                     }
721                     fld.cx.bt_push(ExpnInfo {
722                         call_site: it.span,
723                         callee: NameAndSpan {
724                             format: MacroBang(extname),
725                             span: span,
726                             allow_internal_unstable: allow_internal_unstable,
727                         }
728                     });
729                     // mark before expansion:
730                     let marked_before = mark_tts(&tts[..], fm);
731                     expander.expand(fld.cx, it.span, &marked_before[..])
732                 }
733                 IdentTT(ref expander, span, allow_internal_unstable) => {
734                     if it.ident.name == parse::token::special_idents::invalid.name {
735                         fld.cx.span_err(path_span,
736                                         &format!("macro {}! expects an ident argument",
737                                                 extname));
738                         return SmallVector::zero();
739                     }
740                     fld.cx.bt_push(ExpnInfo {
741                         call_site: it.span,
742                         callee: NameAndSpan {
743                             format: MacroBang(extname),
744                             span: span,
745                             allow_internal_unstable: allow_internal_unstable,
746                         }
747                     });
748                     // mark before expansion:
749                     let marked_tts = mark_tts(&tts[..], fm);
750                     expander.expand(fld.cx, it.span, it.ident, marked_tts)
751                 }
752                 MacroRulesTT => {
753                     if it.ident.name == parse::token::special_idents::invalid.name {
754                         fld.cx.span_err(path_span,
755                                         &format!("macro_rules! expects an ident argument")
756                                         );
757                         return SmallVector::zero();
758                     }
759
760                     fld.cx.bt_push(ExpnInfo {
761                         call_site: it.span,
762                         callee: NameAndSpan {
763                             format: MacroBang(extname),
764                             span: None,
765                             // `macro_rules!` doesn't directly allow
766                             // unstable (this is orthogonal to whether
767                             // the macro it creates allows it)
768                             allow_internal_unstable: false,
769                         }
770                     });
771                     // DON'T mark before expansion.
772
773                     let allow_internal_unstable = attr::contains_name(&it.attrs,
774                                                                       "allow_internal_unstable");
775
776                     // ensure any #[allow_internal_unstable]s are
777                     // detected (including nested macro definitions
778                     // etc.)
779                     if allow_internal_unstable && !fld.cx.ecfg.enable_allow_internal_unstable() {
780                         feature_gate::emit_feature_err(
781                             &fld.cx.parse_sess.span_diagnostic,
782                             "allow_internal_unstable",
783                             it.span,
784                             feature_gate::EXPLAIN_ALLOW_INTERNAL_UNSTABLE)
785                     }
786
787                     let def = ast::MacroDef {
788                         ident: it.ident,
789                         attrs: it.attrs.clone(),
790                         id: ast::DUMMY_NODE_ID,
791                         span: it.span,
792                         imported_from: None,
793                         export: attr::contains_name(&it.attrs, "macro_export"),
794                         use_locally: true,
795                         allow_internal_unstable: allow_internal_unstable,
796                         body: tts,
797                     };
798                     fld.cx.insert_macro(def);
799
800                     // macro_rules! has a side effect but expands to nothing.
801                     fld.cx.bt_pop();
802                     return SmallVector::zero();
803                 }
804                 _ => {
805                     fld.cx.span_err(it.span,
806                                     &format!("{}! is not legal in item position",
807                                             extname));
808                     return SmallVector::zero();
809                 }
810             }
811         };
812
813         expanded.make_items()
814     };
815
816     let items = match items {
817         Some(items) => {
818             items.into_iter()
819                 .map(|i| mark_item(i, fm))
820                 .flat_map(|i| fld.fold_item(i).into_iter())
821                 .collect()
822         }
823         None => {
824             fld.cx.span_err(path_span,
825                             &format!("non-item macro in item position: {}",
826                                     extname));
827             return SmallVector::zero();
828         }
829     };
830
831     fld.cx.bt_pop();
832     items
833 }
834
835 /// Expand a stmt
836 fn expand_stmt(stmt: P<Stmt>, fld: &mut MacroExpander) -> SmallVector<P<Stmt>> {
837     let stmt = stmt.and_then(|stmt| stmt);
838     let (mac, style) = match stmt.node {
839         StmtMac(mac, style) => (mac, style),
840         _ => return expand_non_macro_stmt(stmt, fld)
841     };
842
843     let maybe_new_items =
844         expand_mac_invoc(mac.and_then(|m| m), stmt.span,
845                          |r| r.make_stmts(),
846                          |stmts, mark| stmts.move_map(|m| mark_stmt(m, mark)),
847                          fld);
848
849     let mut fully_expanded = match maybe_new_items {
850         Some(stmts) => {
851             // Keep going, outside-in.
852             let new_items = stmts.into_iter().flat_map(|s| {
853                 fld.fold_stmt(s).into_iter()
854             }).collect();
855             fld.cx.bt_pop();
856             new_items
857         }
858         None => SmallVector::zero()
859     };
860
861     // If this is a macro invocation with a semicolon, then apply that
862     // semicolon to the final statement produced by expansion.
863     if style == MacStmtWithSemicolon {
864         if let Some(stmt) = fully_expanded.pop() {
865             let new_stmt = stmt.map(|Spanned {node, span}| {
866                 Spanned {
867                     node: match node {
868                         StmtExpr(e, stmt_id) => StmtSemi(e, stmt_id),
869                         _ => node /* might already have a semi */
870                     },
871                     span: span
872                 }
873             });
874             fully_expanded.push(new_stmt);
875         }
876     }
877
878     fully_expanded
879 }
880
881 // expand a non-macro stmt. this is essentially the fallthrough for
882 // expand_stmt, above.
883 fn expand_non_macro_stmt(Spanned {node, span: stmt_span}: Stmt, fld: &mut MacroExpander)
884                          -> SmallVector<P<Stmt>> {
885     // is it a let?
886     match node {
887         StmtDecl(decl, node_id) => decl.and_then(|Spanned {node: decl, span}| match decl {
888             DeclLocal(local) => {
889                 // take it apart:
890                 let rewritten_local = local.map(|Local {id, pat, ty, init, span}| {
891                     // expand the ty since TyFixedLengthVec contains an Expr
892                     // and thus may have a macro use
893                     let expanded_ty = ty.map(|t| fld.fold_ty(t));
894                     // expand the pat (it might contain macro uses):
895                     let expanded_pat = fld.fold_pat(pat);
896                     // find the PatIdents in the pattern:
897                     // oh dear heaven... this is going to include the enum
898                     // names, as well... but that should be okay, as long as
899                     // the new names are gensyms for the old ones.
900                     // generate fresh names, push them to a new pending list
901                     let idents = pattern_bindings(&*expanded_pat);
902                     let mut new_pending_renames =
903                         idents.iter().map(|ident| (*ident, fresh_name(ident))).collect();
904                     // rewrite the pattern using the new names (the old
905                     // ones have already been applied):
906                     let rewritten_pat = {
907                         // nested binding to allow borrow to expire:
908                         let mut rename_fld = IdentRenamer{renames: &mut new_pending_renames};
909                         rename_fld.fold_pat(expanded_pat)
910                     };
911                     // add them to the existing pending renames:
912                     fld.cx.syntax_env.info().pending_renames
913                           .extend(new_pending_renames);
914                     Local {
915                         id: id,
916                         ty: expanded_ty,
917                         pat: rewritten_pat,
918                         // also, don't forget to expand the init:
919                         init: init.map(|e| fld.fold_expr(e)),
920                         span: span
921                     }
922                 });
923                 SmallVector::one(P(Spanned {
924                     node: StmtDecl(P(Spanned {
925                             node: DeclLocal(rewritten_local),
926                             span: span
927                         }),
928                         node_id),
929                     span: stmt_span
930                 }))
931             }
932             _ => {
933                 noop_fold_stmt(Spanned {
934                     node: StmtDecl(P(Spanned {
935                             node: decl,
936                             span: span
937                         }),
938                         node_id),
939                     span: stmt_span
940                 }, fld)
941             }
942         }),
943         _ => {
944             noop_fold_stmt(Spanned {
945                 node: node,
946                 span: stmt_span
947             }, fld)
948         }
949     }
950 }
951
952 // expand the arm of a 'match', renaming for macro hygiene
953 fn expand_arm(arm: ast::Arm, fld: &mut MacroExpander) -> ast::Arm {
954     // expand pats... they might contain macro uses:
955     let expanded_pats = arm.pats.move_map(|pat| fld.fold_pat(pat));
956     if expanded_pats.is_empty() {
957         panic!("encountered match arm with 0 patterns");
958     }
959     // all of the pats must have the same set of bindings, so use the
960     // first one to extract them and generate new names:
961     let idents = pattern_bindings(&*expanded_pats[0]);
962     let new_renames = idents.into_iter().map(|id| (id, fresh_name(&id))).collect();
963     // apply the renaming, but only to the PatIdents:
964     let mut rename_pats_fld = PatIdentRenamer{renames:&new_renames};
965     let rewritten_pats = expanded_pats.move_map(|pat| rename_pats_fld.fold_pat(pat));
966     // apply renaming and then expansion to the guard and the body:
967     let mut rename_fld = IdentRenamer{renames:&new_renames};
968     let rewritten_guard =
969         arm.guard.map(|g| fld.fold_expr(rename_fld.fold_expr(g)));
970     let rewritten_body = fld.fold_expr(rename_fld.fold_expr(arm.body));
971     ast::Arm {
972         attrs: fold::fold_attrs(arm.attrs, fld),
973         pats: rewritten_pats,
974         guard: rewritten_guard,
975         body: rewritten_body,
976     }
977 }
978
979 /// A visitor that extracts the PatIdent (binding) paths
980 /// from a given thingy and puts them in a mutable
981 /// array
982 #[derive(Clone)]
983 struct PatIdentFinder {
984     ident_accumulator: Vec<ast::Ident>
985 }
986
987 impl<'v> Visitor<'v> for PatIdentFinder {
988     fn visit_pat(&mut self, pattern: &ast::Pat) {
989         match *pattern {
990             ast::Pat { id: _, node: ast::PatIdent(_, ref path1, ref inner), span: _ } => {
991                 self.ident_accumulator.push(path1.node);
992                 // visit optional subpattern of PatIdent:
993                 if let Some(ref subpat) = *inner {
994                     self.visit_pat(&**subpat)
995                 }
996             }
997             // use the default traversal for non-PatIdents
998             _ => visit::walk_pat(self, pattern)
999         }
1000     }
1001 }
1002
1003 /// find the PatIdent paths in a pattern
1004 fn pattern_bindings(pat: &ast::Pat) -> Vec<ast::Ident> {
1005     let mut name_finder = PatIdentFinder{ident_accumulator:Vec::new()};
1006     name_finder.visit_pat(pat);
1007     name_finder.ident_accumulator
1008 }
1009
1010 /// find the PatIdent paths in a
1011 fn fn_decl_arg_bindings(fn_decl: &ast::FnDecl) -> Vec<ast::Ident> {
1012     let mut pat_idents = PatIdentFinder{ident_accumulator:Vec::new()};
1013     for arg in &fn_decl.inputs {
1014         pat_idents.visit_pat(&*arg.pat);
1015     }
1016     pat_idents.ident_accumulator
1017 }
1018
1019 // expand a block. pushes a new exts_frame, then calls expand_block_elts
1020 pub fn expand_block(blk: P<Block>, fld: &mut MacroExpander) -> P<Block> {
1021     // see note below about treatment of exts table
1022     with_exts_frame!(fld.cx.syntax_env,false,
1023                      expand_block_elts(blk, fld))
1024 }
1025
1026 // expand the elements of a block.
1027 pub fn expand_block_elts(b: P<Block>, fld: &mut MacroExpander) -> P<Block> {
1028     b.map(|Block {id, stmts, expr, rules, span}| {
1029         let new_stmts = stmts.into_iter().flat_map(|x| {
1030             // perform all pending renames
1031             let renamed_stmt = {
1032                 let pending_renames = &mut fld.cx.syntax_env.info().pending_renames;
1033                 let mut rename_fld = IdentRenamer{renames:pending_renames};
1034                 rename_fld.fold_stmt(x).expect_one("rename_fold didn't return one value")
1035             };
1036             // expand macros in the statement
1037             fld.fold_stmt(renamed_stmt).into_iter()
1038         }).collect();
1039         let new_expr = expr.map(|x| {
1040             let expr = {
1041                 let pending_renames = &mut fld.cx.syntax_env.info().pending_renames;
1042                 let mut rename_fld = IdentRenamer{renames:pending_renames};
1043                 rename_fld.fold_expr(x)
1044             };
1045             fld.fold_expr(expr)
1046         });
1047         Block {
1048             id: fld.new_id(id),
1049             stmts: new_stmts,
1050             expr: new_expr,
1051             rules: rules,
1052             span: span
1053         }
1054     })
1055 }
1056
1057 fn expand_pat(p: P<ast::Pat>, fld: &mut MacroExpander) -> P<ast::Pat> {
1058     match p.node {
1059         PatMac(_) => {}
1060         _ => return noop_fold_pat(p, fld)
1061     }
1062     p.map(|ast::Pat {node, span, ..}| {
1063         let (pth, tts) = match node {
1064             PatMac(mac) => match mac.node {
1065                 MacInvocTT(pth, tts, _) => {
1066                     (pth, tts)
1067                 }
1068             },
1069             _ => unreachable!()
1070         };
1071         if pth.segments.len() > 1 {
1072             fld.cx.span_err(pth.span, "expected macro name without module separators");
1073             return DummyResult::raw_pat(span);
1074         }
1075         let extname = pth.segments[0].identifier.name;
1076         let marked_after = match fld.cx.syntax_env.find(&extname) {
1077             None => {
1078                 fld.cx.span_err(pth.span,
1079                                 &format!("macro undefined: '{}!'",
1080                                         extname));
1081                 // let compilation continue
1082                 return DummyResult::raw_pat(span);
1083             }
1084
1085             Some(rc) => match *rc {
1086                 NormalTT(ref expander, tt_span, allow_internal_unstable) => {
1087                     fld.cx.bt_push(ExpnInfo {
1088                         call_site: span,
1089                         callee: NameAndSpan {
1090                             format: MacroBang(extname),
1091                             span: tt_span,
1092                             allow_internal_unstable: allow_internal_unstable,
1093                         }
1094                     });
1095
1096                     let fm = fresh_mark();
1097                     let marked_before = mark_tts(&tts[..], fm);
1098                     let mac_span = fld.cx.original_span();
1099                     let pat = expander.expand(fld.cx,
1100                                               mac_span,
1101                                               &marked_before[..]).make_pat();
1102                     let expanded = match pat {
1103                         Some(e) => e,
1104                         None => {
1105                             fld.cx.span_err(
1106                                 pth.span,
1107                                 &format!(
1108                                     "non-pattern macro in pattern position: {}",
1109                                     extname
1110                                     )
1111                             );
1112                             return DummyResult::raw_pat(span);
1113                         }
1114                     };
1115
1116                     // mark after:
1117                     mark_pat(expanded,fm)
1118                 }
1119                 _ => {
1120                     fld.cx.span_err(span,
1121                                     &format!("{}! is not legal in pattern position",
1122                                             extname));
1123                     return DummyResult::raw_pat(span);
1124                 }
1125             }
1126         };
1127
1128         let fully_expanded =
1129             fld.fold_pat(marked_after).node.clone();
1130         fld.cx.bt_pop();
1131
1132         ast::Pat {
1133             id: ast::DUMMY_NODE_ID,
1134             node: fully_expanded,
1135             span: span
1136         }
1137     })
1138 }
1139
1140 /// A tree-folder that applies every rename in its (mutable) list
1141 /// to every identifier, including both bindings and varrefs
1142 /// (and lots of things that will turn out to be neither)
1143 pub struct IdentRenamer<'a> {
1144     renames: &'a mtwt::RenameList,
1145 }
1146
1147 impl<'a> Folder for IdentRenamer<'a> {
1148     fn fold_ident(&mut self, id: Ident) -> Ident {
1149         Ident {
1150             name: id.name,
1151             ctxt: mtwt::apply_renames(self.renames, id.ctxt),
1152         }
1153     }
1154     fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
1155         fold::noop_fold_mac(mac, self)
1156     }
1157 }
1158
1159 /// A tree-folder that applies every rename in its list to
1160 /// the idents that are in PatIdent patterns. This is more narrowly
1161 /// focused than IdentRenamer, and is needed for FnDecl,
1162 /// where we want to rename the args but not the fn name or the generics etc.
1163 pub struct PatIdentRenamer<'a> {
1164     renames: &'a mtwt::RenameList,
1165 }
1166
1167 impl<'a> Folder for PatIdentRenamer<'a> {
1168     fn fold_pat(&mut self, pat: P<ast::Pat>) -> P<ast::Pat> {
1169         match pat.node {
1170             ast::PatIdent(..) => {},
1171             _ => return noop_fold_pat(pat, self)
1172         }
1173
1174         pat.map(|ast::Pat {id, node, span}| match node {
1175             ast::PatIdent(binding_mode, Spanned{span: sp, node: ident}, sub) => {
1176                 let new_ident = Ident{name: ident.name,
1177                                       ctxt: mtwt::apply_renames(self.renames, ident.ctxt)};
1178                 let new_node =
1179                     ast::PatIdent(binding_mode,
1180                                   Spanned{span: self.new_span(sp), node: new_ident},
1181                                   sub.map(|p| self.fold_pat(p)));
1182                 ast::Pat {
1183                     id: id,
1184                     node: new_node,
1185                     span: self.new_span(span)
1186                 }
1187             },
1188             _ => unreachable!()
1189         })
1190     }
1191     fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
1192         fold::noop_fold_mac(mac, self)
1193     }
1194 }
1195
1196 fn expand_annotatable(a: Annotatable,
1197                       fld: &mut MacroExpander)
1198                       -> SmallVector<Annotatable> {
1199     let a = expand_item_multi_modifier(a, fld);
1200
1201     let mut decorator_items = SmallVector::zero();
1202     let mut new_attrs = Vec::new();
1203     expand_decorators(a.clone(), fld, &mut decorator_items, &mut new_attrs);
1204
1205     let mut new_items: SmallVector<Annotatable> = match a {
1206         Annotatable::Item(it) => match it.node {
1207             ast::ItemMac(..) => {
1208                 expand_item_mac(it, fld).into_iter().map(|i| Annotatable::Item(i)).collect()
1209             }
1210             ast::ItemMod(_) | ast::ItemForeignMod(_) => {
1211                 let valid_ident =
1212                     it.ident.name != parse::token::special_idents::invalid.name;
1213
1214                 if valid_ident {
1215                     fld.cx.mod_push(it.ident);
1216                 }
1217                 let macro_use = contains_macro_use(fld, &new_attrs[..]);
1218                 let result = with_exts_frame!(fld.cx.syntax_env,
1219                                               macro_use,
1220                                               noop_fold_item(it, fld));
1221                 if valid_ident {
1222                     fld.cx.mod_pop();
1223                 }
1224                 result.into_iter().map(|i| Annotatable::Item(i)).collect()
1225             },
1226             _ => {
1227                 let it = P(ast::Item {
1228                     attrs: new_attrs,
1229                     ..(*it).clone()
1230                 });
1231                 noop_fold_item(it, fld).into_iter().map(|i| Annotatable::Item(i)).collect()
1232             }
1233         },
1234
1235         Annotatable::TraitItem(it) => match it.node {
1236             ast::MethodTraitItem(_, Some(_)) => SmallVector::one(it.map(|ti| ast::TraitItem {
1237                 id: ti.id,
1238                 ident: ti.ident,
1239                 attrs: ti.attrs,
1240                 node: match ti.node  {
1241                     ast::MethodTraitItem(sig, Some(body)) => {
1242                         let (sig, body) = expand_and_rename_method(sig, body, fld);
1243                         ast::MethodTraitItem(sig, Some(body))
1244                     }
1245                     _ => unreachable!()
1246                 },
1247                 span: fld.new_span(ti.span)
1248             })),
1249             _ => fold::noop_fold_trait_item(it, fld)
1250         }.into_iter().map(Annotatable::TraitItem).collect(),
1251
1252         Annotatable::ImplItem(ii) => {
1253             expand_impl_item(ii, fld).into_iter().map(Annotatable::ImplItem).collect()
1254         }
1255     };
1256
1257     new_items.push_all(decorator_items);
1258     new_items
1259 }
1260
1261 // Partition a set of attributes into one kind of attribute, and other kinds.
1262 macro_rules! partition {
1263     ($fn_name: ident, $variant: ident) => {
1264         #[allow(deprecated)] // The `allow` is needed because the `Modifier` variant might be used.
1265         fn $fn_name(attrs: &[ast::Attribute],
1266                     fld: &MacroExpander)
1267                      -> (Vec<ast::Attribute>, Vec<ast::Attribute>) {
1268             attrs.iter().cloned().partition(|attr| {
1269                 match fld.cx.syntax_env.find(&intern(&attr.name())) {
1270                     Some(rc) => match *rc {
1271                         $variant(..) => true,
1272                         _ => false
1273                     },
1274                     _ => false
1275                 }
1276             })
1277         }
1278     }
1279 }
1280
1281 partition!(modifiers, Modifier);
1282 partition!(multi_modifiers, MultiModifier);
1283
1284
1285 #[allow(deprecated)] // The `allow` is needed because the `Decorator` variant is used.
1286 fn expand_decorators(a: Annotatable,
1287                      fld: &mut MacroExpander,
1288                      decorator_items: &mut SmallVector<Annotatable>,
1289                      new_attrs: &mut Vec<ast::Attribute>)
1290 {
1291     for attr in a.attrs() {
1292         let mname = intern(&attr.name());
1293         match fld.cx.syntax_env.find(&mname) {
1294             Some(rc) => match *rc {
1295                 Decorator(ref dec) => {
1296                     attr::mark_used(&attr);
1297
1298                     fld.cx.bt_push(ExpnInfo {
1299                         call_site: attr.span,
1300                         callee: NameAndSpan {
1301                             format: MacroAttribute(mname),
1302                             span: Some(attr.span),
1303                             // attributes can do whatever they like,
1304                             // for now.
1305                             allow_internal_unstable: true,
1306                         }
1307                     });
1308
1309                     // we'd ideally decorator_items.push_all(expand_item(item, fld)),
1310                     // but that double-mut-borrows fld
1311                     let mut items: SmallVector<Annotatable> = SmallVector::zero();
1312                     dec.expand(fld.cx,
1313                                attr.span,
1314                                &attr.node.value,
1315                                &a.clone().expect_item(),
1316                                &mut |item| items.push(Annotatable::Item(item)));
1317                     decorator_items.extend(items.into_iter()
1318                         .flat_map(|ann| expand_annotatable(ann, fld).into_iter()));
1319
1320                     fld.cx.bt_pop();
1321                 }
1322                 MultiDecorator(ref dec) => {
1323                     attr::mark_used(&attr);
1324
1325                     fld.cx.bt_push(ExpnInfo {
1326                         call_site: attr.span,
1327                         callee: NameAndSpan {
1328                             format: MacroAttribute(mname),
1329                             span: Some(attr.span),
1330                             // attributes can do whatever they like,
1331                             // for now.
1332                             allow_internal_unstable: true,
1333                         }
1334                     });
1335
1336                     // we'd ideally decorator_items.push_all(expand_annotatable(ann, fld)),
1337                     // but that double-mut-borrows fld
1338                     let mut items: SmallVector<Annotatable> = SmallVector::zero();
1339                     dec.expand(fld.cx,
1340                                attr.span,
1341                                &attr.node.value,
1342                                &a,
1343                                &mut |ann| items.push(ann));
1344                     decorator_items.extend(items.into_iter()
1345                         .flat_map(|ann| expand_annotatable(ann, fld).into_iter()));
1346
1347                     fld.cx.bt_pop();
1348                 }
1349                 _ => new_attrs.push((*attr).clone()),
1350             },
1351             _ => new_attrs.push((*attr).clone()),
1352         }
1353     }
1354 }
1355
1356 fn expand_item_multi_modifier(mut it: Annotatable,
1357                               fld: &mut MacroExpander)
1358                               -> Annotatable {
1359     let (modifiers, other_attrs) = multi_modifiers(it.attrs(), fld);
1360
1361     // Update the attrs, leave everything else alone. Is this mutation really a good idea?
1362     it = it.fold_attrs(other_attrs);
1363
1364     if modifiers.is_empty() {
1365         return it
1366     }
1367
1368     for attr in &modifiers {
1369         let mname = intern(&attr.name());
1370
1371         match fld.cx.syntax_env.find(&mname) {
1372             Some(rc) => match *rc {
1373                 MultiModifier(ref mac) => {
1374                     attr::mark_used(attr);
1375                     fld.cx.bt_push(ExpnInfo {
1376                         call_site: attr.span,
1377                         callee: NameAndSpan {
1378                             format: MacroAttribute(mname),
1379                             span: Some(attr.span),
1380                             // attributes can do whatever they like,
1381                             // for now
1382                             allow_internal_unstable: true,
1383                         }
1384                     });
1385                     it = mac.expand(fld.cx, attr.span, &*attr.node.value, it);
1386                     fld.cx.bt_pop();
1387                 }
1388                 _ => unreachable!()
1389             },
1390             _ => unreachable!()
1391         }
1392     }
1393
1394     // Expansion may have added new ItemModifiers.
1395     expand_item_multi_modifier(it, fld)
1396 }
1397
1398 #[allow(deprecated)] // This is needed because the `ItemModifier` trait is used
1399 fn expand_item_modifiers(mut it: P<ast::Item>,
1400                          fld: &mut MacroExpander)
1401                          -> P<ast::Item> {
1402     // partition the attributes into ItemModifiers and others
1403     let (modifiers, other_attrs) = modifiers(&it.attrs, fld);
1404
1405     // update the attrs, leave everything else alone. Is this mutation really a good idea?
1406     it = P(ast::Item {
1407         attrs: other_attrs,
1408         ..(*it).clone()
1409     });
1410
1411     if modifiers.is_empty() {
1412         let it = expand_item_multi_modifier(Annotatable::Item(it), fld);
1413         return it.expect_item();
1414     }
1415
1416     for attr in &modifiers {
1417         let mname = intern(&attr.name());
1418
1419         match fld.cx.syntax_env.find(&mname) {
1420             Some(rc) => match *rc {
1421                 Modifier(ref mac) => {
1422                     attr::mark_used(attr);
1423                     fld.cx.bt_push(ExpnInfo {
1424                         call_site: attr.span,
1425                         callee: NameAndSpan {
1426                             format: MacroAttribute(mname),
1427                             span: Some(attr.span),
1428                             // attributes can do whatever they like,
1429                             // for now
1430                             allow_internal_unstable: true,
1431                         }
1432                     });
1433                     it = mac.expand(fld.cx, attr.span, &*attr.node.value, it);
1434                     fld.cx.bt_pop();
1435                 }
1436                 _ => unreachable!()
1437             },
1438             _ => unreachable!()
1439         }
1440     }
1441
1442     // Expansion may have added new ItemModifiers.
1443     // It is possible, that an item modifier could expand to a multi-modifier or
1444     // vice versa. In this case we will expand all modifiers before multi-modifiers,
1445     // which might give an odd ordering. However, I think it is unlikely that the
1446     // two kinds will be mixed, and old-style multi-modifiers are deprecated.
1447     expand_item_modifiers(it, fld)
1448 }
1449
1450 fn expand_impl_item(ii: P<ast::ImplItem>, fld: &mut MacroExpander)
1451                  -> SmallVector<P<ast::ImplItem>> {
1452     match ii.node {
1453         ast::MethodImplItem(..) => SmallVector::one(ii.map(|ii| ast::ImplItem {
1454             id: ii.id,
1455             ident: ii.ident,
1456             attrs: ii.attrs,
1457             vis: ii.vis,
1458             node: match ii.node  {
1459                 ast::MethodImplItem(sig, body) => {
1460                     let (sig, body) = expand_and_rename_method(sig, body, fld);
1461                     ast::MethodImplItem(sig, body)
1462                 }
1463                 _ => unreachable!()
1464             },
1465             span: fld.new_span(ii.span)
1466         })),
1467         ast::MacImplItem(_) => {
1468             let (span, mac) = ii.and_then(|ii| match ii.node {
1469                 ast::MacImplItem(mac) => (ii.span, mac),
1470                 _ => unreachable!()
1471             });
1472             let maybe_new_items =
1473                 expand_mac_invoc(mac, span,
1474                                  |r| r.make_impl_items(),
1475                                  |meths, mark| meths.move_map(|m| mark_impl_item(m, mark)),
1476                                  fld);
1477
1478             match maybe_new_items {
1479                 Some(impl_items) => {
1480                     // expand again if necessary
1481                     let new_items = impl_items.into_iter().flat_map(|ii| {
1482                         expand_impl_item(ii, fld).into_iter()
1483                     }).collect();
1484                     fld.cx.bt_pop();
1485                     new_items
1486                 }
1487                 None => SmallVector::zero()
1488             }
1489         }
1490         _ => fold::noop_fold_impl_item(ii, fld)
1491     }
1492 }
1493
1494 /// Given a fn_decl and a block and a MacroExpander, expand the fn_decl, then use the
1495 /// PatIdents in its arguments to perform renaming in the FnDecl and
1496 /// the block, returning both the new FnDecl and the new Block.
1497 fn expand_and_rename_fn_decl_and_block(fn_decl: P<ast::FnDecl>, block: P<ast::Block>,
1498                                        fld: &mut MacroExpander)
1499                                        -> (P<ast::FnDecl>, P<ast::Block>) {
1500     let expanded_decl = fld.fold_fn_decl(fn_decl);
1501     let idents = fn_decl_arg_bindings(&*expanded_decl);
1502     let renames =
1503         idents.iter().map(|id : &ast::Ident| (*id,fresh_name(id))).collect();
1504     // first, a renamer for the PatIdents, for the fn_decl:
1505     let mut rename_pat_fld = PatIdentRenamer{renames: &renames};
1506     let rewritten_fn_decl = rename_pat_fld.fold_fn_decl(expanded_decl);
1507     // now, a renamer for *all* idents, for the body:
1508     let mut rename_fld = IdentRenamer{renames: &renames};
1509     let rewritten_body = fld.fold_block(rename_fld.fold_block(block));
1510     (rewritten_fn_decl,rewritten_body)
1511 }
1512
1513 fn expand_and_rename_method(sig: ast::MethodSig, body: P<ast::Block>,
1514                             fld: &mut MacroExpander)
1515                             -> (ast::MethodSig, P<ast::Block>) {
1516     let (rewritten_fn_decl, rewritten_body)
1517         = expand_and_rename_fn_decl_and_block(sig.decl, body, fld);
1518     (ast::MethodSig {
1519         generics: fld.fold_generics(sig.generics),
1520         abi: sig.abi,
1521         explicit_self: fld.fold_explicit_self(sig.explicit_self),
1522         unsafety: sig.unsafety,
1523         constness: sig.constness,
1524         decl: rewritten_fn_decl
1525     }, rewritten_body)
1526 }
1527
1528 pub fn expand_type(t: P<ast::Ty>, fld: &mut MacroExpander) -> P<ast::Ty> {
1529     let t = match t.node.clone() {
1530         ast::Ty_::TyMac(mac) => {
1531             if fld.cx.ecfg.features.unwrap().type_macros {
1532                 let expanded_ty = match expand_mac_invoc(mac, t.span,
1533                                                          |r| r.make_ty(),
1534                                                          mark_ty,
1535                                                          fld) {
1536                     Some(ty) => ty,
1537                     None => {
1538                         return DummyResult::raw_ty(t.span);
1539                     }
1540                 };
1541
1542                 // Keep going, outside-in.
1543                 let fully_expanded = fld.fold_ty(expanded_ty);
1544                 fld.cx.bt_pop();
1545
1546                 fully_expanded.map(|t| ast::Ty {
1547                     id: ast::DUMMY_NODE_ID,
1548                     node: t.node,
1549                     span: t.span,
1550                     })
1551             } else {
1552                 feature_gate::emit_feature_err(
1553                     &fld.cx.parse_sess.span_diagnostic,
1554                     "type_macros",
1555                     t.span,
1556                     "type macros are experimental (see issue: #27336)");
1557
1558                 DummyResult::raw_ty(t.span)
1559             }
1560         }
1561         _ => t
1562     };
1563
1564     fold::noop_fold_ty(t, fld)
1565 }
1566
1567 /// A tree-folder that performs macro expansion
1568 pub struct MacroExpander<'a, 'b:'a> {
1569     pub cx: &'a mut ExtCtxt<'b>,
1570 }
1571
1572 impl<'a, 'b> MacroExpander<'a, 'b> {
1573     pub fn new(cx: &'a mut ExtCtxt<'b>) -> MacroExpander<'a, 'b> {
1574         MacroExpander { cx: cx }
1575     }
1576 }
1577
1578 impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
1579     fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
1580         expand_expr(expr, self)
1581     }
1582
1583     fn fold_pat(&mut self, pat: P<ast::Pat>) -> P<ast::Pat> {
1584         expand_pat(pat, self)
1585     }
1586
1587     fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
1588         expand_item(item, self)
1589     }
1590
1591     fn fold_item_underscore(&mut self, item: ast::Item_) -> ast::Item_ {
1592         expand_item_underscore(item, self)
1593     }
1594
1595     fn fold_stmt(&mut self, stmt: P<ast::Stmt>) -> SmallVector<P<ast::Stmt>> {
1596         expand_stmt(stmt, self)
1597     }
1598
1599     fn fold_block(&mut self, block: P<Block>) -> P<Block> {
1600         expand_block(block, self)
1601     }
1602
1603     fn fold_arm(&mut self, arm: ast::Arm) -> ast::Arm {
1604         expand_arm(arm, self)
1605     }
1606
1607     fn fold_trait_item(&mut self, i: P<ast::TraitItem>) -> SmallVector<P<ast::TraitItem>> {
1608         expand_annotatable(Annotatable::TraitItem(i), self)
1609             .into_iter().map(|i| i.expect_trait_item()).collect()
1610     }
1611
1612     fn fold_impl_item(&mut self, i: P<ast::ImplItem>) -> SmallVector<P<ast::ImplItem>> {
1613         expand_annotatable(Annotatable::ImplItem(i), self)
1614             .into_iter().map(|i| i.expect_impl_item()).collect()
1615     }
1616
1617     fn fold_ty(&mut self, ty: P<ast::Ty>) -> P<ast::Ty> {
1618         expand_type(ty, self)
1619     }
1620
1621     fn new_span(&mut self, span: Span) -> Span {
1622         new_span(self.cx, span)
1623     }
1624 }
1625
1626 fn new_span(cx: &ExtCtxt, sp: Span) -> Span {
1627     /* this discards information in the case of macro-defining macros */
1628     Span {
1629         lo: sp.lo,
1630         hi: sp.hi,
1631         expn_id: cx.backtrace(),
1632     }
1633 }
1634
1635 pub struct ExpansionConfig<'feat> {
1636     pub crate_name: String,
1637     pub features: Option<&'feat Features>,
1638     pub recursion_limit: usize,
1639     pub trace_mac: bool,
1640 }
1641
1642 macro_rules! feature_tests {
1643     ($( fn $getter:ident = $field:ident, )*) => {
1644         $(
1645             pub fn $getter(&self) -> bool {
1646                 match self.features {
1647                     Some(&Features { $field: true, .. }) => true,
1648                     _ => false,
1649                 }
1650             }
1651         )*
1652     }
1653 }
1654
1655 impl<'feat> ExpansionConfig<'feat> {
1656     pub fn default(crate_name: String) -> ExpansionConfig<'static> {
1657         ExpansionConfig {
1658             crate_name: crate_name,
1659             features: None,
1660             recursion_limit: 64,
1661             trace_mac: false,
1662         }
1663     }
1664
1665     feature_tests! {
1666         fn enable_quotes = allow_quote,
1667         fn enable_asm = allow_asm,
1668         fn enable_log_syntax = allow_log_syntax,
1669         fn enable_concat_idents = allow_concat_idents,
1670         fn enable_trace_macros = allow_trace_macros,
1671         fn enable_allow_internal_unstable = allow_internal_unstable,
1672         fn enable_custom_derive = allow_custom_derive,
1673         fn enable_pushpop_unsafe = allow_pushpop_unsafe,
1674     }
1675 }
1676
1677 pub fn expand_crate<'feat>(parse_sess: &parse::ParseSess,
1678                            cfg: ExpansionConfig<'feat>,
1679                            // these are the macros being imported to this crate:
1680                            imported_macros: Vec<ast::MacroDef>,
1681                            user_exts: Vec<NamedSyntaxExtension>,
1682                            feature_gated_cfgs: &mut Vec<GatedCfg>,
1683                            c: Crate) -> Crate {
1684     let mut cx = ExtCtxt::new(parse_sess, c.config.clone(), cfg,
1685                               feature_gated_cfgs);
1686     if std_inject::no_core(&c) {
1687         cx.crate_root = None;
1688     } else if std_inject::no_std(&c) {
1689         cx.crate_root = Some("core");
1690     } else {
1691         cx.crate_root = Some("std");
1692     }
1693
1694     let mut expander = MacroExpander::new(&mut cx);
1695
1696     for def in imported_macros {
1697         expander.cx.insert_macro(def);
1698     }
1699
1700     for (name, extension) in user_exts {
1701         expander.cx.syntax_env.insert(name, extension);
1702     }
1703
1704     let mut ret = expander.fold_crate(c);
1705     ret.exported_macros = expander.cx.exported_macros.clone();
1706     parse_sess.span_diagnostic.handler().abort_if_errors();
1707     return ret;
1708 }
1709
1710 // HYGIENIC CONTEXT EXTENSION:
1711 // all of these functions are for walking over
1712 // ASTs and making some change to the context of every
1713 // element that has one. a CtxtFn is a trait-ified
1714 // version of a closure in (SyntaxContext -> SyntaxContext).
1715 // the ones defined here include:
1716 // Marker - add a mark to a context
1717
1718 // A Marker adds the given mark to the syntax context
1719 struct Marker { mark: Mrk }
1720
1721 impl Folder for Marker {
1722     fn fold_ident(&mut self, id: Ident) -> Ident {
1723         ast::Ident {
1724             name: id.name,
1725             ctxt: mtwt::apply_mark(self.mark, id.ctxt)
1726         }
1727     }
1728     fn fold_mac(&mut self, Spanned {node, span}: ast::Mac) -> ast::Mac {
1729         Spanned {
1730             node: match node {
1731                 MacInvocTT(path, tts, ctxt) => {
1732                     MacInvocTT(self.fold_path(path),
1733                                self.fold_tts(&tts[..]),
1734                                mtwt::apply_mark(self.mark, ctxt))
1735                 }
1736             },
1737             span: span,
1738         }
1739     }
1740 }
1741
1742 // apply a given mark to the given token trees. Used prior to expansion of a macro.
1743 fn mark_tts(tts: &[TokenTree], m: Mrk) -> Vec<TokenTree> {
1744     noop_fold_tts(tts, &mut Marker{mark:m})
1745 }
1746
1747 // apply a given mark to the given expr. Used following the expansion of a macro.
1748 fn mark_expr(expr: P<ast::Expr>, m: Mrk) -> P<ast::Expr> {
1749     Marker{mark:m}.fold_expr(expr)
1750 }
1751
1752 // apply a given mark to the given pattern. Used following the expansion of a macro.
1753 fn mark_pat(pat: P<ast::Pat>, m: Mrk) -> P<ast::Pat> {
1754     Marker{mark:m}.fold_pat(pat)
1755 }
1756
1757 // apply a given mark to the given stmt. Used following the expansion of a macro.
1758 fn mark_stmt(stmt: P<ast::Stmt>, m: Mrk) -> P<ast::Stmt> {
1759     Marker{mark:m}.fold_stmt(stmt)
1760         .expect_one("marking a stmt didn't return exactly one stmt")
1761 }
1762
1763 // apply a given mark to the given item. Used following the expansion of a macro.
1764 fn mark_item(expr: P<ast::Item>, m: Mrk) -> P<ast::Item> {
1765     Marker{mark:m}.fold_item(expr)
1766         .expect_one("marking an item didn't return exactly one item")
1767 }
1768
1769 // apply a given mark to the given item. Used following the expansion of a macro.
1770 fn mark_impl_item(ii: P<ast::ImplItem>, m: Mrk) -> P<ast::ImplItem> {
1771     Marker{mark:m}.fold_impl_item(ii)
1772         .expect_one("marking an impl item didn't return exactly one impl item")
1773 }
1774
1775 fn mark_ty(ty: P<ast::Ty>, m: Mrk) -> P<ast::Ty> {
1776     Marker { mark: m }.fold_ty(ty)
1777 }
1778
1779 /// Check that there are no macro invocations left in the AST:
1780 pub fn check_for_macros(sess: &parse::ParseSess, krate: &ast::Crate) {
1781     visit::walk_crate(&mut MacroExterminator{sess:sess}, krate);
1782 }
1783
1784 /// A visitor that ensures that no macro invocations remain in an AST.
1785 struct MacroExterminator<'a>{
1786     sess: &'a parse::ParseSess
1787 }
1788
1789 impl<'a, 'v> Visitor<'v> for MacroExterminator<'a> {
1790     fn visit_mac(&mut self, mac: &ast::Mac) {
1791         self.sess.span_diagnostic.span_bug(mac.span,
1792                                            "macro exterminator: expected AST \
1793                                            with no macro invocations");
1794     }
1795 }
1796
1797
1798 #[cfg(test)]
1799 mod tests {
1800     use super::{pattern_bindings, expand_crate};
1801     use super::{PatIdentFinder, IdentRenamer, PatIdentRenamer, ExpansionConfig};
1802     use ast;
1803     use ast::Name;
1804     use codemap;
1805     use ext::mtwt;
1806     use fold::Folder;
1807     use parse;
1808     use parse::token;
1809     use util::parser_testing::{string_to_parser};
1810     use util::parser_testing::{string_to_pat, string_to_crate, strs_to_idents};
1811     use visit;
1812     use visit::Visitor;
1813
1814     // a visitor that extracts the paths
1815     // from a given thingy and puts them in a mutable
1816     // array (passed in to the traversal)
1817     #[derive(Clone)]
1818     struct PathExprFinderContext {
1819         path_accumulator: Vec<ast::Path> ,
1820     }
1821
1822     impl<'v> Visitor<'v> for PathExprFinderContext {
1823         fn visit_expr(&mut self, expr: &ast::Expr) {
1824             if let ast::ExprPath(None, ref p) = expr.node {
1825                 self.path_accumulator.push(p.clone());
1826             }
1827             visit::walk_expr(self, expr);
1828         }
1829     }
1830
1831     // find the variable references in a crate
1832     fn crate_varrefs(the_crate : &ast::Crate) -> Vec<ast::Path> {
1833         let mut path_finder = PathExprFinderContext{path_accumulator:Vec::new()};
1834         visit::walk_crate(&mut path_finder, the_crate);
1835         path_finder.path_accumulator
1836     }
1837
1838     /// A Visitor that extracts the identifiers from a thingy.
1839     // as a side note, I'm starting to want to abstract over these....
1840     struct IdentFinder {
1841         ident_accumulator: Vec<ast::Ident>
1842     }
1843
1844     impl<'v> Visitor<'v> for IdentFinder {
1845         fn visit_ident(&mut self, _: codemap::Span, id: ast::Ident){
1846             self.ident_accumulator.push(id);
1847         }
1848     }
1849
1850     /// Find the idents in a crate
1851     fn crate_idents(the_crate: &ast::Crate) -> Vec<ast::Ident> {
1852         let mut ident_finder = IdentFinder{ident_accumulator: Vec::new()};
1853         visit::walk_crate(&mut ident_finder, the_crate);
1854         ident_finder.ident_accumulator
1855     }
1856
1857     // these following tests are quite fragile, in that they don't test what
1858     // *kind* of failure occurs.
1859
1860     fn test_ecfg() -> ExpansionConfig<'static> {
1861         ExpansionConfig::default("test".to_string())
1862     }
1863
1864     // make sure that macros can't escape fns
1865     #[should_panic]
1866     #[test] fn macros_cant_escape_fns_test () {
1867         let src = "fn bogus() {macro_rules! z (() => (3+4));}\
1868                    fn inty() -> i32 { z!() }".to_string();
1869         let sess = parse::ParseSess::new();
1870         let crate_ast = parse::parse_crate_from_source_str(
1871             "<test>".to_string(),
1872             src,
1873             Vec::new(), &sess);
1874         // should fail:
1875         expand_crate(&sess,test_ecfg(),vec!(),vec!(), &mut vec![], crate_ast);
1876     }
1877
1878     // make sure that macros can't escape modules
1879     #[should_panic]
1880     #[test] fn macros_cant_escape_mods_test () {
1881         let src = "mod foo {macro_rules! z (() => (3+4));}\
1882                    fn inty() -> i32 { z!() }".to_string();
1883         let sess = parse::ParseSess::new();
1884         let crate_ast = parse::parse_crate_from_source_str(
1885             "<test>".to_string(),
1886             src,
1887             Vec::new(), &sess);
1888         expand_crate(&sess,test_ecfg(),vec!(),vec!(), &mut vec![], crate_ast);
1889     }
1890
1891     // macro_use modules should allow macros to escape
1892     #[test] fn macros_can_escape_flattened_mods_test () {
1893         let src = "#[macro_use] mod foo {macro_rules! z (() => (3+4));}\
1894                    fn inty() -> i32 { z!() }".to_string();
1895         let sess = parse::ParseSess::new();
1896         let crate_ast = parse::parse_crate_from_source_str(
1897             "<test>".to_string(),
1898             src,
1899             Vec::new(), &sess);
1900         expand_crate(&sess, test_ecfg(), vec!(), vec!(), &mut vec![], crate_ast);
1901     }
1902
1903     fn expand_crate_str(crate_str: String) -> ast::Crate {
1904         let ps = parse::ParseSess::new();
1905         let crate_ast = panictry!(string_to_parser(&ps, crate_str).parse_crate_mod());
1906         // the cfg argument actually does matter, here...
1907         expand_crate(&ps,test_ecfg(),vec!(),vec!(), &mut vec![], crate_ast)
1908     }
1909
1910     // find the pat_ident paths in a crate
1911     fn crate_bindings(the_crate : &ast::Crate) -> Vec<ast::Ident> {
1912         let mut name_finder = PatIdentFinder{ident_accumulator:Vec::new()};
1913         visit::walk_crate(&mut name_finder, the_crate);
1914         name_finder.ident_accumulator
1915     }
1916
1917     #[test] fn macro_tokens_should_match(){
1918         expand_crate_str(
1919             "macro_rules! m((a)=>(13)) ;fn main(){m!(a);}".to_string());
1920     }
1921
1922     // should be able to use a bound identifier as a literal in a macro definition:
1923     #[test] fn self_macro_parsing(){
1924         expand_crate_str(
1925             "macro_rules! foo ((zz) => (287;));
1926             fn f(zz: i32) {foo!(zz);}".to_string()
1927             );
1928     }
1929
1930     // renaming tests expand a crate and then check that the bindings match
1931     // the right varrefs. The specification of the test case includes the
1932     // text of the crate, and also an array of arrays.  Each element in the
1933     // outer array corresponds to a binding in the traversal of the AST
1934     // induced by visit.  Each of these arrays contains a list of indexes,
1935     // interpreted as the varrefs in the varref traversal that this binding
1936     // should match.  So, for instance, in a program with two bindings and
1937     // three varrefs, the array [[1, 2], [0]] would indicate that the first
1938     // binding should match the second two varrefs, and the second binding
1939     // should match the first varref.
1940     //
1941     // Put differently; this is a sparse representation of a boolean matrix
1942     // indicating which bindings capture which identifiers.
1943     //
1944     // Note also that this matrix is dependent on the implicit ordering of
1945     // the bindings and the varrefs discovered by the name-finder and the path-finder.
1946     //
1947     // The comparisons are done post-mtwt-resolve, so we're comparing renamed
1948     // names; differences in marks don't matter any more.
1949     //
1950     // oog... I also want tests that check "bound-identifier-=?". That is,
1951     // not just "do these have the same name", but "do they have the same
1952     // name *and* the same marks"? Understanding this is really pretty painful.
1953     // in principle, you might want to control this boolean on a per-varref basis,
1954     // but that would make things even harder to understand, and might not be
1955     // necessary for thorough testing.
1956     type RenamingTest = (&'static str, Vec<Vec<usize>>, bool);
1957
1958     #[test]
1959     fn automatic_renaming () {
1960         let tests: Vec<RenamingTest> =
1961             vec!(// b & c should get new names throughout, in the expr too:
1962                 ("fn a() -> i32 { let b = 13; let c = b; b+c }",
1963                  vec!(vec!(0,1),vec!(2)), false),
1964                 // both x's should be renamed (how is this causing a bug?)
1965                 ("fn main () {let x: i32 = 13;x;}",
1966                  vec!(vec!(0)), false),
1967                 // the use of b after the + should be renamed, the other one not:
1968                 ("macro_rules! f (($x:ident) => (b + $x)); fn a() -> i32 { let b = 13; f!(b)}",
1969                  vec!(vec!(1)), false),
1970                 // the b before the plus should not be renamed (requires marks)
1971                 ("macro_rules! f (($x:ident) => ({let b=9; ($x + b)})); fn a() -> i32 { f!(b)}",
1972                  vec!(vec!(1)), false),
1973                 // the marks going in and out of letty should cancel, allowing that $x to
1974                 // capture the one following the semicolon.
1975                 // this was an awesome test case, and caught a *lot* of bugs.
1976                 ("macro_rules! letty(($x:ident) => (let $x = 15;));
1977                   macro_rules! user(($x:ident) => ({letty!($x); $x}));
1978                   fn main() -> i32 {user!(z)}",
1979                  vec!(vec!(0)), false)
1980                 );
1981         for (idx,s) in tests.iter().enumerate() {
1982             run_renaming_test(s,idx);
1983         }
1984     }
1985
1986     // no longer a fixme #8062: this test exposes a *potential* bug; our system does
1987     // not behave exactly like MTWT, but a conversation with Matthew Flatt
1988     // suggests that this can only occur in the presence of local-expand, which
1989     // we have no plans to support. ... unless it's needed for item hygiene....
1990     #[ignore]
1991     #[test]
1992     fn issue_8062(){
1993         run_renaming_test(
1994             &("fn main() {let hrcoo = 19; macro_rules! getx(()=>(hrcoo)); getx!();}",
1995               vec!(vec!(0)), true), 0)
1996     }
1997
1998     // FIXME #6994:
1999     // the z flows into and out of two macros (g & f) along one path, and one
2000     // (just g) along the other, so the result of the whole thing should
2001     // be "let z_123 = 3; z_123"
2002     #[ignore]
2003     #[test]
2004     fn issue_6994(){
2005         run_renaming_test(
2006             &("macro_rules! g (($x:ident) =>
2007               ({macro_rules! f(($y:ident)=>({let $y=3;$x}));f!($x)}));
2008               fn a(){g!(z)}",
2009               vec!(vec!(0)),false),
2010             0)
2011     }
2012
2013     // match variable hygiene. Should expand into
2014     // fn z() {match 8 {x_1 => {match 9 {x_2 | x_2 if x_2 == x_1 => x_2 + x_1}}}}
2015     #[test]
2016     fn issue_9384(){
2017         run_renaming_test(
2018             &("macro_rules! bad_macro (($ex:expr) => ({match 9 {x | x if x == $ex => x + $ex}}));
2019               fn z() {match 8 {x => bad_macro!(x)}}",
2020               // NB: the third "binding" is the repeat of the second one.
2021               vec!(vec!(1,3),vec!(0,2),vec!(0,2)),
2022               true),
2023             0)
2024     }
2025
2026     // interpolated nodes weren't getting labeled.
2027     // should expand into
2028     // fn main(){let g1_1 = 13; g1_1}}
2029     #[test]
2030     fn pat_expand_issue_15221(){
2031         run_renaming_test(
2032             &("macro_rules! inner ( ($e:pat ) => ($e));
2033               macro_rules! outer ( ($e:pat ) => (inner!($e)));
2034               fn main() { let outer!(g) = 13; g;}",
2035               vec!(vec!(0)),
2036               true),
2037             0)
2038     }
2039
2040     // create a really evil test case where a $x appears inside a binding of $x
2041     // but *shouldn't* bind because it was inserted by a different macro....
2042     // can't write this test case until we have macro-generating macros.
2043
2044     // method arg hygiene
2045     // method expands to fn get_x(&self_0, x_1: i32) {self_0 + self_2 + x_3 + x_1}
2046     #[test]
2047     fn method_arg_hygiene(){
2048         run_renaming_test(
2049             &("macro_rules! inject_x (()=>(x));
2050               macro_rules! inject_self (()=>(self));
2051               struct A;
2052               impl A{fn get_x(&self, x: i32) {self + inject_self!() + inject_x!() + x;} }",
2053               vec!(vec!(0),vec!(3)),
2054               true),
2055             0)
2056     }
2057
2058     // ooh, got another bite?
2059     // expands to struct A; impl A {fn thingy(&self_1) {self_1;}}
2060     #[test]
2061     fn method_arg_hygiene_2(){
2062         run_renaming_test(
2063             &("struct A;
2064               macro_rules! add_method (($T:ty) =>
2065               (impl $T {  fn thingy(&self) {self;} }));
2066               add_method!(A);",
2067               vec!(vec!(0)),
2068               true),
2069             0)
2070     }
2071
2072     // item fn hygiene
2073     // expands to fn q(x_1: i32){fn g(x_2: i32){x_2 + x_1};}
2074     #[test]
2075     fn issue_9383(){
2076         run_renaming_test(
2077             &("macro_rules! bad_macro (($ex:expr) => (fn g(x: i32){ x + $ex }));
2078               fn q(x: i32) { bad_macro!(x); }",
2079               vec!(vec!(1),vec!(0)),true),
2080             0)
2081     }
2082
2083     // closure arg hygiene (ExprClosure)
2084     // expands to fn f(){(|x_1 : i32| {(x_2 + x_1)})(3);}
2085     #[test]
2086     fn closure_arg_hygiene(){
2087         run_renaming_test(
2088             &("macro_rules! inject_x (()=>(x));
2089             fn f(){(|x : i32| {(inject_x!() + x)})(3);}",
2090               vec!(vec!(1)),
2091               true),
2092             0)
2093     }
2094
2095     // macro_rules in method position. Sadly, unimplemented.
2096     #[test]
2097     fn macro_in_method_posn(){
2098         expand_crate_str(
2099             "macro_rules! my_method (() => (fn thirteen(&self) -> i32 {13}));
2100             struct A;
2101             impl A{ my_method!(); }
2102             fn f(){A.thirteen;}".to_string());
2103     }
2104
2105     // another nested macro
2106     // expands to impl Entries {fn size_hint(&self_1) {self_1;}
2107     #[test]
2108     fn item_macro_workaround(){
2109         run_renaming_test(
2110             &("macro_rules! item { ($i:item) => {$i}}
2111               struct Entries;
2112               macro_rules! iterator_impl {
2113               () => { item!( impl Entries { fn size_hint(&self) { self;}});}}
2114               iterator_impl! { }",
2115               vec!(vec!(0)), true),
2116             0)
2117     }
2118
2119     // run one of the renaming tests
2120     fn run_renaming_test(t: &RenamingTest, test_idx: usize) {
2121         let invalid_name = token::special_idents::invalid.name;
2122         let (teststr, bound_connections, bound_ident_check) = match *t {
2123             (ref str,ref conns, bic) => (str.to_string(), conns.clone(), bic)
2124         };
2125         let cr = expand_crate_str(teststr.to_string());
2126         let bindings = crate_bindings(&cr);
2127         let varrefs = crate_varrefs(&cr);
2128
2129         // must be one check clause for each binding:
2130         assert_eq!(bindings.len(),bound_connections.len());
2131         for (binding_idx,shouldmatch) in bound_connections.iter().enumerate() {
2132             let binding_name = mtwt::resolve(bindings[binding_idx]);
2133             let binding_marks = mtwt::marksof(bindings[binding_idx].ctxt, invalid_name);
2134             // shouldmatch can't name varrefs that don't exist:
2135             assert!((shouldmatch.is_empty()) ||
2136                     (varrefs.len() > *shouldmatch.iter().max().unwrap()));
2137             for (idx,varref) in varrefs.iter().enumerate() {
2138                 let print_hygiene_debug_info = || {
2139                     // good lord, you can't make a path with 0 segments, can you?
2140                     let final_varref_ident = match varref.segments.last() {
2141                         Some(pathsegment) => pathsegment.identifier,
2142                         None => panic!("varref with 0 path segments?")
2143                     };
2144                     let varref_name = mtwt::resolve(final_varref_ident);
2145                     let varref_idents : Vec<ast::Ident>
2146                         = varref.segments.iter().map(|s| s.identifier)
2147                         .collect();
2148                     println!("varref #{}: {:?}, resolves to {}",idx, varref_idents, varref_name);
2149                     println!("varref's first segment's string: \"{}\"", final_varref_ident);
2150                     println!("binding #{}: {}, resolves to {}",
2151                              binding_idx, bindings[binding_idx], binding_name);
2152                     mtwt::with_sctable(|x| mtwt::display_sctable(x));
2153                 };
2154                 if shouldmatch.contains(&idx) {
2155                     // it should be a path of length 1, and it should
2156                     // be free-identifier=? or bound-identifier=? to the given binding
2157                     assert_eq!(varref.segments.len(),1);
2158                     let varref_name = mtwt::resolve(varref.segments[0].identifier);
2159                     let varref_marks = mtwt::marksof(varref.segments[0]
2160                                                            .identifier
2161                                                            .ctxt,
2162                                                      invalid_name);
2163                     if !(varref_name==binding_name) {
2164                         println!("uh oh, should match but doesn't:");
2165                         print_hygiene_debug_info();
2166                     }
2167                     assert_eq!(varref_name,binding_name);
2168                     if bound_ident_check {
2169                         // we're checking bound-identifier=?, and the marks
2170                         // should be the same, too:
2171                         assert_eq!(varref_marks,binding_marks.clone());
2172                     }
2173                 } else {
2174                     let varref_name = mtwt::resolve(varref.segments[0].identifier);
2175                     let fail = (varref.segments.len() == 1)
2176                         && (varref_name == binding_name);
2177                     // temp debugging:
2178                     if fail {
2179                         println!("failure on test {}",test_idx);
2180                         println!("text of test case: \"{}\"", teststr);
2181                         println!("");
2182                         println!("uh oh, matches but shouldn't:");
2183                         print_hygiene_debug_info();
2184                     }
2185                     assert!(!fail);
2186                 }
2187             }
2188         }
2189     }
2190
2191     #[test]
2192     fn fmt_in_macro_used_inside_module_macro() {
2193         let crate_str = "macro_rules! fmt_wrap(($b:expr)=>($b.to_string()));
2194 macro_rules! foo_module (() => (mod generated { fn a() { let xx = 147; fmt_wrap!(xx);}}));
2195 foo_module!();
2196 ".to_string();
2197         let cr = expand_crate_str(crate_str);
2198         // find the xx binding
2199         let bindings = crate_bindings(&cr);
2200         let cxbinds: Vec<&ast::Ident> =
2201             bindings.iter().filter(|b| b.name == "xx").collect();
2202         let cxbinds: &[&ast::Ident] = &cxbinds[..];
2203         let cxbind = match (cxbinds.len(), cxbinds.get(0)) {
2204             (1, Some(b)) => *b,
2205             _ => panic!("expected just one binding for ext_cx")
2206         };
2207         let resolved_binding = mtwt::resolve(*cxbind);
2208         let varrefs = crate_varrefs(&cr);
2209
2210         // the xx binding should bind all of the xx varrefs:
2211         for (idx,v) in varrefs.iter().filter(|p| {
2212             p.segments.len() == 1
2213             && p.segments[0].identifier.name == "xx"
2214         }).enumerate() {
2215             if mtwt::resolve(v.segments[0].identifier) != resolved_binding {
2216                 println!("uh oh, xx binding didn't match xx varref:");
2217                 println!("this is xx varref \\# {}", idx);
2218                 println!("binding: {}", cxbind);
2219                 println!("resolves to: {}", resolved_binding);
2220                 println!("varref: {}", v.segments[0].identifier);
2221                 println!("resolves to: {}",
2222                          mtwt::resolve(v.segments[0].identifier));
2223                 mtwt::with_sctable(|x| mtwt::display_sctable(x));
2224             }
2225             assert_eq!(mtwt::resolve(v.segments[0].identifier),
2226                        resolved_binding);
2227         };
2228     }
2229
2230     #[test]
2231     fn pat_idents(){
2232         let pat = string_to_pat(
2233             "(a,Foo{x:c @ (b,9),y:Bar(4,d)})".to_string());
2234         let idents = pattern_bindings(&*pat);
2235         assert_eq!(idents, strs_to_idents(vec!("a","c","b","d")));
2236     }
2237
2238     // test the list of identifier patterns gathered by the visitor. Note that
2239     // 'None' is listed as an identifier pattern because we don't yet know that
2240     // it's the name of a 0-ary variant, and that 'i' appears twice in succession.
2241     #[test]
2242     fn crate_bindings_test(){
2243         let the_crate = string_to_crate("fn main (a: i32) -> i32 {|b| {
2244         match 34 {None => 3, Some(i) | i => j, Foo{k:z,l:y} => \"banana\"}} }".to_string());
2245         let idents = crate_bindings(&the_crate);
2246         assert_eq!(idents, strs_to_idents(vec!("a","b","None","i","i","z","y")));
2247     }
2248
2249     // test the IdentRenamer directly
2250     #[test]
2251     fn ident_renamer_test () {
2252         let the_crate = string_to_crate("fn f(x: i32){let x = x; x}".to_string());
2253         let f_ident = token::str_to_ident("f");
2254         let x_ident = token::str_to_ident("x");
2255         let int_ident = token::str_to_ident("i32");
2256         let renames = vec!((x_ident,Name(16)));
2257         let mut renamer = IdentRenamer{renames: &renames};
2258         let renamed_crate = renamer.fold_crate(the_crate);
2259         let idents = crate_idents(&renamed_crate);
2260         let resolved : Vec<ast::Name> = idents.iter().map(|id| mtwt::resolve(*id)).collect();
2261         assert_eq!(resolved, [f_ident.name,Name(16),int_ident.name,Name(16),Name(16),Name(16)]);
2262     }
2263
2264     // test the PatIdentRenamer; only PatIdents get renamed
2265     #[test]
2266     fn pat_ident_renamer_test () {
2267         let the_crate = string_to_crate("fn f(x: i32){let x = x; x}".to_string());
2268         let f_ident = token::str_to_ident("f");
2269         let x_ident = token::str_to_ident("x");
2270         let int_ident = token::str_to_ident("i32");
2271         let renames = vec!((x_ident,Name(16)));
2272         let mut renamer = PatIdentRenamer{renames: &renames};
2273         let renamed_crate = renamer.fold_crate(the_crate);
2274         let idents = crate_idents(&renamed_crate);
2275         let resolved : Vec<ast::Name> = idents.iter().map(|id| mtwt::resolve(*id)).collect();
2276         let x_name = x_ident.name;
2277         assert_eq!(resolved, [f_ident.name,Name(16),int_ident.name,Name(16),x_name,x_name]);
2278     }
2279 }