]> git.proxmox.com Git - rustc.git/blame - src/libsyntax/visit.rs
New upstream version 1.35.0+dfsg1
[rustc.git] / src / libsyntax / visit.rs
CommitLineData
1a4d82fc
JJ
1//! AST walker. Each overridden visit method has full control over what
2//! happens with its node, it can do its own traversal of the node's children,
3//! call `visit::walk_*` to apply the default traversal algorithm, or prevent
4//! deeper traversal by doing nothing.
5//!
6//! Note: it is an important invariant that the default visitor walks the body
7//! of a function in "execution order" (more concretely, reverse post-order
8//! with respect to the CFG implied by the AST), meaning that if AST node A may
9fa01778 9//! execute before AST node B, then A is visited first. The borrow checker in
1a4d82fc
JJ
10//! particular relies on this property.
11//!
12//! Note: walking an AST before macro expansion is probably a bad idea. For
13//! instance, a walker looking for item names in a module will miss all of
14//! those that are created by the expansion of a macro.
15
9fa01778
XL
16use crate::ast::*;
17use crate::parse::token::Token;
18use crate::tokenstream::{TokenTree, TokenStream};
19
3157f602 20use syntax_pos::Span;
1a4d82fc 21
8faf50e0 22#[derive(Copy, Clone)]
1a4d82fc
JJ
23pub enum FnKind<'a> {
24 /// fn foo() or extern "Abi" fn foo()
532ac7d7 25 ItemFn(Ident, &'a FnHeader, &'a Visibility, &'a Block),
1a4d82fc
JJ
26
27 /// fn foo(&self)
476ff2be 28 Method(Ident, &'a MethodSig, Option<&'a Visibility>, &'a Block),
1a4d82fc 29
476ff2be
SL
30 /// |x, y| body
31 Closure(&'a Expr),
1a4d82fc
JJ
32}
33
34/// Each method of the Visitor trait is a hook to be potentially
9fa01778 35/// overridden. Each method's default implementation recursively visits
1a4d82fc 36/// the substructure of the input via the corresponding `walk` method;
0731742a 37/// e.g., the `visit_mod` method by default calls `visit::walk_mod`.
1a4d82fc
JJ
38///
39/// If you want to ensure that your code handles every variant
9fa01778 40/// explicitly, you need to override each method. (And you also need
1a4d82fc
JJ
41/// to monitor future changes to `Visitor` in case a new method with a
42/// new default implementation gets introduced.)
476ff2be 43pub trait Visitor<'ast>: Sized {
1a4d82fc
JJ
44 fn visit_name(&mut self, _span: Span, _name: Name) {
45 // Nothing to do.
46 }
83c7162d
XL
47 fn visit_ident(&mut self, ident: Ident) {
48 walk_ident(self, ident);
1a4d82fc 49 }
7cac9316
XL
50 fn visit_mod(&mut self, m: &'ast Mod, _s: Span, _attrs: &[Attribute], _n: NodeId) {
51 walk_mod(self, m);
52 }
476ff2be 53 fn visit_foreign_item(&mut self, i: &'ast ForeignItem) { walk_foreign_item(self, i) }
cc61c64b 54 fn visit_global_asm(&mut self, ga: &'ast GlobalAsm) { walk_global_asm(self, ga) }
476ff2be
SL
55 fn visit_item(&mut self, i: &'ast Item) { walk_item(self, i) }
56 fn visit_local(&mut self, l: &'ast Local) { walk_local(self, l) }
57 fn visit_block(&mut self, b: &'ast Block) { walk_block(self, b) }
58 fn visit_stmt(&mut self, s: &'ast Stmt) { walk_stmt(self, s) }
59 fn visit_arm(&mut self, a: &'ast Arm) { walk_arm(self, a) }
60 fn visit_pat(&mut self, p: &'ast Pat) { walk_pat(self, p) }
94b46f34 61 fn visit_anon_const(&mut self, c: &'ast AnonConst) { walk_anon_const(self, c) }
476ff2be
SL
62 fn visit_expr(&mut self, ex: &'ast Expr) { walk_expr(self, ex) }
63 fn visit_expr_post(&mut self, _ex: &'ast Expr) { }
64 fn visit_ty(&mut self, t: &'ast Ty) { walk_ty(self, t) }
8faf50e0
XL
65 fn visit_generic_param(&mut self, param: &'ast GenericParam) {
66 walk_generic_param(self, param)
67 }
476ff2be 68 fn visit_generics(&mut self, g: &'ast Generics) { walk_generics(self, g) }
8bb4bdeb
XL
69 fn visit_where_predicate(&mut self, p: &'ast WherePredicate) {
70 walk_where_predicate(self, p)
71 }
476ff2be
SL
72 fn visit_fn(&mut self, fk: FnKind<'ast>, fd: &'ast FnDecl, s: Span, _: NodeId) {
73 walk_fn(self, fk, fd, s)
74 }
75 fn visit_trait_item(&mut self, ti: &'ast TraitItem) { walk_trait_item(self, ti) }
76 fn visit_impl_item(&mut self, ii: &'ast ImplItem) { walk_impl_item(self, ii) }
77 fn visit_trait_ref(&mut self, t: &'ast TraitRef) { walk_trait_ref(self, t) }
8faf50e0
XL
78 fn visit_param_bound(&mut self, bounds: &'ast GenericBound) {
79 walk_param_bound(self, bounds)
1a4d82fc 80 }
476ff2be 81 fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
1a4d82fc
JJ
82 walk_poly_trait_ref(self, t, m)
83 }
476ff2be
SL
84 fn visit_variant_data(&mut self, s: &'ast VariantData, _: Ident,
85 _: &'ast Generics, _: NodeId, _: Span) {
1a4d82fc
JJ
86 walk_struct_def(self, s)
87 }
476ff2be
SL
88 fn visit_struct_field(&mut self, s: &'ast StructField) { walk_struct_field(self, s) }
89 fn visit_enum_def(&mut self, enum_definition: &'ast EnumDef,
90 generics: &'ast Generics, item_id: NodeId, _: Span) {
b039eaaf 91 walk_enum_def(self, enum_definition, generics, item_id)
c1a9b12d 92 }
476ff2be 93 fn visit_variant(&mut self, v: &'ast Variant, g: &'ast Generics, item_id: NodeId) {
b039eaaf 94 walk_variant(self, v, g, item_id)
1a4d82fc 95 }
2c00a5a8
XL
96 fn visit_label(&mut self, label: &'ast Label) {
97 walk_label(self, label)
98 }
476ff2be 99 fn visit_lifetime(&mut self, lifetime: &'ast Lifetime) {
b039eaaf 100 walk_lifetime(self, lifetime)
1a4d82fc 101 }
476ff2be 102 fn visit_mac(&mut self, _mac: &'ast Mac) {
1a4d82fc 103 panic!("visit_mac disabled by default");
0731742a 104 // N.B., see note about macros above.
1a4d82fc
JJ
105 // if you really want a visitor that
106 // works on macros, use this
107 // definition in your trait impl:
108 // visit::walk_mac(self, _mac)
109 }
7cac9316
XL
110 fn visit_mac_def(&mut self, _mac: &'ast MacroDef, _id: NodeId) {
111 // Nothing to do
112 }
476ff2be 113 fn visit_path(&mut self, path: &'ast Path, _id: NodeId) {
1a4d82fc
JJ
114 walk_path(self, path)
115 }
ff7c6d11
XL
116 fn visit_use_tree(&mut self, use_tree: &'ast UseTree, id: NodeId, _nested: bool) {
117 walk_use_tree(self, use_tree, id)
b039eaaf 118 }
476ff2be 119 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'ast PathSegment) {
1a4d82fc
JJ
120 walk_path_segment(self, path_span, path_segment)
121 }
8faf50e0
XL
122 fn visit_generic_args(&mut self, path_span: Span, generic_args: &'ast GenericArgs) {
123 walk_generic_args(self, path_span, generic_args)
124 }
125 fn visit_generic_arg(&mut self, generic_arg: &'ast GenericArg) {
126 match generic_arg {
127 GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
128 GenericArg::Type(ty) => self.visit_ty(ty),
9fa01778 129 GenericArg::Const(ct) => self.visit_anon_const(ct),
8faf50e0 130 }
1a4d82fc 131 }
476ff2be 132 fn visit_assoc_type_binding(&mut self, type_binding: &'ast TypeBinding) {
1a4d82fc
JJ
133 walk_assoc_type_binding(self, type_binding)
134 }
abe05a73
XL
135 fn visit_attribute(&mut self, attr: &'ast Attribute) {
136 walk_attribute(self, attr)
137 }
138 fn visit_tt(&mut self, tt: TokenTree) {
139 walk_tt(self, tt)
140 }
141 fn visit_tts(&mut self, tts: TokenStream) {
142 walk_tts(self, tts)
143 }
144 fn visit_token(&mut self, _t: Token) {}
145 // FIXME: add `visit_interpolated` and `walk_interpolated`
476ff2be 146 fn visit_vis(&mut self, vis: &'ast Visibility) {
54a0048b
SL
147 walk_vis(self, vis)
148 }
476ff2be 149 fn visit_fn_ret_ty(&mut self, ret_ty: &'ast FunctionRetTy) {
5bcae85e
SL
150 walk_fn_ret_ty(self, ret_ty)
151 }
532ac7d7
XL
152 fn visit_fn_header(&mut self, _header: &'ast FnHeader) {
153 // Nothing to do
154 }
223e47cc
LB
155}
156
b039eaaf
SL
157#[macro_export]
158macro_rules! walk_list {
159 ($visitor: expr, $method: ident, $list: expr) => {
160 for elem in $list {
161 $visitor.$method(elem)
162 }
163 };
164 ($visitor: expr, $method: ident, $list: expr, $($extra_args: expr),*) => {
165 for elem in $list {
166 $visitor.$method(elem, $($extra_args,)*)
167 }
1a4d82fc
JJ
168 }
169}
170
83c7162d
XL
171pub fn walk_ident<'a, V: Visitor<'a>>(visitor: &mut V, ident: Ident) {
172 visitor.visit_name(ident.span, ident.name);
b039eaaf
SL
173}
174
476ff2be 175pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) {
7cac9316 176 visitor.visit_mod(&krate.module, krate.span, &krate.attrs, CRATE_NODE_ID);
b039eaaf 177 walk_list!(visitor, visit_attribute, &krate.attrs);
1a4d82fc
JJ
178}
179
476ff2be 180pub fn walk_mod<'a, V: Visitor<'a>>(visitor: &mut V, module: &'a Mod) {
b039eaaf 181 walk_list!(visitor, visit_item, &module.items);
1a4d82fc
JJ
182}
183
476ff2be 184pub fn walk_local<'a, V: Visitor<'a>>(visitor: &mut V, local: &'a Local) {
3157f602
XL
185 for attr in local.attrs.iter() {
186 visitor.visit_attribute(attr);
187 }
b039eaaf
SL
188 visitor.visit_pat(&local.pat);
189 walk_list!(visitor, visit_ty, &local.ty);
190 walk_list!(visitor, visit_expr, &local.init);
191}
192
2c00a5a8 193pub fn walk_label<'a, V: Visitor<'a>>(visitor: &mut V, label: &'a Label) {
83c7162d 194 visitor.visit_ident(label.ident);
2c00a5a8
XL
195}
196
476ff2be 197pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime) {
83c7162d 198 visitor.visit_ident(lifetime.ident);
b039eaaf
SL
199}
200
476ff2be
SL
201pub fn walk_poly_trait_ref<'a, V>(visitor: &mut V,
202 trait_ref: &'a PolyTraitRef,
203 _: &TraitBoundModifier)
204 where V: Visitor<'a>,
1a4d82fc 205{
ff7c6d11 206 walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
1a4d82fc
JJ
207 visitor.visit_trait_ref(&trait_ref.trait_ref);
208}
209
476ff2be 210pub fn walk_trait_ref<'a, V: Visitor<'a>>(visitor: &mut V, trait_ref: &'a TraitRef) {
1a4d82fc
JJ
211 visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
212}
213
476ff2be 214pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
a7813a04 215 visitor.visit_vis(&item.vis);
83c7162d 216 visitor.visit_ident(item.ident);
1a4d82fc 217 match item.node {
0531ce1d
XL
218 ItemKind::ExternCrate(orig_name) => {
219 if let Some(orig_name) = orig_name {
220 visitor.visit_name(item.span, orig_name);
2c00a5a8 221 }
b039eaaf 222 }
ff7c6d11
XL
223 ItemKind::Use(ref use_tree) => {
224 visitor.visit_use_tree(use_tree, item.id, false)
85aaf69f 225 }
7453a54e
SL
226 ItemKind::Static(ref typ, _, ref expr) |
227 ItemKind::Const(ref typ, ref expr) => {
b039eaaf
SL
228 visitor.visit_ty(typ);
229 visitor.visit_expr(expr);
1a4d82fc 230 }
532ac7d7 231 ItemKind::Fn(ref declaration, ref header, ref generics, ref body) => {
abe05a73 232 visitor.visit_generics(generics);
532ac7d7 233 visitor.visit_fn_header(header);
8faf50e0
XL
234 visitor.visit_fn(FnKind::ItemFn(item.ident, header,
235 &item.vis, body),
b039eaaf 236 declaration,
1a4d82fc
JJ
237 item.span,
238 item.id)
239 }
7453a54e 240 ItemKind::Mod(ref module) => {
7cac9316 241 visitor.visit_mod(module, item.span, &item.attrs, item.id)
1a4d82fc 242 }
7453a54e 243 ItemKind::ForeignMod(ref foreign_module) => {
b039eaaf 244 walk_list!(visitor, visit_foreign_item, &foreign_module.items);
223e47cc 245 }
cc61c64b 246 ItemKind::GlobalAsm(ref ga) => visitor.visit_global_asm(ga),
532ac7d7 247 ItemKind::Ty(ref typ, ref generics) => {
b039eaaf 248 visitor.visit_ty(typ);
532ac7d7 249 visitor.visit_generics(generics)
1a4d82fc 250 }
532ac7d7 251 ItemKind::Existential(ref bounds, ref generics) => {
8faf50e0 252 walk_list!(visitor, visit_param_bound, bounds);
532ac7d7 253 visitor.visit_generics(generics)
8faf50e0 254 }
532ac7d7
XL
255 ItemKind::Enum(ref enum_definition, ref generics) => {
256 visitor.visit_generics(generics);
257 visitor.visit_enum_def(enum_definition, generics, item.id, item.span)
1a4d82fc 258 }
7cac9316 259 ItemKind::Impl(_, _, _,
532ac7d7 260 ref generics,
b039eaaf 261 ref opt_trait_reference,
1a4d82fc
JJ
262 ref typ,
263 ref impl_items) => {
532ac7d7 264 visitor.visit_generics(generics);
b039eaaf
SL
265 walk_list!(visitor, visit_trait_ref, opt_trait_reference);
266 visitor.visit_ty(typ);
267 walk_list!(visitor, visit_impl_item, impl_items);
970d7e83 268 }
9e0c209e
SL
269 ItemKind::Struct(ref struct_definition, ref generics) |
270 ItemKind::Union(ref struct_definition, ref generics) => {
1a4d82fc 271 visitor.visit_generics(generics);
b039eaaf
SL
272 visitor.visit_variant_data(struct_definition, item.ident,
273 generics, item.id, item.span);
1a4d82fc 274 }
abe05a73 275 ItemKind::Trait(.., ref generics, ref bounds, ref methods) => {
1a4d82fc 276 visitor.visit_generics(generics);
8faf50e0 277 walk_list!(visitor, visit_param_bound, bounds);
b039eaaf 278 walk_list!(visitor, visit_trait_item, methods);
1a4d82fc 279 }
ff7c6d11
XL
280 ItemKind::TraitAlias(ref generics, ref bounds) => {
281 visitor.visit_generics(generics);
8faf50e0 282 walk_list!(visitor, visit_param_bound, bounds);
ff7c6d11 283 }
7453a54e 284 ItemKind::Mac(ref mac) => visitor.visit_mac(mac),
7cac9316 285 ItemKind::MacroDef(ref ts) => visitor.visit_mac_def(ts, item.id),
1a4d82fc 286 }
b039eaaf 287 walk_list!(visitor, visit_attribute, &item.attrs);
223e47cc
LB
288}
289
476ff2be
SL
290pub fn walk_enum_def<'a, V: Visitor<'a>>(visitor: &mut V,
291 enum_definition: &'a EnumDef,
292 generics: &'a Generics,
3157f602 293 item_id: NodeId) {
b039eaaf 294 walk_list!(visitor, visit_variant, &enum_definition.variants, generics, item_id);
1a4d82fc 295}
223e47cc 296
476ff2be
SL
297pub fn walk_variant<'a, V>(visitor: &mut V,
298 variant: &'a Variant,
299 generics: &'a Generics,
300 item_id: NodeId)
301 where V: Visitor<'a>,
3157f602 302{
83c7162d
XL
303 visitor.visit_ident(variant.node.ident);
304 visitor.visit_variant_data(&variant.node.data, variant.node.ident,
b039eaaf 305 generics, item_id, variant.span);
94b46f34 306 walk_list!(visitor, visit_anon_const, &variant.node.disr_expr);
b039eaaf 307 walk_list!(visitor, visit_attribute, &variant.node.attrs);
1a4d82fc
JJ
308}
309
476ff2be 310pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
1a4d82fc 311 match typ.node {
c30ab7b3 312 TyKind::Slice(ref ty) | TyKind::Paren(ref ty) => {
b039eaaf 313 visitor.visit_ty(ty)
1a4d82fc 314 }
7453a54e 315 TyKind::Ptr(ref mutable_type) => {
b039eaaf 316 visitor.visit_ty(&mutable_type.ty)
1a4d82fc 317 }
7453a54e 318 TyKind::Rptr(ref opt_lifetime, ref mutable_type) => {
b039eaaf
SL
319 walk_list!(visitor, visit_lifetime, opt_lifetime);
320 visitor.visit_ty(&mutable_type.ty)
1a4d82fc 321 }
532ac7d7 322 TyKind::Never | TyKind::CVarArgs => {}
7453a54e 323 TyKind::Tup(ref tuple_element_types) => {
b039eaaf 324 walk_list!(visitor, visit_ty, tuple_element_types);
1a4d82fc 325 }
7453a54e 326 TyKind::BareFn(ref function_declaration) => {
ff7c6d11 327 walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
94b46f34 328 walk_fn_decl(visitor, &function_declaration.decl);
1a4d82fc 329 }
7453a54e 330 TyKind::Path(ref maybe_qself, ref path) => {
c34b1796
AL
331 if let Some(ref qself) = *maybe_qself {
332 visitor.visit_ty(&qself.ty);
333 }
334 visitor.visit_path(path, typ.id);
1a4d82fc 335 }
94b46f34 336 TyKind::Array(ref ty, ref length) => {
b039eaaf 337 visitor.visit_ty(ty);
94b46f34 338 visitor.visit_anon_const(length)
1a4d82fc 339 }
abe05a73 340 TyKind::TraitObject(ref bounds, ..) |
8faf50e0
XL
341 TyKind::ImplTrait(_, ref bounds) => {
342 walk_list!(visitor, visit_param_bound, bounds);
5bcae85e 343 }
7453a54e 344 TyKind::Typeof(ref expression) => {
94b46f34 345 visitor.visit_anon_const(expression)
1a4d82fc 346 }
cc61c64b 347 TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
7453a54e 348 TyKind::Mac(ref mac) => {
e9174d1e
SL
349 visitor.visit_mac(mac)
350 }
1a4d82fc
JJ
351 }
352}
353
476ff2be 354pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) {
85aaf69f 355 for segment in &path.segments {
1a4d82fc
JJ
356 visitor.visit_path_segment(path.span, segment);
357 }
358}
359
ff7c6d11
XL
360pub fn walk_use_tree<'a, V: Visitor<'a>>(
361 visitor: &mut V, use_tree: &'a UseTree, id: NodeId,
362) {
363 visitor.visit_path(&use_tree.prefix, id);
ff7c6d11 364 match use_tree.kind {
94b46f34
XL
365 UseTreeKind::Simple(rename, ..) => {
366 // the extra IDs are handled during HIR lowering
0531ce1d 367 if let Some(rename) = rename {
83c7162d 368 visitor.visit_ident(rename);
0531ce1d 369 }
ff7c6d11
XL
370 }
371 UseTreeKind::Glob => {},
372 UseTreeKind::Nested(ref use_trees) => {
373 for &(ref nested_tree, nested_id) in use_trees {
374 visitor.visit_use_tree(nested_tree, nested_id, true);
375 }
376 }
377 }
b039eaaf
SL
378}
379
476ff2be
SL
380pub fn walk_path_segment<'a, V: Visitor<'a>>(visitor: &mut V,
381 path_span: Span,
382 segment: &'a PathSegment) {
83c7162d 383 visitor.visit_ident(segment.ident);
8faf50e0
XL
384 if let Some(ref args) = segment.args {
385 visitor.visit_generic_args(path_span, args);
32a655c1 386 }
1a4d82fc
JJ
387}
388
8faf50e0
XL
389pub fn walk_generic_args<'a, V>(visitor: &mut V,
390 _path_span: Span,
391 generic_args: &'a GenericArgs)
476ff2be 392 where V: Visitor<'a>,
3157f602 393{
8faf50e0
XL
394 match *generic_args {
395 GenericArgs::AngleBracketed(ref data) => {
396 walk_list!(visitor, visit_generic_arg, &data.args);
b039eaaf 397 walk_list!(visitor, visit_assoc_type_binding, &data.bindings);
1a4d82fc 398 }
8faf50e0 399 GenericArgs::Parenthesized(ref data) => {
b039eaaf
SL
400 walk_list!(visitor, visit_ty, &data.inputs);
401 walk_list!(visitor, visit_ty, &data.output);
1a4d82fc
JJ
402 }
403 }
404}
405
476ff2be
SL
406pub fn walk_assoc_type_binding<'a, V: Visitor<'a>>(visitor: &mut V,
407 type_binding: &'a TypeBinding) {
83c7162d 408 visitor.visit_ident(type_binding.ident);
b039eaaf 409 visitor.visit_ty(&type_binding.ty);
1a4d82fc
JJ
410}
411
476ff2be 412pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) {
1a4d82fc 413 match pattern.node {
3157f602 414 PatKind::TupleStruct(ref path, ref children, _) => {
7453a54e 415 visitor.visit_path(path, pattern.id);
3157f602 416 walk_list!(visitor, visit_pat, children);
7453a54e 417 }
3157f602
XL
418 PatKind::Path(ref opt_qself, ref path) => {
419 if let Some(ref qself) = *opt_qself {
420 visitor.visit_ty(&qself.ty);
421 }
d9579d0f
AL
422 visitor.visit_path(path, pattern.id)
423 }
7453a54e 424 PatKind::Struct(ref path, ref fields, _) => {
1a4d82fc 425 visitor.visit_path(path, pattern.id);
85aaf69f 426 for field in fields {
32a655c1 427 walk_list!(visitor, visit_attribute, field.node.attrs.iter());
83c7162d 428 visitor.visit_ident(field.node.ident);
b039eaaf 429 visitor.visit_pat(&field.node.pat)
223e47cc
LB
430 }
431 }
3157f602 432 PatKind::Tuple(ref tuple_elements, _) => {
b039eaaf 433 walk_list!(visitor, visit_pat, tuple_elements);
1a4d82fc 434 }
7453a54e 435 PatKind::Box(ref subpattern) |
0531ce1d
XL
436 PatKind::Ref(ref subpattern, _) |
437 PatKind::Paren(ref subpattern) => {
b039eaaf 438 visitor.visit_pat(subpattern)
1a4d82fc 439 }
83c7162d
XL
440 PatKind::Ident(_, ident, ref optional_subpattern) => {
441 visitor.visit_ident(ident);
b039eaaf 442 walk_list!(visitor, visit_pat, optional_subpattern);
223e47cc 443 }
7453a54e 444 PatKind::Lit(ref expression) => visitor.visit_expr(expression),
32a655c1 445 PatKind::Range(ref lower_bound, ref upper_bound, _) => {
b039eaaf 446 visitor.visit_expr(lower_bound);
32a655c1 447 visitor.visit_expr(upper_bound);
223e47cc 448 }
7453a54e 449 PatKind::Wild => (),
c30ab7b3 450 PatKind::Slice(ref prepatterns, ref slice_pattern, ref postpatterns) => {
b039eaaf
SL
451 walk_list!(visitor, visit_pat, prepatterns);
452 walk_list!(visitor, visit_pat, slice_pattern);
453 walk_list!(visitor, visit_pat, postpatterns);
223e47cc 454 }
7453a54e 455 PatKind::Mac(ref mac) => visitor.visit_mac(mac),
1a4d82fc
JJ
456 }
457}
458
476ff2be 459pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, foreign_item: &'a ForeignItem) {
a7813a04 460 visitor.visit_vis(&foreign_item.vis);
83c7162d 461 visitor.visit_ident(foreign_item.ident);
1a4d82fc
JJ
462
463 match foreign_item.node {
7453a54e 464 ForeignItemKind::Fn(ref function_declaration, ref generics) => {
b039eaaf 465 walk_fn_decl(visitor, function_declaration);
1a4d82fc
JJ
466 visitor.visit_generics(generics)
467 }
7453a54e 468 ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
abe05a73 469 ForeignItemKind::Ty => (),
83c7162d 470 ForeignItemKind::Macro(ref mac) => visitor.visit_mac(mac),
223e47cc 471 }
223e47cc 472
b039eaaf 473 walk_list!(visitor, visit_attribute, &foreign_item.attrs);
1a4d82fc
JJ
474}
475
cc61c64b
XL
476pub fn walk_global_asm<'a, V: Visitor<'a>>(_: &mut V, _: &'a GlobalAsm) {
477 // Empty!
478}
479
8faf50e0 480pub fn walk_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a GenericBound) {
1a4d82fc 481 match *bound {
8faf50e0
XL
482 GenericBound::Trait(ref typ, ref modifier) => visitor.visit_poly_trait_ref(typ, modifier),
483 GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
223e47cc
LB
484 }
485}
486
ff7c6d11 487pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a GenericParam) {
8faf50e0
XL
488 visitor.visit_ident(param.ident);
489 walk_list!(visitor, visit_attribute, param.attrs.iter());
490 walk_list!(visitor, visit_param_bound, &param.bounds);
491 match param.kind {
492 GenericParamKind::Lifetime => {}
493 GenericParamKind::Type { ref default } => walk_list!(visitor, visit_ty, default),
9fa01778 494 GenericParamKind::Const { ref ty, .. } => visitor.visit_ty(ty),
1a4d82fc 495 }
ff7c6d11
XL
496}
497
498pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) {
499 walk_list!(visitor, visit_generic_param, &generics.params);
8bb4bdeb
XL
500 walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
501}
502
503pub fn walk_where_predicate<'a, V: Visitor<'a>>(visitor: &mut V, predicate: &'a WherePredicate) {
504 match *predicate {
505 WherePredicate::BoundPredicate(WhereBoundPredicate{ref bounded_ty,
506 ref bounds,
ff7c6d11 507 ref bound_generic_params,
8bb4bdeb
XL
508 ..}) => {
509 visitor.visit_ty(bounded_ty);
8faf50e0 510 walk_list!(visitor, visit_param_bound, bounds);
ff7c6d11 511 walk_list!(visitor, visit_generic_param, bound_generic_params);
8bb4bdeb
XL
512 }
513 WherePredicate::RegionPredicate(WhereRegionPredicate{ref lifetime,
514 ref bounds,
515 ..}) => {
516 visitor.visit_lifetime(lifetime);
8faf50e0 517 walk_list!(visitor, visit_param_bound, bounds);
8bb4bdeb
XL
518 }
519 WherePredicate::EqPredicate(WhereEqPredicate{ref lhs_ty,
520 ref rhs_ty,
521 ..}) => {
522 visitor.visit_ty(lhs_ty);
523 visitor.visit_ty(rhs_ty);
223e47cc
LB
524 }
525 }
526}
527
476ff2be 528pub fn walk_fn_ret_ty<'a, V: Visitor<'a>>(visitor: &mut V, ret_ty: &'a FunctionRetTy) {
7453a54e 529 if let FunctionRetTy::Ty(ref output_ty) = *ret_ty {
b039eaaf 530 visitor.visit_ty(output_ty)
223e47cc
LB
531 }
532}
533
476ff2be 534pub fn walk_fn_decl<'a, V: Visitor<'a>>(visitor: &mut V, function_declaration: &'a FnDecl) {
85aaf69f 535 for argument in &function_declaration.inputs {
b039eaaf
SL
536 visitor.visit_pat(&argument.pat);
537 visitor.visit_ty(&argument.ty)
223e47cc 538 }
5bcae85e 539 visitor.visit_fn_ret_ty(&function_declaration.output)
223e47cc
LB
540}
541
476ff2be
SL
542pub fn walk_fn<'a, V>(visitor: &mut V, kind: FnKind<'a>, declaration: &'a FnDecl, _span: Span)
543 where V: Visitor<'a>,
544{
545 match kind {
532ac7d7
XL
546 FnKind::ItemFn(_, header, _, body) => {
547 visitor.visit_fn_header(header);
476ff2be
SL
548 walk_fn_decl(visitor, declaration);
549 visitor.visit_block(body);
1a4d82fc 550 }
532ac7d7
XL
551 FnKind::Method(_, sig, _, body) => {
552 visitor.visit_fn_header(&sig.header);
476ff2be
SL
553 walk_fn_decl(visitor, declaration);
554 visitor.visit_block(body);
555 }
556 FnKind::Closure(body) => {
557 walk_fn_decl(visitor, declaration);
558 visitor.visit_expr(body);
1a4d82fc 559 }
1a4d82fc 560 }
b039eaaf 561}
1a4d82fc 562
476ff2be 563pub fn walk_trait_item<'a, V: Visitor<'a>>(visitor: &mut V, trait_item: &'a TraitItem) {
83c7162d 564 visitor.visit_ident(trait_item.ident);
b039eaaf 565 walk_list!(visitor, visit_attribute, &trait_item.attrs);
abe05a73 566 visitor.visit_generics(&trait_item.generics);
c34b1796 567 match trait_item.node {
7453a54e 568 TraitItemKind::Const(ref ty, ref default) => {
d9579d0f 569 visitor.visit_ty(ty);
b039eaaf 570 walk_list!(visitor, visit_expr, default);
d9579d0f 571 }
7453a54e 572 TraitItemKind::Method(ref sig, None) => {
532ac7d7 573 visitor.visit_fn_header(&sig.header);
c34b1796
AL
574 walk_fn_decl(visitor, &sig.decl);
575 }
7453a54e 576 TraitItemKind::Method(ref sig, Some(ref body)) => {
476ff2be
SL
577 visitor.visit_fn(FnKind::Method(trait_item.ident, sig, None, body),
578 &sig.decl, trait_item.span, trait_item.id);
c34b1796 579 }
7453a54e 580 TraitItemKind::Type(ref bounds, ref default) => {
8faf50e0 581 walk_list!(visitor, visit_param_bound, bounds);
b039eaaf 582 walk_list!(visitor, visit_ty, default);
c34b1796 583 }
3157f602
XL
584 TraitItemKind::Macro(ref mac) => {
585 visitor.visit_mac(mac);
586 }
c34b1796 587 }
223e47cc
LB
588}
589
476ff2be 590pub fn walk_impl_item<'a, V: Visitor<'a>>(visitor: &mut V, impl_item: &'a ImplItem) {
a7813a04 591 visitor.visit_vis(&impl_item.vis);
83c7162d 592 visitor.visit_ident(impl_item.ident);
b039eaaf 593 walk_list!(visitor, visit_attribute, &impl_item.attrs);
abe05a73 594 visitor.visit_generics(&impl_item.generics);
c34b1796 595 match impl_item.node {
92a42be0 596 ImplItemKind::Const(ref ty, ref expr) => {
d9579d0f
AL
597 visitor.visit_ty(ty);
598 visitor.visit_expr(expr);
599 }
92a42be0 600 ImplItemKind::Method(ref sig, ref body) => {
476ff2be
SL
601 visitor.visit_fn(FnKind::Method(impl_item.ident, sig, Some(&impl_item.vis), body),
602 &sig.decl, impl_item.span, impl_item.id);
c34b1796 603 }
92a42be0 604 ImplItemKind::Type(ref ty) => {
c34b1796
AL
605 visitor.visit_ty(ty);
606 }
8faf50e0
XL
607 ImplItemKind::Existential(ref bounds) => {
608 walk_list!(visitor, visit_param_bound, bounds);
609 }
92a42be0 610 ImplItemKind::Macro(ref mac) => {
c34b1796 611 visitor.visit_mac(mac);
1a4d82fc 612 }
223e47cc
LB
613 }
614}
615
476ff2be 616pub fn walk_struct_def<'a, V: Visitor<'a>>(visitor: &mut V, struct_definition: &'a VariantData) {
b039eaaf 617 walk_list!(visitor, visit_struct_field, struct_definition.fields());
223e47cc
LB
618}
619
476ff2be 620pub fn walk_struct_field<'a, V: Visitor<'a>>(visitor: &mut V, struct_field: &'a StructField) {
a7813a04 621 visitor.visit_vis(&struct_field.vis);
2c00a5a8 622 if let Some(ident) = struct_field.ident {
83c7162d 623 visitor.visit_ident(ident);
2c00a5a8 624 }
54a0048b
SL
625 visitor.visit_ty(&struct_field.ty);
626 walk_list!(visitor, visit_attribute, &struct_field.attrs);
223e47cc
LB
627}
628
476ff2be 629pub fn walk_block<'a, V: Visitor<'a>>(visitor: &mut V, block: &'a Block) {
b039eaaf 630 walk_list!(visitor, visit_stmt, &block.stmts);
223e47cc
LB
631}
632
476ff2be 633pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) {
1a4d82fc 634 match statement.node {
3157f602
XL
635 StmtKind::Local(ref local) => visitor.visit_local(local),
636 StmtKind::Item(ref item) => visitor.visit_item(item),
637 StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
b039eaaf 638 visitor.visit_expr(expression)
1a4d82fc 639 }
3157f602
XL
640 StmtKind::Mac(ref mac) => {
641 let (ref mac, _, ref attrs) = **mac;
92a42be0 642 visitor.visit_mac(mac);
3157f602 643 for attr in attrs.iter() {
92a42be0
SL
644 visitor.visit_attribute(attr);
645 }
646 }
223e47cc
LB
647 }
648}
649
532ac7d7
XL
650pub fn walk_mac<'a, V: Visitor<'a>>(visitor: &mut V, mac: &'a Mac) {
651 visitor.visit_path(&mac.node.path, DUMMY_NODE_ID);
223e47cc
LB
652}
653
94b46f34
XL
654pub fn walk_anon_const<'a, V: Visitor<'a>>(visitor: &mut V, constant: &'a AnonConst) {
655 visitor.visit_expr(&constant.value);
656}
657
476ff2be 658pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
3157f602
XL
659 for attr in expression.attrs.iter() {
660 visitor.visit_attribute(attr);
661 }
1a4d82fc 662 match expression.node {
7453a54e 663 ExprKind::Box(ref subexpression) => {
b039eaaf
SL
664 visitor.visit_expr(subexpression)
665 }
83c7162d 666 ExprKind::ObsoleteInPlace(ref place, ref subexpression) => {
b039eaaf
SL
667 visitor.visit_expr(place);
668 visitor.visit_expr(subexpression)
223e47cc 669 }
32a655c1 670 ExprKind::Array(ref subexpressions) => {
b039eaaf 671 walk_list!(visitor, visit_expr, subexpressions);
1a4d82fc 672 }
7453a54e 673 ExprKind::Repeat(ref element, ref count) => {
b039eaaf 674 visitor.visit_expr(element);
94b46f34 675 visitor.visit_anon_const(count)
1a4d82fc 676 }
7453a54e 677 ExprKind::Struct(ref path, ref fields, ref optional_base) => {
1a4d82fc 678 visitor.visit_path(path, expression.id);
85aaf69f 679 for field in fields {
32a655c1 680 walk_list!(visitor, visit_attribute, field.attrs.iter());
83c7162d 681 visitor.visit_ident(field.ident);
b039eaaf 682 visitor.visit_expr(&field.expr)
970d7e83 683 }
b039eaaf 684 walk_list!(visitor, visit_expr, optional_base);
223e47cc 685 }
7453a54e 686 ExprKind::Tup(ref subexpressions) => {
b039eaaf 687 walk_list!(visitor, visit_expr, subexpressions);
223e47cc 688 }
7453a54e 689 ExprKind::Call(ref callee_expression, ref arguments) => {
9e0c209e 690 visitor.visit_expr(callee_expression);
b039eaaf 691 walk_list!(visitor, visit_expr, arguments);
1a4d82fc 692 }
041b39d2
XL
693 ExprKind::MethodCall(ref segment, ref arguments) => {
694 visitor.visit_path_segment(expression.span, segment);
9e0c209e 695 walk_list!(visitor, visit_expr, arguments);
223e47cc 696 }
7453a54e 697 ExprKind::Binary(_, ref left_expression, ref right_expression) => {
b039eaaf
SL
698 visitor.visit_expr(left_expression);
699 visitor.visit_expr(right_expression)
1a4d82fc 700 }
7453a54e 701 ExprKind::AddrOf(_, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
b039eaaf 702 visitor.visit_expr(subexpression)
1a4d82fc 703 }
7453a54e
SL
704 ExprKind::Lit(_) => {}
705 ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
b039eaaf
SL
706 visitor.visit_expr(subexpression);
707 visitor.visit_ty(typ)
1a4d82fc 708 }
7453a54e 709 ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
b039eaaf
SL
710 visitor.visit_expr(head_expression);
711 visitor.visit_block(if_block);
712 walk_list!(visitor, visit_expr, optional_else);
1a4d82fc 713 }
2c00a5a8
XL
714 ExprKind::While(ref subexpression, ref block, ref opt_label) => {
715 walk_list!(visitor, visit_label, opt_label);
b039eaaf
SL
716 visitor.visit_expr(subexpression);
717 visitor.visit_block(block);
1a4d82fc 718 }
0531ce1d
XL
719 ExprKind::IfLet(ref pats, ref subexpression, ref if_block, ref optional_else) => {
720 walk_list!(visitor, visit_pat, pats);
b039eaaf
SL
721 visitor.visit_expr(subexpression);
722 visitor.visit_block(if_block);
723 walk_list!(visitor, visit_expr, optional_else);
724 }
0531ce1d 725 ExprKind::WhileLet(ref pats, ref subexpression, ref block, ref opt_label) => {
2c00a5a8 726 walk_list!(visitor, visit_label, opt_label);
0531ce1d 727 walk_list!(visitor, visit_pat, pats);
b039eaaf
SL
728 visitor.visit_expr(subexpression);
729 visitor.visit_block(block);
b039eaaf 730 }
2c00a5a8
XL
731 ExprKind::ForLoop(ref pattern, ref subexpression, ref block, ref opt_label) => {
732 walk_list!(visitor, visit_label, opt_label);
b039eaaf
SL
733 visitor.visit_pat(pattern);
734 visitor.visit_expr(subexpression);
735 visitor.visit_block(block);
b039eaaf 736 }
2c00a5a8
XL
737 ExprKind::Loop(ref block, ref opt_label) => {
738 walk_list!(visitor, visit_label, opt_label);
b039eaaf 739 visitor.visit_block(block);
b039eaaf 740 }
7453a54e 741 ExprKind::Match(ref subexpression, ref arms) => {
b039eaaf
SL
742 visitor.visit_expr(subexpression);
743 walk_list!(visitor, visit_arm, arms);
223e47cc 744 }
8faf50e0 745 ExprKind::Closure(_, _, _, ref function_declaration, ref body, _decl_span) => {
476ff2be 746 visitor.visit_fn(FnKind::Closure(body),
b039eaaf 747 function_declaration,
1a4d82fc
JJ
748 expression.span,
749 expression.id)
750 }
94b46f34
XL
751 ExprKind::Block(ref block, ref opt_label) => {
752 walk_list!(visitor, visit_label, opt_label);
753 visitor.visit_block(block);
754 }
8faf50e0
XL
755 ExprKind::Async(_, _, ref body) => {
756 visitor.visit_block(body);
757 }
7453a54e 758 ExprKind::Assign(ref left_hand_expression, ref right_hand_expression) => {
9e0c209e 759 visitor.visit_expr(left_hand_expression);
b039eaaf 760 visitor.visit_expr(right_hand_expression);
1a4d82fc 761 }
7453a54e 762 ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
9e0c209e 763 visitor.visit_expr(left_expression);
b039eaaf 764 visitor.visit_expr(right_expression);
223e47cc 765 }
83c7162d 766 ExprKind::Field(ref subexpression, ident) => {
b039eaaf 767 visitor.visit_expr(subexpression);
83c7162d 768 visitor.visit_ident(ident);
223e47cc 769 }
7453a54e 770 ExprKind::Index(ref main_expression, ref index_expression) => {
b039eaaf
SL
771 visitor.visit_expr(main_expression);
772 visitor.visit_expr(index_expression)
223e47cc 773 }
54a0048b 774 ExprKind::Range(ref start, ref end, _) => {
b039eaaf
SL
775 walk_list!(visitor, visit_expr, start);
776 walk_list!(visitor, visit_expr, end);
223e47cc 777 }
7453a54e 778 ExprKind::Path(ref maybe_qself, ref path) => {
c34b1796
AL
779 if let Some(ref qself) = *maybe_qself {
780 visitor.visit_ty(&qself.ty);
781 }
1a4d82fc 782 visitor.visit_path(path, expression.id)
223e47cc 783 }
2c00a5a8
XL
784 ExprKind::Break(ref opt_label, ref opt_expr) => {
785 walk_list!(visitor, visit_label, opt_label);
476ff2be
SL
786 walk_list!(visitor, visit_expr, opt_expr);
787 }
2c00a5a8
XL
788 ExprKind::Continue(ref opt_label) => {
789 walk_list!(visitor, visit_label, opt_label);
b039eaaf 790 }
7453a54e 791 ExprKind::Ret(ref optional_expression) => {
b039eaaf 792 walk_list!(visitor, visit_expr, optional_expression);
1a4d82fc 793 }
7453a54e
SL
794 ExprKind::Mac(ref mac) => visitor.visit_mac(mac),
795 ExprKind::Paren(ref subexpression) => {
b039eaaf 796 visitor.visit_expr(subexpression)
1a4d82fc 797 }
7453a54e 798 ExprKind::InlineAsm(ref ia) => {
b039eaaf 799 for &(_, ref input) in &ia.inputs {
7cac9316 800 visitor.visit_expr(input)
1a4d82fc 801 }
9cc50fc6
SL
802 for output in &ia.outputs {
803 visitor.visit_expr(&output.expr)
1a4d82fc
JJ
804 }
805 }
ea8adc8c
XL
806 ExprKind::Yield(ref optional_expression) => {
807 walk_list!(visitor, visit_expr, optional_expression);
808 }
54a0048b
SL
809 ExprKind::Try(ref subexpression) => {
810 visitor.visit_expr(subexpression)
811 }
b7449926 812 ExprKind::TryBlock(ref body) => {
cc61c64b
XL
813 visitor.visit_block(body)
814 }
0731742a 815 ExprKind::Err => {}
1a4d82fc
JJ
816 }
817
818 visitor.visit_expr_post(expression)
819}
820
476ff2be 821pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
b039eaaf 822 walk_list!(visitor, visit_pat, &arm.pats);
b7449926
XL
823 if let Some(ref g) = &arm.guard {
824 match g {
825 Guard::If(ref e) => visitor.visit_expr(e),
826 }
827 }
b039eaaf
SL
828 visitor.visit_expr(&arm.body);
829 walk_list!(visitor, visit_attribute, &arm.attrs);
223e47cc 830}
54a0048b 831
476ff2be 832pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
0531ce1d 833 if let VisibilityKind::Restricted { ref path, id } = vis.node {
a7813a04 834 visitor.visit_path(path, id);
54a0048b
SL
835 }
836}
abe05a73
XL
837
838pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) {
839 visitor.visit_tts(attr.tokens.clone());
840}
841
842pub fn walk_tt<'a, V: Visitor<'a>>(visitor: &mut V, tt: TokenTree) {
843 match tt {
844 TokenTree::Token(_, tok) => visitor.visit_token(tok),
9fa01778 845 TokenTree::Delimited(_, _, tts) => visitor.visit_tts(tts),
abe05a73
XL
846 }
847}
848
849pub fn walk_tts<'a, V: Visitor<'a>>(visitor: &mut V, tts: TokenStream) {
850 for tt in tts.trees() {
851 visitor.visit_tt(tt);
852 }
853}