@@ -121,7 +121,7 @@ pub enum Node<'ast> {
121
121
NodeLifetime ( & ' ast Lifetime ) ,
122
122
}
123
123
124
- /// Represents an entry and its parent NodeID.
124
+ /// Represents an entry and its parent Node ID
125
125
/// The odd layout is to bring down the total size.
126
126
#[ derive( Copy , Debug ) ]
127
127
enum MapEntry < ' ast > {
@@ -179,7 +179,7 @@ impl<'ast> MapEntry<'ast> {
179
179
}
180
180
}
181
181
182
- fn parent_node ( self ) -> Option < NodeId > {
182
+ fn parent ( self ) -> Option < NodeId > {
183
183
Some ( match self {
184
184
EntryItem ( id, _) => id,
185
185
EntryForeignItem ( id, _) => id,
@@ -283,88 +283,10 @@ impl<'ast> Map<'ast> {
283
283
self . find_entry ( id) . and_then ( |x| x. to_node ( ) )
284
284
}
285
285
286
- /// Similar to get_parent, returns the parent node id or id if there is no
287
- /// parent.
288
- /// This function returns the immediate parent in the AST, whereas get_parent
289
- /// returns the enclosing item. Note that this might not be the actual parent
290
- /// node in the AST - some kinds of nodes are not in the map and these will
291
- /// never appear as the parent_node. So you can always walk the parent_nodes
292
- /// from a node to the root of the ast (unless you get the same id back here
293
- /// that can happen if the id is not in the map itself or is just weird).
294
- pub fn get_parent_node ( & self , id : NodeId ) -> NodeId {
295
- self . find_entry ( id) . and_then ( |x| x. parent_node ( ) ) . unwrap_or ( id)
296
- }
297
-
298
- /// If there is some error when walking the parents (e.g., a node does not
299
- /// have a parent in the map or a node can't be found), then we return the
300
- /// last good node id we found. Note that reaching the crate root (id == 0),
301
- /// is not an error, since items in the crate module have the crate root as
302
- /// parent.
303
- fn walk_parent_nodes < F > ( & self , start_id : NodeId , found : F ) -> Result < NodeId , NodeId >
304
- where F : Fn ( & Node < ' ast > ) -> bool
305
- {
306
- let mut id = start_id;
307
- loop {
308
- let parent_node = self . get_parent_node ( id) ;
309
- if parent_node == 0 {
310
- return Ok ( 0 ) ;
311
- }
312
- if parent_node == id {
313
- return Err ( id) ;
314
- }
315
-
316
- let node = self . find_entry ( parent_node) ;
317
- if node. is_none ( ) {
318
- return Err ( id) ;
319
- }
320
- let node = node. unwrap ( ) . to_node ( ) ;
321
- match node {
322
- Some ( ref node) => {
323
- if found ( node) {
324
- return Ok ( parent_node) ;
325
- }
326
- }
327
- None => {
328
- return Err ( parent_node) ;
329
- }
330
- }
331
- id = parent_node;
332
- }
333
- }
334
-
335
- /// Retrieve the NodeId for `id`'s parent item, or `id` itself if no
336
- /// parent item is in this map. The "parent item" is the closest parent node
337
- /// in the AST which is recorded by the map and is an item, either an item
338
- /// in a module, trait, or impl.
286
+ /// Retrieve the parent NodeId for `id`, or `id` itself if no
287
+ /// parent is registered in this map.
339
288
pub fn get_parent ( & self , id : NodeId ) -> NodeId {
340
- match self . walk_parent_nodes ( id, |node| match * node {
341
- NodeItem ( _) |
342
- NodeForeignItem ( _) |
343
- NodeTraitItem ( _) |
344
- NodeImplItem ( _) => true ,
345
- _ => false ,
346
- } ) {
347
- Ok ( id) => id,
348
- Err ( id) => id,
349
- }
350
- }
351
-
352
- /// Returns the nearest enclosing scope. A scope is an item or block.
353
- /// FIXME it is not clear to me that all items qualify as scopes - statics
354
- /// and associated types probably shouldn't, for example. Behaviour in this
355
- /// regard should be expected to be highly unstable.
356
- pub fn get_enclosing_scope ( & self , id : NodeId ) -> Option < NodeId > {
357
- match self . walk_parent_nodes ( id, |node| match * node {
358
- NodeItem ( _) |
359
- NodeForeignItem ( _) |
360
- NodeTraitItem ( _) |
361
- NodeImplItem ( _) |
362
- NodeBlock ( _) => true ,
363
- _ => false ,
364
- } ) {
365
- Ok ( id) => Some ( id) ,
366
- Err ( _) => None ,
367
- }
289
+ self . find_entry ( id) . and_then ( |x| x. parent ( ) ) . unwrap_or ( id)
368
290
}
369
291
370
292
pub fn get_parent_did ( & self , id : NodeId ) -> DefId {
@@ -668,15 +590,15 @@ impl<'a, 'ast> Iterator for NodesMatchingSuffix<'a, 'ast> {
668
590
return None ;
669
591
}
670
592
self . idx += 1 ;
671
- let name = match self . map . find_entry ( idx) {
672
- Some ( EntryItem ( _ , n) ) => n. name ( ) ,
673
- Some ( EntryForeignItem ( _ , n) ) => n. name ( ) ,
674
- Some ( EntryTraitItem ( _ , n) ) => n. name ( ) ,
675
- Some ( EntryImplItem ( _ , n) ) => n. name ( ) ,
676
- Some ( EntryVariant ( _ , n) ) => n. name ( ) ,
593
+ let ( p , name) = match self . map . find_entry ( idx) {
594
+ Some ( EntryItem ( p , n) ) => ( p , n. name ( ) ) ,
595
+ Some ( EntryForeignItem ( p , n) ) => ( p , n. name ( ) ) ,
596
+ Some ( EntryTraitItem ( p , n) ) => ( p , n. name ( ) ) ,
597
+ Some ( EntryImplItem ( p , n) ) => ( p , n. name ( ) ) ,
598
+ Some ( EntryVariant ( p , n) ) => ( p , n. name ( ) ) ,
677
599
_ => continue ,
678
600
} ;
679
- if self . matches_names ( self . map . get_parent ( idx ) , name) {
601
+ if self . matches_names ( p , name) {
680
602
return Some ( idx)
681
603
}
682
604
}
@@ -725,7 +647,8 @@ impl<F: FoldOps> Folder for IdAndSpanUpdater<F> {
725
647
/// A Visitor that walks over an AST and collects Node's into an AST Map.
726
648
struct NodeCollector < ' ast > {
727
649
map : Vec < MapEntry < ' ast > > ,
728
- parent_node : NodeId ,
650
+ /// The node in which we are currently mapping (an item or a method).
651
+ parent : NodeId
729
652
}
730
653
731
654
impl < ' ast > NodeCollector < ' ast > {
@@ -739,7 +662,7 @@ impl<'ast> NodeCollector<'ast> {
739
662
}
740
663
741
664
fn insert ( & mut self , id : NodeId , node : Node < ' ast > ) {
742
- let entry = MapEntry :: from_node ( self . parent_node , node) ;
665
+ let entry = MapEntry :: from_node ( self . parent , node) ;
743
666
self . insert_entry ( id, entry) ;
744
667
}
745
668
@@ -753,10 +676,8 @@ impl<'ast> NodeCollector<'ast> {
753
676
impl < ' ast > Visitor < ' ast > for NodeCollector < ' ast > {
754
677
fn visit_item ( & mut self , i : & ' ast Item ) {
755
678
self . insert ( i. id , NodeItem ( i) ) ;
756
-
757
- let parent_node = self . parent_node ;
758
- self . parent_node = i. id ;
759
-
679
+ let parent = self . parent ;
680
+ self . parent = i. id ;
760
681
match i. node {
761
682
ItemImpl ( _, _, _, _, _, ref impl_items) => {
762
683
for ii in impl_items {
@@ -806,23 +727,21 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
806
727
_ => { }
807
728
}
808
729
visit:: walk_item ( self , i) ;
809
- self . parent_node = parent_node ;
730
+ self . parent = parent ;
810
731
}
811
732
812
733
fn visit_trait_item ( & mut self , ti : & ' ast TraitItem ) {
813
- let parent_node = self . parent_node ;
814
- self . parent_node = ti. id ;
734
+ let parent = self . parent ;
735
+ self . parent = ti. id ;
815
736
visit:: walk_trait_item ( self , ti) ;
816
- self . parent_node = parent_node ;
737
+ self . parent = parent ;
817
738
}
818
739
819
740
fn visit_impl_item ( & mut self , ii : & ' ast ImplItem ) {
820
- let parent_node = self . parent_node ;
821
- self . parent_node = ii. id ;
822
-
741
+ let parent = self . parent ;
742
+ self . parent = ii. id ;
823
743
visit:: walk_impl_item ( self , ii) ;
824
-
825
- self . parent_node = parent_node;
744
+ self . parent = parent;
826
745
}
827
746
828
747
fn visit_pat ( & mut self , pat : & ' ast Pat ) {
@@ -831,58 +750,38 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
831
750
PatIdent ( ..) => NodeLocal ( pat) ,
832
751
_ => NodePat ( pat)
833
752
} ) ;
834
-
835
- let parent_node = self . parent_node ;
836
- self . parent_node = pat. id ;
837
753
visit:: walk_pat ( self , pat) ;
838
- self . parent_node = parent_node;
839
754
}
840
755
841
756
fn visit_expr ( & mut self , expr : & ' ast Expr ) {
842
757
self . insert ( expr. id , NodeExpr ( expr) ) ;
843
- let parent_node = self . parent_node ;
844
- self . parent_node = expr. id ;
845
758
visit:: walk_expr ( self , expr) ;
846
- self . parent_node = parent_node;
847
759
}
848
760
849
761
fn visit_stmt ( & mut self , stmt : & ' ast Stmt ) {
850
- let id = ast_util:: stmt_id ( stmt) ;
851
- self . insert ( id, NodeStmt ( stmt) ) ;
852
- let parent_node = self . parent_node ;
853
- self . parent_node = id;
762
+ self . insert ( ast_util:: stmt_id ( stmt) , NodeStmt ( stmt) ) ;
854
763
visit:: walk_stmt ( self , stmt) ;
855
- self . parent_node = parent_node;
856
764
}
857
765
858
766
fn visit_fn ( & mut self , fk : visit:: FnKind < ' ast > , fd : & ' ast FnDecl ,
859
- b : & ' ast Block , s : Span , id : NodeId ) {
860
- let parent_node = self . parent_node ;
861
- self . parent_node = id;
767
+ b : & ' ast Block , s : Span , _: NodeId ) {
862
768
self . visit_fn_decl ( fd) ;
863
769
visit:: walk_fn ( self , fk, fd, b, s) ;
864
- self . parent_node = parent_node;
865
770
}
866
771
867
772
fn visit_ty ( & mut self , ty : & ' ast Ty ) {
868
- let parent_node = self . parent_node ;
869
- self . parent_node = ty. id ;
870
773
match ty. node {
871
774
TyBareFn ( ref fd) => {
872
775
self . visit_fn_decl ( & * fd. decl ) ;
873
776
}
874
777
_ => { }
875
778
}
876
779
visit:: walk_ty ( self , ty) ;
877
- self . parent_node = parent_node;
878
780
}
879
781
880
782
fn visit_block ( & mut self , block : & ' ast Block ) {
881
783
self . insert ( block. id , NodeBlock ( block) ) ;
882
- let parent_node = self . parent_node ;
883
- self . parent_node = block. id ;
884
784
visit:: walk_block ( self , block) ;
885
- self . parent_node = parent_node;
886
785
}
887
786
888
787
fn visit_lifetime_ref ( & mut self , lifetime : & ' ast Lifetime ) {
@@ -910,7 +809,7 @@ pub fn map_crate<'ast, F: FoldOps>(forest: &'ast mut Forest, fold_ops: F) -> Map
910
809
911
810
let mut collector = NodeCollector {
912
811
map : vec ! [ ] ,
913
- parent_node : CRATE_NODE_ID ,
812
+ parent : CRATE_NODE_ID
914
813
} ;
915
814
collector. insert_entry ( CRATE_NODE_ID , RootCrate ) ;
916
815
visit:: walk_crate ( & mut collector, & forest. krate ) ;
@@ -965,11 +864,11 @@ pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
965
864
ii : ii
966
865
} ) ;
967
866
968
- let ii_parent_id = fld. new_id ( DUMMY_NODE_ID ) ;
969
867
let mut collector = NodeCollector {
970
868
map : mem:: replace ( & mut * map. map . borrow_mut ( ) , vec ! [ ] ) ,
971
- parent_node : ii_parent_id ,
869
+ parent : fld . new_id ( DUMMY_NODE_ID )
972
870
} ;
871
+ let ii_parent_id = collector. parent ;
973
872
collector. insert_entry ( ii_parent_id, RootInlinedParent ( ii_parent) ) ;
974
873
visit:: walk_inlined_item ( & mut collector, & ii_parent. ii ) ;
975
874
0 commit comments