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