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 tool_attrs(&self, def_id: DefId, attr: &[Symbol]) -> Vec<Attribute>;
73
74 fn all_tool_attrs(&self, def_id: DefId) -> Vec<Attribute>;
76
77 fn span_to_string(&self, span: Span) -> String;
79
80 fn get_filename(&self, span: &Span) -> Filename;
82
83 fn get_lines(&self, span: &Span) -> LineInfo;
85
86 fn item_kind(&self, item: CrateItem) -> ItemKind;
88
89 fn is_foreign_item(&self, item: DefId) -> bool;
91
92 fn foreign_item_kind(&self, def: ForeignDef) -> ForeignItemKind;
94
95 fn adt_kind(&self, def: AdtDef) -> AdtKind;
97
98 fn adt_is_box(&self, def: AdtDef) -> bool;
100
101 fn adt_is_simd(&self, def: AdtDef) -> bool;
103
104 fn adt_is_cstr(&self, def: AdtDef) -> bool;
106
107 fn fn_sig(&self, def: FnDef, args: &GenericArgs) -> PolyFnSig;
109
110 fn intrinsic(&self, item: DefId) -> Option<IntrinsicDef>;
112
113 fn intrinsic_name(&self, def: IntrinsicDef) -> Symbol;
115
116 fn closure_sig(&self, args: &GenericArgs) -> PolyFnSig;
118
119 fn adt_variants_len(&self, def: AdtDef) -> usize;
121
122 fn variant_name(&self, def: VariantDef) -> Symbol;
124 fn variant_fields(&self, def: VariantDef) -> Vec<FieldDef>;
125
126 fn eval_target_usize(&self, cnst: &MirConst) -> Result<u64, Error>;
128 fn eval_target_usize_ty(&self, cnst: &TyConst) -> Result<u64, Error>;
129
130 fn try_new_const_zst(&self, ty: Ty) -> Result<MirConst, Error>;
132
133 fn new_const_str(&self, value: &str) -> MirConst;
135
136 fn new_const_bool(&self, value: bool) -> MirConst;
138
139 fn try_new_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<MirConst, Error>;
141 fn try_new_ty_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<TyConst, Error>;
142
143 fn new_rigid_ty(&self, kind: RigidTy) -> Ty;
145
146 fn new_box_ty(&self, ty: Ty) -> Ty;
148
149 fn def_ty(&self, item: DefId) -> Ty;
151
152 fn def_ty_with_args(&self, item: DefId, args: &GenericArgs) -> Ty;
154
155 fn mir_const_pretty(&self, cnst: &MirConst) -> String;
157
158 fn span_of_an_item(&self, def_id: DefId) -> Span;
160
161 fn ty_const_pretty(&self, ct: TyConstId) -> String;
162
163 fn ty_pretty(&self, ty: Ty) -> String;
165
166 fn ty_kind(&self, ty: Ty) -> TyKind;
168
169 fn rigid_ty_discriminant_ty(&self, ty: &RigidTy) -> Ty;
171
172 fn instance_body(&self, instance: InstanceDef) -> Option<Body>;
174
175 fn instance_ty(&self, instance: InstanceDef) -> Ty;
177
178 fn instance_args(&self, def: InstanceDef) -> GenericArgs;
180
181 fn instance_def_id(&self, instance: InstanceDef) -> DefId;
183
184 fn instance_mangled_name(&self, instance: InstanceDef) -> Symbol;
186
187 fn is_empty_drop_shim(&self, def: InstanceDef) -> bool;
189
190 fn is_empty_async_drop_ctor_shim(&self, def: InstanceDef) -> bool;
192
193 fn mono_instance(&self, def_id: DefId) -> Instance;
196
197 fn requires_monomorphization(&self, def_id: DefId) -> bool;
199
200 fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
202
203 fn resolve_drop_in_place(&self, ty: Ty) -> Instance;
205
206 fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
208
209 fn resolve_closure(
211 &self,
212 def: ClosureDef,
213 args: &GenericArgs,
214 kind: ClosureKind,
215 ) -> Option<Instance>;
216
217 fn eval_static_initializer(&self, def: StaticDef) -> Result<Allocation, Error>;
219
220 fn eval_instance(&self, def: InstanceDef, const_ty: Ty) -> Result<Allocation, Error>;
222
223 fn global_alloc(&self, id: AllocId) -> GlobalAlloc;
225
226 fn vtable_allocation(&self, global_alloc: &GlobalAlloc) -> Option<AllocId>;
228 fn krate(&self, def_id: DefId) -> Crate;
229 fn instance_name(&self, def: InstanceDef, trimmed: bool) -> Symbol;
230
231 fn target_info(&self) -> MachineInfo;
233
234 fn instance_abi(&self, def: InstanceDef) -> Result<FnAbi, Error>;
236
237 fn fn_ptr_abi(&self, fn_ptr: PolyFnSig) -> Result<FnAbi, Error>;
239
240 fn ty_layout(&self, ty: Ty) -> Result<Layout, Error>;
242
243 fn layout_shape(&self, id: Layout) -> LayoutShape;
245
246 fn place_pretty(&self, place: &Place) -> String;
248
249 fn binop_ty(&self, bin_op: BinOp, rhs: Ty, lhs: Ty) -> Ty;
251
252 fn unop_ty(&self, un_op: UnOp, arg: Ty) -> Ty;
254}
255
256scoped_tls::scoped_thread_local!(static TLV: Cell<*const ()>);
259
260pub fn run<F, T>(context: &dyn Context, f: F) -> Result<T, Error>
261where
262 F: FnOnce() -> T,
263{
264 if TLV.is_set() {
265 Err(Error::from("StableMIR already running"))
266 } else {
267 let ptr: *const () = (&raw const context) as _;
268 TLV.set(&Cell::new(ptr), || Ok(f()))
269 }
270}
271
272pub(crate) fn with<R>(f: impl FnOnce(&dyn Context) -> R) -> R {
277 assert!(TLV.is_set());
278 TLV.with(|tlv| {
279 let ptr = tlv.get();
280 assert!(!ptr.is_null());
281 f(unsafe { *(ptr as *const &dyn Context) })
282 })
283}