Rollup merge of #115092 - ouz-a:smir_generic_of, r=spastorino

Add generics_of to smir

Continuing our covering of smir.

r? `@spastorino`
This commit is contained in:
Dylan DPC 2023-08-23 05:35:16 +00:00 committed by GitHub
commit d99466d84e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 129 additions and 13 deletions

View File

@ -80,6 +80,10 @@ pub fn impl_trait_def_id(&self, impl_def: &stable_mir::ty::ImplDef) -> DefId {
self.def_ids[impl_def.0] self.def_ids[impl_def.0]
} }
pub fn generic_def_id(&self, generic_def: &stable_mir::ty::GenericDef) -> DefId {
self.def_ids[generic_def.0]
}
pub fn crate_item(&mut self, did: DefId) -> stable_mir::CrateItem { pub fn crate_item(&mut self, did: DefId) -> stable_mir::CrateItem {
stable_mir::CrateItem(self.create_def_id(did)) stable_mir::CrateItem(self.create_def_id(did))
} }
@ -120,6 +124,10 @@ pub fn trait_def(&mut self, did: DefId) -> stable_mir::ty::TraitDef {
stable_mir::ty::TraitDef(self.create_def_id(did)) stable_mir::ty::TraitDef(self.create_def_id(did))
} }
pub fn generic_def(&mut self, did: DefId) -> stable_mir::ty::GenericDef {
stable_mir::ty::GenericDef(self.create_def_id(did))
}
pub fn const_def(&mut self, did: DefId) -> stable_mir::ty::ConstDef { pub fn const_def(&mut self, did: DefId) -> stable_mir::ty::ConstDef {
stable_mir::ty::ConstDef(self.create_def_id(did)) stable_mir::ty::ConstDef(self.create_def_id(did))
} }

View File

@ -10,7 +10,8 @@
use crate::rustc_internal::{self, opaque}; use crate::rustc_internal::{self, opaque};
use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection, VariantIdx}; use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection, VariantIdx};
use crate::stable_mir::ty::{ use crate::stable_mir::ty::{
allocation_filter, new_allocation, Const, FloatTy, IntTy, Movability, RigidTy, TyKind, UintTy, allocation_filter, new_allocation, Const, FloatTy, GenericDef, GenericParamDef, IntTy,
Movability, RigidTy, TyKind, UintTy,
}; };
use crate::stable_mir::{self, Context}; use crate::stable_mir::{self, Context};
use rustc_hir as hir; use rustc_hir as hir;
@ -101,6 +102,12 @@ fn ty_kind(&mut self, ty: crate::stable_mir::ty::Ty) -> TyKind {
let ty = self.types[ty.0]; let ty = self.types[ty.0];
ty.stable(self) ty.stable(self)
} }
fn generics_of(&mut self, generic_def: &GenericDef) -> stable_mir::ty::Generics {
let def_id = self.generic_def_id(generic_def);
let generic_def = self.tcx.generics_of(def_id);
generic_def.stable(self)
}
} }
pub struct Tables<'tcx> { pub struct Tables<'tcx> {
@ -1205,3 +1212,67 @@ fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
TraitRef { def_id: rustc_internal::trait_def(self.def_id), args: self.args.stable(tables) } TraitRef { def_id: rustc_internal::trait_def(self.def_id), args: self.args.stable(tables) }
} }
} }
impl<'tcx> Stable<'tcx> for ty::Generics {
type T = stable_mir::ty::Generics;
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
use stable_mir::ty::Generics;
let params: Vec<_> = self.params.iter().map(|param| param.stable(tables)).collect();
let param_def_id_to_index =
params.iter().map(|param| (param.def_id, param.index)).collect();
Generics {
parent: self.parent.map(|did| tables.generic_def(did)),
parent_count: self.parent_count,
params,
param_def_id_to_index,
has_self: self.has_self,
has_late_bound_regions: self
.has_late_bound_regions
.as_ref()
.map(|late_bound_regions| late_bound_regions.stable(tables)),
host_effect_index: self.host_effect_index,
}
}
}
impl<'tcx> Stable<'tcx> for rustc_span::Span {
type T = stable_mir::ty::Span;
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
opaque(self)
}
}
impl<'tcx> Stable<'tcx> for rustc_middle::ty::GenericParamDefKind {
type T = stable_mir::ty::GenericParamDefKind;
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
use stable_mir::ty::GenericParamDefKind;
match self {
ty::GenericParamDefKind::Lifetime => GenericParamDefKind::Lifetime,
ty::GenericParamDefKind::Type { has_default, synthetic } => {
GenericParamDefKind::Type { has_default: *has_default, synthetic: *synthetic }
}
ty::GenericParamDefKind::Const { has_default } => {
GenericParamDefKind::Const { has_default: *has_default }
}
}
}
}
impl<'tcx> Stable<'tcx> for rustc_middle::ty::GenericParamDef {
type T = stable_mir::ty::GenericParamDef;
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
GenericParamDef {
name: self.name.to_string(),
def_id: tables.generic_def(self.def_id),
index: self.index,
pure_wrt_drop: self.pure_wrt_drop,
kind: self.kind.stable(tables),
}
}
}

View File

@ -15,7 +15,7 @@
use crate::rustc_smir::Tables; use crate::rustc_smir::Tables;
use self::ty::{ImplDef, ImplTrait, TraitDecl, TraitDef, Ty, TyKind}; use self::ty::{GenericDef, Generics, ImplDef, ImplTrait, TraitDecl, TraitDef, Ty, TyKind};
pub mod mir; pub mod mir;
pub mod ty; pub mod ty;
@ -110,6 +110,7 @@ pub trait Context {
fn trait_decl(&mut self, trait_def: &TraitDef) -> TraitDecl; fn trait_decl(&mut self, trait_def: &TraitDef) -> TraitDecl;
fn all_trait_impls(&mut self) -> ImplTraitDecls; fn all_trait_impls(&mut self) -> ImplTraitDecls;
fn trait_impl(&mut self, trait_impl: &ImplDef) -> ImplTrait; fn trait_impl(&mut self, trait_impl: &ImplDef) -> ImplTrait;
fn generics_of(&mut self, generic_def: &GenericDef) -> Generics;
/// Get information about the local crate. /// Get information about the local crate.
fn local_crate(&self) -> Crate; fn local_crate(&self) -> Crate;
/// Retrieve a list of all external crates. /// Retrieve a list of all external crates.

View File

@ -22,7 +22,7 @@ pub struct Const {
type Ident = Opaque; type Ident = Opaque;
pub(crate) type Region = Opaque; pub(crate) type Region = Opaque;
type Span = Opaque; pub type Span = Opaque;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum TyKind { pub enum TyKind {
@ -87,34 +87,37 @@ pub enum Movability {
Movable, Movable,
} }
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ForeignDef(pub(crate) DefId); pub struct ForeignDef(pub(crate) DefId);
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct FnDef(pub(crate) DefId); pub struct FnDef(pub(crate) DefId);
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ClosureDef(pub(crate) DefId); pub struct ClosureDef(pub(crate) DefId);
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct GeneratorDef(pub(crate) DefId); pub struct GeneratorDef(pub(crate) DefId);
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ParamDef(pub(crate) DefId); pub struct ParamDef(pub(crate) DefId);
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct BrNamedDef(pub(crate) DefId); pub struct BrNamedDef(pub(crate) DefId);
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct AdtDef(pub(crate) DefId); pub struct AdtDef(pub(crate) DefId);
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct AliasDef(pub(crate) DefId); pub struct AliasDef(pub(crate) DefId);
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct TraitDef(pub(crate) DefId); pub struct TraitDef(pub(crate) DefId);
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct GenericDef(pub(crate) DefId);
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ConstDef(pub(crate) DefId); pub struct ConstDef(pub(crate) DefId);
impl TraitDef { impl TraitDef {
@ -132,6 +135,12 @@ pub fn trait_impl(&self) -> ImplTrait {
} }
} }
impl GenericDef {
pub fn generics_of(&self) -> Generics {
with(|tcx| tcx.generics_of(self))
}
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct GenericArgs(pub Vec<GenericArgKind>); pub struct GenericArgs(pub Vec<GenericArgKind>);
@ -461,3 +470,30 @@ pub struct TraitRef {
pub def_id: TraitDef, pub def_id: TraitDef,
pub args: GenericArgs, pub args: GenericArgs,
} }
#[derive(Clone, Debug)]
pub struct Generics {
pub parent: Option<GenericDef>,
pub parent_count: usize,
pub params: Vec<GenericParamDef>,
pub param_def_id_to_index: Vec<(GenericDef, u32)>,
pub has_self: bool,
pub has_late_bound_regions: Option<Span>,
pub host_effect_index: Option<usize>,
}
#[derive(Clone, Debug)]
pub enum GenericParamDefKind {
Lifetime,
Type { has_default: bool, synthetic: bool },
Const { has_default: bool },
}
#[derive(Clone, Debug)]
pub struct GenericParamDef {
pub name: super::Symbol,
pub def_id: GenericDef,
pub index: u32,
pub pure_wrt_drop: bool,
pub kind: GenericParamDefKind,
}