1use std::fmt;
2use std::ops::{Deref, Range};
34use rustc_data_structures::intern::Interned;
5use rustc_data_structures::range_set::RangeSet;
6use rustc_macros::StableHash;
78use crate::layout::{FieldIdx, VariantIdx};
9use crate::{
10Align, BackendRepr, FieldsShape, Float, HasDataLayout, LayoutData, Niche, Numeric, PointeeInfo,
11Primitive, Size, Variants,
12};
1314// Explicitly import `Float` to avoid ambiguity with `Primitive::Float`.
1516#[derive(#[automatically_derived]impl<'a> ::core::marker::Copy for Layout<'a> { }Copy, #[automatically_derived]#[doc(hidden)]unsafe impl<'a>
::core::clone::TrivialClone for Layout<'a> {
}
#[automatically_derived]impl<'a> ::core::clone::Clone for Layout<'a> {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
let _:
::core::clone::AssertParamIsClone<Interned<'a,
LayoutData<FieldIdx, VariantIdx>>>;
*self
}
}Clone, #[automatically_derived]impl<'a> ::core::marker::StructuralPartialEq for
Layout<'a> {
}
#[automatically_derived]impl<'a> ::core::cmp::PartialEq for Layout<'a> {
#[inline]
fn eq(&self, other: &Self) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]impl<'a> ::core::cmp::Eq for Layout<'a> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _:
::core::cmp::AssertParamIsEq<Interned<'a,
LayoutData<FieldIdx, VariantIdx>>>;
}
}Eq, #[automatically_derived]impl<'a> ::core::hash::Hash for Layout<'a> {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.0, state)
}
}Hash, const _: () =
{
impl<'a> ::rustc_data_structures::stable_hash::StableHash for
Layout<'a> {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
Layout(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash)]
17#[rustc_pass_by_value]
18pub struct Layout<'a>(pub Interned<'a, LayoutData<FieldIdx, VariantIdx>>);
1920impl<'a> fmt::Debugfor Layout<'a> {
21fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22// See comment on `<LayoutData as Debug>::fmt` above.
23self.0.0.fmt(f)
24 }
25}
2627impl<'a> Dereffor Layout<'a> {
28type Target = &'a LayoutData<FieldIdx, VariantIdx>;
29fn deref(&self) -> &&'a LayoutData<FieldIdx, VariantIdx> {
30&self.0.0
31}
32}
3334impl<'a> Layout<'a> {
35pub fn fields(self) -> &'a FieldsShape<FieldIdx> {
36&self.0.0.fields
37 }
3839pub fn variants(self) -> &'a Variants<FieldIdx, VariantIdx> {
40&self.0.0.variants
41 }
4243pub fn backend_repr(self) -> BackendRepr {
44self.0.0.backend_repr
45 }
4647pub fn largest_niche(self) -> Option<Niche> {
48self.0.0.largest_niche
49 }
5051pub fn align(self) -> Align {
52self.0.0.align
53 }
5455pub fn size(self) -> Size {
56self.0.0.size
57 }
5859pub fn max_repr_align(self) -> Option<Align> {
60self.0.0.max_repr_align
61 }
6263pub fn unadjusted_abi_align(self) -> Align {
64self.0.0.unadjusted_abi_align
65 }
66}
6768/// The layout of a type, alongside the type itself.
69/// Provides various type traversal APIs (e.g., recursing into fields).
70///
71/// Note that the layout is NOT guaranteed to always be identical
72/// to that obtained from `layout_of(ty)`, as we need to produce
73/// layouts for which Rust types do not exist, such as enum variants
74/// or synthetic fields of enums (i.e., discriminants) and wide pointers.
75#[derive(#[automatically_derived]impl<'a, Ty: ::core::marker::Copy>
::core::marker::Copy for TyAndLayout<'a, Ty> {
}Copy, #[automatically_derived]impl<'a, Ty: ::core::clone::Clone>
::core::clone::Clone for TyAndLayout<'a, Ty> {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
TyAndLayout {
ty: ::core::clone::Clone::clone(&self.ty),
layout: ::core::clone::Clone::clone(&self.layout),
}
}
}Clone, #[automatically_derived]impl<'a, Ty: ::core::cmp::PartialEq>
::core::marker::StructuralPartialEq for TyAndLayout<'a, Ty> {
}
#[automatically_derived]impl<'a, Ty: ::core::cmp::PartialEq>
::core::cmp::PartialEq for TyAndLayout<'a, Ty> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.ty == other.ty && self.layout == other.layout
}
}PartialEq, #[automatically_derived]impl<'a, Ty: ::core::cmp::Eq> ::core::cmp::Eq for
TyAndLayout<'a, Ty> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<Ty>;
let _: ::core::cmp::AssertParamIsEq<Layout<'a>>;
}
}Eq, #[automatically_derived]impl<'a, Ty: ::core::hash::Hash> ::core::hash::Hash
for TyAndLayout<'a, Ty> {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.ty, state);
::core::hash::Hash::hash(&self.layout, state)
}
}Hash, const _: () =
{
impl<'a, Ty> ::rustc_data_structures::stable_hash::StableHash for
TyAndLayout<'a, Ty> where
Ty: ::rustc_data_structures::stable_hash::StableHash {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
TyAndLayout { ty: ref __binding_0, layout: ref __binding_1 }
=> {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash)]
76pub struct TyAndLayout<'a, Ty> {
77pub ty: Ty,
78pub layout: Layout<'a>,
79}
8081impl<'a, Ty: fmt::Display> fmt::Debugfor TyAndLayout<'a, Ty> {
82fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83// Print the type in a readable way, not its debug representation.
84f.debug_struct("TyAndLayout")
85 .field("ty", &format_args!("{0}", self.ty)format_args!("{}", self.ty))
86 .field("layout", &self.layout)
87 .finish()
88 }
89}
9091impl<'a, Ty> Dereffor TyAndLayout<'a, Ty> {
92type Target = &'a LayoutData<FieldIdx, VariantIdx>;
93fn deref(&self) -> &&'a LayoutData<FieldIdx, VariantIdx> {
94&self.layout.0.0
95}
96}
9798impl<'a, Ty> AsRef<LayoutData<FieldIdx, VariantIdx>> for TyAndLayout<'a, Ty> {
99fn as_ref(&self) -> &LayoutData<FieldIdx, VariantIdx> {
100&*self.layout.0.0
101}
102}
103104/// Trait that needs to be implemented by the higher-level type representation
105/// (e.g. `rustc_middle::ty::Ty`), to provide `rustc_target::abi` functionality.
106pub trait TyAbiInterface<'a, C>: Sized + std::fmt::Debug + std::fmt::Display {
107fn ty_and_layout_for_variant(
108 this: TyAndLayout<'a, Self>,
109 cx: &C,
110 variant_index: VariantIdx,
111 ) -> TyAndLayout<'a, Self>;
112fn ty_and_layout_field(this: TyAndLayout<'a, Self>, cx: &C, i: usize) -> TyAndLayout<'a, Self>;
113fn ty_and_layout_pointee_info_at(
114 this: TyAndLayout<'a, Self>,
115 cx: &C,
116 offset: Size,
117 ) -> Option<PointeeInfo>;
118fn is_adt(this: TyAndLayout<'a, Self>) -> bool;
119fn is_enum(this: TyAndLayout<'a, Self>) -> bool;
120fn is_never(this: TyAndLayout<'a, Self>) -> bool;
121fn is_tuple(this: TyAndLayout<'a, Self>) -> bool;
122fn is_unit(this: TyAndLayout<'a, Self>) -> bool;
123fn is_transparent(this: TyAndLayout<'a, Self>) -> bool;
124fn is_complex_number_lang_item(this: TyAndLayout<'a, Self>, cx: &C) -> bool;
125fn is_x87_f80_lang_item(this: TyAndLayout<'a, Self>, cx: &C) -> bool;
126fn is_scalable_vector(this: TyAndLayout<'a, Self>) -> bool;
127/// See [`TyAndLayout::pass_indirectly_in_non_rustic_abis`] for details.
128fn is_pass_indirectly_in_non_rustic_abis_flag_set(this: TyAndLayout<'a, Self>) -> bool;
129}
130131impl<'a, Ty> TyAndLayout<'a, Ty> {
132/// Synthetize a layout representing the variant-specific fields of an enum-like layout.
133 ///
134 /// Note that the resulting layout *does not* fully describes `self.ty` at that specific
135 /// variant: prefix fields (e.g. in coroutines) and tag information are lost.
136 ///
137 /// If you don't need type information about the variant's fields, prefer using
138 /// `self.layout.variants` directly.
139pub fn for_variant<C>(self, cx: &C, variant_index: VariantIdx) -> Self
140where
141Ty: TyAbiInterface<'a, C>,
142 {
143 Ty::ty_and_layout_for_variant(self, cx, variant_index)
144 }
145146pub fn field<C>(self, cx: &C, i: usize) -> Self
147where
148Ty: TyAbiInterface<'a, C>,
149 {
150 Ty::ty_and_layout_field(self, cx, i)
151 }
152153pub fn pointee_info_at<C>(self, cx: &C, offset: Size) -> Option<PointeeInfo>
154where
155Ty: TyAbiInterface<'a, C>,
156 {
157 Ty::ty_and_layout_pointee_info_at(self, cx, offset)
158 }
159160pub fn is_single_vector_element<C>(self, cx: &C, expected_size: Size) -> bool161where
162Ty: TyAbiInterface<'a, C>,
163 C: HasDataLayout,
164 {
165match self.backend_repr {
166 BackendRepr::SimdVector { .. } => self.size == expected_size,
167 BackendRepr::Memory { .. } => {
168if self.fields.count() == 1 && self.fields.offset(0).bytes() == 0 {
169self.field(cx, 0).is_single_vector_element(cx, expected_size)
170 } else {
171false
172}
173 }
174_ => false,
175 }
176 }
177178pub fn is_adt<C>(self) -> bool179where
180Ty: TyAbiInterface<'a, C>,
181 {
182 Ty::is_adt(self)
183 }
184185pub fn is_enum<C>(self) -> bool186where
187Ty: TyAbiInterface<'a, C>,
188 {
189 Ty::is_enum(self)
190 }
191192pub fn is_never<C>(self) -> bool193where
194Ty: TyAbiInterface<'a, C>,
195 {
196 Ty::is_never(self)
197 }
198199pub fn is_tuple<C>(self) -> bool200where
201Ty: TyAbiInterface<'a, C>,
202 {
203 Ty::is_tuple(self)
204 }
205206pub fn is_unit<C>(self) -> bool207where
208Ty: TyAbiInterface<'a, C>,
209 {
210 Ty::is_unit(self)
211 }
212213pub fn is_transparent<C>(self) -> bool214where
215Ty: TyAbiInterface<'a, C>,
216 {
217 Ty::is_transparent(self)
218 }
219220/// Returns `true` if this type needs to match the ABI of the C `_Complex` type. See
221 /// [`TyAndLayout::complex_number`] for details.
222pub fn is_complex_number<C>(self, cx: &C) -> bool223where
224Ty: TyAbiInterface<'a, C> + Copy,
225 {
226self.complex_number(cx).is_some()
227 }
228229pub fn is_scalable_vector<C>(self) -> bool230where
231Ty: TyAbiInterface<'a, C>,
232 {
233 Ty::is_scalable_vector(self)
234 }
235236/// Returns `true` if this type is the `x87_f80` lang item.
237 ///
238 /// Note that this function returns `false` for the `x87_f80_raw` lang item.
239pub fn is_x87_f80<C>(self, cx: &C) -> bool240where
241Ty: TyAbiInterface<'a, C> + Copy,
242 {
243// A 1-ZST is never x87_f80.
244Ty::is_x87_f80_lang_item(self.peel_transparent_wrappers_from_non_1zst(cx), cx)
245 }
246247/// If this method returns `true`, then this type should always have a `PassMode` of
248 /// `Indirect { mode: IndirectMode::Pointer, .. }` when being used as the argument type of a
249 /// function with a non-Rustic ABI (this is true for structs annotated with the
250 /// `#[rustc_pass_indirectly_in_non_rustic_abis]` attribute).
251 ///
252 /// This is used to replicate some of the behaviour of C array-to-pointer decay; however unlike
253 /// C any changes the caller makes to the passed value will not be reflected in the callee, so
254 /// the attribute is only useful for types where observing the value in the caller after the
255 /// function call isn't allowed (a.k.a. `va_list`).
256 ///
257 /// This function handles transparent types automatically.
258pub fn pass_indirectly_in_non_rustic_abis<C>(self, cx: &C) -> bool259where
260Ty: TyAbiInterface<'a, C> + Copy,
261 {
262// FIXME: Peeling the wrappers above would not work correctly if we are a 1-ZST. So we make
263 // `#[rustc_pass_indirectly_in_non_rustic_abis]` a NOP on 1-ZST. In the future,
264 // `non_1zst_field` should become `non_trivial_abi_field` and
265 // `#[rustc_pass_indirectly_in_non_rustic_abis]` should make a type have non-trivial ABI.
266if self.is_1zst() {
267return false;
268 }
269270let base = self.peel_transparent_wrappers_from_non_1zst(cx);
271 Ty::is_pass_indirectly_in_non_rustic_abis_flag_set(base)
272 }
273274/// Recursively peel away transparent wrappers, returning the inner value.
275 /// Will not peel anything if `self` is a 1-ZST! Callers need to either check
276 /// that the result is not a 1-ZST, or have separate logic for that.
277 ///
278 /// The return value is not `repr(transparent)` and/or does
279 /// not have a non-1zst field.
280pub fn peel_transparent_wrappers_from_non_1zst<C>(mut self, cx: &C) -> Self
281where
282Ty: TyAbiInterface<'a, C> + Copy,
283 {
284while self.is_transparent()
285 && let Some((_, field)) = self.non_1zst_field(cx)
286 {
287self = field;
288 }
289290self291 }
292293/// Finds the one field that is not a 1-ZST.
294 /// Returns `None` if there are multiple non-1-ZST fields or only 1-ZST-fields.
295pub fn non_1zst_field<C>(&self, cx: &C) -> Option<(FieldIdx, Self)>
296where
297Ty: TyAbiInterface<'a, C> + Copy,
298 {
299let mut found = None;
300for field_idx in 0..self.fields.count() {
301let field = self.field(cx, field_idx);
302if field.is_1zst() {
303continue;
304 }
305if found.is_some() {
306// More than one non-1-ZST field.
307return None;
308 }
309 found = Some((FieldIdx::from_usize(field_idx), field));
310 }
311found312 }
313314/// Finds the one field that is not a ZST.
315 /// Returns `None` if there are multiple non-ZST fields or only ZST-fields.
316 ///
317 /// Note that this function checks for ZSTs, not just 1-ZSTs.
318pub fn non_zst_field_ignore_alignment<C>(&self, cx: &C) -> Option<(FieldIdx, Self)>
319where
320Ty: TyAbiInterface<'a, C> + Copy,
321 {
322let mut found = None;
323for field_idx in 0..self.fields.count() {
324let field = self.field(cx, field_idx);
325if field.is_zst() {
326continue;
327 }
328if found.is_some() {
329// More than one non-ZST field.
330return None;
331 }
332 found = Some((FieldIdx::from_usize(field_idx), field));
333 }
334found335 }
336337/// If this type should match the ABI of the C `_Complex` type, returns the primitive that is
338 /// used for its components.
339 ///
340 /// This function only returns `Some(T)` for `core::num::Complex<T>` where `T` is
341 /// either a float or an integer. `repr(transparent)` wrapper types are automatically handled.
342pub fn complex_number<C>(&self, cx: &C) -> Option<Numeric>
343where
344Ty: TyAbiInterface<'a, C> + Copy,
345 {
346// We're checking for scalar repr below which excludes 1-ZST.
347let complex = self.peel_transparent_wrappers_from_non_1zst(cx);
348if !Ty::is_complex_number_lang_item(complex, cx) {
349return None;
350 }
351352let component = complex.field(cx, 0).peel_transparent_wrappers_from_non_1zst(cx);
353354if component.is_x87_f80(cx) {
355return Some(Numeric::Float(Float::X87F80));
356 }
357358let BackendRepr::Scalar(scalar) = component.backend_repr else {
359return None;
360 };
361362// Only Complex<{ float }> and Complex<{ integer }> have special layout.
363 //
364 // Explicitly spell out all the float types so that any new ones have to be added to
365 // one of the match branches.
366let primitive = scalar.primitive();
367match primitive {
368 Primitive::Int(integer, is_signed) => Some(Numeric::Int(integer, is_signed)),
369 Primitive::Float(
370 float @ (Float::F16 | Float::F32 | Float::F64 | Float::F128 | Float::PpcF128),
371 ) => Some(Numeric::Float(float)),
372 Primitive::Float(Float::X87F80) => None, // x87_f80_raw has incorrect alignment.
373Primitive::Pointer(..) | Primitive::Float(Float::F16B) => None,
374 }
375 }
376377/// Returns `Some` if this type has the ABI of the C `_Complex` type with float components.
378 /// See [`TyAndLayout::complex_number`] for details.
379pub fn complex_float<C>(&self, cx: &C) -> Option<Float>
380where
381Ty: TyAbiInterface<'a, C> + Copy,
382 {
383match self.complex_number(cx) {
384Some(Numeric::Float(float)) => Some(float),
385_ => None,
386 }
387 }
388389/// Whether this type/layout has any padding that is dependent on a variant, i.e. has bytes that
390 /// are padding for some, but not all, valid values of this type.
391pub fn has_variant_dependent_padding<C>(&self, cx: &C) -> bool392where
393Ty: TyAbiInterface<'a, C> + Copy,
394 {
395match self.variants {
396 Variants::Multiple { .. } => true,
397 Variants::Empty => false,
398 Variants::Single { .. } => match &self.fields {
399 FieldsShape::Primitive | FieldsShape::Union(_) => false,
400 FieldsShape::Array { count, .. } => {
401*count > 0 && self.field(cx, 0).has_variant_dependent_padding(cx)
402 }
403 FieldsShape::Arbitrary { offsets, .. } => {
404 (0..offsets.len()).any(|i| self.field(cx, i).has_variant_dependent_padding(cx))
405 }
406 },
407 }
408 }
409410/// The ranges of bytes that are always ignored by the representation relation of this type.
411 ///
412 /// In other words, for any sequence of bytes, if we reset the these padding bytes to uninit,
413 /// then these two sequences of bytes represent the same value (or they are both invalid).
414 /// This is the "guaranteed" padding. There may be more bytes that are padding for some
415 /// but not all variants of this type; those are not included.
416 /// (E.g. `Option<i8>` has no guaranteed padding so the empty range set is returned, but its `None` value still has padding).
417pub fn variant_independent_padding_ranges<C>(&self, cx: &C) -> Vec<Range<Size>>
418where
419Ty: TyAbiInterface<'a, C> + Copy,
420 {
421let mut data = RangeSet::new();
422self.add_data_ranges(cx, Size::ZERO, &mut data);
423424// Find gaps between the data ranges.
425let mut uninit_ranges = Vec::new();
426let mut covered_until = Size::ZERO;
427for &(offset, size) in data.0.iter() {
428if offset > covered_until {
429 uninit_ranges.push(covered_until..offset);
430 }
431 covered_until = Ord::max(covered_until, offset + size);
432 }
433434// Add trailing padding.
435if self.size > covered_until {
436uninit_ranges.push(covered_until..self.size);
437 }
438439uninit_ranges440 }
441442/// The ranges of bytes that are ignored by the representation relation of this variant.
443 ///
444 /// The result does not include variant-independent padding.
445pub fn variant_dependent_padding_ranges<C>(
446&self,
447 cx: &C,
448 variant_index: VariantIdx,
449 ) -> Vec<Range<Size>>
450where
451Ty: TyAbiInterface<'a, C> + Copy,
452 {
453let Variants::Multiple { .. } = self.variants else {
454return Vec::new();
455 };
456457// Bytes that are data in some variant.
458let mut any = RangeSet::new();
459self.add_data_ranges(cx, Size::ZERO, &mut any);
460461// Bytes that are data in this variant.
462let mut this = RangeSet::new();
463464// The variants do not contain e.g. the discriminant or coroutine upvars.
465let FieldsShape::Arbitrary { offsets, in_memory_order: _ } = &self.fields else {
466{
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("a multi-variant layout should have `Arbitrary` fields")));
}unreachable!("a multi-variant layout should have `Arbitrary` fields")467 };
468469// So add them explicitly.
470for (field, &offset) in offsets.iter_enumerated() {
471let field = self.field(cx, field.as_usize());
472 field.add_data_ranges(cx, offset, &mut this);
473 }
474475self.for_variant(cx, variant_index).add_data_ranges(cx, Size::ZERO, &mut this);
476477// Padding specific to this variant: data in some variant, but not in this one.
478any.difference(&this).0.iter().map(|&(offset, size)| offset..offset + size).collect()
479 }
480481/// Extend `out` with all ranges of bytes that *may* carry relevant data for values of this type.
482 /// For enums and unions there are offsets that are initialized for some
483 /// variants but not for others; those offset *will* get added to `out`.
484fn add_data_ranges<C>(self, cx: &C, base_offset: Size, out: &mut RangeSet<Size>)
485where
486Ty: TyAbiInterface<'a, C> + Copy,
487 {
488if self.is_zst() {
489return;
490 }
491492// Visit the fields of this value. For enum values the fields include the discriminant.
493match &self.fields {
494 FieldsShape::Primitive => {
495out.add_range(base_offset, self.size);
496 }
497&FieldsShape::Union(field_count) => {
498for field in 0..field_count.get() {
499let field = self.field(cx, field);
500 field.add_data_ranges(cx, base_offset, out);
501 }
502 }
503&FieldsShape::Array { stride, count } => {
504let elem = self.field(cx, 0);
505506// For scalars we know there is no padding between the elements,
507 // so the entire array is a single big data range.
508if elem.backend_repr.is_scalar() {
509out.add_range(base_offset, elem.size * count);
510 } else {
511// FIXME: this is really inefficient for large arrays.
512for idx in 0..count {
513 elem.add_data_ranges(cx, base_offset + idx * stride, out);
514 }
515 }
516 }
517 FieldsShape::Arbitrary { offsets, in_memory_order: _ } => {
518for (field, &offset) in offsets.iter_enumerated() {
519let field = self.field(cx, field.as_usize());
520 field.add_data_ranges(cx, base_offset + offset, out);
521 }
522 }
523 }
524525// Visit the fields of each variant.
526match &self.variants {
527 Variants::Empty | Variants::Single { index: _ } => { /* done */ }
528 Variants::Multiple { variants, .. } => {
529for variant in variants.indices() {
530let variant = self.for_variant(cx, variant);
531 variant.add_data_ranges(cx, base_offset, out);
532 }
533 }
534 }
535 }
536}