Skip to content

Commit

Permalink
Add return_result!, defer_P!, visit_{id,span}!, try_v! and visit_{o,l…
Browse files Browse the repository at this point in the history
…ist}!
  • Loading branch information
maxcabrajac committed Oct 12, 2024
1 parent cf591ff commit ba06226
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions compiler/rustc_ast/src/visitors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ macro_rules! make_ast_visitor {
};
}

#[allow(unused)]
macro_rules! return_result {
($V: ty) => {
macro_if!($($mut)? { () } else { <$V>::Result::output() })
};
}

macro_rules! make_visit {
(
$ty: ty
Expand All @@ -65,6 +72,73 @@ macro_rules! make_ast_visitor {
};
}

#[allow(unused)]
macro_rules! deref_P {
($p: expr) => {
macro_if!{$($mut)? {
$p.deref_mut()
} else {
$p
}}
};
}

#[allow(unused)]
macro_rules! visit_id {
($vis: ident, $id: ident) => {
macro_if!{ $($mut)? {
$vis.visit_id($id)
} else {
// assign to _ to prevent unused_variable warnings
let _ = (&$vis, &$id);
}}
};
}

#[allow(unused)]
macro_rules! visit_span {
($vis: ident, $span: ident) => {
macro_if!{ $($mut)? {
$vis.visit_span($span)
} else {
// assign to _ to prevent unused_variable warnings
let _ = (&$vis, &$span);
}}
};
}

#[allow(unused)]
macro_rules! try_v {
($visit: expr) => {
macro_if!{$($mut)? { $visit } else { try_visit!($visit) }}
};
}

#[allow(unused)]
macro_rules! visit_o {
($opt: expr, $fn: expr) => {
if let Some(elem) = $opt {
try_v!($fn(elem))
}
};
}

#[allow(unused)]
macro_rules! visit_list {
($visitor: expr, $visit: ident, $flat_map: ident, $list: expr $$(; $$($arg: expr),*)?) => {
macro_if!{$($mut)? {
$list.flat_map_in_place(|x| $visitor.$flat_map(x $$(, $$($arg),*)?))
} else {
visit_list!($visitor, $visit, $list $$(; $$($arg),*)?)
}}
};
($visitor: expr, $visit: ident, $list: expr $$(; $$($arg: expr),*)?) => {
for elem in $list {
try_v!($visitor.$visit(elem $$(, $$($arg),*)?));
}
};
}

/// Each method of the traits `Visitor` and `MutVisitor` trait is a hook
/// to be potentially overridden. Each method's default implementation
/// recursively visits the substructure of the input via the corresponding
Expand Down

0 comments on commit ba06226

Please sign in to comment.