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