deimos-lang/dmc-lib/src/symbol/class_symbol.rs
2026-03-21 11:36:42 -05:00

116 lines
3.3 KiB
Rust

use crate::ast::fqn_util::fqn_parts_to_string;
use crate::source_range::SourceRange;
use crate::symbol::constructor_symbol::ConstructorSymbol;
use crate::symbol::field_symbol::FieldSymbol;
use crate::symbol::function_symbol::FunctionSymbol;
use crate::symbol::generic_parameter_symbol::GenericParameterSymbol;
use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
use std::hash::{Hash, Hasher};
use std::rc::Rc;
pub struct ClassSymbol {
declared_name: Rc<str>,
declared_name_source_range: Option<SourceRange>,
fqn_parts: Vec<Rc<str>>,
is_extern: bool,
scope_id: usize,
generic_parameters: Vec<Rc<GenericParameterSymbol>>,
constructor_symbol: Option<Rc<ConstructorSymbol>>,
fields: HashMap<Rc<str>, Rc<FieldSymbol>>,
functions: HashMap<Rc<str>, Rc<FunctionSymbol>>,
}
impl ClassSymbol {
pub fn new(
declared_name: &Rc<str>,
declared_name_source_range: Option<SourceRange>,
fqn_parts: Vec<Rc<str>>,
is_extern: bool,
scope_id: usize,
generic_parameters: Vec<Rc<GenericParameterSymbol>>,
constructor_symbol: Option<Rc<ConstructorSymbol>>,
fields: Vec<Rc<FieldSymbol>>,
functions: Vec<Rc<FunctionSymbol>>,
) -> Self {
Self {
declared_name: declared_name.clone(),
declared_name_source_range,
fqn_parts,
is_extern,
scope_id,
generic_parameters,
constructor_symbol,
fields: fields
.into_iter()
.map(|fs| (fs.declared_name_owned(), fs))
.collect(),
functions: functions
.into_iter()
.map(|fs| (fs.declared_name_owned(), fs))
.collect(),
}
}
pub fn declared_name(&self) -> &str {
&self.declared_name
}
pub fn declared_name_owned(&self) -> Rc<str> {
self.declared_name.clone()
}
pub fn declared_name_source_range(&self) -> Option<&SourceRange> {
self.declared_name_source_range.as_ref()
}
pub fn fqn_parts(&self) -> &[Rc<str>] {
&self.fqn_parts
}
pub fn scope_id(&self) -> usize {
self.scope_id
}
pub fn generic_parameters(&self) -> &[Rc<GenericParameterSymbol>] {
&self.generic_parameters
}
pub fn constructor_symbol(&self) -> Option<&ConstructorSymbol> {
self.constructor_symbol.as_ref().map(|s| s.as_ref())
}
pub fn constructor_symbol_owned(&self) -> Option<Rc<ConstructorSymbol>> {
self.constructor_symbol.as_ref().cloned()
}
pub fn fields(&self) -> &HashMap<Rc<str>, Rc<FieldSymbol>> {
&self.fields
}
pub fn functions(&self) -> &HashMap<Rc<str>, Rc<FunctionSymbol>> {
&self.functions
}
}
impl Eq for ClassSymbol {}
impl PartialEq for ClassSymbol {
fn eq(&self, other: &Self) -> bool {
self.declared_name == other.declared_name && self.scope_id == other.scope_id
}
}
impl Hash for ClassSymbol {
fn hash<H: Hasher>(&self, state: &mut H) {
self.declared_name.hash(state);
self.scope_id.hash(state);
}
}
impl Debug for ClassSymbol {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "ClassSymbol({})", fqn_parts_to_string(&self.fqn_parts))
}
}