@@ -8,6 +8,7 @@ use crate::dep_graph::{DepGraph, DepKind, DepNode, DepNodeIndex};
8
8
use crate :: hir:: { HirOwner , HirOwnerItems } ;
9
9
use crate :: middle:: cstore:: CrateStoreDyn ;
10
10
use crate :: ty:: query:: Providers ;
11
+ use crate :: ty:: TyCtxt ;
11
12
use rustc_ast:: ast:: { self , Name , NodeId } ;
12
13
use rustc_data_structures:: fx:: FxHashMap ;
13
14
use rustc_data_structures:: svh:: Svh ;
@@ -138,9 +139,30 @@ impl<'hir> Entry<'hir> {
138
139
pub ( super ) type HirEntryMap < ' hir > = IndexVec < DefIndex , IndexVec < ItemLocalId , Option < Entry < ' hir > > > > ;
139
140
140
141
/// Represents a mapping from `NodeId`s to AST elements and their parent `NodeId`s.
141
- #[ derive( Clone ) ]
142
+ pub struct EarlyMap < ' hir > {
143
+ pub krate : & ' hir Crate < ' hir > ,
144
+
145
+ pub dep_graph : DepGraph ,
146
+
147
+ /// The SVH of the local crate.
148
+ pub crate_hash : Svh ,
149
+
150
+ pub ( super ) owner_map : FxHashMap < DefIndex , & ' hir HirOwner < ' hir > > ,
151
+ pub ( super ) owner_items_map : FxHashMap < DefIndex , & ' hir HirOwnerItems < ' hir > > ,
152
+
153
+ pub ( super ) map : HirEntryMap < ' hir > ,
154
+
155
+ pub ( crate ) definitions : & ' hir Definitions ,
156
+
157
+ /// The reverse mapping of `node_to_hir_id`.
158
+ pub ( super ) hir_to_node_id : FxHashMap < HirId , NodeId > ,
159
+ }
160
+
161
+ /// Represents a mapping from `NodeId`s to AST elements and their parent `NodeId`s.
142
162
pub struct Map < ' hir > {
143
- krate : & ' hir Crate < ' hir > ,
163
+ pub ( super ) tcx : TyCtxt < ' hir > ,
164
+
165
+ pub ( super ) krate : & ' hir Crate < ' hir > ,
144
166
145
167
pub dep_graph : DepGraph ,
146
168
@@ -150,12 +172,12 @@ pub struct Map<'hir> {
150
172
pub ( super ) owner_map : FxHashMap < DefIndex , & ' hir HirOwner < ' hir > > ,
151
173
pub ( super ) owner_items_map : FxHashMap < DefIndex , & ' hir HirOwnerItems < ' hir > > ,
152
174
153
- map : HirEntryMap < ' hir > ,
175
+ pub ( super ) map : HirEntryMap < ' hir > ,
154
176
155
- definitions : Definitions ,
177
+ pub ( super ) definitions : & ' hir Definitions ,
156
178
157
179
/// The reverse mapping of `node_to_hir_id`.
158
- hir_to_node_id : FxHashMap < HirId , NodeId > ,
180
+ pub ( super ) hir_to_node_id : FxHashMap < HirId , NodeId > ,
159
181
}
160
182
161
183
/// An iterator that walks up the ancestor tree of a given `HirId`.
@@ -406,11 +428,11 @@ impl<'hir> Map<'hir> {
406
428
}
407
429
408
430
pub fn body ( & self , id : BodyId ) -> & ' hir Body < ' hir > {
409
- self . read ( id . hir_id ) ;
410
-
411
- // N.B., intentionally bypass `self.krate()` so that we
412
- // do not trigger a read of the whole krate here
413
- self . krate . body ( id )
431
+ self . tcx
432
+ . hir_owner_items ( DefId :: local ( id . hir_id . owner ) )
433
+ . bodies
434
+ . get ( & id . hir_id . local_id )
435
+ . unwrap ( )
414
436
}
415
437
416
438
pub fn fn_decl_by_hir_id ( & self , hir_id : HirId ) -> Option < & ' hir FnDecl < ' hir > > {
@@ -966,45 +988,6 @@ impl<'hir> Map<'hir> {
966
988
attrs. unwrap_or ( & [ ] )
967
989
}
968
990
969
- /// Returns an iterator that yields all the hir ids in the map.
970
- fn all_ids < ' a > ( & ' a self ) -> impl Iterator < Item = HirId > + ' a {
971
- // This code is a bit awkward because the map is implemented as 2 levels of arrays,
972
- // see the comment on `HirEntryMap`.
973
- // Iterate over all the indices and return a reference to
974
- // local maps and their index given that they exist.
975
- self . map . iter_enumerated ( ) . flat_map ( move |( owner, local_map) | {
976
- // Iterate over each valid entry in the local map.
977
- local_map. iter_enumerated ( ) . filter_map ( move |( i, entry) | {
978
- entry. map ( move |_| {
979
- // Reconstruct the `HirId` based on the 3 indices we used to find it.
980
- HirId { owner, local_id : i }
981
- } )
982
- } )
983
- } )
984
- }
985
-
986
- /// Returns an iterator that yields the node id's with paths that
987
- /// match `parts`. (Requires `parts` is non-empty.)
988
- ///
989
- /// For example, if given `parts` equal to `["bar", "quux"]`, then
990
- /// the iterator will produce node id's for items with paths
991
- /// such as `foo::bar::quux`, `bar::quux`, `other::bar::quux`, and
992
- /// any other such items it can find in the map.
993
- pub fn nodes_matching_suffix < ' a > (
994
- & ' a self ,
995
- parts : & ' a [ String ] ,
996
- ) -> impl Iterator < Item = NodeId > + ' a {
997
- let nodes = NodesMatchingSuffix {
998
- map : self ,
999
- item_name : parts. last ( ) . unwrap ( ) ,
1000
- in_which : & parts[ ..parts. len ( ) - 1 ] ,
1001
- } ;
1002
-
1003
- self . all_ids ( )
1004
- . filter ( move |hir| nodes. matches_suffix ( * hir) )
1005
- . map ( move |hir| self . hir_to_node_id ( hir) )
1006
- }
1007
-
1008
991
pub fn span ( & self , hir_id : HirId ) -> Span {
1009
992
self . read ( hir_id) ; // reveals span from node
1010
993
match self . find_entry ( hir_id) . map ( |entry| entry. node ) {
@@ -1087,82 +1070,6 @@ impl<'hir> intravisit::Map<'hir> for Map<'hir> {
1087
1070
}
1088
1071
}
1089
1072
1090
- pub struct NodesMatchingSuffix < ' a > {
1091
- map : & ' a Map < ' a > ,
1092
- item_name : & ' a String ,
1093
- in_which : & ' a [ String ] ,
1094
- }
1095
-
1096
- impl < ' a > NodesMatchingSuffix < ' a > {
1097
- /// Returns `true` only if some suffix of the module path for parent
1098
- /// matches `self.in_which`.
1099
- ///
1100
- /// In other words: let `[x_0,x_1,...,x_k]` be `self.in_which`;
1101
- /// returns true if parent's path ends with the suffix
1102
- /// `x_0::x_1::...::x_k`.
1103
- fn suffix_matches ( & self , parent : HirId ) -> bool {
1104
- let mut cursor = parent;
1105
- for part in self . in_which . iter ( ) . rev ( ) {
1106
- let ( mod_id, mod_name) = match find_first_mod_parent ( self . map , cursor) {
1107
- None => return false ,
1108
- Some ( ( node_id, name) ) => ( node_id, name) ,
1109
- } ;
1110
- if mod_name. as_str ( ) != * part {
1111
- return false ;
1112
- }
1113
- cursor = self . map . get_parent_item ( mod_id) ;
1114
- }
1115
- return true ;
1116
-
1117
- // Finds the first mod in parent chain for `id`, along with
1118
- // that mod's name.
1119
- //
1120
- // If `id` itself is a mod named `m` with parent `p`, then
1121
- // returns `Some(id, m, p)`. If `id` has no mod in its parent
1122
- // chain, then returns `None`.
1123
- fn find_first_mod_parent ( map : & Map < ' _ > , mut id : HirId ) -> Option < ( HirId , Name ) > {
1124
- loop {
1125
- if let Node :: Item ( item) = map. find ( id) ? {
1126
- if item_is_mod ( & item) {
1127
- return Some ( ( id, item. ident . name ) ) ;
1128
- }
1129
- }
1130
- let parent = map. get_parent_item ( id) ;
1131
- if parent == id {
1132
- return None ;
1133
- }
1134
- id = parent;
1135
- }
1136
-
1137
- fn item_is_mod ( item : & Item < ' _ > ) -> bool {
1138
- match item. kind {
1139
- ItemKind :: Mod ( _) => true ,
1140
- _ => false ,
1141
- }
1142
- }
1143
- }
1144
- }
1145
-
1146
- // We are looking at some node `n` with a given name and parent
1147
- // id; do their names match what I am seeking?
1148
- fn matches_names ( & self , parent_of_n : HirId , name : Name ) -> bool {
1149
- name. as_str ( ) == * self . item_name && self . suffix_matches ( parent_of_n)
1150
- }
1151
-
1152
- fn matches_suffix ( & self , hir : HirId ) -> bool {
1153
- let name = match self . map . find_entry ( hir) . map ( |entry| entry. node ) {
1154
- Some ( Node :: Item ( n) ) => n. name ( ) ,
1155
- Some ( Node :: ForeignItem ( n) ) => n. name ( ) ,
1156
- Some ( Node :: TraitItem ( n) ) => n. name ( ) ,
1157
- Some ( Node :: ImplItem ( n) ) => n. name ( ) ,
1158
- Some ( Node :: Variant ( n) ) => n. name ( ) ,
1159
- Some ( Node :: Field ( n) ) => n. name ( ) ,
1160
- _ => return false ,
1161
- } ;
1162
- self . matches_names ( self . map . get_parent_item ( hir) , name)
1163
- }
1164
- }
1165
-
1166
1073
trait Named {
1167
1074
fn name ( & self ) -> Name ;
1168
1075
}
@@ -1211,7 +1118,7 @@ pub fn map_crate<'hir>(
1211
1118
krate : & ' hir Crate < ' hir > ,
1212
1119
dep_graph : DepGraph ,
1213
1120
definitions : Definitions ,
1214
- ) -> Map < ' hir > {
1121
+ ) -> EarlyMap < ' hir > {
1215
1122
let _prof_timer = sess. prof . generic_activity ( "build_hir_map" ) ;
1216
1123
1217
1124
// Build the reverse mapping of `node_to_hir_id`.
@@ -1233,15 +1140,15 @@ pub fn map_crate<'hir>(
1233
1140
collector. finalize_and_compute_crate_hash ( crate_disambiguator, cstore, cmdline_args)
1234
1141
} ;
1235
1142
1236
- let map = Map {
1143
+ let map = EarlyMap {
1237
1144
krate,
1238
1145
dep_graph,
1239
1146
crate_hash,
1240
1147
map,
1241
1148
owner_map,
1242
1149
owner_items_map : owner_items_map. into_iter ( ) . map ( |( k, v) | ( k, & * v) ) . collect ( ) ,
1243
1150
hir_to_node_id,
1244
- definitions,
1151
+ definitions : arena . alloc ( definitions ) ,
1245
1152
} ;
1246
1153
1247
1154
sess. time ( "validate_HIR_map" , || {
0 commit comments