1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use codemap::{respan, Span, Spanned};
16 use util::small_vector::SmallVector;
18 // We may eventually want to be able to fold over type parameters, too.
20 fn fold_crate(&mut self, c: Crate) -> Crate {
21 noop_fold_crate(c, self)
24 fn fold_meta_items(&mut self, meta_items: &[@MetaItem]) -> ~[@MetaItem] {
25 meta_items.map(|x| fold_meta_item_(*x, self))
28 fn fold_view_paths(&mut self, view_paths: &[@ViewPath]) -> ~[@ViewPath] {
29 view_paths.map(|view_path| {
30 let inner_view_path = match view_path.node {
31 ViewPathSimple(ref ident, ref path, node_id) => {
32 ViewPathSimple(ident.clone(),
36 ViewPathGlob(ref path, node_id) => {
37 ViewPathGlob(self.fold_path(path), self.new_id(node_id))
39 ViewPathList(ref path, ref path_list_idents, node_id) => {
40 ViewPathList(self.fold_path(path),
41 path_list_idents.map(|path_list_ident| {
42 let id = self.new_id(path_list_ident.node
45 node: PathListIdent_ {
46 name: path_list_ident.node
59 node: inner_view_path,
60 span: self.new_span(view_path.span),
65 fn fold_view_item(&mut self, vi: &ViewItem) -> ViewItem {
66 noop_fold_view_item(vi, self)
69 fn fold_foreign_item(&mut self, ni: @ForeignItem) -> @ForeignItem {
70 noop_fold_foreign_item(ni, self)
73 fn fold_item(&mut self, i: @Item) -> SmallVector<@Item> {
74 noop_fold_item(i, self)
77 fn fold_struct_field(&mut self, sf: &StructField) -> StructField {
78 let fold_attribute = |x| fold_attribute_(x, self);
81 node: ast::StructField_ {
83 id: self.new_id(sf.node.id),
84 ty: self.fold_ty(sf.node.ty),
85 attrs: sf.node.attrs.map(|e| fold_attribute(*e))
87 span: self.new_span(sf.span)
91 fn fold_item_underscore(&mut self, i: &Item_) -> Item_ {
92 noop_fold_item_underscore(i, self)
95 fn fold_fn_decl(&mut self, d: &FnDecl) -> P<FnDecl> {
96 noop_fold_fn_decl(d, self)
99 fn fold_type_method(&mut self, m: &TypeMethod) -> TypeMethod {
100 noop_fold_type_method(m, self)
103 fn fold_method(&mut self, m: @Method) -> @Method {
104 noop_fold_method(m, self)
107 fn fold_block(&mut self, b: P<Block>) -> P<Block> {
108 noop_fold_block(b, self)
111 fn fold_stmt(&mut self, s: &Stmt) -> SmallVector<@Stmt> {
112 noop_fold_stmt(s, self)
115 fn fold_arm(&mut self, a: &Arm) -> Arm {
117 pats: a.pats.map(|x| self.fold_pat(*x)),
118 guard: a.guard.map(|x| self.fold_expr(x)),
119 body: self.fold_block(a.body),
123 fn fold_pat(&mut self, p: @Pat) -> @Pat {
124 noop_fold_pat(p, self)
127 fn fold_decl(&mut self, d: @Decl) -> SmallVector<@Decl> {
128 let node = match d.node {
129 DeclLocal(ref l) => SmallVector::one(DeclLocal(self.fold_local(*l))),
131 self.fold_item(it).move_iter().map(|i| DeclItem(i)).collect()
135 node.move_iter().map(|node| {
143 fn fold_expr(&mut self, e: @Expr) -> @Expr {
144 noop_fold_expr(e, self)
147 fn fold_ty(&mut self, t: P<Ty>) -> P<Ty> {
148 let node = match t.node {
149 TyNil | TyBot | TyInfer => t.node.clone(),
150 TyBox(ty) => TyBox(self.fold_ty(ty)),
151 TyUniq(ty) => TyUniq(self.fold_ty(ty)),
152 TyVec(ty) => TyVec(self.fold_ty(ty)),
153 TyPtr(ref mt) => TyPtr(fold_mt(mt, self)),
154 TyRptr(ref region, ref mt) => {
155 TyRptr(fold_opt_lifetime(region, self), fold_mt(mt, self))
157 TyClosure(ref f) => {
158 TyClosure(@ClosureTy {
161 region: fold_opt_lifetime(&f.region, self),
162 onceness: f.onceness,
163 bounds: fold_opt_bounds(&f.bounds, self),
164 decl: self.fold_fn_decl(f.decl),
165 lifetimes: f.lifetimes.map(|l| fold_lifetime(l, self)),
170 lifetimes: f.lifetimes.map(|l| fold_lifetime(l, self)),
173 decl: self.fold_fn_decl(f.decl)
176 TyTup(ref tys) => TyTup(tys.map(|&ty| self.fold_ty(ty))),
177 TyPath(ref path, ref bounds, id) => {
178 TyPath(self.fold_path(path),
179 fold_opt_bounds(bounds, self),
182 TyFixedLengthVec(ty, e) => {
183 TyFixedLengthVec(self.fold_ty(ty), self.fold_expr(e))
185 TyTypeof(expr) => TyTypeof(self.fold_expr(expr)),
188 id: self.new_id(t.id),
189 span: self.new_span(t.span),
194 fn fold_mod(&mut self, m: &Mod) -> Mod {
195 noop_fold_mod(m, self)
198 fn fold_foreign_mod(&mut self, nm: &ForeignMod) -> ForeignMod {
201 view_items: nm.view_items
203 .map(|x| self.fold_view_item(x))
207 .map(|x| self.fold_foreign_item(*x))
212 fn fold_variant(&mut self, v: &Variant) -> P<Variant> {
215 TupleVariantKind(ref variant_args) => {
216 kind = TupleVariantKind(variant_args.map(|x|
217 fold_variant_arg_(x, self)))
219 StructVariantKind(ref struct_def) => {
220 kind = StructVariantKind(@ast::StructDef {
221 fields: struct_def.fields.iter()
222 .map(|f| self.fold_struct_field(f)).collect(),
223 ctor_id: struct_def.ctor_id.map(|c| self.new_id(c))
228 let fold_attribute = |x| fold_attribute_(x, self);
229 let attrs = v.node.attrs.map(|x| fold_attribute(*x));
231 let de = match v.node.disr_expr {
232 Some(e) => Some(self.fold_expr(e)),
235 let node = ast::Variant_ {
239 id: self.new_id(v.node.id),
245 span: self.new_span(v.span),
249 fn fold_ident(&mut self, i: Ident) -> Ident {
253 fn fold_path(&mut self, p: &Path) -> Path {
255 span: self.new_span(p.span),
257 segments: p.segments.map(|segment| ast::PathSegment {
258 identifier: self.fold_ident(segment.identifier),
259 lifetimes: segment.lifetimes.map(|l| fold_lifetime(l, self)),
260 types: segment.types.map(|&typ| self.fold_ty(typ)),
265 fn fold_local(&mut self, l: @Local) -> @Local {
267 ty: self.fold_ty(l.ty),
268 pat: self.fold_pat(l.pat),
269 init: l.init.map(|e| self.fold_expr(e)),
270 id: self.new_id(l.id),
271 span: self.new_span(l.span),
275 fn fold_mac(&mut self, macro: &Mac) -> Mac {
277 node: match macro.node {
278 MacInvocTT(ref p, ref tts, ctxt) => {
279 MacInvocTT(self.fold_path(p),
280 fold_tts(*tts, self),
284 span: self.new_span(macro.span)
288 fn map_exprs(&self, f: |@Expr| -> @Expr, es: &[@Expr]) -> ~[@Expr] {
292 fn new_id(&mut self, i: NodeId) -> NodeId {
296 fn new_span(&mut self, sp: Span) -> Span {
300 fn fold_explicit_self(&mut self, es: &ExplicitSelf) -> ExplicitSelf {
302 span: self.new_span(es.span),
303 node: self.fold_explicit_self_(&es.node)
307 fn fold_explicit_self_(&mut self, es: &ExplicitSelf_) -> ExplicitSelf_ {
309 SelfStatic | SelfValue | SelfUniq | SelfBox => *es,
310 SelfRegion(ref lifetime, m) => {
311 SelfRegion(fold_opt_lifetime(lifetime, self), m)
317 /* some little folds that probably aren't useful to have in Folder itself*/
319 //used in noop_fold_item and noop_fold_crate and noop_fold_crate_directive
320 fn fold_meta_item_<T: Folder>(mi: @MetaItem, fld: &mut T) -> @MetaItem {
324 MetaWord(ref id) => MetaWord((*id).clone()),
325 MetaList(ref id, ref mis) => {
326 let fold_meta_item = |x| fold_meta_item_(x, fld);
327 MetaList((*id).clone(), mis.map(|e| fold_meta_item(*e)))
329 MetaNameValue(ref id, ref s) => {
330 MetaNameValue((*id).clone(), (*s).clone())
333 span: fld.new_span(mi.span) }
336 //used in noop_fold_item and noop_fold_crate
337 fn fold_attribute_<T: Folder>(at: Attribute, fld: &mut T) -> Attribute {
339 span: fld.new_span(at.span),
340 node: ast::Attribute_ {
341 style: at.node.style,
342 value: fold_meta_item_(at.node.value, fld),
343 is_sugared_doc: at.node.is_sugared_doc
348 //used in noop_fold_foreign_item and noop_fold_fn_decl
349 fn fold_arg_<T: Folder>(a: &Arg, fld: &mut T) -> Arg {
351 ty: fld.fold_ty(a.ty),
352 pat: fld.fold_pat(a.pat),
353 id: fld.new_id(a.id),
357 // build a new vector of tts by appling the Folder's fold_ident to
358 // all of the identifiers in the token trees.
359 pub fn fold_tts<T: Folder>(tts: &[TokenTree], fld: &mut T) -> ~[TokenTree] {
362 TTTok(span, ref tok) =>
363 TTTok(span,maybe_fold_ident(tok,fld)),
364 TTDelim(tts) => TTDelim(@fold_tts(*tts, fld)),
365 TTSeq(span, pattern, ref sep, is_optional) =>
367 @fold_tts(*pattern, fld),
368 sep.as_ref().map(|tok|maybe_fold_ident(tok,fld)),
370 TTNonterminal(sp,ref ident) =>
371 TTNonterminal(sp,fld.fold_ident(*ident))
376 // apply ident folder if it's an ident, otherwise leave it alone
377 fn maybe_fold_ident<T: Folder>(t: &token::Token, fld: &mut T) -> token::Token {
379 token::IDENT(id, followed_by_colons) => {
380 token::IDENT(fld.fold_ident(id), followed_by_colons)
386 pub fn noop_fold_fn_decl<T: Folder>(decl: &FnDecl, fld: &mut T) -> P<FnDecl> {
388 inputs: decl.inputs.map(|x| fold_arg_(x, fld)), // bad copy
389 output: fld.fold_ty(decl.output),
391 variadic: decl.variadic
395 fn fold_ty_param_bound<T: Folder>(tpb: &TyParamBound, fld: &mut T)
398 TraitTyParamBound(ref ty) => TraitTyParamBound(fold_trait_ref(ty, fld)),
399 RegionTyParamBound => RegionTyParamBound
403 pub fn fold_ty_param<T: Folder>(tp: &TyParam, fld: &mut T) -> TyParam {
406 id: fld.new_id(tp.id),
407 bounds: tp.bounds.map(|x| fold_ty_param_bound(x, fld)),
408 default: tp.default.map(|x| fld.fold_ty(x))
412 pub fn fold_ty_params<T: Folder>(tps: &OptVec<TyParam>, fld: &mut T)
414 tps.map(|tp| fold_ty_param(tp, fld))
417 pub fn fold_lifetime<T: Folder>(l: &Lifetime, fld: &mut T) -> Lifetime {
419 id: fld.new_id(l.id),
420 span: fld.new_span(l.span),
425 pub fn fold_lifetimes<T: Folder>(lts: &OptVec<Lifetime>, fld: &mut T)
426 -> OptVec<Lifetime> {
427 lts.map(|l| fold_lifetime(l, fld))
430 pub fn fold_opt_lifetime<T: Folder>(o_lt: &Option<Lifetime>, fld: &mut T)
431 -> Option<Lifetime> {
432 o_lt.as_ref().map(|lt| fold_lifetime(lt, fld))
435 pub fn fold_generics<T: Folder>(generics: &Generics, fld: &mut T) -> Generics {
436 Generics {ty_params: fold_ty_params(&generics.ty_params, fld),
437 lifetimes: fold_lifetimes(&generics.lifetimes, fld)}
440 fn fold_struct_def<T: Folder>(struct_def: @StructDef, fld: &mut T) -> @StructDef {
442 fields: struct_def.fields.map(|f| fold_struct_field(f, fld)),
443 ctor_id: struct_def.ctor_id.map(|cid| fld.new_id(cid)),
447 fn fold_trait_ref<T: Folder>(p: &TraitRef, fld: &mut T) -> TraitRef {
449 path: fld.fold_path(&p.path),
450 ref_id: fld.new_id(p.ref_id),
454 fn fold_struct_field<T: Folder>(f: &StructField, fld: &mut T) -> StructField {
456 node: ast::StructField_ {
458 id: fld.new_id(f.node.id),
459 ty: fld.fold_ty(f.node.ty),
460 attrs: f.node.attrs.map(|a| fold_attribute_(*a, fld)),
462 span: fld.new_span(f.span),
466 fn fold_field_<T: Folder>(field: Field, folder: &mut T) -> Field {
468 ident: respan(field.ident.span, folder.fold_ident(field.ident.node)),
469 expr: folder.fold_expr(field.expr),
470 span: folder.new_span(field.span),
474 fn fold_mt<T: Folder>(mt: &MutTy, folder: &mut T) -> MutTy {
476 ty: folder.fold_ty(mt.ty),
481 fn fold_opt_bounds<T: Folder>(b: &Option<OptVec<TyParamBound>>, folder: &mut T)
482 -> Option<OptVec<TyParamBound>> {
483 b.as_ref().map(|bounds| {
485 fold_ty_param_bound(bound, folder)
490 fn fold_variant_arg_<T: Folder>(va: &VariantArg, folder: &mut T) -> VariantArg {
492 ty: folder.fold_ty(va.ty),
493 id: folder.new_id(va.id)
497 pub fn noop_fold_view_item<T: Folder>(vi: &ViewItem, folder: &mut T)
499 let inner_view_item = match vi.node {
500 ViewItemExternMod(ref ident,
503 ViewItemExternMod(ident.clone(),
505 folder.new_id(node_id))
507 ViewItemUse(ref view_paths) => {
508 ViewItemUse(folder.fold_view_paths(*view_paths))
512 node: inner_view_item,
513 attrs: vi.attrs.map(|a| fold_attribute_(*a, folder)),
515 span: folder.new_span(vi.span),
519 pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> {
520 let view_items = b.view_items.map(|x| folder.fold_view_item(x));
521 let stmts = b.stmts.iter().flat_map(|s| folder.fold_stmt(*s).move_iter()).collect();
523 view_items: view_items,
525 expr: b.expr.map(|x| folder.fold_expr(x)),
526 id: folder.new_id(b.id),
528 span: folder.new_span(b.span),
532 pub fn noop_fold_item_underscore<T: Folder>(i: &Item_, folder: &mut T) -> Item_ {
534 ItemStatic(t, m, e) => {
535 ItemStatic(folder.fold_ty(t), m, folder.fold_expr(e))
537 ItemFn(decl, purity, abi, ref generics, body) => {
539 folder.fold_fn_decl(decl),
542 fold_generics(generics, folder),
543 folder.fold_block(body)
546 ItemMod(ref m) => ItemMod(folder.fold_mod(m)),
547 ItemForeignMod(ref nm) => ItemForeignMod(folder.fold_foreign_mod(nm)),
548 ItemTy(t, ref generics) => {
549 ItemTy(folder.fold_ty(t), fold_generics(generics, folder))
551 ItemEnum(ref enum_definition, ref generics) => {
554 variants: enum_definition.variants.map(|&x| {
555 folder.fold_variant(x)
558 fold_generics(generics, folder))
560 ItemStruct(ref struct_def, ref generics) => {
561 let struct_def = fold_struct_def(*struct_def, folder);
562 ItemStruct(struct_def, fold_generics(generics, folder))
564 ItemImpl(ref generics, ref ifce, ty, ref methods) => {
565 ItemImpl(fold_generics(generics, folder),
566 ifce.as_ref().map(|p| fold_trait_ref(p, folder)),
568 methods.map(|x| folder.fold_method(*x))
571 ItemTrait(ref generics, ref traits, ref methods) => {
572 let methods = methods.map(|method| {
574 Required(ref m) => Required(folder.fold_type_method(m)),
575 Provided(method) => Provided(folder.fold_method(method))
578 ItemTrait(fold_generics(generics, folder),
579 traits.map(|p| fold_trait_ref(p, folder)),
582 ItemMac(ref m) => ItemMac(folder.fold_mac(m)),
586 pub fn noop_fold_type_method<T: Folder>(m: &TypeMethod, fld: &mut T) -> TypeMethod {
588 ident: fld.fold_ident(m.ident),
589 attrs: m.attrs.map(|a| fold_attribute_(*a, fld)),
591 decl: fld.fold_fn_decl(m.decl),
592 generics: fold_generics(&m.generics, fld),
593 explicit_self: fld.fold_explicit_self(&m.explicit_self),
594 id: fld.new_id(m.id),
595 span: fld.new_span(m.span),
599 pub fn noop_fold_mod<T: Folder>(m: &Mod, folder: &mut T) -> Mod {
601 view_items: m.view_items
603 .map(|x| folder.fold_view_item(x)).collect(),
604 items: m.items.iter().flat_map(|x| folder.fold_item(*x).move_iter()).collect(),
608 pub fn noop_fold_crate<T: Folder>(c: Crate, folder: &mut T) -> Crate {
609 let fold_meta_item = |x| fold_meta_item_(x, folder);
610 let fold_attribute = |x| fold_attribute_(x, folder);
613 module: folder.fold_mod(&c.module),
614 attrs: c.attrs.map(|x| fold_attribute(*x)),
615 config: c.config.map(|x| fold_meta_item(*x)),
616 span: folder.new_span(c.span),
620 pub fn noop_fold_item<T: Folder>(i: &Item, folder: &mut T) -> SmallVector<@Item> {
621 let fold_attribute = |x| fold_attribute_(x, folder);
623 SmallVector::one(@Item {
624 ident: folder.fold_ident(i.ident),
625 attrs: i.attrs.map(|e| fold_attribute(*e)),
626 id: folder.new_id(i.id),
627 node: folder.fold_item_underscore(&i.node),
629 span: folder.new_span(i.span)
633 pub fn noop_fold_foreign_item<T: Folder>(ni: &ForeignItem, folder: &mut T) -> @ForeignItem {
635 ident: folder.fold_ident(ni.ident),
636 attrs: ni.attrs.map(|x| fold_attribute_(*x, folder)),
637 node: match ni.node {
638 ForeignItemFn(ref fdec, ref generics) => {
639 ForeignItemFn(P(FnDecl {
640 inputs: fdec.inputs.map(|a| fold_arg_(a, folder)),
641 output: folder.fold_ty(fdec.output),
643 variadic: fdec.variadic
644 }), fold_generics(generics, folder))
646 ForeignItemStatic(t, m) => {
647 ForeignItemStatic(folder.fold_ty(t), m)
650 id: folder.new_id(ni.id),
651 span: folder.new_span(ni.span),
656 pub fn noop_fold_method<T: Folder>(m: &Method, folder: &mut T) -> @Method {
658 ident: folder.fold_ident(m.ident),
659 attrs: m.attrs.map(|a| fold_attribute_(*a, folder)),
660 generics: fold_generics(&m.generics, folder),
661 explicit_self: folder.fold_explicit_self(&m.explicit_self),
663 decl: folder.fold_fn_decl(m.decl),
664 body: folder.fold_block(m.body),
665 id: folder.new_id(m.id),
666 span: folder.new_span(m.span),
671 pub fn noop_fold_pat<T: Folder>(p: @Pat, folder: &mut T) -> @Pat {
672 let node = match p.node {
674 PatWildMulti => PatWildMulti,
675 PatIdent(binding_mode, ref pth, ref sub) => {
676 PatIdent(binding_mode,
677 folder.fold_path(pth),
678 sub.map(|x| folder.fold_pat(x)))
680 PatLit(e) => PatLit(folder.fold_expr(e)),
681 PatEnum(ref pth, ref pats) => {
682 PatEnum(folder.fold_path(pth),
683 pats.as_ref().map(|pats| pats.map(|x| folder.fold_pat(*x))))
685 PatStruct(ref pth, ref fields, etc) => {
686 let pth_ = folder.fold_path(pth);
687 let fs = fields.map(|f| {
690 pat: folder.fold_pat(f.pat)
693 PatStruct(pth_, fs, etc)
695 PatTup(ref elts) => PatTup(elts.map(|x| folder.fold_pat(*x))),
696 PatUniq(inner) => PatUniq(folder.fold_pat(inner)),
697 PatRegion(inner) => PatRegion(folder.fold_pat(inner)),
698 PatRange(e1, e2) => {
699 PatRange(folder.fold_expr(e1), folder.fold_expr(e2))
701 PatVec(ref before, ref slice, ref after) => {
702 PatVec(before.map(|x| folder.fold_pat(*x)),
703 slice.map(|x| folder.fold_pat(x)),
704 after.map(|x| folder.fold_pat(*x)))
709 id: folder.new_id(p.id),
710 span: folder.new_span(p.span),
715 pub fn noop_fold_expr<T: Folder>(e: @Expr, folder: &mut T) -> @Expr {
716 let fold_field = |x| fold_field_(x, folder);
718 let node = match e.node {
719 ExprVstore(e, v) => {
720 ExprVstore(folder.fold_expr(e), v)
723 ExprBox(folder.fold_expr(p), folder.fold_expr(e))
725 ExprVec(ref exprs, mutt) => {
726 ExprVec(exprs.map(|&x| folder.fold_expr(x)), mutt)
728 ExprRepeat(expr, count, mutt) => {
729 ExprRepeat(folder.fold_expr(expr), folder.fold_expr(count), mutt)
731 ExprTup(ref elts) => ExprTup(elts.map(|x| folder.fold_expr(*x))),
732 ExprCall(f, ref args, blk) => {
733 ExprCall(folder.fold_expr(f),
734 args.map(|&x| folder.fold_expr(x)),
737 ExprMethodCall(callee_id, i, ref tps, ref args, blk) => {
739 folder.new_id(callee_id),
740 folder.fold_ident(i),
741 tps.map(|&x| folder.fold_ty(x)),
742 args.map(|&x| folder.fold_expr(x)),
746 ExprBinary(callee_id, binop, lhs, rhs) => {
747 ExprBinary(folder.new_id(callee_id),
749 folder.fold_expr(lhs),
750 folder.fold_expr(rhs))
752 ExprUnary(callee_id, binop, ohs) => {
753 ExprUnary(folder.new_id(callee_id), binop, folder.fold_expr(ohs))
755 ExprLit(_) => e.node.clone(),
756 ExprCast(expr, ty) => {
757 ExprCast(folder.fold_expr(expr), folder.fold_ty(ty))
759 ExprAddrOf(m, ohs) => ExprAddrOf(m, folder.fold_expr(ohs)),
760 ExprIf(cond, tr, fl) => {
761 ExprIf(folder.fold_expr(cond),
762 folder.fold_block(tr),
763 fl.map(|x| folder.fold_expr(x)))
765 ExprWhile(cond, body) => {
766 ExprWhile(folder.fold_expr(cond), folder.fold_block(body))
768 ExprForLoop(pat, iter, body, ref maybe_ident) => {
769 ExprForLoop(folder.fold_pat(pat),
770 folder.fold_expr(iter),
771 folder.fold_block(body),
772 maybe_ident.map(|i| folder.fold_ident(i)))
774 ExprLoop(body, opt_ident) => {
775 ExprLoop(folder.fold_block(body),
776 opt_ident.map(|x| folder.fold_ident(x)))
778 ExprMatch(expr, ref arms) => {
779 ExprMatch(folder.fold_expr(expr),
780 arms.map(|x| folder.fold_arm(x)))
782 ExprFnBlock(decl, body) => {
783 ExprFnBlock(folder.fold_fn_decl(decl), folder.fold_block(body))
785 ExprProc(decl, body) => {
786 ExprProc(folder.fold_fn_decl(decl), folder.fold_block(body))
788 ExprBlock(blk) => ExprBlock(folder.fold_block(blk)),
789 ExprAssign(el, er) => {
790 ExprAssign(folder.fold_expr(el), folder.fold_expr(er))
792 ExprAssignOp(callee_id, op, el, er) => {
793 ExprAssignOp(folder.new_id(callee_id),
795 folder.fold_expr(el),
796 folder.fold_expr(er))
798 ExprField(el, id, ref tys) => {
799 ExprField(folder.fold_expr(el),
800 folder.fold_ident(id),
801 tys.map(|&x| folder.fold_ty(x)))
803 ExprIndex(callee_id, el, er) => {
804 ExprIndex(folder.new_id(callee_id),
805 folder.fold_expr(el),
806 folder.fold_expr(er))
808 ExprPath(ref pth) => ExprPath(folder.fold_path(pth)),
809 ExprLogLevel => ExprLogLevel,
810 ExprBreak(opt_ident) => ExprBreak(opt_ident),
811 ExprAgain(opt_ident) => ExprAgain(opt_ident),
813 ExprRet(e.map(|x| folder.fold_expr(x)))
815 ExprInlineAsm(ref a) => {
816 ExprInlineAsm(InlineAsm {
817 inputs: a.inputs.map(|&(c, input)| (c, folder.fold_expr(input))),
818 outputs: a.outputs.map(|&(c, out)| (c, folder.fold_expr(out))),
822 ExprMac(ref mac) => ExprMac(folder.fold_mac(mac)),
823 ExprStruct(ref path, ref fields, maybe_expr) => {
824 ExprStruct(folder.fold_path(path),
825 fields.map(|x| fold_field(*x)),
826 maybe_expr.map(|x| folder.fold_expr(x)))
828 ExprParen(ex) => ExprParen(folder.fold_expr(ex))
832 id: folder.new_id(e.id),
834 span: folder.new_span(e.span),
838 pub fn noop_fold_stmt<T: Folder>(s: &Stmt, folder: &mut T) -> SmallVector<@Stmt> {
839 let nodes = match s.node {
840 StmtDecl(d, nid) => {
841 folder.fold_decl(d).move_iter()
842 .map(|d| StmtDecl(d, folder.new_id(nid)))
845 StmtExpr(e, nid) => {
846 SmallVector::one(StmtExpr(folder.fold_expr(e), folder.new_id(nid)))
848 StmtSemi(e, nid) => {
849 SmallVector::one(StmtSemi(folder.fold_expr(e), folder.new_id(nid)))
851 StmtMac(ref mac, semi) => SmallVector::one(StmtMac(folder.fold_mac(mac), semi))
854 nodes.move_iter().map(|node| @Spanned {
856 span: folder.new_span(s.span),
863 use util::parser_testing::{string_to_crate, matches_codepattern};
868 // this version doesn't care about getting comments or docstrings in.
869 fn fake_print_crate(s: &mut pprust::State, crate: &ast::Crate) {
870 pprust::print_mod(s, &crate.module, crate.attrs);
873 // change every identifier to "zz"
874 struct ToZzIdentFolder;
876 impl Folder for ToZzIdentFolder {
877 fn fold_ident(&mut self, _: ast::Ident) -> ast::Ident {
878 token::str_to_ident("zz")
882 // maybe add to expand.rs...
883 macro_rules! assert_pred (
884 ($pred:expr, $predname:expr, $a:expr , $b:expr) => (
886 let pred_val = $pred;
889 if !(pred_val(a_val,b_val)) {
890 fail!("expected args satisfying {}, got {:?} and {:?}",
891 $predname, a_val, b_val);
897 // make sure idents get transformed everywhere
898 #[test] fn ident_transformation () {
899 let mut zz_fold = ToZzIdentFolder;
900 let ast = string_to_crate(@"#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}");
901 assert_pred!(matches_codepattern,
902 "matches_codepattern",
903 pprust::to_str(&mut zz_fold.fold_crate(ast),fake_print_crate,
904 token::get_ident_interner()),
905 ~"#[a]mod zz{fn zz(zz:zz,zz:zz){zz!(zz,zz,zz);zz;zz}}");
908 // even inside macro defs....
909 #[test] fn ident_transformation_in_defs () {
910 let mut zz_fold = ToZzIdentFolder;
911 let ast = string_to_crate(@"macro_rules! a {(b $c:expr $(d $e:token)f+
912 => (g $(d $d $e)+))} ");
913 assert_pred!(matches_codepattern,
914 "matches_codepattern",
915 pprust::to_str(&mut zz_fold.fold_crate(ast),fake_print_crate,
916 token::get_ident_interner()),
917 ~"zz!zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)))");