]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_builtin_macros/src/cfg_eval.rs
Add back missing doc
[rust.git] / compiler / rustc_builtin_macros / src / cfg_eval.rs
1 use crate::util::check_builtin_macro_attribute;
2
3 use rustc_ast as ast;
4 use rustc_ast::mut_visit::MutVisitor;
5 use rustc_ast::tokenstream::CanSynthesizeMissingTokens;
6 use rustc_ast::visit::Visitor;
7 use rustc_ast::{mut_visit, visit};
8 use rustc_ast::{AstLike, Attribute};
9 use rustc_expand::base::{Annotatable, ExtCtxt};
10 use rustc_expand::config::StripUnconfigured;
11 use rustc_expand::configure;
12 use rustc_parse::parser::ForceCollect;
13 use rustc_session::utils::FlattenNonterminals;
14
15 use rustc_ast::ptr::P;
16 use rustc_span::symbol::sym;
17 use rustc_span::Span;
18 use smallvec::SmallVec;
19
20 crate fn expand(
21     ecx: &mut ExtCtxt<'_>,
22     _span: Span,
23     meta_item: &ast::MetaItem,
24     annotatable: Annotatable,
25 ) -> Vec<Annotatable> {
26     check_builtin_macro_attribute(ecx, meta_item, sym::cfg_eval);
27     vec![cfg_eval(ecx, annotatable)]
28 }
29
30 crate fn cfg_eval(ecx: &ExtCtxt<'_>, annotatable: Annotatable) -> Annotatable {
31     CfgEval {
32         cfg: &mut StripUnconfigured {
33             sess: ecx.sess,
34             features: ecx.ecfg.features,
35             config_tokens: true,
36         },
37     }
38     .configure_annotatable(annotatable)
39     // Since the item itself has already been configured by the `InvocationCollector`,
40     // we know that fold result vector will contain exactly one element.
41     .unwrap()
42 }
43
44 struct CfgEval<'a, 'b> {
45     cfg: &'a mut StripUnconfigured<'b>,
46 }
47
48 fn flat_map_annotatable(
49     vis: &mut impl MutVisitor,
50     annotatable: Annotatable,
51 ) -> Option<Annotatable> {
52     match annotatable {
53         Annotatable::Item(item) => vis.flat_map_item(item).pop().map(Annotatable::Item),
54         Annotatable::TraitItem(item) => {
55             vis.flat_map_trait_item(item).pop().map(Annotatable::TraitItem)
56         }
57         Annotatable::ImplItem(item) => {
58             vis.flat_map_impl_item(item).pop().map(Annotatable::ImplItem)
59         }
60         Annotatable::ForeignItem(item) => {
61             vis.flat_map_foreign_item(item).pop().map(Annotatable::ForeignItem)
62         }
63         Annotatable::Stmt(stmt) => {
64             vis.flat_map_stmt(stmt.into_inner()).pop().map(P).map(Annotatable::Stmt)
65         }
66         Annotatable::Expr(mut expr) => {
67             vis.visit_expr(&mut expr);
68             Some(Annotatable::Expr(expr))
69         }
70         Annotatable::Arm(arm) => vis.flat_map_arm(arm).pop().map(Annotatable::Arm),
71         Annotatable::ExprField(field) => {
72             vis.flat_map_expr_field(field).pop().map(Annotatable::ExprField)
73         }
74         Annotatable::PatField(fp) => vis.flat_map_pat_field(fp).pop().map(Annotatable::PatField),
75         Annotatable::GenericParam(param) => {
76             vis.flat_map_generic_param(param).pop().map(Annotatable::GenericParam)
77         }
78         Annotatable::Param(param) => vis.flat_map_param(param).pop().map(Annotatable::Param),
79         Annotatable::FieldDef(sf) => vis.flat_map_field_def(sf).pop().map(Annotatable::FieldDef),
80         Annotatable::Variant(v) => vis.flat_map_variant(v).pop().map(Annotatable::Variant),
81     }
82 }
83
84 struct CfgFinder {
85     has_cfg_or_cfg_attr: bool,
86 }
87
88 impl CfgFinder {
89     fn has_cfg_or_cfg_attr(annotatable: &Annotatable) -> bool {
90         let mut finder = CfgFinder { has_cfg_or_cfg_attr: false };
91         match annotatable {
92             Annotatable::Item(item) => finder.visit_item(&item),
93             Annotatable::TraitItem(item) => finder.visit_assoc_item(&item, visit::AssocCtxt::Trait),
94             Annotatable::ImplItem(item) => finder.visit_assoc_item(&item, visit::AssocCtxt::Impl),
95             Annotatable::ForeignItem(item) => finder.visit_foreign_item(&item),
96             Annotatable::Stmt(stmt) => finder.visit_stmt(&stmt),
97             Annotatable::Expr(expr) => finder.visit_expr(&expr),
98             Annotatable::Arm(arm) => finder.visit_arm(&arm),
99             Annotatable::ExprField(field) => finder.visit_expr_field(&field),
100             Annotatable::PatField(field) => finder.visit_pat_field(&field),
101             Annotatable::GenericParam(param) => finder.visit_generic_param(&param),
102             Annotatable::Param(param) => finder.visit_param(&param),
103             Annotatable::FieldDef(field) => finder.visit_field_def(&field),
104             Annotatable::Variant(variant) => finder.visit_variant(&variant),
105         };
106         finder.has_cfg_or_cfg_attr
107     }
108 }
109
110 impl<'ast> visit::Visitor<'ast> for CfgFinder {
111     fn visit_attribute(&mut self, attr: &'ast Attribute) {
112         // We want short-circuiting behavior, so don't use the '|=' operator.
113         self.has_cfg_or_cfg_attr = self.has_cfg_or_cfg_attr
114             || attr
115                 .ident()
116                 .map_or(false, |ident| ident.name == sym::cfg || ident.name == sym::cfg_attr);
117     }
118 }
119
120 impl CfgEval<'_, '_> {
121     fn configure<T: AstLike>(&mut self, node: T) -> Option<T> {
122         self.cfg.configure(node)
123     }
124
125     fn configure_annotatable(&mut self, mut annotatable: Annotatable) -> Option<Annotatable> {
126         // Tokenizing and re-parsing the `Annotatable` can have a significant
127         // performance impact, so try to avoid it if possible
128         if !CfgFinder::has_cfg_or_cfg_attr(&annotatable) {
129             return Some(annotatable);
130         }
131
132         // The majority of parsed attribute targets will never need to have early cfg-expansion
133         // run (e.g. they are not part of a `#[derive]` or `#[cfg_eval]` macro inoput).
134         // Therefore, we normally do not capture the necessary information about `#[cfg]`
135         // and `#[cfg_attr]` attributes during parsing.
136         //
137         // Therefore, when we actually *do* run early cfg-expansion, we need to tokenize
138         // and re-parse the attribute target, this time capturing information about
139         // the location of `#[cfg]` and `#[cfg_attr]` in the token stream. The tokenization
140         // process is lossless, so this process is invisible to proc-macros.
141
142         // FIXME - get rid of this clone
143         let nt = annotatable.clone().into_nonterminal();
144
145         let mut orig_tokens = rustc_parse::nt_to_tokenstream(
146             &nt,
147             &self.cfg.sess.parse_sess,
148             CanSynthesizeMissingTokens::No,
149         );
150
151         // 'Flatten' all nonterminals (i.e. `TokenKind::Interpolated`)
152         // to `None`-delimited groups containing the corresponding tokens. This
153         // is normally delayed until the proc-macro server actually needs to
154         // provide a `TokenKind::Interpolated` to a proc-macro. We do this earlier,
155         // so that we can handle cases like:
156         //
157         // ```rust
158         // #[cfg_eval] #[cfg] $item
159         //```
160         //
161         // where `$item` is `#[cfg_attr] struct Foo {}`. We want to make
162         // sure to evaluate *all* `#[cfg]` and `#[cfg_attr]` attributes - the simplest
163         // way to do this is to do a single parse of a stream without any nonterminals.
164         let mut flatten = FlattenNonterminals {
165             nt_to_tokenstream: rustc_parse::nt_to_tokenstream,
166             parse_sess: &self.cfg.sess.parse_sess,
167             synthesize_tokens: CanSynthesizeMissingTokens::No,
168         };
169         orig_tokens = flatten.process_token_stream(orig_tokens);
170
171         // Re-parse the tokens, setting the `capture_cfg` flag to save extra information
172         // to the captured `AttrAnnotatedTokenStream` (specifically, we capture
173         // `AttrAnnotatedTokenTree::AttributesData` for all occurences of `#[cfg]` and `#[cfg_attr]`)
174         let mut parser =
175             rustc_parse::stream_to_parser(&self.cfg.sess.parse_sess, orig_tokens, None);
176         parser.capture_cfg = true;
177         annotatable = match annotatable {
178             Annotatable::Item(_) => {
179                 Annotatable::Item(parser.parse_item(ForceCollect::Yes).unwrap().unwrap())
180             }
181             Annotatable::TraitItem(_) => Annotatable::TraitItem(
182                 parser.parse_trait_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
183             ),
184             Annotatable::ImplItem(_) => Annotatable::ImplItem(
185                 parser.parse_impl_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
186             ),
187             Annotatable::ForeignItem(_) => Annotatable::ForeignItem(
188                 parser.parse_foreign_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
189             ),
190             Annotatable::Stmt(_) => {
191                 Annotatable::Stmt(P(parser.parse_stmt(ForceCollect::Yes).unwrap().unwrap()))
192             }
193             Annotatable::Expr(_) => Annotatable::Expr(parser.parse_expr_force_collect().unwrap()),
194             _ => unreachable!(),
195         };
196
197         // Now that we have our re-parsed `AttrAnnotatedTokenStream`, recursively configuring
198         // our attribute target will correctly the tokens as well.
199         flat_map_annotatable(self, annotatable)
200     }
201 }
202
203 impl MutVisitor for CfgEval<'_, '_> {
204     fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
205         self.cfg.configure_expr(expr);
206         mut_visit::noop_visit_expr(expr, self);
207     }
208
209     fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
210         let mut expr = configure!(self, expr);
211         mut_visit::noop_visit_expr(&mut expr, self);
212         Some(expr)
213     }
214
215     fn flat_map_generic_param(
216         &mut self,
217         param: ast::GenericParam,
218     ) -> SmallVec<[ast::GenericParam; 1]> {
219         mut_visit::noop_flat_map_generic_param(configure!(self, param), self)
220     }
221
222     fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
223         mut_visit::noop_flat_map_stmt(configure!(self, stmt), self)
224     }
225
226     fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
227         mut_visit::noop_flat_map_item(configure!(self, item), self)
228     }
229
230     fn flat_map_impl_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
231         mut_visit::noop_flat_map_assoc_item(configure!(self, item), self)
232     }
233
234     fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
235         mut_visit::noop_flat_map_assoc_item(configure!(self, item), self)
236     }
237
238     fn flat_map_foreign_item(
239         &mut self,
240         foreign_item: P<ast::ForeignItem>,
241     ) -> SmallVec<[P<ast::ForeignItem>; 1]> {
242         mut_visit::noop_flat_map_foreign_item(configure!(self, foreign_item), self)
243     }
244
245     fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
246         mut_visit::noop_flat_map_arm(configure!(self, arm), self)
247     }
248
249     fn flat_map_expr_field(&mut self, field: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
250         mut_visit::noop_flat_map_expr_field(configure!(self, field), self)
251     }
252
253     fn flat_map_pat_field(&mut self, fp: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
254         mut_visit::noop_flat_map_pat_field(configure!(self, fp), self)
255     }
256
257     fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
258         mut_visit::noop_flat_map_param(configure!(self, p), self)
259     }
260
261     fn flat_map_field_def(&mut self, sf: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
262         mut_visit::noop_flat_map_field_def(configure!(self, sf), self)
263     }
264
265     fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
266         mut_visit::noop_flat_map_variant(configure!(self, variant), self)
267     }
268 }