Skip to main content

charon_driver/frontend/types/
adt.rs

1use crate::frontend::prelude::*;
2
3use rustc_middle::ty;
4
5sinto_reexport!(rustc_abi::Align);
6
7/// Reflects [`ty::VariantDiscr`]
8#[derive(Debug, Clone, AdtInto)]
9#[args(<'tcx, S: UnderOwnerState<'tcx>>, from: ty::VariantDiscr, state: S as gstate)]
10pub enum DiscriminantDefinition {
11    Explicit(DefId),
12    Relative(u32),
13}
14
15/// Reflects [`ty::util::Discr`]
16#[derive(Debug, Clone, AdtInto)]
17#[args(< S: UnderOwnerState<'tcx>>, from: ty::util::Discr<'tcx>, state: S as gstate)]
18pub struct DiscriminantValue<'tcx> {
19    pub val: u128,
20    pub ty: Ty<'tcx>,
21}
22
23/// Reflects [`ty::Visibility`]
24#[derive(Debug, Clone)]
25pub enum Visibility<Id> {
26    Public,
27    Restricted(Id),
28}
29
30impl<S, T: SInto<S, U>, U> SInto<S, Visibility<U>> for ty::Visibility<T> {
31    fn sinto(&self, s: &S) -> Visibility<U> {
32        use ty::Visibility as T;
33        match self {
34            T::Public => Visibility::Public,
35            T::Restricted(id) => Visibility::Restricted(id.sinto(s)),
36        }
37    }
38}
39
40/// Reflects [`ty::FieldDef`]
41#[derive(Debug, Clone)]
42pub struct FieldDef<'tcx> {
43    pub did: DefId,
44    /// Field definition of [tuple
45    /// structs](https://doc.rust-lang.org/book/ch05-01-defining-structs.html#using-tuple-structs-without-named-fields-to-create-different-types)
46    /// are anonymous, in that case `name` is [`None`].
47    pub name: Option<Symbol>,
48    pub vis: Visibility<DefId>,
49    pub ty: Ty<'tcx>,
50    pub span: Span,
51}
52
53impl<'tcx> FieldDef<'tcx> {
54    pub fn sfrom<S: UnderOwnerState<'tcx>>(
55        s: &S,
56        fdef: &ty::FieldDef,
57        instantiate: ty::GenericArgs<'tcx>,
58    ) -> FieldDef<'tcx> {
59        let tcx = s.base().tcx;
60        let ty = normalize(tcx, s.typing_env(), fdef.ty(tcx, instantiate)).sinto(s);
61        let name = {
62            let name = fdef.name.sinto(s);
63            let is_user_provided = {
64                // SH: Note that the only way I found of checking if the user wrote the name or if it
65                // is just an integer generated by rustc is by checking if it is just made of
66                // numerals...
67                name.to_string().parse::<usize>().is_err()
68            };
69            is_user_provided.then_some(name)
70        };
71
72        FieldDef {
73            did: fdef.did.sinto(s),
74            name,
75            vis: fdef.vis.map_id(|mod_id| mod_id.to_def_id()).sinto(s),
76            ty,
77            span: tcx.def_span(fdef.did).sinto(s),
78        }
79    }
80}
81
82/// Reflects [`ty::VariantDef`]
83#[derive(Debug, Clone)]
84pub struct VariantDef<'tcx> {
85    pub def_id: DefId,
86    pub ctor: Option<(CtorKind, DefId)>,
87    pub name: Symbol,
88    pub discr_def: DiscriminantDefinition,
89    pub discr_val: DiscriminantValue<'tcx>,
90    /// The definitions of the fields on this variant. In case of [tuple
91    /// structs/variants](https://doc.rust-lang.org/book/ch05-01-defining-structs.html#using-tuple-structs-without-named-fields-to-create-different-types),
92    /// the fields are anonymous, otherwise fields are named.
93    pub fields: IndexVec<FieldIdx, FieldDef<'tcx>>,
94    /// Span of the definition of the variant
95    pub span: Span,
96}
97
98impl<'tcx> VariantDef<'tcx> {
99    pub(crate) fn sfrom<S: UnderOwnerState<'tcx>>(
100        s: &S,
101        def: &ty::VariantDef,
102        discr_val: ty::util::Discr<'tcx>,
103        instantiate: Option<ty::GenericArgs<'tcx>>,
104    ) -> Self {
105        let def_id = def.def_id.sinto(s);
106        let instantiate = instantiate.unwrap_or_else(|| def_id.identity_args(s));
107        VariantDef {
108            def_id,
109            ctor: def.ctor.sinto(s),
110            name: def.name.sinto(s),
111            discr_def: def.discr.sinto(s),
112            discr_val: discr_val.sinto(s),
113            fields: def
114                .fields
115                .iter()
116                .map(|f| FieldDef::sfrom(s, f, instantiate))
117                .collect(),
118            span: s.base().tcx.def_span(def.def_id).sinto(s),
119        }
120    }
121}
122
123/// Reflects [`ty::AdtKind`]
124#[derive(Debug, Copy, Clone)]
125pub enum AdtKind {
126    Struct,
127    Union,
128    Enum,
129    /// We sometimes pretend arrays are an ADT and generate a `FullDef` for them.
130    Array,
131    /// We sometimes pretend slices are an ADT and generate a `FullDef` for them.
132    Slice,
133    /// Tuples get a type declaration of their own, we always generate a `FullDef` for them.
134    Tuple,
135    /// `str` gets a type declaration of its own, we always generate a `FullDef` for it.
136    Str,
137    /// The struct that represents the vtable for a trait.
138    VTable,
139}
140
141impl<'tcx, S: UnderOwnerState<'tcx>> SInto<S, AdtKind> for ty::AdtKind {
142    fn sinto(&self, _s: &S) -> AdtKind {
143        match self {
144            ty::AdtKind::Struct => AdtKind::Struct,
145            ty::AdtKind::Union => AdtKind::Union,
146            ty::AdtKind::Enum => AdtKind::Enum,
147        }
148    }
149}
150
151/// Reflects [`rustc_abi::ReprOptions`].
152#[derive(Debug, Clone, AdtInto)]
153#[args(< S: UnderOwnerState<'tcx>>, from: rustc_abi::ReprOptions, state: S as s)]
154pub struct ReprOptions<'tcx> {
155    /// Whether an explicit integer representation was specified.
156    #[value(self.int.is_some())]
157    pub int_specified: bool,
158    /// The actual discriminant type resulting from the representation options.
159    #[value({
160        use rustc_middle::ty::util::IntTypeExt;
161        self.discr_type().to_ty(s.base().tcx).sinto(s)
162    })]
163    pub typ: Ty<'tcx>,
164    pub align: Option<Align>,
165    pub pack: Option<Align>,
166    #[value(ReprFlags { is_c: self.c(), is_transparent: self.transparent(), is_simd: self.simd() })]
167    pub flags: ReprFlags,
168}
169
170/// The representation flags without the ones irrelevant outside of rustc.
171#[derive(Debug, Default, Clone)]
172pub struct ReprFlags {
173    pub is_c: bool,
174    pub is_transparent: bool,
175    pub is_simd: bool,
176}