@@ -13,10 +13,12 @@ use crate::rustc_smir::stable_mir::ty::{BoundRegion, EarlyBoundRegion, Region};
13
13
use rustc_hir as hir;
14
14
use rustc_middle:: mir;
15
15
use rustc_middle:: mir:: interpret:: { alloc_range, AllocId } ;
16
- use rustc_middle:: ty:: { self , Ty , TyCtxt , Variance } ;
16
+ use rustc_middle:: mir:: mono:: MonoItem ;
17
+ use rustc_middle:: ty:: { self , Instance , ParamEnv , Ty , TyCtxt , Variance } ;
17
18
use rustc_span:: def_id:: { CrateNum , DefId , LOCAL_CRATE } ;
18
19
use rustc_target:: abi:: FieldIdx ;
19
- use stable_mir:: mir:: { CopyNonOverlapping , Statement , UserTypeProjection , VariantIdx } ;
20
+ use stable_mir:: mir:: mono:: InstanceDef ;
21
+ use stable_mir:: mir:: { Body , CopyNonOverlapping , Statement , UserTypeProjection , VariantIdx } ;
20
22
use stable_mir:: ty:: {
21
23
FloatTy , GenericParamDef , IntTy , LineInfo , Movability , RigidTy , Span , TyKind , UintTy ,
22
24
} ;
@@ -119,29 +121,7 @@ impl<'tcx> Context for Tables<'tcx> {
119
121
120
122
fn mir_body ( & mut self , item : stable_mir:: DefId ) -> stable_mir:: mir:: Body {
121
123
let def_id = self [ item] ;
122
- let mir = self . tcx . instance_mir ( ty:: InstanceDef :: Item ( def_id) ) ;
123
- stable_mir:: mir:: Body {
124
- blocks : mir
125
- . basic_blocks
126
- . iter ( )
127
- . map ( |block| stable_mir:: mir:: BasicBlock {
128
- terminator : block. terminator ( ) . stable ( self ) ,
129
- statements : block
130
- . statements
131
- . iter ( )
132
- . map ( |statement| statement. stable ( self ) )
133
- . collect ( ) ,
134
- } )
135
- . collect ( ) ,
136
- locals : mir
137
- . local_decls
138
- . iter ( )
139
- . map ( |decl| stable_mir:: mir:: LocalDecl {
140
- ty : self . intern_ty ( decl. ty ) ,
141
- span : decl. source_info . span . stable ( self ) ,
142
- } )
143
- . collect ( ) ,
144
- }
124
+ self . tcx . instance_mir ( ty:: InstanceDef :: Item ( def_id) ) . stable ( self )
145
125
}
146
126
147
127
fn ty_kind ( & mut self , ty : stable_mir:: ty:: Ty ) -> TyKind {
@@ -190,6 +170,34 @@ impl<'tcx> Context for Tables<'tcx> {
190
170
. collect ( ) ,
191
171
}
192
172
}
173
+
174
+ fn instance_body ( & mut self , _def : InstanceDef ) -> Body {
175
+ todo ! ( "Monomorphize the body" )
176
+ }
177
+
178
+ fn instance_ty ( & mut self , def : InstanceDef ) -> stable_mir:: ty:: Ty {
179
+ let instance = self . instances [ def] ;
180
+ let ty = instance. ty ( self . tcx , ParamEnv :: empty ( ) ) ;
181
+ self . intern_ty ( ty)
182
+ }
183
+
184
+ fn instance_def_id ( & mut self , def : InstanceDef ) -> stable_mir:: DefId {
185
+ let def_id = self . instances [ def] . def_id ( ) ;
186
+ self . create_def_id ( def_id)
187
+ }
188
+
189
+ fn mono_instance ( & mut self , item : stable_mir:: CrateItem ) -> stable_mir:: mir:: mono:: Instance {
190
+ let def_id = self [ item. 0 ] ;
191
+ Instance :: mono ( self . tcx , def_id) . stable ( self )
192
+ }
193
+
194
+ fn requires_monomorphization ( & self , def_id : stable_mir:: DefId ) -> bool {
195
+ let def_id = self [ def_id] ;
196
+ let generics = self . tcx . generics_of ( def_id) ;
197
+ let result = generics. requires_monomorphization ( self . tcx ) ;
198
+ println ! ( "req {result}: {def_id:?}" ) ;
199
+ result
200
+ }
193
201
}
194
202
195
203
#[ derive( Clone ) ]
@@ -224,7 +232,8 @@ pub struct Tables<'tcx> {
224
232
pub def_ids : IndexMap < DefId , stable_mir:: DefId > ,
225
233
pub alloc_ids : IndexMap < AllocId , stable_mir:: AllocId > ,
226
234
pub spans : IndexMap < rustc_span:: Span , Span > ,
227
- pub types : Vec < MaybeStable < stable_mir:: ty:: TyKind , Ty < ' tcx > > > ,
235
+ pub types : Vec < MaybeStable < TyKind , Ty < ' tcx > > > ,
236
+ pub instances : IndexMap < ty:: Instance < ' tcx > , InstanceDef > ,
228
237
}
229
238
230
239
impl < ' tcx > Tables < ' tcx > {
@@ -254,6 +263,35 @@ pub(crate) trait Stable<'tcx> {
254
263
fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T ;
255
264
}
256
265
266
+ impl < ' tcx > Stable < ' tcx > for mir:: Body < ' tcx > {
267
+ type T = stable_mir:: mir:: Body ;
268
+
269
+ fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
270
+ stable_mir:: mir:: Body {
271
+ blocks : self
272
+ . basic_blocks
273
+ . iter ( )
274
+ . map ( |block| stable_mir:: mir:: BasicBlock {
275
+ terminator : block. terminator ( ) . stable ( tables) ,
276
+ statements : block
277
+ . statements
278
+ . iter ( )
279
+ . map ( |statement| statement. stable ( tables) )
280
+ . collect ( ) ,
281
+ } )
282
+ . collect ( ) ,
283
+ locals : self
284
+ . local_decls
285
+ . iter ( )
286
+ . map ( |decl| stable_mir:: mir:: LocalDecl {
287
+ ty : tables. intern_ty ( decl. ty ) ,
288
+ span : decl. source_info . span . stable ( tables) ,
289
+ } )
290
+ . collect ( ) ,
291
+ }
292
+ }
293
+ }
294
+
257
295
impl < ' tcx > Stable < ' tcx > for mir:: Statement < ' tcx > {
258
296
type T = stable_mir:: mir:: Statement ;
259
297
fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
@@ -1637,3 +1675,38 @@ impl<'tcx> Stable<'tcx> for DefKind {
1637
1675
opaque ( self )
1638
1676
}
1639
1677
}
1678
+
1679
+ impl < ' tcx > Stable < ' tcx > for ty:: Instance < ' tcx > {
1680
+ type T = stable_mir:: mir:: mono:: Instance ;
1681
+
1682
+ fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1683
+ let def = tables. instance_def ( * self ) ;
1684
+ let kind = match self . def {
1685
+ ty:: InstanceDef :: Item ( ..) => stable_mir:: mir:: mono:: InstanceKind :: Item ,
1686
+ ty:: InstanceDef :: Intrinsic ( ..) => stable_mir:: mir:: mono:: InstanceKind :: Intrinsic ,
1687
+ ty:: InstanceDef :: Virtual ( ..) => stable_mir:: mir:: mono:: InstanceKind :: Virtual ,
1688
+ ty:: InstanceDef :: VTableShim ( ..)
1689
+ | ty:: InstanceDef :: ReifyShim ( ..)
1690
+ | ty:: InstanceDef :: FnPtrAddrShim ( ..)
1691
+ | ty:: InstanceDef :: ClosureOnceShim { .. }
1692
+ | ty:: InstanceDef :: ThreadLocalShim ( ..)
1693
+ | ty:: InstanceDef :: DropGlue ( ..)
1694
+ | ty:: InstanceDef :: CloneShim ( ..)
1695
+ | ty:: InstanceDef :: FnPtrShim ( ..) => stable_mir:: mir:: mono:: InstanceKind :: Shim ,
1696
+ } ;
1697
+ stable_mir:: mir:: mono:: Instance { def, kind }
1698
+ }
1699
+ }
1700
+
1701
+ impl < ' tcx > Stable < ' tcx > for MonoItem < ' tcx > {
1702
+ type T = stable_mir:: mir:: mono:: MonoItem ;
1703
+
1704
+ fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1705
+ use stable_mir:: mir:: mono:: MonoItem as StableMonoItem ;
1706
+ match self {
1707
+ MonoItem :: Fn ( instance) => StableMonoItem :: Fn ( instance. stable ( tables) ) ,
1708
+ MonoItem :: Static ( def_id) => StableMonoItem :: Static ( tables. static_def ( * def_id) ) ,
1709
+ MonoItem :: GlobalAsm ( item_id) => StableMonoItem :: GlobalAsm ( opaque ( item_id) ) ,
1710
+ }
1711
+ }
1712
+ }
0 commit comments