1use std::ops::Deref as _;
2
3use rustc_abi::{
4 Align, BackendRepr, FieldIdx, FieldsShape, Size, TagEncoding, VariantIdx, Variants,
5};
6use rustc_middle::mir::PlaceTy;
7use rustc_middle::mir::interpret::Scalar;
8use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutOf, TyAndLayout};
9use rustc_middle::ty::{self, Ty};
10use rustc_middle::{bug, mir};
11use rustc_span::DUMMY_SP;
12use tracing::{debug, instrument};
13
14use super::operand::OperandValue;
15use super::{FunctionCx, LocalRef};
16use crate::common::IntPredicate;
17use crate::size_of_val;
18use crate::traits::*;
19
20#[derive(#[automatically_derived]
impl<V: ::core::marker::Copy> ::core::marker::Copy for PlaceValue<V> { }Copy, #[automatically_derived]
impl<V: ::core::clone::Clone> ::core::clone::Clone for PlaceValue<V> {
#[inline]
fn clone(&self) -> PlaceValue<V> {
PlaceValue {
llval: ::core::clone::Clone::clone(&self.llval),
llextra: ::core::clone::Clone::clone(&self.llextra),
align: ::core::clone::Clone::clone(&self.align),
}
}
}Clone, #[automatically_derived]
impl<V: ::core::fmt::Debug> ::core::fmt::Debug for PlaceValue<V> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f, "PlaceValue",
"llval", &self.llval, "llextra", &self.llextra, "align",
&&self.align)
}
}Debug)]
27pub struct PlaceValue<V> {
28 pub llval: V,
30
31 pub llextra: Option<V>,
33
34 pub align: Align,
36}
37
38impl<V: CodegenObject> PlaceValue<V> {
39 pub fn new_sized(llval: V, align: Align) -> PlaceValue<V> {
43 PlaceValue { llval, llextra: None, align }
44 }
45
46 pub fn alloca<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx, Value = V>>(
51 bx: &mut Bx,
52 size: Size,
53 align: Align,
54 ) -> PlaceValue<V> {
55 let llval = bx.alloca(size, align);
56 PlaceValue::new_sized(llval, align)
57 }
58
59 pub fn with_type<'tcx>(self, layout: TyAndLayout<'tcx>) -> PlaceRef<'tcx, V> {
61 if !(layout.is_unsized() || layout.is_uninhabited() || self.llextra.is_none())
{
{
::core::panicking::panic_fmt(format_args!("Had pointer metadata {0:?} for sized type {1:?}",
self.llextra, layout));
}
};assert!(
62 layout.is_unsized() || layout.is_uninhabited() || self.llextra.is_none(),
63 "Had pointer metadata {:?} for sized type {layout:?}",
64 self.llextra,
65 );
66 PlaceRef { val: self, layout }
67 }
68
69 pub fn address(self) -> OperandValue<V> {
74 if let Some(llextra) = self.llextra {
75 OperandValue::Pair(self.llval, llextra)
76 } else {
77 OperandValue::Immediate(self.llval)
78 }
79 }
80}
81
82#[derive(#[automatically_derived]
impl<'tcx, V: ::core::marker::Copy> ::core::marker::Copy for PlaceRef<'tcx, V>
{
}Copy, #[automatically_derived]
impl<'tcx, V: ::core::clone::Clone> ::core::clone::Clone for PlaceRef<'tcx, V>
{
#[inline]
fn clone(&self) -> PlaceRef<'tcx, V> {
PlaceRef {
val: ::core::clone::Clone::clone(&self.val),
layout: ::core::clone::Clone::clone(&self.layout),
}
}
}Clone, #[automatically_derived]
impl<'tcx, V: ::core::fmt::Debug> ::core::fmt::Debug for PlaceRef<'tcx, V> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "PlaceRef",
"val", &self.val, "layout", &&self.layout)
}
}Debug)]
83pub struct PlaceRef<'tcx, V> {
84 pub val: PlaceValue<V>,
86
87 pub layout: TyAndLayout<'tcx>,
93}
94
95impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
96 pub fn new_sized(llval: V, layout: TyAndLayout<'tcx>) -> PlaceRef<'tcx, V> {
97 PlaceRef::new_sized_aligned(llval, layout, layout.align.abi)
98 }
99
100 pub fn new_sized_aligned(
101 llval: V,
102 layout: TyAndLayout<'tcx>,
103 align: Align,
104 ) -> PlaceRef<'tcx, V> {
105 if !layout.is_sized() {
::core::panicking::panic("assertion failed: layout.is_sized()")
};assert!(layout.is_sized());
106 PlaceValue::new_sized(llval, align).with_type(layout)
107 }
108
109 pub fn alloca<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
112 bx: &mut Bx,
113 layout: TyAndLayout<'tcx>,
114 ) -> Self {
115 if layout.peel_transparent_wrappers(bx).deref().is_scalable_vector() {
116 Self::alloca_scalable(bx, layout)
117 } else {
118 Self::alloca_size(bx, layout.size, layout)
119 }
120 }
121
122 pub fn alloca_size<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
123 bx: &mut Bx,
124 size: Size,
125 layout: TyAndLayout<'tcx>,
126 ) -> Self {
127 if !layout.is_sized() {
{
::core::panicking::panic_fmt(format_args!("tried to statically allocate unsized place"));
}
};assert!(layout.is_sized(), "tried to statically allocate unsized place");
128 PlaceValue::alloca(bx, size, layout.align.abi).with_type(layout)
129 }
130
131 pub fn alloca_unsized_indirect<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
135 bx: &mut Bx,
136 layout: TyAndLayout<'tcx>,
137 ) -> Self {
138 if !layout.is_unsized() {
{
::core::panicking::panic_fmt(format_args!("tried to allocate indirect place for sized values"));
}
};assert!(layout.is_unsized(), "tried to allocate indirect place for sized values");
139 let ptr_ty = Ty::new_mut_ptr(bx.cx().tcx(), layout.ty);
140 let ptr_layout = bx.cx().layout_of(ptr_ty);
141 Self::alloca(bx, ptr_layout)
142 }
143
144 pub fn len<Cx: ConstCodegenMethods<Value = V>>(&self, cx: &Cx) -> V {
145 if let FieldsShape::Array { count, .. } = self.layout.fields {
146 if self.layout.is_unsized() {
147 {
match (&count, &0) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};assert_eq!(count, 0);
148 self.val.llextra.unwrap()
149 } else {
150 cx.const_usize(count)
151 }
152 } else {
153 ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected layout `{0:#?}` in PlaceRef::len",
self.layout))bug!("unexpected layout `{:#?}` in PlaceRef::len", self.layout)
154 }
155 }
156
157 fn alloca_scalable<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
158 bx: &mut Bx,
159 layout: TyAndLayout<'tcx>,
160 ) -> Self {
161 PlaceValue::new_sized(
162 bx.alloca_with_ty(layout.peel_transparent_wrappers(bx)),
163 layout.align.abi,
164 )
165 .with_type(layout)
166 }
167}
168
169impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
170 pub fn project_field<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
172 self,
173 bx: &mut Bx,
174 ix: usize,
175 ) -> Self {
176 let field = self.layout.field(bx.cx(), ix);
177 let offset = self.layout.fields.offset(ix);
178 let effective_field_align = self.val.align.restrict_for_offset(offset);
179
180 let mut simple = || {
183 let llval = if offset.bytes() == 0 {
184 self.val.llval
185 } else {
186 bx.inbounds_ptradd(self.val.llval, bx.const_usize(offset.bytes()))
187 };
188 let val = PlaceValue {
189 llval,
190 llextra: if bx.cx().tcx().type_has_metadata(field.ty, bx.cx().typing_env()) {
191 self.val.llextra
192 } else {
193 None
194 },
195 align: effective_field_align,
196 };
197 val.with_type(field)
198 };
199
200 match field.ty.kind() {
206 _ if field.is_sized() => return simple(),
207 ty::Slice(..) | ty::Str => return simple(),
208 _ if offset.bytes() == 0 => return simple(),
209 _ => {}
210 }
211
212 let meta = self.val.llextra;
228
229 let unaligned_offset = bx.cx().const_usize(offset.bytes());
230
231 let (_, mut unsized_align) =
233 size_of_val::size_and_align_of_dst(bx, field.ty, meta, DUMMY_SP);
234
235 if let ty::Adt(def, _) = self.layout.ty.kind()
237 && let Some(packed) = def.repr().pack
238 {
239 let packed = bx.const_usize(packed.bytes());
240 let cmp = bx.icmp(IntPredicate::IntULT, unsized_align, packed);
241 unsized_align = bx.select(cmp, unsized_align, packed)
242 }
243
244 let offset = round_up_const_value_to_alignment(bx, unaligned_offset, unsized_align);
246
247 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/mir/place.rs:247",
"rustc_codegen_ssa::mir::place", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/mir/place.rs"),
::tracing_core::__macro_support::Option::Some(247u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::mir::place"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("struct_field_ptr: DST field offset: {0:?}",
offset) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("struct_field_ptr: DST field offset: {:?}", offset);
248
249 let ptr = bx.inbounds_ptradd(self.val.llval, offset);
251 let val =
252 PlaceValue { llval: ptr, llextra: self.val.llextra, align: effective_field_align };
253 val.with_type(field)
254 }
255
256 pub fn codegen_set_discr<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
259 &self,
260 bx: &mut Bx,
261 variant_index: VariantIdx,
262 ) {
263 match codegen_tag_value(bx.cx(), variant_index, self.layout) {
264 Err(UninhabitedVariantError) => {
265 bx.abort();
268 }
269 Ok(Some((tag_field, imm))) => {
270 let tag_place = self.project_field(bx, tag_field.as_usize());
271 OperandValue::Immediate(imm).store(bx, tag_place);
272 }
273 Ok(None) => {}
274 }
275 }
276
277 pub fn project_index<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
278 &self,
279 bx: &mut Bx,
280 llindex: V,
281 ) -> Self {
282 let layout = self.layout.field(bx, 0);
285 let offset = if let Some(llindex) = bx.const_to_opt_uint(llindex) {
286 layout.size.checked_mul(llindex, bx).unwrap_or(layout.size)
287 } else {
288 layout.size
289 };
290
291 let llval = bx.inbounds_nuw_gep(bx.cx().backend_type(layout), self.val.llval, &[llindex]);
292 let align = self.val.align.restrict_for_offset(offset);
293 PlaceValue::new_sized(llval, align).with_type(layout)
294 }
295
296 pub fn project_downcast<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
297 &self,
298 bx: &mut Bx,
299 variant_index: VariantIdx,
300 ) -> Self {
301 let mut downcast = *self;
302 downcast.layout = self.layout.for_variant(bx.cx(), variant_index);
303 downcast
304 }
305
306 pub fn project_type<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
307 &self,
308 bx: &mut Bx,
309 ty: Ty<'tcx>,
310 ) -> Self {
311 let mut downcast = *self;
312 downcast.layout = bx.cx().layout_of(ty);
313 downcast
314 }
315
316 pub fn storage_live<Bx: BuilderMethods<'a, 'tcx, Value = V>>(&self, bx: &mut Bx) {
317 bx.lifetime_start(self.val.llval, self.layout.size);
318 }
319
320 pub fn storage_dead<Bx: BuilderMethods<'a, 'tcx, Value = V>>(&self, bx: &mut Bx) {
321 bx.lifetime_end(self.val.llval, self.layout.size);
322 }
323
324 pub fn unaligned(self) -> Self {
326 let Self { val, layout } = self;
327 let val = PlaceValue { align: Align::ONE, ..val };
328 Self { val, layout }
329 }
330}
331
332impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
333 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("codegen_place",
"rustc_codegen_ssa::mir::place", ::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/mir/place.rs"),
::tracing_core::__macro_support::Option::Some(333u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::mir::place"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("place_ref")
}> =
::tracing::__macro_support::FieldName::new("place_ref");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&place_ref)
as &dyn ::tracing::field::Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: PlaceRef<'tcx, Bx::Value> =
loop {};
return __tracing_attr_fake_return;
}
{
let cx = self.cx;
let tcx = self.cx.tcx();
let mut base = 0;
let mut cg_base =
match self.locals[place_ref.local] {
LocalRef::Place(place) => place,
LocalRef::UnsizedPlace(place) =>
bx.load_operand(place).deref(cx),
LocalRef::Operand(..) => {
if place_ref.is_indirect_first_projection() {
base = 1;
let cg_base =
self.codegen_consume(bx,
mir::PlaceRef {
projection: &place_ref.projection[..0],
..place_ref
});
cg_base.deref(bx.cx())
} else {
::rustc_middle::util::bug::bug_fmt(format_args!("using operand local {0:?} as place",
place_ref));
}
}
LocalRef::PendingOperand => {
::rustc_middle::util::bug::bug_fmt(format_args!("using still-pending operand local {0:?} as place",
place_ref));
}
};
for elem in place_ref.projection[base..].iter() {
cg_base =
match *elem {
mir::ProjectionElem::Deref =>
bx.load_operand(cg_base).deref(bx.cx()),
mir::ProjectionElem::Field(ref field, _) => {
if !!cg_base.layout.ty.is_any_ptr() {
{
::core::panicking::panic_fmt(format_args!("Bad PlaceRef: destructing pointers should use cast/PtrMetadata, but tried to access field {0:?} of pointer {1:?}",
field, cg_base));
}
};
cg_base.project_field(bx, field.index())
}
mir::ProjectionElem::OpaqueCast(ty) => {
::rustc_middle::util::bug::bug_fmt(format_args!("encountered OpaqueCast({0}) in codegen",
ty))
}
mir::ProjectionElem::UnwrapUnsafeBinder(ty) => {
cg_base.project_type(bx, self.monomorphize(ty))
}
mir::ProjectionElem::Index(index) => {
let index = &mir::Operand::Copy(mir::Place::from(index));
let index = self.codegen_operand(bx, index);
let llindex = index.immediate();
cg_base.project_index(bx, llindex)
}
mir::ProjectionElem::ConstantIndex {
offset, from_end: false, min_length: _ } => {
let lloffset = bx.cx().const_usize(offset);
cg_base.project_index(bx, lloffset)
}
mir::ProjectionElem::ConstantIndex {
offset, from_end: true, min_length: _ } => {
let lloffset = bx.cx().const_usize(offset);
let lllen = cg_base.len(bx.cx());
let llindex = bx.sub(lllen, lloffset);
cg_base.project_index(bx, llindex)
}
mir::ProjectionElem::Subslice { from, to, from_end } => {
let mut subslice =
cg_base.project_index(bx, bx.cx().const_usize(from));
let projected_ty =
PlaceTy::from_ty(cg_base.layout.ty).projection_ty(tcx,
*elem).ty;
subslice.layout =
bx.cx().layout_of(self.monomorphize(projected_ty));
if subslice.layout.is_unsized() {
if !from_end {
{
::core::panicking::panic_fmt(format_args!("slice subslices should be `from_end`"));
}
};
subslice.val.llextra =
Some(bx.sub(cg_base.val.llextra.unwrap(),
bx.cx().const_usize(from + to)));
}
subslice
}
mir::ProjectionElem::Downcast(_, v) =>
cg_base.project_downcast(bx, v),
};
}
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/mir/place.rs:413",
"rustc_codegen_ssa::mir::place", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/mir/place.rs"),
::tracing_core::__macro_support::Option::Some(413u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::mir::place"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("codegen_place(place={0:?}) => {1:?}",
place_ref, cg_base) as &dyn ::tracing::field::Value))])
});
} else { ; }
};
cg_base
}
}
}#[instrument(level = "trace", skip(self, bx))]
334 pub fn codegen_place(
335 &mut self,
336 bx: &mut Bx,
337 place_ref: mir::PlaceRef<'tcx>,
338 ) -> PlaceRef<'tcx, Bx::Value> {
339 let cx = self.cx;
340 let tcx = self.cx.tcx();
341
342 let mut base = 0;
343 let mut cg_base = match self.locals[place_ref.local] {
344 LocalRef::Place(place) => place,
345 LocalRef::UnsizedPlace(place) => bx.load_operand(place).deref(cx),
346 LocalRef::Operand(..) => {
347 if place_ref.is_indirect_first_projection() {
348 base = 1;
349 let cg_base = self.codegen_consume(
350 bx,
351 mir::PlaceRef { projection: &place_ref.projection[..0], ..place_ref },
352 );
353 cg_base.deref(bx.cx())
354 } else {
355 bug!("using operand local {:?} as place", place_ref);
356 }
357 }
358 LocalRef::PendingOperand => {
359 bug!("using still-pending operand local {:?} as place", place_ref);
360 }
361 };
362 for elem in place_ref.projection[base..].iter() {
363 cg_base = match *elem {
364 mir::ProjectionElem::Deref => bx.load_operand(cg_base).deref(bx.cx()),
365 mir::ProjectionElem::Field(ref field, _) => {
366 assert!(
367 !cg_base.layout.ty.is_any_ptr(),
368 "Bad PlaceRef: destructing pointers should use cast/PtrMetadata, \
369 but tried to access field {field:?} of pointer {cg_base:?}",
370 );
371 cg_base.project_field(bx, field.index())
372 }
373 mir::ProjectionElem::OpaqueCast(ty) => {
374 bug!("encountered OpaqueCast({ty}) in codegen")
375 }
376 mir::ProjectionElem::UnwrapUnsafeBinder(ty) => {
377 cg_base.project_type(bx, self.monomorphize(ty))
378 }
379 mir::ProjectionElem::Index(index) => {
380 let index = &mir::Operand::Copy(mir::Place::from(index));
381 let index = self.codegen_operand(bx, index);
382 let llindex = index.immediate();
383 cg_base.project_index(bx, llindex)
384 }
385 mir::ProjectionElem::ConstantIndex { offset, from_end: false, min_length: _ } => {
386 let lloffset = bx.cx().const_usize(offset);
387 cg_base.project_index(bx, lloffset)
388 }
389 mir::ProjectionElem::ConstantIndex { offset, from_end: true, min_length: _ } => {
390 let lloffset = bx.cx().const_usize(offset);
391 let lllen = cg_base.len(bx.cx());
392 let llindex = bx.sub(lllen, lloffset);
393 cg_base.project_index(bx, llindex)
394 }
395 mir::ProjectionElem::Subslice { from, to, from_end } => {
396 let mut subslice = cg_base.project_index(bx, bx.cx().const_usize(from));
397 let projected_ty =
398 PlaceTy::from_ty(cg_base.layout.ty).projection_ty(tcx, *elem).ty;
399 subslice.layout = bx.cx().layout_of(self.monomorphize(projected_ty));
400
401 if subslice.layout.is_unsized() {
402 assert!(from_end, "slice subslices should be `from_end`");
403 subslice.val.llextra = Some(
404 bx.sub(cg_base.val.llextra.unwrap(), bx.cx().const_usize(from + to)),
405 );
406 }
407
408 subslice
409 }
410 mir::ProjectionElem::Downcast(_, v) => cg_base.project_downcast(bx, v),
411 };
412 }
413 debug!("codegen_place(place={:?}) => {:?}", place_ref, cg_base);
414 cg_base
415 }
416
417 pub fn monomorphized_place_ty(&self, place_ref: mir::PlaceRef<'tcx>) -> Ty<'tcx> {
418 let tcx = self.cx.tcx();
419 let place_ty = place_ref.ty(self.mir, tcx);
420 self.monomorphize(place_ty.ty)
421 }
422}
423
424fn round_up_const_value_to_alignment<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
425 bx: &mut Bx,
426 value: Bx::Value,
427 align: Bx::Value,
428) -> Bx::Value {
429 let one = bx.const_usize(1);
460 let align_minus_1 = bx.sub(align, one);
461 let neg_value = bx.neg(value);
462 let offset = bx.and(neg_value, align_minus_1);
463 bx.add(value, offset)
464}
465
466pub(super) fn codegen_tag_value<'tcx, V>(
476 cx: &impl CodegenMethods<'tcx, Value = V>,
477 variant_index: VariantIdx,
478 layout: TyAndLayout<'tcx>,
479) -> Result<Option<(FieldIdx, V)>, UninhabitedVariantError> {
480 if layout.is_variant_uninhabited(variant_index) {
483 return Err(UninhabitedVariantError);
484 }
485
486 Ok(match layout.variants {
487 Variants::Empty => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("we already handled uninhabited types")));
}unreachable!("we already handled uninhabited types"),
488 Variants::Single { index } => {
489 {
match (&index, &variant_index) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};assert_eq!(index, variant_index);
490 None
491 }
492
493 Variants::Multiple { tag_encoding: TagEncoding::Direct, tag_field, .. } => {
494 let discr = layout.ty.discriminant_for_variant(cx.tcx(), variant_index);
495 let to = discr.unwrap().val;
496 let tag_layout = layout.field(cx, tag_field.as_usize());
497 let tag_llty = cx.immediate_backend_type(tag_layout);
498 let imm = cx.const_uint_big(tag_llty, to);
499 Some((tag_field, imm))
500 }
501 Variants::Multiple {
502 tag_encoding: TagEncoding::Niche { untagged_variant, ref niche_variants, niche_start },
503 tag_field,
504 ..
505 } => {
506 if variant_index != untagged_variant {
507 let niche_layout = layout.field(cx, tag_field.as_usize());
508 let niche_llty = cx.immediate_backend_type(niche_layout);
509 let BackendRepr::Scalar(scalar) = niche_layout.backend_repr else {
510 ::rustc_middle::util::bug::bug_fmt(format_args!("expected a scalar placeref for the niche"));bug!("expected a scalar placeref for the niche");
511 };
512 let niche_value = variant_index.as_u32() - niche_variants.start.as_u32();
517 let niche_value = (niche_value as u128).wrapping_add(niche_start);
518 let niche_value = niche_value & niche_layout.size.unsigned_int_max();
519
520 let niche_llval = cx.scalar_to_backend(
521 Scalar::from_uint(niche_value, niche_layout.size),
522 scalar,
523 niche_llty,
524 );
525 Some((tag_field, niche_llval))
526 } else {
527 None
528 }
529 }
530 })
531}
532
533#[derive(#[automatically_derived]
impl ::core::fmt::Debug for UninhabitedVariantError {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f, "UninhabitedVariantError")
}
}Debug)]
534pub(super) struct UninhabitedVariantError;