]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_expand/src/placeholders.rs
Auto merge of #87820 - elichai:patch-2, r=kennytm
[rust.git] / compiler / rustc_expand / src / placeholders.rs
1 use crate::expand::{AstFragment, AstFragmentKind};
2
3 use rustc_ast as ast;
4 use rustc_ast::mut_visit::*;
5 use rustc_ast::ptr::P;
6 use rustc_span::source_map::DUMMY_SP;
7 use rustc_span::symbol::Ident;
8
9 use smallvec::{smallvec, SmallVec};
10
11 use rustc_data_structures::fx::FxHashMap;
12
13 pub fn placeholder(
14     kind: AstFragmentKind,
15     id: ast::NodeId,
16     vis: Option<ast::Visibility>,
17 ) -> AstFragment {
18     fn mac_placeholder() -> ast::MacCall {
19         ast::MacCall {
20             path: ast::Path { span: DUMMY_SP, segments: Vec::new(), tokens: None },
21             args: P(ast::MacArgs::Empty),
22             prior_type_ascription: None,
23         }
24     }
25
26     let ident = Ident::invalid();
27     let attrs = Vec::new();
28     let vis = vis.unwrap_or(ast::Visibility {
29         span: DUMMY_SP,
30         kind: ast::VisibilityKind::Inherited,
31         tokens: None,
32     });
33     let span = DUMMY_SP;
34     let expr_placeholder = || {
35         P(ast::Expr {
36             id,
37             span,
38             attrs: ast::AttrVec::new(),
39             kind: ast::ExprKind::MacCall(mac_placeholder()),
40             tokens: None,
41         })
42     };
43     let ty =
44         || P(ast::Ty { id, kind: ast::TyKind::MacCall(mac_placeholder()), span, tokens: None });
45     let pat =
46         || P(ast::Pat { id, kind: ast::PatKind::MacCall(mac_placeholder()), span, tokens: None });
47
48     match kind {
49         AstFragmentKind::Expr => AstFragment::Expr(expr_placeholder()),
50         AstFragmentKind::OptExpr => AstFragment::OptExpr(Some(expr_placeholder())),
51         AstFragmentKind::Items => AstFragment::Items(smallvec![P(ast::Item {
52             id,
53             span,
54             ident,
55             vis,
56             attrs,
57             kind: ast::ItemKind::MacCall(mac_placeholder()),
58             tokens: None,
59         })]),
60         AstFragmentKind::TraitItems => AstFragment::TraitItems(smallvec![P(ast::AssocItem {
61             id,
62             span,
63             ident,
64             vis,
65             attrs,
66             kind: ast::AssocItemKind::MacCall(mac_placeholder()),
67             tokens: None,
68         })]),
69         AstFragmentKind::ImplItems => AstFragment::ImplItems(smallvec![P(ast::AssocItem {
70             id,
71             span,
72             ident,
73             vis,
74             attrs,
75             kind: ast::AssocItemKind::MacCall(mac_placeholder()),
76             tokens: None,
77         })]),
78         AstFragmentKind::ForeignItems => {
79             AstFragment::ForeignItems(smallvec![P(ast::ForeignItem {
80                 id,
81                 span,
82                 ident,
83                 vis,
84                 attrs,
85                 kind: ast::ForeignItemKind::MacCall(mac_placeholder()),
86                 tokens: None,
87             })])
88         }
89         AstFragmentKind::Pat => AstFragment::Pat(P(ast::Pat {
90             id,
91             span,
92             kind: ast::PatKind::MacCall(mac_placeholder()),
93             tokens: None,
94         })),
95         AstFragmentKind::Ty => AstFragment::Ty(P(ast::Ty {
96             id,
97             span,
98             kind: ast::TyKind::MacCall(mac_placeholder()),
99             tokens: None,
100         })),
101         AstFragmentKind::Stmts => AstFragment::Stmts(smallvec![{
102             let mac = P(ast::MacCallStmt {
103                 mac: mac_placeholder(),
104                 style: ast::MacStmtStyle::Braces,
105                 attrs: ast::AttrVec::new(),
106                 tokens: None,
107             });
108             ast::Stmt { id, span, kind: ast::StmtKind::MacCall(mac) }
109         }]),
110         AstFragmentKind::Arms => AstFragment::Arms(smallvec![ast::Arm {
111             attrs: Default::default(),
112             body: expr_placeholder(),
113             guard: None,
114             id,
115             pat: pat(),
116             span,
117             is_placeholder: true,
118         }]),
119         AstFragmentKind::Fields => AstFragment::Fields(smallvec![ast::ExprField {
120             attrs: Default::default(),
121             expr: expr_placeholder(),
122             id,
123             ident,
124             is_shorthand: false,
125             span,
126             is_placeholder: true,
127         }]),
128         AstFragmentKind::FieldPats => AstFragment::FieldPats(smallvec![ast::PatField {
129             attrs: Default::default(),
130             id,
131             ident,
132             is_shorthand: false,
133             pat: pat(),
134             span,
135             is_placeholder: true,
136         }]),
137         AstFragmentKind::GenericParams => AstFragment::GenericParams(smallvec![{
138             ast::GenericParam {
139                 attrs: Default::default(),
140                 bounds: Default::default(),
141                 id,
142                 ident,
143                 is_placeholder: true,
144                 kind: ast::GenericParamKind::Lifetime,
145             }
146         }]),
147         AstFragmentKind::Params => AstFragment::Params(smallvec![ast::Param {
148             attrs: Default::default(),
149             id,
150             pat: pat(),
151             span,
152             ty: ty(),
153             is_placeholder: true,
154         }]),
155         AstFragmentKind::StructFields => AstFragment::StructFields(smallvec![ast::FieldDef {
156             attrs: Default::default(),
157             id,
158             ident: None,
159             span,
160             ty: ty(),
161             vis,
162             is_placeholder: true,
163         }]),
164         AstFragmentKind::Variants => AstFragment::Variants(smallvec![ast::Variant {
165             attrs: Default::default(),
166             data: ast::VariantData::Struct(Default::default(), false),
167             disr_expr: None,
168             id,
169             ident,
170             span,
171             vis,
172             is_placeholder: true,
173         }]),
174     }
175 }
176
177 #[derive(Default)]
178 pub struct PlaceholderExpander {
179     expanded_fragments: FxHashMap<ast::NodeId, AstFragment>,
180 }
181
182 impl PlaceholderExpander {
183     pub fn add(&mut self, id: ast::NodeId, mut fragment: AstFragment) {
184         fragment.mut_visit_with(self);
185         self.expanded_fragments.insert(id, fragment);
186     }
187
188     fn remove(&mut self, id: ast::NodeId) -> AstFragment {
189         self.expanded_fragments.remove(&id).unwrap()
190     }
191 }
192
193 impl MutVisitor for PlaceholderExpander {
194     fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
195         if arm.is_placeholder {
196             self.remove(arm.id).make_arms()
197         } else {
198             noop_flat_map_arm(arm, self)
199         }
200     }
201
202     fn flat_map_expr_field(&mut self, field: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
203         if field.is_placeholder {
204             self.remove(field.id).make_expr_fields()
205         } else {
206             noop_flat_map_expr_field(field, self)
207         }
208     }
209
210     fn flat_map_pat_field(&mut self, fp: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
211         if fp.is_placeholder {
212             self.remove(fp.id).make_pat_fields()
213         } else {
214             noop_flat_map_pat_field(fp, self)
215         }
216     }
217
218     fn flat_map_generic_param(
219         &mut self,
220         param: ast::GenericParam,
221     ) -> SmallVec<[ast::GenericParam; 1]> {
222         if param.is_placeholder {
223             self.remove(param.id).make_generic_params()
224         } else {
225             noop_flat_map_generic_param(param, self)
226         }
227     }
228
229     fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
230         if p.is_placeholder {
231             self.remove(p.id).make_params()
232         } else {
233             noop_flat_map_param(p, self)
234         }
235     }
236
237     fn flat_map_field_def(&mut self, sf: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
238         if sf.is_placeholder {
239             self.remove(sf.id).make_field_defs()
240         } else {
241             noop_flat_map_field_def(sf, self)
242         }
243     }
244
245     fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
246         if variant.is_placeholder {
247             self.remove(variant.id).make_variants()
248         } else {
249             noop_flat_map_variant(variant, self)
250         }
251     }
252
253     fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
254         match item.kind {
255             ast::ItemKind::MacCall(_) => self.remove(item.id).make_items(),
256             _ => noop_flat_map_item(item, self),
257         }
258     }
259
260     fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
261         match item.kind {
262             ast::AssocItemKind::MacCall(_) => self.remove(item.id).make_trait_items(),
263             _ => noop_flat_map_assoc_item(item, self),
264         }
265     }
266
267     fn flat_map_impl_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
268         match item.kind {
269             ast::AssocItemKind::MacCall(_) => self.remove(item.id).make_impl_items(),
270             _ => noop_flat_map_assoc_item(item, self),
271         }
272     }
273
274     fn flat_map_foreign_item(
275         &mut self,
276         item: P<ast::ForeignItem>,
277     ) -> SmallVec<[P<ast::ForeignItem>; 1]> {
278         match item.kind {
279             ast::ForeignItemKind::MacCall(_) => self.remove(item.id).make_foreign_items(),
280             _ => noop_flat_map_foreign_item(item, self),
281         }
282     }
283
284     fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
285         match expr.kind {
286             ast::ExprKind::MacCall(_) => *expr = self.remove(expr.id).make_expr(),
287             _ => noop_visit_expr(expr, self),
288         }
289     }
290
291     fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
292         match expr.kind {
293             ast::ExprKind::MacCall(_) => self.remove(expr.id).make_opt_expr(),
294             _ => noop_filter_map_expr(expr, self),
295         }
296     }
297
298     fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
299         let (style, mut stmts) = match stmt.kind {
300             ast::StmtKind::MacCall(mac) => (mac.style, self.remove(stmt.id).make_stmts()),
301             _ => return noop_flat_map_stmt(stmt, self),
302         };
303
304         if style == ast::MacStmtStyle::Semicolon {
305             // Implement the proposal described in
306             // https://github.com/rust-lang/rust/issues/61733#issuecomment-509626449
307             //
308             // The macro invocation expands to the list of statements. If the
309             // list of statements is empty, then 'parse' the trailing semicolon
310             // on the original invocation as an empty statement. That is:
311             //
312             // `empty();` is parsed as a single `StmtKind::Empty`
313             //
314             // If the list of statements is non-empty, see if the final
315             // statement already has a trailing semicolon.
316             //
317             // If it doesn't have a semicolon, then 'parse' the trailing
318             // semicolon from the invocation as part of the final statement,
319             // using `stmt.add_trailing_semicolon()`
320             //
321             // If it does have a semicolon, then 'parse' the trailing semicolon
322             // from the invocation as a new StmtKind::Empty
323
324             // FIXME: We will need to preserve the original semicolon token and
325             // span as part of #15701
326             let empty_stmt =
327                 ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Empty, span: DUMMY_SP };
328
329             if let Some(stmt) = stmts.pop() {
330                 if stmt.has_trailing_semicolon() {
331                     stmts.push(stmt);
332                     stmts.push(empty_stmt);
333                 } else {
334                     stmts.push(stmt.add_trailing_semicolon());
335                 }
336             } else {
337                 stmts.push(empty_stmt);
338             }
339         }
340
341         stmts
342     }
343
344     fn visit_pat(&mut self, pat: &mut P<ast::Pat>) {
345         match pat.kind {
346             ast::PatKind::MacCall(_) => *pat = self.remove(pat.id).make_pat(),
347             _ => noop_visit_pat(pat, self),
348         }
349     }
350
351     fn visit_ty(&mut self, ty: &mut P<ast::Ty>) {
352         match ty.kind {
353             ast::TyKind::MacCall(_) => *ty = self.remove(ty.id).make_ty(),
354             _ => noop_visit_ty(ty, self),
355         }
356     }
357 }