diff --git a/compiler/rustc_smir/src/rustc_internal/mod.rs b/compiler/rustc_smir/src/rustc_internal/mod.rs index 1a9dea99f643f..581789d0be623 100644 --- a/compiler/rustc_smir/src/rustc_internal/mod.rs +++ b/compiler/rustc_smir/src/rustc_internal/mod.rs @@ -94,7 +94,7 @@ impl<'tcx> Tables<'tcx> { stable_mir::ty::Prov(self.create_alloc_id(aid)) } - fn create_def_id(&mut self, did: DefId) -> stable_mir::DefId { + pub(super) 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 { diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 7d1122c2fd21d..330e356640668 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -1610,3 +1610,62 @@ impl<'tcx> Stable<'tcx> for DefKind { opaque(self) } } + +impl<'tcx> Stable<'tcx> for ty::Instance<'tcx> { + type T = stable_mir::ty::Instance; + + fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { + let ty::Instance { def, args } = self; + stable_mir::ty::Instance { def: def.stable(tables), args: args.stable(tables) } + } +} + +impl<'tcx> Stable<'tcx> for ty::InstanceDef<'tcx> { + type T = stable_mir::ty::InstanceDef; + + fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { + use stable_mir::ty::InstanceDef::*; + + match *self { + ty::InstanceDef::Item(did) => Item(tables.create_def_id(did)), + ty::InstanceDef::Intrinsic(did) => Intrinsic(tables.create_def_id(did)), + ty::InstanceDef::VTableShim(did) => VTableShim(tables.create_def_id(did)), + ty::InstanceDef::ReifyShim(did) => ReifyShim(tables.create_def_id(did)), + ty::InstanceDef::FnPtrShim(did, ty) => { + FnPtrShim(tables.create_def_id(did), tables.intern_ty(ty)) + } + ty::InstanceDef::Virtual(did, vtable_idx) => { + Virtual(tables.create_def_id(did), vtable_idx) + } + ty::InstanceDef::ClosureOnceShim { call_once, track_caller } => { + ClosureOnceShim { call_once: tables.create_def_id(call_once), track_caller } + } + ty::InstanceDef::ThreadLocalShim(did) => ThreadLocalShim(tables.create_def_id(did)), + ty::InstanceDef::DropGlue(did, drop_glue) => { + DropGlue(tables.create_def_id(did), drop_glue.map(|ty| tables.intern_ty(ty))) + } + ty::InstanceDef::CloneShim(did, ty) => { + CloneShim(tables.create_def_id(did), tables.intern_ty(ty)) + } + ty::InstanceDef::FnPtrAddrShim(did, ty) => { + FnPtrAddrShim(tables.create_def_id(did), tables.intern_ty(ty)) + } + } + } +} + +impl<'tcx> Stable<'tcx> for mir::mono::MonoItem<'tcx> { + type T = stable_mir::mir::mono::MonoItem; + + fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { + use stable_mir::mir::mono::MonoItem::*; + + match *self { + mir::mono::MonoItem::Fn(instance) => Fn(instance.stable(tables)), + mir::mono::MonoItem::Static(did) => Static(tables.create_def_id(did)), + mir::mono::MonoItem::GlobalAsm(iid) => { + GlobalAsm(opaque(tables.tcx.hir().item(iid).expect_global_asm())) + } + } + } +} diff --git a/compiler/stable_mir/src/mir.rs b/compiler/stable_mir/src/mir.rs index a9dbc3463f80b..3138bb1ec832c 100644 --- a/compiler/stable_mir/src/mir.rs +++ b/compiler/stable_mir/src/mir.rs @@ -1,3 +1,4 @@ mod body; +pub mod mono; pub use body::*; diff --git a/compiler/stable_mir/src/mir/mono.rs b/compiler/stable_mir/src/mir/mono.rs new file mode 100644 index 0000000000000..fbff454d0e74f --- /dev/null +++ b/compiler/stable_mir/src/mir/mono.rs @@ -0,0 +1,8 @@ +use crate::{ty::Instance, DefId, Opaque}; + +#[derive(Clone, Debug)] +pub enum MonoItem { + Fn(Instance), + Static(DefId), + GlobalAsm(Opaque), +} diff --git a/compiler/stable_mir/src/ty.rs b/compiler/stable_mir/src/ty.rs index 6029e3c11adef..44eefbcc055fe 100644 --- a/compiler/stable_mir/src/ty.rs +++ b/compiler/stable_mir/src/ty.rs @@ -565,3 +565,24 @@ pub enum ImplPolarity { Negative, Reservation, } + +#[derive(Clone, Debug)] +pub struct Instance { + pub def: InstanceDef, + pub args: GenericArgs, +} + +#[derive(Clone, Debug)] +pub enum InstanceDef { + Item(DefId), + Intrinsic(DefId), + VTableShim(DefId), + ReifyShim(DefId), + FnPtrShim(DefId, Ty), + Virtual(DefId, usize), + ClosureOnceShim { call_once: DefId, track_caller: bool }, + ThreadLocalShim(DefId), + DropGlue(DefId, Option), + CloneShim(DefId, Ty), + FnPtrAddrShim(DefId, Ty), +}