Lines
100 %
Functions
Branches
//! The source a closure keeps for the host to carry on (PERSIST P12).
use crate::ast::{Expr, LambdaParams};
use crate::compiler::context::closure::EnvField;
use crate::compiler::special::{calls_form_bound, expanded_function};
use crate::error::Result;
use crate::runtime::{ClosureSource, SymbolTable};
use super::constants::reads_unbound_constant;
use super::emit::CallingConvention;
/// The source of the closure a host carries on to the next unit as a lambda,
/// its body macro-expanded now so a later macro redefinition cannot change it:
/// none when its environment shares a cell, its body holds a resolved runtime
/// variable, calls a function that does not outlive the unit, or reads a
/// lexical constant that is not plain data.
pub(super) fn closure_source(
symbols: &SymbolTable,
(params, body): (&LambdaParams, &Expr),
env_fields: &[EnvField],
cc: CallingConvention,
) -> Result<Option<ClosureSource>> {
let (params, body) = expanded_function(symbols, None, params, body)?;
let carryable = cc == CallingConvention::Typed
&& env_fields.iter().all(|field| field.cell.is_none())
&& !body.any_form(Expr::is_runtime_variable)
&& !calls_form_bound(symbols, &body)
&& !reads_unbound_constant(symbols, ¶ms, &body);
let fields = env_fields
.iter()
.map(|field| (field.name.clone(), field.ty))
.collect();
Ok(carryable.then(|| ClosureSource::new(¶ms, &body, fields)))
}