@@ -17,7 +17,6 @@ pub use self::definitions::{Definitions, DefKey, DefPath, DefPathData,
17
17
18
18
use dep_graph:: { DepGraph , DepNode } ;
19
19
20
- use middle:: cstore:: InlinedItem ;
21
20
use hir:: def_id:: { CRATE_DEF_INDEX , DefId , DefIndex } ;
22
21
23
22
use syntax:: abi:: Abi ;
@@ -26,6 +25,7 @@ use syntax::codemap::Spanned;
26
25
use syntax_pos:: Span ;
27
26
28
27
use hir:: * ;
28
+ use hir:: intravisit:: Visitor ;
29
29
use hir:: print as pprust;
30
30
31
31
use arena:: TypedArena ;
@@ -38,6 +38,15 @@ mod collector;
38
38
mod def_collector;
39
39
pub mod definitions;
40
40
41
+ /// The data we save and restore about an inlined item or method. This is not
42
+ /// part of the AST that we parse from a file, but it becomes part of the tree
43
+ /// that we trans.
44
+ #[ derive( Debug ) ]
45
+ struct InlinedItem {
46
+ def_id : DefId ,
47
+ body : Body ,
48
+ }
49
+
41
50
#[ derive( Copy , Clone , Debug ) ]
42
51
pub enum Node < ' ast > {
43
52
NodeItem ( & ' ast Item ) ,
@@ -60,14 +69,12 @@ pub enum Node<'ast> {
60
69
NodeLifetime ( & ' ast Lifetime ) ,
61
70
NodeTyParam ( & ' ast TyParam ) ,
62
71
NodeVisibility ( & ' ast Visibility ) ,
63
-
64
- NodeInlinedItem ( & ' ast InlinedItem ) ,
65
72
}
66
73
67
74
/// Represents an entry and its parent NodeID.
68
75
/// The odd layout is to bring down the total size.
69
76
#[ derive( Copy , Debug ) ]
70
- pub enum MapEntry < ' ast > {
77
+ enum MapEntry < ' ast > {
71
78
/// Placeholder for holes in the map.
72
79
NotPresent ,
73
80
@@ -121,8 +128,6 @@ impl<'ast> MapEntry<'ast> {
121
128
NodeLifetime ( n) => EntryLifetime ( p, n) ,
122
129
NodeTyParam ( n) => EntryTyParam ( p, n) ,
123
130
NodeVisibility ( n) => EntryVisibility ( p, n) ,
124
-
125
- NodeInlinedItem ( n) => RootInlinedParent ( n) ,
126
131
}
127
132
}
128
133
@@ -171,10 +176,49 @@ impl<'ast> MapEntry<'ast> {
171
176
EntryLifetime ( _, n) => NodeLifetime ( n) ,
172
177
EntryTyParam ( _, n) => NodeTyParam ( n) ,
173
178
EntryVisibility ( _, n) => NodeVisibility ( n) ,
174
- RootInlinedParent ( n) => NodeInlinedItem ( n) ,
175
179
_ => return None
176
180
} )
177
181
}
182
+
183
+ fn is_body_owner ( self , node_id : NodeId ) -> bool {
184
+ match self {
185
+ EntryItem ( _, item) => {
186
+ match item. node {
187
+ ItemConst ( _, body) |
188
+ ItemStatic ( .., body) |
189
+ ItemFn ( _, _, _, _, _, body) => body. node_id == node_id,
190
+ _ => false
191
+ }
192
+ }
193
+
194
+ EntryTraitItem ( _, item) => {
195
+ match item. node {
196
+ TraitItemKind :: Const ( _, Some ( body) ) |
197
+ TraitItemKind :: Method ( _, TraitMethod :: Provided ( body) ) => {
198
+ body. node_id == node_id
199
+ }
200
+ _ => false
201
+ }
202
+ }
203
+
204
+ EntryImplItem ( _, item) => {
205
+ match item. node {
206
+ ImplItemKind :: Const ( _, body) |
207
+ ImplItemKind :: Method ( _, body) => body. node_id == node_id,
208
+ _ => false
209
+ }
210
+ }
211
+
212
+ EntryExpr ( _, expr) => {
213
+ match expr. node {
214
+ ExprClosure ( .., body, _) => body. node_id == node_id,
215
+ _ => false
216
+ }
217
+ }
218
+
219
+ _ => false
220
+ }
221
+ }
178
222
}
179
223
180
224
/// Stores a crate and any number of inlined items from other crates.
@@ -250,42 +294,19 @@ impl<'ast> Map<'ast> {
250
294
if !self . is_inlined_node_id ( id) {
251
295
let mut last_expr = None ;
252
296
loop {
253
- match map[ id. as_usize ( ) ] {
254
- EntryItem ( _, item) => {
255
- assert_eq ! ( id, item. id) ;
256
- let def_id = self . local_def_id ( id) ;
257
-
258
- if let Some ( last_id) = last_expr {
259
- // The body of the item may have a separate dep node
260
- if self . is_item_body ( last_id, item) {
261
- return DepNode :: HirBody ( def_id) ;
262
- }
263
- }
264
- return DepNode :: Hir ( def_id) ;
265
- }
266
-
267
- EntryTraitItem ( _, item) => {
268
- let def_id = self . local_def_id ( id) ;
269
-
270
- if let Some ( last_id) = last_expr {
271
- // The body of the item may have a separate dep node
272
- if self . is_trait_item_body ( last_id, item) {
273
- return DepNode :: HirBody ( def_id) ;
274
- }
275
- }
276
- return DepNode :: Hir ( def_id) ;
277
- }
278
-
279
- EntryImplItem ( _, item) => {
280
- let def_id = self . local_def_id ( id) ;
281
-
297
+ let entry = map[ id. as_usize ( ) ] ;
298
+ match entry {
299
+ EntryItem ( ..) |
300
+ EntryTraitItem ( ..) |
301
+ EntryImplItem ( ..) => {
282
302
if let Some ( last_id) = last_expr {
283
- // The body of the item may have a separate dep node
284
- if self . is_impl_item_body ( last_id, item) {
303
+ // The body may have a separate dep node
304
+ if entry. is_body_owner ( last_id) {
305
+ let def_id = self . local_def_id ( id) ;
285
306
return DepNode :: HirBody ( def_id) ;
286
307
}
287
308
}
288
- return DepNode :: Hir ( def_id ) ;
309
+ return DepNode :: Hir ( self . local_def_id ( id ) ) ;
289
310
}
290
311
291
312
EntryVariant ( p, v) => {
@@ -377,33 +398,6 @@ impl<'ast> Map<'ast> {
377
398
}
378
399
}
379
400
380
- fn is_item_body ( & self , node_id : NodeId , item : & Item ) -> bool {
381
- match item. node {
382
- ItemConst ( _, body) |
383
- ItemStatic ( .., body) |
384
- ItemFn ( _, _, _, _, _, body) => body. node_id == node_id,
385
- _ => false
386
- }
387
- }
388
-
389
- fn is_trait_item_body ( & self , node_id : NodeId , item : & TraitItem ) -> bool {
390
- match item. node {
391
- TraitItemKind :: Const ( _, Some ( body) ) |
392
- TraitItemKind :: Method ( _, TraitMethod :: Provided ( body) ) => {
393
- body. node_id == node_id
394
- }
395
- _ => false
396
- }
397
- }
398
-
399
- fn is_impl_item_body ( & self , node_id : NodeId , item : & ImplItem ) -> bool {
400
- match item. node {
401
- ImplItemKind :: Const ( _, body) |
402
- ImplItemKind :: Method ( _, body) => body. node_id == node_id,
403
- _ => false
404
- }
405
- }
406
-
407
401
pub fn num_local_def_ids ( & self ) -> usize {
408
402
self . definitions . len ( )
409
403
}
@@ -483,6 +477,23 @@ impl<'ast> Map<'ast> {
483
477
self . forest . krate . body ( id)
484
478
}
485
479
480
+ /// Returns the `NodeId` that corresponds to the definition of
481
+ /// which this is the body of, i.e. a `fn`, `const` or `static`
482
+ /// item (possibly associated), or a closure, or the body itself
483
+ /// for embedded constant expressions (e.g. `N` in `[T; N]`).
484
+ pub fn body_owner ( & self , BodyId { node_id } : BodyId ) -> NodeId {
485
+ let parent = self . get_parent_node ( node_id) ;
486
+ if self . map . borrow ( ) [ parent. as_usize ( ) ] . is_body_owner ( node_id) {
487
+ parent
488
+ } else {
489
+ node_id
490
+ }
491
+ }
492
+
493
+ pub fn body_owner_def_id ( & self , id : BodyId ) -> DefId {
494
+ self . local_def_id ( self . body_owner ( id) )
495
+ }
496
+
486
497
/// Get the attributes on the krate. This is preferable to
487
498
/// invoking `krate.attrs` because it registers a tighter
488
499
/// dep-graph access.
@@ -726,9 +737,9 @@ impl<'ast> Map<'ast> {
726
737
}
727
738
}
728
739
729
- pub fn expect_inlined_item ( & self , id : NodeId ) -> & ' ast InlinedItem {
740
+ pub fn expect_inlined_body ( & self , id : NodeId ) -> & ' ast Body {
730
741
match self . find_entry ( id) {
731
- Some ( RootInlinedParent ( inlined_item) ) => inlined_item,
742
+ Some ( RootInlinedParent ( inlined_item) ) => & inlined_item. body ,
732
743
_ => bug ! ( "expected inlined item, found {}" , self . node_to_string( id) ) ,
733
744
}
734
745
}
@@ -969,24 +980,28 @@ pub fn map_crate<'ast>(forest: &'ast mut Forest,
969
980
}
970
981
}
971
982
972
- /// Used for items loaded from external crate that are being inlined into this
983
+ /// Used for bodies loaded from external crate that are being inlined into this
973
984
/// crate.
974
- pub fn map_decoded_item < ' ast > ( map : & Map < ' ast > ,
975
- ii : InlinedItem ,
976
- ii_parent_id : NodeId )
977
- -> & ' ast InlinedItem {
985
+ pub fn map_decoded_body < ' ast > ( map : & Map < ' ast > ,
986
+ def_id : DefId ,
987
+ body : Body ,
988
+ parent_id : NodeId )
989
+ -> & ' ast Body {
978
990
let _ignore = map. forest . dep_graph . in_ignore ( ) ;
979
991
980
- let ii = map. forest . inlined_items . alloc ( ii) ;
992
+ let ii = map. forest . inlined_items . alloc ( InlinedItem {
993
+ def_id : def_id,
994
+ body : body
995
+ } ) ;
981
996
982
997
let mut collector = NodeCollector :: extend ( map. krate ( ) ,
983
998
ii,
984
- ii_parent_id ,
999
+ parent_id ,
985
1000
mem:: replace ( & mut * map. map . borrow_mut ( ) , vec ! [ ] ) ) ;
986
- ii . visit ( & mut collector ) ;
1001
+ collector . visit_body ( & ii . body ) ;
987
1002
* map. map . borrow_mut ( ) = collector. map ;
988
1003
989
- ii
1004
+ & ii . body
990
1005
}
991
1006
992
1007
pub trait NodePrinter {
@@ -1016,8 +1031,6 @@ impl<'a> NodePrinter for pprust::State<'a> {
1016
1031
// printing.
1017
1032
NodeLocal ( _) => bug ! ( "cannot print isolated Local" ) ,
1018
1033
NodeStructCtor ( _) => bug ! ( "cannot print isolated StructCtor" ) ,
1019
-
1020
- NodeInlinedItem ( _) => bug ! ( "cannot print inlined item" ) ,
1021
1034
}
1022
1035
}
1023
1036
}
@@ -1131,9 +1144,6 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
1131
1144
Some ( NodeVisibility ( ref vis) ) => {
1132
1145
format ! ( "visibility {:?}{}" , vis, id_str)
1133
1146
}
1134
- Some ( NodeInlinedItem ( _) ) => {
1135
- format ! ( "inlined item {}" , id_str)
1136
- }
1137
1147
None => {
1138
1148
format ! ( "unknown node{}" , id_str)
1139
1149
}
0 commit comments