1 use rustc_data_structures::fx::FxHashMap;
3 use rustc_hir::def_id::LocalDefId;
4 use rustc_hir::definitions;
5 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
7 use rustc_index::vec::{Idx, IndexVec};
8 use rustc_session::Session;
9 use rustc_span::source_map::SourceMap;
10 use rustc_span::{Span, DUMMY_SP};
12 use std::iter::repeat;
15 /// A visitor that walks over the HIR and collects `Node`s into a HIR map.
16 pub(super) struct NodeCollector<'a, 'hir> {
18 source_map: &'a SourceMap,
19 bodies: &'a IndexVec<ItemLocalId, Option<&'hir Body<'hir>>>,
22 nodes: IndexVec<ItemLocalId, Option<ParentedNode<'hir>>>,
23 parenting: FxHashMap<LocalDefId, ItemLocalId>,
25 /// The parent of this node
26 parent_node: hir::ItemLocalId,
30 definitions: &'a definitions::Definitions,
33 fn insert_vec_map<K: Idx, V: Clone>(map: &mut IndexVec<K, Option<V>>, k: K, v: V) {
37 map.extend(repeat(None).take(i - len + 1));
39 debug_assert!(map[k].is_none());
43 pub(super) fn index_hir<'hir>(
45 definitions: &definitions::Definitions,
46 item: hir::OwnerNode<'hir>,
47 bodies: &IndexVec<ItemLocalId, Option<&'hir Body<'hir>>>,
48 ) -> (IndexVec<ItemLocalId, Option<ParentedNode<'hir>>>, FxHashMap<LocalDefId, ItemLocalId>) {
49 let mut nodes = IndexVec::new();
50 nodes.push(Some(ParentedNode { parent: ItemLocalId::new(0), node: item.into() }));
51 let mut collector = NodeCollector {
52 source_map: sess.source_map(),
55 parent_node: ItemLocalId::new(0),
58 parenting: FxHashMap::default(),
62 OwnerNode::Crate(citem) => collector.visit_mod(&citem, citem.inner, hir::CRATE_HIR_ID),
63 OwnerNode::Item(item) => collector.visit_item(item),
64 OwnerNode::TraitItem(item) => collector.visit_trait_item(item),
65 OwnerNode::ImplItem(item) => collector.visit_impl_item(item),
66 OwnerNode::ForeignItem(item) => collector.visit_foreign_item(item),
69 (collector.nodes, collector.parenting)
72 impl<'a, 'hir> NodeCollector<'a, 'hir> {
73 fn insert(&mut self, span: Span, hir_id: HirId, node: Node<'hir>) {
74 debug_assert_eq!(self.owner, hir_id.owner);
75 debug_assert_ne!(hir_id.local_id.as_u32(), 0);
77 // Make sure that the DepNode of some node coincides with the HirId
78 // owner of that node.
79 if cfg!(debug_assertions) {
80 if hir_id.owner != self.owner {
82 "inconsistent DepNode at `{:?}` for `{:?}`: \
83 current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})",
84 self.source_map.span_to_diagnostic_string(span),
86 self.definitions.def_path(self.owner).to_string_no_crate_verbose(),
88 self.definitions.def_path(hir_id.owner).to_string_no_crate_verbose(),
97 ParentedNode { parent: self.parent_node, node: node },
101 fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_node_id: HirId, f: F) {
102 debug_assert_eq!(parent_node_id.owner, self.owner);
103 let parent_node = self.parent_node;
104 self.parent_node = parent_node_id.local_id;
106 self.parent_node = parent_node;
109 fn insert_nested(&mut self, item: LocalDefId) {
110 self.parenting.insert(item, self.parent_node);
114 impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
117 /// Because we want to track parent items and so forth, enable
118 /// deep walking so that we walk nested items in the context of
119 /// their outer items.
121 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
122 panic!("`visit_nested_xxx` must be manually implemented in this visitor");
125 fn visit_nested_item(&mut self, item: ItemId) {
126 debug!("visit_nested_item: {:?}", item);
127 self.insert_nested(item.def_id);
130 fn visit_nested_trait_item(&mut self, item_id: TraitItemId) {
131 self.insert_nested(item_id.def_id);
134 fn visit_nested_impl_item(&mut self, item_id: ImplItemId) {
135 self.insert_nested(item_id.def_id);
138 fn visit_nested_foreign_item(&mut self, foreign_id: ForeignItemId) {
139 self.insert_nested(foreign_id.def_id);
142 fn visit_nested_body(&mut self, id: BodyId) {
143 debug_assert_eq!(id.hir_id.owner, self.owner);
144 let body = self.bodies[id.hir_id.local_id].unwrap();
145 self.visit_body(body);
148 fn visit_param(&mut self, param: &'hir Param<'hir>) {
149 let node = Node::Param(param);
150 self.insert(param.pat.span, param.hir_id, node);
151 self.with_parent(param.hir_id, |this| {
152 intravisit::walk_param(this, param);
156 fn visit_item(&mut self, i: &'hir Item<'hir>) {
157 debug!("visit_item: {:?}", i);
158 debug_assert_eq!(i.def_id, self.owner);
159 self.with_parent(i.hir_id(), |this| {
160 if let ItemKind::Struct(ref struct_def, _) = i.kind {
161 // If this is a tuple or unit-like struct, register the constructor.
162 if let Some(ctor_hir_id) = struct_def.ctor_hir_id() {
163 this.insert(i.span, ctor_hir_id, Node::Ctor(struct_def));
166 intravisit::walk_item(this, i);
170 fn visit_foreign_item(&mut self, fi: &'hir ForeignItem<'hir>) {
171 debug_assert_eq!(fi.def_id, self.owner);
172 self.with_parent(fi.hir_id(), |this| {
173 intravisit::walk_foreign_item(this, fi);
177 fn visit_generic_param(&mut self, param: &'hir GenericParam<'hir>) {
178 self.insert(param.span, param.hir_id, Node::GenericParam(param));
179 intravisit::walk_generic_param(self, param);
182 fn visit_const_param_default(&mut self, param: HirId, ct: &'hir AnonConst) {
183 self.with_parent(param, |this| {
184 intravisit::walk_const_param_default(this, ct);
188 fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) {
189 debug_assert_eq!(ti.def_id, self.owner);
190 self.with_parent(ti.hir_id(), |this| {
191 intravisit::walk_trait_item(this, ti);
195 fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) {
196 debug_assert_eq!(ii.def_id, self.owner);
197 self.with_parent(ii.hir_id(), |this| {
198 intravisit::walk_impl_item(this, ii);
202 fn visit_pat(&mut self, pat: &'hir Pat<'hir>) {
204 if let PatKind::Binding(..) = pat.kind { Node::Binding(pat) } else { Node::Pat(pat) };
205 self.insert(pat.span, pat.hir_id, node);
207 self.with_parent(pat.hir_id, |this| {
208 intravisit::walk_pat(this, pat);
212 fn visit_arm(&mut self, arm: &'hir Arm<'hir>) {
213 let node = Node::Arm(arm);
215 self.insert(arm.span, arm.hir_id, node);
217 self.with_parent(arm.hir_id, |this| {
218 intravisit::walk_arm(this, arm);
222 fn visit_anon_const(&mut self, constant: &'hir AnonConst) {
223 self.insert(DUMMY_SP, constant.hir_id, Node::AnonConst(constant));
225 self.with_parent(constant.hir_id, |this| {
226 intravisit::walk_anon_const(this, constant);
230 fn visit_expr(&mut self, expr: &'hir Expr<'hir>) {
231 self.insert(expr.span, expr.hir_id, Node::Expr(expr));
233 self.with_parent(expr.hir_id, |this| {
234 intravisit::walk_expr(this, expr);
238 fn visit_stmt(&mut self, stmt: &'hir Stmt<'hir>) {
239 self.insert(stmt.span, stmt.hir_id, Node::Stmt(stmt));
241 self.with_parent(stmt.hir_id, |this| {
242 intravisit::walk_stmt(this, stmt);
246 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'hir PathSegment<'hir>) {
247 if let Some(hir_id) = path_segment.hir_id {
248 self.insert(path_span, hir_id, Node::PathSegment(path_segment));
250 intravisit::walk_path_segment(self, path_span, path_segment);
253 fn visit_ty(&mut self, ty: &'hir Ty<'hir>) {
254 self.insert(ty.span, ty.hir_id, Node::Ty(ty));
256 self.with_parent(ty.hir_id, |this| {
257 intravisit::walk_ty(this, ty);
261 fn visit_infer(&mut self, inf: &'hir InferArg) {
262 self.insert(inf.span, inf.hir_id, Node::Infer(inf));
264 self.with_parent(inf.hir_id, |this| {
265 intravisit::walk_inf(this, inf);
269 fn visit_trait_ref(&mut self, tr: &'hir TraitRef<'hir>) {
270 self.insert(tr.path.span, tr.hir_ref_id, Node::TraitRef(tr));
272 self.with_parent(tr.hir_ref_id, |this| {
273 intravisit::walk_trait_ref(this, tr);
279 fk: intravisit::FnKind<'hir>,
280 fd: &'hir FnDecl<'hir>,
285 assert_eq!(self.owner, id.owner);
286 assert_eq!(self.parent_node, id.local_id);
287 intravisit::walk_fn(self, fk, fd, b, s, id);
290 fn visit_block(&mut self, block: &'hir Block<'hir>) {
291 self.insert(block.span, block.hir_id, Node::Block(block));
292 self.with_parent(block.hir_id, |this| {
293 intravisit::walk_block(this, block);
297 fn visit_local(&mut self, l: &'hir Local<'hir>) {
298 self.insert(l.span, l.hir_id, Node::Local(l));
299 self.with_parent(l.hir_id, |this| {
300 intravisit::walk_local(this, l);
304 fn visit_lifetime(&mut self, lifetime: &'hir Lifetime) {
305 self.insert(lifetime.span, lifetime.hir_id, Node::Lifetime(lifetime));
308 fn visit_vis(&mut self, visibility: &'hir Visibility<'hir>) {
309 match visibility.node {
310 VisibilityKind::Public | VisibilityKind::Crate(_) | VisibilityKind::Inherited => {}
311 VisibilityKind::Restricted { hir_id, .. } => {
312 self.insert(visibility.span, hir_id, Node::Visibility(visibility));
313 self.with_parent(hir_id, |this| {
314 intravisit::walk_vis(this, visibility);
320 fn visit_variant(&mut self, v: &'hir Variant<'hir>, g: &'hir Generics<'hir>, item_id: HirId) {
321 self.insert(v.span, v.id, Node::Variant(v));
322 self.with_parent(v.id, |this| {
323 // Register the constructor of this variant.
324 if let Some(ctor_hir_id) = v.data.ctor_hir_id() {
325 this.insert(v.span, ctor_hir_id, Node::Ctor(&v.data));
327 intravisit::walk_variant(this, v, g, item_id);
331 fn visit_field_def(&mut self, field: &'hir FieldDef<'hir>) {
332 self.insert(field.span, field.hir_id, Node::Field(field));
333 self.with_parent(field.hir_id, |this| {
334 intravisit::walk_field_def(this, field);
338 fn visit_trait_item_ref(&mut self, ii: &'hir TraitItemRef) {
339 // Do not visit the duplicate information in TraitItemRef. We want to
340 // map the actual nodes, not the duplicate ones in the *Ref.
341 let TraitItemRef { id, ident: _, kind: _, span: _, defaultness: _ } = *ii;
343 self.visit_nested_trait_item(id);
346 fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef) {
347 // Do not visit the duplicate information in ImplItemRef. We want to
348 // map the actual nodes, not the duplicate ones in the *Ref.
349 let ImplItemRef { id, ident: _, kind: _, span: _, defaultness: _ } = *ii;
351 self.visit_nested_impl_item(id);
354 fn visit_foreign_item_ref(&mut self, fi: &'hir ForeignItemRef) {
355 // Do not visit the duplicate information in ForeignItemRef. We want to
356 // map the actual nodes, not the duplicate ones in the *Ref.
357 let ForeignItemRef { id, ident: _, span: _ } = *fi;
359 self.visit_nested_foreign_item(id);