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                            Ok(ctx
423                                .translate_vtable_struct_ref(span, &trait_predicate.trait_ref)?
424                                .unwrap())
425                        },
426                    )?;
427                    let vtable = self.erase_region_binder(vtable);
428                    PtrMetadata::VTable(vtable)
429                }
430                _ => unreachable!("Unexpected hax type {hax_ty:?} for dynamic type: {ty:?}"),
431            },
432            ty::Param(..) => PtrMetadata::InheritFrom(self.translate_ty(span, &hax_ty)?),
433            ty::Placeholder(..) | ty::Infer(..) | ty::Bound(..) => {
434                panic!(
435                    "We should never encounter a placeholder, infer, or bound type from ptr_metadata translation. Got: {tail_ty:?}"
436                )
437            }
438            _ => PtrMetadata::None,
439        };
440
441        Ok(ret)
442    }
443
444    /// Translate a type layout.
445    ///
446    /// Translates the layout as queried from rustc into
447    /// the more restricted [`Layout`].
448    #[tracing::instrument(skip(self))]
449    pub fn translate_layout(&self, item: &hax::ItemRef) -> Option<Layout> {
450        use rustc_abi as r_abi;
451
452        fn translate_variant_layout(
453            variant_layout: &r_abi::LayoutData<r_abi::FieldIdx, r_abi::VariantIdx>,
454            tag: Option<ScalarValue>,
455        ) -> VariantLayout {
456            let field_offsets = match &variant_layout.fields {
457                r_abi::FieldsShape::Arbitrary { offsets, .. } => {
458                    offsets.iter().map(|o| o.bytes()).collect()
459                }
460                r_abi::FieldsShape::Primitive | r_abi::FieldsShape::Union(_) => IndexVec::default(),
461                r_abi::FieldsShape::Array { .. } => panic!("Unexpected layout shape"),
462            };
463            VariantLayout {
464                field_offsets,
465                uninhabited: variant_layout.is_uninhabited(),
466                tag,
467            }
468        }
469
470        fn translate_primitive_int(int_ty: r_abi::Integer, signed: bool) -> IntegerTy {
471            if signed {
472                IntegerTy::Signed(match int_ty {
473                    r_abi::Integer::I8 => IntTy::I8,
474                    r_abi::Integer::I16 => IntTy::I16,
475                    r_abi::Integer::I32 => IntTy::I32,
476                    r_abi::Integer::I64 => IntTy::I64,
477                    r_abi::Integer::I128 => IntTy::I128,
478                })
479            } else {
480                IntegerTy::Unsigned(match int_ty {
481                    r_abi::Integer::I8 => UIntTy::U8,
482                    r_abi::Integer::I16 => UIntTy::U16,
483                    r_abi::Integer::I32 => UIntTy::U32,
484                    r_abi::Integer::I64 => UIntTy::U64,
485                    r_abi::Integer::I128 => UIntTy::U128,
486                })
487            }
488        }
489
490        let tcx = self.t_ctx.tcx;
491        let rdefid = item.def_id.real_rust_def_id();
492        let hax_state = self.hax_state_with_id();
493        assert_eq!(hax_state.owner(), item.def_id);
494        let ty_env = hax_state.typing_env();
495        let ty = tcx
496            .type_of(rdefid)
497            .instantiate(tcx, item.rustc_args(hax_state));
498        let pseudo_input = ty_env.as_query_input(ty);
499
500        // If layout computation returns an error, we return `None`.
501        let layout = tcx.layout_of(pseudo_input).ok()?.layout;
502        let (size, align) = if layout.is_sized() {
503            (
504                Some(layout.size().bytes()),
505                Some(layout.align().abi.bytes()),
506            )
507        } else {
508            (None, None)
509        };
510
511        // Get the layout of the discriminant when there is one (even if it is encoded in a niche).
512        let discriminant_layout = match layout.variants() {
513            r_abi::Variants::Multiple {
514                tag,
515                tag_encoding,
516                tag_field,
517                ..
518            } => {
519                // The tag_field is the index into the `offsets` vector.
520                let r_abi::FieldsShape::Arbitrary { offsets, .. } = layout.fields() else {
521                    unreachable!()
522                };
523
524                let tag_ty = match tag.primitive() {
525                    r_abi::Primitive::Int(int_ty, signed) => {
526                        translate_primitive_int(int_ty, signed)
527                    }
528                    // Try to handle pointer as integers of the same size.
529                    r_abi::Primitive::Pointer(_) => IntegerTy::Signed(IntTy::Isize),
530                    r_abi::Primitive::Float(_) => {
531                        unreachable!()
532                    }
533                };
534
535                let encoding = match tag_encoding {
536                    r_abi::TagEncoding::Direct => TagEncoding::Direct,
537                    r_abi::TagEncoding::Niche {
538                        untagged_variant, ..
539                    } => TagEncoding::Niche {
540                        untagged_variant: VariantId::from_usize(r_abi::VariantIdx::as_usize(
541                            *untagged_variant,
542                        )),
543                    },
544                };
545                offsets.get(*tag_field).map(|s| DiscriminantLayout {
546                    offset: r_abi::Size::bytes(*s),
547                    tag_ty,
548                    encoding,
549                })
550            }
551            r_abi::Variants::Single { .. } | r_abi::Variants::Empty => None,
552        };
553
554        let mut variant_layouts: IndexVec<VariantId, VariantLayout> = IndexVec::new();
555
556        match layout.variants() {
557            r_abi::Variants::Multiple { variants, .. } => {
558                let tag_ty = discriminant_layout
559                    .as_ref()
560                    .expect("No discriminant layout for enum?")
561                    .tag_ty;
562                let ptr_size = self.t_ctx.translated.target_information.target_pointer_size;
563                let tag_size = r_abi::Size::from_bytes(tag_ty.target_size(ptr_size));
564
565                for (id, variant_layout) in variants.iter_enumerated() {
566                    let tag = if variant_layout.is_uninhabited() {
567                        None
568                    } else {
569                        tcx.tag_for_variant(ty_env.as_query_input((ty, id)))
570                            .map(|s| match tag_ty {
571                                IntegerTy::Signed(int_ty) => {
572                                    ScalarValue::from_int(ptr_size, int_ty, s.to_int(tag_size))
573                                        .unwrap()
574                                }
575                                IntegerTy::Unsigned(uint_ty) => {
576                                    ScalarValue::from_uint(ptr_size, uint_ty, s.to_uint(tag_size))
577                                        .unwrap()
578                                }
579                            })
580                    };
581                    variant_layouts.push(translate_variant_layout(variant_layout, tag));
582                }
583            }
584            r_abi::Variants::Single { index } => {
585                if let r_abi::FieldsShape::Arbitrary { .. } = layout.fields() {
586                    let n_variants = match ty.kind() {
587                        _ if let Some(range) = ty.variant_range(tcx) => range.end.index(),
588                        _ => 1,
589                    };
590                    // All the variants not initialized below are uninhabited.
591                    variant_layouts = (0..n_variants)
592                        .map(|_| VariantLayout {
593                            field_offsets: IndexVec::default(),
594                            uninhabited: true,
595                            tag: None,
596                        })
597                        .collect();
598                    variant_layouts[index.index()] = translate_variant_layout(&layout, None);
599                }
600            }
601            r_abi::Variants::Empty => {}
602        }
603
604        Some(Layout {
605            size,
606            align,
607            discriminant_layout,
608            uninhabited: layout.is_uninhabited(),
609            variant_layouts,
610        })
611    }
612
613    /// Generate a naive layout for this type.
614    pub fn generate_naive_layout(&self, span: Span, ty: &TypeDeclKind) -> Result<Layout, Error> {
615        match ty {
616            TypeDeclKind::Struct(fields) => {
617                let mut size = 0;
618                let mut align = 0;
619                let ptr_size = self.t_ctx.translated.target_information.target_pointer_size;
620                let field_offsets = fields.map_ref(|field| {
621                    let offset = size;
622                    let size_of_ty = match field.ty.kind() {
623                        TyKind::Literal(literal_ty) => literal_ty.target_size(ptr_size) as u64,
624                        // This is a lie, the pointers could be fat...
625                        TyKind::Ref(..) | TyKind::RawPtr(..) | TyKind::FnPtr(..) => ptr_size,
626                        _ => panic!("Unsupported type for `generate_naive_layout`: {ty:?}"),
627                    };
628                    size += size_of_ty;
629                    // For these types, align == size is good enough.
630                    align = std::cmp::max(align, size);
631                    offset
632                });
633
634                Ok(Layout {
635                    size: Some(size),
636                    align: Some(align),
637                    discriminant_layout: None,
638                    uninhabited: false,
639                    variant_layouts: IndexVec::from_array([VariantLayout {
640                        field_offsets,
641                        tag: None,
642                        uninhabited: false,
643                    }]),
644                })
645            }
646            _ => raise_error!(
647                self,
648                span,
649                "`generate_naive_layout` only supports structs at the moment"
650            ),
651        }
652    }
653
654    /// Translate the body of a type declaration.
655    ///
656    /// Note that the type may be external, in which case we translate the body
657    /// only if it is public (i.e., it is a public enumeration, or it is a
658    /// struct with only public fields).
659    pub(crate) fn translate_adt_def(
660        &mut self,
661        trans_id: TypeDeclId,
662        def_span: Span,
663        item_meta: &ItemMeta,
664        def: &hax::FullDef,
665    ) -> Result<TypeDeclKind, Error> {
666        use hax::AdtKind;
667        let hax::FullDefKind::Adt {
668            adt_kind, variants, ..
669        } = def.kind()
670        else {
671            unreachable!()
672        };
673
674        if item_meta.opacity.is_opaque() {
675            return Ok(TypeDeclKind::Opaque);
676        }
677
678        trace!("{}", trans_id);
679
680        // In case the type is external, check if we should consider the type as
681        // transparent (i.e., extract its body). If it is an enumeration, then yes
682        // (because the variants of public enumerations are public, together with their
683        // fields). If it is a structure, we check if all the fields are public.
684        let contents_are_public = match adt_kind {
685            AdtKind::Enum => true,
686            AdtKind::Struct | AdtKind::Union => {
687                // Check the unique variant
688                error_assert!(self, def_span, variants.len() == 1);
689                variants[hax::VariantIdx::from(0usize)]
690                    .fields
691                    .iter()
692                    .all(|f| matches!(f.vis, Visibility::Public))
693            }
694            // The rest are fake adt kinds that won't reach here.
695            _ => unreachable!(),
696        };
697
698        if item_meta
699            .opacity
700            .with_content_visibility(contents_are_public)
701            .is_opaque()
702        {
703            return Ok(TypeDeclKind::Opaque);
704        }
705
706        // The type is transparent: explore the variants
707        let mut translated_variants: IndexVec<VariantId, Variant> = Default::default();
708        for (i, var_def) in variants.iter().enumerate() {
709            trace!("variant {i}: {var_def:?}");
710
711            let mut fields: IndexVec<FieldId, Field> = Default::default();
712            /* This is for sanity: check that either all the fields have names, or
713             * none of them has */
714            let mut have_names: Option<bool> = None;
715            for (j, field_def) in var_def.fields.iter().enumerate() {
716                trace!("variant {i}: field {j}: {field_def:?}");
717                let field_span = self.t_ctx.translate_span(&field_def.span);
718                // Translate the field type
719                let ty = self.translate_ty(field_span, &field_def.ty)?;
720                let field_full_def =
721                    self.hax_def(&def.this().with_def_id(self.hax_state(), &field_def.did))?;
722                let field_attrs = self.t_ctx.translate_attr_info(&field_full_def);
723
724                // Retrieve the field name.
725                let field_name = field_def.name.map(|s| s.to_string());
726                // Sanity check
727                match &have_names {
728                    None => {
729                        have_names = match &field_name {
730                            None => Some(false),
731                            Some(_) => Some(true),
732                        }
733                    }
734                    Some(b) => {
735                        error_assert!(self, field_span, *b == field_name.is_some());
736                    }
737                };
738
739                // Store the field
740                let field = Field {
741                    span: field_span,
742                    attr_info: field_attrs,
743                    name: field_name,
744                    ty,
745                };
746                fields.push(field);
747            }
748
749            let discriminant = self.translate_discriminant(def_span, &var_def.discr_val)?;
750            let variant_span = self.t_ctx.translate_span(&var_def.span);
751            let variant_name = var_def.name.to_string();
752            let variant_full_def =
753                self.hax_def(&def.this().with_def_id(self.hax_state(), &var_def.def_id))?;
754            let variant_attrs = self.t_ctx.translate_attr_info(&variant_full_def);
755
756            let mut variant = Variant {
757                span: variant_span,
758                attr_info: variant_attrs,
759                name: variant_name,
760                fields,
761                discriminant,
762            };
763            // Propagate a `#[charon::variants_prefix(..)]` or `#[charon::variants_suffix(..)]` attribute to the variants.
764            if variant.attr_info.rename.is_none() {
765                let prefix = item_meta
766                    .attr_info
767                    .attributes
768                    .iter()
769                    .filter_map(|a| a.as_variants_prefix())
770                    .next()
771                    .map(|attr| attr.as_str());
772                let suffix = item_meta
773                    .attr_info
774                    .attributes
775                    .iter()
776                    .filter_map(|a| a.as_variants_suffix())
777                    .next()
778                    .map(|attr| attr.as_str());
779                if prefix.is_some() || suffix.is_some() {
780                    let prefix = prefix.unwrap_or_default();
781                    let suffix = suffix.unwrap_or_default();
782                    let name = &variant.name;
783                    variant.attr_info.rename = Some(format!("{prefix}{name}{suffix}"));
784                }
785            }
786            translated_variants.push(variant);
787        }
788
789        // Register the type
790        let type_def_kind: TypeDeclKind = match adt_kind {
791            AdtKind::Struct => TypeDeclKind::Struct(translated_variants[0].fields.clone()),
792            AdtKind::Enum => TypeDeclKind::Enum(translated_variants),
793            AdtKind::Union => TypeDeclKind::Union(translated_variants[0].fields.clone()),
794            // The rest are fake adt kinds that won't reach here.
795            _ => unreachable!(),
796        };
797
798        Ok(type_def_kind)
799    }
800
801    fn translate_discriminant(
802        &mut self,
803        def_span: Span,
804        discr: &hax::DiscriminantValue,
805    ) -> Result<Literal, Error> {
806        let ty = self.translate_ty(def_span, &discr.ty)?;
807        let lit_ty = ty.kind().as_literal().unwrap();
808        match Literal::from_bits(lit_ty, discr.val) {
809            Some(lit) => Ok(lit),
810            None => raise_error!(self, def_span, "unexpected discriminant type: {ty:?}",),
811        }
812    }
813
814    pub fn translate_repr_options(&mut self, hax_repr_options: &hax::ReprOptions) -> ReprOptions {
815        let repr_algo = if hax_repr_options.flags.is_c {
816            ReprAlgorithm::C
817        } else {
818            ReprAlgorithm::Rust
819        };
820
821        let align_mod = if let Some(align) = &hax_repr_options.align {
822            Some(AlignmentModifier::Align(align.bytes))
823        } else if let Some(pack) = &hax_repr_options.pack {
824            Some(AlignmentModifier::Pack(pack.bytes))
825        } else {
826            None
827        };
828
829        ReprOptions {
830            transparent: hax_repr_options.flags.is_transparent,
831            explicit_discr_type: hax_repr_options.int_specified,
832            repr_algo,
833            align_modif: align_mod,
834        }
835    }
836}