]>
Commit | Line | Data |
---|---|---|
dfeec247 | 1 | use rustc_data_structures::fx::FxHashMap; |
3c0e092e | 2 | use rustc_data_structures::sorted_map::SortedMap; |
dfeec247 | 3 | use rustc_hir as hir; |
17df50a5 | 4 | use rustc_hir::def_id::LocalDefId; |
17df50a5 | 5 | use rustc_hir::definitions; |
5099ac24 | 6 | use rustc_hir::intravisit::{self, Visitor}; |
dfeec247 | 7 | use rustc_hir::*; |
ba9703b0 | 8 | use rustc_index::vec::{Idx, IndexVec}; |
923072b8 | 9 | use rustc_middle::span_bug; |
17df50a5 | 10 | use rustc_session::Session; |
dfeec247 | 11 | use rustc_span::source_map::SourceMap; |
17df50a5 | 12 | use rustc_span::{Span, DUMMY_SP}; |
b039eaaf | 13 | |
3c0e092e | 14 | use tracing::debug; |
ea8adc8c | 15 | |
dc9dc135 | 16 | /// A visitor that walks over the HIR and collects `Node`s into a HIR map. |
3b2f2976 | 17 | pub(super) struct NodeCollector<'a, 'hir> { |
13cf67c4 XL |
18 | /// Source map |
19 | source_map: &'a SourceMap, | |
3c0e092e | 20 | bodies: &'a SortedMap<ItemLocalId, &'hir Body<'hir>>, |
13cf67c4 | 21 | |
3c0e092e XL |
22 | /// Outputs |
23 | nodes: IndexVec<ItemLocalId, Option<ParentedNode<'hir>>>, | |
24 | parenting: FxHashMap<LocalDefId, ItemLocalId>, | |
ba9703b0 | 25 | |
a7813a04 | 26 | /// The parent of this node |
3c0e092e | 27 | parent_node: hir::ItemLocalId, |
3b2f2976 | 28 | |
3c0e092e | 29 | owner: LocalDefId, |
3b2f2976 | 30 | |
3b2f2976 | 31 | definitions: &'a definitions::Definitions, |
ba9703b0 XL |
32 | } |
33 | ||
04454e1e | 34 | #[tracing::instrument(level = "debug", skip(sess, definitions, bodies))] |
3c0e092e XL |
35 | pub(super) fn index_hir<'hir>( |
36 | sess: &Session, | |
37 | definitions: &definitions::Definitions, | |
38 | item: hir::OwnerNode<'hir>, | |
39 | bodies: &SortedMap<ItemLocalId, &'hir Body<'hir>>, | |
40 | ) -> (IndexVec<ItemLocalId, Option<ParentedNode<'hir>>>, FxHashMap<LocalDefId, ItemLocalId>) { | |
41 | let mut nodes = IndexVec::new(); | |
42 | // This node's parent should never be accessed: the owner's parent is computed by the | |
43 | // hir_owner_parent query. Make it invalid (= ItemLocalId::MAX) to force an ICE whenever it is | |
44 | // used. | |
45 | nodes.push(Some(ParentedNode { parent: ItemLocalId::INVALID, node: item.into() })); | |
46 | let mut collector = NodeCollector { | |
47 | source_map: sess.source_map(), | |
48 | definitions, | |
49 | owner: item.def_id(), | |
50 | parent_node: ItemLocalId::new(0), | |
51 | nodes, | |
52 | bodies, | |
53 | parenting: FxHashMap::default(), | |
54 | }; | |
55 | ||
56 | match item { | |
04454e1e FG |
57 | OwnerNode::Crate(citem) => { |
58 | collector.visit_mod(&citem, citem.spans.inner_span, hir::CRATE_HIR_ID) | |
59 | } | |
3c0e092e XL |
60 | OwnerNode::Item(item) => collector.visit_item(item), |
61 | OwnerNode::TraitItem(item) => collector.visit_trait_item(item), | |
62 | OwnerNode::ImplItem(item) => collector.visit_impl_item(item), | |
63 | OwnerNode::ForeignItem(item) => collector.visit_foreign_item(item), | |
64 | }; | |
65 | ||
66 | (collector.nodes, collector.parenting) | |
b039eaaf SL |
67 | } |
68 | ||
3b2f2976 | 69 | impl<'a, 'hir> NodeCollector<'a, 'hir> { |
04454e1e | 70 | #[tracing::instrument(level = "debug", skip(self))] |
9fa01778 | 71 | fn insert(&mut self, span: Span, hir_id: HirId, node: Node<'hir>) { |
3c0e092e | 72 | debug_assert_eq!(self.owner, hir_id.owner); |
94222f64 | 73 | debug_assert_ne!(hir_id.local_id.as_u32(), 0); |
3b2f2976 XL |
74 | |
75 | // Make sure that the DepNode of some node coincides with the HirId | |
76 | // owner of that node. | |
77 | if cfg!(debug_assertions) { | |
3c0e092e | 78 | if hir_id.owner != self.owner { |
923072b8 FG |
79 | span_bug!( |
80 | span, | |
3c0e092e | 81 | "inconsistent DepNode at `{:?}` for `{:?}`: \ |
ba9703b0 | 82 | current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})", |
17df50a5 | 83 | self.source_map.span_to_diagnostic_string(span), |
3c0e092e XL |
84 | node, |
85 | self.definitions.def_path(self.owner).to_string_no_crate_verbose(), | |
86 | self.owner, | |
1b1a35ee | 87 | self.definitions.def_path(hir_id.owner).to_string_no_crate_verbose(), |
13cf67c4 | 88 | hir_id.owner, |
13cf67c4 | 89 | ) |
3b2f2976 XL |
90 | } |
91 | } | |
92 | ||
3c0e092e | 93 | self.nodes.insert(hir_id.local_id, ParentedNode { parent: self.parent_node, node: node }); |
b039eaaf | 94 | } |
a7813a04 | 95 | |
60c5eb7d | 96 | fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_node_id: HirId, f: F) { |
3c0e092e | 97 | debug_assert_eq!(parent_node_id.owner, self.owner); |
a7813a04 | 98 | let parent_node = self.parent_node; |
3c0e092e | 99 | self.parent_node = parent_node_id.local_id; |
a7813a04 XL |
100 | f(self); |
101 | self.parent_node = parent_node; | |
102 | } | |
3b2f2976 | 103 | |
17df50a5 | 104 | fn insert_nested(&mut self, item: LocalDefId) { |
3c0e092e | 105 | self.parenting.insert(item, self.parent_node); |
17df50a5 | 106 | } |
b039eaaf SL |
107 | } |
108 | ||
3b2f2976 | 109 | impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { |
92a42be0 SL |
110 | /// Because we want to track parent items and so forth, enable |
111 | /// deep walking so that we walk nested items in the context of | |
112 | /// their outer items. | |
476ff2be | 113 | |
92a42be0 | 114 | fn visit_nested_item(&mut self, item: ItemId) { |
7453a54e | 115 | debug!("visit_nested_item: {:?}", item); |
17df50a5 | 116 | self.insert_nested(item.def_id); |
32a655c1 SL |
117 | } |
118 | ||
119 | fn visit_nested_trait_item(&mut self, item_id: TraitItemId) { | |
17df50a5 | 120 | self.insert_nested(item_id.def_id); |
92a42be0 SL |
121 | } |
122 | ||
476ff2be | 123 | fn visit_nested_impl_item(&mut self, item_id: ImplItemId) { |
17df50a5 | 124 | self.insert_nested(item_id.def_id); |
476ff2be SL |
125 | } |
126 | ||
fc512014 | 127 | fn visit_nested_foreign_item(&mut self, foreign_id: ForeignItemId) { |
17df50a5 | 128 | self.insert_nested(foreign_id.def_id); |
fc512014 XL |
129 | } |
130 | ||
32a655c1 | 131 | fn visit_nested_body(&mut self, id: BodyId) { |
3c0e092e XL |
132 | debug_assert_eq!(id.hir_id.owner, self.owner); |
133 | let body = self.bodies[&id.hir_id.local_id]; | |
134 | self.visit_body(body); | |
476ff2be SL |
135 | } |
136 | ||
dfeec247 | 137 | fn visit_param(&mut self, param: &'hir Param<'hir>) { |
e1599b0c XL |
138 | let node = Node::Param(param); |
139 | self.insert(param.pat.span, param.hir_id, node); | |
140 | self.with_parent(param.hir_id, |this| { | |
141 | intravisit::walk_param(this, param); | |
416331ca XL |
142 | }); |
143 | } | |
144 | ||
04454e1e | 145 | #[tracing::instrument(level = "debug", skip(self))] |
dfeec247 | 146 | fn visit_item(&mut self, i: &'hir Item<'hir>) { |
3c0e092e XL |
147 | debug_assert_eq!(i.def_id, self.owner); |
148 | self.with_parent(i.hir_id(), |this| { | |
94222f64 XL |
149 | if let ItemKind::Struct(ref struct_def, _) = i.kind { |
150 | // If this is a tuple or unit-like struct, register the constructor. | |
151 | if let Some(ctor_hir_id) = struct_def.ctor_hir_id() { | |
152 | this.insert(i.span, ctor_hir_id, Node::Ctor(struct_def)); | |
b039eaaf | 153 | } |
94222f64 XL |
154 | } |
155 | intravisit::walk_item(this, i); | |
a7813a04 | 156 | }); |
b039eaaf SL |
157 | } |
158 | ||
04454e1e | 159 | #[tracing::instrument(level = "debug", skip(self))] |
fc512014 | 160 | fn visit_foreign_item(&mut self, fi: &'hir ForeignItem<'hir>) { |
3c0e092e XL |
161 | debug_assert_eq!(fi.def_id, self.owner); |
162 | self.with_parent(fi.hir_id(), |this| { | |
94222f64 | 163 | intravisit::walk_foreign_item(this, fi); |
a7813a04 | 164 | }); |
b039eaaf SL |
165 | } |
166 | ||
dfeec247 | 167 | fn visit_generic_param(&mut self, param: &'hir GenericParam<'hir>) { |
cdc7bbd5 XL |
168 | self.insert(param.span, param.hir_id, Node::GenericParam(param)); |
169 | intravisit::walk_generic_param(self, param); | |
170 | } | |
29967ef6 | 171 | |
cdc7bbd5 | 172 | fn visit_const_param_default(&mut self, param: HirId, ct: &'hir AnonConst) { |
94222f64 XL |
173 | self.with_parent(param, |this| { |
174 | intravisit::walk_const_param_default(this, ct); | |
175 | }) | |
b039eaaf SL |
176 | } |
177 | ||
04454e1e | 178 | #[tracing::instrument(level = "debug", skip(self))] |
dfeec247 | 179 | fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) { |
3c0e092e XL |
180 | debug_assert_eq!(ti.def_id, self.owner); |
181 | self.with_parent(ti.hir_id(), |this| { | |
94222f64 | 182 | intravisit::walk_trait_item(this, ti); |
a7813a04 | 183 | }); |
b039eaaf SL |
184 | } |
185 | ||
04454e1e | 186 | #[tracing::instrument(level = "debug", skip(self))] |
dfeec247 | 187 | fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) { |
3c0e092e XL |
188 | debug_assert_eq!(ii.def_id, self.owner); |
189 | self.with_parent(ii.hir_id(), |this| { | |
94222f64 | 190 | intravisit::walk_impl_item(this, ii); |
a7813a04 | 191 | }); |
b039eaaf SL |
192 | } |
193 | ||
dfeec247 | 194 | fn visit_pat(&mut self, pat: &'hir Pat<'hir>) { |
064997fb | 195 | self.insert(pat.span, pat.hir_id, Node::Pat(pat)); |
b039eaaf | 196 | |
9fa01778 | 197 | self.with_parent(pat.hir_id, |this| { |
a7813a04 XL |
198 | intravisit::walk_pat(this, pat); |
199 | }); | |
b039eaaf SL |
200 | } |
201 | ||
dfeec247 | 202 | fn visit_arm(&mut self, arm: &'hir Arm<'hir>) { |
dc9dc135 XL |
203 | let node = Node::Arm(arm); |
204 | ||
205 | self.insert(arm.span, arm.hir_id, node); | |
206 | ||
207 | self.with_parent(arm.hir_id, |this| { | |
208 | intravisit::walk_arm(this, arm); | |
209 | }); | |
210 | } | |
211 | ||
94b46f34 | 212 | fn visit_anon_const(&mut self, constant: &'hir AnonConst) { |
9fa01778 | 213 | self.insert(DUMMY_SP, constant.hir_id, Node::AnonConst(constant)); |
94b46f34 | 214 | |
9fa01778 | 215 | self.with_parent(constant.hir_id, |this| { |
94b46f34 XL |
216 | intravisit::walk_anon_const(this, constant); |
217 | }); | |
218 | } | |
219 | ||
dfeec247 | 220 | fn visit_expr(&mut self, expr: &'hir Expr<'hir>) { |
9fa01778 | 221 | self.insert(expr.span, expr.hir_id, Node::Expr(expr)); |
b039eaaf | 222 | |
9fa01778 | 223 | self.with_parent(expr.hir_id, |this| { |
a7813a04 XL |
224 | intravisit::walk_expr(this, expr); |
225 | }); | |
b039eaaf SL |
226 | } |
227 | ||
dfeec247 | 228 | fn visit_stmt(&mut self, stmt: &'hir Stmt<'hir>) { |
9fa01778 | 229 | self.insert(stmt.span, stmt.hir_id, Node::Stmt(stmt)); |
a7813a04 | 230 | |
9fa01778 | 231 | self.with_parent(stmt.hir_id, |this| { |
a7813a04 XL |
232 | intravisit::walk_stmt(this, stmt); |
233 | }); | |
b039eaaf SL |
234 | } |
235 | ||
dfeec247 | 236 | fn visit_path_segment(&mut self, path_span: Span, path_segment: &'hir PathSegment<'hir>) { |
9fa01778 XL |
237 | if let Some(hir_id) = path_segment.hir_id { |
238 | self.insert(path_span, hir_id, Node::PathSegment(path_segment)); | |
13cf67c4 XL |
239 | } |
240 | intravisit::walk_path_segment(self, path_span, path_segment); | |
241 | } | |
242 | ||
dfeec247 | 243 | fn visit_ty(&mut self, ty: &'hir Ty<'hir>) { |
9fa01778 | 244 | self.insert(ty.span, ty.hir_id, Node::Ty(ty)); |
5bcae85e | 245 | |
9fa01778 | 246 | self.with_parent(ty.hir_id, |this| { |
5bcae85e SL |
247 | intravisit::walk_ty(this, ty); |
248 | }); | |
249 | } | |
250 | ||
94222f64 XL |
251 | fn visit_infer(&mut self, inf: &'hir InferArg) { |
252 | self.insert(inf.span, inf.hir_id, Node::Infer(inf)); | |
253 | ||
254 | self.with_parent(inf.hir_id, |this| { | |
255 | intravisit::walk_inf(this, inf); | |
256 | }); | |
257 | } | |
258 | ||
dfeec247 | 259 | fn visit_trait_ref(&mut self, tr: &'hir TraitRef<'hir>) { |
9fa01778 | 260 | self.insert(tr.path.span, tr.hir_ref_id, Node::TraitRef(tr)); |
476ff2be | 261 | |
9fa01778 | 262 | self.with_parent(tr.hir_ref_id, |this| { |
476ff2be SL |
263 | intravisit::walk_trait_ref(this, tr); |
264 | }); | |
265 | } | |
266 | ||
60c5eb7d XL |
267 | fn visit_fn( |
268 | &mut self, | |
269 | fk: intravisit::FnKind<'hir>, | |
dfeec247 | 270 | fd: &'hir FnDecl<'hir>, |
60c5eb7d XL |
271 | b: BodyId, |
272 | s: Span, | |
273 | id: HirId, | |
274 | ) { | |
3c0e092e XL |
275 | assert_eq!(self.owner, id.owner); |
276 | assert_eq!(self.parent_node, id.local_id); | |
5bcae85e | 277 | intravisit::walk_fn(self, fk, fd, b, s, id); |
b039eaaf SL |
278 | } |
279 | ||
dfeec247 | 280 | fn visit_block(&mut self, block: &'hir Block<'hir>) { |
9fa01778 XL |
281 | self.insert(block.span, block.hir_id, Node::Block(block)); |
282 | self.with_parent(block.hir_id, |this| { | |
a7813a04 XL |
283 | intravisit::walk_block(this, block); |
284 | }); | |
b039eaaf SL |
285 | } |
286 | ||
dfeec247 | 287 | fn visit_local(&mut self, l: &'hir Local<'hir>) { |
9fa01778 | 288 | self.insert(l.span, l.hir_id, Node::Local(l)); |
94222f64 XL |
289 | self.with_parent(l.hir_id, |this| { |
290 | intravisit::walk_local(this, l); | |
291 | }) | |
3b2f2976 XL |
292 | } |
293 | ||
32a655c1 | 294 | fn visit_lifetime(&mut self, lifetime: &'hir Lifetime) { |
9fa01778 | 295 | self.insert(lifetime.span, lifetime.hir_id, Node::Lifetime(lifetime)); |
b039eaaf | 296 | } |
476ff2be | 297 | |
dfeec247 | 298 | fn visit_variant(&mut self, v: &'hir Variant<'hir>, g: &'hir Generics<'hir>, item_id: HirId) { |
e1599b0c XL |
299 | self.insert(v.span, v.id, Node::Variant(v)); |
300 | self.with_parent(v.id, |this| { | |
532ac7d7 | 301 | // Register the constructor of this variant. |
e1599b0c XL |
302 | if let Some(ctor_hir_id) = v.data.ctor_hir_id() { |
303 | this.insert(v.span, ctor_hir_id, Node::Ctor(&v.data)); | |
532ac7d7 | 304 | } |
32a655c1 SL |
305 | intravisit::walk_variant(this, v, g, item_id); |
306 | }); | |
307 | } | |
308 | ||
6a06907d | 309 | fn visit_field_def(&mut self, field: &'hir FieldDef<'hir>) { |
9fa01778 XL |
310 | self.insert(field.span, field.hir_id, Node::Field(field)); |
311 | self.with_parent(field.hir_id, |this| { | |
6a06907d | 312 | intravisit::walk_field_def(this, field); |
476ff2be SL |
313 | }); |
314 | } | |
3b2f2976 | 315 | |
923072b8 FG |
316 | fn visit_assoc_type_binding(&mut self, type_binding: &'hir TypeBinding<'hir>) { |
317 | self.insert(type_binding.span, type_binding.hir_id, Node::TypeBinding(type_binding)); | |
318 | self.with_parent(type_binding.hir_id, |this| { | |
319 | intravisit::walk_assoc_type_binding(this, type_binding) | |
320 | }) | |
321 | } | |
322 | ||
3b2f2976 XL |
323 | fn visit_trait_item_ref(&mut self, ii: &'hir TraitItemRef) { |
324 | // Do not visit the duplicate information in TraitItemRef. We want to | |
325 | // map the actual nodes, not the duplicate ones in the *Ref. | |
064997fb | 326 | let TraitItemRef { id, ident: _, kind: _, span: _ } = *ii; |
3b2f2976 XL |
327 | |
328 | self.visit_nested_trait_item(id); | |
329 | } | |
330 | ||
c295e0f8 | 331 | fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef) { |
3b2f2976 XL |
332 | // Do not visit the duplicate information in ImplItemRef. We want to |
333 | // map the actual nodes, not the duplicate ones in the *Ref. | |
064997fb | 334 | let ImplItemRef { id, ident: _, kind: _, span: _, trait_item_def_id: _ } = *ii; |
3b2f2976 XL |
335 | |
336 | self.visit_nested_impl_item(id); | |
337 | } | |
fc512014 | 338 | |
c295e0f8 | 339 | fn visit_foreign_item_ref(&mut self, fi: &'hir ForeignItemRef) { |
fc512014 XL |
340 | // Do not visit the duplicate information in ForeignItemRef. We want to |
341 | // map the actual nodes, not the duplicate ones in the *Ref. | |
c295e0f8 | 342 | let ForeignItemRef { id, ident: _, span: _ } = *fi; |
fc512014 XL |
343 | |
344 | self.visit_nested_foreign_item(id); | |
345 | } | |
b039eaaf | 346 | } |