1use std::cell::Cell;
7
8use crate::abi::{FnAbi, Layout, LayoutShape};
9use crate::crate_def::Attribute;
10use crate::mir::alloc::{AllocId, GlobalAlloc};
11use crate::mir::mono::{Instance, InstanceDef, StaticDef};
12use crate::mir::{BinOp, Body, Place, UnOp};
13use crate::target::MachineInfo;
14use crate::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 crate::{
21 Crate, CrateItem, CrateItems, CrateNum, DefId, Error, Filename, ImplTraitDecls, ItemKind,
22 Symbol, TraitDecls, mir,
23};
24
25pub trait Context {
28 fn entry_fn(&self) -> Option<CrateItem>;
29 fn all_local_items(&self) -> CrateItems;
31 fn mir_body(&self, item: DefId) -> mir::Body;
34 fn has_body(&self, item: DefId) -> bool;
36 fn foreign_modules(&self, crate_num: CrateNum) -> Vec<ForeignModuleDef>;
37
38 fn crate_functions(&self, crate_num: CrateNum) -> Vec<FnDef>;
40
41 fn crate_statics(&self, crate_num: CrateNum) -> Vec<StaticDef>;
43 fn foreign_module(&self, mod_def: ForeignModuleDef) -> ForeignModule;
44 fn foreign_items(&self, mod_def: ForeignModuleDef) -> Vec<ForeignDef>;
45 fn all_trait_decls(&self) -> TraitDecls;
46 fn trait_decls(&self, crate_num: CrateNum) -> TraitDecls;
47 fn trait_decl(&self, trait_def: &TraitDef) -> TraitDecl;
48 fn all_trait_impls(&self) -> ImplTraitDecls;
49 fn trait_impls(&self, crate_num: CrateNum) -> ImplTraitDecls;
50 fn trait_impl(&self, trait_impl: &ImplDef) -> ImplTrait;
51 fn generics_of(&self, def_id: DefId) -> Generics;
52 fn predicates_of(&self, def_id: DefId) -> GenericPredicates;
53 fn explicit_predicates_of(&self, def_id: DefId) -> GenericPredicates;
54 fn local_crate(&self) -> Crate;
56 fn external_crates(&self) -> Vec<Crate>;
58
59 fn find_crates(&self, name: &str) -> Vec<Crate>;
61
62 fn def_name(&self, def_id: DefId, trimmed: bool) -> Symbol;
64
65 fn get_attrs_by_path(&self, def_id: DefId, attr: &[Symbol]) -> Vec<Attribute>;
70
71 fn get_all_attrs(&self, def_id: DefId) -> Vec<Attribute>;
73
74 fn span_to_string(&self, span: Span) -> String;
76
77 fn get_filename(&self, span: &Span) -> Filename;
79
80 fn get_lines(&self, span: &Span) -> LineInfo;
82
83 fn item_kind(&self, item: CrateItem) -> ItemKind;
85
86 fn is_foreign_item(&self, item: DefId) -> bool;
88
89 fn foreign_item_kind(&self, def: ForeignDef) -> ForeignItemKind;
91
92 fn adt_kind(&self, def: AdtDef) -> AdtKind;
94
95 fn adt_is_box(&self, def: AdtDef) -> bool;
97
98 fn adt_is_simd(&self, def: AdtDef) -> bool;
100
101 fn adt_is_cstr(&self, def: AdtDef) -> bool;
103
104 fn fn_sig(&self, def: FnDef, args: &GenericArgs) -> PolyFnSig;
106
107 fn intrinsic(&self, item: DefId) -> Option<IntrinsicDef>;
109
110 fn intrinsic_name(&self, def: IntrinsicDef) -> Symbol;
112
113 fn closure_sig(&self, args: &GenericArgs) -> PolyFnSig;
115
116 fn adt_variants_len(&self, def: AdtDef) -> usize;
118
119 fn variant_name(&self, def: VariantDef) -> Symbol;
121 fn variant_fields(&self, def: VariantDef) -> Vec<FieldDef>;
122
123 fn eval_target_usize(&self, cnst: &MirConst) -> Result<u64, Error>;
125 fn eval_target_usize_ty(&self, cnst: &TyConst) -> Result<u64, Error>;
126
127 fn try_new_const_zst(&self, ty: Ty) -> Result<MirConst, Error>;
129
130 fn new_const_str(&self, value: &str) -> MirConst;
132
133 fn new_const_bool(&self, value: bool) -> MirConst;
135
136 fn try_new_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<MirConst, Error>;
138 fn try_new_ty_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<TyConst, Error>;
139
140 fn new_rigid_ty(&self, kind: RigidTy) -> Ty;
142
143 fn new_box_ty(&self, ty: Ty) -> Ty;
145
146 fn def_ty(&self, item: DefId) -> Ty;
148
149 fn def_ty_with_args(&self, item: DefId, args: &GenericArgs) -> Ty;
151
152 fn mir_const_pretty(&self, cnst: &MirConst) -> String;
154
155 fn span_of_an_item(&self, def_id: DefId) -> Span;
157
158 fn ty_const_pretty(&self, ct: TyConstId) -> String;
159
160 fn ty_pretty(&self, ty: Ty) -> String;
162
163 fn ty_kind(&self, ty: Ty) -> TyKind;
165
166 fn rigid_ty_discriminant_ty(&self, ty: &RigidTy) -> Ty;
168
169 fn instance_body(&self, instance: InstanceDef) -> Option<Body>;
171
172 fn instance_ty(&self, instance: InstanceDef) -> Ty;
174
175 fn instance_args(&self, def: InstanceDef) -> GenericArgs;
177
178 fn instance_def_id(&self, instance: InstanceDef) -> DefId;
180
181 fn instance_mangled_name(&self, instance: InstanceDef) -> Symbol;
183
184 fn is_empty_drop_shim(&self, def: InstanceDef) -> bool;
186
187 fn is_empty_async_drop_ctor_shim(&self, def: InstanceDef) -> bool;
189
190 fn mono_instance(&self, def_id: DefId) -> Instance;
193
194 fn requires_monomorphization(&self, def_id: DefId) -> bool;
196
197 fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
199
200 fn resolve_drop_in_place(&self, ty: Ty) -> Instance;
202
203 fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
205
206 fn resolve_closure(
208 &self,
209 def: ClosureDef,
210 args: &GenericArgs,
211 kind: ClosureKind,
212 ) -> Option<Instance>;
213
214 fn eval_static_initializer(&self, def: StaticDef) -> Result<Allocation, Error>;
216
217 fn eval_instance(&self, def: InstanceDef, const_ty: Ty) -> Result<Allocation, Error>;
219
220 fn global_alloc(&self, id: AllocId) -> GlobalAlloc;
222
223 fn vtable_allocation(&self, global_alloc: &GlobalAlloc) -> Option<AllocId>;
225 fn krate(&self, def_id: DefId) -> Crate;
226 fn instance_name(&self, def: InstanceDef, trimmed: bool) -> Symbol;
227
228 fn target_info(&self) -> MachineInfo;
230
231 fn instance_abi(&self, def: InstanceDef) -> Result<FnAbi, Error>;
233
234 fn fn_ptr_abi(&self, fn_ptr: PolyFnSig) -> Result<FnAbi, Error>;
236
237 fn ty_layout(&self, ty: Ty) -> Result<Layout, Error>;
239
240 fn layout_shape(&self, id: Layout) -> LayoutShape;
242
243 fn place_pretty(&self, place: &Place) -> String;
245
246 fn binop_ty(&self, bin_op: BinOp, rhs: Ty, lhs: Ty) -> Ty;
248
249 fn unop_ty(&self, un_op: UnOp, arg: Ty) -> Ty;
251}
252
253scoped_tls::scoped_thread_local!(static TLV: Cell<*const ()>);
256
257pub fn run<F, T>(context: &dyn Context, f: F) -> Result<T, Error>
258where
259 F: FnOnce() -> T,
260{
261 if TLV.is_set() {
262 Err(Error::from("StableMIR already running"))
263 } else {
264 let ptr: *const () = (&raw const context) as _;
265 TLV.set(&Cell::new(ptr), || Ok(f()))
266 }
267}
268
269pub(crate) fn with<R>(f: impl FnOnce(&dyn Context) -> R) -> R {
274 assert!(TLV.is_set());
275 TLV.with(|tlv| {
276 let ptr = tlv.get();
277 assert!(!ptr.is_null());
278 f(unsafe { *(ptr as *const &dyn Context) })
279 })
280}