1
Fork 0
rust/src/librustc/hir/mod.rs

87 lines
2.8 KiB
Rust
Raw Normal View History

2020-03-05 18:07:42 -03:00
//! HIR datatypes. See the [rustc dev guide] for more info.
//!
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
pub mod exports;
pub mod map;
use crate::ich::StableHashingContext;
use crate::ty::query::Providers;
2020-02-06 11:59:29 +01:00
use crate::ty::TyCtxt;
2020-02-10 14:29:21 +01:00
use rustc_data_structures::fingerprint::Fingerprint;
2020-02-07 11:14:47 +01:00
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE};
2020-02-07 11:14:47 +01:00
use rustc_hir::Body;
use rustc_hir::HirId;
2020-02-07 11:14:47 +01:00
use rustc_hir::ItemLocalId;
use rustc_hir::Node;
use rustc_index::vec::IndexVec;
2020-02-06 11:59:29 +01:00
pub struct Owner<'tcx> {
2020-02-07 11:14:47 +01:00
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 { parent, node } = self;
hcx.while_hashing_hir_bodies(false, |hcx| {
parent.hash_stable(hcx, hasher);
node.hash_stable(hcx, hasher);
});
}
}
2020-02-10 14:29:21 +01:00
#[derive(Clone)]
pub struct ParentedNode<'tcx> {
2020-02-10 14:29:21 +01:00
parent: ItemLocalId,
node: Node<'tcx>,
}
pub struct OwnerNodes<'tcx> {
2020-02-10 14:29:21 +01:00
hash: Fingerprint,
nodes: IndexVec<ItemLocalId, Option<ParentedNode<'tcx>>>,
2020-02-07 11:14:47 +01:00
bodies: FxHashMap<ItemLocalId, &'tcx Body<'tcx>>,
}
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for OwnerNodes<'tcx> {
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
// We ignore the `nodes` and `bodies` fields since these refer to information included in
2020-02-10 14:29:21 +01:00
// `hash` which is hashed in the collector and used for the crate hash.
let OwnerNodes { hash, nodes: _, bodies: _ } = *self;
2020-02-10 14:29:21 +01:00
hash.hash_stable(hcx, hasher);
}
}
2020-02-06 11:59:29 +01:00
impl<'tcx> TyCtxt<'tcx> {
#[inline(always)]
2020-02-09 15:32:00 +01:00
pub fn hir(self) -> map::Map<'tcx> {
map::Map { tcx: self }
2020-02-06 11:59:29 +01:00
}
pub fn parent_module(self, id: HirId) -> LocalDefId {
self.parent_module_from_def_id(id.owner)
}
2020-02-06 11:59:29 +01:00
}
pub fn provide(providers: &mut Providers<'_>) {
providers.parent_module_from_def_id = |tcx, id| {
let hir = tcx.hir();
hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id.to_def_id()).unwrap()))
.expect_local()
};
2020-02-09 15:32:00 +01:00
providers.hir_crate = |tcx, _| tcx.untracked_crate;
providers.index_hir = map::index_hir;
2020-02-07 18:25:36 +01:00
providers.hir_module_items = |tcx, id| {
let hir = tcx.hir();
let module = hir.as_local_hir_id(id.to_def_id()).unwrap();
2020-02-09 15:32:00 +01:00
&tcx.untracked_crate.modules[&module]
2020-02-07 13:13:35 +01:00
};
providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature.unwrap();
providers.hir_owner_nodes = |tcx, id| {
tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_ref().map(|nodes| &**nodes).unwrap()
2020-02-10 17:00:49 +01:00
};
map::provide(providers);
}