1use crate::intr::IntrinsicFunctionDefinitionRef;
6use crate::modules::ModuleRef;
7use crate::{
8 AliasType, AliasTypeRef, AnonymousStructType, Constant, ConstantRef, EnumType, EnumTypeRef,
9 EnumVariantType, EnumVariantTypeRef, ExternalFunctionDefinition, ExternalFunctionDefinitionRef,
10 ExternalType, ExternalTypeRef, InternalFunctionDefinition, InternalFunctionDefinitionRef,
11 NamedStructType, Node, SemanticError, StructTypeField, StructTypeRef, Type,
12};
13use seq_map::SeqMap;
14use std::cell::RefCell;
15use std::fmt::Debug;
16use std::rc::Rc;
17use tiny_ver::TinyVersion;
18use tracing::info;
19
20#[derive(Debug, Clone)]
21pub enum FuncDef {
22 Internal(InternalFunctionDefinitionRef),
23 Intrinsic(IntrinsicFunctionDefinitionRef),
24 External(ExternalFunctionDefinitionRef),
25}
26
27#[derive(Clone, Debug)]
28pub enum GeneratorKind {
29 Slice,
30 SlicePair,
31 Sparse,
32 Map,
33 Vec,
34}
35
36#[derive(Clone, Debug)]
37pub struct TypeGenerator {
38 pub kind: GeneratorKind,
39 pub arity: usize,
40}
41
42#[derive(Clone, Debug)]
43pub enum Symbol {
44 Type(Type),
45 Module(ModuleRef),
46 PackageVersion(TinyVersion),
47 Constant(ConstantRef),
48 FunctionDefinition(FuncDef),
49 Alias(AliasTypeRef),
50 }
52
53impl Symbol {
54 #[must_use]
55 pub const fn is_basic_type(&self) -> bool {
56 matches!(self, Self::Type(..))
57 }
58}
59
60#[derive(Debug, Clone)]
61pub struct SymbolTable {
62 symbols: SeqMap<String, Symbol>,
63}
64
65pub type SymbolTableRef = Rc<SymbolTable>;
66
67impl Default for SymbolTable {
68 fn default() -> Self {
69 Self::new()
70 }
71}
72
73impl SymbolTable {
74 #[must_use]
75 pub fn new() -> Self {
76 Self {
77 symbols: SeqMap::default(),
78 }
79 }
80
81 #[must_use]
82 pub fn is_empty(&self) -> bool {
83 self.symbols.is_empty()
84 }
85
86 #[must_use]
87 pub const fn symbols(&self) -> &SeqMap<String, Symbol> {
88 &self.symbols
89 }
90
91 pub fn structs(&self) -> SeqMap<String, StructTypeRef> {
92 let mut structs = SeqMap::new();
93
94 for (name, symbol) in &self.symbols {
95 if let Symbol::Type(ty) = symbol {
96 if let Type::NamedStruct(struct_ref) = ty {
97 structs
98 .insert(name.to_string(), struct_ref.clone())
99 .unwrap();
100 }
101 }
102 }
103
104 structs
105 }
106
107 pub fn extend_from(&mut self, symbol_table: &Self) -> Result<(), SemanticError> {
110 for (name, symbol) in symbol_table.symbols() {
111 self.add_symbol(name, symbol.clone())?;
112 }
113 Ok(())
114 }
115
116 pub fn extend_basic_from(&mut self, symbol_table: &Self) -> Result<(), SemanticError> {
119 for (name, symbol) in symbol_table.symbols() {
120 if symbol.is_basic_type() {
121 self.add_symbol(name, symbol.clone())?;
122 }
123 }
124 Ok(())
125 }
126
127 #[must_use]
128 pub fn get_package_version(&self, name: &str) -> Option<String> {
129 match self.get_symbol(name)? {
130 Symbol::PackageVersion(name) => Some(name.to_string()),
131 _ => None,
132 }
133 }
134
135 pub fn add_constant(&mut self, constant: Constant) -> Result<ConstantRef, SemanticError> {
138 let constant_ref = Rc::new(constant);
139
140 self.add_constant_link(constant_ref.clone())?;
141
142 Ok(constant_ref)
143 }
144
145 pub fn add_constant_link(&mut self, constant_ref: ConstantRef) -> Result<(), SemanticError> {
148 let name = constant_ref.assigned_name.clone();
149
150 self.symbols
151 .insert(name.to_string(), Symbol::Constant(constant_ref))
152 .map_err(|_| SemanticError::DuplicateConstName(name.to_string()))?;
153
154 Ok(())
155 }
156
157 pub fn add_alias(&mut self, alias_type: AliasType) -> Result<AliasTypeRef, SemanticError> {
160 let alias_ref = Rc::new(alias_type);
161 self.add_alias_link(alias_ref.clone())?;
162 Ok(alias_ref)
163 }
164
165 pub fn add_alias_link(&mut self, alias_type_ref: AliasTypeRef) -> Result<(), SemanticError> {
168 let name = alias_type_ref.assigned_name.clone();
169 self.symbols
170 .insert(name.clone(), Symbol::Alias(alias_type_ref.clone()))
171 .map_err(|_| SemanticError::DuplicateStructName(name))?;
172
173 Ok(())
174 }
175
176 pub fn add_external_type(
177 &mut self,
178 external: ExternalType,
179 ) -> Result<ExternalTypeRef, SemanticError> {
180 let external_ref = Rc::new(external);
181 self.add_external_type_link(external_ref.clone())?;
182 Ok(external_ref)
183 }
184
185 pub fn add_external_type_link(
188 &mut self,
189 external_type_ref: ExternalTypeRef,
190 ) -> Result<(), SemanticError> {
191 let name = external_type_ref.type_name.clone();
192 self.symbols
193 .insert(
194 name.clone(),
195 Symbol::Type(Type::External(external_type_ref)),
196 )
197 .map_err(|_| SemanticError::DuplicateStructName(name))?;
198 Ok(())
199 }
200
201 pub fn add_struct(
204 &mut self,
205 struct_type: NamedStructType,
206 ) -> Result<StructTypeRef, SemanticError> {
207 let struct_ref = Rc::new(RefCell::new(struct_type));
208 self.add_struct_link(struct_ref.clone())?;
209 Ok(struct_ref)
210 }
211
212 pub fn add_generated_struct(
215 &mut self,
216 name: &str,
217 fields: &[(&str, Type)],
218 ) -> Result<StructTypeRef, SemanticError> {
219 let mut defined_fields = SeqMap::new();
220 for (name, field_type) in fields {
221 defined_fields
222 .insert(
223 name.to_string(),
224 StructTypeField {
225 identifier: None,
226 field_type: field_type.clone(),
227 },
228 )
229 .unwrap();
230 }
231
232 let struct_type = NamedStructType {
233 name: Node::default(),
234 assigned_name: name.to_string(),
235 anon_struct_type: AnonymousStructType::new(defined_fields),
236 functions: SeqMap::default(),
237 };
238
239 let struct_ref = Rc::new(RefCell::new(struct_type));
240
241 self.add_struct_link(struct_ref.clone())?;
242
243 Ok(struct_ref)
244 }
245
246 pub fn add_struct_link(&mut self, struct_type_ref: StructTypeRef) -> Result<(), SemanticError> {
249 let name = struct_type_ref.borrow().assigned_name.clone();
250 self.symbols
251 .insert(
252 name.clone(),
253 Symbol::Type(Type::NamedStruct(struct_type_ref)),
254 )
255 .map_err(|_| SemanticError::DuplicateStructName(name))?;
256 Ok(())
257 }
258
259 pub fn add_enum_type(&mut self, enum_type: EnumType) -> Result<EnumTypeRef, SemanticError> {
260 let enum_type_ref = Rc::new(RefCell::new(enum_type));
261 self.add_enum_type_link(enum_type_ref.clone())?;
262 Ok(enum_type_ref)
263 }
264
265 pub fn add_enum_type_link(&mut self, enum_type_ref: EnumTypeRef) -> Result<(), SemanticError> {
266 let ty = Type::Enum(enum_type_ref.clone());
267 self.symbols
268 .insert(
269 enum_type_ref.borrow().assigned_name.clone(),
270 Symbol::Type(ty),
271 )
272 .map_err(|_| {
273 SemanticError::DuplicateEnumType(enum_type_ref.borrow().assigned_name.clone())
274 })?;
275
276 Ok(())
277 }
278
279 pub fn add_enum_variant(
280 &mut self,
281 enum_type_name: EnumTypeRef,
282 enum_variant: EnumVariantType,
283 ) -> Result<EnumVariantTypeRef, SemanticError> {
284 let enum_variant_ref = Rc::new(enum_variant);
285 enum_type_name
286 .borrow_mut()
287 .variants
288 .insert(
289 enum_variant_ref.common().assigned_name.clone(),
290 enum_variant_ref.clone(),
291 )
292 .map_err(|_err| {
293 SemanticError::DuplicateEnumVariantType(
294 enum_type_name.borrow().assigned_name.clone(),
295 enum_variant_ref.common().assigned_name.clone(),
296 )
297 })?;
298
299 Ok(enum_variant_ref)
300 }
301
302 pub fn add_internal_function(
303 &mut self,
304 name: &str,
305 function: InternalFunctionDefinition,
306 ) -> Result<InternalFunctionDefinitionRef, SemanticError> {
307 let function_ref = Rc::new(function);
308 self.symbols
309 .insert(
310 name.to_string(),
311 Symbol::FunctionDefinition(FuncDef::Internal(function_ref.clone())),
312 )
313 .expect("todo: add seqmap error handling");
314 Ok(function_ref)
315 }
316
317 pub fn add_internal_function_link(
318 &mut self,
319 name: &str,
320 function_ref: InternalFunctionDefinitionRef,
321 ) -> Result<(), SemanticError> {
322 self.symbols
323 .insert(
324 name.to_string(),
325 Symbol::FunctionDefinition(FuncDef::Internal(function_ref.clone())),
326 )
327 .expect("todo: add seqmap error handling");
328 Ok(())
329 }
330
331 pub fn get_symbol(&self, name: &str) -> Option<&Symbol> {
332 self.symbols.get(&name.to_string())
333 }
334
335 pub fn add_symbol(&mut self, name: &str, symbol: Symbol) -> Result<(), SemanticError> {
336 info!(name, ?symbol, "add_symbol");
337 self.symbols
338 .insert(name.to_string(), symbol)
339 .map_err(|_| SemanticError::DuplicateSymbolName)
340 }
341
342 pub fn get_type(&self, name: &str) -> Option<&Type> {
343 if let Some(found_symbol) = self.get_symbol(name) {
344 if let Symbol::Type(type_ref) = found_symbol {
345 return Some(type_ref);
346 }
347 }
348
349 None
350 }
351
352 pub fn get_struct(&self, name: &str) -> Option<&StructTypeRef> {
353 match self.get_type(name)? {
354 Type::NamedStruct(struct_ref) => Some(struct_ref),
355 _ => None,
356 }
357 }
358
359 pub fn get_enum(&self, name: &str) -> Option<&EnumTypeRef> {
360 match self.get_type(name)? {
361 Type::Enum(enum_type) => Some(enum_type),
362 _ => None,
363 }
364 }
365
366 #[must_use]
367 pub fn get_enum_variant_type(
368 &self,
369 enum_type_name: &str,
370 variant_name: &str,
371 ) -> Option<EnumVariantTypeRef> {
372 self.get_enum(enum_type_name).as_ref().map_or_else(
373 || None,
374 |found_enum| {
375 found_enum
376 .borrow()
377 .variants
378 .get(&variant_name.to_string())
379 .cloned()
380 },
381 )
382 }
383
384 pub fn get_constant(&self, name: &str) -> Option<&ConstantRef> {
385 match self.get_symbol(name)? {
386 Symbol::Constant(constant) => Some(constant),
387 _ => None,
388 }
389 }
390 #[must_use]
391 pub fn get_module_link(&self, name: &str) -> Option<&ModuleRef> {
392 match self.get_symbol(name)? {
393 Symbol::Module(module_ref) => Some(module_ref),
394 _ => None,
395 }
396 }
397
398 #[must_use]
401 pub fn get_function(&self, name: &str) -> Option<&FuncDef> {
402 match self.get_symbol(name)? {
403 Symbol::FunctionDefinition(func_def) => Some(func_def),
404 _ => None,
405 }
406 }
407
408 #[must_use]
409 pub fn get_internal_function(&self, name: &str) -> Option<&InternalFunctionDefinitionRef> {
410 match self.get_function(name)? {
411 FuncDef::Internal(internal_fn) => Some(internal_fn),
412 FuncDef::External(_) => None,
413 FuncDef::Intrinsic(_) => None,
414 }
415 }
416
417 #[must_use]
418 pub fn get_intrinsic_function(&self, name: &str) -> Option<&IntrinsicFunctionDefinitionRef> {
419 match self.get_function(name)? {
420 FuncDef::Intrinsic(intrinsic_fn) => Some(intrinsic_fn),
421 _ => None,
422 }
423 }
424
425 #[must_use]
426 pub fn get_external_function_declaration(
427 &self,
428 name: &str,
429 ) -> Option<&ExternalFunctionDefinitionRef> {
430 match self.get_function(name)? {
431 FuncDef::External(external_def) => Some(external_def),
432 _ => None,
433 }
434 }
435
436 pub fn get_external_type(&self, name: &str) -> Option<&ExternalTypeRef> {
437 match self.get_type(name)? {
438 Type::External(ext_type) => Some(ext_type),
439 _ => None,
440 }
441 }
442
443 fn insert_symbol(&mut self, name: &str, symbol: Symbol) -> Result<(), SemanticError> {
444 self.symbols
445 .insert(name.to_string(), symbol)
446 .map_err(|_| SemanticError::DuplicateSymbolName)
447 }
448
449 pub fn add_external_function_declaration(
450 &mut self,
451 decl: ExternalFunctionDefinition,
452 ) -> Result<ExternalFunctionDefinitionRef, SemanticError> {
453 let decl_ref = Rc::new(decl);
454
455 self.add_external_function_declaration_link(decl_ref.clone())?;
456
457 Ok(decl_ref)
458 }
459
460 pub fn add_external_function_declaration_link(
461 &mut self,
462 decl_ref: ExternalFunctionDefinitionRef,
463 ) -> Result<(), SemanticError> {
464 self.insert_symbol(
465 &decl_ref.assigned_name,
466 Symbol::FunctionDefinition(FuncDef::External(decl_ref.clone())),
467 )
468 .map_err(|_| {
469 SemanticError::DuplicateExternalFunction(decl_ref.assigned_name.to_string())
470 })?;
471 Ok(())
472 }
473
474 pub fn add_module_link(&mut self, name: &str, ns: ModuleRef) -> Result<(), SemanticError> {
475 self.insert_symbol(name, Symbol::Module(ns))
476 .map_err(|_| SemanticError::DuplicateNamespaceLink(name.to_string()))?;
477 Ok(())
478 }
479}