]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/hir/mod.rs
Rollup merge of #92630 - steffahn:lift_bounds_on_BuildHasherDefault, r=yaahc
[rust.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 map;
6 pub mod nested_filter;
7 pub mod place;
8
9 use crate::ty::query::Providers;
10 use crate::ty::TyCtxt;
11 use rustc_data_structures::fingerprint::Fingerprint;
12 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
13 use rustc_hir::def_id::LocalDefId;
14 use rustc_hir::*;
15 use rustc_query_system::ich::StableHashingContext;
16 use rustc_span::DUMMY_SP;
17
18 /// Top-level HIR node for current owner. This only contains the node for which
19 /// `HirId::local_id == 0`, and excludes bodies.
20 ///
21 /// This struct exists to encapsulate all access to the hir_owner query in this module, and to
22 /// implement HashStable without hashing bodies.
23 #[derive(Copy, Clone, Debug)]
24 pub struct Owner<'tcx> {
25     node: OwnerNode<'tcx>,
26     hash_without_bodies: Fingerprint,
27 }
28
29 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Owner<'tcx> {
30     #[inline]
31     fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
32         let Owner { node: _, hash_without_bodies } = self;
33         hash_without_bodies.hash_stable(hcx, hasher)
34     }
35 }
36
37 /// Gather the LocalDefId for each item-like within a module, including items contained within
38 /// bodies.  The Ids are in visitor order.  This is used to partition a pass between modules.
39 #[derive(Debug, HashStable)]
40 pub struct ModuleItems {
41     submodules: Box<[LocalDefId]>,
42     items: Box<[ItemId]>,
43     trait_items: Box<[TraitItemId]>,
44     impl_items: Box<[ImplItemId]>,
45     foreign_items: Box<[ForeignItemId]>,
46 }
47
48 impl<'tcx> TyCtxt<'tcx> {
49     #[inline(always)]
50     pub fn hir(self) -> map::Map<'tcx> {
51         map::Map { tcx: self }
52     }
53
54     pub fn parent_module(self, id: HirId) -> LocalDefId {
55         self.parent_module_from_def_id(id.owner)
56     }
57 }
58
59 pub fn provide(providers: &mut Providers) {
60     providers.parent_module_from_def_id = |tcx, id| {
61         let hir = tcx.hir();
62         hir.get_module_parent_node(hir.local_def_id_to_hir_id(id))
63     };
64     providers.hir_crate = |tcx, ()| tcx.untracked_crate;
65     providers.crate_hash = map::crate_hash;
66     providers.hir_module_items = map::hir_module_items;
67     providers.hir_owner = |tcx, id| {
68         let owner = tcx.hir_crate(()).owners.get(id)?.as_ref()?;
69         let node = owner.node();
70         Some(Owner { node, hash_without_bodies: owner.nodes.hash_without_bodies })
71     };
72     providers.hir_owner_nodes = |tcx, id| tcx.hir_crate(()).owners[id].as_ref().map(|i| &i.nodes);
73     providers.hir_owner_parent = |tcx, id| {
74         // Accessing the def_key is ok since its value is hashed as part of `id`'s DefPathHash.
75         let parent = tcx.untracked_resolutions.definitions.def_key(id).parent;
76         let parent = parent.map_or(CRATE_HIR_ID, |local_def_index| {
77             let def_id = LocalDefId { local_def_index };
78             let mut parent_hir_id =
79                 tcx.untracked_resolutions.definitions.local_def_id_to_hir_id(def_id);
80             if let Some(local_id) =
81                 tcx.hir_crate(()).owners[parent_hir_id.owner].as_ref().unwrap().parenting.get(&id)
82             {
83                 parent_hir_id.local_id = *local_id;
84             }
85             parent_hir_id
86         });
87         parent
88     };
89     providers.hir_attrs =
90         |tcx, id| tcx.hir_crate(()).owners[id].as_ref().map_or(AttributeMap::EMPTY, |o| &o.attrs);
91     providers.source_span = |tcx, def_id| tcx.resolutions(()).definitions.def_span(def_id);
92     providers.def_span = |tcx, def_id| tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP);
93     providers.fn_arg_names = |tcx, id| {
94         let hir = tcx.hir();
95         let hir_id = hir.local_def_id_to_hir_id(id.expect_local());
96         if let Some(body_id) = hir.maybe_body_owned_by(hir_id) {
97             tcx.arena.alloc_from_iter(hir.body_param_names(body_id))
98         } else if let Node::TraitItem(&TraitItem {
99             kind: TraitItemKind::Fn(_, TraitFn::Required(idents)),
100             ..
101         }) = hir.get(hir_id)
102         {
103             tcx.arena.alloc_slice(idents)
104         } else {
105             span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", id);
106         }
107     };
108     providers.opt_def_kind = |tcx, def_id| tcx.hir().opt_def_kind(def_id.expect_local());
109     providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
110     providers.expn_that_defined = |tcx, id| {
111         let id = id.expect_local();
112         tcx.resolutions(()).definitions.expansion_that_defined(id)
113     };
114 }