9
9
10
10
use std:: cell:: RefCell ;
11
11
use std:: fmt:: Debug ;
12
+ use std:: ops:: Index ;
12
13
14
+ use bridge:: * ;
13
15
use context:: SmirCtxt ;
14
16
use rustc_middle:: mir;
15
17
use rustc_middle:: mir:: interpret:: AllocId ;
16
18
use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
19
+ use rustc_span:: Span ;
17
20
use rustc_span:: def_id:: { CrateNum , DefId , LOCAL_CRATE } ;
18
21
19
22
use crate :: rustc_internal:: IndexMap ;
20
23
21
24
pub mod alloc;
25
+ pub mod bridge;
22
26
mod builder;
23
27
pub mod context;
24
28
@@ -29,14 +33,14 @@ pub struct SmirContainer<'tcx, B: Bridge> {
29
33
}
30
34
31
35
pub struct Tables < ' tcx , B : Bridge > {
32
- pub ( crate ) def_ids : IndexMap < DefId , B :: DefId > ,
33
- pub ( crate ) alloc_ids : IndexMap < AllocId , B :: AllocId > ,
34
- pub ( crate ) spans : IndexMap < rustc_span:: Span , B :: Span > ,
35
- pub ( crate ) types : IndexMap < Ty < ' tcx > , B :: Ty > ,
36
- pub ( crate ) instances : IndexMap < ty:: Instance < ' tcx > , B :: InstanceDef > ,
37
- pub ( crate ) ty_consts : IndexMap < ty:: Const < ' tcx > , B :: TyConstId > ,
38
- pub ( crate ) mir_consts : IndexMap < mir:: Const < ' tcx > , B :: MirConstId > ,
39
- pub ( crate ) layouts : IndexMap < rustc_abi:: Layout < ' tcx > , B :: Layout > ,
36
+ pub def_ids : IndexMap < DefId , B :: DefId > ,
37
+ pub alloc_ids : IndexMap < AllocId , B :: AllocId > ,
38
+ pub spans : IndexMap < rustc_span:: Span , B :: Span > ,
39
+ pub types : IndexMap < Ty < ' tcx > , B :: Ty > ,
40
+ pub instances : IndexMap < ty:: Instance < ' tcx > , B :: InstanceDef > ,
41
+ pub ty_consts : IndexMap < ty:: Const < ' tcx > , B :: TyConstId > ,
42
+ pub mir_consts : IndexMap < mir:: Const < ' tcx > , B :: MirConstId > ,
43
+ pub layouts : IndexMap < rustc_abi:: Layout < ' tcx > , B :: Layout > ,
40
44
}
41
45
42
46
impl < ' tcx , B : Bridge > Default for Tables < ' tcx , B > {
@@ -54,23 +58,142 @@ impl<'tcx, B: Bridge> Default for Tables<'tcx, B> {
54
58
}
55
59
}
56
60
61
+ impl < ' tcx , B : Bridge > Index < B :: DefId > for Tables < ' tcx , B > {
62
+ type Output = DefId ;
63
+
64
+ #[ inline( always) ]
65
+ fn index ( & self , index : B :: DefId ) -> & Self :: Output {
66
+ & self . def_ids [ index]
67
+ }
68
+ }
69
+
57
70
impl < ' tcx , B : Bridge > Tables < ' tcx , B > {
58
- pub ( crate ) fn intern_ty ( & mut self , ty : Ty < ' tcx > ) -> B :: Ty {
71
+ pub fn intern_ty ( & mut self , ty : Ty < ' tcx > ) -> B :: Ty {
59
72
self . types . create_or_fetch ( ty)
60
73
}
61
74
62
- pub ( crate ) fn intern_ty_const ( & mut self , ct : ty:: Const < ' tcx > ) -> B :: TyConstId {
75
+ pub fn intern_ty_const ( & mut self , ct : ty:: Const < ' tcx > ) -> B :: TyConstId {
63
76
self . ty_consts . create_or_fetch ( ct)
64
77
}
65
78
66
- pub ( crate ) fn intern_mir_const ( & mut self , constant : mir:: Const < ' tcx > ) -> B :: MirConstId {
79
+ pub fn intern_mir_const ( & mut self , constant : mir:: Const < ' tcx > ) -> B :: MirConstId {
67
80
self . mir_consts . create_or_fetch ( constant)
68
81
}
82
+
83
+ pub fn create_def_id ( & mut self , did : DefId ) -> B :: DefId {
84
+ self . def_ids . create_or_fetch ( did)
85
+ }
86
+
87
+ pub fn create_alloc_id ( & mut self , aid : AllocId ) -> B :: AllocId {
88
+ self . alloc_ids . create_or_fetch ( aid)
89
+ }
90
+
91
+ pub fn create_span ( & mut self , span : Span ) -> B :: Span {
92
+ self . spans . create_or_fetch ( span)
93
+ }
94
+
95
+ pub fn instance_def ( & mut self , instance : ty:: Instance < ' tcx > ) -> B :: InstanceDef {
96
+ self . instances . create_or_fetch ( instance)
97
+ }
98
+
99
+ pub fn layout_id ( & mut self , layout : rustc_abi:: Layout < ' tcx > ) -> B :: Layout {
100
+ self . layouts . create_or_fetch ( layout)
101
+ }
102
+
103
+ pub fn crate_item ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: CrateItem {
104
+ B :: CrateItem :: new ( self . create_def_id ( did) )
105
+ }
106
+
107
+ pub fn adt_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: AdtDef {
108
+ B :: AdtDef :: new ( self . create_def_id ( did) )
109
+ }
110
+
111
+ pub fn foreign_module_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: ForeignModuleDef {
112
+ B :: ForeignModuleDef :: new ( self . create_def_id ( did) )
113
+ }
114
+
115
+ pub fn foreign_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: ForeignDef {
116
+ B :: ForeignDef :: new ( self . create_def_id ( did) )
117
+ }
118
+
119
+ pub fn fn_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: FnDef {
120
+ B :: FnDef :: new ( self . create_def_id ( did) )
121
+ }
122
+
123
+ pub fn closure_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: ClosureDef {
124
+ B :: ClosureDef :: new ( self . create_def_id ( did) )
125
+ }
126
+
127
+ pub fn coroutine_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: CoroutineDef {
128
+ B :: CoroutineDef :: new ( self . create_def_id ( did) )
129
+ }
130
+
131
+ pub fn coroutine_closure_def (
132
+ & mut self ,
133
+ did : rustc_span:: def_id:: DefId ,
134
+ ) -> B :: CoroutineClosureDef {
135
+ B :: CoroutineClosureDef :: new ( self . create_def_id ( did) )
136
+ }
137
+
138
+ pub fn alias_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: AliasDef {
139
+ B :: AliasDef :: new ( self . create_def_id ( did) )
140
+ }
141
+
142
+ pub fn param_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: ParamDef {
143
+ B :: ParamDef :: new ( self . create_def_id ( did) )
144
+ }
145
+
146
+ pub fn br_named_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: BrNamedDef {
147
+ B :: BrNamedDef :: new ( self . create_def_id ( did) )
148
+ }
149
+
150
+ pub fn trait_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: TraitDef {
151
+ B :: TraitDef :: new ( self . create_def_id ( did) )
152
+ }
153
+
154
+ pub fn generic_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: GenericDef {
155
+ B :: GenericDef :: new ( self . create_def_id ( did) )
156
+ }
157
+
158
+ pub fn const_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: ConstDef {
159
+ B :: ConstDef :: new ( self . create_def_id ( did) )
160
+ }
161
+
162
+ pub fn impl_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: ImplDef {
163
+ B :: ImplDef :: new ( self . create_def_id ( did) )
164
+ }
165
+
166
+ pub fn region_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: RegionDef {
167
+ B :: RegionDef :: new ( self . create_def_id ( did) )
168
+ }
169
+
170
+ pub fn coroutine_witness_def (
171
+ & mut self ,
172
+ did : rustc_span:: def_id:: DefId ,
173
+ ) -> B :: CoroutineWitnessDef {
174
+ B :: CoroutineWitnessDef :: new ( self . create_def_id ( did) )
175
+ }
176
+
177
+ pub fn assoc_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: AssocDef {
178
+ B :: AssocDef :: new ( self . create_def_id ( did) )
179
+ }
180
+
181
+ pub fn opaque_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: OpaqueDef {
182
+ B :: OpaqueDef :: new ( self . create_def_id ( did) )
183
+ }
184
+
185
+ pub fn prov ( & mut self , aid : rustc_middle:: mir:: interpret:: AllocId ) -> B :: Prov {
186
+ B :: Prov :: new ( self . create_alloc_id ( aid) )
187
+ }
188
+
189
+ pub fn static_def ( & mut self , did : rustc_span:: def_id:: DefId ) -> B :: StaticDef {
190
+ B :: StaticDef :: new ( self . create_def_id ( did) )
191
+ }
69
192
}
70
193
71
194
/// A trait defining types that are used to emulate StableMIR components, which is really
72
195
/// useful when programming in stable_mir-agnostic settings.
73
- pub trait Bridge {
196
+ pub trait Bridge : Sized {
74
197
type DefId : Copy + Debug + PartialEq + IndexedVal ;
75
198
type AllocId : Copy + Debug + PartialEq + IndexedVal ;
76
199
type Span : Copy + Debug + PartialEq + IndexedVal ;
@@ -79,12 +202,29 @@ pub trait Bridge {
79
202
type TyConstId : Copy + Debug + PartialEq + IndexedVal ;
80
203
type MirConstId : Copy + Debug + PartialEq + IndexedVal ;
81
204
type Layout : Copy + Debug + PartialEq + IndexedVal ;
82
- type Error : SmirError ;
83
- }
84
205
85
- pub trait SmirError {
86
- fn new ( msg : String ) -> Self ;
87
- fn from_internal < T : Debug > ( err : T ) -> Self ;
206
+ type Error : SmirError ;
207
+ type CrateItem : CrateItem < Self > ;
208
+ type AdtDef : AdtDef < Self > ;
209
+ type ForeignModuleDef : ForeignModuleDef < Self > ;
210
+ type ForeignDef : ForeignDef < Self > ;
211
+ type FnDef : FnDef < Self > ;
212
+ type ClosureDef : ClosureDef < Self > ;
213
+ type CoroutineDef : CoroutineDef < Self > ;
214
+ type CoroutineClosureDef : CoroutineClosureDef < Self > ;
215
+ type AliasDef : AliasDef < Self > ;
216
+ type ParamDef : ParamDef < Self > ;
217
+ type BrNamedDef : BrNamedDef < Self > ;
218
+ type TraitDef : TraitDef < Self > ;
219
+ type GenericDef : GenericDef < Self > ;
220
+ type ConstDef : ConstDef < Self > ;
221
+ type ImplDef : ImplDef < Self > ;
222
+ type RegionDef : RegionDef < Self > ;
223
+ type CoroutineWitnessDef : CoroutineWitnessDef < Self > ;
224
+ type AssocDef : AssocDef < Self > ;
225
+ type OpaqueDef : OpaqueDef < Self > ;
226
+ type Prov : Prov < Self > ;
227
+ type StaticDef : StaticDef < Self > ;
88
228
}
89
229
90
230
pub trait IndexedVal {
0 commit comments