1
Fork 0

Add FieldDef to StableMIR and methods to get type

This commit is contained in:
Celina G. Val 2023-12-04 20:08:25 -08:00
parent e19c7cd159
commit 1720b108f7
6 changed files with 95 additions and 5 deletions

View file

@ -12,8 +12,8 @@ use stable_mir::mir::alloc::GlobalAlloc;
use stable_mir::mir::mono::{InstanceDef, StaticDef};
use stable_mir::mir::Body;
use stable_mir::ty::{
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FnDef, GenericArgs, LineInfo,
PolyFnSig, RigidTy, Span, TyKind, VariantDef,
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, GenericArgs,
LineInfo, PolyFnSig, RigidTy, Span, TyKind, VariantDef,
};
use stable_mir::{self, Crate, CrateItem, DefId, Error, Filename, ItemKind, Symbol};
use std::cell::RefCell;
@ -219,6 +219,11 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
def.internal(&mut *tables).name.to_string()
}
fn variant_fields(&self, def: VariantDef) -> Vec<FieldDef> {
let mut tables = self.0.borrow_mut();
def.internal(&mut *tables).fields.iter().map(|f| f.stable(&mut *tables)).collect()
}
fn eval_target_usize(&self, cnst: &Const) -> Result<u64, Error> {
let mut tables = self.0.borrow_mut();
let mir_const = cnst.internal(&mut *tables);
@ -250,6 +255,18 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
tables.tcx.type_of(item.internal(&mut *tables)).instantiate_identity().stable(&mut *tables)
}
fn def_ty_with_args(
&self,
item: stable_mir::DefId,
args: &GenericArgs,
) -> Result<stable_mir::ty::Ty, Error> {
let mut tables = self.0.borrow_mut();
let args = args.internal(&mut *tables);
let def_ty = tables.tcx.type_of(item.internal(&mut *tables));
// FIXME(celinval): use try_fold instead to avoid crashing.
Ok(def_ty.instantiate(tables.tcx, args).stable(&mut *tables))
}
fn const_literal(&self, cnst: &stable_mir::ty::Const) -> String {
internal(cnst).to_string()
}

View file

@ -60,6 +60,14 @@ impl<'tcx> Stable<'tcx> for rustc_hir::CoroutineKind {
}
}
impl<'tcx> Stable<'tcx> for rustc_span::Symbol {
type T = stable_mir::Symbol;
fn stable(&self, _tables: &mut Tables<'tcx>) -> Self::T {
self.to_string()
}
}
impl<'tcx> Stable<'tcx> for rustc_span::Span {
type T = stable_mir::ty::Span;
@ -68,6 +76,16 @@ impl<'tcx> Stable<'tcx> for rustc_span::Span {
}
}
impl<'tcx, T> Stable<'tcx> for &[T]
where
T: Stable<'tcx>,
{
type T = Vec<T::T>;
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
self.iter().map(|e| e.stable(tables)).collect()
}
}
impl<'tcx, T, U> Stable<'tcx> for (T, U)
where
T: Stable<'tcx>,

View file

@ -137,6 +137,17 @@ impl<'tcx> Stable<'tcx> for ty::AdtKind {
}
}
impl<'tcx> Stable<'tcx> for ty::FieldDef {
type T = stable_mir::ty::FieldDef;
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
stable_mir::ty::FieldDef {
def: tables.create_def_id(self.did),
name: self.name.stable(tables),
}
}
}
impl<'tcx> Stable<'tcx> for ty::GenericArgs<'tcx> {
type T = stable_mir::ty::GenericArgs;
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {