Auto merge of #44012 - michaelwoerister:cache-hir-dep-node-index, r=nikomatsakis

incr.comp.: Cache Hir-DepNodeIndices in the HIR map.

In preparation for red/green. This should also be faster than before without any additional memory cost.

r? @nikomatsakis
This commit is contained in:
bors 2017-08-25 06:40:10 +00:00
commit 32b50e280f
8 changed files with 341 additions and 228 deletions

View file

@ -244,8 +244,8 @@ impl DepGraphEdges {
self.edges.insert((source, target)); self.edges.insert((source, target));
} }
pub fn add_node(&mut self, node: DepNode) { pub fn add_node(&mut self, node: DepNode) -> DepNodeIndex {
self.get_or_create_node(node); self.get_or_create_node(node)
} }
#[inline] #[inline]

View file

@ -173,6 +173,14 @@ impl DepGraph {
self.data.as_ref().unwrap().edges.borrow_mut().add_node(node); self.data.as_ref().unwrap().edges.borrow_mut().add_node(node);
} }
pub fn alloc_input_node(&self, node: DepNode) -> DepNodeIndex {
if let Some(ref data) = self.data {
data.edges.borrow_mut().add_node(node)
} else {
DepNodeIndex::INVALID
}
}
/// Indicates that a previous work product exists for `v`. This is /// Indicates that a previous work product exists for `v`. This is
/// invoked during initial start-up based on what nodes are clean /// invoked during initial start-up based on what nodes are clean
/// (and what files exist in the incr. directory). /// (and what files exist in the incr. directory).

View file

@ -827,7 +827,16 @@ pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref:
pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) { pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) {
// NB: Deliberately force a compilation error if/when new fields are added. // NB: Deliberately force a compilation error if/when new fields are added.
let ImplItem { id: _, name, ref vis, ref defaultness, ref attrs, ref node, span } = *impl_item; let ImplItem {
id: _,
hir_id: _,
name,
ref vis,
ref defaultness,
ref attrs,
ref node,
span
} = *impl_item;
visitor.visit_name(span, name); visitor.visit_name(span, name);
visitor.visit_vis(vis); visitor.visit_vis(vis);

View file

@ -1315,7 +1315,12 @@ impl<'a> LoweringContext<'a> {
path.span = span; path.span = span;
self.allocate_hir_id_counter(import.id, import); self.allocate_hir_id_counter(import.id, import);
self.with_hir_id_owner(import.id, |this| { let LoweredNodeId {
node_id: import_node_id,
hir_id: import_hir_id,
} = self.lower_node_id(import.id);
self.with_hir_id_owner(import_node_id, |this| {
let vis = match *vis { let vis = match *vis {
hir::Visibility::Public => hir::Visibility::Public, hir::Visibility::Public => hir::Visibility::Public,
hir::Visibility::Crate => hir::Visibility::Crate, hir::Visibility::Crate => hir::Visibility::Crate,
@ -1329,8 +1334,9 @@ impl<'a> LoweringContext<'a> {
} }
}; };
this.items.insert(import.id, hir::Item { this.items.insert(import_node_id, hir::Item {
id: import.id, id: import_node_id,
hir_id: import_hir_id,
name: import.rename.unwrap_or(ident).name, name: import.rename.unwrap_or(ident).name,
attrs: attrs.clone(), attrs: attrs.clone(),
node: hir::ItemUse(P(path), hir::UseKind::Single), node: hir::ItemUse(P(path), hir::UseKind::Single),
@ -1461,8 +1467,11 @@ impl<'a> LoweringContext<'a> {
fn lower_trait_item(&mut self, i: &TraitItem) -> hir::TraitItem { fn lower_trait_item(&mut self, i: &TraitItem) -> hir::TraitItem {
self.with_parent_def(i.id, |this| { self.with_parent_def(i.id, |this| {
let LoweredNodeId { node_id, hir_id } = this.lower_node_id(i.id);
hir::TraitItem { hir::TraitItem {
id: this.lower_node_id(i.id).node_id, id: node_id,
hir_id,
name: this.lower_ident(i.ident), name: this.lower_ident(i.ident),
attrs: this.lower_attrs(&i.attrs), attrs: this.lower_attrs(&i.attrs),
node: match i.node { node: match i.node {
@ -1522,8 +1531,11 @@ impl<'a> LoweringContext<'a> {
fn lower_impl_item(&mut self, i: &ImplItem) -> hir::ImplItem { fn lower_impl_item(&mut self, i: &ImplItem) -> hir::ImplItem {
self.with_parent_def(i.id, |this| { self.with_parent_def(i.id, |this| {
let LoweredNodeId { node_id, hir_id } = this.lower_node_id(i.id);
hir::ImplItem { hir::ImplItem {
id: this.lower_node_id(i.id).node_id, id: node_id,
hir_id,
name: this.lower_ident(i.ident), name: this.lower_ident(i.ident),
attrs: this.lower_attrs(&i.attrs), attrs: this.lower_attrs(&i.attrs),
vis: this.lower_visibility(&i.vis, None), vis: this.lower_visibility(&i.vis, None),
@ -1614,8 +1626,11 @@ impl<'a> LoweringContext<'a> {
this.lower_item_kind(i.id, &mut name, &attrs, &mut vis, &i.node) this.lower_item_kind(i.id, &mut name, &attrs, &mut vis, &i.node)
}); });
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(i.id);
Some(hir::Item { Some(hir::Item {
id: self.lower_node_id(i.id).node_id, id: node_id,
hir_id,
name, name,
attrs, attrs,
node, node,

View file

@ -10,33 +10,55 @@
use super::*; use super::*;
use dep_graph::{DepGraph, DepKind, DepNodeIndex};
use hir::intravisit::{Visitor, NestedVisitorMap}; use hir::intravisit::{Visitor, NestedVisitorMap};
use std::iter::repeat; use std::iter::repeat;
use syntax::ast::{NodeId, CRATE_NODE_ID}; use syntax::ast::{NodeId, CRATE_NODE_ID};
use syntax_pos::Span; use syntax_pos::Span;
/// A Visitor that walks over the HIR and collects Nodes into a HIR map /// A Visitor that walks over the HIR and collects Nodes into a HIR map
pub struct NodeCollector<'hir> { pub(super) struct NodeCollector<'a, 'hir> {
/// The crate /// The crate
pub krate: &'hir Crate, krate: &'hir Crate,
/// The node map /// The node map
pub(super) map: Vec<MapEntry<'hir>>, map: Vec<MapEntry<'hir>>,
/// The parent of this node /// The parent of this node
pub parent_node: NodeId, parent_node: NodeId,
current_dep_node_owner: DefIndex,
current_dep_node_index: DepNodeIndex,
dep_graph: &'a DepGraph,
definitions: &'a definitions::Definitions,
} }
impl<'hir> NodeCollector<'hir> { impl<'a, 'hir> NodeCollector<'a, 'hir> {
pub fn root(krate: &'hir Crate) -> NodeCollector<'hir> { pub(super) fn root(krate: &'hir Crate,
dep_graph: &'a DepGraph,
definitions: &'a definitions::Definitions)
-> NodeCollector<'a, 'hir> {
let root_mod_def_path_hash = definitions.def_path_hash(CRATE_DEF_INDEX);
let root_mod_dep_node = root_mod_def_path_hash.to_dep_node(DepKind::Hir);
let root_mod_dep_node_index = dep_graph.alloc_input_node(root_mod_dep_node);
let mut collector = NodeCollector { let mut collector = NodeCollector {
krate, krate,
map: vec![], map: vec![],
parent_node: CRATE_NODE_ID, parent_node: CRATE_NODE_ID,
current_dep_node_index: root_mod_dep_node_index,
current_dep_node_owner: CRATE_DEF_INDEX,
dep_graph,
definitions,
}; };
collector.insert_entry(CRATE_NODE_ID, RootCrate); collector.insert_entry(CRATE_NODE_ID, RootCrate(root_mod_dep_node_index));
collector collector
} }
pub(super) fn into_map(self) -> Vec<MapEntry<'hir>> {
self.map
}
fn insert_entry(&mut self, id: NodeId, entry: MapEntry<'hir>) { fn insert_entry(&mut self, id: NodeId, entry: MapEntry<'hir>) {
debug!("hir_map: {:?} => {:?}", id, entry); debug!("hir_map: {:?} => {:?}", id, entry);
let len = self.map.len(); let len = self.map.len();
@ -47,8 +69,55 @@ impl<'hir> NodeCollector<'hir> {
} }
fn insert(&mut self, id: NodeId, node: Node<'hir>) { fn insert(&mut self, id: NodeId, node: Node<'hir>) {
let entry = MapEntry::from_node(self.parent_node, node); let parent = self.parent_node;
let dep_node_index = self.current_dep_node_index;
let entry = match node {
NodeItem(n) => EntryItem(parent, dep_node_index, n),
NodeForeignItem(n) => EntryForeignItem(parent, dep_node_index, n),
NodeTraitItem(n) => EntryTraitItem(parent, dep_node_index, n),
NodeImplItem(n) => EntryImplItem(parent, dep_node_index, n),
NodeVariant(n) => EntryVariant(parent, dep_node_index, n),
NodeField(n) => EntryField(parent, dep_node_index, n),
NodeExpr(n) => EntryExpr(parent, dep_node_index, n),
NodeStmt(n) => EntryStmt(parent, dep_node_index, n),
NodeTy(n) => EntryTy(parent, dep_node_index, n),
NodeTraitRef(n) => EntryTraitRef(parent, dep_node_index, n),
NodeBinding(n) => EntryBinding(parent, dep_node_index, n),
NodePat(n) => EntryPat(parent, dep_node_index, n),
NodeBlock(n) => EntryBlock(parent, dep_node_index, n),
NodeStructCtor(n) => EntryStructCtor(parent, dep_node_index, n),
NodeLifetime(n) => EntryLifetime(parent, dep_node_index, n),
NodeTyParam(n) => EntryTyParam(parent, dep_node_index, n),
NodeVisibility(n) => EntryVisibility(parent, dep_node_index, n),
NodeLocal(n) => EntryLocal(parent, dep_node_index, n),
};
// Make sure that the DepNode of some node coincides with the HirId
// owner of that node.
if cfg!(debug_assertions) {
let hir_id_owner = self.definitions.node_to_hir_id(id).owner;
if hir_id_owner != self.current_dep_node_owner {
let node_str = match self.definitions.opt_def_index(id) {
Some(def_index) => {
self.definitions.def_path(def_index).to_string_no_crate()
}
None => format!("{:?}", node)
};
bug!("inconsistent DepNode for `{}`: \
current_dep_node_owner={}, hir_id.owner={}",
node_str,
self.definitions
.def_path(self.current_dep_node_owner)
.to_string_no_crate(),
self.definitions.def_path(hir_id_owner).to_string_no_crate())
}
}
self.insert_entry(id, entry); self.insert_entry(id, entry);
} }
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_id: NodeId, f: F) { fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_id: NodeId, f: F) {
@ -57,9 +126,27 @@ impl<'hir> NodeCollector<'hir> {
f(self); f(self);
self.parent_node = parent_node; self.parent_node = parent_node;
} }
fn with_dep_node_owner<F: FnOnce(&mut Self)>(&mut self,
dep_node_owner: DefIndex,
f: F) {
let prev_owner = self.current_dep_node_owner;
let prev_index = self.current_dep_node_index;
// When we enter a new owner (item, impl item, or trait item), we always
// start out again with DepKind::Hir.
let new_dep_node = self.definitions
.def_path_hash(dep_node_owner)
.to_dep_node(DepKind::Hir);
self.current_dep_node_index = self.dep_graph.alloc_input_node(new_dep_node);
self.current_dep_node_owner = dep_node_owner;
f(self);
self.current_dep_node_index = prev_index;
self.current_dep_node_owner = prev_owner;
}
} }
impl<'hir> Visitor<'hir> for NodeCollector<'hir> { impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
/// Because we want to track parent items and so forth, enable /// Because we want to track parent items and so forth, enable
/// deep walking so that we walk nested items in the context of /// deep walking so that we walk nested items in the context of
/// their outer items. /// their outer items.
@ -82,25 +169,37 @@ impl<'hir> Visitor<'hir> for NodeCollector<'hir> {
} }
fn visit_nested_body(&mut self, id: BodyId) { fn visit_nested_body(&mut self, id: BodyId) {
// When we enter a body, we switch to DepKind::HirBody.
// Note that current_dep_node_index might already be DepKind::HirBody,
// e.g. when entering the body of a closure that is already part of a
// surrounding body. That's expected and not a problem.
let prev_index = self.current_dep_node_index;
let new_dep_node = self.definitions
.def_path_hash(self.current_dep_node_owner)
.to_dep_node(DepKind::HirBody);
self.current_dep_node_index = self.dep_graph.alloc_input_node(new_dep_node);
self.visit_body(self.krate.body(id)); self.visit_body(self.krate.body(id));
self.current_dep_node_index = prev_index;
} }
fn visit_item(&mut self, i: &'hir Item) { fn visit_item(&mut self, i: &'hir Item) {
debug!("visit_item: {:?}", i); debug!("visit_item: {:?}", i);
debug_assert_eq!(i.hir_id.owner,
self.insert(i.id, NodeItem(i)); self.definitions.opt_def_index(i.id).unwrap());
self.with_dep_node_owner(i.hir_id.owner, |this| {
self.with_parent(i.id, |this| { this.insert(i.id, NodeItem(i));
match i.node { this.with_parent(i.id, |this| {
ItemStruct(ref struct_def, _) => { match i.node {
// If this is a tuple-like struct, register the constructor. ItemStruct(ref struct_def, _) => {
if !struct_def.is_struct() { // If this is a tuple-like struct, register the constructor.
this.insert(struct_def.id(), NodeStructCtor(struct_def)); if !struct_def.is_struct() {
this.insert(struct_def.id(), NodeStructCtor(struct_def));
}
} }
_ => {}
} }
_ => {} intravisit::walk_item(this, i);
} });
intravisit::walk_item(this, i);
}); });
} }
@ -121,18 +220,26 @@ impl<'hir> Visitor<'hir> for NodeCollector<'hir> {
} }
fn visit_trait_item(&mut self, ti: &'hir TraitItem) { fn visit_trait_item(&mut self, ti: &'hir TraitItem) {
self.insert(ti.id, NodeTraitItem(ti)); debug_assert_eq!(ti.hir_id.owner,
self.definitions.opt_def_index(ti.id).unwrap());
self.with_dep_node_owner(ti.hir_id.owner, |this| {
this.insert(ti.id, NodeTraitItem(ti));
self.with_parent(ti.id, |this| { this.with_parent(ti.id, |this| {
intravisit::walk_trait_item(this, ti); intravisit::walk_trait_item(this, ti);
});
}); });
} }
fn visit_impl_item(&mut self, ii: &'hir ImplItem) { fn visit_impl_item(&mut self, ii: &'hir ImplItem) {
self.insert(ii.id, NodeImplItem(ii)); debug_assert_eq!(ii.hir_id.owner,
self.definitions.opt_def_index(ii.id).unwrap());
self.with_dep_node_owner(ii.hir_id.owner, |this| {
this.insert(ii.id, NodeImplItem(ii));
self.with_parent(ii.id, |this| { this.with_parent(ii.id, |this| {
intravisit::walk_impl_item(this, ii); intravisit::walk_impl_item(this, ii);
});
}); });
} }
@ -238,4 +345,33 @@ impl<'hir> Visitor<'hir> for NodeCollector<'hir> {
intravisit::walk_struct_field(this, field); intravisit::walk_struct_field(this, field);
}); });
} }
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.
let TraitItemRef {
id,
name: _,
kind: _,
span: _,
defaultness: _,
} = *ii;
self.visit_nested_trait_item(id);
}
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.
let ImplItemRef {
id,
name: _,
kind: _,
span: _,
vis: _,
defaultness: _,
} = *ii;
self.visit_nested_impl_item(id);
}
} }

View file

@ -15,7 +15,7 @@ pub use self::def_collector::{DefCollector, MacroInvocationData};
pub use self::definitions::{Definitions, DefKey, DefPath, DefPathData, pub use self::definitions::{Definitions, DefKey, DefPath, DefPathData,
DisambiguatedDefPathData, DefPathHash}; DisambiguatedDefPathData, DefPathHash};
use dep_graph::{DepGraph, DepNode, DepKind}; use dep_graph::{DepGraph, DepNode, DepKind, DepNodeIndex};
use hir::def_id::{CRATE_DEF_INDEX, DefId, DefIndexAddressSpace}; use hir::def_id::{CRATE_DEF_INDEX, DefId, DefIndexAddressSpace};
@ -74,27 +74,28 @@ enum MapEntry<'hir> {
NotPresent, NotPresent,
/// All the node types, with a parent ID. /// All the node types, with a parent ID.
EntryItem(NodeId, &'hir Item), EntryItem(NodeId, DepNodeIndex, &'hir Item),
EntryForeignItem(NodeId, &'hir ForeignItem), EntryForeignItem(NodeId, DepNodeIndex, &'hir ForeignItem),
EntryTraitItem(NodeId, &'hir TraitItem), EntryTraitItem(NodeId, DepNodeIndex, &'hir TraitItem),
EntryImplItem(NodeId, &'hir ImplItem), EntryImplItem(NodeId, DepNodeIndex, &'hir ImplItem),
EntryVariant(NodeId, &'hir Variant), EntryVariant(NodeId, DepNodeIndex, &'hir Variant),
EntryField(NodeId, &'hir StructField), EntryField(NodeId, DepNodeIndex, &'hir StructField),
EntryExpr(NodeId, &'hir Expr), EntryExpr(NodeId, DepNodeIndex, &'hir Expr),
EntryStmt(NodeId, &'hir Stmt), EntryStmt(NodeId, DepNodeIndex, &'hir Stmt),
EntryTy(NodeId, &'hir Ty), EntryTy(NodeId, DepNodeIndex, &'hir Ty),
EntryTraitRef(NodeId, &'hir TraitRef), EntryTraitRef(NodeId, DepNodeIndex, &'hir TraitRef),
EntryBinding(NodeId, &'hir Pat), EntryBinding(NodeId, DepNodeIndex, &'hir Pat),
EntryPat(NodeId, &'hir Pat), EntryPat(NodeId, DepNodeIndex, &'hir Pat),
EntryBlock(NodeId, &'hir Block), EntryBlock(NodeId, DepNodeIndex, &'hir Block),
EntryStructCtor(NodeId, &'hir VariantData), EntryStructCtor(NodeId, DepNodeIndex, &'hir VariantData),
EntryLifetime(NodeId, &'hir Lifetime), EntryLifetime(NodeId, DepNodeIndex, &'hir Lifetime),
EntryTyParam(NodeId, &'hir TyParam), EntryTyParam(NodeId, DepNodeIndex, &'hir TyParam),
EntryVisibility(NodeId, &'hir Visibility), EntryVisibility(NodeId, DepNodeIndex, &'hir Visibility),
EntryLocal(NodeId, &'hir Local), EntryLocal(NodeId, DepNodeIndex, &'hir Local),
/// Roots for node trees. /// Roots for node trees. The DepNodeIndex is the dependency node of the
RootCrate, /// crate's root module.
RootCrate(DepNodeIndex),
} }
impl<'hir> Clone for MapEntry<'hir> { impl<'hir> Clone for MapEntry<'hir> {
@ -104,82 +105,59 @@ impl<'hir> Clone for MapEntry<'hir> {
} }
impl<'hir> MapEntry<'hir> { impl<'hir> MapEntry<'hir> {
fn from_node(p: NodeId, node: Node<'hir>) -> MapEntry<'hir> {
match node {
NodeItem(n) => EntryItem(p, n),
NodeForeignItem(n) => EntryForeignItem(p, n),
NodeTraitItem(n) => EntryTraitItem(p, n),
NodeImplItem(n) => EntryImplItem(p, n),
NodeVariant(n) => EntryVariant(p, n),
NodeField(n) => EntryField(p, n),
NodeExpr(n) => EntryExpr(p, n),
NodeStmt(n) => EntryStmt(p, n),
NodeTy(n) => EntryTy(p, n),
NodeTraitRef(n) => EntryTraitRef(p, n),
NodeBinding(n) => EntryBinding(p, n),
NodePat(n) => EntryPat(p, n),
NodeBlock(n) => EntryBlock(p, n),
NodeStructCtor(n) => EntryStructCtor(p, n),
NodeLifetime(n) => EntryLifetime(p, n),
NodeTyParam(n) => EntryTyParam(p, n),
NodeVisibility(n) => EntryVisibility(p, n),
NodeLocal(n) => EntryLocal(p, n),
}
}
fn parent_node(self) -> Option<NodeId> { fn parent_node(self) -> Option<NodeId> {
Some(match self { Some(match self {
EntryItem(id, _) => id, EntryItem(id, _, _) => id,
EntryForeignItem(id, _) => id, EntryForeignItem(id, _, _) => id,
EntryTraitItem(id, _) => id, EntryTraitItem(id, _, _) => id,
EntryImplItem(id, _) => id, EntryImplItem(id, _, _) => id,
EntryVariant(id, _) => id, EntryVariant(id, _, _) => id,
EntryField(id, _) => id, EntryField(id, _, _) => id,
EntryExpr(id, _) => id, EntryExpr(id, _, _) => id,
EntryStmt(id, _) => id, EntryStmt(id, _, _) => id,
EntryTy(id, _) => id, EntryTy(id, _, _) => id,
EntryTraitRef(id, _) => id, EntryTraitRef(id, _, _) => id,
EntryBinding(id, _) => id, EntryBinding(id, _, _) => id,
EntryPat(id, _) => id, EntryPat(id, _, _) => id,
EntryBlock(id, _) => id, EntryBlock(id, _, _) => id,
EntryStructCtor(id, _) => id, EntryStructCtor(id, _, _) => id,
EntryLifetime(id, _) => id, EntryLifetime(id, _, _) => id,
EntryTyParam(id, _) => id, EntryTyParam(id, _, _) => id,
EntryVisibility(id, _) => id, EntryVisibility(id, _, _) => id,
EntryLocal(id, _) => id, EntryLocal(id, _, _) => id,
NotPresent | NotPresent |
RootCrate => return None, RootCrate(_) => return None,
}) })
} }
fn to_node(self) -> Option<Node<'hir>> { fn to_node(self) -> Option<Node<'hir>> {
Some(match self { Some(match self {
EntryItem(_, n) => NodeItem(n), EntryItem(_, _, n) => NodeItem(n),
EntryForeignItem(_, n) => NodeForeignItem(n), EntryForeignItem(_, _, n) => NodeForeignItem(n),
EntryTraitItem(_, n) => NodeTraitItem(n), EntryTraitItem(_, _, n) => NodeTraitItem(n),
EntryImplItem(_, n) => NodeImplItem(n), EntryImplItem(_, _, n) => NodeImplItem(n),
EntryVariant(_, n) => NodeVariant(n), EntryVariant(_, _, n) => NodeVariant(n),
EntryField(_, n) => NodeField(n), EntryField(_, _, n) => NodeField(n),
EntryExpr(_, n) => NodeExpr(n), EntryExpr(_, _, n) => NodeExpr(n),
EntryStmt(_, n) => NodeStmt(n), EntryStmt(_, _, n) => NodeStmt(n),
EntryTy(_, n) => NodeTy(n), EntryTy(_, _, n) => NodeTy(n),
EntryTraitRef(_, n) => NodeTraitRef(n), EntryTraitRef(_, _, n) => NodeTraitRef(n),
EntryBinding(_, n) => NodeBinding(n), EntryBinding(_, _, n) => NodeBinding(n),
EntryPat(_, n) => NodePat(n), EntryPat(_, _, n) => NodePat(n),
EntryBlock(_, n) => NodeBlock(n), EntryBlock(_, _, n) => NodeBlock(n),
EntryStructCtor(_, n) => NodeStructCtor(n), EntryStructCtor(_, _, n) => NodeStructCtor(n),
EntryLifetime(_, n) => NodeLifetime(n), EntryLifetime(_, _, n) => NodeLifetime(n),
EntryTyParam(_, n) => NodeTyParam(n), EntryTyParam(_, _, n) => NodeTyParam(n),
EntryVisibility(_, n) => NodeVisibility(n), EntryVisibility(_, _, n) => NodeVisibility(n),
EntryLocal(_, n) => NodeLocal(n), EntryLocal(_, _, n) => NodeLocal(n),
_ => return None _ => return None
}) })
} }
fn associated_body(self) -> Option<BodyId> { fn associated_body(self) -> Option<BodyId> {
match self { match self {
EntryItem(_, item) => { EntryItem(_, _, item) => {
match item.node { match item.node {
ItemConst(_, body) | ItemConst(_, body) |
ItemStatic(.., body) | ItemStatic(.., body) |
@ -188,7 +166,7 @@ impl<'hir> MapEntry<'hir> {
} }
} }
EntryTraitItem(_, item) => { EntryTraitItem(_, _, item) => {
match item.node { match item.node {
TraitItemKind::Const(_, Some(body)) | TraitItemKind::Const(_, Some(body)) |
TraitItemKind::Method(_, TraitMethod::Provided(body)) => Some(body), TraitItemKind::Method(_, TraitMethod::Provided(body)) => Some(body),
@ -196,7 +174,7 @@ impl<'hir> MapEntry<'hir> {
} }
} }
EntryImplItem(_, item) => { EntryImplItem(_, _, item) => {
match item.node { match item.node {
ImplItemKind::Const(_, body) | ImplItemKind::Const(_, body) |
ImplItemKind::Method(_, body) => Some(body), ImplItemKind::Method(_, body) => Some(body),
@ -204,7 +182,7 @@ impl<'hir> MapEntry<'hir> {
} }
} }
EntryExpr(_, expr) => { EntryExpr(_, _, expr) => {
match expr.node { match expr.node {
ExprClosure(.., body, _) => Some(body), ExprClosure(.., body, _) => Some(body),
_ => None, _ => None,
@ -282,83 +260,40 @@ impl<'hir> Map<'hir> {
/// read recorded). If the function just returns a DefId or /// read recorded). If the function just returns a DefId or
/// NodeId, no actual content was returned, so no read is needed. /// NodeId, no actual content was returned, so no read is needed.
pub fn read(&self, id: NodeId) { pub fn read(&self, id: NodeId) {
self.dep_graph.read(self.dep_node(id)); let entry = self.map[id.as_usize()];
} match entry {
EntryItem(_, dep_node_index, _) |
fn dep_node(&self, id0: NodeId) -> DepNode { EntryTraitItem(_, dep_node_index, _) |
let mut id = id0; EntryImplItem(_, dep_node_index, _) |
let mut last_expr = None; EntryVariant(_, dep_node_index, _) |
loop { EntryForeignItem(_, dep_node_index, _) |
let entry = self.map[id.as_usize()]; EntryField(_, dep_node_index, _) |
match entry { EntryStmt(_, dep_node_index, _) |
EntryItem(..) | EntryTy(_, dep_node_index, _) |
EntryTraitItem(..) | EntryTraitRef(_, dep_node_index, _) |
EntryImplItem(..) => { EntryBinding(_, dep_node_index, _) |
let def_index = self.definitions.opt_def_index(id).unwrap(); EntryPat(_, dep_node_index, _) |
EntryBlock(_, dep_node_index, _) |
EntryStructCtor(_, dep_node_index, _) |
EntryLifetime(_, dep_node_index, _) |
EntryTyParam(_, dep_node_index, _) |
EntryVisibility(_, dep_node_index, _) |
EntryExpr(_, dep_node_index, _) |
EntryLocal(_, dep_node_index, _) |
RootCrate(dep_node_index) => {
self.dep_graph.read_index(dep_node_index);
}
NotPresent => {
// Some nodes, notably macro definitions, are not
// present in the map for whatever reason, but
// they *do* have def-ids. So if we encounter an
// empty hole, check for that case.
if let Some(def_index) = self.definitions.opt_def_index(id) {
let def_path_hash = self.definitions.def_path_hash(def_index); let def_path_hash = self.definitions.def_path_hash(def_index);
self.dep_graph.read(def_path_hash.to_dep_node(DepKind::Hir));
if let Some(last_id) = last_expr { } else {
// The body may have a separate dep node bug!("called HirMap::read() with invalid NodeId")
if entry.is_body_owner(last_id) {
return def_path_hash.to_dep_node(DepKind::HirBody);
}
}
return def_path_hash.to_dep_node(DepKind::Hir);
} }
EntryVariant(p, v) => {
id = p;
if last_expr.is_some() {
if v.node.disr_expr.map(|e| e.node_id) == last_expr {
// The enum parent holds both Hir and HirBody nodes.
let def_index = self.definitions.opt_def_index(id).unwrap();
let def_path_hash = self.definitions.def_path_hash(def_index);
return def_path_hash.to_dep_node(DepKind::HirBody);
}
}
}
EntryForeignItem(p, _) |
EntryField(p, _) |
EntryStmt(p, _) |
EntryTy(p, _) |
EntryTraitRef(p, _) |
EntryBinding(p, _) |
EntryPat(p, _) |
EntryBlock(p, _) |
EntryStructCtor(p, _) |
EntryLifetime(p, _) |
EntryTyParam(p, _) |
EntryVisibility(p, _) |
EntryLocal(p, _) =>
id = p,
EntryExpr(p, _) => {
last_expr = Some(id);
id = p;
}
RootCrate => {
let def_path_hash = self.definitions.def_path_hash(CRATE_DEF_INDEX);
return def_path_hash.to_dep_node(DepKind::Hir);
}
NotPresent =>
// Some nodes, notably macro definitions, are not
// present in the map for whatever reason, but
// they *do* have def-ids. So if we encounter an
// empty hole, check for that case.
return self.definitions.opt_def_index(id)
.map(|def_index| {
let def_path_hash = self.definitions.def_path_hash(def_index);
def_path_hash.to_dep_node(DepKind::Hir)
})
.unwrap_or_else(|| {
bug!("Walking parents from `{}` \
led to `NotPresent` at `{}`",
id0, id)
}),
} }
} }
} }
@ -481,7 +416,7 @@ impl<'hir> Map<'hir> {
Some(body_id) Some(body_id)
} else { } else {
// For some expressions, the expression is its own body. // For some expressions, the expression is its own body.
if let EntryExpr(_, expr) = entry { if let EntryExpr(_, _, expr) = entry {
Some(BodyId { node_id: expr.id }) Some(BodyId { node_id: expr.id })
} else { } else {
None None
@ -760,7 +695,7 @@ impl<'hir> Map<'hir> {
pub fn get_foreign_abi(&self, id: NodeId) -> Abi { pub fn get_foreign_abi(&self, id: NodeId) -> Abi {
let parent = self.get_parent(id); let parent = self.get_parent(id);
let abi = match self.find_entry(parent) { let abi = match self.find_entry(parent) {
Some(EntryItem(_, i)) => { Some(EntryItem(_, _, i)) => {
match i.node { match i.node {
ItemForeignMod(ref nm) => Some(nm.abi), ItemForeignMod(ref nm) => Some(nm.abi),
_ => None _ => None
@ -911,27 +846,27 @@ impl<'hir> Map<'hir> {
pub fn span(&self, id: NodeId) -> Span { pub fn span(&self, id: NodeId) -> Span {
self.read(id); // reveals span from node self.read(id); // reveals span from node
match self.find_entry(id) { match self.find_entry(id) {
Some(EntryItem(_, item)) => item.span, Some(EntryItem(_, _, item)) => item.span,
Some(EntryForeignItem(_, foreign_item)) => foreign_item.span, Some(EntryForeignItem(_, _, foreign_item)) => foreign_item.span,
Some(EntryTraitItem(_, trait_method)) => trait_method.span, Some(EntryTraitItem(_, _, trait_method)) => trait_method.span,
Some(EntryImplItem(_, impl_item)) => impl_item.span, Some(EntryImplItem(_, _, impl_item)) => impl_item.span,
Some(EntryVariant(_, variant)) => variant.span, Some(EntryVariant(_, _, variant)) => variant.span,
Some(EntryField(_, field)) => field.span, Some(EntryField(_, _, field)) => field.span,
Some(EntryExpr(_, expr)) => expr.span, Some(EntryExpr(_, _, expr)) => expr.span,
Some(EntryStmt(_, stmt)) => stmt.span, Some(EntryStmt(_, _, stmt)) => stmt.span,
Some(EntryTy(_, ty)) => ty.span, Some(EntryTy(_, _, ty)) => ty.span,
Some(EntryTraitRef(_, tr)) => tr.path.span, Some(EntryTraitRef(_, _, tr)) => tr.path.span,
Some(EntryBinding(_, pat)) => pat.span, Some(EntryBinding(_, _, pat)) => pat.span,
Some(EntryPat(_, pat)) => pat.span, Some(EntryPat(_, _, pat)) => pat.span,
Some(EntryBlock(_, block)) => block.span, Some(EntryBlock(_, _, block)) => block.span,
Some(EntryStructCtor(_, _)) => self.expect_item(self.get_parent(id)).span, Some(EntryStructCtor(_, _, _)) => self.expect_item(self.get_parent(id)).span,
Some(EntryLifetime(_, lifetime)) => lifetime.span, Some(EntryLifetime(_, _, lifetime)) => lifetime.span,
Some(EntryTyParam(_, ty_param)) => ty_param.span, Some(EntryTyParam(_, _, ty_param)) => ty_param.span,
Some(EntryVisibility(_, &Visibility::Restricted { ref path, .. })) => path.span, Some(EntryVisibility(_, _, &Visibility::Restricted { ref path, .. })) => path.span,
Some(EntryVisibility(_, v)) => bug!("unexpected Visibility {:?}", v), Some(EntryVisibility(_, _, v)) => bug!("unexpected Visibility {:?}", v),
Some(EntryLocal(_, local)) => local.span, Some(EntryLocal(_, _, local)) => local.span,
Some(RootCrate) => self.forest.krate.span, Some(RootCrate(_)) => self.forest.krate.span,
Some(NotPresent) | None => { Some(NotPresent) | None => {
bug!("hir::map::Map::span: id not in map: {:?}", id) bug!("hir::map::Map::span: id not in map: {:?}", id)
} }
@ -1029,12 +964,12 @@ impl<'a, 'hir> Iterator for NodesMatchingSuffix<'a, 'hir> {
} }
self.idx = NodeId::from_u32(self.idx.as_u32() + 1); self.idx = NodeId::from_u32(self.idx.as_u32() + 1);
let name = match self.map.find_entry(idx) { let name = match self.map.find_entry(idx) {
Some(EntryItem(_, n)) => n.name(), Some(EntryItem(_, _, n)) => n.name(),
Some(EntryForeignItem(_, n))=> n.name(), Some(EntryForeignItem(_, _, n))=> n.name(),
Some(EntryTraitItem(_, n)) => n.name(), Some(EntryTraitItem(_, _, n)) => n.name(),
Some(EntryImplItem(_, n)) => n.name(), Some(EntryImplItem(_, _, n)) => n.name(),
Some(EntryVariant(_, n)) => n.name(), Some(EntryVariant(_, _, n)) => n.name(),
Some(EntryField(_, n)) => n.name(), Some(EntryField(_, _, n)) => n.name(),
_ => continue, _ => continue,
}; };
if self.matches_names(self.map.get_parent(idx), name) { if self.matches_names(self.map.get_parent(idx), name) {
@ -1060,9 +995,13 @@ impl Named for ImplItem { fn name(&self) -> Name { self.name } }
pub fn map_crate<'hir>(forest: &'hir mut Forest, pub fn map_crate<'hir>(forest: &'hir mut Forest,
definitions: Definitions) definitions: Definitions)
-> Map<'hir> { -> Map<'hir> {
let mut collector = NodeCollector::root(&forest.krate); let map = {
intravisit::walk_crate(&mut collector, &forest.krate); let mut collector = NodeCollector::root(&forest.krate,
let map = collector.map; &forest.dep_graph,
&definitions);
intravisit::walk_crate(&mut collector, &forest.krate);
collector.into_map()
};
if log_enabled!(::log::LogLevel::Debug) { if log_enabled!(::log::LogLevel::Debug) {
// This only makes sense for ordered stores; note the // This only makes sense for ordered stores; note the

View file

@ -1220,6 +1220,7 @@ pub struct TraitItemId {
pub struct TraitItem { pub struct TraitItem {
pub id: NodeId, pub id: NodeId,
pub name: Name, pub name: Name,
pub hir_id: HirId,
pub attrs: HirVec<Attribute>, pub attrs: HirVec<Attribute>,
pub node: TraitItemKind, pub node: TraitItemKind,
pub span: Span, pub span: Span,
@ -1261,6 +1262,7 @@ pub struct ImplItemId {
pub struct ImplItem { pub struct ImplItem {
pub id: NodeId, pub id: NodeId,
pub name: Name, pub name: Name,
pub hir_id: HirId,
pub vis: Visibility, pub vis: Visibility,
pub defaultness: Defaultness, pub defaultness: Defaultness,
pub attrs: HirVec<Attribute>, pub attrs: HirVec<Attribute>,
@ -1667,8 +1669,9 @@ pub struct ItemId {
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct Item { pub struct Item {
pub name: Name, pub name: Name,
pub attrs: HirVec<Attribute>,
pub id: NodeId, pub id: NodeId,
pub hir_id: HirId,
pub attrs: HirVec<Attribute>,
pub node: Item_, pub node: Item_,
pub vis: Visibility, pub vis: Visibility,
pub span: Span, pub span: Span,

View file

@ -731,6 +731,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for hir::T
hasher: &mut StableHasher<W>) { hasher: &mut StableHasher<W>) {
let hir::TraitItem { let hir::TraitItem {
id, id,
hir_id: _,
name, name,
ref attrs, ref attrs,
ref node, ref node,
@ -764,6 +765,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for hir::I
hasher: &mut StableHasher<W>) { hasher: &mut StableHasher<W>) {
let hir::ImplItem { let hir::ImplItem {
id, id,
hir_id: _,
name, name,
ref vis, ref vis,
defaultness, defaultness,
@ -920,6 +922,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for hir::I
name, name,
ref attrs, ref attrs,
id, id,
hir_id: _,
ref node, ref node,
ref vis, ref vis,
span span