1 use crate::expand::{AstFragment, AstFragmentKind};
3 use rustc_ast::mut_visit::*;
5 use rustc_data_structures::fx::FxHashMap;
6 use rustc_span::source_map::DUMMY_SP;
7 use rustc_span::symbol::Ident;
8 use smallvec::{smallvec, SmallVec};
12 kind: AstFragmentKind,
14 vis: Option<ast::Visibility>,
16 fn mac_placeholder() -> P<ast::MacCall> {
18 path: ast::Path { span: DUMMY_SP, segments: ThinVec::new(), tokens: None },
19 args: P(ast::DelimArgs {
20 dspan: ast::tokenstream::DelimSpan::dummy(),
21 delim: ast::MacDelimiter::Parenthesis,
22 tokens: ast::tokenstream::TokenStream::new(Vec::new()),
24 prior_type_ascription: None,
28 let ident = Ident::empty();
29 let attrs = ast::AttrVec::new();
30 let vis = vis.unwrap_or(ast::Visibility {
32 kind: ast::VisibilityKind::Inherited,
36 let expr_placeholder = || {
40 attrs: ast::AttrVec::new(),
41 kind: ast::ExprKind::MacCall(mac_placeholder()),
46 || P(ast::Ty { id, kind: ast::TyKind::MacCall(mac_placeholder()), span, tokens: None });
48 || P(ast::Pat { id, kind: ast::PatKind::MacCall(mac_placeholder()), span, tokens: None });
51 AstFragmentKind::Crate => AstFragment::Crate(ast::Crate {
52 attrs: Default::default(),
53 items: Default::default(),
54 spans: ast::ModSpans { inner_span: span, ..Default::default() },
58 AstFragmentKind::Expr => AstFragment::Expr(expr_placeholder()),
59 AstFragmentKind::OptExpr => AstFragment::OptExpr(Some(expr_placeholder())),
60 AstFragmentKind::MethodReceiverExpr => AstFragment::MethodReceiverExpr(expr_placeholder()),
61 AstFragmentKind::Items => AstFragment::Items(smallvec![P(ast::Item {
67 kind: ast::ItemKind::MacCall(mac_placeholder()),
70 AstFragmentKind::TraitItems => AstFragment::TraitItems(smallvec![P(ast::AssocItem {
76 kind: ast::AssocItemKind::MacCall(mac_placeholder()),
79 AstFragmentKind::ImplItems => AstFragment::ImplItems(smallvec![P(ast::AssocItem {
85 kind: ast::AssocItemKind::MacCall(mac_placeholder()),
88 AstFragmentKind::ForeignItems => {
89 AstFragment::ForeignItems(smallvec![P(ast::ForeignItem {
95 kind: ast::ForeignItemKind::MacCall(mac_placeholder()),
99 AstFragmentKind::Pat => AstFragment::Pat(P(ast::Pat {
102 kind: ast::PatKind::MacCall(mac_placeholder()),
105 AstFragmentKind::Ty => AstFragment::Ty(P(ast::Ty {
108 kind: ast::TyKind::MacCall(mac_placeholder()),
111 AstFragmentKind::Stmts => AstFragment::Stmts(smallvec![{
112 let mac = P(ast::MacCallStmt {
113 mac: mac_placeholder(),
114 style: ast::MacStmtStyle::Braces,
115 attrs: ast::AttrVec::new(),
118 ast::Stmt { id, span, kind: ast::StmtKind::MacCall(mac) }
120 AstFragmentKind::Arms => AstFragment::Arms(smallvec![ast::Arm {
121 attrs: Default::default(),
122 body: expr_placeholder(),
127 is_placeholder: true,
129 AstFragmentKind::ExprFields => AstFragment::ExprFields(smallvec![ast::ExprField {
130 attrs: Default::default(),
131 expr: expr_placeholder(),
136 is_placeholder: true,
138 AstFragmentKind::PatFields => AstFragment::PatFields(smallvec![ast::PatField {
139 attrs: Default::default(),
145 is_placeholder: true,
147 AstFragmentKind::GenericParams => AstFragment::GenericParams(smallvec![{
149 attrs: Default::default(),
150 bounds: Default::default(),
153 is_placeholder: true,
154 kind: ast::GenericParamKind::Lifetime,
158 AstFragmentKind::Params => AstFragment::Params(smallvec![ast::Param {
159 attrs: Default::default(),
164 is_placeholder: true,
166 AstFragmentKind::FieldDefs => AstFragment::FieldDefs(smallvec![ast::FieldDef {
167 attrs: Default::default(),
173 is_placeholder: true,
175 AstFragmentKind::Variants => AstFragment::Variants(smallvec![ast::Variant {
176 attrs: Default::default(),
177 data: ast::VariantData::Struct(Default::default(), false),
183 is_placeholder: true,
189 pub struct PlaceholderExpander {
190 expanded_fragments: FxHashMap<ast::NodeId, AstFragment>,
193 impl PlaceholderExpander {
194 pub fn add(&mut self, id: ast::NodeId, mut fragment: AstFragment) {
195 fragment.mut_visit_with(self);
196 self.expanded_fragments.insert(id, fragment);
199 fn remove(&mut self, id: ast::NodeId) -> AstFragment {
200 self.expanded_fragments.remove(&id).unwrap()
204 impl MutVisitor for PlaceholderExpander {
205 fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
206 if arm.is_placeholder {
207 self.remove(arm.id).make_arms()
209 noop_flat_map_arm(arm, self)
213 fn flat_map_expr_field(&mut self, field: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
214 if field.is_placeholder {
215 self.remove(field.id).make_expr_fields()
217 noop_flat_map_expr_field(field, self)
221 fn flat_map_pat_field(&mut self, fp: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
222 if fp.is_placeholder {
223 self.remove(fp.id).make_pat_fields()
225 noop_flat_map_pat_field(fp, self)
229 fn flat_map_generic_param(
231 param: ast::GenericParam,
232 ) -> SmallVec<[ast::GenericParam; 1]> {
233 if param.is_placeholder {
234 self.remove(param.id).make_generic_params()
236 noop_flat_map_generic_param(param, self)
240 fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
241 if p.is_placeholder {
242 self.remove(p.id).make_params()
244 noop_flat_map_param(p, self)
248 fn flat_map_field_def(&mut self, sf: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
249 if sf.is_placeholder {
250 self.remove(sf.id).make_field_defs()
252 noop_flat_map_field_def(sf, self)
256 fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
257 if variant.is_placeholder {
258 self.remove(variant.id).make_variants()
260 noop_flat_map_variant(variant, self)
264 fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
266 ast::ItemKind::MacCall(_) => self.remove(item.id).make_items(),
267 _ => noop_flat_map_item(item, self),
271 fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
273 ast::AssocItemKind::MacCall(_) => self.remove(item.id).make_trait_items(),
274 _ => noop_flat_map_assoc_item(item, self),
278 fn flat_map_impl_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
280 ast::AssocItemKind::MacCall(_) => self.remove(item.id).make_impl_items(),
281 _ => noop_flat_map_assoc_item(item, self),
285 fn flat_map_foreign_item(
287 item: P<ast::ForeignItem>,
288 ) -> SmallVec<[P<ast::ForeignItem>; 1]> {
290 ast::ForeignItemKind::MacCall(_) => self.remove(item.id).make_foreign_items(),
291 _ => noop_flat_map_foreign_item(item, self),
295 fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
297 ast::ExprKind::MacCall(_) => *expr = self.remove(expr.id).make_expr(),
298 _ => noop_visit_expr(expr, self),
302 fn visit_method_receiver_expr(&mut self, expr: &mut P<ast::Expr>) {
304 ast::ExprKind::MacCall(_) => *expr = self.remove(expr.id).make_method_receiver_expr(),
305 _ => noop_visit_expr(expr, self),
309 fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
311 ast::ExprKind::MacCall(_) => self.remove(expr.id).make_opt_expr(),
312 _ => noop_filter_map_expr(expr, self),
316 fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
317 let (style, mut stmts) = match stmt.kind {
318 ast::StmtKind::MacCall(mac) => (mac.style, self.remove(stmt.id).make_stmts()),
319 _ => return noop_flat_map_stmt(stmt, self),
322 if style == ast::MacStmtStyle::Semicolon {
323 // Implement the proposal described in
324 // https://github.com/rust-lang/rust/issues/61733#issuecomment-509626449
326 // The macro invocation expands to the list of statements. If the
327 // list of statements is empty, then 'parse' the trailing semicolon
328 // on the original invocation as an empty statement. That is:
330 // `empty();` is parsed as a single `StmtKind::Empty`
332 // If the list of statements is non-empty, see if the final
333 // statement already has a trailing semicolon.
335 // If it doesn't have a semicolon, then 'parse' the trailing
336 // semicolon from the invocation as part of the final statement,
337 // using `stmt.add_trailing_semicolon()`
339 // If it does have a semicolon, then 'parse' the trailing semicolon
340 // from the invocation as a new StmtKind::Empty
342 // FIXME: We will need to preserve the original semicolon token and
343 // span as part of #15701
345 ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Empty, span: DUMMY_SP };
347 if let Some(stmt) = stmts.pop() {
348 if stmt.has_trailing_semicolon() {
350 stmts.push(empty_stmt);
352 stmts.push(stmt.add_trailing_semicolon());
355 stmts.push(empty_stmt);
362 fn visit_pat(&mut self, pat: &mut P<ast::Pat>) {
364 ast::PatKind::MacCall(_) => *pat = self.remove(pat.id).make_pat(),
365 _ => noop_visit_pat(pat, self),
369 fn visit_ty(&mut self, ty: &mut P<ast::Ty>) {
371 ast::TyKind::MacCall(_) => *ty = self.remove(ty.id).make_ty(),
372 _ => noop_visit_ty(ty, self),
376 fn visit_crate(&mut self, krate: &mut ast::Crate) {
377 if krate.is_placeholder {
378 *krate = self.remove(krate.id).make_crate();
380 noop_visit_crate(krate, self)