1use std::fmt;
2use std::ops::Deref;
3
4use rustc_data_structures::intern::Interned;
5use rustc_macros::HashStable_Generic;
6
7use crate::{
8 AbiAlign, Align, BackendRepr, FieldsShape, Float, HasDataLayout, LayoutData, Niche,
9 PointeeInfo, Primitive, Scalar, Size, TargetDataLayout, Variants,
10};
11
12rustc_index::newtype_index! {
15 #[derive(HashStable_Generic)]
37 #[encodable]
38 #[orderable]
39 pub struct FieldIdx {}
40}
41
42impl FieldIdx {
43 pub const ONE: FieldIdx = FieldIdx::from_u32(1);
47}
48
49rustc_index::newtype_index! {
50 #[derive(HashStable_Generic)]
61 #[encodable]
62 #[orderable]
63 pub struct VariantIdx {
64 const FIRST_VARIANT = 0;
66 }
67}
68#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable_Generic)]
69#[rustc_pass_by_value]
70pub struct Layout<'a>(pub Interned<'a, LayoutData<FieldIdx, VariantIdx>>);
71
72impl<'a> fmt::Debug for Layout<'a> {
73 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74 self.0.0.fmt(f)
76 }
77}
78
79impl<'a> Deref for Layout<'a> {
80 type Target = &'a LayoutData<FieldIdx, VariantIdx>;
81 fn deref(&self) -> &&'a LayoutData<FieldIdx, VariantIdx> {
82 &self.0.0
83 }
84}
85
86impl<'a> Layout<'a> {
87 pub fn fields(self) -> &'a FieldsShape<FieldIdx> {
88 &self.0.0.fields
89 }
90
91 pub fn variants(self) -> &'a Variants<FieldIdx, VariantIdx> {
92 &self.0.0.variants
93 }
94
95 pub fn backend_repr(self) -> BackendRepr {
96 self.0.0.backend_repr
97 }
98
99 pub fn largest_niche(self) -> Option<Niche> {
100 self.0.0.largest_niche
101 }
102
103 pub fn align(self) -> AbiAlign {
104 self.0.0.align
105 }
106
107 pub fn size(self) -> Size {
108 self.0.0.size
109 }
110
111 pub fn max_repr_align(self) -> Option<Align> {
112 self.0.0.max_repr_align
113 }
114
115 pub fn unadjusted_abi_align(self) -> Align {
116 self.0.0.unadjusted_abi_align
117 }
118
119 pub fn is_pointer_like(self, data_layout: &TargetDataLayout) -> bool {
124 self.size() == data_layout.pointer_size
125 && self.align().abi == data_layout.pointer_align.abi
126 && matches!(self.backend_repr(), BackendRepr::Scalar(Scalar::Initialized { .. }))
127 }
128}
129
130#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable_Generic)]
138pub struct TyAndLayout<'a, Ty> {
139 pub ty: Ty,
140 pub layout: Layout<'a>,
141}
142
143impl<'a, Ty: fmt::Display> fmt::Debug for TyAndLayout<'a, Ty> {
144 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145 f.debug_struct("TyAndLayout")
147 .field("ty", &format_args!("{}", self.ty))
148 .field("layout", &self.layout)
149 .finish()
150 }
151}
152
153impl<'a, Ty> Deref for TyAndLayout<'a, Ty> {
154 type Target = &'a LayoutData<FieldIdx, VariantIdx>;
155 fn deref(&self) -> &&'a LayoutData<FieldIdx, VariantIdx> {
156 &self.layout.0.0
157 }
158}
159
160impl<'a, Ty> AsRef<LayoutData<FieldIdx, VariantIdx>> for TyAndLayout<'a, Ty> {
161 fn as_ref(&self) -> &LayoutData<FieldIdx, VariantIdx> {
162 &*self.layout.0.0
163 }
164}
165
166pub trait TyAbiInterface<'a, C>: Sized + std::fmt::Debug {
169 fn ty_and_layout_for_variant(
170 this: TyAndLayout<'a, Self>,
171 cx: &C,
172 variant_index: VariantIdx,
173 ) -> TyAndLayout<'a, Self>;
174 fn ty_and_layout_field(this: TyAndLayout<'a, Self>, cx: &C, i: usize) -> TyAndLayout<'a, Self>;
175 fn ty_and_layout_pointee_info_at(
176 this: TyAndLayout<'a, Self>,
177 cx: &C,
178 offset: Size,
179 ) -> Option<PointeeInfo>;
180 fn is_adt(this: TyAndLayout<'a, Self>) -> bool;
181 fn is_never(this: TyAndLayout<'a, Self>) -> bool;
182 fn is_tuple(this: TyAndLayout<'a, Self>) -> bool;
183 fn is_unit(this: TyAndLayout<'a, Self>) -> bool;
184 fn is_transparent(this: TyAndLayout<'a, Self>) -> bool;
185}
186
187impl<'a, Ty> TyAndLayout<'a, Ty> {
188 pub fn for_variant<C>(self, cx: &C, variant_index: VariantIdx) -> Self
189 where
190 Ty: TyAbiInterface<'a, C>,
191 {
192 Ty::ty_and_layout_for_variant(self, cx, variant_index)
193 }
194
195 pub fn field<C>(self, cx: &C, i: usize) -> Self
196 where
197 Ty: TyAbiInterface<'a, C>,
198 {
199 Ty::ty_and_layout_field(self, cx, i)
200 }
201
202 pub fn pointee_info_at<C>(self, cx: &C, offset: Size) -> Option<PointeeInfo>
203 where
204 Ty: TyAbiInterface<'a, C>,
205 {
206 Ty::ty_and_layout_pointee_info_at(self, cx, offset)
207 }
208
209 pub fn is_single_fp_element<C>(self, cx: &C) -> bool
210 where
211 Ty: TyAbiInterface<'a, C>,
212 C: HasDataLayout,
213 {
214 match self.backend_repr {
215 BackendRepr::Scalar(scalar) => {
216 matches!(scalar.primitive(), Primitive::Float(Float::F32 | Float::F64))
217 }
218 BackendRepr::Memory { .. } => {
219 if self.fields.count() == 1 && self.fields.offset(0).bytes() == 0 {
220 self.field(cx, 0).is_single_fp_element(cx)
221 } else {
222 false
223 }
224 }
225 _ => false,
226 }
227 }
228
229 pub fn is_single_vector_element<C>(self, cx: &C, expected_size: Size) -> bool
230 where
231 Ty: TyAbiInterface<'a, C>,
232 C: HasDataLayout,
233 {
234 match self.backend_repr {
235 BackendRepr::SimdVector { .. } => self.size == expected_size,
236 BackendRepr::Memory { .. } => {
237 if self.fields.count() == 1 && self.fields.offset(0).bytes() == 0 {
238 self.field(cx, 0).is_single_vector_element(cx, expected_size)
239 } else {
240 false
241 }
242 }
243 _ => false,
244 }
245 }
246
247 pub fn is_adt<C>(self) -> bool
248 where
249 Ty: TyAbiInterface<'a, C>,
250 {
251 Ty::is_adt(self)
252 }
253
254 pub fn is_never<C>(self) -> bool
255 where
256 Ty: TyAbiInterface<'a, C>,
257 {
258 Ty::is_never(self)
259 }
260
261 pub fn is_tuple<C>(self) -> bool
262 where
263 Ty: TyAbiInterface<'a, C>,
264 {
265 Ty::is_tuple(self)
266 }
267
268 pub fn is_unit<C>(self) -> bool
269 where
270 Ty: TyAbiInterface<'a, C>,
271 {
272 Ty::is_unit(self)
273 }
274
275 pub fn is_transparent<C>(self) -> bool
276 where
277 Ty: TyAbiInterface<'a, C>,
278 {
279 Ty::is_transparent(self)
280 }
281
282 pub fn non_1zst_field<C>(&self, cx: &C) -> Option<(FieldIdx, Self)>
285 where
286 Ty: TyAbiInterface<'a, C> + Copy,
287 {
288 let mut found = None;
289 for field_idx in 0..self.fields.count() {
290 let field = self.field(cx, field_idx);
291 if field.is_1zst() {
292 continue;
293 }
294 if found.is_some() {
295 return None;
297 }
298 found = Some((FieldIdx::from_usize(field_idx), field));
299 }
300 found
301 }
302}