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