librustc: Remove garbage collected functions from front/{config,test} and metadata/{tydecode,tyencode}

This commit is contained in:
Patrick Walton 2013-08-29 15:04:09 -07:00
parent 3e5de06135
commit 33993535ef
4 changed files with 62 additions and 65 deletions

View File

@ -9,14 +9,11 @@
// except according to those terms. // except according to those terms.
use std::option;
use syntax::fold::ast_fold; use syntax::fold::ast_fold;
use syntax::{ast, fold, attr}; use syntax::{ast, fold, attr};
type in_cfg_pred = @fn(attrs: &[ast::Attribute]) -> bool; struct Context<'self> {
in_cfg: &'self fn(attrs: &[ast::Attribute]) -> bool,
struct Context {
in_cfg: in_cfg_pred
} }
// Support conditional compilation by transforming the AST, stripping out // Support conditional compilation by transforming the AST, stripping out
@ -27,56 +24,55 @@ pub fn strip_unconfigured_items(crate: @ast::Crate) -> @ast::Crate {
} }
} }
struct ItemRemover { impl<'self> fold::ast_fold for Context<'self> {
ctxt: @Context,
}
impl fold::ast_fold for ItemRemover {
fn fold_mod(&self, module: &ast::_mod) -> ast::_mod { fn fold_mod(&self, module: &ast::_mod) -> ast::_mod {
fold_mod(self.ctxt, module, self) fold_mod(self, module)
} }
fn fold_block(&self, block: &ast::Block) -> ast::Block { fn fold_block(&self, block: &ast::Block) -> ast::Block {
fold_block(self.ctxt, block, self) fold_block(self, block)
} }
fn fold_foreign_mod(&self, foreign_module: &ast::foreign_mod) fn fold_foreign_mod(&self, foreign_module: &ast::foreign_mod)
-> ast::foreign_mod { -> ast::foreign_mod {
fold_foreign_mod(self.ctxt, foreign_module, self) fold_foreign_mod(self, foreign_module)
} }
fn fold_item_underscore(&self, item: &ast::item_) -> ast::item_ { fn fold_item_underscore(&self, item: &ast::item_) -> ast::item_ {
fold_item_underscore(self.ctxt, item, self) fold_item_underscore(self, item)
} }
} }
pub fn strip_items(crate: &ast::Crate, in_cfg: in_cfg_pred) -> @ast::Crate { pub fn strip_items(crate: &ast::Crate,
let ctxt = @Context { in_cfg: &fn(attrs: &[ast::Attribute]) -> bool)
-> @ast::Crate {
let ctxt = Context {
in_cfg: in_cfg, in_cfg: in_cfg,
}; };
let precursor = ItemRemover { @ctxt.fold_crate(crate)
ctxt: ctxt,
};
@precursor.fold_crate(crate)
} }
fn filter_item(cx: @Context, item: @ast::item) -> fn filter_item(cx: &Context, item: @ast::item) -> Option<@ast::item> {
Option<@ast::item> { if item_in_cfg(cx, item) {
if item_in_cfg(cx, item) { option::Some(item) } else { option::None } Some(item)
}
fn filter_view_item<'r>(cx: @Context, view_item: &'r ast::view_item)-> Option<&'r ast::view_item> {
if view_item_in_cfg(cx, view_item) {
option::Some(view_item)
} else { } else {
option::None None
} }
} }
fn fold_mod(cx: @Context, m: &ast::_mod, fld: &ItemRemover) -> ast::_mod { fn filter_view_item<'r>(cx: &Context, view_item: &'r ast::view_item)
let filtered_items = do m.items.iter().filter_map |a| { -> Option<&'r ast::view_item> {
filter_item(cx, *a).and_then(|x| fld.fold_item(x)) if view_item_in_cfg(cx, view_item) {
Some(view_item)
} else {
None
}
}
fn fold_mod(cx: &Context, m: &ast::_mod) -> ast::_mod {
let filtered_items = do m.items.iter().filter_map |a| {
filter_item(cx, *a).and_then(|x| cx.fold_item(x))
}.collect(); }.collect();
let filtered_view_items = do m.view_items.iter().filter_map |a| { let filtered_view_items = do m.view_items.iter().filter_map |a| {
do filter_view_item(cx, a).map_move |x| { do filter_view_item(cx, a).map_move |x| {
fld.fold_view_item(x) cx.fold_view_item(x)
} }
}.collect(); }.collect();
ast::_mod { ast::_mod {
@ -85,22 +81,23 @@ fn fold_mod(cx: @Context, m: &ast::_mod, fld: &ItemRemover) -> ast::_mod {
} }
} }
fn filter_foreign_item(cx: @Context, item: @ast::foreign_item) -> fn filter_foreign_item(cx: &Context, item: @ast::foreign_item)
Option<@ast::foreign_item> { -> Option<@ast::foreign_item> {
if foreign_item_in_cfg(cx, item) { if foreign_item_in_cfg(cx, item) {
option::Some(item) Some(item)
} else { option::None } } else {
None
}
} }
fn fold_foreign_mod(cx: @Context, nm: &ast::foreign_mod, fld: &ItemRemover) fn fold_foreign_mod(cx: &Context, nm: &ast::foreign_mod) -> ast::foreign_mod {
-> ast::foreign_mod {
let filtered_items = nm.items let filtered_items = nm.items
.iter() .iter()
.filter_map(|a| filter_foreign_item(cx, *a)) .filter_map(|a| filter_foreign_item(cx, *a))
.collect(); .collect();
let filtered_view_items = do nm.view_items.iter().filter_map |a| { let filtered_view_items = do nm.view_items.iter().filter_map |a| {
do filter_view_item(cx, a).map_move |x| { do filter_view_item(cx, a).map_move |x| {
fld.fold_view_item(x) cx.fold_view_item(x)
} }
}.collect(); }.collect();
ast::foreign_mod { ast::foreign_mod {
@ -111,8 +108,7 @@ fn fold_foreign_mod(cx: @Context, nm: &ast::foreign_mod, fld: &ItemRemover)
} }
} }
fn fold_item_underscore(cx: @Context, item: &ast::item_, fld: &ItemRemover) fn fold_item_underscore(cx: &Context, item: &ast::item_) -> ast::item_ {
-> ast::item_ {
let item = match *item { let item = match *item {
ast::item_impl(ref a, ref b, ref c, ref methods) => { ast::item_impl(ref a, ref b, ref c, ref methods) => {
let methods = methods.iter().filter(|m| method_in_cfg(cx, **m)) let methods = methods.iter().filter(|m| method_in_cfg(cx, **m))
@ -120,67 +116,70 @@ fn fold_item_underscore(cx: @Context, item: &ast::item_, fld: &ItemRemover)
ast::item_impl((*a).clone(), (*b).clone(), (*c).clone(), methods) ast::item_impl((*a).clone(), (*b).clone(), (*c).clone(), methods)
} }
ast::item_trait(ref a, ref b, ref methods) => { ast::item_trait(ref a, ref b, ref methods) => {
let methods = methods.iter().filter(|m| trait_method_in_cfg(cx, *m) ) let methods = methods.iter()
.map(|x| (*x).clone()).collect(); .filter(|m| trait_method_in_cfg(cx, *m) )
.map(|x| (*x).clone())
.collect();
ast::item_trait((*a).clone(), (*b).clone(), methods) ast::item_trait((*a).clone(), (*b).clone(), methods)
} }
ref item => (*item).clone(), ref item => (*item).clone(),
}; };
fold::noop_fold_item_underscore(&item, fld) fold::noop_fold_item_underscore(&item, cx)
} }
fn filter_stmt(cx: @Context, stmt: @ast::Stmt) -> fn filter_stmt(cx: &Context, stmt: @ast::Stmt) -> Option<@ast::Stmt> {
Option<@ast::Stmt> {
match stmt.node { match stmt.node {
ast::StmtDecl(decl, _) => { ast::StmtDecl(decl, _) => {
match decl.node { match decl.node {
ast::DeclItem(item) => { ast::DeclItem(item) => {
if item_in_cfg(cx, item) { if item_in_cfg(cx, item) {
option::Some(stmt) Some(stmt)
} else { option::None } } else {
None
}
} }
_ => option::Some(stmt) _ => Some(stmt)
} }
} }
_ => option::Some(stmt) _ => Some(stmt),
} }
} }
fn fold_block(cx: @Context, b: &ast::Block, fld: &ItemRemover) -> ast::Block { fn fold_block(cx: &Context, b: &ast::Block) -> ast::Block {
let resulting_stmts = do b.stmts.iter().filter_map |a| { let resulting_stmts = do b.stmts.iter().filter_map |a| {
filter_stmt(cx, *a).and_then(|stmt| fld.fold_stmt(stmt)) filter_stmt(cx, *a).and_then(|stmt| cx.fold_stmt(stmt))
}.collect(); }.collect();
let filtered_view_items = do b.view_items.iter().filter_map |a| { let filtered_view_items = do b.view_items.iter().filter_map |a| {
filter_view_item(cx, a).map(|x| fld.fold_view_item(*x)) filter_view_item(cx, a).map(|x| cx.fold_view_item(*x))
}.collect(); }.collect();
ast::Block { ast::Block {
view_items: filtered_view_items, view_items: filtered_view_items,
stmts: resulting_stmts, stmts: resulting_stmts,
expr: b.expr.map(|x| fld.fold_expr(*x)), expr: b.expr.map(|x| cx.fold_expr(*x)),
id: b.id, id: b.id,
rules: b.rules, rules: b.rules,
span: b.span, span: b.span,
} }
} }
fn item_in_cfg(cx: @Context, item: @ast::item) -> bool { fn item_in_cfg(cx: &Context, item: @ast::item) -> bool {
return (cx.in_cfg)(item.attrs); return (cx.in_cfg)(item.attrs);
} }
fn foreign_item_in_cfg(cx: @Context, item: @ast::foreign_item) -> bool { fn foreign_item_in_cfg(cx: &Context, item: @ast::foreign_item) -> bool {
return (cx.in_cfg)(item.attrs); return (cx.in_cfg)(item.attrs);
} }
fn view_item_in_cfg(cx: @Context, item: &ast::view_item) -> bool { fn view_item_in_cfg(cx: &Context, item: &ast::view_item) -> bool {
return (cx.in_cfg)(item.attrs); return (cx.in_cfg)(item.attrs);
} }
fn method_in_cfg(cx: @Context, meth: @ast::method) -> bool { fn method_in_cfg(cx: &Context, meth: @ast::method) -> bool {
return (cx.in_cfg)(meth.attrs); return (cx.in_cfg)(meth.attrs);
} }
fn trait_method_in_cfg(cx: @Context, meth: &ast::trait_method) -> bool { fn trait_method_in_cfg(cx: &Context, meth: &ast::trait_method) -> bool {
match *meth { match *meth {
ast::required(ref meth) => (cx.in_cfg)(meth.attrs), ast::required(ref meth) => (cx.in_cfg)(meth.attrs),
ast::provided(@ref meth) => (cx.in_cfg)(meth.attrs) ast::provided(@ref meth) => (cx.in_cfg)(meth.attrs)
@ -192,3 +191,4 @@ fn trait_method_in_cfg(cx: @Context, meth: &ast::trait_method) -> bool {
fn in_cfg(cfg: &[@ast::MetaItem], attrs: &[ast::Attribute]) -> bool { fn in_cfg(cfg: &[@ast::MetaItem], attrs: &[ast::Attribute]) -> bool {
attr::test_cfg(cfg, attrs.iter().map(|x| *x)) attr::test_cfg(cfg, attrs.iter().map(|x| *x))
} }

View File

@ -27,8 +27,6 @@ use syntax::opt_vec;
use syntax::print::pprust; use syntax::print::pprust;
use syntax::{ast, ast_util}; use syntax::{ast, ast_util};
type node_id_gen = @fn() -> ast::NodeId;
struct Test { struct Test {
span: Span, span: Span,
path: ~[ast::Ident], path: ~[ast::Ident],

View File

@ -95,8 +95,7 @@ pub fn parse_ident(st: &mut PState, last: char) -> ast::Ident {
return parse_ident_(st, |a| is_last(last, a) ); return parse_ident_(st, |a| is_last(last, a) );
} }
fn parse_ident_(st: &mut PState, is_last: @fn(char) -> bool) -> fn parse_ident_(st: &mut PState, is_last: &fn(char) -> bool) -> ast::Ident {
ast::Ident {
let rslt = scan(st, is_last, str::from_utf8); let rslt = scan(st, is_last, str::from_utf8);
return st.tcx.sess.ident_of(rslt); return st.tcx.sess.ident_of(rslt);
} }

View File

@ -26,7 +26,7 @@ use syntax::print::pprust::*;
pub struct ctxt { pub struct ctxt {
diag: @mut span_handler, diag: @mut span_handler,
// Def -> str Callback: // Def -> str Callback:
ds: @fn(DefId) -> ~str, ds: extern "Rust" fn(DefId) -> ~str,
// The type context. // The type context.
tcx: ty::ctxt, tcx: ty::ctxt,
abbrevs: abbrev_ctxt abbrevs: abbrev_ctxt