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_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,
cstore: &CStore,
cfg: ast::CrateConfig,
@ -147,23 +154,25 @@ pub fn compile_input(sess: &Session,
"early lint checks",
|| lint::check_ast_crate(sess, &expanded_crate));
let resolve::CrateMap {
def_map,
freevars,
maybe_unused_trait_imports,
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, resolutions) = {
resolve::with_resolver(sess, &defs.borrow(), control.make_glob_map, |mut resolver| {
time(sess.time_passes(), "name resolution", || {
resolve::resolve_crate(&mut resolver, &expanded_crate);
});
let analysis = ty::CrateAnalysis {
export_map: export_map,
(ty::CrateAnalysis {
export_map: resolver.export_map,
access_levels: AccessLevels::default(),
reachable: NodeSet(),
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.
@ -218,13 +227,10 @@ pub fn compile_input(sess: &Session,
phase_3_run_analysis_passes(sess,
hir_map,
analysis,
resolutions,
&arenas,
&id,
analysis,
def_map,
freevars,
trait_map,
maybe_unused_trait_imports,
|tcx, mir_map, analysis, result| {
{
// 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.
pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
hir_map: hir_map::Map<'tcx>,
mut analysis: ty::CrateAnalysis,
resolutions: Resolutions,
arenas: &'tcx ty::CtxtArenas<'tcx>,
name: &str,
mut analysis: ty::CrateAnalysis,
def_map: RefCell<DefMap>,
freevars: FreevarMap,
trait_map: TraitMap,
maybe_unused_trait_imports: NodeSet,
f: F)
-> Result<R, usize>
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",
|| middle::resolve_lifetime::krate(sess,
&hir_map,
&def_map.borrow()))?;
&resolutions.def_map.borrow()))?;
time(time_passes,
"looking for entry point",
@ -840,17 +843,18 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
time(time_passes,
"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 trait_map = resolutions.trait_map;
TyCtxt::create_and_enter(sess,
arenas,
def_map,
resolutions.def_map,
named_region_map,
hir_map,
freevars,
maybe_unused_trait_imports,
resolutions.freevars,
resolutions.maybe_unused_trait_imports,
region_map,
lang_items,
index,

View File

@ -995,11 +995,11 @@ pub struct Resolver<'a> {
// The idents for the primitive types.
primitive_type_table: PrimitiveTypeTable,
def_map: DefMap,
freevars: FreevarMap,
pub def_map: DefMap,
pub freevars: FreevarMap,
freevars_seen: NodeMap<NodeMap<usize>>,
export_map: ExportMap,
trait_map: TraitMap,
pub export_map: ExportMap,
pub trait_map: TraitMap,
// A map from nodes to modules, both normal (`mod`) modules and anonymous modules.
// 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
emit_errors: bool,
make_glob_map: bool,
pub make_glob_map: bool,
// Maps imports to the names of items actually imported (this actually maps
// all imports, but only glob imports are actually interesting).
glob_map: GlobMap,
pub glob_map: GlobMap,
used_imports: HashSet<(NodeId, Namespace)>,
used_crates: HashSet<CrateNum>,
maybe_unused_trait_imports: NodeSet,
pub maybe_unused_trait_imports: NodeSet,
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)]
pub enum MakeGlobMap {
Yes,
@ -3579,11 +3570,7 @@ pub enum MakeGlobMap {
}
/// Entry point to crate resolution.
pub fn resolve_crate<'a>(session: &'a Session,
krate: &'a Crate,
definitions: &'a Definitions,
make_glob_map: MakeGlobMap)
-> CrateMap {
pub fn resolve_crate<'a, 'b>(resolver: &'b mut Resolver<'a>, krate: &'b Crate) {
// Currently, we ignore the name resolution data structures for
// the purposes of dependency tracking. Instead we will run name
// 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
// some point.
let arenas = Resolver::arenas();
let mut resolver = create_resolver(session, definitions, krate, make_glob_map, &arenas);
resolver.build_reduced_graph(krate);
resolve_imports::resolve_imports(resolver);
resolver.resolve_crate(krate);
check_unused::check_crate(&mut resolver, krate);
check_unused::check_crate(resolver, krate);
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.
fn create_resolver<'a>(session: &'a Session,
pub fn with_resolver<'a, T, F>(session: &'a Session,
definitions: &'a Definitions,
krate: &'a Crate,
make_glob_map: MakeGlobMap,
arenas: &'a ResolverArenas<'a>)
-> Resolver<'a> {
let mut resolver = Resolver::new(session, definitions, make_glob_map, arenas);
resolver.build_reduced_graph(krate);
resolve_imports::resolve_imports(&mut resolver);
resolver
f: F) -> T
where F: for<'b> FnOnce(Resolver<'b>) -> T,
{
let arenas = Resolver::arenas();
let resolver = Resolver::new(session, definitions, make_glob_map, &arenas);
f(resolver)
}
__build_diagnostic_array! { librustc_resolve, DIAGNOSTICS }