@@ -8,14 +8,15 @@ use crate::hir;
88use crate :: hir:: def_id:: { CrateNum , DefId , DefIndex , LOCAL_CRATE , CRATE_DEF_INDEX } ;
99use crate :: ich:: Fingerprint ;
1010use crate :: session:: CrateDisambiguator ;
11- use crate :: util:: nodemap:: NodeMap ;
11+ use crate :: util:: nodemap:: { HirIdMap , NodeMap } ;
1212
1313use rustc_data_structures:: fx:: FxHashMap ;
1414use rustc_data_structures:: indexed_vec:: { IndexVec } ;
1515use rustc_data_structures:: stable_hasher:: StableHasher ;
1616use std:: borrow:: Borrow ;
1717use std:: fmt:: Write ;
1818use std:: hash:: Hash ;
19+ use std:: mem;
1920use syntax:: ast;
2021use syntax:: ext:: hygiene:: ExpnId ;
2122use syntax:: symbol:: { Symbol , sym, InternedString } ;
@@ -92,6 +93,10 @@ impl DefPathTable {
9293pub struct Definitions {
9394 table : DefPathTable ,
9495 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 > ,
95100 def_index_to_node : Vec < ast:: NodeId > ,
96101 pub ( super ) node_to_hir_id : IndexVec < ast:: NodeId , hir:: HirId > ,
97102 /// If `ExpnId` is an ID of some macro expansion,
@@ -360,11 +365,21 @@ impl Definitions {
360365 self . node_to_def_index . get ( & node) . cloned ( )
361366 }
362367
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+
363373 #[ inline]
364374 pub fn opt_local_def_id ( & self , node : ast:: NodeId ) -> Option < DefId > {
365375 self . opt_def_index ( node) . map ( DefId :: local)
366376 }
367377
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+
368383 #[ inline]
369384 pub fn local_def_id ( & self , node : ast:: NodeId ) -> DefId {
370385 self . opt_local_def_id ( node) . unwrap ( )
@@ -440,6 +455,7 @@ impl Definitions {
440455 assert ! ( self . def_index_to_node. is_empty( ) ) ;
441456 self . def_index_to_node . push ( ast:: CRATE_NODE_ID ) ;
442457 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) ;
443459 self . set_invocation_parent ( ExpnId :: root ( ) , root_index) ;
444460
445461 // Allocate some other `DefIndex`es that always must exist.
@@ -452,6 +468,7 @@ impl Definitions {
452468 pub fn create_def_with_parent ( & mut self ,
453469 parent : DefIndex ,
454470 node_id : ast:: NodeId ,
471+ hir_id : Option < hir:: HirId > ,
455472 data : DefPathData ,
456473 expn_id : ExpnId ,
457474 span : Span )
@@ -499,6 +516,12 @@ impl Definitions {
499516 if node_id != ast:: DUMMY_NODE_ID {
500517 debug ! ( "create_def_with_parent: def_index_to_node[{:?} <-> {:?}" , index, node_id) ;
501518 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+ }
502525 }
503526
504527 if expn_id != ExpnId :: root ( ) {
@@ -522,6 +545,15 @@ impl Definitions {
522545 self . node_to_hir_id = mapping;
523546 }
524547
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+
525557 pub fn expansion_that_defined ( & self , index : DefIndex ) -> ExpnId {
526558 self . expansions_that_defined . get ( & index) . cloned ( ) . unwrap_or ( ExpnId :: root ( ) )
527559 }
@@ -611,6 +643,7 @@ macro_rules! define_global_metadata_kind {
611643 definitions. create_def_with_parent(
612644 CRATE_DEF_INDEX ,
613645 ast:: DUMMY_NODE_ID ,
646+ Some ( hir:: DUMMY_HIR_ID ) ,
614647 DefPathData :: GlobalMetaData ( instance. name( ) . as_interned_str( ) ) ,
615648 ExpnId :: root( ) ,
616649 DUMMY_SP
0 commit comments