Last active
December 20, 2015 06:39
-
-
Save kemurphy/6087201 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn rewrite_tuple_struct_or_variant_calls(c: &Crate, dm: DefMap) -> @Crate { | |
fn fold_expr(e: &ast::expr_, fld: @fold::ast_fold, dm: DefMap) -> Option<ast::expr_> { | |
let fold_unnamed_field = |x| { | |
ast::Field { | |
ident: special_idents::unnamed_field, | |
expr: fld.fold_expr(x), | |
span: fld.new_span(x.span), | |
} | |
}; | |
let f: @ast::expr; | |
let args: &~[@ast::expr]; | |
match *e { | |
expr_call(f_, ref args_, _) => { | |
f = f_; | |
args = args_; | |
} | |
_ => { return None; } | |
}; | |
let path; | |
match f.node { | |
expr_path(ref p) => { | |
path = p; | |
} | |
_ => { return None; } | |
}; | |
match dm.find(&f.id) { | |
Some(&v) => { | |
match v { | |
ast::def_variant(*) |ast::def_struct(*) => { | |
return Some(expr_struct( | |
fld.fold_path(path), | |
args.map(|x| fold_unnamed_field(*x)), | |
None | |
)); | |
} | |
_ => {} | |
} | |
} | |
_ => {} | |
} | |
None | |
}; | |
let rewriter = @fold::AstFoldFns { | |
fold_expr: |e, s, fld| { | |
let f = fold_expr(e, fld, dm); | |
(fold::noop_fold_expr(if f.is_some() { f.get_ref() } else { e }, fld), s) | |
}, | |
.. *fold::default_ast_fold() | |
}; | |
@fold::make_fold(rewriter).fold_crate(c) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment