2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
2013-01-07 16:16:52 -06:00
|
|
|
|
2012-09-04 13:54:36 -05:00
|
|
|
use syntax::{ast, fold, attr};
|
2011-06-30 00:32:08 -05:00
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::option;
|
|
|
|
use core::vec;
|
|
|
|
|
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;
|
|
|
|
|
2013-01-07 16:16:52 -06:00
|
|
|
type in_cfg_pred = fn@(+attrs: ~[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-06-30 18:19:07 -05:00
|
|
|
do strip_items(crate) |attrs| {
|
2013-01-07 16:16:52 -06:00
|
|
|
in_cfg(/*bad*/copy crate.node.config, attrs)
|
2012-01-05 19:30:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-01-08 16:00:45 -06:00
|
|
|
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),
|
2013-01-07 16:16:52 -06:00
|
|
|
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-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);
|
2012-08-01 19:30:05 -05:00
|
|
|
return res;
|
2011-06-30 00:32:08 -05:00
|
|
|
}
|
|
|
|
|
2012-01-05 19:30:00 -06:00
|
|
|
fn filter_item(cx: ctxt, &&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 }
|
2011-06-30 00:32:08 -05:00
|
|
|
}
|
|
|
|
|
2012-07-09 16:02:39 -05:00
|
|
|
fn filter_view_item(cx: ctxt, &&view_item: @ast::view_item
|
2012-08-20 14:23:37 -05:00
|
|
|
)-> Option<@ast::view_item> {
|
2012-07-09 16:02:39 -05:00
|
|
|
if view_item_in_cfg(cx, view_item) {
|
2012-08-20 14:23:37 -05:00
|
|
|
option::Some(view_item)
|
2012-07-09 16:02:39 -05:00
|
|
|
} else {
|
2012-08-20 14:23:37 -05:00
|
|
|
option::None
|
2012-07-09 16:02:39 -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-09-28 00:20:47 -05:00
|
|
|
let filtered_items = vec::filter_map(m.items, |a| filter_item(cx, *a));
|
|
|
|
let filtered_view_items = vec::filter_map(m.view_items,
|
|
|
|
|a| filter_view_item(cx, *a));
|
2012-08-01 19:30:05 -05:00
|
|
|
return {
|
2012-09-21 20:43:30 -05:00
|
|
|
view_items: vec::map(filtered_view_items, |x| fld.fold_view_item(*x)),
|
2012-09-28 00:20:47 -05:00
|
|
|
items: vec::filter_map(filtered_items, |x| fld.fold_item(*x))
|
2012-08-01 19:30:05 -05:00
|
|
|
};
|
2011-06-30 00:32:08 -05:00
|
|
|
}
|
|
|
|
|
2012-06-26 18:18:37 -05:00
|
|
|
fn filter_foreign_item(cx: ctxt, &&item: @ast::foreign_item) ->
|
2012-08-20 14:23:37 -05:00
|
|
|
Option<@ast::foreign_item> {
|
2012-06-26 18:18:37 -05:00
|
|
|
if foreign_item_in_cfg(cx, item) {
|
2012-08-20 14:23:37 -05:00
|
|
|
option::Some(item)
|
|
|
|
} else { option::None }
|
2011-07-05 15:29:21 -05:00
|
|
|
}
|
|
|
|
|
2012-06-26 18:18:37 -05:00
|
|
|
fn fold_foreign_mod(cx: ctxt, nm: ast::foreign_mod,
|
|
|
|
fld: fold::ast_fold) -> ast::foreign_mod {
|
2012-09-28 00:20:47 -05:00
|
|
|
let filtered_items = vec::filter_map(nm.items,
|
|
|
|
|a| filter_foreign_item(cx, *a));
|
|
|
|
let filtered_view_items = vec::filter_map(nm.view_items,
|
|
|
|
|a| filter_view_item(cx, *a));
|
2012-08-01 19:30:05 -05:00
|
|
|
return {
|
2012-08-29 14:22:05 -05:00
|
|
|
sort: nm.sort,
|
2012-11-27 19:25:55 -06:00
|
|
|
abi: nm.abi,
|
2012-09-21 20:43:30 -05:00
|
|
|
view_items: vec::map(filtered_view_items, |x| fld.fold_view_item(*x)),
|
2012-08-01 19:30:05 -05:00
|
|
|
items: filtered_items
|
|
|
|
};
|
2011-07-05 15:29:21 -05:00
|
|
|
}
|
|
|
|
|
2013-01-07 16:16:52 -06:00
|
|
|
fn fold_item_underscore(cx: ctxt, +item: ast::item_,
|
2012-11-01 18:39:32 -05:00
|
|
|
fld: fold::ast_fold) -> ast::item_ {
|
2012-11-01 17:48:37 -05:00
|
|
|
let item = match item {
|
2012-11-13 21:08:01 -06:00
|
|
|
ast::item_impl(a, b, c, methods) => {
|
2013-01-07 23:15:25 -06:00
|
|
|
let methods = methods.filtered(|m| method_in_cfg(cx, *m) );
|
2012-11-13 21:08:01 -06:00
|
|
|
ast::item_impl(a, b, c, methods)
|
2012-11-01 17:48:37 -05:00
|
|
|
}
|
2013-01-07 16:16:52 -06:00
|
|
|
ast::item_trait(ref a, ref b, ref methods) => {
|
2013-01-07 23:15:25 -06:00
|
|
|
let methods = methods.filtered(|m| trait_method_in_cfg(cx, m) );
|
2013-01-07 16:16:52 -06:00
|
|
|
ast::item_trait(/*bad*/copy *a, /*bad*/copy *b, methods)
|
2012-11-01 17:48:37 -05:00
|
|
|
}
|
2013-01-07 16:16:52 -06:00
|
|
|
item => item
|
2012-11-01 17:48:37 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
fold::noop_fold_item_underscore(item, fld)
|
|
|
|
}
|
|
|
|
|
2012-01-05 19:30:00 -06:00
|
|
|
fn filter_stmt(cx: ctxt, &&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) => {
|
2012-01-05 19:30:00 -06:00
|
|
|
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-06-30 15:04:02 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
_ => option::Some(stmt)
|
2011-06-30 15:04:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:52:28 -06:00
|
|
|
fn fold_block(
|
|
|
|
cx: ctxt,
|
|
|
|
b: ast::blk_,
|
|
|
|
fld: fold::ast_fold
|
|
|
|
) -> ast::blk_ {
|
2012-09-28 00:20:47 -05:00
|
|
|
let filtered_stmts = vec::filter_map(b.stmts, |a| filter_stmt(cx, *a));
|
2013-01-14 22:52:28 -06:00
|
|
|
ast::blk_ {
|
|
|
|
view_items: /*bad*/copy b.view_items,
|
|
|
|
stmts: vec::map(filtered_stmts, |x| fld.fold_stmt(*x)),
|
|
|
|
expr: option::map(&b.expr, |x| fld.fold_expr(*x)),
|
|
|
|
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 {
|
2013-01-07 16:16:52 -06:00
|
|
|
return (cx.in_cfg)(/*bad*/copy item.attrs);
|
2011-07-05 15:29:21 -05:00
|
|
|
}
|
|
|
|
|
2012-06-26 18:18:37 -05:00
|
|
|
fn foreign_item_in_cfg(cx: ctxt, item: @ast::foreign_item) -> bool {
|
2013-01-07 16:16:52 -06:00
|
|
|
return (cx.in_cfg)(/*bad*/copy item.attrs);
|
2011-07-05 15:29:21 -05:00
|
|
|
}
|
|
|
|
|
2012-07-09 16:02:39 -05:00
|
|
|
fn view_item_in_cfg(cx: ctxt, item: @ast::view_item) -> bool {
|
2013-01-07 16:16:52 -06:00
|
|
|
return (cx.in_cfg)(/*bad*/copy item.attrs);
|
2012-07-09 16:02:39 -05:00
|
|
|
}
|
|
|
|
|
2012-11-01 17:48:37 -05:00
|
|
|
fn method_in_cfg(cx: ctxt, meth: @ast::method) -> bool {
|
2013-01-07 16:16:52 -06:00
|
|
|
return (cx.in_cfg)(/*bad*/copy meth.attrs);
|
2012-11-01 17:48:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn trait_method_in_cfg(cx: ctxt, meth: &ast::trait_method) -> bool {
|
|
|
|
match *meth {
|
2013-01-07 16:16:52 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2013-01-07 16:16:52 -06: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))
|
|
|
|
}
|
|
|
|
|
2013-01-07 16:16:52 -06:00
|
|
|
fn metas_in_cfg(cfg: ast::crate_cfg, +metas: ~[@ast::meta_item]) -> bool {
|
2011-06-30 16:12:11 -05:00
|
|
|
// The "cfg" attributes on the item
|
2012-07-14 00:57:48 -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,
|
2012-09-28 00:20:47 -05:00
|
|
|
|i| attr::get_meta_item_list(*i)));
|
2011-10-29 19:35:45 -05:00
|
|
|
|
|
|
|
let has_cfg_metas = vec::len(cfg_metas) > 0u;
|
2012-08-01 19:30:05 -05:00
|
|
|
if !has_cfg_metas { return true; }
|
2011-06-30 00:32:08 -05:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
for cfg_metas.each |cfg_mi| {
|
2013-01-07 16:16:52 -06:00
|
|
|
if attr::contains(/*bad*/copy cfg, *cfg_mi) { return true; }
|
2011-06-30 00:32:08 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
return false;
|
2011-06-30 00:32:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Local Variables:
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|