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