55//! expressions) that are mostly just leftovers.
66
77use rustc_ast:: ast;
8- use rustc_ast:: node_id:: NodeMap ;
98use rustc_data_structures:: fx:: FxHashMap ;
109use rustc_data_structures:: stable_hasher:: StableHasher ;
1110use rustc_hir as hir;
12- use rustc_hir:: def_id:: { CrateNum , DefId , DefIndex , CRATE_DEF_INDEX , LOCAL_CRATE } ;
11+ use rustc_hir:: def_id:: { CrateNum , DefId , DefIndex , LocalDefId , CRATE_DEF_INDEX , LOCAL_CRATE } ;
1312use rustc_index:: vec:: IndexVec ;
1413use rustc_session:: CrateDisambiguator ;
1514use rustc_span:: hygiene:: ExpnId ;
@@ -78,25 +77,29 @@ impl DefPathTable {
7877#[ derive( Clone , Default ) ]
7978pub struct Definitions {
8079 table : DefPathTable ,
81- node_to_def_index : NodeMap < DefIndex > ,
82- def_index_to_node : IndexVec < DefIndex , ast:: NodeId > ,
8380
84- pub ( super ) node_to_hir_id : IndexVec < ast:: NodeId , hir:: HirId > ,
85- /// The reverse mapping of `node_to_hir_id`.
86- pub ( super ) hir_to_node_id : FxHashMap < hir:: HirId , ast:: NodeId > ,
81+ def_id_to_span : IndexVec < LocalDefId , Span > ,
82+
83+ // FIXME(eddyb) don't go through `ast::NodeId` to convert between `HirId`
84+ // and `LocalDefId` - ideally all `LocalDefId`s would be HIR owners.
85+ node_id_to_def_id : FxHashMap < ast:: NodeId , LocalDefId > ,
86+ def_id_to_node_id : IndexVec < LocalDefId , ast:: NodeId > ,
87+
88+ pub ( super ) node_id_to_hir_id : IndexVec < ast:: NodeId , hir:: HirId > ,
89+ /// The reverse mapping of `node_id_to_hir_id`.
90+ pub ( super ) hir_id_to_node_id : FxHashMap < hir:: HirId , ast:: NodeId > ,
8791
8892 /// If `ExpnId` is an ID of some macro expansion,
8993 /// then `DefId` is the normal module (`mod`) in which the expanded macro was defined.
9094 parent_modules_of_macro_defs : FxHashMap < ExpnId , DefId > ,
91- /// Item with a given `DefIndex` was defined during macro expansion with ID `ExpnId`.
92- expansions_that_defined : FxHashMap < DefIndex , ExpnId > ,
93- next_disambiguator : FxHashMap < ( DefIndex , DefPathData ) , u32 > ,
94- def_index_to_span : FxHashMap < DefIndex , Span > ,
95+ /// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`.
96+ expansions_that_defined : FxHashMap < LocalDefId , ExpnId > ,
97+ next_disambiguator : FxHashMap < ( LocalDefId , DefPathData ) , u32 > ,
9598 /// When collecting definitions from an AST fragment produced by a macro invocation `ExpnId`
9699 /// we know what parent node that fragment should be attached to thanks to this table.
97- invocation_parents : FxHashMap < ExpnId , DefIndex > ,
100+ invocation_parents : FxHashMap < ExpnId , LocalDefId > ,
98101 /// Indices of unnamed struct or variant fields with unresolved attributes.
99- placeholder_field_indices : NodeMap < usize > ,
102+ placeholder_field_indices : FxHashMap < ast :: NodeId , usize > ,
100103}
101104
102105/// A unique identifier that we can use to lookup a definition
@@ -296,43 +299,41 @@ impl Definitions {
296299 self . table . index_to_key . len ( )
297300 }
298301
299- pub fn def_key ( & self , index : DefIndex ) -> DefKey {
300- self . table . def_key ( index )
302+ pub fn def_key ( & self , id : LocalDefId ) -> DefKey {
303+ self . table . def_key ( id . local_def_index )
301304 }
302305
303306 #[ inline( always) ]
304- pub fn def_path_hash ( & self , index : DefIndex ) -> DefPathHash {
305- self . table . def_path_hash ( index )
307+ pub fn def_path_hash ( & self , id : LocalDefId ) -> DefPathHash {
308+ self . table . def_path_hash ( id . local_def_index )
306309 }
307310
308311 /// Returns the path from the crate root to `index`. The root
309312 /// nodes are not included in the path (i.e., this will be an
310313 /// empty vector for the crate root). For an inlined item, this
311314 /// will be the path of the item in the external crate (but the
312315 /// path will begin with the path to the external crate).
313- pub fn def_path ( & self , index : DefIndex ) -> DefPath {
314- DefPath :: make ( LOCAL_CRATE , index, |p| self . def_key ( p) )
316+ pub fn def_path ( & self , id : LocalDefId ) -> DefPath {
317+ DefPath :: make ( LOCAL_CRATE , id. local_def_index , |index| {
318+ self . def_key ( LocalDefId { local_def_index : index } )
319+ } )
315320 }
316321
317322 #[ inline]
318- pub fn opt_def_index ( & self , node : ast:: NodeId ) -> Option < DefIndex > {
319- self . node_to_def_index . get ( & node) . copied ( )
320- }
321-
322- #[ inline]
323- pub fn opt_local_def_id ( & self , node : ast:: NodeId ) -> Option < DefId > {
324- self . opt_def_index ( node) . map ( DefId :: local)
323+ pub fn opt_local_def_id ( & self , node : ast:: NodeId ) -> Option < LocalDefId > {
324+ self . node_id_to_def_id . get ( & node) . copied ( )
325325 }
326326
327+ // FIXME(eddyb) this function can and should return `LocalDefId`.
327328 #[ inline]
328329 pub fn local_def_id ( & self , node : ast:: NodeId ) -> DefId {
329- self . opt_local_def_id ( node) . unwrap ( )
330+ self . opt_local_def_id ( node) . unwrap ( ) . to_def_id ( )
330331 }
331332
332333 #[ inline]
333334 pub fn as_local_node_id ( & self , def_id : DefId ) -> Option < ast:: NodeId > {
334- if def_id. krate == LOCAL_CRATE {
335- let node_id = self . def_index_to_node [ def_id. index ] ;
335+ if let Some ( def_id) = def_id . as_local ( ) {
336+ let node_id = self . def_id_to_node_id [ def_id] ;
336337 if node_id != ast:: DUMMY_NODE_ID {
337338 return Some ( node_id) ;
338339 }
@@ -342,47 +343,44 @@ impl Definitions {
342343
343344 #[ inline]
344345 pub fn as_local_hir_id ( & self , def_id : DefId ) -> Option < hir:: HirId > {
345- if def_id. krate == LOCAL_CRATE {
346- let hir_id = self . def_index_to_hir_id ( def_id. index ) ;
346+ if let Some ( def_id) = def_id . as_local ( ) {
347+ let hir_id = self . local_def_id_to_hir_id ( def_id) ;
347348 if hir_id != hir:: DUMMY_HIR_ID { Some ( hir_id) } else { None }
348349 } else {
349350 None
350351 }
351352 }
352353
354+ // FIXME(eddyb) rename to `hir_id_to_node_id`.
353355 #[ inline]
354356 pub fn hir_to_node_id ( & self , hir_id : hir:: HirId ) -> ast:: NodeId {
355- self . hir_to_node_id [ & hir_id]
357+ self . hir_id_to_node_id [ & hir_id]
356358 }
357359
360+ // FIXME(eddyb) rename to `node_id_to_hir_id`.
358361 #[ inline]
359362 pub fn node_to_hir_id ( & self , node_id : ast:: NodeId ) -> hir:: HirId {
360- self . node_to_hir_id [ node_id]
363+ self . node_id_to_hir_id [ node_id]
361364 }
362365
363366 #[ inline]
364- pub fn def_index_to_hir_id ( & self , def_index : DefIndex ) -> hir:: HirId {
365- let node_id = self . def_index_to_node [ def_index ] ;
366- self . node_to_hir_id [ node_id]
367+ pub fn local_def_id_to_hir_id ( & self , id : LocalDefId ) -> hir:: HirId {
368+ let node_id = self . def_id_to_node_id [ id ] ;
369+ self . node_id_to_hir_id [ node_id]
367370 }
368371
369- /// Retrieves the span of the given `DefId` if `DefId` is in the local crate, the span exists
370- /// and it's not `DUMMY_SP`.
372+ /// Retrieves the span of the given `DefId` if `DefId` is in the local crate.
371373 #[ inline]
372374 pub fn opt_span ( & self , def_id : DefId ) -> Option < Span > {
373- if def_id. krate == LOCAL_CRATE {
374- self . def_index_to_span . get ( & def_id. index ) . copied ( )
375- } else {
376- None
377- }
375+ if let Some ( def_id) = def_id. as_local ( ) { Some ( self . def_id_to_span [ def_id] ) } else { None }
378376 }
379377
380378 /// Adds a root definition (no parent) and a few other reserved definitions.
381379 pub fn create_root_def (
382380 & mut self ,
383381 crate_name : & str ,
384382 crate_disambiguator : CrateDisambiguator ,
385- ) -> DefIndex {
383+ ) -> LocalDefId {
386384 let key = DefKey {
387385 parent : None ,
388386 disambiguated_data : DisambiguatedDefPathData {
@@ -395,36 +393,38 @@ impl Definitions {
395393 let def_path_hash = key. compute_stable_hash ( parent_hash) ;
396394
397395 // Create the definition.
398- let root_index = self . table . allocate ( key, def_path_hash) ;
399- assert_eq ! ( root_index, CRATE_DEF_INDEX ) ;
400- assert ! ( self . def_index_to_node. is_empty( ) ) ;
401- self . def_index_to_node . push ( ast:: CRATE_NODE_ID ) ;
402- self . node_to_def_index . insert ( ast:: CRATE_NODE_ID , root_index) ;
403- self . set_invocation_parent ( ExpnId :: root ( ) , root_index) ;
396+ let root = LocalDefId { local_def_index : self . table . allocate ( key, def_path_hash) } ;
397+ assert_eq ! ( root. local_def_index, CRATE_DEF_INDEX ) ;
398+
399+ assert_eq ! ( self . def_id_to_node_id. push( ast:: CRATE_NODE_ID ) , root) ;
400+ assert_eq ! ( self . def_id_to_span. push( rustc_span:: DUMMY_SP ) , root) ;
401+
402+ self . node_id_to_def_id . insert ( ast:: CRATE_NODE_ID , root) ;
403+ self . set_invocation_parent ( ExpnId :: root ( ) , root) ;
404404
405- root_index
405+ root
406406 }
407407
408408 /// Adds a definition with a parent definition.
409409 pub fn create_def_with_parent (
410410 & mut self ,
411- parent : DefIndex ,
411+ parent : LocalDefId ,
412412 node_id : ast:: NodeId ,
413413 data : DefPathData ,
414414 expn_id : ExpnId ,
415415 span : Span ,
416- ) -> DefIndex {
416+ ) -> LocalDefId {
417417 debug ! (
418418 "create_def_with_parent(parent={:?}, node_id={:?}, data={:?})" ,
419419 parent, node_id, data
420420 ) ;
421421
422422 assert ! (
423- !self . node_to_def_index . contains_key( & node_id) ,
423+ !self . node_id_to_def_id . contains_key( & node_id) ,
424424 "adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}" ,
425425 node_id,
426426 data,
427- self . table. def_key( self . node_to_def_index [ & node_id] )
427+ self . table. def_key( self . node_id_to_def_id [ & node_id] . local_def_index ) ,
428428 ) ;
429429
430430 // The root node must be created with `create_root_def()`.
@@ -439,59 +439,55 @@ impl Definitions {
439439 } ;
440440
441441 let key = DefKey {
442- parent : Some ( parent) ,
442+ parent : Some ( parent. local_def_index ) ,
443443 disambiguated_data : DisambiguatedDefPathData { data, disambiguator } ,
444444 } ;
445445
446- let parent_hash = self . table . def_path_hash ( parent) ;
446+ let parent_hash = self . table . def_path_hash ( parent. local_def_index ) ;
447447 let def_path_hash = key. compute_stable_hash ( parent_hash) ;
448448
449449 debug ! ( "create_def_with_parent: after disambiguation, key = {:?}" , key) ;
450450
451451 // Create the definition.
452- let index = self . table . allocate ( key, def_path_hash) ;
453- assert_eq ! ( index. index( ) , self . def_index_to_node. len( ) ) ;
454- self . def_index_to_node . push ( node_id) ;
452+ let def_id = LocalDefId { local_def_index : self . table . allocate ( key, def_path_hash) } ;
455453
456- // Some things for which we allocate `DefIndex`es don't correspond to
454+ assert_eq ! ( self . def_id_to_node_id. push( node_id) , def_id) ;
455+ assert_eq ! ( self . def_id_to_span. push( span) , def_id) ;
456+
457+ // Some things for which we allocate `LocalDefId`s don't correspond to
457458 // anything in the AST, so they don't have a `NodeId`. For these cases
458- // we don't need a mapping from `NodeId` to `DefIndex `.
459+ // we don't need a mapping from `NodeId` to `LocalDefId `.
459460 if node_id != ast:: DUMMY_NODE_ID {
460- debug ! ( "create_def_with_parent: def_index_to_node [{:?} <-> {:?}" , index , node_id) ;
461- self . node_to_def_index . insert ( node_id, index ) ;
461+ debug ! ( "create_def_with_parent: def_id_to_node_id [{:?}] <-> {:?}" , def_id , node_id) ;
462+ self . node_id_to_def_id . insert ( node_id, def_id ) ;
462463 }
463464
464465 if expn_id != ExpnId :: root ( ) {
465- self . expansions_that_defined . insert ( index, expn_id) ;
466- }
467-
468- // The span is added if it isn't dummy.
469- if !span. is_dummy ( ) {
470- self . def_index_to_span . insert ( index, span) ;
466+ self . expansions_that_defined . insert ( def_id, expn_id) ;
471467 }
472468
473- index
469+ def_id
474470 }
475471
476472 /// Initializes the `ast::NodeId` to `HirId` mapping once it has been generated during
477473 /// AST to HIR lowering.
478474 pub fn init_node_id_to_hir_id_mapping ( & mut self , mapping : IndexVec < ast:: NodeId , hir:: HirId > ) {
479475 assert ! (
480- self . node_to_hir_id . is_empty( ) ,
476+ self . node_id_to_hir_id . is_empty( ) ,
481477 "trying to initialize `NodeId` -> `HirId` mapping twice"
482478 ) ;
483- self . node_to_hir_id = mapping;
479+ self . node_id_to_hir_id = mapping;
484480
485- // Build the reverse mapping of `node_to_hir_id `.
486- self . hir_to_node_id = self
487- . node_to_hir_id
481+ // Build the reverse mapping of `node_id_to_hir_id `.
482+ self . hir_id_to_node_id = self
483+ . node_id_to_hir_id
488484 . iter_enumerated ( )
489485 . map ( |( node_id, & hir_id) | ( hir_id, node_id) )
490486 . collect ( ) ;
491487 }
492488
493- pub fn expansion_that_defined ( & self , index : DefIndex ) -> ExpnId {
494- self . expansions_that_defined . get ( & index ) . copied ( ) . unwrap_or ( ExpnId :: root ( ) )
489+ pub fn expansion_that_defined ( & self , id : LocalDefId ) -> ExpnId {
490+ self . expansions_that_defined . get ( & id ) . copied ( ) . unwrap_or ( ExpnId :: root ( ) )
495491 }
496492
497493 pub fn parent_module_of_macro_def ( & self , expn_id : ExpnId ) -> DefId {
@@ -502,13 +498,13 @@ impl Definitions {
502498 self . parent_modules_of_macro_defs . insert ( expn_id, module) ;
503499 }
504500
505- pub fn invocation_parent ( & self , invoc_id : ExpnId ) -> DefIndex {
501+ pub fn invocation_parent ( & self , invoc_id : ExpnId ) -> LocalDefId {
506502 self . invocation_parents [ & invoc_id]
507503 }
508504
509- pub fn set_invocation_parent ( & mut self , invoc_id : ExpnId , parent : DefIndex ) {
505+ pub fn set_invocation_parent ( & mut self , invoc_id : ExpnId , parent : LocalDefId ) {
510506 let old_parent = self . invocation_parents . insert ( invoc_id, parent) ;
511- assert ! ( old_parent. is_none( ) , "parent `DefIndex ` is reset for an invocation" ) ;
507+ assert ! ( old_parent. is_none( ) , "parent `LocalDefId ` is reset for an invocation" ) ;
512508 }
513509
514510 pub fn placeholder_field_index ( & self , node_id : ast:: NodeId ) -> usize {
0 commit comments