Refactor the interface that resolve exposes to driver

This commit is contained in:
Jeffrey Seyfried 2016-05-04 06:00:27 +00:00
parent 8428447253
commit 983b4d3925
2 changed files with 55 additions and 83 deletions

View File

@ -61,6 +61,13 @@ use syntax::visit;
use syntax; use syntax;
use syntax_ext; use syntax_ext;
pub struct Resolutions {
pub def_map: RefCell<DefMap>,
pub freevars: FreevarMap,
pub trait_map: TraitMap,
pub maybe_unused_trait_imports: NodeSet,
}
pub fn compile_input(sess: &Session, pub fn compile_input(sess: &Session,
cstore: &CStore, cstore: &CStore,
cfg: ast::CrateConfig, cfg: ast::CrateConfig,
@ -147,23 +154,25 @@ pub fn compile_input(sess: &Session,
"early lint checks", "early lint checks",
|| lint::check_ast_crate(sess, &expanded_crate)); || lint::check_ast_crate(sess, &expanded_crate));
let resolve::CrateMap { let (analysis, resolutions) = {
def_map, resolve::with_resolver(sess, &defs.borrow(), control.make_glob_map, |mut resolver| {
freevars, time(sess.time_passes(), "name resolution", || {
maybe_unused_trait_imports, resolve::resolve_crate(&mut resolver, &expanded_crate);
export_map,
trait_map,
glob_map,
} = time(sess.time_passes(), "name resolution", || {
resolve::resolve_crate(sess, &expanded_crate, &defs.borrow(), control.make_glob_map)
}); });
let analysis = ty::CrateAnalysis { (ty::CrateAnalysis {
export_map: export_map, export_map: resolver.export_map,
access_levels: AccessLevels::default(), access_levels: AccessLevels::default(),
reachable: NodeSet(), reachable: NodeSet(),
name: &id, name: &id,
glob_map: glob_map, glob_map: if resolver.make_glob_map { Some(resolver.glob_map) } else { None },
}, Resolutions {
def_map: RefCell::new(resolver.def_map),
freevars: resolver.freevars,
trait_map: resolver.trait_map,
maybe_unused_trait_imports: resolver.maybe_unused_trait_imports,
})
})
}; };
// Lower ast -> hir. // Lower ast -> hir.
@ -218,13 +227,10 @@ pub fn compile_input(sess: &Session,
phase_3_run_analysis_passes(sess, phase_3_run_analysis_passes(sess,
hir_map, hir_map,
analysis,
resolutions,
&arenas, &arenas,
&id, &id,
analysis,
def_map,
freevars,
trait_map,
maybe_unused_trait_imports,
|tcx, mir_map, analysis, result| { |tcx, mir_map, analysis, result| {
{ {
// Eventually, we will want to track plugins. // Eventually, we will want to track plugins.
@ -785,13 +791,10 @@ pub fn assign_node_ids(sess: &Session, krate: ast::Crate) -> ast::Crate {
/// structures carrying the results of the analysis. /// structures carrying the results of the analysis.
pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session, pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
hir_map: hir_map::Map<'tcx>, hir_map: hir_map::Map<'tcx>,
mut analysis: ty::CrateAnalysis,
resolutions: Resolutions,
arenas: &'tcx ty::CtxtArenas<'tcx>, arenas: &'tcx ty::CtxtArenas<'tcx>,
name: &str, name: &str,
mut analysis: ty::CrateAnalysis,
def_map: RefCell<DefMap>,
freevars: FreevarMap,
trait_map: TraitMap,
maybe_unused_trait_imports: NodeSet,
f: F) f: F)
-> Result<R, usize> -> Result<R, usize>
where F: FnOnce(&TyCtxt<'tcx>, Option<MirMap<'tcx>>, ty::CrateAnalysis, CompileResult) -> R where F: FnOnce(&TyCtxt<'tcx>, Option<MirMap<'tcx>>, ty::CrateAnalysis, CompileResult) -> R
@ -820,7 +823,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
"lifetime resolution", "lifetime resolution",
|| middle::resolve_lifetime::krate(sess, || middle::resolve_lifetime::krate(sess,
&hir_map, &hir_map,
&def_map.borrow()))?; &resolutions.def_map.borrow()))?;
time(time_passes, time(time_passes,
"looking for entry point", "looking for entry point",
@ -840,17 +843,18 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
time(time_passes, time(time_passes,
"static item recursion checking", "static item recursion checking",
|| static_recursion::check_crate(sess, &def_map.borrow(), &hir_map))?; || static_recursion::check_crate(sess, &resolutions.def_map.borrow(), &hir_map))?;
let index = stability::Index::new(&hir_map); let index = stability::Index::new(&hir_map);
let trait_map = resolutions.trait_map;
TyCtxt::create_and_enter(sess, TyCtxt::create_and_enter(sess,
arenas, arenas,
def_map, resolutions.def_map,
named_region_map, named_region_map,
hir_map, hir_map,
freevars, resolutions.freevars,
maybe_unused_trait_imports, resolutions.maybe_unused_trait_imports,
region_map, region_map,
lang_items, lang_items,
index, index,

View File

@ -995,11 +995,11 @@ pub struct Resolver<'a> {
// The idents for the primitive types. // The idents for the primitive types.
primitive_type_table: PrimitiveTypeTable, primitive_type_table: PrimitiveTypeTable,
def_map: DefMap, pub def_map: DefMap,
freevars: FreevarMap, pub freevars: FreevarMap,
freevars_seen: NodeMap<NodeMap<usize>>, freevars_seen: NodeMap<NodeMap<usize>>,
export_map: ExportMap, pub export_map: ExportMap,
trait_map: TraitMap, pub trait_map: TraitMap,
// A map from nodes to modules, both normal (`mod`) modules and anonymous modules. // A map from nodes to modules, both normal (`mod`) modules and anonymous modules.
// Anonymous modules are pseudo-modules that are implicitly created around items // Anonymous modules are pseudo-modules that are implicitly created around items
@ -1022,14 +1022,14 @@ pub struct Resolver<'a> {
// so as to avoid printing duplicate errors // so as to avoid printing duplicate errors
emit_errors: bool, emit_errors: bool,
make_glob_map: bool, pub make_glob_map: bool,
// Maps imports to the names of items actually imported (this actually maps // Maps imports to the names of items actually imported (this actually maps
// all imports, but only glob imports are actually interesting). // all imports, but only glob imports are actually interesting).
glob_map: GlobMap, pub glob_map: GlobMap,
used_imports: HashSet<(NodeId, Namespace)>, used_imports: HashSet<(NodeId, Namespace)>,
used_crates: HashSet<CrateNum>, used_crates: HashSet<CrateNum>,
maybe_unused_trait_imports: NodeSet, pub maybe_unused_trait_imports: NodeSet,
privacy_errors: Vec<PrivacyError<'a>>, privacy_errors: Vec<PrivacyError<'a>>,
@ -3563,15 +3563,6 @@ fn err_path_resolution() -> PathResolution {
} }
pub struct CrateMap {
pub def_map: RefCell<DefMap>,
pub freevars: FreevarMap,
pub maybe_unused_trait_imports: NodeSet,
pub export_map: ExportMap,
pub trait_map: TraitMap,
pub glob_map: Option<GlobMap>,
}
#[derive(PartialEq,Copy, Clone)] #[derive(PartialEq,Copy, Clone)]
pub enum MakeGlobMap { pub enum MakeGlobMap {
Yes, Yes,
@ -3579,11 +3570,7 @@ pub enum MakeGlobMap {
} }
/// Entry point to crate resolution. /// Entry point to crate resolution.
pub fn resolve_crate<'a>(session: &'a Session, pub fn resolve_crate<'a, 'b>(resolver: &'b mut Resolver<'a>, krate: &'b Crate) {
krate: &'a Crate,
definitions: &'a Definitions,
make_glob_map: MakeGlobMap)
-> CrateMap {
// Currently, we ignore the name resolution data structures for // Currently, we ignore the name resolution data structures for
// the purposes of dependency tracking. Instead we will run name // the purposes of dependency tracking. Instead we will run name
// resolution and include its output in the hash of each item, // resolution and include its output in the hash of each item,
@ -3592,42 +3579,23 @@ pub fn resolve_crate<'a>(session: &'a Session,
// resolution on those contents. Hopefully we'll push this back at // resolution on those contents. Hopefully we'll push this back at
// some point. // some point.
let arenas = Resolver::arenas(); resolver.build_reduced_graph(krate);
let mut resolver = create_resolver(session, definitions, krate, make_glob_map, &arenas); resolve_imports::resolve_imports(resolver);
resolver.resolve_crate(krate); resolver.resolve_crate(krate);
check_unused::check_crate(&mut resolver, krate); check_unused::check_crate(resolver, krate);
resolver.report_privacy_errors(); resolver.report_privacy_errors();
CrateMap {
def_map: RefCell::new(resolver.def_map),
freevars: resolver.freevars,
maybe_unused_trait_imports: resolver.maybe_unused_trait_imports,
export_map: resolver.export_map,
trait_map: resolver.trait_map,
glob_map: if resolver.make_glob_map {
Some(resolver.glob_map)
} else {
None
},
}
} }
/// Builds a name resolution walker. pub fn with_resolver<'a, T, F>(session: &'a Session,
fn create_resolver<'a>(session: &'a Session,
definitions: &'a Definitions, definitions: &'a Definitions,
krate: &'a Crate,
make_glob_map: MakeGlobMap, make_glob_map: MakeGlobMap,
arenas: &'a ResolverArenas<'a>) f: F) -> T
-> Resolver<'a> { where F: for<'b> FnOnce(Resolver<'b>) -> T,
let mut resolver = Resolver::new(session, definitions, make_glob_map, arenas); {
let arenas = Resolver::arenas();
resolver.build_reduced_graph(krate); let resolver = Resolver::new(session, definitions, make_glob_map, &arenas);
f(resolver)
resolve_imports::resolve_imports(&mut resolver);
resolver
} }
__build_diagnostic_array! { librustc_resolve, DIAGNOSTICS } __build_diagnostic_array! { librustc_resolve, DIAGNOSTICS }