77//!
88//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
99
10- use crate :: stable_mir:: { self , Context } ;
11- use rustc_middle:: ty:: TyCtxt ;
10+ use crate :: stable_mir:: { self , ty :: TyKind , Context } ;
11+ use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
1212use rustc_span:: def_id:: { CrateNum , DefId , LOCAL_CRATE } ;
1313use tracing:: debug;
1414
@@ -34,7 +34,7 @@ impl<'tcx> Context for Tables<'tcx> {
3434 fn entry_fn ( & mut self ) -> Option < stable_mir:: CrateItem > {
3535 Some ( self . crate_item ( self . tcx . entry_fn ( ( ) ) ?. 0 ) )
3636 }
37- fn mir_body ( & self , item : & stable_mir:: CrateItem ) -> stable_mir:: mir:: Body {
37+ fn mir_body ( & mut self , item : & stable_mir:: CrateItem ) -> stable_mir:: mir:: Body {
3838 let def_id = self . item_def_id ( item) ;
3939 let mir = self . tcx . optimized_mir ( def_id) ;
4040 stable_mir:: mir:: Body {
@@ -46,17 +46,68 @@ impl<'tcx> Context for Tables<'tcx> {
4646 statements : block. statements . iter ( ) . map ( rustc_statement_to_statement) . collect ( ) ,
4747 } )
4848 . collect ( ) ,
49+ locals : mir. local_decls . iter ( ) . map ( |decl| self . intern_ty ( decl. ty ) ) . collect ( ) ,
4950 }
5051 }
5152
5253 fn rustc_tables ( & mut self , f : & mut dyn FnMut ( & mut Tables < ' _ > ) ) {
5354 f ( self )
5455 }
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+ }
5560}
5661
5762pub struct Tables < ' tcx > {
5863 pub tcx : TyCtxt < ' tcx > ,
5964 pub def_ids : Vec < DefId > ,
65+ pub types : Vec < Ty < ' tcx > > ,
66+ }
67+
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+ }
60111}
61112
62113/// Build a stable mir crate from a given crate number.
0 commit comments