Move parenting info to index_hir.
This commit is contained in:
parent
99d3798b6c
commit
18bffdb10e
6 changed files with 54 additions and 42 deletions
|
@ -1,6 +1,6 @@
|
|||
use crate::arena::Arena;
|
||||
use crate::hir::map::{HirOwnerData, Map};
|
||||
use crate::hir::{Owner, OwnerNodes, ParentedNode};
|
||||
use crate::hir::{IndexedHir, Owner, OwnerNodes, ParentedNode};
|
||||
use crate::ich::StableHashingContext;
|
||||
use rustc_data_structures::fingerprint::Fingerprint;
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
|
@ -29,6 +29,7 @@ pub(super) struct NodeCollector<'a, 'hir> {
|
|||
source_map: &'a SourceMap,
|
||||
|
||||
map: IndexVec<LocalDefId, HirOwnerData<'hir>>,
|
||||
parenting: FxHashMap<LocalDefId, HirId>,
|
||||
|
||||
/// The parent of this node
|
||||
parent_node: hir::HirId,
|
||||
|
@ -109,6 +110,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
|
|||
map: (0..definitions.def_index_count())
|
||||
.map(|_| HirOwnerData { signature: None, with_bodies: None })
|
||||
.collect(),
|
||||
parenting: FxHashMap::default(),
|
||||
};
|
||||
collector.insert_entry(
|
||||
hir::CRATE_HIR_ID,
|
||||
|
@ -119,15 +121,13 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
|
|||
collector
|
||||
}
|
||||
|
||||
pub(super) fn finalize_and_compute_crate_hash(
|
||||
mut self,
|
||||
) -> IndexVec<LocalDefId, HirOwnerData<'hir>> {
|
||||
pub(super) fn finalize_and_compute_crate_hash(mut self) -> IndexedHir<'hir> {
|
||||
// Insert bodies into the map
|
||||
for (id, body) in self.krate.bodies.iter() {
|
||||
let bodies = &mut self.map[id.hir_id.owner].with_bodies.as_mut().unwrap().bodies;
|
||||
assert!(bodies.insert(id.hir_id.local_id, body).is_none());
|
||||
}
|
||||
self.map
|
||||
IndexedHir { map: self.map, parenting: self.parenting }
|
||||
}
|
||||
|
||||
fn insert_entry(&mut self, id: HirId, entry: Entry<'hir>, hash: Fingerprint) {
|
||||
|
@ -152,8 +152,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
|
|||
nodes.hash = hash;
|
||||
|
||||
debug_assert!(data.signature.is_none());
|
||||
data.signature =
|
||||
Some(self.arena.alloc(Owner { parent: entry.parent, node: entry.node }));
|
||||
data.signature = Some(self.arena.alloc(Owner { node: entry.node }));
|
||||
|
||||
let dk_parent = self.definitions.def_key(id.owner).parent;
|
||||
if let Some(dk_parent) = dk_parent {
|
||||
|
@ -165,6 +164,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
|
|||
id.owner, dk_parent, entry.parent,
|
||||
)
|
||||
}
|
||||
|
||||
debug_assert_eq!(self.parenting.get(&id.owner), Some(&entry.parent));
|
||||
}
|
||||
} else {
|
||||
assert_eq!(entry.parent.owner, id.owner);
|
||||
|
@ -234,6 +235,22 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
|
|||
f(self, hash);
|
||||
self.current_dep_node_owner = prev_owner;
|
||||
}
|
||||
|
||||
fn insert_nested(&mut self, item: LocalDefId) {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
let dk_parent = self.definitions.def_key(item).parent.unwrap();
|
||||
let dk_parent = LocalDefId { local_def_index: dk_parent };
|
||||
let dk_parent = self.definitions.local_def_id_to_hir_id(dk_parent);
|
||||
debug_assert_eq!(
|
||||
dk_parent.owner, self.parent_node.owner,
|
||||
"Different parents for {:?}",
|
||||
item
|
||||
)
|
||||
}
|
||||
|
||||
assert_eq!(self.parenting.insert(item, self.parent_node), None);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
|
||||
|
@ -249,18 +266,22 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
|
|||
|
||||
fn visit_nested_item(&mut self, item: ItemId) {
|
||||
debug!("visit_nested_item: {:?}", item);
|
||||
self.insert_nested(item.def_id);
|
||||
self.visit_item(self.krate.item(item));
|
||||
}
|
||||
|
||||
fn visit_nested_trait_item(&mut self, item_id: TraitItemId) {
|
||||
self.insert_nested(item_id.def_id);
|
||||
self.visit_trait_item(self.krate.trait_item(item_id));
|
||||
}
|
||||
|
||||
fn visit_nested_impl_item(&mut self, item_id: ImplItemId) {
|
||||
self.insert_nested(item_id.def_id);
|
||||
self.visit_impl_item(self.krate.impl_item(item_id));
|
||||
}
|
||||
|
||||
fn visit_nested_foreign_item(&mut self, foreign_id: ForeignItemId) {
|
||||
self.insert_nested(foreign_id.def_id);
|
||||
self.visit_foreign_item(self.krate.foreign_item(foreign_id));
|
||||
}
|
||||
|
||||
|
@ -448,6 +469,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
|
|||
self.definitions.local_def_id_to_hir_id(LocalDefId { local_def_index })
|
||||
});
|
||||
self.with_parent(parent, |this| {
|
||||
this.insert_nested(macro_def.def_id);
|
||||
this.with_dep_node_owner(macro_def.def_id, macro_def, |this, hash| {
|
||||
this.insert_with_hash(
|
||||
macro_def.span,
|
||||
|
|
|
@ -285,8 +285,8 @@ impl<'hir> Map<'hir> {
|
|||
|
||||
pub fn find_parent_node(&self, id: HirId) -> Option<HirId> {
|
||||
if id.local_id == ItemLocalId::from_u32(0) {
|
||||
let owner = self.tcx.hir_owner(id.owner)?;
|
||||
Some(owner.parent)
|
||||
let index = self.tcx.index_hir(LOCAL_CRATE);
|
||||
index.parenting.get(&id.owner).copied()
|
||||
} else {
|
||||
let owner = self.tcx.hir_owner_nodes(id.owner)?;
|
||||
let node = owner.nodes[id.local_id].as_ref()?;
|
||||
|
@ -296,7 +296,7 @@ impl<'hir> Map<'hir> {
|
|||
}
|
||||
|
||||
pub fn get_parent_node(&self, hir_id: HirId) -> HirId {
|
||||
self.find_parent_node(hir_id).unwrap()
|
||||
self.find_parent_node(hir_id).unwrap_or(CRATE_HIR_ID)
|
||||
}
|
||||
|
||||
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
|
||||
|
@ -934,17 +934,13 @@ pub(super) fn index_hir<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> &'tcx Indexe
|
|||
|
||||
let _prof_timer = tcx.sess.prof.generic_activity("build_hir_map");
|
||||
|
||||
let map = {
|
||||
let hcx = tcx.create_stable_hashing_context();
|
||||
let hcx = tcx.create_stable_hashing_context();
|
||||
let mut collector =
|
||||
NodeCollector::root(tcx.sess, &**tcx.arena, tcx.untracked_crate, &tcx.definitions, hcx);
|
||||
intravisit::walk_crate(&mut collector, tcx.untracked_crate);
|
||||
|
||||
let mut collector =
|
||||
NodeCollector::root(tcx.sess, &**tcx.arena, tcx.untracked_crate, &tcx.definitions, hcx);
|
||||
intravisit::walk_crate(&mut collector, tcx.untracked_crate);
|
||||
|
||||
collector.finalize_and_compute_crate_hash()
|
||||
};
|
||||
|
||||
tcx.arena.alloc(IndexedHir { map })
|
||||
let map = collector.finalize_and_compute_crate_hash();
|
||||
tcx.arena.alloc(map)
|
||||
}
|
||||
|
||||
pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
|
||||
|
|
|
@ -28,21 +28,18 @@ struct HirOwnerData<'hir> {
|
|||
#[derive(Debug)]
|
||||
pub struct IndexedHir<'hir> {
|
||||
map: IndexVec<LocalDefId, HirOwnerData<'hir>>,
|
||||
parenting: FxHashMap<LocalDefId, HirId>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Owner<'tcx> {
|
||||
parent: HirId,
|
||||
node: Node<'tcx>,
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Owner<'tcx> {
|
||||
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
||||
let Owner { node, parent } = self;
|
||||
hcx.while_hashing_hir_bodies(false, |hcx| {
|
||||
parent.hash_stable(hcx, hasher);
|
||||
node.hash_stable(hcx, hasher)
|
||||
});
|
||||
let Owner { node } = self;
|
||||
hcx.while_hashing_hir_bodies(false, |hcx| node.hash_stable(hcx, hasher));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue