Skip to content

Commit d45dd94

Browse files
arielb1Ariel Ben-Yehuda
authored andcommitted
make CrateStore a trait object
rustdoc still broken
1 parent 11dbb69 commit d45dd94

File tree

29 files changed

+200
-143
lines changed

29 files changed

+200
-143
lines changed

src/librustc/metadata/creader.rs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ use log;
4343

4444
pub struct LocalCrateReader<'a, 'b:'a> {
4545
sess: &'a Session,
46+
cstore: &'a CStore,
4647
creader: CrateReader<'a>,
4748
ast_map: &'a hir_map::Map<'b>,
4849
}
4950

5051
pub struct CrateReader<'a> {
5152
sess: &'a Session,
53+
cstore: &'a CStore,
5254
next_crate_num: ast::CrateNum,
5355
foreign_item_map: FnvHashMap<String, Vec<ast::NodeId>>,
5456
}
@@ -113,6 +115,7 @@ pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option<Span>) {
113115

114116

115117
fn register_native_lib(sess: &Session,
118+
cstore: &CStore,
116119
span: Option<Span>,
117120
name: String,
118121
kind: cstore::NativeLibraryKind) {
@@ -139,7 +142,7 @@ fn register_native_lib(sess: &Session,
139142
None => sess.err(msg),
140143
}
141144
}
142-
sess.cstore.add_used_library(name, kind);
145+
cstore.add_used_library(name, kind);
143146
}
144147

145148
// Extra info about a crate loaded for plugins or exported macros.
@@ -164,10 +167,11 @@ impl PMDSource {
164167
}
165168

166169
impl<'a> CrateReader<'a> {
167-
pub fn new(sess: &'a Session) -> CrateReader<'a> {
170+
pub fn new(sess: &'a Session, cstore: &'a CStore) -> CrateReader<'a> {
168171
CrateReader {
169172
sess: sess,
170-
next_crate_num: sess.cstore.next_crate_num(),
173+
cstore: cstore,
174+
next_crate_num: cstore.next_crate_num(),
171175
foreign_item_map: FnvHashMap(),
172176
}
173177
}
@@ -224,7 +228,7 @@ impl<'a> CrateReader<'a> {
224228
fn existing_match(&self, name: &str, hash: Option<&Svh>, kind: PathKind)
225229
-> Option<ast::CrateNum> {
226230
let mut ret = None;
227-
self.sess.cstore.iter_crate_data(|cnum, data| {
231+
self.cstore.iter_crate_data(|cnum, data| {
228232
if data.name != name { return }
229233

230234
match hash {
@@ -242,7 +246,7 @@ impl<'a> CrateReader<'a> {
242246
// We're also sure to compare *paths*, not actual byte slices. The
243247
// `source` stores paths which are normalized which may be different
244248
// from the strings on the command line.
245-
let source = self.sess.cstore.do_get_used_crate_source(cnum).unwrap();
249+
let source = self.cstore.do_get_used_crate_source(cnum).unwrap();
246250
if let Some(locs) = self.sess.opts.externs.get(name) {
247251
let found = locs.iter().any(|l| {
248252
let l = fs::canonicalize(l).ok();
@@ -342,8 +346,8 @@ impl<'a> CrateReader<'a> {
342346
cnum: cnum,
343347
};
344348

345-
self.sess.cstore.set_crate_data(cnum, cmeta.clone());
346-
self.sess.cstore.add_used_crate_source(source.clone());
349+
self.cstore.set_crate_data(cnum, cmeta.clone());
350+
self.cstore.add_used_crate_source(source.clone());
347351
(cnum, cmeta, source)
348352
}
349353

@@ -398,7 +402,7 @@ impl<'a> CrateReader<'a> {
398402
let meta_hash = decoder::get_crate_hash(library.metadata
399403
.as_slice());
400404
let mut result = LookupResult::Loaded(library);
401-
self.sess.cstore.iter_crate_data(|cnum, data| {
405+
self.cstore.iter_crate_data(|cnum, data| {
402406
if data.name() == name && meta_hash == data.hash() {
403407
assert!(hash.is_none());
404408
result = LookupResult::Previous(cnum);
@@ -410,11 +414,11 @@ impl<'a> CrateReader<'a> {
410414

411415
match result {
412416
LookupResult::Previous(cnum) => {
413-
let data = self.sess.cstore.get_crate_data(cnum);
417+
let data = self.cstore.get_crate_data(cnum);
414418
if explicitly_linked && !data.explicitly_linked.get() {
415419
data.explicitly_linked.set(explicitly_linked);
416420
}
417-
(cnum, data, self.sess.cstore.do_get_used_crate_source(cnum).unwrap())
421+
(cnum, data, self.cstore.do_get_used_crate_source(cnum).unwrap())
418422
}
419423
LookupResult::Loaded(library) => {
420424
self.register_crate(root, ident, name, span, library,
@@ -512,7 +516,7 @@ impl<'a> CrateReader<'a> {
512516
let source_name = format!("<{} macros>", item.ident);
513517
let mut macros = vec![];
514518
decoder::each_exported_macro(ekrate.metadata.as_slice(),
515-
&*self.sess.cstore.intr,
519+
&*self.cstore.intr,
516520
|name, attrs, body| {
517521
// NB: Don't use parse::parse_tts_from_source_str because it parses with
518522
// quote_depth > 0.
@@ -593,14 +597,14 @@ impl<'a> CrateReader<'a> {
593597
}
594598

595599
fn register_statically_included_foreign_items(&mut self) {
596-
let libs = self.sess.cstore.get_used_libraries();
600+
let libs = self.cstore.get_used_libraries();
597601
for (lib, list) in self.foreign_item_map.iter() {
598602
let is_static = libs.borrow().iter().any(|&(ref name, kind)| {
599603
lib == name && kind == cstore::NativeStatic
600604
});
601605
if is_static {
602606
for id in list {
603-
self.sess.cstore.add_statically_included_foreign_item(*id);
607+
self.cstore.add_statically_included_foreign_item(*id);
604608
}
605609
}
606610
}
@@ -614,7 +618,7 @@ impl<'a> CrateReader<'a> {
614618
// also bail out as we don't need to implicitly inject one.
615619
let mut needs_allocator = false;
616620
let mut found_required_allocator = false;
617-
self.sess.cstore.iter_crate_data(|cnum, data| {
621+
self.cstore.iter_crate_data(|cnum, data| {
618622
needs_allocator = needs_allocator || data.needs_allocator();
619623
if data.is_allocator() {
620624
debug!("{} required by rlib and is an allocator", data.name());
@@ -693,7 +697,7 @@ impl<'a> CrateReader<'a> {
693697
//
694698
// Here we inject a dependency from all crates with #![needs_allocator]
695699
// to the crate tagged with #![allocator] for this compilation unit.
696-
self.sess.cstore.iter_crate_data(|cnum, data| {
700+
self.cstore.iter_crate_data(|cnum, data| {
697701
if !data.needs_allocator() {
698702
return
699703
}
@@ -707,10 +711,10 @@ impl<'a> CrateReader<'a> {
707711

708712
fn validate(me: &CrateReader, krate: ast::CrateNum,
709713
allocator: ast::CrateNum) {
710-
let data = me.sess.cstore.get_crate_data(krate);
714+
let data = me.cstore.get_crate_data(krate);
711715
if data.needs_allocator() {
712716
let krate_name = data.name();
713-
let data = me.sess.cstore.get_crate_data(allocator);
717+
let data = me.cstore.get_crate_data(allocator);
714718
let alloc_name = data.name();
715719
me.sess.err(&format!("the allocator crate `{}` cannot depend \
716720
on a crate that needs an allocator, but \
@@ -726,10 +730,11 @@ impl<'a> CrateReader<'a> {
726730
}
727731

728732
impl<'a, 'b> LocalCrateReader<'a, 'b> {
729-
pub fn new(sess: &'a Session, map: &'a hir_map::Map<'b>) -> LocalCrateReader<'a, 'b> {
733+
pub fn new(sess: &'a Session, cstore: &'a CStore, map: &'a hir_map::Map<'b>) -> LocalCrateReader<'a, 'b> {
730734
LocalCrateReader {
731735
sess: sess,
732-
creader: CrateReader::new(sess),
736+
cstore: cstore,
737+
creader: CrateReader::new(sess, cstore),
733738
ast_map: map,
734739
}
735740
}
@@ -743,19 +748,19 @@ impl<'a, 'b> LocalCrateReader<'a, 'b> {
743748
self.creader.inject_allocator_crate();
744749

745750
if log_enabled!(log::INFO) {
746-
dump_crates(&self.sess.cstore);
751+
dump_crates(&self.cstore);
747752
}
748753

749754
for &(ref name, kind) in &self.sess.opts.libs {
750-
register_native_lib(self.sess, None, name.clone(), kind);
755+
register_native_lib(self.sess, self.cstore, None, name.clone(), kind);
751756
}
752757
self.creader.register_statically_included_foreign_items();
753758
}
754759

755760
fn process_crate(&self, c: &hir::Crate) {
756761
for a in c.attrs.iter().filter(|m| m.name() == "link_args") {
757762
match a.value_str() {
758-
Some(ref linkarg) => self.sess.cstore.add_used_link_args(&linkarg),
763+
Some(ref linkarg) => self.cstore.add_used_link_args(&linkarg),
759764
None => { /* fallthrough */ }
760765
}
761766
}
@@ -783,7 +788,7 @@ impl<'a, 'b> LocalCrateReader<'a, 'b> {
783788
self.ast_map.with_path(i.id, |path| {
784789
cmeta.update_local_path(path)
785790
});
786-
self.sess.cstore.add_extern_mod_stmt_cnum(info.id, cnum);
791+
self.cstore.add_extern_mod_stmt_cnum(info.id, cnum);
787792
}
788793
None => ()
789794
}
@@ -801,7 +806,7 @@ impl<'a, 'b> LocalCrateReader<'a, 'b> {
801806
// First, add all of the custom #[link_args] attributes
802807
for m in i.attrs.iter().filter(|a| a.check_name("link_args")) {
803808
if let Some(linkarg) = m.value_str() {
804-
self.sess.cstore.add_used_link_args(&linkarg);
809+
self.cstore.add_used_link_args(&linkarg);
805810
}
806811
}
807812

@@ -836,7 +841,7 @@ impl<'a, 'b> LocalCrateReader<'a, 'b> {
836841
InternedString::new("foo")
837842
}
838843
};
839-
register_native_lib(self.sess, Some(m.span), n.to_string(), kind);
844+
register_native_lib(self.sess, self.cstore, Some(m.span), n.to_string(), kind);
840845
}
841846

842847
// Finally, process the #[linked_from = "..."] attribute

src/librustc/metadata/cstore.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ enum_from_u32! {
9494

9595
// Where a crate came from on the local filesystem. One of these two options
9696
// must be non-None.
97-
#[derive(PartialEq, Clone)]
97+
#[derive(PartialEq, Clone, Debug)]
9898
pub struct CrateSource {
9999
pub dylib: Option<(PathBuf, PathKind)>,
100100
pub rlib: Option<(PathBuf, PathKind)>,
@@ -266,7 +266,7 @@ impl CStore {
266266
self.statically_included_foreign_items.borrow_mut().insert(id);
267267
}
268268

269-
pub fn is_statically_included_foreign_item(&self, id: ast::NodeId) -> bool {
269+
pub fn do_is_statically_included_foreign_item(&self, id: ast::NodeId) -> bool {
270270
self.statically_included_foreign_items.borrow().contains(&id)
271271
}
272272
}

src/librustc/metadata/decoder.rs

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@ use back::svh::Svh;
2222
use metadata::cstore::crate_metadata;
2323
use metadata::cstore::LOCAL_CRATE;
2424
use metadata::common::*;
25-
use metadata::csearch::MethodInfo;
26-
use metadata::csearch;
2725
use metadata::cstore;
2826
use metadata::encoder::def_to_u64;
2927
use metadata::index;
3028
use metadata::inline::InlinedItem;
3129
use metadata::tydecode::TyDecoder;
30+
use metadata::util::FoundAst;
3231
use middle::def;
3332
use middle::def_id::{DefId, DefIndex};
3433
use middle::lang_items;
@@ -771,24 +770,24 @@ pub type DecodeInlinedItem<'a> =
771770

772771
pub fn maybe_get_item_ast<'tcx>(cdata: Cmd, tcx: &ty::ctxt<'tcx>, id: DefIndex,
773772
mut decode_inlined_item: DecodeInlinedItem)
774-
-> csearch::FoundAst<'tcx> {
773+
-> FoundAst<'tcx> {
775774
debug!("Looking up item: {:?}", id);
776775
let item_doc = cdata.lookup_item(id);
777776
let item_did = item_def_id(item_doc, cdata);
778777
let path = item_path(item_doc).split_last().unwrap().1.to_vec();
779778
let def_path = def_path(cdata, id);
780779
match decode_inlined_item(cdata, tcx, path, def_path, item_doc, item_did) {
781-
Ok(ii) => csearch::FoundAst::Found(ii),
780+
Ok(ii) => FoundAst::Found(ii),
782781
Err((path, def_path)) => {
783782
match item_parent_item(cdata, item_doc) {
784783
Some(did) => {
785784
let parent_item = cdata.lookup_item(did.index);
786785
match decode_inlined_item(cdata, tcx, path, def_path, parent_item, did) {
787-
Ok(ii) => csearch::FoundAst::FoundParent(did, ii),
788-
Err(_) => csearch::FoundAst::NotFound
786+
Ok(ii) => FoundAst::FoundParent(did, ii),
787+
Err(_) => FoundAst::NotFound
789788
}
790789
}
791-
None => csearch::FoundAst::NotFound
790+
None => FoundAst::NotFound
792791
}
793792
}
794793
}
@@ -997,42 +996,6 @@ pub fn get_associated_consts<'tcx>(intr: Rc<IdentInterner>,
997996
}).collect()
998997
}
999998

1000-
pub fn get_methods_if_impl(intr: Rc<IdentInterner>,
1001-
cdata: Cmd,
1002-
node_id: DefIndex)
1003-
-> Option<Vec<MethodInfo> > {
1004-
let item = cdata.lookup_item(node_id);
1005-
if item_family(item) != Impl {
1006-
return None;
1007-
}
1008-
1009-
// If this impl implements a trait, don't consider it.
1010-
if reader::tagged_docs(item, tag_item_trait_ref).next().is_some() {
1011-
return None;
1012-
}
1013-
1014-
let impl_method_ids = reader::tagged_docs(item, tag_item_impl_item)
1015-
.map(|impl_method_doc| item_def_id(impl_method_doc, cdata));
1016-
1017-
let mut impl_methods = Vec::new();
1018-
for impl_method_id in impl_method_ids {
1019-
let impl_method_doc = cdata.lookup_item(impl_method_id.index);
1020-
let family = item_family(impl_method_doc);
1021-
match family {
1022-
StaticMethod | Method => {
1023-
impl_methods.push(MethodInfo {
1024-
name: item_name(&*intr, impl_method_doc),
1025-
def_id: item_def_id(impl_method_doc, cdata),
1026-
vis: item_visibility(impl_method_doc),
1027-
});
1028-
}
1029-
_ => {}
1030-
}
1031-
}
1032-
1033-
return Some(impl_methods);
1034-
}
1035-
1036999
/// If node_id is the constructor of a tuple struct, retrieve the NodeId of
10371000
/// the actual type definition, otherwise, return None
10381001
pub fn get_tuple_struct_definition_if_ctor(cdata: Cmd,

src/librustc/metadata/encoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use metadata::decoder;
2222
use metadata::tyencode;
2323
use metadata::index::{self, IndexData};
2424
use metadata::inline::InlinedItemRef;
25+
use metadata::util::CrateStore;
2526
use middle::def;
2627
use middle::def_id::{CRATE_DEF_INDEX, DefId};
2728
use middle::dependency_format::Linkage;
@@ -1652,8 +1653,7 @@ fn encode_lang_items(ecx: &EncodeContext, rbml_w: &mut Encoder) {
16521653
fn encode_native_libraries(ecx: &EncodeContext, rbml_w: &mut Encoder) {
16531654
rbml_w.start_tag(tag_native_libraries);
16541655

1655-
for &(ref lib, kind) in ecx.tcx.sess.cstore.get_used_libraries()
1656-
.borrow().iter() {
1656+
for &(ref lib, kind) in ecx.tcx.sess.cstore.used_libraries().iter() {
16571657
match kind {
16581658
cstore::NativeStatic => {} // these libraries are not propagated
16591659
cstore::NativeFramework | cstore::NativeUnknown => {

src/librustc/metadata/macro_import.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
1313
use session::Session;
1414
use metadata::creader::CrateReader;
15+
use metadata::cstore::CStore;
1516

1617
use std::collections::{HashSet, HashMap};
1718
use syntax::codemap::Span;
@@ -30,11 +31,11 @@ struct MacroLoader<'a> {
3031
}
3132

3233
impl<'a> MacroLoader<'a> {
33-
fn new(sess: &'a Session) -> MacroLoader<'a> {
34+
fn new(sess: &'a Session, cstore: &'a CStore) -> MacroLoader<'a> {
3435
MacroLoader {
3536
sess: sess,
3637
span_whitelist: HashSet::new(),
37-
reader: CrateReader::new(sess),
38+
reader: CrateReader::new(sess, cstore),
3839
macros: vec![],
3940
}
4041
}
@@ -45,8 +46,10 @@ pub fn call_bad_macro_reexport(a: &Session, b: Span) {
4546
}
4647

4748
/// Read exported macros.
48-
pub fn read_macro_defs(sess: &Session, krate: &ast::Crate) -> Vec<ast::MacroDef> {
49-
let mut loader = MacroLoader::new(sess);
49+
pub fn read_macro_defs(sess: &Session, cstore: &CStore, krate: &ast::Crate)
50+
-> Vec<ast::MacroDef>
51+
{
52+
let mut loader = MacroLoader::new(sess, cstore);
5053

5154
// We need to error on `#[macro_use] extern crate` when it isn't at the
5255
// crate root, because `$crate` won't work properly. Identify these by

src/librustc/metadata/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub mod encoder;
1515
pub mod decoder;
1616
pub mod creader;
1717
pub mod cstore;
18-
pub mod csearch;
1918
pub mod index;
2019
pub mod loader;
2120
pub mod filesearch;

0 commit comments

Comments
 (0)