polycomp 0.0.0-stage → 0.2.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 +24 -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 +246 -0
- package/src/bundle.rs +3180 -0
- package/src/diagnostics.rs +276 -0
- package/src/interpreter.rs +1214 -0
- package/src/ir.rs +239 -0
- package/src/jit.rs +1099 -0
- package/src/lib.rs +11 -0
- package/src/lowering.rs +826 -0
- package/src/main.rs +634 -0
- package/src/optimizer.rs +602 -0
- package/src/parser.rs +2605 -0
- package/src/resolver.rs +613 -0
- package/src/runtime.rs +829 -0
- package/README.md +0 -3
package/src/resolver.rs
ADDED
|
@@ -0,0 +1,613 @@
|
|
|
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
|
+
/// alias -> target module specifier
|
|
17
|
+
pub namespaces: FxHashMap<String, String>,
|
|
18
|
+
/// local_name -> (target module specifier, original exported name)
|
|
19
|
+
pub named: FxHashMap<String, ImportedSymbol>,
|
|
20
|
+
/// Wildcard/require specifiers searched in order if symbol isn't locally defined
|
|
21
|
+
pub wildcards: Vec<(String, Span)>,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
#[derive(Debug, Clone)]
|
|
25
|
+
pub struct LoadedModule {
|
|
26
|
+
pub id: usize,
|
|
27
|
+
pub path: PathBuf,
|
|
28
|
+
pub language: Language,
|
|
29
|
+
pub ast: ModuleAst,
|
|
30
|
+
pub bindings: ImportBindings,
|
|
31
|
+
pub function_index: FxHashMap<String, usize>,
|
|
32
|
+
pub struct_index: FxHashMap<String, usize>,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
#[derive(Debug, Clone, Default)]
|
|
36
|
+
pub struct ImportStats {
|
|
37
|
+
pub modules_parsed: usize,
|
|
38
|
+
pub parsed_paths: Vec<PathBuf>,
|
|
39
|
+
pub cache_hits: usize,
|
|
40
|
+
}
|
|
41
|
+
#[derive(Debug, Clone)]
|
|
42
|
+
pub struct StaticCheckReport {
|
|
43
|
+
pub modules_parsed: usize,
|
|
44
|
+
pub reachable_functions: usize,
|
|
45
|
+
pub parsed_paths: Vec<PathBuf>,
|
|
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
|
+
/// Loads & parses ONLY the requested file (never eagerly parsing its imports).
|
|
68
|
+
pub fn load_module(&mut self, raw_path: &Path) -> Result<usize, PolyError> {
|
|
69
|
+
let canonical = canonicalize_soft(raw_path)?;
|
|
70
|
+
if let Some(&id) = self.path_to_id.get(&canonical) {
|
|
71
|
+
self.stats.cache_hits += 1;
|
|
72
|
+
return Ok(id);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let lang = Language::from_path(&canonical).ok_or_else(|| {
|
|
76
|
+
PolyError::msg(
|
|
77
|
+
"E0201",
|
|
78
|
+
format!(
|
|
79
|
+
"unsupported source extension for '{}'; expected .rs, .py, .js, .ts, .c, or .h",
|
|
80
|
+
canonical.display()
|
|
81
|
+
),
|
|
82
|
+
)
|
|
83
|
+
})?;
|
|
84
|
+
|
|
85
|
+
let source = std::fs::read_to_string(&canonical).map_err(|e| {
|
|
86
|
+
PolyError::msg(
|
|
87
|
+
"E0202",
|
|
88
|
+
format!("failed to read module '{}': {e}", canonical.display()),
|
|
89
|
+
)
|
|
90
|
+
})?;
|
|
91
|
+
|
|
92
|
+
let file_id = self.sources.add_file(canonical.clone(), source);
|
|
93
|
+
let src_ref = &self.sources.get(file_id).unwrap().source;
|
|
94
|
+
let ast = parse_module(file_id, lang, src_ref)?;
|
|
95
|
+
|
|
96
|
+
let bindings = build_import_bindings(&ast.imports);
|
|
97
|
+
let mut function_index = FxHashMap::default();
|
|
98
|
+
for (i, f) in ast.functions.iter().enumerate() {
|
|
99
|
+
function_index.insert(f.name.clone(), i);
|
|
100
|
+
}
|
|
101
|
+
let mut struct_index = FxHashMap::default();
|
|
102
|
+
for (i, s) in ast.structs.iter().enumerate() {
|
|
103
|
+
struct_index.insert(s.name.clone(), i);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
let mod_id = self.modules.len();
|
|
107
|
+
self.stats.modules_parsed += 1;
|
|
108
|
+
self.stats.parsed_paths.push(canonical.clone());
|
|
109
|
+
if self.trace_imports {
|
|
110
|
+
eprintln!(
|
|
111
|
+
"[polycomp:import] parsed module #{mod_id} ({}) <- {}",
|
|
112
|
+
lang.name(),
|
|
113
|
+
canonical.display()
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
self.modules.push(LoadedModule {
|
|
118
|
+
id: mod_id,
|
|
119
|
+
path: canonical.clone(),
|
|
120
|
+
language: lang,
|
|
121
|
+
ast,
|
|
122
|
+
bindings,
|
|
123
|
+
function_index,
|
|
124
|
+
struct_index,
|
|
125
|
+
});
|
|
126
|
+
self.path_to_id.insert(canonical, mod_id);
|
|
127
|
+
Ok(mod_id)
|
|
128
|
+
}
|
|
129
|
+
/// Loads source text directly from memory (stdin / JS raw source),
|
|
130
|
+
/// resolving relative imports against `virtual_path`'s directory.
|
|
131
|
+
pub fn load_source_in_memory(
|
|
132
|
+
&mut self,
|
|
133
|
+
virtual_path: &Path,
|
|
134
|
+
lang: Language,
|
|
135
|
+
source: String,
|
|
136
|
+
) -> Result<usize, PolyError> {
|
|
137
|
+
let abs = if virtual_path.is_absolute() {
|
|
138
|
+
virtual_path.to_path_buf()
|
|
139
|
+
} else {
|
|
140
|
+
std::env::current_dir()?.join(virtual_path)
|
|
141
|
+
};
|
|
142
|
+
let file_id = self.sources.add_file(abs.clone(), source);
|
|
143
|
+
let src_ref = &self.sources.get(file_id).unwrap().source;
|
|
144
|
+
let ast = parse_module(file_id, lang, src_ref)?;
|
|
145
|
+
|
|
146
|
+
let bindings = build_import_bindings(&ast.imports);
|
|
147
|
+
let mut function_index = FxHashMap::default();
|
|
148
|
+
for (i, f) in ast.functions.iter().enumerate() {
|
|
149
|
+
function_index.insert(f.name.clone(), i);
|
|
150
|
+
}
|
|
151
|
+
let mut struct_index = FxHashMap::default();
|
|
152
|
+
for (i, s) in ast.structs.iter().enumerate() {
|
|
153
|
+
struct_index.insert(s.name.clone(), i);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
let mod_id = self.modules.len();
|
|
157
|
+
self.stats.modules_parsed += 1;
|
|
158
|
+
self.stats.parsed_paths.push(abs.clone());
|
|
159
|
+
if self.trace_imports {
|
|
160
|
+
eprintln!(
|
|
161
|
+
"[polycomp:import] parsed inline module #{mod_id} ({}) <- {}",
|
|
162
|
+
lang.name(),
|
|
163
|
+
abs.display()
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
self.modules.push(LoadedModule {
|
|
168
|
+
id: mod_id,
|
|
169
|
+
path: abs.clone(),
|
|
170
|
+
language: lang,
|
|
171
|
+
ast,
|
|
172
|
+
bindings,
|
|
173
|
+
function_index,
|
|
174
|
+
struct_index,
|
|
175
|
+
});
|
|
176
|
+
self.path_to_id.insert(abs, mod_id);
|
|
177
|
+
Ok(mod_id)
|
|
178
|
+
}
|
|
179
|
+
pub fn get_module(&self, id: usize) -> &LoadedModule {
|
|
180
|
+
&self.modules[id]
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/// Resolves a module specifier relative to `from_module_id`, probing extensions if omitted.
|
|
184
|
+
pub fn resolve_specifier_path(
|
|
185
|
+
&self,
|
|
186
|
+
from_module_id: usize,
|
|
187
|
+
specifier: &str,
|
|
188
|
+
span: Span,
|
|
189
|
+
) -> Result<PathBuf, PolyError> {
|
|
190
|
+
let base_dir = self.modules[from_module_id]
|
|
191
|
+
.path
|
|
192
|
+
.parent()
|
|
193
|
+
.unwrap_or_else(|| Path::new("."));
|
|
194
|
+
|
|
195
|
+
let candidate = base_dir.join(specifier);
|
|
196
|
+
if candidate.is_file() {
|
|
197
|
+
return canonicalize_soft(&candidate);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if candidate.extension().is_none() {
|
|
201
|
+
for ext in ["rs", "py", "js", "c", "ts", "h"] {
|
|
202
|
+
let with_ext = candidate.with_extension(ext);
|
|
203
|
+
if with_ext.is_file() {
|
|
204
|
+
return canonicalize_soft(&with_ext);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
for ext in ["rs", "py", "js", "c"] {
|
|
208
|
+
let index = candidate.join(format!("mod.{ext}"));
|
|
209
|
+
if index.is_file() {
|
|
210
|
+
return canonicalize_soft(&index);
|
|
211
|
+
}
|
|
212
|
+
let idx_js = candidate.join(format!("index.{ext}"));
|
|
213
|
+
if idx_js.is_file() {
|
|
214
|
+
return canonicalize_soft(&idx_js);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
Err(PolyError::single(
|
|
220
|
+
Diagnostic::error(
|
|
221
|
+
"E0203",
|
|
222
|
+
format!(
|
|
223
|
+
"cannot find imported module '{}' (searched relative to '{}')",
|
|
224
|
+
specifier,
|
|
225
|
+
base_dir.display()
|
|
226
|
+
),
|
|
227
|
+
)
|
|
228
|
+
.with_span(span)
|
|
229
|
+
.with_help("check the relative path or file extension (.rs, .py, .js, .c)"),
|
|
230
|
+
))
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/// Resolves a symbol lazily when actually invoked at runtime (or checked).
|
|
234
|
+
pub fn resolve_symbol_lazy(
|
|
235
|
+
&mut self,
|
|
236
|
+
from_module_id: usize,
|
|
237
|
+
symbol: &str,
|
|
238
|
+
span: Span,
|
|
239
|
+
) -> Result<(usize, String), PolyError> {
|
|
240
|
+
// 1. Local function inside the calling module
|
|
241
|
+
if self.modules[from_module_id]
|
|
242
|
+
.function_index
|
|
243
|
+
.contains_key(symbol)
|
|
244
|
+
{
|
|
245
|
+
return Ok((from_module_id, symbol.to_string()));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// 2. Explicit named import (`from M import x, y as alias` / `import x, y from M`)
|
|
249
|
+
if let Some(imp) = self.modules[from_module_id].bindings.named.get(symbol).cloned() {
|
|
250
|
+
let target_path =
|
|
251
|
+
self.resolve_specifier_path(from_module_id, &imp.source_specifier, imp.span)?;
|
|
252
|
+
let target_mod_id = self.load_module(&target_path)?;
|
|
253
|
+
return self.verify_exported_symbol(target_mod_id, &imp.original_name, span);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// 3. Wildcard / `polycomp require` fallback chain (lazy: loaded in declaration order until match)
|
|
257
|
+
let wildcards = self.modules[from_module_id].bindings.wildcards.clone();
|
|
258
|
+
for (spec, wspan) in wildcards {
|
|
259
|
+
let target_path = self.resolve_specifier_path(from_module_id, &spec, wspan)?;
|
|
260
|
+
let target_mod_id = self.load_module(&target_path)?;
|
|
261
|
+
if let Some(&fn_idx) = self.modules[target_mod_id].function_index.get(symbol) {
|
|
262
|
+
let f = &self.modules[target_mod_id].ast.functions[fn_idx];
|
|
263
|
+
if !f.is_exported {
|
|
264
|
+
return Err(PolyError::single(
|
|
265
|
+
Diagnostic::error(
|
|
266
|
+
"E0204",
|
|
267
|
+
format!(
|
|
268
|
+
"function '{}' in '{}' is private; mark it `pub` or `export`",
|
|
269
|
+
symbol,
|
|
270
|
+
self.modules[target_mod_id].path.display()
|
|
271
|
+
),
|
|
272
|
+
)
|
|
273
|
+
.with_span(span),
|
|
274
|
+
));
|
|
275
|
+
}
|
|
276
|
+
return Ok((target_mod_id, symbol.to_string()));
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Produce helpful suggestion from already-loaded local + named symbols
|
|
281
|
+
let mut known: Vec<String> = self.modules[from_module_id]
|
|
282
|
+
.function_index
|
|
283
|
+
.keys()
|
|
284
|
+
.cloned()
|
|
285
|
+
.collect();
|
|
286
|
+
known.extend(
|
|
287
|
+
self.modules[from_module_id]
|
|
288
|
+
.bindings
|
|
289
|
+
.named
|
|
290
|
+
.keys()
|
|
291
|
+
.cloned(),
|
|
292
|
+
);
|
|
293
|
+
let mut diag = Diagnostic::error(
|
|
294
|
+
"E0205",
|
|
295
|
+
format!("undefined function '{symbol}'"),
|
|
296
|
+
)
|
|
297
|
+
.with_span(span);
|
|
298
|
+
if let Some(sugg) = suggest_did_you_mean(symbol, known.iter().map(|s| s.as_str())) {
|
|
299
|
+
diag = diag.with_help(format!("did you mean `{sugg}`?"));
|
|
300
|
+
}
|
|
301
|
+
Err(PolyError::single(diag))
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
pub fn resolve_member_function_lazy(
|
|
305
|
+
&mut self,
|
|
306
|
+
from_module_id: usize,
|
|
307
|
+
namespace_alias: &str,
|
|
308
|
+
member_name: &str,
|
|
309
|
+
span: Span,
|
|
310
|
+
) -> Result<(usize, String), PolyError> {
|
|
311
|
+
let Some(spec) = self.modules[from_module_id]
|
|
312
|
+
.bindings
|
|
313
|
+
.namespaces
|
|
314
|
+
.get(namespace_alias)
|
|
315
|
+
.cloned()
|
|
316
|
+
else {
|
|
317
|
+
return Err(PolyError::spanned(
|
|
318
|
+
"E0206",
|
|
319
|
+
format!("unknown module alias '{namespace_alias}'"),
|
|
320
|
+
span,
|
|
321
|
+
));
|
|
322
|
+
};
|
|
323
|
+
let path = self.resolve_specifier_path(from_module_id, &spec, span)?;
|
|
324
|
+
let target_mod_id = self.load_module(&path)?;
|
|
325
|
+
self.verify_exported_symbol(target_mod_id, member_name, span)
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
fn verify_exported_symbol(
|
|
329
|
+
&self,
|
|
330
|
+
target_mod_id: usize,
|
|
331
|
+
orig_name: &str,
|
|
332
|
+
call_span: Span,
|
|
333
|
+
) -> Result<(usize, String), PolyError> {
|
|
334
|
+
let target = &self.modules[target_mod_id];
|
|
335
|
+
if let Some(&idx) = target.function_index.get(orig_name) {
|
|
336
|
+
let decl = &target.ast.functions[idx];
|
|
337
|
+
if !decl.is_exported {
|
|
338
|
+
return Err(PolyError::single(
|
|
339
|
+
Diagnostic::error(
|
|
340
|
+
"E0204",
|
|
341
|
+
format!(
|
|
342
|
+
"symbol '{}' in '{}' is not exported",
|
|
343
|
+
orig_name,
|
|
344
|
+
target.path.display()
|
|
345
|
+
),
|
|
346
|
+
)
|
|
347
|
+
.with_span(call_span)
|
|
348
|
+
.with_help("add `pub` (Rust) or `export` (JS) to expose it"),
|
|
349
|
+
));
|
|
350
|
+
}
|
|
351
|
+
return Ok((target_mod_id, orig_name.to_string()));
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
let mut diag = Diagnostic::error(
|
|
355
|
+
"E0207",
|
|
356
|
+
format!(
|
|
357
|
+
"module '{}' does not export function '{}'",
|
|
358
|
+
target.path.display(),
|
|
359
|
+
orig_name
|
|
360
|
+
),
|
|
361
|
+
)
|
|
362
|
+
.with_span(call_span);
|
|
363
|
+
if let Some(sugg) =
|
|
364
|
+
suggest_did_you_mean(orig_name, target.function_index.keys().map(|s| s.as_str()))
|
|
365
|
+
{
|
|
366
|
+
diag = diag.with_help(format!("did you mean `{sugg}`?"));
|
|
367
|
+
}
|
|
368
|
+
Err(PolyError::single(diag))
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/// Static reachability validation from entry function (still selective: never touches unused functions/imports).
|
|
372
|
+
pub fn check_reachable_from_entry(
|
|
373
|
+
&mut self,
|
|
374
|
+
entry_mod: usize,
|
|
375
|
+
entry_fn: &str,
|
|
376
|
+
) -> Result<Vec<(usize, String)>, PolyError> {
|
|
377
|
+
let mut visited: FxHashSet<(usize, String)> = FxHashSet::default();
|
|
378
|
+
let mut queue: Vec<(usize, String, Span)> = vec![(entry_mod, entry_fn.to_string(), Span::dummy())];
|
|
379
|
+
let mut order = Vec::new();
|
|
380
|
+
|
|
381
|
+
while let Some((mod_id, fn_name, _sp)) = queue.pop() {
|
|
382
|
+
if !visited.insert((mod_id, fn_name.clone())) {
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
order.push((mod_id, fn_name.clone()));
|
|
386
|
+
|
|
387
|
+
let idx = *self.modules[mod_id]
|
|
388
|
+
.function_index
|
|
389
|
+
.get(&fn_name)
|
|
390
|
+
.ok_or_else(|| {
|
|
391
|
+
PolyError::msg(
|
|
392
|
+
"E0208",
|
|
393
|
+
format!(
|
|
394
|
+
"entry function '{fn_name}' not found in '{}'",
|
|
395
|
+
self.modules[mod_id].path.display()
|
|
396
|
+
),
|
|
397
|
+
)
|
|
398
|
+
})?;
|
|
399
|
+
|
|
400
|
+
let stmts = self.modules[mod_id].ast.functions[idx].body.clone();
|
|
401
|
+
let mut calls = Vec::new();
|
|
402
|
+
collect_static_calls_in_stmts(&stmts, &mut calls);
|
|
403
|
+
|
|
404
|
+
for (callee_kind, cspan) in calls {
|
|
405
|
+
match callee_kind {
|
|
406
|
+
StaticCall::Direct(name) => {
|
|
407
|
+
if crate::ir::BuiltinId::from_name(&name).is_some() {
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
if is_local_param_or_var(&self.modules[mod_id].ast.functions[idx], &name) {
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
let (t_mod, t_fn) = self.resolve_symbol_lazy(mod_id, &name, cspan)?;
|
|
414
|
+
queue.push((t_mod, t_fn, cspan));
|
|
415
|
+
}
|
|
416
|
+
StaticCall::Qualified(alias, member) => {
|
|
417
|
+
if self.modules[mod_id].bindings.namespaces.contains_key(&alias) {
|
|
418
|
+
let (t_mod, t_fn) =
|
|
419
|
+
self.resolve_member_function_lazy(mod_id, &alias, &member, cspan)?;
|
|
420
|
+
queue.push((t_mod, t_fn, cspan));
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
Ok(order)
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
fn build_import_bindings(imports: &[ImportDecl]) -> ImportBindings {
|
|
432
|
+
let mut b = ImportBindings::default();
|
|
433
|
+
for decl in imports {
|
|
434
|
+
match &decl.kind {
|
|
435
|
+
ImportKind::Namespace { alias } => {
|
|
436
|
+
b.namespaces
|
|
437
|
+
.insert(alias.clone(), decl.module_specifier.clone());
|
|
438
|
+
}
|
|
439
|
+
ImportKind::Named { items } => {
|
|
440
|
+
for item in items {
|
|
441
|
+
b.named.insert(
|
|
442
|
+
item.alias.clone(),
|
|
443
|
+
ImportedSymbol {
|
|
444
|
+
source_specifier: decl.module_specifier.clone(),
|
|
445
|
+
original_name: item.name.clone(),
|
|
446
|
+
span: item.span,
|
|
447
|
+
},
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
ImportKind::Wildcard => {
|
|
452
|
+
b.wildcards
|
|
453
|
+
.push((decl.module_specifier.clone(), decl.span));
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
b
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
fn canonicalize_soft(path: &Path) -> Result<PathBuf, PolyError> {
|
|
461
|
+
if let Ok(c) = std::fs::canonicalize(path) {
|
|
462
|
+
return Ok(c);
|
|
463
|
+
}
|
|
464
|
+
if path.is_absolute() {
|
|
465
|
+
Ok(path.to_path_buf())
|
|
466
|
+
} else {
|
|
467
|
+
Ok(std::env::current_dir()?.join(path))
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
enum StaticCall {
|
|
472
|
+
Direct(String),
|
|
473
|
+
Qualified(String, String),
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
fn is_local_param_or_var(f: &FunctionDecl, name: &str) -> bool {
|
|
477
|
+
if f.params.iter().any(|p| p.name == name) {
|
|
478
|
+
return true;
|
|
479
|
+
}
|
|
480
|
+
fn has_var(stmts: &[Stmt], name: &str) -> bool {
|
|
481
|
+
for s in stmts {
|
|
482
|
+
match s {
|
|
483
|
+
Stmt::VarDecl { name: n, .. } if n == name => return true,
|
|
484
|
+
Stmt::If {
|
|
485
|
+
then_body,
|
|
486
|
+
else_body,
|
|
487
|
+
..
|
|
488
|
+
} => {
|
|
489
|
+
if has_var(then_body, name) || has_var(else_body, name) {
|
|
490
|
+
return true;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
Stmt::While { body, .. }
|
|
494
|
+
| Stmt::ForRange { body, .. }
|
|
495
|
+
| Stmt::ForIn { body, .. }
|
|
496
|
+
| Stmt::Block(body, _) => {
|
|
497
|
+
if has_var(body, name) {
|
|
498
|
+
return true;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
_ => {}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
false
|
|
505
|
+
}
|
|
506
|
+
has_var(&f.body, name)
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
fn collect_static_calls_in_stmts(stmts: &[Stmt], out: &mut Vec<(StaticCall, Span)>) {
|
|
510
|
+
for s in stmts {
|
|
511
|
+
match s {
|
|
512
|
+
Stmt::VarDecl { init: Some(e), .. } => collect_calls_in_expr(e, out),
|
|
513
|
+
Stmt::Assign { target, value, .. } => {
|
|
514
|
+
match target {
|
|
515
|
+
AssignTarget::Field(obj, _) => collect_calls_in_expr(obj, out),
|
|
516
|
+
AssignTarget::Index(obj, idx) => {
|
|
517
|
+
collect_calls_in_expr(obj, out);
|
|
518
|
+
collect_calls_in_expr(idx, out);
|
|
519
|
+
}
|
|
520
|
+
AssignTarget::Var(_) => {}
|
|
521
|
+
}
|
|
522
|
+
collect_calls_in_expr(value, out);
|
|
523
|
+
}
|
|
524
|
+
Stmt::If {
|
|
525
|
+
cond,
|
|
526
|
+
then_body,
|
|
527
|
+
else_body,
|
|
528
|
+
..
|
|
529
|
+
} => {
|
|
530
|
+
collect_calls_in_expr(cond, out);
|
|
531
|
+
collect_static_calls_in_stmts(then_body, out);
|
|
532
|
+
collect_static_calls_in_stmts(else_body, out);
|
|
533
|
+
}
|
|
534
|
+
Stmt::While { cond, body, .. } => {
|
|
535
|
+
collect_calls_in_expr(cond, out);
|
|
536
|
+
collect_static_calls_in_stmts(body, out);
|
|
537
|
+
}
|
|
538
|
+
Stmt::ForRange {
|
|
539
|
+
start,
|
|
540
|
+
end,
|
|
541
|
+
step,
|
|
542
|
+
body,
|
|
543
|
+
..
|
|
544
|
+
} => {
|
|
545
|
+
collect_calls_in_expr(start, out);
|
|
546
|
+
collect_calls_in_expr(end, out);
|
|
547
|
+
if let Some(st) = step {
|
|
548
|
+
collect_calls_in_expr(st, out);
|
|
549
|
+
}
|
|
550
|
+
collect_static_calls_in_stmts(body, out);
|
|
551
|
+
}
|
|
552
|
+
Stmt::ForIn { iter, body, .. } => {
|
|
553
|
+
collect_calls_in_expr(iter, out);
|
|
554
|
+
collect_static_calls_in_stmts(body, out);
|
|
555
|
+
}
|
|
556
|
+
Stmt::Return(Some(e), _) | Stmt::ExprStmt(e, _) => collect_calls_in_expr(e, out),
|
|
557
|
+
Stmt::Block(b, _) => collect_static_calls_in_stmts(b, out),
|
|
558
|
+
_ => {}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
fn collect_calls_in_expr(expr: &Expr, out: &mut Vec<(StaticCall, Span)>) {
|
|
564
|
+
match expr {
|
|
565
|
+
Expr::Call { callee, args, span } => {
|
|
566
|
+
match &**callee {
|
|
567
|
+
Expr::Var(name, _) => out.push((StaticCall::Direct(name.clone()), *span)),
|
|
568
|
+
Expr::Member { object, field, .. } => {
|
|
569
|
+
if let Expr::Var(ns, _) = &**object {
|
|
570
|
+
out.push((StaticCall::Qualified(ns.clone(), field.clone()), *span));
|
|
571
|
+
} else {
|
|
572
|
+
collect_calls_in_expr(object, out);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
other => collect_calls_in_expr(other, out),
|
|
576
|
+
}
|
|
577
|
+
for a in args {
|
|
578
|
+
collect_calls_in_expr(a, out);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
Expr::Array(items, _) => {
|
|
582
|
+
for i in items {
|
|
583
|
+
collect_calls_in_expr(i, out);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
Expr::ObjectLit(_, fields, _) => {
|
|
587
|
+
for (_, v) in fields {
|
|
588
|
+
collect_calls_in_expr(v, out);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
Expr::Unary(_, inner, _) => collect_calls_in_expr(inner, out),
|
|
592
|
+
Expr::Binary(_, l, r, _) => {
|
|
593
|
+
collect_calls_in_expr(l, out);
|
|
594
|
+
collect_calls_in_expr(r, out);
|
|
595
|
+
}
|
|
596
|
+
Expr::Member { object, .. } => collect_calls_in_expr(object, out),
|
|
597
|
+
Expr::Index { object, index, .. } => {
|
|
598
|
+
collect_calls_in_expr(object, out);
|
|
599
|
+
collect_calls_in_expr(index, out);
|
|
600
|
+
}
|
|
601
|
+
Expr::Ternary {
|
|
602
|
+
cond,
|
|
603
|
+
then_expr,
|
|
604
|
+
else_expr,
|
|
605
|
+
..
|
|
606
|
+
} => {
|
|
607
|
+
collect_calls_in_expr(cond, out);
|
|
608
|
+
collect_calls_in_expr(then_expr, out);
|
|
609
|
+
collect_calls_in_expr(else_expr, out);
|
|
610
|
+
}
|
|
611
|
+
_ => {}
|
|
612
|
+
}
|
|
613
|
+
}
|