]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_middle/src/hir/mod.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / compiler / rustc_middle / src / hir / mod.rs
1 //! HIR datatypes. See the [rustc dev guide] for more info.
2 //!
3 //! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
4
5 pub mod exports;
6 pub mod map;
7 pub mod place;
8
9 use crate::ich::StableHashingContext;
10 use crate::ty::query::Providers;
11 use crate::ty::TyCtxt;
12 use rustc_data_structures::fingerprint::Fingerprint;
13 use rustc_data_structures::fx::FxHashMap;
14 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
15 use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE};
16 use rustc_hir::*;
17 use rustc_index::vec::IndexVec;
18
19 pub struct Owner<'tcx> {
20 parent: HirId,
21 node: Node<'tcx>,
22 }
23
24 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Owner<'tcx> {
25 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
26 let Owner { parent, node } = self;
27 hcx.while_hashing_hir_bodies(false, |hcx| {
28 parent.hash_stable(hcx, hasher);
29 node.hash_stable(hcx, hasher);
30 });
31 }
32 }
33
34 #[derive(Clone)]
35 pub struct ParentedNode<'tcx> {
36 parent: ItemLocalId,
37 node: Node<'tcx>,
38 }
39
40 pub struct OwnerNodes<'tcx> {
41 hash: Fingerprint,
42 nodes: IndexVec<ItemLocalId, Option<ParentedNode<'tcx>>>,
43 bodies: FxHashMap<ItemLocalId, &'tcx Body<'tcx>>,
44 }
45
46 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for OwnerNodes<'tcx> {
47 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
48 // We ignore the `nodes` and `bodies` fields since these refer to information included in
49 // `hash` which is hashed in the collector and used for the crate hash.
50 let OwnerNodes { hash, nodes: _, bodies: _ } = *self;
51 hash.hash_stable(hcx, hasher);
52 }
53 }
54
55 impl<'tcx> TyCtxt<'tcx> {
56 #[inline(always)]
57 pub fn hir(self) -> map::Map<'tcx> {
58 map::Map { tcx: self }
59 }
60
61 pub fn parent_module(self, id: HirId) -> LocalDefId {
62 self.parent_module_from_def_id(id.owner)
63 }
64 }
65
66 pub fn provide(providers: &mut Providers) {
67 providers.parent_module_from_def_id = |tcx, id| {
68 let hir = tcx.hir();
69 hir.local_def_id(hir.get_module_parent_node(hir.local_def_id_to_hir_id(id)))
70 };
71 providers.hir_crate = |tcx, _| tcx.untracked_crate;
72 providers.index_hir = map::index_hir;
73 providers.hir_module_items = |tcx, id| {
74 let hir = tcx.hir();
75 let module = hir.local_def_id_to_hir_id(id);
76 &tcx.untracked_crate.modules[&module]
77 };
78 providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature;
79 providers.hir_owner_nodes = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_deref();
80 providers.fn_arg_names = |tcx, id| {
81 let hir = tcx.hir();
82 let hir_id = hir.local_def_id_to_hir_id(id.expect_local());
83 if let Some(body_id) = hir.maybe_body_owned_by(hir_id) {
84 tcx.arena.alloc_from_iter(hir.body_param_names(body_id))
85 } else if let Node::TraitItem(&TraitItem {
86 kind: TraitItemKind::Fn(_, TraitFn::Required(idents)),
87 ..
88 }) = hir.get(hir_id)
89 {
90 tcx.arena.alloc_slice(idents)
91 } else {
92 span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", id);
93 }
94 };
95 map::provide(providers);
96 }