blob: 0364f5181f7414e82172a9a013e65ab4be7789c0 [file] [log] [blame]
David Tolnay55337722016-09-11 12:58:56 -07001// Adapted from libsyntax.
2
3//! AST walker. Each overridden visit method has full control over what
4//! happens with its node, it can do its own traversal of the node's children,
5//! call `visit::walk_*` to apply the default traversal algorithm, or prevent
6//! deeper traversal by doing nothing.
7//!
8//! Note: it is an important invariant that the default visitor walks the body
9//! of a function in "execution order" (more concretely, reverse post-order
10//! with respect to the CFG implied by the AST), meaning that if AST node A may
11//! execute before AST node B, then A is visited first. The borrow checker in
12//! particular relies on this property.
13//!
14//! Note: walking an AST before macro expansion is probably a bad idea. For
15//! instance, a walker looking for item names in a module will miss all of
16//! those that are created by the expansion of a macro.
17
18use super::*;
19
20/// Each method of the Visitor trait is a hook to be potentially
21/// overridden. Each method's default implementation recursively visits
22/// the substructure of the input via the corresponding `walk` method;
23/// e.g. the `visit_mod` method by default calls `visit::walk_mod`.
24///
25/// If you want to ensure that your code handles every variant
26/// explicitly, you need to override each method. (And you also need
27/// to monitor future changes to `Visitor` in case a new method with a
28/// new default implementation gets introduced.)
29pub trait Visitor: Sized {
30 fn visit_ident(&mut self, _ident: &Ident) {}
David Tolnayc4fbf402016-09-24 09:31:47 -070031 fn visit_macro_input(&mut self, macro_input: &MacroInput) {
32 walk_macro_input(self, macro_input)
David Tolnay55337722016-09-11 12:58:56 -070033 }
34 fn visit_ty(&mut self, ty: &Ty) {
35 walk_ty(self, ty)
36 }
37 fn visit_generics(&mut self, generics: &Generics) {
38 walk_generics(self, generics)
39 }
40 fn visit_ty_param_bound(&mut self, bound: &TyParamBound) {
41 walk_ty_param_bound(self, bound)
42 }
43 fn visit_poly_trait_ref(&mut self, trait_ref: &PolyTraitRef, modifier: &TraitBoundModifier) {
44 walk_poly_trait_ref(self, trait_ref, modifier)
45 }
46 fn visit_variant_data(&mut self, data: &VariantData, _ident: &Ident, _generics: &Generics) {
47 walk_variant_data(self, data)
48 }
49 fn visit_field(&mut self, field: &Field) {
50 walk_field(self, field)
51 }
52 fn visit_variant(&mut self, variant: &Variant, generics: &Generics) {
53 walk_variant(self, variant, generics)
54 }
55 fn visit_lifetime(&mut self, _lifetime: &Lifetime) {}
56 fn visit_lifetime_def(&mut self, lifetime: &LifetimeDef) {
57 walk_lifetime_def(self, lifetime)
58 }
59 fn visit_path(&mut self, path: &Path) {
60 walk_path(self, path)
61 }
62 fn visit_path_segment(&mut self, path_segment: &PathSegment) {
63 walk_path_segment(self, path_segment)
64 }
65 fn visit_path_parameters(&mut self, path_parameters: &PathParameters) {
66 walk_path_parameters(self, path_parameters)
67 }
68 fn visit_assoc_type_binding(&mut self, type_binding: &TypeBinding) {
69 walk_assoc_type_binding(self, type_binding)
70 }
71 fn visit_attribute(&mut self, _attr: &Attribute) {}
72 fn visit_fn_ret_ty(&mut self, ret_ty: &FunctionRetTy) {
73 walk_fn_ret_ty(self, ret_ty)
74 }
David Tolnay68eb4c32016-10-07 23:30:32 -070075 fn visit_const_expr(&mut self, expr: &ConstExpr) {
76 walk_const_expr(self, expr)
David Tolnay429168f2016-10-05 23:41:04 -070077 }
David Tolnay68eb4c32016-10-07 23:30:32 -070078 fn visit_lit(&mut self, _lit: &Lit) {}
Michael Layzellb52df322017-01-22 17:36:55 -050079
80 #[cfg(feature = "type-macros")]
81 fn visit_mac(&mut self, mac: &Mac) {
82 walk_mac(self, mac);
83 }
84
85 #[cfg(feature = "full")]
86 fn visit_crate(&mut self, _crate: &Crate) {
87 walk_crate(self, _crate);
88 }
89 #[cfg(feature = "full")]
90 fn visit_item(&mut self, item: &Item) {
91 walk_item(self, item);
92 }
93 #[cfg(feature = "full")]
94 fn visit_expr(&mut self, expr: &Expr) {
95 walk_expr(self, expr);
96 }
97 #[cfg(feature = "full")]
98 fn visit_foreign_item(&mut self, foreign_item: &ForeignItem) {
99 walk_foreign_item(self, foreign_item);
100 }
101 #[cfg(feature = "full")]
102 fn visit_pat(&mut self, pat: &Pat) {
103 walk_pat(self, pat);
104 }
105 #[cfg(feature = "full")]
106 fn visit_fn_decl(&mut self, fn_decl: &FnDecl) {
107 walk_fn_decl(self, fn_decl);
108 }
109 #[cfg(feature = "full")]
110 fn visit_trait_item(&mut self, trait_item: &TraitItem) {
111 walk_trait_item(self, trait_item);
112 }
113 #[cfg(feature = "full")]
114 fn visit_impl_item(&mut self, impl_item: &ImplItem) {
115 walk_impl_item(self, impl_item);
116 }
117 #[cfg(feature = "full")]
118 fn visit_method_sig(&mut self, method_sig: &MethodSig) {
119 walk_method_sig(self, method_sig);
120 }
121 #[cfg(feature = "full")]
122 fn visit_stmt(&mut self, stmt: &Stmt) {
123 walk_stmt(self, stmt);
124 }
125 #[cfg(feature = "full")]
126 fn visit_local(&mut self, local: &Local) {
127 walk_local(self, local);
128 }
129 #[cfg(feature = "full")]
130 fn visit_view_path(&mut self, view_path: &ViewPath) {
131 walk_view_path(self, view_path);
132 }
David Tolnay55337722016-09-11 12:58:56 -0700133}
134
135#[macro_export]
136macro_rules! walk_list {
137 ($visitor: expr, $method: ident, $list: expr) => {
138 for elem in $list {
139 $visitor.$method(elem)
140 }
141 };
142 ($visitor: expr, $method: ident, $list: expr, $($extra_args: expr),*) => {
143 for elem in $list {
144 $visitor.$method(elem, $($extra_args,)*)
145 }
146 }
147}
148
149pub fn walk_opt_ident<V: Visitor>(visitor: &mut V, opt_ident: &Option<Ident>) {
150 if let Some(ref ident) = *opt_ident {
151 visitor.visit_ident(ident);
152 }
153}
154
155pub fn walk_lifetime_def<V: Visitor>(visitor: &mut V, lifetime_def: &LifetimeDef) {
156 visitor.visit_lifetime(&lifetime_def.lifetime);
157 walk_list!(visitor, visit_lifetime, &lifetime_def.bounds);
158}
159
160pub fn walk_poly_trait_ref<V>(visitor: &mut V, trait_ref: &PolyTraitRef, _: &TraitBoundModifier)
David Tolnaydaaf7742016-10-03 11:11:43 -0700161 where V: Visitor
David Tolnay55337722016-09-11 12:58:56 -0700162{
163 walk_list!(visitor, visit_lifetime_def, &trait_ref.bound_lifetimes);
164 visitor.visit_path(&trait_ref.trait_ref);
165}
166
David Tolnayc4fbf402016-09-24 09:31:47 -0700167pub fn walk_macro_input<V: Visitor>(visitor: &mut V, macro_input: &MacroInput) {
168 visitor.visit_ident(&macro_input.ident);
169 visitor.visit_generics(&macro_input.generics);
170 match macro_input.body {
David Tolnay55337722016-09-11 12:58:56 -0700171 Body::Enum(ref variants) => {
David Tolnayc4fbf402016-09-24 09:31:47 -0700172 walk_list!(visitor, visit_variant, variants, &macro_input.generics);
David Tolnay55337722016-09-11 12:58:56 -0700173 }
174 Body::Struct(ref variant_data) => {
David Tolnaydaaf7742016-10-03 11:11:43 -0700175 visitor.visit_variant_data(variant_data, &macro_input.ident, &macro_input.generics);
David Tolnay55337722016-09-11 12:58:56 -0700176 }
177 }
David Tolnayc4fbf402016-09-24 09:31:47 -0700178 walk_list!(visitor, visit_attribute, &macro_input.attrs);
David Tolnay55337722016-09-11 12:58:56 -0700179}
180
181pub fn walk_variant<V>(visitor: &mut V, variant: &Variant, generics: &Generics)
David Tolnaydaaf7742016-10-03 11:11:43 -0700182 where V: Visitor
David Tolnay55337722016-09-11 12:58:56 -0700183{
184 visitor.visit_ident(&variant.ident);
185 visitor.visit_variant_data(&variant.data, &variant.ident, generics);
186 walk_list!(visitor, visit_attribute, &variant.attrs);
187}
188
189pub fn walk_ty<V: Visitor>(visitor: &mut V, ty: &Ty) {
190 match *ty {
David Tolnay429168f2016-10-05 23:41:04 -0700191 Ty::Slice(ref inner) |
David Tolnaydaaf7742016-10-03 11:11:43 -0700192 Ty::Paren(ref inner) => visitor.visit_ty(inner),
193 Ty::Ptr(ref mutable_type) => visitor.visit_ty(&mutable_type.ty),
David Tolnay55337722016-09-11 12:58:56 -0700194 Ty::Rptr(ref opt_lifetime, ref mutable_type) => {
195 walk_list!(visitor, visit_lifetime, opt_lifetime);
196 visitor.visit_ty(&mutable_type.ty)
197 }
David Tolnay58f6f672016-10-19 08:44:25 -0700198 Ty::Never | Ty::Infer => {}
David Tolnay55337722016-09-11 12:58:56 -0700199 Ty::Tup(ref tuple_element_types) => {
200 walk_list!(visitor, visit_ty, tuple_element_types);
201 }
David Tolnay1391b522016-10-03 21:05:45 -0700202 Ty::BareFn(ref bare_fn) => {
203 walk_list!(visitor, visit_lifetime_def, &bare_fn.lifetimes);
204 for argument in &bare_fn.inputs {
205 walk_opt_ident(visitor, &argument.name);
206 visitor.visit_ty(&argument.ty)
207 }
208 visitor.visit_fn_ret_ty(&bare_fn.output)
David Tolnay55337722016-09-11 12:58:56 -0700209 }
210 Ty::Path(ref maybe_qself, ref path) => {
211 if let Some(ref qself) = *maybe_qself {
212 visitor.visit_ty(&qself.ty);
213 }
214 visitor.visit_path(path);
215 }
David Tolnay429168f2016-10-05 23:41:04 -0700216 Ty::Array(ref inner, ref len) => {
David Tolnay55337722016-09-11 12:58:56 -0700217 visitor.visit_ty(inner);
David Tolnay68eb4c32016-10-07 23:30:32 -0700218 visitor.visit_const_expr(len);
David Tolnay55337722016-09-11 12:58:56 -0700219 }
David Tolnayfa23f572017-01-23 00:19:11 -0800220 Ty::TraitObject(ref bounds) |
David Tolnay55337722016-09-11 12:58:56 -0700221 Ty::ImplTrait(ref bounds) => {
222 walk_list!(visitor, visit_ty_param_bound, bounds);
223 }
Michael Layzellb52df322017-01-22 17:36:55 -0500224 Ty::Mac(ref mac) => {
225 #[cfg(feature = "type-macros")]
226 fn walk_tymac<V: Visitor>(visitor: &mut V, mac: &Mac) {
227 visitor.visit_mac(mac);
228 }
229 #[cfg(not(feature = "type-macros"))]
230 fn walk_tymac<V: Visitor>(_: &mut V, _: &super::ty::Mac) {}
231 walk_tymac(visitor, mac);
232 }
David Tolnay55337722016-09-11 12:58:56 -0700233 }
234}
235
236pub fn walk_path<V: Visitor>(visitor: &mut V, path: &Path) {
237 for segment in &path.segments {
238 visitor.visit_path_segment(segment);
239 }
240}
241
242pub fn walk_path_segment<V: Visitor>(visitor: &mut V, segment: &PathSegment) {
243 visitor.visit_ident(&segment.ident);
244 visitor.visit_path_parameters(&segment.parameters);
245}
246
247pub fn walk_path_parameters<V>(visitor: &mut V, path_parameters: &PathParameters)
David Tolnaydaaf7742016-10-03 11:11:43 -0700248 where V: Visitor
David Tolnay55337722016-09-11 12:58:56 -0700249{
250 match *path_parameters {
251 PathParameters::AngleBracketed(ref data) => {
252 walk_list!(visitor, visit_ty, &data.types);
253 walk_list!(visitor, visit_lifetime, &data.lifetimes);
254 walk_list!(visitor, visit_assoc_type_binding, &data.bindings);
255 }
256 PathParameters::Parenthesized(ref data) => {
257 walk_list!(visitor, visit_ty, &data.inputs);
258 walk_list!(visitor, visit_ty, &data.output);
259 }
260 }
261}
262
263pub fn walk_assoc_type_binding<V: Visitor>(visitor: &mut V, type_binding: &TypeBinding) {
264 visitor.visit_ident(&type_binding.ident);
265 visitor.visit_ty(&type_binding.ty);
266}
267
268pub fn walk_ty_param_bound<V: Visitor>(visitor: &mut V, bound: &TyParamBound) {
269 match *bound {
270 TyParamBound::Trait(ref ty, ref modifier) => {
271 visitor.visit_poly_trait_ref(ty, modifier);
272 }
273 TyParamBound::Region(ref lifetime) => {
274 visitor.visit_lifetime(lifetime);
275 }
276 }
277}
278
279pub fn walk_generics<V: Visitor>(visitor: &mut V, generics: &Generics) {
280 for param in &generics.ty_params {
281 visitor.visit_ident(&param.ident);
282 walk_list!(visitor, visit_ty_param_bound, &param.bounds);
283 walk_list!(visitor, visit_ty, &param.default);
284 }
285 walk_list!(visitor, visit_lifetime_def, &generics.lifetimes);
286 for predicate in &generics.where_clause.predicates {
287 match *predicate {
David Tolnaydaaf7742016-10-03 11:11:43 -0700288 WherePredicate::BoundPredicate(WhereBoundPredicate { ref bounded_ty,
289 ref bounds,
290 ref bound_lifetimes,
291 .. }) => {
David Tolnay55337722016-09-11 12:58:56 -0700292 visitor.visit_ty(bounded_ty);
293 walk_list!(visitor, visit_ty_param_bound, bounds);
294 walk_list!(visitor, visit_lifetime_def, bound_lifetimes);
295 }
David Tolnaydaaf7742016-10-03 11:11:43 -0700296 WherePredicate::RegionPredicate(WhereRegionPredicate { ref lifetime,
297 ref bounds,
298 .. }) => {
David Tolnay55337722016-09-11 12:58:56 -0700299 visitor.visit_lifetime(lifetime);
300 walk_list!(visitor, visit_lifetime, bounds);
301 }
David Tolnayfa23f572017-01-23 00:19:11 -0800302 WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty,
303 ref rhs_ty,
304 .. }) => {
305 visitor.visit_ty(lhs_ty);
306 visitor.visit_ty(rhs_ty);
307 }
David Tolnay55337722016-09-11 12:58:56 -0700308 }
309 }
310}
311
312pub fn walk_fn_ret_ty<V: Visitor>(visitor: &mut V, ret_ty: &FunctionRetTy) {
313 if let FunctionRetTy::Ty(ref output_ty) = *ret_ty {
314 visitor.visit_ty(output_ty)
315 }
316}
317
David Tolnay55337722016-09-11 12:58:56 -0700318pub fn walk_variant_data<V: Visitor>(visitor: &mut V, data: &VariantData) {
319 walk_list!(visitor, visit_field, data.fields());
320}
321
322pub fn walk_field<V: Visitor>(visitor: &mut V, field: &Field) {
323 walk_opt_ident(visitor, &field.ident);
324 visitor.visit_ty(&field.ty);
325 walk_list!(visitor, visit_attribute, &field.attrs);
326}
David Tolnay429168f2016-10-05 23:41:04 -0700327
David Tolnay68eb4c32016-10-07 23:30:32 -0700328pub fn walk_const_expr<V: Visitor>(visitor: &mut V, len: &ConstExpr) {
David Tolnay429168f2016-10-05 23:41:04 -0700329 match *len {
David Tolnay68eb4c32016-10-07 23:30:32 -0700330 ConstExpr::Call(ref function, ref args) => {
David Tolnay58f6f672016-10-19 08:44:25 -0700331 visitor.visit_const_expr(function);
David Tolnay68eb4c32016-10-07 23:30:32 -0700332 walk_list!(visitor, visit_const_expr, args);
333 }
334 ConstExpr::Binary(_op, ref left, ref right) => {
335 visitor.visit_const_expr(left);
336 visitor.visit_const_expr(right);
337 }
338 ConstExpr::Unary(_op, ref v) => {
339 visitor.visit_const_expr(v);
340 }
341 ConstExpr::Lit(ref lit) => {
342 visitor.visit_lit(lit);
343 }
344 ConstExpr::Cast(ref expr, ref ty) => {
345 visitor.visit_const_expr(expr);
346 visitor.visit_ty(ty);
347 }
348 ConstExpr::Path(ref path) => {
David Tolnay429168f2016-10-05 23:41:04 -0700349 visitor.visit_path(path);
350 }
David Tolnay67588752016-10-30 12:23:10 -0700351 ConstExpr::Index(ref expr, ref index) => {
352 visitor.visit_const_expr(expr);
353 visitor.visit_const_expr(index);
354 }
David Tolnaye7b0d322016-10-30 10:27:23 -0700355 ConstExpr::Paren(ref expr) => {
356 visitor.visit_const_expr(expr);
357 }
Michael Layzellb52df322017-01-22 17:36:55 -0500358 ConstExpr::Other(ref other) => {
359 #[cfg(feature = "full")]
360 fn walk_other<V: Visitor>(visitor: &mut V, other: &Expr) {
361 visitor.visit_expr(other);
362 }
363 #[cfg(not(feature = "full"))]
364 fn walk_other<V: Visitor>(_: &mut V, _: &super::constant::Other) {}
365 walk_other(visitor, other);
366 }
367 }
368}
369
370#[cfg(feature = "type-macros")]
371pub fn walk_mac<V: Visitor>(visitor: &mut V, mac: &Mac) {
372 visitor.visit_path(&mac.path);
373}
374
375#[cfg(feature = "full")]
376pub fn walk_crate<V: Visitor>(visitor: &mut V, _crate: &Crate) {
377 walk_list!(visitor, visit_attribute, &_crate.attrs);
378 walk_list!(visitor, visit_item, &_crate.items);
379}
380
381#[cfg(feature = "full")]
382pub fn walk_item<V: Visitor>(visitor: &mut V, item: &Item) {
383 visitor.visit_ident(&item.ident);
384 walk_list!(visitor, visit_attribute, &item.attrs);
385 match item.node {
386 ItemKind::ExternCrate(ref ident) => {
387 walk_opt_ident(visitor, ident);
388 }
389 ItemKind::Use(ref view_path) => {
390 visitor.visit_view_path(view_path);
391 }
392 ItemKind::Static(ref ty, _, ref expr) => {
393 visitor.visit_ty(ty);
394 visitor.visit_expr(expr);
395 }
396 ItemKind::Const(ref ty, ref expr) => {
397 visitor.visit_ty(ty);
398 visitor.visit_expr(expr);
399 }
400 ItemKind::Fn(ref decl, _, _, _, ref generics, ref body) => {
401 visitor.visit_fn_decl(decl);
402 visitor.visit_generics(generics);
403 walk_list!(visitor, visit_stmt, &body.stmts);
404 }
405 ItemKind::Mod(ref maybe_items) => {
406 if let Some(ref items) = *maybe_items {
407 walk_list!(visitor, visit_item, items);
408 }
409 }
410 ItemKind::ForeignMod(ref foreign_mod) => {
411 walk_list!(visitor, visit_foreign_item, &foreign_mod.items);
412 }
413 ItemKind::Ty(ref ty, ref generics) => {
414 visitor.visit_ty(ty);
415 visitor.visit_generics(generics);
416 }
417 ItemKind::Enum(ref variant, ref generics) => {
418 walk_list!(visitor, visit_variant, variant, generics);
419 }
420 ItemKind::Struct(ref variant_data, ref generics) => {
421 visitor.visit_variant_data(variant_data, &item.ident, generics);
422 }
423 ItemKind::Union(ref variant_data, ref generics) => {
424 visitor.visit_variant_data(variant_data, &item.ident, generics);
425 }
426 ItemKind::Trait(_, ref generics, ref bounds, ref trait_items) => {
427 visitor.visit_generics(generics);
428 walk_list!(visitor, visit_ty_param_bound, bounds);
429 walk_list!(visitor, visit_trait_item, trait_items);
430 }
431 ItemKind::DefaultImpl(_, ref path) => {
432 visitor.visit_path(path);
433 }
434 ItemKind::Impl(_, _, ref generics, ref maybe_path, ref ty, ref impl_items) => {
435 visitor.visit_generics(generics);
436 if let Some(ref path) = *maybe_path {
437 visitor.visit_path(path);
438 }
439 visitor.visit_ty(ty);
440 walk_list!(visitor, visit_impl_item, impl_items);
441 }
442 ItemKind::Mac(ref mac) => {
443 visitor.visit_mac(mac)
444 }
445 }
446}
447
448#[cfg(feature = "full")]
449pub fn walk_expr<V: Visitor>(visitor: &mut V, expr: &Expr) {
450 walk_list!(visitor, visit_attribute, &expr.attrs);
451 match expr.node {
452 ExprKind::Box(ref expr) => {
453 visitor.visit_expr(expr);
454 }
455 ExprKind::InPlace(ref place, ref value) => {
456 visitor.visit_expr(place);
457 visitor.visit_expr(value);
458 }
David Tolnayfa23f572017-01-23 00:19:11 -0800459 ExprKind::Array(ref exprs) => {
Michael Layzellb52df322017-01-22 17:36:55 -0500460 walk_list!(visitor, visit_expr, exprs);
461 }
462 ExprKind::Call(ref callee, ref args) => {
463 visitor.visit_expr(callee);
464 walk_list!(visitor, visit_expr, args);
465 }
466 ExprKind::MethodCall(ref name, ref ty_args, ref args) => {
467 visitor.visit_ident(name);
468 walk_list!(visitor, visit_ty, ty_args);
469 walk_list!(visitor, visit_expr, args);
470 }
471 ExprKind::Tup(ref exprs) => {
472 walk_list!(visitor, visit_expr, exprs);
473 }
474 ExprKind::Binary(_, ref lhs, ref rhs) => {
475 visitor.visit_expr(lhs);
476 visitor.visit_expr(rhs);
477 }
478 ExprKind::Unary(_, ref operand) => {
479 visitor.visit_expr(operand);
480 }
481 ExprKind::Lit(ref lit) => {
482 visitor.visit_lit(lit);
483 }
484 ExprKind::Cast(ref expr, ref ty) => {
485 visitor.visit_expr(expr);
486 visitor.visit_ty(ty);
487 }
488 ExprKind::Type(ref expr, ref ty) => {
489 visitor.visit_expr(expr);
490 visitor.visit_ty(ty);
491 }
492 ExprKind::If(ref cond, ref cons, ref maybe_alt) => {
493 visitor.visit_expr(cond);
494 walk_list!(visitor, visit_stmt, &cons.stmts);
495 if let Some(ref alt) = *maybe_alt {
496 visitor.visit_expr(alt);
497 }
498 }
499 ExprKind::IfLet(ref pat, ref cond, ref cons, ref maybe_alt) => {
500 visitor.visit_pat(pat);
501 visitor.visit_expr(cond);
502 walk_list!(visitor, visit_stmt, &cons.stmts);
503 if let Some(ref alt) = *maybe_alt {
504 visitor.visit_expr(alt);
505 }
506 }
507 ExprKind::While(ref cond, ref body, ref label) => {
508 visitor.visit_expr(cond);
509 walk_list!(visitor, visit_stmt, &body.stmts);
510 walk_opt_ident(visitor, label);
511 }
512 ExprKind::WhileLet(ref pat, ref cond, ref body, ref label) => {
513 visitor.visit_pat(pat);
514 visitor.visit_expr(cond);
515 walk_list!(visitor, visit_stmt, &body.stmts);
516 walk_opt_ident(visitor, label);
517 }
518 ExprKind::ForLoop(ref pat, ref expr, ref body, ref label) => {
519 visitor.visit_pat(pat);
520 visitor.visit_expr(expr);
521 walk_list!(visitor, visit_stmt, &body.stmts);
522 walk_opt_ident(visitor, label);
523 }
524 ExprKind::Loop(ref body, ref label) => {
525 walk_list!(visitor, visit_stmt, &body.stmts);
526 walk_opt_ident(visitor, label);
527 }
528 ExprKind::Match(ref expr, ref arms) => {
529 visitor.visit_expr(expr);
530 for &Arm{ref attrs, ref pats, ref guard, ref body} in arms {
531 walk_list!(visitor, visit_attribute, attrs);
532 walk_list!(visitor, visit_pat, pats);
533 if let Some(ref guard) = *guard {
534 visitor.visit_expr(guard);
535 }
536 visitor.visit_expr(body);
537 }
538 }
539 ExprKind::Closure(_, ref decl, ref expr) => {
540 visitor.visit_fn_decl(decl);
541 visitor.visit_expr(expr);
542 }
543 ExprKind::Block(_, ref block) => {
544 walk_list!(visitor, visit_stmt, &block.stmts);
545 }
546 ExprKind::Assign(ref lhs, ref rhs) => {
547 visitor.visit_expr(lhs);
548 visitor.visit_expr(rhs);
549 }
550 ExprKind::AssignOp(_, ref lhs, ref rhs) => {
551 visitor.visit_expr(lhs);
552 visitor.visit_expr(rhs);
553 }
554 ExprKind::Field(ref obj, ref field) => {
555 visitor.visit_expr(obj);
556 visitor.visit_ident(field);
557 }
558 ExprKind::TupField(ref obj, _) => {
559 visitor.visit_expr(obj);
560 }
561 ExprKind::Index(ref obj, ref idx) => {
562 visitor.visit_expr(obj);
563 visitor.visit_expr(idx);
564 }
565 ExprKind::Range(ref maybe_start, ref maybe_end, _) => {
566 if let Some(ref start) = *maybe_start {
567 visitor.visit_expr(start);
568 }
569 if let Some(ref end) = *maybe_end {
570 visitor.visit_expr(end);
571 }
572 }
573 ExprKind::Path(ref maybe_qself, ref path) => {
574 if let Some(ref qself) = *maybe_qself {
575 visitor.visit_ty(&qself.ty);
576 }
577 visitor.visit_path(path);
578 }
579 ExprKind::AddrOf(_, ref expr) => {
580 visitor.visit_expr(expr);
581 }
582 ExprKind::Break(ref maybe_label, ref maybe_expr) => {
583 walk_opt_ident(visitor, maybe_label);
584 if let Some(ref expr) = *maybe_expr {
585 visitor.visit_expr(expr);
586 }
587 }
588 ExprKind::Continue(ref maybe_label) => {
589 walk_opt_ident(visitor, maybe_label);
590 }
591 ExprKind::Ret(ref maybe_expr) => {
592 if let Some(ref expr) = *maybe_expr {
593 visitor.visit_expr(expr);
594 }
595 }
596 ExprKind::Mac(ref mac) => {
597 visitor.visit_mac(mac);
598 }
599 ExprKind::Struct(ref path, ref fields, ref maybe_base) => {
600 visitor.visit_path(path);
601 for &FieldValue{ref ident, ref expr, ..} in fields {
602 visitor.visit_ident(ident);
603 visitor.visit_expr(expr);
604 }
605 if let Some(ref base) = *maybe_base {
606 visitor.visit_expr(base);
607 }
608 }
609 ExprKind::Repeat(ref value, ref times) => {
610 visitor.visit_expr(value);
611 visitor.visit_expr(times);
612 }
613 ExprKind::Paren(ref expr) => {
614 visitor.visit_expr(expr);
615 }
616 ExprKind::Try(ref expr) => {
617 visitor.visit_expr(expr);
618 }
619 }
620}
621
622#[cfg(feature = "full")]
623pub fn walk_foreign_item<V: Visitor>(visitor: &mut V, foreign_item: &ForeignItem) {
624 visitor.visit_ident(&foreign_item.ident);
625 walk_list!(visitor, visit_attribute, &foreign_item.attrs);
626 match foreign_item.node {
627 ForeignItemKind::Fn(ref decl, ref generics) => {
628 visitor.visit_fn_decl(decl);
629 visitor.visit_generics(generics);
630 }
631 ForeignItemKind::Static(ref ty, _) => {
632 visitor.visit_ty(ty);
633 }
634 }
635}
636
637#[cfg(feature = "full")]
638pub fn walk_pat<V: Visitor>(visitor: &mut V, pat: &Pat) {
639 match *pat {
640 Pat::Wild => {}
641 Pat::Ident(_, ref ident, ref maybe_pat) => {
642 visitor.visit_ident(ident);
643 if let Some(ref pat) = *maybe_pat {
644 visitor.visit_pat(pat);
645 }
646 }
647 Pat::Struct(ref path, ref field_pats, _) => {
648 visitor.visit_path(path);
649 for &FieldPat{ref ident, ref pat, ..} in field_pats {
650 visitor.visit_ident(ident);
651 visitor.visit_pat(pat);
652 }
653 }
654 Pat::TupleStruct(ref path, ref pats, _) => {
655 visitor.visit_path(path);
656 walk_list!(visitor, visit_pat, pats);
657 }
658 Pat::Path(ref maybe_qself, ref path) => {
659 if let Some(ref qself) = *maybe_qself {
660 visitor.visit_ty(&qself.ty);
661 }
662 visitor.visit_path(path);
663 }
664 Pat::Tuple(ref pats, _) => {
665 walk_list!(visitor, visit_pat, pats);
666 }
667 Pat::Box(ref pat) |
668 Pat::Ref(ref pat, _) => {
669 visitor.visit_pat(pat);
670 }
671 Pat::Lit(ref expr) => {
672 visitor.visit_expr(expr);
673 }
674 Pat::Range(ref start, ref end) => {
675 visitor.visit_expr(start);
676 visitor.visit_expr(end);
677 }
678 Pat::Slice(ref start, ref maybe_mid, ref end) => {
679 walk_list!(visitor, visit_pat, start);
680 if let Some(ref mid) = *maybe_mid {
681 visitor.visit_pat(mid);
682 }
683 walk_list!(visitor, visit_pat, end);
684 }
685 Pat::Mac(ref mac) => {
686 visitor.visit_mac(mac);
687 }
688 }
689}
690
691#[cfg(feature = "full")]
692pub fn walk_fn_decl<V: Visitor>(visitor: &mut V, fn_decl: &FnDecl) {
693 for input in &fn_decl.inputs {
694 match *input {
695 FnArg::SelfRef(_, _) | FnArg::SelfValue(_) => {}
696 FnArg::Captured(ref pat, ref ty) => {
697 visitor.visit_pat(pat);
698 visitor.visit_ty(ty);
699 }
700 FnArg::Ignored(ref ty) => {
701 visitor.visit_ty(ty);
702 }
703 }
704 }
705 visitor.visit_fn_ret_ty(&fn_decl.output);
706}
707
708#[cfg(feature = "full")]
709pub fn walk_trait_item<V: Visitor>(visitor: &mut V, trait_item: &TraitItem) {
710 visitor.visit_ident(&trait_item.ident);
711 walk_list!(visitor, visit_attribute, &trait_item.attrs);
712 match trait_item.node {
713 TraitItemKind::Const(ref ty, ref maybe_expr) => {
714 visitor.visit_ty(ty);
715 if let Some(ref expr) = *maybe_expr {
716 visitor.visit_expr(expr);
717 }
718 }
719 TraitItemKind::Method(ref method_sig, ref maybe_block) => {
720 visitor.visit_method_sig(method_sig);
721 if let Some(ref block) = *maybe_block {
722 walk_list!(visitor, visit_stmt, &block.stmts);
723 }
724 }
725 TraitItemKind::Type(ref bounds, ref maybe_ty) => {
726 walk_list!(visitor, visit_ty_param_bound, bounds);
727 if let Some(ref ty) = *maybe_ty {
728 visitor.visit_ty(ty);
729 }
730 }
731 TraitItemKind::Macro(ref mac) => {
732 visitor.visit_mac(mac);
733 }
734 }
735}
736
737#[cfg(feature = "full")]
738pub fn walk_impl_item<V: Visitor>(visitor: &mut V, impl_item: &ImplItem) {
739 visitor.visit_ident(&impl_item.ident);
740 walk_list!(visitor, visit_attribute, &impl_item.attrs);
741 match impl_item.node {
742 ImplItemKind::Const(ref ty, ref expr) => {
743 visitor.visit_ty(ty);
744 visitor.visit_expr(expr);
745 }
746 ImplItemKind::Method(ref method_sig, ref block) => {
747 visitor.visit_method_sig(method_sig);
748 walk_list!(visitor, visit_stmt, &block.stmts);
749 }
750 ImplItemKind::Type(ref ty) => {
751 visitor.visit_ty(ty);
752 }
753 ImplItemKind::Macro(ref mac) => {
754 visitor.visit_mac(mac);
755 }
756 }
757}
758
759#[cfg(feature = "full")]
760pub fn walk_method_sig<V: Visitor>(visitor: &mut V, method_sig: &MethodSig) {
761 visitor.visit_fn_decl(&method_sig.decl);
762 visitor.visit_generics(&method_sig.generics);
763}
764
765#[cfg(feature = "full")]
766pub fn walk_stmt<V: Visitor>(visitor: &mut V, stmt: &Stmt) {
767 match *stmt {
768 Stmt::Local(ref local) => {
769 visitor.visit_local(local);
770 }
771 Stmt::Item(ref item) => {
772 visitor.visit_item(item);
773 }
774 Stmt::Expr(ref expr) |
775 Stmt::Semi(ref expr) => {
776 visitor.visit_expr(expr);
777 }
778 Stmt::Mac(ref details) => {
779 let (ref mac, _, ref attrs) = **details;
780 visitor.visit_mac(mac);
781 walk_list!(visitor, visit_attribute, attrs);
782 }
783 }
784}
785
786#[cfg(feature = "full")]
787pub fn walk_local<V: Visitor>(visitor: &mut V, local: &Local) {
788 visitor.visit_pat(&local.pat);
789 if let Some(ref ty) = local.ty {
790 visitor.visit_ty(ty);
791 }
792 if let Some(ref init) = local.init {
793 visitor.visit_expr(init);
794 }
795 walk_list!(visitor, visit_attribute, &local.attrs);
796}
797
798#[cfg(feature = "full")]
799pub fn walk_view_path<V: Visitor>(visitor: &mut V, view_path: &ViewPath) {
800 match *view_path {
801 ViewPath::Simple(ref path, ref maybe_ident) => {
802 visitor.visit_path(path);
803 walk_opt_ident(visitor, maybe_ident);
804 }
805 ViewPath::Glob(ref path) => {
806 visitor.visit_path(path);
807 }
808 ViewPath::List(ref path, ref items) => {
809 visitor.visit_path(path);
810 for &PathListItem{ref name, ref rename} in items {
811 visitor.visit_ident(name);
812 walk_opt_ident(visitor, rename);
813 }
814 }
David Tolnay429168f2016-10-05 23:41:04 -0700815 }
816}