diff --git a/src/librustc/hir/map/definitions.rs b/src/librustc/hir/map/definitions.rs index d90a9ef7d80..ef574b61aa7 100644 --- a/src/librustc/hir/map/definitions.rs +++ b/src/librustc/hir/map/definitions.rs @@ -193,7 +193,7 @@ pub enum DefPathData { // they are treated specially by the `def_path` function. /// The crate root (marker) CrateRoot, - + // Catch-all for random DefId things like DUMMY_NODE_ID Misc, @@ -243,6 +243,10 @@ impl Definitions { } } + pub fn def_path_table(&self) -> &DefPathTable { + &self.table + } + /// Get the number of definitions. pub fn len(&self) -> usize { self.def_index_to_node.len() diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 569d697f374..e5c5b430811 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -29,7 +29,7 @@ use hir::*; use hir::print as pprust; use arena::TypedArena; -use std::cell::RefCell; +use std::cell::{RefCell, Ref}; use std::io; use std::mem; @@ -395,6 +395,10 @@ impl<'ast> Map<'ast> { self.definitions.borrow().len() } + pub fn definitions(&self) -> Ref { + self.definitions.borrow() + } + pub fn def_key(&self, def_id: DefId) -> DefKey { assert!(def_id.is_local()); self.definitions.borrow().def_key(def_id.index) diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index d36242537b8..9eed5cb8fe8 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -300,7 +300,7 @@ impl<'a> CrateLoader<'a> { let mut cmeta = cstore::CrateMetadata { name: name, 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(|_| { self.load_derive_macros(&crate_root, dylib.clone().map(|p| p.0), span) }), diff --git a/src/librustc_metadata/cstore.rs b/src/librustc_metadata/cstore.rs index 7700ebde181..7ec847d24cf 100644 --- a/src/librustc_metadata/cstore.rs +++ b/src/librustc_metadata/cstore.rs @@ -16,7 +16,7 @@ use schema; use rustc::dep_graph::DepGraph; 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::middle::cstore::{DepKind, ExternCrate}; use rustc_back::PanicStrategy; @@ -78,7 +78,7 @@ pub struct CrateMetadata { /// hashmap, which gives the reverse mapping. This allows us to /// quickly retrace a `DefPath`, which is needed for incremental /// compilation support. - pub key_map: FxHashMap, + pub def_path_table: DefPathTable, pub dep_kind: Cell, pub source: CrateSource, diff --git a/src/librustc_metadata/cstore_impl.rs b/src/librustc_metadata/cstore_impl.rs index 1a1bb1432ee..7305b239515 100644 --- a/src/librustc_metadata/cstore_impl.rs +++ b/src/librustc_metadata/cstore_impl.rs @@ -341,7 +341,7 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore { def: DefKey) -> Option { 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 diff --git a/src/librustc_metadata/decoder.rs b/src/librustc_metadata/decoder.rs index d487d2e6da6..cf550f03d8e 100644 --- a/src/librustc_metadata/decoder.rs +++ b/src/librustc_metadata/decoder.rs @@ -12,12 +12,10 @@ use astencode::decode_inlined_item; use cstore::{self, CrateMetadata, MetadataBlob, NativeLibrary}; -use index::Index; use schema::*; use rustc::hir::map as hir_map; use rustc::hir::map::{DefKey, DefPathData}; -use rustc::util::nodemap::FxHashMap; use rustc::hir; use rustc::hir::intravisit::IdRange; @@ -456,14 +454,6 @@ impl<'a, 'tcx> MetadataBlob { 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) -> FxHashMap { - 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<()> { write!(out, "=External Dependencies=\n")?; let root = self.get_root(); @@ -543,9 +533,8 @@ impl<'a, 'tcx> CrateMetadata { } } - fn item_name(&self, item: &Entry<'tcx>) -> ast::Name { - item.def_key - .decode(self) + fn item_name(&self, item_index: DefIndex) -> ast::Name { + self.def_key(item_index) .disambiguated_data .data .get_opt_name() @@ -594,12 +583,12 @@ impl<'a, 'tcx> CrateMetadata { (ty::VariantDef { 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| { let f = self.entry(index); ty::FieldDef { did: self.local_def_id(index), - name: self.item_name(&f), + name: self.item_name(index), vis: f.visibility } }).collect(), @@ -771,7 +760,7 @@ impl<'a, 'tcx> CrateMetadata { if let Some(def) = self.get_def(child_index) { callback(def::Export { 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)) = (self.get_def(child_index), def_key.disambiguated_data.data.get_opt_name()) { callback(def::Export { @@ -886,7 +875,7 @@ impl<'a, 'tcx> CrateMetadata { pub fn get_associated_item(&self, id: DefIndex) -> Option { let item = self.entry(id); 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()), 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 // look at the definition 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 { item = self.entry(def_key.parent.unwrap()); } @@ -974,7 +963,7 @@ impl<'a, 'tcx> CrateMetadata { self.entry(id) .children .decode(self) - .map(|index| self.item_name(&self.entry(index))) + .map(|index| self.item_name(index)) .collect() } @@ -1036,7 +1025,7 @@ impl<'a, 'tcx> CrateMetadata { } pub fn get_trait_of_item(&self, id: DefIndex) -> Option { - 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 { EntryKind::Trait(_) => Some(self.local_def_id(parent_index)), _ => None, @@ -1082,7 +1071,7 @@ impl<'a, 'tcx> CrateMetadata { pub fn get_macro(&self, id: DefIndex) -> (ast::Name, MacroDef) { let entry = self.entry(id); 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!(), } } @@ -1135,20 +1124,8 @@ impl<'a, 'tcx> CrateMetadata { } } - pub fn def_key(&self, id: DefIndex) -> hir_map::DefKey { - debug!("def_key: id={:?}", id); - 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) - } + pub fn def_key(&self, index: DefIndex) -> DefKey { + self.def_path_table.def_key(index) } // Returns the path leading to the thing with this `id`. Note that diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index 443f3fbaa6e..cf032013ac9 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -16,6 +16,7 @@ use rustc::middle::cstore::{InlinedItemRef, LinkMeta}; use rustc::middle::cstore::{LinkagePreference, NativeLibrary}; use rustc::hir::def; 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::lang_items; use rustc::mir; @@ -233,13 +234,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { 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 { - let tcx = self.tcx; - self.lazy(&tcx.map.def_key(def_id)) - } - fn encode_item_variances(&mut self, def_id: DefId) -> LazySeq { let tcx = self.tcx; 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)), visibility: enum_vis.simplify(), 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)), children: self.lazy_seq(variant.fields.iter().map(|f| { assert!(f.did.is_local()); @@ -315,7 +308,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { kind: EntryKind::Mod(self.lazy(&data)), visibility: vis.simplify(), span: self.lazy(&md.inner), - def_key: self.encode_def_key(def_id), attributes: self.encode_attributes(attrs), children: self.lazy_seq(md.item_ids.iter().map(|item_id| { tcx.map.local_def_id(item_id.id).index @@ -396,7 +388,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { kind: EntryKind::Field, visibility: field.vis.simplify(), 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), children: LazySeq::empty(), stability: self.encode_stability(def_id), @@ -430,7 +421,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { kind: EntryKind::Struct(self.lazy(&data)), visibility: struct_vis.simplify(), span: self.lazy(&tcx.def_span(def_id)), - def_key: self.encode_def_key(def_id), attributes: LazySeq::empty(), children: LazySeq::empty(), stability: self.encode_stability(def_id), @@ -497,7 +487,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { kind: kind, visibility: trait_item.vis.simplify(), span: self.lazy(&ast_item.span), - def_key: self.encode_def_key(def_id), attributes: self.encode_attributes(&ast_item.attrs), children: LazySeq::empty(), stability: self.encode_stability(def_id), @@ -587,7 +576,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { kind: kind, visibility: impl_item.vis.simplify(), span: self.lazy(&ast_item.span), - def_key: self.encode_def_key(def_id), attributes: self.encode_attributes(&ast_item.attrs), children: LazySeq::empty(), stability: self.encode_stability(def_id), @@ -750,7 +738,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { kind: kind, visibility: item.vis.simplify(), span: self.lazy(&item.span), - def_key: self.encode_def_key(def_id), attributes: self.encode_attributes(&item.attrs), children: match item.node { hir::ItemForeignMod(ref fm) => { @@ -858,14 +845,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { /// Serialize the text of exported macros 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 { kind: EntryKind::MacroDef(self.lazy(&MacroDef { body: ::syntax::print::pprust::tts_to_string(¯o_def.body) })), visibility: ty::Visibility::Public, span: self.lazy(¯o_def.span), - def_key: self.encode_def_key(def_id), attributes: self.encode_attributes(¯o_def.attrs), children: LazySeq::empty(), @@ -967,7 +952,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { kind: kind, visibility: nitem.vis.simplify(), span: self.lazy(&nitem.span), - def_key: self.encode_def_key(def_id), attributes: self.encode_attributes(&nitem.attrs), children: LazySeq::empty(), stability: self.encode_stability(def_id), @@ -1050,7 +1034,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { kind: EntryKind::Type, visibility: ty::Visibility::Public, span: self.lazy(&tcx.def_span(def_id)), - def_key: self.encode_def_key(def_id), attributes: LazySeq::empty(), children: LazySeq::empty(), stability: None, @@ -1079,7 +1062,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { kind: EntryKind::Closure(self.lazy(&data)), visibility: ty::Visibility::Public, 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)), children: LazySeq::empty(), stability: None, @@ -1179,6 +1161,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { }) .map(|filemap| &**filemap)) } + + fn encode_def_path_table(&mut self) -> Lazy { + let definitions = self.tcx.map.definitions(); + self.lazy(definitions.def_path_table()) + } } struct ImplVisitor<'a, 'tcx: 'a> { @@ -1276,6 +1263,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let codemap = self.encode_codemap(); 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. i = self.position(); let impls = self.encode_impls(); @@ -1321,6 +1313,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { lang_items_missing: lang_items_missing, native_libraries: native_libraries, codemap: codemap, + def_path_table: def_path_table, impls: impls, exported_symbols: exported_symbols, index: index, @@ -1343,6 +1336,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { println!(" codemap bytes: {}", codemap_bytes); println!(" impl bytes: {}", impl_bytes); println!(" exp. symbols bytes: {}", exported_symbols_bytes); + println!(" def-path table bytes: {}", def_path_table_bytes); println!(" item bytes: {}", item_bytes); println!(" index bytes: {}", index_bytes); println!(" zero bytes: {}", zero_bytes); diff --git a/src/librustc_metadata/schema.rs b/src/librustc_metadata/schema.rs index f92051cbf19..0b6606a00d3 100644 --- a/src/librustc_metadata/schema.rs +++ b/src/librustc_metadata/schema.rs @@ -179,6 +179,7 @@ pub struct CrateRoot { pub lang_items_missing: LazySeq, pub native_libraries: LazySeq, pub codemap: LazySeq, + pub def_path_table: Lazy, pub impls: LazySeq, pub exported_symbols: LazySeq, pub index: LazySeq, @@ -202,7 +203,6 @@ pub struct Entry<'tcx> { pub kind: EntryKind<'tcx>, pub visibility: ty::Visibility, pub span: Lazy, - pub def_key: Lazy, pub attributes: LazySeq, pub children: LazySeq, pub stability: Option>,