polycomp 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.toml +25 -0
- package/bin/native/polycomp.exe +0 -0
- package/bin/polycomp.js +25 -0
- package/js/frontend.js +1061 -0
- package/js/index.cjs +245 -0
- package/js/index.d.ts +2 -0
- package/js/index.mjs +19 -0
- package/js/polycomp-vm.mjs +1047 -0
- package/package.json +41 -5
- package/src/ast.rs +329 -0
- package/src/bundle.rs +3307 -0
- package/src/diagnostics.rs +276 -0
- package/src/interpreter.rs +1565 -0
- package/src/ir.rs +272 -0
- package/src/jit.rs +1151 -0
- package/src/lib.rs +11 -0
- package/src/lowering.rs +1157 -0
- package/src/main.rs +655 -0
- package/src/optimizer.rs +660 -0
- package/src/parser.rs +4755 -0
- package/src/resolver.rs +722 -0
- package/src/runtime.rs +1104 -0
- package/README.md +0 -3
package/src/resolver.rs
ADDED
|
@@ -0,0 +1,722 @@
|
|
|
1
|
+
use crate::ast::*;
|
|
2
|
+
use crate::diagnostics::{Diagnostic, PolyError, SourceMap, Span, suggest_did_you_mean};
|
|
3
|
+
use crate::parser::parse_module;
|
|
4
|
+
use rustc_hash::{FxHashMap, FxHashSet};
|
|
5
|
+
use std::path::{Path, PathBuf};
|
|
6
|
+
|
|
7
|
+
#[derive(Debug, Clone)]
|
|
8
|
+
pub struct ImportedSymbol {
|
|
9
|
+
pub source_specifier: String,
|
|
10
|
+
pub original_name: String,
|
|
11
|
+
pub span: Span,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
#[derive(Debug, Clone, Default)]
|
|
15
|
+
pub struct ImportBindings {
|
|
16
|
+
pub namespaces: FxHashMap<String, String>,
|
|
17
|
+
pub named: FxHashMap<String, ImportedSymbol>,
|
|
18
|
+
pub wildcards: Vec<(String, Span)>,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
#[derive(Debug, Clone)]
|
|
22
|
+
pub struct LoadedModule {
|
|
23
|
+
pub id: usize,
|
|
24
|
+
pub path: PathBuf,
|
|
25
|
+
pub language: Language,
|
|
26
|
+
pub ast: ModuleAst,
|
|
27
|
+
pub bindings: ImportBindings,
|
|
28
|
+
pub function_index: FxHashMap<String, usize>,
|
|
29
|
+
pub struct_index: FxHashMap<String, usize>,
|
|
30
|
+
pub enum_index: FxHashMap<String, usize>,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
#[derive(Debug, Clone, Default)]
|
|
34
|
+
pub struct ImportStats {
|
|
35
|
+
pub modules_parsed: usize,
|
|
36
|
+
pub parsed_paths: Vec<PathBuf>,
|
|
37
|
+
pub cache_hits: usize,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
#[derive(Debug, Clone)]
|
|
41
|
+
pub struct StaticCheckReport {
|
|
42
|
+
pub modules_parsed: usize,
|
|
43
|
+
pub reachable_functions: usize,
|
|
44
|
+
pub parsed_paths: Vec<PathBuf>,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
#[derive(Debug)]
|
|
48
|
+
pub struct ModuleResolver {
|
|
49
|
+
pub sources: SourceMap,
|
|
50
|
+
pub modules: Vec<LoadedModule>,
|
|
51
|
+
path_to_id: FxHashMap<PathBuf, usize>,
|
|
52
|
+
pub stats: ImportStats,
|
|
53
|
+
pub trace_imports: bool,
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
impl ModuleResolver {
|
|
57
|
+
pub fn new(trace_imports: bool) -> Self {
|
|
58
|
+
Self {
|
|
59
|
+
sources: SourceMap::default(),
|
|
60
|
+
modules: Vec::new(),
|
|
61
|
+
path_to_id: FxHashMap::default(),
|
|
62
|
+
stats: ImportStats::default(),
|
|
63
|
+
trace_imports,
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
pub fn load_module(&mut self, raw_path: &Path) -> Result<usize, PolyError> {
|
|
68
|
+
let canonical = canonicalize_soft(raw_path)?;
|
|
69
|
+
if let Some(&id) = self.path_to_id.get(&canonical) {
|
|
70
|
+
self.stats.cache_hits += 1;
|
|
71
|
+
return Ok(id);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
let lang = Language::from_path(&canonical).ok_or_else(|| {
|
|
75
|
+
PolyError::msg(
|
|
76
|
+
"E0201",
|
|
77
|
+
format!(
|
|
78
|
+
"unsupported source extension for '{}'; expected .rs, .py, .js, .ts, .c, .cpp, .cs, or .go",
|
|
79
|
+
canonical.display()
|
|
80
|
+
),
|
|
81
|
+
)
|
|
82
|
+
})?;
|
|
83
|
+
|
|
84
|
+
let source = std::fs::read_to_string(&canonical).map_err(|e| {
|
|
85
|
+
PolyError::msg(
|
|
86
|
+
"E0202",
|
|
87
|
+
format!("failed to read module '{}': {e}", canonical.display()),
|
|
88
|
+
)
|
|
89
|
+
})?;
|
|
90
|
+
|
|
91
|
+
let file_id = self.sources.add_file(canonical.clone(), source);
|
|
92
|
+
let src_ref = &self.sources.get(file_id).unwrap().source;
|
|
93
|
+
let ast = parse_module(file_id, lang, src_ref)?;
|
|
94
|
+
Ok(self.register_parsed_module(canonical, lang, ast))
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
pub fn load_source_in_memory(
|
|
98
|
+
&mut self,
|
|
99
|
+
virtual_path: &Path,
|
|
100
|
+
lang: Language,
|
|
101
|
+
source: String,
|
|
102
|
+
) -> Result<usize, PolyError> {
|
|
103
|
+
let abs = if virtual_path.is_absolute() {
|
|
104
|
+
virtual_path.to_path_buf()
|
|
105
|
+
} else {
|
|
106
|
+
std::env::current_dir()?.join(virtual_path)
|
|
107
|
+
};
|
|
108
|
+
let file_id = self.sources.add_file(abs.clone(), source);
|
|
109
|
+
let src_ref = &self.sources.get(file_id).unwrap().source;
|
|
110
|
+
let ast = parse_module(file_id, lang, src_ref)?;
|
|
111
|
+
Ok(self.register_parsed_module(abs, lang, ast))
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
fn register_parsed_module(
|
|
115
|
+
&mut self,
|
|
116
|
+
canonical: PathBuf,
|
|
117
|
+
lang: Language,
|
|
118
|
+
ast: ModuleAst,
|
|
119
|
+
) -> usize {
|
|
120
|
+
let bindings = build_import_bindings(&ast.imports);
|
|
121
|
+
let mut function_index = FxHashMap::default();
|
|
122
|
+
for (i, f) in ast.functions.iter().enumerate() {
|
|
123
|
+
function_index.insert(f.name.clone(), i);
|
|
124
|
+
}
|
|
125
|
+
let mut struct_index = FxHashMap::default();
|
|
126
|
+
for (i, s) in ast.structs.iter().enumerate() {
|
|
127
|
+
struct_index.insert(s.name.clone(), i);
|
|
128
|
+
}
|
|
129
|
+
let mut enum_index = FxHashMap::default();
|
|
130
|
+
for (i, e) in ast.enums.iter().enumerate() {
|
|
131
|
+
enum_index.insert(e.name.clone(), i);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
let mod_id = self.modules.len();
|
|
135
|
+
self.stats.modules_parsed += 1;
|
|
136
|
+
self.stats.parsed_paths.push(canonical.clone());
|
|
137
|
+
if self.trace_imports {
|
|
138
|
+
eprintln!(
|
|
139
|
+
"[polycomp:import] parsed module #{mod_id} ({}) <- {}",
|
|
140
|
+
lang.name(),
|
|
141
|
+
canonical.display()
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
self.modules.push(LoadedModule {
|
|
146
|
+
id: mod_id,
|
|
147
|
+
path: canonical.clone(),
|
|
148
|
+
language: lang,
|
|
149
|
+
ast,
|
|
150
|
+
bindings,
|
|
151
|
+
function_index,
|
|
152
|
+
struct_index,
|
|
153
|
+
enum_index,
|
|
154
|
+
});
|
|
155
|
+
self.path_to_id.insert(canonical, mod_id);
|
|
156
|
+
mod_id
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
pub fn get_module(&self, id: usize) -> &LoadedModule {
|
|
160
|
+
&self.modules[id]
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
pub fn resolve_specifier_path(
|
|
164
|
+
&self,
|
|
165
|
+
from_module_id: usize,
|
|
166
|
+
specifier: &str,
|
|
167
|
+
span: Span,
|
|
168
|
+
) -> Result<PathBuf, PolyError> {
|
|
169
|
+
let base_dir = self.modules[from_module_id]
|
|
170
|
+
.path
|
|
171
|
+
.parent()
|
|
172
|
+
.unwrap_or_else(|| Path::new("."));
|
|
173
|
+
|
|
174
|
+
let candidate = base_dir.join(specifier);
|
|
175
|
+
if candidate.is_file() {
|
|
176
|
+
return canonicalize_soft(&candidate);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if candidate.extension().is_none() {
|
|
180
|
+
for ext in [
|
|
181
|
+
"rs", "py", "ts", "mts", "js", "c", "cpp", "cc", "cs", "go", "hpp", "h",
|
|
182
|
+
] {
|
|
183
|
+
let with_ext = candidate.with_extension(ext);
|
|
184
|
+
if with_ext.is_file() {
|
|
185
|
+
return canonicalize_soft(&with_ext);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
for ext in ["rs", "py", "ts", "js", "c", "cpp", "cs", "go"] {
|
|
189
|
+
let index = candidate.join(format!("mod.{ext}"));
|
|
190
|
+
if index.is_file() {
|
|
191
|
+
return canonicalize_soft(&index);
|
|
192
|
+
}
|
|
193
|
+
let idx_js = candidate.join(format!("index.{ext}"));
|
|
194
|
+
if idx_js.is_file() {
|
|
195
|
+
return canonicalize_soft(&idx_js);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
Err(PolyError::single(
|
|
201
|
+
Diagnostic::error(
|
|
202
|
+
"E0203",
|
|
203
|
+
format!(
|
|
204
|
+
"cannot find imported module '{}' (searched relative to '{}')",
|
|
205
|
+
specifier,
|
|
206
|
+
base_dir.display()
|
|
207
|
+
),
|
|
208
|
+
)
|
|
209
|
+
.with_span(span)
|
|
210
|
+
.with_help("check the relative path or file extension (.rs, .py, .ts, .js, .c, .cpp, .cs, .go)"),
|
|
211
|
+
))
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
pub fn resolve_symbol_lazy(
|
|
215
|
+
&mut self,
|
|
216
|
+
from_module_id: usize,
|
|
217
|
+
symbol: &str,
|
|
218
|
+
span: Span,
|
|
219
|
+
) -> Result<(usize, String), PolyError> {
|
|
220
|
+
// 1. Direct local function
|
|
221
|
+
if self.modules[from_module_id]
|
|
222
|
+
.function_index
|
|
223
|
+
.contains_key(symbol)
|
|
224
|
+
{
|
|
225
|
+
return Ok((from_module_id, symbol.to_string()));
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// 2. C++/C# `using namespace Foo;` local lookup (e.g. `apply_fee` -> `Foo::apply_fee`)
|
|
229
|
+
let namespaces = self.modules[from_module_id].ast.using_namespaces.clone();
|
|
230
|
+
for ns in namespaces {
|
|
231
|
+
let scoped = format!("{ns}::{symbol}");
|
|
232
|
+
if self.modules[from_module_id]
|
|
233
|
+
.function_index
|
|
234
|
+
.contains_key(&scoped)
|
|
235
|
+
{
|
|
236
|
+
return Ok((from_module_id, scoped));
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// 3. Explicit named import
|
|
241
|
+
if let Some(imp) = self.modules[from_module_id].bindings.named.get(symbol).cloned() {
|
|
242
|
+
let target_path =
|
|
243
|
+
self.resolve_specifier_path(from_module_id, &imp.source_specifier, imp.span)?;
|
|
244
|
+
let target_mod_id = self.load_module(&target_path)?;
|
|
245
|
+
return self.verify_exported_symbol(target_mod_id, &imp.original_name, span);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// 4. Wildcard / `polycomp require` / `#include "..."` fallback chain
|
|
249
|
+
let wildcards = self.modules[from_module_id].bindings.wildcards.clone();
|
|
250
|
+
for (spec, wspan) in wildcards {
|
|
251
|
+
let target_path = self.resolve_specifier_path(from_module_id, &spec, wspan)?;
|
|
252
|
+
let target_mod_id = self.load_module(&target_path)?;
|
|
253
|
+
if let Some(&fn_idx) = self.modules[target_mod_id].function_index.get(symbol) {
|
|
254
|
+
let f = &self.modules[target_mod_id].ast.functions[fn_idx];
|
|
255
|
+
if !f.is_exported {
|
|
256
|
+
return Err(PolyError::single(
|
|
257
|
+
Diagnostic::error(
|
|
258
|
+
"E0204",
|
|
259
|
+
format!(
|
|
260
|
+
"function '{}' in '{}' is private",
|
|
261
|
+
symbol,
|
|
262
|
+
self.modules[target_mod_id].path.display()
|
|
263
|
+
),
|
|
264
|
+
)
|
|
265
|
+
.with_span(span),
|
|
266
|
+
));
|
|
267
|
+
}
|
|
268
|
+
return Ok((target_mod_id, symbol.to_string()));
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
let mut known: Vec<String> = self.modules[from_module_id]
|
|
273
|
+
.function_index
|
|
274
|
+
.keys()
|
|
275
|
+
.cloned()
|
|
276
|
+
.collect();
|
|
277
|
+
known.extend(
|
|
278
|
+
self.modules[from_module_id]
|
|
279
|
+
.bindings
|
|
280
|
+
.named
|
|
281
|
+
.keys()
|
|
282
|
+
.cloned(),
|
|
283
|
+
);
|
|
284
|
+
let mut diag = Diagnostic::error("E0205", format!("undefined function '{symbol}'"))
|
|
285
|
+
.with_span(span);
|
|
286
|
+
if let Some(sugg) = suggest_did_you_mean(symbol, known.iter().map(|s| s.as_str())) {
|
|
287
|
+
diag = diag.with_help(format!("did you mean `{sugg}`?"));
|
|
288
|
+
}
|
|
289
|
+
Err(PolyError::single(diag))
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
pub fn resolve_member_function_lazy(
|
|
293
|
+
&mut self,
|
|
294
|
+
from_module_id: usize,
|
|
295
|
+
namespace_alias: &str,
|
|
296
|
+
member_name: &str,
|
|
297
|
+
span: Span,
|
|
298
|
+
) -> Result<(usize, String), PolyError> {
|
|
299
|
+
// Check local `Class::Method` or `namespace::func` first
|
|
300
|
+
let scoped = format!("{namespace_alias}::{member_name}");
|
|
301
|
+
if self.modules[from_module_id]
|
|
302
|
+
.function_index
|
|
303
|
+
.contains_key(&scoped)
|
|
304
|
+
{
|
|
305
|
+
return Ok((from_module_id, scoped));
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
let Some(spec) = self.modules[from_module_id]
|
|
309
|
+
.bindings
|
|
310
|
+
.namespaces
|
|
311
|
+
.get(namespace_alias)
|
|
312
|
+
.cloned()
|
|
313
|
+
else {
|
|
314
|
+
return Err(PolyError::spanned(
|
|
315
|
+
"E0206",
|
|
316
|
+
format!("unknown module or class namespace '{namespace_alias}'"),
|
|
317
|
+
span,
|
|
318
|
+
));
|
|
319
|
+
};
|
|
320
|
+
let path = self.resolve_specifier_path(from_module_id, &spec, span)?;
|
|
321
|
+
let target_mod_id = self.load_module(&path)?;
|
|
322
|
+
self.verify_exported_symbol(target_mod_id, member_name, span)
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/// Resolves C# / Go / C++ entry points flexibly (`main`, `Main`, `Program::Main`, `Program::main`).
|
|
326
|
+
pub fn resolve_entry_fn_name(&self, mod_id: usize, requested: &str) -> Option<String> {
|
|
327
|
+
let m = &self.modules[mod_id];
|
|
328
|
+
if m.function_index.contains_key(requested) {
|
|
329
|
+
return Some(requested.to_string());
|
|
330
|
+
}
|
|
331
|
+
if requested == "main" {
|
|
332
|
+
if m.function_index.contains_key("Main") {
|
|
333
|
+
return Some("Main".to_string());
|
|
334
|
+
}
|
|
335
|
+
for k in m.function_index.keys() {
|
|
336
|
+
if k.ends_with("::Main") || k.ends_with("::main") {
|
|
337
|
+
return Some(k.clone());
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
None
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
fn verify_exported_symbol(
|
|
345
|
+
&self,
|
|
346
|
+
target_mod_id: usize,
|
|
347
|
+
orig_name: &str,
|
|
348
|
+
call_span: Span,
|
|
349
|
+
) -> Result<(usize, String), PolyError> {
|
|
350
|
+
let target = &self.modules[target_mod_id];
|
|
351
|
+
if let Some(&idx) = target.function_index.get(orig_name) {
|
|
352
|
+
let decl = &target.ast.functions[idx];
|
|
353
|
+
if !decl.is_exported {
|
|
354
|
+
return Err(PolyError::single(
|
|
355
|
+
Diagnostic::error(
|
|
356
|
+
"E0204",
|
|
357
|
+
format!(
|
|
358
|
+
"symbol '{}' in '{}' is not exported",
|
|
359
|
+
orig_name,
|
|
360
|
+
target.path.display()
|
|
361
|
+
),
|
|
362
|
+
)
|
|
363
|
+
.with_span(call_span),
|
|
364
|
+
));
|
|
365
|
+
}
|
|
366
|
+
return Ok((target_mod_id, orig_name.to_string()));
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
let mut diag = Diagnostic::error(
|
|
370
|
+
"E0207",
|
|
371
|
+
format!(
|
|
372
|
+
"module '{}' does not export function '{}'",
|
|
373
|
+
target.path.display(),
|
|
374
|
+
orig_name
|
|
375
|
+
),
|
|
376
|
+
)
|
|
377
|
+
.with_span(call_span);
|
|
378
|
+
if let Some(sugg) =
|
|
379
|
+
suggest_did_you_mean(orig_name, target.function_index.keys().map(|s| s.as_str()))
|
|
380
|
+
{
|
|
381
|
+
diag = diag.with_help(format!("did you mean `{sugg}`?"));
|
|
382
|
+
}
|
|
383
|
+
Err(PolyError::single(diag))
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
pub fn check_reachable_from_entry(
|
|
387
|
+
&mut self,
|
|
388
|
+
entry_mod: usize,
|
|
389
|
+
entry_fn: &str,
|
|
390
|
+
) -> Result<Vec<(usize, String)>, PolyError> {
|
|
391
|
+
let actual_entry = self
|
|
392
|
+
.resolve_entry_fn_name(entry_mod, entry_fn)
|
|
393
|
+
.unwrap_or_else(|| entry_fn.to_string());
|
|
394
|
+
let mut visited: FxHashSet<(usize, String)> = FxHashSet::default();
|
|
395
|
+
let mut queue: Vec<(usize, String, Span)> =
|
|
396
|
+
vec![(entry_mod, actual_entry, Span::dummy())];
|
|
397
|
+
let mut order = Vec::new();
|
|
398
|
+
|
|
399
|
+
while let Some((mod_id, fn_name, _sp)) = queue.pop() {
|
|
400
|
+
if !visited.insert((mod_id, fn_name.clone())) {
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
order.push((mod_id, fn_name.clone()));
|
|
404
|
+
|
|
405
|
+
let idx = *self.modules[mod_id]
|
|
406
|
+
.function_index
|
|
407
|
+
.get(&fn_name)
|
|
408
|
+
.ok_or_else(|| {
|
|
409
|
+
PolyError::msg(
|
|
410
|
+
"E0208",
|
|
411
|
+
format!(
|
|
412
|
+
"entry function '{fn_name}' not found in '{}'",
|
|
413
|
+
self.modules[mod_id].path.display()
|
|
414
|
+
),
|
|
415
|
+
)
|
|
416
|
+
})?;
|
|
417
|
+
|
|
418
|
+
let stmts = self.modules[mod_id].ast.functions[idx].body.clone();
|
|
419
|
+
let mut calls = Vec::new();
|
|
420
|
+
collect_static_calls_in_stmts(&stmts, &mut calls);
|
|
421
|
+
|
|
422
|
+
for (callee_kind, cspan) in calls {
|
|
423
|
+
match callee_kind {
|
|
424
|
+
StaticCall::Direct(name) => {
|
|
425
|
+
if crate::ir::BuiltinId::from_name(&name).is_some()
|
|
426
|
+
|| matches!(
|
|
427
|
+
name.as_str(),
|
|
428
|
+
"static_cast" | "reinterpret_cast" | "const_cast" | "dynamic_cast"
|
|
429
|
+
)
|
|
430
|
+
{
|
|
431
|
+
continue;
|
|
432
|
+
}
|
|
433
|
+
if is_local_param_or_var(&self.modules[mod_id].ast.functions[idx], &name) {
|
|
434
|
+
continue;
|
|
435
|
+
}
|
|
436
|
+
if let Some((t_mod, t_fn)) = self
|
|
437
|
+
.resolve_symbol_lazy(mod_id, &name, cspan)
|
|
438
|
+
.ok()
|
|
439
|
+
{
|
|
440
|
+
queue.push((t_mod, t_fn, cspan));
|
|
441
|
+
} else if !self.modules[mod_id].struct_index.contains_key(&name) {
|
|
442
|
+
let (t_mod, t_fn) = self.resolve_symbol_lazy(mod_id, &name, cspan)?;
|
|
443
|
+
queue.push((t_mod, t_fn, cspan));
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
StaticCall::Qualified(alias, member) => {
|
|
447
|
+
if matches!(
|
|
448
|
+
alias.as_str(),
|
|
449
|
+
"console" | "Console" | "Math" | "math" | "std" | "fmt" | "strings" | "re" | "regexp" | "Regex" | "String" | "Number" | "JSON"
|
|
450
|
+
) {
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
if self.modules[mod_id].bindings.namespaces.contains_key(&alias)
|
|
454
|
+
|| self.modules[mod_id]
|
|
455
|
+
.function_index
|
|
456
|
+
.contains_key(&format!("{alias}::{member}"))
|
|
457
|
+
{
|
|
458
|
+
let (t_mod, t_fn) =
|
|
459
|
+
self.resolve_member_function_lazy(mod_id, &alias, &member, cspan)?;
|
|
460
|
+
queue.push((t_mod, t_fn, cspan));
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
Ok(order)
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
fn build_import_bindings(imports: &[ImportDecl]) -> ImportBindings {
|
|
472
|
+
let mut b = ImportBindings::default();
|
|
473
|
+
for decl in imports {
|
|
474
|
+
match &decl.kind {
|
|
475
|
+
ImportKind::Namespace { alias } => {
|
|
476
|
+
b.namespaces
|
|
477
|
+
.insert(alias.clone(), decl.module_specifier.clone());
|
|
478
|
+
}
|
|
479
|
+
ImportKind::Named { items } => {
|
|
480
|
+
for item in items {
|
|
481
|
+
b.named.insert(
|
|
482
|
+
item.alias.clone(),
|
|
483
|
+
ImportedSymbol {
|
|
484
|
+
source_specifier: decl.module_specifier.clone(),
|
|
485
|
+
original_name: item.name.clone(),
|
|
486
|
+
span: item.span,
|
|
487
|
+
},
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
ImportKind::Wildcard => {
|
|
492
|
+
b.wildcards
|
|
493
|
+
.push((decl.module_specifier.clone(), decl.span));
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
b
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
fn canonicalize_soft(path: &Path) -> Result<PathBuf, PolyError> {
|
|
501
|
+
if let Ok(c) = std::fs::canonicalize(path) {
|
|
502
|
+
return Ok(c);
|
|
503
|
+
}
|
|
504
|
+
if path.is_absolute() {
|
|
505
|
+
Ok(path.to_path_buf())
|
|
506
|
+
} else {
|
|
507
|
+
Ok(std::env::current_dir()?.join(path))
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
enum StaticCall {
|
|
512
|
+
Direct(String),
|
|
513
|
+
Qualified(String, String),
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
fn is_local_param_or_var(f: &FunctionDecl, name: &str) -> bool {
|
|
517
|
+
if f.params.iter().any(|p| p.name == name) {
|
|
518
|
+
return true;
|
|
519
|
+
}
|
|
520
|
+
fn has_var(stmts: &[Stmt], name: &str) -> bool {
|
|
521
|
+
for s in stmts {
|
|
522
|
+
match s {
|
|
523
|
+
Stmt::VarDecl { name: n, .. } if n == name => return true,
|
|
524
|
+
Stmt::MultiVarDecl { names, .. } if names.iter().any(|n| n == name) => return true,
|
|
525
|
+
Stmt::If {
|
|
526
|
+
init,
|
|
527
|
+
then_body,
|
|
528
|
+
else_body,
|
|
529
|
+
..
|
|
530
|
+
} => {
|
|
531
|
+
if init.as_ref().is_some_and(|i| has_var(&[(**i).clone()], name))
|
|
532
|
+
|| has_var(then_body, name)
|
|
533
|
+
|| has_var(else_body, name)
|
|
534
|
+
{
|
|
535
|
+
return true;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
Stmt::While { body, .. }
|
|
539
|
+
| Stmt::ForRange { body, .. }
|
|
540
|
+
| Stmt::ForIn { body, .. }
|
|
541
|
+
| Stmt::Block(body, _) => {
|
|
542
|
+
if has_var(body, name) {
|
|
543
|
+
return true;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
Stmt::Switch { cases, default, .. } => {
|
|
547
|
+
if cases.iter().any(|(_, b)| has_var(b, name)) || has_var(default, name) {
|
|
548
|
+
return true;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
Stmt::TryCatch {
|
|
552
|
+
try_body,
|
|
553
|
+
catch_var,
|
|
554
|
+
catch_body,
|
|
555
|
+
finally_body,
|
|
556
|
+
..
|
|
557
|
+
} => {
|
|
558
|
+
if catch_var.as_deref() == Some(name)
|
|
559
|
+
|| has_var(try_body, name)
|
|
560
|
+
|| has_var(catch_body, name)
|
|
561
|
+
|| has_var(finally_body, name)
|
|
562
|
+
{
|
|
563
|
+
return true;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
_ => {}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
false
|
|
570
|
+
}
|
|
571
|
+
has_var(&f.body, name)
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
fn collect_static_calls_in_stmts(stmts: &[Stmt], out: &mut Vec<(StaticCall, Span)>) {
|
|
575
|
+
for s in stmts {
|
|
576
|
+
match s {
|
|
577
|
+
Stmt::VarDecl { init: Some(e), .. }
|
|
578
|
+
| Stmt::MultiVarDecl { init: e, .. }
|
|
579
|
+
| Stmt::Throw(e, _)
|
|
580
|
+
| Stmt::Defer(e, _)
|
|
581
|
+
| Stmt::Return(Some(e), _)
|
|
582
|
+
| Stmt::ExprStmt(e, _) => collect_calls_in_expr(e, out),
|
|
583
|
+
Stmt::Assign { target, value, .. } => {
|
|
584
|
+
match target {
|
|
585
|
+
AssignTarget::Field(obj, _) => collect_calls_in_expr(obj, out),
|
|
586
|
+
AssignTarget::Index(obj, idx) => {
|
|
587
|
+
collect_calls_in_expr(obj, out);
|
|
588
|
+
collect_calls_in_expr(idx, out);
|
|
589
|
+
}
|
|
590
|
+
AssignTarget::Var(_) | AssignTarget::Tuple(_) => {}
|
|
591
|
+
}
|
|
592
|
+
collect_calls_in_expr(value, out);
|
|
593
|
+
}
|
|
594
|
+
Stmt::If {
|
|
595
|
+
init,
|
|
596
|
+
cond,
|
|
597
|
+
then_body,
|
|
598
|
+
else_body,
|
|
599
|
+
..
|
|
600
|
+
} => {
|
|
601
|
+
if let Some(i) = init {
|
|
602
|
+
collect_static_calls_in_stmts(&[(**i).clone()], out);
|
|
603
|
+
}
|
|
604
|
+
collect_calls_in_expr(cond, out);
|
|
605
|
+
collect_static_calls_in_stmts(then_body, out);
|
|
606
|
+
collect_static_calls_in_stmts(else_body, out);
|
|
607
|
+
}
|
|
608
|
+
Stmt::While { cond, body, .. } => {
|
|
609
|
+
collect_calls_in_expr(cond, out);
|
|
610
|
+
collect_static_calls_in_stmts(body, out);
|
|
611
|
+
}
|
|
612
|
+
Stmt::ForRange {
|
|
613
|
+
start,
|
|
614
|
+
end,
|
|
615
|
+
step,
|
|
616
|
+
body,
|
|
617
|
+
..
|
|
618
|
+
} => {
|
|
619
|
+
collect_calls_in_expr(start, out);
|
|
620
|
+
collect_calls_in_expr(end, out);
|
|
621
|
+
if let Some(st) = step {
|
|
622
|
+
collect_calls_in_expr(st, out);
|
|
623
|
+
}
|
|
624
|
+
collect_static_calls_in_stmts(body, out);
|
|
625
|
+
}
|
|
626
|
+
Stmt::ForIn { iter, body, .. } => {
|
|
627
|
+
collect_calls_in_expr(iter, out);
|
|
628
|
+
collect_static_calls_in_stmts(body, out);
|
|
629
|
+
}
|
|
630
|
+
Stmt::Switch {
|
|
631
|
+
expr,
|
|
632
|
+
cases,
|
|
633
|
+
default,
|
|
634
|
+
..
|
|
635
|
+
} => {
|
|
636
|
+
if let Some(e) = expr {
|
|
637
|
+
collect_calls_in_expr(e, out);
|
|
638
|
+
}
|
|
639
|
+
for (vals, body) in cases {
|
|
640
|
+
for v in vals {
|
|
641
|
+
collect_calls_in_expr(v, out);
|
|
642
|
+
}
|
|
643
|
+
collect_static_calls_in_stmts(body, out);
|
|
644
|
+
}
|
|
645
|
+
collect_static_calls_in_stmts(default, out);
|
|
646
|
+
}
|
|
647
|
+
Stmt::TryCatch {
|
|
648
|
+
try_body,
|
|
649
|
+
catch_body,
|
|
650
|
+
finally_body,
|
|
651
|
+
..
|
|
652
|
+
} => {
|
|
653
|
+
collect_static_calls_in_stmts(try_body, out);
|
|
654
|
+
collect_static_calls_in_stmts(catch_body, out);
|
|
655
|
+
collect_static_calls_in_stmts(finally_body, out);
|
|
656
|
+
}
|
|
657
|
+
Stmt::Block(b, _) => collect_static_calls_in_stmts(b, out),
|
|
658
|
+
_ => {}
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
fn collect_calls_in_expr(expr: &Expr, out: &mut Vec<(StaticCall, Span)>) {
|
|
664
|
+
match expr {
|
|
665
|
+
Expr::Call { callee, args, span } => {
|
|
666
|
+
match &**callee {
|
|
667
|
+
Expr::Var(name, _) => out.push((StaticCall::Direct(name.clone()), *span)),
|
|
668
|
+
Expr::Member { object, field, .. } => {
|
|
669
|
+
if let Expr::Var(ns, _) = &**object {
|
|
670
|
+
out.push((StaticCall::Qualified(ns.clone(), field.clone()), *span));
|
|
671
|
+
} else {
|
|
672
|
+
collect_calls_in_expr(object, out);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
other => collect_calls_in_expr(other, out),
|
|
676
|
+
}
|
|
677
|
+
for a in args {
|
|
678
|
+
collect_calls_in_expr(a, out);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
Expr::TemplateStr { parts, .. } => {
|
|
682
|
+
for p in parts {
|
|
683
|
+
if let TemplatePart::Expr { expr, .. } = p {
|
|
684
|
+
collect_calls_in_expr(expr, out);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
Expr::Array(items, _) | Expr::Tuple(items, _) => {
|
|
689
|
+
for i in items {
|
|
690
|
+
collect_calls_in_expr(i, out);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
Expr::ObjectLit(_, fields, _) => {
|
|
694
|
+
for (_, v) in fields {
|
|
695
|
+
collect_calls_in_expr(v, out);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
Expr::Unary(_, inner, _) | Expr::Cast { expr: inner, .. } => {
|
|
699
|
+
collect_calls_in_expr(inner, out)
|
|
700
|
+
}
|
|
701
|
+
Expr::Binary(_, l, r, _) => {
|
|
702
|
+
collect_calls_in_expr(l, out);
|
|
703
|
+
collect_calls_in_expr(r, out);
|
|
704
|
+
}
|
|
705
|
+
Expr::Member { object, .. } => collect_calls_in_expr(object, out),
|
|
706
|
+
Expr::Index { object, index, .. } => {
|
|
707
|
+
collect_calls_in_expr(object, out);
|
|
708
|
+
collect_calls_in_expr(index, out);
|
|
709
|
+
}
|
|
710
|
+
Expr::Ternary {
|
|
711
|
+
cond,
|
|
712
|
+
then_expr,
|
|
713
|
+
else_expr,
|
|
714
|
+
..
|
|
715
|
+
} => {
|
|
716
|
+
collect_calls_in_expr(cond, out);
|
|
717
|
+
collect_calls_in_expr(then_expr, out);
|
|
718
|
+
collect_calls_in_expr(else_expr, out);
|
|
719
|
+
}
|
|
720
|
+
_ => {}
|
|
721
|
+
}
|
|
722
|
+
}
|