]> git.lizzy.rs Git - rust.git/commitdiff
definitions: Store DefPath data in separate table in metadata
authorMichael Woerister <michaelwoerister@posteo.net>
Fri, 16 Dec 2016 17:51:36 +0000 (12:51 -0500)
committerMichael Woerister <michaelwoerister@posteo.net>
Sat, 17 Dec 2016 00:14:16 +0000 (19:14 -0500)
src/librustc/hir/map/definitions.rs
src/librustc/hir/map/mod.rs
src/librustc_metadata/creader.rs
src/librustc_metadata/cstore.rs
src/librustc_metadata/cstore_impl.rs
src/librustc_metadata/decoder.rs
src/librustc_metadata/encoder.rs
src/librustc_metadata/schema.rs

index d90a9ef7d80d3237739ffb964a5d96538b0a867e..ef574b61aa7e9cedd9b98931e247a2beaa46282d 100644 (file)
@@ -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 @@ pub fn new() -> 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()
index 569d697f374a197e1cde48d97813369840bbaf1e..e5c5b430811672f939575df19e893444512f9e0a 100644 (file)
@@ -29,7 +29,7 @@
 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 @@ pub fn num_local_def_ids(&self) -> usize {
         self.definitions.borrow().len()
     }
 
+    pub fn definitions(&self) -> Ref<Definitions> {
+        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)
index d36242537b8e54a255f9920796d4102f7bb97c54..9eed5cb8fe8c4eb6a1c5a963d5fe65e09361bd4a 100644 (file)
@@ -300,7 +300,7 @@ fn register_crate(&mut self,
         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)
             }),
index 7700ebde181333772a7fbda715e721ed85831e18..7ec847d24cfa3fc73823ce1a427550454ef172ba 100644 (file)
@@ -16,7 +16,7 @@
 
 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<DefKey, DefIndex>,
+    pub def_path_table: DefPathTable,
 
     pub dep_kind: Cell<DepKind>,
     pub source: CrateSource,
index 1a1bb1432eec1fc2b625178df786104cdda97ee0..7305b23951590795928d319096ccf8cab3829022 100644 (file)
@@ -341,7 +341,7 @@ fn def_index_for_def_key(&self,
                              def: DefKey)
                              -> Option<DefIndex> {
         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
index d487d2e6da679547f4738d9482a88bd04fbee0e9..cf550f03d8eb839f1a2ac729743d9f0e6ea8430a 100644 (file)
 
 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 @@ pub fn get_root(&self) -> CrateRoot {
         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<()> {
         write!(out, "=External Dependencies=\n")?;
         let root = self.get_root();
@@ -543,9 +533,8 @@ fn local_def_id(&self, index: DefIndex) -> DefId {
         }
     }
 
-    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 @@ fn get_variant(&self,
 
         (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 @@ pub fn each_child_of_item<F>(&self, id: DefIndex, mut callback: F)
                             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 @@ pub fn each_child_of_item<F>(&self, id: DefIndex, mut callback: F)
                     _ => {}
                 }
 
-                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 @@ pub fn maybe_get_item_mir(&self,
     pub fn get_associated_item(&self, id: DefIndex) -> Option<ty::AssociatedItem> {
         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 @@ pub fn get_item_attrs(&self, node_id: DefIndex) -> Vec<ast::Attribute> {
         // 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 @@ pub fn get_struct_field_names(&self, id: DefIndex) -> Vec<ast::Name> {
         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 @@ pub fn get_implementations_for_trait(&self, filter: Option<DefId>, result: &mut
     }
 
     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 {
                 EntryKind::Trait(_) => Some(self.local_def_id(parent_index)),
                 _ => None,
@@ -1082,7 +1071,7 @@ pub fn get_exported_symbols(&self) -> Vec<DefId> {
     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 @@ pub fn closure_ty(&self,
         }
     }
 
-    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
index 443f3fbaa6e41df93d4ef12f7e03da34fb208621..cf032013ac962976117a497cd3f9f0c41cd391ab 100644 (file)
@@ -16,6 +16,7 @@
 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 @@ fn encode_with_shorthand<T, U, M>(&mut self,
         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> {
         let tcx = self.tcx;
         self.lazy_seq(tcx.item_variances(def_id).iter().cloned())
@@ -276,7 +270,6 @@ fn encode_enum_variant_info(&mut self,
             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 @@ fn encode_info_for_mod(&mut self,
             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 @@ fn encode_field(&mut self,
             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 @@ fn encode_struct_ctor(&mut self, (adt_def_id, def_id): (DefId, DefId)) -> Entry<
             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 @@ fn encode_info_for_trait_item(&mut self, def_id: DefId) -> Entry<'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 @@ fn encode_info_for_impl_item(&mut self, def_id: DefId) -> Entry<'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 @@ fn encode_info_for_item(&mut self, (def_id, item): (DefId, &'tcx hir::Item)) ->
             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 @@ fn encode_info_for_item(&mut self, (def_id, item): (DefId, &'tcx hir::Item)) ->
 
     /// 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(&macro_def.body)
             })),
             visibility: ty::Visibility::Public,
             span: self.lazy(&macro_def.span),
-            def_key: self.encode_def_key(def_id),
 
             attributes: self.encode_attributes(&macro_def.attrs),
             children: LazySeq::empty(),
@@ -967,7 +952,6 @@ fn encode_info_for_foreign_item(&mut self,
             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 @@ fn encode_info_for_anon_ty(&mut self, def_id: DefId) -> Entry<'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 @@ fn encode_info_for_closure(&mut self, def_id: DefId) -> Entry<'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 @@ fn encode_codemap(&mut self) -> LazySeq<syntax_pos::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> {
@@ -1276,6 +1263,11 @@ fn encode_crate_root(&mut self) -> Lazy<CrateRoot> {
         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 @@ fn encode_crate_root(&mut self) -> Lazy<CrateRoot> {
             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 @@ fn encode_crate_root(&mut self) -> Lazy<CrateRoot> {
             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);
index f92051cbf1943a03338f8b3e8b1d8bcc5f4464f0..0b6606a00d3c041174759b55f50f5f27c036be39 100644 (file)
@@ -179,6 +179,7 @@ pub struct CrateRoot {
     pub lang_items_missing: LazySeq<lang_items::LangItem>,
     pub native_libraries: LazySeq<NativeLibrary>,
     pub codemap: LazySeq<syntax_pos::FileMap>,
+    pub def_path_table: Lazy<hir::map::definitions::DefPathTable>,
     pub impls: LazySeq<TraitImpls>,
     pub exported_symbols: LazySeq<DefIndex>,
     pub index: LazySeq<index::Index>,
@@ -202,7 +203,6 @@ pub struct Entry<'tcx> {
     pub kind: EntryKind<'tcx>,
     pub visibility: ty::Visibility,
     pub span: Lazy<Span>,
-    pub def_key: Lazy<hir::map::DefKey>,
     pub attributes: LazySeq<ast::Attribute>,
     pub children: LazySeq<DefIndex>,
     pub stability: Option<Lazy<attr::Stability>>,