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;
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>,
}
}
-/// "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());
}
-> 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);
{
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);
--- /dev/null
+// 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)
+ }
+}
+