1
//! The symbol table's regions (0014 Stage 2): the bodies that run zero or
2
//! more times, the forms a caught exit may skip and the function-body flag.
3

            
4
use crate::ast::Expr;
5
use crate::error::{Error, Result};
6

            
7
use super::table::SymbolTable;
8

            
9
137297
fn deeper(depth: usize) -> Result<usize> {
10
137297
    depth
11
137297
        .checked_add(1)
12
        .ok_or_else(|| Error::Compile("internal: region depth overflowed".to_string()))
13
137297
}
14

            
15
137297
fn shallower(depth: usize) -> Result<usize> {
16
137297
    depth
17
137297
        .checked_sub(1)
18
        .ok_or_else(|| Error::Compile("internal: left a region never entered".to_string()))
19
137297
}
20

            
21
/// A form (BLOCK, TAGBODY or HANDLER-CASE) a caught exit may skip the rest of,
22
/// and the definitions inside the form that run before that exit.
23
#[derive(Clone, Debug, PartialEq)]
24
pub(crate) struct ExitRegion {
25
    pub(crate) form: &'static str,
26
    pub(crate) kept: Vec<FormId>,
27
}
28

            
29
/// The identity of a definition form: the address of its operands in the
30
/// body being compiled, so equal text in another place is another form.
31
pub(crate) type FormId = usize;
32

            
33
#[must_use]
34
27922
pub(crate) fn form_id(operands: &[Expr]) -> FormId {
35
27922
    operands.as_ptr().addr()
36
27922
}
37

            
38
impl ExitRegion {
39
    /// Whether the definition with the operands `args` runs before the exit.
40
23536
    fn keeps(&self, args: &[Expr]) -> bool {
41
23536
        self.kept.contains(&form_id(args))
42
23536
    }
43
}
44

            
45
impl SymbolTable {
46
    /// [`Self::exit_skippable`].
47
137297
    pub(crate) fn enter_skippable(&mut self) -> Result<()> {
48
137297
        self.skippable_depth = deeper(self.skippable_depth)?;
49
137297
        Ok(())
50
137297
    }
51

            
52
137297
    pub(crate) fn exit_skippable(&mut self) -> Result<()> {
53
137297
        self.skippable_depth = shallower(self.skippable_depth)?;
54
137297
        Ok(())
55
137297
    }
56

            
57
    /// Enters the forms after a possible exit the `region` catches. Paired
58
    /// with [`Self::leave_exit_region`].
59
94525
    pub(crate) fn enter_exit_region(&mut self, region: ExitRegion) {
60
94525
        self.exit_regions.push(region);
61
94525
    }
62

            
63
94525
    pub(crate) fn leave_exit_region(&mut self) -> Result<()> {
64
94525
        self.exit_regions
65
94525
            .pop()
66
94525
            .map(drop)
67
            .ok_or_else(|| Error::Compile("internal: left a region never entered".to_string()))
68
94525
    }
69

            
70
    /// Whether the definition with the operands `args` being compiled runs
71
    /// whenever the forms before it ran: no runtime branch, loop, caught exit
72
    /// or function body holds it.
73
    #[must_use]
74
125777
    pub(crate) fn runs_surely(&self, args: &[Expr]) -> bool {
75
125777
        !self.is_under_any_runtime_guard()
76
125152
            && !self.in_skippable_region()
77
125152
            && self.skipping_region(args).is_none()
78
121196
            && self.function_owner().is_none()
79
125777
    }
80

            
81
    /// The innermost form whose caught exit may skip the definition with the
82
    /// operands `args` being compiled.
83
    #[must_use]
84
7371573
    pub(crate) fn skipping_region(&self, args: &[Expr]) -> Option<&'static str> {
85
7371573
        self.exit_regions
86
7371573
            .iter()
87
7371573
            .rev()
88
23536
            .find(|region| !region.keeps(args))
89
19165
            .map(|region| region.form)
90
7371573
    }
91

            
92
    /// Marks this table as the environment of the body of function `owner`.
93
3893860
    pub(crate) fn enter_function_body(&mut self, owner: u64) {
94
3893860
        self.function_owner = Some(owner);
95
3893860
        self.close_loop_blocks();
96
3893860
    }
97

            
98
    #[must_use]
99
10399102
    pub(crate) fn function_owner(&self) -> Option<u64> {
100
10399102
        self.function_owner
101
10399102
    }
102

            
103
    /// True while compiling a body that runs zero or more times (see
104
    /// [`Self::skippable_depth`]).
105
    #[must_use]
106
7754364
    pub(crate) fn in_skippable_region(&self) -> bool {
107
7754364
        self.skippable_depth > 0
108
7754364
    }
109
}