2012-12-04 00:48:01 +00: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-05-17 22:28:44 +00:00
|
|
|
|
2013-08-29 19:10:02 +00:00
|
|
|
use syntax::fold::ast_fold;
|
2012-09-04 18:54:36 +00:00
|
|
|
use syntax::{ast, fold, attr};
|
2011-06-30 05:32:08 +00:00
|
|
|
|
2013-08-29 22:04:09 +00:00
|
|
|
struct Context<'self> {
|
2013-11-20 01:36:32 +00:00
|
|
|
in_cfg: 'self |attrs: &[ast::Attribute]| -> bool,
|
2013-02-19 07:40:42 +00:00
|
|
|
}
|
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
|
2013-09-28 02:46:09 +00:00
|
|
|
pub fn strip_unconfigured_items(crate: ast::Crate) -> ast::Crate {
|
|
|
|
let config = crate.config.clone();
|
2013-11-21 23:42:55 +00:00
|
|
|
strip_items(crate, |attrs| in_cfg(config, attrs))
|
2012-01-06 01:30:00 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 22:04:09 +00:00
|
|
|
impl<'self> fold::ast_fold for Context<'self> {
|
2013-08-29 19:10:02 +00:00
|
|
|
fn fold_mod(&self, module: &ast::_mod) -> ast::_mod {
|
2013-08-29 22:04:09 +00:00
|
|
|
fold_mod(self, module)
|
2013-08-29 19:10:02 +00:00
|
|
|
}
|
2013-11-30 22:00:39 +00:00
|
|
|
fn fold_block(&self, block: ast::P<ast::Block>) -> ast::P<ast::Block> {
|
2013-08-29 22:04:09 +00:00
|
|
|
fold_block(self, block)
|
2013-08-29 19:10:02 +00:00
|
|
|
}
|
|
|
|
fn fold_foreign_mod(&self, foreign_module: &ast::foreign_mod)
|
|
|
|
-> ast::foreign_mod {
|
2013-08-29 22:04:09 +00:00
|
|
|
fold_foreign_mod(self, foreign_module)
|
2013-08-29 19:10:02 +00:00
|
|
|
}
|
|
|
|
fn fold_item_underscore(&self, item: &ast::item_) -> ast::item_ {
|
2013-08-29 22:04:09 +00:00
|
|
|
fold_item_underscore(self, item)
|
2013-08-29 19:10:02 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-30 05:32:08 +00:00
|
|
|
|
2013-09-28 02:46:09 +00:00
|
|
|
pub fn strip_items(crate: ast::Crate,
|
2013-11-19 21:22:03 +00:00
|
|
|
in_cfg: |attrs: &[ast::Attribute]| -> bool)
|
2013-09-28 02:46:09 +00:00
|
|
|
-> ast::Crate {
|
2013-08-29 22:04:09 +00:00
|
|
|
let ctxt = Context {
|
2013-08-29 19:10:02 +00:00
|
|
|
in_cfg: in_cfg,
|
2013-06-28 00:41:35 +00:00
|
|
|
};
|
2013-09-28 02:46:09 +00:00
|
|
|
ctxt.fold_crate(crate)
|
2011-06-30 05:32:08 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 22:04:09 +00:00
|
|
|
fn filter_view_item<'r>(cx: &Context, view_item: &'r ast::view_item)
|
|
|
|
-> Option<&'r ast::view_item> {
|
2012-07-09 21:02:39 +00:00
|
|
|
if view_item_in_cfg(cx, view_item) {
|
2013-08-29 22:04:09 +00:00
|
|
|
Some(view_item)
|
2012-07-09 21:02:39 +00:00
|
|
|
} else {
|
2013-08-29 22:04:09 +00:00
|
|
|
None
|
2012-07-09 21:02:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-29 22:04:09 +00:00
|
|
|
fn fold_mod(cx: &Context, m: &ast::_mod) -> ast::_mod {
|
2013-11-25 07:08:53 +00:00
|
|
|
let filtered_items = m.items.iter()
|
|
|
|
.filter(|&a| item_in_cfg(cx, *a))
|
|
|
|
.flat_map(|&x| cx.fold_item(x).move_iter())
|
|
|
|
.collect();
|
2013-11-21 23:42:55 +00:00
|
|
|
let filtered_view_items = m.view_items.iter().filter_map(|a| {
|
|
|
|
filter_view_item(cx, a).map(|x| cx.fold_view_item(x))
|
|
|
|
}).collect();
|
2013-01-16 00:05:20 +00:00
|
|
|
ast::_mod {
|
2013-07-02 02:38:19 +00:00
|
|
|
view_items: filtered_view_items,
|
|
|
|
items: filtered_items
|
2013-01-16 00:05:20 +00:00
|
|
|
}
|
2011-06-30 05:32:08 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 22:04:09 +00:00
|
|
|
fn filter_foreign_item(cx: &Context, item: @ast::foreign_item)
|
|
|
|
-> Option<@ast::foreign_item> {
|
2012-06-26 23:18:37 +00:00
|
|
|
if foreign_item_in_cfg(cx, item) {
|
2013-08-29 22:04:09 +00:00
|
|
|
Some(item)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2011-07-05 20:29:21 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 22:04:09 +00:00
|
|
|
fn fold_foreign_mod(cx: &Context, nm: &ast::foreign_mod) -> ast::foreign_mod {
|
2013-08-29 19:10:02 +00:00
|
|
|
let filtered_items = nm.items
|
|
|
|
.iter()
|
|
|
|
.filter_map(|a| filter_foreign_item(cx, *a))
|
|
|
|
.collect();
|
2013-11-21 23:42:55 +00:00
|
|
|
let filtered_view_items = nm.view_items.iter().filter_map(|a| {
|
|
|
|
filter_view_item(cx, a).map(|x| cx.fold_view_item(x))
|
|
|
|
}).collect();
|
2013-01-16 00:05:20 +00:00
|
|
|
ast::foreign_mod {
|
2013-03-14 02:25:28 +00:00
|
|
|
abis: nm.abis,
|
2013-07-02 02:38:19 +00:00
|
|
|
view_items: filtered_view_items,
|
2012-08-02 00:30:05 +00:00
|
|
|
items: filtered_items
|
2013-01-16 00:05:20 +00:00
|
|
|
}
|
2011-07-05 20:29:21 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 22:04:09 +00:00
|
|
|
fn fold_item_underscore(cx: &Context, item: &ast::item_) -> ast::item_ {
|
2013-02-18 06:20:36 +00:00
|
|
|
let item = match *item {
|
2013-11-30 22:00:39 +00:00
|
|
|
ast::item_impl(ref a, ref b, c, ref methods) => {
|
2013-07-02 02:38:19 +00:00
|
|
|
let methods = methods.iter().filter(|m| method_in_cfg(cx, **m))
|
2013-08-10 03:09:47 +00:00
|
|
|
.map(|x| *x).collect();
|
2013-11-30 22:00:39 +00:00
|
|
|
ast::item_impl((*a).clone(), (*b).clone(), c, methods)
|
2012-11-01 22:48:37 +00:00
|
|
|
}
|
2013-01-07 22:16:52 +00:00
|
|
|
ast::item_trait(ref a, ref b, ref methods) => {
|
2013-08-29 22:04:09 +00:00
|
|
|
let methods = methods.iter()
|
|
|
|
.filter(|m| trait_method_in_cfg(cx, *m) )
|
|
|
|
.map(|x| (*x).clone())
|
|
|
|
.collect();
|
2013-07-02 19:47:32 +00:00
|
|
|
ast::item_trait((*a).clone(), (*b).clone(), methods)
|
2012-11-01 22:48:37 +00:00
|
|
|
}
|
2013-07-02 19:47:32 +00:00
|
|
|
ref item => (*item).clone(),
|
2012-11-01 22:48:37 +00:00
|
|
|
};
|
|
|
|
|
2013-08-29 22:04:09 +00:00
|
|
|
fold::noop_fold_item_underscore(&item, cx)
|
2012-11-01 22:48:37 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 07:08:53 +00:00
|
|
|
fn retain_stmt(cx: &Context, stmt: @ast::Stmt) -> bool {
|
2012-08-06 19:34:08 +00:00
|
|
|
match stmt.node {
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::StmtDecl(decl, _) => {
|
2012-08-06 19:34:08 +00:00
|
|
|
match decl.node {
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::DeclItem(item) => {
|
2013-11-25 07:08:53 +00:00
|
|
|
item_in_cfg(cx, item)
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2013-11-25 07:08:53 +00:00
|
|
|
_ => true
|
2011-06-30 20:04:02 +00:00
|
|
|
}
|
2011-07-27 12:19:39 +00:00
|
|
|
}
|
2013-11-25 07:08:53 +00:00
|
|
|
_ => true
|
2011-06-30 20:04:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-30 22:00:39 +00:00
|
|
|
fn fold_block(cx: &Context, b: ast::P<ast::Block>) -> ast::P<ast::Block> {
|
2013-11-25 07:08:53 +00:00
|
|
|
let resulting_stmts = b.stmts.iter()
|
|
|
|
.filter(|&a| retain_stmt(cx, *a))
|
|
|
|
.flat_map(|&stmt| cx.fold_stmt(stmt).move_iter())
|
|
|
|
.collect();
|
2013-11-21 23:42:55 +00:00
|
|
|
let filtered_view_items = b.view_items.iter().filter_map(|a| {
|
2013-09-20 06:08:47 +00:00
|
|
|
filter_view_item(cx, a).map(|x| cx.fold_view_item(x))
|
2013-11-21 23:42:55 +00:00
|
|
|
}).collect();
|
2013-11-30 22:00:39 +00:00
|
|
|
ast::P(ast::Block {
|
2013-06-05 04:43:41 +00:00
|
|
|
view_items: filtered_view_items,
|
|
|
|
stmts: resulting_stmts,
|
2013-09-20 06:08:47 +00:00
|
|
|
expr: b.expr.map(|x| cx.fold_expr(x)),
|
2013-01-15 04:52:28 +00:00
|
|
|
id: b.id,
|
|
|
|
rules: b.rules,
|
2013-07-16 18:08:35 +00:00
|
|
|
span: b.span,
|
2013-11-30 22:00:39 +00:00
|
|
|
})
|
2011-06-30 20:04:02 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 22:04:09 +00:00
|
|
|
fn item_in_cfg(cx: &Context, item: @ast::item) -> bool {
|
2013-07-02 19:47:32 +00:00
|
|
|
return (cx.in_cfg)(item.attrs);
|
2011-07-05 20:29:21 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 22:04:09 +00:00
|
|
|
fn foreign_item_in_cfg(cx: &Context, item: @ast::foreign_item) -> bool {
|
2013-07-02 19:47:32 +00:00
|
|
|
return (cx.in_cfg)(item.attrs);
|
2011-07-05 20:29:21 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 22:04:09 +00:00
|
|
|
fn view_item_in_cfg(cx: &Context, item: &ast::view_item) -> bool {
|
2013-07-05 08:28:53 +00:00
|
|
|
return (cx.in_cfg)(item.attrs);
|
2012-07-09 21:02:39 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 22:04:09 +00:00
|
|
|
fn method_in_cfg(cx: &Context, meth: @ast::method) -> bool {
|
2013-07-02 19:47:32 +00:00
|
|
|
return (cx.in_cfg)(meth.attrs);
|
2012-11-01 22:48:37 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 22:04:09 +00:00
|
|
|
fn trait_method_in_cfg(cx: &Context, meth: &ast::trait_method) -> bool {
|
2012-11-01 22:48:37 +00:00
|
|
|
match *meth {
|
2013-07-02 19:47:32 +00:00
|
|
|
ast::required(ref meth) => (cx.in_cfg)(meth.attrs),
|
|
|
|
ast::provided(@ref meth) => (cx.in_cfg)(meth.attrs)
|
2012-11-01 22:48:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2013-07-19 11:51:37 +00:00
|
|
|
fn in_cfg(cfg: &[@ast::MetaItem], attrs: &[ast::Attribute]) -> bool {
|
2013-08-10 03:09:47 +00:00
|
|
|
attr::test_cfg(cfg, attrs.iter().map(|x| *x))
|
2011-06-30 05:32:08 +00:00
|
|
|
}
|
2013-08-29 22:04:09 +00:00
|
|
|
|