Skip to main content

charon_driver/translate/
translate_types.rs

1use itertools::Itertools;
2use rustc_middle::ty;
3use rustc_span::sym;
4
5use super::translate_ctx::*;
6use charon_lib::ast::*;
7use charon_lib::ids::{IndexMap, IndexVec};
8use hax::{HasOwner, HasParamEnv, Visibility};
9
10impl<'tcx, 'ctx> ItemTransCtx<'tcx, 'ctx> {
11    /// Translate an erased region. If we're inside a body, this will return a fresh body region
12    /// instead.
13    pub(crate) fn translate_erased_region(&mut self) -> Region {
14        if let Some(v) = &mut self.lifetime_freshener {
15            Region::Body(v.push(()))
16        } else {
17            Region::Erased
18        }
19    }
20
21    /// Erase a region binder by supplying erased lifetimes (or fresh body lifetimes) for all its
22    /// arguments.
23    pub(crate) fn erase_region_binder<T: TyVisitable>(&mut self, b: RegionBinder<T>) -> T {
24        let regions = b
25            .regions
26            .map_ref_indexed(|_, _| self.translate_erased_region());
27        b.apply(regions)
28    }
29
30    // Translate a region
31    pub(crate) fn translate_region(
32        &mut self,
33        span: Span,
34        region: &hax::Region,
35    ) -> Result<Region, Error> {
36        use hax::RegionKind::*;
37        match &region.kind {
38            ReErased => Ok(self.translate_erased_region()),
39            ReStatic => Ok(Region::Static),
40            ReBound(hax::BoundVarIndexKind::Bound(id), br) => {
41                Ok(match self.lookup_bound_region(span, *id, br.var) {
42                    Ok(var) => Region::Var(var),
43                    Err(_) => Region::Erased,
44                })
45            }
46            ReEarlyParam(region) => Ok(match self.lookup_early_region(span, region) {
47                Ok(var) => Region::Var(var),
48                Err(_) => Region::Erased,
49            }),
50            ReVar(..) | RePlaceholder(..) => {
51                // Shouldn't exist outside of type inference.
52                raise_error!(
53                    self,
54                    span,
55                    "Should not exist outside of type inference: {region:?}"
56                )
57            }
58            ReBound(..) | ReLateParam(..) | ReError(..) => {
59                raise_error!(self, span, "Unexpected region kind: {region:?}")
60            }
61        }
62    }
63
64    pub(crate) fn translate_hax_int_ty(int_ty: &hax::IntTy) -> IntTy {
65        match int_ty {
66            hax::IntTy::Isize => IntTy::Isize,
67            hax::IntTy::I8 => IntTy::I8,
68            hax::IntTy::I16 => IntTy::I16,
69            hax::IntTy::I32 => IntTy::I32,
70            hax::IntTy::I64 => IntTy::I64,
71            hax::IntTy::I128 => IntTy::I128,
72        }
73    }
74
75    pub(crate) fn translate_hax_uint_ty(uint_ty: &hax::UintTy) -> UIntTy {
76        use hax::UintTy;
77        match uint_ty {
78            UintTy::Usize => UIntTy::Usize,
79            UintTy::U8 => UIntTy::U8,
80            UintTy::U16 => UIntTy::U16,
81            UintTy::U32 => UIntTy::U32,
82            UintTy::U64 => UIntTy::U64,
83            UintTy::U128 => UIntTy::U128,
84        }
85    }
86
87    /// Translate a Ty.
88    ///
89    /// Typically used in this module to translate the fields of a structure/
90    /// enumeration definition, or later to translate the type of a variable.
91    ///
92    /// Note that we take as parameter a function to translate regions, because
93    /// regions can be translated in several manners (non-erased region or erased
94    /// regions), in which case the return type is different.
95    #[tracing::instrument(skip(self, span))]
96    pub(crate) fn translate_ty(&mut self, span: Span, hax_ty: &hax::Ty) -> Result<Ty, Error> {
97        let mut ty = if let Some(ty) = self
98            .innermost_binder()
99            .type_trans_cache
100            .get(&hax_ty)
101            .cloned()
102        {
103            ty
104        } else {
105            let ty = self
106                .translate_ty_inner(span, hax_ty)
107                .unwrap_or_else(|e| TyKind::Error(e.msg).into_ty());
108            self.innermost_binder_mut()
109                .type_trans_cache
110                .insert(hax_ty.clone(), ty.clone());
111            ty
112        };
113        if let Some(v) = &mut self.lifetime_freshener {
114            // We might be reusing a value from cache: we must refresh the erased & body regions.
115            ty = ty.replace_erased_regions(|| Region::Body(v.push(())));
116        }
117        Ok(ty)
118    }
119
120    fn translate_ty_inner(&mut self, span: Span, ty: &hax::Ty) -> Result<Ty, Error> {
121        trace!("{:?}", ty);
122        let kind = match ty.kind() {
123            hax::TyKind::Bool => TyKind::Literal(LiteralTy::Bool),
124            hax::TyKind::Char => TyKind::Literal(LiteralTy::Char),
125            hax::TyKind::Int(int_ty) => {
126                TyKind::Literal(LiteralTy::Int(Self::translate_hax_int_ty(int_ty)))
127            }
128            hax::TyKind::Uint(uint_ty) => {
129                TyKind::Literal(LiteralTy::UInt(Self::translate_hax_uint_ty(uint_ty)))
130            }
131            hax::TyKind::Float(float_ty) => {
132                use hax::FloatTy;
133                TyKind::Literal(LiteralTy::Float(match float_ty {
134                    FloatTy::F16 => types::FloatTy::F16,
135                    FloatTy::F32 => types::FloatTy::F32,
136                    FloatTy::F64 => types::FloatTy::F64,
137                    FloatTy::F128 => types::FloatTy::F128,
138                }))
139            }
140            hax::TyKind::Never => TyKind::Never,
141
142            hax::TyKind::Alias(alias) => match &alias.kind {
143                hax::AliasKind::Projection {
144                    impl_expr,
145                    assoc_item,
146                } => {
147                    let trait_ref = self.translate_trait_impl_expr(span, impl_expr)?;
148                    let name = self.t_ctx.translate_trait_item_name(&assoc_item.def_id)?;
149                    TyKind::TraitType(trait_ref, name)
150                }
151                hax::AliasKind::Opaque { hidden_ty, .. } => {
152                    return self.translate_ty(span, hidden_ty);
153                }
154                _ => {
155                    raise_error!(self, span, "Unsupported alias type: {:?}", alias.kind)
156                }
157            },
158
159            hax::TyKind::Adt(item) => {
160                let tref = self.translate_type_decl_ref(span, item)?;
161                TyKind::Adt(tref)
162            }
163            hax::TyKind::Str => {
164                let tref = TypeDeclRef::new(TypeId::Builtin(BuiltinTy::Str), GenericArgs::empty());
165                TyKind::Adt(tref)
166            }
167            hax::TyKind::Array(item_ref) => {
168                let mut args = self.translate_generic_args(span, &item_ref.generic_args, &[])?;
169                assert!(args.types.elem_count() == 1 && args.const_generics.elem_count() == 1);
170                TyKind::Array(
171                    args.types.pop().unwrap(),
172                    Box::new(args.const_generics.pop().unwrap()),
173                )
174            }
175            hax::TyKind::Slice(item_ref) => {
176                let mut args = self.translate_generic_args(span, &item_ref.generic_args, &[])?;
177                assert!(args.types.elem_count() == 1);
178                TyKind::Slice(args.types.pop().unwrap())
179            }
180            hax::TyKind::Tuple(item_ref) => {
181                let args = self.translate_generic_args(span, &item_ref.generic_args, &[])?;
182                let tref = TypeDeclRef::new(TypeId::Tuple, args);
183                TyKind::Adt(tref)
184            }
185            hax::TyKind::Ref(region, ty, mutability) => {
186                trace!("Ref");
187
188                let region = self.translate_region(span, region)?;
189                let ty = self.translate_ty(span, ty)?;
190                let kind = if mutability.is_mut() {
191                    RefKind::Mut
192                } else {
193                    RefKind::Shared
194                };
195                TyKind::Ref(region, ty, kind)
196            }
197            hax::TyKind::RawPtr(ty, mutbl) => {
198                trace!("RawPtr: {:?}", (ty, mutbl));
199                let ty = self.translate_ty(span, ty)?;
200                let kind = if mutbl.is_mut() {
201                    RefKind::Mut
202                } else {
203                    RefKind::Shared
204                };
205                TyKind::RawPtr(ty, kind)
206            }
207
208            hax::TyKind::Param(param) => {
209                // A type parameter, for example `T` in `fn f<T>(x : T) {}`.
210                // Note that this type parameter may actually have been
211                // instantiated (in our environment, we may map it to another
212                // type): we just have to look it up.
213                // Note that if we are using this function to translate a field
214                // type in a type definition, it should actually map to a type
215                // parameter.
216                match self.lookup_type_var(span, param) {
217                    Ok(var) => TyKind::TypeVar(var),
218                    Err(err) => TyKind::Error(err.msg),
219                }
220            }
221
222            hax::TyKind::Foreign(item) => {
223                let tref = self.translate_type_decl_ref(span, item)?;
224                TyKind::Adt(tref)
225            }
226
227            hax::TyKind::Arrow(sig) => {
228                trace!("Arrow");
229                trace!("bound vars: {:?}", sig.bound_vars);
230                let sig = self.translate_poly_fun_sig(span, sig)?;
231                TyKind::FnPtr(sig)
232            }
233            hax::TyKind::FnDef { item, .. } => {
234                let fnref = self.translate_bound_fn_ptr(span, item, TransItemSourceKind::Fun)?;
235                TyKind::FnDef(fnref)
236            }
237            hax::TyKind::Closure(args) => {
238                let tref = self.translate_closure_type_ref(span, args)?;
239                TyKind::Adt(tref)
240            }
241
242            hax::TyKind::Dynamic(dyn_binder, region) => {
243                self.check_no_monomorphize(span)?;
244                // Translate the region outside the binder.
245                let region = self.translate_region(span, region)?;
246
247                let binder = self.translate_dyn_binder(span, dyn_binder, |ctx, ty, ()| {
248                    let region = region.move_under_binder();
249                    ctx.innermost_binder_mut()
250                        .params
251                        .types_outlive
252                        .push(RegionBinder::empty(OutlivesPred(ty.clone(), region)));
253                    Ok(ty)
254                })?;
255
256                if let hax::ClauseKind::Trait(trait_predicate) = dyn_binder.predicates.predicates[0]
257                    .0
258                    .kind
259                    .hax_skip_binder_ref()
260                {
261                    // TODO(dyn): for now, we consider traits with associated types to not be dyn
262                    // compatible because we don't know how to handle them; for these we skip
263                    // translating the vtable.
264                    if self.trait_is_dyn_compatible(&trait_predicate.trait_ref.def_id)? {
265                        // Ensure the vtable type is translated. The first predicate is the one that
266                        // can have methods, i.e. a vtable.
267                        let _: TypeDeclId = self.register_item(
268                            span,
269                            &trait_predicate.trait_ref,
270                            TransItemSourceKind::VTable,
271                        );
272                    }
273                }
274                TyKind::DynTrait(DynPredicate { binder })
275            }
276
277            hax::TyKind::Infer(_) => {
278                raise_error!(self, span, "Unsupported type: infer type")
279            }
280            hax::TyKind::Coroutine(..) => {
281                raise_error!(self, span, "Coroutine types are not supported yet")
282            }
283            hax::TyKind::Bound(_, _) => {
284                raise_error!(self, span, "Unexpected type kind: bound")
285            }
286            hax::TyKind::Placeholder(_) => {
287                raise_error!(self, span, "Unsupported type: placeholder")
288            }
289
290            hax::TyKind::Error => {
291                raise_error!(self, span, "Type checking error")
292            }
293            hax::TyKind::Todo(s) => {
294                raise_error!(self, span, "Unsupported type: {:?}", s)
295            }
296        };
297        Ok(kind.into_ty())
298    }
299
300    pub(crate) fn translate_rustc_ty(
301        &mut self,
302        span: Span,
303        ty: &ty::Ty<'tcx>,
304    ) -> Result<Ty, Error> {
305        let ty = self.t_ctx.catch_sinto(&self.hax_state, span, ty)?;
306        self.translate_ty(span, &ty)
307    }
308
309    pub fn translate_poly_fun_sig(
310        &mut self,
311        span: Span,
312        sig: &hax::Binder<hax::TyFnSig>,
313    ) -> Result<RegionBinder<FunSig>, Error> {
314        self.translate_region_binder(span, sig, |ctx, sig| ctx.translate_fun_sig(span, sig))
315    }
316    pub fn translate_fun_sig(&mut self, span: Span, sig: &hax::TyFnSig) -> Result<FunSig, Error> {
317        let inputs = sig
318            .inputs
319            .iter()
320            .map(|x| self.translate_ty(span, x))
321            .try_collect()?;
322        let output = self.translate_ty(span, &sig.output)?;
323        Ok(FunSig {
324            is_unsafe: sig.safety == hax::Safety::Unsafe,
325            inputs,
326            output,
327        })
328    }
329
330    /// Translate generic args. Don't call directly; use `translate_xxx_ref` as much as possible.
331    pub fn translate_generic_args(
332        &mut self,
333        span: Span,
334        substs: &[hax::GenericArg],
335        trait_refs: &[hax::ImplExpr],
336    ) -> Result<GenericArgs, Error> {
337        use hax::GenericArg::*;
338        trace!("{:?}", substs);
339
340        let mut regions = IndexMap::new();
341        let mut types = IndexMap::new();
342        let mut const_generics = IndexMap::new();
343        for param in substs {
344            match param {
345                Type(param_ty) => {
346                    types.push(self.translate_ty(span, param_ty)?);
347                }
348                Lifetime(region) => {
349                    regions.push(self.translate_region(span, region)?);
350                }
351                Const(c) => {
352                    const_generics.push(self.translate_constant_expr(span, c)?);
353                }
354            }
355        }
356        let trait_refs = self.translate_trait_impl_exprs(span, trait_refs)?;
357
358        Ok(GenericArgs {
359            regions,
360            types,
361            const_generics,
362            trait_refs,
363        })
364    }
365
366    /// Checks whether the given id corresponds to a built-in type.
367    pub(crate) fn recognize_builtin_type(
368        &mut self,
369        item: &hax::ItemRef,
370    ) -> Result<Option<BuiltinTy>, Error> {
371        let def = self.hax_def(item)?;
372        let ty = if def.lang_item == Some(sym::owned_box) && self.t_ctx.options.treat_box_as_builtin
373        {
374            Some(BuiltinTy::Box)
375        } else {
376            None
377        };
378        Ok(ty)
379    }
380
381    /// Translate a Dynamically Sized Type metadata kind.
382    ///
383    /// Returns `None` if the type is generic, or if it is not a DST.
384    pub fn translate_ptr_metadata(
385        &mut self,
386        span: Span,
387        item: &hax::ItemRef,
388    ) -> Result<PtrMetadata, Error> {
389        // prepare the call to the method
390        use rustc_middle::ty;
391        let tcx = self.t_ctx.tcx;
392        let rdefid = item.def_id.real_rust_def_id();
393        let hax_state = &self.hax_state;
394        let ty_env = hax_state.typing_env();
395        let ty = tcx
396            .type_of(rdefid)
397            .instantiate(tcx, item.rustc_args(hax_state));
398
399        // Get the tail type, which determines the metadata of `ty`.
400        let tail_ty = tcx.struct_tail_raw(
401            ty,
402            &rustc_middle::traits::ObligationCause::dummy(),
403            |ty| tcx.try_normalize_erasing_regions(ty_env, ty).unwrap_or(ty),
404            || {},
405        );
406        let hax_ty: hax::Ty = self.t_ctx.catch_sinto(hax_state, span, &tail_ty)?;
407
408        // If we're hiding `Sized`, let's consider everything to be sized.
409        let everything_is_sized = self.t_ctx.options.hide_marker_traits;
410        let ret = match tail_ty.kind() {
411            _ if everything_is_sized || tail_ty.is_sized(tcx, ty_env) => PtrMetadata::None,
412            ty::Str | ty::Slice(..) => PtrMetadata::Length,
413            ty::Dynamic(..) => match hax_ty.kind() {
414                hax::TyKind::Dynamic(dyn_binder, _) => {
415                    let vtable = self.translate_region_binder(
416                        span,
417                        &dyn_binder.predicates.predicates[0].0.kind,
418                        |ctx, kind: &hax::ClauseKind| {
419                            let hax::ClauseKind::Trait(trait_predicate) = kind else {
420                                unreachable!()
421                            };
422                            ctx.translate_vtable_struct_ref(span, &trait_predicate.trait_ref)
423                        },
424                    )?;
425                    let vtable = self.erase_region_binder(vtable);
426                    PtrMetadata::VTable(vtable)
427                }
428                _ => unreachable!("Unexpected hax type {hax_ty:?} for dynamic type: {ty:?}"),
429            },
430            ty::Param(..) => PtrMetadata::InheritFrom(self.translate_ty(span, &hax_ty)?),
431            ty::Placeholder(..) | ty::Infer(..) | ty::Bound(..) => {
432                panic!(
433                    "We should never encounter a placeholder, infer, or bound type from ptr_metadata translation. Got: {tail_ty:?}"
434                )
435            }
436            _ => PtrMetadata::None,
437        };
438
439        Ok(ret)
440    }
441
442    /// Translate a type layout.
443    ///
444    /// Translates the layout as queried from rustc into
445    /// the more restricted [`Layout`].
446    #[tracing::instrument(skip(self))]
447    pub fn translate_layout(&self, item: &hax::ItemRef) -> Option<Layout> {
448        use rustc_abi as r_abi;
449
450        fn translate_variant_layout(
451            variant_layout: &r_abi::LayoutData<r_abi::FieldIdx, r_abi::VariantIdx>,
452            tag: Option<ScalarValue>,
453        ) -> VariantLayout {
454            let field_offsets = match &variant_layout.fields {
455                r_abi::FieldsShape::Arbitrary { offsets, .. } => {
456                    offsets.iter().map(|o| o.bytes()).collect()
457                }
458                r_abi::FieldsShape::Primitive | r_abi::FieldsShape::Union(_) => IndexVec::default(),
459                r_abi::FieldsShape::Array { .. } => panic!("Unexpected layout shape"),
460            };
461            VariantLayout {
462                field_offsets,
463                uninhabited: variant_layout.is_uninhabited(),
464                tag,
465            }
466        }
467
468        fn translate_primitive_int(int_ty: r_abi::Integer, signed: bool) -> IntegerTy {
469            if signed {
470                IntegerTy::Signed(match int_ty {
471                    r_abi::Integer::I8 => IntTy::I8,
472                    r_abi::Integer::I16 => IntTy::I16,
473                    r_abi::Integer::I32 => IntTy::I32,
474                    r_abi::Integer::I64 => IntTy::I64,
475                    r_abi::Integer::I128 => IntTy::I128,
476                })
477            } else {
478                IntegerTy::Unsigned(match int_ty {
479                    r_abi::Integer::I8 => UIntTy::U8,
480                    r_abi::Integer::I16 => UIntTy::U16,
481                    r_abi::Integer::I32 => UIntTy::U32,
482                    r_abi::Integer::I64 => UIntTy::U64,
483                    r_abi::Integer::I128 => UIntTy::U128,
484                })
485            }
486        }
487
488        let tcx = self.t_ctx.tcx;
489        let rdefid = item.def_id.real_rust_def_id();
490        let hax_state = self.hax_state_with_id();
491        assert_eq!(hax_state.owner(), item.def_id);
492        let ty_env = hax_state.typing_env();
493        let ty = tcx
494            .type_of(rdefid)
495            .instantiate(tcx, item.rustc_args(hax_state));
496        let pseudo_input = ty_env.as_query_input(ty);
497
498        // If layout computation returns an error, we return `None`.
499        let layout = tcx.layout_of(pseudo_input).ok()?.layout;
500        let (size, align) = if layout.is_sized() {
501            (
502                Some(layout.size().bytes()),
503                Some(layout.align().abi.bytes()),
504            )
505        } else {
506            (None, None)
507        };
508
509        // Get the layout of the discriminant when there is one (even if it is encoded in a niche).
510        let discriminant_layout = match layout.variants() {
511            r_abi::Variants::Multiple {
512                tag,
513                tag_encoding,
514                tag_field,
515                ..
516            } => {
517                // The tag_field is the index into the `offsets` vector.
518                let r_abi::FieldsShape::Arbitrary { offsets, .. } = layout.fields() else {
519                    unreachable!()
520                };
521
522                let tag_ty = match tag.primitive() {
523                    r_abi::Primitive::Int(int_ty, signed) => {
524                        translate_primitive_int(int_ty, signed)
525                    }
526                    // Try to handle pointer as integers of the same size.
527                    r_abi::Primitive::Pointer(_) => IntegerTy::Signed(IntTy::Isize),
528                    r_abi::Primitive::Float(_) => {
529                        unreachable!()
530                    }
531                };
532
533                let encoding = match tag_encoding {
534                    r_abi::TagEncoding::Direct => TagEncoding::Direct,
535                    r_abi::TagEncoding::Niche {
536                        untagged_variant, ..
537                    } => TagEncoding::Niche {
538                        untagged_variant: VariantId::from_usize(r_abi::VariantIdx::as_usize(
539                            *untagged_variant,
540                        )),
541                    },
542                };
543                offsets.get(*tag_field).map(|s| DiscriminantLayout {
544                    offset: r_abi::Size::bytes(*s),
545                    tag_ty,
546                    encoding,
547                })
548            }
549            r_abi::Variants::Single { .. } | r_abi::Variants::Empty => None,
550        };
551
552        let mut variant_layouts: IndexVec<VariantId, VariantLayout> = IndexVec::new();
553
554        match layout.variants() {
555            r_abi::Variants::Multiple { variants, .. } => {
556                let tag_ty = discriminant_layout
557                    .as_ref()
558                    .expect("No discriminant layout for enum?")
559                    .tag_ty;
560                let ptr_size = self.t_ctx.translated.target_information.target_pointer_size;
561                let tag_size = r_abi::Size::from_bytes(tag_ty.target_size(ptr_size));
562
563                for (id, variant_layout) in variants.iter_enumerated() {
564                    let tag = if variant_layout.is_uninhabited() {
565                        None
566                    } else {
567                        tcx.tag_for_variant(ty_env.as_query_input((ty, id)))
568                            .map(|s| match tag_ty {
569                                IntegerTy::Signed(int_ty) => {
570                                    ScalarValue::from_int(ptr_size, int_ty, s.to_int(tag_size))
571                                        .unwrap()
572                                }
573                                IntegerTy::Unsigned(uint_ty) => {
574                                    ScalarValue::from_uint(ptr_size, uint_ty, s.to_uint(tag_size))
575                                        .unwrap()
576                                }
577                            })
578                    };
579                    variant_layouts.push(translate_variant_layout(variant_layout, tag));
580                }
581            }
582            r_abi::Variants::Single { index } => {
583                if let r_abi::FieldsShape::Arbitrary { .. } = layout.fields() {
584                    let n_variants = match ty.kind() {
585                        _ if let Some(range) = ty.variant_range(tcx) => range.end.index(),
586                        _ => 1,
587                    };
588                    // All the variants not initialized below are uninhabited.
589                    variant_layouts = (0..n_variants)
590                        .map(|_| VariantLayout {
591                            field_offsets: IndexVec::default(),
592                            uninhabited: true,
593                            tag: None,
594                        })
595                        .collect();
596                    variant_layouts[index.index()] = translate_variant_layout(&layout, None);
597                }
598            }
599            r_abi::Variants::Empty => {}
600        }
601
602        Some(Layout {
603            size,
604            align,
605            discriminant_layout,
606            uninhabited: layout.is_uninhabited(),
607            variant_layouts,
608        })
609    }
610
611    /// Generate a naive layout for this type.
612    pub fn generate_naive_layout(&self, span: Span, ty: &TypeDeclKind) -> Result<Layout, Error> {
613        match ty {
614            TypeDeclKind::Struct(fields) => {
615                let mut size = 0;
616                let mut align = 0;
617                let ptr_size = self.t_ctx.translated.target_information.target_pointer_size;
618                let field_offsets = fields.map_ref(|field| {
619                    let offset = size;
620                    let size_of_ty = match field.ty.kind() {
621                        TyKind::Literal(literal_ty) => literal_ty.target_size(ptr_size) as u64,
622                        // This is a lie, the pointers could be fat...
623                        TyKind::Ref(..) | TyKind::RawPtr(..) | TyKind::FnPtr(..) => ptr_size,
624                        _ => panic!("Unsupported type for `generate_naive_layout`: {ty:?}"),
625                    };
626                    size += size_of_ty;
627                    // For these types, align == size is good enough.
628                    align = std::cmp::max(align, size);
629                    offset
630                });
631
632                Ok(Layout {
633                    size: Some(size),
634                    align: Some(align),
635                    discriminant_layout: None,
636                    uninhabited: false,
637                    variant_layouts: IndexVec::from_array([VariantLayout {
638                        field_offsets,
639                        tag: None,
640                        uninhabited: false,
641                    }]),
642                })
643            }
644            _ => raise_error!(
645                self,
646                span,
647                "`generate_naive_layout` only supports structs at the moment"
648            ),
649        }
650    }
651
652    /// Translate the body of a type declaration.
653    ///
654    /// Note that the type may be external, in which case we translate the body
655    /// only if it is public (i.e., it is a public enumeration, or it is a
656    /// struct with only public fields).
657    pub(crate) fn translate_adt_def(
658        &mut self,
659        trans_id: TypeDeclId,
660        def_span: Span,
661        item_meta: &ItemMeta,
662        def: &hax::FullDef,
663    ) -> Result<TypeDeclKind, Error> {
664        use hax::AdtKind;
665        let hax::FullDefKind::Adt {
666            adt_kind, variants, ..
667        } = def.kind()
668        else {
669            unreachable!()
670        };
671
672        if item_meta.opacity.is_opaque() {
673            return Ok(TypeDeclKind::Opaque);
674        }
675
676        trace!("{}", trans_id);
677
678        // In case the type is external, check if we should consider the type as
679        // transparent (i.e., extract its body). If it is an enumeration, then yes
680        // (because the variants of public enumerations are public, together with their
681        // fields). If it is a structure, we check if all the fields are public.
682        let contents_are_public = match adt_kind {
683            AdtKind::Enum => true,
684            AdtKind::Struct | AdtKind::Union => {
685                // Check the unique variant
686                error_assert!(self, def_span, variants.len() == 1);
687                variants[hax::VariantIdx::from(0usize)]
688                    .fields
689                    .iter()
690                    .all(|f| matches!(f.vis, Visibility::Public))
691            }
692            // The rest are fake adt kinds that won't reach here.
693            _ => unreachable!(),
694        };
695
696        if item_meta
697            .opacity
698            .with_content_visibility(contents_are_public)
699            .is_opaque()
700        {
701            return Ok(TypeDeclKind::Opaque);
702        }
703
704        // The type is transparent: explore the variants
705        let mut translated_variants: IndexVec<VariantId, Variant> = Default::default();
706        for (i, var_def) in variants.iter().enumerate() {
707            trace!("variant {i}: {var_def:?}");
708
709            let mut fields: IndexVec<FieldId, Field> = Default::default();
710            /* This is for sanity: check that either all the fields have names, or
711             * none of them has */
712            let mut have_names: Option<bool> = None;
713            for (j, field_def) in var_def.fields.iter().enumerate() {
714                trace!("variant {i}: field {j}: {field_def:?}");
715                let field_span = self.t_ctx.translate_span(&field_def.span);
716                // Translate the field type
717                let ty = self.translate_ty(field_span, &field_def.ty)?;
718                let field_full_def =
719                    self.hax_def(&def.this().with_def_id(self.hax_state(), &field_def.did))?;
720                let field_attrs = self.t_ctx.translate_attr_info(&field_full_def);
721
722                // Retrieve the field name.
723                let field_name = field_def.name.map(|s| s.to_string());
724                // Sanity check
725                match &have_names {
726                    None => {
727                        have_names = match &field_name {
728                            None => Some(false),
729                            Some(_) => Some(true),
730                        }
731                    }
732                    Some(b) => {
733                        error_assert!(self, field_span, *b == field_name.is_some());
734                    }
735                };
736
737                // Store the field
738                let field = Field {
739                    span: field_span,
740                    attr_info: field_attrs,
741                    name: field_name,
742                    ty,
743                };
744                fields.push(field);
745            }
746
747            let discriminant = self.translate_discriminant(def_span, &var_def.discr_val)?;
748            let variant_span = self.t_ctx.translate_span(&var_def.span);
749            let variant_name = var_def.name.to_string();
750            let variant_full_def =
751                self.hax_def(&def.this().with_def_id(self.hax_state(), &var_def.def_id))?;
752            let variant_attrs = self.t_ctx.translate_attr_info(&variant_full_def);
753
754            let mut variant = Variant {
755                span: variant_span,
756                attr_info: variant_attrs,
757                name: variant_name,
758                fields,
759                discriminant,
760            };
761            // Propagate a `#[charon::variants_prefix(..)]` or `#[charon::variants_suffix(..)]` attribute to the variants.
762            if variant.attr_info.rename.is_none() {
763                let prefix = item_meta
764                    .attr_info
765                    .attributes
766                    .iter()
767                    .filter_map(|a| a.as_variants_prefix())
768                    .next()
769                    .map(|attr| attr.as_str());
770                let suffix = item_meta
771                    .attr_info
772                    .attributes
773                    .iter()
774                    .filter_map(|a| a.as_variants_suffix())
775                    .next()
776                    .map(|attr| attr.as_str());
777                if prefix.is_some() || suffix.is_some() {
778                    let prefix = prefix.unwrap_or_default();
779                    let suffix = suffix.unwrap_or_default();
780                    let name = &variant.name;
781                    variant.attr_info.rename = Some(format!("{prefix}{name}{suffix}"));
782                }
783            }
784            translated_variants.push(variant);
785        }
786
787        // Register the type
788        let type_def_kind: TypeDeclKind = match adt_kind {
789            AdtKind::Struct => TypeDeclKind::Struct(translated_variants[0].fields.clone()),
790            AdtKind::Enum => TypeDeclKind::Enum(translated_variants),
791            AdtKind::Union => TypeDeclKind::Union(translated_variants[0].fields.clone()),
792            // The rest are fake adt kinds that won't reach here.
793            _ => unreachable!(),
794        };
795
796        Ok(type_def_kind)
797    }
798
799    fn translate_discriminant(
800        &mut self,
801        def_span: Span,
802        discr: &hax::DiscriminantValue,
803    ) -> Result<Literal, Error> {
804        let ty = self.translate_ty(def_span, &discr.ty)?;
805        let lit_ty = ty.kind().as_literal().unwrap();
806        match Literal::from_bits(lit_ty, discr.val) {
807            Some(lit) => Ok(lit),
808            None => raise_error!(self, def_span, "unexpected discriminant type: {ty:?}",),
809        }
810    }
811
812    pub fn translate_repr_options(&mut self, hax_repr_options: &hax::ReprOptions) -> ReprOptions {
813        let repr_algo = if hax_repr_options.flags.is_c {
814            ReprAlgorithm::C
815        } else {
816            ReprAlgorithm::Rust
817        };
818
819        let align_mod = if let Some(align) = &hax_repr_options.align {
820            Some(AlignmentModifier::Align(align.bytes))
821        } else if let Some(pack) = &hax_repr_options.pack {
822            Some(AlignmentModifier::Pack(pack.bytes))
823        } else {
824            None
825        };
826
827        ReprOptions {
828            transparent: hax_repr_options.flags.is_transparent,
829            explicit_discr_type: hax_repr_options.int_specified,
830            repr_algo,
831            align_modif: align_mod,
832        }
833    }
834}