gruel_air/sema/gather.rs
1//! Output from the declaration gathering phase.
2//!
3//! This module contains the [`GatherOutput`] struct which holds the state built
4//! during declaration gathering that is needed for function body analysis.
5
6use std::collections::HashMap;
7
8use gruel_error::PreviewFeatures;
9use gruel_rir::Rir;
10use lasso::{Spur, ThreadedRodeo};
11
12use crate::intern_pool::TypeInternPool;
13use crate::param_arena::ParamArena;
14use crate::types::{EnumId, StructId};
15
16use super::info::{ConstInfo, FunctionInfo, MethodInfo};
17use super::{KnownSymbols, Sema};
18
19/// Output from the declaration gathering phase.
20///
21/// This contains the state built during declaration gathering that is needed
22/// for function body analysis. After gathering, this can be converted back
23/// into a `Sema` for sequential analysis, or used to drive parallel analysis.
24///
25/// # Architecture
26///
27/// The separation of declaration gathering from body analysis enables:
28/// 1. **Parallel type checking** - Each function can be analyzed independently
29/// 2. **Clearer architecture** - Separation of concerns
30/// 3. **Foundation for incremental** - Can cache SemaContext across compilations
31/// 4. **Better error recovery** - One function's error doesn't block others
32///
33/// # Usage
34///
35/// ```ignore
36/// // Option A: Simple path - all-in-one analysis
37/// let sema = Sema::new(rir, interner, preview);
38/// let output = sema.analyze_all()?;
39///
40/// // Option B: Parallel path (work in progress)
41/// // Build SemaContext and analyze in parallel
42/// let ctx = sema.build_sema_context();
43/// let results: Vec<_> = functions.par_iter()
44/// .map(|f| analyze_function_body(&ctx, f))
45/// .collect();
46/// ```
47#[derive(Debug)]
48pub struct GatherOutput<'a> {
49 /// Reference to the RIR being analyzed.
50 pub rir: &'a Rir,
51 /// Reference to the string interner.
52 pub interner: &'a ThreadedRodeo,
53 /// Struct lookup: maps struct name symbol to StructId.
54 pub structs: HashMap<Spur, StructId>,
55 /// Enum lookup: maps enum name symbol to EnumId.
56 pub enums: HashMap<Spur, EnumId>,
57 /// Function lookup: maps function name to info.
58 pub functions: HashMap<Spur, FunctionInfo>,
59 /// Method lookup: maps (struct_id, method_name) to info.
60 pub methods: HashMap<(StructId, Spur), MethodInfo>,
61 /// Constant lookup: maps const name to info.
62 pub constants: HashMap<Spur, ConstInfo>,
63 /// Enabled preview features.
64 pub preview_features: PreviewFeatures,
65 /// StructId of the synthetic String type.
66 pub builtin_string_id: Option<StructId>,
67 /// EnumId of the synthetic Arch enum (for @target_arch intrinsic).
68 pub builtin_arch_id: Option<EnumId>,
69 /// EnumId of the synthetic Os enum (for @target_os intrinsic).
70 pub builtin_os_id: Option<EnumId>,
71 /// EnumId of the synthetic TypeKind enum (for @typeInfo intrinsic).
72 pub builtin_typekind_id: Option<EnumId>,
73 /// Type intern pool (ADR-0024 Phase 1).
74 pub type_pool: TypeInternPool,
75 /// Arena storage for function/method parameter data.
76 pub param_arena: ParamArena,
77}
78
79impl<'a> GatherOutput<'a> {
80 /// Convert this gather output back into a Sema for function body analysis.
81 ///
82 /// This is used for sequential analysis. The returned Sema has all
83 /// declarations already collected and is ready to analyze function bodies.
84 pub fn into_sema(self) -> Sema<'a> {
85 Sema {
86 rir: self.rir,
87 interner: self.interner,
88 functions: self.functions,
89 structs: self.structs,
90 enums: self.enums,
91 methods: self.methods,
92 enum_methods: HashMap::new(),
93 constants: self.constants,
94 preview_features: self.preview_features,
95 builtin_string_id: self.builtin_string_id,
96 builtin_arch_id: self.builtin_arch_id,
97 builtin_os_id: self.builtin_os_id,
98 builtin_typekind_id: self.builtin_typekind_id,
99 known: KnownSymbols::new(self.interner),
100 type_pool: self.type_pool,
101 module_registry: crate::sema_context::ModuleRegistry::new(),
102 file_paths: HashMap::new(),
103 param_arena: self.param_arena,
104 anon_struct_method_sigs: HashMap::new(),
105 anon_struct_captured_values: HashMap::new(),
106 anon_enum_method_sigs: HashMap::new(),
107 anon_enum_captured_values: HashMap::new(),
108 comptime_steps_used: 0,
109 comptime_return_value: None,
110 comptime_call_depth: 0,
111 comptime_heap: Vec::new(),
112 comptime_type_overrides: HashMap::new(),
113 comptime_dbg_output: Vec::new(),
114 comptime_log_output: Vec::new(),
115 suppress_comptime_dbg_print: false,
116 }
117 }
118}