blob: c4e6c2d62d457e4df3e57a00997959794c6718cc [file] [log] [blame]
David Tolnay55337722016-09-11 12:58:56 -07001// 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
18use 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.)
29pub trait Visitor: Sized {
30 fn visit_ident(&mut self, _ident: &Ident) {}
David Tolnayc4fbf402016-09-24 09:31:47 -070031 fn visit_macro_input(&mut self, macro_input: &MacroInput) {
32 walk_macro_input(self, macro_input)
David Tolnay55337722016-09-11 12:58:56 -070033 }
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 }
75}
76
77#[macro_export]
78macro_rules! walk_list {
79 ($visitor: expr, $method: ident, $list: expr) => {
80 for elem in $list {
81 $visitor.$method(elem)
82 }
83 };
84 ($visitor: expr, $method: ident, $list: expr, $($extra_args: expr),*) => {
85 for elem in $list {
86 $visitor.$method(elem, $($extra_args,)*)
87 }
88 }
89}
90
91pub fn walk_opt_ident<V: Visitor>(visitor: &mut V, opt_ident: &Option<Ident>) {
92 if let Some(ref ident) = *opt_ident {
93 visitor.visit_ident(ident);
94 }
95}
96
97pub fn walk_lifetime_def<V: Visitor>(visitor: &mut V, lifetime_def: &LifetimeDef) {
98 visitor.visit_lifetime(&lifetime_def.lifetime);
99 walk_list!(visitor, visit_lifetime, &lifetime_def.bounds);
100}
101
102pub fn walk_poly_trait_ref<V>(visitor: &mut V, trait_ref: &PolyTraitRef, _: &TraitBoundModifier)
David Tolnaydaaf7742016-10-03 11:11:43 -0700103 where V: Visitor
David Tolnay55337722016-09-11 12:58:56 -0700104{
105 walk_list!(visitor, visit_lifetime_def, &trait_ref.bound_lifetimes);
106 visitor.visit_path(&trait_ref.trait_ref);
107}
108
David Tolnayc4fbf402016-09-24 09:31:47 -0700109pub fn walk_macro_input<V: Visitor>(visitor: &mut V, macro_input: &MacroInput) {
110 visitor.visit_ident(&macro_input.ident);
111 visitor.visit_generics(&macro_input.generics);
112 match macro_input.body {
David Tolnay55337722016-09-11 12:58:56 -0700113 Body::Enum(ref variants) => {
David Tolnayc4fbf402016-09-24 09:31:47 -0700114 walk_list!(visitor, visit_variant, variants, &macro_input.generics);
David Tolnay55337722016-09-11 12:58:56 -0700115 }
116 Body::Struct(ref variant_data) => {
David Tolnaydaaf7742016-10-03 11:11:43 -0700117 visitor.visit_variant_data(variant_data, &macro_input.ident, &macro_input.generics);
David Tolnay55337722016-09-11 12:58:56 -0700118 }
119 }
David Tolnayc4fbf402016-09-24 09:31:47 -0700120 walk_list!(visitor, visit_attribute, &macro_input.attrs);
David Tolnay55337722016-09-11 12:58:56 -0700121}
122
123pub fn walk_variant<V>(visitor: &mut V, variant: &Variant, generics: &Generics)
David Tolnaydaaf7742016-10-03 11:11:43 -0700124 where V: Visitor
David Tolnay55337722016-09-11 12:58:56 -0700125{
126 visitor.visit_ident(&variant.ident);
127 visitor.visit_variant_data(&variant.data, &variant.ident, generics);
128 walk_list!(visitor, visit_attribute, &variant.attrs);
129}
130
131pub fn walk_ty<V: Visitor>(visitor: &mut V, ty: &Ty) {
132 match *ty {
David Tolnaydaaf7742016-10-03 11:11:43 -0700133 Ty::Vec(ref inner) |
134 Ty::Paren(ref inner) => visitor.visit_ty(inner),
135 Ty::Ptr(ref mutable_type) => visitor.visit_ty(&mutable_type.ty),
David Tolnay55337722016-09-11 12:58:56 -0700136 Ty::Rptr(ref opt_lifetime, ref mutable_type) => {
137 walk_list!(visitor, visit_lifetime, opt_lifetime);
138 visitor.visit_ty(&mutable_type.ty)
139 }
David Tolnaydaaf7742016-10-03 11:11:43 -0700140 Ty::Never => {}
David Tolnay55337722016-09-11 12:58:56 -0700141 Ty::Tup(ref tuple_element_types) => {
142 walk_list!(visitor, visit_ty, tuple_element_types);
143 }
144 Ty::BareFn(ref function_declaration) => {
145 walk_fn_decl(visitor, &function_declaration.decl);
146 walk_list!(visitor, visit_lifetime_def, &function_declaration.lifetimes);
147 }
148 Ty::Path(ref maybe_qself, ref path) => {
149 if let Some(ref qself) = *maybe_qself {
150 visitor.visit_ty(&qself.ty);
151 }
152 visitor.visit_path(path);
153 }
154 Ty::ObjectSum(ref inner, ref bounds) => {
155 visitor.visit_ty(inner);
156 walk_list!(visitor, visit_ty_param_bound, bounds);
157 }
158 Ty::FixedLengthVec(ref inner, _len) => {
159 visitor.visit_ty(inner);
160 }
161 Ty::PolyTraitRef(ref bounds) => {
162 walk_list!(visitor, visit_ty_param_bound, bounds);
163 }
164 Ty::ImplTrait(ref bounds) => {
165 walk_list!(visitor, visit_ty_param_bound, bounds);
166 }
167 Ty::Infer => {}
168 }
169}
170
171pub fn walk_path<V: Visitor>(visitor: &mut V, path: &Path) {
172 for segment in &path.segments {
173 visitor.visit_path_segment(segment);
174 }
175}
176
177pub fn walk_path_segment<V: Visitor>(visitor: &mut V, segment: &PathSegment) {
178 visitor.visit_ident(&segment.ident);
179 visitor.visit_path_parameters(&segment.parameters);
180}
181
182pub fn walk_path_parameters<V>(visitor: &mut V, path_parameters: &PathParameters)
David Tolnaydaaf7742016-10-03 11:11:43 -0700183 where V: Visitor
David Tolnay55337722016-09-11 12:58:56 -0700184{
185 match *path_parameters {
186 PathParameters::AngleBracketed(ref data) => {
187 walk_list!(visitor, visit_ty, &data.types);
188 walk_list!(visitor, visit_lifetime, &data.lifetimes);
189 walk_list!(visitor, visit_assoc_type_binding, &data.bindings);
190 }
191 PathParameters::Parenthesized(ref data) => {
192 walk_list!(visitor, visit_ty, &data.inputs);
193 walk_list!(visitor, visit_ty, &data.output);
194 }
195 }
196}
197
198pub fn walk_assoc_type_binding<V: Visitor>(visitor: &mut V, type_binding: &TypeBinding) {
199 visitor.visit_ident(&type_binding.ident);
200 visitor.visit_ty(&type_binding.ty);
201}
202
203pub fn walk_ty_param_bound<V: Visitor>(visitor: &mut V, bound: &TyParamBound) {
204 match *bound {
205 TyParamBound::Trait(ref ty, ref modifier) => {
206 visitor.visit_poly_trait_ref(ty, modifier);
207 }
208 TyParamBound::Region(ref lifetime) => {
209 visitor.visit_lifetime(lifetime);
210 }
211 }
212}
213
214pub fn walk_generics<V: Visitor>(visitor: &mut V, generics: &Generics) {
215 for param in &generics.ty_params {
216 visitor.visit_ident(&param.ident);
217 walk_list!(visitor, visit_ty_param_bound, &param.bounds);
218 walk_list!(visitor, visit_ty, &param.default);
219 }
220 walk_list!(visitor, visit_lifetime_def, &generics.lifetimes);
221 for predicate in &generics.where_clause.predicates {
222 match *predicate {
David Tolnaydaaf7742016-10-03 11:11:43 -0700223 WherePredicate::BoundPredicate(WhereBoundPredicate { ref bounded_ty,
224 ref bounds,
225 ref bound_lifetimes,
226 .. }) => {
David Tolnay55337722016-09-11 12:58:56 -0700227 visitor.visit_ty(bounded_ty);
228 walk_list!(visitor, visit_ty_param_bound, bounds);
229 walk_list!(visitor, visit_lifetime_def, bound_lifetimes);
230 }
David Tolnaydaaf7742016-10-03 11:11:43 -0700231 WherePredicate::RegionPredicate(WhereRegionPredicate { ref lifetime,
232 ref bounds,
233 .. }) => {
David Tolnay55337722016-09-11 12:58:56 -0700234 visitor.visit_lifetime(lifetime);
235 walk_list!(visitor, visit_lifetime, bounds);
236 }
237 }
238 }
239}
240
241pub fn walk_fn_ret_ty<V: Visitor>(visitor: &mut V, ret_ty: &FunctionRetTy) {
242 if let FunctionRetTy::Ty(ref output_ty) = *ret_ty {
243 visitor.visit_ty(output_ty)
244 }
245}
246
247pub fn walk_fn_decl<V: Visitor>(visitor: &mut V, function_declaration: &FnDecl) {
248 for argument in &function_declaration.inputs {
249 walk_opt_ident(visitor, &argument.pat);
250 visitor.visit_ty(&argument.ty)
251 }
252 visitor.visit_fn_ret_ty(&function_declaration.output)
253}
254
255pub fn walk_variant_data<V: Visitor>(visitor: &mut V, data: &VariantData) {
256 walk_list!(visitor, visit_field, data.fields());
257}
258
259pub fn walk_field<V: Visitor>(visitor: &mut V, field: &Field) {
260 walk_opt_ident(visitor, &field.ident);
261 visitor.visit_ty(&field.ty);
262 walk_list!(visitor, visit_attribute, &field.attrs);
263}