99
1010use std:: cell:: RefCell ;
1111use std:: fmt:: Debug ;
12+ use std:: ops:: Index ;
1213
14+ use bridge:: * ;
1315use context:: SmirCtxt ;
1416use rustc_hir:: def:: DefKind ;
1517use rustc_middle:: mir;
1618use rustc_middle:: mir:: interpret:: AllocId ;
1719use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
20+ use rustc_span:: Span ;
1821use rustc_span:: def_id:: { CrateNum , DefId , LOCAL_CRATE } ;
1922
2023use crate :: rustc_internal:: IndexMap ;
2124
2225pub mod alloc;
26+ pub mod bridge;
2327mod builder;
2428pub mod context;
2529
@@ -30,14 +34,14 @@ pub struct SmirContainer<'tcx, B: Bridge> {
3034}
3135
3236pub struct Tables < ' tcx , B : Bridge > {
33- pub ( crate ) def_ids : IndexMap < DefId , B :: DefId > ,
34- pub ( crate ) alloc_ids : IndexMap < AllocId , B :: AllocId > ,
35- pub ( crate ) spans : IndexMap < rustc_span:: Span , B :: Span > ,
36- pub ( crate ) types : IndexMap < Ty < ' tcx > , B :: Ty > ,
37- pub ( crate ) instances : IndexMap < ty:: Instance < ' tcx > , B :: InstanceDef > ,
38- pub ( crate ) ty_consts : IndexMap < ty:: Const < ' tcx > , B :: TyConstId > ,
39- pub ( crate ) mir_consts : IndexMap < mir:: Const < ' tcx > , B :: MirConstId > ,
40- pub ( crate ) layouts : IndexMap < rustc_abi:: Layout < ' tcx > , B :: Layout > ,
37+ pub def_ids : IndexMap < DefId , B :: DefId > ,
38+ pub alloc_ids : IndexMap < AllocId , B :: AllocId > ,
39+ pub spans : IndexMap < rustc_span:: Span , B :: Span > ,
40+ pub types : IndexMap < Ty < ' tcx > , B :: Ty > ,
41+ pub instances : IndexMap < ty:: Instance < ' tcx > , B :: InstanceDef > ,
42+ pub ty_consts : IndexMap < ty:: Const < ' tcx > , B :: TyConstId > ,
43+ pub mir_consts : IndexMap < mir:: Const < ' tcx > , B :: MirConstId > ,
44+ pub layouts : IndexMap < rustc_abi:: Layout < ' tcx > , B :: Layout > ,
4145}
4246
4347impl < ' tcx , B : Bridge > Default for Tables < ' tcx , B > {
@@ -55,23 +59,142 @@ impl<'tcx, B: Bridge> Default for Tables<'tcx, B> {
5559 }
5660}
5761
62+ impl < ' tcx , B : Bridge > Index < B :: DefId > for Tables < ' tcx , B > {
63+ type Output = DefId ;
64+
65+ #[ inline( always) ]
66+ fn index ( & self , index : B :: DefId ) -> & Self :: Output {
67+ & self . def_ids [ index]
68+ }
69+ }
70+
5871impl < ' tcx , B : Bridge > Tables < ' tcx , B > {
59- pub ( crate ) fn intern_ty ( & mut self , ty : Ty < ' tcx > ) -> B :: Ty {
72+ pub fn intern_ty ( & mut self , ty : Ty < ' tcx > ) -> B :: Ty {
6073 self . types . create_or_fetch ( ty)
6174 }
6275
63- pub ( crate ) fn intern_ty_const ( & mut self , ct : ty:: Const < ' tcx > ) -> B :: TyConstId {
76+ pub fn intern_ty_const ( & mut self , ct : ty:: Const < ' tcx > ) -> B :: TyConstId {
6477 self . ty_consts . create_or_fetch ( ct)
6578 }
6679
67- pub ( crate ) fn intern_mir_const ( & mut self , constant : mir:: Const < ' tcx > ) -> B :: MirConstId {
80+ pub fn intern_mir_const ( & mut self , constant : mir:: Const < ' tcx > ) -> B :: MirConstId {
6881 self . mir_consts . create_or_fetch ( constant)
6982 }
83+
84+ pub fn create_def_id ( & mut self , did : DefId ) -> B :: DefId {
85+ self . def_ids . create_or_fetch ( did)
86+ }
87+
88+ pub fn create_alloc_id ( & mut self , aid : AllocId ) -> B :: AllocId {
89+ self . alloc_ids . create_or_fetch ( aid)
90+ }
91+
92+ pub fn create_span ( & mut self , span : Span ) -> B :: Span {
93+ self . spans . create_or_fetch ( span)
94+ }
95+
96+ pub fn instance_def ( & mut self , instance : ty:: Instance < ' tcx > ) -> B :: InstanceDef {
97+ self . instances . create_or_fetch ( instance)
98+ }
99+
100+ pub fn layout_id ( & mut self , layout : rustc_abi:: Layout < ' tcx > ) -> B :: Layout {
101+ self . layouts . create_or_fetch ( layout)
102+ }
103+
104+ pub fn crate_item ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: CrateItem {
105+ B :: CrateItem :: new ( self . create_def_id ( did) )
106+ }
107+
108+ pub fn adt_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: AdtDef {
109+ B :: AdtDef :: new ( self . create_def_id ( did) )
110+ }
111+
112+ pub fn foreign_module_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: ForeignModuleDef {
113+ B :: ForeignModuleDef :: new ( self . create_def_id ( did) )
114+ }
115+
116+ pub fn foreign_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: ForeignDef {
117+ B :: ForeignDef :: new ( self . create_def_id ( did) )
118+ }
119+
120+ pub fn fn_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: FnDef {
121+ B :: FnDef :: new ( self . create_def_id ( did) )
122+ }
123+
124+ pub fn closure_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: ClosureDef {
125+ B :: ClosureDef :: new ( self . create_def_id ( did) )
126+ }
127+
128+ pub fn coroutine_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: CoroutineDef {
129+ B :: CoroutineDef :: new ( self . create_def_id ( did) )
130+ }
131+
132+ pub fn coroutine_closure_def (
133+ & mut self ,
134+ did : rustc_span:: def_id:: DefId ,
135+ ) -> B :: CoroutineClosureDef {
136+ B :: CoroutineClosureDef :: new ( self . create_def_id ( did) )
137+ }
138+
139+ pub fn alias_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: AliasDef {
140+ B :: AliasDef :: new ( self . create_def_id ( did) )
141+ }
142+
143+ pub fn param_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: ParamDef {
144+ B :: ParamDef :: new ( self . create_def_id ( did) )
145+ }
146+
147+ pub fn br_named_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: BrNamedDef {
148+ B :: BrNamedDef :: new ( self . create_def_id ( did) )
149+ }
150+
151+ pub fn trait_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: TraitDef {
152+ B :: TraitDef :: new ( self . create_def_id ( did) )
153+ }
154+
155+ pub fn generic_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: GenericDef {
156+ B :: GenericDef :: new ( self . create_def_id ( did) )
157+ }
158+
159+ pub fn const_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: ConstDef {
160+ B :: ConstDef :: new ( self . create_def_id ( did) )
161+ }
162+
163+ pub fn impl_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: ImplDef {
164+ B :: ImplDef :: new ( self . create_def_id ( did) )
165+ }
166+
167+ pub fn region_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: RegionDef {
168+ B :: RegionDef :: new ( self . create_def_id ( did) )
169+ }
170+
171+ pub fn coroutine_witness_def (
172+ & mut self ,
173+ did : rustc_span:: def_id:: DefId ,
174+ ) -> B :: CoroutineWitnessDef {
175+ B :: CoroutineWitnessDef :: new ( self . create_def_id ( did) )
176+ }
177+
178+ pub fn assoc_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: AssocDef {
179+ B :: AssocDef :: new ( self . create_def_id ( did) )
180+ }
181+
182+ pub fn opaque_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: OpaqueDef {
183+ B :: OpaqueDef :: new ( self . create_def_id ( did) )
184+ }
185+
186+ pub fn prov ( & mut self , aid : rustc_middle:: mir:: interpret:: AllocId ) -> B :: Prov {
187+ B :: Prov :: new ( self . create_alloc_id ( aid) )
188+ }
189+
190+ pub fn static_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: StaticDef {
191+ B :: StaticDef :: new ( self . create_def_id ( did) )
192+ }
70193}
71194
72195/// A trait defining types that are used to emulate StableMIR components, which is really
73196/// useful when programming in stable_mir-agnostic settings.
74- pub trait Bridge {
197+ pub trait Bridge : Sized {
75198 type DefId : Copy + Debug + PartialEq + IndexedVal ;
76199 type AllocId : Copy + Debug + PartialEq + IndexedVal ;
77200 type Span : Copy + Debug + PartialEq + IndexedVal ;
@@ -80,12 +203,29 @@ pub trait Bridge {
80203 type TyConstId : Copy + Debug + PartialEq + IndexedVal ;
81204 type MirConstId : Copy + Debug + PartialEq + IndexedVal ;
82205 type Layout : Copy + Debug + PartialEq + IndexedVal ;
83- type Error : SmirError ;
84- }
85206
86- pub trait SmirError {
87- fn new ( msg : String ) -> Self ;
88- fn from_internal < T : Debug > ( err : T ) -> Self ;
207+ type Error : SmirError ;
208+ type CrateItem : CrateItem < Self > ;
209+ type AdtDef : AdtDef < Self > ;
210+ type ForeignModuleDef : ForeignModuleDef < Self > ;
211+ type ForeignDef : ForeignDef < Self > ;
212+ type FnDef : FnDef < Self > ;
213+ type ClosureDef : ClosureDef < Self > ;
214+ type CoroutineDef : CoroutineDef < Self > ;
215+ type CoroutineClosureDef : CoroutineClosureDef < Self > ;
216+ type AliasDef : AliasDef < Self > ;
217+ type ParamDef : ParamDef < Self > ;
218+ type BrNamedDef : BrNamedDef < Self > ;
219+ type TraitDef : TraitDef < Self > ;
220+ type GenericDef : GenericDef < Self > ;
221+ type ConstDef : ConstDef < Self > ;
222+ type ImplDef : ImplDef < Self > ;
223+ type RegionDef : RegionDef < Self > ;
224+ type CoroutineWitnessDef : CoroutineWitnessDef < Self > ;
225+ type AssocDef : AssocDef < Self > ;
226+ type OpaqueDef : OpaqueDef < Self > ;
227+ type Prov : Prov < Self > ;
228+ type StaticDef : StaticDef < Self > ;
89229}
90230
91231pub trait IndexedVal {
0 commit comments