Lines
100 %
Functions
97.73 %
Branches
//! The session's struct instances (rstruct S5): a global that holds a struct
//! carries an [`Expr::Instance`] leaf, and the instance's field constants live
//! here, so aliasing, mutation through an unwritten global and cycles survive
//! from one unit to the next.
use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet};
use crate::ast::{Expr, StructId};
use super::carry::Carried;
use super::table::SymbolTable;
/// A live instance: its layout and the constant of each field.
#[derive(Clone, Debug, PartialEq)]
pub struct StructInstance {
pub layout: StructId,
pub fields: Vec<Expr>,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct InstanceHeap {
instances: BTreeMap<u32, StructInstance>,
next_serial: u32,
/// What a host read of one instance after a unit: its fields, or `None` when
/// it holds a value no constant holds (a closure, an entity, compound money).
pub type Found = BTreeMap<u32, Option<StructInstance>>;
/// The serials of the instances `expr` mentions.
fn mentioned(expr: &Expr) -> Vec<u32> {
let found = RefCell::new(Vec::new());
expr.any_form(|form| {
if let Expr::Instance(serial, _) = form {
found.borrow_mut().push(*serial);
false
});
found.into_inner()
impl SymbolTable {
/// The serial the next unit's first new instance takes.
#[must_use]
pub fn next_serial(&self) -> u32 {
self.instances.next_serial
/// The live instances, by serial.
pub fn instances(&self) -> impl Iterator<Item = (u32, &StructInstance)> {
self.instances
.instances
.iter()
.map(|(serial, instance)| (*serial, instance))
/// Replaces the heap with what a host read after a unit. An instance that
/// is opaque, or reaches one or one never read, is dropped and so is every
/// global that reaches it, loud when a later unit reads it; an instance no
/// global or carried closure reaches is swept. The serial counter never
/// goes back.
pub fn settle_instances(&mut self, found: Found, next_serial: u32) {
let usable = usable(found);
self.demote_unreachable(&usable);
self.instances.instances = self.swept(usable);
self.instances.next_serial = self.instances.next_serial.max(next_serial);
fn global_values(&self) -> impl Iterator<Item = (&String, &Expr)> {
self.symbols
.filter_map(|(name, symbol)| symbol.value().map(|value| (name, value)))
fn demote_unreachable(&mut self, usable: &BTreeMap<u32, StructInstance>) {
let lost: Vec<(String, &'static str)> = self
.global_values()
.filter(|(_, value)| {
mentioned(value)
.any(|serial| !usable.contains_key(serial))
})
.map(|(name, value)| match value {
Expr::Lambda(..) => (name.clone(), "closure"),
_ => (name.clone(), "struct"),
.collect();
for (name, type_name) in lost {
self.carry(&name, Carried::Opaque(type_name.to_string()));
fn swept(&self, mut usable: BTreeMap<u32, StructInstance>) -> BTreeMap<u32, StructInstance> {
let mut marked = BTreeSet::new();
let mut pending: Vec<u32> = self
.flat_map(|(_, value)| mentioned(value))
while let Some(serial) = pending.pop() {
if marked.insert(serial)
&& let Some(instance) = usable.get(&serial)
{
pending.extend(instance.fields.iter().flat_map(mentioned));
usable.retain(|serial, _| marked.contains(serial));
usable
/// The instances every field of which is a constant of live instances.
fn usable(found: Found) -> BTreeMap<u32, StructInstance> {
let mut usable: BTreeMap<u32, StructInstance> = found
.into_iter()
.filter_map(|(serial, instance)| instance.map(|instance| (serial, instance)))
loop {
let dangling: Vec<u32> = usable
.filter(|(_, instance)| {
instance
.fields
.flat_map(mentioned)
.any(|serial| !usable.contains_key(&serial))
.map(|(serial, _)| *serial)
if dangling.is_empty() {
return usable;
for serial in dangling {
usable.remove(&serial);
#[cfg(test)]
#[path = "instances_tests.rs"]
mod tests;