2020-02-07 11:45:04 +01:00
|
|
|
use crate::arena::Arena;
|
2021-05-28 21:14:11 +02:00
|
|
|
use crate::hir::map::Map;
|
|
|
|
use crate::hir::{IndexedHir, OwnerNodes, ParentedNode};
|
2020-01-07 14:38:27 +01:00
|
|
|
use crate::ich::StableHashingContext;
|
|
|
|
use rustc_data_structures::fingerprint::Fingerprint;
|
2019-12-24 05:02:53 +01:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-01-07 14:38:27 +01:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
2021-02-28 18:58:50 +01:00
|
|
|
use rustc_hir::def_id::LocalDefId;
|
2020-01-07 14:38:27 +01:00
|
|
|
use rustc_hir::def_id::CRATE_DEF_INDEX;
|
2021-02-28 18:58:50 +01:00
|
|
|
use rustc_hir::definitions;
|
2020-01-07 18:12:06 +01:00
|
|
|
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
|
2020-01-07 14:38:27 +01:00
|
|
|
use rustc_hir::*;
|
2020-02-10 17:00:49 +01:00
|
|
|
use rustc_index::vec::{Idx, IndexVec};
|
2021-02-28 18:58:50 +01:00
|
|
|
use rustc_session::Session;
|
2020-01-01 19:25:28 +01:00
|
|
|
use rustc_span::source_map::SourceMap;
|
2021-02-28 18:58:50 +01:00
|
|
|
use rustc_span::{Span, DUMMY_SP};
|
2015-09-10 15:53:08 -04:00
|
|
|
|
2020-01-07 14:38:27 +01:00
|
|
|
use std::iter::repeat;
|
2017-09-14 17:43:03 +02:00
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
/// A visitor that walks over the HIR and collects `Node`s into a HIR map.
|
2017-08-18 20:24:19 +02:00
|
|
|
pub(super) struct NodeCollector<'a, 'hir> {
|
2020-02-07 11:45:04 +01:00
|
|
|
arena: &'hir Arena<'hir>,
|
|
|
|
|
2015-12-21 22:24:15 +01:00
|
|
|
/// The crate
|
2019-11-28 11:49:29 +01:00
|
|
|
krate: &'hir Crate<'hir>,
|
2018-11-21 13:35:14 -05:00
|
|
|
|
|
|
|
/// Source map
|
|
|
|
source_map: &'a SourceMap,
|
|
|
|
|
2021-05-28 21:14:11 +02:00
|
|
|
map: IndexVec<LocalDefId, Option<&'hir mut OwnerNodes<'hir>>>,
|
2021-03-05 20:09:33 +01:00
|
|
|
parenting: FxHashMap<LocalDefId, HirId>,
|
2020-02-07 11:45:04 +01:00
|
|
|
|
2015-12-21 22:24:15 +01:00
|
|
|
/// The parent of this node
|
2019-02-15 15:21:56 +01:00
|
|
|
parent_node: hir::HirId,
|
2019-02-03 09:14:31 +01:00
|
|
|
|
2020-03-18 20:27:59 +02:00
|
|
|
current_dep_node_owner: LocalDefId,
|
2017-08-18 20:24:19 +02:00
|
|
|
|
|
|
|
definitions: &'a definitions::Definitions,
|
2017-09-14 17:43:03 +02:00
|
|
|
|
|
|
|
hcx: StableHashingContext<'a>,
|
2018-12-22 12:40:23 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 17:00:49 +01: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 21:29:34 +01:00
|
|
|
debug_assert!(map[k].is_none());
|
2020-02-10 17:00:49 +01:00
|
|
|
map[k] = Some(v);
|
|
|
|
}
|
|
|
|
|
2020-02-08 05:18:34 +01:00
|
|
|
fn hash_body(
|
2019-01-20 05:44:02 +01:00
|
|
|
hcx: &mut StableHashingContext<'_>,
|
2020-01-07 14:22:44 +01:00
|
|
|
item_like: impl for<'a> HashStable<StableHashingContext<'a>>,
|
2020-02-10 14:29:21 +01:00
|
|
|
) -> Fingerprint {
|
2021-02-28 18:58:50 +01:00
|
|
|
let mut stable_hasher = StableHasher::new();
|
|
|
|
hcx.while_hashing_hir_bodies(true, |hcx| {
|
|
|
|
item_like.hash_stable(hcx, &mut stable_hasher);
|
|
|
|
});
|
|
|
|
stable_hasher.finish()
|
2020-01-07 14:19:35 +01:00
|
|
|
}
|
|
|
|
|
2021-03-03 21:50:45 +01:00
|
|
|
/// Represents an entry and its parent `HirId`.
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct Entry<'hir> {
|
|
|
|
parent: HirId,
|
|
|
|
node: Node<'hir>,
|
|
|
|
}
|
|
|
|
|
2017-08-18 20:24:19 +02:00
|
|
|
impl<'a, 'hir> NodeCollector<'a, 'hir> {
|
2019-12-22 17:42:04 -05:00
|
|
|
pub(super) fn root(
|
|
|
|
sess: &'a Session,
|
2020-02-07 11:45:04 +01:00
|
|
|
arena: &'hir Arena<'hir>,
|
2019-12-22 17:42:04 -05:00
|
|
|
krate: &'hir Crate<'hir>,
|
|
|
|
definitions: &'a definitions::Definitions,
|
|
|
|
mut hcx: StableHashingContext<'a>,
|
|
|
|
) -> NodeCollector<'a, 'hir> {
|
2021-07-25 12:03:24 +02:00
|
|
|
let hash = hash_body(&mut hcx, krate.module());
|
2017-09-14 17:43:03 +02:00
|
|
|
|
2016-04-14 11:55:34 +12:00
|
|
|
let mut collector = NodeCollector {
|
2020-02-07 11:45:04 +01:00
|
|
|
arena,
|
2017-07-03 11:19:51 -07:00
|
|
|
krate,
|
2018-06-06 22:13:52 +02:00
|
|
|
source_map: sess.source_map(),
|
2019-02-15 15:21:56 +01:00
|
|
|
parent_node: hir::CRATE_HIR_ID,
|
2020-03-18 20:27:59 +02:00
|
|
|
current_dep_node_owner: LocalDefId { local_def_index: CRATE_DEF_INDEX },
|
2017-08-18 20:24:19 +02:00
|
|
|
definitions,
|
2017-09-14 17:43:03 +02:00
|
|
|
hcx,
|
2021-05-28 21:14:11 +02:00
|
|
|
map: IndexVec::from_fn_n(|_| None, definitions.def_index_count()),
|
2021-03-05 20:09:33 +01:00
|
|
|
parenting: FxHashMap::default(),
|
2016-04-14 11:55:34 +12:00
|
|
|
};
|
2019-12-22 17:42:04 -05:00
|
|
|
collector.insert_entry(
|
|
|
|
hir::CRATE_HIR_ID,
|
2021-07-25 12:03:24 +02:00
|
|
|
Entry { parent: hir::CRATE_HIR_ID, node: Node::Crate(&krate.module()) },
|
2020-02-10 14:29:21 +01:00
|
|
|
hash,
|
2019-12-22 17:42:04 -05:00
|
|
|
);
|
2016-04-14 11:55:34 +12:00
|
|
|
|
|
|
|
collector
|
|
|
|
}
|
|
|
|
|
2021-03-05 20:09:33 +01:00
|
|
|
pub(super) fn finalize_and_compute_crate_hash(mut self) -> IndexedHir<'hir> {
|
2020-02-07 13:13:35 +01:00
|
|
|
// Insert bodies into the map
|
|
|
|
for (id, body) in self.krate.bodies.iter() {
|
2021-05-28 21:14:11 +02:00
|
|
|
let bodies = &mut self.map[id.hir_id.owner].as_mut().unwrap().bodies;
|
2020-02-07 13:13:35 +01:00
|
|
|
assert!(bodies.insert(id.hir_id.local_id, body).is_none());
|
|
|
|
}
|
2021-03-05 20:09:33 +01:00
|
|
|
IndexedHir { map: self.map, parenting: self.parenting }
|
2017-08-18 20:24:19 +02:00
|
|
|
}
|
|
|
|
|
2020-02-10 14:29:21 +01:00
|
|
|
fn insert_entry(&mut self, id: HirId, entry: Entry<'hir>, hash: Fingerprint) {
|
2020-02-07 11:45:04 +01:00
|
|
|
let i = id.local_id.as_u32() as usize;
|
|
|
|
|
|
|
|
let arena = self.arena;
|
|
|
|
|
2020-02-10 17:00:49 +01:00
|
|
|
let data = &mut self.map[id.owner];
|
|
|
|
|
2021-05-28 21:14:11 +02:00
|
|
|
if i == 0 {
|
|
|
|
debug_assert!(data.is_none());
|
|
|
|
*data = Some(arena.alloc(OwnerNodes {
|
2020-02-10 14:29:21 +01:00
|
|
|
hash,
|
2020-03-18 03:48:17 +02:00
|
|
|
nodes: IndexVec::new(),
|
2020-02-09 12:13:08 +01:00
|
|
|
bodies: FxHashMap::default(),
|
2020-02-10 17:00:49 +01:00
|
|
|
}));
|
2021-02-13 20:41:02 +01:00
|
|
|
|
|
|
|
let dk_parent = self.definitions.def_key(id.owner).parent;
|
|
|
|
if let Some(dk_parent) = dk_parent {
|
|
|
|
let dk_parent = LocalDefId { local_def_index: dk_parent };
|
|
|
|
let dk_parent = self.definitions.local_def_id_to_hir_id(dk_parent);
|
|
|
|
if dk_parent.owner != entry.parent.owner {
|
|
|
|
panic!(
|
|
|
|
"Different parents for {:?} => dk_parent={:?} actual={:?}",
|
|
|
|
id.owner, dk_parent, entry.parent,
|
|
|
|
)
|
|
|
|
}
|
2021-03-05 20:09:33 +01:00
|
|
|
|
|
|
|
debug_assert_eq!(self.parenting.get(&id.owner), Some(&entry.parent));
|
2021-02-13 20:41:02 +01:00
|
|
|
}
|
2020-02-07 11:45:04 +01:00
|
|
|
} else {
|
2021-05-28 21:14:11 +02:00
|
|
|
debug_assert_eq!(entry.parent.owner, id.owner);
|
2020-02-07 11:45:04 +01:00
|
|
|
}
|
2021-05-28 21:14:11 +02:00
|
|
|
|
|
|
|
let data = data.as_mut().unwrap();
|
|
|
|
|
|
|
|
insert_vec_map(
|
|
|
|
&mut data.nodes,
|
|
|
|
id.local_id,
|
|
|
|
ParentedNode { parent: entry.parent.local_id, node: entry.node },
|
|
|
|
);
|
2016-04-14 11:55:34 +12:00
|
|
|
}
|
|
|
|
|
2019-02-15 10:30:44 +01:00
|
|
|
fn insert(&mut self, span: Span, hir_id: HirId, node: Node<'hir>) {
|
2020-02-10 14:29:21 +01:00
|
|
|
self.insert_with_hash(span, hir_id, node, Fingerprint::ZERO)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn insert_with_hash(&mut self, span: Span, hir_id: HirId, node: Node<'hir>, hash: Fingerprint) {
|
2020-02-08 05:18:34 +01:00
|
|
|
let entry = Entry { parent: self.parent_node, node };
|
2017-08-18 20:24:19 +02:00
|
|
|
|
|
|
|
// Make sure that the DepNode of some node coincides with the HirId
|
|
|
|
// owner of that node.
|
|
|
|
if cfg!(debug_assertions) {
|
2018-06-20 10:44:31 +02:00
|
|
|
if hir_id.owner != self.current_dep_node_owner {
|
2020-06-12 18:43:58 +01:00
|
|
|
let node_str = match self.definitions.opt_hir_id_to_local_def_id(hir_id) {
|
2020-09-23 23:38:38 +01:00
|
|
|
Some(def_id) => self.definitions.def_path(def_id).to_string_no_crate_verbose(),
|
2019-12-22 17:42:04 -05:00
|
|
|
None => format!("{:?}", node),
|
2017-08-18 20:24:19 +02:00
|
|
|
};
|
|
|
|
|
2018-11-21 13:35:14 -05:00
|
|
|
span_bug!(
|
|
|
|
span,
|
|
|
|
"inconsistent DepNode at `{:?}` for `{}`: \
|
2020-04-13 17:52:40 +01:00
|
|
|
current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})",
|
2021-05-03 01:14:25 +01:00
|
|
|
self.source_map.span_to_diagnostic_string(span),
|
2017-08-18 20:24:19 +02:00
|
|
|
node_str,
|
2020-09-23 23:38:38 +01:00
|
|
|
self.definitions
|
|
|
|
.def_path(self.current_dep_node_owner)
|
|
|
|
.to_string_no_crate_verbose(),
|
2018-10-11 21:15:18 +13:00
|
|
|
self.current_dep_node_owner,
|
2020-09-23 23:38:38 +01:00
|
|
|
self.definitions.def_path(hir_id.owner).to_string_no_crate_verbose(),
|
2018-10-11 21:15:18 +13:00
|
|
|
hir_id.owner,
|
2018-11-21 13:35:14 -05:00
|
|
|
)
|
2017-08-18 20:24:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 14:29:21 +01:00
|
|
|
self.insert_entry(hir_id, entry, hash);
|
2016-04-14 11:55:34 +12:00
|
|
|
}
|
2016-04-14 17:24:30 +12:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_node_id: HirId, f: F) {
|
2019-02-15 15:21:56 +01:00
|
|
|
let parent_node = self.parent_node;
|
|
|
|
self.parent_node = parent_node_id;
|
2016-04-14 17:24:30 +12:00
|
|
|
f(self);
|
2019-02-15 15:21:56 +01:00
|
|
|
self.parent_node = parent_node;
|
2016-04-14 17:24:30 +12:00
|
|
|
}
|
2017-08-18 20:24:19 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn with_dep_node_owner<
|
|
|
|
T: for<'b> HashStable<StableHashingContext<'b>>,
|
2020-02-10 14:29:21 +01:00
|
|
|
F: FnOnce(&mut Self, Fingerprint),
|
2019-12-22 17:42:04 -05:00
|
|
|
>(
|
|
|
|
&mut self,
|
2020-03-18 20:27:59 +02:00
|
|
|
dep_node_owner: LocalDefId,
|
2019-12-22 17:42:04 -05:00
|
|
|
item_like: &T,
|
|
|
|
f: F,
|
|
|
|
) {
|
2017-08-18 20:24:19 +02:00
|
|
|
let prev_owner = self.current_dep_node_owner;
|
2021-02-28 18:58:50 +01:00
|
|
|
let hash = hash_body(&mut self.hcx, item_like);
|
2017-09-14 17:43:03 +02:00
|
|
|
|
2017-08-18 20:24:19 +02:00
|
|
|
self.current_dep_node_owner = dep_node_owner;
|
2020-02-10 14:29:21 +01:00
|
|
|
f(self, hash);
|
2017-08-18 20:24:19 +02:00
|
|
|
self.current_dep_node_owner = prev_owner;
|
|
|
|
}
|
2021-03-05 20:09:33 +01: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);
|
|
|
|
}
|
2016-04-14 11:55:34 +12:00
|
|
|
}
|
|
|
|
|
2017-08-18 20:24:19 +02:00
|
|
|
impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
|
2020-01-07 17:25:33 +01:00
|
|
|
type Map = Map<'hir>;
|
|
|
|
|
2016-04-14 11:55:34 +12:00
|
|
|
/// 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.
|
2016-11-09 16:45:26 -05:00
|
|
|
|
2020-02-09 15:32:00 +01:00
|
|
|
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
|
2019-09-06 03:57:44 +01:00
|
|
|
panic!("`visit_nested_xxx` must be manually implemented in this visitor");
|
2016-11-09 16:45:26 -05:00
|
|
|
}
|
|
|
|
|
2016-04-14 11:55:34 +12:00
|
|
|
fn visit_nested_item(&mut self, item: ItemId) {
|
|
|
|
debug!("visit_nested_item: {:?}", item);
|
2021-03-05 20:09:33 +01:00
|
|
|
self.insert_nested(item.def_id);
|
2021-01-30 12:06:04 +01:00
|
|
|
self.visit_item(self.krate.item(item));
|
2016-04-14 11:55:34 +12:00
|
|
|
}
|
|
|
|
|
2016-12-04 04:21:06 +02:00
|
|
|
fn visit_nested_trait_item(&mut self, item_id: TraitItemId) {
|
2021-03-05 20:09:33 +01:00
|
|
|
self.insert_nested(item_id.def_id);
|
2017-01-04 04:01:58 +02:00
|
|
|
self.visit_trait_item(self.krate.trait_item(item_id));
|
2016-12-04 04:21:06 +02:00
|
|
|
}
|
|
|
|
|
2016-11-04 18:20:15 -04:00
|
|
|
fn visit_nested_impl_item(&mut self, item_id: ImplItemId) {
|
2021-03-05 20:09:33 +01:00
|
|
|
self.insert_nested(item_id.def_id);
|
2017-01-04 04:01:58 +02:00
|
|
|
self.visit_impl_item(self.krate.impl_item(item_id));
|
2016-11-04 18:20:15 -04:00
|
|
|
}
|
|
|
|
|
2020-11-11 21:57:54 +01:00
|
|
|
fn visit_nested_foreign_item(&mut self, foreign_id: ForeignItemId) {
|
2021-03-05 20:09:33 +01:00
|
|
|
self.insert_nested(foreign_id.def_id);
|
2020-11-11 21:57:54 +01:00
|
|
|
self.visit_foreign_item(self.krate.foreign_item(foreign_id));
|
|
|
|
}
|
|
|
|
|
2016-12-21 12:32:59 +02:00
|
|
|
fn visit_nested_body(&mut self, id: BodyId) {
|
2017-01-04 04:01:58 +02:00
|
|
|
self.visit_body(self.krate.body(id));
|
2016-10-28 22:58:32 +02:00
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
fn visit_param(&mut self, param: &'hir Param<'hir>) {
|
2019-08-27 13:24:32 +02:00
|
|
|
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 19:52:37 -03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-28 19:28:50 +01:00
|
|
|
fn visit_item(&mut self, i: &'hir Item<'hir>) {
|
2016-04-14 11:55:34 +12:00
|
|
|
debug!("visit_item: {:?}", i);
|
2021-01-30 17:47:51 +01:00
|
|
|
self.with_dep_node_owner(i.def_id, i, |this, hash| {
|
|
|
|
let hir_id = i.hir_id();
|
|
|
|
this.insert_with_hash(i.span, hir_id, Node::Item(i), hash);
|
|
|
|
this.with_parent(hir_id, |this| {
|
2019-09-26 17:51:36 +01:00
|
|
|
if let ItemKind::Struct(ref struct_def, _) = i.kind {
|
2019-03-21 23:38:50 +01:00
|
|
|
// If this is a tuple or unit-like struct, register the constructor.
|
|
|
|
if let Some(ctor_hir_id) = struct_def.ctor_hir_id() {
|
2019-03-24 18:21:59 +01:00
|
|
|
this.insert(i.span, ctor_hir_id, Node::Ctor(struct_def));
|
2016-04-14 17:24:30 +12:00
|
|
|
}
|
2016-04-14 11:55:34 +12:00
|
|
|
}
|
2017-08-18 20:24:19 +02:00
|
|
|
intravisit::walk_item(this, i);
|
|
|
|
});
|
2016-04-14 17:24:30 +12:00
|
|
|
});
|
2016-04-14 11:55:34 +12:00
|
|
|
}
|
|
|
|
|
2020-11-11 21:57:54 +01:00
|
|
|
fn visit_foreign_item(&mut self, fi: &'hir ForeignItem<'hir>) {
|
2021-02-01 00:33:38 +01:00
|
|
|
self.with_dep_node_owner(fi.def_id, fi, |this, hash| {
|
|
|
|
this.insert_with_hash(fi.span, fi.hir_id(), Node::ForeignItem(fi), hash);
|
2016-04-14 11:55:34 +12:00
|
|
|
|
2021-02-01 00:33:38 +01:00
|
|
|
this.with_parent(fi.hir_id(), |this| {
|
2020-11-11 21:57:54 +01:00
|
|
|
intravisit::walk_foreign_item(this, fi);
|
|
|
|
});
|
2016-04-14 17:24:30 +12:00
|
|
|
});
|
2016-04-14 11:55:34 +12:00
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
fn visit_generic_param(&mut self, param: &'hir GenericParam<'hir>) {
|
2021-03-23 22:47:22 +01:00
|
|
|
self.insert(param.span, param.hir_id, Node::GenericParam(param));
|
|
|
|
intravisit::walk_generic_param(self, param);
|
2016-04-14 11:55:34 +12:00
|
|
|
}
|
|
|
|
|
2021-03-01 12:50:09 +01:00
|
|
|
fn visit_const_param_default(&mut self, param: HirId, ct: &'hir AnonConst) {
|
|
|
|
self.with_parent(param, |this| intravisit::walk_const_param_default(this, ct))
|
|
|
|
}
|
|
|
|
|
2019-11-28 21:47:10 +01:00
|
|
|
fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) {
|
2021-01-30 20:46:50 +01:00
|
|
|
self.with_dep_node_owner(ti.def_id, ti, |this, hash| {
|
|
|
|
this.insert_with_hash(ti.span, ti.hir_id(), Node::TraitItem(ti), hash);
|
2017-08-18 20:24:19 +02:00
|
|
|
|
2021-01-30 20:46:50 +01:00
|
|
|
this.with_parent(ti.hir_id(), |this| {
|
2017-08-18 20:24:19 +02:00
|
|
|
intravisit::walk_trait_item(this, ti);
|
|
|
|
});
|
2016-04-14 17:24:30 +12:00
|
|
|
});
|
2016-04-14 11:55:34 +12:00
|
|
|
}
|
|
|
|
|
2019-11-28 22:16:44 +01:00
|
|
|
fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) {
|
2021-01-30 23:25:03 +01:00
|
|
|
self.with_dep_node_owner(ii.def_id, ii, |this, hash| {
|
|
|
|
this.insert_with_hash(ii.span, ii.hir_id(), Node::ImplItem(ii), hash);
|
2017-08-18 20:24:19 +02:00
|
|
|
|
2021-01-30 23:25:03 +01:00
|
|
|
this.with_parent(ii.hir_id(), |this| {
|
2017-08-18 20:24:19 +02:00
|
|
|
intravisit::walk_impl_item(this, ii);
|
|
|
|
});
|
2016-04-14 17:24:30 +12:00
|
|
|
});
|
2016-04-14 11:55:34 +12:00
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
fn visit_pat(&mut self, pat: &'hir Pat<'hir>) {
|
2019-12-22 17:42:04 -05:00
|
|
|
let node =
|
|
|
|
if let PatKind::Binding(..) = pat.kind { Node::Binding(pat) } else { Node::Pat(pat) };
|
2019-02-15 10:30:44 +01:00
|
|
|
self.insert(pat.span, pat.hir_id, node);
|
2015-09-10 15:53:08 -04:00
|
|
|
|
2019-02-15 12:14:36 +01:00
|
|
|
self.with_parent(pat.hir_id, |this| {
|
2016-04-14 17:24:30 +12:00
|
|
|
intravisit::walk_pat(this, pat);
|
|
|
|
});
|
2015-09-10 15:53:08 -04:00
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01: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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-17 21:28:50 +03:00
|
|
|
fn visit_anon_const(&mut self, constant: &'hir AnonConst) {
|
2019-02-15 10:30:44 +01:00
|
|
|
self.insert(DUMMY_SP, constant.hir_id, Node::AnonConst(constant));
|
2018-05-17 21:28:50 +03:00
|
|
|
|
2019-02-15 12:14:36 +01:00
|
|
|
self.with_parent(constant.hir_id, |this| {
|
2018-05-17 21:28:50 +03:00
|
|
|
intravisit::walk_anon_const(this, constant);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
fn visit_expr(&mut self, expr: &'hir Expr<'hir>) {
|
2019-02-15 10:30:44 +01:00
|
|
|
self.insert(expr.span, expr.hir_id, Node::Expr(expr));
|
2015-09-10 15:53:08 -04:00
|
|
|
|
2019-02-15 12:14:36 +01:00
|
|
|
self.with_parent(expr.hir_id, |this| {
|
2016-04-14 17:24:30 +12:00
|
|
|
intravisit::walk_expr(this, expr);
|
|
|
|
});
|
2015-09-10 15:53:08 -04:00
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
fn visit_stmt(&mut self, stmt: &'hir Stmt<'hir>) {
|
2019-02-15 10:30:44 +01:00
|
|
|
self.insert(stmt.span, stmt.hir_id, Node::Stmt(stmt));
|
2016-04-14 17:24:30 +12:00
|
|
|
|
2019-02-15 12:14:36 +01:00
|
|
|
self.with_parent(stmt.hir_id, |this| {
|
2016-04-14 17:24:30 +12:00
|
|
|
intravisit::walk_stmt(this, stmt);
|
|
|
|
});
|
2015-09-10 15:53:08 -04:00
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
fn visit_path_segment(&mut self, path_span: Span, path_segment: &'hir PathSegment<'hir>) {
|
2019-02-15 15:21:56 +01:00
|
|
|
if let Some(hir_id) = path_segment.hir_id {
|
2019-02-15 10:30:44 +01:00
|
|
|
self.insert(path_span, hir_id, Node::PathSegment(path_segment));
|
2018-09-11 15:08:47 +12:00
|
|
|
}
|
|
|
|
intravisit::walk_path_segment(self, path_span, path_segment);
|
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
fn visit_ty(&mut self, ty: &'hir Ty<'hir>) {
|
2019-02-15 10:30:44 +01:00
|
|
|
self.insert(ty.span, ty.hir_id, Node::Ty(ty));
|
2016-08-06 03:12:20 +03:00
|
|
|
|
2019-02-15 12:14:36 +01:00
|
|
|
self.with_parent(ty.hir_id, |this| {
|
2016-08-06 03:12:20 +03:00
|
|
|
intravisit::walk_ty(this, ty);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
fn visit_trait_ref(&mut self, tr: &'hir TraitRef<'hir>) {
|
2019-02-15 10:30:44 +01:00
|
|
|
self.insert(tr.path.span, tr.hir_ref_id, Node::TraitRef(tr));
|
2016-10-30 08:04:52 +02:00
|
|
|
|
2019-02-15 12:14:36 +01:00
|
|
|
self.with_parent(tr.hir_ref_id, |this| {
|
2016-10-30 08:04:52 +02:00
|
|
|
intravisit::walk_trait_ref(this, tr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn visit_fn(
|
|
|
|
&mut self,
|
|
|
|
fk: intravisit::FnKind<'hir>,
|
2019-11-30 17:46:46 +01:00
|
|
|
fd: &'hir FnDecl<'hir>,
|
2019-12-22 17:42:04 -05:00
|
|
|
b: BodyId,
|
|
|
|
s: Span,
|
|
|
|
id: HirId,
|
|
|
|
) {
|
2019-02-15 15:21:56 +01:00
|
|
|
assert_eq!(self.parent_node, id);
|
2016-07-28 05:58:45 -04:00
|
|
|
intravisit::walk_fn(self, fk, fd, b, s, id);
|
2015-09-10 15:53:08 -04:00
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
fn visit_block(&mut self, block: &'hir Block<'hir>) {
|
2019-02-15 10:30:44 +01:00
|
|
|
self.insert(block.span, block.hir_id, Node::Block(block));
|
2019-02-15 12:14:36 +01:00
|
|
|
self.with_parent(block.hir_id, |this| {
|
2016-04-14 17:24:30 +12:00
|
|
|
intravisit::walk_block(this, block);
|
|
|
|
});
|
2015-09-10 15:53:08 -04:00
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
fn visit_local(&mut self, l: &'hir Local<'hir>) {
|
2019-02-15 10:30:44 +01:00
|
|
|
self.insert(l.span, l.hir_id, Node::Local(l));
|
2019-12-22 17:42:04 -05:00
|
|
|
self.with_parent(l.hir_id, |this| intravisit::walk_local(this, l))
|
2017-08-17 23:56:11 -07:00
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
fn visit_lifetime(&mut self, lifetime: &'hir Lifetime) {
|
2019-02-15 10:30:44 +01:00
|
|
|
self.insert(lifetime.span, lifetime.hir_id, Node::Lifetime(lifetime));
|
2015-09-10 15:53:08 -04:00
|
|
|
}
|
2016-10-28 06:52:45 +00:00
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
fn visit_vis(&mut self, visibility: &'hir Visibility<'hir>) {
|
2018-06-30 20:34:18 -07:00
|
|
|
match visibility.node {
|
2019-12-22 17:42:04 -05:00
|
|
|
VisibilityKind::Public | VisibilityKind::Crate(_) | VisibilityKind::Inherited => {}
|
2019-02-15 12:14:36 +01:00
|
|
|
VisibilityKind::Restricted { hir_id, .. } => {
|
2019-02-15 10:30:44 +01:00
|
|
|
self.insert(visibility.span, hir_id, Node::Visibility(visibility));
|
2019-02-15 12:14:36 +01:00
|
|
|
self.with_parent(hir_id, |this| {
|
2016-10-30 08:04:52 +02:00
|
|
|
intravisit::walk_vis(this, visibility);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 23:50:47 +01:00
|
|
|
fn visit_macro_def(&mut self, macro_def: &'hir MacroDef<'hir>) {
|
2020-12-27 17:57:17 +01:00
|
|
|
// Exported macros are visited directly from the crate root,
|
|
|
|
// so they do not have `parent_node` set.
|
|
|
|
// Find the correct enclosing module from their DefKey.
|
2021-01-31 18:20:18 +01:00
|
|
|
let def_key = self.definitions.def_key(macro_def.def_id);
|
2020-12-27 17:57:17 +01:00
|
|
|
let parent = def_key.parent.map_or(hir::CRATE_HIR_ID, |local_def_index| {
|
|
|
|
self.definitions.local_def_id_to_hir_id(LocalDefId { local_def_index })
|
|
|
|
});
|
|
|
|
self.with_parent(parent, |this| {
|
2021-03-05 20:09:33 +01:00
|
|
|
this.insert_nested(macro_def.def_id);
|
2021-01-31 18:20:18 +01:00
|
|
|
this.with_dep_node_owner(macro_def.def_id, macro_def, |this, hash| {
|
2020-12-27 17:57:17 +01:00
|
|
|
this.insert_with_hash(
|
|
|
|
macro_def.span,
|
2021-01-31 18:20:18 +01:00
|
|
|
macro_def.hir_id(),
|
2020-12-27 17:57:17 +01:00
|
|
|
Node::MacroDef(macro_def),
|
|
|
|
hash,
|
|
|
|
);
|
|
|
|
})
|
2017-09-14 17:43:03 +02:00
|
|
|
});
|
2016-10-28 06:52:45 +00:00
|
|
|
}
|
2016-11-09 20:57:48 +02:00
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
fn visit_variant(&mut self, v: &'hir Variant<'hir>, g: &'hir Generics<'hir>, item_id: HirId) {
|
2019-08-13 21:40:21 -03:00
|
|
|
self.insert(v.span, v.id, Node::Variant(v));
|
|
|
|
self.with_parent(v.id, |this| {
|
2019-03-21 23:38:50 +01:00
|
|
|
// Register the constructor of this variant.
|
2019-08-13 21:40:21 -03:00
|
|
|
if let Some(ctor_hir_id) = v.data.ctor_hir_id() {
|
|
|
|
this.insert(v.span, ctor_hir_id, Node::Ctor(&v.data));
|
2019-03-21 23:38:50 +01:00
|
|
|
}
|
2016-12-21 12:32:59 +02:00
|
|
|
intravisit::walk_variant(this, v, g, item_id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-16 00:36:07 +03:00
|
|
|
fn visit_field_def(&mut self, field: &'hir FieldDef<'hir>) {
|
2019-02-15 10:30:44 +01:00
|
|
|
self.insert(field.span, field.hir_id, Node::Field(field));
|
2019-02-15 12:14:36 +01:00
|
|
|
self.with_parent(field.hir_id, |this| {
|
2021-03-16 00:36:07 +03:00
|
|
|
intravisit::walk_field_def(this, field);
|
2016-11-09 20:57:48 +02:00
|
|
|
});
|
|
|
|
}
|
2017-08-18 20:24:19 +02: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 17:42:04 -05:00
|
|
|
let TraitItemRef { id, ident: _, kind: _, span: _, defaultness: _ } = *ii;
|
2017-08-18 20:24:19 +02:00
|
|
|
|
|
|
|
self.visit_nested_trait_item(id);
|
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef<'hir>) {
|
2017-08-18 20:24:19 +02:00
|
|
|
// Do not visit the duplicate information in ImplItemRef. We want to
|
|
|
|
// map the actual nodes, not the duplicate ones in the *Ref.
|
2019-12-22 17:42:04 -05:00
|
|
|
let ImplItemRef { id, ident: _, kind: _, span: _, vis: _, defaultness: _ } = *ii;
|
2017-08-18 20:24:19 +02:00
|
|
|
|
|
|
|
self.visit_nested_impl_item(id);
|
|
|
|
}
|
2020-11-28 18:08:11 +01:00
|
|
|
|
|
|
|
fn visit_foreign_item_ref(&mut self, fi: &'hir ForeignItemRef<'hir>) {
|
|
|
|
// Do not visit the duplicate information in ForeignItemRef. We want to
|
|
|
|
// map the actual nodes, not the duplicate ones in the *Ref.
|
|
|
|
let ForeignItemRef { id, ident: _, span: _, vis: _ } = *fi;
|
|
|
|
|
|
|
|
self.visit_nested_foreign_item(id);
|
|
|
|
}
|
2015-09-10 15:53:08 -04:00
|
|
|
}
|