Skip to content

Commit f5fbefa

Browse files
arielb1Ariel Ben-Yehuda
authored and
Ariel Ben-Yehuda
committed
remove csearch from resolve and typeck
1 parent 3877664 commit f5fbefa

File tree

5 files changed

+130
-74
lines changed

5 files changed

+130
-74
lines changed

src/librustc/metadata/util.rs

+82-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use middle::lang_items;
1616
use middle::ty;
1717
use middle::def_id::{DefId, DefIndex};
1818

19+
use std::any::Any;
1920
use std::rc::Rc;
2021
use syntax::ast;
2122
use syntax::attr;
@@ -24,9 +25,18 @@ use rustc_front::hir;
2425
pub use metadata::csearch::FoundAst;
2526
pub use metadata::cstore::LinkagePreference;
2627
pub use metadata::decoder::DecodeInlinedItem;
28+
pub use metadata::decoder::DefLike;
2729
pub use metadata::inline::InlinedItem;
2830

29-
pub trait CrateStore<'tcx> {
31+
pub use self::DefLike::{DlDef, DlField, DlImpl};
32+
33+
pub struct ChildItem {
34+
pub def: DefLike,
35+
pub name: ast::Name,
36+
pub vis: hir::Visibility
37+
}
38+
39+
pub trait CrateStore<'tcx> : Any {
3040
// item info
3141
fn stability(&self, def: DefId) -> Option<attr::Stability>;
3242
fn closure_kind(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId)
@@ -75,17 +85,24 @@ pub trait CrateStore<'tcx> {
7585
fn is_const_fn(&self, did: DefId) -> bool;
7686
fn is_defaulted_trait(&self, did: DefId) -> bool;
7787
fn is_impl(&self, did: DefId) -> bool;
88+
fn is_static_method(&self, did: DefId) -> bool;
7889

7990
// metadata
8091
fn dylib_dependency_formats(&self, cnum: ast::CrateNum)
81-
-> Vec<(ast::CrateNum, cstore::LinkagePreference)>;
92+
-> Vec<(ast::CrateNum, LinkagePreference)>;
8293
fn lang_items(&self, cnum: ast::CrateNum) -> Vec<(DefIndex, usize)>;
83-
fn missing_lang_items(&self, cnum: ast::CrateNum)
84-
-> Vec<lang_items::LangItem>;
94+
fn missing_lang_items(&self, cnum: ast::CrateNum) -> Vec<lang_items::LangItem>;
8595
fn is_staged_api(&self, cnum: ast::CrateNum) -> bool;
96+
fn plugin_registrar_fn(&self, cnum: ast::CrateNum) -> Option<DefId>;
8697

87-
// misc.
98+
// resolve
8899
fn def_path(&self, def: DefId) -> ast_map::DefPath;
100+
fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>;
101+
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
102+
fn item_children(&self, did: DefId) -> Vec<ChildItem>;
103+
fn crate_top_level_items(&self, cnum: ast::CrateNum) -> Vec<ChildItem>;
104+
105+
// misc.
89106
fn maybe_get_item_ast(&'tcx self, tcx: &ty::ctxt<'tcx>, def: DefId)
90107
-> FoundAst<'tcx>;
91108
}
@@ -278,8 +295,13 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
278295
decoder::is_impl(&*cdata, did.index)
279296
}
280297

298+
fn is_static_method(&self, def: DefId) -> bool {
299+
let cdata = self.get_crate_data(def.krate);
300+
decoder::is_static_method(&*cdata, def.index)
301+
}
302+
281303
fn dylib_dependency_formats(&self, cnum: ast::CrateNum)
282-
-> Vec<(ast::CrateNum, cstore::LinkagePreference)>
304+
-> Vec<(ast::CrateNum, LinkagePreference)>
283305
{
284306
let cdata = self.get_crate_data(cnum);
285307
decoder::get_dylib_dependency_formats(&cdata)
@@ -307,13 +329,66 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
307329
self.get_crate_data(cnum).staged_api
308330
}
309331

310-
fn def_path(&self, def: DefId) -> ast_map::DefPath {
332+
fn plugin_registrar_fn(&self, cnum: ast::CrateNum) -> Option<DefId>
333+
{
334+
let cdata = self.get_crate_data(cnum);
335+
decoder::get_plugin_registrar_fn(cdata.data()).map(|index| DefId {
336+
krate: cnum,
337+
index: index
338+
})
339+
}
340+
341+
fn def_path(&self, def: DefId) -> ast_map::DefPath
342+
{
311343
let cdata = self.get_crate_data(def.krate);
312344
let path = decoder::def_path(&*cdata, def.index);
313345
let local_path = cdata.local_def_path();
314346
local_path.into_iter().chain(path).collect()
315347
}
316348

349+
fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>
350+
{
351+
let cdata = self.get_crate_data(did.krate);
352+
decoder::get_tuple_struct_definition_if_ctor(&*cdata, did.index)
353+
}
354+
355+
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>
356+
{
357+
let cdata = self.get_crate_data(def.krate);
358+
decoder::get_struct_field_names(&self.intr, &*cdata, def.index)
359+
}
360+
361+
fn item_children(&self, def_id: DefId) -> Vec<ChildItem>
362+
{
363+
let mut result = vec![];
364+
let crate_data = self.get_crate_data(def_id.krate);
365+
let get_crate_data = |cnum| self.get_crate_data(cnum);
366+
decoder::each_child_of_item(
367+
self.intr.clone(), &*crate_data,
368+
def_id.index, get_crate_data,
369+
|def, name, vis| result.push(ChildItem {
370+
def: def,
371+
name: name,
372+
vis: vis
373+
}));
374+
result
375+
}
376+
377+
fn crate_top_level_items(&self, cnum: ast::CrateNum) -> Vec<ChildItem>
378+
{
379+
let mut result = vec![];
380+
let crate_data = self.get_crate_data(cnum);
381+
let get_crate_data = |cnum| self.get_crate_data(cnum);
382+
decoder::each_top_level_item_of_crate(
383+
self.intr.clone(), &*crate_data, get_crate_data,
384+
|def, name, vis| result.push(ChildItem {
385+
def: def,
386+
name: name,
387+
vis: vis
388+
}));
389+
result
390+
}
391+
317392
fn maybe_get_item_ast(&'tcx self, tcx: &ty::ctxt<'tcx>, def: DefId)
318393
-> FoundAst<'tcx>
319394
{

src/librustc_lint/builtin.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
//! Use the former for unit-like structs and the latter for structs with
2929
//! a `pub fn new()`.
3030
31-
use metadata::decoder;
3231
use middle::{cfg, def, infer, stability, traits};
3332
use middle::def_id::DefId;
3433
use middle::subst::Substs;
3534
use middle::ty::{self, Ty};
3635
use middle::ty::adjustment;
36+
use rustc::metadata::util::CrateStore;
3737
use rustc::front::map as hir_map;
3838
use util::nodemap::{NodeSet};
3939
use lint::{Level, LateContext, LintContext, LintArray, Lint};
@@ -936,8 +936,8 @@ impl LateLintPass for PluginAsLibrary {
936936
_ => return,
937937
};
938938

939-
let md = match cx.sess().cstore.find_extern_mod_stmt_cnum(it.id) {
940-
Some(cnum) => cx.sess().cstore.get_crate_data(cnum),
939+
let prfn = match cx.sess().cstore.find_extern_mod_stmt_cnum(it.id) {
940+
Some(cnum) => cx.sess().cstore.plugin_registrar_fn(cnum),
941941
None => {
942942
// Probably means we aren't linking the crate for some reason.
943943
//
@@ -946,7 +946,7 @@ impl LateLintPass for PluginAsLibrary {
946946
}
947947
};
948948

949-
if decoder::get_plugin_registrar_fn(md.data()).is_some() {
949+
if prfn.is_some() {
950950
cx.span_lint(PLUGIN_AS_LIBRARY, it.span,
951951
"compiler plugin used as an ordinary library");
952952
}

src/librustc_resolve/build_reduced_graph.rs

+25-44
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ use {resolve_error, ResolutionError};
2828

2929
use self::DuplicateCheckingMode::*;
3030

31-
use rustc::metadata::csearch;
32-
use rustc::metadata::decoder::{DefLike, DlDef, DlField, DlImpl};
31+
use rustc::metadata::util::{CrateStore, ChildItem, DlDef, DlField, DlImpl};
3332
use rustc::middle::def::*;
3433
use rustc::middle::def_id::{CRATE_DEF_INDEX, DefId};
3534

@@ -625,7 +624,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
625624
}
626625
DefFn(ctor_id, true) => {
627626
child_name_bindings.define_value(
628-
csearch::get_tuple_struct_definition_if_ctor(&self.session.cstore, ctor_id)
627+
self.session.cstore.tuple_struct_definition_if_ctor(ctor_id)
629628
.map_or(def, |_| DefStruct(ctor_id)), DUMMY_SP, modifiers);
630629
}
631630
DefFn(..) |
@@ -654,11 +653,10 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
654653
// If this is a trait, add all the trait item names to the trait
655654
// info.
656655

657-
let trait_item_def_ids = csearch::get_trait_item_def_ids(&self.session.cstore,
658-
def_id);
656+
let trait_item_def_ids = self.session.cstore.trait_item_def_ids(def_id);
659657
for trait_item_def in &trait_item_def_ids {
660-
let trait_item_name = csearch::get_trait_name(&self.session.cstore,
661-
trait_item_def.def_id());
658+
let trait_item_name =
659+
self.session.cstore.item_name(trait_item_def.def_id());
662660

663661
debug!("(building reduced graph for external crate) ... adding trait item \
664662
'{}'",
@@ -695,7 +693,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
695693
debug!("(building reduced graph for external crate) building type and value for \
696694
{}",
697695
final_ident);
698-
let fields = csearch::get_struct_field_names(&self.session.cstore, def_id);
696+
child_name_bindings.define_type(def, DUMMY_SP, modifiers);
697+
let fields = self.session.cstore.struct_field_names(def_id);
699698

700699
if fields.is_empty() {
701700
child_name_bindings.define_value(def, DUMMY_SP, modifiers);
@@ -719,39 +718,29 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
719718
/// Builds the reduced graph for a single item in an external crate.
720719
fn build_reduced_graph_for_external_crate_def(&mut self,
721720
root: &Rc<Module>,
722-
def_like: DefLike,
723-
name: Name,
724-
def_visibility: Visibility) {
725-
match def_like {
721+
xcdef: ChildItem) {
722+
match xcdef.def {
726723
DlDef(def) => {
727724
// Add the new child item, if necessary.
728725
match def {
729726
DefForeignMod(def_id) => {
730727
// Foreign modules have no names. Recur and populate
731728
// eagerly.
732-
csearch::each_child_of_item(&self.session.cstore,
733-
def_id,
734-
|def_like,
735-
child_name,
736-
vis| {
737-
self.build_reduced_graph_for_external_crate_def(
738-
root,
739-
def_like,
740-
child_name,
741-
vis)
742-
});
729+
for child in self.session.cstore.item_children(def_id) {
730+
self.build_reduced_graph_for_external_crate_def(root, child)
731+
}
743732
}
744733
_ => {
745-
let child_name_bindings = self.add_child(name,
734+
let child_name_bindings = self.add_child(xcdef.name,
746735
root,
747736
OverwriteDuplicates,
748737
DUMMY_SP);
749738

750739
self.handle_external_def(def,
751-
def_visibility,
740+
xcdef.vis,
752741
&child_name_bindings,
753-
&name.as_str(),
754-
name,
742+
&xcdef.name.as_str(),
743+
xcdef.name,
755744
root);
756745
}
757746
}
@@ -778,16 +767,11 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
778767
Some(def_id) => def_id,
779768
};
780769

781-
csearch::each_child_of_item(&self.session.cstore,
782-
def_id,
783-
|def_like, child_name, visibility| {
784-
debug!("(populating external module) ... found ident: {}",
785-
child_name);
786-
self.build_reduced_graph_for_external_crate_def(module,
787-
def_like,
788-
child_name,
789-
visibility)
790-
});
770+
for child in self.session.cstore.item_children(def_id) {
771+
debug!("(populating external module) ... found ident: {}",
772+
child.name);
773+
self.build_reduced_graph_for_external_crate_def(module, child);
774+
}
791775
module.populated.set(true)
792776
}
793777

@@ -803,13 +787,10 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
803787
/// Builds the reduced graph rooted at the 'use' directive for an external
804788
/// crate.
805789
fn build_reduced_graph_for_external_crate(&mut self, root: &Rc<Module>) {
806-
csearch::each_top_level_item_of_crate(&self.session.cstore,
807-
root.def_id()
808-
.unwrap()
809-
.krate,
810-
|def_like, name, visibility| {
811-
self.build_reduced_graph_for_external_crate_def(root, def_like, name, visibility)
812-
});
790+
let root_cnum = root.def_id().unwrap().krate;
791+
for child in self.session.cstore.crate_top_level_items(root_cnum) {
792+
self.build_reduced_graph_for_external_crate_def(root, child);
793+
}
813794
}
814795

815796
/// Creates and adds an import directive to the given module.

src/librustc_resolve/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ use self::FallbackChecks::*;
5454
use rustc::front::map as hir_map;
5555
use rustc::session::Session;
5656
use rustc::lint;
57-
use rustc::metadata::csearch;
58-
use rustc::metadata::decoder::{DefLike, DlDef};
57+
use rustc::metadata::util::{CrateStore, DefLike, DlDef};
5958
use rustc::middle::def::*;
6059
use rustc::middle::def_id::DefId;
6160
use rustc::middle::pat_util::pat_bindings_hygienic;
@@ -1235,7 +1234,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12351234
if let Some(node_id) = self.ast_map.as_local_node_id(did) {
12361235
self.ast_map.expect_item(node_id).name
12371236
} else {
1238-
csearch::get_trait_name(&self.session.cstore, did)
1237+
self.session.cstore.item_name(did)
12391238
}
12401239
}
12411240

@@ -3298,7 +3297,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
32983297
};
32993298
sig.explicit_self.node == hir::SelfStatic
33003299
} else {
3301-
csearch::is_static_method(&this.session.cstore, did)
3300+
this.session.cstore.is_static_method(did)
33023301
}
33033302
}
33043303

src/librustc_typeck/check/method/suggest.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use middle::def_id::DefId;
2222
use middle::lang_items::FnOnceTraitLangItem;
2323
use middle::subst::Substs;
2424
use middle::traits::{Obligation, SelectionContext};
25-
use metadata::{csearch, cstore, decoder};
25+
use metadata::util::{self as mdutil, CrateStore, DefLike};
2626
use util::nodemap::{FnvHashSet};
2727

2828
use syntax::ast;
@@ -418,31 +418,32 @@ pub fn all_traits<'a>(ccx: &'a CrateCtxt) -> AllTraits<'a> {
418418
fn handle_external_def(traits: &mut AllTraitsVec,
419419
external_mods: &mut FnvHashSet<DefId>,
420420
ccx: &CrateCtxt,
421-
cstore: &cstore::CStore,
422-
dl: decoder::DefLike) {
421+
cstore: &mdutil::CrateStore,
422+
dl: mdutil::DefLike) {
423423
match dl {
424-
decoder::DlDef(def::DefTrait(did)) => {
424+
mdutil::DlDef(def::DefTrait(did)) => {
425425
traits.push(TraitInfo::new(did));
426426
}
427-
decoder::DlDef(def::DefMod(did)) => {
427+
mdutil::DlDef(def::DefMod(did)) => {
428428
if !external_mods.insert(did) {
429429
return;
430430
}
431-
csearch::each_child_of_item(cstore, did, |dl, _, _| {
431+
for child in cstore.item_children(did) {
432432
handle_external_def(traits, external_mods,
433-
ccx, cstore, dl)
434-
})
433+
ccx, cstore, child.def)
434+
}
435435
}
436436
_ => {}
437437
}
438438
}
439-
let cstore = &ccx.tcx.sess.cstore;
440-
cstore.iter_crate_data(|cnum, _| {
441-
csearch::each_top_level_item_of_crate(cstore, cnum, |dl, _, _| {
442-
handle_external_def(&mut traits,
443-
&mut external_mods,
444-
ccx, cstore, dl)
445-
})
439+
let cstore: &mdutil::CrateStore = &ccx.tcx.sess.cstore;
440+
441+
// FIXME: privatize this
442+
ccx.tcx.sess.cstore.iter_crate_data(|cnum, _| {
443+
for child in cstore.crate_top_level_items(cnum) {
444+
handle_external_def(&mut traits, &mut external_mods,
445+
ccx, cstore, child.def)
446+
}
446447
});
447448

448449
*ccx.all_traits.borrow_mut() = Some(traits);

0 commit comments

Comments
 (0)