-
Notifications
You must be signed in to change notification settings - Fork 13.1k
/
Copy pathmod.rs
155 lines (137 loc) · 5.27 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//! HIR datatypes. See the [rustc dev guide] for more info.
//!
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
pub mod exports;
pub mod map;
pub mod place;
use crate::ich::StableHashingContext;
use crate::ty::query::Providers;
use crate::ty::TyCtxt;
use rustc_ast::Attribute;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hir::def_id::LocalDefId;
use rustc_hir::*;
use rustc_index::vec::IndexVec;
use rustc_span::DUMMY_SP;
use std::collections::BTreeMap;
#[derive(Debug)]
struct HirOwnerData<'hir> {
signature: Option<&'hir Owner<'hir>>,
with_bodies: Option<&'hir mut OwnerNodes<'hir>>,
}
#[derive(Debug)]
pub struct IndexedHir<'hir> {
map: IndexVec<LocalDefId, HirOwnerData<'hir>>,
parenting: FxHashMap<LocalDefId, HirId>,
}
#[derive(Debug)]
pub struct Owner<'tcx> {
node: Node<'tcx>,
}
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Owner<'tcx> {
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
let Owner { node } = self;
hcx.while_hashing_hir_bodies(false, |hcx| node.hash_stable(hcx, hasher));
}
}
#[derive(Clone, Debug)]
pub struct ParentedNode<'tcx> {
parent: ItemLocalId,
node: Node<'tcx>,
}
#[derive(Debug)]
pub struct OwnerNodes<'tcx> {
hash: Fingerprint,
nodes: IndexVec<ItemLocalId, Option<ParentedNode<'tcx>>>,
bodies: FxHashMap<ItemLocalId, &'tcx Body<'tcx>>,
}
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for OwnerNodes<'tcx> {
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
// We ignore the `nodes` and `bodies` fields since these refer to information included in
// `hash` which is hashed in the collector and used for the crate hash.
let OwnerNodes { hash, nodes: _, bodies: _ } = *self;
hash.hash_stable(hcx, hasher);
}
}
#[derive(Copy, Clone)]
pub struct AttributeMap<'tcx> {
map: &'tcx BTreeMap<HirId, &'tcx [Attribute]>,
prefix: LocalDefId,
}
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for AttributeMap<'tcx> {
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
let range = self.range();
range.clone().count().hash_stable(hcx, hasher);
for (key, value) in range {
key.hash_stable(hcx, hasher);
value.hash_stable(hcx, hasher);
}
}
}
impl<'tcx> std::fmt::Debug for AttributeMap<'tcx> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AttributeMap")
.field("prefix", &self.prefix)
.field("range", &&self.range().collect::<Vec<_>>()[..])
.finish()
}
}
impl<'tcx> AttributeMap<'tcx> {
fn get(&self, id: ItemLocalId) -> &'tcx [Attribute] {
self.map.get(&HirId { owner: self.prefix, local_id: id }).copied().unwrap_or(&[])
}
fn range(&self) -> std::collections::btree_map::Range<'_, rustc_hir::HirId, &[Attribute]> {
let local_zero = ItemLocalId::from_u32(0);
let range = HirId { owner: self.prefix, local_id: local_zero }..HirId {
owner: LocalDefId { local_def_index: self.prefix.local_def_index + 1 },
local_id: local_zero,
};
self.map.range(range)
}
}
impl<'tcx> TyCtxt<'tcx> {
#[inline(always)]
pub fn hir(self) -> map::Map<'tcx> {
map::Map { tcx: self }
}
pub fn parent_module(self, id: HirId) -> LocalDefId {
self.parent_module_from_def_id(id.owner)
}
}
pub fn provide(providers: &mut Providers) {
providers.parent_module_from_def_id = |tcx, id| {
let hir = tcx.hir();
hir.local_def_id(hir.get_module_parent_node(hir.local_def_id_to_hir_id(id)))
};
providers.hir_crate = |tcx, ()| tcx.untracked_crate;
providers.index_hir = map::index_hir;
providers.crate_hash = map::crate_hash;
providers.hir_module_items = |tcx, id| &tcx.untracked_crate.modules[&id];
providers.hir_owner = |tcx, id| tcx.index_hir(()).map[id].signature;
providers.hir_owner_nodes = |tcx, id| tcx.index_hir(()).map[id].with_bodies.as_deref();
providers.hir_owner_parent = |tcx, id| {
let index = tcx.index_hir(());
index.parenting.get(&id).copied().unwrap_or(CRATE_HIR_ID)
};
providers.hir_attrs = |tcx, id| AttributeMap { map: &tcx.untracked_crate.attrs, prefix: id };
providers.def_span = |tcx, def_id| tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP);
providers.fn_arg_names = |tcx, id| {
let hir = tcx.hir();
let hir_id = hir.local_def_id_to_hir_id(id.expect_local());
if let Some(body_id) = hir.maybe_body_owned_by(hir_id) {
tcx.arena.alloc_from_iter(hir.body_param_names(body_id))
} else if let Node::TraitItem(&TraitItem {
kind: TraitItemKind::Fn(_, TraitFn::Required(idents)),
..
}) = hir.get(hir_id)
{
tcx.arena.alloc_slice(idents)
} else {
span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", id);
}
};
providers.opt_def_kind = |tcx, def_id| tcx.hir().opt_def_kind(def_id.expect_local());
providers.all_local_trait_impls = |tcx, ()| &tcx.hir_crate(()).trait_impls;
}