Skip to main content

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 rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
7
8use gruel_rir::Rir;
9use gruel_util::PreviewFeatures;
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 @type_info intrinsic).
72    pub builtin_typekind_id: Option<EnumId>,
73    /// EnumId of the synthetic Ownership enum (for @ownership intrinsic).
74    pub builtin_ownership_id: Option<EnumId>,
75    /// EnumId of the prelude `ThreadSafety` enum (ADR-0084).
76    pub builtin_thread_safety_id: Option<EnumId>,
77    /// EnumId of the prelude `Ordering` enum (ADR-0078 Phase 4).
78    pub builtin_ordering_id: Option<EnumId>,
79    /// Type intern pool (ADR-0024 Phase 1).
80    pub type_pool: TypeInternPool,
81    /// Arena storage for function/method parameter data.
82    pub param_arena: ParamArena,
83}
84
85impl<'a> GatherOutput<'a> {
86    /// Convert this gather output back into a Sema for function body analysis.
87    ///
88    /// This is used for sequential analysis. The returned Sema has all
89    /// declarations already collected and is ready to analyze function bodies.
90    pub fn into_sema(self) -> Sema<'a> {
91        Sema {
92            rir: self.rir,
93            interner: self.interner,
94            functions: self.functions,
95            structs: self.structs,
96            enums: self.enums,
97            interfaces: HashMap::default(),
98            interface_defs: Vec::new(),
99            comptime_interface_bounds: HashMap::default(),
100            interface_vtables_needed: HashMap::default(),
101            methods: self.methods,
102            enum_methods: HashMap::default(),
103            derives: HashMap::default(),
104            derive_bindings: Vec::new(),
105            pending_anon_derive_errors: Vec::new(),
106            pending_anon_eval_errors: Vec::new(),
107            constants: self.constants,
108            preview_features: self.preview_features,
109            builtin_string_id: self.builtin_string_id,
110            builtin_arch_id: self.builtin_arch_id,
111            builtin_os_id: self.builtin_os_id,
112            builtin_typekind_id: self.builtin_typekind_id,
113            builtin_ownership_id: self.builtin_ownership_id,
114            builtin_thread_safety_id: self.builtin_thread_safety_id,
115            builtin_ordering_id: self.builtin_ordering_id,
116            lang_items: super::LangItems::default(),
117            known: KnownSymbols::new(self.interner),
118            type_pool: self.type_pool,
119            module_registry: crate::sema_context::ModuleRegistry::new(),
120            file_paths: HashMap::default(),
121            param_arena: self.param_arena,
122            inline_struct_drops: HashMap::default(),
123            inline_enum_drops: HashMap::default(),
124            anon_struct_method_sigs: HashMap::default(),
125            anon_struct_captured_values: HashMap::default(),
126            anon_struct_type_subst: HashMap::default(),
127            anon_enum_method_sigs: HashMap::default(),
128            anon_enum_captured_values: HashMap::default(),
129            anon_enum_type_subst: HashMap::default(),
130            vec_instance_registry: HashMap::default(),
131            comptime_ctor_fn: None,
132            comptime_steps_used: 0,
133            comptime_return_value: None,
134            comptime_call_depth: 0,
135            comptime_heap: Vec::new(),
136            comptime_type_overrides: HashMap::default(),
137            comptime_dbg_output: Vec::new(),
138            comptime_log_output: Vec::new(),
139            suppress_comptime_dbg_print: false,
140            current_self: None,
141            target: gruel_target::Target::host(),
142            mark_affine_decls: HashSet::default(),
143            mark_thread_safety_decls: rustc_hash::FxHashMap::default(),
144        }
145    }
146}