1use std::fmt;
2use std::ops::{Deref, Range};
3
4use rustc_data_structures::intern::Interned;
5use rustc_data_structures::range_set::RangeSet;
6use rustc_macros::StableHash;
7
8use crate::layout::{FieldIdx, VariantIdx};
9use crate::{
10 AbiAlign, Align, BackendRepr, FieldsShape, Float, HasDataLayout, LayoutData, Niche, Numeric,
11 PointeeInfo, Primitive, Size, Variants,
12};
13
14#[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]
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>>);
19
20impl<'a> fmt::Debug for Layout<'a> {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 self.0.0.fmt(f)
24 }
25}
26
27impl<'a> Deref for Layout<'a> {
28 type Target = &'a LayoutData<FieldIdx, VariantIdx>;
29 fn deref(&self) -> &&'a LayoutData<FieldIdx, VariantIdx> {
30 &self.0.0
31 }
32}
33
34impl<'a> Layout<'a> {
35 pub fn fields(self) -> &'a FieldsShape<FieldIdx> {
36 &self.0.0.fields
37 }
38
39 pub fn variants(self) -> &'a Variants<FieldIdx, VariantIdx> {
40 &self.0.0.variants
41 }
42
43 pub fn backend_repr(self) -> BackendRepr {
44 self.0.0.backend_repr
45 }
46
47 pub fn largest_niche(self) -> Option<Niche> {
48 self.0.0.largest_niche
49 }
50
51 pub fn align(self) -> AbiAlign {
52 self.0.0.align
53 }
54
55 pub fn size(self) -> Size {
56 self.0.0.size
57 }
58
59 pub fn max_repr_align(self) -> Option<Align> {
60 self.0.0.max_repr_align
61 }
62
63 pub fn unadjusted_abi_align(self) -> Align {
64 self.0.0.unadjusted_abi_align
65 }
66}
67
68#[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]
fn clone(&self) -> Self {
Self {
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> {
77 pub ty: Ty,
78 pub layout: Layout<'a>,
79}
80
81impl<'a, Ty: fmt::Display> fmt::Debug for TyAndLayout<'a, Ty> {
82 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83 f.debug_struct("TyAndLayout")
85 .field("ty", &format_args!("{0}", self.ty)format_args!("{}", self.ty))
86 .field("layout", &self.layout)
87 .finish()
88 }
89}
90
91impl<'a, Ty> Deref for TyAndLayout<'a, Ty> {
92 type Target = &'a LayoutData<FieldIdx, VariantIdx>;
93 fn deref(&self) -> &&'a LayoutData<FieldIdx, VariantIdx> {
94 &self.layout.0.0
95 }
96}
97
98impl<'a, Ty> AsRef<LayoutData<FieldIdx, VariantIdx>> for TyAndLayout<'a, Ty> {
99 fn as_ref(&self) -> &LayoutData<FieldIdx, VariantIdx> {
100 &*self.layout.0.0
101 }
102}
103
104pub trait TyAbiInterface<'a, C>: Sized + std::fmt::Debug + std::fmt::Display {
107 fn ty_and_layout_for_variant(
108 this: TyAndLayout<'a, Self>,
109 cx: &C,
110 variant_index: VariantIdx,
111 ) -> TyAndLayout<'a, Self>;
112 fn ty_and_layout_field(this: TyAndLayout<'a, Self>, cx: &C, i: usize) -> TyAndLayout<'a, Self>;
113 fn ty_and_layout_pointee_info_at(
114 this: TyAndLayout<'a, Self>,
115 cx: &C,
116 offset: Size,
117 ) -> Option<PointeeInfo>;
118 fn is_adt(this: TyAndLayout<'a, Self>) -> bool;
119 fn is_enum(this: TyAndLayout<'a, Self>) -> bool;
120 fn is_never(this: TyAndLayout<'a, Self>) -> bool;
121 fn is_tuple(this: TyAndLayout<'a, Self>) -> bool;
122 fn is_unit(this: TyAndLayout<'a, Self>) -> bool;
123 fn is_transparent(this: TyAndLayout<'a, Self>) -> bool;
124 fn is_complex_number_lang_item(this: TyAndLayout<'a, Self>, cx: &C) -> bool;
125 fn is_scalable_vector(this: TyAndLayout<'a, Self>) -> bool;
126 fn is_pass_indirectly_in_non_rustic_abis_flag_set(this: TyAndLayout<'a, Self>) -> bool;
128}
129
130impl<'a, Ty> TyAndLayout<'a, Ty> {
131 pub fn for_variant<C>(self, cx: &C, variant_index: VariantIdx) -> Self
139 where
140 Ty: TyAbiInterface<'a, C>,
141 {
142 Ty::ty_and_layout_for_variant(self, cx, variant_index)
143 }
144
145 pub fn field<C>(self, cx: &C, i: usize) -> Self
146 where
147 Ty: TyAbiInterface<'a, C>,
148 {
149 Ty::ty_and_layout_field(self, cx, i)
150 }
151
152 pub fn pointee_info_at<C>(self, cx: &C, offset: Size) -> Option<PointeeInfo>
153 where
154 Ty: TyAbiInterface<'a, C>,
155 {
156 Ty::ty_and_layout_pointee_info_at(self, cx, offset)
157 }
158
159 pub fn is_single_vector_element<C>(self, cx: &C, expected_size: Size) -> bool
160 where
161 Ty: TyAbiInterface<'a, C>,
162 C: HasDataLayout,
163 {
164 match self.backend_repr {
165 BackendRepr::SimdVector { .. } => self.size == expected_size,
166 BackendRepr::Memory { .. } => {
167 if self.fields.count() == 1 && self.fields.offset(0).bytes() == 0 {
168 self.field(cx, 0).is_single_vector_element(cx, expected_size)
169 } else {
170 false
171 }
172 }
173 _ => false,
174 }
175 }
176
177 pub fn is_adt<C>(self) -> bool
178 where
179 Ty: TyAbiInterface<'a, C>,
180 {
181 Ty::is_adt(self)
182 }
183
184 pub fn is_enum<C>(self) -> bool
185 where
186 Ty: TyAbiInterface<'a, C>,
187 {
188 Ty::is_enum(self)
189 }
190
191 pub fn is_never<C>(self) -> bool
192 where
193 Ty: TyAbiInterface<'a, C>,
194 {
195 Ty::is_never(self)
196 }
197
198 pub fn is_tuple<C>(self) -> bool
199 where
200 Ty: TyAbiInterface<'a, C>,
201 {
202 Ty::is_tuple(self)
203 }
204
205 pub fn is_unit<C>(self) -> bool
206 where
207 Ty: TyAbiInterface<'a, C>,
208 {
209 Ty::is_unit(self)
210 }
211
212 pub fn is_transparent<C>(self) -> bool
213 where
214 Ty: TyAbiInterface<'a, C>,
215 {
216 Ty::is_transparent(self)
217 }
218
219 pub fn is_complex_number<C>(self, cx: &C) -> bool
222 where
223 Ty: TyAbiInterface<'a, C> + Copy,
224 {
225 self.complex_number(cx).is_some()
226 }
227
228 pub fn is_scalable_vector<C>(self) -> bool
229 where
230 Ty: TyAbiInterface<'a, C>,
231 {
232 Ty::is_scalable_vector(self)
233 }
234
235 pub fn pass_indirectly_in_non_rustic_abis<C>(self, cx: &C) -> bool
247 where
248 Ty: TyAbiInterface<'a, C> + Copy,
249 {
250 let base = self.peel_transparent_wrappers(cx);
251 Ty::is_pass_indirectly_in_non_rustic_abis_flag_set(base)
252 }
253
254 pub fn peel_transparent_wrappers<C>(mut self, cx: &C) -> Self
259 where
260 Ty: TyAbiInterface<'a, C> + Copy,
261 {
262 while self.is_transparent()
263 && let Some((_, field)) = self.non_1zst_field(cx)
264 {
265 self = field;
266 }
267
268 self
269 }
270
271 pub fn non_1zst_field<C>(&self, cx: &C) -> Option<(FieldIdx, Self)>
274 where
275 Ty: TyAbiInterface<'a, C> + Copy,
276 {
277 let mut found = None;
278 for field_idx in 0..self.fields.count() {
279 let field = self.field(cx, field_idx);
280 if field.is_1zst() {
281 continue;
282 }
283 if found.is_some() {
284 return None;
286 }
287 found = Some((FieldIdx::from_usize(field_idx), field));
288 }
289 found
290 }
291
292 pub fn non_zst_field_ignore_alignment<C>(&self, cx: &C) -> Option<(FieldIdx, Self)>
297 where
298 Ty: TyAbiInterface<'a, C> + Copy,
299 {
300 let mut found = None;
301 for field_idx in 0..self.fields.count() {
302 let field = self.field(cx, field_idx);
303 if field.is_zst() {
304 continue;
305 }
306 if found.is_some() {
307 return None;
309 }
310 found = Some((FieldIdx::from_usize(field_idx), field));
311 }
312 found
313 }
314
315 pub fn complex_number<C>(&self, cx: &C) -> Option<Numeric>
321 where
322 Ty: TyAbiInterface<'a, C> + Copy,
323 {
324 let complex = self.peel_transparent_wrappers(cx);
325 if !Ty::is_complex_number_lang_item(complex, cx) {
326 return None;
327 }
328
329 let component = complex.field(cx, 0).peel_transparent_wrappers(cx);
330
331 let BackendRepr::Scalar(scalar) = component.backend_repr else {
332 return None;
333 };
334
335 let primitive = scalar.primitive();
340 match primitive {
341 Primitive::Int(integer, is_signed) => Some(Numeric::Int(integer, is_signed)),
342 Primitive::Float(
343 float @ (Float::F16 | Float::F32 | Float::F64 | Float::F128 | Float::PpcF128),
344 ) => Some(Numeric::Float(float)),
345 Primitive::Pointer(..) | Primitive::Float(Float::F16B) => None,
346 }
347 }
348
349 pub fn complex_float<C>(&self, cx: &C) -> Option<Float>
352 where
353 Ty: TyAbiInterface<'a, C> + Copy,
354 {
355 match self.complex_number(cx) {
356 Some(Numeric::Float(float)) => Some(float),
357 _ => None,
358 }
359 }
360
361 pub fn has_variant_dependent_padding<C>(&self, cx: &C) -> bool
364 where
365 Ty: TyAbiInterface<'a, C> + Copy,
366 {
367 match self.variants {
368 Variants::Multiple { .. } => true,
369 Variants::Empty => false,
370 Variants::Single { .. } => match &self.fields {
371 FieldsShape::Primitive | FieldsShape::Union(_) => false,
372 FieldsShape::Array { count, .. } => {
373 *count > 0 && self.field(cx, 0).has_variant_dependent_padding(cx)
374 }
375 FieldsShape::Arbitrary { offsets, .. } => {
376 (0..offsets.len()).any(|i| self.field(cx, i).has_variant_dependent_padding(cx))
377 }
378 },
379 }
380 }
381
382 pub fn variant_independent_padding_ranges<C>(&self, cx: &C) -> Vec<Range<Size>>
390 where
391 Ty: TyAbiInterface<'a, C> + Copy,
392 {
393 let mut data = RangeSet::new();
394 self.add_data_ranges(cx, Size::ZERO, &mut data);
395
396 let mut uninit_ranges = Vec::new();
398 let mut covered_until = Size::ZERO;
399 for &(offset, size) in data.0.iter() {
400 if offset > covered_until {
401 uninit_ranges.push(covered_until..offset);
402 }
403 covered_until = Ord::max(covered_until, offset + size);
404 }
405
406 if self.size > covered_until {
408 uninit_ranges.push(covered_until..self.size);
409 }
410
411 uninit_ranges
412 }
413
414 pub fn variant_dependent_padding_ranges<C>(
418 &self,
419 cx: &C,
420 variant_index: VariantIdx,
421 ) -> Vec<Range<Size>>
422 where
423 Ty: TyAbiInterface<'a, C> + Copy,
424 {
425 let Variants::Multiple { .. } = self.variants else {
426 return Vec::new();
427 };
428
429 let mut any = RangeSet::new();
431 self.add_data_ranges(cx, Size::ZERO, &mut any);
432
433 let mut this = RangeSet::new();
435
436 let FieldsShape::Arbitrary { offsets, in_memory_order: _ } = &self.fields else {
438 {
::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")
439 };
440
441 for (field, &offset) in offsets.iter_enumerated() {
443 let field = self.field(cx, field.as_usize());
444 field.add_data_ranges(cx, offset, &mut this);
445 }
446
447 self.for_variant(cx, variant_index).add_data_ranges(cx, Size::ZERO, &mut this);
448
449 any.difference(&this).0.iter().map(|&(offset, size)| offset..offset + size).collect()
451 }
452
453 fn add_data_ranges<C>(self, cx: &C, base_offset: Size, out: &mut RangeSet<Size>)
457 where
458 Ty: TyAbiInterface<'a, C> + Copy,
459 {
460 if self.is_zst() {
461 return;
462 }
463
464 match &self.fields {
466 FieldsShape::Primitive => {
467 out.add_range(base_offset, self.size);
468 }
469 &FieldsShape::Union(field_count) => {
470 for field in 0..field_count.get() {
471 let field = self.field(cx, field);
472 field.add_data_ranges(cx, base_offset, out);
473 }
474 }
475 &FieldsShape::Array { stride, count } => {
476 let elem = self.field(cx, 0);
477
478 if elem.backend_repr.is_scalar() {
481 out.add_range(base_offset, elem.size * count);
482 } else {
483 for idx in 0..count {
485 elem.add_data_ranges(cx, base_offset + idx * stride, out);
486 }
487 }
488 }
489 FieldsShape::Arbitrary { offsets, in_memory_order: _ } => {
490 for (field, &offset) in offsets.iter_enumerated() {
491 let field = self.field(cx, field.as_usize());
492 field.add_data_ranges(cx, base_offset + offset, out);
493 }
494 }
495 }
496
497 match &self.variants {
499 Variants::Empty | Variants::Single { index: _ } => { }
500 Variants::Multiple { variants, .. } => {
501 for variant in variants.indices() {
502 let variant = self.for_variant(cx, variant);
503 variant.add_data_ranges(cx, base_offset, out);
504 }
505 }
506 }
507 }
508}