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::{span, spanned};
18 fn fold_crate(@self, &Crate) -> Crate;
19 fn fold_view_item(@self, &view_item) -> view_item;
20 fn fold_foreign_item(@self, @foreign_item) -> @foreign_item;
21 fn fold_item(@self, @item) -> Option<@item>;
22 fn fold_struct_field(@self, @struct_field) -> @struct_field;
23 fn fold_item_underscore(@self, &item_) -> item_;
24 fn fold_method(@self, @method) -> @method;
25 fn fold_block(@self, &Block) -> Block;
26 fn fold_stmt(@self, &stmt) -> Option<@stmt>;
27 fn fold_arm(@self, &arm) -> arm;
28 fn fold_pat(@self, @pat) -> @pat;
29 fn fold_decl(@self, @decl) -> Option<@decl>;
30 fn fold_expr(@self, @expr) -> @expr;
31 fn fold_ty(@self, &Ty) -> Ty;
32 fn fold_mod(@self, &_mod) -> _mod;
33 fn fold_foreign_mod(@self, &foreign_mod) -> foreign_mod;
34 fn fold_variant(@self, &variant) -> variant;
35 fn fold_ident(@self, ident) -> ident;
36 fn fold_path(@self, &Path) -> Path;
37 fn fold_local(@self, @Local) -> @Local;
38 fn map_exprs(@self, @fn(@expr) -> @expr, &[@expr]) -> ~[@expr];
39 fn new_id(@self, NodeId) -> NodeId;
40 fn new_span(@self, span) -> span;
43 // We may eventually want to be able to fold over type parameters, too
45 pub struct AstFoldFns {
46 //unlike the others, item_ is non-trivial
47 fold_crate: @fn(&Crate, @ast_fold) -> Crate,
48 fold_view_item: @fn(&view_item_, @ast_fold) -> view_item_,
49 fold_foreign_item: @fn(@foreign_item, @ast_fold) -> @foreign_item,
50 fold_item: @fn(@item, @ast_fold) -> Option<@item>,
51 fold_struct_field: @fn(@struct_field, @ast_fold) -> @struct_field,
52 fold_item_underscore: @fn(&item_, @ast_fold) -> item_,
53 fold_method: @fn(@method, @ast_fold) -> @method,
54 fold_block: @fn(&Block, @ast_fold) -> Block,
55 fold_stmt: @fn(&stmt_, span, @ast_fold) -> (Option<stmt_>, span),
56 fold_arm: @fn(&arm, @ast_fold) -> arm,
57 fold_pat: @fn(&pat_, span, @ast_fold) -> (pat_, span),
58 fold_decl: @fn(&decl_, span, @ast_fold) -> (Option<decl_>, span),
59 fold_expr: @fn(&expr_, span, @ast_fold) -> (expr_, span),
60 fold_ty: @fn(&ty_, span, @ast_fold) -> (ty_, span),
61 fold_mod: @fn(&_mod, @ast_fold) -> _mod,
62 fold_foreign_mod: @fn(&foreign_mod, @ast_fold) -> foreign_mod,
63 fold_variant: @fn(&variant_, span, @ast_fold) -> (variant_, span),
64 fold_ident: @fn(ident, @ast_fold) -> ident,
65 fold_path: @fn(&Path, @ast_fold) -> Path,
66 fold_local: @fn(@Local, @ast_fold) -> @Local,
67 map_exprs: @fn(@fn(@expr) -> @expr, &[@expr]) -> ~[@expr],
68 new_id: @fn(NodeId) -> NodeId,
69 new_span: @fn(span) -> span
72 pub type ast_fold_fns = @AstFoldFns;
74 /* some little folds that probably aren't useful to have in ast_fold itself*/
76 //used in noop_fold_item and noop_fold_crate and noop_fold_crate_directive
77 fn fold_meta_item_(mi: @MetaItem, fld: @ast_fold) -> @MetaItem {
81 MetaWord(id) => MetaWord(id),
82 MetaList(id, ref mis) => {
83 let fold_meta_item = |x| fold_meta_item_(x, fld);
86 mis.map(|e| fold_meta_item(*e))
89 MetaNameValue(id, s) => MetaNameValue(id, s)
91 span: fld.new_span(mi.span) }
93 //used in noop_fold_item and noop_fold_crate
94 fn fold_attribute_(at: Attribute, fld: @ast_fold) -> Attribute {
96 span: fld.new_span(at.span),
97 node: ast::Attribute_ {
99 value: fold_meta_item_(at.node.value, fld),
100 is_sugared_doc: at.node.is_sugared_doc
105 //used in noop_fold_foreign_item and noop_fold_fn_decl
106 fn fold_arg_(a: arg, fld: @ast_fold) -> arg {
108 is_mutbl: a.is_mutbl,
109 ty: fld.fold_ty(&a.ty),
110 pat: fld.fold_pat(a.pat),
111 id: fld.new_id(a.id),
115 //used in noop_fold_expr, and possibly elsewhere in the future
116 fn fold_mac_(m: &mac, fld: @ast_fold) -> mac {
119 mac_invoc_tt(ref p,ref tts) =>
120 mac_invoc_tt(fld.fold_path(p),
123 span: fld.new_span(m.span)
127 fn fold_tts(tts : &[token_tree], fld: @ast_fold) -> ~[token_tree] {
130 tt_tok(span, ref tok) =>
131 tt_tok(span,maybe_fold_ident(tok,fld)),
133 tt_delim(@mut fold_tts(**tts, fld)),
134 tt_seq(span, ref pattern, ref sep, is_optional) =>
136 @mut fold_tts(**pattern, fld),
137 sep.map(|tok|maybe_fold_ident(tok,fld)),
139 tt_nonterminal(sp,ref ident) =>
140 tt_nonterminal(sp,fld.fold_ident(*ident))
145 // apply ident folder if it's an ident, otherwise leave it alone
146 fn maybe_fold_ident(t: &token::Token, fld: @ast_fold) -> token::Token {
148 token::IDENT(id,followed_by_colons) =>
149 token::IDENT(fld.fold_ident(id),followed_by_colons),
154 pub fn fold_fn_decl(decl: &ast::fn_decl, fld: @ast_fold) -> ast::fn_decl {
156 inputs: decl.inputs.map(|x| fold_arg_(/*bad*/ (*x).clone(), fld)),
157 output: fld.fold_ty(&decl.output),
162 fn fold_ty_param_bound(tpb: &TyParamBound, fld: @ast_fold) -> TyParamBound {
164 TraitTyParamBound(ref ty) => TraitTyParamBound(fold_trait_ref(ty, fld)),
165 RegionTyParamBound => RegionTyParamBound
169 pub fn fold_ty_param(tp: TyParam,
170 fld: @ast_fold) -> TyParam {
171 TyParam {ident: tp.ident,
172 id: fld.new_id(tp.id),
173 bounds: tp.bounds.map(|x| fold_ty_param_bound(x, fld))}
176 pub fn fold_ty_params(tps: &OptVec<TyParam>,
177 fld: @ast_fold) -> OptVec<TyParam> {
178 let tps = /*bad*/ (*tps).clone();
179 tps.map_move(|tp| fold_ty_param(tp, fld))
182 pub fn fold_lifetime(l: &Lifetime,
183 fld: @ast_fold) -> Lifetime {
184 Lifetime {id: fld.new_id(l.id),
185 span: fld.new_span(l.span),
189 pub fn fold_lifetimes(lts: &OptVec<Lifetime>,
190 fld: @ast_fold) -> OptVec<Lifetime> {
191 lts.map(|l| fold_lifetime(l, fld))
194 pub fn fold_generics(generics: &Generics, fld: @ast_fold) -> Generics {
195 Generics {ty_params: fold_ty_params(&generics.ty_params, fld),
196 lifetimes: fold_lifetimes(&generics.lifetimes, fld)}
199 pub fn noop_fold_crate(c: &Crate, fld: @ast_fold) -> Crate {
200 let fold_meta_item = |x| fold_meta_item_(x, fld);
201 let fold_attribute = |x| fold_attribute_(x, fld);
204 module: fld.fold_mod(&c.module),
205 attrs: c.attrs.map(|x| fold_attribute(*x)),
206 config: c.config.map(|x| fold_meta_item(*x)),
207 span: fld.new_span(c.span),
211 fn noop_fold_view_item(vi: &view_item_, _fld: @ast_fold) -> view_item_ {
212 return /* FIXME (#2543) */ (*vi).clone();
216 fn noop_fold_foreign_item(ni: @foreign_item, fld: @ast_fold)
218 let fold_arg = |x| fold_arg_(x, fld);
219 let fold_attribute = |x| fold_attribute_(x, fld);
222 ident: fld.fold_ident(ni.ident),
223 attrs: ni.attrs.map(|x| fold_attribute(*x)),
226 foreign_item_fn(ref fdec, ref generics) => {
229 inputs: fdec.inputs.map(|a|
230 fold_arg(/*bad*/(*a).clone())),
231 output: fld.fold_ty(&fdec.output),
234 fold_generics(generics, fld))
236 foreign_item_static(ref t, m) => {
237 foreign_item_static(fld.fold_ty(t), m)
240 id: fld.new_id(ni.id),
241 span: fld.new_span(ni.span),
246 pub fn noop_fold_item(i: @item, fld: @ast_fold) -> Option<@item> {
247 let fold_attribute = |x| fold_attribute_(x, fld);
249 Some(@ast::item { ident: fld.fold_ident(i.ident),
250 attrs: i.attrs.map(|e| fold_attribute(*e)),
251 id: fld.new_id(i.id),
252 node: fld.fold_item_underscore(&i.node),
254 span: fld.new_span(i.span) })
257 fn noop_fold_struct_field(sf: @struct_field, fld: @ast_fold)
259 let fold_attribute = |x| fold_attribute_(x, fld);
262 node: ast::struct_field_ {
265 ty: fld.fold_ty(&sf.node.ty),
266 attrs: sf.node.attrs.map(|e| fold_attribute(*e))
272 pub fn noop_fold_item_underscore(i: &item_, fld: @ast_fold) -> item_ {
274 item_static(ref t, m, e) => item_static(fld.fold_ty(t), m, fld.fold_expr(e)),
275 item_fn(ref decl, purity, abi, ref generics, ref body) => {
277 fold_fn_decl(decl, fld),
280 fold_generics(generics, fld),
284 item_mod(ref m) => item_mod(fld.fold_mod(m)),
285 item_foreign_mod(ref nm) => {
286 item_foreign_mod(fld.fold_foreign_mod(nm))
288 item_ty(ref t, ref generics) => {
289 item_ty(fld.fold_ty(t), fold_generics(generics, fld))
291 item_enum(ref enum_definition, ref generics) => {
294 variants: do enum_definition.variants.map |x| {
298 fold_generics(generics, fld))
300 item_struct(ref struct_def, ref generics) => {
301 let struct_def = fold_struct_def(*struct_def, fld);
302 item_struct(struct_def, /* FIXME (#2543) */ (*generics).clone())
304 item_impl(ref generics, ref ifce, ref ty, ref methods) => {
306 fold_generics(generics, fld),
307 ifce.map(|p| fold_trait_ref(p, fld)),
309 methods.map(|x| fld.fold_method(*x))
312 item_trait(ref generics, ref traits, ref methods) => {
313 let methods = do methods.map |method| {
315 required(*) => (*method).clone(),
316 provided(method) => provided(fld.fold_method(method))
320 fold_generics(generics, fld),
321 traits.map(|p| fold_trait_ref(p, fld)),
326 // FIXME #2888: we might actually want to do something here.
327 // ... okay, we're doing something. It would probably be nicer
328 // to add something to the ast_fold trait, but I'll defer
330 item_mac(fold_mac_(m,fld))
335 fn fold_struct_def(struct_def: @ast::struct_def, fld: @ast_fold)
336 -> @ast::struct_def {
338 fields: struct_def.fields.map(|f| fold_struct_field(*f, fld)),
339 ctor_id: struct_def.ctor_id.map(|cid| fld.new_id(*cid)),
343 fn fold_trait_ref(p: &trait_ref, fld: @ast_fold) -> trait_ref {
345 path: fld.fold_path(&p.path),
346 ref_id: fld.new_id(p.ref_id),
350 fn fold_struct_field(f: @struct_field, fld: @ast_fold) -> @struct_field {
352 node: ast::struct_field_ {
354 id: fld.new_id(f.node.id),
355 ty: fld.fold_ty(&f.node.ty),
356 attrs: /* FIXME (#2543) */ f.node.attrs.clone(),
358 span: fld.new_span(f.span),
362 fn noop_fold_method(m: @method, fld: @ast_fold) -> @method {
364 ident: fld.fold_ident(m.ident),
365 attrs: /* FIXME (#2543) */ m.attrs.clone(),
366 generics: fold_generics(&m.generics, fld),
367 explicit_self: m.explicit_self,
369 decl: fold_fn_decl(&m.decl, fld),
370 body: fld.fold_block(&m.body),
371 id: fld.new_id(m.id),
372 span: fld.new_span(m.span),
373 self_id: fld.new_id(m.self_id),
379 pub fn noop_fold_block(b: &Block, fld: @ast_fold) -> Block {
380 let view_items = b.view_items.map(|x| fld.fold_view_item(x));
382 for stmt in b.stmts.iter() {
383 match fld.fold_stmt(*stmt) {
385 Some(stmt) => stmts.push(stmt)
389 view_items: view_items,
391 expr: b.expr.map(|x| fld.fold_expr(*x)),
392 id: fld.new_id(b.id),
398 fn noop_fold_stmt(s: &stmt_, fld: @ast_fold) -> Option<stmt_> {
399 let fold_mac = |x| fold_mac_(x, fld);
401 stmt_decl(d, nid) => {
402 match fld.fold_decl(d) {
403 Some(d) => Some(stmt_decl(d, fld.new_id(nid))),
407 stmt_expr(e, nid) => {
408 Some(stmt_expr(fld.fold_expr(e), fld.new_id(nid)))
410 stmt_semi(e, nid) => {
411 Some(stmt_semi(fld.fold_expr(e), fld.new_id(nid)))
413 stmt_mac(ref mac, semi) => Some(stmt_mac(fold_mac(mac), semi))
417 fn noop_fold_arm(a: &arm, fld: @ast_fold) -> arm {
419 pats: a.pats.map(|x| fld.fold_pat(*x)),
420 guard: a.guard.map_move(|x| fld.fold_expr(x)),
421 body: fld.fold_block(&a.body),
425 pub fn noop_fold_pat(p: &pat_, fld: @ast_fold) -> pat_ {
427 pat_wild => pat_wild,
428 pat_ident(binding_mode, ref pth, ref sub) => {
432 sub.map_move(|x| fld.fold_pat(x))
435 pat_lit(e) => pat_lit(fld.fold_expr(e)),
436 pat_enum(ref pth, ref pats) => {
439 pats.map(|pats| pats.map(|x| fld.fold_pat(*x)))
442 pat_struct(ref pth, ref fields, etc) => {
443 let pth_ = fld.fold_path(pth);
444 let fs = do fields.map |f| {
447 pat: fld.fold_pat(f.pat)
450 pat_struct(pth_, fs, etc)
452 pat_tup(ref elts) => pat_tup(elts.map(|x| fld.fold_pat(*x))),
453 pat_box(inner) => pat_box(fld.fold_pat(inner)),
454 pat_uniq(inner) => pat_uniq(fld.fold_pat(inner)),
455 pat_region(inner) => pat_region(fld.fold_pat(inner)),
456 pat_range(e1, e2) => {
457 pat_range(fld.fold_expr(e1), fld.fold_expr(e2))
459 pat_vec(ref before, ref slice, ref after) => {
461 before.map(|x| fld.fold_pat(*x)),
462 slice.map_move(|x| fld.fold_pat(x)),
463 after.map(|x| fld.fold_pat(*x))
469 fn noop_fold_decl(d: &decl_, fld: @ast_fold) -> Option<decl_> {
471 decl_local(ref l) => Some(decl_local(fld.fold_local(*l))),
473 match fld.fold_item(it) {
474 Some(it_folded) => Some(decl_item(it_folded)),
481 pub fn wrap<T>(f: @fn(&T, @ast_fold) -> T)
482 -> @fn(&T, span, @ast_fold) -> (T, span) {
483 let result: @fn(&T, span, @ast_fold) -> (T, span) = |x, s, fld| {
489 pub fn noop_fold_expr(e: &expr_, fld: @ast_fold) -> expr_ {
490 fn fold_field_(field: Field, fld: @ast_fold) -> Field {
492 ident: fld.fold_ident(field.ident),
493 expr: fld.fold_expr(field.expr),
494 span: fld.new_span(field.span),
497 let fold_field = |x| fold_field_(x, fld);
499 let fold_mac = |x| fold_mac_(x, fld);
502 expr_vstore(e, v) => {
503 expr_vstore(fld.fold_expr(e), v)
505 expr_vec(ref exprs, mutt) => {
506 expr_vec(fld.map_exprs(|x| fld.fold_expr(x), *exprs), mutt)
508 expr_repeat(expr, count, mutt) => {
509 expr_repeat(fld.fold_expr(expr), fld.fold_expr(count), mutt)
511 expr_tup(ref elts) => expr_tup(elts.map(|x| fld.fold_expr(*x))),
512 expr_call(f, ref args, blk) => {
515 fld.map_exprs(|x| fld.fold_expr(x), *args),
519 expr_method_call(callee_id, f, i, ref tps, ref args, blk) => {
521 fld.new_id(callee_id),
524 tps.map(|x| fld.fold_ty(x)),
525 fld.map_exprs(|x| fld.fold_expr(x), *args),
529 expr_binary(callee_id, binop, lhs, rhs) => {
531 fld.new_id(callee_id),
537 expr_unary(callee_id, binop, ohs) => {
539 fld.new_id(callee_id),
544 expr_do_body(f) => expr_do_body(fld.fold_expr(f)),
545 expr_lit(_) => (*e).clone(),
546 expr_cast(expr, ref ty) => {
547 expr_cast(fld.fold_expr(expr), (*ty).clone())
549 expr_addr_of(m, ohs) => expr_addr_of(m, fld.fold_expr(ohs)),
550 expr_if(cond, ref tr, fl) => {
554 fl.map_move(|x| fld.fold_expr(x))
557 expr_while(cond, ref body) => {
558 expr_while(fld.fold_expr(cond), fld.fold_block(body))
560 expr_for_loop(pat, iter, ref body) => {
561 expr_for_loop(fld.fold_pat(pat),
563 fld.fold_block(body))
565 expr_loop(ref body, opt_ident) => {
567 fld.fold_block(body),
568 opt_ident.map_move(|x| fld.fold_ident(x))
571 expr_match(expr, ref arms) => {
574 arms.map(|x| fld.fold_arm(x))
577 expr_fn_block(ref decl, ref body) => {
579 fold_fn_decl(decl, fld),
583 expr_block(ref blk) => expr_block(fld.fold_block(blk)),
584 expr_assign(el, er) => {
585 expr_assign(fld.fold_expr(el), fld.fold_expr(er))
587 expr_assign_op(callee_id, op, el, er) => {
589 fld.new_id(callee_id),
595 expr_field(el, id, ref tys) => {
597 fld.fold_expr(el), fld.fold_ident(id),
598 tys.map(|x| fld.fold_ty(x))
601 expr_index(callee_id, el, er) => {
603 fld.new_id(callee_id),
608 expr_path(ref pth) => expr_path(fld.fold_path(pth)),
609 expr_self => expr_self,
610 expr_break(ref opt_ident) => {
611 expr_break(opt_ident.map_move(|x| fld.fold_ident(x)))
613 expr_again(ref opt_ident) => {
614 expr_again(opt_ident.map_move(|x| fld.fold_ident(x)))
617 expr_ret(e.map_move(|x| fld.fold_expr(x)))
625 expr_inline_asm(ref a) => {
626 expr_inline_asm(inline_asm {
627 inputs: a.inputs.map(|&(c, input)| (c, fld.fold_expr(input))),
628 outputs: a.outputs.map(|&(c, out)| (c, fld.fold_expr(out))),
632 expr_mac(ref mac) => expr_mac(fold_mac(mac)),
633 expr_struct(ref path, ref fields, maybe_expr) => {
636 fields.map(|x| fold_field(*x)),
637 maybe_expr.map_move(|x| fld.fold_expr(x))
640 expr_paren(ex) => expr_paren(fld.fold_expr(ex))
644 pub fn noop_fold_ty(t: &ty_, fld: @ast_fold) -> ty_ {
645 let fold_mac = |x| fold_mac_(x, fld);
646 fn fold_mt(mt: &mt, fld: @ast_fold) -> mt {
648 ty: ~fld.fold_ty(mt.ty),
652 fn fold_field(f: TypeField, fld: @ast_fold) -> TypeField {
654 ident: fld.fold_ident(f.ident),
655 mt: fold_mt(&f.mt, fld),
656 span: fld.new_span(f.span),
659 fn fold_opt_bounds(b: &Option<OptVec<TyParamBound>>, fld: @ast_fold)
660 -> Option<OptVec<TyParamBound>> {
662 do bounds.map |bound| { fold_ty_param_bound(bound, fld) }
666 ty_nil | ty_bot | ty_infer => (*t).clone(),
667 ty_box(ref mt) => ty_box(fold_mt(mt, fld)),
668 ty_uniq(ref mt) => ty_uniq(fold_mt(mt, fld)),
669 ty_vec(ref mt) => ty_vec(fold_mt(mt, fld)),
670 ty_ptr(ref mt) => ty_ptr(fold_mt(mt, fld)),
671 ty_rptr(region, ref mt) => ty_rptr(region, fold_mt(mt, fld)),
672 ty_closure(ref f) => {
673 ty_closure(@TyClosure {
677 onceness: f.onceness,
678 bounds: fold_opt_bounds(&f.bounds, fld),
679 decl: fold_fn_decl(&f.decl, fld),
680 lifetimes: f.lifetimes.clone(),
683 ty_bare_fn(ref f) => {
684 ty_bare_fn(@TyBareFn {
685 lifetimes: f.lifetimes.clone(),
688 decl: fold_fn_decl(&f.decl, fld)
691 ty_tup(ref tys) => ty_tup(tys.map(|ty| fld.fold_ty(ty))),
692 ty_path(ref path, ref bounds, id) =>
693 ty_path(fld.fold_path(path), fold_opt_bounds(bounds, fld), fld.new_id(id)),
694 ty_fixed_length_vec(ref mt, e) => {
700 ty_typeof(e) => ty_typeof(fld.fold_expr(e)),
701 ty_mac(ref mac) => ty_mac(fold_mac(mac))
706 pub fn noop_fold_mod(m: &_mod, fld: @ast_fold) -> _mod {
708 view_items: m.view_items.iter().map(|x| fld.fold_view_item(x)).collect(),
709 items: m.items.iter().filter_map(|x| fld.fold_item(*x)).collect(),
713 fn noop_fold_foreign_mod(nm: &foreign_mod, fld: @ast_fold) -> foreign_mod {
717 view_items: nm.view_items.iter().map(|x| fld.fold_view_item(x)).collect(),
718 items: nm.items.iter().map(|x| fld.fold_foreign_item(*x)).collect(),
722 fn noop_fold_variant(v: &variant_, fld: @ast_fold) -> variant_ {
723 fn fold_variant_arg_(va: variant_arg, fld: @ast_fold) -> variant_arg {
724 ast::variant_arg { ty: fld.fold_ty(&va.ty), id: fld.new_id(va.id) }
726 let fold_variant_arg = |x| fold_variant_arg_(x, fld);
730 tuple_variant_kind(ref variant_args) => {
731 kind = tuple_variant_kind(do variant_args.map |x| {
732 fold_variant_arg(/*bad*/ (*x).clone())
735 struct_variant_kind(ref struct_def) => {
736 kind = struct_variant_kind(@ast::struct_def {
737 fields: struct_def.fields.iter()
738 .map(|f| fld.fold_struct_field(*f)).collect(),
739 ctor_id: struct_def.ctor_id.map(|c| fld.new_id(*c))
744 let fold_attribute = |x| fold_attribute_(x, fld);
745 let attrs = v.attrs.map(|x| fold_attribute(*x));
747 let de = match v.disr_expr {
748 Some(e) => Some(fld.fold_expr(e)),
755 id: fld.new_id(v.id),
761 fn noop_fold_ident(i: ident, _fld: @ast_fold) -> ident {
765 fn noop_fold_path(p: &Path, fld: @ast_fold) -> Path {
767 span: fld.new_span(p.span),
769 segments: p.segments.map(|segment| ast::PathSegment {
770 identifier: fld.fold_ident(segment.identifier),
771 lifetime: segment.lifetime,
772 types: segment.types.map(|typ| fld.fold_ty(typ)),
777 fn noop_fold_local(l: @Local, fld: @ast_fold) -> @Local {
779 is_mutbl: l.is_mutbl,
780 ty: fld.fold_ty(&l.ty),
781 pat: fld.fold_pat(l.pat),
782 init: l.init.map_move(|e| fld.fold_expr(e)),
783 id: fld.new_id(l.id),
784 span: fld.new_span(l.span),
788 /* temporarily eta-expand because of a compiler bug with using `fn<T>` as a
790 fn noop_map_exprs(f: @fn(@expr) -> @expr, es: &[@expr]) -> ~[@expr] {
794 fn noop_id(i: NodeId) -> NodeId { return i; }
796 fn noop_span(sp: span) -> span { return sp; }
798 pub fn default_ast_fold() -> ast_fold_fns {
800 fold_crate: noop_fold_crate,
801 fold_view_item: noop_fold_view_item,
802 fold_foreign_item: noop_fold_foreign_item,
803 fold_item: noop_fold_item,
804 fold_struct_field: noop_fold_struct_field,
805 fold_item_underscore: noop_fold_item_underscore,
806 fold_method: noop_fold_method,
807 fold_block: noop_fold_block,
808 fold_stmt: |x, s, fld| (noop_fold_stmt(x, fld), s),
809 fold_arm: noop_fold_arm,
810 fold_pat: wrap(noop_fold_pat),
811 fold_decl: |x, s, fld| (noop_fold_decl(x, fld), s),
812 fold_expr: wrap(noop_fold_expr),
813 fold_ty: wrap(noop_fold_ty),
814 fold_mod: noop_fold_mod,
815 fold_foreign_mod: noop_fold_foreign_mod,
816 fold_variant: wrap(noop_fold_variant),
817 fold_ident: noop_fold_ident,
818 fold_path: noop_fold_path,
819 fold_local: noop_fold_local,
820 map_exprs: noop_map_exprs,
826 impl ast_fold for AstFoldFns {
827 /* naturally, a macro to write these would be nice */
828 fn fold_crate(@self, c: &Crate) -> Crate {
829 (self.fold_crate)(c, self as @ast_fold)
831 fn fold_view_item(@self, x: &view_item) -> view_item {
833 node: (self.fold_view_item)(&x.node, self as @ast_fold),
834 attrs: x.attrs.iter().map(|a| fold_attribute_(*a, self as @ast_fold)).collect(),
836 span: (self.new_span)(x.span),
839 fn fold_foreign_item(@self, x: @foreign_item) -> @foreign_item {
840 (self.fold_foreign_item)(x, self as @ast_fold)
842 fn fold_item(@self, i: @item) -> Option<@item> {
843 (self.fold_item)(i, self as @ast_fold)
845 fn fold_struct_field(@self, sf: @struct_field) -> @struct_field {
847 node: ast::struct_field_ {
850 ty: self.fold_ty(&sf.node.ty),
851 attrs: sf.node.attrs.clone(),
853 span: (self.new_span)(sf.span),
856 fn fold_item_underscore(@self, i: &item_) -> item_ {
857 (self.fold_item_underscore)(i, self as @ast_fold)
859 fn fold_method(@self, x: @method) -> @method {
860 (self.fold_method)(x, self as @ast_fold)
862 fn fold_block(@self, x: &Block) -> Block {
863 (self.fold_block)(x, self as @ast_fold)
865 fn fold_stmt(@self, x: &stmt) -> Option<@stmt> {
866 let (n_opt, s) = (self.fold_stmt)(&x.node, x.span, self as @ast_fold);
868 Some(n) => Some(@spanned { node: n, span: (self.new_span)(s) }),
872 fn fold_arm(@self, x: &arm) -> arm {
873 (self.fold_arm)(x, self as @ast_fold)
875 fn fold_pat(@self, x: @pat) -> @pat {
876 let (n, s) = (self.fold_pat)(&x.node, x.span, self as @ast_fold);
878 id: (self.new_id)(x.id),
880 span: (self.new_span)(s),
883 fn fold_decl(@self, x: @decl) -> Option<@decl> {
884 let (n_opt, s) = (self.fold_decl)(&x.node, x.span, self as @ast_fold);
886 Some(n) => Some(@spanned { node: n, span: (self.new_span)(s) }),
890 fn fold_expr(@self, x: @expr) -> @expr {
891 let (n, s) = (self.fold_expr)(&x.node, x.span, self as @ast_fold);
893 id: (self.new_id)(x.id),
895 span: (self.new_span)(s),
898 fn fold_ty(@self, x: &Ty) -> Ty {
899 let (n, s) = (self.fold_ty)(&x.node, x.span, self as @ast_fold);
901 id: (self.new_id)(x.id),
903 span: (self.new_span)(s),
906 fn fold_mod(@self, x: &_mod) -> _mod {
907 (self.fold_mod)(x, self as @ast_fold)
909 fn fold_foreign_mod(@self, x: &foreign_mod) -> foreign_mod {
910 (self.fold_foreign_mod)(x, self as @ast_fold)
912 fn fold_variant(@self, x: &variant) -> variant {
913 let (n, s) = (self.fold_variant)(&x.node, x.span, self as @ast_fold);
914 spanned { node: n, span: (self.new_span)(s) }
916 fn fold_ident(@self, x: ident) -> ident {
917 (self.fold_ident)(x, self as @ast_fold)
919 fn fold_path(@self, x: &Path) -> Path {
920 (self.fold_path)(x, self as @ast_fold)
922 fn fold_local(@self, x: @Local) -> @Local {
923 (self.fold_local)(x, self as @ast_fold)
926 f: @fn(@expr) -> @expr,
929 (self.map_exprs)(f, e)
931 fn new_id(@self, node_id: ast::NodeId) -> NodeId {
932 (self.new_id)(node_id)
934 fn new_span(@self, span: span) -> span {
935 (self.new_span)(span)
939 pub trait AstFoldExtensions {
940 fn fold_attributes(&self, attrs: ~[Attribute]) -> ~[Attribute];
943 impl AstFoldExtensions for @ast_fold {
944 fn fold_attributes(&self, attrs: ~[Attribute]) -> ~[Attribute] {
945 attrs.map(|x| fold_attribute_(*x, *self))
949 pub fn make_fold(afp: ast_fold_fns) -> @ast_fold {
956 use util::parser_testing::{string_to_crate, matches_codepattern};
962 // given a function from idents to idents, produce
963 // an ast_fold that applies that function:
964 pub fn fun_to_ident_folder(f: @fn(ast::ident)->ast::ident) -> @ast_fold{
965 let afp = default_ast_fold();
966 let f_pre = @AstFoldFns{
967 fold_ident : |id, _| f(id),
973 // this version doesn't care about getting comments or docstrings in.
974 fn fake_print_crate(s: @pprust::ps, crate: &ast::Crate) {
975 pprust::print_mod(s, &crate.module, crate.attrs);
978 // change every identifier to "zz"
979 pub fn to_zz() -> @fn(ast::ident)->ast::ident {
980 let zz_id = token::str_to_ident("zz");
984 // maybe add to expand.rs...
985 macro_rules! assert_pred (
986 ($pred:expr, $predname:expr, $a:expr , $b:expr) => (
988 let pred_val = $pred;
991 if !(pred_val(a_val,b_val)) {
992 fail!("expected args satisfying %s, got %? and %?",
993 $predname, a_val, b_val);
999 // make sure idents get transformed everywhere
1000 #[test] fn ident_transformation () {
1001 let zz_fold = fun_to_ident_folder(to_zz());
1002 let ast = string_to_crate(@"#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}");
1003 assert_pred!(matches_codepattern,
1004 "matches_codepattern",
1005 pprust::to_str(&zz_fold.fold_crate(ast),fake_print_crate,
1006 token::get_ident_interner()),
1007 ~"#[a]mod zz{fn zz(zz:zz,zz:zz){zz!(zz,zz,zz);zz;zz}}");
1010 // even inside macro defs....
1011 #[test] fn ident_transformation_in_defs () {
1012 let zz_fold = fun_to_ident_folder(to_zz());
1013 let ast = string_to_crate(@"macro_rules! a {(b $c:expr $(d $e:token)f+
1014 => (g $(d $d $e)+))} ");
1015 assert_pred!(matches_codepattern,
1016 "matches_codepattern",
1017 pprust::to_str(&zz_fold.fold_crate(ast),fake_print_crate,
1018 token::get_ident_interner()),
1019 ~"zz!zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)))");