1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use attr::AttrMetaMethods;
12 use diagnostic::SpanHandler;
14 use {ast, fold, attr};
18 use util::small_vector::SmallVector;
20 /// A folder that strips out items that do not belong in the current
22 struct Context<F> where F: FnMut(&[ast::Attribute]) -> bool {
26 // Support conditional compilation by transforming the AST, stripping out
27 // any items that do not belong in the current configuration
28 pub fn strip_unconfigured_items(diagnostic: &SpanHandler, krate: ast::Crate) -> ast::Crate {
29 let config = krate.config.clone();
30 strip_items(krate, |attrs| in_cfg(diagnostic, config.as_slice(), attrs))
33 impl<F> fold::Folder for Context<F> where F: FnMut(&[ast::Attribute]) -> bool {
34 fn fold_mod(&mut self, module: ast::Mod) -> ast::Mod {
35 fold_mod(self, module)
37 fn fold_block(&mut self, block: P<ast::Block>) -> P<ast::Block> {
38 fold_block(self, block)
40 fn fold_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
41 fold_foreign_mod(self, foreign_mod)
43 fn fold_item_underscore(&mut self, item: ast::Item_) -> ast::Item_ {
44 fold_item_underscore(self, item)
46 fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
49 fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
50 fold::noop_fold_mac(mac, self)
52 fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
57 pub fn strip_items<F>(krate: ast::Crate, in_cfg: F) -> ast::Crate where
58 F: FnMut(&[ast::Attribute]) -> bool,
60 let mut ctxt = Context {
63 ctxt.fold_crate(krate)
66 fn filter_view_item<F>(cx: &mut Context<F>,
67 view_item: ast::ViewItem)
68 -> Option<ast::ViewItem> where
69 F: FnMut(&[ast::Attribute]) -> bool
71 if view_item_in_cfg(cx, &view_item) {
78 fn fold_mod<F>(cx: &mut Context<F>,
80 view_items, items}: ast::Mod) -> ast::Mod where
81 F: FnMut(&[ast::Attribute]) -> bool
85 view_items: view_items.into_iter().filter_map(|a| {
86 filter_view_item(cx, a).map(|x| cx.fold_view_item(x))
88 items: items.into_iter().flat_map(|a| {
89 cx.fold_item(a).into_iter()
94 fn filter_foreign_item<F>(cx: &mut Context<F>,
95 item: P<ast::ForeignItem>)
96 -> Option<P<ast::ForeignItem>> where
97 F: FnMut(&[ast::Attribute]) -> bool
99 if foreign_item_in_cfg(cx, &*item) {
106 fn fold_foreign_mod<F>(cx: &mut Context<F>,
107 ast::ForeignMod {abi, view_items, items}: ast::ForeignMod)
108 -> ast::ForeignMod where
109 F: FnMut(&[ast::Attribute]) -> bool
113 view_items: view_items.into_iter().filter_map(|a| {
114 filter_view_item(cx, a).map(|x| cx.fold_view_item(x))
116 items: items.into_iter()
117 .filter_map(|a| filter_foreign_item(cx, a))
122 fn fold_item<F>(cx: &mut Context<F>, item: P<ast::Item>) -> SmallVector<P<ast::Item>> where
123 F: FnMut(&[ast::Attribute]) -> bool
125 if item_in_cfg(cx, &*item) {
126 SmallVector::one(item.map(|i| cx.fold_item_simple(i)))
132 fn fold_item_underscore<F>(cx: &mut Context<F>, item: ast::Item_) -> ast::Item_ where
133 F: FnMut(&[ast::Attribute]) -> bool
135 let item = match item {
136 ast::ItemImpl(u, a, b, c, impl_items) => {
137 let impl_items = impl_items.into_iter()
138 .filter(|ii| impl_item_in_cfg(cx, ii))
140 ast::ItemImpl(u, a, b, c, impl_items)
142 ast::ItemTrait(u, a, b, methods) => {
143 let methods = methods.into_iter()
144 .filter(|m| trait_method_in_cfg(cx, m))
146 ast::ItemTrait(u, a, b, methods)
148 ast::ItemStruct(def, generics) => {
149 ast::ItemStruct(fold_struct(cx, def), generics)
151 ast::ItemEnum(def, generics) => {
152 let variants = def.variants.into_iter().filter_map(|v| {
153 if !(cx.in_cfg)(v.node.attrs.as_slice()) {
156 Some(v.map(|Spanned {node: ast::Variant_ {id, name, attrs, kind,
157 disr_expr, vis}, span}| {
159 node: ast::Variant_ {
164 ast::TupleVariantKind(..) => kind,
165 ast::StructVariantKind(def) => {
166 ast::StructVariantKind(fold_struct(cx, def))
169 disr_expr: disr_expr,
177 ast::ItemEnum(ast::EnumDef {
178 variants: variants.collect(),
184 fold::noop_fold_item_underscore(item, cx)
187 fn fold_struct<F>(cx: &mut Context<F>, def: P<ast::StructDef>) -> P<ast::StructDef> where
188 F: FnMut(&[ast::Attribute]) -> bool
190 def.map(|ast::StructDef { fields, ctor_id }| {
192 fields: fields.into_iter().filter(|m| {
193 (cx.in_cfg)(m.node.attrs.as_slice())
200 fn retain_stmt<F>(cx: &mut Context<F>, stmt: &ast::Stmt) -> bool where
201 F: FnMut(&[ast::Attribute]) -> bool
204 ast::StmtDecl(ref decl, _) => {
206 ast::DeclItem(ref item) => {
207 item_in_cfg(cx, &**item)
216 fn fold_block<F>(cx: &mut Context<F>, b: P<ast::Block>) -> P<ast::Block> where
217 F: FnMut(&[ast::Attribute]) -> bool
219 b.map(|ast::Block {id, view_items, stmts, expr, rules, span}| {
220 let resulting_stmts: Vec<P<ast::Stmt>> =
221 stmts.into_iter().filter(|a| retain_stmt(cx, &**a)).collect();
222 let resulting_stmts = resulting_stmts.into_iter()
223 .flat_map(|stmt| cx.fold_stmt(stmt).into_iter())
225 let filtered_view_items = view_items.into_iter().filter_map(|a| {
226 filter_view_item(cx, a).map(|x| cx.fold_view_item(x))
230 view_items: filtered_view_items,
231 stmts: resulting_stmts,
232 expr: expr.map(|x| cx.fold_expr(x)),
239 fn fold_expr<F>(cx: &mut Context<F>, expr: P<ast::Expr>) -> P<ast::Expr> where
240 F: FnMut(&[ast::Attribute]) -> bool
242 expr.map(|ast::Expr {id, span, node}| {
243 fold::noop_fold_expr(ast::Expr {
246 ast::ExprMatch(m, arms, source) => {
247 ast::ExprMatch(m, arms.into_iter()
248 .filter(|a| (cx.in_cfg)(a.attrs.as_slice()))
258 fn item_in_cfg<F>(cx: &mut Context<F>, item: &ast::Item) -> bool where
259 F: FnMut(&[ast::Attribute]) -> bool
261 return (cx.in_cfg)(item.attrs.as_slice());
264 fn foreign_item_in_cfg<F>(cx: &mut Context<F>, item: &ast::ForeignItem) -> bool where
265 F: FnMut(&[ast::Attribute]) -> bool
267 return (cx.in_cfg)(item.attrs.as_slice());
270 fn view_item_in_cfg<F>(cx: &mut Context<F>, item: &ast::ViewItem) -> bool where
271 F: FnMut(&[ast::Attribute]) -> bool
273 return (cx.in_cfg)(item.attrs.as_slice());
276 fn trait_method_in_cfg<F>(cx: &mut Context<F>, meth: &ast::TraitItem) -> bool where
277 F: FnMut(&[ast::Attribute]) -> bool
280 ast::RequiredMethod(ref meth) => (cx.in_cfg)(meth.attrs.as_slice()),
281 ast::ProvidedMethod(ref meth) => (cx.in_cfg)(meth.attrs.as_slice()),
282 ast::TypeTraitItem(ref typ) => (cx.in_cfg)(typ.attrs.as_slice()),
286 fn impl_item_in_cfg<F>(cx: &mut Context<F>, impl_item: &ast::ImplItem) -> bool where
287 F: FnMut(&[ast::Attribute]) -> bool
290 ast::MethodImplItem(ref meth) => (cx.in_cfg)(meth.attrs.as_slice()),
291 ast::TypeImplItem(ref typ) => (cx.in_cfg)(typ.attrs.as_slice()),
295 // Determine if an item should be translated in the current crate
296 // configuration based on the item's attributes
297 fn in_cfg(diagnostic: &SpanHandler, cfg: &[P<ast::MetaItem>], attrs: &[ast::Attribute]) -> bool {
298 attrs.iter().all(|attr| {
299 let mis = match attr.node.value.node {
300 ast::MetaList(_, ref mis) if attr.check_name("cfg") => mis,
305 diagnostic.span_err(attr.span, "expected 1 cfg-pattern");
309 attr::cfg_matches(diagnostic, cfg, &*mis[0])