blob: a5d4f41afca66981cbe556f6141cf27b5ba17918 [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
Michael Layzellb52df322017-01-22 17:36:55 -050080 fn visit_mac(&mut self, mac: &Mac) {
81 walk_mac(self, mac);
82 }
83
84 #[cfg(feature = "full")]
85 fn visit_crate(&mut self, _crate: &Crate) {
86 walk_crate(self, _crate);
87 }
88 #[cfg(feature = "full")]
89 fn visit_item(&mut self, item: &Item) {
90 walk_item(self, item);
91 }
92 #[cfg(feature = "full")]
93 fn visit_expr(&mut self, expr: &Expr) {
94 walk_expr(self, expr);
95 }
96 #[cfg(feature = "full")]
97 fn visit_foreign_item(&mut self, foreign_item: &ForeignItem) {
98 walk_foreign_item(self, foreign_item);
99 }
100 #[cfg(feature = "full")]
101 fn visit_pat(&mut self, pat: &Pat) {
102 walk_pat(self, pat);
103 }
104 #[cfg(feature = "full")]
105 fn visit_fn_decl(&mut self, fn_decl: &FnDecl) {
106 walk_fn_decl(self, fn_decl);
107 }
108 #[cfg(feature = "full")]
109 fn visit_trait_item(&mut self, trait_item: &TraitItem) {
110 walk_trait_item(self, trait_item);
111 }
112 #[cfg(feature = "full")]
113 fn visit_impl_item(&mut self, impl_item: &ImplItem) {
114 walk_impl_item(self, impl_item);
115 }
116 #[cfg(feature = "full")]
117 fn visit_method_sig(&mut self, method_sig: &MethodSig) {
118 walk_method_sig(self, method_sig);
119 }
120 #[cfg(feature = "full")]
121 fn visit_stmt(&mut self, stmt: &Stmt) {
122 walk_stmt(self, stmt);
123 }
124 #[cfg(feature = "full")]
125 fn visit_local(&mut self, local: &Local) {
126 walk_local(self, local);
127 }
128 #[cfg(feature = "full")]
129 fn visit_view_path(&mut self, view_path: &ViewPath) {
130 walk_view_path(self, view_path);
131 }
David Tolnay55337722016-09-11 12:58:56 -0700132}
133
David Tolnay55337722016-09-11 12:58:56 -0700134macro_rules! walk_list {
David Tolnaybdae3642017-01-23 00:39:55 -0800135 ($visitor:expr, $method:ident, $list:expr $(, $extra_args:expr)*) => {
David Tolnay55337722016-09-11 12:58:56 -0700136 for elem in $list {
David Tolnaybdae3642017-01-23 00:39:55 -0800137 $visitor.$method(elem $(, $extra_args)*)
David Tolnay55337722016-09-11 12:58:56 -0700138 }
139 };
David Tolnay55337722016-09-11 12:58:56 -0700140}
141
142pub fn walk_opt_ident<V: Visitor>(visitor: &mut V, opt_ident: &Option<Ident>) {
143 if let Some(ref ident) = *opt_ident {
144 visitor.visit_ident(ident);
145 }
146}
147
148pub fn walk_lifetime_def<V: Visitor>(visitor: &mut V, lifetime_def: &LifetimeDef) {
149 visitor.visit_lifetime(&lifetime_def.lifetime);
150 walk_list!(visitor, visit_lifetime, &lifetime_def.bounds);
151}
152
153pub fn walk_poly_trait_ref<V>(visitor: &mut V, trait_ref: &PolyTraitRef, _: &TraitBoundModifier)
David Tolnaydaaf7742016-10-03 11:11:43 -0700154 where V: Visitor
David Tolnay55337722016-09-11 12:58:56 -0700155{
156 walk_list!(visitor, visit_lifetime_def, &trait_ref.bound_lifetimes);
157 visitor.visit_path(&trait_ref.trait_ref);
158}
159
David Tolnay0e837402016-12-22 17:25:55 -0500160pub fn walk_derive_input<V: Visitor>(visitor: &mut V, derive_input: &DeriveInput) {
161 visitor.visit_ident(&derive_input.ident);
162 visitor.visit_generics(&derive_input.generics);
163 match derive_input.body {
David Tolnay55337722016-09-11 12:58:56 -0700164 Body::Enum(ref variants) => {
David Tolnay0e837402016-12-22 17:25:55 -0500165 walk_list!(visitor, visit_variant, variants, &derive_input.generics);
David Tolnay55337722016-09-11 12:58:56 -0700166 }
167 Body::Struct(ref variant_data) => {
David Tolnay0e837402016-12-22 17:25:55 -0500168 visitor.visit_variant_data(variant_data, &derive_input.ident, &derive_input.generics);
David Tolnay55337722016-09-11 12:58:56 -0700169 }
170 }
David Tolnay0e837402016-12-22 17:25:55 -0500171 walk_list!(visitor, visit_attribute, &derive_input.attrs);
David Tolnay55337722016-09-11 12:58:56 -0700172}
173
174pub fn walk_variant<V>(visitor: &mut V, variant: &Variant, generics: &Generics)
David Tolnaydaaf7742016-10-03 11:11:43 -0700175 where V: Visitor
David Tolnay55337722016-09-11 12:58:56 -0700176{
177 visitor.visit_ident(&variant.ident);
178 visitor.visit_variant_data(&variant.data, &variant.ident, generics);
179 walk_list!(visitor, visit_attribute, &variant.attrs);
180}
181
182pub fn walk_ty<V: Visitor>(visitor: &mut V, ty: &Ty) {
183 match *ty {
David Tolnay429168f2016-10-05 23:41:04 -0700184 Ty::Slice(ref inner) |
David Tolnaydaaf7742016-10-03 11:11:43 -0700185 Ty::Paren(ref inner) => visitor.visit_ty(inner),
186 Ty::Ptr(ref mutable_type) => visitor.visit_ty(&mutable_type.ty),
David Tolnay55337722016-09-11 12:58:56 -0700187 Ty::Rptr(ref opt_lifetime, ref mutable_type) => {
188 walk_list!(visitor, visit_lifetime, opt_lifetime);
189 visitor.visit_ty(&mutable_type.ty)
190 }
David Tolnay58f6f672016-10-19 08:44:25 -0700191 Ty::Never | Ty::Infer => {}
David Tolnay55337722016-09-11 12:58:56 -0700192 Ty::Tup(ref tuple_element_types) => {
193 walk_list!(visitor, visit_ty, tuple_element_types);
194 }
David Tolnay1391b522016-10-03 21:05:45 -0700195 Ty::BareFn(ref bare_fn) => {
196 walk_list!(visitor, visit_lifetime_def, &bare_fn.lifetimes);
197 for argument in &bare_fn.inputs {
198 walk_opt_ident(visitor, &argument.name);
199 visitor.visit_ty(&argument.ty)
200 }
201 visitor.visit_fn_ret_ty(&bare_fn.output)
David Tolnay55337722016-09-11 12:58:56 -0700202 }
203 Ty::Path(ref maybe_qself, ref path) => {
204 if let Some(ref qself) = *maybe_qself {
205 visitor.visit_ty(&qself.ty);
206 }
207 visitor.visit_path(path);
208 }
David Tolnay429168f2016-10-05 23:41:04 -0700209 Ty::Array(ref inner, ref len) => {
David Tolnay55337722016-09-11 12:58:56 -0700210 visitor.visit_ty(inner);
David Tolnay68eb4c32016-10-07 23:30:32 -0700211 visitor.visit_const_expr(len);
David Tolnay55337722016-09-11 12:58:56 -0700212 }
David Tolnayfa23f572017-01-23 00:19:11 -0800213 Ty::TraitObject(ref bounds) |
David Tolnay55337722016-09-11 12:58:56 -0700214 Ty::ImplTrait(ref bounds) => {
215 walk_list!(visitor, visit_ty_param_bound, bounds);
216 }
Michael Layzellb52df322017-01-22 17:36:55 -0500217 Ty::Mac(ref mac) => {
David Tolnay3f36a0a2017-01-23 17:59:46 -0800218 visitor.visit_mac(mac);
Michael Layzellb52df322017-01-22 17:36:55 -0500219 }
David Tolnay55337722016-09-11 12:58:56 -0700220 }
221}
222
223pub fn walk_path<V: Visitor>(visitor: &mut V, path: &Path) {
224 for segment in &path.segments {
225 visitor.visit_path_segment(segment);
226 }
227}
228
229pub fn walk_path_segment<V: Visitor>(visitor: &mut V, segment: &PathSegment) {
230 visitor.visit_ident(&segment.ident);
231 visitor.visit_path_parameters(&segment.parameters);
232}
233
234pub fn walk_path_parameters<V>(visitor: &mut V, path_parameters: &PathParameters)
David Tolnaydaaf7742016-10-03 11:11:43 -0700235 where V: Visitor
David Tolnay55337722016-09-11 12:58:56 -0700236{
237 match *path_parameters {
238 PathParameters::AngleBracketed(ref data) => {
239 walk_list!(visitor, visit_ty, &data.types);
240 walk_list!(visitor, visit_lifetime, &data.lifetimes);
241 walk_list!(visitor, visit_assoc_type_binding, &data.bindings);
242 }
243 PathParameters::Parenthesized(ref data) => {
244 walk_list!(visitor, visit_ty, &data.inputs);
245 walk_list!(visitor, visit_ty, &data.output);
246 }
247 }
248}
249
250pub fn walk_assoc_type_binding<V: Visitor>(visitor: &mut V, type_binding: &TypeBinding) {
251 visitor.visit_ident(&type_binding.ident);
252 visitor.visit_ty(&type_binding.ty);
253}
254
255pub fn walk_ty_param_bound<V: Visitor>(visitor: &mut V, bound: &TyParamBound) {
256 match *bound {
257 TyParamBound::Trait(ref ty, ref modifier) => {
258 visitor.visit_poly_trait_ref(ty, modifier);
259 }
260 TyParamBound::Region(ref lifetime) => {
261 visitor.visit_lifetime(lifetime);
262 }
263 }
264}
265
266pub fn walk_generics<V: Visitor>(visitor: &mut V, generics: &Generics) {
267 for param in &generics.ty_params {
268 visitor.visit_ident(&param.ident);
269 walk_list!(visitor, visit_ty_param_bound, &param.bounds);
270 walk_list!(visitor, visit_ty, &param.default);
271 }
272 walk_list!(visitor, visit_lifetime_def, &generics.lifetimes);
273 for predicate in &generics.where_clause.predicates {
274 match *predicate {
David Tolnaydaaf7742016-10-03 11:11:43 -0700275 WherePredicate::BoundPredicate(WhereBoundPredicate { ref bounded_ty,
276 ref bounds,
277 ref bound_lifetimes,
278 .. }) => {
David Tolnay55337722016-09-11 12:58:56 -0700279 visitor.visit_ty(bounded_ty);
280 walk_list!(visitor, visit_ty_param_bound, bounds);
281 walk_list!(visitor, visit_lifetime_def, bound_lifetimes);
282 }
David Tolnaydaaf7742016-10-03 11:11:43 -0700283 WherePredicate::RegionPredicate(WhereRegionPredicate { ref lifetime,
284 ref bounds,
285 .. }) => {
David Tolnay55337722016-09-11 12:58:56 -0700286 visitor.visit_lifetime(lifetime);
287 walk_list!(visitor, visit_lifetime, bounds);
288 }
David Tolnayfa23f572017-01-23 00:19:11 -0800289 WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty,
290 ref rhs_ty,
291 .. }) => {
292 visitor.visit_ty(lhs_ty);
293 visitor.visit_ty(rhs_ty);
294 }
David Tolnay55337722016-09-11 12:58:56 -0700295 }
296 }
297}
298
299pub fn walk_fn_ret_ty<V: Visitor>(visitor: &mut V, ret_ty: &FunctionRetTy) {
300 if let FunctionRetTy::Ty(ref output_ty) = *ret_ty {
301 visitor.visit_ty(output_ty)
302 }
303}
304
David Tolnay55337722016-09-11 12:58:56 -0700305pub fn walk_variant_data<V: Visitor>(visitor: &mut V, data: &VariantData) {
306 walk_list!(visitor, visit_field, data.fields());
307}
308
309pub fn walk_field<V: Visitor>(visitor: &mut V, field: &Field) {
310 walk_opt_ident(visitor, &field.ident);
311 visitor.visit_ty(&field.ty);
312 walk_list!(visitor, visit_attribute, &field.attrs);
313}
David Tolnay429168f2016-10-05 23:41:04 -0700314
David Tolnay68eb4c32016-10-07 23:30:32 -0700315pub fn walk_const_expr<V: Visitor>(visitor: &mut V, len: &ConstExpr) {
David Tolnay429168f2016-10-05 23:41:04 -0700316 match *len {
David Tolnay68eb4c32016-10-07 23:30:32 -0700317 ConstExpr::Call(ref function, ref args) => {
David Tolnay58f6f672016-10-19 08:44:25 -0700318 visitor.visit_const_expr(function);
David Tolnay68eb4c32016-10-07 23:30:32 -0700319 walk_list!(visitor, visit_const_expr, args);
320 }
321 ConstExpr::Binary(_op, ref left, ref right) => {
322 visitor.visit_const_expr(left);
323 visitor.visit_const_expr(right);
324 }
325 ConstExpr::Unary(_op, ref v) => {
326 visitor.visit_const_expr(v);
327 }
328 ConstExpr::Lit(ref lit) => {
329 visitor.visit_lit(lit);
330 }
331 ConstExpr::Cast(ref expr, ref ty) => {
332 visitor.visit_const_expr(expr);
333 visitor.visit_ty(ty);
334 }
335 ConstExpr::Path(ref path) => {
David Tolnay429168f2016-10-05 23:41:04 -0700336 visitor.visit_path(path);
337 }
David Tolnay67588752016-10-30 12:23:10 -0700338 ConstExpr::Index(ref expr, ref index) => {
339 visitor.visit_const_expr(expr);
340 visitor.visit_const_expr(index);
341 }
David Tolnaye7b0d322016-10-30 10:27:23 -0700342 ConstExpr::Paren(ref expr) => {
343 visitor.visit_const_expr(expr);
344 }
Michael Layzellb52df322017-01-22 17:36:55 -0500345 ConstExpr::Other(ref other) => {
346 #[cfg(feature = "full")]
347 fn walk_other<V: Visitor>(visitor: &mut V, other: &Expr) {
348 visitor.visit_expr(other);
349 }
350 #[cfg(not(feature = "full"))]
351 fn walk_other<V: Visitor>(_: &mut V, _: &super::constant::Other) {}
352 walk_other(visitor, other);
353 }
354 }
355}
356
Michael Layzellb52df322017-01-22 17:36:55 -0500357pub fn walk_mac<V: Visitor>(visitor: &mut V, mac: &Mac) {
358 visitor.visit_path(&mac.path);
359}
360
361#[cfg(feature = "full")]
362pub fn walk_crate<V: Visitor>(visitor: &mut V, _crate: &Crate) {
363 walk_list!(visitor, visit_attribute, &_crate.attrs);
364 walk_list!(visitor, visit_item, &_crate.items);
365}
366
367#[cfg(feature = "full")]
368pub fn walk_item<V: Visitor>(visitor: &mut V, item: &Item) {
369 visitor.visit_ident(&item.ident);
370 walk_list!(visitor, visit_attribute, &item.attrs);
371 match item.node {
372 ItemKind::ExternCrate(ref ident) => {
373 walk_opt_ident(visitor, ident);
374 }
375 ItemKind::Use(ref view_path) => {
376 visitor.visit_view_path(view_path);
377 }
378 ItemKind::Static(ref ty, _, ref expr) => {
379 visitor.visit_ty(ty);
380 visitor.visit_expr(expr);
381 }
382 ItemKind::Const(ref ty, ref expr) => {
383 visitor.visit_ty(ty);
384 visitor.visit_expr(expr);
385 }
386 ItemKind::Fn(ref decl, _, _, _, ref generics, ref body) => {
387 visitor.visit_fn_decl(decl);
388 visitor.visit_generics(generics);
389 walk_list!(visitor, visit_stmt, &body.stmts);
390 }
391 ItemKind::Mod(ref maybe_items) => {
392 if let Some(ref items) = *maybe_items {
393 walk_list!(visitor, visit_item, items);
394 }
395 }
396 ItemKind::ForeignMod(ref foreign_mod) => {
397 walk_list!(visitor, visit_foreign_item, &foreign_mod.items);
398 }
399 ItemKind::Ty(ref ty, ref generics) => {
400 visitor.visit_ty(ty);
401 visitor.visit_generics(generics);
402 }
403 ItemKind::Enum(ref variant, ref generics) => {
404 walk_list!(visitor, visit_variant, variant, generics);
405 }
406 ItemKind::Struct(ref variant_data, ref generics) => {
407 visitor.visit_variant_data(variant_data, &item.ident, generics);
408 }
409 ItemKind::Union(ref variant_data, ref generics) => {
410 visitor.visit_variant_data(variant_data, &item.ident, generics);
411 }
412 ItemKind::Trait(_, ref generics, ref bounds, ref trait_items) => {
413 visitor.visit_generics(generics);
414 walk_list!(visitor, visit_ty_param_bound, bounds);
415 walk_list!(visitor, visit_trait_item, trait_items);
416 }
417 ItemKind::DefaultImpl(_, ref path) => {
418 visitor.visit_path(path);
419 }
420 ItemKind::Impl(_, _, ref generics, ref maybe_path, ref ty, ref impl_items) => {
421 visitor.visit_generics(generics);
422 if let Some(ref path) = *maybe_path {
423 visitor.visit_path(path);
424 }
425 visitor.visit_ty(ty);
426 walk_list!(visitor, visit_impl_item, impl_items);
427 }
428 ItemKind::Mac(ref mac) => {
429 visitor.visit_mac(mac)
430 }
431 }
432}
433
434#[cfg(feature = "full")]
435pub fn walk_expr<V: Visitor>(visitor: &mut V, expr: &Expr) {
436 walk_list!(visitor, visit_attribute, &expr.attrs);
437 match expr.node {
438 ExprKind::Box(ref expr) => {
439 visitor.visit_expr(expr);
440 }
441 ExprKind::InPlace(ref place, ref value) => {
442 visitor.visit_expr(place);
443 visitor.visit_expr(value);
444 }
David Tolnayfa23f572017-01-23 00:19:11 -0800445 ExprKind::Array(ref exprs) => {
Michael Layzellb52df322017-01-22 17:36:55 -0500446 walk_list!(visitor, visit_expr, exprs);
447 }
448 ExprKind::Call(ref callee, ref args) => {
449 visitor.visit_expr(callee);
450 walk_list!(visitor, visit_expr, args);
451 }
452 ExprKind::MethodCall(ref name, ref ty_args, ref args) => {
453 visitor.visit_ident(name);
454 walk_list!(visitor, visit_ty, ty_args);
455 walk_list!(visitor, visit_expr, args);
456 }
457 ExprKind::Tup(ref exprs) => {
458 walk_list!(visitor, visit_expr, exprs);
459 }
460 ExprKind::Binary(_, ref lhs, ref rhs) => {
461 visitor.visit_expr(lhs);
462 visitor.visit_expr(rhs);
463 }
464 ExprKind::Unary(_, ref operand) => {
465 visitor.visit_expr(operand);
466 }
467 ExprKind::Lit(ref lit) => {
468 visitor.visit_lit(lit);
469 }
470 ExprKind::Cast(ref expr, ref ty) => {
471 visitor.visit_expr(expr);
472 visitor.visit_ty(ty);
473 }
474 ExprKind::Type(ref expr, ref ty) => {
475 visitor.visit_expr(expr);
476 visitor.visit_ty(ty);
477 }
478 ExprKind::If(ref cond, ref cons, ref maybe_alt) => {
479 visitor.visit_expr(cond);
480 walk_list!(visitor, visit_stmt, &cons.stmts);
481 if let Some(ref alt) = *maybe_alt {
482 visitor.visit_expr(alt);
483 }
484 }
485 ExprKind::IfLet(ref pat, ref cond, ref cons, ref maybe_alt) => {
486 visitor.visit_pat(pat);
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::While(ref cond, ref body, ref label) => {
494 visitor.visit_expr(cond);
495 walk_list!(visitor, visit_stmt, &body.stmts);
496 walk_opt_ident(visitor, label);
497 }
498 ExprKind::WhileLet(ref pat, ref cond, ref body, ref label) => {
499 visitor.visit_pat(pat);
500 visitor.visit_expr(cond);
501 walk_list!(visitor, visit_stmt, &body.stmts);
502 walk_opt_ident(visitor, label);
503 }
504 ExprKind::ForLoop(ref pat, ref expr, ref body, ref label) => {
505 visitor.visit_pat(pat);
506 visitor.visit_expr(expr);
507 walk_list!(visitor, visit_stmt, &body.stmts);
508 walk_opt_ident(visitor, label);
509 }
510 ExprKind::Loop(ref body, ref label) => {
511 walk_list!(visitor, visit_stmt, &body.stmts);
512 walk_opt_ident(visitor, label);
513 }
514 ExprKind::Match(ref expr, ref arms) => {
515 visitor.visit_expr(expr);
516 for &Arm{ref attrs, ref pats, ref guard, ref body} in arms {
517 walk_list!(visitor, visit_attribute, attrs);
518 walk_list!(visitor, visit_pat, pats);
519 if let Some(ref guard) = *guard {
520 visitor.visit_expr(guard);
521 }
522 visitor.visit_expr(body);
523 }
524 }
525 ExprKind::Closure(_, ref decl, ref expr) => {
526 visitor.visit_fn_decl(decl);
527 visitor.visit_expr(expr);
528 }
529 ExprKind::Block(_, ref block) => {
530 walk_list!(visitor, visit_stmt, &block.stmts);
531 }
532 ExprKind::Assign(ref lhs, ref rhs) => {
533 visitor.visit_expr(lhs);
534 visitor.visit_expr(rhs);
535 }
536 ExprKind::AssignOp(_, ref lhs, ref rhs) => {
537 visitor.visit_expr(lhs);
538 visitor.visit_expr(rhs);
539 }
540 ExprKind::Field(ref obj, ref field) => {
541 visitor.visit_expr(obj);
542 visitor.visit_ident(field);
543 }
544 ExprKind::TupField(ref obj, _) => {
545 visitor.visit_expr(obj);
546 }
547 ExprKind::Index(ref obj, ref idx) => {
548 visitor.visit_expr(obj);
549 visitor.visit_expr(idx);
550 }
551 ExprKind::Range(ref maybe_start, ref maybe_end, _) => {
552 if let Some(ref start) = *maybe_start {
553 visitor.visit_expr(start);
554 }
555 if let Some(ref end) = *maybe_end {
556 visitor.visit_expr(end);
557 }
558 }
559 ExprKind::Path(ref maybe_qself, ref path) => {
560 if let Some(ref qself) = *maybe_qself {
561 visitor.visit_ty(&qself.ty);
562 }
563 visitor.visit_path(path);
564 }
565 ExprKind::AddrOf(_, ref expr) => {
566 visitor.visit_expr(expr);
567 }
568 ExprKind::Break(ref maybe_label, ref maybe_expr) => {
569 walk_opt_ident(visitor, maybe_label);
570 if let Some(ref expr) = *maybe_expr {
571 visitor.visit_expr(expr);
572 }
573 }
574 ExprKind::Continue(ref maybe_label) => {
575 walk_opt_ident(visitor, maybe_label);
576 }
577 ExprKind::Ret(ref maybe_expr) => {
578 if let Some(ref expr) = *maybe_expr {
579 visitor.visit_expr(expr);
580 }
581 }
582 ExprKind::Mac(ref mac) => {
583 visitor.visit_mac(mac);
584 }
585 ExprKind::Struct(ref path, ref fields, ref maybe_base) => {
586 visitor.visit_path(path);
587 for &FieldValue{ref ident, ref expr, ..} in fields {
588 visitor.visit_ident(ident);
589 visitor.visit_expr(expr);
590 }
591 if let Some(ref base) = *maybe_base {
592 visitor.visit_expr(base);
593 }
594 }
595 ExprKind::Repeat(ref value, ref times) => {
596 visitor.visit_expr(value);
597 visitor.visit_expr(times);
598 }
599 ExprKind::Paren(ref expr) => {
600 visitor.visit_expr(expr);
601 }
602 ExprKind::Try(ref expr) => {
603 visitor.visit_expr(expr);
604 }
605 }
606}
607
608#[cfg(feature = "full")]
609pub fn walk_foreign_item<V: Visitor>(visitor: &mut V, foreign_item: &ForeignItem) {
610 visitor.visit_ident(&foreign_item.ident);
611 walk_list!(visitor, visit_attribute, &foreign_item.attrs);
612 match foreign_item.node {
613 ForeignItemKind::Fn(ref decl, ref generics) => {
614 visitor.visit_fn_decl(decl);
615 visitor.visit_generics(generics);
616 }
617 ForeignItemKind::Static(ref ty, _) => {
618 visitor.visit_ty(ty);
619 }
620 }
621}
622
623#[cfg(feature = "full")]
624pub fn walk_pat<V: Visitor>(visitor: &mut V, pat: &Pat) {
625 match *pat {
626 Pat::Wild => {}
627 Pat::Ident(_, ref ident, ref maybe_pat) => {
628 visitor.visit_ident(ident);
629 if let Some(ref pat) = *maybe_pat {
630 visitor.visit_pat(pat);
631 }
632 }
633 Pat::Struct(ref path, ref field_pats, _) => {
634 visitor.visit_path(path);
635 for &FieldPat{ref ident, ref pat, ..} in field_pats {
636 visitor.visit_ident(ident);
637 visitor.visit_pat(pat);
638 }
639 }
640 Pat::TupleStruct(ref path, ref pats, _) => {
641 visitor.visit_path(path);
642 walk_list!(visitor, visit_pat, pats);
643 }
644 Pat::Path(ref maybe_qself, ref path) => {
645 if let Some(ref qself) = *maybe_qself {
646 visitor.visit_ty(&qself.ty);
647 }
648 visitor.visit_path(path);
649 }
650 Pat::Tuple(ref pats, _) => {
651 walk_list!(visitor, visit_pat, pats);
652 }
653 Pat::Box(ref pat) |
654 Pat::Ref(ref pat, _) => {
655 visitor.visit_pat(pat);
656 }
657 Pat::Lit(ref expr) => {
658 visitor.visit_expr(expr);
659 }
660 Pat::Range(ref start, ref end) => {
661 visitor.visit_expr(start);
662 visitor.visit_expr(end);
663 }
664 Pat::Slice(ref start, ref maybe_mid, ref end) => {
665 walk_list!(visitor, visit_pat, start);
666 if let Some(ref mid) = *maybe_mid {
667 visitor.visit_pat(mid);
668 }
669 walk_list!(visitor, visit_pat, end);
670 }
671 Pat::Mac(ref mac) => {
672 visitor.visit_mac(mac);
673 }
674 }
675}
676
677#[cfg(feature = "full")]
678pub fn walk_fn_decl<V: Visitor>(visitor: &mut V, fn_decl: &FnDecl) {
679 for input in &fn_decl.inputs {
680 match *input {
681 FnArg::SelfRef(_, _) | FnArg::SelfValue(_) => {}
682 FnArg::Captured(ref pat, ref ty) => {
683 visitor.visit_pat(pat);
684 visitor.visit_ty(ty);
685 }
686 FnArg::Ignored(ref ty) => {
687 visitor.visit_ty(ty);
688 }
689 }
690 }
691 visitor.visit_fn_ret_ty(&fn_decl.output);
692}
693
694#[cfg(feature = "full")]
695pub fn walk_trait_item<V: Visitor>(visitor: &mut V, trait_item: &TraitItem) {
696 visitor.visit_ident(&trait_item.ident);
697 walk_list!(visitor, visit_attribute, &trait_item.attrs);
698 match trait_item.node {
699 TraitItemKind::Const(ref ty, ref maybe_expr) => {
700 visitor.visit_ty(ty);
701 if let Some(ref expr) = *maybe_expr {
702 visitor.visit_expr(expr);
703 }
704 }
705 TraitItemKind::Method(ref method_sig, ref maybe_block) => {
706 visitor.visit_method_sig(method_sig);
707 if let Some(ref block) = *maybe_block {
708 walk_list!(visitor, visit_stmt, &block.stmts);
709 }
710 }
711 TraitItemKind::Type(ref bounds, ref maybe_ty) => {
712 walk_list!(visitor, visit_ty_param_bound, bounds);
713 if let Some(ref ty) = *maybe_ty {
714 visitor.visit_ty(ty);
715 }
716 }
717 TraitItemKind::Macro(ref mac) => {
718 visitor.visit_mac(mac);
719 }
720 }
721}
722
723#[cfg(feature = "full")]
724pub fn walk_impl_item<V: Visitor>(visitor: &mut V, impl_item: &ImplItem) {
725 visitor.visit_ident(&impl_item.ident);
726 walk_list!(visitor, visit_attribute, &impl_item.attrs);
727 match impl_item.node {
728 ImplItemKind::Const(ref ty, ref expr) => {
729 visitor.visit_ty(ty);
730 visitor.visit_expr(expr);
731 }
732 ImplItemKind::Method(ref method_sig, ref block) => {
733 visitor.visit_method_sig(method_sig);
734 walk_list!(visitor, visit_stmt, &block.stmts);
735 }
736 ImplItemKind::Type(ref ty) => {
737 visitor.visit_ty(ty);
738 }
739 ImplItemKind::Macro(ref mac) => {
740 visitor.visit_mac(mac);
741 }
742 }
743}
744
745#[cfg(feature = "full")]
746pub fn walk_method_sig<V: Visitor>(visitor: &mut V, method_sig: &MethodSig) {
747 visitor.visit_fn_decl(&method_sig.decl);
748 visitor.visit_generics(&method_sig.generics);
749}
750
751#[cfg(feature = "full")]
752pub fn walk_stmt<V: Visitor>(visitor: &mut V, stmt: &Stmt) {
753 match *stmt {
754 Stmt::Local(ref local) => {
755 visitor.visit_local(local);
756 }
757 Stmt::Item(ref item) => {
758 visitor.visit_item(item);
759 }
760 Stmt::Expr(ref expr) |
761 Stmt::Semi(ref expr) => {
762 visitor.visit_expr(expr);
763 }
764 Stmt::Mac(ref details) => {
765 let (ref mac, _, ref attrs) = **details;
766 visitor.visit_mac(mac);
767 walk_list!(visitor, visit_attribute, attrs);
768 }
769 }
770}
771
772#[cfg(feature = "full")]
773pub fn walk_local<V: Visitor>(visitor: &mut V, local: &Local) {
774 visitor.visit_pat(&local.pat);
775 if let Some(ref ty) = local.ty {
776 visitor.visit_ty(ty);
777 }
778 if let Some(ref init) = local.init {
779 visitor.visit_expr(init);
780 }
781 walk_list!(visitor, visit_attribute, &local.attrs);
782}
783
784#[cfg(feature = "full")]
785pub fn walk_view_path<V: Visitor>(visitor: &mut V, view_path: &ViewPath) {
786 match *view_path {
787 ViewPath::Simple(ref path, ref maybe_ident) => {
788 visitor.visit_path(path);
789 walk_opt_ident(visitor, maybe_ident);
790 }
791 ViewPath::Glob(ref path) => {
792 visitor.visit_path(path);
793 }
794 ViewPath::List(ref path, ref items) => {
795 visitor.visit_path(path);
796 for &PathListItem{ref name, ref rename} in items {
797 visitor.visit_ident(name);
798 walk_opt_ident(visitor, rename);
799 }
800 }
David Tolnay429168f2016-10-05 23:41:04 -0700801 }
802}