rustc_expand/
placeholders.rs

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