1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
mod insert;
mod kinds;
mod normalization;
pub use kinds::{
EnumInfo, FieldInfo, FunctionInfo, ImplInfo, ImportError, LetInfo, ModuleInfo, ParamInfo,
StructInfo, SymbolKind, TraitImplInfo, TraitInfo,
};
use crate::ast::{GenericParam, Type, Visibility};
use std::collections::HashMap;
use std::path::PathBuf;
/// Symbol table for tracking all definitions
#[derive(Debug, Clone)]
pub struct SymbolTable {
/// Unified traits (no model/view distinction)
pub traits: HashMap<String, TraitInfo>,
/// Unified structs (replaces models and views)
pub structs: HashMap<String, StructInfo>,
/// Inherent impl blocks (impl Struct)
pub impls: HashMap<String, ImplInfo>,
/// Trait implementations (impl Trait for Struct), keyed by struct name
pub trait_impls: HashMap<String, Vec<TraitImplInfo>>,
/// Enums with variant information
pub enums: HashMap<String, EnumInfo>,
/// Let bindings with type information
pub lets: HashMap<String, LetInfo>,
/// Standalone functions (multiple overloads per name allowed)
pub functions: HashMap<String, Vec<FunctionInfo>>,
/// Modules with nested symbol tables
pub modules: HashMap<String, ModuleInfo>,
/// Track which module each symbol came from (None = current module)
module_origins: HashMap<String, Option<PathBuf>>,
/// Track the logical module path for imported symbols (e.g., `["utils", "helpers"]`)
module_logical_paths: HashMap<String, Vec<String>>,
}
impl SymbolTable {
#[must_use]
pub fn new() -> Self {
Self {
traits: HashMap::new(),
structs: HashMap::new(),
impls: HashMap::new(),
trait_impls: HashMap::new(),
enums: HashMap::new(),
lets: HashMap::new(),
functions: HashMap::new(),
modules: HashMap::new(),
module_origins: HashMap::new(),
module_logical_paths: HashMap::new(),
}
}
/// Get all traits implemented by a struct (via impl Trait for Struct blocks)
#[must_use]
pub fn get_all_traits_for_struct(&self, struct_name: &str) -> Vec<String> {
let mut traits = Vec::new();
// Get traits from trait impl blocks (impl Trait for Struct)
if let Some(impls) = self.trait_impls.get(struct_name) {
for impl_info in impls {
if !traits.contains(&impl_info.trait_name) {
traits.push(impl_info.trait_name.clone());
}
}
}
traits
}
/// Get all traits implemented by an enum (from : Trait syntax and impl Trait for Enum)
#[must_use]
pub fn get_all_traits_for_enum(&self, enum_name: &str) -> Vec<String> {
let mut all_traits = Vec::new();
// Get traits from enum definition (: Trait syntax)
if let Some(enum_info) = self.enums.get(enum_name) {
all_traits.extend(enum_info.traits.clone());
}
// Get traits from trait_impls (impl Trait for Enum)
if let Some(impls) = self.trait_impls.get(enum_name) {
for impl_info in impls {
if !all_traits.contains(&impl_info.trait_name) {
all_traits.push(impl_info.trait_name.clone());
}
}
}
all_traits
}
/// Get enum variants
#[must_use]
pub fn get_enum_variants(
&self,
name: &str,
) -> Option<&HashMap<String, (usize, crate::location::Span)>> {
self.enums.get(name).map(|info| &info.variants)
}
/// Get the first function overload by name (for backward compatibility)
#[must_use]
pub fn get_function(&self, name: &str) -> Option<&FunctionInfo> {
self.functions.get(name).and_then(|v| v.first())
}
/// Get all overloads for a function name
#[must_use]
pub fn get_function_overloads(&self, name: &str) -> &[FunctionInfo] {
self.functions.get(name).map_or(&[], |v| v.as_slice())
}
/// Get the inferred type of a let binding.
#[must_use]
pub fn get_let_type(&self, name: &str) -> Option<&crate::semantic::sem_type::SemType> {
self.lets.get(name).and_then(|info| info.inferred_type.as_ref())
}
/// Find a symbol in any table (functions are excluded — they allow overloads)
pub(super) fn find_any(&self, name: &str) -> Option<(SymbolKind, crate::location::Span)> {
if let Some(info) = self.traits.get(name) {
return Some((SymbolKind::Trait, info.span));
}
if let Some(info) = self.structs.get(name) {
return Some((SymbolKind::Struct, info.span));
}
if let Some(info) = self.impls.get(name) {
return Some((SymbolKind::Impl, info.span));
}
if let Some(info) = self.enums.get(name) {
return Some((SymbolKind::Enum, info.span));
}
if let Some(info) = self.lets.get(name) {
return Some((SymbolKind::Let, info.span));
}
if let Some(info) = self.modules.get(name) {
return Some((SymbolKind::Module, info.span));
}
None
}
/// Get trait info
#[must_use]
pub fn get_trait(&self, name: &str) -> Option<&TraitInfo> {
self.traits.get(name)
}
/// Get struct info
#[must_use]
pub fn get_struct(&self, name: &str) -> Option<&StructInfo> {
self.structs.get(name)
}
/// Get struct info, supporting module-qualified names like "`fill::Solid`"
#[must_use]
pub fn get_struct_qualified(&self, name: &str) -> Option<&StructInfo> {
// Try direct lookup first
if let Some(info) = self.structs.get(name) {
return Some(info);
}
// Try module-qualified lookup (e.g., "fill::Solid")
if let Some((module_name, struct_name)) = name.split_once("::") {
if let Some(module_info) = self.modules.get(module_name) {
return module_info.symbols.get_struct_qualified(struct_name);
}
}
None
}
/// Get enum info, supporting module-qualified names like "`fill::PatternRepeat`"
#[must_use]
pub fn get_enum_qualified(&self, name: &str) -> Option<&EnumInfo> {
// Try direct lookup first
if let Some(info) = self.enums.get(name) {
return Some(info);
}
// Try module-qualified lookup (e.g., "fill::PatternRepeat")
if let Some((module_name, enum_name)) = name.split_once("::") {
if let Some(module_info) = self.modules.get(module_name) {
return module_info.symbols.get_enum_qualified(enum_name);
}
}
None
}
/// Check whether a name resolves to a struct, including
/// module-qualified paths like `geometry::Point`. Use this in
/// dispatch / inference paths that may see a qualified call.
#[must_use]
pub fn is_struct_qualified(&self, name: &str) -> bool {
self.get_struct_qualified(name).is_some()
}
/// Check if a name is a struct
#[must_use]
pub fn is_struct(&self, name: &str) -> bool {
self.structs.contains_key(name)
}
/// Check if a name is an enum
#[must_use]
pub fn is_enum(&self, name: &str) -> bool {
self.enums.contains_key(name)
}
/// Check if a name is a let binding
#[must_use]
pub fn is_let(&self, name: &str) -> bool {
self.lets.contains_key(name)
}
/// Check if a name is a type (struct or enum)
#[must_use]
pub fn is_type(&self, name: &str) -> bool {
self.structs.contains_key(name) || self.enums.contains_key(name)
}
/// Get all required fields from a trait (including composed traits)
///
/// Returns `name -> Type`. Doc comments are not propagated through the
/// composition flattening — callers needing per-field docs should walk
/// `TraitInfo.fields` directly on the leaf trait.
#[must_use]
pub fn get_all_trait_fields(&self, name: &str) -> HashMap<String, Type> {
let mut all_fields = HashMap::new();
if let Some(trait_info) = self.get_trait(name) {
// Add fields from composed traits first
for composed_trait in &trait_info.composed_traits {
let composed_fields = self.get_all_trait_fields(composed_trait);
all_fields.extend(composed_fields);
}
// Add fields from this trait (can override composed trait fields)
for f in &trait_info.fields {
all_fields.insert(f.name.clone(), f.ty.clone());
}
}
all_fields
}
/// Get generic parameters for a type (trait, struct, or enum)
#[must_use]
pub fn get_generics(&self, type_name: &str) -> Option<Vec<GenericParam>> {
// Check traits
if let Some(info) = self.traits.get(type_name) {
return Some(info.generics.clone());
}
// Check structs
if let Some(info) = self.structs.get(type_name) {
return Some(info.generics.clone());
}
// Check enums
if let Some(info) = self.enums.get(type_name) {
return Some(info.generics.clone());
}
None
}
/// Check if a name is a trait
#[must_use]
pub fn is_trait(&self, name: &str) -> bool {
self.traits.contains_key(name)
}
/// Get all public symbols in this table
#[must_use]
pub fn all_public_symbols(&self) -> Vec<String> {
let mut symbols = Vec::new();
for (name, info) in &self.traits {
if info.visibility == Visibility::Public {
symbols.push(name.clone());
}
}
for (name, info) in &self.structs {
if info.visibility == Visibility::Public {
symbols.push(name.clone());
}
}
for (name, info) in &self.enums {
if info.visibility == Visibility::Public {
symbols.push(name.clone());
}
}
for (name, info) in &self.lets {
if info.visibility == Visibility::Public {
symbols.push(name.clone());
}
}
for (name, info) in &self.modules {
if info.visibility == Visibility::Public {
symbols.push(name.clone());
}
}
for (name, overloads) in &self.functions {
if overloads.iter().any(|f| f.visibility == Visibility::Public) {
symbols.push(name.clone());
}
}
symbols.sort();
symbols
}
/// Get the module origin for a symbol.
///
/// Returns `Some(path)` if the symbol was imported from another module,
/// or `None` if the symbol is defined locally.
///
/// # Arguments
///
/// * `name` - The name of the symbol to look up
///
/// # Returns
///
/// * `Some(&PathBuf)` - The filesystem path of the module the symbol was imported from
/// * `None` - The symbol is local or not found
#[must_use]
pub fn get_module_origin(&self, name: &str) -> Option<&PathBuf> {
self.module_origins.get(name).and_then(|opt| opt.as_ref())
}
/// Get the logical module path for an imported symbol.
///
/// Returns `Some(path)` if the symbol was imported from another module,
/// or `None` if the symbol is local.
///
/// # Arguments
///
/// * `name` - The name of the symbol to look up
///
/// # Returns
///
/// * `Some(&Vec<String>)` - The logical module path (e.g., `["utils", "helpers"]`)
/// * `None` - The symbol is local or not found
#[must_use]
pub fn get_module_logical_path(&self, name: &str) -> Option<&Vec<String>> {
self.module_logical_paths.get(name)
}
/// Get the kind of a symbol (struct, trait, enum, etc.)
#[must_use]
pub fn get_symbol_kind(&self, name: &str) -> Option<SymbolKind> {
if self.structs.contains_key(name) {
Some(SymbolKind::Struct)
} else if self.traits.contains_key(name) {
Some(SymbolKind::Trait)
} else if self.enums.contains_key(name) {
Some(SymbolKind::Enum)
} else if self.lets.contains_key(name) {
Some(SymbolKind::Let)
} else if self.modules.contains_key(name) {
Some(SymbolKind::Module)
} else if self.impls.contains_key(name) {
Some(SymbolKind::Impl)
} else if self.functions.contains_key(name) {
Some(SymbolKind::Function)
} else {
None
}
}
}
impl Default for SymbolTable {
fn default() -> Self {
Self::new()
}
}