5
5
6
6
use std:: cell:: Cell ;
7
7
8
+ use rustc_smir:: context:: SmirCtxt ;
8
9
use stable_mir:: abi:: { FnAbi , Layout , LayoutShape } ;
9
10
use stable_mir:: crate_def:: Attribute ;
10
11
use stable_mir:: mir:: alloc:: { AllocId , GlobalAlloc } ;
@@ -22,47 +23,116 @@ use stable_mir::{
22
23
ItemKind , Symbol , TraitDecls , mir,
23
24
} ;
24
25
25
- use crate :: stable_mir;
26
+ use crate :: { rustc_smir, stable_mir} ;
27
+
28
+ /// Stable public API for querying compiler information.
29
+ ///
30
+ /// All queries are delegated to an internal [`SmirCtxt`] that provides
31
+ /// similar APIs but based on internal rustc constructs.
32
+ ///
33
+ /// Do not use this directly. This is currently used in the macro expansion.
34
+ pub ( crate ) struct SmirInterface < ' tcx > {
35
+ pub ( crate ) cx : SmirCtxt < ' tcx > ,
36
+ }
37
+
38
+ impl < ' tcx > SmirInterface < ' tcx > {
39
+ pub ( crate ) fn entry_fn ( & self ) -> Option < CrateItem > {
40
+ self . cx . entry_fn ( )
41
+ }
26
42
27
- /// This trait defines the interface between stable_mir and the Rust compiler.
28
- /// Do not use this directly.
29
- pub trait Context {
30
- fn entry_fn ( & self ) -> Option < CrateItem > ;
31
43
/// Retrieve all items of the local crate that have a MIR associated with them.
32
- fn all_local_items ( & self ) -> CrateItems ;
44
+ pub ( crate ) fn all_local_items ( & self ) -> CrateItems {
45
+ self . cx . all_local_items ( )
46
+ }
47
+
33
48
/// Retrieve the body of a function.
34
49
/// This function will panic if the body is not available.
35
- fn mir_body ( & self , item : DefId ) -> mir:: Body ;
50
+ pub ( crate ) fn mir_body ( & self , item : DefId ) -> mir:: Body {
51
+ self . cx . mir_body ( item)
52
+ }
53
+
36
54
/// Check whether the body of a function is available.
37
- fn has_body ( & self , item : DefId ) -> bool ;
38
- fn foreign_modules ( & self , crate_num : CrateNum ) -> Vec < ForeignModuleDef > ;
55
+ pub ( crate ) fn has_body ( & self , item : DefId ) -> bool {
56
+ self . cx . has_body ( item)
57
+ }
58
+
59
+ pub ( crate ) fn foreign_modules ( & self , crate_num : CrateNum ) -> Vec < ForeignModuleDef > {
60
+ self . cx . foreign_modules ( crate_num)
61
+ }
39
62
40
63
/// Retrieve all functions defined in this crate.
41
- fn crate_functions ( & self , crate_num : CrateNum ) -> Vec < FnDef > ;
64
+ pub ( crate ) fn crate_functions ( & self , crate_num : CrateNum ) -> Vec < FnDef > {
65
+ self . cx . crate_functions ( crate_num)
66
+ }
42
67
43
68
/// Retrieve all static items defined in this crate.
44
- fn crate_statics ( & self , crate_num : CrateNum ) -> Vec < StaticDef > ;
45
- fn foreign_module ( & self , mod_def : ForeignModuleDef ) -> ForeignModule ;
46
- fn foreign_items ( & self , mod_def : ForeignModuleDef ) -> Vec < ForeignDef > ;
47
- fn all_trait_decls ( & self ) -> TraitDecls ;
48
- fn trait_decls ( & self , crate_num : CrateNum ) -> TraitDecls ;
49
- fn trait_decl ( & self , trait_def : & TraitDef ) -> TraitDecl ;
50
- fn all_trait_impls ( & self ) -> ImplTraitDecls ;
51
- fn trait_impls ( & self , crate_num : CrateNum ) -> ImplTraitDecls ;
52
- fn trait_impl ( & self , trait_impl : & ImplDef ) -> ImplTrait ;
53
- fn generics_of ( & self , def_id : DefId ) -> Generics ;
54
- fn predicates_of ( & self , def_id : DefId ) -> GenericPredicates ;
55
- fn explicit_predicates_of ( & self , def_id : DefId ) -> GenericPredicates ;
69
+ pub ( crate ) fn crate_statics ( & self , crate_num : CrateNum ) -> Vec < StaticDef > {
70
+ self . cx . crate_statics ( crate_num)
71
+ }
72
+
73
+ pub ( crate ) fn foreign_module ( & self , mod_def : ForeignModuleDef ) -> ForeignModule {
74
+ self . cx . foreign_module ( mod_def)
75
+ }
76
+
77
+ pub ( crate ) fn foreign_items ( & self , mod_def : ForeignModuleDef ) -> Vec < ForeignDef > {
78
+ self . cx . foreign_items ( mod_def)
79
+ }
80
+
81
+ pub ( crate ) fn all_trait_decls ( & self ) -> TraitDecls {
82
+ self . cx . all_trait_decls ( )
83
+ }
84
+
85
+ pub ( crate ) fn trait_decls ( & self , crate_num : CrateNum ) -> TraitDecls {
86
+ self . cx . trait_decls ( crate_num)
87
+ }
88
+
89
+ pub ( crate ) fn trait_decl ( & self , trait_def : & TraitDef ) -> TraitDecl {
90
+ self . cx . trait_decl ( trait_def)
91
+ }
92
+
93
+ pub ( crate ) fn all_trait_impls ( & self ) -> ImplTraitDecls {
94
+ self . cx . all_trait_impls ( )
95
+ }
96
+
97
+ pub ( crate ) fn trait_impls ( & self , crate_num : CrateNum ) -> ImplTraitDecls {
98
+ self . cx . trait_impls ( crate_num)
99
+ }
100
+
101
+ pub ( crate ) fn trait_impl ( & self , trait_impl : & ImplDef ) -> ImplTrait {
102
+ self . cx . trait_impl ( trait_impl)
103
+ }
104
+
105
+ pub ( crate ) fn generics_of ( & self , def_id : DefId ) -> Generics {
106
+ self . cx . generics_of ( def_id)
107
+ }
108
+
109
+ pub ( crate ) fn predicates_of ( & self , def_id : DefId ) -> GenericPredicates {
110
+ self . cx . predicates_of ( def_id)
111
+ }
112
+
113
+ pub ( crate ) fn explicit_predicates_of ( & self , def_id : DefId ) -> GenericPredicates {
114
+ self . cx . explicit_predicates_of ( def_id)
115
+ }
116
+
56
117
/// Get information about the local crate.
57
- fn local_crate ( & self ) -> Crate ;
118
+ pub ( crate ) fn local_crate ( & self ) -> Crate {
119
+ self . cx . local_crate ( )
120
+ }
121
+
58
122
/// Retrieve a list of all external crates.
59
- fn external_crates ( & self ) -> Vec < Crate > ;
123
+ pub ( crate ) fn external_crates ( & self ) -> Vec < Crate > {
124
+ self . cx . external_crates ( )
125
+ }
60
126
61
127
/// Find a crate with the given name.
62
- fn find_crates ( & self , name : & str ) -> Vec < Crate > ;
128
+ pub ( crate ) fn find_crates ( & self , name : & str ) -> Vec < Crate > {
129
+ self . cx . find_crates ( name)
130
+ }
63
131
64
- /// Returns the name of given `DefId`
65
- fn def_name ( & self , def_id : DefId , trimmed : bool ) -> Symbol ;
132
+ /// Returns the name of given `DefId`.
133
+ pub ( crate ) fn def_name ( & self , def_id : DefId , trimmed : bool ) -> Symbol {
134
+ self . cx . def_name ( def_id, trimmed)
135
+ }
66
136
67
137
/// Return registered tool attributes with the given attribute name.
68
138
///
@@ -71,218 +141,362 @@ pub trait Context {
71
141
///
72
142
/// Single segmented name like `#[clippy]` is specified as `&["clippy".to_string()]`.
73
143
/// Multi-segmented name like `#[rustfmt::skip]` is specified as `&["rustfmt".to_string(), "skip".to_string()]`.
74
- fn tool_attrs ( & self , def_id : DefId , attr : & [ Symbol ] ) -> Vec < Attribute > ;
144
+ pub ( crate ) fn tool_attrs ( & self , def_id : DefId , attr : & [ Symbol ] ) -> Vec < Attribute > {
145
+ self . cx . tool_attrs ( def_id, attr)
146
+ }
75
147
76
148
/// Get all tool attributes of a definition.
77
- fn all_tool_attrs ( & self , def_id : DefId ) -> Vec < Attribute > ;
149
+ pub ( crate ) fn all_tool_attrs ( & self , def_id : DefId ) -> Vec < Attribute > {
150
+ self . cx . all_tool_attrs ( def_id)
151
+ }
78
152
79
- /// Returns printable, human readable form of `Span`
80
- fn span_to_string ( & self , span : Span ) -> String ;
153
+ /// Returns printable, human readable form of `Span`.
154
+ pub ( crate ) fn span_to_string ( & self , span : Span ) -> String {
155
+ self . cx . span_to_string ( span)
156
+ }
81
157
82
- /// Return filename from given `Span`, for diagnostic purposes
83
- fn get_filename ( & self , span : & Span ) -> Filename ;
158
+ /// Return filename from given `Span`, for diagnostic purposes.
159
+ pub ( crate ) fn get_filename ( & self , span : & Span ) -> Filename {
160
+ self . cx . get_filename ( span)
161
+ }
84
162
85
- /// Return lines corresponding to this `Span`
86
- fn get_lines ( & self , span : & Span ) -> LineInfo ;
163
+ /// Return lines corresponding to this `Span`.
164
+ pub ( crate ) fn get_lines ( & self , span : & Span ) -> LineInfo {
165
+ self . cx . get_lines ( span)
166
+ }
87
167
88
- /// Returns the `kind` of given `DefId`
89
- fn item_kind ( & self , item : CrateItem ) -> ItemKind ;
168
+ /// Returns the `kind` of given `DefId`.
169
+ pub ( crate ) fn item_kind ( & self , item : CrateItem ) -> ItemKind {
170
+ self . cx . item_kind ( item)
171
+ }
90
172
91
173
/// Returns whether this is a foreign item.
92
- fn is_foreign_item ( & self , item : DefId ) -> bool ;
174
+ pub ( crate ) fn is_foreign_item ( & self , item : DefId ) -> bool {
175
+ self . cx . is_foreign_item ( item)
176
+ }
93
177
94
178
/// Returns the kind of a given foreign item.
95
- fn foreign_item_kind ( & self , def : ForeignDef ) -> ForeignItemKind ;
179
+ pub ( crate ) fn foreign_item_kind ( & self , def : ForeignDef ) -> ForeignItemKind {
180
+ self . cx . foreign_item_kind ( def)
181
+ }
96
182
97
- /// Returns the kind of a given algebraic data type
98
- fn adt_kind ( & self , def : AdtDef ) -> AdtKind ;
183
+ /// Returns the kind of a given algebraic data type.
184
+ pub ( crate ) fn adt_kind ( & self , def : AdtDef ) -> AdtKind {
185
+ self . cx . adt_kind ( def)
186
+ }
99
187
100
188
/// Returns if the ADT is a box.
101
- fn adt_is_box ( & self , def : AdtDef ) -> bool ;
189
+ pub ( crate ) fn adt_is_box ( & self , def : AdtDef ) -> bool {
190
+ self . cx . adt_is_box ( def)
191
+ }
102
192
103
193
/// Returns whether this ADT is simd.
104
- fn adt_is_simd ( & self , def : AdtDef ) -> bool ;
194
+ pub ( crate ) fn adt_is_simd ( & self , def : AdtDef ) -> bool {
195
+ self . cx . adt_is_simd ( def)
196
+ }
105
197
106
198
/// Returns whether this definition is a C string.
107
- fn adt_is_cstr ( & self , def : AdtDef ) -> bool ;
199
+ pub ( crate ) fn adt_is_cstr ( & self , def : AdtDef ) -> bool {
200
+ self . cx . adt_is_cstr ( def)
201
+ }
108
202
109
203
/// Retrieve the function signature for the given generic arguments.
110
- fn fn_sig ( & self , def : FnDef , args : & GenericArgs ) -> PolyFnSig ;
204
+ pub ( crate ) fn fn_sig ( & self , def : FnDef , args : & GenericArgs ) -> PolyFnSig {
205
+ self . cx . fn_sig ( def, args)
206
+ }
111
207
112
208
/// Retrieve the intrinsic definition if the item corresponds one.
113
- fn intrinsic ( & self , item : DefId ) -> Option < IntrinsicDef > ;
209
+ pub ( crate ) fn intrinsic ( & self , item : DefId ) -> Option < IntrinsicDef > {
210
+ self . cx . intrinsic ( item)
211
+ }
114
212
115
213
/// Retrieve the plain function name of an intrinsic.
116
- fn intrinsic_name ( & self , def : IntrinsicDef ) -> Symbol ;
214
+ pub ( crate ) fn intrinsic_name ( & self , def : IntrinsicDef ) -> Symbol {
215
+ self . cx . intrinsic_name ( def)
216
+ }
117
217
118
218
/// Retrieve the closure signature for the given generic arguments.
119
- fn closure_sig ( & self , args : & GenericArgs ) -> PolyFnSig ;
219
+ pub ( crate ) fn closure_sig ( & self , args : & GenericArgs ) -> PolyFnSig {
220
+ self . cx . closure_sig ( args)
221
+ }
120
222
121
223
/// The number of variants in this ADT.
122
- fn adt_variants_len ( & self , def : AdtDef ) -> usize ;
224
+ pub ( crate ) fn adt_variants_len ( & self , def : AdtDef ) -> usize {
225
+ self . cx . adt_variants_len ( def)
226
+ }
123
227
124
228
/// The name of a variant.
125
- fn variant_name ( & self , def : VariantDef ) -> Symbol ;
126
- fn variant_fields ( & self , def : VariantDef ) -> Vec < FieldDef > ;
229
+ pub ( crate ) fn variant_name ( & self , def : VariantDef ) -> Symbol {
230
+ self . cx . variant_name ( def)
231
+ }
232
+
233
+ pub ( crate ) fn variant_fields ( & self , def : VariantDef ) -> Vec < FieldDef > {
234
+ self . cx . variant_fields ( def)
235
+ }
127
236
128
237
/// Evaluate constant as a target usize.
129
- fn eval_target_usize ( & self , cnst : & MirConst ) -> Result < u64 , Error > ;
130
- fn eval_target_usize_ty ( & self , cnst : & TyConst ) -> Result < u64 , Error > ;
238
+ pub ( crate ) fn eval_target_usize ( & self , cnst : & MirConst ) -> Result < u64 , Error > {
239
+ self . cx . eval_target_usize ( cnst)
240
+ }
241
+
242
+ pub ( crate ) fn eval_target_usize_ty ( & self , cnst : & TyConst ) -> Result < u64 , Error > {
243
+ self . cx . eval_target_usize_ty ( cnst)
244
+ }
131
245
132
246
/// Create a new zero-sized constant.
133
- fn try_new_const_zst ( & self , ty : Ty ) -> Result < MirConst , Error > ;
247
+ pub ( crate ) fn try_new_const_zst ( & self , ty : Ty ) -> Result < MirConst , Error > {
248
+ self . cx . try_new_const_zst ( ty)
249
+ }
134
250
135
251
/// Create a new constant that represents the given string value.
136
- fn new_const_str ( & self , value : & str ) -> MirConst ;
252
+ pub ( crate ) fn new_const_str ( & self , value : & str ) -> MirConst {
253
+ self . cx . new_const_str ( value)
254
+ }
137
255
138
256
/// Create a new constant that represents the given boolean value.
139
- fn new_const_bool ( & self , value : bool ) -> MirConst ;
257
+ pub ( crate ) fn new_const_bool ( & self , value : bool ) -> MirConst {
258
+ self . cx . new_const_bool ( value)
259
+ }
140
260
141
261
/// Create a new constant that represents the given value.
142
- fn try_new_const_uint ( & self , value : u128 , uint_ty : UintTy ) -> Result < MirConst , Error > ;
143
- fn try_new_ty_const_uint ( & self , value : u128 , uint_ty : UintTy ) -> Result < TyConst , Error > ;
262
+ pub ( crate ) fn try_new_const_uint (
263
+ & self ,
264
+ value : u128 ,
265
+ uint_ty : UintTy ,
266
+ ) -> Result < MirConst , Error > {
267
+ self . cx . try_new_const_uint ( value, uint_ty)
268
+ }
269
+
270
+ pub ( crate ) fn try_new_ty_const_uint (
271
+ & self ,
272
+ value : u128 ,
273
+ uint_ty : UintTy ,
274
+ ) -> Result < TyConst , Error > {
275
+ self . cx . try_new_ty_const_uint ( value, uint_ty)
276
+ }
144
277
145
278
/// Create a new type from the given kind.
146
- fn new_rigid_ty ( & self , kind : RigidTy ) -> Ty ;
279
+ pub ( crate ) fn new_rigid_ty ( & self , kind : RigidTy ) -> Ty {
280
+ self . cx . new_rigid_ty ( kind)
281
+ }
147
282
148
283
/// Create a new box type, `Box<T>`, for the given inner type `T`.
149
- fn new_box_ty ( & self , ty : Ty ) -> Ty ;
284
+ pub ( crate ) fn new_box_ty ( & self , ty : Ty ) -> Ty {
285
+ self . cx . new_box_ty ( ty)
286
+ }
150
287
151
288
/// Returns the type of given crate item.
152
- fn def_ty ( & self , item : DefId ) -> Ty ;
289
+ pub ( crate ) fn def_ty ( & self , item : DefId ) -> Ty {
290
+ self . cx . def_ty ( item)
291
+ }
153
292
154
293
/// Returns the type of given definition instantiated with the given arguments.
155
- fn def_ty_with_args ( & self , item : DefId , args : & GenericArgs ) -> Ty ;
294
+ pub ( crate ) fn def_ty_with_args ( & self , item : DefId , args : & GenericArgs ) -> Ty {
295
+ self . cx . def_ty_with_args ( item, args)
296
+ }
156
297
157
298
/// Returns literal value of a const as a string.
158
- fn mir_const_pretty ( & self , cnst : & MirConst ) -> String ;
299
+ pub ( crate ) fn mir_const_pretty ( & self , cnst : & MirConst ) -> String {
300
+ self . cx . mir_const_pretty ( cnst)
301
+ }
159
302
160
- /// `Span` of an item
161
- fn span_of_an_item ( & self , def_id : DefId ) -> Span ;
303
+ /// `Span` of an item.
304
+ pub ( crate ) fn span_of_an_item ( & self , def_id : DefId ) -> Span {
305
+ self . cx . span_of_an_item ( def_id)
306
+ }
162
307
163
- fn ty_const_pretty ( & self , ct : TyConstId ) -> String ;
308
+ pub ( crate ) fn ty_const_pretty ( & self , ct : TyConstId ) -> String {
309
+ self . cx . ty_const_pretty ( ct)
310
+ }
164
311
165
312
/// Obtain the representation of a type.
166
- fn ty_pretty ( & self , ty : Ty ) -> String ;
313
+ pub ( crate ) fn ty_pretty ( & self , ty : Ty ) -> String {
314
+ self . cx . ty_pretty ( ty)
315
+ }
167
316
168
317
/// Obtain the representation of a type.
169
- fn ty_kind ( & self , ty : Ty ) -> TyKind ;
318
+ pub ( crate ) fn ty_kind ( & self , ty : Ty ) -> TyKind {
319
+ self . cx . ty_kind ( ty)
320
+ }
170
321
171
- // Get the discriminant Ty for this Ty if there's one.
172
- fn rigid_ty_discriminant_ty ( & self , ty : & RigidTy ) -> Ty ;
322
+ /// Get the discriminant Ty for this Ty if there's one.
323
+ pub ( crate ) fn rigid_ty_discriminant_ty ( & self , ty : & RigidTy ) -> Ty {
324
+ self . cx . rigid_ty_discriminant_ty ( ty)
325
+ }
173
326
174
327
/// Get the body of an Instance which is already monomorphized.
175
- fn instance_body ( & self , instance : InstanceDef ) -> Option < Body > ;
328
+ pub ( crate ) fn instance_body ( & self , instance : InstanceDef ) -> Option < Body > {
329
+ self . cx . instance_body ( instance)
330
+ }
176
331
177
332
/// Get the instance type with generic instantiations applied and lifetimes erased.
178
- fn instance_ty ( & self , instance : InstanceDef ) -> Ty ;
333
+ pub ( crate ) fn instance_ty ( & self , instance : InstanceDef ) -> Ty {
334
+ self . cx . instance_ty ( instance)
335
+ }
179
336
180
337
/// Get the instantiation types.
181
- fn instance_args ( & self , def : InstanceDef ) -> GenericArgs ;
338
+ pub ( crate ) fn instance_args ( & self , def : InstanceDef ) -> GenericArgs {
339
+ self . cx . instance_args ( def)
340
+ }
182
341
183
342
/// Get the instance.
184
- fn instance_def_id ( & self , instance : InstanceDef ) -> DefId ;
343
+ pub ( crate ) fn instance_def_id ( & self , instance : InstanceDef ) -> DefId {
344
+ self . cx . instance_def_id ( instance)
345
+ }
185
346
186
347
/// Get the instance mangled name.
187
- fn instance_mangled_name ( & self , instance : InstanceDef ) -> Symbol ;
348
+ pub ( crate ) fn instance_mangled_name ( & self , instance : InstanceDef ) -> Symbol {
349
+ self . cx . instance_mangled_name ( instance)
350
+ }
188
351
189
352
/// Check if this is an empty DropGlue shim.
190
- fn is_empty_drop_shim ( & self , def : InstanceDef ) -> bool ;
353
+ pub ( crate ) fn is_empty_drop_shim ( & self , def : InstanceDef ) -> bool {
354
+ self . cx . is_empty_drop_shim ( def)
355
+ }
191
356
192
357
/// Check if this is an empty AsyncDropGlueCtor shim.
193
- fn is_empty_async_drop_ctor_shim ( & self , def : InstanceDef ) -> bool ;
358
+ pub ( crate ) fn is_empty_async_drop_ctor_shim ( & self , def : InstanceDef ) -> bool {
359
+ self . cx . is_empty_async_drop_ctor_shim ( def)
360
+ }
194
361
195
362
/// Convert a non-generic crate item into an instance.
196
363
/// This function will panic if the item is generic.
197
- fn mono_instance ( & self , def_id : DefId ) -> Instance ;
364
+ pub ( crate ) fn mono_instance ( & self , def_id : DefId ) -> Instance {
365
+ self . cx . mono_instance ( def_id)
366
+ }
198
367
199
368
/// Item requires monomorphization.
200
- fn requires_monomorphization ( & self , def_id : DefId ) -> bool ;
369
+ pub ( crate ) fn requires_monomorphization ( & self , def_id : DefId ) -> bool {
370
+ self . cx . requires_monomorphization ( def_id)
371
+ }
201
372
202
373
/// Resolve an instance from the given function definition and generic arguments.
203
- fn resolve_instance ( & self , def : FnDef , args : & GenericArgs ) -> Option < Instance > ;
374
+ pub ( crate ) fn resolve_instance ( & self , def : FnDef , args : & GenericArgs ) -> Option < Instance > {
375
+ self . cx . resolve_instance ( def, args)
376
+ }
204
377
205
378
/// Resolve an instance for drop_in_place for the given type.
206
- fn resolve_drop_in_place ( & self , ty : Ty ) -> Instance ;
379
+ pub ( crate ) fn resolve_drop_in_place ( & self , ty : Ty ) -> Instance {
380
+ self . cx . resolve_drop_in_place ( ty)
381
+ }
207
382
208
383
/// Resolve instance for a function pointer.
209
- fn resolve_for_fn_ptr ( & self , def : FnDef , args : & GenericArgs ) -> Option < Instance > ;
384
+ pub ( crate ) fn resolve_for_fn_ptr ( & self , def : FnDef , args : & GenericArgs ) -> Option < Instance > {
385
+ self . cx . resolve_for_fn_ptr ( def, args)
386
+ }
210
387
211
388
/// Resolve instance for a closure with the requested type.
212
- fn resolve_closure (
389
+ pub ( crate ) fn resolve_closure (
213
390
& self ,
214
391
def : ClosureDef ,
215
392
args : & GenericArgs ,
216
393
kind : ClosureKind ,
217
- ) -> Option < Instance > ;
394
+ ) -> Option < Instance > {
395
+ self . cx . resolve_closure ( def, args, kind)
396
+ }
218
397
219
398
/// Evaluate a static's initializer.
220
- fn eval_static_initializer ( & self , def : StaticDef ) -> Result < Allocation , Error > ;
399
+ pub ( crate ) fn eval_static_initializer ( & self , def : StaticDef ) -> Result < Allocation , Error > {
400
+ self . cx . eval_static_initializer ( def)
401
+ }
221
402
222
403
/// Try to evaluate an instance into a constant.
223
- fn eval_instance ( & self , def : InstanceDef , const_ty : Ty ) -> Result < Allocation , Error > ;
404
+ pub ( crate ) fn eval_instance (
405
+ & self ,
406
+ def : InstanceDef ,
407
+ const_ty : Ty ,
408
+ ) -> Result < Allocation , Error > {
409
+ self . cx . eval_instance ( def, const_ty)
410
+ }
224
411
225
412
/// Retrieve global allocation for the given allocation ID.
226
- fn global_alloc ( & self , id : AllocId ) -> GlobalAlloc ;
413
+ pub ( crate ) fn global_alloc ( & self , id : AllocId ) -> GlobalAlloc {
414
+ self . cx . global_alloc ( id)
415
+ }
227
416
228
417
/// Retrieve the id for the virtual table.
229
- fn vtable_allocation ( & self , global_alloc : & GlobalAlloc ) -> Option < AllocId > ;
230
- fn krate ( & self , def_id : DefId ) -> Crate ;
231
- fn instance_name ( & self , def : InstanceDef , trimmed : bool ) -> Symbol ;
418
+ pub ( crate ) fn vtable_allocation ( & self , global_alloc : & GlobalAlloc ) -> Option < AllocId > {
419
+ self . cx . vtable_allocation ( global_alloc)
420
+ }
421
+
422
+ pub ( crate ) fn krate ( & self , def_id : DefId ) -> Crate {
423
+ self . cx . krate ( def_id)
424
+ }
425
+
426
+ pub ( crate ) fn instance_name ( & self , def : InstanceDef , trimmed : bool ) -> Symbol {
427
+ self . cx . instance_name ( def, trimmed)
428
+ }
232
429
233
430
/// Return information about the target machine.
234
- fn target_info ( & self ) -> MachineInfo ;
431
+ pub ( crate ) fn target_info ( & self ) -> MachineInfo {
432
+ self . cx . target_info ( )
433
+ }
235
434
236
435
/// Get an instance ABI.
237
- fn instance_abi ( & self , def : InstanceDef ) -> Result < FnAbi , Error > ;
436
+ pub ( crate ) fn instance_abi ( & self , def : InstanceDef ) -> Result < FnAbi , Error > {
437
+ self . cx . instance_abi ( def)
438
+ }
238
439
239
440
/// Get the ABI of a function pointer.
240
- fn fn_ptr_abi ( & self , fn_ptr : PolyFnSig ) -> Result < FnAbi , Error > ;
441
+ pub ( crate ) fn fn_ptr_abi ( & self , fn_ptr : PolyFnSig ) -> Result < FnAbi , Error > {
442
+ self . cx . fn_ptr_abi ( fn_ptr)
443
+ }
241
444
242
445
/// Get the layout of a type.
243
- fn ty_layout ( & self , ty : Ty ) -> Result < Layout , Error > ;
446
+ pub ( crate ) fn ty_layout ( & self , ty : Ty ) -> Result < Layout , Error > {
447
+ self . cx . ty_layout ( ty)
448
+ }
244
449
245
450
/// Get the layout shape.
246
- fn layout_shape ( & self , id : Layout ) -> LayoutShape ;
451
+ pub ( crate ) fn layout_shape ( & self , id : Layout ) -> LayoutShape {
452
+ self . cx . layout_shape ( id)
453
+ }
247
454
248
455
/// Get a debug string representation of a place.
249
- fn place_pretty ( & self , place : & Place ) -> String ;
456
+ pub ( crate ) fn place_pretty ( & self , place : & Place ) -> String {
457
+ self . cx . place_pretty ( place)
458
+ }
250
459
251
460
/// Get the resulting type of binary operation.
252
- fn binop_ty ( & self , bin_op : BinOp , rhs : Ty , lhs : Ty ) -> Ty ;
461
+ pub ( crate ) fn binop_ty ( & self , bin_op : BinOp , rhs : Ty , lhs : Ty ) -> Ty {
462
+ self . cx . binop_ty ( bin_op, rhs, lhs)
463
+ }
253
464
254
465
/// Get the resulting type of unary operation.
255
- fn unop_ty ( & self , un_op : UnOp , arg : Ty ) -> Ty ;
466
+ pub ( crate ) fn unop_ty ( & self , un_op : UnOp , arg : Ty ) -> Ty {
467
+ self . cx . unop_ty ( un_op, arg)
468
+ }
256
469
257
470
/// Get all associated items of a definition.
258
- fn associated_items ( & self , def_id : DefId ) -> AssocItems ;
471
+ pub ( crate ) fn associated_items ( & self , def_id : DefId ) -> AssocItems {
472
+ self . cx . associated_items ( def_id)
473
+ }
259
474
}
260
475
261
- // A thread local variable that stores a pointer to the tables mapping between TyCtxt
262
- // datastructures and stable MIR datastructures
476
+ // A thread local variable that stores a pointer to [`SmirInterface`].
263
477
scoped_tls:: scoped_thread_local!( static TLV : Cell <* const ( ) >) ;
264
478
265
- pub fn run < F , T > ( context : & dyn Context , f : F ) -> Result < T , Error >
479
+ pub ( crate ) fn run < ' tcx , T , F > ( interface : & SmirInterface < ' tcx > , f : F ) -> Result < T , Error >
266
480
where
267
481
F : FnOnce ( ) -> T ,
268
482
{
269
483
if TLV . is_set ( ) {
270
484
Err ( Error :: from ( "StableMIR already running" ) )
271
485
} else {
272
- let ptr: * const ( ) = ( & raw const context ) as _ ;
486
+ let ptr: * const ( ) = ( interface as * const SmirInterface < ' tcx > ) as * const ( ) ;
273
487
TLV . set ( & Cell :: new ( ptr) , || Ok ( f ( ) ) )
274
488
}
275
489
}
276
490
277
- /// Execute the given function with access the compiler [Context ].
491
+ /// Execute the given function with access the [`SmirInterface` ].
278
492
///
279
- /// I.e., This function will load the current context and calls a function with it.
493
+ /// I.e., This function will load the current interface and calls a function with it.
280
494
/// Do not nest these, as that will ICE.
281
- pub ( crate ) fn with < R > ( f : impl FnOnce ( & dyn Context ) -> R ) -> R {
495
+ pub ( crate ) fn with < R > ( f : impl FnOnce ( & SmirInterface < ' _ > ) -> R ) -> R {
282
496
assert ! ( TLV . is_set( ) ) ;
283
497
TLV . with ( |tlv| {
284
498
let ptr = tlv. get ( ) ;
285
499
assert ! ( !ptr. is_null( ) ) ;
286
- f ( unsafe { * ( ptr as * const & dyn Context ) } )
500
+ f ( unsafe { & * ( ptr as * const SmirInterface < ' _ > ) } )
287
501
} )
288
502
}
0 commit comments