slang_frontend/semantic_analysis/analyzer_modules/
core.rs

1use slang_ir::ast::*;
2use slang_shared::CompilationContext;
3
4use super::super::{
5    traits::SemanticResult,
6    visitors::{expression_visitor::ExpressionVisitor, statement_visitor::StatementVisitor},
7};
8
9/// Core analyzer that coordinates between statement and expression visitors
10///
11/// This analyzer provides a unified interface for semantic analysis by delegating
12/// to specialized visitors. It maintains efficient visitor reuse to avoid
13/// unnecessary allocations and improve performance.
14pub struct CoreAnalyzer<'a> {
15    context: &'a mut CompilationContext,
16}
17
18impl<'a> CoreAnalyzer<'a> {
19    /// Create a new core analyzer
20    ///
21    /// # Arguments
22    /// * `context` - The compilation context for symbol management and types
23    pub fn new(context: &'a mut CompilationContext) -> Self {
24        Self { context }
25    }
26
27    /// Provides access to the compilation context.
28    pub fn context(&mut self) -> &mut CompilationContext {
29        self.context
30    }
31
32    /// Analyze a statement using the appropriate visitor
33    ///
34    /// This method reuses a single visitor instance for all statement operations
35    /// to improve performance and reduce allocations.
36    pub fn analyze_statement(&mut self, stmt: &Statement) -> SemanticResult {
37        let mut stmt_visitor = StatementVisitor::new(self.context);
38
39        match stmt {
40            Statement::FunctionDeclaration(fn_decl) => {
41                stmt_visitor.visit_function_declaration(fn_decl)
42            }
43            Statement::Let(let_stmt) => stmt_visitor.visit_let_statement(let_stmt),
44            Statement::Assignment(assign_stmt) => {
45                stmt_visitor.visit_assignment_statement(assign_stmt)
46            }
47            Statement::Return(return_stmt) => stmt_visitor.visit_return_statement(return_stmt),
48            Statement::TypeDefinition(type_def) => {
49                stmt_visitor.visit_type_definition_statement(type_def)
50            }
51            Statement::Expression(expr) => stmt_visitor.visit_expression_statement(expr),
52            Statement::If(if_stmt) => stmt_visitor.visit_if_statement(if_stmt),
53        }
54    }
55
56    /// Analyze an expression using the expression visitor
57    ///
58    /// This method reuses a single visitor instance for all expression operations
59    /// to improve performance and reduce allocations.
60    pub fn analyze_expression(&mut self, expr: &Expression) -> SemanticResult {
61        let mut expr_visitor = ExpressionVisitor::new(self.context);
62        expr_visitor.visit_expression(expr)
63    }
64
65    /// Analyze a block expression
66    ///
67    /// This method reuses a single visitor instance for all block operations
68    /// to improve performance and reduce allocations.
69    pub fn analyze_block(&mut self, block: &BlockExpr) -> SemanticResult {
70        let mut expr_visitor = ExpressionVisitor::new(self.context);
71        expr_visitor.visit_block_expression(block)
72    }
73}