Add method to get type of an Rvalue in StableMIR

This commit is contained in:
Celina G. Val
2023-12-06 13:45:07 -08:00
parent 7a34091eed
commit 281b65aad9
5 changed files with 177 additions and 0 deletions

View File

@@ -31,15 +31,50 @@ impl Ty {
Ok(Ty::from_rigid_kind(RigidTy::Array(elem_ty, Const::try_from_target_usize(size)?)))
}
/// Create a new array type from Const length.
pub fn new_array_with_const_len(elem_ty: Ty, len: Const) -> Ty {
Ty::from_rigid_kind(RigidTy::Array(elem_ty, len))
}
/// Create a new pointer type.
pub fn new_ptr(pointee_ty: Ty, mutability: Mutability) -> Ty {
Ty::from_rigid_kind(RigidTy::RawPtr(pointee_ty, mutability))
}
/// Create a new reference type.
pub fn new_ref(reg: Region, pointee_ty: Ty, mutability: Mutability) -> Ty {
Ty::from_rigid_kind(RigidTy::Ref(reg, pointee_ty, mutability))
}
/// Create a new pointer type.
pub fn new_tuple(tys: &[Ty]) -> Ty {
Ty::from_rigid_kind(RigidTy::Tuple(Vec::from(tys)))
}
/// Create a new closure type.
pub fn new_closure(def: ClosureDef, args: GenericArgs) -> Ty {
Ty::from_rigid_kind(RigidTy::Closure(def, args))
}
/// Create a new coroutine type.
pub fn new_coroutine(def: CoroutineDef, args: GenericArgs, mov: Movability) -> Ty {
Ty::from_rigid_kind(RigidTy::Coroutine(def, args, mov))
}
/// Create a new box type that represents `Box<T>`, for the given inner type `T`.
pub fn new_box(inner_ty: Ty) -> Ty {
with(|cx| cx.new_box_ty(inner_ty))
}
/// Create a type representing `usize`.
pub fn usize_ty() -> Ty {
Ty::from_rigid_kind(RigidTy::Uint(UintTy::Usize))
}
/// Create a type representing `bool`.
pub fn bool_ty() -> Ty {
Ty::from_rigid_kind(RigidTy::Bool)
}
}
impl Ty {
@@ -251,6 +286,7 @@ impl TyKind {
}
/// Get the function signature for function like types (Fn, FnPtr, Closure, Coroutine)
/// FIXME(closure)
pub fn fn_sig(&self) -> Option<PolyFnSig> {
match self {
TyKind::RigidTy(RigidTy::FnDef(def, args)) => Some(with(|cx| cx.fn_sig(*def, args))),
@@ -258,6 +294,11 @@ impl TyKind {
_ => None,
}
}
/// Get the discriminant type for this type.
pub fn discriminant_ty(&self) -> Option<Ty> {
self.rigid().map(|ty| with(|cx| cx.rigid_ty_discriminant_ty(ty)))
}
}
pub struct TypeAndMut {
@@ -289,6 +330,13 @@ pub enum RigidTy {
CoroutineWitness(CoroutineWitnessDef, GenericArgs),
}
impl RigidTy {
/// Get the discriminant type for this type.
pub fn discriminant_ty(&self) -> Ty {
with(|cx| cx.rigid_ty_discriminant_ty(self))
}
}
impl From<RigidTy> for TyKind {
fn from(value: RigidTy) -> Self {
TyKind::RigidTy(value)