Add place.ty() and Ty build from a kind to smir
This commit is contained in:
parent
46ecc10c69
commit
d3fa6a0e35
11 changed files with 321 additions and 41 deletions
|
@ -9,8 +9,8 @@ use rustc_middle::ty::{self as rustc_ty, Ty as InternalTy};
|
|||
use rustc_span::Symbol;
|
||||
use stable_mir::mir::mono::{Instance, MonoItem, StaticDef};
|
||||
use stable_mir::ty::{
|
||||
Binder, BoundRegionKind, BoundTyKind, BoundVariableKind, ClosureKind, Const, GenericArgKind,
|
||||
GenericArgs, Region, TraitRef, Ty,
|
||||
AdtDef, Binder, BoundRegionKind, BoundTyKind, BoundVariableKind, ClosureKind, Const, FloatTy,
|
||||
GenericArgKind, GenericArgs, IntTy, Region, RigidTy, TraitRef, Ty, UintTy,
|
||||
};
|
||||
use stable_mir::{AllocId, CrateItem, DefId};
|
||||
|
||||
|
@ -63,6 +63,82 @@ impl<'tcx> RustcInternal<'tcx> for Ty {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'tcx> RustcInternal<'tcx> for RigidTy {
|
||||
type T = rustc_ty::TyKind<'tcx>;
|
||||
|
||||
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T {
|
||||
match self {
|
||||
RigidTy::Bool => rustc_ty::TyKind::Bool,
|
||||
RigidTy::Char => rustc_ty::TyKind::Char,
|
||||
RigidTy::Int(int_ty) => rustc_ty::TyKind::Int(int_ty.internal(tables)),
|
||||
RigidTy::Uint(uint_ty) => rustc_ty::TyKind::Uint(uint_ty.internal(tables)),
|
||||
RigidTy::Float(float_ty) => rustc_ty::TyKind::Float(float_ty.internal(tables)),
|
||||
RigidTy::Never => rustc_ty::TyKind::Never,
|
||||
RigidTy::Array(ty, cnst) => {
|
||||
rustc_ty::TyKind::Array(ty.internal(tables), ty_const(cnst, tables))
|
||||
}
|
||||
RigidTy::Adt(def, args) => {
|
||||
rustc_ty::TyKind::Adt(def.internal(tables), args.internal(tables))
|
||||
}
|
||||
RigidTy::Str => rustc_ty::TyKind::Str,
|
||||
RigidTy::Slice(ty) => rustc_ty::TyKind::Slice(ty.internal(tables)),
|
||||
RigidTy::RawPtr(..)
|
||||
| RigidTy::Ref(..)
|
||||
| RigidTy::Foreign(_)
|
||||
| RigidTy::FnDef(_, _)
|
||||
| RigidTy::FnPtr(_)
|
||||
| RigidTy::Closure(..)
|
||||
| RigidTy::Coroutine(..)
|
||||
| RigidTy::CoroutineWitness(..)
|
||||
| RigidTy::Dynamic(..)
|
||||
| RigidTy::Tuple(..) => {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> RustcInternal<'tcx> for IntTy {
|
||||
type T = rustc_ty::IntTy;
|
||||
|
||||
fn internal(&self, _tables: &mut Tables<'tcx>) -> Self::T {
|
||||
match self {
|
||||
IntTy::Isize => rustc_ty::IntTy::Isize,
|
||||
IntTy::I8 => rustc_ty::IntTy::I8,
|
||||
IntTy::I16 => rustc_ty::IntTy::I16,
|
||||
IntTy::I32 => rustc_ty::IntTy::I32,
|
||||
IntTy::I64 => rustc_ty::IntTy::I64,
|
||||
IntTy::I128 => rustc_ty::IntTy::I128,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> RustcInternal<'tcx> for UintTy {
|
||||
type T = rustc_ty::UintTy;
|
||||
|
||||
fn internal(&self, _tables: &mut Tables<'tcx>) -> Self::T {
|
||||
match self {
|
||||
UintTy::Usize => rustc_ty::UintTy::Usize,
|
||||
UintTy::U8 => rustc_ty::UintTy::U8,
|
||||
UintTy::U16 => rustc_ty::UintTy::U16,
|
||||
UintTy::U32 => rustc_ty::UintTy::U32,
|
||||
UintTy::U64 => rustc_ty::UintTy::U64,
|
||||
UintTy::U128 => rustc_ty::UintTy::U128,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> RustcInternal<'tcx> for FloatTy {
|
||||
type T = rustc_ty::FloatTy;
|
||||
|
||||
fn internal(&self, _tables: &mut Tables<'tcx>) -> Self::T {
|
||||
match self {
|
||||
FloatTy::F32 => rustc_ty::FloatTy::F32,
|
||||
FloatTy::F64 => rustc_ty::FloatTy::F64,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn ty_const<'tcx>(constant: &Const, tables: &mut Tables<'tcx>) -> rustc_ty::Const<'tcx> {
|
||||
match constant.internal(tables) {
|
||||
rustc_middle::mir::Const::Ty(c) => c,
|
||||
|
@ -183,6 +259,17 @@ impl<'tcx> RustcInternal<'tcx> for ClosureKind {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'tcx> RustcInternal<'tcx> for AdtDef {
|
||||
type T = rustc_ty::AdtDef<'tcx>;
|
||||
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T {
|
||||
let ty = tables.tcx.type_of(self.0.internal(&mut *tables)).instantiate_identity().kind();
|
||||
let rustc_ty::TyKind::Adt(def, _) = ty else {
|
||||
panic!("Expected an ADT definition, but found: {ty:?}")
|
||||
};
|
||||
*def
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx, T> RustcInternal<'tcx> for &T
|
||||
where
|
||||
T: RustcInternal<'tcx>,
|
||||
|
|
|
@ -14,7 +14,7 @@ use rustc_hir::def::DefKind;
|
|||
use rustc_middle::mir;
|
||||
use rustc_middle::mir::interpret::{alloc_range, AllocId};
|
||||
use rustc_middle::mir::mono::MonoItem;
|
||||
use rustc_middle::ty::{self, Instance, ParamEnv, Ty, TyCtxt, Variance};
|
||||
use rustc_middle::ty::{self, Instance, ParamEnv, ScalarInt, Ty, TyCtxt, Variance};
|
||||
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
|
||||
use rustc_target::abi::FieldIdx;
|
||||
use stable_mir::mir::mono::InstanceDef;
|
||||
|
@ -24,7 +24,7 @@ use stable_mir::ty::{
|
|||
FloatTy, FnDef, GenericArgs, GenericParamDef, IntTy, LineInfo, Movability, RigidTy, Span,
|
||||
TyKind, UintTy,
|
||||
};
|
||||
use stable_mir::{self, opaque, Context, CrateItem, Filename, ItemKind};
|
||||
use stable_mir::{self, opaque, Context, CrateItem, Error, Filename, ItemKind};
|
||||
use std::cell::RefCell;
|
||||
use tracing::debug;
|
||||
|
||||
|
@ -91,13 +91,14 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
|
|||
new_item_kind(tables.tcx.def_kind(tables[item.0]))
|
||||
}
|
||||
|
||||
fn is_foreign_item(&self, item: CrateItem) -> bool {
|
||||
let tables = self.0.borrow();
|
||||
tables.tcx.is_foreign_item(tables[item.0])
|
||||
}
|
||||
|
||||
fn adt_kind(&self, def: AdtDef) -> AdtKind {
|
||||
let mut tables = self.0.borrow_mut();
|
||||
let ty = tables.tcx.type_of(def.0.internal(&mut *tables)).instantiate_identity().kind();
|
||||
let ty::TyKind::Adt(def, _) = ty else {
|
||||
panic!("Expected an ADT definition, but found: {ty:?}")
|
||||
};
|
||||
def.adt_kind().stable(&mut *tables)
|
||||
def.internal(&mut *tables).adt_kind().stable(&mut *tables)
|
||||
}
|
||||
|
||||
fn def_ty(&self, item: stable_mir::DefId) -> stable_mir::ty::Ty {
|
||||
|
@ -302,6 +303,37 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
|
|||
let closure_kind = kind.internal(&mut *tables);
|
||||
Instance::resolve_closure(tables.tcx, def_id, args_ref, closure_kind).stable(&mut *tables)
|
||||
}
|
||||
|
||||
fn adt_is_box(&self, def: AdtDef) -> bool {
|
||||
let mut tables = self.0.borrow_mut();
|
||||
def.internal(&mut *tables).is_box()
|
||||
}
|
||||
|
||||
fn eval_target_usize(&self, cnst: &Const) -> Result<u64, Error> {
|
||||
let mut tables = self.0.borrow_mut();
|
||||
let mir_const = cnst.internal(&mut *tables);
|
||||
mir_const
|
||||
.try_eval_target_usize(tables.tcx, ParamEnv::empty())
|
||||
.ok_or_else(|| Error::new(format!("Const `{cnst:?}` cannot be encoded as u64")))
|
||||
}
|
||||
|
||||
fn usize_to_const(&self, val: u64) -> Result<Const, Error> {
|
||||
let mut tables = self.0.borrow_mut();
|
||||
let ty = tables.tcx.types.usize;
|
||||
let size = tables.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap().size;
|
||||
|
||||
let scalar = ScalarInt::try_from_uint(val, size).ok_or_else(|| {
|
||||
Error::new(format!("Value overflow: cannot covert `{val}` to usize."))
|
||||
})?;
|
||||
Ok(ty::Const::new_value(tables.tcx, ty::ValTree::from_scalar_int(scalar), ty)
|
||||
.stable(&mut *tables))
|
||||
}
|
||||
|
||||
fn new_rigid_ty(&self, kind: RigidTy) -> stable_mir::ty::Ty {
|
||||
let mut tables = self.0.borrow_mut();
|
||||
let internal_kind = kind.internal(&mut *tables);
|
||||
tables.tcx.mk_ty_from_kind(internal_kind).stable(&mut *tables)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct TablesWrapper<'tcx>(pub(crate) RefCell<Tables<'tcx>>);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue