rust-analyzer/crates/hir-ty/src/layout/adt.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

187 lines
6.7 KiB
Rust
Raw Normal View History

2022-10-23 08:12:05 +00:00
//! Compute the binary representation of structs, unions and enums
use std::{cmp, ops::Bound};
2022-10-23 08:12:05 +00:00
2023-12-07 09:57:51 +00:00
use base_db::salsa::Cycle;
2022-10-23 08:12:05 +00:00
use hir_def::{
data::adt::VariantData,
layout::{Integer, LayoutCalculator, ReprOptions, TargetDataLayout},
AdtId, VariantId,
2022-10-23 08:12:05 +00:00
};
2024-01-08 14:39:35 +00:00
use rustc_index::IndexVec;
use smallvec::SmallVec;
2023-05-25 21:15:37 +00:00
use triomphe::Arc;
2022-10-23 08:12:05 +00:00
use crate::{
db::HirDatabase,
lang_items::is_unsafe_cell,
layout::{field_ty, Layout, LayoutError, RustcEnumVariantIdx},
Substitution, TraitEnvironment,
};
2022-10-27 19:58:34 +00:00
2023-05-25 21:15:37 +00:00
use super::LayoutCx;
2022-10-23 08:12:05 +00:00
pub(crate) fn struct_variant_idx() -> RustcEnumVariantIdx {
RustcEnumVariantIdx(0)
2022-12-06 22:29:38 +00:00
}
2022-10-23 08:12:05 +00:00
pub fn layout_of_adt_query(
db: &dyn HirDatabase,
def: AdtId,
subst: Substitution,
trait_env: Arc<TraitEnvironment>,
2023-05-25 21:15:37 +00:00
) -> Result<Arc<Layout>, LayoutError> {
let krate = trait_env.krate;
let Ok(target) = db.target_data_layout(krate) else {
2023-07-03 18:34:09 +00:00
return Err(LayoutError::TargetLayoutNotAvailable);
};
let cx = LayoutCx { target: &target };
let dl = cx.current_data_layout();
2022-10-23 08:12:05 +00:00
let handle_variant = |def: VariantId, var: &VariantData| {
var.fields()
.iter()
.map(|(fd, _)| db.layout_of_ty(field_ty(db, def, fd, &subst), trait_env.clone()))
2022-10-23 08:12:05 +00:00
.collect::<Result<Vec<_>, _>>()
};
2023-05-15 09:59:03 +00:00
let (variants, repr) = match def {
2022-10-23 08:12:05 +00:00
AdtId::StructId(s) => {
let data = db.struct_data(s);
let mut r = SmallVec::<[_; 1]>::new();
2022-12-06 22:29:38 +00:00
r.push(handle_variant(s.into(), &data.variant_data)?);
2023-05-15 09:59:03 +00:00
(r, data.repr.unwrap_or_default())
2022-12-06 22:29:38 +00:00
}
AdtId::UnionId(id) => {
let data = db.union_data(id);
let mut r = SmallVec::new();
2022-12-06 22:29:38 +00:00
r.push(handle_variant(id.into(), &data.variant_data)?);
2023-05-15 09:59:03 +00:00
(r, data.repr.unwrap_or_default())
2022-10-23 08:12:05 +00:00
}
AdtId::EnumId(e) => {
let data = db.enum_data(e);
let r = data
.variants
.iter()
.map(|&(v, _)| handle_variant(v.into(), &db.enum_variant_data(v).variant_data))
.collect::<Result<SmallVec<_>, _>>()?;
2023-05-15 09:59:03 +00:00
(r, data.repr.unwrap_or_default())
2022-10-23 08:12:05 +00:00
}
};
2023-05-25 21:15:37 +00:00
let variants = variants
.iter()
2023-07-06 14:03:17 +00:00
.map(|it| it.iter().map(|it| &**it).collect::<Vec<_>>())
2023-05-25 21:15:37 +00:00
.collect::<SmallVec<[_; 1]>>();
2023-09-29 18:42:26 +00:00
let variants = variants.iter().map(|it| it.iter().collect()).collect::<IndexVec<_, _>>();
2023-05-25 21:15:37 +00:00
let result = if matches!(def, AdtId::UnionId(..)) {
cx.layout_of_union(&repr, &variants).ok_or(LayoutError::Unknown)?
2022-10-23 08:12:05 +00:00
} else {
2022-12-06 22:29:38 +00:00
cx.layout_of_struct_or_enum(
&repr,
&variants,
2023-05-15 09:59:03 +00:00
matches!(def, AdtId::EnumId(..)),
2023-03-30 07:37:52 +00:00
is_unsafe_cell(db, def),
2022-12-06 22:29:38 +00:00
layout_scalar_valid_range(db, def),
2024-01-18 12:59:49 +00:00
|min, max| repr_discr(dl, &repr, min, max).unwrap_or((Integer::I8, false)),
2022-12-06 22:29:38 +00:00
variants.iter_enumerated().filter_map(|(id, _)| {
let AdtId::EnumId(e) = def else { return None };
let d = db.const_eval_discriminant(db.enum_data(e).variants[id.0].0).ok()?;
2022-12-06 22:29:38 +00:00
Some((id, d))
}),
// FIXME: The current code for niche-filling relies on variant indices
// instead of actual discriminants, so enums with
// explicit discriminants (RFC #2363) would misbehave and we should disable
// niche optimization for them.
// The code that do it in rustc:
// repr.inhibit_enum_layout_opt() || def
// .variants()
// .iter_enumerated()
// .any(|(i, v)| v.discr != ty::VariantDiscr::Relative(i.as_u32()))
repr.inhibit_enum_layout_opt(),
2023-05-15 09:59:03 +00:00
!matches!(def, AdtId::EnumId(..))
2022-12-06 22:29:38 +00:00
&& variants
.iter()
.next()
2023-09-29 18:42:26 +00:00
.and_then(|it| it.iter().last().map(|it| !it.is_unsized()))
2022-12-06 22:29:38 +00:00
.unwrap_or(true),
)
2023-05-25 21:15:37 +00:00
.ok_or(LayoutError::SizeOverflow)?
};
Ok(Arc::new(result))
2022-10-23 08:12:05 +00:00
}
2022-10-27 19:58:34 +00:00
fn layout_scalar_valid_range(db: &dyn HirDatabase, def: AdtId) -> (Bound<u128>, Bound<u128>) {
let attrs = db.attrs(def.into());
let get = |name| {
let attr = attrs.by_key(name).tt_values();
for tree in attr {
2023-07-06 14:03:17 +00:00
if let Some(it) = tree.token_trees.first() {
let text = it.to_string().replace('_', "");
let (text, base) = match text.as_bytes() {
[b'0', b'x', ..] => (&text[2..], 16),
[b'0', b'o', ..] => (&text[2..], 8),
[b'0', b'b', ..] => (&text[2..], 2),
_ => (&*text, 10),
};
if let Ok(it) = u128::from_str_radix(text, base) {
2023-07-06 14:03:17 +00:00
return Bound::Included(it);
2022-10-27 19:58:34 +00:00
}
}
}
Bound::Unbounded
};
(get("rustc_layout_scalar_valid_range_start"), get("rustc_layout_scalar_valid_range_end"))
}
2022-10-23 08:12:05 +00:00
pub fn layout_of_adt_recover(
_: &dyn HirDatabase,
2023-12-07 09:57:51 +00:00
_: &Cycle,
2022-10-23 08:12:05 +00:00
_: &AdtId,
_: &Substitution,
_: &Arc<TraitEnvironment>,
2023-05-25 21:15:37 +00:00
) -> Result<Arc<Layout>, LayoutError> {
2023-11-14 12:32:04 +00:00
Err(LayoutError::RecursiveTypeWithoutIndirection)
2022-10-23 08:12:05 +00:00
}
/// Finds the appropriate Integer type and signedness for the given
/// signed discriminant range and `#[repr]` attribute.
/// N.B.: `u128` values above `i128::MAX` will be treated as signed, but
/// that shouldn't affect anything, other than maybe debuginfo.
fn repr_discr(
dl: &TargetDataLayout,
repr: &ReprOptions,
min: i128,
max: i128,
) -> Result<(Integer, bool), LayoutError> {
// Theoretically, negative values could be larger in unsigned representation
// than the unsigned representation of the signed minimum. However, if there
// are any negative values, the only valid unsigned representation is u128
// which can fit all i128 values, so the result remains unaffected.
let unsigned_fit = Integer::fit_unsigned(cmp::max(min as u128, max as u128));
let signed_fit = cmp::max(Integer::fit_signed(min), Integer::fit_signed(max));
if let Some(ity) = repr.int {
let discr = Integer::from_attr(dl, ity);
let fit = if ity.is_signed() { signed_fit } else { unsigned_fit };
if discr < fit {
2023-11-14 12:32:04 +00:00
return Err(LayoutError::UserReprTooSmall);
}
return Ok((discr, ity.is_signed()));
}
let at_least = if repr.c() {
// This is usually I32, however it can be different on some platforms,
// notably hexagon and arm-none/thumb-none
dl.c_enum_min_size
} else {
// repr(Rust) enums try to be as small as possible
Integer::I8
};
// If there are no negative values, we can use the unsigned fit.
Ok(if min >= 0 {
(cmp::max(unsigned_fit, at_least), false)
} else {
(cmp::max(signed_fit, at_least), true)
})
}