10
10
11
11
use rustc:: dep_graph:: DepGraph ;
12
12
use 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 ;
14
15
use rustc_mir as mir;
15
16
use rustc:: mir:: mir_map:: MirMap ;
16
17
use rustc:: session:: { Session , CompileResult , compile_result_from_err_count} ;
@@ -60,6 +61,14 @@ use syntax::visit;
60
61
use syntax;
61
62
use syntax_ext;
62
63
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
+
63
72
pub fn compile_input ( sess : & Session ,
64
73
cstore : & CStore ,
65
74
cfg : ast:: CrateConfig ,
@@ -139,15 +148,17 @@ pub fn compile_input(sess: &Session,
139
148
140
149
time ( sess. time_passes ( ) ,
141
150
"external crate/lib resolution" ,
142
- || LocalCrateReader :: new ( sess, & cstore, & defs, & expanded_crate, & id)
151
+ || LocalCrateReader :: new ( sess, & cstore, defs, & expanded_crate, & id)
143
152
. read_crates ( & dep_graph) ) ;
144
153
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
+ } ;
151
162
152
163
// Discard MTWT tables that aren't required past lowering to HIR.
153
164
if !keep_mtwt_tables ( sess) {
@@ -157,6 +168,7 @@ pub fn compile_input(sess: &Session,
157
168
let arenas = ty:: CtxtArenas :: new ( ) ;
158
169
159
170
// Construct the HIR map
171
+ let hir_forest = & mut hir_forest;
160
172
let hir_map = time ( sess. time_passes ( ) ,
161
173
"indexing hir" ,
162
174
move || hir_map:: map_crate ( hir_forest, defs) ) ;
@@ -175,6 +187,8 @@ pub fn compile_input(sess: &Session,
175
187
& arenas,
176
188
& cstore,
177
189
& hir_map,
190
+ & analysis,
191
+ & resolutions,
178
192
& expanded_crate,
179
193
& hir_map. krate( ) ,
180
194
& id) ,
@@ -185,10 +199,6 @@ pub fn compile_input(sess: &Session,
185
199
hir:: check_attr:: check_crate ( sess, & expanded_crate) ;
186
200
} ) ;
187
201
188
- time ( sess. time_passes ( ) ,
189
- "early lint checks" ,
190
- || lint:: check_ast_crate ( sess, & expanded_crate) ) ;
191
-
192
202
let opt_crate = if keep_ast ( sess) {
193
203
Some ( & expanded_crate)
194
204
} else {
@@ -198,9 +208,10 @@ pub fn compile_input(sess: &Session,
198
208
199
209
phase_3_run_analysis_passes ( sess,
200
210
hir_map,
211
+ analysis,
212
+ resolutions,
201
213
& arenas,
202
214
& id,
203
- control. make_glob_map ,
204
215
|tcx, mir_map, analysis, result| {
205
216
{
206
217
// Eventually, we will want to track plugins.
@@ -353,6 +364,7 @@ pub struct CompileState<'a, 'b, 'ast: 'a, 'tcx: 'b> where 'ast: 'tcx {
353
364
pub expanded_crate : Option < & ' a ast:: Crate > ,
354
365
pub hir_crate : Option < & ' a hir:: Crate > ,
355
366
pub ast_map : Option < & ' a hir_map:: Map < ' ast > > ,
367
+ pub resolutions : Option < & ' a Resolutions > ,
356
368
pub mir_map : Option < & ' b MirMap < ' tcx > > ,
357
369
pub analysis : Option < & ' a ty:: CrateAnalysis < ' a > > ,
358
370
pub tcx : Option < & ' b TyCtxt < ' tcx > > ,
@@ -377,6 +389,7 @@ impl<'a, 'b, 'ast, 'tcx> CompileState<'a, 'b, 'ast, 'tcx> {
377
389
expanded_crate : None ,
378
390
hir_crate : None ,
379
391
ast_map : None ,
392
+ resolutions : None ,
380
393
analysis : None ,
381
394
mir_map : None ,
382
395
tcx : None ,
@@ -423,6 +436,8 @@ impl<'a, 'b, 'ast, 'tcx> CompileState<'a, 'b, 'ast, 'tcx> {
423
436
arenas : & ' ast ty:: CtxtArenas < ' ast > ,
424
437
cstore : & ' a CStore ,
425
438
hir_map : & ' a hir_map:: Map < ' ast > ,
439
+ analysis : & ' a ty:: CrateAnalysis ,
440
+ resolutions : & ' a Resolutions ,
426
441
krate : & ' a ast:: Crate ,
427
442
hir_crate : & ' a hir:: Crate ,
428
443
crate_name : & ' a str )
@@ -432,6 +447,8 @@ impl<'a, 'b, 'ast, 'tcx> CompileState<'a, 'b, 'ast, 'tcx> {
432
447
arenas : Some ( arenas) ,
433
448
cstore : Some ( cstore) ,
434
449
ast_map : Some ( hir_map) ,
450
+ analysis : Some ( analysis) ,
451
+ resolutions : Some ( resolutions) ,
435
452
expanded_crate : Some ( krate) ,
436
453
hir_crate : Some ( hir_crate) ,
437
454
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 {
756
773
krate
757
774
}
758
775
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
+
759
809
/// Run the resolution, typechecking, region checking and other
760
810
/// miscellaneous analysis passes on the crate. Return various
761
811
/// structures carrying the results of the analysis.
762
812
pub fn phase_3_run_analysis_passes < ' tcx , F , R > ( sess : & ' tcx Session ,
763
813
hir_map : hir_map:: Map < ' tcx > ,
814
+ mut analysis : ty:: CrateAnalysis ,
815
+ resolutions : Resolutions ,
764
816
arenas : & ' tcx ty:: CtxtArenas < ' tcx > ,
765
817
name : & str ,
766
- make_glob_map : resolve:: MakeGlobMap ,
767
818
f : F )
768
819
-> Result < R , usize >
769
820
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,
788
839
} )
789
840
} ) ?;
790
841
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
-
810
842
let named_region_map = time ( time_passes,
811
843
"lifetime resolution" ,
812
844
|| middle:: resolve_lifetime:: krate ( sess,
813
845
& hir_map,
814
- & def_map. borrow ( ) ) ) ?;
846
+ & resolutions . def_map . borrow ( ) ) ) ?;
815
847
816
848
time ( time_passes,
817
849
"looking for entry point" ,
@@ -831,17 +863,18 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
831
863
832
864
time ( time_passes,
833
865
"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) ) ?;
835
867
836
868
let index = stability:: Index :: new ( & hir_map) ;
837
869
870
+ let trait_map = resolutions. trait_map ;
838
871
TyCtxt :: create_and_enter ( sess,
839
872
arenas,
840
- def_map,
873
+ resolutions . def_map ,
841
874
named_region_map,
842
875
hir_map,
843
- freevars,
844
- maybe_unused_trait_imports,
876
+ resolutions . freevars ,
877
+ resolutions . maybe_unused_trait_imports ,
845
878
region_map,
846
879
lang_items,
847
880
index,
0 commit comments