@@ -8,14 +8,15 @@ use crate::hir;
8
8
use crate :: hir:: def_id:: { CrateNum , DefId , DefIndex , LOCAL_CRATE , CRATE_DEF_INDEX } ;
9
9
use crate :: ich:: Fingerprint ;
10
10
use crate :: session:: CrateDisambiguator ;
11
- use crate :: util:: nodemap:: NodeMap ;
11
+ use crate :: util:: nodemap:: { HirIdMap , NodeMap } ;
12
12
13
13
use rustc_data_structures:: fx:: FxHashMap ;
14
14
use rustc_data_structures:: indexed_vec:: { IndexVec } ;
15
15
use rustc_data_structures:: stable_hasher:: StableHasher ;
16
16
use std:: borrow:: Borrow ;
17
17
use std:: fmt:: Write ;
18
18
use std:: hash:: Hash ;
19
+ use std:: mem;
19
20
use syntax:: ast;
20
21
use syntax:: ext:: hygiene:: ExpnId ;
21
22
use syntax:: symbol:: { Symbol , sym, InternedString } ;
@@ -92,6 +93,10 @@ impl DefPathTable {
92
93
pub struct Definitions {
93
94
table : DefPathTable ,
94
95
node_to_def_index : NodeMap < DefIndex > ,
96
+ pub hir_to_def_index : HirIdMap < DefIndex > ,
97
+ /// `DefIndex`es created by `DefCollector::create_def` before the AST lowering; used
98
+ /// to complete the `hir_to_def_index` mapping afterwards.
99
+ defs_awaiting_hir_id : NodeMap < DefIndex > ,
95
100
def_index_to_node : Vec < ast:: NodeId > ,
96
101
pub ( super ) node_to_hir_id : IndexVec < ast:: NodeId , hir:: HirId > ,
97
102
/// If `ExpnId` is an ID of some macro expansion,
@@ -360,11 +365,21 @@ impl Definitions {
360
365
self . node_to_def_index . get ( & node) . cloned ( )
361
366
}
362
367
368
+ #[ inline]
369
+ pub fn opt_def_index_from_hir_id ( & self , hir : hir:: HirId ) -> Option < DefIndex > {
370
+ self . hir_to_def_index . get ( & hir) . cloned ( )
371
+ }
372
+
363
373
#[ inline]
364
374
pub fn opt_local_def_id ( & self , node : ast:: NodeId ) -> Option < DefId > {
365
375
self . opt_def_index ( node) . map ( DefId :: local)
366
376
}
367
377
378
+ #[ inline]
379
+ pub fn opt_local_def_id_from_hir_id ( & self , hir : hir:: HirId ) -> Option < DefId > {
380
+ self . opt_def_index_from_hir_id ( hir) . map ( DefId :: local)
381
+ }
382
+
368
383
#[ inline]
369
384
pub fn local_def_id ( & self , node : ast:: NodeId ) -> DefId {
370
385
self . opt_local_def_id ( node) . unwrap ( )
@@ -440,6 +455,7 @@ impl Definitions {
440
455
assert ! ( self . def_index_to_node. is_empty( ) ) ;
441
456
self . def_index_to_node . push ( ast:: CRATE_NODE_ID ) ;
442
457
self . node_to_def_index . insert ( ast:: CRATE_NODE_ID , root_index) ;
458
+ self . hir_to_def_index . insert ( hir:: CRATE_HIR_ID , root_index) ;
443
459
self . set_invocation_parent ( ExpnId :: root ( ) , root_index) ;
444
460
445
461
// Allocate some other `DefIndex`es that always must exist.
@@ -452,6 +468,7 @@ impl Definitions {
452
468
pub fn create_def_with_parent ( & mut self ,
453
469
parent : DefIndex ,
454
470
node_id : ast:: NodeId ,
471
+ hir_id : Option < hir:: HirId > ,
455
472
data : DefPathData ,
456
473
expn_id : ExpnId ,
457
474
span : Span )
@@ -499,6 +516,12 @@ impl Definitions {
499
516
if node_id != ast:: DUMMY_NODE_ID {
500
517
debug ! ( "create_def_with_parent: def_index_to_node[{:?} <-> {:?}" , index, node_id) ;
501
518
self . node_to_def_index . insert ( node_id, index) ;
519
+
520
+ if let Some ( hir_id) = hir_id {
521
+ self . hir_to_def_index . insert ( hir_id, index) ;
522
+ } else {
523
+ self . defs_awaiting_hir_id . insert ( node_id, index) ;
524
+ }
502
525
}
503
526
504
527
if expn_id != ExpnId :: root ( ) {
@@ -522,6 +545,15 @@ impl Definitions {
522
545
self . node_to_hir_id = mapping;
523
546
}
524
547
548
+ /// Fill the missing bits of the `HirId` to `DefIndex` mapping after the AST lowering; see
549
+ /// the related comment to the `defs_awaiting_hir_id` map in the `Definitions` struct.
550
+ pub fn finalize_hir_to_def_index_mapping ( & mut self ) {
551
+ for ( node_id, def_id) in mem:: replace ( & mut self . defs_awaiting_hir_id , Default :: default ( ) ) {
552
+ let hir_id = self . node_to_hir_id [ node_id] ;
553
+ self . hir_to_def_index . insert ( hir_id, def_id) ;
554
+ }
555
+ }
556
+
525
557
pub fn expansion_that_defined ( & self , index : DefIndex ) -> ExpnId {
526
558
self . expansions_that_defined . get ( & index) . cloned ( ) . unwrap_or ( ExpnId :: root ( ) )
527
559
}
@@ -611,6 +643,7 @@ macro_rules! define_global_metadata_kind {
611
643
definitions. create_def_with_parent(
612
644
CRATE_DEF_INDEX ,
613
645
ast:: DUMMY_NODE_ID ,
646
+ Some ( hir:: DUMMY_HIR_ID ) ,
614
647
DefPathData :: GlobalMetaData ( instance. name( ) . as_interned_str( ) ) ,
615
648
ExpnId :: root( ) ,
616
649
DUMMY_SP
0 commit comments