]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/fold.rs
libsyntax/librustc: Allow calling variadic foreign functions.
[rust.git] / src / libsyntax / fold.rs
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.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use ast::*;
12 use ast;
13 use codemap::{respan, Span, Spanned};
14 use parse::token;
15 use opt_vec::OptVec;
16
17 // We may eventually want to be able to fold over type parameters, too.
18 pub trait ast_fold {
19     fn fold_crate(&self, c: Crate) -> Crate {
20         noop_fold_crate(c, self)
21     }
22
23     fn fold_meta_items(&self, meta_items: &[@MetaItem]) -> ~[@MetaItem] {
24         meta_items.map(|x| fold_meta_item_(*x, self))
25     }
26
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(),
32                                      self.fold_path(path),
33                                      self.new_id(node_id))
34                 }
35                 view_path_glob(ref path, node_id) => {
36                     view_path_glob(self.fold_path(path), self.new_id(node_id))
37                 }
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
42                                                                         .id);
43                                     Spanned {
44                                         node: path_list_ident_ {
45                                             name: path_list_ident.node
46                                                                  .name
47                                                                  .clone(),
48                                             id: id,
49                                         },
50                                         span: self.new_span(
51                                             path_list_ident.span)
52                                    }
53                                   }),
54                                   self.new_id(node_id))
55                 }
56             };
57             @Spanned {
58                 node: inner_view_path,
59                 span: self.new_span(view_path.span),
60             }
61         })
62     }
63
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,
67                                  string,
68                                  ref meta_items,
69                                  node_id) => {
70                 view_item_extern_mod(ident.clone(),
71                                      string,
72                                      self.fold_meta_items(*meta_items),
73                                      self.new_id(node_id))
74             }
75             view_item_use(ref view_paths) => {
76                 view_item_use(self.fold_view_paths(*view_paths))
77             }
78         };
79         view_item {
80             node: inner_view_item,
81             attrs: vi.attrs.map(|a| fold_attribute_(*a, self)),
82             vis: vi.vis,
83             span: self.new_span(vi.span),
84         }
85     }
86
87     fn fold_foreign_item(&self, ni: @foreign_item) -> @foreign_item {
88         let fold_attribute = |x| fold_attribute_(x, self);
89
90         @ast::foreign_item {
91             ident: self.fold_ident(ni.ident),
92             attrs: ni.attrs.map(|x| fold_attribute(*x)),
93             node:
94                 match ni.node {
95                     foreign_item_fn(ref fdec, ref generics) => {
96                         foreign_item_fn(
97                             ast::fn_decl {
98                                 inputs: fdec.inputs.map(|a| fold_arg_(a,
99                                                                       self)),
100                                 output: self.fold_ty(&fdec.output),
101                                 cf: fdec.cf,
102                                 variadic: fdec.variadic
103                             },
104                             fold_generics(generics, self))
105                     }
106                     foreign_item_static(ref t, m) => {
107                         foreign_item_static(self.fold_ty(t), m)
108                     }
109                 },
110             id: self.new_id(ni.id),
111             span: self.new_span(ni.span),
112             vis: ni.vis,
113         }
114     }
115
116     fn fold_item(&self, i: @item) -> Option<@item> {
117         noop_fold_item(i, self)
118     }
119
120     fn fold_struct_field(&self, sf: @struct_field) -> @struct_field {
121         let fold_attribute = |x| fold_attribute_(x, self);
122
123         @Spanned {
124             node: ast::struct_field_ {
125                 kind: sf.node.kind,
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))
129             },
130             span: self.new_span(sf.span)
131         }
132     }
133
134     fn fold_item_underscore(&self, i: &item_) -> item_ {
135         noop_fold_item_underscore(i, self)
136     }
137
138     fn fold_type_method(&self, m: &TypeMethod) -> TypeMethod {
139         noop_fold_type_method(m, self)
140     }
141
142     fn fold_method(&self, m: @method) -> @method {
143         @ast::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,
148             purity: m.purity,
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),
154             vis: m.vis,
155         }
156     }
157
158     fn fold_block(&self, b: &Block) -> Block {
159         noop_fold_block(b, self)
160     }
161
162     fn fold_stmt(&self, s: &Stmt) -> Option<@Stmt> {
163         noop_fold_stmt(s, self)
164     }
165
166     fn fold_arm(&self, a: &Arm) -> Arm {
167         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),
171         }
172     }
173
174     fn fold_pat(&self, p: @Pat) -> @Pat {
175         let node = match p.node {
176             PatWild => PatWild,
177             PatIdent(binding_mode, ref pth, ref sub) => {
178                 PatIdent(binding_mode,
179                          self.fold_path(pth),
180                          sub.map(|x| self.fold_pat(x)))
181             }
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))))
186             }
187             PatStruct(ref pth, ref fields, etc) => {
188                 let pth_ = self.fold_path(pth);
189                 let fs = do fields.map |f| {
190                     ast::FieldPat {
191                         ident: f.ident,
192                         pat: self.fold_pat(f.pat)
193                     }
194                 };
195                 PatStruct(pth_, fs, etc)
196             }
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))
203             },
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)))
208             }
209         };
210
211         @Pat {
212             id: self.new_id(p.id),
213             span: self.new_span(p.span),
214             node: node,
215         }
216     }
217
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))),
221             DeclItem(it) => {
222                 match self.fold_item(it) {
223                     Some(it_folded) => Some(DeclItem(it_folded)),
224                     None => None,
225                 }
226             }
227         };
228
229         node.map(|node| {
230             @Spanned {
231                 node: node,
232                 span: d.span,
233             }
234         })
235     }
236
237     fn fold_expr(&self, e: @Expr) -> @Expr {
238         noop_fold_expr(e, self)
239     }
240
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 {
251                     sigil: f.sigil,
252                     purity: f.purity,
253                     region: f.region,
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)),
258                 })
259             }
260             ty_bare_fn(ref f) => {
261                 ty_bare_fn(@TyBareFn {
262                     lifetimes: f.lifetimes.map(|l| fold_lifetime(l, self)),
263                     purity: f.purity,
264                     abis: f.abis,
265                     decl: fold_fn_decl(&f.decl, self)
266                 })
267             }
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),
272                         self.new_id(id))
273             }
274             ty_fixed_length_vec(ref mt, e) => {
275                 ty_fixed_length_vec(fold_mt(mt, self), self.fold_expr(e))
276             }
277             ty_mac(ref mac) => ty_mac(self.fold_mac(mac)),
278             ty_typeof(expr) => ty_typeof(self.fold_expr(expr)),
279         };
280         Ty {
281             id: self.new_id(t.id),
282             span: self.new_span(t.span),
283             node: node,
284         }
285     }
286
287     fn fold_mod(&self, m: &_mod) -> _mod {
288         noop_fold_mod(m, self)
289     }
290
291     fn fold_foreign_mod(&self, nm: &foreign_mod) -> foreign_mod {
292         ast::foreign_mod {
293             abis: nm.abis,
294             view_items: nm.view_items
295                           .iter()
296                           .map(|x| self.fold_view_item(x))
297                           .collect(),
298             items: nm.items
299                      .iter()
300                      .map(|x| self.fold_foreign_item(*x))
301                      .collect(),
302         }
303     }
304
305     fn fold_variant(&self, v: &variant) -> variant {
306         let kind;
307         match v.node.kind {
308             tuple_variant_kind(ref variant_args) => {
309                 kind = tuple_variant_kind(variant_args.map(|x|
310                     fold_variant_arg_(x, self)))
311             }
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))
317                 })
318             }
319         }
320
321         let fold_attribute = |x| fold_attribute_(x, self);
322         let attrs = v.node.attrs.map(|x| fold_attribute(*x));
323
324         let de = match v.node.disr_expr {
325           Some(e) => Some(self.fold_expr(e)),
326           None => None
327         };
328         let node = ast::variant_ {
329             name: v.node.name,
330             attrs: attrs,
331             kind: kind,
332             id: self.new_id(v.node.id),
333             disr_expr: de,
334             vis: v.node.vis,
335         };
336         Spanned {
337             node: node,
338             span: self.new_span(v.span),
339         }
340     }
341
342     fn fold_ident(&self, i: Ident) -> Ident {
343         i
344     }
345
346     fn fold_path(&self, p: &Path) -> Path {
347         ast::Path {
348             span: self.new_span(p.span),
349             global: p.global,
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)),
354             })
355         }
356     }
357
358     fn fold_local(&self, l: @Local) -> @Local {
359         @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),
365         }
366     }
367
368     fn fold_mac(&self, macro: &mac) -> mac {
369         Spanned {
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),
374                                  ctxt)
375                 }
376             },
377             span: self.new_span(macro.span)
378         }
379     }
380
381     fn map_exprs(&self, f: &fn(@Expr) -> @Expr, es: &[@Expr]) -> ~[@Expr] {
382         es.map(|x| f(*x))
383     }
384
385     fn new_id(&self, i: NodeId) -> NodeId {
386         i
387     }
388
389     fn new_span(&self, sp: Span) -> Span {
390         sp
391     }
392 }
393
394 /* some little folds that probably aren't useful to have in ast_fold itself*/
395
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 {
398     @Spanned {
399         node:
400             match mi.node {
401                 MetaWord(id) => MetaWord(id),
402                 MetaList(id, ref mis) => {
403                     let fold_meta_item = |x| fold_meta_item_(x, fld);
404                     MetaList(
405                         id,
406                         mis.map(|e| fold_meta_item(*e))
407                     )
408                 }
409                 MetaNameValue(id, s) => MetaNameValue(id, s)
410             },
411         span: fld.new_span(mi.span) }
412 }
413
414 //used in noop_fold_item and noop_fold_crate
415 fn fold_attribute_<T:ast_fold>(at: Attribute, fld: &T) -> Attribute {
416     Spanned {
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
422         }
423     }
424 }
425
426 //used in noop_fold_foreign_item and noop_fold_fn_decl
427 fn fold_arg_<T:ast_fold>(a: &arg, fld: &T) -> arg {
428     ast::arg {
429         ty: fld.fold_ty(&a.ty),
430         pat: fld.fold_pat(a.pat),
431         id: fld.new_id(a.id),
432     }
433 }
434
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] {
438     do tts.map |tt| {
439         match *tt {
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) =>
444             tt_seq(span,
445                    @mut fold_tts(**pattern, fld),
446                    sep.as_ref().map(|tok|maybe_fold_ident(tok,fld)),
447                    is_optional),
448             tt_nonterminal(sp,ref ident) =>
449             tt_nonterminal(sp,fld.fold_ident(*ident))
450         }
451     }
452 }
453
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 {
456     match *t {
457         token::IDENT(id, followed_by_colons) => {
458             token::IDENT(fld.fold_ident(id), followed_by_colons)
459         }
460         _ => (*t).clone()
461     }
462 }
463
464 pub fn fold_fn_decl<T:ast_fold>(decl: &ast::fn_decl, fld: &T)
465                                 -> ast::fn_decl {
466     ast::fn_decl {
467         inputs: decl.inputs.map(|x| fold_arg_(x, fld)), // bad copy
468         output: fld.fold_ty(&decl.output),
469         cf: decl.cf,
470         variadic: decl.variadic
471     }
472 }
473
474 fn fold_ty_param_bound<T:ast_fold>(tpb: &TyParamBound, fld: &T)
475                                    -> TyParamBound {
476     match *tpb {
477         TraitTyParamBound(ref ty) => TraitTyParamBound(fold_trait_ref(ty, fld)),
478         RegionTyParamBound => RegionTyParamBound
479     }
480 }
481
482 pub fn fold_ty_param<T:ast_fold>(tp: &TyParam, fld: &T) -> TyParam {
483     TyParam {
484         ident: tp.ident,
485         id: fld.new_id(tp.id),
486         bounds: tp.bounds.map(|x| fold_ty_param_bound(x, fld)),
487     }
488 }
489
490 pub fn fold_ty_params<T:ast_fold>(tps: &OptVec<TyParam>, fld: &T)
491                                   -> OptVec<TyParam> {
492     tps.map(|tp| fold_ty_param(tp, fld))
493 }
494
495 pub fn fold_lifetime<T:ast_fold>(l: &Lifetime, fld: &T) -> Lifetime {
496     Lifetime {
497         id: fld.new_id(l.id),
498         span: fld.new_span(l.span),
499         ident: l.ident
500     }
501 }
502
503 pub fn fold_lifetimes<T:ast_fold>(lts: &OptVec<Lifetime>, fld: &T)
504                                   -> OptVec<Lifetime> {
505     lts.map(|l| fold_lifetime(l, fld))
506 }
507
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)}
511 }
512
513 fn fold_struct_def<T:ast_fold>(struct_def: @ast::struct_def, fld: &T)
514                                -> @ast::struct_def {
515     @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)),
518     }
519 }
520
521 fn noop_fold_view_item(vi: &view_item_, fld: @ast_fold) -> view_item_ {
522     match *vi {
523         view_item_extern_mod(ident, name, ref meta_items, node_id) => {
524             view_item_extern_mod(ident,
525                                  name,
526                                  fld.fold_meta_items(*meta_items),
527                                  fld.new_id(node_id))
528         }
529         view_item_use(ref view_paths) => {
530             view_item_use(fld.fold_view_paths(*view_paths))
531         }
532     }
533 }
534
535 fn fold_trait_ref<T:ast_fold>(p: &trait_ref, fld: &T) -> trait_ref {
536     ast::trait_ref {
537         path: fld.fold_path(&p.path),
538         ref_id: fld.new_id(p.ref_id),
539     }
540 }
541
542 fn fold_struct_field<T:ast_fold>(f: @struct_field, fld: &T) -> @struct_field {
543     @Spanned {
544         node: ast::struct_field_ {
545             kind: f.node.kind,
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)),
549         },
550         span: fld.new_span(f.span),
551     }
552 }
553
554 fn fold_field_<T:ast_fold>(field: Field, folder: &T) -> Field {
555     ast::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),
559     }
560 }
561
562 fn fold_mt<T:ast_fold>(mt: &mt, folder: &T) -> mt {
563     mt {
564         ty: ~folder.fold_ty(mt.ty),
565         mutbl: mt.mutbl,
566     }
567 }
568
569 fn fold_field<T:ast_fold>(f: TypeField, folder: &T) -> TypeField {
570     ast::TypeField {
571         ident: folder.fold_ident(f.ident),
572         mt: fold_mt(&f.mt, folder),
573         span: folder.new_span(f.span),
574     }
575 }
576
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)
582         }
583     }
584 }
585
586 fn fold_variant_arg_<T:ast_fold>(va: &variant_arg, folder: &T)
587                                  -> variant_arg {
588     ast::variant_arg {
589         ty: folder.fold_ty(&va.ty),
590         id: folder.new_id(va.id)
591     }
592 }
593
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));
596     let mut stmts = ~[];
597     for stmt in b.stmts.iter() {
598         match folder.fold_stmt(*stmt) {
599             None => {}
600             Some(stmt) => stmts.push(stmt)
601         }
602     }
603     ast::Block {
604         view_items: view_items,
605         stmts: stmts,
606         expr: b.expr.map(|x| folder.fold_expr(x)),
607         id: folder.new_id(b.id),
608         rules: b.rules,
609         span: folder.new_span(b.span),
610     }
611 }
612
613 pub fn noop_fold_item_underscore<T:ast_fold>(i: &item_, folder: &T) -> item_ {
614     match *i {
615         item_static(ref t, m, e) => {
616             item_static(folder.fold_ty(t), m, folder.fold_expr(e))
617         }
618         item_fn(ref decl, purity, abi, ref generics, ref body) => {
619             item_fn(
620                 fold_fn_decl(decl, folder),
621                 purity,
622                 abi,
623                 fold_generics(generics, folder),
624                 folder.fold_block(body)
625             )
626         }
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))
630         }
631         item_ty(ref t, ref generics) => {
632             item_ty(folder.fold_ty(t),
633                     fold_generics(generics, folder))
634         }
635         item_enum(ref enum_definition, ref generics) => {
636             item_enum(
637                 ast::enum_def {
638                     variants: do enum_definition.variants.map |x| {
639                         folder.fold_variant(x)
640                     },
641                 },
642                 fold_generics(generics, folder))
643         }
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))
647         }
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)),
651                       folder.fold_ty(ty),
652                       methods.map(|x| folder.fold_method(*x))
653             )
654         }
655         item_trait(ref generics, ref traits, ref methods) => {
656             let methods = do methods.map |method| {
657                 match *method {
658                     required(ref m) => required(folder.fold_type_method(m)),
659                     provided(method) => provided(folder.fold_method(method))
660                 }
661             };
662             item_trait(fold_generics(generics, folder),
663                        traits.map(|p| fold_trait_ref(p, folder)),
664                        methods)
665         }
666         item_mac(ref m) => item_mac(folder.fold_mac(m)),
667     }
668 }
669
670 pub fn noop_fold_type_method<T:ast_fold>(m: &TypeMethod, fld: &T)
671                                          -> TypeMethod {
672     TypeMethod {
673         ident: fld.fold_ident(m.ident),
674         attrs: m.attrs.map(|a| fold_attribute_(*a, fld)),
675         purity: m.purity,
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),
681     }
682 }
683
684 pub fn noop_fold_mod<T:ast_fold>(m: &_mod, folder: &T) -> _mod {
685     ast::_mod {
686         view_items: m.view_items
687                      .iter()
688                      .map(|x| folder.fold_view_item(x)).collect(),
689         items: m.items.iter().filter_map(|x| folder.fold_item(*x)).collect(),
690     }
691 }
692
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);
696
697     Crate {
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),
702     }
703 }
704
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);
708
709     Some(@ast::item {
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),
714         vis: i.vis,
715         span: folder.new_span(i.span)
716     })
717 }
718
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);
721
722     let node = match e.node {
723         ExprVstore(e, v) => {
724             ExprVstore(folder.fold_expr(e), v)
725         }
726         ExprVec(ref exprs, mutt) => {
727             ExprVec(folder.map_exprs(|x| folder.fold_expr(x), *exprs), mutt)
728         }
729         ExprRepeat(expr, count, mutt) => {
730             ExprRepeat(folder.fold_expr(expr), folder.fold_expr(count), mutt)
731         }
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),
736                      blk)
737         }
738         ExprMethodCall(callee_id, f, i, ref tps, ref args, blk) => {
739             ExprMethodCall(
740                 folder.new_id(callee_id),
741                 folder.fold_expr(f),
742                 folder.fold_ident(i),
743                 tps.map(|x| folder.fold_ty(x)),
744                 folder.map_exprs(|x| folder.fold_expr(x), *args),
745                 blk
746             )
747         }
748         ExprBinary(callee_id, binop, lhs, rhs) => {
749             ExprBinary(folder.new_id(callee_id),
750                        binop,
751                        folder.fold_expr(lhs),
752                        folder.fold_expr(rhs))
753         }
754         ExprUnary(callee_id, binop, ohs) => {
755             ExprUnary(folder.new_id(callee_id), binop, folder.fold_expr(ohs))
756         }
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))
761         }
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)))
767         }
768         ExprWhile(cond, ref body) => {
769             ExprWhile(folder.fold_expr(cond), folder.fold_block(body))
770         }
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)))
776         }
777         ExprLoop(ref body, opt_ident) => {
778             ExprLoop(folder.fold_block(body),
779                      opt_ident.map(|x| folder.fold_ident(x)))
780         }
781         ExprMatch(expr, ref arms) => {
782             ExprMatch(folder.fold_expr(expr),
783                       arms.map(|x| folder.fold_arm(x)))
784         }
785         ExprFnBlock(ref decl, ref body) => {
786             ExprFnBlock(
787                 fold_fn_decl(decl, folder),
788                 folder.fold_block(body)
789             )
790         }
791         ExprProc(ref decl, ref body) => {
792             ExprProc(fold_fn_decl(decl, folder), folder.fold_block(body))
793         }
794         ExprBlock(ref blk) => ExprBlock(folder.fold_block(blk)),
795         ExprAssign(el, er) => {
796             ExprAssign(folder.fold_expr(el), folder.fold_expr(er))
797         }
798         ExprAssignOp(callee_id, op, el, er) => {
799             ExprAssignOp(folder.new_id(callee_id),
800                          op,
801                          folder.fold_expr(el),
802                          folder.fold_expr(er))
803         }
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)))
808         }
809         ExprIndex(callee_id, el, er) => {
810             ExprIndex(folder.new_id(callee_id),
811                       folder.fold_expr(el),
812                       folder.fold_expr(er))
813         }
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),
819         ExprRet(ref e) => {
820             ExprRet(e.map(|x| folder.fold_expr(x)))
821         }
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))),
826                 .. (*a).clone()
827             })
828         }
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)))
834         },
835         ExprParen(ex) => ExprParen(folder.fold_expr(ex))
836     };
837
838     @Expr {
839         id: folder.new_id(e.id),
840         node: node,
841         span: folder.new_span(e.span),
842     }
843 }
844
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))),
850                 None => None,
851             }
852         }
853         StmtExpr(e, nid) => {
854             Some(StmtExpr(folder.fold_expr(e), folder.new_id(nid)))
855         }
856         StmtSemi(e, nid) => {
857             Some(StmtSemi(folder.fold_expr(e), folder.new_id(nid)))
858         }
859         StmtMac(ref mac, semi) => Some(StmtMac(folder.fold_mac(mac), semi))
860     };
861
862     node.map(|node| @Spanned {
863         node: node,
864         span: folder.new_span(s.span),
865     })
866 }
867
868 #[cfg(test)]
869 mod test {
870     use ast;
871     use util::parser_testing::{string_to_crate, matches_codepattern};
872     use parse::token;
873     use print::pprust;
874     use super::*;
875
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);
879     }
880
881     // change every identifier to "zz"
882     struct ToZzIdentFolder;
883
884     impl ast_fold for ToZzIdentFolder {
885         fn fold_ident(&self, _: ast::Ident) -> ast::Ident {
886             token::str_to_ident("zz")
887         }
888     }
889
890     // maybe add to expand.rs...
891     macro_rules! assert_pred (
892         ($pred:expr, $predname:expr, $a:expr , $b:expr) => (
893             {
894                 let pred_val = $pred;
895                 let a_val = $a;
896                 let b_val = $b;
897                 if !(pred_val(a_val,b_val)) {
898                     fail!("expected args satisfying {}, got {:?} and {:?}",
899                           $predname, a_val, b_val);
900                 }
901             }
902         )
903     )
904
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}}");
914     }
915
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)+)))");
926     }
927 }
928