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    }
192}
193
194#[derive(Default)]
195pub(crate) struct PlaceholderExpander {
196    expanded_fragments: FxHashMap<ast::NodeId, AstFragment>,
197}
198
199impl PlaceholderExpander {
200    pub(crate) fn add(&mut self, id: ast::NodeId, mut fragment: AstFragment) {
201        fragment.mut_visit_with(self);
202        self.expanded_fragments.insert(id, fragment);
203    }
204
205    fn remove(&mut self, id: ast::NodeId) -> AstFragment {
206        self.expanded_fragments.remove(&id).unwrap()
207    }
208}
209
210impl MutVisitor for PlaceholderExpander {
211    fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
212        if arm.is_placeholder {
213            self.remove(arm.id).make_arms()
214        } else {
215            walk_flat_map_arm(self, arm)
216        }
217    }
218
219    fn flat_map_expr_field(&mut self, field: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
220        if field.is_placeholder {
221            self.remove(field.id).make_expr_fields()
222        } else {
223            walk_flat_map_expr_field(self, field)
224        }
225    }
226
227    fn flat_map_pat_field(&mut self, fp: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
228        if fp.is_placeholder {
229            self.remove(fp.id).make_pat_fields()
230        } else {
231            walk_flat_map_pat_field(self, fp)
232        }
233    }
234
235    fn flat_map_generic_param(
236        &mut self,
237        param: ast::GenericParam,
238    ) -> SmallVec<[ast::GenericParam; 1]> {
239        if param.is_placeholder {
240            self.remove(param.id).make_generic_params()
241        } else {
242            walk_flat_map_generic_param(self, param)
243        }
244    }
245
246    fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
247        if p.is_placeholder {
248            self.remove(p.id).make_params()
249        } else {
250            walk_flat_map_param(self, p)
251        }
252    }
253
254    fn flat_map_field_def(&mut self, sf: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
255        if sf.is_placeholder {
256            self.remove(sf.id).make_field_defs()
257        } else {
258            walk_flat_map_field_def(self, sf)
259        }
260    }
261
262    fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
263        if variant.is_placeholder {
264            self.remove(variant.id).make_variants()
265        } else {
266            walk_flat_map_variant(self, variant)
267        }
268    }
269
270    fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
271        match item.kind {
272            ast::ItemKind::MacCall(_) => self.remove(item.id).make_items(),
273            _ => walk_flat_map_item(self, item),
274        }
275    }
276
277    fn flat_map_assoc_item(
278        &mut self,
279        item: P<ast::AssocItem>,
280        ctxt: AssocCtxt,
281    ) -> SmallVec<[P<ast::AssocItem>; 1]> {
282        match item.kind {
283            ast::AssocItemKind::MacCall(_) => {
284                let it = self.remove(item.id);
285                match ctxt {
286                    AssocCtxt::Trait => it.make_trait_items(),
287                    AssocCtxt::Impl => it.make_impl_items(),
288                }
289            }
290            _ => walk_flat_map_assoc_item(self, item, ctxt),
291        }
292    }
293
294    fn flat_map_foreign_item(
295        &mut self,
296        item: P<ast::ForeignItem>,
297    ) -> SmallVec<[P<ast::ForeignItem>; 1]> {
298        match item.kind {
299            ast::ForeignItemKind::MacCall(_) => self.remove(item.id).make_foreign_items(),
300            _ => walk_flat_map_foreign_item(self, item),
301        }
302    }
303
304    fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
305        match expr.kind {
306            ast::ExprKind::MacCall(_) => *expr = self.remove(expr.id).make_expr(),
307            _ => walk_expr(self, expr),
308        }
309    }
310
311    fn visit_method_receiver_expr(&mut self, expr: &mut P<ast::Expr>) {
312        match expr.kind {
313            ast::ExprKind::MacCall(_) => *expr = self.remove(expr.id).make_method_receiver_expr(),
314            _ => walk_expr(self, expr),
315        }
316    }
317
318    fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
319        match expr.kind {
320            ast::ExprKind::MacCall(_) => self.remove(expr.id).make_opt_expr(),
321            _ => noop_filter_map_expr(self, expr),
322        }
323    }
324
325    fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
326        let (style, mut stmts) = match stmt.kind {
327            ast::StmtKind::MacCall(mac) => (mac.style, self.remove(stmt.id).make_stmts()),
328            _ => return walk_flat_map_stmt(self, stmt),
329        };
330
331        if style == ast::MacStmtStyle::Semicolon {
332            // Implement the proposal described in
333            // https://github.com/rust-lang/rust/issues/61733#issuecomment-509626449
334            //
335            // The macro invocation expands to the list of statements. If the
336            // list of statements is empty, then 'parse' the trailing semicolon
337            // on the original invocation as an empty statement. That is:
338            //
339            // `empty();` is parsed as a single `StmtKind::Empty`
340            //
341            // If the list of statements is non-empty, see if the final
342            // statement already has a trailing semicolon.
343            //
344            // If it doesn't have a semicolon, then 'parse' the trailing
345            // semicolon from the invocation as part of the final statement,
346            // using `stmt.add_trailing_semicolon()`
347            //
348            // If it does have a semicolon, then 'parse' the trailing semicolon
349            // from the invocation as a new StmtKind::Empty
350
351            // FIXME: We will need to preserve the original semicolon token and
352            // span as part of #15701
353            let empty_stmt =
354                ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Empty, span: DUMMY_SP };
355
356            if let Some(stmt) = stmts.pop() {
357                if stmt.has_trailing_semicolon() {
358                    stmts.push(stmt);
359                    stmts.push(empty_stmt);
360                } else {
361                    stmts.push(stmt.add_trailing_semicolon());
362                }
363            } else {
364                stmts.push(empty_stmt);
365            }
366        }
367
368        stmts
369    }
370
371    fn visit_pat(&mut self, pat: &mut P<ast::Pat>) {
372        match pat.kind {
373            ast::PatKind::MacCall(_) => *pat = self.remove(pat.id).make_pat(),
374            _ => walk_pat(self, pat),
375        }
376    }
377
378    fn visit_ty(&mut self, ty: &mut P<ast::Ty>) {
379        match ty.kind {
380            ast::TyKind::MacCall(_) => *ty = self.remove(ty.id).make_ty(),
381            _ => walk_ty(self, ty),
382        }
383    }
384
385    fn visit_crate(&mut self, krate: &mut ast::Crate) {
386        if krate.is_placeholder {
387            *krate = self.remove(krate.id).make_crate();
388        } else {
389            walk_crate(self, krate)
390        }
391    }
392}