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.
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.
11 //! A Folder represents an AST->AST fold; it accepts an AST piece,
12 //! and returns a piece of the same type. So, for instance, macro
13 //! expansion is a Folder that walks over an AST and produces another
16 //! Note: using a Folder (other than the MacroExpander Folder) on
17 //! an AST before macro expansion is probably a bad idea. For instance,
18 //! a folder renaming item names in a module will miss all of those
19 //! that are created by the expansion of a macro.
24 use codemap::{respan, Span, Spanned};
25 use owned_slice::OwnedSlice;
29 use util::small_vector::SmallVector;
33 // This could have a better place to live.
34 pub trait MoveMap<T> {
35 fn move_map<F>(self, f: F) -> Self where F: FnMut(T) -> T;
38 impl<T> MoveMap<T> for Vec<T> {
39 fn move_map<F>(mut self, mut f: F) -> Vec<T> where F: FnMut(T) -> T {
40 for p in self.iter_mut() {
42 // FIXME(#5016) this shouldn't need to zero to be safe.
43 ptr::write(p, f(ptr::read_and_zero(p)));
50 impl<T> MoveMap<T> for OwnedSlice<T> {
51 fn move_map<F>(self, f: F) -> OwnedSlice<T> where F: FnMut(T) -> T {
52 OwnedSlice::from_vec(self.into_vec().move_map(f))
56 pub trait Folder : Sized {
57 // Any additions to this trait should happen in form
58 // of a call to a public `noop_*` function that only calls
59 // out to the folder again, not other `noop_*` functions.
61 // This is a necessary API workaround to the problem of not
62 // being able to call out to the super default method
63 // in an overridden default method.
65 fn fold_crate(&mut self, c: Crate) -> Crate {
66 noop_fold_crate(c, self)
69 fn fold_meta_items(&mut self, meta_items: Vec<P<MetaItem>>) -> Vec<P<MetaItem>> {
70 noop_fold_meta_items(meta_items, self)
73 fn fold_meta_item(&mut self, meta_item: P<MetaItem>) -> P<MetaItem> {
74 noop_fold_meta_item(meta_item, self)
77 fn fold_view_path(&mut self, view_path: P<ViewPath>) -> P<ViewPath> {
78 noop_fold_view_path(view_path, self)
81 fn fold_view_item(&mut self, vi: ViewItem) -> ViewItem {
82 noop_fold_view_item(vi, self)
85 fn fold_foreign_item(&mut self, ni: P<ForeignItem>) -> P<ForeignItem> {
86 noop_fold_foreign_item(ni, self)
89 fn fold_item(&mut self, i: P<Item>) -> SmallVector<P<Item>> {
90 noop_fold_item(i, self)
93 fn fold_item_simple(&mut self, i: Item) -> Item {
94 noop_fold_item_simple(i, self)
97 fn fold_struct_field(&mut self, sf: StructField) -> StructField {
98 noop_fold_struct_field(sf, self)
101 fn fold_item_underscore(&mut self, i: Item_) -> Item_ {
102 noop_fold_item_underscore(i, self)
105 fn fold_trait_item(&mut self, i: TraitItem) -> SmallVector<TraitItem> {
106 noop_fold_trait_item(i, self)
109 fn fold_impl_item(&mut self, i: ImplItem) -> SmallVector<ImplItem> {
110 noop_fold_impl_item(i, self)
113 fn fold_fn_decl(&mut self, d: P<FnDecl>) -> P<FnDecl> {
114 noop_fold_fn_decl(d, self)
117 fn fold_type_method(&mut self, m: TypeMethod) -> TypeMethod {
118 noop_fold_type_method(m, self)
121 fn fold_method(&mut self, m: P<Method>) -> SmallVector<P<Method>> {
122 noop_fold_method(m, self)
125 fn fold_block(&mut self, b: P<Block>) -> P<Block> {
126 noop_fold_block(b, self)
129 fn fold_stmt(&mut self, s: P<Stmt>) -> SmallVector<P<Stmt>> {
130 s.and_then(|s| noop_fold_stmt(s, self))
133 fn fold_arm(&mut self, a: Arm) -> Arm {
134 noop_fold_arm(a, self)
137 fn fold_pat(&mut self, p: P<Pat>) -> P<Pat> {
138 noop_fold_pat(p, self)
141 fn fold_decl(&mut self, d: P<Decl>) -> SmallVector<P<Decl>> {
142 noop_fold_decl(d, self)
145 fn fold_expr(&mut self, e: P<Expr>) -> P<Expr> {
146 e.map(|e| noop_fold_expr(e, self))
149 fn fold_ty(&mut self, t: P<Ty>) -> P<Ty> {
150 noop_fold_ty(t, self)
153 fn fold_qpath(&mut self, t: P<QPath>) -> P<QPath> {
154 noop_fold_qpath(t, self)
157 fn fold_ty_binding(&mut self, t: P<TypeBinding>) -> P<TypeBinding> {
158 noop_fold_ty_binding(t, self)
161 fn fold_mod(&mut self, m: Mod) -> Mod {
162 noop_fold_mod(m, self)
165 fn fold_foreign_mod(&mut self, nm: ForeignMod) -> ForeignMod {
166 noop_fold_foreign_mod(nm, self)
169 fn fold_variant(&mut self, v: P<Variant>) -> P<Variant> {
170 noop_fold_variant(v, self)
173 fn fold_ident(&mut self, i: Ident) -> Ident {
174 noop_fold_ident(i, self)
177 fn fold_uint(&mut self, i: uint) -> uint {
178 noop_fold_uint(i, self)
181 fn fold_path(&mut self, p: Path) -> Path {
182 noop_fold_path(p, self)
185 fn fold_path_parameters(&mut self, p: PathParameters) -> PathParameters {
186 noop_fold_path_parameters(p, self)
189 fn fold_angle_bracketed_parameter_data(&mut self, p: AngleBracketedParameterData)
190 -> AngleBracketedParameterData
192 noop_fold_angle_bracketed_parameter_data(p, self)
195 fn fold_parenthesized_parameter_data(&mut self, p: ParenthesizedParameterData)
196 -> ParenthesizedParameterData
198 noop_fold_parenthesized_parameter_data(p, self)
201 fn fold_local(&mut self, l: P<Local>) -> P<Local> {
202 noop_fold_local(l, self)
205 fn fold_mac(&mut self, _mac: Mac) -> Mac {
206 panic!("fold_mac disabled by default");
207 // NB: see note about macros above.
208 // if you really want a folder that
209 // works on macros, use this
210 // definition in your trait impl:
211 // fold::noop_fold_mac(_mac, self)
214 fn fold_explicit_self(&mut self, es: ExplicitSelf) -> ExplicitSelf {
215 noop_fold_explicit_self(es, self)
218 fn fold_explicit_self_underscore(&mut self, es: ExplicitSelf_) -> ExplicitSelf_ {
219 noop_fold_explicit_self_underscore(es, self)
222 fn fold_lifetime(&mut self, l: Lifetime) -> Lifetime {
223 noop_fold_lifetime(l, self)
226 fn fold_lifetime_def(&mut self, l: LifetimeDef) -> LifetimeDef {
227 noop_fold_lifetime_def(l, self)
230 fn fold_attribute(&mut self, at: Attribute) -> Attribute {
231 noop_fold_attribute(at, self)
234 fn fold_arg(&mut self, a: Arg) -> Arg {
235 noop_fold_arg(a, self)
238 fn fold_generics(&mut self, generics: Generics) -> Generics {
239 noop_fold_generics(generics, self)
242 fn fold_trait_ref(&mut self, p: TraitRef) -> TraitRef {
243 noop_fold_trait_ref(p, self)
246 fn fold_poly_trait_ref(&mut self, p: PolyTraitRef) -> PolyTraitRef {
247 noop_fold_poly_trait_ref(p, self)
250 fn fold_struct_def(&mut self, struct_def: P<StructDef>) -> P<StructDef> {
251 noop_fold_struct_def(struct_def, self)
254 fn fold_lifetimes(&mut self, lts: Vec<Lifetime>) -> Vec<Lifetime> {
255 noop_fold_lifetimes(lts, self)
258 fn fold_lifetime_defs(&mut self, lts: Vec<LifetimeDef>) -> Vec<LifetimeDef> {
259 noop_fold_lifetime_defs(lts, self)
262 fn fold_ty_param(&mut self, tp: TyParam) -> TyParam {
263 noop_fold_ty_param(tp, self)
266 fn fold_ty_params(&mut self, tps: OwnedSlice<TyParam>) -> OwnedSlice<TyParam> {
267 noop_fold_ty_params(tps, self)
270 fn fold_tt(&mut self, tt: &TokenTree) -> TokenTree {
271 noop_fold_tt(tt, self)
274 fn fold_tts(&mut self, tts: &[TokenTree]) -> Vec<TokenTree> {
275 noop_fold_tts(tts, self)
278 fn fold_token(&mut self, t: token::Token) -> token::Token {
279 noop_fold_token(t, self)
282 fn fold_interpolated(&mut self, nt: token::Nonterminal) -> token::Nonterminal {
283 noop_fold_interpolated(nt, self)
286 fn fold_opt_lifetime(&mut self, o_lt: Option<Lifetime>) -> Option<Lifetime> {
287 noop_fold_opt_lifetime(o_lt, self)
290 fn fold_variant_arg(&mut self, va: VariantArg) -> VariantArg {
291 noop_fold_variant_arg(va, self)
294 fn fold_opt_bounds(&mut self, b: Option<OwnedSlice<TyParamBound>>)
295 -> Option<OwnedSlice<TyParamBound>> {
296 noop_fold_opt_bounds(b, self)
299 fn fold_bounds(&mut self, b: OwnedSlice<TyParamBound>)
300 -> OwnedSlice<TyParamBound> {
301 noop_fold_bounds(b, self)
304 fn fold_ty_param_bound(&mut self, tpb: TyParamBound) -> TyParamBound {
305 noop_fold_ty_param_bound(tpb, self)
308 fn fold_mt(&mut self, mt: MutTy) -> MutTy {
309 noop_fold_mt(mt, self)
312 fn fold_field(&mut self, field: Field) -> Field {
313 noop_fold_field(field, self)
316 fn fold_where_clause(&mut self, where_clause: WhereClause)
318 noop_fold_where_clause(where_clause, self)
321 fn fold_where_predicate(&mut self, where_predicate: WherePredicate)
323 noop_fold_where_predicate(where_predicate, self)
326 fn fold_typedef(&mut self, typedef: Typedef) -> Typedef {
327 noop_fold_typedef(typedef, self)
330 fn fold_associated_type(&mut self, associated_type: AssociatedType)
332 noop_fold_associated_type(associated_type, self)
335 fn new_id(&mut self, i: NodeId) -> NodeId {
339 fn new_span(&mut self, sp: Span) -> Span {
344 pub fn noop_fold_meta_items<T: Folder>(meta_items: Vec<P<MetaItem>>, fld: &mut T)
345 -> Vec<P<MetaItem>> {
346 meta_items.move_map(|x| fld.fold_meta_item(x))
349 pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<ViewPath> {
350 view_path.map(|Spanned {node, span}| Spanned {
352 ViewPathSimple(ident, path, node_id) => {
353 let id = fld.new_id(node_id);
354 ViewPathSimple(ident, fld.fold_path(path), id)
356 ViewPathGlob(path, node_id) => {
357 let id = fld.new_id(node_id);
358 ViewPathGlob(fld.fold_path(path), id)
360 ViewPathList(path, path_list_idents, node_id) => {
361 let id = fld.new_id(node_id);
362 ViewPathList(fld.fold_path(path),
363 path_list_idents.move_map(|path_list_ident| {
365 node: match path_list_ident.node {
366 PathListIdent { id, name } =>
371 PathListMod { id } =>
372 PathListMod { id: fld.new_id(id) }
374 span: fld.new_span(path_list_ident.span)
380 span: fld.new_span(span)
384 pub fn noop_fold_arm<T: Folder>(Arm {attrs, pats, guard, body}: Arm, fld: &mut T) -> Arm {
386 attrs: attrs.move_map(|x| fld.fold_attribute(x)),
387 pats: pats.move_map(|x| fld.fold_pat(x)),
388 guard: guard.map(|x| fld.fold_expr(x)),
389 body: fld.fold_expr(body),
393 pub fn noop_fold_decl<T: Folder>(d: P<Decl>, fld: &mut T) -> SmallVector<P<Decl>> {
394 d.and_then(|Spanned {node, span}| match node {
395 DeclLocal(l) => SmallVector::one(P(Spanned {
396 node: DeclLocal(fld.fold_local(l)),
397 span: fld.new_span(span)
399 DeclItem(it) => fld.fold_item(it).into_iter().map(|i| P(Spanned {
401 span: fld.new_span(span)
406 pub fn noop_fold_ty_binding<T: Folder>(b: P<TypeBinding>, fld: &mut T) -> P<TypeBinding> {
407 b.map(|TypeBinding { id, ident, ty, span }| TypeBinding {
411 span: fld.new_span(span),
415 pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> {
416 t.map(|Ty {id, node, span}| Ty {
420 TyVec(ty) => TyVec(fld.fold_ty(ty)),
421 TyPtr(mt) => TyPtr(fld.fold_mt(mt)),
422 TyRptr(region, mt) => {
423 TyRptr(fld.fold_opt_lifetime(region), fld.fold_mt(mt))
426 TyBareFn(f.map(|BareFnTy {lifetimes, unsafety, abi, decl}| BareFnTy {
427 lifetimes: fld.fold_lifetime_defs(lifetimes),
430 decl: fld.fold_fn_decl(decl)
433 TyTup(tys) => TyTup(tys.move_map(|ty| fld.fold_ty(ty))),
434 TyParen(ty) => TyParen(fld.fold_ty(ty)),
435 TyPath(path, id) => {
436 let id = fld.new_id(id);
437 TyPath(fld.fold_path(path), id)
439 TyObjectSum(ty, bounds) => {
440 TyObjectSum(fld.fold_ty(ty),
441 fld.fold_bounds(bounds))
444 TyQPath(fld.fold_qpath(qpath))
446 TyFixedLengthVec(ty, e) => {
447 TyFixedLengthVec(fld.fold_ty(ty), fld.fold_expr(e))
450 TyTypeof(fld.fold_expr(expr))
452 TyPolyTraitRef(bounds) => {
453 TyPolyTraitRef(bounds.move_map(|b| fld.fold_ty_param_bound(b)))
456 span: fld.new_span(span)
460 pub fn noop_fold_qpath<T: Folder>(qpath: P<QPath>, fld: &mut T) -> P<QPath> {
463 self_type: fld.fold_ty(qpath.self_type),
464 trait_ref: qpath.trait_ref.map(|tr| fld.fold_trait_ref(tr)),
465 item_path: PathSegment {
466 identifier: fld.fold_ident(qpath.item_path.identifier),
467 parameters: fld.fold_path_parameters(qpath.item_path.parameters),
473 pub fn noop_fold_foreign_mod<T: Folder>(ForeignMod {abi, view_items, items}: ForeignMod,
474 fld: &mut T) -> ForeignMod {
477 view_items: view_items.move_map(|x| fld.fold_view_item(x)),
478 items: items.move_map(|x| fld.fold_foreign_item(x)),
482 pub fn noop_fold_variant<T: Folder>(v: P<Variant>, fld: &mut T) -> P<Variant> {
483 v.map(|Spanned {node: Variant_ {id, name, attrs, kind, disr_expr, vis}, span}| Spanned {
487 attrs: attrs.move_map(|x| fld.fold_attribute(x)),
489 TupleVariantKind(variant_args) => {
490 TupleVariantKind(variant_args.move_map(|x|
491 fld.fold_variant_arg(x)))
493 StructVariantKind(struct_def) => {
494 StructVariantKind(fld.fold_struct_def(struct_def))
497 disr_expr: disr_expr.map(|e| fld.fold_expr(e)),
500 span: fld.new_span(span),
504 pub fn noop_fold_ident<T: Folder>(i: Ident, _: &mut T) -> Ident {
508 pub fn noop_fold_uint<T: Folder>(i: uint, _: &mut T) -> uint {
512 pub fn noop_fold_path<T: Folder>(Path {global, segments, span}: Path, fld: &mut T) -> Path {
515 segments: segments.move_map(|PathSegment {identifier, parameters}| PathSegment {
516 identifier: fld.fold_ident(identifier),
517 parameters: fld.fold_path_parameters(parameters),
519 span: fld.new_span(span)
523 pub fn noop_fold_path_parameters<T: Folder>(path_parameters: PathParameters, fld: &mut T)
526 match path_parameters {
527 AngleBracketedParameters(data) =>
528 AngleBracketedParameters(fld.fold_angle_bracketed_parameter_data(data)),
529 ParenthesizedParameters(data) =>
530 ParenthesizedParameters(fld.fold_parenthesized_parameter_data(data)),
534 pub fn noop_fold_angle_bracketed_parameter_data<T: Folder>(data: AngleBracketedParameterData,
536 -> AngleBracketedParameterData
538 let AngleBracketedParameterData { lifetimes, types, bindings } = data;
539 AngleBracketedParameterData { lifetimes: fld.fold_lifetimes(lifetimes),
540 types: types.move_map(|ty| fld.fold_ty(ty)),
541 bindings: bindings.move_map(|b| fld.fold_ty_binding(b)) }
544 pub fn noop_fold_parenthesized_parameter_data<T: Folder>(data: ParenthesizedParameterData,
546 -> ParenthesizedParameterData
548 let ParenthesizedParameterData { inputs, output } = data;
549 ParenthesizedParameterData { inputs: inputs.move_map(|ty| fld.fold_ty(ty)),
550 output: output.map(|ty| fld.fold_ty(ty)) }
553 pub fn noop_fold_local<T: Folder>(l: P<Local>, fld: &mut T) -> P<Local> {
554 l.map(|Local {id, pat, ty, init, source, span}| Local {
556 ty: ty.map(|t| fld.fold_ty(t)),
557 pat: fld.fold_pat(pat),
558 init: init.map(|e| fld.fold_expr(e)),
560 span: fld.new_span(span)
564 pub fn noop_fold_attribute<T: Folder>(at: Attribute, fld: &mut T) -> Attribute {
565 let Spanned {node: Attribute_ {id, style, value, is_sugared_doc}, span} = at;
570 value: fld.fold_meta_item(value),
571 is_sugared_doc: is_sugared_doc
573 span: fld.new_span(span)
577 pub fn noop_fold_explicit_self_underscore<T: Folder>(es: ExplicitSelf_, fld: &mut T)
580 SelfStatic | SelfValue(_) => es,
581 SelfRegion(lifetime, m, ident) => {
582 SelfRegion(fld.fold_opt_lifetime(lifetime), m, ident)
584 SelfExplicit(typ, ident) => {
585 SelfExplicit(fld.fold_ty(typ), ident)
590 pub fn noop_fold_explicit_self<T: Folder>(Spanned {span, node}: ExplicitSelf, fld: &mut T)
593 node: fld.fold_explicit_self_underscore(node),
594 span: fld.new_span(span)
599 pub fn noop_fold_mac<T: Folder>(Spanned {node, span}: Mac, fld: &mut T) -> Mac {
602 MacInvocTT(p, tts, ctxt) => {
603 MacInvocTT(fld.fold_path(p), fld.fold_tts(tts.as_slice()), ctxt)
606 span: fld.new_span(span)
610 pub fn noop_fold_meta_item<T: Folder>(mi: P<MetaItem>, fld: &mut T) -> P<MetaItem> {
611 mi.map(|Spanned {node, span}| Spanned {
613 MetaWord(id) => MetaWord(id),
614 MetaList(id, mis) => {
615 MetaList(id, mis.move_map(|e| fld.fold_meta_item(e)))
617 MetaNameValue(id, s) => MetaNameValue(id, s)
619 span: fld.new_span(span)
623 pub fn noop_fold_arg<T: Folder>(Arg {id, pat, ty}: Arg, fld: &mut T) -> Arg {
626 pat: fld.fold_pat(pat),
631 pub fn noop_fold_tt<T: Folder>(tt: &TokenTree, fld: &mut T) -> TokenTree {
633 TtToken(span, ref tok) =>
634 TtToken(span, fld.fold_token(tok.clone())),
635 TtDelimited(span, ref delimed) => {
636 TtDelimited(span, Rc::new(
638 delim: delimed.delim,
639 open_span: delimed.open_span,
640 tts: fld.fold_tts(delimed.tts.as_slice()),
641 close_span: delimed.close_span,
645 TtSequence(span, ref seq) =>
647 Rc::new(SequenceRepetition {
648 tts: fld.fold_tts(seq.tts.as_slice()),
649 separator: seq.separator.clone().map(|tok| fld.fold_token(tok)),
655 pub fn noop_fold_tts<T: Folder>(tts: &[TokenTree], fld: &mut T) -> Vec<TokenTree> {
656 tts.iter().map(|tt| fld.fold_tt(tt)).collect()
659 // apply ident folder if it's an ident, apply other folds to interpolated nodes
660 pub fn noop_fold_token<T: Folder>(t: token::Token, fld: &mut T) -> token::Token {
662 token::Ident(id, followed_by_colons) => {
663 token::Ident(fld.fold_ident(id), followed_by_colons)
665 token::Lifetime(id) => token::Lifetime(fld.fold_ident(id)),
666 token::Interpolated(nt) => token::Interpolated(fld.fold_interpolated(nt)),
667 token::SubstNt(ident, namep) => {
668 token::SubstNt(fld.fold_ident(ident), namep)
670 token::MatchNt(name, kind, namep, kindp) => {
671 token::MatchNt(fld.fold_ident(name), fld.fold_ident(kind), namep, kindp)
677 /// apply folder to elements of interpolated nodes
679 // NB: this can occur only when applying a fold to partially expanded code, where
680 // parsed pieces have gotten implanted ito *other* macro invocations. This is relevant
681 // for macro hygiene, but possibly not elsewhere.
683 // One problem here occurs because the types for fold_item, fold_stmt, etc. allow the
684 // folder to return *multiple* items; this is a problem for the nodes here, because
685 // they insist on having exactly one piece. One solution would be to mangle the fold
686 // trait to include one-to-many and one-to-one versions of these entry points, but that
687 // would probably confuse a lot of people and help very few. Instead, I'm just going
688 // to put in dynamic checks. I think the performance impact of this will be pretty much
689 // nonexistent. The danger is that someone will apply a fold to a partially expanded
690 // node, and will be confused by the fact that their "fold_item" or "fold_stmt" isn't
691 // getting called on NtItem or NtStmt nodes. Hopefully they'll wind up reading this
692 // comment, and doing something appropriate.
694 // BTW, design choice: I considered just changing the type of, e.g., NtItem to contain
695 // multiple items, but decided against it when I looked at parse_item_or_view_item and
696 // tried to figure out what I would do with multiple items there....
697 pub fn noop_fold_interpolated<T: Folder>(nt: token::Nonterminal, fld: &mut T)
698 -> token::Nonterminal {
700 token::NtItem(item) =>
701 token::NtItem(fld.fold_item(item)
702 // this is probably okay, because the only folds likely
703 // to peek inside interpolated nodes will be renamings/markings,
704 // which map single items to single items
705 .expect_one("expected fold to produce exactly one item")),
706 token::NtBlock(block) => token::NtBlock(fld.fold_block(block)),
707 token::NtStmt(stmt) =>
708 token::NtStmt(fld.fold_stmt(stmt)
709 // this is probably okay, because the only folds likely
710 // to peek inside interpolated nodes will be renamings/markings,
711 // which map single items to single items
712 .expect_one("expected fold to produce exactly one statement")),
713 token::NtPat(pat) => token::NtPat(fld.fold_pat(pat)),
714 token::NtExpr(expr) => token::NtExpr(fld.fold_expr(expr)),
715 token::NtTy(ty) => token::NtTy(fld.fold_ty(ty)),
716 token::NtIdent(box id, is_mod_name) =>
717 token::NtIdent(box fld.fold_ident(id), is_mod_name),
718 token::NtMeta(meta_item) => token::NtMeta(fld.fold_meta_item(meta_item)),
719 token::NtPath(box path) => token::NtPath(box fld.fold_path(path)),
720 token::NtTT(tt) => token::NtTT(P(fld.fold_tt(&*tt))),
724 pub fn noop_fold_fn_decl<T: Folder>(decl: P<FnDecl>, fld: &mut T) -> P<FnDecl> {
725 decl.map(|FnDecl {inputs, output, variadic}| FnDecl {
726 inputs: inputs.move_map(|x| fld.fold_arg(x)),
727 output: match output {
728 Return(ty) => Return(fld.fold_ty(ty)),
729 DefaultReturn(span) => DefaultReturn(span),
730 NoReturn(span) => NoReturn(span)
736 pub fn noop_fold_ty_param_bound<T>(tpb: TyParamBound, fld: &mut T)
740 TraitTyParamBound(ty, modifier) => TraitTyParamBound(fld.fold_poly_trait_ref(ty), modifier),
741 RegionTyParamBound(lifetime) => RegionTyParamBound(fld.fold_lifetime(lifetime)),
745 pub fn noop_fold_ty_param<T: Folder>(tp: TyParam, fld: &mut T) -> TyParam {
746 let TyParam {id, ident, bounds, default, span} = tp;
750 bounds: fld.fold_bounds(bounds),
751 default: default.map(|x| fld.fold_ty(x)),
756 pub fn noop_fold_ty_params<T: Folder>(tps: OwnedSlice<TyParam>, fld: &mut T)
757 -> OwnedSlice<TyParam> {
758 tps.move_map(|tp| fld.fold_ty_param(tp))
761 pub fn noop_fold_lifetime<T: Folder>(l: Lifetime, fld: &mut T) -> Lifetime {
763 id: fld.new_id(l.id),
765 span: fld.new_span(l.span)
769 pub fn noop_fold_lifetime_def<T: Folder>(l: LifetimeDef, fld: &mut T)
772 lifetime: fld.fold_lifetime(l.lifetime),
773 bounds: fld.fold_lifetimes(l.bounds),
777 pub fn noop_fold_lifetimes<T: Folder>(lts: Vec<Lifetime>, fld: &mut T) -> Vec<Lifetime> {
778 lts.move_map(|l| fld.fold_lifetime(l))
781 pub fn noop_fold_lifetime_defs<T: Folder>(lts: Vec<LifetimeDef>, fld: &mut T)
782 -> Vec<LifetimeDef> {
783 lts.move_map(|l| fld.fold_lifetime_def(l))
786 pub fn noop_fold_opt_lifetime<T: Folder>(o_lt: Option<Lifetime>, fld: &mut T)
787 -> Option<Lifetime> {
788 o_lt.map(|lt| fld.fold_lifetime(lt))
791 pub fn noop_fold_generics<T: Folder>(Generics {ty_params, lifetimes, where_clause}: Generics,
792 fld: &mut T) -> Generics {
794 ty_params: fld.fold_ty_params(ty_params),
795 lifetimes: fld.fold_lifetime_defs(lifetimes),
796 where_clause: fld.fold_where_clause(where_clause),
800 pub fn noop_fold_where_clause<T: Folder>(
801 WhereClause {id, predicates}: WhereClause,
806 predicates: predicates.move_map(|predicate| {
807 fld.fold_where_predicate(predicate)
812 pub fn noop_fold_where_predicate<T: Folder>(
813 pred: WherePredicate,
817 ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate{bounded_ty,
820 ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
821 bounded_ty: fld.fold_ty(bounded_ty),
822 bounds: bounds.move_map(|x| fld.fold_ty_param_bound(x)),
823 span: fld.new_span(span)
826 ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate{lifetime,
829 ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
830 span: fld.new_span(span),
831 lifetime: fld.fold_lifetime(lifetime),
832 bounds: bounds.move_map(|bound| fld.fold_lifetime(bound))
835 ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{id,
839 ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{
841 path: fld.fold_path(path),
843 span: fld.new_span(span)
849 pub fn noop_fold_typedef<T>(t: Typedef, folder: &mut T)
852 let new_id = folder.new_id(t.id);
853 let new_span = folder.new_span(t.span);
854 let new_attrs = t.attrs.iter().map(|attr| {
855 folder.fold_attribute((*attr).clone())
857 let new_ident = folder.fold_ident(t.ident);
858 let new_type = folder.fold_ty(t.typ);
869 pub fn noop_fold_associated_type<T>(at: AssociatedType, folder: &mut T)
873 let new_attrs = at.attrs
875 .map(|attr| folder.fold_attribute((*attr).clone()))
877 let new_param = folder.fold_ty_param(at.ty_param);
878 ast::AssociatedType {
884 pub fn noop_fold_struct_def<T: Folder>(struct_def: P<StructDef>, fld: &mut T) -> P<StructDef> {
885 struct_def.map(|StructDef { fields, ctor_id }| StructDef {
886 fields: fields.move_map(|f| fld.fold_struct_field(f)),
887 ctor_id: ctor_id.map(|cid| fld.new_id(cid)),
891 pub fn noop_fold_trait_ref<T: Folder>(p: TraitRef, fld: &mut T) -> TraitRef {
892 let id = fld.new_id(p.ref_id);
898 path: fld.fold_path(path),
903 pub fn noop_fold_poly_trait_ref<T: Folder>(p: PolyTraitRef, fld: &mut T) -> PolyTraitRef {
905 bound_lifetimes: fld.fold_lifetime_defs(p.bound_lifetimes),
906 trait_ref: fld.fold_trait_ref(p.trait_ref)
910 pub fn noop_fold_struct_field<T: Folder>(f: StructField, fld: &mut T) -> StructField {
911 let StructField {node: StructField_ {id, kind, ty, attrs}, span} = f;
917 attrs: attrs.move_map(|a| fld.fold_attribute(a))
919 span: fld.new_span(span)
923 pub fn noop_fold_field<T: Folder>(Field {ident, expr, span}: Field, folder: &mut T) -> Field {
925 ident: respan(ident.span, folder.fold_ident(ident.node)),
926 expr: folder.fold_expr(expr),
927 span: folder.new_span(span)
931 pub fn noop_fold_mt<T: Folder>(MutTy {ty, mutbl}: MutTy, folder: &mut T) -> MutTy {
933 ty: folder.fold_ty(ty),
938 pub fn noop_fold_opt_bounds<T: Folder>(b: Option<OwnedSlice<TyParamBound>>, folder: &mut T)
939 -> Option<OwnedSlice<TyParamBound>> {
940 b.map(|bounds| folder.fold_bounds(bounds))
943 fn noop_fold_bounds<T: Folder>(bounds: TyParamBounds, folder: &mut T)
945 bounds.move_map(|bound| folder.fold_ty_param_bound(bound))
948 fn noop_fold_variant_arg<T: Folder>(VariantArg {id, ty}: VariantArg, folder: &mut T)
951 id: folder.new_id(id),
952 ty: folder.fold_ty(ty)
956 pub fn noop_fold_view_item<T: Folder>(ViewItem {node, attrs, vis, span}: ViewItem,
957 folder: &mut T) -> ViewItem {
960 ViewItemExternCrate(ident, string, node_id) => {
961 ViewItemExternCrate(ident, string,
962 folder.new_id(node_id))
964 ViewItemUse(view_path) => {
965 ViewItemUse(folder.fold_view_path(view_path))
968 attrs: attrs.move_map(|a| folder.fold_attribute(a)),
970 span: folder.new_span(span)
974 pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> {
975 b.map(|Block {id, view_items, stmts, expr, rules, span}| Block {
976 id: folder.new_id(id),
977 view_items: view_items.move_map(|x| folder.fold_view_item(x)),
978 stmts: stmts.into_iter().flat_map(|s| folder.fold_stmt(s).into_iter()).collect(),
979 expr: expr.map(|x| folder.fold_expr(x)),
981 span: folder.new_span(span),
985 pub fn noop_fold_item_underscore<T: Folder>(i: Item_, folder: &mut T) -> Item_ {
987 ItemStatic(t, m, e) => {
988 ItemStatic(folder.fold_ty(t), m, folder.fold_expr(e))
991 ItemConst(folder.fold_ty(t), folder.fold_expr(e))
993 ItemFn(decl, unsafety, abi, generics, body) => {
995 folder.fold_fn_decl(decl),
998 folder.fold_generics(generics),
999 folder.fold_block(body)
1002 ItemMod(m) => ItemMod(folder.fold_mod(m)),
1003 ItemForeignMod(nm) => ItemForeignMod(folder.fold_foreign_mod(nm)),
1004 ItemTy(t, generics) => {
1005 ItemTy(folder.fold_ty(t), folder.fold_generics(generics))
1007 ItemEnum(enum_definition, generics) => {
1010 variants: enum_definition.variants.move_map(|x| folder.fold_variant(x)),
1012 folder.fold_generics(generics))
1014 ItemStruct(struct_def, generics) => {
1015 let struct_def = folder.fold_struct_def(struct_def);
1016 ItemStruct(struct_def, folder.fold_generics(generics))
1018 ItemImpl(unsafety, polarity, generics, ifce, ty, impl_items) => {
1019 let new_impl_items = impl_items.into_iter().flat_map(|item| {
1020 folder.fold_impl_item(item).into_iter()
1022 let ifce = match ifce {
1024 Some(ref trait_ref) => {
1025 Some(folder.fold_trait_ref((*trait_ref).clone()))
1030 folder.fold_generics(generics),
1035 ItemTrait(unsafety, generics, bounds, items) => {
1036 let bounds = folder.fold_bounds(bounds);
1037 let items = items.into_iter().flat_map(|item| {
1038 folder.fold_trait_item(item).into_iter()
1041 folder.fold_generics(generics),
1045 ItemMac(m) => ItemMac(folder.fold_mac(m)),
1049 pub fn noop_fold_trait_item<T: Folder>(i: TraitItem, folder: &mut T) -> SmallVector<TraitItem> {
1051 RequiredMethod(m) => {
1052 SmallVector::one(RequiredMethod(
1053 folder.fold_type_method(m)))
1055 ProvidedMethod(method) => {
1056 folder.fold_method(method).into_iter()
1057 .map(|m| ProvidedMethod(m)).collect()
1059 TypeTraitItem(at) => {
1060 SmallVector::one(TypeTraitItem(P(
1061 folder.fold_associated_type(
1067 pub fn noop_fold_impl_item<T: Folder>(i: ImplItem, folder: &mut T) -> SmallVector<ImplItem> {
1069 MethodImplItem(ref x) => {
1070 folder.fold_method((*x).clone()).into_iter().map(|m| MethodImplItem(m)).collect()
1072 TypeImplItem(ref t) => {
1073 SmallVector::one(TypeImplItem(
1074 P(folder.fold_typedef((**t).clone()))))
1079 pub fn noop_fold_type_method<T: Folder>(m: TypeMethod, fld: &mut T) -> TypeMethod {
1094 ident: fld.fold_ident(ident),
1095 attrs: attrs.move_map(|a| fld.fold_attribute(a)),
1098 decl: fld.fold_fn_decl(decl),
1099 generics: fld.fold_generics(generics),
1100 explicit_self: fld.fold_explicit_self(explicit_self),
1102 span: fld.new_span(span)
1106 pub fn noop_fold_mod<T: Folder>(Mod {inner, view_items, items}: Mod, folder: &mut T) -> Mod {
1108 inner: folder.new_span(inner),
1109 view_items: view_items.move_map(|x| folder.fold_view_item(x)),
1110 items: items.into_iter().flat_map(|x| folder.fold_item(x).into_iter()).collect(),
1114 pub fn noop_fold_crate<T: Folder>(Crate {module, attrs, config, mut exported_macros, span}: Crate,
1115 folder: &mut T) -> Crate {
1116 let config = folder.fold_meta_items(config);
1118 let mut items = folder.fold_item(P(ast::Item {
1119 ident: token::special_idents::invalid,
1121 id: ast::DUMMY_NODE_ID,
1124 node: ast::ItemMod(module),
1127 let (module, attrs, span) = match items.next() {
1129 assert!(items.next().is_none(),
1130 "a crate cannot expand to more than one item");
1131 item.and_then(|ast::Item { attrs, span, node, .. }| {
1133 ast::ItemMod(m) => (m, attrs, span),
1134 _ => panic!("fold converted a module to not a module"),
1140 view_items: Vec::new(),
1142 }, Vec::new(), span)
1145 for def in exported_macros.iter_mut() {
1146 def.id = folder.new_id(def.id);
1153 exported_macros: exported_macros,
1158 // fold one item into possibly many items
1159 pub fn noop_fold_item<T: Folder>(i: P<Item>, folder: &mut T) -> SmallVector<P<Item>> {
1160 SmallVector::one(i.map(|i| folder.fold_item_simple(i)))
1163 // fold one item into exactly one item
1164 pub fn noop_fold_item_simple<T: Folder>(Item {id, ident, attrs, node, vis, span}: Item,
1165 folder: &mut T) -> Item {
1166 let id = folder.new_id(id);
1167 let node = folder.fold_item_underscore(node);
1168 let ident = match node {
1169 // The node may have changed, recompute the "pretty" impl name.
1170 ItemImpl(_, _, _, ref maybe_trait, ref ty, _) => {
1171 ast_util::impl_pretty_name(maybe_trait, &**ty)
1178 ident: folder.fold_ident(ident),
1179 attrs: attrs.move_map(|e| folder.fold_attribute(e)),
1182 span: folder.new_span(span)
1186 pub fn noop_fold_foreign_item<T: Folder>(ni: P<ForeignItem>, folder: &mut T) -> P<ForeignItem> {
1187 ni.map(|ForeignItem {id, ident, attrs, node, span, vis}| ForeignItem {
1188 id: folder.new_id(id),
1189 ident: folder.fold_ident(ident),
1190 attrs: attrs.move_map(|x| folder.fold_attribute(x)),
1192 ForeignItemFn(fdec, generics) => {
1193 ForeignItemFn(folder.fold_fn_decl(fdec), folder.fold_generics(generics))
1195 ForeignItemStatic(t, m) => {
1196 ForeignItemStatic(folder.fold_ty(t), m)
1200 span: folder.new_span(span)
1204 // Default fold over a method.
1205 // Invariant: produces exactly one method.
1206 pub fn noop_fold_method<T: Folder>(m: P<Method>, folder: &mut T) -> SmallVector<P<Method>> {
1207 SmallVector::one(m.map(|Method {id, attrs, node, span}| Method {
1208 id: folder.new_id(id),
1209 attrs: attrs.move_map(|a| folder.fold_attribute(a)),
1219 MethDecl(folder.fold_ident(ident),
1220 folder.fold_generics(generics),
1222 folder.fold_explicit_self(explicit_self),
1224 folder.fold_fn_decl(decl),
1225 folder.fold_block(body),
1228 MethMac(mac) => MethMac(folder.fold_mac(mac)),
1230 span: folder.new_span(span)
1234 pub fn noop_fold_pat<T: Folder>(p: P<Pat>, folder: &mut T) -> P<Pat> {
1235 p.map(|Pat {id, node, span}| Pat {
1236 id: folder.new_id(id),
1238 PatWild(k) => PatWild(k),
1239 PatIdent(binding_mode, pth1, sub) => {
1240 PatIdent(binding_mode,
1241 Spanned{span: folder.new_span(pth1.span),
1242 node: folder.fold_ident(pth1.node)},
1243 sub.map(|x| folder.fold_pat(x)))
1245 PatLit(e) => PatLit(folder.fold_expr(e)),
1246 PatEnum(pth, pats) => {
1247 PatEnum(folder.fold_path(pth),
1248 pats.map(|pats| pats.move_map(|x| folder.fold_pat(x))))
1250 PatStruct(pth, fields, etc) => {
1251 let pth = folder.fold_path(pth);
1252 let fs = fields.move_map(|f| {
1253 Spanned { span: folder.new_span(f.span),
1254 node: ast::FieldPat {
1255 ident: f.node.ident,
1256 pat: folder.fold_pat(f.node.pat),
1257 is_shorthand: f.node.is_shorthand,
1260 PatStruct(pth, fs, etc)
1262 PatTup(elts) => PatTup(elts.move_map(|x| folder.fold_pat(x))),
1263 PatBox(inner) => PatBox(folder.fold_pat(inner)),
1264 PatRegion(inner, mutbl) => PatRegion(folder.fold_pat(inner), mutbl),
1265 PatRange(e1, e2) => {
1266 PatRange(folder.fold_expr(e1), folder.fold_expr(e2))
1268 PatVec(before, slice, after) => {
1269 PatVec(before.move_map(|x| folder.fold_pat(x)),
1270 slice.map(|x| folder.fold_pat(x)),
1271 after.move_map(|x| folder.fold_pat(x)))
1273 PatMac(mac) => PatMac(folder.fold_mac(mac))
1275 span: folder.new_span(span)
1279 pub fn noop_fold_expr<T: Folder>(Expr {id, node, span}: Expr, folder: &mut T) -> Expr {
1281 id: folder.new_id(id),
1284 ExprBox(p.map(|e|folder.fold_expr(e)), folder.fold_expr(e))
1287 ExprVec(exprs.move_map(|x| folder.fold_expr(x)))
1289 ExprRepeat(expr, count) => {
1290 ExprRepeat(folder.fold_expr(expr), folder.fold_expr(count))
1292 ExprTup(elts) => ExprTup(elts.move_map(|x| folder.fold_expr(x))),
1293 ExprCall(f, args) => {
1294 ExprCall(folder.fold_expr(f),
1295 args.move_map(|x| folder.fold_expr(x)))
1297 ExprMethodCall(i, tps, args) => {
1299 respan(i.span, folder.fold_ident(i.node)),
1300 tps.move_map(|x| folder.fold_ty(x)),
1301 args.move_map(|x| folder.fold_expr(x)))
1303 ExprBinary(binop, lhs, rhs) => {
1305 folder.fold_expr(lhs),
1306 folder.fold_expr(rhs))
1308 ExprUnary(binop, ohs) => {
1309 ExprUnary(binop, folder.fold_expr(ohs))
1311 ExprLit(l) => ExprLit(l),
1312 ExprCast(expr, ty) => {
1313 ExprCast(folder.fold_expr(expr), folder.fold_ty(ty))
1315 ExprAddrOf(m, ohs) => ExprAddrOf(m, folder.fold_expr(ohs)),
1316 ExprIf(cond, tr, fl) => {
1317 ExprIf(folder.fold_expr(cond),
1318 folder.fold_block(tr),
1319 fl.map(|x| folder.fold_expr(x)))
1321 ExprIfLet(pat, expr, tr, fl) => {
1322 ExprIfLet(folder.fold_pat(pat),
1323 folder.fold_expr(expr),
1324 folder.fold_block(tr),
1325 fl.map(|x| folder.fold_expr(x)))
1327 ExprWhile(cond, body, opt_ident) => {
1328 ExprWhile(folder.fold_expr(cond),
1329 folder.fold_block(body),
1330 opt_ident.map(|i| folder.fold_ident(i)))
1332 ExprWhileLet(pat, expr, body, opt_ident) => {
1333 ExprWhileLet(folder.fold_pat(pat),
1334 folder.fold_expr(expr),
1335 folder.fold_block(body),
1336 opt_ident.map(|i| folder.fold_ident(i)))
1338 ExprForLoop(pat, iter, body, opt_ident) => {
1339 ExprForLoop(folder.fold_pat(pat),
1340 folder.fold_expr(iter),
1341 folder.fold_block(body),
1342 opt_ident.map(|i| folder.fold_ident(i)))
1344 ExprLoop(body, opt_ident) => {
1345 ExprLoop(folder.fold_block(body),
1346 opt_ident.map(|i| folder.fold_ident(i)))
1348 ExprMatch(expr, arms, source) => {
1349 ExprMatch(folder.fold_expr(expr),
1350 arms.move_map(|x| folder.fold_arm(x)),
1353 ExprClosure(capture_clause, opt_kind, decl, body) => {
1354 ExprClosure(capture_clause,
1356 folder.fold_fn_decl(decl),
1357 folder.fold_block(body))
1359 ExprBlock(blk) => ExprBlock(folder.fold_block(blk)),
1360 ExprAssign(el, er) => {
1361 ExprAssign(folder.fold_expr(el), folder.fold_expr(er))
1363 ExprAssignOp(op, el, er) => {
1365 folder.fold_expr(el),
1366 folder.fold_expr(er))
1368 ExprField(el, ident) => {
1369 ExprField(folder.fold_expr(el),
1370 respan(ident.span, folder.fold_ident(ident.node)))
1372 ExprTupField(el, ident) => {
1373 ExprTupField(folder.fold_expr(el),
1374 respan(ident.span, folder.fold_uint(ident.node)))
1376 ExprIndex(el, er) => {
1377 ExprIndex(folder.fold_expr(el), folder.fold_expr(er))
1379 ExprRange(e1, e2) => {
1380 ExprRange(e1.map(|x| folder.fold_expr(x)),
1381 e2.map(|x| folder.fold_expr(x)))
1383 ExprPath(pth) => ExprPath(folder.fold_path(pth)),
1384 ExprQPath(qpath) => ExprQPath(folder.fold_qpath(qpath)),
1385 ExprBreak(opt_ident) => ExprBreak(opt_ident.map(|x| folder.fold_ident(x))),
1386 ExprAgain(opt_ident) => ExprAgain(opt_ident.map(|x| folder.fold_ident(x))),
1387 ExprRet(e) => ExprRet(e.map(|x| folder.fold_expr(x))),
1388 ExprInlineAsm(InlineAsm {
1398 }) => ExprInlineAsm(InlineAsm {
1399 inputs: inputs.move_map(|(c, input)| {
1400 (c, folder.fold_expr(input))
1402 outputs: outputs.move_map(|(c, out, is_rw)| {
1403 (c, folder.fold_expr(out), is_rw)
1406 asm_str_style: asm_str_style,
1409 alignstack: alignstack,
1413 ExprMac(mac) => ExprMac(folder.fold_mac(mac)),
1414 ExprStruct(path, fields, maybe_expr) => {
1415 ExprStruct(folder.fold_path(path),
1416 fields.move_map(|x| folder.fold_field(x)),
1417 maybe_expr.map(|x| folder.fold_expr(x)))
1419 ExprParen(ex) => ExprParen(folder.fold_expr(ex))
1421 span: folder.new_span(span)
1425 pub fn noop_fold_stmt<T: Folder>(Spanned {node, span}: Stmt, folder: &mut T)
1426 -> SmallVector<P<Stmt>> {
1427 let span = folder.new_span(span);
1429 StmtDecl(d, id) => {
1430 let id = folder.new_id(id);
1431 folder.fold_decl(d).into_iter().map(|d| P(Spanned {
1432 node: StmtDecl(d, id),
1436 StmtExpr(e, id) => {
1437 let id = folder.new_id(id);
1438 SmallVector::one(P(Spanned {
1439 node: StmtExpr(folder.fold_expr(e), id),
1443 StmtSemi(e, id) => {
1444 let id = folder.new_id(id);
1445 SmallVector::one(P(Spanned {
1446 node: StmtSemi(folder.fold_expr(e), id),
1450 StmtMac(mac, semi) => SmallVector::one(P(Spanned {
1451 node: StmtMac(mac.map(|m| folder.fold_mac(m)), semi),
1461 use util::parser_testing::{string_to_crate, matches_codepattern};
1467 // this version doesn't care about getting comments or docstrings in.
1468 fn fake_print_crate(s: &mut pprust::State,
1469 krate: &ast::Crate) -> io::IoResult<()> {
1470 s.print_mod(&krate.module, krate.attrs.as_slice())
1473 // change every identifier to "zz"
1474 struct ToZzIdentFolder;
1476 impl Folder for ToZzIdentFolder {
1477 fn fold_ident(&mut self, _: ast::Ident) -> ast::Ident {
1478 token::str_to_ident("zz")
1480 fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
1481 fold::noop_fold_mac(mac, self)
1485 // maybe add to expand.rs...
1486 macro_rules! assert_pred {
1487 ($pred:expr, $predname:expr, $a:expr , $b:expr) => (
1489 let pred_val = $pred;
1492 if !(pred_val(a_val.as_slice(),b_val.as_slice())) {
1493 panic!("expected args satisfying {}, got {} and {}",
1494 $predname, a_val, b_val);
1500 // make sure idents get transformed everywhere
1501 #[test] fn ident_transformation () {
1502 let mut zz_fold = ToZzIdentFolder;
1503 let ast = string_to_crate(
1504 "#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}".to_string());
1505 let folded_crate = zz_fold.fold_crate(ast);
1507 matches_codepattern,
1508 "matches_codepattern",
1509 pprust::to_string(|s| fake_print_crate(s, &folded_crate)),
1510 "#[a]mod zz{fn zz(zz:zz,zz:zz){zz!(zz,zz,zz);zz;zz}}".to_string());
1513 // even inside macro defs....
1514 #[test] fn ident_transformation_in_defs () {
1515 let mut zz_fold = ToZzIdentFolder;
1516 let ast = string_to_crate(
1517 "macro_rules! a {(b $c:expr $(d $e:token)f+ => \
1518 (g $(d $d $e)+))} ".to_string());
1519 let folded_crate = zz_fold.fold_crate(ast);
1521 matches_codepattern,
1522 "matches_codepattern",
1523 pprust::to_string(|s| fake_print_crate(s, &folded_crate)),
1524 "zz!zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)));".to_string());