]> git.lizzy.rs Git - rust.git/blob - src/librustc_middle/hir/mod.rs
485f9b7ce8a6c204e37933ab83e33cfc386dd6b1
[rust.git] / src / librustc_middle / 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
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};
15 use rustc_hir::*;
16 use rustc_index::vec::IndexVec;
17
18 pub struct Owner<'tcx> {
19     parent: HirId,
20     node: Node<'tcx>,
21 }
22
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);
29         });
30     }
31 }
32
33 #[derive(Clone)]
34 pub struct ParentedNode<'tcx> {
35     parent: ItemLocalId,
36     node: Node<'tcx>,
37 }
38
39 pub struct OwnerNodes<'tcx> {
40     hash: Fingerprint,
41     nodes: IndexVec<ItemLocalId, Option<ParentedNode<'tcx>>>,
42     bodies: FxHashMap<ItemLocalId, &'tcx Body<'tcx>>,
43 }
44
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);
51     }
52 }
53
54 impl<'tcx> TyCtxt<'tcx> {
55     #[inline(always)]
56     pub fn hir(self) -> map::Map<'tcx> {
57         map::Map { tcx: self }
58     }
59
60     pub fn parent_module(self, id: HirId) -> LocalDefId {
61         self.parent_module_from_def_id(id.owner)
62     }
63 }
64
65 pub fn provide(providers: &mut Providers) {
66     providers.parent_module_from_def_id = |tcx, id| {
67         let hir = tcx.hir();
68         hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id)))
69     };
70     providers.hir_crate = |tcx, _| tcx.untracked_crate;
71     providers.index_hir = map::index_hir;
72     providers.hir_module_items = |tcx, id| {
73         let hir = tcx.hir();
74         let module = hir.as_local_hir_id(id);
75         &tcx.untracked_crate.modules[&module]
76     };
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| {
80         let hir = tcx.hir();
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)),
86             ..
87         }) = hir.get(hir_id)
88         {
89             tcx.arena.alloc_slice(idents)
90         } else {
91             span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", id);
92         }
93     };
94     map::provide(providers);
95 }