charon_driver/frontend/types/
adt.rs1use crate::frontend::prelude::*;
2
3use rustc_middle::ty;
4
5sinto_reexport!(rustc_abi::Align);
6
7#[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#[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#[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#[derive(Debug, Clone)]
42pub struct FieldDef<'tcx> {
43 pub did: DefId,
44 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 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#[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 pub fields: IndexVec<FieldIdx, FieldDef<'tcx>>,
94 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#[derive(Debug, Copy, Clone)]
125pub enum AdtKind {
126 Struct,
127 Union,
128 Enum,
129 Array,
131 Slice,
133 Tuple,
135 Str,
137 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#[derive(Debug, Clone, AdtInto)]
153#[args(< S: UnderOwnerState<'tcx>>, from: rustc_abi::ReprOptions, state: S as s)]
154pub struct ReprOptions<'tcx> {
155 #[value(self.int.is_some())]
157 pub int_specified: bool,
158 #[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#[derive(Debug, Default, Clone)]
172pub struct ReprFlags {
173 pub is_c: bool,
174 pub is_transparent: bool,
175 pub is_simd: bool,
176}