7
7
//!
8
8
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
9
9
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 } ;
12
12
use rustc_span:: def_id:: { CrateNum , DefId , LOCAL_CRATE } ;
13
13
use tracing:: debug;
14
14
@@ -34,7 +34,7 @@ impl<'tcx> Context for Tables<'tcx> {
34
34
fn entry_fn ( & mut self ) -> Option < stable_mir:: CrateItem > {
35
35
Some ( self . crate_item ( self . tcx . entry_fn ( ( ) ) ?. 0 ) )
36
36
}
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 {
38
38
let def_id = self . item_def_id ( item) ;
39
39
let mir = self . tcx . optimized_mir ( def_id) ;
40
40
stable_mir:: mir:: Body {
@@ -46,17 +46,68 @@ impl<'tcx> Context for Tables<'tcx> {
46
46
statements : block. statements . iter ( ) . map ( rustc_statement_to_statement) . collect ( ) ,
47
47
} )
48
48
. collect ( ) ,
49
+ locals : mir. local_decls . iter ( ) . map ( |decl| self . intern_ty ( decl. ty ) ) . collect ( ) ,
49
50
}
50
51
}
51
52
52
53
fn rustc_tables ( & mut self , f : & mut dyn FnMut ( & mut Tables < ' _ > ) ) {
53
54
f ( self )
54
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
+ }
55
60
}
56
61
57
62
pub struct Tables < ' tcx > {
58
63
pub tcx : TyCtxt < ' tcx > ,
59
64
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
+ }
60
111
}
61
112
62
113
/// Build a stable mir crate from a given crate number.
0 commit comments