2011-07-05 19:02:51 -05:00
|
|
|
import std::ivec;
|
2011-06-30 00:32:08 -05:00
|
|
|
import std::option;
|
2011-07-05 04:48:19 -05:00
|
|
|
import syntax::ast;
|
|
|
|
import syntax::fold;
|
2011-06-30 00:32:08 -05:00
|
|
|
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
|
|
|
|
fn strip_unconfigured_items(@ast::crate crate) -> @ast::crate {
|
|
|
|
auto cfg = crate.node.config;
|
|
|
|
|
2011-06-30 15:04:02 -05:00
|
|
|
auto precursor = rec(fold_mod = bind fold_mod(cfg, _, _),
|
2011-07-05 15:29:21 -05:00
|
|
|
fold_block = bind fold_block(cfg, _, _),
|
|
|
|
fold_native_mod = bind fold_native_mod(cfg, _, _)
|
2011-06-30 00:32:08 -05:00
|
|
|
with *fold::default_ast_fold());
|
|
|
|
|
|
|
|
auto fold = fold::make_fold(precursor);
|
|
|
|
auto res = @fold.fold_crate(*crate);
|
|
|
|
// FIXME: This is necessary to break a circular reference
|
|
|
|
fold::dummy_out(fold);
|
|
|
|
ret res;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn filter_item(&ast::crate_cfg cfg,
|
|
|
|
&@ast::item item) -> option::t[@ast::item] {
|
2011-07-05 15:29:21 -05:00
|
|
|
if (item_in_cfg(cfg, item)) {
|
2011-06-30 00:32:08 -05:00
|
|
|
option::some(item)
|
|
|
|
} else {
|
|
|
|
option::none
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_mod(&ast::crate_cfg cfg, &ast::_mod m,
|
|
|
|
fold::ast_fold fld) -> ast::_mod {
|
|
|
|
auto filter = bind filter_item(cfg, _);
|
2011-07-07 17:31:54 -05:00
|
|
|
auto filtered_items = ivec::filter_map(filter, m.items);
|
|
|
|
ret rec(view_items=ivec::map(fld.fold_view_item, m.view_items),
|
|
|
|
items=ivec::map(fld.fold_item, filtered_items));
|
2011-06-30 00:32:08 -05:00
|
|
|
}
|
|
|
|
|
2011-07-05 15:29:21 -05:00
|
|
|
fn filter_native_item(&ast::crate_cfg cfg, &@ast::native_item item)
|
|
|
|
-> option::t[@ast::native_item] {
|
|
|
|
if (native_item_in_cfg(cfg, item)) {
|
|
|
|
option::some(item)
|
|
|
|
} else {
|
|
|
|
option::none
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_native_mod(&ast::crate_cfg cfg, &ast::native_mod nm,
|
|
|
|
fold::ast_fold fld) -> ast::native_mod {
|
|
|
|
auto filter = bind filter_native_item(cfg, _);
|
2011-07-07 17:31:54 -05:00
|
|
|
auto filtered_items = ivec::filter_map(filter, nm.items);
|
2011-07-05 15:29:21 -05:00
|
|
|
ret rec(native_name=nm.native_name,
|
|
|
|
abi=nm.abi,
|
2011-07-07 17:31:54 -05:00
|
|
|
view_items=ivec::map(fld.fold_view_item, nm.view_items),
|
2011-07-05 15:29:21 -05:00
|
|
|
items=filtered_items);
|
|
|
|
}
|
|
|
|
|
2011-06-30 15:04:02 -05:00
|
|
|
fn filter_stmt(&ast::crate_cfg cfg,
|
|
|
|
&@ast::stmt stmt) -> option::t[@ast::stmt] {
|
|
|
|
alt (stmt.node) {
|
|
|
|
case (ast::stmt_decl(?decl, _)) {
|
|
|
|
alt (decl.node) {
|
|
|
|
case (ast::decl_item(?item)) {
|
2011-07-05 15:29:21 -05:00
|
|
|
if (item_in_cfg(cfg, item)) {
|
2011-06-30 15:04:02 -05:00
|
|
|
option::some(stmt)
|
|
|
|
} else {
|
|
|
|
option::none
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case (_) { option::some(stmt) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case (_) { option::some(stmt) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-25 16:16:12 -05:00
|
|
|
fn fold_block(&ast::crate_cfg cfg, &ast::blk_ b,
|
|
|
|
fold::ast_fold fld) -> ast::blk_ {
|
2011-06-30 15:04:02 -05:00
|
|
|
auto filter = bind filter_stmt(cfg, _);
|
2011-07-06 21:00:00 -05:00
|
|
|
auto filtered_stmts = ivec::filter_map(filter, b.stmts);
|
|
|
|
ret rec(stmts=ivec::map(fld.fold_stmt, filtered_stmts),
|
2011-06-30 15:04:02 -05:00
|
|
|
expr=option::map(fld.fold_expr, b.expr),
|
|
|
|
id=b.id);
|
|
|
|
}
|
|
|
|
|
2011-07-05 15:29:21 -05:00
|
|
|
fn item_in_cfg(&ast::crate_cfg cfg, &@ast::item item) -> bool {
|
|
|
|
ret in_cfg(cfg, item.attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn native_item_in_cfg(&ast::crate_cfg cfg, &@ast::native_item item) -> bool {
|
|
|
|
ret in_cfg(cfg, item.attrs);
|
|
|
|
}
|
|
|
|
|
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
|
2011-07-05 19:02:51 -05:00
|
|
|
fn in_cfg(&ast::crate_cfg cfg, &ast::attribute[] attrs) -> bool {
|
2011-06-30 00:32:08 -05:00
|
|
|
|
2011-06-30 16:12:11 -05:00
|
|
|
// The "cfg" attributes on the item
|
2011-07-05 15:29:21 -05:00
|
|
|
auto item_cfg_attrs = attr::find_attrs_by_name(attrs, "cfg");
|
2011-07-05 19:02:51 -05:00
|
|
|
auto item_has_cfg_attrs = ivec::len(item_cfg_attrs) > 0u;
|
2011-06-30 00:32:08 -05:00
|
|
|
if (!item_has_cfg_attrs) { ret true; }
|
|
|
|
|
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
|
|
|
|
auto item_cfg_metas = {
|
2011-07-05 19:57:34 -05:00
|
|
|
fn extract_metas(&(@ast::meta_item)[] inner_items,
|
2011-06-30 16:12:11 -05:00
|
|
|
&@ast::meta_item cfg_item)
|
2011-07-05 19:57:34 -05:00
|
|
|
-> (@ast::meta_item)[] {
|
2011-06-30 16:12:11 -05:00
|
|
|
|
|
|
|
alt (cfg_item.node) {
|
|
|
|
case (ast::meta_list(?name, ?items)) {
|
|
|
|
assert name == "cfg";
|
|
|
|
inner_items + items
|
|
|
|
}
|
|
|
|
case (_) { inner_items }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto cfg_metas = attr::attr_metas(item_cfg_attrs);
|
2011-07-05 19:57:34 -05:00
|
|
|
ivec::foldl(extract_metas, ~[], cfg_metas)
|
2011-06-30 16:12:11 -05:00
|
|
|
};
|
2011-06-30 00:32:08 -05:00
|
|
|
|
|
|
|
for (@ast::meta_item cfg_mi in item_cfg_metas) {
|
2011-07-05 19:57:34 -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
|
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|