@@ -26,30 +26,51 @@ use crate::stable_mir;
2626mod alloc;
2727mod builder;
2828pub mod context;
29- mod convert;
30-
31- pub struct Tables < ' tcx > {
32- pub ( crate ) tcx : TyCtxt < ' tcx > ,
33- pub ( crate ) def_ids : IndexMap < DefId , stable_mir:: DefId > ,
34- pub ( crate ) alloc_ids : IndexMap < AllocId , stable_mir:: mir:: alloc:: AllocId > ,
35- pub ( crate ) spans : IndexMap < rustc_span:: Span , Span > ,
36- pub ( crate ) types : IndexMap < Ty < ' tcx > , stable_mir:: ty:: Ty > ,
37- pub ( crate ) instances : IndexMap < ty:: Instance < ' tcx > , InstanceDef > ,
38- pub ( crate ) ty_consts : IndexMap < ty:: Const < ' tcx > , TyConstId > ,
39- pub ( crate ) mir_consts : IndexMap < mir:: Const < ' tcx > , MirConstId > ,
40- pub ( crate ) layouts : IndexMap < rustc_abi:: Layout < ' tcx > , Layout > ,
41- }
42-
43- impl < ' tcx > Tables < ' tcx > {
44- pub ( crate ) fn intern_ty ( & mut self , ty : Ty < ' tcx > ) -> stable_mir:: ty:: Ty {
29+
30+ /// A container which is used for TLS.
31+ pub struct SmirContainer < ' tcx , B : Bridge > {
32+ pub tables : RefCell < Tables < ' tcx , B > > ,
33+ pub cx : RefCell < SmirCtxt < ' tcx , B > > ,
34+ }
35+
36+ pub struct Tables < ' tcx , B : Bridge > {
37+ tcx : TyCtxt < ' tcx > ,
38+ pub ( crate ) def_ids : IndexMap < DefId , B :: DefId > ,
39+ pub ( crate ) alloc_ids : IndexMap < AllocId , B :: AllocId > ,
40+ pub ( crate ) spans : IndexMap < rustc_span:: Span , B :: Span > ,
41+ pub ( crate ) types : IndexMap < Ty < ' tcx > , B :: Ty > ,
42+ pub ( crate ) instances : IndexMap < ty:: Instance < ' tcx > , B :: InstanceDef > ,
43+ pub ( crate ) ty_consts : IndexMap < ty:: Const < ' tcx > , B :: TyConstId > ,
44+ pub ( crate ) mir_consts : IndexMap < mir:: Const < ' tcx > , B :: MirConstId > ,
45+ pub ( crate ) layouts : IndexMap < rustc_abi:: Layout < ' tcx > , B :: Layout > ,
46+ }
47+
48+ impl < ' tcx , B : Bridge > Tables < ' tcx , B > {
49+ pub ( crate ) fn new ( tcx : TyCtxt < ' tcx > ) -> Self {
50+ Self {
51+ tcx,
52+ def_ids : IndexMap :: default ( ) ,
53+ alloc_ids : IndexMap :: default ( ) ,
54+ spans : IndexMap :: default ( ) ,
55+ types : IndexMap :: default ( ) ,
56+ instances : IndexMap :: default ( ) ,
57+ ty_consts : IndexMap :: default ( ) ,
58+ mir_consts : IndexMap :: default ( ) ,
59+ layouts : IndexMap :: default ( ) ,
60+ }
61+ }
62+ }
63+
64+ impl < ' tcx , B : Bridge > Tables < ' tcx , B > {
65+ pub ( crate ) fn intern_ty ( & mut self , ty : Ty < ' tcx > ) -> B :: Ty {
4566 self . types . create_or_fetch ( ty)
4667 }
4768
48- pub ( crate ) fn intern_ty_const ( & mut self , ct : ty:: Const < ' tcx > ) -> TyConstId {
69+ pub ( crate ) fn intern_ty_const ( & mut self , ct : ty:: Const < ' tcx > ) -> B :: TyConstId {
4970 self . ty_consts . create_or_fetch ( ct)
5071 }
5172
52- pub ( crate ) fn intern_mir_const ( & mut self , constant : mir:: Const < ' tcx > ) -> MirConstId {
73+ pub ( crate ) fn intern_mir_const ( & mut self , constant : mir:: Const < ' tcx > ) -> B :: MirConstId {
5374 self . mir_consts . create_or_fetch ( constant)
5475 }
5576
@@ -81,19 +102,44 @@ impl<'tcx> Tables<'tcx> {
81102 !must_override && self . tcx . is_mir_available ( def_id)
82103 }
83104
84- fn to_fn_def ( & mut self , def_id : DefId ) -> Option < FnDef > {
105+ fn filter_fn_def ( & mut self , def_id : DefId ) -> Option < DefId > {
85106 if matches ! ( self . tcx. def_kind( def_id) , DefKind :: Fn | DefKind :: AssocFn ) {
86- Some ( self . fn_def ( def_id) )
107+ Some ( def_id)
87108 } else {
88109 None
89110 }
90111 }
91112
92- fn to_static ( & mut self , def_id : DefId ) -> Option < StaticDef > {
93- matches ! ( self . tcx. def_kind( def_id) , DefKind :: Static { .. } ) . then ( || self . static_def ( def_id) )
113+ fn filter_static_def ( & mut self , def_id : DefId ) -> Option < DefId > {
114+ matches ! ( self . tcx. def_kind( def_id) , DefKind :: Static { .. } ) . then ( || def_id)
94115 }
95116}
96117
118+ /// A trait defining types that are used to emulate StableMIR components, which is really
119+ /// useful when programming in stable_mir-agnostic settings.
120+ pub trait Bridge {
121+ type DefId : Copy + Debug + PartialEq + IndexedVal ;
122+ type AllocId : Copy + Debug + PartialEq + IndexedVal ;
123+ type Span : Copy + Debug + PartialEq + IndexedVal ;
124+ type Ty : Copy + Debug + PartialEq + IndexedVal ;
125+ type InstanceDef : Copy + Debug + PartialEq + IndexedVal ;
126+ type TyConstId : Copy + Debug + PartialEq + IndexedVal ;
127+ type MirConstId : Copy + Debug + PartialEq + IndexedVal ;
128+ type Layout : Copy + Debug + PartialEq + IndexedVal ;
129+ type Error : SmirError ;
130+ }
131+
132+ pub trait SmirError {
133+ fn new ( msg : String ) -> Self ;
134+ fn from_internal < T : Debug > ( err : T ) -> Self ;
135+ }
136+
137+ pub trait IndexedVal {
138+ fn to_val ( index : usize ) -> Self ;
139+
140+ fn to_index ( & self ) -> usize ;
141+ }
142+
97143/// Iterate over the definitions of the given crate.
98144pub ( crate ) fn filter_def_ids < F , T > ( tcx : TyCtxt < ' _ > , krate : CrateNum , mut func : F ) -> Vec < T >
99145where
0 commit comments