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};
17 // We may eventually want to be able to fold over type parameters, too.
19 fn fold_crate(&self, c: Crate) -> Crate {
20 noop_fold_crate(c, self)
23 fn fold_meta_items(&self, meta_items: &[@MetaItem]) -> ~[@MetaItem] {
24 meta_items.map(|x| fold_meta_item_(*x, self))
27 fn fold_view_paths(&self, view_paths: &[@view_path]) -> ~[@view_path] {
28 view_paths.map(|view_path| {
29 let inner_view_path = match view_path.node {
30 view_path_simple(ref ident, ref path, node_id) => {
31 view_path_simple(ident.clone(),
35 view_path_glob(ref path, node_id) => {
36 view_path_glob(self.fold_path(path), self.new_id(node_id))
38 view_path_list(ref path, ref path_list_idents, node_id) => {
39 view_path_list(self.fold_path(path),
40 path_list_idents.map(|path_list_ident| {
41 let id = self.new_id(path_list_ident.node
44 node: path_list_ident_ {
45 name: path_list_ident.node
58 node: inner_view_path,
59 span: self.new_span(view_path.span),
64 fn fold_view_item(&self, vi: &view_item) -> view_item {
65 let inner_view_item = match vi.node {
66 view_item_extern_mod(ref ident,
70 view_item_extern_mod(ident.clone(),
72 self.fold_meta_items(*meta_items),
75 view_item_use(ref view_paths) => {
76 view_item_use(self.fold_view_paths(*view_paths))
80 node: inner_view_item,
81 attrs: vi.attrs.map(|a| fold_attribute_(*a, self)),
83 span: self.new_span(vi.span),
87 fn fold_foreign_item(&self, ni: @foreign_item) -> @foreign_item {
88 let fold_attribute = |x| fold_attribute_(x, self);
91 ident: self.fold_ident(ni.ident),
92 attrs: ni.attrs.map(|x| fold_attribute(*x)),
95 foreign_item_fn(ref fdec, ref generics) => {
98 inputs: fdec.inputs.map(|a| fold_arg_(a,
100 output: self.fold_ty(&fdec.output),
102 variadic: fdec.variadic
104 fold_generics(generics, self))
106 foreign_item_static(ref t, m) => {
107 foreign_item_static(self.fold_ty(t), m)
110 id: self.new_id(ni.id),
111 span: self.new_span(ni.span),
116 fn fold_item(&self, i: @item) -> Option<@item> {
117 noop_fold_item(i, self)
120 fn fold_struct_field(&self, sf: @struct_field) -> @struct_field {
121 let fold_attribute = |x| fold_attribute_(x, self);
124 node: ast::struct_field_ {
126 id: self.new_id(sf.node.id),
127 ty: self.fold_ty(&sf.node.ty),
128 attrs: sf.node.attrs.map(|e| fold_attribute(*e))
130 span: self.new_span(sf.span)
134 fn fold_item_underscore(&self, i: &item_) -> item_ {
135 noop_fold_item_underscore(i, self)
138 fn fold_type_method(&self, m: &TypeMethod) -> TypeMethod {
139 noop_fold_type_method(m, self)
142 fn fold_method(&self, m: @method) -> @method {
144 ident: self.fold_ident(m.ident),
145 attrs: m.attrs.map(|a| fold_attribute_(*a, self)),
146 generics: fold_generics(&m.generics, self),
147 explicit_self: m.explicit_self,
149 decl: fold_fn_decl(&m.decl, self),
150 body: self.fold_block(&m.body),
151 id: self.new_id(m.id),
152 span: self.new_span(m.span),
153 self_id: self.new_id(m.self_id),
158 fn fold_block(&self, b: &Block) -> Block {
159 noop_fold_block(b, self)
162 fn fold_stmt(&self, s: &Stmt) -> Option<@Stmt> {
163 noop_fold_stmt(s, self)
166 fn fold_arm(&self, a: &Arm) -> Arm {
168 pats: a.pats.map(|x| self.fold_pat(*x)),
169 guard: a.guard.map(|x| self.fold_expr(x)),
170 body: self.fold_block(&a.body),
174 fn fold_pat(&self, p: @Pat) -> @Pat {
175 let node = match p.node {
177 PatIdent(binding_mode, ref pth, ref sub) => {
178 PatIdent(binding_mode,
180 sub.map(|x| self.fold_pat(x)))
182 PatLit(e) => PatLit(self.fold_expr(e)),
183 PatEnum(ref pth, ref pats) => {
184 PatEnum(self.fold_path(pth),
185 pats.as_ref().map(|pats| pats.map(|x| self.fold_pat(*x))))
187 PatStruct(ref pth, ref fields, etc) => {
188 let pth_ = self.fold_path(pth);
189 let fs = do fields.map |f| {
192 pat: self.fold_pat(f.pat)
195 PatStruct(pth_, fs, etc)
197 PatTup(ref elts) => PatTup(elts.map(|x| self.fold_pat(*x))),
198 PatBox(inner) => PatBox(self.fold_pat(inner)),
199 PatUniq(inner) => PatUniq(self.fold_pat(inner)),
200 PatRegion(inner) => PatRegion(self.fold_pat(inner)),
201 PatRange(e1, e2) => {
202 PatRange(self.fold_expr(e1), self.fold_expr(e2))
204 PatVec(ref before, ref slice, ref after) => {
205 PatVec(before.map(|x| self.fold_pat(*x)),
206 slice.map(|x| self.fold_pat(x)),
207 after.map(|x| self.fold_pat(*x)))
212 id: self.new_id(p.id),
213 span: self.new_span(p.span),
218 fn fold_decl(&self, d: @Decl) -> Option<@Decl> {
219 let node = match d.node {
220 DeclLocal(ref l) => Some(DeclLocal(self.fold_local(*l))),
222 match self.fold_item(it) {
223 Some(it_folded) => Some(DeclItem(it_folded)),
237 fn fold_expr(&self, e: @Expr) -> @Expr {
238 noop_fold_expr(e, self)
241 fn fold_ty(&self, t: &Ty) -> Ty {
242 let node = match t.node {
243 ty_nil | ty_bot | ty_infer => t.node.clone(),
244 ty_box(ref mt) => ty_box(fold_mt(mt, self)),
245 ty_uniq(ref mt) => ty_uniq(fold_mt(mt, self)),
246 ty_vec(ref mt) => ty_vec(fold_mt(mt, self)),
247 ty_ptr(ref mt) => ty_ptr(fold_mt(mt, self)),
248 ty_rptr(region, ref mt) => ty_rptr(region, fold_mt(mt, self)),
249 ty_closure(ref f) => {
250 ty_closure(@TyClosure {
254 onceness: f.onceness,
255 bounds: fold_opt_bounds(&f.bounds, self),
256 decl: fold_fn_decl(&f.decl, self),
257 lifetimes: f.lifetimes.map(|l| fold_lifetime(l, self)),
260 ty_bare_fn(ref f) => {
261 ty_bare_fn(@TyBareFn {
262 lifetimes: f.lifetimes.map(|l| fold_lifetime(l, self)),
265 decl: fold_fn_decl(&f.decl, self)
268 ty_tup(ref tys) => ty_tup(tys.map(|ty| self.fold_ty(ty))),
269 ty_path(ref path, ref bounds, id) => {
270 ty_path(self.fold_path(path),
271 fold_opt_bounds(bounds, self),
274 ty_fixed_length_vec(ref mt, e) => {
275 ty_fixed_length_vec(fold_mt(mt, self), self.fold_expr(e))
277 ty_mac(ref mac) => ty_mac(self.fold_mac(mac)),
278 ty_typeof(expr) => ty_typeof(self.fold_expr(expr)),
281 id: self.new_id(t.id),
282 span: self.new_span(t.span),
287 fn fold_mod(&self, m: &_mod) -> _mod {
288 noop_fold_mod(m, self)
291 fn fold_foreign_mod(&self, nm: &foreign_mod) -> foreign_mod {
294 view_items: nm.view_items
296 .map(|x| self.fold_view_item(x))
300 .map(|x| self.fold_foreign_item(*x))
305 fn fold_variant(&self, v: &variant) -> variant {
308 tuple_variant_kind(ref variant_args) => {
309 kind = tuple_variant_kind(variant_args.map(|x|
310 fold_variant_arg_(x, self)))
312 struct_variant_kind(ref struct_def) => {
313 kind = struct_variant_kind(@ast::struct_def {
314 fields: struct_def.fields.iter()
315 .map(|f| self.fold_struct_field(*f)).collect(),
316 ctor_id: struct_def.ctor_id.map(|c| self.new_id(c))
321 let fold_attribute = |x| fold_attribute_(x, self);
322 let attrs = v.node.attrs.map(|x| fold_attribute(*x));
324 let de = match v.node.disr_expr {
325 Some(e) => Some(self.fold_expr(e)),
328 let node = ast::variant_ {
332 id: self.new_id(v.node.id),
338 span: self.new_span(v.span),
342 fn fold_ident(&self, i: Ident) -> Ident {
346 fn fold_path(&self, p: &Path) -> Path {
348 span: self.new_span(p.span),
350 segments: p.segments.map(|segment| ast::PathSegment {
351 identifier: self.fold_ident(segment.identifier),
352 lifetime: segment.lifetime,
353 types: segment.types.map(|typ| self.fold_ty(typ)),
358 fn fold_local(&self, l: @Local) -> @Local {
360 ty: self.fold_ty(&l.ty),
361 pat: self.fold_pat(l.pat),
362 init: l.init.map(|e| self.fold_expr(e)),
363 id: self.new_id(l.id),
364 span: self.new_span(l.span),
368 fn fold_mac(&self, macro: &mac) -> mac {
370 node: match macro.node {
371 mac_invoc_tt(ref p, ref tts, ctxt) => {
372 mac_invoc_tt(self.fold_path(p),
373 fold_tts(*tts, self),
377 span: self.new_span(macro.span)
381 fn map_exprs(&self, f: &fn(@Expr) -> @Expr, es: &[@Expr]) -> ~[@Expr] {
385 fn new_id(&self, i: NodeId) -> NodeId {
389 fn new_span(&self, sp: Span) -> Span {
394 /* some little folds that probably aren't useful to have in ast_fold itself*/
396 //used in noop_fold_item and noop_fold_crate and noop_fold_crate_directive
397 fn fold_meta_item_<T:ast_fold>(mi: @MetaItem, fld: &T) -> @MetaItem {
401 MetaWord(id) => MetaWord(id),
402 MetaList(id, ref mis) => {
403 let fold_meta_item = |x| fold_meta_item_(x, fld);
406 mis.map(|e| fold_meta_item(*e))
409 MetaNameValue(id, s) => MetaNameValue(id, s)
411 span: fld.new_span(mi.span) }
414 //used in noop_fold_item and noop_fold_crate
415 fn fold_attribute_<T:ast_fold>(at: Attribute, fld: &T) -> Attribute {
417 span: fld.new_span(at.span),
418 node: ast::Attribute_ {
419 style: at.node.style,
420 value: fold_meta_item_(at.node.value, fld),
421 is_sugared_doc: at.node.is_sugared_doc
426 //used in noop_fold_foreign_item and noop_fold_fn_decl
427 fn fold_arg_<T:ast_fold>(a: &arg, fld: &T) -> arg {
429 ty: fld.fold_ty(&a.ty),
430 pat: fld.fold_pat(a.pat),
431 id: fld.new_id(a.id),
435 // build a new vector of tts by appling the ast_fold's fold_ident to
436 // all of the identifiers in the token trees.
437 pub fn fold_tts<T:ast_fold>(tts: &[token_tree], fld: &T) -> ~[token_tree] {
440 tt_tok(span, ref tok) =>
441 tt_tok(span,maybe_fold_ident(tok,fld)),
442 tt_delim(ref tts) => tt_delim(@mut fold_tts(**tts, fld)),
443 tt_seq(span, ref pattern, ref sep, is_optional) =>
445 @mut fold_tts(**pattern, fld),
446 sep.as_ref().map(|tok|maybe_fold_ident(tok,fld)),
448 tt_nonterminal(sp,ref ident) =>
449 tt_nonterminal(sp,fld.fold_ident(*ident))
454 // apply ident folder if it's an ident, otherwise leave it alone
455 fn maybe_fold_ident<T:ast_fold>(t: &token::Token, fld: &T) -> token::Token {
457 token::IDENT(id, followed_by_colons) => {
458 token::IDENT(fld.fold_ident(id), followed_by_colons)
464 pub fn fold_fn_decl<T:ast_fold>(decl: &ast::fn_decl, fld: &T)
467 inputs: decl.inputs.map(|x| fold_arg_(x, fld)), // bad copy
468 output: fld.fold_ty(&decl.output),
470 variadic: decl.variadic
474 fn fold_ty_param_bound<T:ast_fold>(tpb: &TyParamBound, fld: &T)
477 TraitTyParamBound(ref ty) => TraitTyParamBound(fold_trait_ref(ty, fld)),
478 RegionTyParamBound => RegionTyParamBound
482 pub fn fold_ty_param<T:ast_fold>(tp: &TyParam, fld: &T) -> TyParam {
485 id: fld.new_id(tp.id),
486 bounds: tp.bounds.map(|x| fold_ty_param_bound(x, fld)),
490 pub fn fold_ty_params<T:ast_fold>(tps: &OptVec<TyParam>, fld: &T)
492 tps.map(|tp| fold_ty_param(tp, fld))
495 pub fn fold_lifetime<T:ast_fold>(l: &Lifetime, fld: &T) -> Lifetime {
497 id: fld.new_id(l.id),
498 span: fld.new_span(l.span),
503 pub fn fold_lifetimes<T:ast_fold>(lts: &OptVec<Lifetime>, fld: &T)
504 -> OptVec<Lifetime> {
505 lts.map(|l| fold_lifetime(l, fld))
508 pub fn fold_generics<T:ast_fold>(generics: &Generics, fld: &T) -> Generics {
509 Generics {ty_params: fold_ty_params(&generics.ty_params, fld),
510 lifetimes: fold_lifetimes(&generics.lifetimes, fld)}
513 fn fold_struct_def<T:ast_fold>(struct_def: @ast::struct_def, fld: &T)
514 -> @ast::struct_def {
516 fields: struct_def.fields.map(|f| fold_struct_field(*f, fld)),
517 ctor_id: struct_def.ctor_id.map(|cid| fld.new_id(cid)),
521 fn noop_fold_view_item(vi: &view_item_, fld: @ast_fold) -> view_item_ {
523 view_item_extern_mod(ident, name, ref meta_items, node_id) => {
524 view_item_extern_mod(ident,
526 fld.fold_meta_items(*meta_items),
529 view_item_use(ref view_paths) => {
530 view_item_use(fld.fold_view_paths(*view_paths))
535 fn fold_trait_ref<T:ast_fold>(p: &trait_ref, fld: &T) -> trait_ref {
537 path: fld.fold_path(&p.path),
538 ref_id: fld.new_id(p.ref_id),
542 fn fold_struct_field<T:ast_fold>(f: @struct_field, fld: &T) -> @struct_field {
544 node: ast::struct_field_ {
546 id: fld.new_id(f.node.id),
547 ty: fld.fold_ty(&f.node.ty),
548 attrs: f.node.attrs.map(|a| fold_attribute_(*a, fld)),
550 span: fld.new_span(f.span),
554 fn fold_field_<T:ast_fold>(field: Field, folder: &T) -> Field {
556 ident: respan(field.ident.span, folder.fold_ident(field.ident.node)),
557 expr: folder.fold_expr(field.expr),
558 span: folder.new_span(field.span),
562 fn fold_mt<T:ast_fold>(mt: &mt, folder: &T) -> mt {
564 ty: ~folder.fold_ty(mt.ty),
569 fn fold_field<T:ast_fold>(f: TypeField, folder: &T) -> TypeField {
571 ident: folder.fold_ident(f.ident),
572 mt: fold_mt(&f.mt, folder),
573 span: folder.new_span(f.span),
577 fn fold_opt_bounds<T:ast_fold>(b: &Option<OptVec<TyParamBound>>, folder: &T)
578 -> Option<OptVec<TyParamBound>> {
579 do b.as_ref().map |bounds| {
580 do bounds.map |bound| {
581 fold_ty_param_bound(bound, folder)
586 fn fold_variant_arg_<T:ast_fold>(va: &variant_arg, folder: &T)
589 ty: folder.fold_ty(&va.ty),
590 id: folder.new_id(va.id)
594 pub fn noop_fold_block<T:ast_fold>(b: &Block, folder: &T) -> Block {
595 let view_items = b.view_items.map(|x| folder.fold_view_item(x));
597 for stmt in b.stmts.iter() {
598 match folder.fold_stmt(*stmt) {
600 Some(stmt) => stmts.push(stmt)
604 view_items: view_items,
606 expr: b.expr.map(|x| folder.fold_expr(x)),
607 id: folder.new_id(b.id),
609 span: folder.new_span(b.span),
613 pub fn noop_fold_item_underscore<T:ast_fold>(i: &item_, folder: &T) -> item_ {
615 item_static(ref t, m, e) => {
616 item_static(folder.fold_ty(t), m, folder.fold_expr(e))
618 item_fn(ref decl, purity, abi, ref generics, ref body) => {
620 fold_fn_decl(decl, folder),
623 fold_generics(generics, folder),
624 folder.fold_block(body)
627 item_mod(ref m) => item_mod(folder.fold_mod(m)),
628 item_foreign_mod(ref nm) => {
629 item_foreign_mod(folder.fold_foreign_mod(nm))
631 item_ty(ref t, ref generics) => {
632 item_ty(folder.fold_ty(t),
633 fold_generics(generics, folder))
635 item_enum(ref enum_definition, ref generics) => {
638 variants: do enum_definition.variants.map |x| {
639 folder.fold_variant(x)
642 fold_generics(generics, folder))
644 item_struct(ref struct_def, ref generics) => {
645 let struct_def = fold_struct_def(*struct_def, folder);
646 item_struct(struct_def, fold_generics(generics, folder))
648 item_impl(ref generics, ref ifce, ref ty, ref methods) => {
649 item_impl(fold_generics(generics, folder),
650 ifce.as_ref().map(|p| fold_trait_ref(p, folder)),
652 methods.map(|x| folder.fold_method(*x))
655 item_trait(ref generics, ref traits, ref methods) => {
656 let methods = do methods.map |method| {
658 required(ref m) => required(folder.fold_type_method(m)),
659 provided(method) => provided(folder.fold_method(method))
662 item_trait(fold_generics(generics, folder),
663 traits.map(|p| fold_trait_ref(p, folder)),
666 item_mac(ref m) => item_mac(folder.fold_mac(m)),
670 pub fn noop_fold_type_method<T:ast_fold>(m: &TypeMethod, fld: &T)
673 ident: fld.fold_ident(m.ident),
674 attrs: m.attrs.map(|a| fold_attribute_(*a, fld)),
676 decl: fold_fn_decl(&m.decl, fld),
677 generics: fold_generics(&m.generics, fld),
678 explicit_self: m.explicit_self,
679 id: fld.new_id(m.id),
680 span: fld.new_span(m.span),
684 pub fn noop_fold_mod<T:ast_fold>(m: &_mod, folder: &T) -> _mod {
686 view_items: m.view_items
688 .map(|x| folder.fold_view_item(x)).collect(),
689 items: m.items.iter().filter_map(|x| folder.fold_item(*x)).collect(),
693 pub fn noop_fold_crate<T:ast_fold>(c: Crate, folder: &T) -> Crate {
694 let fold_meta_item = |x| fold_meta_item_(x, folder);
695 let fold_attribute = |x| fold_attribute_(x, folder);
698 module: folder.fold_mod(&c.module),
699 attrs: c.attrs.map(|x| fold_attribute(*x)),
700 config: c.config.map(|x| fold_meta_item(*x)),
701 span: folder.new_span(c.span),
705 pub fn noop_fold_item<T:ast_fold>(i: @ast::item, folder: &T)
706 -> Option<@ast::item> {
707 let fold_attribute = |x| fold_attribute_(x, folder);
710 ident: folder.fold_ident(i.ident),
711 attrs: i.attrs.map(|e| fold_attribute(*e)),
712 id: folder.new_id(i.id),
713 node: folder.fold_item_underscore(&i.node),
715 span: folder.new_span(i.span)
719 pub fn noop_fold_expr<T:ast_fold>(e: @ast::Expr, folder: &T) -> @ast::Expr {
720 let fold_field = |x| fold_field_(x, folder);
722 let node = match e.node {
723 ExprVstore(e, v) => {
724 ExprVstore(folder.fold_expr(e), v)
726 ExprVec(ref exprs, mutt) => {
727 ExprVec(folder.map_exprs(|x| folder.fold_expr(x), *exprs), mutt)
729 ExprRepeat(expr, count, mutt) => {
730 ExprRepeat(folder.fold_expr(expr), folder.fold_expr(count), mutt)
732 ExprTup(ref elts) => ExprTup(elts.map(|x| folder.fold_expr(*x))),
733 ExprCall(f, ref args, blk) => {
734 ExprCall(folder.fold_expr(f),
735 folder.map_exprs(|x| folder.fold_expr(x), *args),
738 ExprMethodCall(callee_id, f, i, ref tps, ref args, blk) => {
740 folder.new_id(callee_id),
742 folder.fold_ident(i),
743 tps.map(|x| folder.fold_ty(x)),
744 folder.map_exprs(|x| folder.fold_expr(x), *args),
748 ExprBinary(callee_id, binop, lhs, rhs) => {
749 ExprBinary(folder.new_id(callee_id),
751 folder.fold_expr(lhs),
752 folder.fold_expr(rhs))
754 ExprUnary(callee_id, binop, ohs) => {
755 ExprUnary(folder.new_id(callee_id), binop, folder.fold_expr(ohs))
757 ExprDoBody(f) => ExprDoBody(folder.fold_expr(f)),
758 ExprLit(_) => e.node.clone(),
759 ExprCast(expr, ref ty) => {
760 ExprCast(folder.fold_expr(expr), folder.fold_ty(ty))
762 ExprAddrOf(m, ohs) => ExprAddrOf(m, folder.fold_expr(ohs)),
763 ExprIf(cond, ref tr, fl) => {
764 ExprIf(folder.fold_expr(cond),
765 folder.fold_block(tr),
766 fl.map(|x| folder.fold_expr(x)))
768 ExprWhile(cond, ref body) => {
769 ExprWhile(folder.fold_expr(cond), folder.fold_block(body))
771 ExprForLoop(pat, iter, ref body, ref maybe_ident) => {
772 ExprForLoop(folder.fold_pat(pat),
773 folder.fold_expr(iter),
774 folder.fold_block(body),
775 maybe_ident.map(|i| folder.fold_ident(i)))
777 ExprLoop(ref body, opt_ident) => {
778 ExprLoop(folder.fold_block(body),
779 opt_ident.map(|x| folder.fold_ident(x)))
781 ExprMatch(expr, ref arms) => {
782 ExprMatch(folder.fold_expr(expr),
783 arms.map(|x| folder.fold_arm(x)))
785 ExprFnBlock(ref decl, ref body) => {
787 fold_fn_decl(decl, folder),
788 folder.fold_block(body)
791 ExprProc(ref decl, ref body) => {
792 ExprProc(fold_fn_decl(decl, folder), folder.fold_block(body))
794 ExprBlock(ref blk) => ExprBlock(folder.fold_block(blk)),
795 ExprAssign(el, er) => {
796 ExprAssign(folder.fold_expr(el), folder.fold_expr(er))
798 ExprAssignOp(callee_id, op, el, er) => {
799 ExprAssignOp(folder.new_id(callee_id),
801 folder.fold_expr(el),
802 folder.fold_expr(er))
804 ExprField(el, id, ref tys) => {
805 ExprField(folder.fold_expr(el),
806 folder.fold_ident(id),
807 tys.map(|x| folder.fold_ty(x)))
809 ExprIndex(callee_id, el, er) => {
810 ExprIndex(folder.new_id(callee_id),
811 folder.fold_expr(el),
812 folder.fold_expr(er))
814 ExprPath(ref pth) => ExprPath(folder.fold_path(pth)),
815 ExprSelf => ExprSelf,
816 ExprLogLevel => ExprLogLevel,
817 ExprBreak(opt_ident) => ExprBreak(opt_ident),
818 ExprAgain(opt_ident) => ExprAgain(opt_ident),
820 ExprRet(e.map(|x| folder.fold_expr(x)))
822 ExprInlineAsm(ref a) => {
823 ExprInlineAsm(inline_asm {
824 inputs: a.inputs.map(|&(c, input)| (c, folder.fold_expr(input))),
825 outputs: a.outputs.map(|&(c, out)| (c, folder.fold_expr(out))),
829 ExprMac(ref mac) => ExprMac(folder.fold_mac(mac)),
830 ExprStruct(ref path, ref fields, maybe_expr) => {
831 ExprStruct(folder.fold_path(path),
832 fields.map(|x| fold_field(*x)),
833 maybe_expr.map(|x| folder.fold_expr(x)))
835 ExprParen(ex) => ExprParen(folder.fold_expr(ex))
839 id: folder.new_id(e.id),
841 span: folder.new_span(e.span),
845 pub fn noop_fold_stmt<T:ast_fold>(s: &Stmt, folder: &T) -> Option<@Stmt> {
846 let node = match s.node {
847 StmtDecl(d, nid) => {
848 match folder.fold_decl(d) {
849 Some(d) => Some(StmtDecl(d, folder.new_id(nid))),
853 StmtExpr(e, nid) => {
854 Some(StmtExpr(folder.fold_expr(e), folder.new_id(nid)))
856 StmtSemi(e, nid) => {
857 Some(StmtSemi(folder.fold_expr(e), folder.new_id(nid)))
859 StmtMac(ref mac, semi) => Some(StmtMac(folder.fold_mac(mac), semi))
862 node.map(|node| @Spanned {
864 span: folder.new_span(s.span),
871 use util::parser_testing::{string_to_crate, matches_codepattern};
876 // this version doesn't care about getting comments or docstrings in.
877 fn fake_print_crate(s: @pprust::ps, crate: &ast::Crate) {
878 pprust::print_mod(s, &crate.module, crate.attrs);
881 // change every identifier to "zz"
882 struct ToZzIdentFolder;
884 impl ast_fold for ToZzIdentFolder {
885 fn fold_ident(&self, _: ast::Ident) -> ast::Ident {
886 token::str_to_ident("zz")
890 // maybe add to expand.rs...
891 macro_rules! assert_pred (
892 ($pred:expr, $predname:expr, $a:expr , $b:expr) => (
894 let pred_val = $pred;
897 if !(pred_val(a_val,b_val)) {
898 fail!("expected args satisfying {}, got {:?} and {:?}",
899 $predname, a_val, b_val);
905 // make sure idents get transformed everywhere
906 #[test] fn ident_transformation () {
907 let zz_fold = ToZzIdentFolder;
908 let ast = string_to_crate(@"#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}");
909 assert_pred!(matches_codepattern,
910 "matches_codepattern",
911 pprust::to_str(&zz_fold.fold_crate(ast),fake_print_crate,
912 token::get_ident_interner()),
913 ~"#[a]mod zz{fn zz(zz:zz,zz:zz){zz!(zz,zz,zz);zz;zz}}");
916 // even inside macro defs....
917 #[test] fn ident_transformation_in_defs () {
918 let zz_fold = ToZzIdentFolder;
919 let ast = string_to_crate(@"macro_rules! a {(b $c:expr $(d $e:token)f+
920 => (g $(d $d $e)+))} ");
921 assert_pred!(matches_codepattern,
922 "matches_codepattern",
923 pprust::to_str(&zz_fold.fold_crate(ast),fake_print_crate,
924 token::get_ident_interner()),
925 ~"zz!zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)))");