rust/src/comp/front/config.rs

130 lines
4.0 KiB
Rust
Raw Normal View History

2011-09-12 18:13:28 -05:00
import std::{vec, option};
import syntax::{ast, fold};
import attr;
export strip_unconfigured_items;
// 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 {
let cfg = crate.node.config;
2011-07-27 07:19:39 -05: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-07-27 07:19:39 -05:00
let fold = fold::make_fold(precursor);
let res = @fold.fold_crate(*crate);
ret res;
}
fn filter_item(cfg: ast::crate_cfg, &&item: @ast::item) ->
option::t<@ast::item> {
2011-07-27 07:19:39 -05:00
if item_in_cfg(cfg, item) { option::some(item) } else { option::none }
}
fn fold_mod(cfg: ast::crate_cfg, m: ast::_mod, fld: fold::ast_fold) ->
2011-07-27 07:19:39 -05:00
ast::_mod {
let filter = bind filter_item(cfg, _);
2011-08-15 18:38:23 -05:00
let filtered_items = vec::filter_map(filter, m.items);
ret {view_items: vec::map(fld.fold_view_item, m.view_items),
items: vec::map(fld.fold_item, filtered_items)};
}
fn filter_native_item(cfg: ast::crate_cfg, &&item: @ast::native_item) ->
option::t<@ast::native_item> {
2011-07-27 07:19:39 -05:00
if native_item_in_cfg(cfg, item) {
option::some(item)
2011-07-27 07:19:39 -05:00
} else { option::none }
}
fn fold_native_mod(cfg: ast::crate_cfg, nm: ast::native_mod,
2011-07-27 07:19:39 -05:00
fld: fold::ast_fold) -> ast::native_mod {
let filter = bind filter_native_item(cfg, _);
2011-08-15 18:38:23 -05:00
let filtered_items = vec::filter_map(filter, nm.items);
2011-07-27 07:19:39 -05:00
ret {native_name: nm.native_name,
abi: nm.abi,
view_items: vec::map(fld.fold_view_item, nm.view_items),
2011-07-27 07:19:39 -05:00
items: filtered_items};
}
fn filter_stmt(cfg: ast::crate_cfg, &&stmt: @ast::stmt) ->
option::t<@ast::stmt> {
2011-07-27 07:19:39 -05: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-07-27 07:19:39 -05:00
}
_ { option::some(stmt) }
}
}
fn fold_block(cfg: ast::crate_cfg, b: ast::blk_, fld: fold::ast_fold) ->
2011-07-27 07:19:39 -05:00
ast::blk_ {
let filter = bind filter_stmt(cfg, _);
2011-08-15 18:38:23 -05:00
let filtered_stmts = vec::filter_map(filter, b.stmts);
ret {stmts: vec::map(fld.fold_stmt, filtered_stmts),
expr: option::map(fld.fold_expr, b.expr),
2011-09-02 17:34:58 -05:00
id: b.id,
rules: b.rules};
}
fn item_in_cfg(cfg: ast::crate_cfg, item: @ast::item) -> bool {
ret in_cfg(cfg, item.attrs);
}
fn native_item_in_cfg(cfg: ast::crate_cfg, item: @ast::native_item) -> bool {
ret in_cfg(cfg, item.attrs);
}
// Determine if an item should be translated in the current crate
// configuration based on the item's attributes
fn in_cfg(cfg: ast::crate_cfg, attrs: [ast::attribute]) -> bool {
// The "cfg" attributes on the item
2011-09-02 17:34:58 -05:00
let item_cfg_attrs = attr::find_attrs_by_name(attrs, "cfg");
2011-08-15 18:38:23 -05:00
let item_has_cfg_attrs = vec::len(item_cfg_attrs) > 0u;
2011-07-27 07:19:39 -05:00
if !item_has_cfg_attrs { ret true; }
// 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
let item_cfg_metas = {
fn extract_metas(&&inner_items: [@ast::meta_item],
&&cfg_item: @ast::meta_item) -> [@ast::meta_item] {
alt cfg_item.node {
ast::meta_list(name, items) {
assert (name == "cfg");
inner_items + items
}
_ { inner_items }
}
}
let cfg_metas = attr::attr_metas(item_cfg_attrs);
vec::foldl(extract_metas, [], cfg_metas)
};
for cfg_mi: @ast::meta_item in item_cfg_metas {
2011-07-27 07:19:39 -05:00
if attr::contains(cfg, cfg_mi) { ret true; }
}
ret false;
}
// Local Variables:
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End: