Lines
96.77 %
Functions
100 %
Branches
//! The local type of a DO variable, sized for both surfaces.
use crate::ast::{Expr, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::{classify_stack_type, eval_value};
use crate::compiler::recompile::compiled_in_trial;
use crate::error::Result;
use crate::runtime::SymbolTable;
use super::super::setf_scan::{setf_rhs_of, step_pair_element};
/// The form that sizes a DO variable: its step, else the first body store into it.
pub(in crate::compiler::special) fn var_type_hint<'a>(
step: Option<&'a Expr>,
body: &'a [Expr],
name: &str,
) -> Option<&'a Expr> {
step.or_else(|| setf_rhs_of(body, name).into_iter().next())
}
/// The form that yields a step's value: the last form of a `PROGN`, the
/// stored value of the last pair of a `SETF`/`SETQ`.
fn step_value_form(step: &Expr) -> &Expr {
let Expr::List(elems) = step else {
return step;
};
let Some(Expr::Symbol(head)) = elems.first() else {
match (head.to_ascii_uppercase().as_str(), &elems[1..]) {
("PROGN", [.., last]) => step_value_form(last),
("SETF" | "SETQ", [pairs @ .., value]) if pairs.len() % 2 == 1 => step_value_form(value),
_ => step,
/// What the eval surface learns of a nil-init DO variable's step.
enum StepFact {
/// No exit form was reached: the step's value types as codegen pushes it.
Typed(Option<WasmType>),
/// The step reached an exit form or the evaluator rejected it; only
/// compiling it tells whether it falls through and what it pushes.
Exits,
fn step_fact(symbols: &SymbolTable, step: &Expr) -> StepFact {
let exits = symbols.measurements().exits();
match eval_value(&mut symbols.speculative_clone(), step) {
Ok(value) if symbols.measurements().exits() == exits => {
StepFact::Typed(classify_stack_type(&value))
_ => StepFact::Exits,
/// What every other init is sized by: the shared classifier, with Bytes as
/// StringRef and `I32` for forms it cannot type (their own compile declares the
/// real type, so a mismatch is an emit error).
fn init_type(expr: &Expr) -> WasmType {
match expr {
Expr::Bytes(_) => WasmType::StringRef,
_ => classify_stack_type(expr).unwrap_or(WasmType::I32),
/// How a DO variable is sized, decided once for both surfaces.
enum Sizing<'a> {
Known(WasmType),
/// A step that reaches an exit form: only compiling it tells the type,
/// else `fallback`.
Compile {
step: &'a Expr,
fallback: WasmType,
},
fn sizing<'a>(expr: &Expr, step_hint: Option<&'a Expr>, symbols: &SymbolTable) -> Sizing<'a> {
let fallback = init_type(expr);
let nil_step = matches!(expr, Expr::Nil).then_some(step_hint).flatten();
let Some(step) = nil_step.map(step_value_form) else {
return Sizing::Known(fallback);
// A `(cons V acc)` step is sized from the car's element, which the shared
// classifier (typing a bare nil as `Bool`) cannot see.
if let Some(elem) = step_pair_element(step, symbols) {
return Sizing::Known(WasmType::PairRef(elem));
match step_fact(symbols, step) {
StepFact::Typed(ty) => Sizing::Known(ty.unwrap_or(fallback)),
StepFact::Exits => Sizing::Compile { step, fallback },
/// The local type of a DO variable the eval surface sizes; `None` when only
/// codegen can tell, so the loop's own type is left to it.
pub(in crate::compiler::special) fn infer_wasm_type(
expr: &Expr,
step_hint: Option<&Expr>,
symbols: &SymbolTable,
) -> Option<WasmType> {
match sizing(expr, step_hint, symbols) {
Sizing::Known(ty) => Some(ty),
Sizing::Compile { .. } => None,
/// The local type of a DO variable codegen sizes.
pub(in crate::compiler::special) fn infer_compiled_type(
ctx: &mut CompileContext,
emit: &FunctionEmitter,
symbols: &mut SymbolTable,
) -> Result<WasmType> {
Sizing::Known(ty) => Ok(ty),
Sizing::Compile { step, fallback } => {
let ty = compiled_in_trial(ctx, symbols, emit, step, |_, ty| Ok(ty))?;
Ok(ty.flatten().unwrap_or(fallback))