@@ -18,31 +18,33 @@ use middle::cstore::InlinedItem;
18
18
19
19
use syntax:: ast:: * ;
20
20
use syntax:: visit;
21
- use syntax:: parse:: token;
21
+ use syntax:: parse:: token:: { self , keywords } ;
22
22
23
23
/// Creates def ids for nodes in the HIR.
24
- pub struct DefCollector < ' ast > {
24
+ pub struct DefCollector < ' a > {
25
25
// If we are walking HIR (c.f., AST), we need to keep a reference to the
26
26
// crate.
27
- hir_crate : Option < & ' ast hir:: Crate > ,
28
- definitions : & ' ast mut Definitions ,
27
+ hir_crate : Option < & ' a hir:: Crate > ,
28
+ definitions : & ' a mut Definitions ,
29
29
parent_def : Option < DefIndex > ,
30
+ pub visit_macro_invoc : Option < & ' a mut FnMut ( NodeId , DefIndex ) > ,
30
31
}
31
32
32
- impl < ' ast > DefCollector < ' ast > {
33
- pub fn new ( definitions : & ' ast mut Definitions ) -> DefCollector < ' ast > {
33
+ impl < ' a > DefCollector < ' a > {
34
+ pub fn new ( definitions : & ' a mut Definitions ) -> Self {
34
35
DefCollector {
35
36
hir_crate : None ,
36
37
definitions : definitions,
37
38
parent_def : None ,
39
+ visit_macro_invoc : None ,
38
40
}
39
41
}
40
42
41
43
pub fn extend ( parent_node : NodeId ,
42
44
parent_def_path : DefPath ,
43
45
parent_def_id : DefId ,
44
- definitions : & ' ast mut Definitions )
45
- -> DefCollector < ' ast > {
46
+ definitions : & ' a mut Definitions )
47
+ -> Self {
46
48
let mut collector = DefCollector :: new ( definitions) ;
47
49
48
50
assert_eq ! ( parent_def_path. krate, parent_def_id. krate) ;
@@ -65,7 +67,7 @@ impl<'ast> DefCollector<'ast> {
65
67
self . create_def_with_parent ( Some ( CRATE_DEF_INDEX ) , DUMMY_NODE_ID , DefPathData :: Misc ) ;
66
68
}
67
69
68
- pub fn walk_item ( & mut self , ii : & ' ast InlinedItem , krate : & ' ast hir:: Crate ) {
70
+ pub fn walk_item ( & mut self , ii : & ' a InlinedItem , krate : & ' a hir:: Crate ) {
69
71
self . hir_crate = Some ( krate) ;
70
72
ii. visit ( self ) ;
71
73
}
@@ -84,7 +86,7 @@ impl<'ast> DefCollector<'ast> {
84
86
self . definitions . create_def_with_parent ( parent, node_id, data)
85
87
}
86
88
87
- fn with_parent < F : FnOnce ( & mut Self ) > ( & mut self , parent_def : DefIndex , f : F ) {
89
+ pub fn with_parent < F : FnOnce ( & mut Self ) > ( & mut self , parent_def : DefIndex , f : F ) {
88
90
let parent = self . parent_def ;
89
91
self . parent_def = Some ( parent_def) ;
90
92
f ( self ) ;
@@ -106,7 +108,7 @@ impl<'ast> DefCollector<'ast> {
106
108
self . create_def ( expr. id , DefPathData :: Initializer ) ;
107
109
}
108
110
109
- fn visit_hir_const_integer ( & mut self , expr : & ' ast hir:: Expr ) {
111
+ fn visit_hir_const_integer ( & mut self , expr : & hir:: Expr ) {
110
112
// FIXME(eddyb) Closures should have separate
111
113
// function definition IDs and expression IDs.
112
114
if let hir:: ExprClosure ( ..) = expr. node {
@@ -115,9 +117,15 @@ impl<'ast> DefCollector<'ast> {
115
117
116
118
self . create_def ( expr. id , DefPathData :: Initializer ) ;
117
119
}
120
+
121
+ fn visit_macro_invoc ( & mut self , id : NodeId ) {
122
+ if let Some ( ref mut visit) = self . visit_macro_invoc {
123
+ visit ( id, self . parent_def . unwrap ( ) ) ;
124
+ }
125
+ }
118
126
}
119
127
120
- impl < ' ast > visit:: Visitor for DefCollector < ' ast > {
128
+ impl < ' a > visit:: Visitor for DefCollector < ' a > {
121
129
fn visit_item ( & mut self , i : & Item ) {
122
130
debug ! ( "visit_item: {:?}" , i) ;
123
131
@@ -129,10 +137,14 @@ impl<'ast> visit::Visitor for DefCollector<'ast> {
129
137
ItemKind :: Enum ( ..) | ItemKind :: Struct ( ..) | ItemKind :: Union ( ..) | ItemKind :: Trait ( ..) |
130
138
ItemKind :: ExternCrate ( ..) | ItemKind :: ForeignMod ( ..) | ItemKind :: Ty ( ..) =>
131
139
DefPathData :: TypeNs ( i. ident . name . as_str ( ) ) ,
140
+ ItemKind :: Mod ( ..) if i. ident == keywords:: Invalid . ident ( ) => {
141
+ return visit:: walk_item ( self , i) ;
142
+ }
132
143
ItemKind :: Mod ( ..) => DefPathData :: Module ( i. ident . name . as_str ( ) ) ,
133
144
ItemKind :: Static ( ..) | ItemKind :: Const ( ..) | ItemKind :: Fn ( ..) =>
134
145
DefPathData :: ValueNs ( i. ident . name . as_str ( ) ) ,
135
- ItemKind :: Mac ( ..) => DefPathData :: MacroDef ( i. ident . name . as_str ( ) ) ,
146
+ ItemKind :: Mac ( ..) if i. id == DUMMY_NODE_ID => return , // Scope placeholder
147
+ ItemKind :: Mac ( ..) => return self . visit_macro_invoc ( i. id ) ,
136
148
ItemKind :: Use ( ..) => DefPathData :: Misc ,
137
149
} ;
138
150
let def = self . create_def ( i. id , def_data) ;
@@ -198,7 +210,7 @@ impl<'ast> visit::Visitor for DefCollector<'ast> {
198
210
TraitItemKind :: Method ( ..) | TraitItemKind :: Const ( ..) =>
199
211
DefPathData :: ValueNs ( ti. ident . name . as_str ( ) ) ,
200
212
TraitItemKind :: Type ( ..) => DefPathData :: TypeNs ( ti. ident . name . as_str ( ) ) ,
201
- TraitItemKind :: Macro ( ..) => DefPathData :: MacroDef ( ti. ident . name . as_str ( ) ) ,
213
+ TraitItemKind :: Macro ( ..) => return self . visit_macro_invoc ( ti. id ) ,
202
214
} ;
203
215
204
216
let def = self . create_def ( ti. id , def_data) ;
@@ -216,7 +228,7 @@ impl<'ast> visit::Visitor for DefCollector<'ast> {
216
228
ImplItemKind :: Method ( ..) | ImplItemKind :: Const ( ..) =>
217
229
DefPathData :: ValueNs ( ii. ident . name . as_str ( ) ) ,
218
230
ImplItemKind :: Type ( ..) => DefPathData :: TypeNs ( ii. ident . name . as_str ( ) ) ,
219
- ImplItemKind :: Macro ( ..) => DefPathData :: MacroDef ( ii. ident . name . as_str ( ) ) ,
231
+ ImplItemKind :: Macro ( ..) => return self . visit_macro_invoc ( ii. id ) ,
220
232
} ;
221
233
222
234
let def = self . create_def ( ii. id , def_data) ;
@@ -232,9 +244,13 @@ impl<'ast> visit::Visitor for DefCollector<'ast> {
232
244
fn visit_pat ( & mut self , pat : & Pat ) {
233
245
let parent_def = self . parent_def ;
234
246
235
- if let PatKind :: Ident ( _, id, _) = pat. node {
236
- let def = self . create_def ( pat. id , DefPathData :: Binding ( id. node . name . as_str ( ) ) ) ;
237
- self . parent_def = Some ( def) ;
247
+ match pat. node {
248
+ PatKind :: Mac ( ..) => return self . visit_macro_invoc ( pat. id ) ,
249
+ PatKind :: Ident ( _, id, _) => {
250
+ let def = self . create_def ( pat. id , DefPathData :: Binding ( id. node . name . as_str ( ) ) ) ;
251
+ self . parent_def = Some ( def) ;
252
+ }
253
+ _ => { }
238
254
}
239
255
240
256
visit:: walk_pat ( self , pat) ;
@@ -244,25 +260,28 @@ impl<'ast> visit::Visitor for DefCollector<'ast> {
244
260
fn visit_expr ( & mut self , expr : & Expr ) {
245
261
let parent_def = self . parent_def ;
246
262
247
- if let ExprKind :: Repeat ( _, ref count) = expr. node {
248
- self . visit_ast_const_integer ( count) ;
249
- }
250
-
251
- if let ExprKind :: Closure ( ..) = expr. node {
252
- let def = self . create_def ( expr. id , DefPathData :: ClosureExpr ) ;
253
- self . parent_def = Some ( def) ;
263
+ match expr. node {
264
+ ExprKind :: Mac ( ..) => return self . visit_macro_invoc ( expr. id ) ,
265
+ ExprKind :: Repeat ( _, ref count) => self . visit_ast_const_integer ( count) ,
266
+ ExprKind :: Closure ( ..) => {
267
+ let def = self . create_def ( expr. id , DefPathData :: ClosureExpr ) ;
268
+ self . parent_def = Some ( def) ;
269
+ }
270
+ _ => { }
254
271
}
255
272
256
273
visit:: walk_expr ( self , expr) ;
257
274
self . parent_def = parent_def;
258
275
}
259
276
260
277
fn visit_ty ( & mut self , ty : & Ty ) {
261
- if let TyKind :: FixedLengthVec ( _, ref length) = ty. node {
262
- self . visit_ast_const_integer ( length) ;
263
- }
264
- if let TyKind :: ImplTrait ( ..) = ty. node {
265
- self . create_def ( ty. id , DefPathData :: ImplTrait ) ;
278
+ match ty. node {
279
+ TyKind :: Mac ( ..) => return self . visit_macro_invoc ( ty. id ) ,
280
+ TyKind :: FixedLengthVec ( _, ref length) => self . visit_ast_const_integer ( length) ,
281
+ TyKind :: ImplTrait ( ..) => {
282
+ self . create_def ( ty. id , DefPathData :: ImplTrait ) ;
283
+ }
284
+ _ => { }
266
285
}
267
286
visit:: walk_ty ( self , ty) ;
268
287
}
@@ -274,6 +293,13 @@ impl<'ast> visit::Visitor for DefCollector<'ast> {
274
293
fn visit_macro_def ( & mut self , macro_def : & MacroDef ) {
275
294
self . create_def ( macro_def. id , DefPathData :: MacroDef ( macro_def. ident . name . as_str ( ) ) ) ;
276
295
}
296
+
297
+ fn visit_stmt ( & mut self , stmt : & Stmt ) {
298
+ match stmt. node {
299
+ StmtKind :: Mac ( ..) => self . visit_macro_invoc ( stmt. id ) ,
300
+ _ => visit:: walk_stmt ( self , stmt) ,
301
+ }
302
+ }
277
303
}
278
304
279
305
// We walk the HIR rather than the AST when reading items from metadata.
0 commit comments