@@ -13,10 +13,12 @@ use crate::rustc_smir::stable_mir::ty::{BoundRegion, EarlyBoundRegion, Region};
1313use rustc_hir as hir;
1414use rustc_middle:: mir;
1515use 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 } ;
1718use rustc_span:: def_id:: { CrateNum , DefId , LOCAL_CRATE } ;
1819use 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 } ;
2022use stable_mir:: ty:: { FloatTy , GenericParamDef , IntTy , Movability , RigidTy , Span , TyKind , UintTy } ;
2123use stable_mir:: { self , opaque, Context } ;
2224use tracing:: debug;
@@ -100,29 +102,7 @@ impl<'tcx> Context for Tables<'tcx> {
100102
101103 fn mir_body ( & mut self , item : stable_mir:: DefId ) -> stable_mir:: mir:: Body {
102104 let def_id = self [ item] ;
103- let mir = self . tcx . instance_mir ( ty:: InstanceDef :: Item ( def_id) ) ;
104- stable_mir:: mir:: Body {
105- blocks : mir
106- . basic_blocks
107- . iter ( )
108- . map ( |block| stable_mir:: mir:: BasicBlock {
109- terminator : block. terminator ( ) . stable ( self ) ,
110- statements : block
111- . statements
112- . iter ( )
113- . map ( |statement| statement. stable ( self ) )
114- . collect ( ) ,
115- } )
116- . collect ( ) ,
117- locals : mir
118- . local_decls
119- . iter ( )
120- . map ( |decl| stable_mir:: mir:: LocalDecl {
121- ty : self . intern_ty ( decl. ty ) ,
122- span : decl. source_info . span . stable ( self ) ,
123- } )
124- . collect ( ) ,
125- }
105+ self . tcx . instance_mir ( ty:: InstanceDef :: Item ( def_id) ) . stable ( self )
126106 }
127107
128108 fn ty_kind ( & mut self , ty : stable_mir:: ty:: Ty ) -> TyKind {
@@ -171,6 +151,34 @@ impl<'tcx> Context for Tables<'tcx> {
171151 . collect ( ) ,
172152 }
173153 }
154+
155+ fn instance_body ( & mut self , _def : InstanceDef ) -> Body {
156+ todo ! ( "Monomorphize the body" )
157+ }
158+
159+ fn instance_ty ( & mut self , def : InstanceDef ) -> stable_mir:: ty:: Ty {
160+ let instance = self . instances [ def] ;
161+ let ty = instance. ty ( self . tcx , ParamEnv :: empty ( ) ) ;
162+ self . intern_ty ( ty)
163+ }
164+
165+ fn instance_def_id ( & mut self , def : InstanceDef ) -> stable_mir:: DefId {
166+ let def_id = self . instances [ def] . def_id ( ) ;
167+ self . create_def_id ( def_id)
168+ }
169+
170+ fn mono_instance ( & mut self , item : stable_mir:: CrateItem ) -> stable_mir:: mir:: mono:: Instance {
171+ let def_id = self [ item. 0 ] ;
172+ Instance :: mono ( self . tcx , def_id) . stable ( self )
173+ }
174+
175+ fn requires_monomorphization ( & self , def_id : stable_mir:: DefId ) -> bool {
176+ let def_id = self [ def_id] ;
177+ let generics = self . tcx . generics_of ( def_id) ;
178+ let result = generics. requires_monomorphization ( self . tcx ) ;
179+ println ! ( "req {result}: {def_id:?}" ) ;
180+ result
181+ }
174182}
175183
176184#[ derive( Clone ) ]
@@ -205,7 +213,8 @@ pub struct Tables<'tcx> {
205213 pub def_ids : IndexMap < DefId , stable_mir:: DefId > ,
206214 pub alloc_ids : IndexMap < AllocId , stable_mir:: AllocId > ,
207215 pub spans : IndexMap < rustc_span:: Span , Span > ,
208- pub types : Vec < MaybeStable < stable_mir:: ty:: TyKind , Ty < ' tcx > > > ,
216+ pub types : Vec < MaybeStable < TyKind , Ty < ' tcx > > > ,
217+ pub instances : IndexMap < ty:: Instance < ' tcx > , InstanceDef > ,
209218}
210219
211220impl < ' tcx > Tables < ' tcx > {
@@ -235,6 +244,35 @@ pub(crate) trait Stable<'tcx> {
235244 fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T ;
236245}
237246
247+ impl < ' tcx > Stable < ' tcx > for mir:: Body < ' tcx > {
248+ type T = stable_mir:: mir:: Body ;
249+
250+ fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
251+ stable_mir:: mir:: Body {
252+ blocks : self
253+ . basic_blocks
254+ . iter ( )
255+ . map ( |block| stable_mir:: mir:: BasicBlock {
256+ terminator : block. terminator ( ) . stable ( tables) ,
257+ statements : block
258+ . statements
259+ . iter ( )
260+ . map ( |statement| statement. stable ( tables) )
261+ . collect ( ) ,
262+ } )
263+ . collect ( ) ,
264+ locals : self
265+ . local_decls
266+ . iter ( )
267+ . map ( |decl| stable_mir:: mir:: LocalDecl {
268+ ty : tables. intern_ty ( decl. ty ) ,
269+ span : decl. source_info . span . stable ( tables) ,
270+ } )
271+ . collect ( ) ,
272+ }
273+ }
274+ }
275+
238276impl < ' tcx > Stable < ' tcx > for mir:: Statement < ' tcx > {
239277 type T = stable_mir:: mir:: Statement ;
240278 fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
@@ -1618,3 +1656,38 @@ impl<'tcx> Stable<'tcx> for DefKind {
16181656 opaque ( self )
16191657 }
16201658}
1659+
1660+ impl < ' tcx > Stable < ' tcx > for ty:: Instance < ' tcx > {
1661+ type T = stable_mir:: mir:: mono:: Instance ;
1662+
1663+ fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1664+ let def = tables. instance_def ( * self ) ;
1665+ let kind = match self . def {
1666+ ty:: InstanceDef :: Item ( ..) => stable_mir:: mir:: mono:: InstanceKind :: Item ,
1667+ ty:: InstanceDef :: Intrinsic ( ..) => stable_mir:: mir:: mono:: InstanceKind :: Intrinsic ,
1668+ ty:: InstanceDef :: Virtual ( ..) => stable_mir:: mir:: mono:: InstanceKind :: Virtual ,
1669+ ty:: InstanceDef :: VTableShim ( ..)
1670+ | ty:: InstanceDef :: ReifyShim ( ..)
1671+ | ty:: InstanceDef :: FnPtrAddrShim ( ..)
1672+ | ty:: InstanceDef :: ClosureOnceShim { .. }
1673+ | ty:: InstanceDef :: ThreadLocalShim ( ..)
1674+ | ty:: InstanceDef :: DropGlue ( ..)
1675+ | ty:: InstanceDef :: CloneShim ( ..)
1676+ | ty:: InstanceDef :: FnPtrShim ( ..) => stable_mir:: mir:: mono:: InstanceKind :: Shim ,
1677+ } ;
1678+ stable_mir:: mir:: mono:: Instance { def, kind }
1679+ }
1680+ }
1681+
1682+ impl < ' tcx > Stable < ' tcx > for MonoItem < ' tcx > {
1683+ type T = stable_mir:: mir:: mono:: MonoItem ;
1684+
1685+ fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1686+ use stable_mir:: mir:: mono:: MonoItem as StableMonoItem ;
1687+ match self {
1688+ MonoItem :: Fn ( instance) => StableMonoItem :: Fn ( instance. stable ( tables) ) ,
1689+ MonoItem :: Static ( def_id) => StableMonoItem :: Static ( tables. static_def ( * def_id) ) ,
1690+ MonoItem :: GlobalAsm ( item_id) => StableMonoItem :: GlobalAsm ( opaque ( item_id) ) ,
1691+ }
1692+ }
1693+ }
0 commit comments