Lines
100 %
Functions
77.78 %
Branches
//! The homes of struct fields: every instance of a layout shares one home per
//! field, typed like a global's home by the join of every store into it.
use std::collections::btree_map::Entry;
use std::fmt;
use super::{Home, HomeKey, locked};
use crate::ast::{StructId, WasmType};
use crate::compiler::join::{Arm, reconcile_arms};
use crate::runtime::SymbolTable;
impl fmt::Display for HomeKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Global(name) => write!(f, "global {name}"),
Self::Field(id, idx) => write!(f, "field {idx} of {id}"),
Self::Binder(_, _, var, _) => write!(f, "variable {var}"),
}
impl SymbolTable {
/// The type a read of field `idx` of `id` unboxes to: the home's type this
/// attempt, opening it from the previous attempt's hint, else provisionally
/// `AnyRef` for the first store to type.
pub(crate) fn field_type(&self, id: StructId, idx: u16) -> WasmType {
let key = HomeKey::Field(id, idx);
let mut book = locked(&self.homes);
let hint = book.hints.get(&key).copied();
book.homes
.entry(key)
.or_insert_with(|| match hint {
Some(ty) => Home::typed(vec![Arm::Value(ty)], None, ty),
None => Home::provisional(),
})
.ty
/// Records a store of `arm` into field `idx` of `id`. The first store a
/// home sees types it, over the previous attempt's hint. A closure rides the
/// slot as an `AnyRef`, which no read casts.
pub(crate) fn note_field_store(&self, id: StructId, idx: u16, arm: Arm) {
let arm = match arm {
Arm::Value(WasmType::Closure(_)) => Arm::Value(WasmType::AnyRef),
other => other,
};
match book.homes.entry(key) {
Entry::Occupied(home) => home.into_mut().arms.push(arm),
Entry::Vacant(slot) => {
let base: Vec<Arm> = hint.map(Arm::Value).into_iter().chain([arm]).collect();
let ty = reconcile_arms(&base);
slot.insert(Home::typed(base, None, ty));
/// How many field and binder homes this attempt opened, each of which may
/// widen.
#[must_use]
pub(crate) fn site_home_count(&self) -> usize {
locked(&self.homes)
.homes
.keys()
.filter(|key| !matches!(key, HomeKey::Global(_)))
.count()
/// A home of this attempt narrower than the join of its stores.
pub(crate) fn unsettled_home(&self) -> Option<HomeKey> {
.iter()
.find(|(_, home)| !home.arms.is_empty() && !home.holds_every_store())
.map(|(key, _)| key.clone())