77//!
88//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
99
10- use crate :: {
11- rustc_internal:: { crate_item, item_def_id} ,
12- stable_mir:: { self } ,
13- } ;
14- use rustc_middle:: ty:: { tls:: with, TyCtxt } ;
15- use rustc_span:: def_id:: { CrateNum , LOCAL_CRATE } ;
10+ use crate :: stable_mir:: { self , Context } ;
11+ use rustc_middle:: ty:: TyCtxt ;
12+ use rustc_span:: def_id:: { CrateNum , DefId , LOCAL_CRATE } ;
1613use tracing:: debug;
1714
18- /// Get information about the local crate.
19- pub fn local_crate ( ) -> stable_mir:: Crate {
20- with ( |tcx| smir_crate ( tcx, LOCAL_CRATE ) )
21- }
15+ impl < ' tcx > Context for Tables < ' tcx > {
16+ fn local_crate ( & self ) -> stable_mir:: Crate {
17+ smir_crate ( self . tcx , LOCAL_CRATE )
18+ }
2219
23- /// Retrieve a list of all external crates.
24- pub fn external_crates ( ) -> Vec < stable_mir:: Crate > {
25- with ( |tcx| tcx. crates ( ( ) ) . iter ( ) . map ( |crate_num| smir_crate ( tcx, * crate_num) ) . collect ( ) )
26- }
20+ fn external_crates ( & self ) -> Vec < stable_mir:: Crate > {
21+ self . tcx . crates ( ( ) ) . iter ( ) . map ( |crate_num| smir_crate ( self . tcx , * crate_num) ) . collect ( )
22+ }
2723
28- /// Find a crate with the given name.
29- pub fn find_crate ( name : & str ) -> Option < stable_mir:: Crate > {
30- with ( |tcx| {
31- [ LOCAL_CRATE ] . iter ( ) . chain ( tcx. crates ( ( ) ) . iter ( ) ) . find_map ( |crate_num| {
32- let crate_name = tcx. crate_name ( * crate_num) . to_string ( ) ;
33- ( name == crate_name) . then ( || smir_crate ( tcx, * crate_num) )
24+ fn find_crate ( & self , name : & str ) -> Option < stable_mir:: Crate > {
25+ [ LOCAL_CRATE ] . iter ( ) . chain ( self . tcx . crates ( ( ) ) . iter ( ) ) . find_map ( |crate_num| {
26+ let crate_name = self . tcx . crate_name ( * crate_num) . to_string ( ) ;
27+ ( name == crate_name) . then ( || smir_crate ( self . tcx , * crate_num) )
3428 } )
35- } )
36- }
37-
38- /// Retrieve all items of the local crate that have a MIR associated with them.
39- pub fn all_local_items ( ) -> stable_mir:: CrateItems {
40- with ( |tcx| tcx. mir_keys ( ( ) ) . iter ( ) . map ( |item| crate_item ( item. to_def_id ( ) ) ) . collect ( ) )
41- }
42-
43- pub fn entry_fn ( ) -> Option < stable_mir:: CrateItem > {
44- with ( |tcx| Some ( crate_item ( tcx. entry_fn ( ( ) ) ?. 0 ) ) )
45- }
46-
47- /// Build a stable mir crate from a given crate number.
48- fn smir_crate ( tcx : TyCtxt < ' _ > , crate_num : CrateNum ) -> stable_mir:: Crate {
49- let crate_name = tcx. crate_name ( crate_num) . to_string ( ) ;
50- let is_local = crate_num == LOCAL_CRATE ;
51- debug ! ( ?crate_name, ?crate_num, "smir_crate" ) ;
52- stable_mir:: Crate { id : crate_num. into ( ) , name : crate_name, is_local }
53- }
29+ }
5430
55- pub fn mir_body ( item : & stable_mir:: CrateItem ) -> stable_mir:: mir:: Body {
56- with ( |tcx| {
57- let def_id = item_def_id ( item) ;
58- let mir = tcx. optimized_mir ( def_id) ;
31+ fn all_local_items ( & mut self ) -> stable_mir:: CrateItems {
32+ self . tcx . mir_keys ( ( ) ) . iter ( ) . map ( |item| self . crate_item ( item. to_def_id ( ) ) ) . collect ( )
33+ }
34+ fn entry_fn ( & mut self ) -> Option < stable_mir:: CrateItem > {
35+ Some ( self . crate_item ( self . tcx . entry_fn ( ( ) ) ?. 0 ) )
36+ }
37+ fn mir_body ( & self , item : & stable_mir:: CrateItem ) -> stable_mir:: mir:: Body {
38+ let def_id = self . item_def_id ( item) ;
39+ let mir = self . tcx . optimized_mir ( def_id) ;
5940 stable_mir:: mir:: Body {
6041 blocks : mir
6142 . basic_blocks
@@ -66,7 +47,24 @@ pub fn mir_body(item: &stable_mir::CrateItem) -> stable_mir::mir::Body {
6647 } )
6748 . collect ( ) ,
6849 }
69- } )
50+ }
51+
52+ fn rustc_tables ( & mut self , f : & mut dyn FnMut ( & mut Tables < ' _ > ) ) {
53+ f ( self )
54+ }
55+ }
56+
57+ pub struct Tables < ' tcx > {
58+ pub tcx : TyCtxt < ' tcx > ,
59+ pub def_ids : Vec < DefId > ,
60+ }
61+
62+ /// Build a stable mir crate from a given crate number.
63+ fn smir_crate ( tcx : TyCtxt < ' _ > , crate_num : CrateNum ) -> stable_mir:: Crate {
64+ let crate_name = tcx. crate_name ( crate_num) . to_string ( ) ;
65+ let is_local = crate_num == LOCAL_CRATE ;
66+ debug ! ( ?crate_name, ?crate_num, "smir_crate" ) ;
67+ stable_mir:: Crate { id : crate_num. into ( ) , name : crate_name, is_local }
7068}
7169
7270fn rustc_statement_to_statement (
0 commit comments