Begin generating semantic reprs for records

This commit is contained in:
Ayaz Hafiz 2023-05-10 15:38:25 -05:00
parent 6714a6fd92
commit c06ffc434b
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
4 changed files with 40 additions and 12 deletions

View file

@ -663,7 +663,7 @@ impl FieldOrderHash {
const ZERO_FIELD_HASH: Self = Self(0);
const IRRELEVANT_NON_ZERO_FIELD_HASH: Self = Self(1);
pub fn from_ordered_fields(fields: &[&Lowercase]) -> Self {
pub fn from_ordered_fields(fields: &[&str]) -> Self {
if fields.is_empty() {
// HACK: we must make sure this is always equivalent to a `ZERO_FIELD_HASH`.
return Self::ZERO_FIELD_HASH;
@ -3262,8 +3262,12 @@ fn layout_from_flat_type<'a>(
)
});
let ordered_field_names =
Vec::from_iter_in(sortables.iter().map(|(label, _)| *label), arena);
let ordered_field_names = Vec::from_iter_in(
sortables
.iter()
.map(|(label, _)| &*arena.alloc_str(label.as_str())),
arena,
);
let field_order_hash =
FieldOrderHash::from_ordered_fields(ordered_field_names.as_slice());
@ -3278,7 +3282,7 @@ fn layout_from_flat_type<'a>(
field_order_hash,
field_layouts: layouts.into_bump_slice(),
},
semantic: SemanticRepr::None,
semantic: SemanticRepr::record(ordered_field_names.into_bump_slice()),
};
Ok(env.cache.put_in(struct_layout))

View file

@ -127,7 +127,7 @@ impl<'a> Layout<'a> {
field_layouts: &[],
field_order_hash: FieldOrderHash::ZERO_FIELD_HASH,
},
semantic: SemanticRepr::None,
semantic: SemanticRepr::EMPTY_RECORD,
};
pub const fn float_width(w: FloatWidth) -> InLayout<'static> {

View file

@ -0,0 +1,24 @@
//! Semantic representations of memory layouts for the purposes of specialization.
/// A semantic representation of a memory layout.
/// Semantic representations describe the shape of a type a [Layout][super::Layout] is generated
/// for. Semantic representations disambiguate types that have the same runtime memory layout, but
/// different shapes.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum SemanticRepr<'a> {
None,
Record(SemaRecord<'a>),
}
impl<'a> SemanticRepr<'a> {
pub(super) const EMPTY_RECORD: Self = Self::Record(SemaRecord { fields: &[] });
pub(super) fn record(fields: &'a [&'a str]) -> Self {
Self::Record(SemaRecord { fields })
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct SemaRecord<'a> {
pub fields: &'a [&'a str],
}