Skip to content

Commit

Permalink
Rollup merge of rust-lang#113629 - spastorino:smir-types-3, r=oli-obk
Browse files Browse the repository at this point in the history
Add Adt to SMIR

r? ```@oli-obk```
  • Loading branch information
matthiaskrgr authored Jul 13, 2023
2 parents 988558c + c80a0f3 commit cf7f464
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
16 changes: 14 additions & 2 deletions compiler/rustc_smir/src/rustc_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
33 changes: 23 additions & 10 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -94,7 +94,25 @@ impl<'tcx> Tables<'tcx> {
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!(),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -190,7 +201,9 @@ impl<'tcx> Stable for mir::Rvalue<'tcx> {
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())
}
Expand Down
22 changes: 21 additions & 1 deletion compiler/rustc_smir/src/stable_mir/ty.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -9,6 +10,9 @@ impl Ty {
}
}

type Const = Opaque;
type Region = Opaque;

#[derive(Clone, Debug)]
pub enum TyKind {
RigidTy(RigidTy),
Expand All @@ -21,6 +25,7 @@ pub enum RigidTy {
Int(IntTy),
Uint(UintTy),
Float(FloatTy),
Adt(AdtDef, AdtSubsts),
Tuple(Vec<Ty>),
}

Expand Down Expand Up @@ -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),
}

0 comments on commit cf7f464

Please sign in to comment.