Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2a07f99

Browse files
authoredApr 24, 2025
Rollup merge of #139852 - makai410:smir-refactor, r=celinval
StableMIR: Implement `CompilerInterface` This PR implements part of [the document](https://hackmd.io/``@celinaval/H1lJBGse0).`` With `TablesWrapper` wrapped by `CompilerInterface`, the stable-mir's TLV stores a pointer to `CompilerInterface`, while the rustc-specific TLV stores a pointer to tables.
2 parents 32b2428 + 5b390cd commit 2a07f99

File tree

5 files changed

+511
-211
lines changed

5 files changed

+511
-211
lines changed
 

‎compiler/rustc_smir/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
pub mod rustc_internal;
2121

22-
// Make this module private for now since external users should not call these directly.
23-
mod rustc_smir;
22+
pub mod rustc_smir;
2423

2524
pub mod stable_mir;

‎compiler/rustc_smir/src/rustc_internal/mod.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ use rustc_span::def_id::{CrateNum, DefId};
1818
use scoped_tls::scoped_thread_local;
1919
use stable_mir::Error;
2020
use stable_mir::abi::Layout;
21+
use stable_mir::compiler_interface::SmirInterface;
2122
use stable_mir::ty::IndexedVal;
2223

23-
use crate::rustc_smir::context::TablesWrapper;
24+
use crate::rustc_smir::context::SmirCtxt;
2425
use crate::rustc_smir::{Stable, Tables};
2526
use crate::stable_mir;
2627

@@ -196,12 +197,12 @@ pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
196197
// datastructures and stable MIR datastructures
197198
scoped_thread_local! (static TLV: Cell<*const ()>);
198199

199-
pub(crate) fn init<'tcx, F, T>(tables: &TablesWrapper<'tcx>, f: F) -> T
200+
pub(crate) fn init<'tcx, F, T>(cx: &SmirCtxt<'tcx>, f: F) -> T
200201
where
201202
F: FnOnce() -> T,
202203
{
203204
assert!(!TLV.is_set());
204-
let ptr = tables as *const _ as *const ();
205+
let ptr = cx as *const _ as *const ();
205206
TLV.set(&Cell::new(ptr), || f())
206207
}
207208

@@ -212,8 +213,8 @@ pub(crate) fn with_tables<R>(f: impl for<'tcx> FnOnce(&mut Tables<'tcx>) -> R) -
212213
TLV.with(|tlv| {
213214
let ptr = tlv.get();
214215
assert!(!ptr.is_null());
215-
let wrapper = ptr as *const TablesWrapper<'_>;
216-
let mut tables = unsafe { (*wrapper).0.borrow_mut() };
216+
let context = ptr as *const SmirCtxt<'_>;
217+
let mut tables = unsafe { (*context).0.borrow_mut() };
217218
f(&mut *tables)
218219
})
219220
}
@@ -222,7 +223,7 @@ pub fn run<F, T>(tcx: TyCtxt<'_>, f: F) -> Result<T, Error>
222223
where
223224
F: FnOnce() -> T,
224225
{
225-
let tables = TablesWrapper(RefCell::new(Tables {
226+
let tables = SmirCtxt(RefCell::new(Tables {
226227
tcx,
227228
def_ids: IndexMap::default(),
228229
alloc_ids: IndexMap::default(),
@@ -233,7 +234,12 @@ where
233234
mir_consts: IndexMap::default(),
234235
layouts: IndexMap::default(),
235236
}));
236-
stable_mir::compiler_interface::run(&tables, || init(&tables, f))
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))
237243
}
238244

239245
/// Instantiate and run the compiler with the provided arguments and callback.

‎compiler/rustc_smir/src/rustc_smir/context.rs

Lines changed: 175 additions & 94 deletions
Large diffs are not rendered by default.

‎compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::stable_mir;
2525

2626
mod alloc;
2727
mod builder;
28-
pub(crate) mod context;
28+
pub mod context;
2929
mod convert;
3030

3131
pub struct Tables<'tcx> {

‎compiler/rustc_smir/src/stable_mir/compiler_interface.rs

Lines changed: 321 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
use std::cell::Cell;
77

8+
use rustc_smir::context::SmirCtxt;
89
use stable_mir::abi::{FnAbi, Layout, LayoutShape};
910
use stable_mir::crate_def::Attribute;
1011
use stable_mir::mir::alloc::{AllocId, GlobalAlloc};
@@ -22,47 +23,116 @@ use stable_mir::{
2223
ItemKind, Symbol, TraitDecls, mir,
2324
};
2425

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+
}
2642

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>;
3143
/// 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+
3348
/// Retrieve the body of a function.
3449
/// 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+
3654
/// 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+
}
3962

4063
/// 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+
}
4267

4368
/// 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+
56117
/// 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+
58122
/// 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+
}
60126

61127
/// 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+
}
63131

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+
}
66136

67137
/// Return registered tool attributes with the given attribute name.
68138
///
@@ -71,218 +141,362 @@ pub trait Context {
71141
///
72142
/// Single segmented name like `#[clippy]` is specified as `&["clippy".to_string()]`.
73143
/// 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+
}
75147

76148
/// 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+
}
78152

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+
}
81157

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+
}
84162

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+
}
87167

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+
}
90172

91173
/// 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+
}
93177

94178
/// 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+
}
96182

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+
}
99187

100188
/// 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+
}
102192

103193
/// 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+
}
105197

106198
/// 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+
}
108202

109203
/// 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+
}
111207

112208
/// 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+
}
114212

115213
/// 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+
}
117217

118218
/// 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+
}
120222

121223
/// 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+
}
123227

124228
/// 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+
}
127236

128237
/// 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+
}
131245

132246
/// 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+
}
134250

135251
/// 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+
}
137255

138256
/// 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+
}
140260

141261
/// 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+
}
144277

145278
/// 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+
}
147282

148283
/// 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+
}
150287

151288
/// 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+
}
153292

154293
/// 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+
}
156297

157298
/// 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+
}
159302

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+
}
162307

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+
}
164311

165312
/// 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+
}
167316

168317
/// 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+
}
170321

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+
}
173326

174327
/// 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+
}
176331

177332
/// 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+
}
179336

180337
/// 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+
}
182341

183342
/// 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+
}
185346

186347
/// 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+
}
188351

189352
/// 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+
}
191356

192357
/// 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+
}
194361

195362
/// Convert a non-generic crate item into an instance.
196363
/// 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+
}
198367

199368
/// 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+
}
201372

202373
/// 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+
}
204377

205378
/// 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+
}
207382

208383
/// 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+
}
210387

211388
/// Resolve instance for a closure with the requested type.
212-
fn resolve_closure(
389+
pub(crate) fn resolve_closure(
213390
&self,
214391
def: ClosureDef,
215392
args: &GenericArgs,
216393
kind: ClosureKind,
217-
) -> Option<Instance>;
394+
) -> Option<Instance> {
395+
self.cx.resolve_closure(def, args, kind)
396+
}
218397

219398
/// 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+
}
221402

222403
/// 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+
}
224411

225412
/// 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+
}
227416

228417
/// 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+
}
232429

233430
/// 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+
}
235434

236435
/// 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+
}
238439

239440
/// 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+
}
241444

242445
/// 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+
}
244449

245450
/// 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+
}
247454

248455
/// 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+
}
250459

251460
/// 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+
}
253464

254465
/// 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+
}
256469

257470
/// 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+
}
259474
}
260475

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`].
263477
scoped_tls::scoped_thread_local!(static TLV: Cell<*const ()>);
264478

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>
266480
where
267481
F: FnOnce() -> T,
268482
{
269483
if TLV.is_set() {
270484
Err(Error::from("StableMIR already running"))
271485
} else {
272-
let ptr: *const () = (&raw const context) as _;
486+
let ptr: *const () = (interface as *const SmirInterface<'tcx>) as *const ();
273487
TLV.set(&Cell::new(ptr), || Ok(f()))
274488
}
275489
}
276490

277-
/// Execute the given function with access the compiler [Context].
491+
/// Execute the given function with access the [`SmirInterface`].
278492
///
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.
280494
/// 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 {
282496
assert!(TLV.is_set());
283497
TLV.with(|tlv| {
284498
let ptr = tlv.get();
285499
assert!(!ptr.is_null());
286-
f(unsafe { *(ptr as *const &dyn Context) })
500+
f(unsafe { &*(ptr as *const SmirInterface<'_>) })
287501
})
288502
}

0 commit comments

Comments
 (0)
Please sign in to comment.