2014-04-22 23:54:48 -05:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
use syntax::fold::Folder;
|
2012-09-04 13:54:36 -05:00
|
|
|
use syntax::{ast, fold, attr};
|
2013-12-20 19:10:59 -06:00
|
|
|
use syntax::codemap;
|
2011-06-30 00:32:08 -05:00
|
|
|
|
2014-06-11 21:33:52 -05:00
|
|
|
use std::gc::{Gc, GC};
|
2014-05-16 12:15:33 -05:00
|
|
|
|
2014-07-09 16:48:12 -05:00
|
|
|
/// A folder that strips out items that do not belong in the current
|
|
|
|
/// configuration.
|
2013-12-10 01:16:18 -06:00
|
|
|
struct Context<'a> {
|
2014-04-07 15:30:48 -05:00
|
|
|
in_cfg: |attrs: &[ast::Attribute]|: 'a -> bool,
|
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
|
2014-02-05 15:15:24 -06:00
|
|
|
pub fn strip_unconfigured_items(krate: ast::Crate) -> ast::Crate {
|
|
|
|
let config = krate.config.clone();
|
2014-02-28 17:25:15 -06:00
|
|
|
strip_items(krate, |attrs| in_cfg(config.as_slice(), attrs))
|
2012-01-05 19:30:00 -06:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
impl<'a> fold::Folder for Context<'a> {
|
|
|
|
fn fold_mod(&mut self, module: &ast::Mod) -> ast::Mod {
|
2013-08-29 17:04:09 -05:00
|
|
|
fold_mod(self, module)
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
2013-12-27 21:34:51 -06:00
|
|
|
fn fold_block(&mut self, block: ast::P<ast::Block>) -> ast::P<ast::Block> {
|
2013-08-29 17:04:09 -05:00
|
|
|
fold_block(self, block)
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_foreign_mod(&mut self, foreign_mod: &ast::ForeignMod) -> ast::ForeignMod {
|
|
|
|
fold_foreign_mod(self, foreign_mod)
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_item_underscore(&mut self, item: &ast::Item_) -> ast::Item_ {
|
2013-08-29 17:04:09 -05:00
|
|
|
fold_item_underscore(self, item)
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
2014-05-16 12:15:33 -05:00
|
|
|
fn fold_expr(&mut self, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
|
2014-04-22 23:54:48 -05:00
|
|
|
fold_expr(self, expr)
|
|
|
|
}
|
2014-07-09 16:48:12 -05:00
|
|
|
fn fold_mac(&mut self, mac: &ast::Mac) -> ast::Mac {
|
2014-07-25 14:00:33 -05:00
|
|
|
fold::noop_fold_mac(mac, self)
|
2014-07-09 16:48:12 -05:00
|
|
|
}
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
2011-06-30 00:32:08 -05:00
|
|
|
|
2014-02-05 15:15:24 -06:00
|
|
|
pub fn strip_items(krate: ast::Crate,
|
2013-11-19 15:22:03 -06:00
|
|
|
in_cfg: |attrs: &[ast::Attribute]| -> bool)
|
2013-09-27 21:46:09 -05:00
|
|
|
-> ast::Crate {
|
2013-12-27 21:34:51 -06:00
|
|
|
let mut ctxt = Context {
|
2013-08-29 14:10:02 -05:00
|
|
|
in_cfg: in_cfg,
|
2013-06-27 19:41:35 -05:00
|
|
|
};
|
2014-02-05 15:15:24 -06:00
|
|
|
ctxt.fold_crate(krate)
|
2011-06-30 00:32:08 -05:00
|
|
|
}
|
|
|
|
|
2014-04-22 01:25:18 -05:00
|
|
|
fn filter_view_item<'r>(cx: &mut Context, view_item: &'r ast::ViewItem)
|
2014-01-09 07:05:33 -06:00
|
|
|
-> Option<&'r ast::ViewItem> {
|
2012-07-09 16:02:39 -05:00
|
|
|
if view_item_in_cfg(cx, view_item) {
|
2013-08-29 17:04:09 -05:00
|
|
|
Some(view_item)
|
2012-07-09 16:02:39 -05:00
|
|
|
} else {
|
2013-08-29 17:04:09 -05:00
|
|
|
None
|
2012-07-09 16:02:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_mod(cx: &mut Context, m: &ast::Mod) -> ast::Mod {
|
2014-05-16 12:15:33 -05:00
|
|
|
let filtered_items: Vec<&Gc<ast::Item>> = m.items.iter()
|
|
|
|
.filter(|a| item_in_cfg(cx, &***a))
|
2014-02-07 18:56:27 -06:00
|
|
|
.collect();
|
|
|
|
let flattened_items = filtered_items.move_iter()
|
2013-11-25 01:08:53 -06:00
|
|
|
.flat_map(|&x| cx.fold_item(x).move_iter())
|
|
|
|
.collect();
|
2013-11-21 17:42:55 -06: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();
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::Mod {
|
2014-04-26 15:05:45 -05:00
|
|
|
inner: m.inner,
|
2013-07-01 21:38:19 -05:00
|
|
|
view_items: filtered_view_items,
|
2014-02-07 18:56:27 -06:00
|
|
|
items: flattened_items
|
2013-01-15 18:05:20 -06:00
|
|
|
}
|
2011-06-30 00:32:08 -05:00
|
|
|
}
|
|
|
|
|
2014-05-16 12:15:33 -05:00
|
|
|
fn filter_foreign_item(cx: &mut Context, item: Gc<ast::ForeignItem>)
|
|
|
|
-> Option<Gc<ast::ForeignItem>> {
|
|
|
|
if foreign_item_in_cfg(cx, &*item) {
|
2013-08-29 17:04:09 -05:00
|
|
|
Some(item)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2011-07-05 15:29:21 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_foreign_mod(cx: &mut Context, nm: &ast::ForeignMod) -> ast::ForeignMod {
|
2013-08-29 14:10:02 -05:00
|
|
|
let filtered_items = nm.items
|
|
|
|
.iter()
|
|
|
|
.filter_map(|a| filter_foreign_item(cx, *a))
|
|
|
|
.collect();
|
2013-11-21 17:42:55 -06: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();
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ForeignMod {
|
2014-04-02 03:19:41 -05:00
|
|
|
abi: nm.abi,
|
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
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn fold_item_underscore(cx: &mut Context, item: &ast::Item_) -> ast::Item_ {
|
2013-02-18 00:20:36 -06:00
|
|
|
let item = match *item {
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ItemImpl(ref a, ref b, c, ref methods) => {
|
2014-05-16 12:15:33 -05:00
|
|
|
let methods = methods.iter().filter(|m| method_in_cfg(cx, &***m))
|
2013-08-09 22:09:47 -05:00
|
|
|
.map(|x| *x).collect();
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ItemImpl((*a).clone(), (*b).clone(), c, methods)
|
2012-11-01 17:48:37 -05:00
|
|
|
}
|
2014-07-07 21:26:02 -05:00
|
|
|
ast::ItemTrait(ref a, ref b, ref c, ref methods) => {
|
2013-08-29 17:04:09 -05:00
|
|
|
let methods = methods.iter()
|
|
|
|
.filter(|m| trait_method_in_cfg(cx, *m) )
|
|
|
|
.map(|x| (*x).clone())
|
|
|
|
.collect();
|
2014-07-07 21:26:02 -05:00
|
|
|
ast::ItemTrait((*a).clone(), (*b).clone(), (*c).clone(), methods)
|
2012-11-01 17:48:37 -05:00
|
|
|
}
|
2014-05-16 12:15:33 -05:00
|
|
|
ast::ItemStruct(ref def, ref generics) => {
|
|
|
|
ast::ItemStruct(fold_struct(cx, &**def), generics.clone())
|
2013-12-20 19:10:59 -06:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ItemEnum(ref def, ref generics) => {
|
2014-02-07 18:56:27 -06:00
|
|
|
let mut variants = def.variants.iter().map(|c| c.clone()).
|
|
|
|
filter_map(|v| {
|
2014-02-28 17:25:15 -06:00
|
|
|
if !(cx.in_cfg)(v.node.attrs.as_slice()) {
|
2014-02-07 18:56:27 -06:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(match v.node.kind {
|
|
|
|
ast::TupleVariantKind(..) => v,
|
2014-05-16 12:15:33 -05:00
|
|
|
ast::StructVariantKind(ref def) => {
|
|
|
|
let def = fold_struct(cx, &**def);
|
|
|
|
box(GC) codemap::Spanned {
|
2014-02-07 18:56:27 -06:00
|
|
|
node: ast::Variant_ {
|
2014-05-16 12:15:33 -05:00
|
|
|
kind: ast::StructVariantKind(def.clone()),
|
2014-02-07 18:56:27 -06:00
|
|
|
..v.node.clone()
|
|
|
|
},
|
|
|
|
..*v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2013-12-20 19:10:59 -06:00
|
|
|
}
|
2014-02-07 18:56:27 -06:00
|
|
|
});
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ItemEnum(ast::EnumDef {
|
2013-12-20 19:10:59 -06:00
|
|
|
variants: variants.collect(),
|
|
|
|
}, generics.clone())
|
|
|
|
}
|
|
|
|
ref item => item.clone(),
|
2012-11-01 17:48:37 -05:00
|
|
|
};
|
|
|
|
|
2013-08-29 17:04:09 -05:00
|
|
|
fold::noop_fold_item_underscore(&item, cx)
|
2012-11-01 17:48:37 -05:00
|
|
|
}
|
|
|
|
|
2014-05-16 12:15:33 -05:00
|
|
|
fn fold_struct(cx: &mut Context, def: &ast::StructDef) -> Gc<ast::StructDef> {
|
2013-12-20 19:10:59 -06:00
|
|
|
let mut fields = def.fields.iter().map(|c| c.clone()).filter(|m| {
|
2014-02-28 17:25:15 -06:00
|
|
|
(cx.in_cfg)(m.node.attrs.as_slice())
|
2013-12-20 19:10:59 -06:00
|
|
|
});
|
2014-05-16 12:15:33 -05:00
|
|
|
box(GC) ast::StructDef {
|
2013-12-20 19:10:59 -06:00
|
|
|
fields: fields.collect(),
|
|
|
|
ctor_id: def.ctor_id,
|
2014-02-24 01:17:02 -06:00
|
|
|
super_struct: def.super_struct.clone(),
|
|
|
|
is_virtual: def.is_virtual,
|
2013-12-20 19:10:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 12:15:33 -05:00
|
|
|
fn retain_stmt(cx: &mut Context, stmt: Gc<ast::Stmt>) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match stmt.node {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::StmtDecl(decl, _) => {
|
2012-08-06 14:34:08 -05:00
|
|
|
match decl.node {
|
2014-05-16 12:15:33 -05:00
|
|
|
ast::DeclItem(ref item) => {
|
|
|
|
item_in_cfg(cx, &**item)
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2013-11-25 01:08:53 -06:00
|
|
|
_ => true
|
2011-06-30 15:04:02 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2013-11-25 01:08:53 -06:00
|
|
|
_ => true
|
2011-06-30 15:04:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 21:34:51 -06:00
|
|
|
fn fold_block(cx: &mut Context, b: ast::P<ast::Block>) -> ast::P<ast::Block> {
|
2014-05-16 12:15:33 -05:00
|
|
|
let resulting_stmts: Vec<&Gc<ast::Stmt>> =
|
2014-02-07 18:56:27 -06:00
|
|
|
b.stmts.iter().filter(|&a| retain_stmt(cx, *a)).collect();
|
|
|
|
let resulting_stmts = resulting_stmts.move_iter()
|
2014-05-16 12:15:33 -05:00
|
|
|
.flat_map(|stmt| cx.fold_stmt(&**stmt).move_iter())
|
2014-02-07 18:56:27 -06:00
|
|
|
.collect();
|
2013-11-21 17:42:55 -06:00
|
|
|
let filtered_view_items = b.view_items.iter().filter_map(|a| {
|
2013-09-20 01:08:47 -05:00
|
|
|
filter_view_item(cx, a).map(|x| cx.fold_view_item(x))
|
2013-11-21 17:42:55 -06:00
|
|
|
}).collect();
|
2013-11-30 16:00:39 -06:00
|
|
|
ast::P(ast::Block {
|
2013-06-04 23:43:41 -05:00
|
|
|
view_items: filtered_view_items,
|
|
|
|
stmts: resulting_stmts,
|
2013-09-20 01:08:47 -05:00
|
|
|
expr: b.expr.map(|x| cx.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-11-30 16:00:39 -06:00
|
|
|
})
|
2011-06-30 15:04:02 -05:00
|
|
|
}
|
|
|
|
|
2014-05-16 12:15:33 -05:00
|
|
|
fn fold_expr(cx: &mut Context, expr: Gc<ast::Expr>) -> Gc<ast::Expr> {
|
2014-04-22 23:54:48 -05:00
|
|
|
let expr = match expr.node {
|
|
|
|
ast::ExprMatch(ref m, ref arms) => {
|
|
|
|
let arms = arms.iter()
|
|
|
|
.filter(|a| (cx.in_cfg)(a.attrs.as_slice()))
|
|
|
|
.map(|a| a.clone())
|
|
|
|
.collect();
|
2014-05-16 12:15:33 -05:00
|
|
|
box(GC) ast::Expr {
|
2014-04-22 23:54:48 -05:00
|
|
|
id: expr.id,
|
|
|
|
span: expr.span.clone(),
|
|
|
|
node: ast::ExprMatch(m.clone(), arms),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => expr.clone()
|
|
|
|
};
|
|
|
|
fold::noop_fold_expr(expr, cx)
|
|
|
|
}
|
|
|
|
|
2014-04-22 01:25:18 -05:00
|
|
|
fn item_in_cfg(cx: &mut Context, item: &ast::Item) -> bool {
|
2014-02-28 17:25:15 -06:00
|
|
|
return (cx.in_cfg)(item.attrs.as_slice());
|
2011-07-05 15:29:21 -05:00
|
|
|
}
|
|
|
|
|
2014-04-22 01:25:18 -05:00
|
|
|
fn foreign_item_in_cfg(cx: &mut Context, item: &ast::ForeignItem) -> bool {
|
2014-02-28 17:25:15 -06:00
|
|
|
return (cx.in_cfg)(item.attrs.as_slice());
|
2011-07-05 15:29:21 -05:00
|
|
|
}
|
|
|
|
|
2014-04-22 01:25:18 -05:00
|
|
|
fn view_item_in_cfg(cx: &mut Context, item: &ast::ViewItem) -> bool {
|
2014-02-28 17:25:15 -06:00
|
|
|
return (cx.in_cfg)(item.attrs.as_slice());
|
2012-07-09 16:02:39 -05:00
|
|
|
}
|
|
|
|
|
2014-04-22 01:25:18 -05:00
|
|
|
fn method_in_cfg(cx: &mut Context, meth: &ast::Method) -> bool {
|
2014-02-28 17:25:15 -06:00
|
|
|
return (cx.in_cfg)(meth.attrs.as_slice());
|
2012-11-01 17:48:37 -05:00
|
|
|
}
|
|
|
|
|
2014-04-22 01:25:18 -05:00
|
|
|
fn trait_method_in_cfg(cx: &mut Context, meth: &ast::TraitMethod) -> bool {
|
2012-11-01 17:48:37 -05:00
|
|
|
match *meth {
|
2014-02-28 17:25:15 -06:00
|
|
|
ast::Required(ref meth) => (cx.in_cfg)(meth.attrs.as_slice()),
|
|
|
|
ast::Provided(meth) => (cx.in_cfg)(meth.attrs.as_slice())
|
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
|
2014-05-16 12:15:33 -05:00
|
|
|
fn in_cfg(cfg: &[Gc<ast::MetaItem>], attrs: &[ast::Attribute]) -> bool {
|
2013-08-09 22:09:47 -05:00
|
|
|
attr::test_cfg(cfg, attrs.iter().map(|x| *x))
|
2011-06-30 00:32:08 -05:00
|
|
|
}
|
2013-08-29 17:04:09 -05:00
|
|
|
|