Rollup merge of #113629 - spastorino:smir-types-3, r=oli-obk

Add Adt to SMIR

r? ````@oli-obk````
This commit is contained in:
Matthias Krüger 2023-07-13 22:33:25 +02:00 committed by GitHub
commit 017112f834
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 13 deletions

View File

@ -27,21 +27,33 @@ pub fn crate_item(did: DefId) -> stable_mir::CrateItem {
with_tables(|t| t.crate_item(did))
}
pub fn adt_def(did: DefId) -> stable_mir::ty::AdtDef {
with_tables(|t| t.adt_def(did))
}
impl<'tcx> Tables<'tcx> {
pub fn item_def_id(&self, item: &stable_mir::CrateItem) -> DefId {
self.def_ids[item.0]
}
pub fn crate_item(&mut self, did: DefId) -> stable_mir::CrateItem {
stable_mir::CrateItem(self.create_def_id(did))
}
pub fn adt_def(&mut self, did: DefId) -> stable_mir::ty::AdtDef {
stable_mir::ty::AdtDef(self.create_def_id(did))
}
fn create_def_id(&mut self, did: DefId) -> stable_mir::DefId {
// FIXME: this becomes inefficient when we have too many ids
for (i, &d) in self.def_ids.iter().enumerate() {
if d == did {
return stable_mir::CrateItem(i);
return i;
}
}
let id = self.def_ids.len();
self.def_ids.push(did);
stable_mir::CrateItem(id)
id
}
}

View File

@ -8,7 +8,7 @@
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
use crate::rustc_internal::{self, opaque};
use crate::stable_mir::ty::{FloatTy, IntTy, RigidTy, TyKind, UintTy};
use crate::stable_mir::ty::{AdtSubsts, FloatTy, GenericArgKind, IntTy, RigidTy, TyKind, UintTy};
use crate::stable_mir::{self, Context};
use rustc_middle::mir;
use rustc_middle::ty::{self, Ty, TyCtxt};
@ -94,7 +94,25 @@ fn rustc_ty_to_ty(&mut self, ty: Ty<'tcx>) -> TyKind {
ty::FloatTy::F32 => TyKind::RigidTy(RigidTy::Float(FloatTy::F32)),
ty::FloatTy::F64 => TyKind::RigidTy(RigidTy::Float(FloatTy::F64)),
},
ty::Adt(_, _) => todo!(),
ty::Adt(adt_def, substs) => TyKind::RigidTy(RigidTy::Adt(
rustc_internal::adt_def(adt_def.did()),
AdtSubsts(
substs
.iter()
.map(|arg| match arg.unpack() {
ty::GenericArgKind::Lifetime(region) => {
GenericArgKind::Lifetime(opaque(&region))
}
ty::GenericArgKind::Type(ty) => {
GenericArgKind::Type(self.intern_ty(ty))
}
ty::GenericArgKind::Const(const_) => {
GenericArgKind::Const(opaque(&const_))
}
})
.collect(),
),
)),
ty::Foreign(_) => todo!(),
ty::Str => todo!(),
ty::Array(_, _) => todo!(),
@ -149,13 +167,6 @@ pub(crate) trait Stable {
fn stable(&self) -> Self::T;
}
impl Stable for DefId {
type T = stable_mir::CrateItem;
fn stable(&self) -> Self::T {
rustc_internal::crate_item(*self)
}
}
impl<'tcx> Stable for mir::Statement<'tcx> {
type T = stable_mir::mir::Statement;
fn stable(&self) -> Self::T {
@ -190,7 +201,9 @@ fn stable(&self) -> Self::T {
Ref(region, kind, place) => {
stable_mir::mir::Rvalue::Ref(opaque(region), kind.stable(), place.stable())
}
ThreadLocalRef(def_id) => stable_mir::mir::Rvalue::ThreadLocalRef(def_id.stable()),
ThreadLocalRef(def_id) => {
stable_mir::mir::Rvalue::ThreadLocalRef(rustc_internal::crate_item(*def_id))
}
AddressOf(mutability, place) => {
stable_mir::mir::Rvalue::AddressOf(mutability.stable(), place.stable())
}

View File

@ -1,4 +1,5 @@
use super::with;
use super::{with, DefId};
use crate::rustc_internal::Opaque;
#[derive(Copy, Clone, Debug)]
pub struct Ty(pub usize);
@ -9,6 +10,9 @@ pub fn kind(&self) -> TyKind {
}
}
type Const = Opaque;
type Region = Opaque;
#[derive(Clone, Debug)]
pub enum TyKind {
RigidTy(RigidTy),
@ -21,6 +25,7 @@ pub enum RigidTy {
Int(IntTy),
Uint(UintTy),
Float(FloatTy),
Adt(AdtDef, AdtSubsts),
Tuple(Vec<Ty>),
}
@ -49,3 +54,18 @@ pub enum FloatTy {
F32,
F64,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct AdtDef(pub(crate) DefId);
#[derive(Clone, Debug)]
pub struct AdtSubsts(pub Vec<GenericArgKind>);
#[derive(Clone, Debug)]
pub enum GenericArgKind {
// FIXME add proper region
Lifetime(Region),
Type(Ty),
// FIXME add proper const
Const(Const),
}