1use std::cell::Cell;
7
8use stable_mir::abi::{FnAbi, Layout, LayoutShape};
9use stable_mir::crate_def::Attribute;
10use stable_mir::mir::alloc::{AllocId, GlobalAlloc};
11use stable_mir::mir::mono::{Instance, InstanceDef, StaticDef};
12use stable_mir::mir::{BinOp, Body, Place, UnOp};
13use stable_mir::target::MachineInfo;
14use stable_mir::ty::{
15 AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, FieldDef, FnDef, ForeignDef,
16 ForeignItemKind, ForeignModule, ForeignModuleDef, GenericArgs, GenericPredicates, Generics,
17 ImplDef, ImplTrait, IntrinsicDef, LineInfo, MirConst, PolyFnSig, RigidTy, Span, TraitDecl,
18 TraitDef, Ty, TyConst, TyConstId, TyKind, UintTy, VariantDef,
19};
20use stable_mir::{
21 AssocItems, Crate, CrateItem, CrateItems, CrateNum, DefId, Error, Filename, ImplTraitDecls,
22 ItemKind, Symbol, TraitDecls, mir,
23};
24
25use crate::stable_mir;
26
27pub trait Context {
30 fn entry_fn(&self) -> Option<CrateItem>;
31 fn all_local_items(&self) -> CrateItems;
33 fn mir_body(&self, item: DefId) -> mir::Body;
36 fn has_body(&self, item: DefId) -> bool;
38 fn foreign_modules(&self, crate_num: CrateNum) -> Vec<ForeignModuleDef>;
39
40 fn crate_functions(&self, crate_num: CrateNum) -> Vec<FnDef>;
42
43 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;
56 fn local_crate(&self) -> Crate;
58 fn external_crates(&self) -> Vec<Crate>;
60
61 fn find_crates(&self, name: &str) -> Vec<Crate>;
63
64 fn def_name(&self, def_id: DefId, trimmed: bool) -> Symbol;
66
67 fn tool_attrs(&self, def_id: DefId, attr: &[Symbol]) -> Vec<Attribute>;
75
76 fn all_tool_attrs(&self, def_id: DefId) -> Vec<Attribute>;
78
79 fn span_to_string(&self, span: Span) -> String;
81
82 fn get_filename(&self, span: &Span) -> Filename;
84
85 fn get_lines(&self, span: &Span) -> LineInfo;
87
88 fn item_kind(&self, item: CrateItem) -> ItemKind;
90
91 fn is_foreign_item(&self, item: DefId) -> bool;
93
94 fn foreign_item_kind(&self, def: ForeignDef) -> ForeignItemKind;
96
97 fn adt_kind(&self, def: AdtDef) -> AdtKind;
99
100 fn adt_is_box(&self, def: AdtDef) -> bool;
102
103 fn adt_is_simd(&self, def: AdtDef) -> bool;
105
106 fn adt_is_cstr(&self, def: AdtDef) -> bool;
108
109 fn fn_sig(&self, def: FnDef, args: &GenericArgs) -> PolyFnSig;
111
112 fn intrinsic(&self, item: DefId) -> Option<IntrinsicDef>;
114
115 fn intrinsic_name(&self, def: IntrinsicDef) -> Symbol;
117
118 fn closure_sig(&self, args: &GenericArgs) -> PolyFnSig;
120
121 fn adt_variants_len(&self, def: AdtDef) -> usize;
123
124 fn variant_name(&self, def: VariantDef) -> Symbol;
126 fn variant_fields(&self, def: VariantDef) -> Vec<FieldDef>;
127
128 fn eval_target_usize(&self, cnst: &MirConst) -> Result<u64, Error>;
130 fn eval_target_usize_ty(&self, cnst: &TyConst) -> Result<u64, Error>;
131
132 fn try_new_const_zst(&self, ty: Ty) -> Result<MirConst, Error>;
134
135 fn new_const_str(&self, value: &str) -> MirConst;
137
138 fn new_const_bool(&self, value: bool) -> MirConst;
140
141 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>;
144
145 fn new_rigid_ty(&self, kind: RigidTy) -> Ty;
147
148 fn new_box_ty(&self, ty: Ty) -> Ty;
150
151 fn def_ty(&self, item: DefId) -> Ty;
153
154 fn def_ty_with_args(&self, item: DefId, args: &GenericArgs) -> Ty;
156
157 fn mir_const_pretty(&self, cnst: &MirConst) -> String;
159
160 fn span_of_an_item(&self, def_id: DefId) -> Span;
162
163 fn ty_const_pretty(&self, ct: TyConstId) -> String;
164
165 fn ty_pretty(&self, ty: Ty) -> String;
167
168 fn ty_kind(&self, ty: Ty) -> TyKind;
170
171 fn rigid_ty_discriminant_ty(&self, ty: &RigidTy) -> Ty;
173
174 fn instance_body(&self, instance: InstanceDef) -> Option<Body>;
176
177 fn instance_ty(&self, instance: InstanceDef) -> Ty;
179
180 fn instance_args(&self, def: InstanceDef) -> GenericArgs;
182
183 fn instance_def_id(&self, instance: InstanceDef) -> DefId;
185
186 fn instance_mangled_name(&self, instance: InstanceDef) -> Symbol;
188
189 fn is_empty_drop_shim(&self, def: InstanceDef) -> bool;
191
192 fn is_empty_async_drop_ctor_shim(&self, def: InstanceDef) -> bool;
194
195 fn mono_instance(&self, def_id: DefId) -> Instance;
198
199 fn requires_monomorphization(&self, def_id: DefId) -> bool;
201
202 fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
204
205 fn resolve_drop_in_place(&self, ty: Ty) -> Instance;
207
208 fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
210
211 fn resolve_closure(
213 &self,
214 def: ClosureDef,
215 args: &GenericArgs,
216 kind: ClosureKind,
217 ) -> Option<Instance>;
218
219 fn eval_static_initializer(&self, def: StaticDef) -> Result<Allocation, Error>;
221
222 fn eval_instance(&self, def: InstanceDef, const_ty: Ty) -> Result<Allocation, Error>;
224
225 fn global_alloc(&self, id: AllocId) -> GlobalAlloc;
227
228 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;
232
233 fn target_info(&self) -> MachineInfo;
235
236 fn instance_abi(&self, def: InstanceDef) -> Result<FnAbi, Error>;
238
239 fn fn_ptr_abi(&self, fn_ptr: PolyFnSig) -> Result<FnAbi, Error>;
241
242 fn ty_layout(&self, ty: Ty) -> Result<Layout, Error>;
244
245 fn layout_shape(&self, id: Layout) -> LayoutShape;
247
248 fn place_pretty(&self, place: &Place) -> String;
250
251 fn binop_ty(&self, bin_op: BinOp, rhs: Ty, lhs: Ty) -> Ty;
253
254 fn unop_ty(&self, un_op: UnOp, arg: Ty) -> Ty;
256
257 fn associated_items(&self, def_id: DefId) -> AssocItems;
259}
260
261scoped_tls::scoped_thread_local!(static TLV: Cell<*const ()>);
264
265pub fn run<F, T>(context: &dyn Context, f: F) -> Result<T, Error>
266where
267 F: FnOnce() -> T,
268{
269 if TLV.is_set() {
270 Err(Error::from("StableMIR already running"))
271 } else {
272 let ptr: *const () = (&raw const context) as _;
273 TLV.set(&Cell::new(ptr), || Ok(f()))
274 }
275}
276
277pub(crate) fn with<R>(f: impl FnOnce(&dyn Context) -> R) -> R {
282 assert!(TLV.is_set());
283 TLV.with(|tlv| {
284 let ptr = tlv.get();
285 assert!(!ptr.is_null());
286 f(unsafe { *(ptr as *const &dyn Context) })
287 })
288}