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