2023-11-24 13:41:45 -08:00
|
|
|
//! Define the interface with the Rust compiler.
|
|
|
|
|
//!
|
|
|
|
|
//! StableMIR users should not use any of the items in this module directly.
|
|
|
|
|
//! These APIs have no stability guarantee.
|
|
|
|
|
|
2023-11-24 15:09:26 -08:00
|
|
|
use std::cell::Cell;
|
|
|
|
|
|
2025-05-05 15:15:15 +08:00
|
|
|
use rustc_hir::def::DefKind;
|
2025-05-11 09:03:51 +00:00
|
|
|
use rustc_smir::{Bridge, SmirContainer};
|
2025-06-03 13:38:00 +09:00
|
|
|
use stable_mir::abi::{FnAbi, Layout, LayoutShape, ReprOptions};
|
2025-05-05 15:35:54 +08:00
|
|
|
use stable_mir::convert::{RustcInternal, Stable};
|
2025-04-05 18:23:07 +08:00
|
|
|
use stable_mir::crate_def::Attribute;
|
|
|
|
|
use stable_mir::mir::alloc::{AllocId, GlobalAlloc};
|
|
|
|
|
use stable_mir::mir::mono::{Instance, InstanceDef, StaticDef};
|
|
|
|
|
use stable_mir::mir::{BinOp, Body, Place, UnOp};
|
2025-05-05 15:15:15 +08:00
|
|
|
use stable_mir::target::{MachineInfo, MachineSize};
|
2025-04-05 18:23:07 +08:00
|
|
|
use stable_mir::ty::{
|
2025-05-27 11:18:37 +03:00
|
|
|
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, CoroutineDef, Discr, 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, VariantIdx,
|
2023-11-24 13:41:45 -08:00
|
|
|
};
|
2025-04-05 18:23:07 +08:00
|
|
|
use stable_mir::{
|
2025-05-05 15:15:15 +08:00
|
|
|
AssocItems, Crate, CrateDef, CrateItem, CrateItems, CrateNum, DefId, Error, Filename,
|
|
|
|
|
ImplTraitDecls, ItemKind, Symbol, TraitDecls, alloc, mir, new_item_kind,
|
2023-11-24 13:41:45 -08:00
|
|
|
};
|
2025-05-05 15:15:15 +08:00
|
|
|
use tracing::debug;
|
2023-11-24 13:41:45 -08:00
|
|
|
|
2025-04-18 14:34:20 +08:00
|
|
|
use crate::{rustc_smir, stable_mir};
|
|
|
|
|
|
2025-05-05 15:15:15 +08:00
|
|
|
pub struct BridgeTys;
|
|
|
|
|
|
|
|
|
|
impl Bridge for BridgeTys {
|
|
|
|
|
type DefId = stable_mir::DefId;
|
|
|
|
|
type AllocId = stable_mir::mir::alloc::AllocId;
|
|
|
|
|
type Span = stable_mir::ty::Span;
|
|
|
|
|
type Ty = stable_mir::ty::Ty;
|
|
|
|
|
type InstanceDef = stable_mir::mir::mono::InstanceDef;
|
|
|
|
|
type TyConstId = stable_mir::ty::TyConstId;
|
|
|
|
|
type MirConstId = stable_mir::ty::MirConstId;
|
|
|
|
|
type Layout = stable_mir::abi::Layout;
|
|
|
|
|
|
2025-05-11 09:03:51 +00:00
|
|
|
type Error = stable_mir::Error;
|
|
|
|
|
type CrateItem = stable_mir::CrateItem;
|
|
|
|
|
type AdtDef = stable_mir::ty::AdtDef;
|
|
|
|
|
type ForeignModuleDef = stable_mir::ty::ForeignModuleDef;
|
|
|
|
|
type ForeignDef = stable_mir::ty::ForeignDef;
|
|
|
|
|
type FnDef = stable_mir::ty::FnDef;
|
|
|
|
|
type ClosureDef = stable_mir::ty::ClosureDef;
|
|
|
|
|
type CoroutineDef = stable_mir::ty::CoroutineDef;
|
|
|
|
|
type CoroutineClosureDef = stable_mir::ty::CoroutineClosureDef;
|
|
|
|
|
type AliasDef = stable_mir::ty::AliasDef;
|
|
|
|
|
type ParamDef = stable_mir::ty::ParamDef;
|
|
|
|
|
type BrNamedDef = stable_mir::ty::BrNamedDef;
|
|
|
|
|
type TraitDef = stable_mir::ty::TraitDef;
|
|
|
|
|
type GenericDef = stable_mir::ty::GenericDef;
|
|
|
|
|
type ConstDef = stable_mir::ty::ConstDef;
|
|
|
|
|
type ImplDef = stable_mir::ty::ImplDef;
|
|
|
|
|
type RegionDef = stable_mir::ty::RegionDef;
|
|
|
|
|
type CoroutineWitnessDef = stable_mir::ty::CoroutineWitnessDef;
|
|
|
|
|
type AssocDef = stable_mir::ty::AssocDef;
|
|
|
|
|
type OpaqueDef = stable_mir::ty::OpaqueDef;
|
|
|
|
|
type Prov = stable_mir::ty::Prov;
|
|
|
|
|
type StaticDef = stable_mir::mir::mono::StaticDef;
|
2025-05-05 15:15:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Stable public API for querying compiler information.
|
|
|
|
|
///
|
|
|
|
|
/// All queries are delegated to [`crate::rustc_smir::context::SmirCtxt`] that provides
|
|
|
|
|
/// similar APIs but based on internal rustc constructs.
|
|
|
|
|
///
|
|
|
|
|
/// Do not use this directly. This is currently used in the macro expansion.
|
|
|
|
|
pub(crate) trait SmirInterface {
|
|
|
|
|
fn entry_fn(&self) -> Option<CrateItem>;
|
|
|
|
|
/// Retrieve all items of the local crate that have a MIR associated with them.
|
|
|
|
|
fn all_local_items(&self) -> CrateItems;
|
|
|
|
|
/// Retrieve the body of a function.
|
|
|
|
|
/// This function will panic if the body is not available.
|
|
|
|
|
fn mir_body(&self, item: DefId) -> mir::Body;
|
|
|
|
|
/// Check whether the body of a function is available.
|
|
|
|
|
fn has_body(&self, item: DefId) -> bool;
|
|
|
|
|
fn foreign_modules(&self, crate_num: CrateNum) -> Vec<ForeignModuleDef>;
|
|
|
|
|
|
|
|
|
|
/// Retrieve all functions defined in this crate.
|
|
|
|
|
fn crate_functions(&self, crate_num: CrateNum) -> Vec<FnDef>;
|
|
|
|
|
|
|
|
|
|
/// Retrieve all static items defined in this crate.
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
/// Get information about the local crate.
|
|
|
|
|
fn local_crate(&self) -> Crate;
|
|
|
|
|
/// Retrieve a list of all external crates.
|
|
|
|
|
fn external_crates(&self) -> Vec<Crate>;
|
|
|
|
|
|
|
|
|
|
/// Find a crate with the given name.
|
|
|
|
|
fn find_crates(&self, name: &str) -> Vec<Crate>;
|
|
|
|
|
|
|
|
|
|
/// Returns the name of given `DefId`
|
|
|
|
|
fn def_name(&self, def_id: DefId, trimmed: bool) -> Symbol;
|
|
|
|
|
|
|
|
|
|
/// Return registered tool attributes with the given attribute name.
|
|
|
|
|
///
|
|
|
|
|
/// FIXME(jdonszelmann): may panic on non-tool attributes. After more attribute work, non-tool
|
|
|
|
|
/// attributes will simply return an empty list.
|
|
|
|
|
///
|
|
|
|
|
/// Single segmented name like `#[clippy]` is specified as `&["clippy".to_string()]`.
|
|
|
|
|
/// Multi-segmented name like `#[rustfmt::skip]` is specified as `&["rustfmt".to_string(), "skip".to_string()]`.
|
|
|
|
|
fn tool_attrs(&self, def_id: DefId, attr: &[Symbol]) -> Vec<Attribute>;
|
|
|
|
|
|
|
|
|
|
/// Get all tool attributes of a definition.
|
|
|
|
|
fn all_tool_attrs(&self, def_id: DefId) -> Vec<Attribute>;
|
|
|
|
|
|
|
|
|
|
/// Returns printable, human readable form of `Span`
|
|
|
|
|
fn span_to_string(&self, span: Span) -> String;
|
|
|
|
|
|
|
|
|
|
/// Return filename from given `Span`, for diagnostic purposes
|
|
|
|
|
fn get_filename(&self, span: &Span) -> Filename;
|
|
|
|
|
|
|
|
|
|
/// Return lines corresponding to this `Span`
|
|
|
|
|
fn get_lines(&self, span: &Span) -> LineInfo;
|
|
|
|
|
|
|
|
|
|
/// Returns the `kind` of given `DefId`
|
|
|
|
|
fn item_kind(&self, item: CrateItem) -> ItemKind;
|
|
|
|
|
|
|
|
|
|
/// Returns whether this is a foreign item.
|
|
|
|
|
fn is_foreign_item(&self, item: DefId) -> bool;
|
|
|
|
|
|
|
|
|
|
/// Returns the kind of a given foreign item.
|
|
|
|
|
fn foreign_item_kind(&self, def: ForeignDef) -> ForeignItemKind;
|
|
|
|
|
|
|
|
|
|
/// Returns the kind of a given algebraic data type
|
|
|
|
|
fn adt_kind(&self, def: AdtDef) -> AdtKind;
|
|
|
|
|
|
|
|
|
|
/// Returns if the ADT is a box.
|
|
|
|
|
fn adt_is_box(&self, def: AdtDef) -> bool;
|
|
|
|
|
|
|
|
|
|
/// Returns whether this ADT is simd.
|
|
|
|
|
fn adt_is_simd(&self, def: AdtDef) -> bool;
|
|
|
|
|
|
|
|
|
|
/// Returns whether this definition is a C string.
|
|
|
|
|
fn adt_is_cstr(&self, def: AdtDef) -> bool;
|
|
|
|
|
|
|
|
|
|
/// Retrieve the function signature for the given generic arguments.
|
|
|
|
|
fn fn_sig(&self, def: FnDef, args: &GenericArgs) -> PolyFnSig;
|
|
|
|
|
|
|
|
|
|
/// Retrieve the intrinsic definition if the item corresponds one.
|
|
|
|
|
fn intrinsic(&self, item: DefId) -> Option<IntrinsicDef>;
|
|
|
|
|
|
|
|
|
|
/// Retrieve the plain function name of an intrinsic.
|
|
|
|
|
fn intrinsic_name(&self, def: IntrinsicDef) -> Symbol;
|
|
|
|
|
|
|
|
|
|
/// Retrieve the closure signature for the given generic arguments.
|
|
|
|
|
fn closure_sig(&self, args: &GenericArgs) -> PolyFnSig;
|
|
|
|
|
|
|
|
|
|
/// The number of variants in this ADT.
|
|
|
|
|
fn adt_variants_len(&self, def: AdtDef) -> usize;
|
|
|
|
|
|
|
|
|
|
/// The name of a variant.
|
|
|
|
|
fn variant_name(&self, def: VariantDef) -> Symbol;
|
|
|
|
|
fn variant_fields(&self, def: VariantDef) -> Vec<FieldDef>;
|
|
|
|
|
|
|
|
|
|
/// Evaluate constant as a target usize.
|
|
|
|
|
fn eval_target_usize(&self, cnst: &MirConst) -> Result<u64, Error>;
|
|
|
|
|
fn eval_target_usize_ty(&self, cnst: &TyConst) -> Result<u64, Error>;
|
|
|
|
|
|
|
|
|
|
/// Create a new zero-sized constant.
|
|
|
|
|
fn try_new_const_zst(&self, ty: Ty) -> Result<MirConst, Error>;
|
|
|
|
|
|
|
|
|
|
/// Create a new constant that represents the given string value.
|
|
|
|
|
fn new_const_str(&self, value: &str) -> MirConst;
|
|
|
|
|
|
|
|
|
|
/// Create a new constant that represents the given boolean value.
|
|
|
|
|
fn new_const_bool(&self, value: bool) -> MirConst;
|
|
|
|
|
|
|
|
|
|
/// Create a new constant that represents the given value.
|
|
|
|
|
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>;
|
|
|
|
|
|
|
|
|
|
/// Create a new type from the given kind.
|
|
|
|
|
fn new_rigid_ty(&self, kind: RigidTy) -> Ty;
|
|
|
|
|
|
|
|
|
|
/// Create a new box type, `Box<T>`, for the given inner type `T`.
|
|
|
|
|
fn new_box_ty(&self, ty: Ty) -> Ty;
|
|
|
|
|
|
|
|
|
|
/// Returns the type of given crate item.
|
|
|
|
|
fn def_ty(&self, item: DefId) -> Ty;
|
|
|
|
|
|
|
|
|
|
/// Returns the type of given definition instantiated with the given arguments.
|
|
|
|
|
fn def_ty_with_args(&self, item: DefId, args: &GenericArgs) -> Ty;
|
|
|
|
|
|
|
|
|
|
/// Returns literal value of a const as a string.
|
|
|
|
|
fn mir_const_pretty(&self, cnst: &MirConst) -> String;
|
|
|
|
|
|
|
|
|
|
/// `Span` of an item
|
|
|
|
|
fn span_of_an_item(&self, def_id: DefId) -> Span;
|
|
|
|
|
|
|
|
|
|
fn ty_const_pretty(&self, ct: TyConstId) -> String;
|
|
|
|
|
|
|
|
|
|
/// Obtain the representation of a type.
|
|
|
|
|
fn ty_pretty(&self, ty: Ty) -> String;
|
|
|
|
|
|
|
|
|
|
/// Obtain the kind of a type.
|
|
|
|
|
fn ty_kind(&self, ty: Ty) -> TyKind;
|
|
|
|
|
|
|
|
|
|
// Get the discriminant Ty for this Ty if there's one.
|
|
|
|
|
fn rigid_ty_discriminant_ty(&self, ty: &RigidTy) -> Ty;
|
|
|
|
|
|
|
|
|
|
/// Get the body of an Instance which is already monomorphized.
|
|
|
|
|
fn instance_body(&self, instance: InstanceDef) -> Option<Body>;
|
|
|
|
|
|
|
|
|
|
/// Get the instance type with generic instantiations applied and lifetimes erased.
|
|
|
|
|
fn instance_ty(&self, instance: InstanceDef) -> Ty;
|
|
|
|
|
|
|
|
|
|
/// Get the instantiation types.
|
|
|
|
|
fn instance_args(&self, def: InstanceDef) -> GenericArgs;
|
|
|
|
|
|
|
|
|
|
/// Get the instance.
|
|
|
|
|
fn instance_def_id(&self, instance: InstanceDef) -> DefId;
|
|
|
|
|
|
|
|
|
|
/// Get the instance mangled name.
|
|
|
|
|
fn instance_mangled_name(&self, instance: InstanceDef) -> Symbol;
|
|
|
|
|
|
|
|
|
|
/// Check if this is an empty DropGlue shim.
|
|
|
|
|
fn is_empty_drop_shim(&self, def: InstanceDef) -> bool;
|
|
|
|
|
|
|
|
|
|
/// Convert a non-generic crate item into an instance.
|
|
|
|
|
/// This function will panic if the item is generic.
|
|
|
|
|
fn mono_instance(&self, def_id: DefId) -> Instance;
|
|
|
|
|
|
|
|
|
|
/// Item requires monomorphization.
|
|
|
|
|
fn requires_monomorphization(&self, def_id: DefId) -> bool;
|
|
|
|
|
|
|
|
|
|
/// Resolve an instance from the given function definition and generic arguments.
|
|
|
|
|
fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
|
|
|
|
|
|
|
|
|
|
/// Resolve an instance for drop_in_place for the given type.
|
|
|
|
|
fn resolve_drop_in_place(&self, ty: Ty) -> Instance;
|
|
|
|
|
|
|
|
|
|
/// Resolve instance for a function pointer.
|
|
|
|
|
fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
|
|
|
|
|
|
|
|
|
|
/// Resolve instance for a closure with the requested type.
|
|
|
|
|
fn resolve_closure(
|
|
|
|
|
&self,
|
|
|
|
|
def: ClosureDef,
|
|
|
|
|
args: &GenericArgs,
|
|
|
|
|
kind: ClosureKind,
|
|
|
|
|
) -> Option<Instance>;
|
|
|
|
|
|
|
|
|
|
/// Evaluate a static's initializer.
|
|
|
|
|
fn eval_static_initializer(&self, def: StaticDef) -> Result<Allocation, Error>;
|
|
|
|
|
|
|
|
|
|
/// Try to evaluate an instance into a constant.
|
|
|
|
|
fn eval_instance(&self, def: InstanceDef, const_ty: Ty) -> Result<Allocation, Error>;
|
|
|
|
|
|
|
|
|
|
/// Retrieve global allocation for the given allocation ID.
|
|
|
|
|
fn global_alloc(&self, id: AllocId) -> GlobalAlloc;
|
|
|
|
|
|
|
|
|
|
/// Retrieve the id for the virtual table.
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
/// Return information about the target machine.
|
|
|
|
|
fn target_info(&self) -> MachineInfo;
|
|
|
|
|
|
|
|
|
|
/// Get an instance ABI.
|
|
|
|
|
fn instance_abi(&self, def: InstanceDef) -> Result<FnAbi, Error>;
|
|
|
|
|
|
|
|
|
|
/// Get the ABI of a function pointer.
|
|
|
|
|
fn fn_ptr_abi(&self, fn_ptr: PolyFnSig) -> Result<FnAbi, Error>;
|
|
|
|
|
|
|
|
|
|
/// Get the layout of a type.
|
|
|
|
|
fn ty_layout(&self, ty: Ty) -> Result<Layout, Error>;
|
|
|
|
|
|
|
|
|
|
/// Get the layout shape.
|
|
|
|
|
fn layout_shape(&self, id: Layout) -> LayoutShape;
|
|
|
|
|
|
|
|
|
|
/// Get a debug string representation of a place.
|
|
|
|
|
fn place_pretty(&self, place: &Place) -> String;
|
|
|
|
|
|
|
|
|
|
/// Get the resulting type of binary operation.
|
|
|
|
|
fn binop_ty(&self, bin_op: BinOp, rhs: Ty, lhs: Ty) -> Ty;
|
|
|
|
|
|
|
|
|
|
/// Get the resulting type of unary operation.
|
|
|
|
|
fn unop_ty(&self, un_op: UnOp, arg: Ty) -> Ty;
|
|
|
|
|
|
|
|
|
|
/// Get all associated items of a definition.
|
|
|
|
|
fn associated_items(&self, def_id: DefId) -> AssocItems;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> SmirInterface for SmirContainer<'tcx, BridgeTys> {
|
|
|
|
|
fn entry_fn(&self) -> Option<CrateItem> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let did = cx.entry_fn();
|
|
|
|
|
Some(tables.crate_item(did?))
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-05 15:15:15 +08:00
|
|
|
/// Retrieve all items of the local crate that have a MIR associated with them.
|
|
|
|
|
fn all_local_items(&self) -> CrateItems {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
cx.all_local_items().iter().map(|did| tables.crate_item(*did)).collect()
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-05 15:15:15 +08:00
|
|
|
/// Retrieve the body of a function.
|
|
|
|
|
/// This function will panic if the body is not available.
|
|
|
|
|
fn mir_body(&self, item: DefId) -> mir::Body {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let did = tables[item];
|
|
|
|
|
cx.mir_body(did).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-05 15:15:15 +08:00
|
|
|
/// Check whether the body of a function is available.
|
|
|
|
|
fn has_body(&self, item: DefId) -> bool {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let tables_ref = &mut *tables;
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let def = item.internal(tables_ref, cx);
|
2025-05-06 12:41:34 +08:00
|
|
|
cx.has_body(def)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-05 15:15:15 +08:00
|
|
|
fn foreign_modules(&self, crate_num: CrateNum) -> Vec<ForeignModuleDef> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
cx.foreign_modules(crate_num.internal(&mut *tables, cx))
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|did| tables.foreign_module_def(*did))
|
|
|
|
|
.collect()
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-05 15:15:15 +08:00
|
|
|
/// Retrieve all functions defined in this crate.
|
|
|
|
|
fn crate_functions(&self, crate_num: CrateNum) -> Vec<FnDef> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
2025-05-06 12:41:34 +08:00
|
|
|
let krate = crate_num.internal(&mut *tables, cx);
|
|
|
|
|
cx.crate_functions(krate).iter().map(|did| tables.fn_def(*did)).collect()
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-05 15:15:15 +08:00
|
|
|
/// Retrieve all static items defined in this crate.
|
|
|
|
|
fn crate_statics(&self, crate_num: CrateNum) -> Vec<StaticDef> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
2025-05-06 12:41:34 +08:00
|
|
|
let krate = crate_num.internal(&mut *tables, cx);
|
|
|
|
|
cx.crate_statics(krate).iter().map(|did| tables.static_def(*did)).collect()
|
2025-05-05 15:15:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn foreign_module(&self, mod_def: ForeignModuleDef) -> ForeignModule {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let did = tables[mod_def.def_id()];
|
|
|
|
|
cx.foreign_module(did).stable(&mut *tables, cx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn foreign_items(&self, mod_def: ForeignModuleDef) -> Vec<ForeignDef> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let did = tables[mod_def.def_id()];
|
|
|
|
|
cx.foreign_items(did).iter().map(|did| tables.foreign_def(*did)).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn all_trait_decls(&self) -> TraitDecls {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
cx.all_trait_decls().map(|did| tables.trait_def(did)).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn trait_decls(&self, crate_num: CrateNum) -> TraitDecls {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let krate = crate_num.internal(&mut *tables, cx);
|
|
|
|
|
cx.trait_decls(krate).iter().map(|did| tables.trait_def(*did)).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn trait_decl(&self, trait_def: &TraitDef) -> TraitDecl {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let did = tables[trait_def.0];
|
|
|
|
|
cx.trait_decl(did).stable(&mut *tables, cx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn all_trait_impls(&self) -> ImplTraitDecls {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
cx.all_trait_impls().iter().map(|did| tables.impl_def(*did)).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn trait_impls(&self, crate_num: CrateNum) -> ImplTraitDecls {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let krate = crate_num.internal(&mut *tables, cx);
|
|
|
|
|
cx.trait_impls(krate).iter().map(|did| tables.impl_def(*did)).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn trait_impl(&self, trait_impl: &ImplDef) -> ImplTrait {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let did = tables[trait_impl.0];
|
|
|
|
|
cx.trait_impl(did).stable(&mut *tables, cx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn generics_of(&self, def_id: DefId) -> Generics {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let did = tables[def_id];
|
|
|
|
|
cx.generics_of(did).stable(&mut *tables, cx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn predicates_of(&self, def_id: DefId) -> GenericPredicates {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let did = tables[def_id];
|
|
|
|
|
let rustc_middle::ty::GenericPredicates { parent, predicates } = cx.predicates_of(did);
|
|
|
|
|
stable_mir::ty::GenericPredicates {
|
|
|
|
|
parent: parent.map(|did| tables.trait_def(did)),
|
|
|
|
|
predicates: predicates
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(clause, span)| {
|
|
|
|
|
(
|
|
|
|
|
clause.as_predicate().kind().skip_binder().stable(&mut *tables, cx),
|
|
|
|
|
span.stable(&mut *tables, cx),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.collect(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn explicit_predicates_of(&self, def_id: DefId) -> GenericPredicates {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let did = tables[def_id];
|
|
|
|
|
let rustc_middle::ty::GenericPredicates { parent, predicates } =
|
|
|
|
|
cx.explicit_predicates_of(did);
|
|
|
|
|
stable_mir::ty::GenericPredicates {
|
|
|
|
|
parent: parent.map(|did| tables.trait_def(did)),
|
|
|
|
|
predicates: predicates
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(clause, span)| {
|
|
|
|
|
(
|
|
|
|
|
clause.as_predicate().kind().skip_binder().stable(&mut *tables, cx),
|
|
|
|
|
span.stable(&mut *tables, cx),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.collect(),
|
|
|
|
|
}
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
2023-11-24 13:41:45 -08:00
|
|
|
/// Get information about the local crate.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn local_crate(&self) -> Crate {
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
self.smir_crate(cx.local_crate_num())
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
2023-11-24 13:41:45 -08:00
|
|
|
/// Retrieve a list of all external crates.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn external_crates(&self) -> Vec<Crate> {
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
cx.external_crates().iter().map(|crate_num| self.smir_crate(*crate_num)).collect()
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
|
|
|
|
/// Find a crate with the given name.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn find_crates(&self, name: &str) -> Vec<Crate> {
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
cx.find_crates(name).iter().map(|crate_num| self.smir_crate(*crate_num)).collect()
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
2025-04-18 14:34:20 +08:00
|
|
|
/// Returns the name of given `DefId`.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn def_name(&self, def_id: DefId, trimmed: bool) -> Symbol {
|
|
|
|
|
let tables = self.tables.borrow();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let did = tables[def_id];
|
|
|
|
|
cx.def_name(did, trimmed)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
2025-02-09 22:50:08 +01:00
|
|
|
/// Return registered tool attributes with the given attribute name.
|
2024-06-28 13:22:15 +08:00
|
|
|
///
|
2025-02-09 22:50:08 +01:00
|
|
|
/// FIXME(jdonszelmann): may panic on non-tool attributes. After more attribute work, non-tool
|
|
|
|
|
/// attributes will simply return an empty list.
|
|
|
|
|
///
|
|
|
|
|
/// Single segmented name like `#[clippy]` is specified as `&["clippy".to_string()]`.
|
2024-06-28 13:22:15 +08:00
|
|
|
/// Multi-segmented name like `#[rustfmt::skip]` is specified as `&["rustfmt".to_string(), "skip".to_string()]`.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn tool_attrs(&self, def_id: DefId, attr: &[Symbol]) -> Vec<Attribute> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let did = tables[def_id];
|
|
|
|
|
cx.tool_attrs(did, attr)
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(attr_str, span)| Attribute::new(attr_str, span.stable(&mut *tables, cx)))
|
|
|
|
|
.collect()
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2024-06-26 12:09:55 +08:00
|
|
|
|
2025-02-09 22:50:08 +01:00
|
|
|
/// Get all tool attributes of a definition.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn all_tool_attrs(&self, def_id: DefId) -> Vec<Attribute> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let did = tables[def_id];
|
|
|
|
|
cx.all_tool_attrs(did)
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(attr_str, span)| Attribute::new(attr_str, span.stable(&mut *tables, cx)))
|
|
|
|
|
.collect()
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2024-06-28 13:22:15 +08:00
|
|
|
|
2025-04-18 14:34:20 +08:00
|
|
|
/// Returns printable, human readable form of `Span`.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn span_to_string(&self, span: Span) -> String {
|
|
|
|
|
let tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let sp = tables.spans[span];
|
|
|
|
|
cx.span_to_string(sp)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
2025-04-18 14:34:20 +08:00
|
|
|
/// Return filename from given `Span`, for diagnostic purposes.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn get_filename(&self, span: &Span) -> Filename {
|
|
|
|
|
let tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let sp = tables.spans[*span];
|
|
|
|
|
cx.get_filename(sp)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
2025-04-18 14:34:20 +08:00
|
|
|
/// Return lines corresponding to this `Span`.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn get_lines(&self, span: &Span) -> LineInfo {
|
|
|
|
|
let tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let sp = tables.spans[*span];
|
|
|
|
|
let lines = cx.get_lines(sp);
|
|
|
|
|
LineInfo::from(lines)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
2025-04-18 14:34:20 +08:00
|
|
|
/// Returns the `kind` of given `DefId`.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn item_kind(&self, item: CrateItem) -> ItemKind {
|
|
|
|
|
let tables = self.tables.borrow();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let did = tables[item.0];
|
|
|
|
|
new_item_kind(cx.def_kind(did))
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
|
|
|
|
/// Returns whether this is a foreign item.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn is_foreign_item(&self, item: DefId) -> bool {
|
|
|
|
|
let tables = self.tables.borrow();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let did = tables[item];
|
|
|
|
|
cx.is_foreign_item(did)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
2024-02-12 19:44:35 +00:00
|
|
|
/// Returns the kind of a given foreign item.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn foreign_item_kind(&self, def: ForeignDef) -> ForeignItemKind {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let def_id = tables[def.def_id()];
|
|
|
|
|
let def_kind = cx.foreign_item_kind(def_id);
|
|
|
|
|
match def_kind {
|
|
|
|
|
DefKind::Fn => ForeignItemKind::Fn(tables.fn_def(def_id)),
|
|
|
|
|
DefKind::Static { .. } => ForeignItemKind::Static(tables.static_def(def_id)),
|
|
|
|
|
DefKind::ForeignTy => {
|
|
|
|
|
use rustc_smir::context::SmirTy;
|
|
|
|
|
ForeignItemKind::Type(tables.intern_ty(cx.new_foreign(def_id)))
|
|
|
|
|
}
|
|
|
|
|
def_kind => unreachable!("Unexpected kind for a foreign item: {:?}", def_kind),
|
|
|
|
|
}
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2024-02-12 19:44:35 +00:00
|
|
|
|
2025-04-18 14:34:20 +08:00
|
|
|
/// Returns the kind of a given algebraic data type.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn adt_kind(&self, def: AdtDef) -> AdtKind {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
cx.adt_kind(def.internal(&mut *tables, cx)).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
|
|
|
|
/// Returns if the ADT is a box.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn adt_is_box(&self, def: AdtDef) -> bool {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
cx.adt_is_box(def.internal(&mut *tables, cx))
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
2023-12-11 14:53:39 -08:00
|
|
|
/// Returns whether this ADT is simd.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn adt_is_simd(&self, def: AdtDef) -> bool {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
cx.adt_is_simd(def.internal(&mut *tables, cx))
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-12-11 14:53:39 -08:00
|
|
|
|
2023-12-15 13:18:41 -08:00
|
|
|
/// Returns whether this definition is a C string.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn adt_is_cstr(&self, def: AdtDef) -> bool {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
cx.adt_is_cstr(def.0.internal(&mut *tables, cx))
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-12-15 13:18:41 -08:00
|
|
|
|
2025-06-03 13:38:00 +09:00
|
|
|
/// Returns the representation options for this ADT
|
|
|
|
|
pub(crate) fn adt_repr(&self, def: AdtDef) -> ReprOptions {
|
|
|
|
|
self.cx.adt_repr(def)
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 16:20:35 -08:00
|
|
|
/// Retrieve the function signature for the given generic arguments.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn fn_sig(&self, def: FnDef, args: &GenericArgs) -> PolyFnSig {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let def_id = def.0.internal(&mut *tables, cx);
|
|
|
|
|
let args_ref = args.internal(&mut *tables, cx);
|
|
|
|
|
cx.fn_sig(def_id, args_ref).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-12-01 16:20:35 -08:00
|
|
|
|
2024-05-20 18:11:07 +00:00
|
|
|
/// Retrieve the intrinsic definition if the item corresponds one.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn intrinsic(&self, item: DefId) -> Option<IntrinsicDef> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let def_id = item.internal(&mut *tables, cx);
|
|
|
|
|
cx.intrinsic(def_id).map(|_| IntrinsicDef(item))
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2024-05-20 18:11:07 +00:00
|
|
|
|
|
|
|
|
/// Retrieve the plain function name of an intrinsic.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn intrinsic_name(&self, def: IntrinsicDef) -> Symbol {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let def_id = def.0.internal(&mut *tables, cx);
|
|
|
|
|
cx.intrinsic_name(def_id)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2024-05-20 18:11:07 +00:00
|
|
|
|
2023-12-11 14:53:39 -08:00
|
|
|
/// Retrieve the closure signature for the given generic arguments.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn closure_sig(&self, args: &GenericArgs) -> PolyFnSig {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let args_ref = args.internal(&mut *tables, cx);
|
|
|
|
|
cx.closure_sig(args_ref).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-12-11 14:53:39 -08:00
|
|
|
|
2023-11-30 20:22:20 -08:00
|
|
|
/// The number of variants in this ADT.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn adt_variants_len(&self, def: AdtDef) -> usize {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
cx.adt_variants_len(def.internal(&mut *tables, cx))
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-30 20:22:20 -08:00
|
|
|
|
2025-05-27 11:12:05 +03:00
|
|
|
/// Discriminant for a given variant index of AdtDef
|
|
|
|
|
pub(crate) fn adt_discr_for_variant(&self, adt: AdtDef, variant: VariantIdx) -> Discr {
|
|
|
|
|
self.cx.adt_discr_for_variant(adt, variant)
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-27 11:18:37 +03:00
|
|
|
/// Discriminant for a given variand index and args of a coroutine
|
|
|
|
|
pub(crate) fn coroutine_discr_for_variant(
|
|
|
|
|
&self,
|
|
|
|
|
coroutine: CoroutineDef,
|
|
|
|
|
args: &GenericArgs,
|
|
|
|
|
variant: VariantIdx,
|
|
|
|
|
) -> Discr {
|
|
|
|
|
self.cx.coroutine_discr_for_variant(coroutine, args, variant)
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-30 20:22:20 -08:00
|
|
|
/// The name of a variant.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn variant_name(&self, def: VariantDef) -> Symbol {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
cx.variant_name(def.internal(&mut *tables, cx))
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-05 15:15:15 +08:00
|
|
|
fn variant_fields(&self, def: VariantDef) -> Vec<FieldDef> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
def.internal(&mut *tables, cx).fields.iter().map(|f| f.stable(&mut *tables, cx)).collect()
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-30 20:22:20 -08:00
|
|
|
|
2023-11-24 13:41:45 -08:00
|
|
|
/// Evaluate constant as a target usize.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn eval_target_usize(&self, mir_const: &MirConst) -> Result<u64, Error> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let cnst = mir_const.internal(&mut *tables, cx);
|
|
|
|
|
cx.eval_target_usize(cnst)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-05 15:15:15 +08:00
|
|
|
fn eval_target_usize_ty(&self, ty_const: &TyConst) -> Result<u64, Error> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let cnst = ty_const.internal(&mut *tables, cx);
|
|
|
|
|
cx.eval_target_usize_ty(cnst)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
2024-03-01 11:00:33 -08:00
|
|
|
/// Create a new zero-sized constant.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn try_new_const_zst(&self, ty: Ty) -> Result<MirConst, Error> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let ty_internal = ty.internal(&mut *tables, cx);
|
|
|
|
|
cx.try_new_const_zst(ty_internal).map(|cnst| cnst.stable(&mut *tables, cx))
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2024-03-01 11:00:33 -08:00
|
|
|
|
|
|
|
|
/// Create a new constant that represents the given string value.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn new_const_str(&self, value: &str) -> MirConst {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
cx.new_const_str(value).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2024-03-01 11:00:33 -08:00
|
|
|
|
|
|
|
|
/// Create a new constant that represents the given boolean value.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn new_const_bool(&self, value: bool) -> MirConst {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
cx.new_const_bool(value).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2024-03-01 11:00:33 -08:00
|
|
|
|
|
|
|
|
/// Create a new constant that represents the given value.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn try_new_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<MirConst, Error> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let ty = cx.ty_new_uint(uint_ty.internal(&mut *tables, cx));
|
|
|
|
|
cx.try_new_const_uint(value, ty).map(|cnst| cnst.stable(&mut *tables, cx))
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-05 15:15:15 +08:00
|
|
|
fn try_new_ty_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<TyConst, Error> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let ty = cx.ty_new_uint(uint_ty.internal(&mut *tables, cx));
|
|
|
|
|
cx.try_new_ty_const_uint(value, ty).map(|cnst| cnst.stable(&mut *tables, cx))
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
|
|
|
|
/// Create a new type from the given kind.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn new_rigid_ty(&self, kind: RigidTy) -> Ty {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let internal_kind = kind.internal(&mut *tables, cx);
|
|
|
|
|
cx.new_rigid_ty(internal_kind).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
2023-12-06 13:45:07 -08:00
|
|
|
/// Create a new box type, `Box<T>`, for the given inner type `T`.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn new_box_ty(&self, ty: Ty) -> Ty {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let inner = ty.internal(&mut *tables, cx);
|
|
|
|
|
cx.new_box_ty(inner).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-12-06 13:45:07 -08:00
|
|
|
|
2023-11-24 13:41:45 -08:00
|
|
|
/// Returns the type of given crate item.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn def_ty(&self, item: DefId) -> Ty {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let inner = item.internal(&mut *tables, cx);
|
|
|
|
|
cx.def_ty(inner).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
2023-12-04 20:08:25 -08:00
|
|
|
/// Returns the type of given definition instantiated with the given arguments.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn def_ty_with_args(&self, item: DefId, args: &GenericArgs) -> Ty {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let inner = item.internal(&mut *tables, cx);
|
|
|
|
|
let args_ref = args.internal(&mut *tables, cx);
|
|
|
|
|
cx.def_ty_with_args(inner, args_ref).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-12-04 20:08:25 -08:00
|
|
|
|
2023-11-24 13:41:45 -08:00
|
|
|
/// Returns literal value of a const as a string.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn mir_const_pretty(&self, cnst: &MirConst) -> String {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
cnst.internal(&mut *tables, cx).to_string()
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
2025-04-18 14:34:20 +08:00
|
|
|
/// `Span` of an item.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn span_of_an_item(&self, def_id: DefId) -> Span {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let did = tables[def_id];
|
|
|
|
|
cx.span_of_an_item(did).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
2025-05-05 15:15:15 +08:00
|
|
|
fn ty_const_pretty(&self, ct: TyConstId) -> String {
|
|
|
|
|
let tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
cx.ty_const_pretty(tables.ty_consts[ct])
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2024-06-04 00:39:35 +01:00
|
|
|
|
2024-03-20 15:55:35 -07:00
|
|
|
/// Obtain the representation of a type.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn ty_pretty(&self, ty: Ty) -> String {
|
|
|
|
|
let tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
cx.ty_pretty(tables.types[ty])
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2024-03-20 15:55:35 -07:00
|
|
|
|
2025-05-05 15:15:15 +08:00
|
|
|
/// Obtain the kind of a type.
|
|
|
|
|
fn ty_kind(&self, ty: Ty) -> TyKind {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
cx.ty_kind(tables.types[ty]).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
2025-04-18 14:34:20 +08:00
|
|
|
/// Get the discriminant Ty for this Ty if there's one.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn rigid_ty_discriminant_ty(&self, ty: &RigidTy) -> Ty {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let internal_kind = ty.internal(&mut *tables, cx);
|
|
|
|
|
cx.rigid_ty_discriminant_ty(internal_kind).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-12-06 13:45:07 -08:00
|
|
|
|
2023-12-01 16:20:35 -08:00
|
|
|
/// Get the body of an Instance which is already monomorphized.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn instance_body(&self, instance: InstanceDef) -> Option<Body> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let instance = tables.instances[instance];
|
2025-05-06 12:41:34 +08:00
|
|
|
cx.instance_body(instance).map(|body| body.stable(&mut *tables, cx))
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
2024-02-12 15:39:32 +09:00
|
|
|
/// Get the instance type with generic instantiations applied and lifetimes erased.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn instance_ty(&self, instance: InstanceDef) -> Ty {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let instance = tables.instances[instance];
|
|
|
|
|
cx.instance_ty(instance).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
2023-12-19 14:08:48 -08:00
|
|
|
/// Get the instantiation types.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn instance_args(&self, def: InstanceDef) -> GenericArgs {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let instance = tables.instances[def];
|
|
|
|
|
cx.instance_args(instance).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-12-19 14:08:48 -08:00
|
|
|
|
2023-11-24 13:41:45 -08:00
|
|
|
/// Get the instance.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn instance_def_id(&self, instance: InstanceDef) -> DefId {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let instance = tables.instances[instance];
|
|
|
|
|
cx.instance_def_id(instance, &mut *tables)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
|
|
|
|
/// Get the instance mangled name.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn instance_mangled_name(&self, instance: InstanceDef) -> Symbol {
|
|
|
|
|
let tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let instance = tables.instances[instance];
|
|
|
|
|
cx.instance_mangled_name(instance)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
2023-12-01 16:20:35 -08:00
|
|
|
/// Check if this is an empty DropGlue shim.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn is_empty_drop_shim(&self, def: InstanceDef) -> bool {
|
|
|
|
|
let tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let instance = tables.instances[def];
|
|
|
|
|
cx.is_empty_drop_shim(instance)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-12-01 16:20:35 -08:00
|
|
|
|
2023-11-24 13:41:45 -08:00
|
|
|
/// Convert a non-generic crate item into an instance.
|
|
|
|
|
/// This function will panic if the item is generic.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn mono_instance(&self, def_id: DefId) -> Instance {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let did = tables[def_id];
|
|
|
|
|
cx.mono_instance(did).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
|
|
|
|
/// Item requires monomorphization.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn requires_monomorphization(&self, def_id: DefId) -> bool {
|
|
|
|
|
let tables = self.tables.borrow();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let did = tables[def_id];
|
|
|
|
|
cx.requires_monomorphization(did)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
|
|
|
|
/// Resolve an instance from the given function definition and generic arguments.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let def_id = def.0.internal(&mut *tables, cx);
|
|
|
|
|
let args_ref = args.internal(&mut *tables, cx);
|
|
|
|
|
cx.resolve_instance(def_id, args_ref).map(|inst| inst.stable(&mut *tables, cx))
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
|
|
|
|
/// Resolve an instance for drop_in_place for the given type.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn resolve_drop_in_place(&self, ty: Ty) -> Instance {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let internal_ty = ty.internal(&mut *tables, cx);
|
|
|
|
|
|
|
|
|
|
cx.resolve_drop_in_place(internal_ty).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
|
|
|
|
/// Resolve instance for a function pointer.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let def_id = def.0.internal(&mut *tables, cx);
|
|
|
|
|
let args_ref = args.internal(&mut *tables, cx);
|
|
|
|
|
cx.resolve_for_fn_ptr(def_id, args_ref).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
|
|
|
|
/// Resolve instance for a closure with the requested type.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn resolve_closure(
|
2023-11-24 13:41:45 -08:00
|
|
|
&self,
|
|
|
|
|
def: ClosureDef,
|
|
|
|
|
args: &GenericArgs,
|
|
|
|
|
kind: ClosureKind,
|
2025-04-18 14:34:20 +08:00
|
|
|
) -> Option<Instance> {
|
2025-05-05 15:15:15 +08:00
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let def_id = def.0.internal(&mut *tables, cx);
|
|
|
|
|
let args_ref = args.internal(&mut *tables, cx);
|
|
|
|
|
let closure_kind = kind.internal(&mut *tables, cx);
|
|
|
|
|
cx.resolve_closure(def_id, args_ref, closure_kind).map(|inst| inst.stable(&mut *tables, cx))
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
|
|
|
|
/// Evaluate a static's initializer.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn eval_static_initializer(&self, def: StaticDef) -> Result<Allocation, Error> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let def_id = def.0.internal(&mut *tables, cx);
|
|
|
|
|
|
|
|
|
|
cx.eval_static_initializer(def_id).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
2023-12-06 13:39:55 -08:00
|
|
|
/// Try to evaluate an instance into a constant.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn eval_instance(&self, def: InstanceDef, const_ty: Ty) -> Result<Allocation, Error> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let instance = tables.instances[def];
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let const_ty = const_ty.internal(&mut *tables, cx);
|
|
|
|
|
cx.eval_instance(instance)
|
|
|
|
|
.map(|const_val| alloc::try_new_allocation(const_ty, const_val, &mut *tables, cx))
|
|
|
|
|
.map_err(|e| e.stable(&mut *tables, cx))?
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-12-06 13:39:55 -08:00
|
|
|
|
2023-11-24 13:41:45 -08:00
|
|
|
/// Retrieve global allocation for the given allocation ID.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn global_alloc(&self, id: AllocId) -> GlobalAlloc {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let alloc_id = id.internal(&mut *tables, cx);
|
|
|
|
|
cx.global_alloc(alloc_id).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
|
|
|
|
|
/// Retrieve the id for the virtual table.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn vtable_allocation(&self, global_alloc: &GlobalAlloc) -> Option<AllocId> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let GlobalAlloc::VTable(ty, trait_ref) = global_alloc else {
|
|
|
|
|
return None;
|
|
|
|
|
};
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let ty = ty.internal(&mut *tables, cx);
|
|
|
|
|
let trait_ref = trait_ref.internal(&mut *tables, cx);
|
|
|
|
|
let alloc_id = cx.vtable_allocation(ty, trait_ref);
|
|
|
|
|
Some(alloc_id.stable(&mut *tables, cx))
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-05 15:15:15 +08:00
|
|
|
fn krate(&self, def_id: DefId) -> Crate {
|
|
|
|
|
let tables = self.tables.borrow();
|
|
|
|
|
self.smir_crate(tables[def_id].krate)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-05 15:15:15 +08:00
|
|
|
fn instance_name(&self, def: InstanceDef, trimmed: bool) -> Symbol {
|
|
|
|
|
let tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let instance = tables.instances[def];
|
|
|
|
|
cx.instance_name(instance, trimmed)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-12-06 13:39:55 -08:00
|
|
|
|
2023-12-06 21:33:49 -08:00
|
|
|
/// Return information about the target machine.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn target_info(&self) -> MachineInfo {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
MachineInfo {
|
|
|
|
|
endian: cx.target_endian().stable(&mut *tables, cx),
|
|
|
|
|
pointer_width: MachineSize::from_bits(cx.target_pointer_size()),
|
|
|
|
|
}
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-12-18 19:52:25 +00:00
|
|
|
|
|
|
|
|
/// Get an instance ABI.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn instance_abi(&self, def: InstanceDef) -> Result<FnAbi, Error> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let instance = tables.instances[def];
|
2025-05-06 12:41:34 +08:00
|
|
|
cx.instance_abi(instance).map(|fn_abi| fn_abi.stable(&mut *tables, cx))
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-12-18 19:52:25 +00:00
|
|
|
|
2024-06-20 11:08:12 +08:00
|
|
|
/// Get the ABI of a function pointer.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn fn_ptr_abi(&self, fn_ptr: PolyFnSig) -> Result<FnAbi, Error> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let sig = fn_ptr.internal(&mut *tables, cx);
|
2025-05-06 12:41:34 +08:00
|
|
|
cx.fn_ptr_abi(sig).map(|fn_abi| fn_abi.stable(&mut *tables, cx))
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2024-06-20 11:08:12 +08:00
|
|
|
|
2023-12-18 19:52:25 +00:00
|
|
|
/// Get the layout of a type.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn ty_layout(&self, ty: Ty) -> Result<Layout, Error> {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let internal_ty = ty.internal(&mut *tables, cx);
|
2025-05-06 12:41:34 +08:00
|
|
|
cx.ty_layout(internal_ty).map(|layout| layout.stable(&mut *tables, cx))
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-12-18 19:52:25 +00:00
|
|
|
|
|
|
|
|
/// Get the layout shape.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn layout_shape(&self, id: Layout) -> LayoutShape {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
id.internal(&mut *tables, cx).0.stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2024-03-20 15:55:35 -07:00
|
|
|
|
|
|
|
|
/// Get a debug string representation of a place.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn place_pretty(&self, place: &Place) -> String {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
|
|
|
|
|
format!("{:?}", place.internal(&mut *tables, cx))
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2024-04-08 15:41:00 -07:00
|
|
|
|
|
|
|
|
/// Get the resulting type of binary operation.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn binop_ty(&self, bin_op: BinOp, rhs: Ty, lhs: Ty) -> Ty {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let rhs_internal = rhs.internal(&mut *tables, cx);
|
|
|
|
|
let lhs_internal = lhs.internal(&mut *tables, cx);
|
|
|
|
|
let bin_op_internal = bin_op.internal(&mut *tables, cx);
|
|
|
|
|
cx.binop_ty(bin_op_internal, rhs_internal, lhs_internal).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2024-04-21 16:11:01 -07:00
|
|
|
|
|
|
|
|
/// Get the resulting type of unary operation.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn unop_ty(&self, un_op: UnOp, arg: Ty) -> Ty {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let un_op = un_op.internal(&mut *tables, cx);
|
|
|
|
|
let arg = arg.internal(&mut *tables, cx);
|
|
|
|
|
cx.unop_ty(un_op, arg).stable(&mut *tables, cx)
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2025-04-01 17:11:53 +08:00
|
|
|
|
|
|
|
|
/// Get all associated items of a definition.
|
2025-05-05 15:15:15 +08:00
|
|
|
fn associated_items(&self, def_id: DefId) -> AssocItems {
|
|
|
|
|
let mut tables = self.tables.borrow_mut();
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let did = tables[def_id];
|
|
|
|
|
cx.associated_items(did).iter().map(|assoc| assoc.stable(&mut *tables, cx)).collect()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-11 09:03:51 +00:00
|
|
|
impl<'tcx> Helper for SmirContainer<'tcx, BridgeTys> {
|
2025-05-05 15:15:15 +08:00
|
|
|
fn smir_crate(&self, crate_num: rustc_span::def_id::CrateNum) -> Crate {
|
|
|
|
|
let cx = &*self.cx.borrow();
|
|
|
|
|
let name = cx.crate_name(crate_num);
|
|
|
|
|
let is_local = cx.crate_is_local(crate_num);
|
|
|
|
|
let id = cx.crate_num_id(crate_num);
|
|
|
|
|
debug!(?name, ?crate_num, "smir_crate");
|
|
|
|
|
Crate { id, name, is_local }
|
2025-04-18 14:34:20 +08:00
|
|
|
}
|
2023-11-24 13:41:45 -08:00
|
|
|
}
|
|
|
|
|
|
2025-05-11 09:03:51 +00:00
|
|
|
trait Helper {
|
|
|
|
|
fn smir_crate(&self, crate_num: rustc_span::def_id::CrateNum) -> Crate;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-18 14:34:20 +08:00
|
|
|
// A thread local variable that stores a pointer to [`SmirInterface`].
|
2024-04-29 16:57:03 +10:00
|
|
|
scoped_tls::scoped_thread_local!(static TLV: Cell<*const ()>);
|
2023-11-24 13:41:45 -08:00
|
|
|
|
2025-05-05 15:15:15 +08:00
|
|
|
pub(crate) fn run<F, T>(interface: &dyn SmirInterface, f: F) -> Result<T, Error>
|
2023-11-24 13:41:45 -08:00
|
|
|
where
|
|
|
|
|
F: FnOnce() -> T,
|
|
|
|
|
{
|
|
|
|
|
if TLV.is_set() {
|
|
|
|
|
Err(Error::from("StableMIR already running"))
|
|
|
|
|
} else {
|
2025-05-05 15:15:15 +08:00
|
|
|
let ptr: *const () = (&raw const interface) as _;
|
2023-11-24 13:41:45 -08:00
|
|
|
TLV.set(&Cell::new(ptr), || Ok(f()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-18 14:34:20 +08:00
|
|
|
/// Execute the given function with access the [`SmirInterface`].
|
2023-11-24 13:41:45 -08:00
|
|
|
///
|
2025-04-18 14:34:20 +08:00
|
|
|
/// I.e., This function will load the current interface and calls a function with it.
|
2023-11-24 13:41:45 -08:00
|
|
|
/// Do not nest these, as that will ICE.
|
2025-05-05 15:15:15 +08:00
|
|
|
pub(crate) fn with<R>(f: impl FnOnce(&dyn SmirInterface) -> R) -> R {
|
2023-11-24 13:41:45 -08:00
|
|
|
assert!(TLV.is_set());
|
|
|
|
|
TLV.with(|tlv| {
|
|
|
|
|
let ptr = tlv.get();
|
|
|
|
|
assert!(!ptr.is_null());
|
2025-05-05 15:15:15 +08:00
|
|
|
f(unsafe { *(ptr as *const &dyn SmirInterface) })
|
2023-11-24 13:41:45 -08:00
|
|
|
})
|
|
|
|
|
}
|