rust/src/librustc/front/config.rs

197 lines
6.1 KiB
Rust
Raw Normal View History

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2012-09-04 13:54:36 -05:00
use syntax::{ast, fold, attr};
type in_cfg_pred = @fn(attrs: ~[ast::attribute]) -> bool;
struct Context {
in_cfg: in_cfg_pred
}
// Support conditional compilation by transforming the AST, stripping out
// any items that do not belong in the current configuration
pub fn strip_unconfigured_items(crate: @ast::crate) -> @ast::crate {
2012-06-30 18:19:07 -05:00
do strip_items(crate) |attrs| {
in_cfg(/*bad*/copy crate.node.config, attrs)
}
}
pub fn strip_items(crate: @ast::crate, in_cfg: in_cfg_pred)
-> @ast::crate {
let ctxt = @Context { in_cfg: in_cfg };
let precursor = @fold::AstFoldFns {
fold_mod: |a,b| fold_mod(ctxt, a, b),
2012-06-30 18:19:07 -05:00
fold_block: fold::wrap(|a,b| fold_block(ctxt, a, b) ),
2012-09-04 15:29:32 -05:00
fold_foreign_mod: |a,b| fold_foreign_mod(ctxt, a, b),
fold_item_underscore: |a,b| {
// Bad copy.
fold_item_underscore(ctxt, copy a, b)
},
2012-09-04 15:29:32 -05:00
.. *fold::default_ast_fold()};
2011-07-27 07:19:39 -05:00
let fold = fold::make_fold(precursor);
let res = @fold.fold_crate(&*crate);
2012-08-01 19:30:05 -05:00
return res;
}
fn filter_item(cx: @Context, item: @ast::item) ->
2012-08-20 14:23:37 -05:00
Option<@ast::item> {
if item_in_cfg(cx, item) { option::Some(item) } else { option::None }
}
fn filter_view_item(cx: @Context, view_item: @ast::view_item
2012-08-20 14:23:37 -05:00
)-> Option<@ast::view_item> {
if view_item_in_cfg(cx, view_item) {
2012-08-20 14:23:37 -05:00
option::Some(view_item)
} else {
2012-08-20 14:23:37 -05:00
option::None
}
}
fn fold_mod(cx: @Context, m: &ast::_mod, fld: @fold::ast_fold) -> ast::_mod {
let filtered_items =
m.items.filter_mapped(|a| filter_item(cx, *a));
let filtered_view_items =
m.view_items.filter_mapped(|a| filter_view_item(cx, *a));
ast::_mod {
view_items: filtered_view_items.map(|x| fld.fold_view_item(*x)),
items: vec::filter_map(filtered_items, |x| fld.fold_item(x))
}
}
fn filter_foreign_item(cx: @Context, item: @ast::foreign_item) ->
2012-08-20 14:23:37 -05:00
Option<@ast::foreign_item> {
if foreign_item_in_cfg(cx, item) {
2012-08-20 14:23:37 -05:00
option::Some(item)
} else { option::None }
}
fn fold_foreign_mod(
cx: @Context,
nm: &ast::foreign_mod,
fld: @fold::ast_fold
) -> ast::foreign_mod {
let filtered_items =
nm.items.filter_mapped(|a| filter_foreign_item(cx, *a));
let filtered_view_items =
nm.view_items.filter_mapped(|a| filter_view_item(cx, *a));
ast::foreign_mod {
sort: nm.sort,
abis: nm.abis,
view_items: vec::map(filtered_view_items, |x| fld.fold_view_item(*x)),
2012-08-01 19:30:05 -05:00
items: filtered_items
}
}
fn fold_item_underscore(cx: @Context, item: &ast::item_,
fld: @fold::ast_fold) -> ast::item_ {
let item = match *item {
ast::item_impl(ref a, b, c, ref methods) => {
let methods = methods.filtered(|m| method_in_cfg(cx, *m) );
ast::item_impl(/*bad*/ copy *a, b, c, methods)
2012-11-01 17:48:37 -05:00
}
ast::item_trait(ref a, ref b, ref methods) => {
let methods = methods.filtered(|m| trait_method_in_cfg(cx, m) );
ast::item_trait(/*bad*/copy *a, /*bad*/copy *b, methods)
2012-11-01 17:48:37 -05:00
}
ref item => /*bad*/ copy *item
2012-11-01 17:48:37 -05:00
};
fold::noop_fold_item_underscore(&item, fld)
2012-11-01 17:48:37 -05:00
}
fn filter_stmt(cx: @Context, stmt: @ast::stmt) ->
2012-08-20 14:23:37 -05:00
Option<@ast::stmt> {
2012-08-06 14:34:08 -05:00
match stmt.node {
2012-08-03 21:59:04 -05:00
ast::stmt_decl(decl, _) => {
2012-08-06 14:34:08 -05:00
match decl.node {
2012-08-03 21:59:04 -05:00
ast::decl_item(item) => {
if item_in_cfg(cx, item) {
2012-08-20 14:23:37 -05:00
option::Some(stmt)
} else { option::None }
2011-07-27 07:19:39 -05:00
}
2012-08-20 14:23:37 -05:00
_ => option::Some(stmt)
}
2011-07-27 07:19:39 -05:00
}
2012-08-20 14:23:37 -05:00
_ => option::Some(stmt)
}
}
fn fold_block(
cx: @Context,
b: &ast::blk_,
fld: @fold::ast_fold
) -> ast::blk_ {
let filtered_stmts =
b.stmts.filter_mapped(|a| filter_stmt(cx, *a));
ast::blk_ {
view_items: /*bad*/copy b.view_items,
stmts: vec::map(filtered_stmts, |x| fld.fold_stmt(*x)),
expr: b.expr.map(|x| fld.fold_expr(*x)),
id: b.id,
rules: b.rules,
}
}
fn item_in_cfg(cx: @Context, item: @ast::item) -> bool {
return (cx.in_cfg)(/*bad*/copy item.attrs);
}
fn foreign_item_in_cfg(cx: @Context, item: @ast::foreign_item) -> bool {
return (cx.in_cfg)(/*bad*/copy item.attrs);
}
fn view_item_in_cfg(cx: @Context, item: @ast::view_item) -> bool {
return (cx.in_cfg)(/*bad*/copy item.attrs);
}
fn method_in_cfg(cx: @Context, meth: @ast::method) -> bool {
return (cx.in_cfg)(/*bad*/copy meth.attrs);
2012-11-01 17:48:37 -05:00
}
fn trait_method_in_cfg(cx: @Context, meth: &ast::trait_method) -> bool {
2012-11-01 17:48:37 -05:00
match *meth {
ast::required(ref meth) => (cx.in_cfg)(/*bad*/copy meth.attrs),
ast::provided(@ref meth) => (cx.in_cfg)(/*bad*/copy meth.attrs)
2012-11-01 17:48:37 -05:00
}
}
// 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 {
metas_in_cfg(cfg, attr::attr_metas(attrs))
}
pub fn metas_in_cfg(cfg: ast::crate_cfg,
metas: ~[@ast::meta_item]) -> bool {
// The "cfg" attributes on the item
let cfg_metas = attr::find_meta_items_by_name(metas, ~"cfg");
// 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 cfg_metas = vec::filter_map(cfg_metas, |i| attr::get_meta_item_list(i));
if cfg_metas.all(|c| c.is_empty()) { return true; }
cfg_metas.any(|cfg_meta| {
cfg_meta.all(|cfg_mi| {
match cfg_mi.node {
ast::meta_list(s, ref it) if *s == ~"not"
=> it.all(|mi| !attr::contains(cfg, *mi)),
_ => attr::contains(cfg, *cfg_mi)
}
})
})
}