2020-03-05 18:07:42 -03:00
|
|
|
//! HIR datatypes. See the [rustc dev guide] for more info.
|
2020-01-02 03:39:11 +01:00
|
|
|
//!
|
2020-03-09 18:33:04 -03:00
|
|
|
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
|
2020-01-02 03:39:11 +01:00
|
|
|
|
2020-01-02 04:53:12 +01:00
|
|
|
pub mod exports;
|
2020-01-02 03:39:11 +01:00
|
|
|
pub mod map;
|
|
|
|
|
2020-02-09 12:13:08 +01:00
|
|
|
use crate::ich::StableHashingContext;
|
2020-01-02 03:39:11 +01:00
|
|
|
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;
|
2020-02-09 12:13:08 +01:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2020-03-18 20:27:59 +02:00
|
|
|
use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE};
|
2020-02-07 11:14:47 +01:00
|
|
|
use rustc_hir::Body;
|
2020-02-11 14:38:16 +01:00
|
|
|
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
|
|
|
|
2020-03-18 03:48:17 +02:00
|
|
|
pub struct Owner<'tcx> {
|
2020-02-07 11:14:47 +01:00
|
|
|
parent: HirId,
|
|
|
|
node: Node<'tcx>,
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:48:17 +02:00
|
|
|
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Owner<'tcx> {
|
2020-02-09 12:13:08 +01:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2020-03-18 03:48:17 +02:00
|
|
|
let Owner { parent, node } = self;
|
2020-02-09 12:13:08 +01:00
|
|
|
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)]
|
2020-03-18 03:48:17 +02:00
|
|
|
pub struct ParentedNode<'tcx> {
|
2020-02-10 14:29:21 +01:00
|
|
|
parent: ItemLocalId,
|
|
|
|
node: Node<'tcx>,
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:48:17 +02:00
|
|
|
pub struct OwnerNodes<'tcx> {
|
2020-02-10 14:29:21 +01:00
|
|
|
hash: Fingerprint,
|
2020-03-18 03:48:17 +02:00
|
|
|
nodes: IndexVec<ItemLocalId, Option<ParentedNode<'tcx>>>,
|
2020-02-07 11:14:47 +01:00
|
|
|
bodies: FxHashMap<ItemLocalId, &'tcx Body<'tcx>>,
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:48:17 +02:00
|
|
|
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for OwnerNodes<'tcx> {
|
2020-02-09 12:13:08 +01:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2020-03-18 03:48:17 +02:00
|
|
|
// 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.
|
2020-03-18 03:48:17 +02:00
|
|
|
let OwnerNodes { hash, nodes: _, bodies: _ } = *self;
|
2020-02-10 14:29:21 +01:00
|
|
|
hash.hash_stable(hcx, hasher);
|
2020-02-09 12:13:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2020-02-11 14:38:16 +01:00
|
|
|
|
2020-03-18 20:27:59 +02:00
|
|
|
pub fn parent_module(self, id: HirId) -> LocalDefId {
|
|
|
|
self.parent_module_from_def_id(id.owner)
|
2020-02-11 14:38:16 +01:00
|
|
|
}
|
2020-02-06 11:59:29 +01:00
|
|
|
}
|
2020-01-02 03:39:11 +01:00
|
|
|
|
|
|
|
pub fn provide(providers: &mut Providers<'_>) {
|
2020-02-11 14:38:16 +01:00
|
|
|
providers.parent_module_from_def_id = |tcx, id| {
|
|
|
|
let hir = tcx.hir();
|
2020-03-18 20:27:59 +02:00
|
|
|
hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id.to_def_id()).unwrap()))
|
|
|
|
.expect_local()
|
2020-02-11 14:38:16 +01:00
|
|
|
};
|
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();
|
2020-03-18 20:27:59 +02:00
|
|
|
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
|
|
|
};
|
2020-03-18 20:27:59 +02:00
|
|
|
providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature.unwrap();
|
2020-03-18 03:48:17 +02:00
|
|
|
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
|
|
|
};
|
2020-01-02 03:39:11 +01:00
|
|
|
map::provide(providers);
|
|
|
|
}
|