|
15 | 15 |
|
16 | 16 | use crate::ast::*; |
17 | 17 |
|
18 | | -use core::ops::ControlFlow; |
19 | | - |
20 | 18 | use rustc_span::symbol::Ident; |
21 | 19 | use rustc_span::Span; |
22 | 20 |
|
| 21 | +pub use rustc_ast_ir::visit::VisitorResult; |
| 22 | +pub use rustc_ast_ir::{try_visit, visit_opt, walk_list, walk_visitable_list}; |
| 23 | + |
23 | 24 | #[derive(Copy, Clone, Debug, PartialEq)] |
24 | 25 | pub enum AssocCtxt { |
25 | 26 | Trait, |
@@ -101,51 +102,6 @@ pub enum LifetimeCtxt { |
101 | 102 | GenericArg, |
102 | 103 | } |
103 | 104 |
|
104 | | -/// Similar to the `Try` trait, but also implemented for `()`. |
105 | | -pub trait VisitorResult { |
106 | | - type Residual; |
107 | | - fn output() -> Self; |
108 | | - fn from_residual(residual: Self::Residual) -> Self; |
109 | | - fn branch(self) -> ControlFlow<Self::Residual>; |
110 | | -} |
111 | | - |
112 | | -impl VisitorResult for () { |
113 | | - type Residual = !; |
114 | | - |
115 | | - fn output() -> Self {} |
116 | | - fn from_residual(_: !) -> Self {} |
117 | | - fn branch(self) -> ControlFlow<!> { |
118 | | - ControlFlow::Continue(()) |
119 | | - } |
120 | | -} |
121 | | - |
122 | | -impl<T> VisitorResult for ControlFlow<T> { |
123 | | - type Residual = T; |
124 | | - |
125 | | - fn output() -> Self { |
126 | | - ControlFlow::Continue(()) |
127 | | - } |
128 | | - fn from_residual(residual: Self::Residual) -> Self { |
129 | | - ControlFlow::Break(residual) |
130 | | - } |
131 | | - fn branch(self) -> ControlFlow<T> { |
132 | | - self |
133 | | - } |
134 | | -} |
135 | | - |
136 | | -#[macro_export] |
137 | | -macro_rules! try_visit { |
138 | | - ($e:expr) => { |
139 | | - match $crate::visit::VisitorResult::branch($e) { |
140 | | - core::ops::ControlFlow::Continue(()) => (), |
141 | | - #[allow(unreachable_code)] |
142 | | - core::ops::ControlFlow::Break(r) => { |
143 | | - return $crate::visit::VisitorResult::from_residual(r); |
144 | | - } |
145 | | - } |
146 | | - }; |
147 | | -} |
148 | | - |
149 | 105 | /// Each method of the `Visitor` trait is a hook to be potentially |
150 | 106 | /// overridden. Each method's default implementation recursively visits |
151 | 107 | /// the substructure of the input via the corresponding `walk` method; |
@@ -316,24 +272,6 @@ pub trait Visitor<'ast>: Sized { |
316 | 272 | } |
317 | 273 | } |
318 | 274 |
|
319 | | -#[macro_export] |
320 | | -macro_rules! walk_list { |
321 | | - ($visitor: expr, $method: ident, $list: expr $(, $($extra_args: expr),* )?) => { |
322 | | - for elem in $list { |
323 | | - $crate::try_visit!($visitor.$method(elem $(, $($extra_args,)* )?)); |
324 | | - } |
325 | | - } |
326 | | -} |
327 | | - |
328 | | -#[macro_export] |
329 | | -macro_rules! visit_opt { |
330 | | - ($visitor: expr, $method: ident, $opt: expr $(, $($extra_args: expr),* )?) => { |
331 | | - if let Some(x) = $opt { |
332 | | - $crate::try_visit!($visitor.$method(x $(, $($extra_args,)* )?)); |
333 | | - } |
334 | | - } |
335 | | -} |
336 | | - |
337 | 275 | pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) -> V::Result { |
338 | 276 | walk_list!(visitor, visit_item, &krate.items); |
339 | 277 | walk_list!(visitor, visit_attribute, &krate.attrs); |
|
0 commit comments