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); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 150 | walk_list!(visitor, visit_lifetime, lifetime_def.bounds.items()); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 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 | { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 156 | if let Some(ref bl) = trait_ref.bound_lifetimes { |
| 157 | walk_list!(visitor, visit_lifetime_def, bl.lifetimes.items()); |
| 158 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 159 | visitor.visit_path(&trait_ref.trait_ref); |
| 160 | } |
| 161 | |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame] | 162 | pub fn walk_derive_input<V: Visitor>(visitor: &mut V, derive_input: &DeriveInput) { |
| 163 | visitor.visit_ident(&derive_input.ident); |
| 164 | visitor.visit_generics(&derive_input.generics); |
| 165 | match derive_input.body { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 166 | Body::Enum(ref data) => { |
| 167 | walk_list!(visitor, visit_variant, |
| 168 | data.variants.items(), |
| 169 | &derive_input.generics); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 170 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 171 | Body::Struct(ref data) => { |
| 172 | visitor.visit_variant_data(&data.data, &derive_input.ident, &derive_input.generics); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame] | 175 | walk_list!(visitor, visit_attribute, &derive_input.attrs); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | pub fn walk_variant<V>(visitor: &mut V, variant: &Variant, generics: &Generics) |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 179 | where V: Visitor |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 180 | { |
| 181 | visitor.visit_ident(&variant.ident); |
| 182 | visitor.visit_variant_data(&variant.data, &variant.ident, generics); |
| 183 | walk_list!(visitor, visit_attribute, &variant.attrs); |
| 184 | } |
| 185 | |
| 186 | pub fn walk_ty<V: Visitor>(visitor: &mut V, ty: &Ty) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 187 | use ty::*; |
| 188 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 189 | match *ty { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 190 | Ty::Slice(TySlice { ref ty, .. }) | |
| 191 | Ty::Paren(TyParen { ref ty, .. }) => visitor.visit_ty(ty), |
| 192 | Ty::Ptr(TyPtr { ref ty, .. }) => visitor.visit_ty(&ty.ty), |
| 193 | Ty::Rptr(TyRptr { ref lifetime, ref ty, .. }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 194 | walk_list!(visitor, visit_lifetime, lifetime); |
| 195 | visitor.visit_ty(&ty.ty) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 196 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 197 | Ty::Never(_) | Ty::Infer(_) => {} |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 198 | Ty::Tup(TyTup { ref tys, .. }) => { |
| 199 | walk_list!(visitor, visit_ty, tys.items()); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 200 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 201 | Ty::BareFn(TyBareFn { ref ty }) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 202 | if let Some(ref l) = ty.lifetimes { |
| 203 | walk_list!(visitor, visit_lifetime_def, l.lifetimes.items()); |
| 204 | } |
| 205 | for argument in ty.inputs.items() { |
| 206 | if let Some((ref name, _)) = argument.name { |
| 207 | visitor.visit_ident(name); |
| 208 | } |
David Tolnay | 1391b52 | 2016-10-03 21:05:45 -0700 | [diff] [blame] | 209 | visitor.visit_ty(&argument.ty) |
| 210 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 211 | visitor.visit_fn_ret_ty(&ty.output) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 212 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 213 | Ty::Path(TyPath { ref qself, ref path }) => { |
| 214 | if let Some(ref qself) = *qself { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 215 | visitor.visit_ty(&qself.ty); |
| 216 | } |
| 217 | visitor.visit_path(path); |
| 218 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 219 | Ty::Array(TyArray { ref ty, ref amt, .. }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 220 | visitor.visit_ty(ty); |
| 221 | visitor.visit_const_expr(amt); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 222 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 223 | Ty::TraitObject(TyTraitObject { ref bounds, .. }) | |
| 224 | Ty::ImplTrait(TyImplTrait { ref bounds, .. }) => { |
| 225 | walk_list!(visitor, visit_ty_param_bound, bounds.items()); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 226 | } |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 227 | Ty::Mac(ref mac) => { |
David Tolnay | 3f36a0a | 2017-01-23 17:59:46 -0800 | [diff] [blame] | 228 | visitor.visit_mac(mac); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 229 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | pub fn walk_path<V: Visitor>(visitor: &mut V, path: &Path) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 234 | for segment in path.segments.items() { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 235 | visitor.visit_path_segment(segment); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | pub fn walk_path_segment<V: Visitor>(visitor: &mut V, segment: &PathSegment) { |
| 240 | visitor.visit_ident(&segment.ident); |
| 241 | visitor.visit_path_parameters(&segment.parameters); |
| 242 | } |
| 243 | |
| 244 | pub fn walk_path_parameters<V>(visitor: &mut V, path_parameters: &PathParameters) |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 245 | where V: Visitor |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 246 | { |
| 247 | match *path_parameters { |
| 248 | PathParameters::AngleBracketed(ref data) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 249 | walk_list!(visitor, visit_ty, data.types.items()); |
| 250 | walk_list!(visitor, visit_lifetime, data.lifetimes.items()); |
| 251 | walk_list!(visitor, visit_assoc_type_binding, data.bindings.items()); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 252 | } |
| 253 | PathParameters::Parenthesized(ref data) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 254 | walk_list!(visitor, visit_ty, data.inputs.items()); |
| 255 | visitor.visit_fn_ret_ty(&data.output); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | pub fn walk_assoc_type_binding<V: Visitor>(visitor: &mut V, type_binding: &TypeBinding) { |
| 261 | visitor.visit_ident(&type_binding.ident); |
| 262 | visitor.visit_ty(&type_binding.ty); |
| 263 | } |
| 264 | |
| 265 | pub fn walk_ty_param_bound<V: Visitor>(visitor: &mut V, bound: &TyParamBound) { |
| 266 | match *bound { |
| 267 | TyParamBound::Trait(ref ty, ref modifier) => { |
| 268 | visitor.visit_poly_trait_ref(ty, modifier); |
| 269 | } |
| 270 | TyParamBound::Region(ref lifetime) => { |
| 271 | visitor.visit_lifetime(lifetime); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | pub fn walk_generics<V: Visitor>(visitor: &mut V, generics: &Generics) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 277 | for param in generics.ty_params.items() { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 278 | visitor.visit_ident(¶m.ident); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 279 | walk_list!(visitor, visit_ty_param_bound, param.bounds.items()); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 280 | walk_list!(visitor, visit_ty, ¶m.default); |
| 281 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 282 | walk_list!(visitor, visit_lifetime_def, generics.lifetimes.items()); |
| 283 | for predicate in generics.where_clause.predicates.items() { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 284 | match *predicate { |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 285 | WherePredicate::BoundPredicate(WhereBoundPredicate { ref bounded_ty, |
| 286 | ref bounds, |
| 287 | ref bound_lifetimes, |
| 288 | .. }) => { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 289 | visitor.visit_ty(bounded_ty); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 290 | walk_list!(visitor, visit_ty_param_bound, bounds.items()); |
| 291 | if let Some(ref l) = *bound_lifetimes { |
| 292 | walk_list!(visitor, visit_lifetime_def, l.lifetimes.items()); |
| 293 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 294 | } |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 295 | WherePredicate::RegionPredicate(WhereRegionPredicate { ref lifetime, |
| 296 | ref bounds, |
| 297 | .. }) => { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 298 | visitor.visit_lifetime(lifetime); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 299 | walk_list!(visitor, visit_lifetime, bounds.items()); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 300 | } |
David Tolnay | 05120ef | 2017-03-12 18:29:26 -0700 | [diff] [blame] | 301 | WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty, .. }) => { |
David Tolnay | fa23f57 | 2017-01-23 00:19:11 -0800 | [diff] [blame] | 302 | visitor.visit_ty(lhs_ty); |
| 303 | visitor.visit_ty(rhs_ty); |
| 304 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | pub fn walk_fn_ret_ty<V: Visitor>(visitor: &mut V, ret_ty: &FunctionRetTy) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 310 | if let FunctionRetTy::Ty(ref output_ty, _) = *ret_ty { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 311 | visitor.visit_ty(output_ty) |
| 312 | } |
| 313 | } |
| 314 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 315 | pub fn walk_variant_data<V: Visitor>(visitor: &mut V, data: &VariantData) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 316 | let fields = match *data { |
| 317 | VariantData::Struct(ref f, _) | |
| 318 | VariantData::Tuple(ref f, _) => f, |
| 319 | VariantData::Unit => return, |
| 320 | }; |
| 321 | walk_list!(visitor, visit_field, fields.items()); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | pub fn walk_field<V: Visitor>(visitor: &mut V, field: &Field) { |
| 325 | walk_opt_ident(visitor, &field.ident); |
| 326 | visitor.visit_ty(&field.ty); |
| 327 | walk_list!(visitor, visit_attribute, &field.attrs); |
| 328 | } |
David Tolnay | 429168f | 2016-10-05 23:41:04 -0700 | [diff] [blame] | 329 | |
David Tolnay | 68eb4c3 | 2016-10-07 23:30:32 -0700 | [diff] [blame] | 330 | pub fn walk_const_expr<V: Visitor>(visitor: &mut V, len: &ConstExpr) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 331 | use constant::*; |
| 332 | use constant::ConstExpr::*; |
| 333 | |
David Tolnay | 429168f | 2016-10-05 23:41:04 -0700 | [diff] [blame] | 334 | match *len { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 335 | Call(ConstCall { ref func, ref args, .. }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 336 | visitor.visit_const_expr(func); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 337 | walk_list!(visitor, visit_const_expr, args.items()); |
David Tolnay | 68eb4c3 | 2016-10-07 23:30:32 -0700 | [diff] [blame] | 338 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 339 | Binary(ConstBinary { ref left, ref right, .. }) => { |
David Tolnay | 68eb4c3 | 2016-10-07 23:30:32 -0700 | [diff] [blame] | 340 | visitor.visit_const_expr(left); |
| 341 | visitor.visit_const_expr(right); |
| 342 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 343 | Lit(ref lit) => { |
David Tolnay | 68eb4c3 | 2016-10-07 23:30:32 -0700 | [diff] [blame] | 344 | visitor.visit_lit(lit); |
| 345 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 346 | Cast(ConstCast { ref expr, ref ty, .. }) => { |
David Tolnay | 68eb4c3 | 2016-10-07 23:30:32 -0700 | [diff] [blame] | 347 | visitor.visit_const_expr(expr); |
| 348 | visitor.visit_ty(ty); |
| 349 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 350 | Path(ref path) => { |
David Tolnay | 429168f | 2016-10-05 23:41:04 -0700 | [diff] [blame] | 351 | visitor.visit_path(path); |
| 352 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 353 | Index(ConstIndex { ref expr, ref index, .. }) => { |
David Tolnay | 6758875 | 2016-10-30 12:23:10 -0700 | [diff] [blame] | 354 | visitor.visit_const_expr(expr); |
| 355 | visitor.visit_const_expr(index); |
| 356 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 357 | Unary(ConstUnary { ref expr, .. }) | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 358 | Paren(ConstParen { ref expr, .. }) => { |
David Tolnay | e7b0d32 | 2016-10-30 10:27:23 -0700 | [diff] [blame] | 359 | visitor.visit_const_expr(expr); |
| 360 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 361 | Other(ref other) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 362 | #[cfg(feature = "full")] |
| 363 | fn walk_other<V: Visitor>(visitor: &mut V, other: &Expr) { |
| 364 | visitor.visit_expr(other); |
| 365 | } |
| 366 | #[cfg(not(feature = "full"))] |
| 367 | fn walk_other<V: Visitor>(_: &mut V, _: &super::constant::Other) {} |
| 368 | walk_other(visitor, other); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 373 | pub fn walk_mac<V: Visitor>(visitor: &mut V, mac: &Mac) { |
| 374 | visitor.visit_path(&mac.path); |
| 375 | } |
| 376 | |
| 377 | #[cfg(feature = "full")] |
| 378 | pub fn walk_crate<V: Visitor>(visitor: &mut V, _crate: &Crate) { |
| 379 | walk_list!(visitor, visit_attribute, &_crate.attrs); |
| 380 | walk_list!(visitor, visit_item, &_crate.items); |
| 381 | } |
| 382 | |
| 383 | #[cfg(feature = "full")] |
| 384 | pub fn walk_item<V: Visitor>(visitor: &mut V, item: &Item) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 385 | use item::*; |
| 386 | |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 387 | visitor.visit_ident(&item.ident); |
| 388 | walk_list!(visitor, visit_attribute, &item.attrs); |
| 389 | match item.node { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 390 | ItemKind::ExternCrate(ItemExternCrate { ref original, .. }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 391 | walk_opt_ident(visitor, original); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 392 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 393 | ItemKind::Use(ItemUse { ref path, .. }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 394 | visitor.visit_view_path(path); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 395 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 396 | ItemKind::Static(ItemStatic { ref ty, ref expr, .. }) | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 397 | ItemKind::Const(ItemConst { ref ty, ref expr, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 398 | visitor.visit_ty(ty); |
| 399 | visitor.visit_expr(expr); |
| 400 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 401 | ItemKind::Fn(ItemFn { ref decl, ref block, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 402 | visitor.visit_fn_decl(decl); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 403 | walk_list!(visitor, visit_stmt, &block.stmts); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 404 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 405 | ItemKind::Mod(ItemMod { ref items, .. }) => { |
| 406 | if let Some((ref items, _)) = *items { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 407 | walk_list!(visitor, visit_item, items); |
| 408 | } |
| 409 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 410 | ItemKind::ForeignMod(ItemForeignMod { ref items, .. }) => { |
| 411 | walk_list!(visitor, visit_foreign_item, items); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 412 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 413 | ItemKind::Ty(ItemTy { ref ty, ref generics, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 414 | visitor.visit_ty(ty); |
| 415 | visitor.visit_generics(generics); |
| 416 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 417 | ItemKind::Enum(ItemEnum { ref variants, ref generics, ..}) => { |
| 418 | walk_list!(visitor, visit_variant, variants.items(), generics); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 419 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 420 | ItemKind::Struct(ItemStruct { ref data, ref generics, .. }) | |
| 421 | ItemKind::Union(ItemUnion { ref data, ref generics, .. }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 422 | visitor.visit_variant_data(data, &item.ident, generics); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 423 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 424 | ItemKind::Trait(ItemTrait { |
| 425 | ref generics, |
| 426 | ref supertraits, |
| 427 | ref items, |
| 428 | .. |
| 429 | }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 430 | visitor.visit_generics(generics); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 431 | walk_list!(visitor, visit_ty_param_bound, supertraits.items()); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 432 | walk_list!(visitor, visit_trait_item, items); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 433 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 434 | ItemKind::DefaultImpl(ItemDefaultImpl { ref path, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 435 | visitor.visit_path(path); |
| 436 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 437 | ItemKind::Impl(ItemImpl { |
| 438 | ref generics, |
| 439 | ref trait_, |
| 440 | ref self_ty, |
| 441 | ref items, |
| 442 | .. |
| 443 | }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 444 | visitor.visit_generics(generics); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 445 | if let Some(ref path) = *trait_ { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 446 | visitor.visit_path(path); |
| 447 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 448 | visitor.visit_ty(self_ty); |
| 449 | walk_list!(visitor, visit_impl_item, items); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 450 | } |
David Tolnay | 05120ef | 2017-03-12 18:29:26 -0700 | [diff] [blame] | 451 | ItemKind::Mac(ref mac) => visitor.visit_mac(mac), |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
| 455 | #[cfg(feature = "full")] |
David Tolnay | 02a8d47 | 2017-02-19 12:59:44 -0800 | [diff] [blame] | 456 | #[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))] |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 457 | pub fn walk_expr<V: Visitor>(visitor: &mut V, expr: &Expr) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 458 | use expr::*; |
| 459 | use expr::ExprKind::*; |
| 460 | |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 461 | walk_list!(visitor, visit_attribute, &expr.attrs); |
| 462 | match expr.node { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 463 | InPlace(ExprInPlace { ref place, ref value, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 464 | visitor.visit_expr(place); |
| 465 | visitor.visit_expr(value); |
| 466 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 467 | Call(ExprCall { ref func, ref args, .. }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 468 | visitor.visit_expr(func); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 469 | walk_list!(visitor, visit_expr, args.items()); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 470 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 471 | MethodCall(ExprMethodCall { ref method, ref typarams, ref args, .. }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 472 | visitor.visit_ident(method); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 473 | walk_list!(visitor, visit_ty, typarams.items()); |
| 474 | walk_list!(visitor, visit_expr, args.items()); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 475 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 476 | Array(ExprArray { ref exprs, ..}) | |
| 477 | Tup(ExprTup { args: ref exprs, .. }) => { |
| 478 | walk_list!(visitor, visit_expr, exprs.items()); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 479 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 480 | Lit(ref lit) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 481 | visitor.visit_lit(lit); |
| 482 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 483 | Cast(ExprCast { ref expr, ref ty, .. }) | |
| 484 | Type(ExprType { ref expr, ref ty, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 485 | visitor.visit_expr(expr); |
| 486 | visitor.visit_ty(ty); |
| 487 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 488 | If(ExprIf { ref cond, ref if_true, ref if_false, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 489 | visitor.visit_expr(cond); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 490 | walk_list!(visitor, visit_stmt, &if_true.stmts); |
| 491 | if let Some(ref alt) = *if_false { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 492 | visitor.visit_expr(alt); |
| 493 | } |
| 494 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 495 | IfLet(ExprIfLet { ref pat, ref expr, ref if_true, ref if_false, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 496 | visitor.visit_pat(pat); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 497 | visitor.visit_expr(expr); |
| 498 | walk_list!(visitor, visit_stmt, &if_true.stmts); |
| 499 | if let Some(ref alt) = *if_false { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 500 | visitor.visit_expr(alt); |
| 501 | } |
| 502 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 503 | While(ExprWhile { ref cond, ref body, ref label, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 504 | visitor.visit_expr(cond); |
| 505 | walk_list!(visitor, visit_stmt, &body.stmts); |
| 506 | walk_opt_ident(visitor, label); |
| 507 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 508 | WhileLet(ExprWhileLet { ref pat, ref expr, ref body, ref label, .. }) | |
| 509 | ForLoop(ExprForLoop { ref pat, ref expr, ref body, ref label, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 510 | visitor.visit_pat(pat); |
| 511 | visitor.visit_expr(expr); |
| 512 | walk_list!(visitor, visit_stmt, &body.stmts); |
| 513 | walk_opt_ident(visitor, label); |
| 514 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 515 | Loop(ExprLoop { ref body, ref label, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 516 | walk_list!(visitor, visit_stmt, &body.stmts); |
| 517 | walk_opt_ident(visitor, label); |
| 518 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 519 | Match(ExprMatch { ref expr, ref arms, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 520 | visitor.visit_expr(expr); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 521 | for &Arm { ref attrs, ref pats, ref guard, ref body, .. } in arms { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 522 | walk_list!(visitor, visit_attribute, attrs); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 523 | walk_list!(visitor, visit_pat, pats.items()); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 524 | if let Some(ref guard) = *guard { |
| 525 | visitor.visit_expr(guard); |
| 526 | } |
| 527 | visitor.visit_expr(body); |
| 528 | } |
| 529 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 530 | Closure(ExprClosure { ref decl, ref body, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 531 | visitor.visit_fn_decl(decl); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 532 | visitor.visit_expr(body); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 533 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 534 | Catch(ExprCatch { ref block, .. }) | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 535 | Block(ExprBlock { ref block, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 536 | walk_list!(visitor, visit_stmt, &block.stmts); |
| 537 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 538 | Binary(ExprBinary { ref left, ref right, .. }) | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 539 | Assign(ExprAssign { ref left, ref right, .. }) | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 540 | AssignOp(ExprAssignOp { ref left, ref right, .. }) => { |
| 541 | visitor.visit_expr(left); |
| 542 | visitor.visit_expr(right); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 543 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 544 | Field(ExprField { ref expr, ref field, .. }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 545 | visitor.visit_expr(expr); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 546 | visitor.visit_ident(field); |
| 547 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 548 | Index(ExprIndex { ref expr, ref index, .. }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 549 | visitor.visit_expr(expr); |
| 550 | visitor.visit_expr(index); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 551 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 552 | Range(ExprRange { ref from, ref to, .. }) => { |
| 553 | if let Some(ref start) = *from { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 554 | visitor.visit_expr(start); |
| 555 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 556 | if let Some(ref end) = *to { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 557 | visitor.visit_expr(end); |
| 558 | } |
| 559 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 560 | Path(ExprPath { ref qself, ref path }) => { |
| 561 | if let Some(ref qself) = *qself { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 562 | visitor.visit_ty(&qself.ty); |
| 563 | } |
| 564 | visitor.visit_path(path); |
| 565 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 566 | Break(ExprBreak { ref label, ref expr, .. }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 567 | walk_opt_ident(visitor, label); |
| 568 | if let Some(ref expr) = *expr { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 569 | visitor.visit_expr(expr); |
| 570 | } |
| 571 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 572 | Continue(ExprContinue { ref label, .. }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 573 | walk_opt_ident(visitor, label); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 574 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 575 | Ret(ExprRet { ref expr, .. }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 576 | if let Some(ref expr) = *expr { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 577 | visitor.visit_expr(expr); |
| 578 | } |
| 579 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 580 | Mac(ref mac) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 581 | visitor.visit_mac(mac); |
| 582 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 583 | Struct(ExprStruct { ref path, ref fields, ref rest, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 584 | visitor.visit_path(path); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 585 | for &FieldValue { ref ident, ref expr, .. } in fields.items() { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 586 | visitor.visit_ident(ident); |
| 587 | visitor.visit_expr(expr); |
| 588 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 589 | if let Some(ref base) = *rest { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 590 | visitor.visit_expr(base); |
| 591 | } |
| 592 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 593 | Repeat(ExprRepeat { ref expr, ref amt, .. }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 594 | visitor.visit_expr(expr); |
| 595 | visitor.visit_expr(amt); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 596 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 597 | TupField(ExprTupField { ref expr, .. }) | |
| 598 | Unary(ExprUnary { ref expr, .. }) | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 599 | Box(ExprBox { ref expr, .. }) | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 600 | AddrOf(ExprAddrOf { ref expr, .. }) | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 601 | Paren(ExprParen { ref expr, .. }) | |
| 602 | Try(ExprTry { ref expr, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 603 | visitor.visit_expr(expr); |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | #[cfg(feature = "full")] |
| 609 | pub fn walk_foreign_item<V: Visitor>(visitor: &mut V, foreign_item: &ForeignItem) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 610 | use item::*; |
| 611 | |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 612 | visitor.visit_ident(&foreign_item.ident); |
| 613 | walk_list!(visitor, visit_attribute, &foreign_item.attrs); |
| 614 | match foreign_item.node { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 615 | ForeignItemKind::Fn(ForeignItemFn { ref decl, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 616 | visitor.visit_fn_decl(decl); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 617 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 618 | ForeignItemKind::Static(ForeignItemStatic { ref ty, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 619 | visitor.visit_ty(ty); |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | #[cfg(feature = "full")] |
| 625 | pub fn walk_pat<V: Visitor>(visitor: &mut V, pat: &Pat) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 626 | use expr::*; |
| 627 | |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 628 | match *pat { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 629 | Pat::Wild(_) => {} |
| 630 | Pat::Ident(PatIdent { ref ident, ref subpat, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 631 | visitor.visit_ident(ident); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 632 | if let Some(ref pat) = *subpat { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 633 | visitor.visit_pat(pat); |
| 634 | } |
| 635 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 636 | Pat::Struct(PatStruct { ref path, ref fields, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 637 | visitor.visit_path(path); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 638 | for &FieldPat { ref ident, ref pat, .. } in fields.items() { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 639 | visitor.visit_ident(ident); |
| 640 | visitor.visit_pat(pat); |
| 641 | } |
| 642 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 643 | Pat::TupleStruct(PatTupleStruct { ref path, ref pat, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 644 | visitor.visit_path(path); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 645 | walk_list!(visitor, visit_pat, pat.pats.items()); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 646 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 647 | Pat::Path(PatPath { ref qself, ref path }) => { |
| 648 | if let Some(ref qself) = *qself { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 649 | visitor.visit_ty(&qself.ty); |
| 650 | } |
| 651 | visitor.visit_path(path); |
| 652 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 653 | Pat::Tuple(PatTuple { ref pats, .. }) => { |
| 654 | walk_list!(visitor, visit_pat, pats.items()); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 655 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 656 | Pat::Box(PatBox { ref pat, .. }) | |
| 657 | Pat::Ref(PatRef { ref pat, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 658 | visitor.visit_pat(pat); |
| 659 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 660 | Pat::Lit(PatLit { ref expr }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 661 | visitor.visit_expr(expr); |
| 662 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 663 | Pat::Range(PatRange { ref lo, ref hi, .. }) => { |
| 664 | visitor.visit_expr(lo); |
| 665 | visitor.visit_expr(hi); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 666 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 667 | Pat::Slice(PatSlice { ref front, ref middle, ref back, .. }) => { |
| 668 | walk_list!(visitor, visit_pat, front.items()); |
| 669 | if let Some(ref mid) = *middle { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 670 | visitor.visit_pat(mid); |
| 671 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 672 | walk_list!(visitor, visit_pat, back.items()); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 673 | } |
| 674 | Pat::Mac(ref mac) => { |
| 675 | visitor.visit_mac(mac); |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | #[cfg(feature = "full")] |
| 681 | pub fn walk_fn_decl<V: Visitor>(visitor: &mut V, fn_decl: &FnDecl) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 682 | use item::*; |
| 683 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 684 | for input in fn_decl.inputs.items() { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 685 | match *input { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 686 | FnArg::SelfRef(_) | |
David Tolnay | 05120ef | 2017-03-12 18:29:26 -0700 | [diff] [blame] | 687 | FnArg::SelfValue(_) => {} |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 688 | FnArg::Captured(ArgCaptured { ref pat, ref ty, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 689 | visitor.visit_pat(pat); |
| 690 | visitor.visit_ty(ty); |
| 691 | } |
| 692 | FnArg::Ignored(ref ty) => { |
| 693 | visitor.visit_ty(ty); |
| 694 | } |
| 695 | } |
| 696 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 697 | visitor.visit_generics(&fn_decl.generics); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 698 | visitor.visit_fn_ret_ty(&fn_decl.output); |
| 699 | } |
| 700 | |
| 701 | #[cfg(feature = "full")] |
| 702 | pub fn walk_trait_item<V: Visitor>(visitor: &mut V, trait_item: &TraitItem) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 703 | use item::*; |
| 704 | |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 705 | visitor.visit_ident(&trait_item.ident); |
| 706 | walk_list!(visitor, visit_attribute, &trait_item.attrs); |
| 707 | match trait_item.node { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 708 | TraitItemKind::Const(TraitItemConst { ref ty, ref default, ..}) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 709 | visitor.visit_ty(ty); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 710 | if let Some(ref expr) = *default { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 711 | visitor.visit_expr(expr); |
| 712 | } |
| 713 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 714 | TraitItemKind::Method(TraitItemMethod { ref sig, ref default, .. }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 715 | visitor.visit_method_sig(sig); |
| 716 | if let Some(ref block) = *default { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 717 | walk_list!(visitor, visit_stmt, &block.stmts); |
| 718 | } |
| 719 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 720 | TraitItemKind::Type(TraitItemType { ref bounds, ref default, .. }) => { |
| 721 | walk_list!(visitor, visit_ty_param_bound, bounds.items()); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 722 | if let Some(ref ty) = *default { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 723 | visitor.visit_ty(ty); |
| 724 | } |
| 725 | } |
| 726 | TraitItemKind::Macro(ref mac) => { |
| 727 | visitor.visit_mac(mac); |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | #[cfg(feature = "full")] |
| 733 | pub fn walk_impl_item<V: Visitor>(visitor: &mut V, impl_item: &ImplItem) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 734 | use item::*; |
| 735 | |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 736 | visitor.visit_ident(&impl_item.ident); |
| 737 | walk_list!(visitor, visit_attribute, &impl_item.attrs); |
| 738 | match impl_item.node { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 739 | ImplItemKind::Const(ImplItemConst { ref ty, ref expr, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 740 | visitor.visit_ty(ty); |
| 741 | visitor.visit_expr(expr); |
| 742 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 743 | ImplItemKind::Method(ImplItemMethod { ref sig, ref block, .. }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 744 | visitor.visit_method_sig(sig); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 745 | walk_list!(visitor, visit_stmt, &block.stmts); |
| 746 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 747 | ImplItemKind::Type(ImplItemType { ref ty, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 748 | visitor.visit_ty(ty); |
| 749 | } |
| 750 | ImplItemKind::Macro(ref mac) => { |
| 751 | visitor.visit_mac(mac); |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | #[cfg(feature = "full")] |
| 757 | pub fn walk_method_sig<V: Visitor>(visitor: &mut V, method_sig: &MethodSig) { |
| 758 | visitor.visit_fn_decl(&method_sig.decl); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | #[cfg(feature = "full")] |
| 762 | pub fn walk_stmt<V: Visitor>(visitor: &mut V, stmt: &Stmt) { |
| 763 | match *stmt { |
| 764 | Stmt::Local(ref local) => { |
| 765 | visitor.visit_local(local); |
| 766 | } |
| 767 | Stmt::Item(ref item) => { |
| 768 | visitor.visit_item(item); |
| 769 | } |
| 770 | Stmt::Expr(ref expr) | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 771 | Stmt::Semi(ref expr, _) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 772 | visitor.visit_expr(expr); |
| 773 | } |
| 774 | Stmt::Mac(ref details) => { |
| 775 | let (ref mac, _, ref attrs) = **details; |
| 776 | visitor.visit_mac(mac); |
| 777 | walk_list!(visitor, visit_attribute, attrs); |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | #[cfg(feature = "full")] |
| 783 | pub fn walk_local<V: Visitor>(visitor: &mut V, local: &Local) { |
| 784 | visitor.visit_pat(&local.pat); |
| 785 | if let Some(ref ty) = local.ty { |
| 786 | visitor.visit_ty(ty); |
| 787 | } |
| 788 | if let Some(ref init) = local.init { |
| 789 | visitor.visit_expr(init); |
| 790 | } |
| 791 | walk_list!(visitor, visit_attribute, &local.attrs); |
| 792 | } |
| 793 | |
| 794 | #[cfg(feature = "full")] |
| 795 | pub fn walk_view_path<V: Visitor>(visitor: &mut V, view_path: &ViewPath) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 796 | use item::*; |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 797 | match *view_path { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 798 | ViewPath::Simple(PathSimple { ref path, ref rename, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 799 | visitor.visit_path(path); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 800 | walk_opt_ident(visitor, rename); |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 801 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 802 | ViewPath::Glob(PathGlob { ref path, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 803 | visitor.visit_path(path); |
| 804 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 805 | ViewPath::List(PathList { ref path, ref items, .. }) => { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 806 | visitor.visit_path(path); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 807 | for &PathListItem { ref name, ref rename, .. } in items.items() { |
Michael Layzell | b52df32 | 2017-01-22 17:36:55 -0500 | [diff] [blame] | 808 | visitor.visit_ident(name); |
| 809 | walk_opt_ident(visitor, rename); |
| 810 | } |
| 811 | } |
David Tolnay | 429168f | 2016-10-05 23:41:04 -0700 | [diff] [blame] | 812 | } |
| 813 | } |