use std::cell::Cell;
use crate::abi::{FnAbi, Layout, LayoutShape};
use crate::crate_def::Attribute;
use crate::mir::alloc::{AllocId, GlobalAlloc};
use crate::mir::mono::{Instance, InstanceDef, StaticDef};
use crate::mir::{BinOp, Body, Place, UnOp};
use crate::target::MachineInfo;
use crate::ty::{
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, FieldDef, FnDef, ForeignDef,
ForeignItemKind, ForeignModule, ForeignModuleDef, GenericArgs, GenericPredicates, Generics,
ImplDef, ImplTrait, IntrinsicDef, LineInfo, MirConst, PolyFnSig, RigidTy, Span, TraitDecl,
TraitDef, Ty, TyConst, TyConstId, TyKind, UintTy, VariantDef,
};
use crate::{
Crate, CrateItem, CrateItems, CrateNum, DefId, Error, Filename, ImplTraitDecls, ItemKind,
Symbol, TraitDecls, mir,
};
pub trait Context {
fn entry_fn(&self) -> Option<CrateItem>;
fn all_local_items(&self) -> CrateItems;
fn mir_body(&self, item: DefId) -> mir::Body;
fn has_body(&self, item: DefId) -> bool;
fn foreign_modules(&self, crate_num: CrateNum) -> Vec<ForeignModuleDef>;
fn crate_functions(&self, crate_num: CrateNum) -> Vec<FnDef>;
fn crate_statics(&self, crate_num: CrateNum) -> Vec<StaticDef>;
fn foreign_module(&self, mod_def: ForeignModuleDef) -> ForeignModule;
fn foreign_items(&self, mod_def: ForeignModuleDef) -> Vec<ForeignDef>;
fn all_trait_decls(&self) -> TraitDecls;
fn trait_decls(&self, crate_num: CrateNum) -> TraitDecls;
fn trait_decl(&self, trait_def: &TraitDef) -> TraitDecl;
fn all_trait_impls(&self) -> ImplTraitDecls;
fn trait_impls(&self, crate_num: CrateNum) -> ImplTraitDecls;
fn trait_impl(&self, trait_impl: &ImplDef) -> ImplTrait;
fn generics_of(&self, def_id: DefId) -> Generics;
fn predicates_of(&self, def_id: DefId) -> GenericPredicates;
fn explicit_predicates_of(&self, def_id: DefId) -> GenericPredicates;
fn local_crate(&self) -> Crate;
fn external_crates(&self) -> Vec<Crate>;
fn find_crates(&self, name: &str) -> Vec<Crate>;
fn def_name(&self, def_id: DefId, trimmed: bool) -> Symbol;
fn get_attrs_by_path(&self, def_id: DefId, attr: &[Symbol]) -> Vec<Attribute>;
fn get_all_attrs(&self, def_id: DefId) -> Vec<Attribute>;
fn span_to_string(&self, span: Span) -> String;
fn get_filename(&self, span: &Span) -> Filename;
fn get_lines(&self, span: &Span) -> LineInfo;
fn item_kind(&self, item: CrateItem) -> ItemKind;
fn is_foreign_item(&self, item: DefId) -> bool;
fn foreign_item_kind(&self, def: ForeignDef) -> ForeignItemKind;
fn adt_kind(&self, def: AdtDef) -> AdtKind;
fn adt_is_box(&self, def: AdtDef) -> bool;
fn adt_is_simd(&self, def: AdtDef) -> bool;
fn adt_is_cstr(&self, def: AdtDef) -> bool;
fn fn_sig(&self, def: FnDef, args: &GenericArgs) -> PolyFnSig;
fn intrinsic(&self, item: DefId) -> Option<IntrinsicDef>;
fn intrinsic_name(&self, def: IntrinsicDef) -> Symbol;
fn closure_sig(&self, args: &GenericArgs) -> PolyFnSig;
fn adt_variants_len(&self, def: AdtDef) -> usize;
fn variant_name(&self, def: VariantDef) -> Symbol;
fn variant_fields(&self, def: VariantDef) -> Vec<FieldDef>;
fn eval_target_usize(&self, cnst: &MirConst) -> Result<u64, Error>;
fn eval_target_usize_ty(&self, cnst: &TyConst) -> Result<u64, Error>;
fn try_new_const_zst(&self, ty: Ty) -> Result<MirConst, Error>;
fn new_const_str(&self, value: &str) -> MirConst;
fn new_const_bool(&self, value: bool) -> MirConst;
fn try_new_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<MirConst, Error>;
fn try_new_ty_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<TyConst, Error>;
fn new_rigid_ty(&self, kind: RigidTy) -> Ty;
fn new_box_ty(&self, ty: Ty) -> Ty;
fn def_ty(&self, item: DefId) -> Ty;
fn def_ty_with_args(&self, item: DefId, args: &GenericArgs) -> Ty;
fn mir_const_pretty(&self, cnst: &MirConst) -> String;
fn span_of_an_item(&self, def_id: DefId) -> Span;
fn ty_const_pretty(&self, ct: TyConstId) -> String;
fn ty_pretty(&self, ty: Ty) -> String;
fn ty_kind(&self, ty: Ty) -> TyKind;
fn rigid_ty_discriminant_ty(&self, ty: &RigidTy) -> Ty;
fn instance_body(&self, instance: InstanceDef) -> Option<Body>;
fn instance_ty(&self, instance: InstanceDef) -> Ty;
fn instance_args(&self, def: InstanceDef) -> GenericArgs;
fn instance_def_id(&self, instance: InstanceDef) -> DefId;
fn instance_mangled_name(&self, instance: InstanceDef) -> Symbol;
fn is_empty_drop_shim(&self, def: InstanceDef) -> bool;
fn is_empty_async_drop_ctor_shim(&self, def: InstanceDef) -> bool;
fn mono_instance(&self, def_id: DefId) -> Instance;
fn requires_monomorphization(&self, def_id: DefId) -> bool;
fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
fn resolve_drop_in_place(&self, ty: Ty) -> Instance;
fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
fn resolve_closure(
&self,
def: ClosureDef,
args: &GenericArgs,
kind: ClosureKind,
) -> Option<Instance>;
fn eval_static_initializer(&self, def: StaticDef) -> Result<Allocation, Error>;
fn eval_instance(&self, def: InstanceDef, const_ty: Ty) -> Result<Allocation, Error>;
fn global_alloc(&self, id: AllocId) -> GlobalAlloc;
fn vtable_allocation(&self, global_alloc: &GlobalAlloc) -> Option<AllocId>;
fn krate(&self, def_id: DefId) -> Crate;
fn instance_name(&self, def: InstanceDef, trimmed: bool) -> Symbol;
fn target_info(&self) -> MachineInfo;
fn instance_abi(&self, def: InstanceDef) -> Result<FnAbi, Error>;
fn fn_ptr_abi(&self, fn_ptr: PolyFnSig) -> Result<FnAbi, Error>;
fn ty_layout(&self, ty: Ty) -> Result<Layout, Error>;
fn layout_shape(&self, id: Layout) -> LayoutShape;
fn place_pretty(&self, place: &Place) -> String;
fn binop_ty(&self, bin_op: BinOp, rhs: Ty, lhs: Ty) -> Ty;
fn unop_ty(&self, un_op: UnOp, arg: Ty) -> Ty;
}
scoped_tls::scoped_thread_local!(static TLV: Cell<*const ()>);
pub fn run<F, T>(context: &dyn Context, f: F) -> Result<T, Error>
where
F: FnOnce() -> T,
{
if TLV.is_set() {
Err(Error::from("StableMIR already running"))
} else {
let ptr: *const () = (&raw const context) as _;
TLV.set(&Cell::new(ptr), || Ok(f()))
}
}
pub(crate) fn with<R>(f: impl FnOnce(&dyn Context) -> R) -> R {
assert!(TLV.is_set());
TLV.with(|tlv| {
let ptr = tlv.get();
assert!(!ptr.is_null());
f(unsafe { *(ptr as *const &dyn Context) })
})
}