Skip to content

Commit

Permalink
Merge pull request #693 from hacspec/discriminant_values
Browse files Browse the repository at this point in the history
Export discriminant values in `AdtDef`
  • Loading branch information
Nadrieril authored Jun 24, 2024
2 parents 09925a7 + 5f44786 commit 1fc4a03
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 25 deletions.
54 changes: 37 additions & 17 deletions frontend/exporter/src/index_vec.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
use crate::prelude::*;
use rustc_index::{Idx, IndexSlice};

#[derive(
Clone, Debug, Serialize, Deserialize, JsonSchema, Hash, PartialEq, Eq, PartialOrd, Ord,
)]
pub struct IndexVec<I: rustc_index::Idx, T> {
pub struct IndexVec<I: Idx, T> {
pub raw: Vec<T>,
_marker: std::marker::PhantomData<fn(_: &I)>,
}

impl<I: rustc_index::Idx, T> Into<IndexVec<I, T>> for rustc_index::IndexVec<I, T> {
fn into(self) -> IndexVec<I, T> {
IndexVec {
raw: self.raw,
_marker: std::marker::PhantomData,
}
impl<I: Idx, T: Sized> IndexVec<I, T> {
pub fn into_iter_enumerated(
self,
) -> impl DoubleEndedIterator<Item = (I, T)> + ExactSizeIterator {
rustc_index::IndexVec::from_raw(self.raw).into_iter_enumerated()
}
pub fn into_iter(self) -> impl DoubleEndedIterator<Item = T> + ExactSizeIterator {
self.raw.into_iter()
}
}

impl<I: rustc_index::Idx, T: Sized> std::ops::Deref for IndexVec<I, T> {
type Target = rustc_index::IndexSlice<I, T>;
impl<I: Idx, T: Sized> std::ops::Deref for IndexVec<I, T> {
type Target = IndexSlice<I, T>;
fn deref(&self) -> &Self::Target {
Self::Target::from_raw(&self.raw)
}
}

impl<I: rustc_index::Idx, T: Sized> std::ops::DerefMut for IndexVec<I, T> {
impl<I: Idx, T: Sized> std::ops::DerefMut for IndexVec<I, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
Self::Target::from_raw_mut(&mut self.raw)
}
}

impl<
S,
J: rustc_index::Idx,
I: rustc_index::Idx + SInto<S, J>,
U: Clone, /*TODO: remove me?*/
T: SInto<S, U>,
> SInto<S, IndexVec<J, U>> for rustc_index::IndexSlice<I, T>
impl<I: Idx, T> Into<IndexVec<I, T>> for rustc_index::IndexVec<I, T> {
fn into(self) -> IndexVec<I, T> {
IndexVec {
raw: self.raw,
_marker: std::marker::PhantomData,
}
}
}

impl<S, J: Idx, I: Idx + SInto<S, J>, U: Clone /*TODO: remove me?*/, T: SInto<S, U>>
SInto<S, IndexVec<J, U>> for IndexSlice<I, T>
{
fn sinto(&self, s: &S) -> IndexVec<J, U> {
IndexVec {
Expand All @@ -46,6 +53,19 @@ impl<
}
}

impl<I, T> FromIterator<T> for IndexVec<I, T>
where
I: Idx,
{
#[inline]
fn from_iter<It: IntoIterator<Item = T>>(iter: It) -> Self {
Self {
raw: Vec::from_iter(iter),
_marker: std::marker::PhantomData,
}
}
}

macro_rules! make_idx_wrapper {
($($mod:ident)::+, $type:ident) => {
#[derive(Copy, Clone, Eq, Debug, Hash, PartialEq, PartialOrd, Ord, Serialize, Deserialize, JsonSchema)]
Expand Down
57 changes: 49 additions & 8 deletions frontend/exporter/src/types/copied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,11 +473,19 @@ pub enum CtorKind {
/// Reflects [`rustc_middle::ty::VariantDiscr`]
#[derive(AdtInto, Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[args(<'tcx, S: UnderOwnerState<'tcx>>, from: rustc_middle::ty::VariantDiscr, state: S as gstate)]
pub enum VariantDiscr {
pub enum DiscriminantDefinition {
Explicit(DefId),
Relative(u32),
}

/// Reflects [`rustc_middle::ty::util::Discr`]
#[derive(AdtInto, Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[args(<'tcx, S: UnderOwnerState<'tcx>>, from: rustc_middle::ty::util::Discr<'tcx>, state: S as gstate)]
pub struct DiscriminantValue {
pub val: u128,
pub ty: Ty,
}

/// Reflects [`rustc_middle::ty::Visibility`]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub enum Visibility<Id = rustc_span::def_id::LocalDefId> {
Expand Down Expand Up @@ -536,24 +544,40 @@ impl<'tcx, S: UnderOwnerState<'tcx>> SInto<S, FieldDef> for rustc_middle::ty::Fi
}

/// Reflects [`rustc_middle::ty::VariantDef`]
#[derive(AdtInto, Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[args(<'tcx, S: UnderOwnerState<'tcx>>, from: rustc_middle::ty::VariantDef, state: S as s)]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub struct VariantDef {
pub def_id: DefId,
pub ctor: Option<(CtorKind, DefId)>,
pub name: Symbol,
pub discr: VariantDiscr,
pub discr_def: DiscriminantDefinition,
pub discr_val: DiscriminantValue,
/// The definitions of the fields on this variant. In case of
/// [tuple
/// structs](https://doc.rust-lang.org/book/ch05-01-defining-structs.html#using-tuple-structs-without-named-fields-to-create-different-types),
/// the fields are anonymous, otherwise fields are named.
#[value(self.fields.raw.sinto(s))]
pub fields: Vec<FieldDef>,
/// Span of the definition of the variant
#[value(s.base().tcx.def_span(self.def_id).sinto(s))]
pub span: Span,
}

impl VariantDef {
fn sfrom<'tcx, S: UnderOwnerState<'tcx>>(
s: &S,
def: &ty::VariantDef,
discr_val: ty::util::Discr<'tcx>,
) -> Self {
VariantDef {
def_id: def.def_id.sinto(s),
ctor: def.ctor.sinto(s),
name: def.name.sinto(s),
discr_def: def.discr.sinto(s),
discr_val: discr_val.sinto(s),
fields: def.fields.raw.sinto(s),
span: s.base().tcx.def_span(def.def_id).sinto(s),
}
}
}

/// Reflects [`rustc_middle::ty::EarlyParamRegion`]
#[derive(
AdtInto, Clone, Debug, Serialize, Deserialize, JsonSchema, Hash, PartialEq, Eq, PartialOrd, Ord,
Expand Down Expand Up @@ -1785,10 +1809,27 @@ pub struct ReprOptions {

impl<'tcx, S: UnderOwnerState<'tcx>> SInto<S, AdtDef> for rustc_middle::ty::AdtDef<'tcx> {
fn sinto(&self, s: &S) -> AdtDef {
let variants = self
.variants()
.iter_enumerated()
.map(|(variant_idx, variant)| {
let discr = if self.is_enum() {
self.discriminant_for_variant(s.base().tcx, variant_idx)
} else {
// Structs and unions have a single variant.
assert_eq!(variant_idx.index(), 0);
rustc_middle::ty::util::Discr {
val: 0,
ty: s.base().tcx.types.isize,
}
};
VariantDef::sfrom(s, variant, discr)
})
.collect();
AdtDef {
did: self.did().sinto(s),
adt_kind: self.adt_kind().sinto(s),
variants: self.variants().sinto(s),
variants,
flags: self.flags().sinto(s),
repr: self.repr().sinto(s),
}
Expand Down Expand Up @@ -2892,7 +2933,7 @@ pub struct Variant<Body: IsBody> {
.find(|v| v.def_id == self.def_id.into()).unwrap();
variant.discr.sinto(s)
})]
pub discr: VariantDiscr,
pub discr: DiscriminantDefinition,
pub span: Span,
#[value(s.base().tcx.hir().attrs(hir_id.clone()).sinto(s))]
pub attributes: Vec<Attribute>,
Expand Down

0 comments on commit 1fc4a03

Please sign in to comment.