Convert trait declaration to SMIR

This commit is contained in:
Santiago Pastorino 2023-08-04 17:23:26 -03:00
parent 4199a3c13a
commit 496faa857c
No known key found for this signature in database
GPG Key ID: 8131A24E0C79EFAF
4 changed files with 76 additions and 4 deletions

View File

@ -68,6 +68,10 @@ impl<'tcx> Tables<'tcx> {
self.def_ids[item.0]
}
pub fn trait_def_id(&self, trait_def: &stable_mir::ty::TraitDef) -> DefId {
self.def_ids[trait_def.0]
}
pub fn crate_item(&mut self, did: DefId) -> stable_mir::CrateItem {
stable_mir::CrateItem(self.create_def_id(did))
}

View File

@ -41,6 +41,13 @@ impl<'tcx> Context for Tables<'tcx> {
fn entry_fn(&mut self) -> Option<stable_mir::CrateItem> {
Some(self.crate_item(self.tcx.entry_fn(())?.0))
}
fn trait_decl(&mut self, trait_def: &stable_mir::ty::TraitDef) -> stable_mir::ty::TraitDecl {
let def_id = self.trait_def_id(trait_def);
let trait_def = self.tcx.trait_def(def_id);
trait_def.stable(self)
}
fn mir_body(&mut self, item: &stable_mir::CrateItem) -> stable_mir::mir::Body {
let def_id = self.item_def_id(item);
let mir = self.tcx.optimized_mir(def_id);
@ -515,7 +522,7 @@ impl<'tcx> Stable<'tcx> for mir::RetagKind {
}
}
impl<'tcx> Stable<'tcx> for rustc_middle::ty::UserTypeAnnotationIndex {
impl<'tcx> Stable<'tcx> for ty::UserTypeAnnotationIndex {
type T = usize;
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
self.as_usize()
@ -1045,7 +1052,7 @@ impl<'tcx> Stable<'tcx> for Ty<'tcx> {
}
}
impl<'tcx> Stable<'tcx> for rustc_middle::ty::ParamTy {
impl<'tcx> Stable<'tcx> for ty::ParamTy {
type T = stable_mir::ty::ParamTy;
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
use stable_mir::ty::ParamTy;
@ -1053,7 +1060,7 @@ impl<'tcx> Stable<'tcx> for rustc_middle::ty::ParamTy {
}
}
impl<'tcx> Stable<'tcx> for rustc_middle::ty::BoundTy {
impl<'tcx> Stable<'tcx> for ty::BoundTy {
type T = stable_mir::ty::BoundTy;
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
use stable_mir::ty::BoundTy;
@ -1091,3 +1098,42 @@ impl<'tcx> Stable<'tcx> for mir::interpret::Allocation {
}
}
}
impl<'tcx> Stable<'tcx> for ty::trait_def::TraitSpecializationKind {
type T = stable_mir::ty::TraitSpecializationKind;
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
use stable_mir::ty::TraitSpecializationKind;
match self {
ty::trait_def::TraitSpecializationKind::None => TraitSpecializationKind::None,
ty::trait_def::TraitSpecializationKind::Marker => TraitSpecializationKind::Marker,
ty::trait_def::TraitSpecializationKind::AlwaysApplicable => {
TraitSpecializationKind::AlwaysApplicable
}
}
}
}
impl<'tcx> Stable<'tcx> for ty::TraitDef {
type T = stable_mir::ty::TraitDecl;
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
use stable_mir::ty::TraitDecl;
TraitDecl {
def_id: rustc_internal::trait_def(self.def_id),
unsafety: self.unsafety.stable(tables),
paren_sugar: self.paren_sugar,
has_auto_impl: self.has_auto_impl,
is_marker: self.is_marker,
is_coinductive: self.is_coinductive,
skip_array_during_method_dispatch: self.skip_array_during_method_dispatch,
specialization_kind: self.specialization_kind.stable(tables),
must_implement_one_of: self
.must_implement_one_of
.as_ref()
.map(|idents| idents.iter().map(|ident| opaque(ident)).collect()),
implement_via_object: self.implement_via_object,
deny_explicit_impl: self.deny_explicit_impl,
}
}
}

View File

@ -15,7 +15,7 @@ use std::cell::Cell;
use crate::rustc_smir::Tables;
use self::ty::{Ty, TyKind};
use self::ty::{TraitDecl, TraitDef, Ty, TyKind};
pub mod mir;
pub mod ty;
@ -84,6 +84,7 @@ pub trait Context {
/// Retrieve all items of the local crate that have a MIR associated with them.
fn all_local_items(&mut self) -> CrateItems;
fn mir_body(&mut self, item: &CrateItem) -> mir::Body;
fn trait_decl(&mut self, trait_def: &TraitDef) -> TraitDecl;
/// Get information about the local crate.
fn local_crate(&self) -> Crate;
/// Retrieve a list of all external crates.

View File

@ -11,6 +11,7 @@ impl Ty {
}
pub(crate) type Const = Opaque;
type Ident = Opaque;
pub(crate) type Region = Opaque;
type Span = Opaque;
@ -258,3 +259,23 @@ pub struct Allocation {
pub align: Align,
pub mutability: Mutability,
}
pub enum TraitSpecializationKind {
None,
Marker,
AlwaysApplicable,
}
pub struct TraitDecl {
pub def_id: TraitDef,
pub unsafety: Safety,
pub paren_sugar: bool,
pub has_auto_impl: bool,
pub is_marker: bool,
pub is_coinductive: bool,
pub skip_array_during_method_dispatch: bool,
pub specialization_kind: TraitSpecializationKind,
pub must_implement_one_of: Option<Vec<Ident>>,
pub implement_via_object: bool,
pub deny_explicit_impl: bool,
}