1
//! The one precedence order for a symbol in head position.
2

            
3
use crate::ast::{ClosureSigId, Expr, LambdaParams, Place, WasmType};
4

            
5
use super::entry::{Symbol, SymbolKind};
6
use super::table::SymbolTable;
7

            
8
/// What a symbol in head position means. Every compiler surface classifies a
9
/// call head through [`SymbolTable::resolve_head`] and never re-spells the
10
/// precedence.
11
#[derive(Debug, Clone, Copy)]
12
pub(crate) enum Head<'a> {
13
    SpecialForm,
14
    /// A native, operator or host-fn stand-in.
15
    Native,
16
    /// Kind `Macro` with a lambda, whichever definer stored the lambda.
17
    Macro(&'a LambdaParams, &'a Expr),
18
    /// A user function: a `defun`, `labels` or `defstruct`.
19
    Function(&'a LambdaParams, &'a Expr),
20
    /// Undefined, or without a callable binding.
21
    Unknown,
22
}
23

            
24
impl Head<'_> {
25
    /// A native, operator or special form: no function or macro has taken the
26
    /// name over (a host fn's stand-in can still be taken).
27
2920986
    pub(crate) fn is_builtin(self) -> bool {
28
2920986
        matches!(self, Head::Native | Head::SpecialForm)
29
2920986
    }
30
}
31

            
32
impl Symbol {
33
    /// The closure held in the value cell, whatever the function cell holds.
34
17770
    pub(crate) fn closure_value(&self) -> Option<(Place, ClosureSigId)> {
35
17770
        match self.value().and_then(Expr::place) {
36
14177
            Some((place, WasmType::Closure(sig))) => Some((place, sig)),
37
3593
            _ => None,
38
        }
39
17770
    }
40

            
41
    /// Only the function cell makes a head; a variable's value never does.
42
411253296
    pub(crate) fn head(&self) -> Head<'_> {
43
411253296
        let Some(function) = &self.function else {
44
26066
            return Head::Unknown;
45
        };
46
411227230
        match (&function.lambda, function.kind) {
47
1869057
            (Some(Expr::Lambda(params, body)), SymbolKind::Macro) => Head::Macro(params, body),
48
193954897
            (Some(Expr::Lambda(params, body)), _) => Head::Function(params, body),
49
128275955
            (_, SymbolKind::Native | SymbolKind::Operator) => Head::Native,
50
87104972
            (_, SymbolKind::SpecialForm) => Head::SpecialForm,
51
22349
            _ => Head::Unknown,
52
        }
53
411253296
    }
54
}
55

            
56
impl SymbolTable {
57
    /// Whether a body naming `name` would change meaning when carried on to a
58
    /// later unit as source: a LABELS function exists only inside its form,
59
    /// and a user macro use that could not be expanded expands again later.
60
171836
    pub(crate) fn is_form_bound(&self, name: &str) -> bool {
61
171836
        Self::local_alias_origin(name).is_some()
62
170581
            || self.lookup(name).is_some_and(|symbol| {
63
170578
                symbol.is_local_function()
64
170370
                    || (symbol.kind() == SymbolKind::Macro
65
423
                        && !crate::reserved::is_builtin_name(self, name))
66
170578
            })
67
171836
    }
68

            
69
411779611
    pub(crate) fn resolve_head(&self, name: &str) -> Head<'_> {
70
411779611
        self.lookup(name).map_or(Head::Unknown, Symbol::head)
71
411779611
    }
72
}