2011-12-14 00:25:51 +00:00
|
|
|
import core::{vec, option};
|
2011-09-12 23:13:28 +00:00
|
|
|
import syntax::{ast, fold};
|
2011-06-30 05:32:08 +00:00
|
|
|
import attr;
|
|
|
|
|
|
|
|
export strip_unconfigured_items;
|
2011-10-30 00:35:45 +00:00
|
|
|
export metas_in_cfg;
|
2011-06-30 05:32:08 +00:00
|
|
|
|
|
|
|
// Support conditional compilation by transforming the AST, stripping out
|
|
|
|
// any items that do not belong in the current configuration
|
2011-07-27 12:19:39 +00:00
|
|
|
fn strip_unconfigured_items(crate: @ast::crate) -> @ast::crate {
|
|
|
|
let cfg = crate.node.config;
|
2011-06-30 05:32:08 +00:00
|
|
|
|
2011-07-27 12:19:39 +00:00
|
|
|
let precursor =
|
|
|
|
{fold_mod: bind fold_mod(cfg, _, _),
|
|
|
|
fold_block: bind fold_block(cfg, _, _),
|
|
|
|
fold_native_mod: bind fold_native_mod(cfg, _, _)
|
|
|
|
with *fold::default_ast_fold()};
|
2011-06-30 05:32:08 +00:00
|
|
|
|
2011-07-27 12:19:39 +00:00
|
|
|
let fold = fold::make_fold(precursor);
|
|
|
|
let res = @fold.fold_crate(*crate);
|
2011-06-30 05:32:08 +00:00
|
|
|
ret res;
|
|
|
|
}
|
|
|
|
|
2011-10-06 10:26:12 +00:00
|
|
|
fn filter_item(cfg: ast::crate_cfg, &&item: @ast::item) ->
|
2011-08-12 14:15:18 +00:00
|
|
|
option::t<@ast::item> {
|
2011-07-27 12:19:39 +00:00
|
|
|
if item_in_cfg(cfg, item) { option::some(item) } else { option::none }
|
2011-06-30 05:32:08 +00:00
|
|
|
}
|
|
|
|
|
2011-09-12 09:27:30 +00:00
|
|
|
fn fold_mod(cfg: ast::crate_cfg, m: ast::_mod, fld: fold::ast_fold) ->
|
2011-07-27 12:19:39 +00:00
|
|
|
ast::_mod {
|
|
|
|
let filter = bind filter_item(cfg, _);
|
2011-12-16 14:27:50 +00: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 05:32:08 +00:00
|
|
|
}
|
|
|
|
|
2011-10-06 10:26:12 +00:00
|
|
|
fn filter_native_item(cfg: ast::crate_cfg, &&item: @ast::native_item) ->
|
2011-08-12 14:15:18 +00:00
|
|
|
option::t<@ast::native_item> {
|
2011-07-27 12:19:39 +00:00
|
|
|
if native_item_in_cfg(cfg, item) {
|
2011-07-05 20:29:21 +00:00
|
|
|
option::some(item)
|
2011-07-27 12:19:39 +00:00
|
|
|
} else { option::none }
|
2011-07-05 20:29:21 +00:00
|
|
|
}
|
|
|
|
|
2011-09-12 09:27:30 +00:00
|
|
|
fn fold_native_mod(cfg: ast::crate_cfg, nm: ast::native_mod,
|
2011-07-27 12:19:39 +00:00
|
|
|
fld: fold::ast_fold) -> ast::native_mod {
|
|
|
|
let filter = bind filter_native_item(cfg, _);
|
2011-12-16 14:27:50 +00: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 12:19:39 +00:00
|
|
|
items: filtered_items};
|
2011-07-05 20:29:21 +00:00
|
|
|
}
|
|
|
|
|
2011-10-06 10:26:12 +00:00
|
|
|
fn filter_stmt(cfg: ast::crate_cfg, &&stmt: @ast::stmt) ->
|
2011-08-12 14:15:18 +00:00
|
|
|
option::t<@ast::stmt> {
|
2011-07-27 12:19:39 +00:00
|
|
|
alt stmt.node {
|
|
|
|
ast::stmt_decl(decl, _) {
|
|
|
|
alt decl.node {
|
|
|
|
ast::decl_item(item) {
|
|
|
|
if item_in_cfg(cfg, item) {
|
|
|
|
option::some(stmt)
|
|
|
|
} else { option::none }
|
|
|
|
}
|
|
|
|
_ { option::some(stmt) }
|
2011-06-30 20:04:02 +00:00
|
|
|
}
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
|
|
|
_ { option::some(stmt) }
|
2011-06-30 20:04:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 09:27:30 +00:00
|
|
|
fn fold_block(cfg: ast::crate_cfg, b: ast::blk_, fld: fold::ast_fold) ->
|
2011-07-27 12:19:39 +00:00
|
|
|
ast::blk_ {
|
|
|
|
let filter = bind filter_stmt(cfg, _);
|
2011-12-16 14:27:50 +00:00
|
|
|
let filtered_stmts = vec::filter_map(b.stmts, filter);
|
2011-11-23 19:57:34 +00:00
|
|
|
ret {view_items: b.view_items,
|
2011-12-16 14:27:50 +00:00
|
|
|
stmts: vec::map(filtered_stmts, fld.fold_stmt),
|
|
|
|
expr: option::map(b.expr, fld.fold_expr),
|
2011-09-02 22:34:58 +00:00
|
|
|
id: b.id,
|
|
|
|
rules: b.rules};
|
2011-06-30 20:04:02 +00:00
|
|
|
}
|
|
|
|
|
2011-09-12 09:27:30 +00:00
|
|
|
fn item_in_cfg(cfg: ast::crate_cfg, item: @ast::item) -> bool {
|
2011-07-05 20:29:21 +00:00
|
|
|
ret in_cfg(cfg, item.attrs);
|
|
|
|
}
|
|
|
|
|
2011-09-12 09:27:30 +00:00
|
|
|
fn native_item_in_cfg(cfg: ast::crate_cfg, item: @ast::native_item) -> bool {
|
2011-07-05 20:29:21 +00:00
|
|
|
ret in_cfg(cfg, item.attrs);
|
|
|
|
}
|
|
|
|
|
2011-06-30 05:32:08 +00:00
|
|
|
// Determine if an item should be translated in the current crate
|
|
|
|
// configuration based on the item's attributes
|
2011-09-12 09:27:30 +00:00
|
|
|
fn in_cfg(cfg: ast::crate_cfg, attrs: [ast::attribute]) -> bool {
|
2011-10-30 00:35:45 +00:00
|
|
|
metas_in_cfg(cfg, attr::attr_metas(attrs))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn metas_in_cfg(cfg: ast::crate_cfg, metas: [@ast::meta_item]) -> bool {
|
2011-06-30 05:32:08 +00:00
|
|
|
|
2011-06-30 21:12:11 +00:00
|
|
|
// The "cfg" attributes on the item
|
2011-10-30 00:35:45 +00:00
|
|
|
let cfg_metas = attr::find_meta_items_by_name(metas, "cfg");
|
2011-06-30 05:32:08 +00:00
|
|
|
|
2011-06-30 21:12:11 +00: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 14:27:50 +00:00
|
|
|
let cfg_metas = vec::concat(vec::filter_map(cfg_metas,
|
|
|
|
{|&&i| attr::get_meta_item_list(i)}));
|
2011-10-30 00:35:45 +00:00
|
|
|
|
|
|
|
let has_cfg_metas = vec::len(cfg_metas) > 0u;
|
|
|
|
if !has_cfg_metas { ret true; }
|
2011-06-30 05:32:08 +00:00
|
|
|
|
2011-10-30 00:35:45 +00:00
|
|
|
for cfg_mi: @ast::meta_item in cfg_metas {
|
2011-07-27 12:19:39 +00:00
|
|
|
if attr::contains(cfg, cfg_mi) { ret true; }
|
2011-06-30 05:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Local Variables:
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|