rustc_expand/
placeholders.rs

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