]> git.lizzy.rs Git - rust.git/blob - src/librustc/front/config.rs
Honor hidden doc attribute of derivable trait methods
[rust.git] / src / librustc / front / config.rs
1 // Copyright 2012 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.
4 //
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.
10
11 use syntax::fold::Folder;
12 use syntax::{ast, fold, attr};
13 use syntax::codemap;
14
15 struct Context<'a> {
16     in_cfg: |attrs: &[ast::Attribute]|: 'a -> bool,
17 }
18
19 // Support conditional compilation by transforming the AST, stripping out
20 // any items that do not belong in the current configuration
21 pub fn strip_unconfigured_items(krate: ast::Crate) -> ast::Crate {
22     let config = krate.config.clone();
23     strip_items(krate, |attrs| in_cfg(config.as_slice(), attrs))
24 }
25
26 impl<'a> fold::Folder for Context<'a> {
27     fn fold_mod(&mut self, module: &ast::Mod) -> ast::Mod {
28         fold_mod(self, module)
29     }
30     fn fold_block(&mut self, block: ast::P<ast::Block>) -> ast::P<ast::Block> {
31         fold_block(self, block)
32     }
33     fn fold_foreign_mod(&mut self, foreign_mod: &ast::ForeignMod) -> ast::ForeignMod {
34         fold_foreign_mod(self, foreign_mod)
35     }
36     fn fold_item_underscore(&mut self, item: &ast::Item_) -> ast::Item_ {
37         fold_item_underscore(self, item)
38     }
39 }
40
41 pub fn strip_items(krate: ast::Crate,
42                    in_cfg: |attrs: &[ast::Attribute]| -> bool)
43                    -> ast::Crate {
44     let mut ctxt = Context {
45         in_cfg: in_cfg,
46     };
47     ctxt.fold_crate(krate)
48 }
49
50 fn filter_view_item<'r>(cx: &Context, view_item: &'r ast::ViewItem)
51                         -> Option<&'r ast::ViewItem> {
52     if view_item_in_cfg(cx, view_item) {
53         Some(view_item)
54     } else {
55         None
56     }
57 }
58
59 fn fold_mod(cx: &mut Context, m: &ast::Mod) -> ast::Mod {
60     let filtered_items: Vec<&@ast::Item> = m.items.iter()
61             .filter(|&a| item_in_cfg(cx, *a))
62             .collect();
63     let flattened_items = filtered_items.move_iter()
64             .flat_map(|&x| cx.fold_item(x).move_iter())
65             .collect();
66     let filtered_view_items = m.view_items.iter().filter_map(|a| {
67         filter_view_item(cx, a).map(|x| cx.fold_view_item(x))
68     }).collect();
69     ast::Mod {
70         view_items: filtered_view_items,
71         items: flattened_items
72     }
73 }
74
75 fn filter_foreign_item(cx: &Context, item: @ast::ForeignItem)
76                        -> Option<@ast::ForeignItem> {
77     if foreign_item_in_cfg(cx, item) {
78         Some(item)
79     } else {
80         None
81     }
82 }
83
84 fn fold_foreign_mod(cx: &mut Context, nm: &ast::ForeignMod) -> ast::ForeignMod {
85     let filtered_items = nm.items
86                            .iter()
87                            .filter_map(|a| filter_foreign_item(cx, *a))
88                            .collect();
89     let filtered_view_items = nm.view_items.iter().filter_map(|a| {
90         filter_view_item(cx, a).map(|x| cx.fold_view_item(x))
91     }).collect();
92     ast::ForeignMod {
93         abi: nm.abi,
94         view_items: filtered_view_items,
95         items: filtered_items
96     }
97 }
98
99 fn fold_item_underscore(cx: &mut Context, item: &ast::Item_) -> ast::Item_ {
100     let item = match *item {
101         ast::ItemImpl(ref a, ref b, c, ref methods) => {
102             let methods = methods.iter().filter(|m| method_in_cfg(cx, **m))
103                 .map(|x| *x).collect();
104             ast::ItemImpl((*a).clone(), (*b).clone(), c, methods)
105         }
106         ast::ItemTrait(ref a, b, ref c, ref methods) => {
107             let methods = methods.iter()
108                                  .filter(|m| trait_method_in_cfg(cx, *m) )
109                                  .map(|x| (*x).clone())
110                                  .collect();
111             ast::ItemTrait((*a).clone(), b, (*c).clone(), methods)
112         }
113         ast::ItemStruct(def, ref generics) => {
114             ast::ItemStruct(fold_struct(cx, def), generics.clone())
115         }
116         ast::ItemEnum(ref def, ref generics) => {
117             let mut variants = def.variants.iter().map(|c| c.clone()).
118             filter_map(|v| {
119                 if !(cx.in_cfg)(v.node.attrs.as_slice()) {
120                     None
121                 } else {
122                     Some(match v.node.kind {
123                                 ast::TupleVariantKind(..) => v,
124                                 ast::StructVariantKind(def) => {
125                                     let def = fold_struct(cx, def);
126                                     @codemap::Spanned {
127                                         node: ast::Variant_ {
128                                             kind: ast::StructVariantKind(def),
129                                             ..v.node.clone()
130                                         },
131                                         ..*v
132                                     }
133                                 }
134                             })
135                     }
136                 });
137             ast::ItemEnum(ast::EnumDef {
138                 variants: variants.collect(),
139             }, generics.clone())
140         }
141         ref item => item.clone(),
142     };
143
144     fold::noop_fold_item_underscore(&item, cx)
145 }
146
147 fn fold_struct(cx: &Context, def: &ast::StructDef) -> @ast::StructDef {
148     let mut fields = def.fields.iter().map(|c| c.clone()).filter(|m| {
149         (cx.in_cfg)(m.node.attrs.as_slice())
150     });
151     @ast::StructDef {
152         fields: fields.collect(),
153         ctor_id: def.ctor_id,
154         super_struct: def.super_struct.clone(),
155         is_virtual: def.is_virtual,
156     }
157 }
158
159 fn retain_stmt(cx: &Context, stmt: @ast::Stmt) -> bool {
160     match stmt.node {
161       ast::StmtDecl(decl, _) => {
162         match decl.node {
163           ast::DeclItem(item) => {
164             item_in_cfg(cx, item)
165           }
166           _ => true
167         }
168       }
169       _ => true
170     }
171 }
172
173 fn fold_block(cx: &mut Context, b: ast::P<ast::Block>) -> ast::P<ast::Block> {
174     let resulting_stmts: Vec<&@ast::Stmt> =
175         b.stmts.iter().filter(|&a| retain_stmt(cx, *a)).collect();
176     let resulting_stmts = resulting_stmts.move_iter()
177         .flat_map(|&stmt| cx.fold_stmt(stmt).move_iter())
178         .collect();
179     let filtered_view_items = b.view_items.iter().filter_map(|a| {
180         filter_view_item(cx, a).map(|x| cx.fold_view_item(x))
181     }).collect();
182     ast::P(ast::Block {
183         view_items: filtered_view_items,
184         stmts: resulting_stmts,
185         expr: b.expr.map(|x| cx.fold_expr(x)),
186         id: b.id,
187         rules: b.rules,
188         span: b.span,
189     })
190 }
191
192 fn item_in_cfg(cx: &Context, item: &ast::Item) -> bool {
193     return (cx.in_cfg)(item.attrs.as_slice());
194 }
195
196 fn foreign_item_in_cfg(cx: &Context, item: &ast::ForeignItem) -> bool {
197     return (cx.in_cfg)(item.attrs.as_slice());
198 }
199
200 fn view_item_in_cfg(cx: &Context, item: &ast::ViewItem) -> bool {
201     return (cx.in_cfg)(item.attrs.as_slice());
202 }
203
204 fn method_in_cfg(cx: &Context, meth: &ast::Method) -> bool {
205     return (cx.in_cfg)(meth.attrs.as_slice());
206 }
207
208 fn trait_method_in_cfg(cx: &Context, meth: &ast::TraitMethod) -> bool {
209     match *meth {
210         ast::Required(ref meth) => (cx.in_cfg)(meth.attrs.as_slice()),
211         ast::Provided(meth) => (cx.in_cfg)(meth.attrs.as_slice())
212     }
213 }
214
215 // Determine if an item should be translated in the current crate
216 // configuration based on the item's attributes
217 fn in_cfg(cfg: &[@ast::MetaItem], attrs: &[ast::Attribute]) -> bool {
218     attr::test_cfg(cfg, attrs.iter().map(|x| *x))
219 }
220