Skip to content

Commit 17b8977

Browse files
committed
Add FnPtr ty to SMIR
1 parent 93bcc2e commit 17b8977

File tree

3 files changed

+191
-1
lines changed

3 files changed

+191
-1
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ pub fn generator_def(did: DefId) -> stable_mir::ty::GeneratorDef {
4747
with_tables(|t| t.generator_def(did))
4848
}
4949

50+
pub fn param_def(did: DefId) -> stable_mir::ty::ParamDef {
51+
with_tables(|t| t.param_def(did))
52+
}
53+
54+
pub fn br_named_def(did: DefId) -> stable_mir::ty::BrNamedDef {
55+
with_tables(|t| t.br_named_def(did))
56+
}
57+
5058
impl<'tcx> Tables<'tcx> {
5159
pub fn item_def_id(&self, item: &stable_mir::CrateItem) -> DefId {
5260
self.def_ids[item.0]
@@ -76,6 +84,14 @@ impl<'tcx> Tables<'tcx> {
7684
stable_mir::ty::GeneratorDef(self.create_def_id(did))
7785
}
7886

87+
pub fn param_def(&mut self, did: DefId) -> stable_mir::ty::ParamDef {
88+
stable_mir::ty::ParamDef(self.create_def_id(did))
89+
}
90+
91+
pub fn br_named_def(&mut self, did: DefId) -> stable_mir::ty::BrNamedDef {
92+
stable_mir::ty::BrNamedDef(self.create_def_id(did))
93+
}
94+
7995
fn create_def_id(&mut self, did: DefId) -> stable_mir::DefId {
8096
// FIXME: this becomes inefficient when we have too many ids
8197
for (i, &d) in self.def_ids.iter().enumerate() {

compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl<'tcx> Tables<'tcx> {
123123
rustc_internal::fn_def(*def_id),
124124
generic_args.stable(self),
125125
)),
126-
ty::FnPtr(_) => todo!(),
126+
ty::FnPtr(poly_fn_sig) => TyKind::RigidTy(RigidTy::FnPtr(poly_fn_sig.stable(self))),
127127
ty::Dynamic(_, _, _) => todo!(),
128128
ty::Closure(def_id, generic_args) => TyKind::RigidTy(RigidTy::Closure(
129129
rustc_internal::closure_def(*def_id),
@@ -581,3 +581,98 @@ impl<'tcx> Stable<'tcx> for ty::GenericArgs<'tcx> {
581581
)
582582
}
583583
}
584+
585+
impl<'tcx> Stable<'tcx> for ty::PolyFnSig<'tcx> {
586+
type T = stable_mir::ty::PolyFnSig;
587+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
588+
use stable_mir::ty::Binder;
589+
590+
Binder {
591+
value: self.skip_binder().stable(tables),
592+
bound_vars: self
593+
.bound_vars()
594+
.iter()
595+
.map(|bound_var| bound_var.stable(tables))
596+
.collect(),
597+
}
598+
}
599+
}
600+
601+
impl<'tcx> Stable<'tcx> for ty::FnSig<'tcx> {
602+
type T = stable_mir::ty::FnSig;
603+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
604+
use rustc_target::spec::abi;
605+
use stable_mir::ty::{Abi, FnSig, Unsafety};
606+
607+
FnSig {
608+
inputs_and_output: self
609+
.inputs_and_output
610+
.iter()
611+
.map(|ty| tables.intern_ty(ty))
612+
.collect(),
613+
c_variadic: self.c_variadic,
614+
unsafety: match self.unsafety {
615+
hir::Unsafety::Normal => Unsafety::Normal,
616+
hir::Unsafety::Unsafe => Unsafety::Unsafe,
617+
},
618+
abi: match self.abi {
619+
abi::Abi::Rust => Abi::Rust,
620+
abi::Abi::C { unwind } => Abi::C { unwind },
621+
abi::Abi::Cdecl { unwind } => Abi::Cdecl { unwind },
622+
abi::Abi::Stdcall { unwind } => Abi::Stdcall { unwind },
623+
abi::Abi::Fastcall { unwind } => Abi::Fastcall { unwind },
624+
abi::Abi::Vectorcall { unwind } => Abi::Vectorcall { unwind },
625+
abi::Abi::Thiscall { unwind } => Abi::Thiscall { unwind },
626+
abi::Abi::Aapcs { unwind } => Abi::Aapcs { unwind },
627+
abi::Abi::Win64 { unwind } => Abi::Win64 { unwind },
628+
abi::Abi::SysV64 { unwind } => Abi::SysV64 { unwind },
629+
abi::Abi::PtxKernel => Abi::PtxKernel,
630+
abi::Abi::Msp430Interrupt => Abi::Msp430Interrupt,
631+
abi::Abi::X86Interrupt => Abi::X86Interrupt,
632+
abi::Abi::AmdGpuKernel => Abi::AmdGpuKernel,
633+
abi::Abi::EfiApi => Abi::EfiApi,
634+
abi::Abi::AvrInterrupt => Abi::AvrInterrupt,
635+
abi::Abi::AvrNonBlockingInterrupt => Abi::AvrNonBlockingInterrupt,
636+
abi::Abi::CCmseNonSecureCall => Abi::CCmseNonSecureCall,
637+
abi::Abi::Wasm => Abi::Wasm,
638+
abi::Abi::System { unwind } => Abi::System { unwind },
639+
abi::Abi::RustIntrinsic => Abi::RustIntrinsic,
640+
abi::Abi::RustCall => Abi::RustCall,
641+
abi::Abi::PlatformIntrinsic => Abi::PlatformIntrinsic,
642+
abi::Abi::Unadjusted => Abi::Unadjusted,
643+
abi::Abi::RustCold => Abi::RustCold,
644+
},
645+
}
646+
}
647+
}
648+
649+
impl<'tcx> Stable<'tcx> for ty::BoundVariableKind {
650+
type T = stable_mir::ty::BoundVariableKind;
651+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
652+
use stable_mir::ty::{BoundRegionKind, BoundTyKind, BoundVariableKind};
653+
654+
match self {
655+
ty::BoundVariableKind::Ty(bound_ty_kind) => {
656+
BoundVariableKind::Ty(match bound_ty_kind {
657+
ty::BoundTyKind::Anon => BoundTyKind::Anon,
658+
ty::BoundTyKind::Param(def_id, symbol) => {
659+
BoundTyKind::Param(rustc_internal::param_def(*def_id), symbol.to_string())
660+
}
661+
})
662+
}
663+
ty::BoundVariableKind::Region(bound_region_kind) => {
664+
BoundVariableKind::Region(match bound_region_kind {
665+
ty::BoundRegionKind::BrAnon(option_span) => {
666+
BoundRegionKind::BrAnon(option_span.map(|span| opaque(&span)))
667+
}
668+
ty::BoundRegionKind::BrNamed(def_id, symbol) => BoundRegionKind::BrNamed(
669+
rustc_internal::br_named_def(*def_id),
670+
symbol.to_string(),
671+
),
672+
ty::BoundRegionKind::BrEnv => BoundRegionKind::BrEnv,
673+
})
674+
}
675+
ty::BoundVariableKind::Const => BoundVariableKind::Const,
676+
}
677+
}
678+
}

compiler/rustc_smir/src/stable_mir/ty.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ impl Ty {
1212

1313
type Const = Opaque;
1414
pub(crate) type Region = Opaque;
15+
type Span = Opaque;
1516

1617
#[derive(Clone, Debug)]
1718
pub enum TyKind {
@@ -33,6 +34,7 @@ pub enum RigidTy {
3334
RawPtr(Ty, Mutability),
3435
Ref(Region, Ty, Mutability),
3536
FnDef(FnDef, GenericArgs),
37+
FnPtr(PolyFnSig),
3638
Closure(ClosureDef, GenericArgs),
3739
Generator(GeneratorDef, GenericArgs, Movability),
3840
Never,
@@ -83,6 +85,12 @@ pub struct ClosureDef(pub(crate) DefId);
8385
#[derive(Clone, PartialEq, Eq, Debug)]
8486
pub struct GeneratorDef(pub(crate) DefId);
8587

88+
#[derive(Clone, PartialEq, Eq, Debug)]
89+
pub struct ParamDef(pub(crate) DefId);
90+
91+
#[derive(Clone, PartialEq, Eq, Debug)]
92+
pub struct BrNamedDef(pub(crate) DefId);
93+
8694
#[derive(Clone, PartialEq, Eq, Debug)]
8795
pub struct AdtDef(pub(crate) DefId);
8896

@@ -95,3 +103,74 @@ pub enum GenericArgKind {
95103
Type(Ty),
96104
Const(Const),
97105
}
106+
107+
pub type PolyFnSig = Binder<FnSig>;
108+
109+
#[derive(Clone, Debug)]
110+
pub struct FnSig {
111+
pub inputs_and_output: Vec<Ty>,
112+
pub c_variadic: bool,
113+
pub unsafety: Unsafety,
114+
pub abi: Abi,
115+
}
116+
117+
#[derive(Clone, PartialEq, Eq, Debug)]
118+
pub enum Unsafety {
119+
Unsafe,
120+
Normal,
121+
}
122+
123+
#[derive(Clone, PartialEq, Eq, Debug)]
124+
pub enum Abi {
125+
Rust,
126+
C { unwind: bool },
127+
Cdecl { unwind: bool },
128+
Stdcall { unwind: bool },
129+
Fastcall { unwind: bool },
130+
Vectorcall { unwind: bool },
131+
Thiscall { unwind: bool },
132+
Aapcs { unwind: bool },
133+
Win64 { unwind: bool },
134+
SysV64 { unwind: bool },
135+
PtxKernel,
136+
Msp430Interrupt,
137+
X86Interrupt,
138+
AmdGpuKernel,
139+
EfiApi,
140+
AvrInterrupt,
141+
AvrNonBlockingInterrupt,
142+
CCmseNonSecureCall,
143+
Wasm,
144+
System { unwind: bool },
145+
RustIntrinsic,
146+
RustCall,
147+
PlatformIntrinsic,
148+
Unadjusted,
149+
RustCold,
150+
}
151+
152+
#[derive(Clone, Debug)]
153+
pub struct Binder<T> {
154+
pub value: T,
155+
pub bound_vars: Vec<BoundVariableKind>,
156+
}
157+
158+
#[derive(Clone, Debug)]
159+
pub enum BoundVariableKind {
160+
Ty(BoundTyKind),
161+
Region(BoundRegionKind),
162+
Const,
163+
}
164+
165+
#[derive(Clone, PartialEq, Eq, Debug)]
166+
pub enum BoundTyKind {
167+
Anon,
168+
Param(ParamDef, String),
169+
}
170+
171+
#[derive(Clone, Debug)]
172+
pub enum BoundRegionKind {
173+
BrAnon(Option<Span>),
174+
BrNamed(BrNamedDef, String),
175+
BrEnv,
176+
}

0 commit comments

Comments
 (0)