8
8
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
9
9
10
10
use crate :: rustc_internal:: { IndexMap , RustcInternal } ;
11
- use crate :: rustc_smir:: hir:: def:: DefKind ;
12
11
use crate :: rustc_smir:: stable_mir:: ty:: { BoundRegion , EarlyBoundRegion , Region } ;
13
12
use rustc_hir as hir;
13
+ use rustc_hir:: def:: DefKind ;
14
14
use rustc_middle:: mir;
15
15
use rustc_middle:: mir:: interpret:: { alloc_range, AllocId } ;
16
16
use rustc_middle:: mir:: mono:: MonoItem ;
@@ -20,10 +20,10 @@ use rustc_target::abi::FieldIdx;
20
20
use stable_mir:: mir:: mono:: InstanceDef ;
21
21
use stable_mir:: mir:: { Body , CopyNonOverlapping , Statement , UserTypeProjection , VariantIdx } ;
22
22
use stable_mir:: ty:: {
23
- Const , ConstId , ConstantKind , FloatTy , GenericParamDef , IntTy , LineInfo , Movability , RigidTy ,
24
- Span , TyKind , UintTy ,
23
+ AdtDef , AdtKind , ClosureDef , ClosureKind , Const , ConstId , ConstantKind , FloatTy , FnDef ,
24
+ GenericArgs , GenericParamDef , IntTy , LineInfo , Movability , RigidTy , Span , TyKind , UintTy ,
25
25
} ;
26
- use stable_mir:: { self , opaque, Context , Filename } ;
26
+ use stable_mir:: { self , opaque, Context , CrateItem , Filename , ItemKind } ;
27
27
use std:: cell:: RefCell ;
28
28
use tracing:: debug;
29
29
@@ -85,9 +85,23 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
85
85
LineInfo { start_line : lines. 1 , start_col : lines. 2 , end_line : lines. 3 , end_col : lines. 4 }
86
86
}
87
87
88
- fn def_kind ( & self , def_id : stable_mir:: DefId ) -> stable_mir:: DefKind {
88
+ fn item_kind ( & self , item : CrateItem ) -> ItemKind {
89
+ let tables = self . 0 . borrow ( ) ;
90
+ new_item_kind ( tables. tcx . def_kind ( tables[ item. 0 ] ) )
91
+ }
92
+
93
+ fn adt_kind ( & self , def : AdtDef ) -> AdtKind {
94
+ let mut tables = self . 0 . borrow_mut ( ) ;
95
+ let ty = tables. tcx . type_of ( def. 0 . internal ( & mut * tables) ) . instantiate_identity ( ) . kind ( ) ;
96
+ let ty:: TyKind :: Adt ( def, _) = ty else {
97
+ panic ! ( "Expected an ADT definition, but found: {ty:?}" )
98
+ } ;
99
+ def. adt_kind ( ) . stable ( & mut * tables)
100
+ }
101
+
102
+ fn def_ty ( & self , item : stable_mir:: DefId ) -> stable_mir:: ty:: Ty {
89
103
let mut tables = self . 0 . borrow_mut ( ) ;
90
- tables. tcx . def_kind ( tables[ def_id ] ) . stable ( & mut * tables)
104
+ tables. tcx . type_of ( item . internal ( & mut * tables) ) . instantiate_identity ( ) . stable ( & mut * tables)
91
105
}
92
106
93
107
fn span_of_an_item ( & self , def_id : stable_mir:: DefId ) -> Span {
@@ -198,10 +212,12 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
198
212
}
199
213
}
200
214
201
- fn instance_body ( & self , def : InstanceDef ) -> Body {
215
+ fn instance_body ( & self , def : InstanceDef ) -> Option < Body > {
202
216
let mut tables = self . 0 . borrow_mut ( ) ;
203
217
let instance = tables. instances [ def] ;
204
- builder:: BodyBuilder :: new ( tables. tcx , instance) . build ( & mut * tables)
218
+ tables
219
+ . has_body ( instance)
220
+ . then ( || builder:: BodyBuilder :: new ( tables. tcx , instance) . build ( & mut * tables) )
205
221
}
206
222
207
223
fn instance_ty ( & self , def : InstanceDef ) -> stable_mir:: ty:: Ty {
@@ -249,6 +265,42 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
249
265
Ok ( None ) | Err ( _) => None ,
250
266
}
251
267
}
268
+
269
+ fn resolve_drop_in_place (
270
+ & self ,
271
+ ty : stable_mir:: ty:: Ty ,
272
+ ) -> Option < stable_mir:: mir:: mono:: Instance > {
273
+ let mut tables = self . 0 . borrow_mut ( ) ;
274
+ let internal_ty = ty. internal ( & mut * tables) ;
275
+ let instance = Instance :: resolve_drop_in_place ( tables. tcx , internal_ty) ;
276
+ matches ! ( instance. def, ty:: InstanceDef :: DropGlue ( _, Some ( _) ) )
277
+ . then ( || instance. stable ( & mut * tables) )
278
+ }
279
+
280
+ fn resolve_for_fn_ptr (
281
+ & self ,
282
+ def : FnDef ,
283
+ args : & GenericArgs ,
284
+ ) -> Option < stable_mir:: mir:: mono:: Instance > {
285
+ let mut tables = self . 0 . borrow_mut ( ) ;
286
+ let def_id = def. 0 . internal ( & mut * tables) ;
287
+ let args_ref = args. internal ( & mut * tables) ;
288
+ Instance :: resolve_for_fn_ptr ( tables. tcx , ParamEnv :: reveal_all ( ) , def_id, args_ref)
289
+ . stable ( & mut * tables)
290
+ }
291
+
292
+ fn resolve_closure (
293
+ & self ,
294
+ def : ClosureDef ,
295
+ args : & GenericArgs ,
296
+ kind : ClosureKind ,
297
+ ) -> Option < stable_mir:: mir:: mono:: Instance > {
298
+ let mut tables = self . 0 . borrow_mut ( ) ;
299
+ let def_id = def. 0 . internal ( & mut * tables) ;
300
+ let args_ref = args. internal ( & mut * tables) ;
301
+ let closure_kind = kind. internal ( & mut * tables) ;
302
+ Instance :: resolve_closure ( tables. tcx , def_id, args_ref, closure_kind) . stable ( & mut * tables)
303
+ }
252
304
}
253
305
254
306
pub ( crate ) struct TablesWrapper < ' tcx > ( pub ( crate ) RefCell < Tables < ' tcx > > ) ;
@@ -271,6 +323,16 @@ impl<'tcx> Tables<'tcx> {
271
323
fn intern_const ( & mut self , constant : mir:: Const < ' tcx > ) -> ConstId {
272
324
self . constants . create_or_fetch ( constant)
273
325
}
326
+
327
+ fn has_body ( & self , instance : Instance < ' tcx > ) -> bool {
328
+ let def_id = instance. def_id ( ) ;
329
+ !self . tcx . is_foreign_item ( def_id)
330
+ && self . tcx . is_mir_available ( def_id)
331
+ && !matches ! (
332
+ instance. def,
333
+ ty:: InstanceDef :: Virtual ( ..) | ty:: InstanceDef :: Intrinsic ( ..)
334
+ )
335
+ }
274
336
}
275
337
276
338
/// Build a stable mir crate from a given crate number.
@@ -281,6 +343,43 @@ fn smir_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> stable_mir::Crate {
281
343
stable_mir:: Crate { id : crate_num. into ( ) , name : crate_name, is_local }
282
344
}
283
345
346
+ fn new_item_kind ( kind : DefKind ) -> ItemKind {
347
+ match kind {
348
+ DefKind :: Mod
349
+ | DefKind :: Struct
350
+ | DefKind :: Union
351
+ | DefKind :: Enum
352
+ | DefKind :: Variant
353
+ | DefKind :: Trait
354
+ | DefKind :: TyAlias
355
+ | DefKind :: ForeignTy
356
+ | DefKind :: TraitAlias
357
+ | DefKind :: AssocTy
358
+ | DefKind :: TyParam
359
+ | DefKind :: ConstParam
360
+ | DefKind :: Macro ( _)
361
+ | DefKind :: ExternCrate
362
+ | DefKind :: Use
363
+ | DefKind :: ForeignMod
364
+ | DefKind :: OpaqueTy
365
+ | DefKind :: Field
366
+ | DefKind :: LifetimeParam
367
+ | DefKind :: GlobalAsm => {
368
+ unreachable ! ( "Not a valid item kind: {kind:?}" ) ;
369
+ }
370
+ DefKind :: Closure
371
+ | DefKind :: Coroutine
372
+ | DefKind :: Ctor ( _, _)
373
+ | DefKind :: AssocFn
374
+ | DefKind :: Impl { .. }
375
+ | DefKind :: Fn => ItemKind :: Fn ,
376
+ DefKind :: Const | DefKind :: InlineConst | DefKind :: AssocConst | DefKind :: AnonConst => {
377
+ ItemKind :: Const
378
+ }
379
+ DefKind :: Static ( _) => ItemKind :: Static ,
380
+ }
381
+ }
382
+
284
383
/// Trait used to convert between an internal MIR type to a Stable MIR type.
285
384
pub trait Stable < ' tcx > {
286
385
/// The stable representation of the type implementing Stable.
@@ -892,6 +991,18 @@ impl<'tcx> Stable<'tcx> for mir::AggregateKind<'tcx> {
892
991
}
893
992
}
894
993
994
+ impl < ' tcx > Stable < ' tcx > for ty:: AdtKind {
995
+ type T = AdtKind ;
996
+
997
+ fn stable ( & self , _tables : & mut Tables < ' tcx > ) -> Self :: T {
998
+ match self {
999
+ ty:: AdtKind :: Struct => AdtKind :: Struct ,
1000
+ ty:: AdtKind :: Union => AdtKind :: Union ,
1001
+ ty:: AdtKind :: Enum => AdtKind :: Enum ,
1002
+ }
1003
+ }
1004
+ }
1005
+
895
1006
impl < ' tcx > Stable < ' tcx > for rustc_hir:: CoroutineSource {
896
1007
type T = stable_mir:: mir:: CoroutineSource ;
897
1008
fn stable ( & self , _: & mut Tables < ' tcx > ) -> Self :: T {
@@ -1028,8 +1139,6 @@ impl<'tcx> Stable<'tcx> for mir::TerminatorKind<'tcx> {
1028
1139
impl < ' tcx > Stable < ' tcx > for ty:: GenericArgs < ' tcx > {
1029
1140
type T = stable_mir:: ty:: GenericArgs ;
1030
1141
fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1031
- use stable_mir:: ty:: GenericArgs ;
1032
-
1033
1142
GenericArgs ( self . iter ( ) . map ( |arg| arg. unpack ( ) . stable ( tables) ) . collect ( ) )
1034
1143
}
1035
1144
}
@@ -1293,7 +1402,7 @@ impl<'tcx> Stable<'tcx> for ty::TyKind<'tcx> {
1293
1402
TyKind :: Bound ( debruijn_idx. as_usize ( ) , bound_ty. stable ( tables) )
1294
1403
}
1295
1404
ty:: Placeholder ( ..) | ty:: CoroutineWitness ( ..) | ty:: Infer ( _) | ty:: Error ( _) => {
1296
- unreachable ! ( ) ;
1405
+ unreachable ! ( "Unexpected ty: {self:?}" ) ;
1297
1406
}
1298
1407
}
1299
1408
}
@@ -1448,7 +1557,7 @@ impl<'tcx> Stable<'tcx> for ty::TraitRef<'tcx> {
1448
1557
fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1449
1558
use stable_mir:: ty:: TraitRef ;
1450
1559
1451
- TraitRef { def_id : tables. trait_def ( self . def_id ) , args : self . args . stable ( tables) }
1560
+ TraitRef :: try_new ( tables. trait_def ( self . def_id ) , self . args . stable ( tables) ) . unwrap ( )
1452
1561
}
1453
1562
}
1454
1563
@@ -1724,15 +1833,6 @@ impl<'tcx> Stable<'tcx> for rustc_span::Span {
1724
1833
}
1725
1834
}
1726
1835
1727
- impl < ' tcx > Stable < ' tcx > for DefKind {
1728
- type T = stable_mir:: DefKind ;
1729
-
1730
- fn stable ( & self , _: & mut Tables < ' tcx > ) -> Self :: T {
1731
- // FIXME: add a real implementation of stable DefKind
1732
- opaque ( self )
1733
- }
1734
- }
1735
-
1736
1836
impl < ' tcx > Stable < ' tcx > for ty:: Instance < ' tcx > {
1737
1837
type T = stable_mir:: mir:: mono:: Instance ;
1738
1838
@@ -1767,3 +1867,25 @@ impl<'tcx> Stable<'tcx> for MonoItem<'tcx> {
1767
1867
}
1768
1868
}
1769
1869
}
1870
+
1871
+ impl < ' tcx , T > Stable < ' tcx > for & T
1872
+ where
1873
+ T : Stable < ' tcx > ,
1874
+ {
1875
+ type T = T :: T ;
1876
+
1877
+ fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1878
+ ( * self ) . stable ( tables)
1879
+ }
1880
+ }
1881
+
1882
+ impl < ' tcx , T > Stable < ' tcx > for Option < T >
1883
+ where
1884
+ T : Stable < ' tcx > ,
1885
+ {
1886
+ type T = Option < T :: T > ;
1887
+
1888
+ fn stable ( & self , tables : & mut Tables < ' tcx > ) -> Self :: T {
1889
+ self . as_ref ( ) . map ( |value| value. stable ( tables) )
1890
+ }
1891
+ }
0 commit comments