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.
14 use codemap::{respan, Span, Spanned};
17 use util::small_vector::SmallVector;
21 // We may eventually want to be able to fold over type parameters, too.
23 fn fold_crate(&mut self, c: Crate) -> Crate {
24 noop_fold_crate(c, self)
27 fn fold_meta_items(&mut self, meta_items: &[@MetaItem]) -> Vec<@MetaItem> {
28 meta_items.iter().map(|x| fold_meta_item_(*x, self)).collect()
31 fn fold_view_paths(&mut self, view_paths: &[@ViewPath]) -> Vec<@ViewPath> {
32 view_paths.iter().map(|view_path| {
33 let inner_view_path = match view_path.node {
34 ViewPathSimple(ref ident, ref path, node_id) => {
35 ViewPathSimple(ident.clone(),
39 ViewPathGlob(ref path, node_id) => {
40 ViewPathGlob(self.fold_path(path), self.new_id(node_id))
42 ViewPathList(ref path, ref path_list_idents, node_id) => {
43 ViewPathList(self.fold_path(path),
44 path_list_idents.map(|path_list_ident| {
45 let id = self.new_id(path_list_ident.node
48 node: PathListIdent_ {
49 name: path_list_ident.node
62 node: inner_view_path,
63 span: self.new_span(view_path.span),
68 fn fold_view_item(&mut self, vi: &ViewItem) -> ViewItem {
69 noop_fold_view_item(vi, self)
72 fn fold_foreign_item(&mut self, ni: @ForeignItem) -> @ForeignItem {
73 noop_fold_foreign_item(ni, self)
76 fn fold_item(&mut self, i: @Item) -> SmallVector<@Item> {
77 noop_fold_item(i, self)
80 fn fold_struct_field(&mut self, sf: &StructField) -> StructField {
82 node: ast::StructField_ {
84 id: self.new_id(sf.node.id),
85 ty: self.fold_ty(sf.node.ty),
86 attrs: sf.node.attrs.map(|e| fold_attribute_(*e, self))
88 span: self.new_span(sf.span)
92 fn fold_item_underscore(&mut self, i: &Item_) -> Item_ {
93 noop_fold_item_underscore(i, self)
96 fn fold_fn_decl(&mut self, d: &FnDecl) -> P<FnDecl> {
97 noop_fold_fn_decl(d, self)
100 fn fold_type_method(&mut self, m: &TypeMethod) -> TypeMethod {
101 noop_fold_type_method(m, self)
104 fn fold_method(&mut self, m: @Method) -> @Method {
105 noop_fold_method(m, self)
108 fn fold_block(&mut self, b: P<Block>) -> P<Block> {
109 noop_fold_block(b, self)
112 fn fold_stmt(&mut self, s: &Stmt) -> SmallVector<@Stmt> {
113 noop_fold_stmt(s, self)
116 fn fold_arm(&mut self, a: &Arm) -> Arm {
118 pats: a.pats.map(|x| self.fold_pat(*x)),
119 guard: a.guard.map(|x| self.fold_expr(x)),
120 body: self.fold_block(a.body),
124 fn fold_pat(&mut self, p: @Pat) -> @Pat {
125 noop_fold_pat(p, self)
128 fn fold_decl(&mut self, d: @Decl) -> SmallVector<@Decl> {
129 let node = match d.node {
130 DeclLocal(ref l) => SmallVector::one(DeclLocal(self.fold_local(*l))),
132 self.fold_item(it).move_iter().map(|i| DeclItem(i)).collect()
136 node.move_iter().map(|node| {
144 fn fold_expr(&mut self, e: @Expr) -> @Expr {
145 noop_fold_expr(e, self)
148 fn fold_ty(&mut self, t: P<Ty>) -> P<Ty> {
149 let node = match t.node {
150 TyNil | TyBot | TyInfer => t.node.clone(),
151 TyBox(ty) => TyBox(self.fold_ty(ty)),
152 TyUniq(ty) => TyUniq(self.fold_ty(ty)),
153 TyVec(ty) => TyVec(self.fold_ty(ty)),
154 TyPtr(ref mt) => TyPtr(fold_mt(mt, self)),
155 TyRptr(ref region, ref mt) => {
156 TyRptr(fold_opt_lifetime(region, self), fold_mt(mt, self))
158 TyClosure(ref f) => {
159 TyClosure(@ClosureTy {
162 region: fold_opt_lifetime(&f.region, self),
163 onceness: f.onceness,
164 bounds: fold_opt_bounds(&f.bounds, self),
165 decl: self.fold_fn_decl(f.decl),
166 lifetimes: f.lifetimes.map(|l| fold_lifetime(l, self)),
171 lifetimes: f.lifetimes.map(|l| fold_lifetime(l, self)),
174 decl: self.fold_fn_decl(f.decl)
177 TyTup(ref tys) => TyTup(tys.map(|&ty| self.fold_ty(ty))),
178 TyPath(ref path, ref bounds, id) => {
179 TyPath(self.fold_path(path),
180 fold_opt_bounds(bounds, self),
183 TyFixedLengthVec(ty, e) => {
184 TyFixedLengthVec(self.fold_ty(ty), self.fold_expr(e))
186 TyTypeof(expr) => TyTypeof(self.fold_expr(expr)),
189 id: self.new_id(t.id),
190 span: self.new_span(t.span),
195 fn fold_mod(&mut self, m: &Mod) -> Mod {
196 noop_fold_mod(m, self)
199 fn fold_foreign_mod(&mut self, nm: &ForeignMod) -> ForeignMod {
202 view_items: nm.view_items
204 .map(|x| self.fold_view_item(x))
208 .map(|x| self.fold_foreign_item(*x))
213 fn fold_variant(&mut self, v: &Variant) -> P<Variant> {
216 TupleVariantKind(ref variant_args) => {
217 kind = TupleVariantKind(variant_args.map(|x|
218 fold_variant_arg_(x, self)))
220 StructVariantKind(ref struct_def) => {
221 kind = StructVariantKind(@ast::StructDef {
222 fields: struct_def.fields.iter()
223 .map(|f| self.fold_struct_field(f)).collect(),
224 ctor_id: struct_def.ctor_id.map(|c| self.new_id(c))
229 let attrs = v.node.attrs.map(|x| fold_attribute_(*x, self));
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 id: self.new_id(l.id), // Needs to be first, for ast_map.
268 ty: self.fold_ty(l.ty),
269 pat: self.fold_pat(l.pat),
270 init: l.init.map(|e| self.fold_expr(e)),
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.as_slice(), self),
284 span: self.new_span(macro.span)
288 fn map_exprs(&self, f: |@Expr| -> @Expr, es: &[@Expr]) -> Vec<@Expr> {
289 es.iter().map(|x| f(*x)).collect()
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 => *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 MetaList((*id).clone(), mis.map(|e| fold_meta_item_(*e, fld)))
328 MetaNameValue(ref id, ref s) => {
329 MetaNameValue((*id).clone(), (*s).clone())
332 span: fld.new_span(mi.span) }
335 //used in noop_fold_item and noop_fold_crate
336 fn fold_attribute_<T: Folder>(at: Attribute, fld: &mut T) -> Attribute {
338 span: fld.new_span(at.span),
339 node: ast::Attribute_ {
340 style: at.node.style,
341 value: fold_meta_item_(at.node.value, fld),
342 is_sugared_doc: at.node.is_sugared_doc
347 //used in noop_fold_foreign_item and noop_fold_fn_decl
348 fn fold_arg_<T: Folder>(a: &Arg, fld: &mut T) -> Arg {
350 id: fld.new_id(a.id), // Needs to be first, for ast_map.
351 ty: fld.fold_ty(a.ty),
352 pat: fld.fold_pat(a.pat),
356 // build a new vector of tts by appling the Folder's fold_ident to
357 // all of the identifiers in the token trees.
359 // This is part of hygiene magic. As far as hygiene is concerned, there
360 // are three types of let pattern bindings or loop labels:
361 // - those defined and used in non-macro part of the program
362 // - those used as part of macro invocation arguments
363 // - those defined and used inside macro definitions
364 // Lexically, type 1 and 2 are in one group and type 3 the other. If they
365 // clash, in order for let and loop label to work hygienically, one group
366 // or the other needs to be renamed. The problem is that type 2 and 3 are
367 // parsed together (inside the macro expand function). After being parsed and
368 // AST being constructed, they can no longer be distinguished from each other.
370 // For that reason, type 2 let bindings and loop labels are actually renamed
371 // in the form of tokens instead of AST nodes, here. There are wasted effort
372 // since many token::IDENT are not necessary part of let bindings and most
373 // token::LIFETIME are certainly not loop labels. But we can't tell in their
374 // token form. So this is less ideal and hacky but it works.
375 pub fn fold_tts<T: Folder>(tts: &[TokenTree], fld: &mut T) -> Vec<TokenTree> {
376 tts.iter().map(|tt| {
378 TTTok(span, ref tok) =>
379 TTTok(span,maybe_fold_ident(tok,fld)),
380 TTDelim(tts) => TTDelim(@fold_tts(tts.as_slice(), fld)),
381 TTSeq(span, pattern, ref sep, is_optional) =>
383 @fold_tts(pattern.as_slice(), fld),
384 sep.as_ref().map(|tok|maybe_fold_ident(tok,fld)),
386 TTNonterminal(sp,ref ident) =>
387 TTNonterminal(sp,fld.fold_ident(*ident))
392 // apply ident folder if it's an ident, otherwise leave it alone
393 fn maybe_fold_ident<T: Folder>(t: &token::Token, fld: &mut T) -> token::Token {
395 token::IDENT(id, followed_by_colons) => {
396 token::IDENT(fld.fold_ident(id), followed_by_colons)
398 token::LIFETIME(id) => token::LIFETIME(fld.fold_ident(id)),
403 pub fn noop_fold_fn_decl<T: Folder>(decl: &FnDecl, fld: &mut T) -> P<FnDecl> {
405 inputs: decl.inputs.map(|x| fold_arg_(x, fld)), // bad copy
406 output: fld.fold_ty(decl.output),
408 variadic: decl.variadic
412 fn fold_ty_param_bound<T: Folder>(tpb: &TyParamBound, fld: &mut T)
415 TraitTyParamBound(ref ty) => TraitTyParamBound(fold_trait_ref(ty, fld)),
416 RegionTyParamBound => RegionTyParamBound
420 pub fn fold_ty_param<T: Folder>(tp: &TyParam, fld: &mut T) -> TyParam {
423 id: fld.new_id(tp.id),
424 bounds: tp.bounds.map(|x| fold_ty_param_bound(x, fld)),
425 default: tp.default.map(|x| fld.fold_ty(x))
429 pub fn fold_ty_params<T: Folder>(tps: &OptVec<TyParam>, fld: &mut T)
431 tps.map(|tp| fold_ty_param(tp, fld))
434 pub fn fold_lifetime<T: Folder>(l: &Lifetime, fld: &mut T) -> Lifetime {
436 id: fld.new_id(l.id),
437 span: fld.new_span(l.span),
442 pub fn fold_lifetimes<T: Folder>(lts: &OptVec<Lifetime>, fld: &mut T)
443 -> OptVec<Lifetime> {
444 lts.map(|l| fold_lifetime(l, fld))
447 pub fn fold_opt_lifetime<T: Folder>(o_lt: &Option<Lifetime>, fld: &mut T)
448 -> Option<Lifetime> {
449 o_lt.as_ref().map(|lt| fold_lifetime(lt, fld))
452 pub fn fold_generics<T: Folder>(generics: &Generics, fld: &mut T) -> Generics {
453 Generics {ty_params: fold_ty_params(&generics.ty_params, fld),
454 lifetimes: fold_lifetimes(&generics.lifetimes, fld)}
457 fn fold_struct_def<T: Folder>(struct_def: @StructDef, fld: &mut T) -> @StructDef {
459 fields: struct_def.fields.map(|f| fold_struct_field(f, fld)),
460 ctor_id: struct_def.ctor_id.map(|cid| fld.new_id(cid)),
464 fn fold_trait_ref<T: Folder>(p: &TraitRef, fld: &mut T) -> TraitRef {
466 path: fld.fold_path(&p.path),
467 ref_id: fld.new_id(p.ref_id),
471 fn fold_struct_field<T: Folder>(f: &StructField, fld: &mut T) -> StructField {
473 node: ast::StructField_ {
475 id: fld.new_id(f.node.id),
476 ty: fld.fold_ty(f.node.ty),
477 attrs: f.node.attrs.map(|a| fold_attribute_(*a, fld)),
479 span: fld.new_span(f.span),
483 fn fold_field_<T: Folder>(field: Field, folder: &mut T) -> Field {
485 ident: respan(field.ident.span, folder.fold_ident(field.ident.node)),
486 expr: folder.fold_expr(field.expr),
487 span: folder.new_span(field.span),
491 fn fold_mt<T: Folder>(mt: &MutTy, folder: &mut T) -> MutTy {
493 ty: folder.fold_ty(mt.ty),
498 fn fold_opt_bounds<T: Folder>(b: &Option<OptVec<TyParamBound>>, folder: &mut T)
499 -> Option<OptVec<TyParamBound>> {
500 b.as_ref().map(|bounds| {
502 fold_ty_param_bound(bound, folder)
507 fn fold_variant_arg_<T: Folder>(va: &VariantArg, folder: &mut T) -> VariantArg {
509 ty: folder.fold_ty(va.ty),
510 id: folder.new_id(va.id)
514 pub fn noop_fold_view_item<T: Folder>(vi: &ViewItem, folder: &mut T)
516 let inner_view_item = match vi.node {
517 ViewItemExternMod(ref ident, ref string, node_id) => {
518 ViewItemExternMod(ident.clone(),
520 folder.new_id(node_id))
522 ViewItemUse(ref view_paths) => {
523 ViewItemUse(folder.fold_view_paths(view_paths.as_slice()))
527 node: inner_view_item,
528 attrs: vi.attrs.map(|a| fold_attribute_(*a, folder)),
530 span: folder.new_span(vi.span),
534 pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> {
535 let view_items = b.view_items.map(|x| folder.fold_view_item(x));
536 let stmts = b.stmts.iter().flat_map(|s| folder.fold_stmt(*s).move_iter()).collect();
538 id: folder.new_id(b.id), // Needs to be first, for ast_map.
539 view_items: view_items,
541 expr: b.expr.map(|x| folder.fold_expr(x)),
543 span: folder.new_span(b.span),
547 pub fn noop_fold_item_underscore<T: Folder>(i: &Item_, folder: &mut T) -> Item_ {
549 ItemStatic(t, m, e) => {
550 ItemStatic(folder.fold_ty(t), m, folder.fold_expr(e))
552 ItemFn(decl, purity, abi, ref generics, body) => {
554 folder.fold_fn_decl(decl),
557 fold_generics(generics, folder),
558 folder.fold_block(body)
561 ItemMod(ref m) => ItemMod(folder.fold_mod(m)),
562 ItemForeignMod(ref nm) => ItemForeignMod(folder.fold_foreign_mod(nm)),
563 ItemTy(t, ref generics) => {
564 ItemTy(folder.fold_ty(t), fold_generics(generics, folder))
566 ItemEnum(ref enum_definition, ref generics) => {
569 variants: enum_definition.variants.map(|&x| {
570 folder.fold_variant(x)
573 fold_generics(generics, folder))
575 ItemStruct(ref struct_def, ref generics) => {
576 let struct_def = fold_struct_def(*struct_def, folder);
577 ItemStruct(struct_def, fold_generics(generics, folder))
579 ItemImpl(ref generics, ref ifce, ty, ref methods) => {
580 ItemImpl(fold_generics(generics, folder),
581 ifce.as_ref().map(|p| fold_trait_ref(p, folder)),
583 methods.map(|x| folder.fold_method(*x))
586 ItemTrait(ref generics, ref traits, ref methods) => {
587 let methods = methods.map(|method| {
589 Required(ref m) => Required(folder.fold_type_method(m)),
590 Provided(method) => Provided(folder.fold_method(method))
593 ItemTrait(fold_generics(generics, folder),
594 traits.map(|p| fold_trait_ref(p, folder)),
597 ItemMac(ref m) => ItemMac(folder.fold_mac(m)),
601 pub fn noop_fold_type_method<T: Folder>(m: &TypeMethod, fld: &mut T) -> TypeMethod {
603 id: fld.new_id(m.id), // Needs to be first, for ast_map.
604 ident: fld.fold_ident(m.ident),
605 attrs: m.attrs.map(|a| fold_attribute_(*a, fld)),
607 decl: fld.fold_fn_decl(m.decl),
608 generics: fold_generics(&m.generics, fld),
609 explicit_self: fld.fold_explicit_self(&m.explicit_self),
610 span: fld.new_span(m.span),
614 pub fn noop_fold_mod<T: Folder>(m: &Mod, folder: &mut T) -> Mod {
616 view_items: m.view_items
618 .map(|x| folder.fold_view_item(x)).collect(),
619 items: m.items.iter().flat_map(|x| folder.fold_item(*x).move_iter()).collect(),
623 pub fn noop_fold_crate<T: Folder>(c: Crate, folder: &mut T) -> Crate {
625 module: folder.fold_mod(&c.module),
626 attrs: c.attrs.map(|x| fold_attribute_(*x, folder)),
627 config: c.config.map(|x| fold_meta_item_(*x, folder)),
628 span: folder.new_span(c.span),
632 pub fn noop_fold_item<T: Folder>(i: &Item, folder: &mut T) -> SmallVector<@Item> {
633 let id = folder.new_id(i.id); // Needs to be first, for ast_map.
634 let node = folder.fold_item_underscore(&i.node);
635 let ident = match node {
636 // The node may have changed, recompute the "pretty" impl name.
637 ItemImpl(_, ref maybe_trait, ty, _) => {
638 ast_util::impl_pretty_name(maybe_trait, ty)
643 SmallVector::one(@Item {
645 ident: folder.fold_ident(ident),
646 attrs: i.attrs.map(|e| fold_attribute_(*e, folder)),
649 span: folder.new_span(i.span)
653 pub fn noop_fold_foreign_item<T: Folder>(ni: &ForeignItem, folder: &mut T) -> @ForeignItem {
655 id: folder.new_id(ni.id), // Needs to be first, for ast_map.
656 ident: folder.fold_ident(ni.ident),
657 attrs: ni.attrs.map(|x| fold_attribute_(*x, folder)),
658 node: match ni.node {
659 ForeignItemFn(ref fdec, ref generics) => {
660 ForeignItemFn(P(FnDecl {
661 inputs: fdec.inputs.map(|a| fold_arg_(a, folder)),
662 output: folder.fold_ty(fdec.output),
664 variadic: fdec.variadic
665 }), fold_generics(generics, folder))
667 ForeignItemStatic(t, m) => {
668 ForeignItemStatic(folder.fold_ty(t), m)
671 span: folder.new_span(ni.span),
676 pub fn noop_fold_method<T: Folder>(m: &Method, folder: &mut T) -> @Method {
678 id: folder.new_id(m.id), // Needs to be first, for ast_map.
679 ident: folder.fold_ident(m.ident),
680 attrs: m.attrs.map(|a| fold_attribute_(*a, folder)),
681 generics: fold_generics(&m.generics, folder),
682 explicit_self: folder.fold_explicit_self(&m.explicit_self),
684 decl: folder.fold_fn_decl(m.decl),
685 body: folder.fold_block(m.body),
686 span: folder.new_span(m.span),
691 pub fn noop_fold_pat<T: Folder>(p: @Pat, folder: &mut T) -> @Pat {
692 let node = match p.node {
694 PatWildMulti => PatWildMulti,
695 PatIdent(binding_mode, ref pth, ref sub) => {
696 PatIdent(binding_mode,
697 folder.fold_path(pth),
698 sub.map(|x| folder.fold_pat(x)))
700 PatLit(e) => PatLit(folder.fold_expr(e)),
701 PatEnum(ref pth, ref pats) => {
702 PatEnum(folder.fold_path(pth),
703 pats.as_ref().map(|pats| pats.map(|x| folder.fold_pat(*x))))
705 PatStruct(ref pth, ref fields, etc) => {
706 let pth_ = folder.fold_path(pth);
707 let fs = fields.map(|f| {
710 pat: folder.fold_pat(f.pat)
713 PatStruct(pth_, fs, etc)
715 PatTup(ref elts) => PatTup(elts.map(|x| folder.fold_pat(*x))),
716 PatUniq(inner) => PatUniq(folder.fold_pat(inner)),
717 PatRegion(inner) => PatRegion(folder.fold_pat(inner)),
718 PatRange(e1, e2) => {
719 PatRange(folder.fold_expr(e1), folder.fold_expr(e2))
721 PatVec(ref before, ref slice, ref after) => {
722 PatVec(before.map(|x| folder.fold_pat(*x)),
723 slice.map(|x| folder.fold_pat(x)),
724 after.map(|x| folder.fold_pat(*x)))
729 id: folder.new_id(p.id),
730 span: folder.new_span(p.span),
735 pub fn noop_fold_expr<T: Folder>(e: @Expr, folder: &mut T) -> @Expr {
736 let node = match e.node {
737 ExprVstore(e, v) => {
738 ExprVstore(folder.fold_expr(e), v)
741 ExprBox(folder.fold_expr(p), folder.fold_expr(e))
743 ExprVec(ref exprs, mutt) => {
744 ExprVec(exprs.map(|&x| folder.fold_expr(x)), mutt)
746 ExprRepeat(expr, count, mutt) => {
747 ExprRepeat(folder.fold_expr(expr), folder.fold_expr(count), mutt)
749 ExprTup(ref elts) => ExprTup(elts.map(|x| folder.fold_expr(*x))),
750 ExprCall(f, ref args) => {
751 ExprCall(folder.fold_expr(f),
752 args.map(|&x| folder.fold_expr(x)))
754 ExprMethodCall(i, ref tps, ref args) => {
756 folder.fold_ident(i),
757 tps.map(|&x| folder.fold_ty(x)),
758 args.map(|&x| folder.fold_expr(x)))
760 ExprBinary(binop, lhs, rhs) => {
762 folder.fold_expr(lhs),
763 folder.fold_expr(rhs))
765 ExprUnary(binop, ohs) => {
766 ExprUnary(binop, folder.fold_expr(ohs))
768 ExprLit(_) => e.node.clone(),
769 ExprCast(expr, ty) => {
770 ExprCast(folder.fold_expr(expr), folder.fold_ty(ty))
772 ExprAddrOf(m, ohs) => ExprAddrOf(m, folder.fold_expr(ohs)),
773 ExprIf(cond, tr, fl) => {
774 ExprIf(folder.fold_expr(cond),
775 folder.fold_block(tr),
776 fl.map(|x| folder.fold_expr(x)))
778 ExprWhile(cond, body) => {
779 ExprWhile(folder.fold_expr(cond), folder.fold_block(body))
781 ExprForLoop(pat, iter, body, ref maybe_ident) => {
782 ExprForLoop(folder.fold_pat(pat),
783 folder.fold_expr(iter),
784 folder.fold_block(body),
785 maybe_ident.map(|i| folder.fold_ident(i)))
787 ExprLoop(body, opt_ident) => {
788 ExprLoop(folder.fold_block(body),
789 opt_ident.map(|x| folder.fold_ident(x)))
791 ExprMatch(expr, ref arms) => {
792 ExprMatch(folder.fold_expr(expr),
793 arms.map(|x| folder.fold_arm(x)))
795 ExprFnBlock(decl, body) => {
796 ExprFnBlock(folder.fold_fn_decl(decl), folder.fold_block(body))
798 ExprProc(decl, body) => {
799 ExprProc(folder.fold_fn_decl(decl), folder.fold_block(body))
801 ExprBlock(blk) => ExprBlock(folder.fold_block(blk)),
802 ExprAssign(el, er) => {
803 ExprAssign(folder.fold_expr(el), folder.fold_expr(er))
805 ExprAssignOp(op, el, er) => {
807 folder.fold_expr(el),
808 folder.fold_expr(er))
810 ExprField(el, id, ref tys) => {
811 ExprField(folder.fold_expr(el),
812 folder.fold_ident(id),
813 tys.map(|&x| folder.fold_ty(x)))
815 ExprIndex(el, er) => {
816 ExprIndex(folder.fold_expr(el), folder.fold_expr(er))
818 ExprPath(ref pth) => ExprPath(folder.fold_path(pth)),
819 ExprLogLevel => ExprLogLevel,
820 ExprBreak(opt_ident) => ExprBreak(opt_ident.map(|x| folder.fold_ident(x))),
821 ExprAgain(opt_ident) => ExprAgain(opt_ident.map(|x| folder.fold_ident(x))),
823 ExprRet(e.map(|x| folder.fold_expr(x)))
825 ExprInlineAsm(ref a) => {
826 ExprInlineAsm(InlineAsm {
827 inputs: a.inputs.map(|&(ref c, input)| {
828 ((*c).clone(), folder.fold_expr(input))
830 outputs: a.outputs.map(|&(ref c, out)| {
831 ((*c).clone(), folder.fold_expr(out))
836 ExprMac(ref mac) => ExprMac(folder.fold_mac(mac)),
837 ExprStruct(ref path, ref fields, maybe_expr) => {
838 ExprStruct(folder.fold_path(path),
839 fields.map(|x| fold_field_(*x, folder)),
840 maybe_expr.map(|x| folder.fold_expr(x)))
842 ExprParen(ex) => ExprParen(folder.fold_expr(ex))
846 id: folder.new_id(e.id),
848 span: folder.new_span(e.span),
852 pub fn noop_fold_stmt<T: Folder>(s: &Stmt, folder: &mut T) -> SmallVector<@Stmt> {
853 let nodes = match s.node {
854 StmtDecl(d, nid) => {
855 folder.fold_decl(d).move_iter()
856 .map(|d| StmtDecl(d, folder.new_id(nid)))
859 StmtExpr(e, nid) => {
860 SmallVector::one(StmtExpr(folder.fold_expr(e), folder.new_id(nid)))
862 StmtSemi(e, nid) => {
863 SmallVector::one(StmtSemi(folder.fold_expr(e), folder.new_id(nid)))
865 StmtMac(ref mac, semi) => SmallVector::one(StmtMac(folder.fold_mac(mac), semi))
868 nodes.move_iter().map(|node| @Spanned {
870 span: folder.new_span(s.span),
878 use util::parser_testing::{string_to_crate, matches_codepattern};
883 // this version doesn't care about getting comments or docstrings in.
884 fn fake_print_crate(s: &mut pprust::State,
885 krate: &ast::Crate) -> io::IoResult<()> {
886 pprust::print_mod(s, &krate.module, krate.attrs.as_slice())
889 // change every identifier to "zz"
890 struct ToZzIdentFolder;
892 impl Folder for ToZzIdentFolder {
893 fn fold_ident(&mut self, _: ast::Ident) -> ast::Ident {
894 token::str_to_ident("zz")
898 // maybe add to expand.rs...
899 macro_rules! assert_pred (
900 ($pred:expr, $predname:expr, $a:expr , $b:expr) => (
902 let pred_val = $pred;
905 if !(pred_val(a_val,b_val)) {
906 fail!("expected args satisfying {}, got {:?} and {:?}",
907 $predname, a_val, b_val);
913 // make sure idents get transformed everywhere
914 #[test] fn ident_transformation () {
915 let mut zz_fold = ToZzIdentFolder;
916 let ast = string_to_crate(
917 ~"#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}");
918 assert_pred!(matches_codepattern,
919 "matches_codepattern",
920 pprust::to_str(&mut zz_fold.fold_crate(ast),fake_print_crate),
921 ~"#[a]mod zz{fn zz(zz:zz,zz:zz){zz!(zz,zz,zz);zz;zz}}");
924 // even inside macro defs....
925 #[test] fn ident_transformation_in_defs () {
926 let mut zz_fold = ToZzIdentFolder;
927 let ast = string_to_crate(
928 ~"macro_rules! a {(b $c:expr $(d $e:token)f+ => \
929 (g $(d $d $e)+))} ");
930 assert_pred!(matches_codepattern,
931 "matches_codepattern",
932 pprust::to_str(&mut zz_fold.fold_crate(ast),fake_print_crate),
933 ~"zz!zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)))");