@@ -16,16 +16,14 @@ use syntax::ast::*;
16
16
use syntax:: visit;
17
17
18
18
/// Creates def ids for nodes in the HIR.
19
- pub struct DefCollector < ' ast > {
20
- pub krate : & ' ast Crate ,
19
+ pub struct DefCollector {
21
20
pub definitions : Definitions ,
22
21
pub parent_def : Option < DefIndex > ,
23
22
}
24
23
25
- impl < ' ast > DefCollector < ' ast > {
26
- pub fn root ( krate : & ' ast Crate ) -> DefCollector < ' ast > {
24
+ impl DefCollector {
25
+ pub fn root ( ) -> DefCollector {
27
26
let mut collector = DefCollector {
28
- krate : krate,
29
27
definitions : Definitions :: new ( ) ,
30
28
parent_def : None ,
31
29
} ;
@@ -37,14 +35,12 @@ impl<'ast> DefCollector<'ast> {
37
35
collector
38
36
}
39
37
40
- pub fn extend ( krate : & ' ast Crate ,
41
- parent_node : NodeId ,
38
+ pub fn extend ( parent_node : NodeId ,
42
39
parent_def_path : DefPath ,
43
40
parent_def_id : DefId ,
44
41
definitions : Definitions )
45
- -> DefCollector < ' ast > {
42
+ -> DefCollector {
46
43
let mut collector = DefCollector {
47
- krate : krate,
48
44
parent_def : None ,
49
45
definitions : definitions,
50
46
} ;
@@ -78,9 +74,16 @@ impl<'ast> DefCollector<'ast> {
78
74
-> DefIndex {
79
75
self . definitions . create_def_with_parent ( parent, node_id, data)
80
76
}
77
+
78
+ fn with_parent < F : FnOnce ( & mut Self ) > ( & mut self , parent_def : DefIndex , f : F ) {
79
+ let parent = self . parent_def ;
80
+ self . parent_def = Some ( parent_def) ;
81
+ f ( self ) ;
82
+ self . parent_def = parent;
83
+ }
81
84
}
82
85
83
- impl < ' ast > visit:: Visitor < ' ast > for DefCollector < ' ast > {
86
+ impl < ' ast > visit:: Visitor < ' ast > for DefCollector {
84
87
fn visit_item ( & mut self , i : & ' ast Item ) {
85
88
debug ! ( "visit_item: {:?}" , i) ;
86
89
@@ -98,60 +101,55 @@ impl<'ast> visit::Visitor<'ast> for DefCollector<'ast> {
98
101
ItemKind :: Mac ( ..) => DefPathData :: MacroDef ( i. ident . name ) ,
99
102
ItemKind :: Use ( ..) => DefPathData :: Misc ,
100
103
} ;
101
-
102
104
let def = self . create_def ( i. id , def_data) ;
103
105
104
- let parent_def = self . parent_def ;
105
- self . parent_def = Some ( def) ;
106
-
107
- match i. node {
108
- ItemKind :: Enum ( ref enum_definition, _) => {
109
- for v in & enum_definition. variants {
110
- let variant_def_index =
111
- self . create_def ( v. node . data . id ( ) ,
112
- DefPathData :: EnumVariant ( v. node . name . name ) ) ;
113
-
114
- for field in v. node . data . fields ( ) {
115
- if let Some ( ident) = field. ident {
116
- self . create_def_with_parent ( Some ( variant_def_index) ,
117
- field. id ,
118
- DefPathData :: Field ( ident. name ) ) ;
106
+ self . with_parent ( def, |this| {
107
+ match i. node {
108
+ ItemKind :: Enum ( ref enum_definition, _) => {
109
+ for v in & enum_definition. variants {
110
+ let variant_def_index =
111
+ this. create_def ( v. node . data . id ( ) ,
112
+ DefPathData :: EnumVariant ( v. node . name . name ) ) ;
113
+
114
+ for field in v. node . data . fields ( ) {
115
+ if let Some ( ident) = field. ident {
116
+ this. create_def_with_parent ( Some ( variant_def_index) ,
117
+ field. id ,
118
+ DefPathData :: Field ( ident. name ) ) ;
119
+ }
119
120
}
120
121
}
121
122
}
122
- }
123
- ItemKind :: Struct ( ref struct_def, _) => {
124
- // If this is a tuple-like struct, register the constructor.
125
- if !struct_def. is_struct ( ) {
126
- self . create_def ( struct_def. id ( ) ,
127
- DefPathData :: StructCtor ) ;
128
- }
123
+ ItemKind :: Struct ( ref struct_def, _) => {
124
+ // If this is a tuple-like struct, register the constructor.
125
+ if !struct_def. is_struct ( ) {
126
+ this. create_def ( struct_def. id ( ) ,
127
+ DefPathData :: StructCtor ) ;
128
+ }
129
129
130
- for field in struct_def. fields ( ) {
131
- if let Some ( ident) = field. ident {
132
- self . create_def ( field. id , DefPathData :: Field ( ident. name ) ) ;
130
+ for field in struct_def. fields ( ) {
131
+ if let Some ( ident) = field. ident {
132
+ this. create_def ( field. id , DefPathData :: Field ( ident. name ) ) ;
133
+ }
133
134
}
134
135
}
136
+ _ => { }
135
137
}
136
- _ => { }
137
- }
138
- visit:: walk_item ( self , i) ;
139
- self . parent_def = parent_def;
138
+ visit:: walk_item ( this, i) ;
139
+ } ) ;
140
140
}
141
141
142
142
fn visit_foreign_item ( & mut self , foreign_item : & ' ast ForeignItem ) {
143
143
let def = self . create_def ( foreign_item. id , DefPathData :: ValueNs ( foreign_item. ident . name ) ) ;
144
144
145
- let parent_def = self . parent_def ;
146
- self . parent_def = Some ( def) ;
147
- visit:: walk_foreign_item ( self , foreign_item) ;
148
- self . parent_def = parent_def;
145
+ self . with_parent ( def, |this| {
146
+ visit:: walk_foreign_item ( this, foreign_item) ;
147
+ } ) ;
149
148
}
150
149
151
150
fn visit_generics ( & mut self , generics : & ' ast Generics ) {
152
151
for ty_param in generics. ty_params . iter ( ) {
153
- self . create_def ( ty_param. id ,
154
- DefPathData :: TypeParam ( ty_param. ident . name ) ) ;
152
+ self . create_def ( ty_param. id , DefPathData :: TypeParam ( ty_param. ident . name ) ) ;
155
153
}
156
154
157
155
visit:: walk_generics ( self , generics) ;
@@ -165,20 +163,13 @@ impl<'ast> visit::Visitor<'ast> for DefCollector<'ast> {
165
163
} ;
166
164
167
165
let def = self . create_def ( ti. id , def_data) ;
168
-
169
- let parent_def = self . parent_def ;
170
- self . parent_def = Some ( def) ;
171
-
172
- match ti. node {
173
- TraitItemKind :: Const ( _, Some ( ref expr) ) => {
174
- self . create_def ( expr. id , DefPathData :: Initializer ) ;
166
+ self . with_parent ( def, |this| {
167
+ if let TraitItemKind :: Const ( _, Some ( ref expr) ) = ti. node {
168
+ this. create_def ( expr. id , DefPathData :: Initializer ) ;
175
169
}
176
- _ => { }
177
- }
178
-
179
- visit:: walk_trait_item ( self , ti) ;
180
170
181
- self . parent_def = parent_def;
171
+ visit:: walk_trait_item ( this, ti) ;
172
+ } ) ;
182
173
}
183
174
184
175
fn visit_impl_item ( & mut self , ii : & ' ast ImplItem ) {
@@ -190,20 +181,13 @@ impl<'ast> visit::Visitor<'ast> for DefCollector<'ast> {
190
181
} ;
191
182
192
183
let def = self . create_def ( ii. id , def_data) ;
193
-
194
- let parent_def = self . parent_def ;
195
- self . parent_def = Some ( def) ;
196
-
197
- match ii. node {
198
- ImplItemKind :: Const ( _, ref expr) => {
199
- self . create_def ( expr. id , DefPathData :: Initializer ) ;
184
+ self . with_parent ( def, |this| {
185
+ if let ImplItemKind :: Const ( _, ref expr) = ii. node {
186
+ this. create_def ( expr. id , DefPathData :: Initializer ) ;
200
187
}
201
- _ => { }
202
- }
203
-
204
- visit:: walk_impl_item ( self , ii) ;
205
188
206
- self . parent_def = parent_def;
189
+ visit:: walk_impl_item ( this, ii) ;
190
+ } ) ;
207
191
}
208
192
209
193
fn visit_pat ( & mut self , pat : & ' ast Pat ) {
@@ -234,14 +218,6 @@ impl<'ast> visit::Visitor<'ast> for DefCollector<'ast> {
234
218
self . parent_def = parent_def;
235
219
}
236
220
237
- fn visit_stmt ( & mut self , stmt : & ' ast Stmt ) {
238
- visit:: walk_stmt ( self , stmt) ;
239
- }
240
-
241
- fn visit_block ( & mut self , block : & ' ast Block ) {
242
- visit:: walk_block ( self , block) ;
243
- }
244
-
245
221
fn visit_lifetime_def ( & mut self , def : & ' ast LifetimeDef ) {
246
222
self . create_def ( def. lifetime . id , DefPathData :: LifetimeDef ( def. lifetime . name ) ) ;
247
223
}
0 commit comments