Lines
100 %
Functions
Branches
//! The one precedence order for a symbol in head position.
use crate::ast::{ClosureSigId, Expr, LambdaParams, Place, WasmType};
use super::entry::{Symbol, SymbolKind};
use super::table::SymbolTable;
/// What a symbol in head position means. Every compiler surface classifies a
/// call head through [`SymbolTable::resolve_head`] and never re-spells the
/// precedence.
#[derive(Debug, Clone, Copy)]
pub(crate) enum Head<'a> {
SpecialForm,
/// A native, operator or host-fn stand-in.
Native,
/// Kind `Macro` with a lambda, whichever definer stored the lambda.
Macro(&'a LambdaParams, &'a Expr),
/// A user function: a `defun`, `labels` or `defstruct`.
Function(&'a LambdaParams, &'a Expr),
/// Undefined, or without a callable binding.
Unknown,
}
impl Head<'_> {
/// A native, operator or special form: no function or macro has taken the
/// name over (a host fn's stand-in can still be taken).
pub(crate) fn is_builtin(self) -> bool {
matches!(self, Head::Native | Head::SpecialForm)
impl Symbol {
/// The closure held in the value cell, whatever the function cell holds.
pub(crate) fn closure_value(&self) -> Option<(Place, ClosureSigId)> {
match self.value().and_then(Expr::place) {
Some((place, WasmType::Closure(sig))) => Some((place, sig)),
_ => None,
/// Only the function cell makes a head; a variable's value never does.
pub(crate) fn head(&self) -> Head<'_> {
let Some(function) = &self.function else {
return Head::Unknown;
};
match (&function.lambda, function.kind) {
(Some(Expr::Lambda(params, body)), SymbolKind::Macro) => Head::Macro(params, body),
(Some(Expr::Lambda(params, body)), _) => Head::Function(params, body),
(_, SymbolKind::Native | SymbolKind::Operator) => Head::Native,
(_, SymbolKind::SpecialForm) => Head::SpecialForm,
_ => Head::Unknown,
impl SymbolTable {
/// Whether a body naming `name` would change meaning when carried on to a
/// later unit as source: a LABELS function exists only inside its form,
/// and a user macro use that could not be expanded expands again later.
pub(crate) fn is_form_bound(&self, name: &str) -> bool {
Self::local_alias_origin(name).is_some()
|| self.lookup(name).is_some_and(|symbol| {
symbol.is_local_function()
|| (symbol.kind() == SymbolKind::Macro
&& !crate::reserved::is_builtin_name(self, name))
})
pub(crate) fn resolve_head(&self, name: &str) -> Head<'_> {
self.lookup(name).map_or(Head::Unknown, Symbol::head)