1010
1111use rustc:: dep_graph:: DepGraph ;
1212use rustc:: hir;
13- use rustc:: hir:: map as hir_map;
13+ use rustc:: hir:: { map as hir_map, FreevarMap , TraitMap } ;
14+ use rustc:: hir:: def:: DefMap ;
1415use rustc_mir as mir;
1516use rustc:: mir:: mir_map:: MirMap ;
1617use rustc:: session:: { Session , CompileResult , compile_result_from_err_count} ;
@@ -60,6 +61,14 @@ use syntax::visit;
6061use syntax;
6162use syntax_ext;
6263
64+ #[ derive( Clone ) ]
65+ pub struct Resolutions {
66+ pub def_map : RefCell < DefMap > ,
67+ pub freevars : FreevarMap ,
68+ pub trait_map : TraitMap ,
69+ pub maybe_unused_trait_imports : NodeSet ,
70+ }
71+
6372pub fn compile_input ( sess : & Session ,
6473 cstore : & CStore ,
6574 cfg : ast:: CrateConfig ,
@@ -139,15 +148,17 @@ pub fn compile_input(sess: &Session,
139148
140149 time ( sess. time_passes ( ) ,
141150 "external crate/lib resolution" ,
142- || LocalCrateReader :: new ( sess, & cstore, & defs, & expanded_crate, & id)
151+ || LocalCrateReader :: new ( sess, & cstore, defs, & expanded_crate, & id)
143152 . read_crates ( & dep_graph) ) ;
144153
145- // Lower ast -> hir.
146- let lcx = LoweringContext :: new ( sess, Some ( & expanded_crate) , defs) ;
147- let hir_forest = & mut time ( sess. time_passes ( ) ,
148- "lowering ast -> hir" ,
149- || hir_map:: Forest :: new ( lower_crate ( & lcx, & expanded_crate) ,
150- dep_graph) ) ;
154+ time ( sess. time_passes ( ) ,
155+ "early lint checks" ,
156+ || lint:: check_ast_crate ( sess, & expanded_crate) ) ;
157+
158+ let ( analysis, resolutions, mut hir_forest) = {
159+ let defs = & mut * defs. borrow_mut ( ) ;
160+ lower_and_resolve ( sess, & id, defs, & expanded_crate, dep_graph, control. make_glob_map )
161+ } ;
151162
152163 // Discard MTWT tables that aren't required past lowering to HIR.
153164 if !keep_mtwt_tables ( sess) {
@@ -157,6 +168,7 @@ pub fn compile_input(sess: &Session,
157168 let arenas = ty:: CtxtArenas :: new ( ) ;
158169
159170 // Construct the HIR map
171+ let hir_forest = & mut hir_forest;
160172 let hir_map = time ( sess. time_passes ( ) ,
161173 "indexing hir" ,
162174 move || hir_map:: map_crate ( hir_forest, defs) ) ;
@@ -175,6 +187,8 @@ pub fn compile_input(sess: &Session,
175187 & arenas,
176188 & cstore,
177189 & hir_map,
190+ & analysis,
191+ & resolutions,
178192 & expanded_crate,
179193 & hir_map. krate( ) ,
180194 & id) ,
@@ -185,10 +199,6 @@ pub fn compile_input(sess: &Session,
185199 hir:: check_attr:: check_crate ( sess, & expanded_crate) ;
186200 } ) ;
187201
188- time ( sess. time_passes ( ) ,
189- "early lint checks" ,
190- || lint:: check_ast_crate ( sess, & expanded_crate) ) ;
191-
192202 let opt_crate = if keep_ast ( sess) {
193203 Some ( & expanded_crate)
194204 } else {
@@ -198,9 +208,10 @@ pub fn compile_input(sess: &Session,
198208
199209 phase_3_run_analysis_passes ( sess,
200210 hir_map,
211+ analysis,
212+ resolutions,
201213 & arenas,
202214 & id,
203- control. make_glob_map ,
204215 |tcx, mir_map, analysis, result| {
205216 {
206217 // Eventually, we will want to track plugins.
@@ -353,6 +364,7 @@ pub struct CompileState<'a, 'b, 'ast: 'a, 'tcx: 'b> where 'ast: 'tcx {
353364 pub expanded_crate : Option < & ' a ast:: Crate > ,
354365 pub hir_crate : Option < & ' a hir:: Crate > ,
355366 pub ast_map : Option < & ' a hir_map:: Map < ' ast > > ,
367+ pub resolutions : Option < & ' a Resolutions > ,
356368 pub mir_map : Option < & ' b MirMap < ' tcx > > ,
357369 pub analysis : Option < & ' a ty:: CrateAnalysis < ' a > > ,
358370 pub tcx : Option < & ' b TyCtxt < ' tcx > > ,
@@ -377,6 +389,7 @@ impl<'a, 'b, 'ast, 'tcx> CompileState<'a, 'b, 'ast, 'tcx> {
377389 expanded_crate : None ,
378390 hir_crate : None ,
379391 ast_map : None ,
392+ resolutions : None ,
380393 analysis : None ,
381394 mir_map : None ,
382395 tcx : None ,
@@ -423,6 +436,8 @@ impl<'a, 'b, 'ast, 'tcx> CompileState<'a, 'b, 'ast, 'tcx> {
423436 arenas : & ' ast ty:: CtxtArenas < ' ast > ,
424437 cstore : & ' a CStore ,
425438 hir_map : & ' a hir_map:: Map < ' ast > ,
439+ analysis : & ' a ty:: CrateAnalysis ,
440+ resolutions : & ' a Resolutions ,
426441 krate : & ' a ast:: Crate ,
427442 hir_crate : & ' a hir:: Crate ,
428443 crate_name : & ' a str )
@@ -432,6 +447,8 @@ impl<'a, 'b, 'ast, 'tcx> CompileState<'a, 'b, 'ast, 'tcx> {
432447 arenas : Some ( arenas) ,
433448 cstore : Some ( cstore) ,
434449 ast_map : Some ( hir_map) ,
450+ analysis : Some ( analysis) ,
451+ resolutions : Some ( resolutions) ,
435452 expanded_crate : Some ( krate) ,
436453 hir_crate : Some ( hir_crate) ,
437454 out_file : out_file. as_ref ( ) . map ( |s| & * * s) ,
@@ -756,14 +773,48 @@ pub fn assign_node_ids(sess: &Session, krate: ast::Crate) -> ast::Crate {
756773 krate
757774}
758775
776+ pub fn lower_and_resolve < ' a > ( sess : & Session ,
777+ id : & ' a str ,
778+ defs : & mut hir_map:: Definitions ,
779+ krate : & ast:: Crate ,
780+ dep_graph : DepGraph ,
781+ make_glob_map : resolve:: MakeGlobMap )
782+ -> ( ty:: CrateAnalysis < ' a > , Resolutions , hir_map:: Forest ) {
783+ resolve:: with_resolver ( sess, defs, make_glob_map, |mut resolver| {
784+ time ( sess. time_passes ( ) , "name resolution" , || {
785+ resolve:: resolve_crate ( & mut resolver, krate) ;
786+ } ) ;
787+
788+ // Lower ast -> hir.
789+ let hir_forest = time ( sess. time_passes ( ) , "lowering ast -> hir" , || {
790+ let lcx = LoweringContext :: new ( sess, Some ( krate) , & mut resolver) ;
791+ hir_map:: Forest :: new ( lower_crate ( & lcx, krate) , dep_graph)
792+ } ) ;
793+
794+ ( ty:: CrateAnalysis {
795+ export_map : resolver. export_map ,
796+ access_levels : AccessLevels :: default ( ) ,
797+ reachable : NodeSet ( ) ,
798+ name : & id,
799+ glob_map : if resolver. make_glob_map { Some ( resolver. glob_map ) } else { None } ,
800+ } , Resolutions {
801+ def_map : RefCell :: new ( resolver. def_map ) ,
802+ freevars : resolver. freevars ,
803+ trait_map : resolver. trait_map ,
804+ maybe_unused_trait_imports : resolver. maybe_unused_trait_imports ,
805+ } , hir_forest)
806+ } )
807+ }
808+
759809/// Run the resolution, typechecking, region checking and other
760810/// miscellaneous analysis passes on the crate. Return various
761811/// structures carrying the results of the analysis.
762812pub fn phase_3_run_analysis_passes < ' tcx , F , R > ( sess : & ' tcx Session ,
763813 hir_map : hir_map:: Map < ' tcx > ,
814+ mut analysis : ty:: CrateAnalysis ,
815+ resolutions : Resolutions ,
764816 arenas : & ' tcx ty:: CtxtArenas < ' tcx > ,
765817 name : & str ,
766- make_glob_map : resolve:: MakeGlobMap ,
767818 f : F )
768819 -> Result < R , usize >
769820 where F : FnOnce ( & TyCtxt < ' tcx > , Option < MirMap < ' tcx > > , ty:: CrateAnalysis , CompileResult ) -> R
@@ -788,30 +839,11 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
788839 } )
789840 } ) ?;
790841
791- let resolve:: CrateMap {
792- def_map,
793- freevars,
794- maybe_unused_trait_imports,
795- export_map,
796- trait_map,
797- glob_map,
798- } = time ( sess. time_passes ( ) ,
799- "name resolution" ,
800- || resolve:: resolve_crate ( sess, & hir_map, make_glob_map) ) ;
801-
802- let mut analysis = ty:: CrateAnalysis {
803- export_map : export_map,
804- access_levels : AccessLevels :: default ( ) ,
805- reachable : NodeSet ( ) ,
806- name : name,
807- glob_map : glob_map,
808- } ;
809-
810842 let named_region_map = time ( time_passes,
811843 "lifetime resolution" ,
812844 || middle:: resolve_lifetime:: krate ( sess,
813845 & hir_map,
814- & def_map. borrow ( ) ) ) ?;
846+ & resolutions . def_map . borrow ( ) ) ) ?;
815847
816848 time ( time_passes,
817849 "looking for entry point" ,
@@ -831,17 +863,18 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
831863
832864 time ( time_passes,
833865 "static item recursion checking" ,
834- || static_recursion:: check_crate ( sess, & def_map. borrow ( ) , & hir_map) ) ?;
866+ || static_recursion:: check_crate ( sess, & resolutions . def_map . borrow ( ) , & hir_map) ) ?;
835867
836868 let index = stability:: Index :: new ( & hir_map) ;
837869
870+ let trait_map = resolutions. trait_map ;
838871 TyCtxt :: create_and_enter ( sess,
839872 arenas,
840- def_map,
873+ resolutions . def_map ,
841874 named_region_map,
842875 hir_map,
843- freevars,
844- maybe_unused_trait_imports,
876+ resolutions . freevars ,
877+ resolutions . maybe_unused_trait_imports ,
845878 region_map,
846879 lang_items,
847880 index,
0 commit comments