]> git.lizzy.rs Git - rust.git/blob - src/librustc_hir/stable_hash_impls.rs
Add HIR queries
[rust.git] / src / librustc_hir / stable_hash_impls.rs
1 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2
3 use crate::hir::{
4     BodyId, Expr, ImplItem, ImplItemId, Item, ItemId, Mod, TraitItem, TraitItemId, Ty,
5     VisibilityKind,
6 };
7 use crate::hir_id::HirId;
8
9 /// Requirements for a `StableHashingContext` to be used in this crate.
10 /// This is a hack to allow using the `HashStable_Generic` derive macro
11 /// instead of implementing everything in librustc.
12 pub trait HashStableContext:
13     rustc_ast::HashStableContext + rustc_target::HashStableContext
14 {
15     fn hash_hir_id(&mut self, _: HirId, hasher: &mut StableHasher);
16     fn hash_body_id(&mut self, _: BodyId, hasher: &mut StableHasher);
17     fn hash_reference_to_item(&mut self, _: HirId, hasher: &mut StableHasher);
18     fn hash_hir_mod(&mut self, _: &Mod<'_>, hasher: &mut StableHasher);
19     fn hash_hir_expr(&mut self, _: &Expr<'_>, hasher: &mut StableHasher);
20     fn hash_hir_ty(&mut self, _: &Ty<'_>, hasher: &mut StableHasher);
21     fn hash_hir_visibility_kind(&mut self, _: &VisibilityKind<'_>, hasher: &mut StableHasher);
22     fn hash_hir_item_like<F: FnOnce(&mut Self)>(&mut self, f: F);
23 }
24
25 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HirId {
26     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
27         hcx.hash_hir_id(*self, hasher)
28     }
29 }
30
31 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for BodyId {
32     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
33         hcx.hash_body_id(*self, hasher)
34     }
35 }
36
37 // The following implementations of HashStable for `ItemId`, `TraitItemId`, and
38 // `ImplItemId` deserve special attention. Normally we do not hash `NodeId`s within
39 // the HIR, since they just signify a HIR nodes own path. But `ItemId` et al
40 // are used when another item in the HIR is *referenced* and we certainly
41 // want to pick up on a reference changing its target, so we hash the NodeIds
42 // in "DefPath Mode".
43
44 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ItemId {
45     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
46         hcx.hash_reference_to_item(self.id, hasher)
47     }
48 }
49
50 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItemId {
51     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
52         hcx.hash_reference_to_item(self.hir_id, hasher)
53     }
54 }
55
56 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItemId {
57     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
58         hcx.hash_reference_to_item(self.hir_id, hasher)
59     }
60 }
61
62 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Mod<'_> {
63     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
64         hcx.hash_hir_mod(self, hasher)
65     }
66 }
67
68 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Expr<'_> {
69     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
70         hcx.hash_hir_expr(self, hasher)
71     }
72 }
73
74 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Ty<'_> {
75     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
76         hcx.hash_hir_ty(self, hasher)
77     }
78 }
79
80 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for VisibilityKind<'_> {
81     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
82         hcx.hash_hir_visibility_kind(self, hasher)
83     }
84 }
85
86 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItem<'_> {
87     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
88         let TraitItem { hir_id: _, ident, ref attrs, ref generics, ref kind, span } = *self;
89
90         hcx.hash_hir_item_like(|hcx| {
91             ident.name.hash_stable(hcx, hasher);
92             attrs.hash_stable(hcx, hasher);
93             generics.hash_stable(hcx, hasher);
94             kind.hash_stable(hcx, hasher);
95             span.hash_stable(hcx, hasher);
96         });
97     }
98 }
99
100 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItem<'_> {
101     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
102         let ImplItem {
103             hir_id: _,
104             ident,
105             ref vis,
106             defaultness,
107             ref attrs,
108             ref generics,
109             ref kind,
110             span,
111         } = *self;
112
113         hcx.hash_hir_item_like(|hcx| {
114             ident.name.hash_stable(hcx, hasher);
115             vis.hash_stable(hcx, hasher);
116             defaultness.hash_stable(hcx, hasher);
117             attrs.hash_stable(hcx, hasher);
118             generics.hash_stable(hcx, hasher);
119             kind.hash_stable(hcx, hasher);
120             span.hash_stable(hcx, hasher);
121         });
122     }
123 }
124
125 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Item<'_> {
126     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
127         let Item { ident, ref attrs, hir_id: _, ref kind, ref vis, span } = *self;
128
129         hcx.hash_hir_item_like(|hcx| {
130             ident.name.hash_stable(hcx, hasher);
131             attrs.hash_stable(hcx, hasher);
132             kind.hash_stable(hcx, hasher);
133             vis.hash_stable(hcx, hasher);
134             span.hash_stable(hcx, hasher);
135         });
136     }
137 }