Lines
99.14 %
Functions
91.94 %
Branches
100 %
//! The names a parameter scan sees: the enclosing scope's variables, minus the
//! names the lambda's own parameters and inner binders hide.
use super::pinned_param_type;
use crate::ast::{ClosureSigId, Expr, LambdaParams, WasmType};
use crate::compiler::expr::{classify_stack_type, probe_value};
use crate::compiler::special::binding::parse_param_list;
use crate::compiler::special::binding::shared::mentions;
use crate::error::Result;
use crate::runtime::{SymbolTable, with_sufficient_stack};
/// What an inner binder binds a name to, as far as the scan can tell.
#[derive(Clone)]
pub(super) enum Bound {
Kind(WasmType),
/// A lambda: the pins of its parameters.
Closure(Vec<Option<WasmType>>),
}
pub(super) struct Scope<'a> {
symbols: &'a SymbolTable,
hidden: Vec<String>,
bound: Vec<(String, Bound)>,
fn numeric(ty: WasmType) -> Option<WasmType> {
matches!(ty, WasmType::I32 | WasmType::Ratio | WasmType::Commodity).then_some(ty)
impl<'a> Scope<'a> {
pub(super) fn new(symbols: &'a SymbolTable, hidden: &[String]) -> Self {
Self {
symbols,
hidden: hidden.to_vec(),
bound: Vec::new(),
pub(super) fn hiding<'n>(&self, names: impl IntoIterator<Item = &'n str>) -> Self {
let mut scope = self.clone();
for name in names {
scope.hidden.push(name.to_string());
scope.bound.retain(|(bound, _)| bound != name);
scope
/// The scope inside a binder of `name` to `value`, when the scan knows what it is.
pub(super) fn binding(&self, name: &str, value: Option<Bound>) -> Self {
let mut scope = self.hiding([name]);
.bound
.extend(value.map(|value| (name.to_string(), value)));
/// What `form` denotes: a variable's binding, a lambda, a closure or a numeric value.
pub(super) fn value_of(&self, form: &Expr, name: &str) -> Option<Bound> {
match form {
Expr::Symbol(variable) if variable == name => None,
Expr::Symbol(variable) => self.variable(variable),
Expr::Number(_) => classify_stack_type(form).map(Bound::Kind),
Expr::Lambda(..) | Expr::List(_) => {
if let Some(pins) = self.closure_pins(form, name) {
return Some(Bound::Closure(pins));
match self.result_type(form, name)? {
WasmType::Closure(sig) => self.recorded(sig),
ty => numeric(ty).map(Bound::Kind),
_ => None,
/// The parameter pins of the lambda `init` denotes: a lambda form, or what
/// a call not mentioning `name` returns.
fn closure_pins(&self, init: &Expr, name: &str) -> Option<Vec<Option<WasmType>>> {
if let Some(params) = self.lambda_form(init) {
let (params, body) = params;
let inner = self.hiding(params.required.iter().map(String::as_str));
return Some(
params
.required
.iter()
.map(|param| pinned_param_type(&body, param, &inner))
.collect(),
);
if matches!(init, Expr::Symbol(_)) || mentions(init, name) {
return None;
self.deeper(|scratch| {
let Ok(probed) = with_sufficient_stack(|| probe_value(scratch, init)) else {
return Ok(None);
};
let scope = Scope {
symbols: scratch,
..self.clone()
Ok(scope
.lambda_form(&probed)
.and_then(|_| scope.closure_pins(&probed, name)))
})
/// `run` on a scratch table one level deeper in the shared recursion budget.
fn deeper<T>(&self, run: impl FnOnce(&mut SymbolTable) -> Result<Option<T>>) -> Option<T> {
self.symbols
.clone()
.within_shared_recursion(run)
.ok()
.flatten()
fn lambda_form(&self, form: &Expr) -> Option<(LambdaParams, Expr)> {
Expr::Lambda(params, body) => Some((params.clone(), body.as_ref().clone())),
Expr::List(elems) => match elems.as_slice() {
[Expr::Symbol(head), names, body @ ..] if head.eq_ignore_ascii_case("LAMBDA") => {
let required = parse_param_list(self.symbols, "LAMBDA", names).ok()?;
Some((LambdaParams::simple(required), Expr::body(body)))
},
fn bound(&self, name: &str) -> Option<&Bound> {
let (_, value) = self.bound.iter().rev().find(|(bound, _)| bound == name)?;
Some(value)
fn is_hidden(&self, name: &str) -> bool {
self.hidden.iter().any(|hidden| hidden == name)
/// The type the closure `callee` takes as its argument number `index`.
pub(super) fn callee_param(&self, callee: &Expr, name: &str, index: usize) -> Option<WasmType> {
match self.value_of(callee, name)? {
Bound::Closure(pins) => pins.get(index).copied().flatten(),
Bound::Kind(_) => None,
fn recorded(&self, sig: ClosureSigId) -> Option<Bound> {
let params = self.symbols.closure_params(sig)?;
Some(Bound::Closure(params.iter().map(|&ty| Some(ty)).collect()))
/// What the variable `name` is bound to: by an inner binder, or in the enclosing scope.
fn variable(&self, name: &str) -> Option<Bound> {
if let Some(bound) = self.bound(name) {
return Some(bound.clone());
if self.is_hidden(name) {
let value = self.symbols.lookup(name)?.value()?;
if let Some((_, WasmType::Closure(sig))) = value.place() {
return self.recorded(sig);
classify_stack_type(value)
.and_then(numeric)
.map(Bound::Kind)
/// The type `form` evaluates to, when it mentions neither `name` nor a hidden name.
fn result_type(&self, form: &Expr, name: &str) -> Option<WasmType> {
if mentions(form, name) || self.hidden.iter().any(|hidden| mentions(form, hidden)) {
let probed = with_sufficient_stack(|| probe_value(&mut self.symbols.clone(), form));
classify_stack_type(&probed.ok()?)