Skip to main content

no_mistakes_postgres_engine/embedded/helpers/program/
mod.rs

1//! A closed, data-only model of a helper function's body.
2//!
3//! The per-file walk lowers each top-level helper once ([`lower`]). The
4//! prepared projection later executes the model against the abstract
5//! arguments of one call site ([`exec`]), forking at tests it cannot decide.
6//! Anything outside the model lowers to `Opaque`, which fails the execution
7//! only when a path actually reaches it, so unrelated unsupported code in a
8//! pruned branch does not block recovery.
9
10use crate::embedded::walk::resolve::functions::provenance::RecoveredSql;
11
12pub mod exec;
13pub mod lower;
14mod scalar_record;
15
16/// Physical position of an expression in the helper's own module.
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub struct Mark {
19    pub line: u32,
20    pub origin: u32,
21}
22
23#[derive(Clone, PartialEq, Eq)]
24pub struct FrozenField {
25    pub name: String,
26    pub sql: RecoveredSql,
27}
28
29/// One key of a frozen literal record returned by a guarded helper.
30#[derive(Clone, PartialEq, Eq)]
31pub struct FrozenEntry {
32    pub key: String,
33    pub fields: Vec<FrozenField>,
34}
35
36#[derive(Clone, PartialEq, Eq)]
37pub struct Program {
38    pub params: Vec<Param>,
39    pub body: Vec<Stmt>,
40    /// Parameter index that selects `frozen_entries`. `None` for ordinary bodies.
41    pub frozen_key: Option<usize>,
42    pub frozen_entries: Vec<FrozenEntry>,
43    pub scalar_fields: Vec<String>,
44}
45
46impl Program {
47    pub fn new(params: Vec<Param>, body: Vec<Stmt>) -> Self {
48        let scalar_fields = scalar_record::fields(&params, &body);
49        Self {
50            params,
51            body,
52            scalar_fields,
53            frozen_key: None,
54            frozen_entries: Vec::new(),
55        }
56    }
57}
58
59#[derive(Clone, PartialEq, Eq)]
60pub struct Param {
61    pub name: String,
62    /// Annotated as the `SQLStatement` type: the only parameter that may
63    /// receive a caller's builder.
64    pub builder: bool,
65    pub default: Option<Expr>,
66}
67
68#[derive(Clone, PartialEq, Eq)]
69pub struct SwitchArm {
70    pub cases: Vec<String>,
71    pub value: Expr,
72    pub mark: Mark,
73}
74
75#[derive(Clone, PartialEq, Eq)]
76pub enum Stmt {
77    Let(String, Expr),
78    Expr(Expr),
79    If(Expr, Vec<Stmt>, Vec<Stmt>),
80    Return(Option<Expr>, Mark),
81    /// Ends the path: the helper never returns, so the executor never runs.
82    Throw,
83    /// `for (const name of iterable) body`, unrolled over a known array.
84    ForOf(String, Expr, Vec<Stmt>),
85    /// Literal returns and a proven throwing `default`.
86    Switch(Expr, Vec<SwitchArm>, Option<Expr>),
87    Opaque,
88}
89
90#[derive(Clone, PartialEq, Eq)]
91pub enum Entry {
92    Prop(String, Expr),
93    Spread(Expr),
94}
95
96#[derive(Clone, PartialEq, Eq)]
97pub enum Expr {
98    /// A string literal, kept with its physical positions.
99    Lit(RecoveredSql),
100    Bool(bool),
101    Null,
102    Undefined,
103    Num,
104    Ident(String),
105    /// An untagged template: every interpolation must be a literal string.
106    Template(Vec<RecoveredSql>, Vec<(Expr, Mark)>),
107    /// A trusted-tag template: interpolations are binds or inlined fragments.
108    Sql(Vec<RecoveredSql>, Vec<(Expr, Mark)>, bool),
109    RawSql(Vec<RecoveredSql>, Vec<(Expr, Mark)>),
110    Member(Box<Expr>, String),
111    /// A runtime key must resolve to a literal own-property name before use.
112    ComputedMember(Box<Expr>, Box<Expr>),
113    Object(Vec<Entry>),
114    Array(Vec<Expr>),
115    Cond(Box<Expr>, Box<Expr>, Box<Expr>),
116    And(Box<Expr>, Box<Expr>),
117    Or(Box<Expr>, Box<Expr>),
118    Nullish(Box<Expr>, Box<Expr>),
119    Not(Box<Expr>),
120    /// `left === right` (strict) or `left == right`, negated for `!==`/`!=`.
121    Eq(Box<Expr>, Box<Expr>, bool, bool),
122    Add(Box<Expr>, Box<Expr>),
123    Append(Box<Expr>, Box<Expr>, Mark),
124    Call(String, Vec<Expr>, u32),
125    Opaque,
126}
127
128#[cfg(test)]
129mod tests;