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