1 use crate::base::ExtCtxt;
2 use crate::expand::{AstFragment, AstFragmentKind};
5 use rustc_ast::mut_visit::*;
7 use rustc_span::source_map::DUMMY_SP;
8 use rustc_span::symbol::Ident;
10 use smallvec::{smallvec, SmallVec};
12 use rustc_data_structures::fx::FxHashMap;
15 kind: AstFragmentKind,
17 vis: Option<ast::Visibility>,
19 fn mac_placeholder() -> 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,
27 let ident = Ident::invalid();
28 let attrs = Vec::new();
29 let vis = vis.unwrap_or(ast::Visibility {
31 kind: ast::VisibilityKind::Inherited,
35 let expr_placeholder = || {
39 attrs: ast::AttrVec::new(),
40 kind: ast::ExprKind::MacCall(mac_placeholder()),
45 || P(ast::Ty { id, kind: ast::TyKind::MacCall(mac_placeholder()), span, tokens: None });
47 || P(ast::Pat { id, kind: ast::PatKind::MacCall(mac_placeholder()), span, tokens: None });
50 AstFragmentKind::Expr => AstFragment::Expr(expr_placeholder()),
51 AstFragmentKind::OptExpr => AstFragment::OptExpr(Some(expr_placeholder())),
52 AstFragmentKind::Items => AstFragment::Items(smallvec![P(ast::Item {
58 kind: ast::ItemKind::MacCall(mac_placeholder()),
61 AstFragmentKind::TraitItems => AstFragment::TraitItems(smallvec![P(ast::AssocItem {
67 kind: ast::AssocItemKind::MacCall(mac_placeholder()),
70 AstFragmentKind::ImplItems => AstFragment::ImplItems(smallvec![P(ast::AssocItem {
76 kind: ast::AssocItemKind::MacCall(mac_placeholder()),
79 AstFragmentKind::ForeignItems => {
80 AstFragment::ForeignItems(smallvec![P(ast::ForeignItem {
86 kind: ast::ForeignItemKind::MacCall(mac_placeholder()),
90 AstFragmentKind::Pat => AstFragment::Pat(P(ast::Pat {
93 kind: ast::PatKind::MacCall(mac_placeholder()),
96 AstFragmentKind::Ty => AstFragment::Ty(P(ast::Ty {
99 kind: ast::TyKind::MacCall(mac_placeholder()),
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(),
109 ast::Stmt { id, span, kind: ast::StmtKind::MacCall(mac) }
111 AstFragmentKind::Arms => AstFragment::Arms(smallvec![ast::Arm {
112 attrs: Default::default(),
113 body: expr_placeholder(),
118 is_placeholder: true,
120 AstFragmentKind::Fields => AstFragment::Fields(smallvec![ast::Field {
121 attrs: Default::default(),
122 expr: expr_placeholder(),
127 is_placeholder: true,
129 AstFragmentKind::FieldPats => AstFragment::FieldPats(smallvec![ast::FieldPat {
130 attrs: Default::default(),
136 is_placeholder: true,
138 AstFragmentKind::GenericParams => AstFragment::GenericParams(smallvec![{
140 attrs: Default::default(),
141 bounds: Default::default(),
144 is_placeholder: true,
145 kind: ast::GenericParamKind::Lifetime,
148 AstFragmentKind::Params => AstFragment::Params(smallvec![ast::Param {
149 attrs: Default::default(),
154 is_placeholder: true,
156 AstFragmentKind::StructFields => AstFragment::StructFields(smallvec![ast::StructField {
157 attrs: Default::default(),
163 is_placeholder: true,
165 AstFragmentKind::Variants => AstFragment::Variants(smallvec![ast::Variant {
166 attrs: Default::default(),
167 data: ast::VariantData::Struct(Default::default(), false),
173 is_placeholder: true,
178 pub struct PlaceholderExpander<'a, 'b> {
179 expanded_fragments: FxHashMap<ast::NodeId, AstFragment>,
180 cx: &'a mut ExtCtxt<'b>,
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 }
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);
194 fn remove(&mut self, id: ast::NodeId) -> AstFragment {
195 self.expanded_fragments.remove(&id).unwrap()
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()
204 noop_flat_map_arm(arm, self)
208 fn flat_map_field(&mut self, field: ast::Field) -> SmallVec<[ast::Field; 1]> {
209 if field.is_placeholder {
210 self.remove(field.id).make_fields()
212 noop_flat_map_field(field, self)
216 fn flat_map_field_pattern(&mut self, fp: ast::FieldPat) -> SmallVec<[ast::FieldPat; 1]> {
217 if fp.is_placeholder {
218 self.remove(fp.id).make_field_patterns()
220 noop_flat_map_field_pattern(fp, self)
224 fn flat_map_generic_param(
226 param: ast::GenericParam,
227 ) -> SmallVec<[ast::GenericParam; 1]> {
228 if param.is_placeholder {
229 self.remove(param.id).make_generic_params()
231 noop_flat_map_generic_param(param, self)
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()
239 noop_flat_map_param(p, self)
243 fn flat_map_struct_field(&mut self, sf: ast::StructField) -> SmallVec<[ast::StructField; 1]> {
244 if sf.is_placeholder {
245 self.remove(sf.id).make_struct_fields()
247 noop_flat_map_struct_field(sf, self)
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()
255 noop_flat_map_variant(variant, self)
259 fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
261 ast::ItemKind::MacCall(_) => self.remove(item.id).make_items(),
262 _ => noop_flat_map_item(item, self),
266 fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
268 ast::AssocItemKind::MacCall(_) => self.remove(item.id).make_trait_items(),
269 _ => noop_flat_map_assoc_item(item, self),
273 fn flat_map_impl_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
275 ast::AssocItemKind::MacCall(_) => self.remove(item.id).make_impl_items(),
276 _ => noop_flat_map_assoc_item(item, self),
280 fn flat_map_foreign_item(
282 item: P<ast::ForeignItem>,
283 ) -> SmallVec<[P<ast::ForeignItem>; 1]> {
285 ast::ForeignItemKind::MacCall(_) => self.remove(item.id).make_foreign_items(),
286 _ => noop_flat_map_foreign_item(item, self),
290 fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
292 ast::ExprKind::MacCall(_) => *expr = self.remove(expr.id).make_expr(),
293 _ => noop_visit_expr(expr, self),
297 fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
299 ast::ExprKind::MacCall(_) => self.remove(expr.id).make_opt_expr(),
300 _ => noop_filter_map_expr(expr, self),
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),
310 if style == ast::MacStmtStyle::Semicolon {
311 // Implement the proposal described in
312 // https://github.com/rust-lang/rust/issues/61733#issuecomment-509626449
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:
318 // `empty();` is parsed as a single `StmtKind::Empty`
320 // If the list of statements is non-empty, see if the final
321 // statement already has a trailing semicolon.
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()`
327 // If it does have a semicolon, then 'parse' the trailing semicolon
328 // from the invocation as a new StmtKind::Empty
330 // FIXME: We will need to preserve the original semicolon token and
331 // span as part of #15701
333 ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Empty, span: DUMMY_SP };
335 if let Some(stmt) = stmts.pop() {
336 if stmt.has_trailing_semicolon() {
338 stmts.push(empty_stmt);
340 stmts.push(stmt.add_trailing_semicolon());
343 stmts.push(empty_stmt);
350 fn visit_pat(&mut self, pat: &mut P<ast::Pat>) {
352 ast::PatKind::MacCall(_) => *pat = self.remove(pat.id).make_pat(),
353 _ => noop_visit_pat(pat, self),
357 fn visit_ty(&mut self, ty: &mut P<ast::Ty>) {
359 ast::TyKind::MacCall(_) => *ty = self.remove(ty.id).make_ty(),
360 _ => noop_visit_ty(ty, self),
364 fn visit_block(&mut self, block: &mut P<ast::Block>) {
365 noop_visit_block(block, self);
367 for stmt in block.stmts.iter_mut() {
369 assert_eq!(stmt.id, ast::DUMMY_NODE_ID);
370 stmt.id = self.cx.resolver.next_node_id();