1use std::cell::Cell;
7
8use rustc_smir::context::SmirCtxt;
9use stable_mir::abi::{FnAbi, Layout, LayoutShape, ReprOptions};
10use stable_mir::crate_def::Attribute;
11use stable_mir::mir::alloc::{AllocId, GlobalAlloc};
12use stable_mir::mir::mono::{Instance, InstanceDef, StaticDef};
13use stable_mir::mir::{BinOp, Body, Place, UnOp};
14use stable_mir::target::MachineInfo;
15use stable_mir::ty::{
16 AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, CoroutineDef, Discr, FieldDef, FnDef,
17 ForeignDef, ForeignItemKind, ForeignModule, ForeignModuleDef, GenericArgs, GenericPredicates,
18 Generics, ImplDef, ImplTrait, IntrinsicDef, LineInfo, MirConst, PolyFnSig, RigidTy, Span,
19 TraitDecl, TraitDef, Ty, TyConst, TyConstId, TyKind, UintTy, VariantDef, VariantIdx,
20};
21use stable_mir::{
22 AssocItems, Crate, CrateItem, CrateItems, CrateNum, DefId, Error, Filename, ImplTraitDecls,
23 ItemKind, Symbol, TraitDecls, mir,
24};
25
26use crate::{rustc_smir, stable_mir};
27
28pub(crate) struct SmirInterface<'tcx> {
35 pub(crate) cx: SmirCtxt<'tcx>,
36}
37
38impl<'tcx> SmirInterface<'tcx> {
39 pub(crate) fn entry_fn(&self) -> Option<CrateItem> {
40 self.cx.entry_fn()
41 }
42
43 pub(crate) fn all_local_items(&self) -> CrateItems {
45 self.cx.all_local_items()
46 }
47
48 pub(crate) fn mir_body(&self, item: DefId) -> mir::Body {
51 self.cx.mir_body(item)
52 }
53
54 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 }
62
63 pub(crate) fn crate_functions(&self, crate_num: CrateNum) -> Vec<FnDef> {
65 self.cx.crate_functions(crate_num)
66 }
67
68 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
117 pub(crate) fn local_crate(&self) -> Crate {
119 self.cx.local_crate()
120 }
121
122 pub(crate) fn external_crates(&self) -> Vec<Crate> {
124 self.cx.external_crates()
125 }
126
127 pub(crate) fn find_crates(&self, name: &str) -> Vec<Crate> {
129 self.cx.find_crates(name)
130 }
131
132 pub(crate) fn def_name(&self, def_id: DefId, trimmed: bool) -> Symbol {
134 self.cx.def_name(def_id, trimmed)
135 }
136
137 pub(crate) fn tool_attrs(&self, def_id: DefId, attr: &[Symbol]) -> Vec<Attribute> {
145 self.cx.tool_attrs(def_id, attr)
146 }
147
148 pub(crate) fn all_tool_attrs(&self, def_id: DefId) -> Vec<Attribute> {
150 self.cx.all_tool_attrs(def_id)
151 }
152
153 pub(crate) fn span_to_string(&self, span: Span) -> String {
155 self.cx.span_to_string(span)
156 }
157
158 pub(crate) fn get_filename(&self, span: &Span) -> Filename {
160 self.cx.get_filename(span)
161 }
162
163 pub(crate) fn get_lines(&self, span: &Span) -> LineInfo {
165 self.cx.get_lines(span)
166 }
167
168 pub(crate) fn item_kind(&self, item: CrateItem) -> ItemKind {
170 self.cx.item_kind(item)
171 }
172
173 pub(crate) fn is_foreign_item(&self, item: DefId) -> bool {
175 self.cx.is_foreign_item(item)
176 }
177
178 pub(crate) fn foreign_item_kind(&self, def: ForeignDef) -> ForeignItemKind {
180 self.cx.foreign_item_kind(def)
181 }
182
183 pub(crate) fn adt_kind(&self, def: AdtDef) -> AdtKind {
185 self.cx.adt_kind(def)
186 }
187
188 pub(crate) fn adt_is_box(&self, def: AdtDef) -> bool {
190 self.cx.adt_is_box(def)
191 }
192
193 pub(crate) fn adt_is_simd(&self, def: AdtDef) -> bool {
195 self.cx.adt_is_simd(def)
196 }
197
198 pub(crate) fn adt_is_cstr(&self, def: AdtDef) -> bool {
200 self.cx.adt_is_cstr(def)
201 }
202
203 pub(crate) fn adt_repr(&self, def: AdtDef) -> ReprOptions {
205 self.cx.adt_repr(def)
206 }
207
208 pub(crate) fn fn_sig(&self, def: FnDef, args: &GenericArgs) -> PolyFnSig {
210 self.cx.fn_sig(def, args)
211 }
212
213 pub(crate) fn intrinsic(&self, item: DefId) -> Option<IntrinsicDef> {
215 self.cx.intrinsic(item)
216 }
217
218 pub(crate) fn intrinsic_name(&self, def: IntrinsicDef) -> Symbol {
220 self.cx.intrinsic_name(def)
221 }
222
223 pub(crate) fn closure_sig(&self, args: &GenericArgs) -> PolyFnSig {
225 self.cx.closure_sig(args)
226 }
227
228 pub(crate) fn adt_variants_len(&self, def: AdtDef) -> usize {
230 self.cx.adt_variants_len(def)
231 }
232
233 pub(crate) fn adt_discr_for_variant(&self, adt: AdtDef, variant: VariantIdx) -> Discr {
235 self.cx.adt_discr_for_variant(adt, variant)
236 }
237
238 pub(crate) fn coroutine_discr_for_variant(
240 &self,
241 coroutine: CoroutineDef,
242 args: &GenericArgs,
243 variant: VariantIdx,
244 ) -> Discr {
245 self.cx.coroutine_discr_for_variant(coroutine, args, variant)
246 }
247
248 pub(crate) fn variant_name(&self, def: VariantDef) -> Symbol {
250 self.cx.variant_name(def)
251 }
252
253 pub(crate) fn variant_fields(&self, def: VariantDef) -> Vec<FieldDef> {
254 self.cx.variant_fields(def)
255 }
256
257 pub(crate) fn eval_target_usize(&self, cnst: &MirConst) -> Result<u64, Error> {
259 self.cx.eval_target_usize(cnst)
260 }
261
262 pub(crate) fn eval_target_usize_ty(&self, cnst: &TyConst) -> Result<u64, Error> {
263 self.cx.eval_target_usize_ty(cnst)
264 }
265
266 pub(crate) fn try_new_const_zst(&self, ty: Ty) -> Result<MirConst, Error> {
268 self.cx.try_new_const_zst(ty)
269 }
270
271 pub(crate) fn new_const_str(&self, value: &str) -> MirConst {
273 self.cx.new_const_str(value)
274 }
275
276 pub(crate) fn new_const_bool(&self, value: bool) -> MirConst {
278 self.cx.new_const_bool(value)
279 }
280
281 pub(crate) fn try_new_const_uint(
283 &self,
284 value: u128,
285 uint_ty: UintTy,
286 ) -> Result<MirConst, Error> {
287 self.cx.try_new_const_uint(value, uint_ty)
288 }
289
290 pub(crate) fn try_new_ty_const_uint(
291 &self,
292 value: u128,
293 uint_ty: UintTy,
294 ) -> Result<TyConst, Error> {
295 self.cx.try_new_ty_const_uint(value, uint_ty)
296 }
297
298 pub(crate) fn new_rigid_ty(&self, kind: RigidTy) -> Ty {
300 self.cx.new_rigid_ty(kind)
301 }
302
303 pub(crate) fn new_box_ty(&self, ty: Ty) -> Ty {
305 self.cx.new_box_ty(ty)
306 }
307
308 pub(crate) fn def_ty(&self, item: DefId) -> Ty {
310 self.cx.def_ty(item)
311 }
312
313 pub(crate) fn def_ty_with_args(&self, item: DefId, args: &GenericArgs) -> Ty {
315 self.cx.def_ty_with_args(item, args)
316 }
317
318 pub(crate) fn mir_const_pretty(&self, cnst: &MirConst) -> String {
320 self.cx.mir_const_pretty(cnst)
321 }
322
323 pub(crate) fn span_of_an_item(&self, def_id: DefId) -> Span {
325 self.cx.span_of_an_item(def_id)
326 }
327
328 pub(crate) fn ty_const_pretty(&self, ct: TyConstId) -> String {
329 self.cx.ty_const_pretty(ct)
330 }
331
332 pub(crate) fn ty_pretty(&self, ty: Ty) -> String {
334 self.cx.ty_pretty(ty)
335 }
336
337 pub(crate) fn ty_kind(&self, ty: Ty) -> TyKind {
339 self.cx.ty_kind(ty)
340 }
341
342 pub(crate) fn rigid_ty_discriminant_ty(&self, ty: &RigidTy) -> Ty {
344 self.cx.rigid_ty_discriminant_ty(ty)
345 }
346
347 pub(crate) fn instance_body(&self, instance: InstanceDef) -> Option<Body> {
349 self.cx.instance_body(instance)
350 }
351
352 pub(crate) fn instance_ty(&self, instance: InstanceDef) -> Ty {
354 self.cx.instance_ty(instance)
355 }
356
357 pub(crate) fn instance_args(&self, def: InstanceDef) -> GenericArgs {
359 self.cx.instance_args(def)
360 }
361
362 pub(crate) fn instance_def_id(&self, instance: InstanceDef) -> DefId {
364 self.cx.instance_def_id(instance)
365 }
366
367 pub(crate) fn instance_mangled_name(&self, instance: InstanceDef) -> Symbol {
369 self.cx.instance_mangled_name(instance)
370 }
371
372 pub(crate) fn is_empty_drop_shim(&self, def: InstanceDef) -> bool {
374 self.cx.is_empty_drop_shim(def)
375 }
376
377 pub(crate) fn mono_instance(&self, def_id: DefId) -> Instance {
380 self.cx.mono_instance(def_id)
381 }
382
383 pub(crate) fn requires_monomorphization(&self, def_id: DefId) -> bool {
385 self.cx.requires_monomorphization(def_id)
386 }
387
388 pub(crate) fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance> {
390 self.cx.resolve_instance(def, args)
391 }
392
393 pub(crate) fn resolve_drop_in_place(&self, ty: Ty) -> Instance {
395 self.cx.resolve_drop_in_place(ty)
396 }
397
398 pub(crate) fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance> {
400 self.cx.resolve_for_fn_ptr(def, args)
401 }
402
403 pub(crate) fn resolve_closure(
405 &self,
406 def: ClosureDef,
407 args: &GenericArgs,
408 kind: ClosureKind,
409 ) -> Option<Instance> {
410 self.cx.resolve_closure(def, args, kind)
411 }
412
413 pub(crate) fn eval_static_initializer(&self, def: StaticDef) -> Result<Allocation, Error> {
415 self.cx.eval_static_initializer(def)
416 }
417
418 pub(crate) fn eval_instance(
420 &self,
421 def: InstanceDef,
422 const_ty: Ty,
423 ) -> Result<Allocation, Error> {
424 self.cx.eval_instance(def, const_ty)
425 }
426
427 pub(crate) fn global_alloc(&self, id: AllocId) -> GlobalAlloc {
429 self.cx.global_alloc(id)
430 }
431
432 pub(crate) fn vtable_allocation(&self, global_alloc: &GlobalAlloc) -> Option<AllocId> {
434 self.cx.vtable_allocation(global_alloc)
435 }
436
437 pub(crate) fn krate(&self, def_id: DefId) -> Crate {
438 self.cx.krate(def_id)
439 }
440
441 pub(crate) fn instance_name(&self, def: InstanceDef, trimmed: bool) -> Symbol {
442 self.cx.instance_name(def, trimmed)
443 }
444
445 pub(crate) fn target_info(&self) -> MachineInfo {
447 self.cx.target_info()
448 }
449
450 pub(crate) fn instance_abi(&self, def: InstanceDef) -> Result<FnAbi, Error> {
452 self.cx.instance_abi(def)
453 }
454
455 pub(crate) fn fn_ptr_abi(&self, fn_ptr: PolyFnSig) -> Result<FnAbi, Error> {
457 self.cx.fn_ptr_abi(fn_ptr)
458 }
459
460 pub(crate) fn ty_layout(&self, ty: Ty) -> Result<Layout, Error> {
462 self.cx.ty_layout(ty)
463 }
464
465 pub(crate) fn layout_shape(&self, id: Layout) -> LayoutShape {
467 self.cx.layout_shape(id)
468 }
469
470 pub(crate) fn place_pretty(&self, place: &Place) -> String {
472 self.cx.place_pretty(place)
473 }
474
475 pub(crate) fn binop_ty(&self, bin_op: BinOp, rhs: Ty, lhs: Ty) -> Ty {
477 self.cx.binop_ty(bin_op, rhs, lhs)
478 }
479
480 pub(crate) fn unop_ty(&self, un_op: UnOp, arg: Ty) -> Ty {
482 self.cx.unop_ty(un_op, arg)
483 }
484
485 pub(crate) fn associated_items(&self, def_id: DefId) -> AssocItems {
487 self.cx.associated_items(def_id)
488 }
489}
490
491scoped_tls::scoped_thread_local!(static TLV: Cell<*const ()>);
493
494pub(crate) fn run<'tcx, T, F>(interface: &SmirInterface<'tcx>, f: F) -> Result<T, Error>
495where
496 F: FnOnce() -> T,
497{
498 if TLV.is_set() {
499 Err(Error::from("StableMIR already running"))
500 } else {
501 let ptr: *const () = (interface as *const SmirInterface<'tcx>) as *const ();
502 TLV.set(&Cell::new(ptr), || Ok(f()))
503 }
504}
505
506pub(crate) fn with<R>(f: impl FnOnce(&SmirInterface<'_>) -> R) -> R {
511 assert!(TLV.is_set());
512 TLV.with(|tlv| {
513 let ptr = tlv.get();
514 assert!(!ptr.is_null());
515 f(unsafe { &*(ptr as *const SmirInterface<'_>) })
516 })
517}