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 , ty:: TyKind , Context } ;
11+ use rustc_middle:: ty:: { self , 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- } )
29+ }
30+
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 ( & mut 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) ;
40+ stable_mir:: mir:: Body {
41+ blocks : mir
42+ . basic_blocks
43+ . iter ( )
44+ . map ( |block| stable_mir:: mir:: BasicBlock {
45+ terminator : rustc_terminator_to_terminator ( block. terminator ( ) ) ,
46+ statements : block. statements . iter ( ) . map ( rustc_statement_to_statement) . collect ( ) ,
47+ } )
48+ . collect ( ) ,
49+ locals : mir. local_decls . iter ( ) . map ( |decl| self . intern_ty ( decl. ty ) ) . collect ( ) ,
50+ }
51+ }
52+
53+ fn rustc_tables ( & mut self , f : & mut dyn FnMut ( & mut Tables < ' _ > ) ) {
54+ f ( self )
55+ }
56+
57+ fn ty_kind ( & mut self , ty : crate :: stable_mir:: ty:: Ty ) -> TyKind {
58+ self . rustc_ty_to_ty ( self . types [ ty. 0 ] )
59+ }
3660}
3761
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 ( ) )
62+ pub struct Tables < ' tcx > {
63+ pub tcx : TyCtxt < ' tcx > ,
64+ pub def_ids : Vec < DefId > ,
65+ pub types : Vec < Ty < ' tcx > > ,
4166}
4267
43- pub fn entry_fn ( ) -> Option < stable_mir:: CrateItem > {
44- with ( |tcx| Some ( crate_item ( tcx. entry_fn ( ( ) ) ?. 0 ) ) )
68+ impl < ' tcx > Tables < ' tcx > {
69+ fn rustc_ty_to_ty ( & mut self , ty : Ty < ' tcx > ) -> TyKind {
70+ match ty. kind ( ) {
71+ ty:: Bool => TyKind :: Bool ,
72+ ty:: Char => todo ! ( ) ,
73+ ty:: Int ( _) => todo ! ( ) ,
74+ ty:: Uint ( _) => todo ! ( ) ,
75+ ty:: Float ( _) => todo ! ( ) ,
76+ ty:: Adt ( _, _) => todo ! ( ) ,
77+ ty:: Foreign ( _) => todo ! ( ) ,
78+ ty:: Str => todo ! ( ) ,
79+ ty:: Array ( _, _) => todo ! ( ) ,
80+ ty:: Slice ( _) => todo ! ( ) ,
81+ ty:: RawPtr ( _) => todo ! ( ) ,
82+ ty:: Ref ( _, _, _) => todo ! ( ) ,
83+ ty:: FnDef ( _, _) => todo ! ( ) ,
84+ ty:: FnPtr ( _) => todo ! ( ) ,
85+ ty:: Placeholder ( ..) => todo ! ( ) ,
86+ ty:: Dynamic ( _, _, _) => todo ! ( ) ,
87+ ty:: Closure ( _, _) => todo ! ( ) ,
88+ ty:: Generator ( _, _, _) => todo ! ( ) ,
89+ ty:: GeneratorWitness ( _) => todo ! ( ) ,
90+ ty:: GeneratorWitnessMIR ( _, _) => todo ! ( ) ,
91+ ty:: Never => todo ! ( ) ,
92+ ty:: Tuple ( fields) => {
93+ TyKind :: Tuple ( fields. iter ( ) . map ( |ty| self . intern_ty ( ty) ) . collect ( ) )
94+ }
95+ ty:: Alias ( _, _) => todo ! ( ) ,
96+ ty:: Param ( _) => todo ! ( ) ,
97+ ty:: Bound ( _, _) => todo ! ( ) ,
98+ ty:: Infer ( _) => todo ! ( ) ,
99+ ty:: Error ( _) => todo ! ( ) ,
100+ }
101+ }
102+
103+ fn intern_ty ( & mut self , ty : Ty < ' tcx > ) -> stable_mir:: ty:: Ty {
104+ if let Some ( id) = self . types . iter ( ) . position ( |& t| t == ty) {
105+ return stable_mir:: ty:: Ty ( id) ;
106+ }
107+ let id = self . types . len ( ) ;
108+ self . types . push ( ty) ;
109+ stable_mir:: ty:: Ty ( id)
110+ }
45111}
46112
47113/// Build a stable mir crate from a given crate number.
@@ -52,23 +118,6 @@ fn smir_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> stable_mir::Crate {
52118 stable_mir:: Crate { id : crate_num. into ( ) , name : crate_name, is_local }
53119}
54120
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) ;
59- stable_mir:: mir:: Body {
60- blocks : mir
61- . basic_blocks
62- . iter ( )
63- . map ( |block| stable_mir:: mir:: BasicBlock {
64- terminator : rustc_terminator_to_terminator ( block. terminator ( ) ) ,
65- statements : block. statements . iter ( ) . map ( rustc_statement_to_statement) . collect ( ) ,
66- } )
67- . collect ( ) ,
68- }
69- } )
70- }
71-
72121fn rustc_statement_to_statement (
73122 s : & rustc_middle:: mir:: Statement < ' _ > ,
74123) -> stable_mir:: mir:: Statement {
0 commit comments