rust/compiler/rustc_middle/src/hir/map/collector.rs

411 lines
14 KiB
Rust
Raw Normal View History

2020-02-07 10:45:04 +00:00
use crate::arena::Arena;
2021-05-28 19:14:11 +00:00
use crate::hir::map::Map;
use crate::hir::{IndexedHir, OwnerNodes, ParentedNode};
2019-12-24 04:02:53 +00:00
use rustc_data_structures::fx::FxHashMap;
use rustc_hir as hir;
2021-02-28 17:58:50 +00:00
use rustc_hir::def_id::LocalDefId;
2021-03-27 12:21:26 +00:00
use rustc_hir::def_id::CRATE_DEF_ID;
2021-02-28 17:58:50 +00:00
use rustc_hir::definitions;
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
2020-01-07 13:38:27 +00:00
use rustc_hir::*;
2020-02-10 16:00:49 +00:00
use rustc_index::vec::{Idx, IndexVec};
2021-02-28 17:58:50 +00:00
use rustc_session::Session;
use rustc_span::source_map::SourceMap;
2021-02-28 17:58:50 +00:00
use rustc_span::{Span, DUMMY_SP};
2020-01-07 13:38:27 +00:00
use std::iter::repeat;
/// A visitor that walks over the HIR and collects `Node`s into a HIR map.
pub(super) struct NodeCollector<'a, 'hir> {
2020-02-07 10:45:04 +00:00
arena: &'hir Arena<'hir>,
/// The crate
2019-11-28 10:49:29 +00:00
krate: &'hir Crate<'hir>,
/// Source map
source_map: &'a SourceMap,
2021-05-28 19:14:11 +00:00
map: IndexVec<LocalDefId, Option<&'hir mut OwnerNodes<'hir>>>,
2021-03-05 19:09:33 +00:00
parenting: FxHashMap<LocalDefId, HirId>,
2020-02-07 10:45:04 +00:00
/// The parent of this node
2019-02-15 14:21:56 +00:00
parent_node: hir::HirId,
2019-02-03 08:14:31 +00:00
current_dep_node_owner: LocalDefId,
definitions: &'a definitions::Definitions,
}
2020-02-10 16:00:49 +00:00
fn insert_vec_map<K: Idx, V: Clone>(map: &mut IndexVec<K, Option<V>>, k: K, v: V) {
let i = k.index();
let len = map.len();
if i >= len {
map.extend(repeat(None).take(i - len + 1));
}
2021-03-14 20:29:34 +00:00
debug_assert!(map[k].is_none());
2020-02-10 16:00:49 +00:00
map[k] = Some(v);
}
impl<'a, 'hir: 'a> NodeCollector<'a, 'hir> {
2019-12-22 22:42:04 +00:00
pub(super) fn root(
sess: &'a Session,
2020-02-07 10:45:04 +00:00
arena: &'hir Arena<'hir>,
2019-12-22 22:42:04 +00:00
krate: &'hir Crate<'hir>,
definitions: &'a definitions::Definitions,
) -> NodeCollector<'a, 'hir> {
let mut collector = NodeCollector {
2020-02-07 10:45:04 +00:00
arena,
krate,
source_map: sess.source_map(),
2019-02-15 14:21:56 +00:00
parent_node: hir::CRATE_HIR_ID,
2021-03-27 12:21:26 +00:00
current_dep_node_owner: CRATE_DEF_ID,
definitions,
2021-05-28 19:14:11 +00:00
map: IndexVec::from_fn_n(|_| None, definitions.def_index_count()),
2021-03-05 19:09:33 +00:00
parenting: FxHashMap::default(),
};
2021-03-27 12:21:26 +00:00
collector.insert_owner(CRATE_DEF_ID, OwnerNode::Crate(krate.module()));
collector
}
2021-07-16 12:42:26 +00:00
pub(super) fn finalize_and_compute_crate_hash(self) -> IndexedHir<'hir> {
2021-03-05 19:09:33 +00:00
IndexedHir { map: self.map, parenting: self.parenting }
}
2021-03-27 12:21:26 +00:00
fn insert_owner(&mut self, owner: LocalDefId, node: OwnerNode<'hir>) {
let mut nodes = IndexVec::new();
nodes.push(Some(ParentedNode { parent: ItemLocalId::new(0), node: node.into() }));
2021-05-28 19:14:11 +00:00
2021-10-09 17:44:55 +00:00
let info = self.krate.owners[owner].as_ref().unwrap();
let hash = info.hash;
let node_hash = info.node_hash;
let bodies = &info.bodies;
2021-03-27 12:21:26 +00:00
debug_assert!(self.map[owner].is_none());
self.map[owner] = Some(self.arena.alloc(OwnerNodes { hash, node_hash, nodes, bodies }));
}
fn insert(&mut self, span: Span, hir_id: HirId, node: Node<'hir>) {
2021-03-27 12:21:26 +00:00
debug_assert_eq!(self.current_dep_node_owner, hir_id.owner);
debug_assert_ne!(hir_id.local_id.as_u32(), 0);
// Make sure that the DepNode of some node coincides with the HirId
// owner of that node.
if cfg!(debug_assertions) {
if hir_id.owner != self.current_dep_node_owner {
let node_str = match self.definitions.opt_hir_id_to_local_def_id(hir_id) {
2020-09-23 22:38:38 +00:00
Some(def_id) => self.definitions.def_path(def_id).to_string_no_crate_verbose(),
2019-12-22 22:42:04 +00:00
None => format!("{:?}", node),
};
span_bug!(
span,
"inconsistent DepNode at `{:?}` for `{}`: \
2020-04-13 16:52:40 +00:00
current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})",
self.source_map.span_to_diagnostic_string(span),
node_str,
2020-09-23 22:38:38 +00:00
self.definitions
.def_path(self.current_dep_node_owner)
.to_string_no_crate_verbose(),
self.current_dep_node_owner,
2020-09-23 22:38:38 +00:00
self.definitions.def_path(hir_id.owner).to_string_no_crate_verbose(),
hir_id.owner,
)
}
}
2021-03-27 12:21:26 +00:00
let nodes = self.map[hir_id.owner].as_mut().unwrap();
debug_assert_eq!(self.parent_node.owner, self.current_dep_node_owner);
insert_vec_map(
&mut nodes.nodes,
hir_id.local_id,
ParentedNode { parent: self.parent_node.local_id, node: node },
);
}
2016-04-14 05:24:30 +00:00
2019-12-22 22:42:04 +00:00
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_node_id: HirId, f: F) {
2019-02-15 14:21:56 +00:00
let parent_node = self.parent_node;
self.parent_node = parent_node_id;
2016-04-14 05:24:30 +00:00
f(self);
2019-02-15 14:21:56 +00:00
self.parent_node = parent_node;
2016-04-14 05:24:30 +00:00
}
2021-03-27 12:21:26 +00:00
fn with_dep_node_owner(&mut self, dep_node_owner: LocalDefId, f: impl FnOnce(&mut Self)) {
let prev_owner = self.current_dep_node_owner;
2021-03-27 12:21:26 +00:00
let prev_parent = self.parent_node;
self.current_dep_node_owner = dep_node_owner;
2021-03-27 12:21:26 +00:00
self.parent_node = HirId::make_owner(dep_node_owner);
f(self);
self.current_dep_node_owner = prev_owner;
2021-03-27 12:21:26 +00:00
self.parent_node = prev_parent;
}
2021-03-05 19:09:33 +00:00
fn insert_nested(&mut self, item: LocalDefId) {
#[cfg(debug_assertions)]
{
let dk_parent = self.definitions.def_key(item).parent.unwrap();
let dk_parent = LocalDefId { local_def_index: dk_parent };
let dk_parent = self.definitions.local_def_id_to_hir_id(dk_parent);
debug_assert_eq!(
dk_parent.owner, self.parent_node.owner,
"Different parents for {:?}",
item
)
}
assert_eq!(self.parenting.insert(item, self.parent_node), None);
}
}
impl<'a, 'hir: 'a> Visitor<'hir> for NodeCollector<'a, 'hir> {
2020-01-07 16:25:33 +00:00
type Map = Map<'hir>;
/// Because we want to track parent items and so forth, enable
/// deep walking so that we walk nested items in the context of
/// their outer items.
2020-02-09 14:32:00 +00:00
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
panic!("`visit_nested_xxx` must be manually implemented in this visitor");
}
fn visit_nested_item(&mut self, item: ItemId) {
debug!("visit_nested_item: {:?}", item);
2021-03-05 19:09:33 +00:00
self.insert_nested(item.def_id);
2021-01-30 11:06:04 +00:00
self.visit_item(self.krate.item(item));
}
fn visit_nested_trait_item(&mut self, item_id: TraitItemId) {
2021-03-05 19:09:33 +00:00
self.insert_nested(item_id.def_id);
self.visit_trait_item(self.krate.trait_item(item_id));
}
fn visit_nested_impl_item(&mut self, item_id: ImplItemId) {
2021-03-05 19:09:33 +00:00
self.insert_nested(item_id.def_id);
self.visit_impl_item(self.krate.impl_item(item_id));
}
2020-11-11 20:57:54 +00:00
fn visit_nested_foreign_item(&mut self, foreign_id: ForeignItemId) {
2021-03-05 19:09:33 +00:00
self.insert_nested(foreign_id.def_id);
2020-11-11 20:57:54 +00:00
self.visit_foreign_item(self.krate.foreign_item(foreign_id));
}
fn visit_nested_body(&mut self, id: BodyId) {
self.visit_body(self.krate.body(id));
2016-10-28 20:58:32 +00:00
}
2019-11-29 12:43:03 +00:00
fn visit_param(&mut self, param: &'hir Param<'hir>) {
let node = Node::Param(param);
self.insert(param.pat.span, param.hir_id, node);
self.with_parent(param.hir_id, |this| {
intravisit::walk_param(this, param);
2019-07-26 22:52:37 +00:00
});
}
2019-11-28 18:28:50 +00:00
fn visit_item(&mut self, i: &'hir Item<'hir>) {
debug!("visit_item: {:?}", i);
2021-03-27 12:21:26 +00:00
self.insert_owner(i.def_id, OwnerNode::Item(i));
self.with_dep_node_owner(i.def_id, |this| {
if let ItemKind::Struct(ref struct_def, _) = i.kind {
// If this is a tuple or unit-like struct, register the constructor.
if let Some(ctor_hir_id) = struct_def.ctor_hir_id() {
this.insert(i.span, ctor_hir_id, Node::Ctor(struct_def));
}
2021-03-27 12:21:26 +00:00
}
intravisit::walk_item(this, i);
2016-04-14 05:24:30 +00:00
});
}
2020-11-11 20:57:54 +00:00
fn visit_foreign_item(&mut self, fi: &'hir ForeignItem<'hir>) {
2021-03-27 12:21:26 +00:00
self.insert_owner(fi.def_id, OwnerNode::ForeignItem(fi));
self.with_dep_node_owner(fi.def_id, |this| {
intravisit::walk_foreign_item(this, fi);
2016-04-14 05:24:30 +00:00
});
}
2019-11-30 16:46:46 +00:00
fn visit_generic_param(&mut self, param: &'hir GenericParam<'hir>) {
self.insert(param.span, param.hir_id, Node::GenericParam(param));
intravisit::walk_generic_param(self, param);
}
2021-03-01 11:50:09 +00:00
fn visit_const_param_default(&mut self, param: HirId, ct: &'hir AnonConst) {
2021-03-27 12:21:26 +00:00
self.with_parent(param, |this| {
intravisit::walk_const_param_default(this, ct);
})
2021-03-01 11:50:09 +00:00
}
2019-11-28 20:47:10 +00:00
fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) {
2021-03-27 12:21:26 +00:00
self.insert_owner(ti.def_id, OwnerNode::TraitItem(ti));
self.with_dep_node_owner(ti.def_id, |this| {
intravisit::walk_trait_item(this, ti);
2016-04-14 05:24:30 +00:00
});
}
2019-11-28 21:16:44 +00:00
fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) {
2021-03-27 12:21:26 +00:00
self.insert_owner(ii.def_id, OwnerNode::ImplItem(ii));
self.with_dep_node_owner(ii.def_id, |this| {
intravisit::walk_impl_item(this, ii);
2016-04-14 05:24:30 +00:00
});
}
2019-11-29 12:43:03 +00:00
fn visit_pat(&mut self, pat: &'hir Pat<'hir>) {
2019-12-22 22:42:04 +00:00
let node =
if let PatKind::Binding(..) = pat.kind { Node::Binding(pat) } else { Node::Pat(pat) };
self.insert(pat.span, pat.hir_id, node);
self.with_parent(pat.hir_id, |this| {
2016-04-14 05:24:30 +00:00
intravisit::walk_pat(this, pat);
});
}
2019-11-29 12:43:03 +00:00
fn visit_arm(&mut self, arm: &'hir Arm<'hir>) {
2019-03-30 22:54:29 +00:00
let node = Node::Arm(arm);
self.insert(arm.span, arm.hir_id, node);
self.with_parent(arm.hir_id, |this| {
intravisit::walk_arm(this, arm);
});
}
fn visit_anon_const(&mut self, constant: &'hir AnonConst) {
self.insert(DUMMY_SP, constant.hir_id, Node::AnonConst(constant));
self.with_parent(constant.hir_id, |this| {
intravisit::walk_anon_const(this, constant);
});
}
2019-11-29 12:43:03 +00:00
fn visit_expr(&mut self, expr: &'hir Expr<'hir>) {
self.insert(expr.span, expr.hir_id, Node::Expr(expr));
self.with_parent(expr.hir_id, |this| {
2016-04-14 05:24:30 +00:00
intravisit::walk_expr(this, expr);
});
}
2019-11-29 12:43:03 +00:00
fn visit_stmt(&mut self, stmt: &'hir Stmt<'hir>) {
self.insert(stmt.span, stmt.hir_id, Node::Stmt(stmt));
2016-04-14 05:24:30 +00:00
self.with_parent(stmt.hir_id, |this| {
2016-04-14 05:24:30 +00:00
intravisit::walk_stmt(this, stmt);
});
}
2019-11-30 16:46:46 +00:00
fn visit_path_segment(&mut self, path_span: Span, path_segment: &'hir PathSegment<'hir>) {
2019-02-15 14:21:56 +00:00
if let Some(hir_id) = path_segment.hir_id {
self.insert(path_span, hir_id, Node::PathSegment(path_segment));
}
intravisit::walk_path_segment(self, path_span, path_segment);
}
2019-11-30 16:46:46 +00:00
fn visit_ty(&mut self, ty: &'hir Ty<'hir>) {
self.insert(ty.span, ty.hir_id, Node::Ty(ty));
self.with_parent(ty.hir_id, |this| {
intravisit::walk_ty(this, ty);
});
}
2021-04-24 21:41:57 +00:00
fn visit_infer(&mut self, inf: &'hir InferArg) {
self.insert(inf.span, inf.hir_id, Node::Infer(inf));
self.with_parent(inf.hir_id, |this| {
intravisit::walk_inf(this, inf);
});
}
2019-11-30 16:46:46 +00:00
fn visit_trait_ref(&mut self, tr: &'hir TraitRef<'hir>) {
self.insert(tr.path.span, tr.hir_ref_id, Node::TraitRef(tr));
self.with_parent(tr.hir_ref_id, |this| {
intravisit::walk_trait_ref(this, tr);
});
}
2019-12-22 22:42:04 +00:00
fn visit_fn(
&mut self,
fk: intravisit::FnKind<'hir>,
2019-11-30 16:46:46 +00:00
fd: &'hir FnDecl<'hir>,
2019-12-22 22:42:04 +00:00
b: BodyId,
s: Span,
id: HirId,
) {
2019-02-15 14:21:56 +00:00
assert_eq!(self.parent_node, id);
intravisit::walk_fn(self, fk, fd, b, s, id);
}
2019-11-29 12:43:03 +00:00
fn visit_block(&mut self, block: &'hir Block<'hir>) {
self.insert(block.span, block.hir_id, Node::Block(block));
self.with_parent(block.hir_id, |this| {
2016-04-14 05:24:30 +00:00
intravisit::walk_block(this, block);
});
}
2019-11-29 12:43:03 +00:00
fn visit_local(&mut self, l: &'hir Local<'hir>) {
self.insert(l.span, l.hir_id, Node::Local(l));
2021-03-27 12:21:26 +00:00
self.with_parent(l.hir_id, |this| {
intravisit::walk_local(this, l);
})
}
2017-01-26 01:21:50 +00:00
fn visit_lifetime(&mut self, lifetime: &'hir Lifetime) {
self.insert(lifetime.span, lifetime.hir_id, Node::Lifetime(lifetime));
}
2016-10-28 06:52:45 +00:00
2019-11-30 16:46:46 +00:00
fn visit_vis(&mut self, visibility: &'hir Visibility<'hir>) {
match visibility.node {
2019-12-22 22:42:04 +00:00
VisibilityKind::Public | VisibilityKind::Crate(_) | VisibilityKind::Inherited => {}
VisibilityKind::Restricted { hir_id, .. } => {
self.insert(visibility.span, hir_id, Node::Visibility(visibility));
self.with_parent(hir_id, |this| {
intravisit::walk_vis(this, visibility);
});
}
}
}
2019-11-30 16:46:46 +00:00
fn visit_variant(&mut self, v: &'hir Variant<'hir>, g: &'hir Generics<'hir>, item_id: HirId) {
2019-08-14 00:40:21 +00:00
self.insert(v.span, v.id, Node::Variant(v));
self.with_parent(v.id, |this| {
// Register the constructor of this variant.
2019-08-14 00:40:21 +00:00
if let Some(ctor_hir_id) = v.data.ctor_hir_id() {
this.insert(v.span, ctor_hir_id, Node::Ctor(&v.data));
}
intravisit::walk_variant(this, v, g, item_id);
});
}
fn visit_field_def(&mut self, field: &'hir FieldDef<'hir>) {
self.insert(field.span, field.hir_id, Node::Field(field));
self.with_parent(field.hir_id, |this| {
intravisit::walk_field_def(this, field);
2016-11-09 18:57:48 +00:00
});
}
fn visit_trait_item_ref(&mut self, ii: &'hir TraitItemRef) {
// Do not visit the duplicate information in TraitItemRef. We want to
// map the actual nodes, not the duplicate ones in the *Ref.
2019-12-22 22:42:04 +00:00
let TraitItemRef { id, ident: _, kind: _, span: _, defaultness: _ } = *ii;
self.visit_nested_trait_item(id);
}
2021-07-15 20:19:39 +00:00
fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef) {
// Do not visit the duplicate information in ImplItemRef. We want to
// map the actual nodes, not the duplicate ones in the *Ref.
2021-07-15 20:19:39 +00:00
let ImplItemRef { id, ident: _, kind: _, span: _, defaultness: _ } = *ii;
self.visit_nested_impl_item(id);
}
2021-07-15 20:19:39 +00:00
fn visit_foreign_item_ref(&mut self, fi: &'hir ForeignItemRef) {
// Do not visit the duplicate information in ForeignItemRef. We want to
// map the actual nodes, not the duplicate ones in the *Ref.
2021-07-15 20:19:39 +00:00
let ForeignItemRef { id, ident: _, span: _ } = *fi;
self.visit_nested_foreign_item(id);
}
}