1 //! HIR datatypes. See the [rustc dev guide] for more info.
3 //! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
8 use crate::ich::StableHashingContext;
9 use crate::ty::query::Providers;
10 use crate::ty::TyCtxt;
11 use rustc_data_structures::fingerprint::Fingerprint;
12 use rustc_data_structures::fx::FxHashMap;
13 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
14 use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE};
16 use rustc_index::vec::IndexVec;
18 pub struct Owner<'tcx> {
23 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Owner<'tcx> {
24 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
25 let Owner { parent, node } = self;
26 hcx.while_hashing_hir_bodies(false, |hcx| {
27 parent.hash_stable(hcx, hasher);
28 node.hash_stable(hcx, hasher);
34 pub struct ParentedNode<'tcx> {
39 pub struct OwnerNodes<'tcx> {
41 nodes: IndexVec<ItemLocalId, Option<ParentedNode<'tcx>>>,
42 bodies: FxHashMap<ItemLocalId, &'tcx Body<'tcx>>,
45 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for OwnerNodes<'tcx> {
46 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
47 // We ignore the `nodes` and `bodies` fields since these refer to information included in
48 // `hash` which is hashed in the collector and used for the crate hash.
49 let OwnerNodes { hash, nodes: _, bodies: _ } = *self;
50 hash.hash_stable(hcx, hasher);
54 impl<'tcx> TyCtxt<'tcx> {
56 pub fn hir(self) -> map::Map<'tcx> {
57 map::Map { tcx: self }
60 pub fn parent_module(self, id: HirId) -> LocalDefId {
61 self.parent_module_from_def_id(id.owner)
65 pub fn provide(providers: &mut Providers) {
66 providers.parent_module_from_def_id = |tcx, id| {
68 hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id)))
70 providers.hir_crate = |tcx, _| tcx.untracked_crate;
71 providers.index_hir = map::index_hir;
72 providers.hir_module_items = |tcx, id| {
74 let module = hir.as_local_hir_id(id);
75 &tcx.untracked_crate.modules[&module]
77 providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature;
78 providers.hir_owner_nodes = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_deref();
79 providers.fn_arg_names = |tcx, id| {
81 let hir_id = hir.as_local_hir_id(id.expect_local());
82 if let Some(body_id) = hir.maybe_body_owned_by(hir_id) {
83 tcx.arena.alloc_from_iter(hir.body_param_names(body_id))
84 } else if let Node::TraitItem(&TraitItem {
85 kind: TraitItemKind::Fn(_, TraitFn::Required(idents)),
89 tcx.arena.alloc_slice(idents)
91 span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", id);
94 map::provide(providers);