Make ast_fold take &mut self

This commit is contained in:
Steven Fackler 2013-12-27 20:34:51 -07:00
parent 933def408c
commit 3965dddf49
10 changed files with 131 additions and 130 deletions

View File

@ -18,14 +18,14 @@ struct NodeIdAssigner {
}
impl ast_fold for NodeIdAssigner {
fn new_id(&self, old_id: ast::NodeId) -> ast::NodeId {
fn new_id(&mut self, old_id: ast::NodeId) -> ast::NodeId {
assert_eq!(old_id, ast::DUMMY_NODE_ID);
self.sess.next_node_id()
}
}
pub fn assign_node_ids(sess: Session, crate: ast::Crate) -> ast::Crate {
let fold = NodeIdAssigner {
let mut fold = NodeIdAssigner {
sess: sess,
};
fold.fold_crate(crate)

View File

@ -24,17 +24,17 @@ pub fn strip_unconfigured_items(crate: ast::Crate) -> ast::Crate {
}
impl<'a> fold::ast_fold for Context<'a> {
fn fold_mod(&self, module: &ast::_mod) -> ast::_mod {
fn fold_mod(&mut self, module: &ast::_mod) -> ast::_mod {
fold_mod(self, module)
}
fn fold_block(&self, block: ast::P<ast::Block>) -> ast::P<ast::Block> {
fn fold_block(&mut self, block: ast::P<ast::Block>) -> ast::P<ast::Block> {
fold_block(self, block)
}
fn fold_foreign_mod(&self, foreign_module: &ast::foreign_mod)
fn fold_foreign_mod(&mut self, foreign_module: &ast::foreign_mod)
-> ast::foreign_mod {
fold_foreign_mod(self, foreign_module)
}
fn fold_item_underscore(&self, item: &ast::item_) -> ast::item_ {
fn fold_item_underscore(&mut self, item: &ast::item_) -> ast::item_ {
fold_item_underscore(self, item)
}
}
@ -42,7 +42,7 @@ impl<'a> fold::ast_fold for Context<'a> {
pub fn strip_items(crate: ast::Crate,
in_cfg: |attrs: &[ast::Attribute]| -> bool)
-> ast::Crate {
let ctxt = Context {
let mut ctxt = Context {
in_cfg: in_cfg,
};
ctxt.fold_crate(crate)
@ -57,7 +57,7 @@ fn filter_view_item<'r>(cx: &Context, view_item: &'r ast::view_item)
}
}
fn fold_mod(cx: &Context, m: &ast::_mod) -> ast::_mod {
fn fold_mod(cx: &mut Context, m: &ast::_mod) -> ast::_mod {
let filtered_items = m.items.iter()
.filter(|&a| item_in_cfg(cx, *a))
.flat_map(|&x| cx.fold_item(x).move_iter())
@ -80,7 +80,7 @@ fn filter_foreign_item(cx: &Context, item: @ast::foreign_item)
}
}
fn fold_foreign_mod(cx: &Context, nm: &ast::foreign_mod) -> ast::foreign_mod {
fn fold_foreign_mod(cx: &mut Context, nm: &ast::foreign_mod) -> ast::foreign_mod {
let filtered_items = nm.items
.iter()
.filter_map(|a| filter_foreign_item(cx, *a))
@ -95,7 +95,7 @@ fn fold_foreign_mod(cx: &Context, nm: &ast::foreign_mod) -> ast::foreign_mod {
}
}
fn fold_item_underscore(cx: &Context, item: &ast::item_) -> ast::item_ {
fn fold_item_underscore(cx: &mut Context, item: &ast::item_) -> ast::item_ {
let item = match *item {
ast::item_impl(ref a, ref b, c, ref methods) => {
let methods = methods.iter().filter(|m| method_in_cfg(cx, **m))
@ -129,7 +129,7 @@ fn retain_stmt(cx: &Context, stmt: @ast::Stmt) -> bool {
}
}
fn fold_block(cx: &Context, b: ast::P<ast::Block>) -> ast::P<ast::Block> {
fn fold_block(cx: &mut Context, b: ast::P<ast::Block>) -> ast::P<ast::Block> {
let resulting_stmts = b.stmts.iter()
.filter(|&a| retain_stmt(cx, *a))
.flat_map(|&stmt| cx.fold_stmt(stmt).move_iter())

View File

@ -56,7 +56,7 @@ struct StandardLibraryInjector {
}
impl fold::ast_fold for StandardLibraryInjector {
fn fold_crate(&self, crate: ast::Crate) -> ast::Crate {
fn fold_crate(&mut self, crate: ast::Crate) -> ast::Crate {
let version = STD_VERSION.to_managed();
let vers_item = attr::mk_name_value_item_str(@"vers", version);
let mut vis = ~[ast::view_item {
@ -108,7 +108,7 @@ impl fold::ast_fold for StandardLibraryInjector {
}
}
fn fold_item(&self, item: @ast::item) -> SmallVector<@ast::item> {
fn fold_item(&mut self, item: @ast::item) -> SmallVector<@ast::item> {
if !no_prelude(item.attrs) {
// only recur if there wasn't `#[no_implicit_prelude];`
// on this item, i.e. this means that the prelude is not
@ -119,7 +119,7 @@ impl fold::ast_fold for StandardLibraryInjector {
}
}
fn fold_mod(&self, module: &ast::_mod) -> ast::_mod {
fn fold_mod(&mut self, module: &ast::_mod) -> ast::_mod {
let prelude_path = ast::Path {
span: dummy_sp(),
global: false,
@ -158,7 +158,7 @@ impl fold::ast_fold for StandardLibraryInjector {
}
fn inject_libstd_ref(sess: Session, crate: ast::Crate) -> ast::Crate {
let fold = StandardLibraryInjector {
let mut fold = StandardLibraryInjector {
sess: sess,
};
fold.fold_crate(crate)

View File

@ -67,7 +67,7 @@ struct TestHarnessGenerator {
}
impl fold::ast_fold for TestHarnessGenerator {
fn fold_crate(&self, c: ast::Crate) -> ast::Crate {
fn fold_crate(&mut self, c: ast::Crate) -> ast::Crate {
let folded = fold::noop_fold_crate(c, self);
// Add a special __test module to the crate that will contain code
@ -78,7 +78,7 @@ impl fold::ast_fold for TestHarnessGenerator {
}
}
fn fold_item(&self, i: @ast::item) -> SmallVector<@ast::item> {
fn fold_item(&mut self, i: @ast::item) -> SmallVector<@ast::item> {
{
let mut path = self.cx.path.borrow_mut();
path.get().push(i.ident);
@ -122,7 +122,7 @@ impl fold::ast_fold for TestHarnessGenerator {
res
}
fn fold_mod(&self, m: &ast::_mod) -> ast::_mod {
fn fold_mod(&mut self, m: &ast::_mod) -> ast::_mod {
// Remove any #[main] from the AST so it doesn't clash with
// the one we're going to add. Only if compiling an executable.
@ -172,7 +172,7 @@ fn generate_test_harness(sess: session::Session, crate: ast::Crate)
}
});
let fold = TestHarnessGenerator {
let mut fold = TestHarnessGenerator {
cx: cx
};
let res = fold.fold_crate(crate);

View File

@ -301,7 +301,7 @@ struct NestedItemsDropper {
}
impl fold::ast_fold for NestedItemsDropper {
fn fold_block(&self, blk: ast::P<ast::Block>) -> ast::P<ast::Block> {
fn fold_block(&mut self, blk: ast::P<ast::Block>) -> ast::P<ast::Block> {
let stmts_sans_items = blk.stmts.iter().filter_map(|stmt| {
match stmt.node {
ast::StmtExpr(_, _) | ast::StmtSemi(_, _) |
@ -340,7 +340,7 @@ impl fold::ast_fold for NestedItemsDropper {
// nested items, as otherwise it would get confused when translating
// inlined items.
fn simplify_ast(ii: &ast::inlined_item) -> ast::inlined_item {
let fld = NestedItemsDropper {
let mut fld = NestedItemsDropper {
contents: (),
};
@ -365,17 +365,17 @@ struct AstRenumberer {
}
impl fold::ast_fold for AstRenumberer {
fn new_id(&self, id: ast::NodeId) -> ast::NodeId {
fn new_id(&mut self, id: ast::NodeId) -> ast::NodeId {
self.xcx.tr_id(id)
}
fn new_span(&self, span: Span) -> Span {
fn new_span(&mut self, span: Span) -> Span {
self.xcx.tr_span(span)
}
}
fn renumber_ast(xcx: @ExtendedDecodeContext, ii: ast::inlined_item)
-> ast::inlined_item {
let fld = AstRenumberer {
let mut fld = AstRenumberer {
xcx: xcx,
};
match ii {

View File

@ -80,7 +80,7 @@ struct ReadyCtx {
fns: ~[ListenerFn]
}
fn fold_mod(_ctx: @mut ReadyCtx, m: &ast::_mod, fold: &CrateSetup)
fn fold_mod(_ctx: @mut ReadyCtx, m: &ast::_mod, fold: &mut CrateSetup)
-> ast::_mod {
fn strip_main(item: @ast::item) -> @ast::item {
@ast::item {
@ -101,7 +101,7 @@ fn fold_mod(_ctx: @mut ReadyCtx, m: &ast::_mod, fold: &CrateSetup)
}, fold)
}
fn fold_item(ctx: @mut ReadyCtx, item: @ast::item, fold: &CrateSetup)
fn fold_item(ctx: @mut ReadyCtx, item: @ast::item, fold: &mut CrateSetup)
-> SmallVector<@ast::item> {
ctx.path.push(item.ident);
@ -145,10 +145,10 @@ struct CrateSetup {
}
impl fold::ast_fold for CrateSetup {
fn fold_item(&self, item: @ast::item) -> SmallVector<@ast::item> {
fn fold_item(&mut self, item: @ast::item) -> SmallVector<@ast::item> {
fold_item(self.ctx, item, self)
}
fn fold_mod(&self, module: &ast::_mod) -> ast::_mod {
fn fold_mod(&mut self, module: &ast::_mod) -> ast::_mod {
fold_mod(self.ctx, module, self)
}
}
@ -162,7 +162,7 @@ pub fn ready_crate(sess: session::Session,
path: ~[],
fns: ~[]
};
let fold = CrateSetup {
let mut fold = CrateSetup {
ctx: ctx,
};
fold.fold_crate(crate)

View File

@ -324,11 +324,11 @@ impl ExtCtxt {
loop {
match e.node {
ast::ExprMac(..) => {
let expander = expand::MacroExpander {
let mut expander = expand::MacroExpander {
extsbox: @mut syntax_expander_table(),
cx: self,
};
e = expand::expand_expr(e, &expander);
e = expand::expand_expr(e, &mut expander);
}
_ => return e
}

View File

@ -908,7 +908,7 @@ struct Duplicator<'a> {
}
impl<'a> ast_fold for Duplicator<'a> {
fn new_id(&self, _: NodeId) -> NodeId {
fn new_id(&mut self, _: NodeId) -> NodeId {
ast::DUMMY_NODE_ID
}
}
@ -925,7 +925,7 @@ pub trait Duplicate {
impl Duplicate for @ast::Expr {
fn duplicate(&self, cx: &ExtCtxt) -> @ast::Expr {
let folder = Duplicator {
let mut folder = Duplicator {
cx: cx,
};
folder.fold_expr(*self)

View File

@ -31,7 +31,7 @@ use util::small_vector::SmallVector;
use std::vec;
pub fn expand_expr(e: @ast::Expr, fld: &MacroExpander) -> @ast::Expr {
pub fn expand_expr(e: @ast::Expr, fld: &mut MacroExpander) -> @ast::Expr {
match e.node {
// expr_mac should really be expr_ext or something; it's the
// entry-point for all syntax extensions.
@ -210,7 +210,7 @@ pub fn expand_expr(e: @ast::Expr, fld: &MacroExpander) -> @ast::Expr {
//
// NB: there is some redundancy between this and expand_item, below, and
// they might benefit from some amount of semantic and language-UI merger.
pub fn expand_mod_items(module_: &ast::_mod, fld: &MacroExpander) -> ast::_mod {
pub fn expand_mod_items(module_: &ast::_mod, fld: &mut MacroExpander) -> ast::_mod {
// Fold the contents first:
let module_ = noop_fold_mod(module_, fld);
@ -263,7 +263,8 @@ macro_rules! with_exts_frame (
static special_block_name : &'static str = " block";
// When we enter a module, record it, for the sake of `module!`
pub fn expand_item(it: @ast::item, fld: &MacroExpander) -> SmallVector<@ast::item> {
pub fn expand_item(it: @ast::item, fld: &mut MacroExpander)
-> SmallVector<@ast::item> {
match it.node {
ast::item_mac(..) => expand_item_mac(it, fld),
ast::item_mod(_) | ast::item_foreign_mod(_) => {
@ -286,7 +287,7 @@ pub fn contains_macro_escape(attrs: &[ast::Attribute]) -> bool {
// Support for item-position macro invocations, exactly the same
// logic as for expression-position macro invocations.
pub fn expand_item_mac(it: @ast::item, fld: &MacroExpander)
pub fn expand_item_mac(it: @ast::item, fld: &mut MacroExpander)
-> SmallVector<@ast::item> {
let (pth, tts, ctxt) = match it.node {
item_mac(codemap::Spanned {
@ -394,7 +395,7 @@ fn insert_macro(exts: SyntaxEnv, name: ast::Name, transformer: @Transformer) {
}
// expand a stmt
pub fn expand_stmt(s: &Stmt, fld: &MacroExpander) -> SmallVector<@Stmt> {
pub fn expand_stmt(s: &Stmt, fld: &mut MacroExpander) -> SmallVector<@Stmt> {
// why the copying here and not in expand_expr?
// looks like classic changed-in-only-one-place
let (pth, tts, semi, ctxt) = match s.node {
@ -487,7 +488,7 @@ pub fn expand_stmt(s: &Stmt, fld: &MacroExpander) -> SmallVector<@Stmt> {
// expand a non-macro stmt. this is essentially the fallthrough for
// expand_stmt, above.
fn expand_non_macro_stmt(s: &Stmt, fld: &MacroExpander)
fn expand_non_macro_stmt(s: &Stmt, fld: &mut MacroExpander)
-> SmallVector<@Stmt> {
// is it a let?
match s.node {
@ -521,7 +522,7 @@ fn expand_non_macro_stmt(s: &Stmt, fld: &MacroExpander)
let new_name = fresh_name(ident);
new_pending_renames.push((*ident,new_name));
}
let rename_fld = renames_to_fold(new_pending_renames);
let mut rename_fld = renames_to_fold(new_pending_renames);
// rewrite the pattern using the new names (the old ones
// have already been applied):
let rewritten_pat = rename_fld.fold_pat(expanded_pat);
@ -609,17 +610,17 @@ pub fn new_name_finder(idents: ~[ast::Ident]) -> NewNameFinderContext {
}
// expand a block. pushes a new exts_frame, then calls expand_block_elts
pub fn expand_block(blk: &Block, fld: &MacroExpander) -> P<Block> {
pub fn expand_block(blk: &Block, fld: &mut MacroExpander) -> P<Block> {
// see note below about treatment of exts table
with_exts_frame!(fld.extsbox,false,
expand_block_elts(blk, fld))
}
// expand the elements of a block.
pub fn expand_block_elts(b: &Block, fld: &MacroExpander) -> P<Block> {
pub fn expand_block_elts(b: &Block, fld: &mut MacroExpander) -> P<Block> {
let block_info = get_block_info(*fld.extsbox);
let pending_renames = block_info.pending_renames;
let rename_fld = renames_to_fold(pending_renames);
let mut rename_fld = renames_to_fold(pending_renames);
let new_view_items = b.view_items.map(|x| fld.fold_view_item(x));
let new_stmts = b.stmts.iter()
.map(|x| rename_fld.fold_stmt(*x)
@ -651,7 +652,7 @@ struct IdentRenamer {
}
impl ast_fold for IdentRenamer {
fn fold_ident(&self, id: ast::Ident) -> ast::Ident {
fn fold_ident(&mut self, id: ast::Ident) -> ast::Ident {
let new_ctxt = self.renames.iter().fold(id.ctxt, |ctxt, &(from, to)| {
new_rename(from, to, ctxt)
});
@ -664,10 +665,10 @@ impl ast_fold for IdentRenamer {
// given a mutable list of renames, return a tree-folder that applies those
// renames.
pub fn renames_to_fold(renames: @mut ~[(ast::Ident,ast::Name)]) -> @ast_fold {
@IdentRenamer {
pub fn renames_to_fold(renames: @mut ~[(ast::Ident,ast::Name)]) -> IdentRenamer {
IdentRenamer {
renames: renames,
} as @ast_fold
}
}
pub fn new_span(cx: &ExtCtxt, sp: Span) -> Span {
@ -897,7 +898,7 @@ struct Injector {
}
impl ast_fold for Injector {
fn fold_mod(&self, module: &ast::_mod) -> ast::_mod {
fn fold_mod(&mut self, module: &ast::_mod) -> ast::_mod {
// Just inject the standard macros at the start of the first module
// in the crate: that is, at the start of the crate file itself.
let items = vec::append(~[ self.sm ], module.items);
@ -923,9 +924,9 @@ pub fn inject_std_macros(parse_sess: @mut parse::ParseSess,
None => fail!("expected core macros to parse correctly")
};
let injector = @Injector {
let mut injector = Injector {
sm: sm,
} as @ast_fold;
};
injector.fold_crate(c)
}
@ -935,27 +936,27 @@ pub struct MacroExpander<'a> {
}
impl<'a> ast_fold for MacroExpander<'a> {
fn fold_expr(&self, expr: @ast::Expr) -> @ast::Expr {
fn fold_expr(&mut self, expr: @ast::Expr) -> @ast::Expr {
expand_expr(expr, self)
}
fn fold_mod(&self, module: &ast::_mod) -> ast::_mod {
fn fold_mod(&mut self, module: &ast::_mod) -> ast::_mod {
expand_mod_items(module, self)
}
fn fold_item(&self, item: @ast::item) -> SmallVector<@ast::item> {
fn fold_item(&mut self, item: @ast::item) -> SmallVector<@ast::item> {
expand_item(item, self)
}
fn fold_stmt(&self, stmt: &ast::Stmt) -> SmallVector<@ast::Stmt> {
fn fold_stmt(&mut self, stmt: &ast::Stmt) -> SmallVector<@ast::Stmt> {
expand_stmt(stmt, self)
}
fn fold_block(&self, block: P<Block>) -> P<Block> {
fn fold_block(&mut self, block: P<Block>) -> P<Block> {
expand_block(block, self)
}
fn new_span(&self, span: Span) -> Span {
fn new_span(&mut self, span: Span) -> Span {
new_span(self.cx, span)
}
}
@ -970,7 +971,7 @@ pub fn expand_crate(parse_sess: @mut parse::ParseSess,
// every method/element of AstFoldFns in fold.rs.
let extsbox = syntax_expander_table();
let cx = ExtCtxt::new(parse_sess, cfg.clone());
let expander = MacroExpander {
let mut expander = MacroExpander {
extsbox: @mut extsbox,
cx: &cx,
};
@ -1046,7 +1047,7 @@ pub struct ContextWrapper {
}
impl ast_fold for ContextWrapper {
fn fold_ident(&self, id: ast::Ident) -> ast::Ident {
fn fold_ident(&mut self, id: ast::Ident) -> ast::Ident {
let ast::Ident {
name,
ctxt
@ -1056,7 +1057,7 @@ impl ast_fold for ContextWrapper {
ctxt: self.context_function.f(ctxt),
}
}
fn fold_mac(&self, m: &ast::mac) -> ast::mac {
fn fold_mac(&mut self, m: &ast::mac) -> ast::mac {
let macro = match m.node {
mac_invoc_tt(ref path, ref tts, ctxt) => {
mac_invoc_tt(self.fold_path(path),
@ -1073,24 +1074,24 @@ impl ast_fold for ContextWrapper {
// given a function from ctxts to ctxts, produce
// an ast_fold that applies that function to all ctxts:
pub fn fun_to_ctxt_folder<T : 'static + CtxtFn>(cf: @T) -> @ContextWrapper {
@ContextWrapper {
pub fn fun_to_ctxt_folder<T : 'static + CtxtFn>(cf: @T) -> ContextWrapper {
ContextWrapper {
context_function: cf as @CtxtFn,
}
}
// just a convenience:
pub fn new_mark_folder(m: Mrk) -> @ContextWrapper {
pub fn new_mark_folder(m: Mrk) -> ContextWrapper {
fun_to_ctxt_folder(@Marker{mark:m})
}
pub fn new_rename_folder(from: ast::Ident, to: ast::Name) -> @ContextWrapper {
pub fn new_rename_folder(from: ast::Ident, to: ast::Name) -> ContextWrapper {
fun_to_ctxt_folder(@Renamer{from:from,to:to})
}
// apply a given mark to the given token trees. Used prior to expansion of a macro.
fn mark_tts(tts : &[token_tree], m : Mrk) -> ~[token_tree] {
fold_tts(tts,new_mark_folder(m))
fold_tts(tts, &mut new_mark_folder(m))
}
// apply a given mark to the given expr. Used following the expansion of a macro.
@ -1295,7 +1296,7 @@ mod test {
let ident_str = @"x";
let tts = string_to_tts(ident_str);
let fm = fresh_mark();
let marked_once = fold::fold_tts(tts,new_mark_folder(fm));
let marked_once = fold::fold_tts(tts,&mut new_mark_folder(fm));
assert_eq!(marked_once.len(),1);
let marked_once_ctxt =
match marked_once[0] {
@ -1317,7 +1318,7 @@ mod test {
let item_ast = string_to_crate(@"fn f() -> int { a }");
let a_name = intern("a");
let a2_name = gensym("a2");
let renamer = new_rename_folder(ast::Ident{name:a_name,ctxt:EMPTY_CTXT},
let mut renamer = new_rename_folder(ast::Ident{name:a_name,ctxt:EMPTY_CTXT},
a2_name);
let renamed_ast = renamer.fold_crate(item_ast.clone());
let mut path_finder = new_path_finder(~[]);

View File

@ -17,15 +17,15 @@ use util::small_vector::SmallVector;
// We may eventually want to be able to fold over type parameters, too.
pub trait ast_fold {
fn fold_crate(&self, c: Crate) -> Crate {
fn fold_crate(&mut self, c: Crate) -> Crate {
noop_fold_crate(c, self)
}
fn fold_meta_items(&self, meta_items: &[@MetaItem]) -> ~[@MetaItem] {
fn fold_meta_items(&mut self, meta_items: &[@MetaItem]) -> ~[@MetaItem] {
meta_items.map(|x| fold_meta_item_(*x, self))
}
fn fold_view_paths(&self, view_paths: &[@view_path]) -> ~[@view_path] {
fn fold_view_paths(&mut self, view_paths: &[@view_path]) -> ~[@view_path] {
view_paths.map(|view_path| {
let inner_view_path = match view_path.node {
view_path_simple(ref ident, ref path, node_id) => {
@ -62,7 +62,7 @@ pub trait ast_fold {
})
}
fn fold_view_item(&self, vi: &view_item) -> view_item {
fn fold_view_item(&mut self, vi: &view_item) -> view_item {
let inner_view_item = match vi.node {
view_item_extern_mod(ref ident,
string,
@ -85,7 +85,7 @@ pub trait ast_fold {
}
}
fn fold_foreign_item(&self, ni: @foreign_item) -> @foreign_item {
fn fold_foreign_item(&mut self, ni: @foreign_item) -> @foreign_item {
let fold_attribute = |x| fold_attribute_(x, self);
@ast::foreign_item {
@ -114,11 +114,11 @@ pub trait ast_fold {
}
}
fn fold_item(&self, i: @item) -> SmallVector<@item> {
fn fold_item(&mut self, i: @item) -> SmallVector<@item> {
noop_fold_item(i, self)
}
fn fold_struct_field(&self, sf: &struct_field) -> struct_field {
fn fold_struct_field(&mut self, sf: &struct_field) -> struct_field {
let fold_attribute = |x| fold_attribute_(x, self);
Spanned {
@ -132,15 +132,15 @@ pub trait ast_fold {
}
}
fn fold_item_underscore(&self, i: &item_) -> item_ {
fn fold_item_underscore(&mut self, i: &item_) -> item_ {
noop_fold_item_underscore(i, self)
}
fn fold_type_method(&self, m: &TypeMethod) -> TypeMethod {
fn fold_type_method(&mut self, m: &TypeMethod) -> TypeMethod {
noop_fold_type_method(m, self)
}
fn fold_method(&self, m: @method) -> @method {
fn fold_method(&mut self, m: @method) -> @method {
@ast::method {
ident: self.fold_ident(m.ident),
attrs: m.attrs.map(|a| fold_attribute_(*a, self)),
@ -156,15 +156,15 @@ pub trait ast_fold {
}
}
fn fold_block(&self, b: P<Block>) -> P<Block> {
fn fold_block(&mut self, b: P<Block>) -> P<Block> {
noop_fold_block(b, self)
}
fn fold_stmt(&self, s: &Stmt) -> SmallVector<@Stmt> {
fn fold_stmt(&mut self, s: &Stmt) -> SmallVector<@Stmt> {
noop_fold_stmt(s, self)
}
fn fold_arm(&self, a: &Arm) -> Arm {
fn fold_arm(&mut self, a: &Arm) -> Arm {
Arm {
pats: a.pats.map(|x| self.fold_pat(*x)),
guard: a.guard.map(|x| self.fold_expr(x)),
@ -172,7 +172,7 @@ pub trait ast_fold {
}
}
fn fold_pat(&self, p: @Pat) -> @Pat {
fn fold_pat(&mut self, p: @Pat) -> @Pat {
let node = match p.node {
PatWild => PatWild,
PatWildMulti => PatWildMulti,
@ -217,7 +217,7 @@ pub trait ast_fold {
}
}
fn fold_decl(&self, d: @Decl) -> SmallVector<@Decl> {
fn fold_decl(&mut self, d: @Decl) -> SmallVector<@Decl> {
let node = match d.node {
DeclLocal(ref l) => SmallVector::one(DeclLocal(self.fold_local(*l))),
DeclItem(it) => {
@ -233,11 +233,11 @@ pub trait ast_fold {
}).collect()
}
fn fold_expr(&self, e: @Expr) -> @Expr {
fn fold_expr(&mut self, e: @Expr) -> @Expr {
noop_fold_expr(e, self)
}
fn fold_ty(&self, t: P<Ty>) -> P<Ty> {
fn fold_ty(&mut self, t: P<Ty>) -> P<Ty> {
let node = match t.node {
ty_nil | ty_bot | ty_infer => t.node.clone(),
ty_box(ref mt) => ty_box(fold_mt(mt, self)),
@ -284,11 +284,11 @@ pub trait ast_fold {
})
}
fn fold_mod(&self, m: &_mod) -> _mod {
fn fold_mod(&mut self, m: &_mod) -> _mod {
noop_fold_mod(m, self)
}
fn fold_foreign_mod(&self, nm: &foreign_mod) -> foreign_mod {
fn fold_foreign_mod(&mut self, nm: &foreign_mod) -> foreign_mod {
ast::foreign_mod {
abis: nm.abis,
view_items: nm.view_items
@ -302,7 +302,7 @@ pub trait ast_fold {
}
}
fn fold_variant(&self, v: &variant) -> P<variant> {
fn fold_variant(&mut self, v: &variant) -> P<variant> {
let kind;
match v.node.kind {
tuple_variant_kind(ref variant_args) => {
@ -339,11 +339,11 @@ pub trait ast_fold {
})
}
fn fold_ident(&self, i: Ident) -> Ident {
fn fold_ident(&mut self, i: Ident) -> Ident {
i
}
fn fold_path(&self, p: &Path) -> Path {
fn fold_path(&mut self, p: &Path) -> Path {
ast::Path {
span: self.new_span(p.span),
global: p.global,
@ -355,7 +355,7 @@ pub trait ast_fold {
}
}
fn fold_local(&self, l: @Local) -> @Local {
fn fold_local(&mut self, l: @Local) -> @Local {
@Local {
ty: self.fold_ty(l.ty),
pat: self.fold_pat(l.pat),
@ -365,7 +365,7 @@ pub trait ast_fold {
}
}
fn fold_mac(&self, macro: &mac) -> mac {
fn fold_mac(&mut self, macro: &mac) -> mac {
Spanned {
node: match macro.node {
mac_invoc_tt(ref p, ref tts, ctxt) => {
@ -382,22 +382,22 @@ pub trait ast_fold {
es.map(|x| f(*x))
}
fn new_id(&self, i: NodeId) -> NodeId {
fn new_id(&mut self, i: NodeId) -> NodeId {
i
}
fn new_span(&self, sp: Span) -> Span {
fn new_span(&mut self, sp: Span) -> Span {
sp
}
fn fold_explicit_self(&self, es: &explicit_self) -> explicit_self {
fn fold_explicit_self(&mut self, es: &explicit_self) -> explicit_self {
Spanned {
span: self.new_span(es.span),
node: self.fold_explicit_self_(&es.node)
}
}
fn fold_explicit_self_(&self, es: &explicit_self_) -> explicit_self_ {
fn fold_explicit_self_(&mut self, es: &explicit_self_) -> explicit_self_ {
match *es {
sty_static | sty_value(_) | sty_uniq(_) | sty_box(_) => {
*es
@ -412,7 +412,7 @@ pub trait ast_fold {
/* some little folds that probably aren't useful to have in ast_fold itself*/
//used in noop_fold_item and noop_fold_crate and noop_fold_crate_directive
fn fold_meta_item_<T:ast_fold>(mi: @MetaItem, fld: &T) -> @MetaItem {
fn fold_meta_item_<T:ast_fold>(mi: @MetaItem, fld: &mut T) -> @MetaItem {
@Spanned {
node:
match mi.node {
@ -430,7 +430,7 @@ fn fold_meta_item_<T:ast_fold>(mi: @MetaItem, fld: &T) -> @MetaItem {
}
//used in noop_fold_item and noop_fold_crate
fn fold_attribute_<T:ast_fold>(at: Attribute, fld: &T) -> Attribute {
fn fold_attribute_<T:ast_fold>(at: Attribute, fld: &mut T) -> Attribute {
Spanned {
span: fld.new_span(at.span),
node: ast::Attribute_ {
@ -442,7 +442,7 @@ fn fold_attribute_<T:ast_fold>(at: Attribute, fld: &T) -> Attribute {
}
//used in noop_fold_foreign_item and noop_fold_fn_decl
fn fold_arg_<T:ast_fold>(a: &arg, fld: &T) -> arg {
fn fold_arg_<T:ast_fold>(a: &arg, fld: &mut T) -> arg {
ast::arg {
ty: fld.fold_ty(a.ty),
pat: fld.fold_pat(a.pat),
@ -452,7 +452,7 @@ fn fold_arg_<T:ast_fold>(a: &arg, fld: &T) -> arg {
// build a new vector of tts by appling the ast_fold's fold_ident to
// all of the identifiers in the token trees.
pub fn fold_tts<T:ast_fold>(tts: &[token_tree], fld: &T) -> ~[token_tree] {
pub fn fold_tts<T:ast_fold>(tts: &[token_tree], fld: &mut T) -> ~[token_tree] {
tts.map(|tt| {
match *tt {
tt_tok(span, ref tok) =>
@ -470,7 +470,7 @@ pub fn fold_tts<T:ast_fold>(tts: &[token_tree], fld: &T) -> ~[token_tree] {
}
// apply ident folder if it's an ident, otherwise leave it alone
fn maybe_fold_ident<T:ast_fold>(t: &token::Token, fld: &T) -> token::Token {
fn maybe_fold_ident<T:ast_fold>(t: &token::Token, fld: &mut T) -> token::Token {
match *t {
token::IDENT(id, followed_by_colons) => {
token::IDENT(fld.fold_ident(id), followed_by_colons)
@ -479,7 +479,7 @@ fn maybe_fold_ident<T:ast_fold>(t: &token::Token, fld: &T) -> token::Token {
}
}
pub fn fold_fn_decl<T:ast_fold>(decl: &ast::fn_decl, fld: &T)
pub fn fold_fn_decl<T:ast_fold>(decl: &ast::fn_decl, fld: &mut T)
-> P<fn_decl> {
P(fn_decl {
inputs: decl.inputs.map(|x| fold_arg_(x, fld)), // bad copy
@ -489,7 +489,7 @@ pub fn fold_fn_decl<T:ast_fold>(decl: &ast::fn_decl, fld: &T)
})
}
fn fold_ty_param_bound<T:ast_fold>(tpb: &TyParamBound, fld: &T)
fn fold_ty_param_bound<T:ast_fold>(tpb: &TyParamBound, fld: &mut T)
-> TyParamBound {
match *tpb {
TraitTyParamBound(ref ty) => TraitTyParamBound(fold_trait_ref(ty, fld)),
@ -497,7 +497,7 @@ fn fold_ty_param_bound<T:ast_fold>(tpb: &TyParamBound, fld: &T)
}
}
pub fn fold_ty_param<T:ast_fold>(tp: &TyParam, fld: &T) -> TyParam {
pub fn fold_ty_param<T:ast_fold>(tp: &TyParam, fld: &mut T) -> TyParam {
TyParam {
ident: tp.ident,
id: fld.new_id(tp.id),
@ -505,12 +505,12 @@ pub fn fold_ty_param<T:ast_fold>(tp: &TyParam, fld: &T) -> TyParam {
}
}
pub fn fold_ty_params<T:ast_fold>(tps: &OptVec<TyParam>, fld: &T)
pub fn fold_ty_params<T:ast_fold>(tps: &OptVec<TyParam>, fld: &mut T)
-> OptVec<TyParam> {
tps.map(|tp| fold_ty_param(tp, fld))
}
pub fn fold_lifetime<T:ast_fold>(l: &Lifetime, fld: &T) -> Lifetime {
pub fn fold_lifetime<T:ast_fold>(l: &Lifetime, fld: &mut T) -> Lifetime {
Lifetime {
id: fld.new_id(l.id),
span: fld.new_span(l.span),
@ -518,22 +518,22 @@ pub fn fold_lifetime<T:ast_fold>(l: &Lifetime, fld: &T) -> Lifetime {
}
}
pub fn fold_lifetimes<T:ast_fold>(lts: &OptVec<Lifetime>, fld: &T)
pub fn fold_lifetimes<T:ast_fold>(lts: &OptVec<Lifetime>, fld: &mut T)
-> OptVec<Lifetime> {
lts.map(|l| fold_lifetime(l, fld))
}
pub fn fold_opt_lifetime<T:ast_fold>(o_lt: &Option<Lifetime>, fld: &T)
pub fn fold_opt_lifetime<T:ast_fold>(o_lt: &Option<Lifetime>, fld: &mut T)
-> Option<Lifetime> {
o_lt.as_ref().map(|lt| fold_lifetime(lt, fld))
}
pub fn fold_generics<T:ast_fold>(generics: &Generics, fld: &T) -> Generics {
pub fn fold_generics<T:ast_fold>(generics: &Generics, fld: &mut T) -> Generics {
Generics {ty_params: fold_ty_params(&generics.ty_params, fld),
lifetimes: fold_lifetimes(&generics.lifetimes, fld)}
}
fn fold_struct_def<T:ast_fold>(struct_def: @ast::struct_def, fld: &T)
fn fold_struct_def<T:ast_fold>(struct_def: @ast::struct_def, fld: &mut T)
-> @ast::struct_def {
@ast::struct_def {
fields: struct_def.fields.map(|f| fold_struct_field(f, fld)),
@ -541,14 +541,14 @@ fn fold_struct_def<T:ast_fold>(struct_def: @ast::struct_def, fld: &T)
}
}
fn fold_trait_ref<T:ast_fold>(p: &trait_ref, fld: &T) -> trait_ref {
fn fold_trait_ref<T:ast_fold>(p: &trait_ref, fld: &mut T) -> trait_ref {
ast::trait_ref {
path: fld.fold_path(&p.path),
ref_id: fld.new_id(p.ref_id),
}
}
fn fold_struct_field<T:ast_fold>(f: &struct_field, fld: &T) -> struct_field {
fn fold_struct_field<T:ast_fold>(f: &struct_field, fld: &mut T) -> struct_field {
Spanned {
node: ast::struct_field_ {
kind: f.node.kind,
@ -560,7 +560,7 @@ fn fold_struct_field<T:ast_fold>(f: &struct_field, fld: &T) -> struct_field {
}
}
fn fold_field_<T:ast_fold>(field: Field, folder: &T) -> Field {
fn fold_field_<T:ast_fold>(field: Field, folder: &mut T) -> Field {
ast::Field {
ident: respan(field.ident.span, folder.fold_ident(field.ident.node)),
expr: folder.fold_expr(field.expr),
@ -568,14 +568,14 @@ fn fold_field_<T:ast_fold>(field: Field, folder: &T) -> Field {
}
}
fn fold_mt<T:ast_fold>(mt: &mt, folder: &T) -> mt {
fn fold_mt<T:ast_fold>(mt: &mt, folder: &mut T) -> mt {
mt {
ty: folder.fold_ty(mt.ty),
mutbl: mt.mutbl,
}
}
fn fold_opt_bounds<T:ast_fold>(b: &Option<OptVec<TyParamBound>>, folder: &T)
fn fold_opt_bounds<T:ast_fold>(b: &Option<OptVec<TyParamBound>>, folder: &mut T)
-> Option<OptVec<TyParamBound>> {
b.as_ref().map(|bounds| {
bounds.map(|bound| {
@ -584,7 +584,7 @@ fn fold_opt_bounds<T:ast_fold>(b: &Option<OptVec<TyParamBound>>, folder: &T)
})
}
fn fold_variant_arg_<T:ast_fold>(va: &variant_arg, folder: &T)
fn fold_variant_arg_<T:ast_fold>(va: &variant_arg, folder: &mut T)
-> variant_arg {
ast::variant_arg {
ty: folder.fold_ty(va.ty),
@ -592,7 +592,7 @@ fn fold_variant_arg_<T:ast_fold>(va: &variant_arg, folder: &T)
}
}
pub fn noop_fold_block<T:ast_fold>(b: P<Block>, folder: &T) -> P<Block> {
pub fn noop_fold_block<T:ast_fold>(b: P<Block>, folder: &mut T) -> P<Block> {
let view_items = b.view_items.map(|x| folder.fold_view_item(x));
let stmts = b.stmts.iter().flat_map(|s| folder.fold_stmt(*s).move_iter()).collect();
P(Block {
@ -605,7 +605,7 @@ pub fn noop_fold_block<T:ast_fold>(b: P<Block>, folder: &T) -> P<Block> {
})
}
pub fn noop_fold_item_underscore<T:ast_fold>(i: &item_, folder: &T) -> item_ {
pub fn noop_fold_item_underscore<T:ast_fold>(i: &item_, folder: &mut T) -> item_ {
match *i {
item_static(t, m, e) => {
item_static(folder.fold_ty(t), m, folder.fold_expr(e))
@ -662,7 +662,7 @@ pub fn noop_fold_item_underscore<T:ast_fold>(i: &item_, folder: &T) -> item_ {
}
}
pub fn noop_fold_type_method<T:ast_fold>(m: &TypeMethod, fld: &T)
pub fn noop_fold_type_method<T:ast_fold>(m: &TypeMethod, fld: &mut T)
-> TypeMethod {
TypeMethod {
ident: fld.fold_ident(m.ident),
@ -676,7 +676,7 @@ pub fn noop_fold_type_method<T:ast_fold>(m: &TypeMethod, fld: &T)
}
}
pub fn noop_fold_mod<T:ast_fold>(m: &_mod, folder: &T) -> _mod {
pub fn noop_fold_mod<T:ast_fold>(m: &_mod, folder: &mut T) -> _mod {
ast::_mod {
view_items: m.view_items
.iter()
@ -685,7 +685,7 @@ pub fn noop_fold_mod<T:ast_fold>(m: &_mod, folder: &T) -> _mod {
}
}
pub fn noop_fold_crate<T:ast_fold>(c: Crate, folder: &T) -> Crate {
pub fn noop_fold_crate<T:ast_fold>(c: Crate, folder: &mut T) -> Crate {
let fold_meta_item = |x| fold_meta_item_(x, folder);
let fold_attribute = |x| fold_attribute_(x, folder);
@ -697,7 +697,7 @@ pub fn noop_fold_crate<T:ast_fold>(c: Crate, folder: &T) -> Crate {
}
}
pub fn noop_fold_item<T:ast_fold>(i: @ast::item, folder: &T)
pub fn noop_fold_item<T:ast_fold>(i: @ast::item, folder: &mut T)
-> SmallVector<@ast::item> {
let fold_attribute = |x| fold_attribute_(x, folder);
@ -711,7 +711,7 @@ pub fn noop_fold_item<T:ast_fold>(i: @ast::item, folder: &T)
})
}
pub fn noop_fold_expr<T:ast_fold>(e: @ast::Expr, folder: &T) -> @ast::Expr {
pub fn noop_fold_expr<T:ast_fold>(e: @ast::Expr, folder: &mut T) -> @ast::Expr {
let fold_field = |x| fold_field_(x, folder);
let node = match e.node {
@ -719,7 +719,7 @@ pub fn noop_fold_expr<T:ast_fold>(e: @ast::Expr, folder: &T) -> @ast::Expr {
ExprVstore(folder.fold_expr(e), v)
}
ExprVec(ref exprs, mutt) => {
ExprVec(folder.map_exprs(|x| folder.fold_expr(x), *exprs), mutt)
ExprVec(exprs.map(|&x| folder.fold_expr(x)), mutt)
}
ExprRepeat(expr, count, mutt) => {
ExprRepeat(folder.fold_expr(expr), folder.fold_expr(count), mutt)
@ -727,7 +727,7 @@ pub fn noop_fold_expr<T:ast_fold>(e: @ast::Expr, folder: &T) -> @ast::Expr {
ExprTup(ref elts) => ExprTup(elts.map(|x| folder.fold_expr(*x))),
ExprCall(f, ref args, blk) => {
ExprCall(folder.fold_expr(f),
folder.map_exprs(|x| folder.fold_expr(x), *args),
args.map(|&x| folder.fold_expr(x)),
blk)
}
ExprMethodCall(callee_id, f, i, ref tps, ref args, blk) => {
@ -736,7 +736,7 @@ pub fn noop_fold_expr<T:ast_fold>(e: @ast::Expr, folder: &T) -> @ast::Expr {
folder.fold_expr(f),
folder.fold_ident(i),
tps.map(|&x| folder.fold_ty(x)),
folder.map_exprs(|x| folder.fold_expr(x), *args),
args.map(|&x| folder.fold_expr(x)),
blk
)
}
@ -837,7 +837,7 @@ pub fn noop_fold_expr<T:ast_fold>(e: @ast::Expr, folder: &T) -> @ast::Expr {
}
}
pub fn noop_fold_stmt<T:ast_fold>(s: &Stmt, folder: &T) -> SmallVector<@Stmt> {
pub fn noop_fold_stmt<T:ast_fold>(s: &Stmt, folder: &mut T) -> SmallVector<@Stmt> {
let nodes = match s.node {
StmtDecl(d, nid) => {
folder.fold_decl(d).move_iter()
@ -876,7 +876,7 @@ mod test {
struct ToZzIdentFolder;
impl ast_fold for ToZzIdentFolder {
fn fold_ident(&self, _: ast::Ident) -> ast::Ident {
fn fold_ident(&mut self, _: ast::Ident) -> ast::Ident {
token::str_to_ident("zz")
}
}
@ -898,23 +898,23 @@ mod test {
// make sure idents get transformed everywhere
#[test] fn ident_transformation () {
let zz_fold = ToZzIdentFolder;
let mut zz_fold = ToZzIdentFolder;
let ast = string_to_crate(@"#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}");
assert_pred!(matches_codepattern,
"matches_codepattern",
pprust::to_str(&zz_fold.fold_crate(ast),fake_print_crate,
pprust::to_str(&mut zz_fold.fold_crate(ast),fake_print_crate,
token::get_ident_interner()),
~"#[a]mod zz{fn zz(zz:zz,zz:zz){zz!(zz,zz,zz);zz;zz}}");
}
// even inside macro defs....
#[test] fn ident_transformation_in_defs () {
let zz_fold = ToZzIdentFolder;
let mut zz_fold = ToZzIdentFolder;
let ast = string_to_crate(@"macro_rules! a {(b $c:expr $(d $e:token)f+
=> (g $(d $d $e)+))} ");
assert_pred!(matches_codepattern,
"matches_codepattern",
pprust::to_str(&zz_fold.fold_crate(ast),fake_print_crate,
pprust::to_str(&mut zz_fold.fold_crate(ast),fake_print_crate,
token::get_ident_interner()),
~"zz!zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)))");
}