1use derive_generic_visitor::*;
2use enum_as_inner::EnumAsInner;
3use serde::{Deserialize, Serialize};
4use serde_state::{DeserializeState, SerializeState};
5
6use crate::ast::*;
7use crate::ids::IndexVec;
8use crate::utils::serialize_map_to_array::SeqHashMapToArray;
9
10#[derive(Debug, Clone)]
24#[derive(SerializeState, DeserializeState, Drive, DriveMut, DriveTwo)]
25#[serde_state(state_implements = DedupSerializerState)]
26pub struct TypeDecl {
27 pub def_id: TypeDeclId,
28 pub item_meta: ItemMeta,
30 pub generics: GenericParams,
31 pub src: TypeSource,
33 pub kind: TypeDeclKind,
35 pub layout: TargetLayouts,
38 #[serde(with = "SeqHashMapToArray::<GenericArgs, TargetLayouts>")]
41 pub monomorphic_layouts: SeqHashMap<GenericArgs, TargetLayouts>,
42 pub ptr_metadata: PtrMetadata,
44 pub marker_traits: Option<Box<ImplementsMarkerTraits>>,
47}
48
49generate_index_type!(VariantId, "Variant");
50generate_index_type!(FieldId, "Field");
51
52#[derive(Debug, Clone)]
53#[derive(EnumAsInner)]
54#[derive(SerializeState, DeserializeState, Drive, DriveMut, DriveTwo)]
55pub enum TypeDeclKind {
56 Struct(IndexVec<FieldId, Field>),
57 Enum(IndexVec<VariantId, Variant>),
58 Union(IndexVec<FieldId, Field>),
59 Opaque,
63 Alias(Ty),
66 #[cfg_attr(feature = "charon_on_charon", charon::rename("TDeclError"))]
69 Error(String),
70}
71
72#[derive(Debug, Clone)]
73#[derive(SerializeState, DeserializeState, Drive, DriveMut, DriveTwo)]
74#[serde_state(stateless)]
75pub struct Variant {
76 pub id: VariantId,
77 pub span: Span,
78 pub attr_info: AttrInfo,
79 #[cfg_attr(feature = "charon_on_charon", charon::rename("variant_name"))]
80 pub name: String,
81 #[serde_state(stateful)]
82 pub fields: IndexVec<FieldId, Field>,
83 pub discriminant: IntegerValue,
87}
88
89#[derive(Debug, Clone)]
90#[derive(SerializeState, DeserializeState, Drive, DriveMut, DriveTwo)]
91#[serde_state(stateless)]
92pub struct Field {
93 pub span: Span,
94 pub attr_info: AttrInfo,
95 #[cfg_attr(feature = "charon_on_charon", charon::rename("field_name"))]
96 pub name: String,
97 pub is_positional: bool,
100 #[cfg_attr(feature = "charon_on_charon", charon::rename("field_ty"))]
101 #[serde_state(stateful)]
102 pub ty: Ty,
103}
104
105#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
109#[derive(EnumAsInner)]
110#[derive(SerializeState, DeserializeState, Drive, DriveMut, DriveTwo)]
111#[serde_state(default_state = ())]
112pub enum PtrMetadata {
113 #[cfg_attr(feature = "charon_on_charon", charon::rename("NoMetadata"))]
115 None,
116 Length,
122 VTable(TypeDeclRef),
124 InheritFrom(Ty),
128}
129
130#[derive(Debug, Clone)]
132#[derive(EnumAsInner)]
133#[derive(SerializeState, DeserializeState, Drive, DriveMut, DriveTwo)]
134#[cfg_attr(feature = "charon_on_charon", charon::variants_suffix("Type"))]
135pub enum TypeSource {
136 Normal,
138 Closure { info: ClosureInfo },
140 VTable {
142 dyn_predicate: DynPredicate,
144 field_map: IndexVec<FieldId, VTableField>,
146 supertrait_map: IndexVec<TraitClauseId, Option<FieldId>>,
149 },
150 Builtin(BuiltinAdt),
152}
153
154#[derive(Debug, Clone, Copy, PartialEq, Eq)]
156#[derive(SerializeState, DeserializeState, Drive, DriveMut, DriveTwo)]
157#[serde_state(stateless)]
158pub struct ImplementsMarkerTraits {
159 pub is_sized: bool,
160 pub is_send: bool,
161 pub is_sync: bool,
162 pub is_freeze: bool,
163 pub is_unpin: bool,
164}
165
166#[derive(Debug, Clone, PartialEq, Eq)]
167#[derive(SerializeState, DeserializeState, Drive, DriveMut, DriveTwo)]
168#[cfg_attr(feature = "charon_on_charon", charon::variants_prefix("VTable"))]
169pub enum VTableField {
170 Size,
171 Align,
172 Drop,
173 Method(TraitMethodId, MetadataPath),
174 SuperTrait(TraitClauseId),
175}
176
177#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
179#[derive(SerializeState, DeserializeState, Drive, DriveMut, DriveTwo)]
180pub struct ClosureInfo {
181 #[serde_state(stateless)]
182 pub kind: ClosureKind,
183 pub closure_body: FunDeclRef,
185 pub fn_once_impl: RegionBinder<TraitImplRef>,
187 pub fn_mut_impl: Option<RegionBinder<TraitImplRef>>,
189 pub fn_impl: Option<RegionBinder<TraitImplRef>>,
191 pub signature: RegionBinder<FunSig>,
193}
194
195#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
196#[derive(Serialize, Deserialize, Drive, DriveMut, DriveTwo)]
197pub enum ClosureKind {
198 Fn,
199 FnMut,
200 FnOnce,
201}
202
203impl TypeDecl {
204 pub fn get_fields(&self, variant: Option<VariantId>) -> Option<&IndexVec<FieldId, Field>> {
205 match &self.kind {
206 TypeDeclKind::Struct(fields) | TypeDeclKind::Union(fields) => Some(fields),
207 TypeDeclKind::Enum(variants) => Some(&variants[variant.unwrap()].fields),
208 _ => None,
209 }
210 }
211
212 pub fn get_field(&self, variant: Option<VariantId>, field: FieldId) -> Option<&Field> {
213 self.get_fields(variant)?.get(field)
214 }
215
216 pub fn get_field_by_name(
217 &self,
218 variant: Option<VariantId>,
219 field_name: &str,
220 ) -> Option<(FieldId, &Field)> {
221 let fields = match &self.kind {
222 TypeDeclKind::Struct(fields) | TypeDeclKind::Union(fields) => fields,
223 TypeDeclKind::Enum(variants) => &variants[variant.unwrap()].fields,
224 _ => return None,
225 };
226 fields
227 .iter_enumerated()
228 .find(|(_, field)| field.name == field_name)
229 }
230
231 pub fn self_ref(&self) -> TypeDeclRef {
233 TypeDeclRef {
234 id: self.def_id,
235 generics: Box::new(self.generics.identity_args()),
236 builtin: self.src.as_builtin().cloned(),
237 }
238 }
239}
240
241impl Variant {
242 pub fn renamed_name(&self) -> &str {
245 self.attr_info
246 .rename
247 .as_deref()
248 .unwrap_or(self.name.as_ref())
249 }
250
251 pub fn is_opaque(&self) -> bool {
253 self.attr_info
254 .attributes
255 .iter()
256 .any(|attr| attr.is_opaque())
257 }
258}
259
260impl Field {
261 pub fn renamed_name(&self) -> &str {
263 self.attr_info.rename.as_deref().unwrap_or(&self.name)
264 }
265
266 pub fn is_opaque(&self) -> bool {
268 self.attr_info
269 .attributes
270 .iter()
271 .any(|attr| attr.is_opaque())
272 }
273}
274
275impl ClosureKind {
276 pub fn method_name(self) -> &'static str {
278 match self {
279 ClosureKind::FnOnce => "call_once",
280 ClosureKind::FnMut => "call_mut",
281 ClosureKind::Fn => "call",
282 }
283 }
284}
285
286impl PtrMetadata {
287 pub fn into_type(self) -> Ty {
288 match self {
289 PtrMetadata::None => Ty::mk_unit(),
290 PtrMetadata::Length => Ty::mk_usize(),
291 PtrMetadata::VTable(type_decl_ref) => Ty::new(TyKind::Ref(
292 Region::Static,
293 Ty::new(TyKind::Adt(type_decl_ref)),
294 RefKind::Shared,
295 )),
296 PtrMetadata::InheritFrom(ty) => Ty::new(TyKind::PtrMetadata(ty)),
297 }
298 }
299}