slang_frontend/semantic_analysis/
traits.rs

1// Core traits for semantic analysis components
2use super::error::SemanticAnalysisError;
3use slang_shared::{Symbol, SymbolKind};
4use slang_types::TypeId;
5
6/// Type alias for result of semantic analysis operations
7/// Contains either a valid TypeId or a SemanticAnalysisError
8pub type SemanticResult = Result<TypeId, SemanticAnalysisError>;
9
10/// Trait for symbol resolution operations
11///
12/// This trait abstracts symbol lookup operations, allowing for different
13/// implementations and easier testing through mocking.
14pub trait SymbolResolver {
15    /// Resolve a variable symbol by name
16    ///
17    /// Returns the symbol if found and it's a variable, None otherwise
18    fn resolve_variable(&self, name: &str) -> Option<&Symbol>;
19
20    /// Resolve a function symbol by name
21    ///
22    /// Returns the symbol if found and it's a function, None otherwise
23    fn resolve_function(&self, name: &str) -> Option<&Symbol>;
24
25    /// Resolve a symbol that can be used as a value (variables and functions)
26    ///
27    /// This allows functions to be accessed as first-class values
28    fn resolve_value(&self, name: &str) -> Option<&Symbol>;
29}
30
31/// Trait for scope management operations
32///
33/// This trait abstracts scope lifecycle management, enabling different
34/// scoping strategies and easier testing.
35pub trait ScopeManager {
36    /// Enter a new scope
37    fn enter_scope(&mut self);
38
39    /// Exit the current scope
40    fn exit_scope(&mut self);
41
42    /// Define a symbol in the current scope
43    fn define_symbol(
44        &mut self,
45        name: String,
46        kind: SymbolKind,
47        type_id: TypeId,
48        is_mutable: bool,
49    ) -> Result<(), String>;
50}