Skip to content

Commit b5fa8ab

Browse files
committed
move CrateIndex into its own module
1 parent d6d0590 commit b5fa8ab

File tree

3 files changed

+68
-37
lines changed

3 files changed

+68
-37
lines changed

src/librustc_metadata/encoder.rs

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use middle::cstore::{LOCAL_CRATE, InlinedItemRef, LinkMeta, tls};
2525
use rustc::hir::def;
2626
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
2727
use middle::dependency_format::Linkage;
28-
use rustc::dep_graph::{DepGraph, DepNode, DepTask};
28+
use rustc::dep_graph::DepNode;
2929
use rustc::traits::specialization_graph;
3030
use rustc::ty::{self, Ty, TyCtxt};
3131
use rustc::ty::util::IntTypeExt;
@@ -54,6 +54,8 @@ use rustc::hir::intravisit::Visitor;
5454
use rustc::hir::intravisit;
5555
use rustc::hir::map::DefKey;
5656

57+
use super::index_builder::{CrateIndex, XRef};
58+
5759
pub struct EncodeContext<'a, 'tcx: 'a> {
5860
pub diag: &'a Handler,
5961
pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
@@ -71,35 +73,6 @@ impl<'a, 'tcx> EncodeContext<'a,'tcx> {
7173
}
7274
}
7375

74-
/// "interned" entries referenced by id
75-
#[derive(PartialEq, Eq, Hash)]
76-
pub enum XRef<'tcx> { Predicate(ty::Predicate<'tcx>) }
77-
78-
struct CrateIndex<'a, 'tcx> {
79-
dep_graph: &'a DepGraph,
80-
items: IndexData,
81-
xrefs: FnvHashMap<XRef<'tcx>, u32>, // sequentially-assigned
82-
}
83-
84-
impl<'a, 'tcx> CrateIndex<'a, 'tcx> {
85-
/// Records that `id` is being emitted at the current offset.
86-
/// This data is later used to construct the item index in the
87-
/// metadata so we can quickly find the data for a given item.
88-
///
89-
/// Returns a dep-graph task that you should keep live as long as
90-
/// the data for this item is being emitted.
91-
fn record(&mut self, id: DefId, rbml_w: &mut Encoder) -> DepTask<'a> {
92-
let position = rbml_w.mark_stable_position();
93-
self.items.record(id, position);
94-
self.dep_graph.in_task(DepNode::MetaData(id))
95-
}
96-
97-
fn add_xref(&mut self, xref: XRef<'tcx>) -> u32 {
98-
let old_len = self.xrefs.len() as u32;
99-
*self.xrefs.entry(xref).or_insert(old_len)
100-
}
101-
}
102-
10376
fn encode_name(rbml_w: &mut Encoder, name: Name) {
10477
rbml_w.wr_tagged_str(tag_paths_data_name, &name.as_str());
10578
}
@@ -1380,11 +1353,7 @@ fn encode_info_for_items<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
13801353
-> CrateIndex<'a, 'tcx> {
13811354
let krate = ecx.tcx.map.krate();
13821355

1383-
let mut index = CrateIndex {
1384-
dep_graph: &ecx.tcx.dep_graph,
1385-
items: IndexData::new(ecx.tcx.map.num_local_def_ids()),
1386-
xrefs: FnvHashMap()
1387-
};
1356+
let mut index = CrateIndex::new(ecx);
13881357
rbml_w.start_tag(tag_items_data);
13891358

13901359
{
@@ -1929,12 +1898,14 @@ fn encode_metadata_inner(rbml_w: &mut Encoder,
19291898
stats.item_bytes = rbml_w.writer.seek(SeekFrom::Current(0)).unwrap() - i;
19301899
rbml_w.end_tag();
19311900

1901+
let (items, xrefs) = index.into_fields();
1902+
19321903
i = rbml_w.writer.seek(SeekFrom::Current(0)).unwrap();
1933-
encode_item_index(rbml_w, index.items);
1904+
encode_item_index(rbml_w, items);
19341905
stats.index_bytes = rbml_w.writer.seek(SeekFrom::Current(0)).unwrap() - i;
19351906

19361907
i = rbml_w.writer.seek(SeekFrom::Current(0)).unwrap();
1937-
encode_xrefs(&ecx, rbml_w, index.xrefs);
1908+
encode_xrefs(&ecx, rbml_w, xrefs);
19381909
stats.xref_bytes = rbml_w.writer.seek(SeekFrom::Current(0)).unwrap() - i;
19391910

19401911
encode_struct_field_attrs(&ecx, rbml_w, krate);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use encoder::EncodeContext;
12+
use index::IndexData;
13+
use rbml::writer::Encoder;
14+
use rustc::dep_graph::{DepGraph, DepNode, DepTask};
15+
use rustc::hir::def_id::DefId;
16+
use rustc::ty;
17+
use rustc_data_structures::fnv::FnvHashMap;
18+
19+
pub struct CrateIndex<'a, 'tcx> {
20+
dep_graph: &'a DepGraph,
21+
items: IndexData,
22+
xrefs: FnvHashMap<XRef<'tcx>, u32>, // sequentially-assigned
23+
}
24+
25+
/// "interned" entries referenced by id
26+
#[derive(PartialEq, Eq, Hash)]
27+
pub enum XRef<'tcx> { Predicate(ty::Predicate<'tcx>) }
28+
29+
impl<'a, 'tcx> CrateIndex<'a, 'tcx> {
30+
pub fn new(ecx: &EncodeContext<'a, 'tcx>) -> Self {
31+
CrateIndex {
32+
dep_graph: &ecx.tcx.dep_graph,
33+
items: IndexData::new(ecx.tcx.map.num_local_def_ids()),
34+
xrefs: FnvHashMap()
35+
}
36+
}
37+
38+
/// Records that `id` is being emitted at the current offset.
39+
/// This data is later used to construct the item index in the
40+
/// metadata so we can quickly find the data for a given item.
41+
///
42+
/// Returns a dep-graph task that you should keep live as long as
43+
/// the data for this item is being emitted.
44+
pub fn record(&mut self, id: DefId, rbml_w: &mut Encoder) -> DepTask<'a> {
45+
let position = rbml_w.mark_stable_position();
46+
self.items.record(id, position);
47+
self.dep_graph.in_task(DepNode::MetaData(id))
48+
}
49+
50+
pub fn add_xref(&mut self, xref: XRef<'tcx>) -> u32 {
51+
let old_len = self.xrefs.len() as u32;
52+
*self.xrefs.entry(xref).or_insert(old_len)
53+
}
54+
55+
pub fn into_fields(self) -> (IndexData, FnvHashMap<XRef<'tcx>, u32>) {
56+
(self.items, self.xrefs)
57+
}
58+
}
59+

src/librustc_metadata/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub mod def_key;
5454
pub mod tyencode;
5555
pub mod tydecode;
5656
pub mod encoder;
57+
mod index_builder;
5758
pub mod decoder;
5859
pub mod creader;
5960
pub mod csearch;

0 commit comments

Comments
 (0)