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-05-17 17:28:44 -05:00
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::option;
|
2012-09-04 13:54:36 -05:00
|
|
|
use syntax::{ast, fold, attr};
|
2011-06-30 00:32:08 -05:00
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
type in_cfg_pred = @fn(attrs: &[ast::Attribute]) -> bool;
|
2012-01-05 19:30:00 -06:00
|
|
|
|
2013-02-19 01:40:42 -06:00
|
|
|
struct Context {
|
2012-01-05 19:30:00 -06:00
|
|
|
in_cfg: in_cfg_pred
|
2013-02-19 01:40:42 -06:00
|
|
|
}
|
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
|
2013-07-19 00:38:55 -05:00
|
|
|
pub fn strip_unconfigured_items(crate: @ast::Crate) -> @ast::Crate {
|
2012-06-30 18:19:07 -05:00
|
|
|
do strip_items(crate) |attrs| {
|
2013-07-19 00:38:55 -05:00
|
|
|
in_cfg(crate.config, attrs)
|
2012-01-05 19:30:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 00:38:55 -05:00
|
|
|
pub fn strip_items(crate: &ast::Crate, in_cfg: in_cfg_pred)
|
|
|
|
-> @ast::Crate {
|
2012-01-05 19:30:00 -06:00
|
|
|
|
2013-02-19 01:40:42 -06:00
|
|
|
let ctxt = @Context { 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),
|
2013-07-16 13:08:35 -05:00
|
|
|
fold_block: |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-06-27 19:41:35 -05:00
|
|
|
fold_item_underscore: |a,b| fold_item_underscore(ctxt, a, b),
|
|
|
|
.. *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);
|
2013-06-27 08:04:22 -05:00
|
|
|
@fold.fold_crate(crate)
|
2011-06-30 00:32:08 -05:00
|
|
|
}
|
|
|
|
|
2013-04-17 11:15:37 -05:00
|
|
|
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 }
|
2011-06-30 00:32:08 -05:00
|
|
|
}
|
|
|
|
|
2013-07-05 03:28:53 -05:00
|
|
|
fn filter_view_item<'r>(cx: @Context, view_item: &'r ast::view_item)-> Option<&'r 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-12 15:00:50 -05:00
|
|
|
fn fold_mod(cx: @Context, m: &ast::_mod, fld: @fold::ast_fold) -> ast::_mod {
|
2013-07-01 21:38:19 -05:00
|
|
|
let filtered_items = do m.items.iter().filter_map |a| {
|
|
|
|
filter_item(cx, *a).chain(|x| fld.fold_item(x))
|
|
|
|
}.collect();
|
|
|
|
let filtered_view_items = do m.view_items.iter().filter_map |a| {
|
2013-08-04 16:59:36 -05:00
|
|
|
do filter_view_item(cx, a).map_move |x| {
|
|
|
|
fld.fold_view_item(x)
|
|
|
|
}
|
2013-07-01 21:38:19 -05:00
|
|
|
}.collect();
|
2013-01-15 18:05:20 -06:00
|
|
|
ast::_mod {
|
2013-07-01 21:38:19 -05:00
|
|
|
view_items: filtered_view_items,
|
|
|
|
items: filtered_items
|
2013-01-15 18:05:20 -06:00
|
|
|
}
|
2011-06-30 00:32:08 -05:00
|
|
|
}
|
|
|
|
|
2013-04-17 11:15:37 -05:00
|
|
|
fn filter_foreign_item(cx: @Context, 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
|
|
|
}
|
|
|
|
|
2013-01-15 18:05:20 -06:00
|
|
|
fn fold_foreign_mod(
|
2013-02-19 01:40:42 -06:00
|
|
|
cx: @Context,
|
2013-02-18 00:20:36 -06:00
|
|
|
nm: &ast::foreign_mod,
|
2013-03-12 15:00:50 -05:00
|
|
|
fld: @fold::ast_fold
|
2013-01-15 18:05:20 -06:00
|
|
|
) -> ast::foreign_mod {
|
2013-07-01 21:38:19 -05:00
|
|
|
let filtered_items = nm.items.iter().filter_map(|a| filter_foreign_item(cx, *a)).collect();
|
|
|
|
let filtered_view_items = do nm.view_items.iter().filter_map |a| {
|
2013-08-04 16:59:36 -05:00
|
|
|
do filter_view_item(cx, a).map_move |x| {
|
|
|
|
fld.fold_view_item(x)
|
|
|
|
}
|
2013-07-01 21:38:19 -05:00
|
|
|
}.collect();
|
2013-01-15 18:05:20 -06:00
|
|
|
ast::foreign_mod {
|
2012-08-29 14:22:05 -05:00
|
|
|
sort: nm.sort,
|
2013-03-13 21:25:28 -05:00
|
|
|
abis: nm.abis,
|
2013-07-01 21:38:19 -05:00
|
|
|
view_items: filtered_view_items,
|
2012-08-01 19:30:05 -05:00
|
|
|
items: filtered_items
|
2013-01-15 18:05:20 -06:00
|
|
|
}
|
2011-07-05 15:29:21 -05:00
|
|
|
}
|
|
|
|
|
2013-02-18 00:20:36 -06:00
|
|
|
fn fold_item_underscore(cx: @Context, item: &ast::item_,
|
2013-03-12 15:00:50 -05:00
|
|
|
fld: @fold::ast_fold) -> ast::item_ {
|
2013-02-18 00:20:36 -06:00
|
|
|
let item = match *item {
|
2013-07-05 23:57:11 -05:00
|
|
|
ast::item_impl(ref a, ref b, ref c, ref methods) => {
|
2013-07-01 21:38:19 -05:00
|
|
|
let methods = methods.iter().filter(|m| method_in_cfg(cx, **m))
|
|
|
|
.transform(|x| *x).collect();
|
2013-07-02 14:47:32 -05:00
|
|
|
ast::item_impl((*a).clone(), (*b).clone(), (*c).clone(), 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-07-01 21:38:19 -05:00
|
|
|
let methods = methods.iter().filter(|m| trait_method_in_cfg(cx, *m) )
|
2013-07-02 14:47:32 -05:00
|
|
|
.transform(|x| (*x).clone()).collect();
|
|
|
|
ast::item_trait((*a).clone(), (*b).clone(), methods)
|
2012-11-01 17:48:37 -05:00
|
|
|
}
|
2013-07-02 14:47:32 -05:00
|
|
|
ref item => (*item).clone(),
|
2012-11-01 17:48:37 -05:00
|
|
|
};
|
|
|
|
|
2013-02-18 00:20:36 -06:00
|
|
|
fold::noop_fold_item_underscore(&item, fld)
|
2012-11-01 17:48:37 -05:00
|
|
|
}
|
|
|
|
|
2013-04-17 11:15: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) => {
|
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(
|
2013-02-19 01:40:42 -06:00
|
|
|
cx: @Context,
|
2013-07-19 00:38:55 -05:00
|
|
|
b: &ast::Block,
|
2013-03-12 15:00:50 -05:00
|
|
|
fld: @fold::ast_fold
|
2013-07-19 00:38:55 -05:00
|
|
|
) -> ast::Block {
|
2013-07-01 21:38:19 -05:00
|
|
|
let resulting_stmts = do b.stmts.iter().filter_map |a| {
|
|
|
|
filter_stmt(cx, *a).chain(|stmt| fld.fold_stmt(stmt))
|
|
|
|
}.collect();
|
|
|
|
let filtered_view_items = do b.view_items.iter().filter_map |a| {
|
2013-08-04 16:59:36 -05:00
|
|
|
filter_view_item(cx, a).map(|x| fld.fold_view_item(*x))
|
2013-07-01 21:38:19 -05:00
|
|
|
}.collect();
|
2013-07-19 00:38:55 -05:00
|
|
|
ast::Block {
|
2013-06-04 23:43:41 -05:00
|
|
|
view_items: filtered_view_items,
|
|
|
|
stmts: resulting_stmts,
|
2013-03-16 14:49:12 -05:00
|
|
|
expr: b.expr.map(|x| fld.fold_expr(*x)),
|
2013-01-14 22:52:28 -06:00
|
|
|
id: b.id,
|
|
|
|
rules: b.rules,
|
2013-07-16 13:08:35 -05:00
|
|
|
span: b.span,
|
2013-01-14 22:52:28 -06:00
|
|
|
}
|
2011-06-30 15:04:02 -05:00
|
|
|
}
|
|
|
|
|
2013-02-19 01:40:42 -06:00
|
|
|
fn item_in_cfg(cx: @Context, item: @ast::item) -> bool {
|
2013-07-02 14:47:32 -05:00
|
|
|
return (cx.in_cfg)(item.attrs);
|
2011-07-05 15:29:21 -05:00
|
|
|
}
|
|
|
|
|
2013-02-19 01:40:42 -06:00
|
|
|
fn foreign_item_in_cfg(cx: @Context, item: @ast::foreign_item) -> bool {
|
2013-07-02 14:47:32 -05:00
|
|
|
return (cx.in_cfg)(item.attrs);
|
2011-07-05 15:29:21 -05:00
|
|
|
}
|
|
|
|
|
2013-07-05 03:28:53 -05:00
|
|
|
fn view_item_in_cfg(cx: @Context, item: &ast::view_item) -> bool {
|
|
|
|
return (cx.in_cfg)(item.attrs);
|
2012-07-09 16:02:39 -05:00
|
|
|
}
|
|
|
|
|
2013-02-19 01:40:42 -06:00
|
|
|
fn method_in_cfg(cx: @Context, meth: @ast::method) -> bool {
|
2013-07-02 14:47:32 -05:00
|
|
|
return (cx.in_cfg)(meth.attrs);
|
2012-11-01 17:48:37 -05:00
|
|
|
}
|
|
|
|
|
2013-02-19 01:40:42 -06:00
|
|
|
fn trait_method_in_cfg(cx: @Context, meth: &ast::trait_method) -> bool {
|
2012-11-01 17:48:37 -05:00
|
|
|
match *meth {
|
2013-07-02 14:47:32 -05:00
|
|
|
ast::required(ref meth) => (cx.in_cfg)(meth.attrs),
|
|
|
|
ast::provided(@ref meth) => (cx.in_cfg)(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-07-19 06:51:37 -05:00
|
|
|
fn in_cfg(cfg: &[@ast::MetaItem], attrs: &[ast::Attribute]) -> bool {
|
|
|
|
attr::test_cfg(cfg, attrs.iter().transform(|x| *x))
|
2011-06-30 00:32:08 -05:00
|
|
|
}
|