blob: 22e6a24086c9734c28ff219c9721dab22d7e0a72 [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 Tolnay0e837402016-12-22 17:25:55 -050031 fn visit_derive_input(&mut self, derive_input: &DeriveInput) {
32 walk_derive_input(self, derive_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
David Tolnay55337722016-09-11 12:58:56 -0700135macro_rules! walk_list {
David Tolnaybdae3642017-01-23 00:39:55 -0800136 ($visitor:expr, $method:ident, $list:expr $(, $extra_args:expr)*) => {
David Tolnay55337722016-09-11 12:58:56 -0700137 for elem in $list {
David Tolnaybdae3642017-01-23 00:39:55 -0800138 $visitor.$method(elem $(, $extra_args)*)
David Tolnay55337722016-09-11 12:58:56 -0700139 }
140 };
David Tolnay55337722016-09-11 12:58:56 -0700141}
142
143pub fn walk_opt_ident<V: Visitor>(visitor: &mut V, opt_ident: &Option<Ident>) {
144 if let Some(ref ident) = *opt_ident {
145 visitor.visit_ident(ident);
146 }
147}
148
149pub fn walk_lifetime_def<V: Visitor>(visitor: &mut V, lifetime_def: &LifetimeDef) {
150 visitor.visit_lifetime(&lifetime_def.lifetime);
151 walk_list!(visitor, visit_lifetime, &lifetime_def.bounds);
152}
153
154pub fn walk_poly_trait_ref<V>(visitor: &mut V, trait_ref: &PolyTraitRef, _: &TraitBoundModifier)
David Tolnaydaaf7742016-10-03 11:11:43 -0700155 where V: Visitor
David Tolnay55337722016-09-11 12:58:56 -0700156{
157 walk_list!(visitor, visit_lifetime_def, &trait_ref.bound_lifetimes);
158 visitor.visit_path(&trait_ref.trait_ref);
159}
160
David Tolnay0e837402016-12-22 17:25:55 -0500161pub fn walk_derive_input<V: Visitor>(visitor: &mut V, derive_input: &DeriveInput) {
162 visitor.visit_ident(&derive_input.ident);
163 visitor.visit_generics(&derive_input.generics);
164 match derive_input.body {
David Tolnay55337722016-09-11 12:58:56 -0700165 Body::Enum(ref variants) => {
David Tolnay0e837402016-12-22 17:25:55 -0500166 walk_list!(visitor, visit_variant, variants, &derive_input.generics);
David Tolnay55337722016-09-11 12:58:56 -0700167 }
168 Body::Struct(ref variant_data) => {
David Tolnay0e837402016-12-22 17:25:55 -0500169 visitor.visit_variant_data(variant_data, &derive_input.ident, &derive_input.generics);
David Tolnay55337722016-09-11 12:58:56 -0700170 }
171 }
David Tolnay0e837402016-12-22 17:25:55 -0500172 walk_list!(visitor, visit_attribute, &derive_input.attrs);
David Tolnay55337722016-09-11 12:58:56 -0700173}
174
175pub fn walk_variant<V>(visitor: &mut V, variant: &Variant, generics: &Generics)
David Tolnaydaaf7742016-10-03 11:11:43 -0700176 where V: Visitor
David Tolnay55337722016-09-11 12:58:56 -0700177{
178 visitor.visit_ident(&variant.ident);
179 visitor.visit_variant_data(&variant.data, &variant.ident, generics);
180 walk_list!(visitor, visit_attribute, &variant.attrs);
181}
182
183pub fn walk_ty<V: Visitor>(visitor: &mut V, ty: &Ty) {
184 match *ty {
David Tolnay429168f2016-10-05 23:41:04 -0700185 Ty::Slice(ref inner) |
David Tolnaydaaf7742016-10-03 11:11:43 -0700186 Ty::Paren(ref inner) => visitor.visit_ty(inner),
187 Ty::Ptr(ref mutable_type) => visitor.visit_ty(&mutable_type.ty),
David Tolnay55337722016-09-11 12:58:56 -0700188 Ty::Rptr(ref opt_lifetime, ref mutable_type) => {
189 walk_list!(visitor, visit_lifetime, opt_lifetime);
190 visitor.visit_ty(&mutable_type.ty)
191 }
David Tolnay58f6f672016-10-19 08:44:25 -0700192 Ty::Never | Ty::Infer => {}
David Tolnay55337722016-09-11 12:58:56 -0700193 Ty::Tup(ref tuple_element_types) => {
194 walk_list!(visitor, visit_ty, tuple_element_types);
195 }
David Tolnay1391b522016-10-03 21:05:45 -0700196 Ty::BareFn(ref bare_fn) => {
197 walk_list!(visitor, visit_lifetime_def, &bare_fn.lifetimes);
198 for argument in &bare_fn.inputs {
199 walk_opt_ident(visitor, &argument.name);
200 visitor.visit_ty(&argument.ty)
201 }
202 visitor.visit_fn_ret_ty(&bare_fn.output)
David Tolnay55337722016-09-11 12:58:56 -0700203 }
204 Ty::Path(ref maybe_qself, ref path) => {
205 if let Some(ref qself) = *maybe_qself {
206 visitor.visit_ty(&qself.ty);
207 }
208 visitor.visit_path(path);
209 }
David Tolnay429168f2016-10-05 23:41:04 -0700210 Ty::Array(ref inner, ref len) => {
David Tolnay55337722016-09-11 12:58:56 -0700211 visitor.visit_ty(inner);
David Tolnay68eb4c32016-10-07 23:30:32 -0700212 visitor.visit_const_expr(len);
David Tolnay55337722016-09-11 12:58:56 -0700213 }
David Tolnayfa23f572017-01-23 00:19:11 -0800214 Ty::TraitObject(ref bounds) |
David Tolnay55337722016-09-11 12:58:56 -0700215 Ty::ImplTrait(ref bounds) => {
216 walk_list!(visitor, visit_ty_param_bound, bounds);
217 }
Michael Layzellb52df322017-01-22 17:36:55 -0500218 Ty::Mac(ref mac) => {
219 #[cfg(feature = "type-macros")]
220 fn walk_tymac<V: Visitor>(visitor: &mut V, mac: &Mac) {
221 visitor.visit_mac(mac);
222 }
223 #[cfg(not(feature = "type-macros"))]
224 fn walk_tymac<V: Visitor>(_: &mut V, _: &super::ty::Mac) {}
225 walk_tymac(visitor, mac);
226 }
David Tolnay55337722016-09-11 12:58:56 -0700227 }
228}
229
230pub fn walk_path<V: Visitor>(visitor: &mut V, path: &Path) {
231 for segment in &path.segments {
232 visitor.visit_path_segment(segment);
233 }
234}
235
236pub fn walk_path_segment<V: Visitor>(visitor: &mut V, segment: &PathSegment) {
237 visitor.visit_ident(&segment.ident);
238 visitor.visit_path_parameters(&segment.parameters);
239}
240
241pub fn walk_path_parameters<V>(visitor: &mut V, path_parameters: &PathParameters)
David Tolnaydaaf7742016-10-03 11:11:43 -0700242 where V: Visitor
David Tolnay55337722016-09-11 12:58:56 -0700243{
244 match *path_parameters {
245 PathParameters::AngleBracketed(ref data) => {
246 walk_list!(visitor, visit_ty, &data.types);
247 walk_list!(visitor, visit_lifetime, &data.lifetimes);
248 walk_list!(visitor, visit_assoc_type_binding, &data.bindings);
249 }
250 PathParameters::Parenthesized(ref data) => {
251 walk_list!(visitor, visit_ty, &data.inputs);
252 walk_list!(visitor, visit_ty, &data.output);
253 }
254 }
255}
256
257pub fn walk_assoc_type_binding<V: Visitor>(visitor: &mut V, type_binding: &TypeBinding) {
258 visitor.visit_ident(&type_binding.ident);
259 visitor.visit_ty(&type_binding.ty);
260}
261
262pub fn walk_ty_param_bound<V: Visitor>(visitor: &mut V, bound: &TyParamBound) {
263 match *bound {
264 TyParamBound::Trait(ref ty, ref modifier) => {
265 visitor.visit_poly_trait_ref(ty, modifier);
266 }
267 TyParamBound::Region(ref lifetime) => {
268 visitor.visit_lifetime(lifetime);
269 }
270 }
271}
272
273pub fn walk_generics<V: Visitor>(visitor: &mut V, generics: &Generics) {
274 for param in &generics.ty_params {
275 visitor.visit_ident(&param.ident);
276 walk_list!(visitor, visit_ty_param_bound, &param.bounds);
277 walk_list!(visitor, visit_ty, &param.default);
278 }
279 walk_list!(visitor, visit_lifetime_def, &generics.lifetimes);
280 for predicate in &generics.where_clause.predicates {
281 match *predicate {
David Tolnaydaaf7742016-10-03 11:11:43 -0700282 WherePredicate::BoundPredicate(WhereBoundPredicate { ref bounded_ty,
283 ref bounds,
284 ref bound_lifetimes,
285 .. }) => {
David Tolnay55337722016-09-11 12:58:56 -0700286 visitor.visit_ty(bounded_ty);
287 walk_list!(visitor, visit_ty_param_bound, bounds);
288 walk_list!(visitor, visit_lifetime_def, bound_lifetimes);
289 }
David Tolnaydaaf7742016-10-03 11:11:43 -0700290 WherePredicate::RegionPredicate(WhereRegionPredicate { ref lifetime,
291 ref bounds,
292 .. }) => {
David Tolnay55337722016-09-11 12:58:56 -0700293 visitor.visit_lifetime(lifetime);
294 walk_list!(visitor, visit_lifetime, bounds);
295 }
David Tolnayfa23f572017-01-23 00:19:11 -0800296 WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty,
297 ref rhs_ty,
298 .. }) => {
299 visitor.visit_ty(lhs_ty);
300 visitor.visit_ty(rhs_ty);
301 }
David Tolnay55337722016-09-11 12:58:56 -0700302 }
303 }
304}
305
306pub fn walk_fn_ret_ty<V: Visitor>(visitor: &mut V, ret_ty: &FunctionRetTy) {
307 if let FunctionRetTy::Ty(ref output_ty) = *ret_ty {
308 visitor.visit_ty(output_ty)
309 }
310}
311
David Tolnay55337722016-09-11 12:58:56 -0700312pub fn walk_variant_data<V: Visitor>(visitor: &mut V, data: &VariantData) {
313 walk_list!(visitor, visit_field, data.fields());
314}
315
316pub fn walk_field<V: Visitor>(visitor: &mut V, field: &Field) {
317 walk_opt_ident(visitor, &field.ident);
318 visitor.visit_ty(&field.ty);
319 walk_list!(visitor, visit_attribute, &field.attrs);
320}
David Tolnay429168f2016-10-05 23:41:04 -0700321
David Tolnay68eb4c32016-10-07 23:30:32 -0700322pub fn walk_const_expr<V: Visitor>(visitor: &mut V, len: &ConstExpr) {
David Tolnay429168f2016-10-05 23:41:04 -0700323 match *len {
David Tolnay68eb4c32016-10-07 23:30:32 -0700324 ConstExpr::Call(ref function, ref args) => {
David Tolnay58f6f672016-10-19 08:44:25 -0700325 visitor.visit_const_expr(function);
David Tolnay68eb4c32016-10-07 23:30:32 -0700326 walk_list!(visitor, visit_const_expr, args);
327 }
328 ConstExpr::Binary(_op, ref left, ref right) => {
329 visitor.visit_const_expr(left);
330 visitor.visit_const_expr(right);
331 }
332 ConstExpr::Unary(_op, ref v) => {
333 visitor.visit_const_expr(v);
334 }
335 ConstExpr::Lit(ref lit) => {
336 visitor.visit_lit(lit);
337 }
338 ConstExpr::Cast(ref expr, ref ty) => {
339 visitor.visit_const_expr(expr);
340 visitor.visit_ty(ty);
341 }
342 ConstExpr::Path(ref path) => {
David Tolnay429168f2016-10-05 23:41:04 -0700343 visitor.visit_path(path);
344 }
David Tolnay67588752016-10-30 12:23:10 -0700345 ConstExpr::Index(ref expr, ref index) => {
346 visitor.visit_const_expr(expr);
347 visitor.visit_const_expr(index);
348 }
David Tolnaye7b0d322016-10-30 10:27:23 -0700349 ConstExpr::Paren(ref expr) => {
350 visitor.visit_const_expr(expr);
351 }
Michael Layzellb52df322017-01-22 17:36:55 -0500352 ConstExpr::Other(ref other) => {
353 #[cfg(feature = "full")]
354 fn walk_other<V: Visitor>(visitor: &mut V, other: &Expr) {
355 visitor.visit_expr(other);
356 }
357 #[cfg(not(feature = "full"))]
358 fn walk_other<V: Visitor>(_: &mut V, _: &super::constant::Other) {}
359 walk_other(visitor, other);
360 }
361 }
362}
363
364#[cfg(feature = "type-macros")]
365pub fn walk_mac<V: Visitor>(visitor: &mut V, mac: &Mac) {
366 visitor.visit_path(&mac.path);
367}
368
369#[cfg(feature = "full")]
370pub fn walk_crate<V: Visitor>(visitor: &mut V, _crate: &Crate) {
371 walk_list!(visitor, visit_attribute, &_crate.attrs);
372 walk_list!(visitor, visit_item, &_crate.items);
373}
374
375#[cfg(feature = "full")]
376pub fn walk_item<V: Visitor>(visitor: &mut V, item: &Item) {
377 visitor.visit_ident(&item.ident);
378 walk_list!(visitor, visit_attribute, &item.attrs);
379 match item.node {
380 ItemKind::ExternCrate(ref ident) => {
381 walk_opt_ident(visitor, ident);
382 }
383 ItemKind::Use(ref view_path) => {
384 visitor.visit_view_path(view_path);
385 }
386 ItemKind::Static(ref ty, _, ref expr) => {
387 visitor.visit_ty(ty);
388 visitor.visit_expr(expr);
389 }
390 ItemKind::Const(ref ty, ref expr) => {
391 visitor.visit_ty(ty);
392 visitor.visit_expr(expr);
393 }
394 ItemKind::Fn(ref decl, _, _, _, ref generics, ref body) => {
395 visitor.visit_fn_decl(decl);
396 visitor.visit_generics(generics);
397 walk_list!(visitor, visit_stmt, &body.stmts);
398 }
399 ItemKind::Mod(ref maybe_items) => {
400 if let Some(ref items) = *maybe_items {
401 walk_list!(visitor, visit_item, items);
402 }
403 }
404 ItemKind::ForeignMod(ref foreign_mod) => {
405 walk_list!(visitor, visit_foreign_item, &foreign_mod.items);
406 }
407 ItemKind::Ty(ref ty, ref generics) => {
408 visitor.visit_ty(ty);
409 visitor.visit_generics(generics);
410 }
411 ItemKind::Enum(ref variant, ref generics) => {
412 walk_list!(visitor, visit_variant, variant, generics);
413 }
414 ItemKind::Struct(ref variant_data, ref generics) => {
415 visitor.visit_variant_data(variant_data, &item.ident, generics);
416 }
417 ItemKind::Union(ref variant_data, ref generics) => {
418 visitor.visit_variant_data(variant_data, &item.ident, generics);
419 }
420 ItemKind::Trait(_, ref generics, ref bounds, ref trait_items) => {
421 visitor.visit_generics(generics);
422 walk_list!(visitor, visit_ty_param_bound, bounds);
423 walk_list!(visitor, visit_trait_item, trait_items);
424 }
425 ItemKind::DefaultImpl(_, ref path) => {
426 visitor.visit_path(path);
427 }
428 ItemKind::Impl(_, _, ref generics, ref maybe_path, ref ty, ref impl_items) => {
429 visitor.visit_generics(generics);
430 if let Some(ref path) = *maybe_path {
431 visitor.visit_path(path);
432 }
433 visitor.visit_ty(ty);
434 walk_list!(visitor, visit_impl_item, impl_items);
435 }
436 ItemKind::Mac(ref mac) => {
437 visitor.visit_mac(mac)
438 }
439 }
440}
441
442#[cfg(feature = "full")]
443pub fn walk_expr<V: Visitor>(visitor: &mut V, expr: &Expr) {
444 walk_list!(visitor, visit_attribute, &expr.attrs);
445 match expr.node {
446 ExprKind::Box(ref expr) => {
447 visitor.visit_expr(expr);
448 }
449 ExprKind::InPlace(ref place, ref value) => {
450 visitor.visit_expr(place);
451 visitor.visit_expr(value);
452 }
David Tolnayfa23f572017-01-23 00:19:11 -0800453 ExprKind::Array(ref exprs) => {
Michael Layzellb52df322017-01-22 17:36:55 -0500454 walk_list!(visitor, visit_expr, exprs);
455 }
456 ExprKind::Call(ref callee, ref args) => {
457 visitor.visit_expr(callee);
458 walk_list!(visitor, visit_expr, args);
459 }
460 ExprKind::MethodCall(ref name, ref ty_args, ref args) => {
461 visitor.visit_ident(name);
462 walk_list!(visitor, visit_ty, ty_args);
463 walk_list!(visitor, visit_expr, args);
464 }
465 ExprKind::Tup(ref exprs) => {
466 walk_list!(visitor, visit_expr, exprs);
467 }
468 ExprKind::Binary(_, ref lhs, ref rhs) => {
469 visitor.visit_expr(lhs);
470 visitor.visit_expr(rhs);
471 }
472 ExprKind::Unary(_, ref operand) => {
473 visitor.visit_expr(operand);
474 }
475 ExprKind::Lit(ref lit) => {
476 visitor.visit_lit(lit);
477 }
478 ExprKind::Cast(ref expr, ref ty) => {
479 visitor.visit_expr(expr);
480 visitor.visit_ty(ty);
481 }
482 ExprKind::Type(ref expr, ref ty) => {
483 visitor.visit_expr(expr);
484 visitor.visit_ty(ty);
485 }
486 ExprKind::If(ref cond, ref cons, ref maybe_alt) => {
487 visitor.visit_expr(cond);
488 walk_list!(visitor, visit_stmt, &cons.stmts);
489 if let Some(ref alt) = *maybe_alt {
490 visitor.visit_expr(alt);
491 }
492 }
493 ExprKind::IfLet(ref pat, ref cond, ref cons, ref maybe_alt) => {
494 visitor.visit_pat(pat);
495 visitor.visit_expr(cond);
496 walk_list!(visitor, visit_stmt, &cons.stmts);
497 if let Some(ref alt) = *maybe_alt {
498 visitor.visit_expr(alt);
499 }
500 }
501 ExprKind::While(ref cond, ref body, ref label) => {
502 visitor.visit_expr(cond);
503 walk_list!(visitor, visit_stmt, &body.stmts);
504 walk_opt_ident(visitor, label);
505 }
506 ExprKind::WhileLet(ref pat, ref cond, ref body, ref label) => {
507 visitor.visit_pat(pat);
508 visitor.visit_expr(cond);
509 walk_list!(visitor, visit_stmt, &body.stmts);
510 walk_opt_ident(visitor, label);
511 }
512 ExprKind::ForLoop(ref pat, ref expr, ref body, ref label) => {
513 visitor.visit_pat(pat);
514 visitor.visit_expr(expr);
515 walk_list!(visitor, visit_stmt, &body.stmts);
516 walk_opt_ident(visitor, label);
517 }
518 ExprKind::Loop(ref body, ref label) => {
519 walk_list!(visitor, visit_stmt, &body.stmts);
520 walk_opt_ident(visitor, label);
521 }
522 ExprKind::Match(ref expr, ref arms) => {
523 visitor.visit_expr(expr);
524 for &Arm{ref attrs, ref pats, ref guard, ref body} in arms {
525 walk_list!(visitor, visit_attribute, attrs);
526 walk_list!(visitor, visit_pat, pats);
527 if let Some(ref guard) = *guard {
528 visitor.visit_expr(guard);
529 }
530 visitor.visit_expr(body);
531 }
532 }
533 ExprKind::Closure(_, ref decl, ref expr) => {
534 visitor.visit_fn_decl(decl);
535 visitor.visit_expr(expr);
536 }
537 ExprKind::Block(_, ref block) => {
538 walk_list!(visitor, visit_stmt, &block.stmts);
539 }
540 ExprKind::Assign(ref lhs, ref rhs) => {
541 visitor.visit_expr(lhs);
542 visitor.visit_expr(rhs);
543 }
544 ExprKind::AssignOp(_, ref lhs, ref rhs) => {
545 visitor.visit_expr(lhs);
546 visitor.visit_expr(rhs);
547 }
548 ExprKind::Field(ref obj, ref field) => {
549 visitor.visit_expr(obj);
550 visitor.visit_ident(field);
551 }
552 ExprKind::TupField(ref obj, _) => {
553 visitor.visit_expr(obj);
554 }
555 ExprKind::Index(ref obj, ref idx) => {
556 visitor.visit_expr(obj);
557 visitor.visit_expr(idx);
558 }
559 ExprKind::Range(ref maybe_start, ref maybe_end, _) => {
560 if let Some(ref start) = *maybe_start {
561 visitor.visit_expr(start);
562 }
563 if let Some(ref end) = *maybe_end {
564 visitor.visit_expr(end);
565 }
566 }
567 ExprKind::Path(ref maybe_qself, ref path) => {
568 if let Some(ref qself) = *maybe_qself {
569 visitor.visit_ty(&qself.ty);
570 }
571 visitor.visit_path(path);
572 }
573 ExprKind::AddrOf(_, ref expr) => {
574 visitor.visit_expr(expr);
575 }
576 ExprKind::Break(ref maybe_label, ref maybe_expr) => {
577 walk_opt_ident(visitor, maybe_label);
578 if let Some(ref expr) = *maybe_expr {
579 visitor.visit_expr(expr);
580 }
581 }
582 ExprKind::Continue(ref maybe_label) => {
583 walk_opt_ident(visitor, maybe_label);
584 }
585 ExprKind::Ret(ref maybe_expr) => {
586 if let Some(ref expr) = *maybe_expr {
587 visitor.visit_expr(expr);
588 }
589 }
590 ExprKind::Mac(ref mac) => {
591 visitor.visit_mac(mac);
592 }
593 ExprKind::Struct(ref path, ref fields, ref maybe_base) => {
594 visitor.visit_path(path);
595 for &FieldValue{ref ident, ref expr, ..} in fields {
596 visitor.visit_ident(ident);
597 visitor.visit_expr(expr);
598 }
599 if let Some(ref base) = *maybe_base {
600 visitor.visit_expr(base);
601 }
602 }
603 ExprKind::Repeat(ref value, ref times) => {
604 visitor.visit_expr(value);
605 visitor.visit_expr(times);
606 }
607 ExprKind::Paren(ref expr) => {
608 visitor.visit_expr(expr);
609 }
610 ExprKind::Try(ref expr) => {
611 visitor.visit_expr(expr);
612 }
613 }
614}
615
616#[cfg(feature = "full")]
617pub fn walk_foreign_item<V: Visitor>(visitor: &mut V, foreign_item: &ForeignItem) {
618 visitor.visit_ident(&foreign_item.ident);
619 walk_list!(visitor, visit_attribute, &foreign_item.attrs);
620 match foreign_item.node {
621 ForeignItemKind::Fn(ref decl, ref generics) => {
622 visitor.visit_fn_decl(decl);
623 visitor.visit_generics(generics);
624 }
625 ForeignItemKind::Static(ref ty, _) => {
626 visitor.visit_ty(ty);
627 }
628 }
629}
630
631#[cfg(feature = "full")]
632pub fn walk_pat<V: Visitor>(visitor: &mut V, pat: &Pat) {
633 match *pat {
634 Pat::Wild => {}
635 Pat::Ident(_, ref ident, ref maybe_pat) => {
636 visitor.visit_ident(ident);
637 if let Some(ref pat) = *maybe_pat {
638 visitor.visit_pat(pat);
639 }
640 }
641 Pat::Struct(ref path, ref field_pats, _) => {
642 visitor.visit_path(path);
643 for &FieldPat{ref ident, ref pat, ..} in field_pats {
644 visitor.visit_ident(ident);
645 visitor.visit_pat(pat);
646 }
647 }
648 Pat::TupleStruct(ref path, ref pats, _) => {
649 visitor.visit_path(path);
650 walk_list!(visitor, visit_pat, pats);
651 }
652 Pat::Path(ref maybe_qself, ref path) => {
653 if let Some(ref qself) = *maybe_qself {
654 visitor.visit_ty(&qself.ty);
655 }
656 visitor.visit_path(path);
657 }
658 Pat::Tuple(ref pats, _) => {
659 walk_list!(visitor, visit_pat, pats);
660 }
661 Pat::Box(ref pat) |
662 Pat::Ref(ref pat, _) => {
663 visitor.visit_pat(pat);
664 }
665 Pat::Lit(ref expr) => {
666 visitor.visit_expr(expr);
667 }
668 Pat::Range(ref start, ref end) => {
669 visitor.visit_expr(start);
670 visitor.visit_expr(end);
671 }
672 Pat::Slice(ref start, ref maybe_mid, ref end) => {
673 walk_list!(visitor, visit_pat, start);
674 if let Some(ref mid) = *maybe_mid {
675 visitor.visit_pat(mid);
676 }
677 walk_list!(visitor, visit_pat, end);
678 }
679 Pat::Mac(ref mac) => {
680 visitor.visit_mac(mac);
681 }
682 }
683}
684
685#[cfg(feature = "full")]
686pub fn walk_fn_decl<V: Visitor>(visitor: &mut V, fn_decl: &FnDecl) {
687 for input in &fn_decl.inputs {
688 match *input {
689 FnArg::SelfRef(_, _) | FnArg::SelfValue(_) => {}
690 FnArg::Captured(ref pat, ref ty) => {
691 visitor.visit_pat(pat);
692 visitor.visit_ty(ty);
693 }
694 FnArg::Ignored(ref ty) => {
695 visitor.visit_ty(ty);
696 }
697 }
698 }
699 visitor.visit_fn_ret_ty(&fn_decl.output);
700}
701
702#[cfg(feature = "full")]
703pub fn walk_trait_item<V: Visitor>(visitor: &mut V, trait_item: &TraitItem) {
704 visitor.visit_ident(&trait_item.ident);
705 walk_list!(visitor, visit_attribute, &trait_item.attrs);
706 match trait_item.node {
707 TraitItemKind::Const(ref ty, ref maybe_expr) => {
708 visitor.visit_ty(ty);
709 if let Some(ref expr) = *maybe_expr {
710 visitor.visit_expr(expr);
711 }
712 }
713 TraitItemKind::Method(ref method_sig, ref maybe_block) => {
714 visitor.visit_method_sig(method_sig);
715 if let Some(ref block) = *maybe_block {
716 walk_list!(visitor, visit_stmt, &block.stmts);
717 }
718 }
719 TraitItemKind::Type(ref bounds, ref maybe_ty) => {
720 walk_list!(visitor, visit_ty_param_bound, bounds);
721 if let Some(ref ty) = *maybe_ty {
722 visitor.visit_ty(ty);
723 }
724 }
725 TraitItemKind::Macro(ref mac) => {
726 visitor.visit_mac(mac);
727 }
728 }
729}
730
731#[cfg(feature = "full")]
732pub fn walk_impl_item<V: Visitor>(visitor: &mut V, impl_item: &ImplItem) {
733 visitor.visit_ident(&impl_item.ident);
734 walk_list!(visitor, visit_attribute, &impl_item.attrs);
735 match impl_item.node {
736 ImplItemKind::Const(ref ty, ref expr) => {
737 visitor.visit_ty(ty);
738 visitor.visit_expr(expr);
739 }
740 ImplItemKind::Method(ref method_sig, ref block) => {
741 visitor.visit_method_sig(method_sig);
742 walk_list!(visitor, visit_stmt, &block.stmts);
743 }
744 ImplItemKind::Type(ref ty) => {
745 visitor.visit_ty(ty);
746 }
747 ImplItemKind::Macro(ref mac) => {
748 visitor.visit_mac(mac);
749 }
750 }
751}
752
753#[cfg(feature = "full")]
754pub fn walk_method_sig<V: Visitor>(visitor: &mut V, method_sig: &MethodSig) {
755 visitor.visit_fn_decl(&method_sig.decl);
756 visitor.visit_generics(&method_sig.generics);
757}
758
759#[cfg(feature = "full")]
760pub fn walk_stmt<V: Visitor>(visitor: &mut V, stmt: &Stmt) {
761 match *stmt {
762 Stmt::Local(ref local) => {
763 visitor.visit_local(local);
764 }
765 Stmt::Item(ref item) => {
766 visitor.visit_item(item);
767 }
768 Stmt::Expr(ref expr) |
769 Stmt::Semi(ref expr) => {
770 visitor.visit_expr(expr);
771 }
772 Stmt::Mac(ref details) => {
773 let (ref mac, _, ref attrs) = **details;
774 visitor.visit_mac(mac);
775 walk_list!(visitor, visit_attribute, attrs);
776 }
777 }
778}
779
780#[cfg(feature = "full")]
781pub fn walk_local<V: Visitor>(visitor: &mut V, local: &Local) {
782 visitor.visit_pat(&local.pat);
783 if let Some(ref ty) = local.ty {
784 visitor.visit_ty(ty);
785 }
786 if let Some(ref init) = local.init {
787 visitor.visit_expr(init);
788 }
789 walk_list!(visitor, visit_attribute, &local.attrs);
790}
791
792#[cfg(feature = "full")]
793pub fn walk_view_path<V: Visitor>(visitor: &mut V, view_path: &ViewPath) {
794 match *view_path {
795 ViewPath::Simple(ref path, ref maybe_ident) => {
796 visitor.visit_path(path);
797 walk_opt_ident(visitor, maybe_ident);
798 }
799 ViewPath::Glob(ref path) => {
800 visitor.visit_path(path);
801 }
802 ViewPath::List(ref path, ref items) => {
803 visitor.visit_path(path);
804 for &PathListItem{ref name, ref rename} in items {
805 visitor.visit_ident(name);
806 walk_opt_ident(visitor, rename);
807 }
808 }
David Tolnay429168f2016-10-05 23:41:04 -0700809 }
810}