]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/hir/mod.rs
Forbid hashing HIR outside of indexing.
[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 exports;
6 pub mod map;
7 pub mod place;
8
9 use crate::ty::query::Providers;
10 use crate::ty::TyCtxt;
11 use rustc_ast::Attribute;
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;
16 use rustc_hir::*;
17 use rustc_index::vec::{Idx, IndexVec};
18 use rustc_query_system::ich::StableHashingContext;
19 use rustc_span::DUMMY_SP;
20 use std::collections::BTreeMap;
21
22 /// Result of HIR indexing.
23 #[derive(Debug)]
24 pub struct IndexedHir<'hir> {
25     /// Contents of the HIR owned by each definition. None for definitions that are not HIR owners.
26     // The `mut` comes from construction time, and is harmless since we only ever hand out
27     // immutable refs to IndexedHir.
28     map: IndexVec<LocalDefId, Option<&'hir mut OwnerNodes<'hir>>>,
29     /// Map from each owner to its parent's HirId inside another owner.
30     // This map is separate from `map` to eventually allow for per-owner indexing.
31     parenting: FxHashMap<LocalDefId, HirId>,
32 }
33
34 /// Top-level HIR node for current owner. This only contains the node for which
35 /// `HirId::local_id == 0`, and excludes bodies.
36 ///
37 /// This struct exists to encapsulate all access to the hir_owner query in this module, and to
38 /// implement HashStable without hashing bodies.
39 #[derive(Copy, Clone, Debug)]
40 pub struct Owner<'tcx> {
41     node: OwnerNode<'tcx>,
42     node_hash: Fingerprint,
43 }
44
45 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Owner<'tcx> {
46     #[inline]
47     fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
48         let Owner { node: _, node_hash } = self;
49         node_hash.hash_stable(hcx, hasher)
50     }
51 }
52
53 /// HIR node coupled with its parent's id in the same HIR owner.
54 ///
55 /// The parent is trash when the node is a HIR owner.
56 #[derive(Clone, Debug)]
57 pub struct ParentedNode<'tcx> {
58     parent: ItemLocalId,
59     node: Node<'tcx>,
60 }
61
62 #[derive(Debug)]
63 pub struct OwnerNodes<'tcx> {
64     /// Pre-computed hash of the full HIR.
65     hash: Fingerprint,
66     /// Pre-computed hash of the top node.
67     node_hash: Fingerprint,
68     /// Full HIR for the current owner.
69     // The zeroth node's parent is trash, but is never accessed.
70     nodes: IndexVec<ItemLocalId, Option<ParentedNode<'tcx>>>,
71     /// Content of local bodies.
72     bodies: &'tcx IndexVec<ItemLocalId, Option<&'tcx Body<'tcx>>>,
73 }
74
75 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for OwnerNodes<'tcx> {
76     #[inline]
77     fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
78         // We ignore the `nodes` and `bodies` fields since these refer to information included in
79         // `hash` which is hashed in the collector and used for the crate hash.
80         let OwnerNodes { hash, node_hash: _, nodes: _, bodies: _ } = *self;
81         hash.hash_stable(hcx, hasher);
82     }
83 }
84
85 /// Attributes owner by a HIR owner.
86 #[derive(Copy, Clone, Debug, HashStable)]
87 pub struct AttributeMap<'tcx> {
88     map: &'tcx BTreeMap<ItemLocalId, &'tcx [Attribute]>,
89 }
90
91 impl<'tcx> AttributeMap<'tcx> {
92     fn new(owner_info: &'tcx Option<OwnerInfo<'tcx>>) -> AttributeMap<'tcx> {
93         const FALLBACK: &'static BTreeMap<ItemLocalId, &'static [Attribute]> = &BTreeMap::new();
94         let map = owner_info.as_ref().map_or(FALLBACK, |info| &info.attrs);
95         AttributeMap { map }
96     }
97
98     fn get(&self, id: ItemLocalId) -> &'tcx [Attribute] {
99         self.map.get(&id).copied().unwrap_or(&[])
100     }
101 }
102
103 /// Gather the LocalDefId for each item-like within a module, including items contained within
104 /// bodies.  The Ids are in visitor order.  This is used to partition a pass between modules.
105 #[derive(Debug, HashStable)]
106 pub struct ModuleItems {
107     submodules: Box<[LocalDefId]>,
108     items: Box<[ItemId]>,
109     trait_items: Box<[TraitItemId]>,
110     impl_items: Box<[ImplItemId]>,
111     foreign_items: Box<[ForeignItemId]>,
112 }
113
114 impl<'tcx> TyCtxt<'tcx> {
115     #[inline(always)]
116     pub fn hir(self) -> map::Map<'tcx> {
117         map::Map { tcx: self }
118     }
119
120     pub fn parent_module(self, id: HirId) -> LocalDefId {
121         self.parent_module_from_def_id(id.owner)
122     }
123 }
124
125 pub fn provide(providers: &mut Providers) {
126     providers.parent_module_from_def_id = |tcx, id| {
127         let hir = tcx.hir();
128         hir.local_def_id(hir.get_module_parent_node(hir.local_def_id_to_hir_id(id)))
129     };
130     providers.hir_crate = |tcx, ()| tcx.untracked_crate;
131     providers.index_hir = map::index_hir;
132     providers.crate_hash = map::crate_hash;
133     providers.hir_module_items = map::hir_module_items;
134     providers.hir_owner = |tcx, id| {
135         let owner = tcx.index_hir(()).map[id].as_ref()?;
136         let node = owner.nodes[ItemLocalId::new(0)].as_ref().unwrap().node;
137         let node = node.as_owner().unwrap(); // Indexing must ensure it is an OwnerNode.
138         Some(Owner { node, node_hash: owner.node_hash })
139     };
140     providers.hir_owner_nodes = |tcx, id| tcx.index_hir(()).map[id].as_deref();
141     providers.hir_owner_parent = |tcx, id| {
142         let index = tcx.index_hir(());
143         index.parenting.get(&id).copied().unwrap_or(CRATE_HIR_ID)
144     };
145     providers.hir_attrs = |tcx, id| AttributeMap::new(&tcx.hir_crate(()).owners[id]);
146     providers.source_span = |tcx, def_id| tcx.resolutions(()).definitions.def_span(def_id);
147     providers.def_span = |tcx, def_id| tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP);
148     providers.fn_arg_names = |tcx, id| {
149         let hir = tcx.hir();
150         let hir_id = hir.local_def_id_to_hir_id(id.expect_local());
151         if let Some(body_id) = hir.maybe_body_owned_by(hir_id) {
152             tcx.arena.alloc_from_iter(hir.body_param_names(body_id))
153         } else if let Node::TraitItem(&TraitItem {
154             kind: TraitItemKind::Fn(_, TraitFn::Required(idents)),
155             ..
156         }) = hir.get(hir_id)
157         {
158             tcx.arena.alloc_slice(idents)
159         } else {
160             span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", id);
161         }
162     };
163     providers.opt_def_kind = |tcx, def_id| tcx.hir().opt_def_kind(def_id.expect_local());
164     providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
165     providers.expn_that_defined = |tcx, id| {
166         let id = id.expect_local();
167         tcx.resolutions(()).definitions.expansion_that_defined(id)
168     };
169 }