Skip to content

Commit 10baf2c

Browse files
committed
add fold_mac field to fold.rs
1 parent c082b8e commit 10baf2c

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed

src/libsyntax/fold.rs

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ use codemap::{Span, Spanned};
1414
use parse::token;
1515
use opt_vec::OptVec;
1616

17+
// this file defines an ast_fold trait for objects that can perform
18+
// a "fold" on Rust ASTs. It also contains a structure that implements
19+
// that trait, and a "default_fold" whose fields contain closures
20+
// that perform "default traversals", visiting all of the sub-elements
21+
// and re-assembling the result. The "fun_to_ident_folder" in the
22+
// test module provides a simple example of creating a very simple
23+
// fold that only looks at identifiers.
24+
1725
pub trait ast_fold {
1826
fn fold_crate(@self, &Crate) -> Crate;
1927
fn fold_view_item(@self, &view_item) -> view_item;
@@ -35,6 +43,7 @@ pub trait ast_fold {
3543
fn fold_ident(@self, Ident) -> Ident;
3644
fn fold_path(@self, &Path) -> Path;
3745
fn fold_local(@self, @Local) -> @Local;
46+
fn fold_mac(@self, &mac) -> mac;
3847
fn map_exprs(@self, @fn(@Expr) -> @Expr, &[@Expr]) -> ~[@Expr];
3948
fn new_id(@self, NodeId) -> NodeId;
4049
fn new_span(@self, Span) -> Span;
@@ -64,6 +73,7 @@ pub struct AstFoldFns {
6473
fold_ident: @fn(Ident, @ast_fold) -> Ident,
6574
fold_path: @fn(&Path, @ast_fold) -> Path,
6675
fold_local: @fn(@Local, @ast_fold) -> @Local,
76+
fold_mac: @fn(&mac_, Span, @ast_fold) -> (mac_, Span),
6777
map_exprs: @fn(@fn(@Expr) -> @Expr, &[@Expr]) -> ~[@Expr],
6878
new_id: @fn(NodeId) -> NodeId,
6979
new_span: @fn(Span) -> Span
@@ -112,19 +122,6 @@ fn fold_arg_(a: arg, fld: @ast_fold) -> arg {
112122
}
113123
}
114124

115-
//used in noop_fold_expr, and possibly elsewhere in the future
116-
fn fold_mac_(m: &mac, fld: @ast_fold) -> mac {
117-
Spanned {
118-
node: match m.node {
119-
mac_invoc_tt(ref p,ref tts,ctxt) =>
120-
mac_invoc_tt(fld.fold_path(p),
121-
fold_tts(*tts,fld),
122-
ctxt)
123-
},
124-
span: fld.new_span(m.span)
125-
}
126-
}
127-
128125
// build a new vector of tts by appling the ast_fold's fold_ident to
129126
// all of the identifiers in the token trees.
130127
pub fn fold_tts(tts : &[token_tree], f : @ast_fold) -> ~[token_tree] {
@@ -326,10 +323,7 @@ pub fn noop_fold_item_underscore(i: &item_, fld: @ast_fold) -> item_ {
326323
)
327324
}
328325
item_mac(ref m) => {
329-
// It would probably be nicer
330-
// to expose this in the ast_fold trait, but I'll defer
331-
// that work.
332-
item_mac(fold_mac_(m,fld))
326+
item_mac(fld.fold_mac(m))
333327
}
334328
}
335329
}
@@ -398,7 +392,6 @@ pub fn noop_fold_block(b: &Block, fld: @ast_fold) -> Block {
398392
}
399393

400394
fn noop_fold_stmt(s: &Stmt_, fld: @ast_fold) -> Option<Stmt_> {
401-
let fold_mac = |x| fold_mac_(x, fld);
402395
match *s {
403396
StmtDecl(d, nid) => {
404397
match fld.fold_decl(d) {
@@ -412,7 +405,7 @@ fn noop_fold_stmt(s: &Stmt_, fld: @ast_fold) -> Option<Stmt_> {
412405
StmtSemi(e, nid) => {
413406
Some(StmtSemi(fld.fold_expr(e), fld.new_id(nid)))
414407
}
415-
StmtMac(ref mac, semi) => Some(StmtMac(fold_mac(mac), semi))
408+
StmtMac(ref mac, semi) => Some(StmtMac(fld.fold_mac(mac), semi))
416409
}
417410
}
418411

@@ -480,6 +473,12 @@ fn noop_fold_decl(d: &Decl_, fld: @ast_fold) -> Option<Decl_> {
480473
}
481474
}
482475

476+
// lift a function in ast-thingy X fold -> ast-thingy to a function
477+
// in (ast-thingy X span X fold) -> (ast-thingy X fold). Basically,
478+
// carries the span around.
479+
// It seems strange to me that the call to new_fold doesn't happen
480+
// here but instead in the impl down below.... probably just an
481+
// accident?
483482
pub fn wrap<T>(f: @fn(&T, @ast_fold) -> T)
484483
-> @fn(&T, Span, @ast_fold) -> (T, Span) {
485484
let result: @fn(&T, Span, @ast_fold) -> (T, Span) = |x, s, fld| {
@@ -498,8 +497,6 @@ pub fn noop_fold_expr(e: &Expr_, fld: @ast_fold) -> Expr_ {
498497
}
499498
let fold_field = |x| fold_field_(x, fld);
500499

501-
let fold_mac = |x| fold_mac_(x, fld);
502-
503500
match *e {
504501
ExprVstore(e, v) => {
505502
ExprVstore(fld.fold_expr(e), v)
@@ -631,7 +628,7 @@ pub fn noop_fold_expr(e: &Expr_, fld: @ast_fold) -> Expr_ {
631628
.. (*a).clone()
632629
})
633630
}
634-
ExprMac(ref mac) => ExprMac(fold_mac(mac)),
631+
ExprMac(ref mac) => ExprMac(fld.fold_mac(mac)),
635632
ExprStruct(ref path, ref fields, maybe_expr) => {
636633
ExprStruct(
637634
fld.fold_path(path),
@@ -644,7 +641,6 @@ pub fn noop_fold_expr(e: &Expr_, fld: @ast_fold) -> Expr_ {
644641
}
645642

646643
pub fn noop_fold_ty(t: &ty_, fld: @ast_fold) -> ty_ {
647-
let fold_mac = |x| fold_mac_(x, fld);
648644
fn fold_mt(mt: &mt, fld: @ast_fold) -> mt {
649645
mt {
650646
ty: ~fld.fold_ty(mt.ty),
@@ -700,7 +696,7 @@ pub fn noop_fold_ty(t: &ty_, fld: @ast_fold) -> ty_ {
700696
)
701697
}
702698
ty_typeof(e) => ty_typeof(fld.fold_expr(e)),
703-
ty_mac(ref mac) => ty_mac(fold_mac(mac))
699+
ty_mac(ref mac) => ty_mac(fld.fold_mac(mac))
704700
}
705701
}
706702

@@ -787,6 +783,19 @@ fn noop_fold_local(l: @Local, fld: @ast_fold) -> @Local {
787783
}
788784
}
789785

786+
// the default macro traversal. visit the path
787+
// using fold_path, and the tts using fold_tts,
788+
// and the span using new_span
789+
fn noop_fold_mac(m: &mac_, fld: @ast_fold) -> mac_ {
790+
match *m {
791+
mac_invoc_tt(ref p,ref tts,ctxt) =>
792+
mac_invoc_tt(fld.fold_path(p),
793+
fold_tts(*tts,fld),
794+
ctxt)
795+
}
796+
}
797+
798+
790799
/* temporarily eta-expand because of a compiler bug with using `fn<T>` as a
791800
value */
792801
fn noop_map_exprs(f: @fn(@Expr) -> @Expr, es: &[@Expr]) -> ~[@Expr] {
@@ -819,6 +828,7 @@ pub fn default_ast_fold() -> ast_fold_fns {
819828
fold_ident: noop_fold_ident,
820829
fold_path: noop_fold_path,
821830
fold_local: noop_fold_local,
831+
fold_mac: wrap(noop_fold_mac),
822832
map_exprs: noop_map_exprs,
823833
new_id: noop_id,
824834
new_span: noop_span,
@@ -924,6 +934,10 @@ impl ast_fold for AstFoldFns {
924934
fn fold_local(@self, x: @Local) -> @Local {
925935
(self.fold_local)(x, self as @ast_fold)
926936
}
937+
fn fold_mac(@self, x: &mac) -> mac {
938+
let (n, s) = (self.fold_mac)(&x.node, x.span, self as @ast_fold);
939+
Spanned { node: n, span: (self.new_span)(s) }
940+
}
927941
fn map_exprs(@self,
928942
f: @fn(@Expr) -> @Expr,
929943
e: &[@Expr])

0 commit comments

Comments
 (0)