1
//! A global function defined under a LABELS function of its name.
2

            
3
use crate::ast::Expr;
4

            
5
use super::entry::{Function, Symbol, SymbolKind};
6
use super::scope::Shadowed;
7
use super::table::SymbolTable;
8

            
9
impl SymbolTable {
10
    /// Whether a global function can hide under an alias a quoted designator
11
    /// names.
12
36667
    pub(super) fn hideable(global: &Function) -> bool {
13
36667
        !global.local
14
32691
            && global.kind != SymbolKind::Macro
15
18468
            && matches!(global.lambda, Some(Expr::Lambda(..)))
16
36667
    }
17

            
18
    /// The function the outermost open frame puts back for `name`.
19
6863401
    pub(super) fn restored_function(&self, name: &str) -> Option<&Option<Function>> {
20
6863401
        self.scopes
21
6863401
            .iter()
22
6863401
            .flatten()
23
215065
            .find_map(|(saved, shadowed)| match shadowed {
24
11871
                Shadowed::Function(prior) if saved == name => Some(prior),
25
209440
                _ => None,
26
215065
            })
27
6863401
    }
28

            
29
    /// Makes `cell` the function the outermost open frame puts back for
30
    /// `name`: a global function defined under a LABELS function of its name
31
    /// is visible once the form ends, and a quoted designator names it
32
    /// meanwhile. That frame also owns the alias, so it outlives inner
33
    /// frames. Hands the cell back when no frame holds the name.
34
5828
    pub(super) fn defer_function(&mut self, name: &str, cell: Function) -> Option<Function> {
35
5828
        let alias = Self::function_alias(name);
36
5828
        let alias_prior = self
37
5828
            .symbols
38
5828
            .get(&alias)
39
3956
            .and_then(|symbol| symbol.function.clone());
40
5828
        let frame = self.scopes.iter_mut().find(|frame| {
41
5412
            frame
42
5412
                .iter()
43
5412
                .any(|(saved, shadowed)| saved == name && matches!(shadowed, Shadowed::Function(_)))
44
5412
        });
45
5828
        let Some(frame) = frame else {
46
416
            return Some(cell);
47
        };
48
5412
        for (saved, shadowed) in frame.iter_mut() {
49
5412
            if let Shadowed::Function(prior) = shadowed
50
5412
                && saved == name
51
            {
52
5412
                *prior = Some(cell.clone());
53
5412
                break;
54
            }
55
        }
56
5412
        if !frame
57
5412
            .iter()
58
11448
            .any(|(saved, shadowed)| *saved == alias && matches!(shadowed, Shadowed::Function(_)))
59
        {
60
1664
            frame.push((alias.clone(), Shadowed::Function(alias_prior)));
61
3748
        }
62
5412
        if Self::hideable(&cell) {
63
4996
            self.define(Symbol::from_function(alias, cell));
64
        } else {
65
416
            self.clear_cell(&alias, |symbol| symbol.function = None);
66
        }
67
5412
        None
68
5828
    }
69
}