Skip to content

Commit f8d4a62

Browse files
committed
Finish implementing RustcInternal for TyKind
This will allow us to provide methods to create `Ty` inside the stable MIR, which can be helpful while handling pointers and other stuff.
1 parent efaf425 commit f8d4a62

File tree

1 file changed

+185
-12
lines changed

1 file changed

+185
-12
lines changed

compiler/rustc_smir/src/rustc_internal/internal.rs

+185-12
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ use rustc_middle::ty::{self as rustc_ty, Ty as InternalTy};
99
use rustc_span::Symbol;
1010
use stable_mir::mir::alloc::AllocId;
1111
use stable_mir::mir::mono::{Instance, MonoItem, StaticDef};
12-
use stable_mir::ty::{AdtDef, Binder, BoundRegionKind, BoundTyKind, BoundVariableKind, ClosureKind, Const, ExistentialTraitRef, FloatTy, GenericArgKind, GenericArgs, IndexedVal, IntTy, Region, RigidTy, Span, TraitRef, Ty, UintTy, VariantDef, VariantIdx};
12+
use stable_mir::mir::{Mutability, Safety};
13+
use stable_mir::ty::{
14+
Abi, AdtDef, Binder, BoundRegionKind, BoundTyKind, BoundVariableKind, ClosureKind, Const,
15+
DynKind, ExistentialPredicate, ExistentialProjection, ExistentialTraitRef, FloatTy, FnSig,
16+
GenericArgKind, GenericArgs, IndexedVal, IntTy, Movability, Region, RigidTy, Span, TermKind,
17+
TraitRef, Ty, UintTy, VariantDef, VariantIdx,
18+
};
1319
use stable_mir::{CrateItem, DefId};
1420

1521
use super::RustcInternal;
@@ -80,17 +86,38 @@ impl<'tcx> RustcInternal<'tcx> for RigidTy {
8086
}
8187
RigidTy::Str => rustc_ty::TyKind::Str,
8288
RigidTy::Slice(ty) => rustc_ty::TyKind::Slice(ty.internal(tables)),
83-
RigidTy::RawPtr(..)
84-
| RigidTy::Ref(..)
85-
| RigidTy::Foreign(_)
86-
| RigidTy::FnDef(_, _)
87-
| RigidTy::FnPtr(_)
88-
| RigidTy::Closure(..)
89-
| RigidTy::Coroutine(..)
90-
| RigidTy::CoroutineWitness(..)
91-
| RigidTy::Dynamic(..)
92-
| RigidTy::Tuple(..) => {
93-
todo!()
89+
RigidTy::RawPtr(ty, mutability) => rustc_ty::TyKind::RawPtr(rustc_ty::TypeAndMut {
90+
ty: ty.internal(tables),
91+
mutbl: mutability.internal(tables),
92+
}),
93+
RigidTy::Ref(region, ty, mutability) => rustc_ty::TyKind::Ref(
94+
region.internal(tables),
95+
ty.internal(tables),
96+
mutability.internal(tables),
97+
),
98+
RigidTy::Foreign(def) => rustc_ty::TyKind::Foreign(def.0.internal(tables)),
99+
RigidTy::FnDef(def, args) => {
100+
rustc_ty::TyKind::FnDef(def.0.internal(tables), args.internal(tables))
101+
}
102+
RigidTy::FnPtr(sig) => rustc_ty::TyKind::FnPtr(sig.internal(tables)),
103+
RigidTy::Closure(def, args) => {
104+
rustc_ty::TyKind::Closure(def.0.internal(tables), args.internal(tables))
105+
}
106+
RigidTy::Coroutine(def, args, mov) => rustc_ty::TyKind::Coroutine(
107+
def.0.internal(tables),
108+
args.internal(tables),
109+
mov.internal(tables),
110+
),
111+
RigidTy::CoroutineWitness(def, args) => {
112+
rustc_ty::TyKind::CoroutineWitness(def.0.internal(tables), args.internal(tables))
113+
}
114+
RigidTy::Dynamic(predicate, region, dyn_kind) => rustc_ty::TyKind::Dynamic(
115+
tables.tcx.mk_poly_existential_predicates(&predicate.internal(tables)),
116+
region.internal(tables),
117+
dyn_kind.internal(tables),
118+
),
119+
RigidTy::Tuple(tys) => {
120+
rustc_ty::TyKind::Tuple(tables.tcx.mk_type_list(&tys.internal(tables)))
94121
}
95122
}
96123
}
@@ -137,6 +164,41 @@ impl<'tcx> RustcInternal<'tcx> for FloatTy {
137164
}
138165
}
139166

167+
impl<'tcx> RustcInternal<'tcx> for Mutability {
168+
type T = rustc_ty::Mutability;
169+
170+
fn internal(&self, _tables: &mut Tables<'tcx>) -> Self::T {
171+
match self {
172+
Mutability::Not => rustc_ty::Mutability::Not,
173+
Mutability::Mut => rustc_ty::Mutability::Mut,
174+
}
175+
}
176+
}
177+
178+
impl<'tcx> RustcInternal<'tcx> for Movability {
179+
type T = rustc_ty::Movability;
180+
181+
fn internal(&self, _tables: &mut Tables<'tcx>) -> Self::T {
182+
match self {
183+
Movability::Static => rustc_ty::Movability::Static,
184+
Movability::Movable => rustc_ty::Movability::Movable,
185+
}
186+
}
187+
}
188+
189+
impl<'tcx> RustcInternal<'tcx> for FnSig {
190+
type T = rustc_ty::FnSig<'tcx>;
191+
192+
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T {
193+
rustc_ty::FnSig {
194+
inputs_and_output: tables.tcx.mk_type_list(&self.inputs_and_output.internal(tables)),
195+
c_variadic: self.c_variadic,
196+
unsafety: self.unsafety.internal(tables),
197+
abi: self.abi.internal(tables),
198+
}
199+
}
200+
}
201+
140202
impl<'tcx> RustcInternal<'tcx> for VariantIdx {
141203
type T = rustc_target::abi::VariantIdx;
142204

@@ -242,6 +304,58 @@ impl<'tcx> RustcInternal<'tcx> for BoundVariableKind {
242304
}
243305
}
244306

307+
impl<'tcx> RustcInternal<'tcx> for DynKind {
308+
type T = rustc_ty::DynKind;
309+
310+
fn internal(&self, _tables: &mut Tables<'tcx>) -> Self::T {
311+
match self {
312+
DynKind::Dyn => rustc_ty::DynKind::Dyn,
313+
DynKind::DynStar => rustc_ty::DynKind::DynStar,
314+
}
315+
}
316+
}
317+
318+
impl<'tcx> RustcInternal<'tcx> for ExistentialPredicate {
319+
type T = rustc_ty::ExistentialPredicate<'tcx>;
320+
321+
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T {
322+
match self {
323+
ExistentialPredicate::Trait(trait_ref) => {
324+
rustc_ty::ExistentialPredicate::Trait(trait_ref.internal(tables))
325+
}
326+
ExistentialPredicate::Projection(proj) => {
327+
rustc_ty::ExistentialPredicate::Projection(proj.internal(tables))
328+
}
329+
ExistentialPredicate::AutoTrait(trait_def) => {
330+
rustc_ty::ExistentialPredicate::AutoTrait(trait_def.0.internal(tables))
331+
}
332+
}
333+
}
334+
}
335+
336+
impl<'tcx> RustcInternal<'tcx> for ExistentialProjection {
337+
type T = rustc_ty::ExistentialProjection<'tcx>;
338+
339+
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T {
340+
rustc_ty::ExistentialProjection {
341+
def_id: self.def_id.0.internal(tables),
342+
args: self.generic_args.internal(tables),
343+
term: self.term.internal(tables),
344+
}
345+
}
346+
}
347+
348+
impl<'tcx> RustcInternal<'tcx> for TermKind {
349+
type T = rustc_ty::Term<'tcx>;
350+
351+
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T {
352+
match self {
353+
TermKind::Type(ty) => ty.internal(tables).into(),
354+
TermKind::Const(const_) => ty_const(const_, tables).into(),
355+
}
356+
}
357+
}
358+
245359
impl<'tcx> RustcInternal<'tcx> for ExistentialTraitRef {
246360
type T = rustc_ty::ExistentialTraitRef<'tcx>;
247361

@@ -291,6 +405,53 @@ impl<'tcx> RustcInternal<'tcx> for AdtDef {
291405
}
292406
}
293407

408+
impl<'tcx> RustcInternal<'tcx> for Abi {
409+
type T = rustc_target::spec::abi::Abi;
410+
411+
fn internal(&self, _tables: &mut Tables<'tcx>) -> Self::T {
412+
match *self {
413+
Abi::Rust => rustc_target::spec::abi::Abi::Rust,
414+
Abi::C { unwind } => rustc_target::spec::abi::Abi::C { unwind },
415+
Abi::Cdecl { unwind } => rustc_target::spec::abi::Abi::Cdecl { unwind },
416+
Abi::Stdcall { unwind } => rustc_target::spec::abi::Abi::Stdcall { unwind },
417+
Abi::Fastcall { unwind } => rustc_target::spec::abi::Abi::Fastcall { unwind },
418+
Abi::Vectorcall { unwind } => rustc_target::spec::abi::Abi::Vectorcall { unwind },
419+
Abi::Thiscall { unwind } => rustc_target::spec::abi::Abi::Thiscall { unwind },
420+
Abi::Aapcs { unwind } => rustc_target::spec::abi::Abi::Aapcs { unwind },
421+
Abi::Win64 { unwind } => rustc_target::spec::abi::Abi::Win64 { unwind },
422+
Abi::SysV64 { unwind } => rustc_target::spec::abi::Abi::SysV64 { unwind },
423+
Abi::PtxKernel => rustc_target::spec::abi::Abi::PtxKernel,
424+
Abi::Msp430Interrupt => rustc_target::spec::abi::Abi::Msp430Interrupt,
425+
Abi::X86Interrupt => rustc_target::spec::abi::Abi::X86Interrupt,
426+
Abi::AmdGpuKernel => rustc_target::spec::abi::Abi::AmdGpuKernel,
427+
Abi::EfiApi => rustc_target::spec::abi::Abi::EfiApi,
428+
Abi::AvrInterrupt => rustc_target::spec::abi::Abi::AvrInterrupt,
429+
Abi::AvrNonBlockingInterrupt => rustc_target::spec::abi::Abi::AvrNonBlockingInterrupt,
430+
Abi::CCmseNonSecureCall => rustc_target::spec::abi::Abi::CCmseNonSecureCall,
431+
Abi::Wasm => rustc_target::spec::abi::Abi::Wasm,
432+
Abi::System { unwind } => rustc_target::spec::abi::Abi::System { unwind },
433+
Abi::RustIntrinsic => rustc_target::spec::abi::Abi::RustIntrinsic,
434+
Abi::RustCall => rustc_target::spec::abi::Abi::RustCall,
435+
Abi::PlatformIntrinsic => rustc_target::spec::abi::Abi::PlatformIntrinsic,
436+
Abi::Unadjusted => rustc_target::spec::abi::Abi::Unadjusted,
437+
Abi::RustCold => rustc_target::spec::abi::Abi::RustCold,
438+
Abi::RiscvInterruptM => rustc_target::spec::abi::Abi::RiscvInterruptM,
439+
Abi::RiscvInterruptS => rustc_target::spec::abi::Abi::RiscvInterruptS,
440+
}
441+
}
442+
}
443+
444+
impl<'tcx> RustcInternal<'tcx> for Safety {
445+
type T = rustc_hir::Unsafety;
446+
447+
fn internal(&self, _tables: &mut Tables<'tcx>) -> Self::T {
448+
match self {
449+
Safety::Unsafe => rustc_hir::Unsafety::Unsafe,
450+
Safety::Normal => rustc_hir::Unsafety::Normal,
451+
}
452+
}
453+
}
454+
294455
impl<'tcx> RustcInternal<'tcx> for Span {
295456
type T = rustc_span::Span;
296457

@@ -309,6 +470,7 @@ where
309470
(*self).internal(tables)
310471
}
311472
}
473+
312474
impl<'tcx, T> RustcInternal<'tcx> for Option<T>
313475
where
314476
T: RustcInternal<'tcx>,
@@ -319,3 +481,14 @@ where
319481
self.as_ref().map(|inner| inner.internal(tables))
320482
}
321483
}
484+
485+
impl<'tcx, T> RustcInternal<'tcx> for Vec<T>
486+
where
487+
T: RustcInternal<'tcx>,
488+
{
489+
type T = Vec<T::T>;
490+
491+
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T {
492+
self.iter().map(|e| e.internal(tables)).collect()
493+
}
494+
}

0 commit comments

Comments
 (0)