Skip to main content

aver/ast/
mod.rs

1pub mod types;
2pub use types::Type;
3
4/// Source line number (1-based). 0 = synthetic/unknown.
5pub type SourceLine = usize;
6
7/// A `bool` that compares as always-equal. Used for `last_use` annotations
8/// on `Expr::Resolved` — metadata that should not affect AST equality
9/// (same pattern as `Spanned` ignoring `line` in its `PartialEq`).
10#[derive(Debug, Clone, Copy, Default)]
11pub struct AnnotBool(pub bool);
12
13impl PartialEq for AnnotBool {
14    fn eq(&self, _: &Self) -> bool {
15        true
16    }
17}
18
19impl From<bool> for AnnotBool {
20    fn from(b: bool) -> Self {
21        Self(b)
22    }
23}
24
25/// AST node with source location plus an optional inferred type.
26///
27/// Line-agnostic equality: two `Spanned` values are equal iff their inner
28/// nodes are equal, regardless of line or attached type. The type slot is a
29/// `OnceLock<Type>` populated by the type checker; backends that have not
30/// been migrated to consume it stay agnostic and continue inferring locally.
31/// `OnceLock` (rather than `OnceCell`) keeps `Spanned` `Sync`, which matters
32/// because parts of the AST live behind `Arc` and cross thread boundaries
33/// (e.g. parallel verify execution, REPL background tasks).
34#[derive(Debug)]
35pub struct Spanned<T> {
36    pub node: T,
37    pub line: SourceLine,
38    pub ty: std::sync::OnceLock<Type>,
39}
40
41// `OnceLock` does not derive `Clone` (the cell is invariant over `T`), so the
42// inner type is cloned manually.
43impl<T: Clone> Clone for Spanned<T> {
44    fn clone(&self) -> Self {
45        let ty = std::sync::OnceLock::new();
46        if let Some(t) = self.ty.get() {
47            let _ = ty.set(t.clone());
48        }
49        Self {
50            node: self.node.clone(),
51            line: self.line,
52            ty,
53        }
54    }
55}
56
57impl<T: PartialEq> PartialEq for Spanned<T> {
58    fn eq(&self, other: &Self) -> bool {
59        self.node == other.node
60    }
61}
62
63impl<T> Spanned<T> {
64    pub fn new(node: T, line: SourceLine) -> Self {
65        Self {
66            node,
67            line,
68            ty: std::sync::OnceLock::new(),
69        }
70    }
71
72    /// Create a Spanned with line=0 (synthetic/generated AST, no source location).
73    pub fn bare(node: T) -> Self {
74        Self::new(node, 0)
75    }
76
77    /// Record the inferred type for this node. No-op if a type is already set
78    /// (later inference passes must not contradict the first one).
79    pub fn set_ty(&self, ty: Type) {
80        let _ = self.ty.set(ty);
81    }
82
83    /// Inferred type for this node, if the type checker has visited it.
84    pub fn ty(&self) -> Option<&Type> {
85        self.ty.get()
86    }
87}
88
89#[derive(Debug, Clone, PartialEq)]
90pub enum Literal {
91    Int(i64),
92    Float(f64),
93    Str(String),
94    Bool(bool),
95    Unit,
96}
97
98#[derive(Debug, Clone, Copy, PartialEq)]
99pub enum BinOp {
100    Add,
101    Sub,
102    Mul,
103    Div,
104    Eq,
105    Neq,
106    Lt,
107    Gt,
108    Lte,
109    Gte,
110}
111
112#[derive(Debug)]
113pub struct MatchArm {
114    pub pattern: Pattern,
115    pub body: Box<Spanned<Expr>>,
116    /// Per-arm slot table for the pattern's bindings, in pattern order.
117    /// Filled by the resolver pass; backend code reads from here
118    /// instead of doing a name lookup, so two arms with the same
119    /// binding name (e.g. `deadline` showing up in both `TaskCreated`
120    /// and `DeadlineSet` with different field types) get separate
121    /// slots without colliding in the function-level slot table.
122    /// Wildcard-position bindings (`_`) are stored as `u16::MAX` and
123    /// must never be read.
124    pub binding_slots: std::sync::OnceLock<Vec<u16>>,
125}
126
127// `OnceLock` doesn't derive Clone (cell is invariant over T); copy
128// the inner manually so the resolver's allocations survive the
129// `Arc::make_mut` clones that happen during multimodule flatten.
130impl Clone for MatchArm {
131    fn clone(&self) -> Self {
132        let binding_slots = std::sync::OnceLock::new();
133        if let Some(v) = self.binding_slots.get() {
134            let _ = binding_slots.set(v.clone());
135        }
136        Self {
137            pattern: self.pattern.clone(),
138            body: self.body.clone(),
139            binding_slots,
140        }
141    }
142}
143
144impl PartialEq for MatchArm {
145    fn eq(&self, other: &Self) -> bool {
146        self.pattern == other.pattern && self.body == other.body
147    }
148}
149
150impl MatchArm {
151    /// Build a fresh arm with no binding-slot stamp yet — resolver
152    /// fills `binding_slots` after slot allocation. Use this from any
153    /// site that synthesises an arm (parser, AST rewrites, effect
154    /// lifting, tests).
155    pub fn new(pattern: Pattern, body: Spanned<Expr>) -> Self {
156        Self {
157            pattern,
158            body: Box::new(body),
159            binding_slots: std::sync::OnceLock::new(),
160        }
161    }
162
163    pub fn new_boxed(pattern: Pattern, body: Box<Spanned<Expr>>) -> Self {
164        Self {
165            pattern,
166            body,
167            binding_slots: std::sync::OnceLock::new(),
168        }
169    }
170}
171
172#[derive(Debug, Clone, PartialEq)]
173pub enum Pattern {
174    Wildcard,
175    Literal(Literal),
176    Ident(String),
177    /// Empty list pattern: `[]`
178    EmptyList,
179    /// Cons-like list pattern: `[head, ..tail]`
180    Cons(String, String),
181    /// Tuple pattern: `(a, b)` / `(_, x)` / nested tuples.
182    Tuple(Vec<Pattern>),
183    /// Constructor pattern: fully-qualified name + list of binding names.
184    /// Built-ins: Result.Ok(x), Result.Err(x), Option.Some(x), Option.None.
185    /// User-defined: Shape.Circle(r), Shape.Rect(w, h), Shape.Point.
186    Constructor(String, Vec<String>),
187}
188
189#[derive(Debug, Clone, PartialEq)]
190pub enum StrPart {
191    Literal(String),
192    Parsed(Box<Spanned<Expr>>),
193}
194
195/// Data for a tail-call expression.
196#[derive(Debug, Clone, PartialEq)]
197pub struct TailCallData {
198    /// Target function name (self or mutual-recursive peer).
199    pub target: String,
200    /// Arguments to pass.
201    pub args: Vec<Spanned<Expr>>,
202}
203
204impl TailCallData {
205    pub fn new(target: String, args: Vec<Spanned<Expr>>) -> Self {
206        Self { target, args }
207    }
208}
209
210#[derive(Debug, Clone, PartialEq)]
211pub enum Expr {
212    Literal(Literal),
213    Ident(String),
214    Attr(Box<Spanned<Expr>>, String),
215    FnCall(Box<Spanned<Expr>>, Vec<Spanned<Expr>>),
216    BinOp(BinOp, Box<Spanned<Expr>>, Box<Spanned<Expr>>),
217    Match {
218        subject: Box<Spanned<Expr>>,
219        arms: Vec<MatchArm>,
220    },
221    Constructor(String, Option<Box<Spanned<Expr>>>),
222    ErrorProp(Box<Spanned<Expr>>),
223    InterpolatedStr(Vec<StrPart>),
224    List(Vec<Spanned<Expr>>),
225    Tuple(Vec<Spanned<Expr>>),
226    /// Map literal: `{"a" => 1, "b" => 2}`
227    MapLiteral(Vec<(Spanned<Expr>, Spanned<Expr>)>),
228    /// Record creation: `User(name = "Alice", age = 30)`
229    RecordCreate {
230        type_name: String,
231        fields: Vec<(String, Spanned<Expr>)>,
232    },
233    /// Record update: `User.update(base, field = newVal, ...)`
234    RecordUpdate {
235        type_name: String,
236        base: Box<Spanned<Expr>>,
237        updates: Vec<(String, Spanned<Expr>)>,
238    },
239    /// Tail-position call to a function in the same SCC (self or mutual recursion).
240    /// Produced by the TCO transform pass before type-checking.
241    /// Reuse info is populated by `ir::reuse::annotate_program_reuse`.
242    TailCall(Box<TailCallData>),
243    /// Independent product: `(a, b, c)!` or `(a, b, c)?!`.
244    /// Elements are independent effectful expressions evaluated with no guaranteed order.
245    /// `unwrap=true` (`?!`): all elements must be Result; unwraps Ok values, propagates first Err.
246    /// `unwrap=false` (`!`): returns raw tuple of results.
247    /// Produces a replay group (effects matched by branch_path + effect_occurrence + type + args).
248    IndependentProduct(Vec<Spanned<Expr>>, bool),
249    /// Compiled variable lookup: `env[last][slot]` — O(1) instead of HashMap scan.
250    /// Produced by the resolver pass for locals inside function bodies.
251    /// `last_use` is set by `ir::last_use` — when true, this is the final
252    /// reference to this slot and backends can move instead of copy.
253    Resolved {
254        slot: u16,
255        name: String,
256        last_use: AnnotBool,
257    },
258}
259
260#[derive(Debug, Clone, PartialEq)]
261pub enum Stmt {
262    Binding(String, Option<String>, Spanned<Expr>),
263    Expr(Spanned<Expr>),
264}
265
266#[derive(Debug, Clone, PartialEq)]
267pub enum FnBody {
268    Block(Vec<Stmt>),
269}
270
271impl FnBody {
272    pub fn from_expr(expr: Spanned<Expr>) -> Self {
273        Self::Block(vec![Stmt::Expr(expr)])
274    }
275
276    pub fn stmts(&self) -> &[Stmt] {
277        match self {
278            Self::Block(stmts) => stmts,
279        }
280    }
281
282    pub fn stmts_mut(&mut self) -> &mut Vec<Stmt> {
283        match self {
284            Self::Block(stmts) => stmts,
285        }
286    }
287
288    pub fn tail_expr(&self) -> Option<&Spanned<Expr>> {
289        match self.stmts().last() {
290            Some(Stmt::Expr(expr)) => Some(expr),
291            _ => None,
292        }
293    }
294
295    pub fn tail_expr_mut(&mut self) -> Option<&mut Spanned<Expr>> {
296        match self.stmts_mut().last_mut() {
297            Some(Stmt::Expr(expr)) => Some(expr),
298            _ => None,
299        }
300    }
301}
302
303/// Compile-time resolution metadata for a function body.
304/// Produced by `resolver::resolve_fn` — maps local variable names to slot indices
305/// so the VM can use `Vec<Value>` instead of `HashMap` lookups.
306#[derive(Debug, Clone, PartialEq)]
307pub struct FnResolution {
308    /// Total number of local slots needed (params + bindings in body).
309    pub local_count: u16,
310    /// Map from local variable name → slot index in the local `Slots` frame.
311    pub local_slots: std::sync::Arc<std::collections::HashMap<String, u16>>,
312    /// Aver type per slot index. Length == `local_count`. Built post-
313    /// typecheck so each entry pulls from the matching `Spanned::ty()`
314    /// stamp on the producer expression, plus pattern-binding shape
315    /// rules (`Result.Ok` → T, `Cons head` → list element, tuple item
316    /// → tuple element, …). Backends that need a typed local table
317    /// (the wasm-gc lowering uses one to declare each `local` with a
318    /// concrete `ValType`) consume this directly instead of re-deriving
319    /// the same information from patterns.
320    ///
321    /// Default `Type::Invalid` for unreachable / unstamped slots — every
322    /// real binding gets overwritten during the slot-types pass, so an
323    /// `Invalid` reaching the backend means the slot was never the
324    /// target of a binding (resolver counted but no expression
325    /// produced into it; usually a wildcard slot the backend skips).
326    pub local_slot_types: std::sync::Arc<Vec<Type>>,
327    /// Whether each slot may share an arena entry with another slot.
328    /// Length == `local_count`. Set by `ir::alias::annotate_program_alias_slots`
329    /// post-`last_use`. Backends that have a `mem::take`-style fast path
330    /// for `Vector.set` / `Map.set` (the VM's `CALL_BUILTIN_OWNED` mask
331    /// plus the fused `VECTOR_SET_OR_KEEP`) must NOT take the fast path
332    /// on a flagged slot — rewriting the shared arena entry would
333    /// mutate the other binding too. Wasm-gc may use it to skip
334    /// clone-on-write when the slot is provably non-aliased; otherwise
335    /// it falls back to `array.copy` + `array.set` on the copy.
336    ///
337    /// Default `false` for slots the analysis hasn't reached (anything
338    /// pre-`last_use`, REPL, partial pipelines), which is the safe-but-
339    /// slow choice everywhere except the VM fast path.
340    pub aliased_slots: std::sync::Arc<Vec<bool>>,
341}
342
343#[derive(Debug, Clone, PartialEq)]
344pub struct FnDef {
345    pub name: String,
346    pub line: usize,
347    pub params: Vec<(String, String)>,
348    pub return_type: String,
349    pub effects: Vec<Spanned<String>>,
350    pub desc: Option<String>,
351    pub body: std::sync::Arc<FnBody>,
352    /// `None` for unresolved (REPL, module loading).
353    pub resolution: Option<FnResolution>,
354}
355
356#[derive(Debug, Clone, PartialEq)]
357pub struct Module {
358    pub name: String,
359    pub line: usize,
360    pub depends: Vec<String>,
361    pub exposes: Vec<String>,
362    pub exposes_opaque: Vec<String>,
363    pub exposes_line: Option<usize>,
364    pub intent: String,
365    /// Module-level effect surface declaration. `None` is legacy/mixed
366    /// (no enforcement, soft warning emitted by `aver check`); `Some([])`
367    /// is explicit pure; `Some([...])` is a declared boundary — every
368    /// function's `! [...]` must be a subset (namespace-level entry like
369    /// `Disk` admits any `Disk.*` method).
370    pub effects: Option<Vec<String>>,
371    pub effects_line: Option<usize>,
372}
373
374#[derive(Debug, Clone, PartialEq)]
375pub enum VerifyGivenDomain {
376    /// Integer range domain in verify law: `1..50` (inclusive).
377    IntRange { start: i64, end: i64 },
378    /// Explicit domain values in verify law: `[v1, v2, ...]`.
379    Explicit(Vec<Spanned<Expr>>),
380}
381
382#[derive(Debug, Clone, PartialEq)]
383pub struct VerifyGiven {
384    pub name: String,
385    pub type_name: String,
386    pub domain: VerifyGivenDomain,
387}
388
389#[derive(Debug, Clone, PartialEq)]
390pub struct VerifyLaw {
391    pub name: String,
392    pub givens: Vec<VerifyGiven>,
393    /// Optional precondition for the law template, written as `when <bool-expr>`.
394    pub when: Option<Spanned<Expr>>,
395    /// Template assertion from source before given-domain expansion.
396    pub lhs: Spanned<Expr>,
397    pub rhs: Spanned<Expr>,
398    /// Per-sample substituted guards for `when`, aligned with `VerifyBlock.cases`.
399    pub sample_guards: Vec<Spanned<Expr>>,
400}
401
402/// Source range for AST nodes that need location tracking.
403/// Used by verify case spans: `cases[i] <-> case_spans[i]`.
404#[derive(Debug, Clone, PartialEq, Default)]
405pub struct SourceSpan {
406    pub line: usize,
407    pub col: usize,
408    pub end_line: usize,
409    pub end_col: usize,
410}
411
412#[derive(Debug, Clone, PartialEq)]
413pub enum VerifyKind {
414    Cases,
415    Law(Box<VerifyLaw>),
416}
417
418#[derive(Debug, Clone, PartialEq)]
419pub struct VerifyBlock {
420    pub fn_name: String,
421    pub line: usize,
422    pub cases: Vec<(Spanned<Expr>, Spanned<Expr>)>,
423    pub case_spans: Vec<SourceSpan>,
424    /// Per-case given bindings for law verify (empty for Cases kind).
425    pub case_givens: Vec<Vec<(String, Spanned<Expr>)>>,
426    /// Parallel to `cases`: `true` when the case was injected by
427    /// `aver verify --hostile` (boundary-value expansion of a law's
428    /// `given` clause), `false` for cases the user wrote directly.
429    /// Empty under non-hostile runs; the renderer uses this to label
430    /// failures as "outside declared given — encode as `when` if
431    /// precondition" when they only fail under the hostile expansion.
432    pub case_hostile_origins: Vec<bool>,
433    /// Parallel to `cases`: per-case hostile effect-profile assignment
434    /// for `--hostile` mode. Each inner Vec lists `(method, profile)`
435    /// pairs (e.g. `("Time.now", "frozen")`) that the runner installs
436    /// as oracle stubs before running the case, alongside any user-given
437    /// stubs. Empty inner Vec for cases that aren't effect-hostile-
438    /// expanded (declared, value-hostile-only, or fns without applicable
439    /// classified effects). All entries empty under non-hostile runs.
440    pub case_hostile_profiles: Vec<Vec<(String, String)>>,
441    pub kind: VerifyKind,
442    /// Oracle v1: `trace` keyword enables trace-aware assertions
443    /// (`.trace.*`, `.result`, event literals in `.contains` / match
444    /// patterns). Without it, a law checks only the return value, so
445    /// adding a debug print does not break proofs that do not care
446    /// about traces.
447    pub trace: bool,
448    /// Oracle v1: `given` clauses declared at the top of a cases-form
449    /// trace block. Law-form stores its givens inside `VerifyKind::Law`;
450    /// cases-form doesn't have that wrapper, so this field carries them
451    /// so the verify runner can build oracle-stub mappings from the
452    /// same data. Empty for non-trace or law-form blocks.
453    pub cases_givens: Vec<VerifyGiven>,
454}
455
456impl VerifyBlock {
457    /// Construct a VerifyBlock with default (zero) spans for each case.
458    /// Use when source location tracking is not needed (codegen, tests).
459    pub fn new_unspanned(
460        fn_name: String,
461        line: usize,
462        cases: Vec<(Spanned<Expr>, Spanned<Expr>)>,
463        kind: VerifyKind,
464    ) -> Self {
465        let case_spans = vec![SourceSpan::default(); cases.len()];
466        let case_hostile_origins = vec![false; cases.len()];
467        let case_hostile_profiles = vec![Vec::new(); cases.len()];
468        Self {
469            fn_name,
470            line,
471            cases,
472            case_spans,
473            case_givens: vec![],
474            case_hostile_origins,
475            case_hostile_profiles,
476            kind,
477            trace: false,
478            cases_givens: vec![],
479        }
480    }
481
482    pub fn iter_cases_with_spans(
483        &self,
484    ) -> impl Iterator<Item = (&(Spanned<Expr>, Spanned<Expr>), &SourceSpan)> {
485        debug_assert_eq!(self.cases.len(), self.case_spans.len());
486        self.cases.iter().zip(&self.case_spans)
487    }
488}
489
490#[derive(Debug, Clone, PartialEq)]
491pub struct DecisionBlock {
492    pub name: String,
493    pub line: usize,
494    pub date: String,
495    pub reason: String,
496    pub chosen: Spanned<DecisionImpact>,
497    pub rejected: Vec<Spanned<DecisionImpact>>,
498    pub impacts: Vec<Spanned<DecisionImpact>>,
499    pub author: Option<String>,
500}
501
502#[derive(Debug, Clone, PartialEq, Eq, Hash)]
503pub enum DecisionImpact {
504    Symbol(String),
505    Semantic(String),
506}
507
508impl DecisionImpact {
509    pub fn text(&self) -> &str {
510        match self {
511            DecisionImpact::Symbol(s) | DecisionImpact::Semantic(s) => s,
512        }
513    }
514
515    pub fn as_context_string(&self) -> String {
516        match self {
517            DecisionImpact::Symbol(s) => s.clone(),
518            DecisionImpact::Semantic(s) => format!("\"{}\"", s),
519        }
520    }
521}
522
523/// A variant in a sum type definition.
524/// e.g. `Circle(Float)` → `TypeVariant { name: "Circle", fields: ["Float"] }`
525#[derive(Debug, Clone, PartialEq)]
526pub struct TypeVariant {
527    pub name: String,
528    pub fields: Vec<String>, // type annotations (e.g. "Float", "String")
529}
530
531/// A user-defined type definition.
532#[derive(Debug, Clone, PartialEq)]
533pub enum TypeDef {
534    /// `type Shape` with variants Circle(Float), Rect(Float, Float), Point
535    Sum {
536        name: String,
537        variants: Vec<TypeVariant>,
538        line: usize,
539    },
540    /// `record User` with fields name: String, age: Int
541    Product {
542        name: String,
543        fields: Vec<(String, String)>,
544        line: usize,
545    },
546}
547
548#[derive(Debug, Clone, PartialEq)]
549pub enum TopLevel {
550    Module(Module),
551    FnDef(FnDef),
552    Verify(VerifyBlock),
553    Decision(DecisionBlock),
554    Stmt(Stmt),
555    TypeDef(TypeDef),
556}