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
use crate::semantic::resolver::Resolver;
use crate::semantic::symbol_table::Symbol;
impl<'a> Resolver<'a> {
// ============================================================
// Import Path Utilities
// ============================================================
/// Parse an import path into components (split by ::)
pub fn parse_import_path(path: &str) -> Vec<String> {
path.split("::").map(|s| s.to_string()).collect()
}
/// Check if an import is a wildcard import (ends with *)
pub fn is_wildcard_import(path: &str) -> bool {
path.ends_with("::*") || path == "*"
}
// ============================================================
// Import Resolution (for expanding import statements)
// ============================================================
/// Resolve an import path to the symbols it brings into scope
pub fn resolve_import(&self, import_path: &str) -> Vec<String> {
if Self::is_wildcard_import(import_path) {
self.resolve_wildcard_import(import_path)
} else if self.resolve_qualified(import_path).is_some() {
vec![import_path.to_string()]
} else {
vec![]
}
}
fn resolve_wildcard_import(&self, import_path: &str) -> Vec<String> {
if import_path == "*" {
return self
.symbol_table()
.iter_symbols()
.filter_map(|symbol| {
let qname = symbol.qualified_name();
if !qname.contains("::") {
Some(qname.to_string())
} else {
None
}
})
.collect();
}
let prefix = import_path.strip_suffix("::*").unwrap_or(import_path);
self.symbol_table()
.iter_symbols()
.filter_map(|symbol| {
let qname = symbol.qualified_name();
if let Some(remainder) = qname.strip_prefix(prefix)
&& let Some(remainder) = remainder.strip_prefix("::")
&& !remainder.contains("::")
{
return Some(qname.to_string());
}
None
})
.collect()
}
// ============================================================
// Import-based Name Resolution (O(1) via export_map)
// ============================================================
/// Resolve a simple name via imports registered in a scope.
/// Uses precomputed export_map for O(1) lookup.
pub(super) fn resolve_via_imports(&self, name: &str, scope_id: usize) -> Option<&Symbol> {
self.symbol_table().lookup_in_export_map(scope_id, name)
}
/// Resolve a simple name via public imports only (for re-export resolution).
/// Uses precomputed export_map for O(1) lookup.
pub(super) fn resolve_via_public_imports(
&self,
name: &str,
scope_id: usize,
) -> Option<&Symbol> {
self.symbol_table().lookup_in_export_map(scope_id, name)
}
}