Skip to content

Commit 8104df2

Browse files
committed
Add method visit_with to MacroGenerable
1 parent 35841ee commit 8104df2

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/libsyntax/ext/expand.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ trait MacroGenerable: Sized {
4141

4242
// Fold this node or list of nodes using the given folder.
4343
fn fold_with<F: Folder>(self, folder: &mut F) -> Self;
44+
fn visit_with<'v, V: Visitor<'v>>(&'v self, visitor: &mut V);
4445

4546
// Return a placeholder expansion to allow compilation to continue after an erroring expansion.
4647
fn dummy(span: Span) -> Self;
@@ -50,7 +51,9 @@ trait MacroGenerable: Sized {
5051
}
5152

5253
macro_rules! impl_macro_generable {
53-
($($ty:ty: $kind_name:expr, .$make:ident, $(.$fold:ident)* $(lift .$fold_elt:ident)*,
54+
($($ty:ty: $kind_name:expr, .$make:ident,
55+
$(.$fold:ident)* $(lift .$fold_elt:ident)*,
56+
$(.$visit:ident)* $(lift .$visit_elt:ident)*,
5457
|$span:ident| $dummy:expr;)*) => { $(
5558
impl MacroGenerable for $ty {
5659
fn kind_name() -> &'static str { $kind_name }
@@ -59,21 +62,27 @@ macro_rules! impl_macro_generable {
5962
$( folder.$fold(self) )*
6063
$( self.into_iter().flat_map(|item| folder. $fold_elt (item)).collect() )*
6164
}
65+
fn visit_with<'v, V: Visitor<'v>>(&'v self, visitor: &mut V) {
66+
$( visitor.$visit(self) )*
67+
$( for item in self.as_slice() { visitor. $visit_elt (item) } )*
68+
}
6269
fn dummy($span: Span) -> Self { $dummy }
6370
}
6471
)* }
6572
}
6673

6774
impl_macro_generable! {
68-
P<ast::Expr>: "expression", .make_expr, .fold_expr, |span| DummyResult::raw_expr(span);
69-
P<ast::Pat>: "pattern", .make_pat, .fold_pat, |span| P(DummyResult::raw_pat(span));
70-
P<ast::Ty>: "type", .make_ty, .fold_ty, |span| DummyResult::raw_ty(span);
71-
SmallVector<ast::ImplItem>:
72-
"impl item", .make_impl_items, lift .fold_impl_item, |_span| SmallVector::zero();
73-
SmallVector<P<ast::Item>>:
74-
"item", .make_items, lift .fold_item, |_span| SmallVector::zero();
75+
P<ast::Pat>: "pattern", .make_pat, .fold_pat, .visit_pat, |span| P(DummyResult::raw_pat(span));
76+
P<ast::Ty>: "type", .make_ty, .fold_ty, .visit_ty, |span| DummyResult::raw_ty(span);
77+
P<ast::Expr>:
78+
"expression", .make_expr, .fold_expr, .visit_expr, |span| DummyResult::raw_expr(span);
7579
SmallVector<ast::Stmt>:
76-
"statement", .make_stmts, lift .fold_stmt, |_span| SmallVector::zero();
80+
"statement", .make_stmts, lift .fold_stmt, lift .visit_stmt, |_span| SmallVector::zero();
81+
SmallVector<P<ast::Item>>:
82+
"item", .make_items, lift .fold_item, lift .visit_item, |_span| SmallVector::zero();
83+
SmallVector<ast::ImplItem>:
84+
"impl item", .make_impl_items, lift .fold_impl_item, lift .visit_impl_item,
85+
|_span| SmallVector::zero();
7786
}
7887

7988
impl MacroGenerable for Option<P<ast::Expr>> {
@@ -85,6 +94,9 @@ impl MacroGenerable for Option<P<ast::Expr>> {
8594
fn fold_with<F: Folder>(self, folder: &mut F) -> Self {
8695
self.and_then(|expr| folder.fold_opt_expr(expr))
8796
}
97+
fn visit_with<'v, V: Visitor<'v>>(&'v self, visitor: &mut V) {
98+
self.as_ref().map(|expr| visitor.visit_expr(expr));
99+
}
88100
}
89101

90102
pub fn expand_expr(expr: ast::Expr, fld: &mut MacroExpander) -> P<ast::Expr> {

0 commit comments

Comments
 (0)