gruel_air/sema/info.rs
1//! Information types for functions, methods, and constants.
2//!
3//! These types store metadata about declarations gathered during the first
4//! phase of semantic analysis. They are used to resolve function calls,
5//! method calls, and constant references during function body analysis.
6
7use gruel_span::{FileId, Span};
8use lasso::Spur;
9
10use crate::param_arena::ParamRange;
11use crate::types::Type;
12
13/// Information about a function.
14#[derive(Debug, Clone, Copy)]
15pub struct FunctionInfo {
16 /// Parameter data (names, types, modes, comptime flags) stored in arena.
17 /// Access via `arena.names(params)`, `arena.types(params)`, etc.
18 pub params: ParamRange,
19 /// Return type
20 pub return_type: Type,
21 /// The return type symbol (before resolution) - needed for generic function specialization
22 pub return_type_sym: Spur,
23 /// The RIR instruction ref for the function body - needed for generic function specialization
24 pub body: gruel_rir::InstRef,
25 /// Span of the function declaration
26 pub span: Span,
27 /// Whether this function has any comptime type parameters
28 pub is_generic: bool,
29 /// Whether this function is public (visible outside its directory)
30 pub is_pub: bool,
31 /// Whether this function is marked `unchecked` (can only be called from checked blocks)
32 pub is_unchecked: bool,
33 /// File ID this function was declared in (for visibility checking)
34 pub file_id: FileId,
35}
36
37/// Information about a method in an impl block.
38///
39/// Note: Captured comptime values for anonymous struct methods are stored at the
40/// struct level in `Sema::anon_struct_captured_values`, not per-method. This ensures
41/// that different instantiations with different captured values create different types.
42#[derive(Debug, Clone, Copy)]
43pub struct MethodInfo {
44 /// The struct type this method belongs to
45 pub struct_type: Type,
46 /// Whether this is a method (has self) or associated function (no self)
47 pub has_self: bool,
48 /// Parameter data (names, types, modes, comptime flags) stored in arena.
49 /// Access via `arena.names(params)`, `arena.types(params)`, etc.
50 /// Note: This excludes `self` if present - only explicit parameters.
51 pub params: ParamRange,
52 /// Return type
53 pub return_type: Type,
54 /// The RIR instruction ref for the method body
55 pub body: gruel_rir::InstRef,
56 /// Span of the method declaration
57 pub span: Span,
58 /// Whether this method is marked `unchecked` (can only be called from checked blocks)
59 pub is_unchecked: bool,
60}
61
62/// Method signature for anonymous struct structural equality comparison.
63///
64/// This captures only the parts of a method that affect structural equality:
65/// method name, whether it has self, parameter types (as symbols), and return type.
66/// Method bodies do NOT affect structural equality - only signatures matter.
67///
68/// Type symbols are stored as Spur (interned strings) rather than resolved Types
69/// because at comparison time, `Self` hasn't been resolved to a concrete StructId yet.
70/// Two methods using `Self` in the same positions are considered structurally equal.
71#[derive(Debug, Clone, PartialEq, Eq)]
72pub struct AnonMethodSig {
73 /// Method name
74 pub name: Spur,
75 /// Whether this is a method (has self) or associated function (no self)
76 pub has_self: bool,
77 /// Parameter type symbols (excluding self parameter)
78 pub param_types: Vec<Spur>,
79 /// Return type symbol
80 pub return_type: Spur,
81}
82
83/// Information about a constant declaration.
84///
85/// Constants are compile-time values. In the module system, they're primarily
86/// used for re-exports:
87/// ```gruel
88/// pub const strings = @import("utils/strings.gruel");
89/// pub const helper = @import("utils/internal.gruel").helper;
90/// ```
91#[derive(Debug, Clone)]
92pub struct ConstInfo {
93 /// Whether this constant is public
94 pub is_pub: bool,
95 /// The type of the constant value (e.g., Type::Module for imports)
96 pub ty: Type,
97 /// The RIR instruction ref for the initializer
98 pub init: gruel_rir::InstRef,
99 /// Span of the const declaration
100 pub span: Span,
101}