@@ -17,12 +17,12 @@ use rustc_span::Span;
17
17
use rustc_span:: def_id:: { CrateNum , DefId } ;
18
18
use scoped_tls:: scoped_thread_local;
19
19
use stable_mir:: Error ;
20
- use stable_mir:: abi:: Layout ;
21
20
use stable_mir:: compiler_interface:: SmirInterface ;
22
21
use stable_mir:: ty:: IndexedVal ;
23
22
24
23
use crate :: rustc_smir:: context:: SmirCtxt ;
25
24
use crate :: rustc_smir:: { Stable , Tables } ;
25
+ use crate :: rustc_smir:: { Bridge , SmirContainer , Tables } ;
26
26
use crate :: stable_mir;
27
27
28
28
mod internal;
@@ -40,7 +40,7 @@ pub mod pretty;
40
40
///
41
41
/// This function will panic if StableMIR has not been properly initialized.
42
42
pub fn stable < ' tcx , S : Stable < ' tcx > > ( item : S ) -> S :: T {
43
- with_tables ( |tables| item. stable ( tables) )
43
+ with_container ( |tables, cx | item. stable ( tables, cx ) )
44
44
}
45
45
46
46
/// Convert a stable item into its internal Rust compiler counterpart, if one exists.
@@ -54,137 +54,40 @@ pub fn stable<'tcx, S: Stable<'tcx>>(item: S) -> S::T {
54
54
/// # Panics
55
55
///
56
56
/// This function will panic if StableMIR has not been properly initialized.
57
- pub fn internal < ' tcx , S > ( tcx : TyCtxt < ' tcx > , item : S ) -> S :: T < ' tcx >
57
+ pub fn internal < ' tcx , S > ( item : S ) -> S :: T < ' tcx >
58
58
where
59
59
S : RustcInternal ,
60
60
{
61
- // The tcx argument ensures that the item won't outlive the type context.
62
- with_tables ( |tables| item. internal ( tables, tcx) )
61
+ with_container ( |tables, cx| item. internal ( tables, cx) )
63
62
}
64
63
65
- impl < ' tcx > Index < stable_mir :: DefId > for Tables < ' tcx > {
64
+ impl < ' tcx , B : Bridge > Index < B :: DefId > for Tables < ' tcx , B > {
66
65
type Output = DefId ;
67
66
68
67
#[ inline( always) ]
69
- fn index ( & self , index : stable_mir :: DefId ) -> & Self :: Output {
68
+ fn index ( & self , index : B :: DefId ) -> & Self :: Output {
70
69
& self . def_ids [ index]
71
70
}
72
71
}
73
72
74
- impl < ' tcx > Index < stable_mir:: ty:: Span > for Tables < ' tcx > {
75
- type Output = Span ;
76
-
77
- #[ inline( always) ]
78
- fn index ( & self , index : stable_mir:: ty:: Span ) -> & Self :: Output {
79
- & self . spans [ index]
80
- }
81
- }
82
-
83
- impl < ' tcx > Tables < ' tcx > {
84
- pub fn crate_item ( & mut self , did : DefId ) -> stable_mir:: CrateItem {
85
- stable_mir:: CrateItem ( self . create_def_id ( did) )
86
- }
87
-
88
- pub fn adt_def ( & mut self , did : DefId ) -> stable_mir:: ty:: AdtDef {
89
- stable_mir:: ty:: AdtDef ( self . create_def_id ( did) )
90
- }
91
-
92
- pub fn foreign_module_def ( & mut self , did : DefId ) -> stable_mir:: ty:: ForeignModuleDef {
93
- stable_mir:: ty:: ForeignModuleDef ( self . create_def_id ( did) )
94
- }
95
-
96
- pub fn foreign_def ( & mut self , did : DefId ) -> stable_mir:: ty:: ForeignDef {
97
- stable_mir:: ty:: ForeignDef ( self . create_def_id ( did) )
98
- }
99
-
100
- pub fn fn_def ( & mut self , did : DefId ) -> stable_mir:: ty:: FnDef {
101
- stable_mir:: ty:: FnDef ( self . create_def_id ( did) )
102
- }
103
-
104
- pub fn closure_def ( & mut self , did : DefId ) -> stable_mir:: ty:: ClosureDef {
105
- stable_mir:: ty:: ClosureDef ( self . create_def_id ( did) )
106
- }
107
-
108
- pub fn coroutine_def ( & mut self , did : DefId ) -> stable_mir:: ty:: CoroutineDef {
109
- stable_mir:: ty:: CoroutineDef ( self . create_def_id ( did) )
110
- }
111
-
112
- pub fn coroutine_closure_def ( & mut self , did : DefId ) -> stable_mir:: ty:: CoroutineClosureDef {
113
- stable_mir:: ty:: CoroutineClosureDef ( self . create_def_id ( did) )
114
- }
115
-
116
- pub fn alias_def ( & mut self , did : DefId ) -> stable_mir:: ty:: AliasDef {
117
- stable_mir:: ty:: AliasDef ( self . create_def_id ( did) )
118
- }
119
-
120
- pub fn param_def ( & mut self , did : DefId ) -> stable_mir:: ty:: ParamDef {
121
- stable_mir:: ty:: ParamDef ( self . create_def_id ( did) )
122
- }
123
-
124
- pub fn br_named_def ( & mut self , did : DefId ) -> stable_mir:: ty:: BrNamedDef {
125
- stable_mir:: ty:: BrNamedDef ( self . create_def_id ( did) )
126
- }
127
-
128
- pub fn trait_def ( & mut self , did : DefId ) -> stable_mir:: ty:: TraitDef {
129
- stable_mir:: ty:: TraitDef ( self . create_def_id ( did) )
130
- }
131
-
132
- pub fn generic_def ( & mut self , did : DefId ) -> stable_mir:: ty:: GenericDef {
133
- stable_mir:: ty:: GenericDef ( self . create_def_id ( did) )
134
- }
135
-
136
- pub fn const_def ( & mut self , did : DefId ) -> stable_mir:: ty:: ConstDef {
137
- stable_mir:: ty:: ConstDef ( self . create_def_id ( did) )
138
- }
139
-
140
- pub fn impl_def ( & mut self , did : DefId ) -> stable_mir:: ty:: ImplDef {
141
- stable_mir:: ty:: ImplDef ( self . create_def_id ( did) )
142
- }
143
-
144
- pub fn region_def ( & mut self , did : DefId ) -> stable_mir:: ty:: RegionDef {
145
- stable_mir:: ty:: RegionDef ( self . create_def_id ( did) )
146
- }
147
-
148
- pub fn coroutine_witness_def ( & mut self , did : DefId ) -> stable_mir:: ty:: CoroutineWitnessDef {
149
- stable_mir:: ty:: CoroutineWitnessDef ( self . create_def_id ( did) )
150
- }
151
-
152
- pub fn assoc_def ( & mut self , did : DefId ) -> stable_mir:: ty:: AssocDef {
153
- stable_mir:: ty:: AssocDef ( self . create_def_id ( did) )
154
- }
155
-
156
- pub fn opaque_def ( & mut self , did : DefId ) -> stable_mir:: ty:: OpaqueDef {
157
- stable_mir:: ty:: OpaqueDef ( self . create_def_id ( did) )
158
- }
159
-
160
- pub fn prov ( & mut self , aid : AllocId ) -> stable_mir:: ty:: Prov {
161
- stable_mir:: ty:: Prov ( self . create_alloc_id ( aid) )
162
- }
163
-
164
- pub ( crate ) fn create_def_id ( & mut self , did : DefId ) -> stable_mir:: DefId {
73
+ impl < ' tcx , B : Bridge > Tables < ' tcx , B > {
74
+ pub fn create_def_id ( & mut self , did : DefId ) -> B :: DefId {
165
75
self . def_ids . create_or_fetch ( did)
166
76
}
167
77
168
- pub ( crate ) fn create_alloc_id ( & mut self , aid : AllocId ) -> stable_mir :: mir :: alloc :: AllocId {
78
+ pub fn create_alloc_id ( & mut self , aid : AllocId ) -> B :: AllocId {
169
79
self . alloc_ids . create_or_fetch ( aid)
170
80
}
171
81
172
- pub ( crate ) fn create_span ( & mut self , span : Span ) -> stable_mir :: ty :: Span {
82
+ pub fn create_span ( & mut self , span : Span ) -> B :: Span {
173
83
self . spans . create_or_fetch ( span)
174
84
}
175
85
176
- pub ( crate ) fn instance_def (
177
- & mut self ,
178
- instance : ty:: Instance < ' tcx > ,
179
- ) -> stable_mir:: mir:: mono:: InstanceDef {
86
+ pub fn instance_def ( & mut self , instance : ty:: Instance < ' tcx > ) -> B :: InstanceDef {
180
87
self . instances . create_or_fetch ( instance)
181
88
}
182
89
183
- pub ( crate ) fn static_def ( & mut self , did : DefId ) -> stable_mir:: mir:: mono:: StaticDef {
184
- stable_mir:: mir:: mono:: StaticDef ( self . create_def_id ( did) )
185
- }
186
-
187
- pub ( crate ) fn layout_id ( & mut self , layout : rustc_abi:: Layout < ' tcx > ) -> Layout {
90
+ pub fn layout_id ( & mut self , layout : rustc_abi:: Layout < ' tcx > ) -> B :: Layout {
188
91
self . layouts . create_or_fetch ( layout)
189
92
}
190
93
}
@@ -197,49 +100,39 @@ pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
197
100
// datastructures and stable MIR datastructures
198
101
scoped_thread_local ! ( static TLV : Cell <* const ( ) >) ;
199
102
200
- pub ( crate ) fn init < ' tcx , F , T > ( cx : & SmirCtxt < ' tcx > , f : F ) -> T
103
+ pub ( crate ) fn init < ' tcx , F , T , B : Bridge > ( container : & SmirContainer < ' tcx , B > , f : F ) -> T
201
104
where
202
105
F : FnOnce ( ) -> T ,
203
106
{
204
107
assert ! ( !TLV . is_set( ) ) ;
205
- let ptr = cx as * const _ as * const ( ) ;
108
+ let ptr = container as * const _ as * const ( ) ;
206
109
TLV . set ( & Cell :: new ( ptr) , || f ( ) )
207
110
}
208
111
209
112
/// Loads the current context and calls a function with it.
210
113
/// Do not nest these, as that will ICE.
211
- pub ( crate ) fn with_tables < R > ( f : impl for < ' tcx > FnOnce ( & mut Tables < ' tcx > ) -> R ) -> R {
114
+ pub ( crate ) fn with_container < ' tcx , R , B : Bridge > (
115
+ f : impl FnOnce ( & mut Tables < ' tcx , B > , & SmirCtxt < ' tcx , B > ) -> R ,
116
+ ) -> R {
212
117
assert ! ( TLV . is_set( ) ) ;
213
118
TLV . with ( |tlv| {
214
119
let ptr = tlv. get ( ) ;
215
120
assert ! ( !ptr. is_null( ) ) ;
216
- let context = ptr as * const SmirCtxt < ' _ > ;
217
- let mut tables = unsafe { ( * context) . 0 . borrow_mut ( ) } ;
218
- f ( & mut * tables)
121
+ let container = ptr as * const SmirContainer < ' tcx , B > ;
122
+ let mut tables = unsafe { ( * container) . tables . borrow_mut ( ) } ;
123
+ let cx = unsafe { ( * container) . cx . borrow ( ) } ;
124
+ f ( & mut * tables, & * cx)
219
125
} )
220
126
}
221
127
222
128
pub fn run < F , T > ( tcx : TyCtxt < ' _ > , f : F ) -> Result < T , Error >
223
129
where
224
130
F : FnOnce ( ) -> T ,
225
131
{
226
- let tables = SmirCtxt ( RefCell :: new ( Tables {
227
- tcx,
228
- def_ids : IndexMap :: default ( ) ,
229
- alloc_ids : IndexMap :: default ( ) ,
230
- spans : IndexMap :: default ( ) ,
231
- types : IndexMap :: default ( ) ,
232
- instances : IndexMap :: default ( ) ,
233
- ty_consts : IndexMap :: default ( ) ,
234
- mir_consts : IndexMap :: default ( ) ,
235
- layouts : IndexMap :: default ( ) ,
236
- } ) ) ;
237
-
238
- let interface = SmirInterface { cx : tables } ;
239
-
240
- // Pass the `SmirInterface` to compiler_interface::run
241
- // and initialize the rustc-specific TLS with tables.
242
- stable_mir:: compiler_interface:: run ( & interface, || init ( & interface. cx , f) )
132
+ let smir_cx = RefCell :: new ( SmirCtxt :: new ( tcx) ) ;
133
+ let container = SmirContainer { tables : RefCell :: new ( Tables :: new ( tcx) ) , cx : smir_cx } ;
134
+
135
+ stable_mir:: compiler_interface:: run ( & container, || init ( & container, f) )
243
136
}
244
137
245
138
/// Instantiate and run the compiler with the provided arguments and callback.
0 commit comments