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