9
9
// except according to those terms.
10
10
11
11
pub use self :: Node :: * ;
12
- pub use self :: PathElem :: * ;
13
12
use self :: MapEntry :: * ;
14
13
use self :: collector:: NodeCollector ;
15
14
pub use self :: definitions:: { Definitions , DefKey , DefPath , DefPathData ,
@@ -25,93 +24,20 @@ use syntax::abi::Abi;
25
24
use syntax:: ast:: { self , Name , NodeId , DUMMY_NODE_ID } ;
26
25
use syntax:: attr:: ThinAttributesExt ;
27
26
use syntax:: codemap:: { Span , Spanned } ;
28
- use syntax:: parse:: token;
29
27
30
28
use hir:: * ;
31
29
use hir:: fold:: Folder ;
32
30
use hir:: print as pprust;
33
31
34
32
use arena:: TypedArena ;
35
33
use std:: cell:: RefCell ;
36
- use std:: fmt;
37
34
use std:: io;
38
- use std:: iter;
39
35
use std:: mem;
40
- use std:: slice;
41
36
42
37
pub mod blocks;
43
38
mod collector;
44
39
pub mod definitions;
45
40
46
- #[ derive( Clone , Copy , PartialEq , Debug ) ]
47
- pub enum PathElem {
48
- PathMod ( Name ) ,
49
- PathName ( Name )
50
- }
51
-
52
- impl PathElem {
53
- pub fn name ( & self ) -> Name {
54
- match * self {
55
- PathMod ( name) | PathName ( name) => name
56
- }
57
- }
58
- }
59
-
60
- impl fmt:: Display for PathElem {
61
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
62
- write ! ( f, "{}" , self . name( ) )
63
- }
64
- }
65
-
66
- #[ derive( Clone ) ]
67
- pub struct LinkedPathNode < ' a > {
68
- node : PathElem ,
69
- next : LinkedPath < ' a > ,
70
- }
71
-
72
- #[ derive( Copy , Clone ) ]
73
- pub struct LinkedPath < ' a > ( Option < & ' a LinkedPathNode < ' a > > ) ;
74
-
75
- impl < ' a > LinkedPath < ' a > {
76
- pub fn empty ( ) -> LinkedPath < ' a > {
77
- LinkedPath ( None )
78
- }
79
-
80
- pub fn from ( node : & ' a LinkedPathNode ) -> LinkedPath < ' a > {
81
- LinkedPath ( Some ( node) )
82
- }
83
- }
84
-
85
- impl < ' a > Iterator for LinkedPath < ' a > {
86
- type Item = PathElem ;
87
-
88
- fn next ( & mut self ) -> Option < PathElem > {
89
- match self . 0 {
90
- Some ( node) => {
91
- * self = node. next ;
92
- Some ( node. node )
93
- }
94
- None => None
95
- }
96
- }
97
- }
98
-
99
- /// The type of the iterator used by with_path.
100
- pub type PathElems < ' a , ' b > = iter:: Chain < iter:: Cloned < slice:: Iter < ' a , PathElem > > , LinkedPath < ' b > > ;
101
-
102
- pub fn path_to_string < PI : Iterator < Item =PathElem > > ( path : PI ) -> String {
103
- let itr = token:: get_ident_interner ( ) ;
104
-
105
- path. fold ( String :: new ( ) , |mut s, e| {
106
- let e = itr. get ( e. name ( ) ) ;
107
- if !s. is_empty ( ) {
108
- s. push_str ( "::" ) ;
109
- }
110
- s. push_str ( & e[ ..] ) ;
111
- s
112
- } )
113
- }
114
-
115
41
#[ derive( Copy , Clone , Debug ) ]
116
42
pub enum Node < ' ast > {
117
43
NodeItem ( & ' ast Item ) ,
@@ -156,7 +82,7 @@ pub enum MapEntry<'ast> {
156
82
157
83
/// Roots for node trees.
158
84
RootCrate ,
159
- RootInlinedParent ( & ' ast InlinedParent )
85
+ RootInlinedParent ( & ' ast InlinedItem )
160
86
}
161
87
162
88
impl < ' ast > Clone for MapEntry < ' ast > {
@@ -165,12 +91,6 @@ impl<'ast> Clone for MapEntry<'ast> {
165
91
}
166
92
}
167
93
168
- #[ derive( Debug ) ]
169
- pub struct InlinedParent {
170
- path : Vec < PathElem > ,
171
- ii : InlinedItem
172
- }
173
-
174
94
impl < ' ast > MapEntry < ' ast > {
175
95
fn from_node ( p : NodeId , node : Node < ' ast > ) -> MapEntry < ' ast > {
176
96
match node {
@@ -233,7 +153,7 @@ impl<'ast> MapEntry<'ast> {
233
153
pub struct Forest {
234
154
krate : Crate ,
235
155
pub dep_graph : DepGraph ,
236
- inlined_items : TypedArena < InlinedParent >
156
+ inlined_items : TypedArena < InlinedItem >
237
157
}
238
158
239
159
impl Forest {
@@ -351,8 +271,10 @@ impl<'ast> Map<'ast> {
351
271
self . definitions . borrow ( ) . def_key ( def_id. index )
352
272
}
353
273
354
- pub fn def_path_from_id ( & self , id : NodeId ) -> DefPath {
355
- self . def_path ( self . local_def_id ( id) )
274
+ pub fn def_path_from_id ( & self , id : NodeId ) -> Option < DefPath > {
275
+ self . opt_local_def_id ( id) . map ( |def_id| {
276
+ self . def_path ( def_id)
277
+ } )
356
278
}
357
279
358
280
pub fn def_path ( & self , def_id : DefId ) -> DefPath {
@@ -551,8 +473,8 @@ impl<'ast> Map<'ast> {
551
473
pub fn get_parent_did ( & self , id : NodeId ) -> DefId {
552
474
let parent = self . get_parent ( id) ;
553
475
match self . find_entry ( parent) {
554
- Some ( RootInlinedParent ( & InlinedParent { ii : II :: TraitItem ( did, _) , .. } ) ) => did ,
555
- Some ( RootInlinedParent ( & InlinedParent { ii : II :: ImplItem ( did, _) , .. } ) ) => did,
476
+ Some ( RootInlinedParent ( & II :: TraitItem ( did, _) ) ) |
477
+ Some ( RootInlinedParent ( & II :: ImplItem ( did, _) ) ) => did,
556
478
_ => self . local_def_id ( parent)
557
479
}
558
480
}
@@ -634,80 +556,21 @@ impl<'ast> Map<'ast> {
634
556
}
635
557
}
636
558
637
- /// returns the name associated with the given NodeId's AST
638
- pub fn get_path_elem ( & self , id : NodeId ) -> PathElem {
639
- let node = self . get ( id) ;
640
- match node {
641
- NodeItem ( item) => {
642
- match item. node {
643
- ItemMod ( _) | ItemForeignMod ( _) => {
644
- PathMod ( item. name )
645
- }
646
- _ => PathName ( item. name )
647
- }
648
- }
649
- NodeForeignItem ( i) => PathName ( i. name ) ,
650
- NodeImplItem ( ii) => PathName ( ii. name ) ,
651
- NodeTraitItem ( ti) => PathName ( ti. name ) ,
652
- NodeVariant ( v) => PathName ( v. node . name ) ,
653
- NodeLifetime ( lt) => PathName ( lt. name ) ,
654
- NodeTyParam ( tp) => PathName ( tp. name ) ,
559
+ /// Returns the name associated with the given NodeId's AST.
560
+ pub fn name ( & self , id : NodeId ) -> Name {
561
+ match self . get ( id) {
562
+ NodeItem ( i) => i. name ,
563
+ NodeForeignItem ( i) => i. name ,
564
+ NodeImplItem ( ii) => ii. name ,
565
+ NodeTraitItem ( ti) => ti. name ,
566
+ NodeVariant ( v) => v. node . name ,
567
+ NodeLifetime ( lt) => lt. name ,
568
+ NodeTyParam ( tp) => tp. name ,
655
569
NodeLocal ( & Pat { node : PatKind :: Ident ( _, l, _) , .. } ) => {
656
- PathName ( l. node . name )
570
+ l. node . name
657
571
} ,
658
- _ => bug ! ( "no path elem for {:?}" , node)
659
- }
660
- }
661
-
662
- pub fn with_path < T , F > ( & self , id : NodeId , f : F ) -> T where
663
- F : FnOnce ( PathElems ) -> T ,
664
- {
665
- self . with_path_next ( id, LinkedPath :: empty ( ) , f)
666
- }
667
-
668
- pub fn path_to_string ( & self , id : NodeId ) -> String {
669
- self . with_path ( id, |path| path_to_string ( path) )
670
- }
671
-
672
- fn path_to_str_with_name ( & self , id : NodeId , name : Name ) -> String {
673
- self . with_path ( id, |path| {
674
- path_to_string ( path. chain ( Some ( PathName ( name) ) ) )
675
- } )
676
- }
677
-
678
- fn with_path_next < T , F > ( & self , id : NodeId , next : LinkedPath , f : F ) -> T where
679
- F : FnOnce ( PathElems ) -> T ,
680
- {
681
- // This function reveals the name of the item and hence is a
682
- // kind of read. This is inefficient, since it walks ancestors
683
- // and we are walking them anyhow, but whatever.
684
- self . read ( id) ;
685
-
686
- let parent = self . get_parent ( id) ;
687
- let parent = match self . find_entry ( id) {
688
- Some ( EntryForeignItem ( ..) ) => {
689
- // Anonymous extern items go in the parent scope.
690
- self . get_parent ( parent)
691
- }
692
- // But tuple struct ctors don't have names, so use the path of its
693
- // parent, the struct item. Similarly with closure expressions.
694
- Some ( EntryStructCtor ( ..) ) | Some ( EntryExpr ( ..) ) => {
695
- return self . with_path_next ( parent, next, f) ;
696
- }
697
- _ => parent
698
- } ;
699
- if parent == id {
700
- match self . find_entry ( id) {
701
- Some ( RootInlinedParent ( data) ) => {
702
- f ( data. path . iter ( ) . cloned ( ) . chain ( next) )
703
- }
704
- _ => f ( [ ] . iter ( ) . cloned ( ) . chain ( next) )
705
- }
706
- } else {
707
- self . with_path_next ( parent, LinkedPath :: from ( & LinkedPathNode {
708
- node : self . get_path_elem ( id) ,
709
- next : next
710
- } ) , f)
572
+ NodeStructCtor ( _) => self . name ( self . get_parent ( id) ) ,
573
+ _ => bug ! ( "no name for {}" , self . node_to_string( id) )
711
574
}
712
575
}
713
576
@@ -958,7 +821,6 @@ pub fn map_crate<'ast>(forest: &'ast mut Forest) -> Map<'ast> {
958
821
/// Used for items loaded from external crate that are being inlined into this
959
822
/// crate.
960
823
pub fn map_decoded_item < ' ast , F : FoldOps > ( map : & Map < ' ast > ,
961
- parent_path : Vec < PathElem > ,
962
824
parent_def_path : DefPath ,
963
825
parent_def_id : DefId ,
964
826
ii : InlinedItem ,
@@ -978,27 +840,24 @@ pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
978
840
II :: Foreign ( i) => II :: Foreign ( i. map ( |i| fld. fold_foreign_item ( i) ) )
979
841
} ;
980
842
981
- let ii_parent = map. forest . inlined_items . alloc ( InlinedParent {
982
- path : parent_path,
983
- ii : ii
984
- } ) ;
843
+ let ii = map. forest . inlined_items . alloc ( ii) ;
985
844
986
845
let ii_parent_id = fld. new_id ( DUMMY_NODE_ID ) ;
987
846
let mut collector =
988
847
NodeCollector :: extend (
989
848
map. krate ( ) ,
990
- ii_parent ,
849
+ ii ,
991
850
ii_parent_id,
992
851
parent_def_path,
993
852
parent_def_id,
994
853
mem:: replace ( & mut * map. map . borrow_mut ( ) , vec ! [ ] ) ,
995
854
mem:: replace ( & mut * map. definitions . borrow_mut ( ) , Definitions :: new ( ) ) ) ;
996
- ii_parent . ii . visit ( & mut collector) ;
855
+ ii. visit ( & mut collector) ;
997
856
998
857
* map. map . borrow_mut ( ) = collector. map ;
999
858
* map. definitions . borrow_mut ( ) = collector. definitions ;
1000
859
1001
- & ii_parent . ii
860
+ ii
1002
861
}
1003
862
1004
863
pub trait NodePrinter {
@@ -1032,9 +891,24 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
1032
891
let id_str = format ! ( " (id={})" , id) ;
1033
892
let id_str = if include_id { & id_str[ ..] } else { "" } ;
1034
893
894
+ let path_str = || {
895
+ // This functionality is used for debugging, try to use TyCtxt to get
896
+ // the user-friendly path, otherwise fall back to stringifying DefPath.
897
+ :: ty:: tls:: with_opt ( |tcx| {
898
+ if let Some ( tcx) = tcx {
899
+ tcx. node_path_str ( id)
900
+ } else if let Some ( path) = map. def_path_from_id ( id) {
901
+ path. data . into_iter ( ) . map ( |elem| {
902
+ elem. data . to_string ( )
903
+ } ) . collect :: < Vec < _ > > ( ) . join ( "::" )
904
+ } else {
905
+ String :: from ( "<missing path>" )
906
+ }
907
+ } )
908
+ } ;
909
+
1035
910
match map. find ( id) {
1036
911
Some ( NodeItem ( item) ) => {
1037
- let path_str = map. path_to_str_with_name ( id, item. name ) ;
1038
912
let item_str = match item. node {
1039
913
ItemExternCrate ( ..) => "extern crate" ,
1040
914
ItemUse ( ..) => "use" ,
@@ -1050,30 +924,21 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
1050
924
ItemImpl ( ..) => "impl" ,
1051
925
ItemDefaultImpl ( ..) => "default impl" ,
1052
926
} ;
1053
- format ! ( "{} {}{}" , item_str, path_str, id_str)
927
+ format ! ( "{} {}{}" , item_str, path_str( ) , id_str)
1054
928
}
1055
- Some ( NodeForeignItem ( item) ) => {
1056
- let path_str = map. path_to_str_with_name ( id, item. name ) ;
1057
- format ! ( "foreign item {}{}" , path_str, id_str)
929
+ Some ( NodeForeignItem ( _) ) => {
930
+ format ! ( "foreign item {}{}" , path_str( ) , id_str)
1058
931
}
1059
932
Some ( NodeImplItem ( ii) ) => {
1060
933
match ii. node {
1061
934
ImplItemKind :: Const ( ..) => {
1062
- format ! ( "assoc const {} in {}{}" ,
1063
- ii. name,
1064
- map. path_to_string( id) ,
1065
- id_str)
935
+ format ! ( "assoc const {} in {}{}" , ii. name, path_str( ) , id_str)
1066
936
}
1067
937
ImplItemKind :: Method ( ..) => {
1068
- format ! ( "method {} in {}{}" ,
1069
- ii. name,
1070
- map. path_to_string( id) , id_str)
938
+ format ! ( "method {} in {}{}" , ii. name, path_str( ) , id_str)
1071
939
}
1072
940
ImplItemKind :: Type ( _) => {
1073
- format ! ( "assoc type {} in {}{}" ,
1074
- ii. name,
1075
- map. path_to_string( id) ,
1076
- id_str)
941
+ format ! ( "assoc type {} in {}{}" , ii. name, path_str( ) , id_str)
1077
942
}
1078
943
}
1079
944
}
@@ -1084,16 +949,12 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
1084
949
TypeTraitItem ( ..) => "assoc type" ,
1085
950
} ;
1086
951
1087
- format ! ( "{} {} in {}{}" ,
1088
- kind,
1089
- ti. name,
1090
- map. path_to_string( id) ,
1091
- id_str)
952
+ format ! ( "{} {} in {}{}" , kind, ti. name, path_str( ) , id_str)
1092
953
}
1093
954
Some ( NodeVariant ( ref variant) ) => {
1094
955
format ! ( "variant {} in {}{}" ,
1095
956
variant. node. name,
1096
- map . path_to_string ( id ) , id_str)
957
+ path_str ( ) , id_str)
1097
958
}
1098
959
Some ( NodeExpr ( ref expr) ) => {
1099
960
format ! ( "expr {}{}" , pprust:: expr_to_string( & expr) , id_str)
@@ -1111,7 +972,7 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
1111
972
format ! ( "block {}{}" , pprust:: block_to_string( & block) , id_str)
1112
973
}
1113
974
Some ( NodeStructCtor ( _) ) => {
1114
- format ! ( "struct_ctor {}{}" , map . path_to_string ( id ) , id_str)
975
+ format ! ( "struct_ctor {}{}" , path_str ( ) , id_str)
1115
976
}
1116
977
Some ( NodeLifetime ( ref l) ) => {
1117
978
format ! ( "lifetime {}{}" ,
0 commit comments