5
5
//! expressions) that are mostly just leftovers.
6
6
7
7
use rustc_ast:: ast;
8
- use rustc_ast:: node_id:: NodeMap ;
9
8
use rustc_data_structures:: fx:: FxHashMap ;
10
9
use rustc_data_structures:: stable_hasher:: StableHasher ;
11
10
use 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 } ;
13
12
use rustc_index:: vec:: IndexVec ;
14
13
use rustc_session:: CrateDisambiguator ;
15
14
use rustc_span:: hygiene:: ExpnId ;
@@ -78,25 +77,29 @@ impl DefPathTable {
78
77
#[ derive( Clone , Default ) ]
79
78
pub struct Definitions {
80
79
table : DefPathTable ,
81
- node_to_def_index : NodeMap < DefIndex > ,
82
- def_index_to_node : IndexVec < DefIndex , ast:: NodeId > ,
83
80
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 > ,
87
91
88
92
/// If `ExpnId` is an ID of some macro expansion,
89
93
/// then `DefId` is the normal module (`mod`) in which the expanded macro was defined.
90
94
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 > ,
95
98
/// When collecting definitions from an AST fragment produced by a macro invocation `ExpnId`
96
99
/// 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 > ,
98
101
/// Indices of unnamed struct or variant fields with unresolved attributes.
99
- placeholder_field_indices : NodeMap < usize > ,
102
+ placeholder_field_indices : FxHashMap < ast :: NodeId , usize > ,
100
103
}
101
104
102
105
/// A unique identifier that we can use to lookup a definition
@@ -296,43 +299,41 @@ impl Definitions {
296
299
self . table . index_to_key . len ( )
297
300
}
298
301
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 )
301
304
}
302
305
303
306
#[ 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 )
306
309
}
307
310
308
311
/// Returns the path from the crate root to `index`. The root
309
312
/// nodes are not included in the path (i.e., this will be an
310
313
/// empty vector for the crate root). For an inlined item, this
311
314
/// will be the path of the item in the external crate (but the
312
315
/// 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
+ } )
315
320
}
316
321
317
322
#[ 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 ( )
325
325
}
326
326
327
+ // FIXME(eddyb) this function can and should return `LocalDefId`.
327
328
#[ inline]
328
329
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 ( )
330
331
}
331
332
332
333
#[ inline]
333
334
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] ;
336
337
if node_id != ast:: DUMMY_NODE_ID {
337
338
return Some ( node_id) ;
338
339
}
@@ -342,47 +343,44 @@ impl Definitions {
342
343
343
344
#[ inline]
344
345
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) ;
347
348
if hir_id != hir:: DUMMY_HIR_ID { Some ( hir_id) } else { None }
348
349
} else {
349
350
None
350
351
}
351
352
}
352
353
354
+ // FIXME(eddyb) rename to `hir_id_to_node_id`.
353
355
#[ inline]
354
356
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]
356
358
}
357
359
360
+ // FIXME(eddyb) rename to `node_id_to_hir_id`.
358
361
#[ inline]
359
362
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]
361
364
}
362
365
363
366
#[ 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]
367
370
}
368
371
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.
371
373
#[ inline]
372
374
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 }
378
376
}
379
377
380
378
/// Adds a root definition (no parent) and a few other reserved definitions.
381
379
pub fn create_root_def (
382
380
& mut self ,
383
381
crate_name : & str ,
384
382
crate_disambiguator : CrateDisambiguator ,
385
- ) -> DefIndex {
383
+ ) -> LocalDefId {
386
384
let key = DefKey {
387
385
parent : None ,
388
386
disambiguated_data : DisambiguatedDefPathData {
@@ -395,36 +393,38 @@ impl Definitions {
395
393
let def_path_hash = key. compute_stable_hash ( parent_hash) ;
396
394
397
395
// 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) ;
404
404
405
- root_index
405
+ root
406
406
}
407
407
408
408
/// Adds a definition with a parent definition.
409
409
pub fn create_def_with_parent (
410
410
& mut self ,
411
- parent : DefIndex ,
411
+ parent : LocalDefId ,
412
412
node_id : ast:: NodeId ,
413
413
data : DefPathData ,
414
414
expn_id : ExpnId ,
415
415
span : Span ,
416
- ) -> DefIndex {
416
+ ) -> LocalDefId {
417
417
debug ! (
418
418
"create_def_with_parent(parent={:?}, node_id={:?}, data={:?})" ,
419
419
parent, node_id, data
420
420
) ;
421
421
422
422
assert ! (
423
- !self . node_to_def_index . contains_key( & node_id) ,
423
+ !self . node_id_to_def_id . contains_key( & node_id) ,
424
424
"adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}" ,
425
425
node_id,
426
426
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 ) ,
428
428
) ;
429
429
430
430
// The root node must be created with `create_root_def()`.
@@ -439,59 +439,55 @@ impl Definitions {
439
439
} ;
440
440
441
441
let key = DefKey {
442
- parent : Some ( parent) ,
442
+ parent : Some ( parent. local_def_index ) ,
443
443
disambiguated_data : DisambiguatedDefPathData { data, disambiguator } ,
444
444
} ;
445
445
446
- let parent_hash = self . table . def_path_hash ( parent) ;
446
+ let parent_hash = self . table . def_path_hash ( parent. local_def_index ) ;
447
447
let def_path_hash = key. compute_stable_hash ( parent_hash) ;
448
448
449
449
debug ! ( "create_def_with_parent: after disambiguation, key = {:?}" , key) ;
450
450
451
451
// 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) } ;
455
453
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
457
458
// 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 `.
459
460
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 ) ;
462
463
}
463
464
464
465
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) ;
471
467
}
472
468
473
- index
469
+ def_id
474
470
}
475
471
476
472
/// Initializes the `ast::NodeId` to `HirId` mapping once it has been generated during
477
473
/// AST to HIR lowering.
478
474
pub fn init_node_id_to_hir_id_mapping ( & mut self , mapping : IndexVec < ast:: NodeId , hir:: HirId > ) {
479
475
assert ! (
480
- self . node_to_hir_id . is_empty( ) ,
476
+ self . node_id_to_hir_id . is_empty( ) ,
481
477
"trying to initialize `NodeId` -> `HirId` mapping twice"
482
478
) ;
483
- self . node_to_hir_id = mapping;
479
+ self . node_id_to_hir_id = mapping;
484
480
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
488
484
. iter_enumerated ( )
489
485
. map ( |( node_id, & hir_id) | ( hir_id, node_id) )
490
486
. collect ( ) ;
491
487
}
492
488
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 ( ) )
495
491
}
496
492
497
493
pub fn parent_module_of_macro_def ( & self , expn_id : ExpnId ) -> DefId {
@@ -502,13 +498,13 @@ impl Definitions {
502
498
self . parent_modules_of_macro_defs . insert ( expn_id, module) ;
503
499
}
504
500
505
- pub fn invocation_parent ( & self , invoc_id : ExpnId ) -> DefIndex {
501
+ pub fn invocation_parent ( & self , invoc_id : ExpnId ) -> LocalDefId {
506
502
self . invocation_parents [ & invoc_id]
507
503
}
508
504
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 ) {
510
506
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" ) ;
512
508
}
513
509
514
510
pub fn placeholder_field_index ( & self , node_id : ast:: NodeId ) -> usize {
0 commit comments