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