2012-03-22 20:16:22 -05:00
|
|
|
import syntax::{ast, fold, attr};
|
2011-06-30 00:32:08 -05:00
|
|
|
|
|
|
|
export strip_unconfigured_items;
|
2011-10-29 19:35:45 -05:00
|
|
|
export metas_in_cfg;
|
2012-01-05 19:30:00 -06:00
|
|
|
export strip_items;
|
|
|
|
|
2012-06-25 22:00:46 -05:00
|
|
|
type in_cfg_pred = fn@([ast::attribute]/~) -> bool;
|
2012-01-05 19:30:00 -06:00
|
|
|
|
|
|
|
type ctxt = @{
|
|
|
|
in_cfg: in_cfg_pred
|
|
|
|
};
|
2011-06-30 00:32:08 -05:00
|
|
|
|
|
|
|
// Support conditional compilation by transforming the AST, stripping out
|
|
|
|
// any items that do not belong in the current configuration
|
2011-07-27 07:19:39 -05:00
|
|
|
fn strip_unconfigured_items(crate: @ast::crate) -> @ast::crate {
|
2012-01-05 19:30:00 -06:00
|
|
|
strip_items(crate) {|attrs|
|
|
|
|
in_cfg(crate.node.config, attrs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn strip_items(crate: @ast::crate, in_cfg: in_cfg_pred)
|
|
|
|
-> @ast::crate {
|
|
|
|
|
|
|
|
let ctxt = @{in_cfg: in_cfg};
|
2011-06-30 00:32:08 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let precursor =
|
2012-06-19 21:34:01 -05:00
|
|
|
@{fold_mod: {|a,b|fold_mod(ctxt, a, b)},
|
|
|
|
fold_block: fold::wrap({|a,b|fold_block(ctxt, a, b)}),
|
|
|
|
fold_native_mod: {|a,b|fold_native_mod(ctxt, a, b)}
|
2012-05-21 20:28:39 -05:00
|
|
|
with *fold::default_ast_fold()};
|
2011-06-30 00:32:08 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let fold = fold::make_fold(precursor);
|
|
|
|
let res = @fold.fold_crate(*crate);
|
2011-06-30 00:32:08 -05:00
|
|
|
ret res;
|
|
|
|
}
|
|
|
|
|
2012-01-05 19:30:00 -06:00
|
|
|
fn filter_item(cx: ctxt, &&item: @ast::item) ->
|
2012-01-31 19:05:20 -06:00
|
|
|
option<@ast::item> {
|
2012-01-05 19:30:00 -06:00
|
|
|
if item_in_cfg(cx, item) { option::some(item) } else { option::none }
|
2011-06-30 00:32:08 -05:00
|
|
|
}
|
|
|
|
|
2012-01-05 19:30:00 -06:00
|
|
|
fn fold_mod(cx: ctxt, m: ast::_mod, fld: fold::ast_fold) ->
|
2011-07-27 07:19:39 -05:00
|
|
|
ast::_mod {
|
2012-06-19 21:34:01 -05:00
|
|
|
let filter = {|a|filter_item(cx, a)};
|
2011-12-16 08:27:50 -06:00
|
|
|
let filtered_items = vec::filter_map(m.items, filter);
|
|
|
|
ret {view_items: vec::map(m.view_items, fld.fold_view_item),
|
|
|
|
items: vec::map(filtered_items, fld.fold_item)};
|
2011-06-30 00:32:08 -05:00
|
|
|
}
|
|
|
|
|
2012-01-05 19:30:00 -06:00
|
|
|
fn filter_native_item(cx: ctxt, &&item: @ast::native_item) ->
|
2012-01-31 19:05:20 -06:00
|
|
|
option<@ast::native_item> {
|
2012-01-05 19:30:00 -06:00
|
|
|
if native_item_in_cfg(cx, item) {
|
2011-07-05 15:29:21 -05:00
|
|
|
option::some(item)
|
2011-07-27 07:19:39 -05:00
|
|
|
} else { option::none }
|
2011-07-05 15:29:21 -05:00
|
|
|
}
|
|
|
|
|
2012-01-05 19:30:00 -06:00
|
|
|
fn fold_native_mod(cx: ctxt, nm: ast::native_mod,
|
2011-07-27 07:19:39 -05:00
|
|
|
fld: fold::ast_fold) -> ast::native_mod {
|
2012-06-19 21:34:01 -05:00
|
|
|
let filter = {|a|filter_native_item(cx, a)};
|
2011-12-16 08:27:50 -06:00
|
|
|
let filtered_items = vec::filter_map(nm.items, filter);
|
|
|
|
ret {view_items: vec::map(nm.view_items, fld.fold_view_item),
|
2011-07-27 07:19:39 -05:00
|
|
|
items: filtered_items};
|
2011-07-05 15:29:21 -05:00
|
|
|
}
|
|
|
|
|
2012-01-05 19:30:00 -06:00
|
|
|
fn filter_stmt(cx: ctxt, &&stmt: @ast::stmt) ->
|
2012-01-31 19:05:20 -06:00
|
|
|
option<@ast::stmt> {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt stmt.node {
|
|
|
|
ast::stmt_decl(decl, _) {
|
|
|
|
alt decl.node {
|
|
|
|
ast::decl_item(item) {
|
2012-01-05 19:30:00 -06:00
|
|
|
if item_in_cfg(cx, item) {
|
2011-07-27 07:19:39 -05:00
|
|
|
option::some(stmt)
|
|
|
|
} else { option::none }
|
|
|
|
}
|
|
|
|
_ { option::some(stmt) }
|
2011-06-30 15:04:02 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
_ { option::some(stmt) }
|
2011-06-30 15:04:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 19:30:00 -06:00
|
|
|
fn fold_block(cx: ctxt, b: ast::blk_, fld: fold::ast_fold) ->
|
2011-07-27 07:19:39 -05:00
|
|
|
ast::blk_ {
|
2012-06-19 21:34:01 -05:00
|
|
|
let filter = {|a|filter_stmt(cx, a)};
|
2011-12-16 08:27:50 -06:00
|
|
|
let filtered_stmts = vec::filter_map(b.stmts, filter);
|
2011-11-23 13:57:34 -06:00
|
|
|
ret {view_items: b.view_items,
|
2011-12-16 08:27:50 -06:00
|
|
|
stmts: vec::map(filtered_stmts, fld.fold_stmt),
|
|
|
|
expr: option::map(b.expr, fld.fold_expr),
|
2011-09-02 17:34:58 -05:00
|
|
|
id: b.id,
|
|
|
|
rules: b.rules};
|
2011-06-30 15:04:02 -05:00
|
|
|
}
|
|
|
|
|
2012-01-05 19:30:00 -06:00
|
|
|
fn item_in_cfg(cx: ctxt, item: @ast::item) -> bool {
|
|
|
|
ret cx.in_cfg(item.attrs);
|
2011-07-05 15:29:21 -05:00
|
|
|
}
|
|
|
|
|
2012-01-05 19:30:00 -06:00
|
|
|
fn native_item_in_cfg(cx: ctxt, item: @ast::native_item) -> bool {
|
|
|
|
ret cx.in_cfg(item.attrs);
|
2011-07-05 15:29:21 -05:00
|
|
|
}
|
|
|
|
|
2011-06-30 00:32:08 -05:00
|
|
|
// Determine if an item should be translated in the current crate
|
|
|
|
// configuration based on the item's attributes
|
2012-06-25 22:00:46 -05:00
|
|
|
fn in_cfg(cfg: ast::crate_cfg, attrs: [ast::attribute]/~) -> bool {
|
2011-10-29 19:35:45 -05:00
|
|
|
metas_in_cfg(cfg, attr::attr_metas(attrs))
|
|
|
|
}
|
|
|
|
|
2012-06-25 22:00:46 -05:00
|
|
|
fn metas_in_cfg(cfg: ast::crate_cfg, metas: [@ast::meta_item]/~) -> bool {
|
2011-06-30 00:32:08 -05:00
|
|
|
|
2011-06-30 16:12:11 -05:00
|
|
|
// The "cfg" attributes on the item
|
2011-10-29 19:35:45 -05:00
|
|
|
let cfg_metas = attr::find_meta_items_by_name(metas, "cfg");
|
2011-06-30 00:32:08 -05:00
|
|
|
|
2011-06-30 16:12:11 -05:00
|
|
|
// Pull the inner meta_items from the #[cfg(meta_item, ...)] attributes,
|
|
|
|
// so we can match against them. This is the list of configurations for
|
|
|
|
// which the item is valid
|
2011-12-16 08:27:50 -06:00
|
|
|
let cfg_metas = vec::concat(vec::filter_map(cfg_metas,
|
|
|
|
{|&&i| attr::get_meta_item_list(i)}));
|
2011-10-29 19:35:45 -05:00
|
|
|
|
|
|
|
let has_cfg_metas = vec::len(cfg_metas) > 0u;
|
|
|
|
if !has_cfg_metas { ret true; }
|
2011-06-30 00:32:08 -05:00
|
|
|
|
2012-04-06 13:01:43 -05:00
|
|
|
for cfg_metas.each {|cfg_mi|
|
2011-07-27 07:19:39 -05:00
|
|
|
if attr::contains(cfg, cfg_mi) { ret true; }
|
2011-06-30 00:32:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Local Variables:
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|