gruel_air/sema/output.rs
1//! Output types from semantic analysis.
2//!
3//! This module contains the final outputs produced by semantic analysis:
4//! - [`AnalyzedFunction`] - A single analyzed function with typed IR
5//! - [`SemaOutput`] - Complete output from analyzing a program
6
7use crate::inst::{Air, AirParamMode};
8use crate::intern_pool::TypeInternPool;
9use crate::types::{InterfaceDef, InterfaceId, StructId, Type};
10use gruel_util::CompileWarning;
11use lasso::Spur;
12use rustc_hash::FxHashMap as HashMap;
13
14/// Vtable witnesses keyed by `(struct_id, interface_id)`. The value is the
15/// conforming type's method-key list in interface declaration order; codegen
16/// looks each one up in the function map to build the vtable global.
17pub type InterfaceVtables = HashMap<(StructId, InterfaceId), Vec<(StructId, Spur)>>;
18
19/// Result of analyzing a function.
20#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
21pub struct AnalyzedFunction {
22 pub name: String,
23 pub air: Air,
24 /// Number of local variable slots needed
25 pub num_locals: u32,
26 /// Number of ABI slots used by parameters.
27 /// For scalar types (i32, bool), each parameter uses 1 slot.
28 /// For struct types, each field uses 1 slot (flattened ABI).
29 pub num_param_slots: u32,
30 /// Passing mode for each parameter slot (normal, inout, or borrow).
31 /// Length matches num_param_slots - for struct params, all slots share
32 /// the same mode as the original parameter.
33 pub param_modes: Vec<AirParamMode>,
34 /// Type of each parameter ABI slot, parallel to `param_modes`.
35 /// Preserved here so backends can declare correct function signatures
36 /// even when DCE has removed unused `Param` instructions from the body.
37 pub param_slot_types: Vec<Type>,
38 /// Whether this function is a destructor (`drop fn`).
39 /// Destructors must not auto-drop their `self` parameter, as the
40 /// destructor IS the drop logic for that value.
41 pub is_destructor: bool,
42}
43
44/// Output from semantic analysis.
45///
46/// Contains all analyzed functions, struct definitions, enum definitions, and any warnings
47/// generated during analysis.
48#[derive(Debug)]
49pub struct SemaOutput {
50 /// Analyzed functions with typed IR.
51 pub functions: Vec<AnalyzedFunction>,
52 /// String literals indexed by their AIR string_const index.
53 pub strings: Vec<String>,
54 /// Byte-blob literals indexed by their AIR bytes_const index. Currently
55 /// populated by `@embed_file`.
56 pub bytes: Vec<Vec<u8>>,
57 /// Warnings collected during analysis.
58 pub warnings: Vec<CompileWarning>,
59 /// Type intern pool (contains all types including arrays).
60 pub type_pool: TypeInternPool,
61 /// Lines of `@dbg` output collected during comptime evaluation.
62 pub comptime_dbg_output: Vec<String>,
63 /// Interface definitions (ADR-0056), indexed by `InterfaceId.0`. Codegen
64 /// uses these to know each interface's method count and slot order when
65 /// emitting vtables.
66 pub interface_defs: Vec<InterfaceDef>,
67 /// Vtable witnesses keyed by `(struct_id, interface_id)`. The witness is
68 /// the conforming type's method-key list in interface declaration order;
69 /// codegen looks each one up in the function map to build the vtable
70 /// global.
71 pub interface_vtables: InterfaceVtables,
72}