remove csearch from resolve and typeck

This commit is contained in:
Ariel Ben-Yehuda 2015-11-20 17:46:39 +02:00 committed by Ariel Ben-Yehuda
parent 3877664b56
commit f5fbefa3af
5 changed files with 130 additions and 74 deletions

View File

@ -16,6 +16,7 @@ use middle::lang_items;
use middle::ty;
use middle::def_id::{DefId, DefIndex};
use std::any::Any;
use std::rc::Rc;
use syntax::ast;
use syntax::attr;
@ -24,9 +25,18 @@ use rustc_front::hir;
pub use metadata::csearch::FoundAst;
pub use metadata::cstore::LinkagePreference;
pub use metadata::decoder::DecodeInlinedItem;
pub use metadata::decoder::DefLike;
pub use metadata::inline::InlinedItem;
pub trait CrateStore<'tcx> {
pub use self::DefLike::{DlDef, DlField, DlImpl};
pub struct ChildItem {
pub def: DefLike,
pub name: ast::Name,
pub vis: hir::Visibility
}
pub trait CrateStore<'tcx> : Any {
// item info
fn stability(&self, def: DefId) -> Option<attr::Stability>;
fn closure_kind(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId)
@ -75,17 +85,24 @@ pub trait CrateStore<'tcx> {
fn is_const_fn(&self, did: DefId) -> bool;
fn is_defaulted_trait(&self, did: DefId) -> bool;
fn is_impl(&self, did: DefId) -> bool;
fn is_static_method(&self, did: DefId) -> bool;
// metadata
fn dylib_dependency_formats(&self, cnum: ast::CrateNum)
-> Vec<(ast::CrateNum, cstore::LinkagePreference)>;
-> Vec<(ast::CrateNum, LinkagePreference)>;
fn lang_items(&self, cnum: ast::CrateNum) -> Vec<(DefIndex, usize)>;
fn missing_lang_items(&self, cnum: ast::CrateNum)
-> Vec<lang_items::LangItem>;
fn missing_lang_items(&self, cnum: ast::CrateNum) -> Vec<lang_items::LangItem>;
fn is_staged_api(&self, cnum: ast::CrateNum) -> bool;
fn plugin_registrar_fn(&self, cnum: ast::CrateNum) -> Option<DefId>;
// resolve
fn def_path(&self, def: DefId) -> ast_map::DefPath;
fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>;
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
fn item_children(&self, did: DefId) -> Vec<ChildItem>;
fn crate_top_level_items(&self, cnum: ast::CrateNum) -> Vec<ChildItem>;
// misc.
fn def_path(&self, def: DefId) -> ast_map::DefPath;
fn maybe_get_item_ast(&'tcx self, tcx: &ty::ctxt<'tcx>, def: DefId)
-> FoundAst<'tcx>;
}
@ -278,8 +295,13 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
decoder::is_impl(&*cdata, did.index)
}
fn is_static_method(&self, def: DefId) -> bool {
let cdata = self.get_crate_data(def.krate);
decoder::is_static_method(&*cdata, def.index)
}
fn dylib_dependency_formats(&self, cnum: ast::CrateNum)
-> Vec<(ast::CrateNum, cstore::LinkagePreference)>
-> Vec<(ast::CrateNum, LinkagePreference)>
{
let cdata = self.get_crate_data(cnum);
decoder::get_dylib_dependency_formats(&cdata)
@ -307,13 +329,66 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
self.get_crate_data(cnum).staged_api
}
fn def_path(&self, def: DefId) -> ast_map::DefPath {
fn plugin_registrar_fn(&self, cnum: ast::CrateNum) -> Option<DefId>
{
let cdata = self.get_crate_data(cnum);
decoder::get_plugin_registrar_fn(cdata.data()).map(|index| DefId {
krate: cnum,
index: index
})
}
fn def_path(&self, def: DefId) -> ast_map::DefPath
{
let cdata = self.get_crate_data(def.krate);
let path = decoder::def_path(&*cdata, def.index);
let local_path = cdata.local_def_path();
local_path.into_iter().chain(path).collect()
}
fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>
{
let cdata = self.get_crate_data(did.krate);
decoder::get_tuple_struct_definition_if_ctor(&*cdata, did.index)
}
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>
{
let cdata = self.get_crate_data(def.krate);
decoder::get_struct_field_names(&self.intr, &*cdata, def.index)
}
fn item_children(&self, def_id: DefId) -> Vec<ChildItem>
{
let mut result = vec![];
let crate_data = self.get_crate_data(def_id.krate);
let get_crate_data = |cnum| self.get_crate_data(cnum);
decoder::each_child_of_item(
self.intr.clone(), &*crate_data,
def_id.index, get_crate_data,
|def, name, vis| result.push(ChildItem {
def: def,
name: name,
vis: vis
}));
result
}
fn crate_top_level_items(&self, cnum: ast::CrateNum) -> Vec<ChildItem>
{
let mut result = vec![];
let crate_data = self.get_crate_data(cnum);
let get_crate_data = |cnum| self.get_crate_data(cnum);
decoder::each_top_level_item_of_crate(
self.intr.clone(), &*crate_data, get_crate_data,
|def, name, vis| result.push(ChildItem {
def: def,
name: name,
vis: vis
}));
result
}
fn maybe_get_item_ast(&'tcx self, tcx: &ty::ctxt<'tcx>, def: DefId)
-> FoundAst<'tcx>
{

View File

@ -28,12 +28,12 @@
//! Use the former for unit-like structs and the latter for structs with
//! a `pub fn new()`.
use metadata::decoder;
use middle::{cfg, def, infer, stability, traits};
use middle::def_id::DefId;
use middle::subst::Substs;
use middle::ty::{self, Ty};
use middle::ty::adjustment;
use rustc::metadata::util::CrateStore;
use rustc::front::map as hir_map;
use util::nodemap::{NodeSet};
use lint::{Level, LateContext, LintContext, LintArray, Lint};
@ -936,8 +936,8 @@ impl LateLintPass for PluginAsLibrary {
_ => return,
};
let md = match cx.sess().cstore.find_extern_mod_stmt_cnum(it.id) {
Some(cnum) => cx.sess().cstore.get_crate_data(cnum),
let prfn = match cx.sess().cstore.find_extern_mod_stmt_cnum(it.id) {
Some(cnum) => cx.sess().cstore.plugin_registrar_fn(cnum),
None => {
// Probably means we aren't linking the crate for some reason.
//
@ -946,7 +946,7 @@ impl LateLintPass for PluginAsLibrary {
}
};
if decoder::get_plugin_registrar_fn(md.data()).is_some() {
if prfn.is_some() {
cx.span_lint(PLUGIN_AS_LIBRARY, it.span,
"compiler plugin used as an ordinary library");
}

View File

@ -28,8 +28,7 @@ use {resolve_error, ResolutionError};
use self::DuplicateCheckingMode::*;
use rustc::metadata::csearch;
use rustc::metadata::decoder::{DefLike, DlDef, DlField, DlImpl};
use rustc::metadata::util::{CrateStore, ChildItem, DlDef, DlField, DlImpl};
use rustc::middle::def::*;
use rustc::middle::def_id::{CRATE_DEF_INDEX, DefId};
@ -625,7 +624,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
}
DefFn(ctor_id, true) => {
child_name_bindings.define_value(
csearch::get_tuple_struct_definition_if_ctor(&self.session.cstore, ctor_id)
self.session.cstore.tuple_struct_definition_if_ctor(ctor_id)
.map_or(def, |_| DefStruct(ctor_id)), DUMMY_SP, modifiers);
}
DefFn(..) |
@ -654,11 +653,10 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
// If this is a trait, add all the trait item names to the trait
// info.
let trait_item_def_ids = csearch::get_trait_item_def_ids(&self.session.cstore,
def_id);
let trait_item_def_ids = self.session.cstore.trait_item_def_ids(def_id);
for trait_item_def in &trait_item_def_ids {
let trait_item_name = csearch::get_trait_name(&self.session.cstore,
trait_item_def.def_id());
let trait_item_name =
self.session.cstore.item_name(trait_item_def.def_id());
debug!("(building reduced graph for external crate) ... adding trait item \
'{}'",
@ -695,7 +693,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
debug!("(building reduced graph for external crate) building type and value for \
{}",
final_ident);
let fields = csearch::get_struct_field_names(&self.session.cstore, def_id);
child_name_bindings.define_type(def, DUMMY_SP, modifiers);
let fields = self.session.cstore.struct_field_names(def_id);
if fields.is_empty() {
child_name_bindings.define_value(def, DUMMY_SP, modifiers);
@ -719,39 +718,29 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
/// Builds the reduced graph for a single item in an external crate.
fn build_reduced_graph_for_external_crate_def(&mut self,
root: &Rc<Module>,
def_like: DefLike,
name: Name,
def_visibility: Visibility) {
match def_like {
xcdef: ChildItem) {
match xcdef.def {
DlDef(def) => {
// Add the new child item, if necessary.
match def {
DefForeignMod(def_id) => {
// Foreign modules have no names. Recur and populate
// eagerly.
csearch::each_child_of_item(&self.session.cstore,
def_id,
|def_like,
child_name,
vis| {
self.build_reduced_graph_for_external_crate_def(
root,
def_like,
child_name,
vis)
});
for child in self.session.cstore.item_children(def_id) {
self.build_reduced_graph_for_external_crate_def(root, child)
}
}
_ => {
let child_name_bindings = self.add_child(name,
let child_name_bindings = self.add_child(xcdef.name,
root,
OverwriteDuplicates,
DUMMY_SP);
self.handle_external_def(def,
def_visibility,
xcdef.vis,
&child_name_bindings,
&name.as_str(),
name,
&xcdef.name.as_str(),
xcdef.name,
root);
}
}
@ -778,16 +767,11 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
Some(def_id) => def_id,
};
csearch::each_child_of_item(&self.session.cstore,
def_id,
|def_like, child_name, visibility| {
debug!("(populating external module) ... found ident: {}",
child_name);
self.build_reduced_graph_for_external_crate_def(module,
def_like,
child_name,
visibility)
});
for child in self.session.cstore.item_children(def_id) {
debug!("(populating external module) ... found ident: {}",
child.name);
self.build_reduced_graph_for_external_crate_def(module, child);
}
module.populated.set(true)
}
@ -803,13 +787,10 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
/// Builds the reduced graph rooted at the 'use' directive for an external
/// crate.
fn build_reduced_graph_for_external_crate(&mut self, root: &Rc<Module>) {
csearch::each_top_level_item_of_crate(&self.session.cstore,
root.def_id()
.unwrap()
.krate,
|def_like, name, visibility| {
self.build_reduced_graph_for_external_crate_def(root, def_like, name, visibility)
});
let root_cnum = root.def_id().unwrap().krate;
for child in self.session.cstore.crate_top_level_items(root_cnum) {
self.build_reduced_graph_for_external_crate_def(root, child);
}
}
/// Creates and adds an import directive to the given module.

View File

@ -54,8 +54,7 @@ use self::FallbackChecks::*;
use rustc::front::map as hir_map;
use rustc::session::Session;
use rustc::lint;
use rustc::metadata::csearch;
use rustc::metadata::decoder::{DefLike, DlDef};
use rustc::metadata::util::{CrateStore, DefLike, DlDef};
use rustc::middle::def::*;
use rustc::middle::def_id::DefId;
use rustc::middle::pat_util::pat_bindings_hygienic;
@ -1235,7 +1234,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
if let Some(node_id) = self.ast_map.as_local_node_id(did) {
self.ast_map.expect_item(node_id).name
} else {
csearch::get_trait_name(&self.session.cstore, did)
self.session.cstore.item_name(did)
}
}
@ -3298,7 +3297,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
};
sig.explicit_self.node == hir::SelfStatic
} else {
csearch::is_static_method(&this.session.cstore, did)
this.session.cstore.is_static_method(did)
}
}

View File

@ -22,7 +22,7 @@ use middle::def_id::DefId;
use middle::lang_items::FnOnceTraitLangItem;
use middle::subst::Substs;
use middle::traits::{Obligation, SelectionContext};
use metadata::{csearch, cstore, decoder};
use metadata::util::{self as mdutil, CrateStore, DefLike};
use util::nodemap::{FnvHashSet};
use syntax::ast;
@ -418,31 +418,32 @@ pub fn all_traits<'a>(ccx: &'a CrateCtxt) -> AllTraits<'a> {
fn handle_external_def(traits: &mut AllTraitsVec,
external_mods: &mut FnvHashSet<DefId>,
ccx: &CrateCtxt,
cstore: &cstore::CStore,
dl: decoder::DefLike) {
cstore: &mdutil::CrateStore,
dl: mdutil::DefLike) {
match dl {
decoder::DlDef(def::DefTrait(did)) => {
mdutil::DlDef(def::DefTrait(did)) => {
traits.push(TraitInfo::new(did));
}
decoder::DlDef(def::DefMod(did)) => {
mdutil::DlDef(def::DefMod(did)) => {
if !external_mods.insert(did) {
return;
}
csearch::each_child_of_item(cstore, did, |dl, _, _| {
for child in cstore.item_children(did) {
handle_external_def(traits, external_mods,
ccx, cstore, dl)
})
ccx, cstore, child.def)
}
}
_ => {}
}
}
let cstore = &ccx.tcx.sess.cstore;
cstore.iter_crate_data(|cnum, _| {
csearch::each_top_level_item_of_crate(cstore, cnum, |dl, _, _| {
handle_external_def(&mut traits,
&mut external_mods,
ccx, cstore, dl)
})
let cstore: &mdutil::CrateStore = &ccx.tcx.sess.cstore;
// FIXME: privatize this
ccx.tcx.sess.cstore.iter_crate_data(|cnum, _| {
for child in cstore.crate_top_level_items(cnum) {
handle_external_def(&mut traits, &mut external_mods,
ccx, cstore, child.def)
}
});
*ccx.all_traits.borrow_mut() = Some(traits);