]> git.lizzy.rs Git - rust.git/commitdiff
move CrateIndex into its own module
authorNiko Matsakis <niko@alum.mit.edu>
Wed, 10 Aug 2016 18:27:06 +0000 (14:27 -0400)
committerNiko Matsakis <niko@alum.mit.edu>
Wed, 17 Aug 2016 11:39:11 +0000 (07:39 -0400)
src/librustc_metadata/encoder.rs
src/librustc_metadata/index_builder.rs [new file with mode: 0644]
src/librustc_metadata/lib.rs

index 320ba3c8d9dc863c9344c800cc6228cf6a247097..3ce9064f80ab529517b77d114f79ee2f83637499 100644 (file)
@@ -25,7 +25,7 @@
 use rustc::hir::def;
 use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
 use middle::dependency_format::Linkage;
-use rustc::dep_graph::{DepGraph, DepNode, DepTask};
+use rustc::dep_graph::DepNode;
 use rustc::traits::specialization_graph;
 use rustc::ty::{self, Ty, TyCtxt};
 use rustc::ty::util::IntTypeExt;
@@ -54,6 +54,8 @@
 use rustc::hir::intravisit;
 use rustc::hir::map::DefKey;
 
+use super::index_builder::{CrateIndex, XRef};
+
 pub struct EncodeContext<'a, 'tcx: 'a> {
     pub diag: &'a Handler,
     pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
@@ -71,35 +73,6 @@ fn local_id(&self, def_id: DefId) -> NodeId {
     }
 }
 
-/// "interned" entries referenced by id
-#[derive(PartialEq, Eq, Hash)]
-pub enum XRef<'tcx> { Predicate(ty::Predicate<'tcx>) }
-
-struct CrateIndex<'a, 'tcx> {
-    dep_graph: &'a DepGraph,
-    items: IndexData,
-    xrefs: FnvHashMap<XRef<'tcx>, u32>, // sequentially-assigned
-}
-
-impl<'a, 'tcx> CrateIndex<'a, 'tcx> {
-    /// Records that `id` is being emitted at the current offset.
-    /// This data is later used to construct the item index in the
-    /// metadata so we can quickly find the data for a given item.
-    ///
-    /// Returns a dep-graph task that you should keep live as long as
-    /// the data for this item is being emitted.
-    fn record(&mut self, id: DefId, rbml_w: &mut Encoder) -> DepTask<'a> {
-        let position = rbml_w.mark_stable_position();
-        self.items.record(id, position);
-        self.dep_graph.in_task(DepNode::MetaData(id))
-    }
-
-    fn add_xref(&mut self, xref: XRef<'tcx>) -> u32 {
-        let old_len = self.xrefs.len() as u32;
-        *self.xrefs.entry(xref).or_insert(old_len)
-    }
-}
-
 fn encode_name(rbml_w: &mut Encoder, name: Name) {
     rbml_w.wr_tagged_str(tag_paths_data_name, &name.as_str());
 }
@@ -1380,11 +1353,7 @@ fn encode_info_for_items<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
                                    -> CrateIndex<'a, 'tcx> {
     let krate = ecx.tcx.map.krate();
 
-    let mut index = CrateIndex {
-        dep_graph: &ecx.tcx.dep_graph,
-        items: IndexData::new(ecx.tcx.map.num_local_def_ids()),
-        xrefs: FnvHashMap()
-    };
+    let mut index = CrateIndex::new(ecx);
     rbml_w.start_tag(tag_items_data);
 
     {
@@ -1929,12 +1898,14 @@ struct Stats {
     stats.item_bytes = rbml_w.writer.seek(SeekFrom::Current(0)).unwrap() - i;
     rbml_w.end_tag();
 
+    let (items, xrefs) = index.into_fields();
+
     i = rbml_w.writer.seek(SeekFrom::Current(0)).unwrap();
-    encode_item_index(rbml_w, index.items);
+    encode_item_index(rbml_w, items);
     stats.index_bytes = rbml_w.writer.seek(SeekFrom::Current(0)).unwrap() - i;
 
     i = rbml_w.writer.seek(SeekFrom::Current(0)).unwrap();
-    encode_xrefs(&ecx, rbml_w, index.xrefs);
+    encode_xrefs(&ecx, rbml_w, xrefs);
     stats.xref_bytes = rbml_w.writer.seek(SeekFrom::Current(0)).unwrap() - i;
 
     encode_struct_field_attrs(&ecx, rbml_w, krate);
diff --git a/src/librustc_metadata/index_builder.rs b/src/librustc_metadata/index_builder.rs
new file mode 100644 (file)
index 0000000..3921a91
--- /dev/null
@@ -0,0 +1,59 @@
+// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use encoder::EncodeContext;
+use index::IndexData;
+use rbml::writer::Encoder;
+use rustc::dep_graph::{DepGraph, DepNode, DepTask};
+use rustc::hir::def_id::DefId;
+use rustc::ty;
+use rustc_data_structures::fnv::FnvHashMap;
+
+pub struct CrateIndex<'a, 'tcx> {
+    dep_graph: &'a DepGraph,
+    items: IndexData,
+    xrefs: FnvHashMap<XRef<'tcx>, u32>, // sequentially-assigned
+}
+
+/// "interned" entries referenced by id
+#[derive(PartialEq, Eq, Hash)]
+pub enum XRef<'tcx> { Predicate(ty::Predicate<'tcx>) }
+
+impl<'a, 'tcx> CrateIndex<'a, 'tcx> {
+    pub fn new(ecx: &EncodeContext<'a, 'tcx>) -> Self {
+        CrateIndex {
+            dep_graph: &ecx.tcx.dep_graph,
+            items: IndexData::new(ecx.tcx.map.num_local_def_ids()),
+            xrefs: FnvHashMap()
+        }
+    }
+
+    /// Records that `id` is being emitted at the current offset.
+    /// This data is later used to construct the item index in the
+    /// metadata so we can quickly find the data for a given item.
+    ///
+    /// Returns a dep-graph task that you should keep live as long as
+    /// the data for this item is being emitted.
+    pub fn record(&mut self, id: DefId, rbml_w: &mut Encoder) -> DepTask<'a> {
+        let position = rbml_w.mark_stable_position();
+        self.items.record(id, position);
+        self.dep_graph.in_task(DepNode::MetaData(id))
+    }
+
+    pub fn add_xref(&mut self, xref: XRef<'tcx>) -> u32 {
+        let old_len = self.xrefs.len() as u32;
+        *self.xrefs.entry(xref).or_insert(old_len)
+    }
+
+    pub fn into_fields(self) -> (IndexData, FnvHashMap<XRef<'tcx>, u32>) {
+        (self.items, self.xrefs)
+    }
+}
+
index cd92493e3db703adc8e7931d51357fbc6292406f..a96fa8a006d8967bb4039147d0104c255c1d0993 100644 (file)
@@ -54,6 +54,7 @@
 pub mod tyencode;
 pub mod tydecode;
 pub mod encoder;
+mod index_builder;
 pub mod decoder;
 pub mod creader;
 pub mod csearch;