Lines
94.64 %
Functions
83.33 %
Branches
100 %
//! The symbol table's regions (0014 Stage 2): the bodies that run zero or
//! more times, the forms a caught exit may skip and the function-body flag.
use crate::ast::Expr;
use crate::error::{Error, Result};
use super::table::SymbolTable;
fn deeper(depth: usize) -> Result<usize> {
depth
.checked_add(1)
.ok_or_else(|| Error::Compile("internal: region depth overflowed".to_string()))
}
fn shallower(depth: usize) -> Result<usize> {
.checked_sub(1)
.ok_or_else(|| Error::Compile("internal: left a region never entered".to_string()))
/// A form (BLOCK, TAGBODY or HANDLER-CASE) a caught exit may skip the rest of,
/// and the definitions inside the form that run before that exit.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct ExitRegion {
pub(crate) form: &'static str,
pub(crate) kept: Vec<FormId>,
/// The identity of a definition form: the address of its operands in the
/// body being compiled, so equal text in another place is another form.
pub(crate) type FormId = usize;
#[must_use]
pub(crate) fn form_id(operands: &[Expr]) -> FormId {
operands.as_ptr().addr()
impl ExitRegion {
/// Whether the definition with the operands `args` runs before the exit.
fn keeps(&self, args: &[Expr]) -> bool {
self.kept.contains(&form_id(args))
impl SymbolTable {
/// [`Self::exit_skippable`].
pub(crate) fn enter_skippable(&mut self) -> Result<()> {
self.skippable_depth = deeper(self.skippable_depth)?;
Ok(())
pub(crate) fn exit_skippable(&mut self) -> Result<()> {
self.skippable_depth = shallower(self.skippable_depth)?;
/// Enters the forms after a possible exit the `region` catches. Paired
/// with [`Self::leave_exit_region`].
pub(crate) fn enter_exit_region(&mut self, region: ExitRegion) {
self.exit_regions.push(region);
pub(crate) fn leave_exit_region(&mut self) -> Result<()> {
self.exit_regions
.pop()
.map(drop)
/// Whether the definition with the operands `args` being compiled runs
/// whenever the forms before it ran: no runtime branch, loop, caught exit
/// or function body holds it.
pub(crate) fn runs_surely(&self, args: &[Expr]) -> bool {
!self.is_under_any_runtime_guard()
&& !self.in_skippable_region()
&& self.skipping_region(args).is_none()
&& self.function_owner().is_none()
/// The innermost form whose caught exit may skip the definition with the
/// operands `args` being compiled.
pub(crate) fn skipping_region(&self, args: &[Expr]) -> Option<&'static str> {
.iter()
.rev()
.find(|region| !region.keeps(args))
.map(|region| region.form)
/// Marks this table as the environment of the body of function `owner`.
pub(crate) fn enter_function_body(&mut self, owner: u64) {
self.function_owner = Some(owner);
self.close_loop_blocks();
pub(crate) fn function_owner(&self) -> Option<u64> {
self.function_owner
/// True while compiling a body that runs zero or more times (see
/// [`Self::skippable_depth`]).
pub(crate) fn in_skippable_region(&self) -> bool {
self.skippable_depth > 0