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