1
Fork 0

definitions: Store DefPath data in separate table in metadata

This commit is contained in:
Michael Woerister 2016-12-16 12:51:36 -05:00
parent aed0cdbfd2
commit ed5a88e3d0
8 changed files with 41 additions and 62 deletions

View file

@ -193,7 +193,7 @@ pub enum DefPathData {
// they are treated specially by the `def_path` function. // they are treated specially by the `def_path` function.
/// The crate root (marker) /// The crate root (marker)
CrateRoot, CrateRoot,
// Catch-all for random DefId things like DUMMY_NODE_ID // Catch-all for random DefId things like DUMMY_NODE_ID
Misc, Misc,
@ -243,6 +243,10 @@ impl Definitions {
} }
} }
pub fn def_path_table(&self) -> &DefPathTable {
&self.table
}
/// Get the number of definitions. /// Get the number of definitions.
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.def_index_to_node.len() self.def_index_to_node.len()

View file

@ -29,7 +29,7 @@ use hir::*;
use hir::print as pprust; use hir::print as pprust;
use arena::TypedArena; use arena::TypedArena;
use std::cell::RefCell; use std::cell::{RefCell, Ref};
use std::io; use std::io;
use std::mem; use std::mem;
@ -395,6 +395,10 @@ impl<'ast> Map<'ast> {
self.definitions.borrow().len() self.definitions.borrow().len()
} }
pub fn definitions(&self) -> Ref<Definitions> {
self.definitions.borrow()
}
pub fn def_key(&self, def_id: DefId) -> DefKey { pub fn def_key(&self, def_id: DefId) -> DefKey {
assert!(def_id.is_local()); assert!(def_id.is_local());
self.definitions.borrow().def_key(def_id.index) self.definitions.borrow().def_key(def_id.index)

View file

@ -300,7 +300,7 @@ impl<'a> CrateLoader<'a> {
let mut cmeta = cstore::CrateMetadata { let mut cmeta = cstore::CrateMetadata {
name: name, name: name,
extern_crate: Cell::new(None), extern_crate: Cell::new(None),
key_map: metadata.load_key_map(crate_root.index), def_path_table: crate_root.def_path_table.decode(&metadata),
proc_macros: crate_root.macro_derive_registrar.map(|_| { proc_macros: crate_root.macro_derive_registrar.map(|_| {
self.load_derive_macros(&crate_root, dylib.clone().map(|p| p.0), span) self.load_derive_macros(&crate_root, dylib.clone().map(|p| p.0), span)
}), }),

View file

@ -16,7 +16,7 @@ use schema;
use rustc::dep_graph::DepGraph; use rustc::dep_graph::DepGraph;
use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, CrateNum, DefIndex, DefId}; use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, CrateNum, DefIndex, DefId};
use rustc::hir::map::DefKey; use rustc::hir::map::definitions::DefPathTable;
use rustc::hir::svh::Svh; use rustc::hir::svh::Svh;
use rustc::middle::cstore::{DepKind, ExternCrate}; use rustc::middle::cstore::{DepKind, ExternCrate};
use rustc_back::PanicStrategy; use rustc_back::PanicStrategy;
@ -78,7 +78,7 @@ pub struct CrateMetadata {
/// hashmap, which gives the reverse mapping. This allows us to /// hashmap, which gives the reverse mapping. This allows us to
/// quickly retrace a `DefPath`, which is needed for incremental /// quickly retrace a `DefPath`, which is needed for incremental
/// compilation support. /// compilation support.
pub key_map: FxHashMap<DefKey, DefIndex>, pub def_path_table: DefPathTable,
pub dep_kind: Cell<DepKind>, pub dep_kind: Cell<DepKind>,
pub source: CrateSource, pub source: CrateSource,

View file

@ -341,7 +341,7 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
def: DefKey) def: DefKey)
-> Option<DefIndex> { -> Option<DefIndex> {
let cdata = self.get_crate_data(cnum); let cdata = self.get_crate_data(cnum);
cdata.key_map.get(&def).cloned() cdata.def_path_table.def_index_for_def_key(&def)
} }
/// Returns the `DefKey` for a given `DefId`. This indicates the /// Returns the `DefKey` for a given `DefId`. This indicates the

View file

@ -12,12 +12,10 @@
use astencode::decode_inlined_item; use astencode::decode_inlined_item;
use cstore::{self, CrateMetadata, MetadataBlob, NativeLibrary}; use cstore::{self, CrateMetadata, MetadataBlob, NativeLibrary};
use index::Index;
use schema::*; use schema::*;
use rustc::hir::map as hir_map; use rustc::hir::map as hir_map;
use rustc::hir::map::{DefKey, DefPathData}; use rustc::hir::map::{DefKey, DefPathData};
use rustc::util::nodemap::FxHashMap;
use rustc::hir; use rustc::hir;
use rustc::hir::intravisit::IdRange; use rustc::hir::intravisit::IdRange;
@ -456,14 +454,6 @@ impl<'a, 'tcx> MetadataBlob {
Lazy::with_position(pos).decode(self) Lazy::with_position(pos).decode(self)
} }
/// Go through each item in the metadata and create a map from that
/// item's def-key to the item's DefIndex.
pub fn load_key_map(&self, index: LazySeq<Index>) -> FxHashMap<DefKey, DefIndex> {
index.iter_enumerated(self.raw_bytes())
.map(|(index, item)| (item.decode(self).def_key.decode(self), index))
.collect()
}
pub fn list_crate_metadata(&self, out: &mut io::Write) -> io::Result<()> { pub fn list_crate_metadata(&self, out: &mut io::Write) -> io::Result<()> {
write!(out, "=External Dependencies=\n")?; write!(out, "=External Dependencies=\n")?;
let root = self.get_root(); let root = self.get_root();
@ -543,9 +533,8 @@ impl<'a, 'tcx> CrateMetadata {
} }
} }
fn item_name(&self, item: &Entry<'tcx>) -> ast::Name { fn item_name(&self, item_index: DefIndex) -> ast::Name {
item.def_key self.def_key(item_index)
.decode(self)
.disambiguated_data .disambiguated_data
.data .data
.get_opt_name() .get_opt_name()
@ -594,12 +583,12 @@ impl<'a, 'tcx> CrateMetadata {
(ty::VariantDef { (ty::VariantDef {
did: self.local_def_id(data.struct_ctor.unwrap_or(index)), did: self.local_def_id(data.struct_ctor.unwrap_or(index)),
name: self.item_name(item), name: self.item_name(index),
fields: item.children.decode(self).map(|index| { fields: item.children.decode(self).map(|index| {
let f = self.entry(index); let f = self.entry(index);
ty::FieldDef { ty::FieldDef {
did: self.local_def_id(index), did: self.local_def_id(index),
name: self.item_name(&f), name: self.item_name(index),
vis: f.visibility vis: f.visibility
} }
}).collect(), }).collect(),
@ -771,7 +760,7 @@ impl<'a, 'tcx> CrateMetadata {
if let Some(def) = self.get_def(child_index) { if let Some(def) = self.get_def(child_index) {
callback(def::Export { callback(def::Export {
def: def, def: def,
name: self.item_name(&self.entry(child_index)), name: self.item_name(child_index),
}); });
} }
} }
@ -783,7 +772,7 @@ impl<'a, 'tcx> CrateMetadata {
_ => {} _ => {}
} }
let def_key = child.def_key.decode(self); let def_key = self.def_key(child_index);
if let (Some(def), Some(name)) = if let (Some(def), Some(name)) =
(self.get_def(child_index), def_key.disambiguated_data.data.get_opt_name()) { (self.get_def(child_index), def_key.disambiguated_data.data.get_opt_name()) {
callback(def::Export { callback(def::Export {
@ -886,7 +875,7 @@ impl<'a, 'tcx> CrateMetadata {
pub fn get_associated_item(&self, id: DefIndex) -> Option<ty::AssociatedItem> { pub fn get_associated_item(&self, id: DefIndex) -> Option<ty::AssociatedItem> {
let item = self.entry(id); let item = self.entry(id);
let parent_and_name = || { let parent_and_name = || {
let def_key = item.def_key.decode(self); let def_key = self.def_key(id);
(self.local_def_id(def_key.parent.unwrap()), (self.local_def_id(def_key.parent.unwrap()),
def_key.disambiguated_data.data.get_opt_name().unwrap()) def_key.disambiguated_data.data.get_opt_name().unwrap())
}; };
@ -963,7 +952,7 @@ impl<'a, 'tcx> CrateMetadata {
// we assume that someone passing in a tuple struct ctor is actually wanting to // we assume that someone passing in a tuple struct ctor is actually wanting to
// look at the definition // look at the definition
let mut item = self.entry(node_id); let mut item = self.entry(node_id);
let def_key = item.def_key.decode(self); let def_key = self.def_key(node_id);
if def_key.disambiguated_data.data == DefPathData::StructCtor { if def_key.disambiguated_data.data == DefPathData::StructCtor {
item = self.entry(def_key.parent.unwrap()); item = self.entry(def_key.parent.unwrap());
} }
@ -974,7 +963,7 @@ impl<'a, 'tcx> CrateMetadata {
self.entry(id) self.entry(id)
.children .children
.decode(self) .decode(self)
.map(|index| self.item_name(&self.entry(index))) .map(|index| self.item_name(index))
.collect() .collect()
} }
@ -1036,7 +1025,7 @@ impl<'a, 'tcx> CrateMetadata {
} }
pub fn get_trait_of_item(&self, id: DefIndex) -> Option<DefId> { pub fn get_trait_of_item(&self, id: DefIndex) -> Option<DefId> {
self.entry(id).def_key.decode(self).parent.and_then(|parent_index| { self.def_key(id).parent.and_then(|parent_index| {
match self.entry(parent_index).kind { match self.entry(parent_index).kind {
EntryKind::Trait(_) => Some(self.local_def_id(parent_index)), EntryKind::Trait(_) => Some(self.local_def_id(parent_index)),
_ => None, _ => None,
@ -1082,7 +1071,7 @@ impl<'a, 'tcx> CrateMetadata {
pub fn get_macro(&self, id: DefIndex) -> (ast::Name, MacroDef) { pub fn get_macro(&self, id: DefIndex) -> (ast::Name, MacroDef) {
let entry = self.entry(id); let entry = self.entry(id);
match entry.kind { match entry.kind {
EntryKind::MacroDef(macro_def) => (self.item_name(&entry), macro_def.decode(self)), EntryKind::MacroDef(macro_def) => (self.item_name(id), macro_def.decode(self)),
_ => bug!(), _ => bug!(),
} }
} }
@ -1135,20 +1124,8 @@ impl<'a, 'tcx> CrateMetadata {
} }
} }
pub fn def_key(&self, id: DefIndex) -> hir_map::DefKey { pub fn def_key(&self, index: DefIndex) -> DefKey {
debug!("def_key: id={:?}", id); self.def_path_table.def_key(index)
if self.is_proc_macro(id) {
let name = self.proc_macros.as_ref().unwrap()[id.as_usize() - 1].0;
hir_map::DefKey {
parent: Some(CRATE_DEF_INDEX),
disambiguated_data: hir_map::DisambiguatedDefPathData {
data: hir_map::DefPathData::MacroDef(name.as_str()),
disambiguator: 0,
},
}
} else {
self.entry(id).def_key.decode(self)
}
} }
// Returns the path leading to the thing with this `id`. Note that // Returns the path leading to the thing with this `id`. Note that

View file

@ -16,6 +16,7 @@ use rustc::middle::cstore::{InlinedItemRef, LinkMeta};
use rustc::middle::cstore::{LinkagePreference, NativeLibrary}; use rustc::middle::cstore::{LinkagePreference, NativeLibrary};
use rustc::hir::def; use rustc::hir::def;
use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefIndex, DefId}; use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefIndex, DefId};
use rustc::hir::map::definitions::DefPathTable;
use rustc::middle::dependency_format::Linkage; use rustc::middle::dependency_format::Linkage;
use rustc::middle::lang_items; use rustc::middle::lang_items;
use rustc::mir; use rustc::mir;
@ -233,13 +234,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
Ok(()) Ok(())
} }
/// For every DefId that we create a metadata item for, we include a
/// serialized copy of its DefKey, which allows us to recreate a path.
fn encode_def_key(&mut self, def_id: DefId) -> Lazy<hir::map::DefKey> {
let tcx = self.tcx;
self.lazy(&tcx.map.def_key(def_id))
}
fn encode_item_variances(&mut self, def_id: DefId) -> LazySeq<ty::Variance> { fn encode_item_variances(&mut self, def_id: DefId) -> LazySeq<ty::Variance> {
let tcx = self.tcx; let tcx = self.tcx;
self.lazy_seq(tcx.item_variances(def_id).iter().cloned()) self.lazy_seq(tcx.item_variances(def_id).iter().cloned())
@ -276,7 +270,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
kind: EntryKind::Variant(self.lazy(&data)), kind: EntryKind::Variant(self.lazy(&data)),
visibility: enum_vis.simplify(), visibility: enum_vis.simplify(),
span: self.lazy(&tcx.def_span(def_id)), span: self.lazy(&tcx.def_span(def_id)),
def_key: self.encode_def_key(def_id),
attributes: self.encode_attributes(&tcx.get_attrs(def_id)), attributes: self.encode_attributes(&tcx.get_attrs(def_id)),
children: self.lazy_seq(variant.fields.iter().map(|f| { children: self.lazy_seq(variant.fields.iter().map(|f| {
assert!(f.did.is_local()); assert!(f.did.is_local());
@ -315,7 +308,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
kind: EntryKind::Mod(self.lazy(&data)), kind: EntryKind::Mod(self.lazy(&data)),
visibility: vis.simplify(), visibility: vis.simplify(),
span: self.lazy(&md.inner), span: self.lazy(&md.inner),
def_key: self.encode_def_key(def_id),
attributes: self.encode_attributes(attrs), attributes: self.encode_attributes(attrs),
children: self.lazy_seq(md.item_ids.iter().map(|item_id| { children: self.lazy_seq(md.item_ids.iter().map(|item_id| {
tcx.map.local_def_id(item_id.id).index tcx.map.local_def_id(item_id.id).index
@ -396,7 +388,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
kind: EntryKind::Field, kind: EntryKind::Field,
visibility: field.vis.simplify(), visibility: field.vis.simplify(),
span: self.lazy(&tcx.def_span(def_id)), span: self.lazy(&tcx.def_span(def_id)),
def_key: self.encode_def_key(def_id),
attributes: self.encode_attributes(&variant_data.fields()[field_index].attrs), attributes: self.encode_attributes(&variant_data.fields()[field_index].attrs),
children: LazySeq::empty(), children: LazySeq::empty(),
stability: self.encode_stability(def_id), stability: self.encode_stability(def_id),
@ -430,7 +421,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
kind: EntryKind::Struct(self.lazy(&data)), kind: EntryKind::Struct(self.lazy(&data)),
visibility: struct_vis.simplify(), visibility: struct_vis.simplify(),
span: self.lazy(&tcx.def_span(def_id)), span: self.lazy(&tcx.def_span(def_id)),
def_key: self.encode_def_key(def_id),
attributes: LazySeq::empty(), attributes: LazySeq::empty(),
children: LazySeq::empty(), children: LazySeq::empty(),
stability: self.encode_stability(def_id), stability: self.encode_stability(def_id),
@ -497,7 +487,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
kind: kind, kind: kind,
visibility: trait_item.vis.simplify(), visibility: trait_item.vis.simplify(),
span: self.lazy(&ast_item.span), span: self.lazy(&ast_item.span),
def_key: self.encode_def_key(def_id),
attributes: self.encode_attributes(&ast_item.attrs), attributes: self.encode_attributes(&ast_item.attrs),
children: LazySeq::empty(), children: LazySeq::empty(),
stability: self.encode_stability(def_id), stability: self.encode_stability(def_id),
@ -587,7 +576,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
kind: kind, kind: kind,
visibility: impl_item.vis.simplify(), visibility: impl_item.vis.simplify(),
span: self.lazy(&ast_item.span), span: self.lazy(&ast_item.span),
def_key: self.encode_def_key(def_id),
attributes: self.encode_attributes(&ast_item.attrs), attributes: self.encode_attributes(&ast_item.attrs),
children: LazySeq::empty(), children: LazySeq::empty(),
stability: self.encode_stability(def_id), stability: self.encode_stability(def_id),
@ -750,7 +738,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
kind: kind, kind: kind,
visibility: item.vis.simplify(), visibility: item.vis.simplify(),
span: self.lazy(&item.span), span: self.lazy(&item.span),
def_key: self.encode_def_key(def_id),
attributes: self.encode_attributes(&item.attrs), attributes: self.encode_attributes(&item.attrs),
children: match item.node { children: match item.node {
hir::ItemForeignMod(ref fm) => { hir::ItemForeignMod(ref fm) => {
@ -858,14 +845,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
/// Serialize the text of exported macros /// Serialize the text of exported macros
fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef) -> Entry<'tcx> { fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef) -> Entry<'tcx> {
let def_id = self.tcx.map.local_def_id(macro_def.id);
Entry { Entry {
kind: EntryKind::MacroDef(self.lazy(&MacroDef { kind: EntryKind::MacroDef(self.lazy(&MacroDef {
body: ::syntax::print::pprust::tts_to_string(&macro_def.body) body: ::syntax::print::pprust::tts_to_string(&macro_def.body)
})), })),
visibility: ty::Visibility::Public, visibility: ty::Visibility::Public,
span: self.lazy(&macro_def.span), span: self.lazy(&macro_def.span),
def_key: self.encode_def_key(def_id),
attributes: self.encode_attributes(&macro_def.attrs), attributes: self.encode_attributes(&macro_def.attrs),
children: LazySeq::empty(), children: LazySeq::empty(),
@ -967,7 +952,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
kind: kind, kind: kind,
visibility: nitem.vis.simplify(), visibility: nitem.vis.simplify(),
span: self.lazy(&nitem.span), span: self.lazy(&nitem.span),
def_key: self.encode_def_key(def_id),
attributes: self.encode_attributes(&nitem.attrs), attributes: self.encode_attributes(&nitem.attrs),
children: LazySeq::empty(), children: LazySeq::empty(),
stability: self.encode_stability(def_id), stability: self.encode_stability(def_id),
@ -1050,7 +1034,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
kind: EntryKind::Type, kind: EntryKind::Type,
visibility: ty::Visibility::Public, visibility: ty::Visibility::Public,
span: self.lazy(&tcx.def_span(def_id)), span: self.lazy(&tcx.def_span(def_id)),
def_key: self.encode_def_key(def_id),
attributes: LazySeq::empty(), attributes: LazySeq::empty(),
children: LazySeq::empty(), children: LazySeq::empty(),
stability: None, stability: None,
@ -1079,7 +1062,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
kind: EntryKind::Closure(self.lazy(&data)), kind: EntryKind::Closure(self.lazy(&data)),
visibility: ty::Visibility::Public, visibility: ty::Visibility::Public,
span: self.lazy(&tcx.def_span(def_id)), span: self.lazy(&tcx.def_span(def_id)),
def_key: self.encode_def_key(def_id),
attributes: self.encode_attributes(&tcx.get_attrs(def_id)), attributes: self.encode_attributes(&tcx.get_attrs(def_id)),
children: LazySeq::empty(), children: LazySeq::empty(),
stability: None, stability: None,
@ -1179,6 +1161,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}) })
.map(|filemap| &**filemap)) .map(|filemap| &**filemap))
} }
fn encode_def_path_table(&mut self) -> Lazy<DefPathTable> {
let definitions = self.tcx.map.definitions();
self.lazy(definitions.def_path_table())
}
} }
struct ImplVisitor<'a, 'tcx: 'a> { struct ImplVisitor<'a, 'tcx: 'a> {
@ -1276,6 +1263,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let codemap = self.encode_codemap(); let codemap = self.encode_codemap();
let codemap_bytes = self.position() - i; let codemap_bytes = self.position() - i;
// Encode DefPathTable
i = self.position();
let def_path_table = self.encode_def_path_table();
let def_path_table_bytes = self.position() - i;
// Encode the def IDs of impls, for coherence checking. // Encode the def IDs of impls, for coherence checking.
i = self.position(); i = self.position();
let impls = self.encode_impls(); let impls = self.encode_impls();
@ -1321,6 +1313,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
lang_items_missing: lang_items_missing, lang_items_missing: lang_items_missing,
native_libraries: native_libraries, native_libraries: native_libraries,
codemap: codemap, codemap: codemap,
def_path_table: def_path_table,
impls: impls, impls: impls,
exported_symbols: exported_symbols, exported_symbols: exported_symbols,
index: index, index: index,
@ -1343,6 +1336,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
println!(" codemap bytes: {}", codemap_bytes); println!(" codemap bytes: {}", codemap_bytes);
println!(" impl bytes: {}", impl_bytes); println!(" impl bytes: {}", impl_bytes);
println!(" exp. symbols bytes: {}", exported_symbols_bytes); println!(" exp. symbols bytes: {}", exported_symbols_bytes);
println!(" def-path table bytes: {}", def_path_table_bytes);
println!(" item bytes: {}", item_bytes); println!(" item bytes: {}", item_bytes);
println!(" index bytes: {}", index_bytes); println!(" index bytes: {}", index_bytes);
println!(" zero bytes: {}", zero_bytes); println!(" zero bytes: {}", zero_bytes);

View file

@ -179,6 +179,7 @@ pub struct CrateRoot {
pub lang_items_missing: LazySeq<lang_items::LangItem>, pub lang_items_missing: LazySeq<lang_items::LangItem>,
pub native_libraries: LazySeq<NativeLibrary>, pub native_libraries: LazySeq<NativeLibrary>,
pub codemap: LazySeq<syntax_pos::FileMap>, pub codemap: LazySeq<syntax_pos::FileMap>,
pub def_path_table: Lazy<hir::map::definitions::DefPathTable>,
pub impls: LazySeq<TraitImpls>, pub impls: LazySeq<TraitImpls>,
pub exported_symbols: LazySeq<DefIndex>, pub exported_symbols: LazySeq<DefIndex>,
pub index: LazySeq<index::Index>, pub index: LazySeq<index::Index>,
@ -202,7 +203,6 @@ pub struct Entry<'tcx> {
pub kind: EntryKind<'tcx>, pub kind: EntryKind<'tcx>,
pub visibility: ty::Visibility, pub visibility: ty::Visibility,
pub span: Lazy<Span>, pub span: Lazy<Span>,
pub def_key: Lazy<hir::map::DefKey>,
pub attributes: LazySeq<ast::Attribute>, pub attributes: LazySeq<ast::Attribute>,
pub children: LazySeq<DefIndex>, pub children: LazySeq<DefIndex>,
pub stability: Option<Lazy<attr::Stability>>, pub stability: Option<Lazy<attr::Stability>>,