@@ -15,23 +15,25 @@ use rustc_data_structures::fx::FxHashMap;
1515use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
1616use rustc_hir:: def_id:: LocalDefId ;
1717use rustc_hir:: * ;
18- use rustc_index:: vec:: IndexVec ;
18+ use rustc_index:: vec:: { Idx , IndexVec } ;
1919use rustc_span:: DUMMY_SP ;
2020use std:: collections:: BTreeMap ;
2121
22- #[ derive( Debug ) ]
23- struct HirOwnerData < ' hir > {
24- signature : Option < & ' hir Owner < ' hir > > ,
25- with_bodies : Option < & ' hir mut OwnerNodes < ' hir > > ,
26- }
27-
22+ /// Result of HIR indexing.
2823#[ derive( Debug ) ]
2924pub struct IndexedHir < ' hir > {
30- map : IndexVec < LocalDefId , HirOwnerData < ' hir > > ,
25+ /// Contents of the HIR owned by each definition. None for definitions that are not HIR owners.
26+ // The `mut` comes from construction time, and is harmless since we only ever hand out
27+ // immutable refs to IndexedHir.
28+ map : IndexVec < LocalDefId , Option < & ' hir mut OwnerNodes < ' hir > > > ,
29+ /// Map from each owner to its parent's HirId inside another owner.
30+ // This map is separate from `map` to eventually allow for per-owner indexing.
3131 parenting : FxHashMap < LocalDefId , HirId > ,
3232}
3333
34- #[ derive( Debug ) ]
34+ /// Top-level HIR node for current owner. This only contains the node for which
35+ /// `HirId::local_id == 0`, and excludes bodies.
36+ #[ derive( Copy , Clone , Debug ) ]
3537pub struct Owner < ' tcx > {
3638 node : Node < ' tcx > ,
3739}
@@ -43,6 +45,9 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Owner<'tcx> {
4345 }
4446}
4547
48+ /// HIR node coupled with its parent's id in the same HIR owner.
49+ ///
50+ /// The parent is trash when the node is a HIR owner.
4651#[ derive( Clone , Debug ) ]
4752pub struct ParentedNode < ' tcx > {
4853 parent : ItemLocalId ,
@@ -51,8 +56,12 @@ pub struct ParentedNode<'tcx> {
5156
5257#[ derive( Debug ) ]
5358pub struct OwnerNodes < ' tcx > {
59+ /// Pre-computed hash of the full HIR.
5460 hash : Fingerprint ,
61+ /// Full HIR for the current owner.
62+ // The zeroth node's parent is trash, but is never accessed.
5563 nodes : IndexVec < ItemLocalId , Option < ParentedNode < ' tcx > > > ,
64+ /// Content of local bodies.
5665 bodies : FxHashMap < ItemLocalId , & ' tcx Body < ' tcx > > ,
5766}
5867
@@ -65,6 +74,8 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for OwnerNodes<'tcx> {
6574 }
6675}
6776
77+ /// Attributes owner by a HIR owner. It is build as a slice inside the attributes map, restricted
78+ /// to the nodes whose `HirId::owner` is `prefix`.
6879#[ derive( Copy , Clone ) ]
6980pub struct AttributeMap < ' tcx > {
7081 map : & ' tcx BTreeMap < HirId , & ' tcx [ Attribute ] > ,
@@ -127,8 +138,12 @@ pub fn provide(providers: &mut Providers) {
127138 providers. index_hir = map:: index_hir;
128139 providers. crate_hash = map:: crate_hash;
129140 providers. hir_module_items = |tcx, id| & tcx. untracked_crate . modules [ & id] ;
130- providers. hir_owner = |tcx, id| tcx. index_hir ( ( ) ) . map [ id] . signature ;
131- providers. hir_owner_nodes = |tcx, id| tcx. index_hir ( ( ) ) . map [ id] . with_bodies . as_deref ( ) ;
141+ providers. hir_owner = |tcx, id| {
142+ let owner = tcx. index_hir ( ( ) ) . map [ id] . as_ref ( ) ?;
143+ let node = owner. nodes [ ItemLocalId :: new ( 0 ) ] . as_ref ( ) ?. node ;
144+ Some ( Owner { node } )
145+ } ;
146+ providers. hir_owner_nodes = |tcx, id| tcx. index_hir ( ( ) ) . map [ id] . as_deref ( ) ;
132147 providers. hir_owner_parent = |tcx, id| {
133148 let index = tcx. index_hir ( ( ) ) ;
134149 index. parenting . get ( & id) . copied ( ) . unwrap_or ( CRATE_HIR_ID )
0 commit comments