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,
11 PointeeInfo, Primitive, Size, Variants,
12};
13
14#[derive(#[automatically_derived]
impl<'a> ::core::marker::Copy for Layout<'a> { }Copy, #[automatically_derived]
impl<'a> ::core::clone::Clone for Layout<'a> {
#[inline]
fn clone(&self) -> Layout<'a> {
let _:
::core::clone::AssertParamIsClone<Interned<'a,
LayoutData<FieldIdx, VariantIdx>>>;
*self
}
}Clone, #[automatically_derived]
impl<'a> ::core::cmp::PartialEq for Layout<'a> {
#[inline]
fn eq(&self, other: &Layout<'a>) -> 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) -> TyAndLayout<'a, Ty> {
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::cmp::PartialEq for
TyAndLayout<'a, Ty> {
#[inline]
fn eq(&self, other: &TyAndLayout<'a, Ty>) -> 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_never(this: TyAndLayout<'a, Self>) -> bool;
120 fn is_tuple(this: TyAndLayout<'a, Self>) -> bool;
121 fn is_unit(this: TyAndLayout<'a, Self>) -> bool;
122 fn is_transparent(this: TyAndLayout<'a, Self>) -> bool;
123 fn is_scalable_vector(this: TyAndLayout<'a, Self>) -> bool;
124 fn is_pass_indirectly_in_non_rustic_abis_flag_set(this: TyAndLayout<'a, Self>) -> bool;
126}
127
128impl<'a, Ty> TyAndLayout<'a, Ty> {
129 pub fn for_variant<C>(self, cx: &C, variant_index: VariantIdx) -> Self
130 where
131 Ty: TyAbiInterface<'a, C>,
132 {
133 Ty::ty_and_layout_for_variant(self, cx, variant_index)
134 }
135
136 pub fn field<C>(self, cx: &C, i: usize) -> Self
137 where
138 Ty: TyAbiInterface<'a, C>,
139 {
140 Ty::ty_and_layout_field(self, cx, i)
141 }
142
143 pub fn pointee_info_at<C>(self, cx: &C, offset: Size) -> Option<PointeeInfo>
144 where
145 Ty: TyAbiInterface<'a, C>,
146 {
147 Ty::ty_and_layout_pointee_info_at(self, cx, offset)
148 }
149
150 pub fn is_single_fp_element<C>(self, cx: &C) -> bool
151 where
152 Ty: TyAbiInterface<'a, C>,
153 C: HasDataLayout,
154 {
155 match self.backend_repr {
156 BackendRepr::Scalar(scalar) => {
157 #[allow(non_exhaustive_omitted_patterns)] match scalar.primitive() {
Primitive::Float(Float::F32 | Float::F64) => true,
_ => false,
}matches!(scalar.primitive(), Primitive::Float(Float::F32 | Float::F64))
158 }
159 BackendRepr::Memory { .. } => {
160 if self.fields.count() == 1 && self.fields.offset(0).bytes() == 0 {
161 self.field(cx, 0).is_single_fp_element(cx)
162 } else {
163 false
164 }
165 }
166 _ => false,
167 }
168 }
169
170 pub fn is_single_vector_element<C>(self, cx: &C, expected_size: Size) -> bool
171 where
172 Ty: TyAbiInterface<'a, C>,
173 C: HasDataLayout,
174 {
175 match self.backend_repr {
176 BackendRepr::SimdVector { .. } => self.size == expected_size,
177 BackendRepr::Memory { .. } => {
178 if self.fields.count() == 1 && self.fields.offset(0).bytes() == 0 {
179 self.field(cx, 0).is_single_vector_element(cx, expected_size)
180 } else {
181 false
182 }
183 }
184 _ => false,
185 }
186 }
187
188 pub fn is_adt<C>(self) -> bool
189 where
190 Ty: TyAbiInterface<'a, C>,
191 {
192 Ty::is_adt(self)
193 }
194
195 pub fn is_never<C>(self) -> bool
196 where
197 Ty: TyAbiInterface<'a, C>,
198 {
199 Ty::is_never(self)
200 }
201
202 pub fn is_tuple<C>(self) -> bool
203 where
204 Ty: TyAbiInterface<'a, C>,
205 {
206 Ty::is_tuple(self)
207 }
208
209 pub fn is_unit<C>(self) -> bool
210 where
211 Ty: TyAbiInterface<'a, C>,
212 {
213 Ty::is_unit(self)
214 }
215
216 pub fn is_transparent<C>(self) -> bool
217 where
218 Ty: TyAbiInterface<'a, C>,
219 {
220 Ty::is_transparent(self)
221 }
222
223 pub fn is_scalable_vector<C>(self) -> bool
224 where
225 Ty: TyAbiInterface<'a, C>,
226 {
227 Ty::is_scalable_vector(self)
228 }
229
230 pub fn pass_indirectly_in_non_rustic_abis<C>(self, cx: &C) -> bool
242 where
243 Ty: TyAbiInterface<'a, C> + Copy,
244 {
245 let base = self.peel_transparent_wrappers(cx);
246 Ty::is_pass_indirectly_in_non_rustic_abis_flag_set(base)
247 }
248
249 pub fn peel_transparent_wrappers<C>(mut self, cx: &C) -> Self
254 where
255 Ty: TyAbiInterface<'a, C> + Copy,
256 {
257 while self.is_transparent()
258 && let Some((_, field)) = self.non_1zst_field(cx)
259 {
260 self = field;
261 }
262
263 self
264 }
265
266 pub fn non_1zst_field<C>(&self, cx: &C) -> Option<(FieldIdx, Self)>
269 where
270 Ty: TyAbiInterface<'a, C> + Copy,
271 {
272 let mut found = None;
273 for field_idx in 0..self.fields.count() {
274 let field = self.field(cx, field_idx);
275 if field.is_1zst() {
276 continue;
277 }
278 if found.is_some() {
279 return None;
281 }
282 found = Some((FieldIdx::from_usize(field_idx), field));
283 }
284 found
285 }
286
287 pub fn padding_ranges<C>(&self, cx: &C) -> Vec<Range<Size>>
295 where
296 Ty: TyAbiInterface<'a, C> + Copy,
297 {
298 let mut data = RangeSet::new();
299 self.add_data_ranges(cx, Size::ZERO, &mut data);
300
301 let mut uninit_ranges = Vec::new();
303 let mut covered_until = Size::ZERO;
304 for &(offset, size) in data.0.iter() {
305 if offset > covered_until {
306 uninit_ranges.push(covered_until..offset);
307 }
308 covered_until = Ord::max(covered_until, offset + size);
309 }
310
311 if self.size > covered_until {
313 uninit_ranges.push(covered_until..self.size);
314 }
315
316 uninit_ranges
317 }
318
319 fn add_data_ranges<C>(self, cx: &C, base_offset: Size, out: &mut RangeSet<Size>)
323 where
324 Ty: TyAbiInterface<'a, C> + Copy,
325 {
326 if self.is_zst() {
327 return;
328 }
329
330 match &self.variants {
331 Variants::Empty => { }
332 Variants::Single { index: _ } => match &self.fields {
333 FieldsShape::Primitive => {
334 out.add_range(base_offset, self.size);
335 }
336 &FieldsShape::Union(field_count) => {
337 for field in 0..field_count.get() {
338 let field = self.field(cx, field);
339 field.add_data_ranges(cx, base_offset, out);
340 }
341 }
342 &FieldsShape::Array { stride, count } => {
343 let elem = self.field(cx, 0);
344
345 if elem.backend_repr.is_scalar() {
348 out.add_range(base_offset, elem.size * count);
349 } else {
350 for idx in 0..count {
352 elem.add_data_ranges(cx, base_offset + idx * stride, out);
353 }
354 }
355 }
356 FieldsShape::Arbitrary { offsets, in_memory_order: _ } => {
357 for (field, &offset) in offsets.iter_enumerated() {
358 let field = self.field(cx, field.as_usize());
359 field.add_data_ranges(cx, base_offset + offset, out);
360 }
361 }
362 },
363 Variants::Multiple { variants, .. } => {
364 for variant in variants.indices() {
365 let variant = self.for_variant(cx, variant);
366 variant.add_data_ranges(cx, base_offset, out);
367 }
368 }
369 }
370 }
371}