1
//! The user functions and macros a walked body reaches.
2

            
3
use super::{CaptureSet, walk, walk_lambda};
4
use crate::ast::Expr;
5
use crate::compiler::expr::fold::template::expand_macro_use;
6
use crate::runtime::{Head, MAX_SHARED_RECURSION_DEPTH, SymbolTable};
7

            
8
/// A macro's expansion is walked in place of its use, unless a LABELS function
9
/// of its name hides it; a use that cannot expand may store to anything the
10
/// closure captures.
11
7425003
pub(super) fn follow_macro(
12
7425003
    symbols: &SymbolTable,
13
7425003
    bound: &[String],
14
7425003
    head: &str,
15
7425003
    args: &[Expr],
16
7425003
    captures: &mut CaptureSet,
17
7425003
) {
18
7425003
    if !matches!(symbols.resolve_head(head), Head::Macro(..))
19
5051
        || bound.contains(&local_function_key(head))
20
    {
21
7420381
        return;
22
4622
    }
23
4622
    if captures.depth >= MAX_SHARED_RECURSION_DEPTH {
24
        captures.refuse(SymbolTable::recursion_ceiling_error());
25
        return;
26
4622
    }
27
4622
    let mut scratch = symbols.clone();
28
4622
    let expanded = scratch
29
4622
        .enter_macro_expansion()
30
4622
        .and_then(|()| expand_macro_use(&mut scratch, head, args));
31
4622
    match expanded {
32
        Ok(Some(expansion)) => {
33
4619
            captures.depth += 1;
34
4619
            walk(&scratch, bound, &expansion, captures);
35
4619
            captures.depth -= 1;
36
        }
37
3
        Ok(None) => captures.opaque_store = true,
38
        Err(error) => captures.refuse(error),
39
    }
40
7425003
}
41

            
42
/// A call to a user function is inlined into the closure body, so the free
43
/// names of the function's own body are the closure's too.
44
7429828
pub(super) fn follow_function(
45
7429828
    symbols: &SymbolTable,
46
7429828
    bound: &[String],
47
7429828
    head: &str,
48
7429828
    captures: &mut CaptureSet,
49
7429828
) {
50
7429828
    if !bound.contains(&local_function_key(head)) {
51
7428754
        follow_global(symbols, bound, head, captures);
52
1074
    }
53
7429828
}
54

            
55
7473503
fn follow_global(symbols: &SymbolTable, bound: &[String], head: &str, captures: &mut CaptureSet) {
56
7473503
    let Head::Function(params, body) = symbols.resolve_head(head) else {
57
5151884
        return;
58
    };
59
2321619
    if captures.followed.iter().any(|name| name == head) {
60
1133362
        return;
61
1188257
    }
62
1188257
    if captures.depth >= MAX_SHARED_RECURSION_DEPTH {
63
833
        captures.refuse(SymbolTable::recursion_ceiling_error());
64
833
        return;
65
1187424
    }
66
1187424
    captures.followed.push(head.to_string());
67
1187424
    captures.depth += 1;
68
1187424
    walk_lambda(symbols, bound, params, body, captures);
69
1187424
    captures.depth -= 1;
70
7473503
}
71

            
72
/// The scope entry that marks `name` as a LABELS function: a lexical function
73
/// shadows a global one only for a call or `#'name`, never for `'name`.
74
7436577
pub(super) fn local_function_key(name: &str) -> String {
75
7436577
    format!("#'{name}")
76
7436577
}
77

            
78
/// `#'g` names a call made later; it sees a LABELS function before a global.
79
4825
pub(super) fn follow_designator(
80
4825
    symbols: &SymbolTable,
81
4825
    bound: &[String],
82
4825
    operand: Option<&Expr>,
83
4825
    captures: &mut CaptureSet,
84
4825
) {
85
4825
    if let Some(Expr::Symbol(name)) = operand {
86
4825
        follow_function(symbols, bound, &name.to_uppercase(), captures);
87
    }
88
4825
}
89

            
90
/// A variable whose compile-time value names a global function calls it when
91
/// the closure funcalls the variable.
92
1281453
pub(super) fn follow_value(
93
1281453
    symbols: &SymbolTable,
94
1281453
    bound: &[String],
95
1281453
    value: &Expr,
96
1281453
    captures: &mut CaptureSet,
97
1281453
) {
98
1281453
    let mut pending = vec![value];
99
9211449
    while let Some(form) = pending.pop() {
100
7929996
        match form {
101
44749
            Expr::Symbol(name) => follow_global(symbols, bound, &name.to_uppercase(), captures),
102
6515624
            Expr::List(items) => pending.extend(items),
103
1
            Expr::Cons(car, cdr) => pending.extend([car.as_ref(), cdr.as_ref()]),
104
            Expr::Quote(inner) | Expr::Datum(inner) | Expr::Lambda(_, inner) => {
105
113534
                pending.push(inner);
106
            }
107
1256088
            _ => {}
108
        }
109
    }
110
1281453
}