windjammer 0.48.0

A simple language inspired by Go, Ruby, and Elixir that transpiles to Rust - 80% of Rust's power with 20% of the complexity
Documentation
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! Function signature storage and lookup for ownership inference.

use std::collections::{HashMap, HashSet};
use std::sync::OnceLock;

use crate::parser::Type;

use super::OwnershipMode;

#[derive(Debug, Clone)]
pub struct FunctionSignature {
    pub name: String,
    pub param_types: Vec<Type>,
    pub param_ownership: Vec<OwnershipMode>,
    pub return_type: Option<Type>,
    pub return_ownership: OwnershipMode,
    pub has_self_receiver: bool,
    pub is_extern: bool,
}

impl FunctionSignature {
    /// Map a call-site argument index to the corresponding parameter index,
    /// accounting for implicit `self` receivers.
    ///
    /// When `has_self_receiver` is true, `param_ownership[0]` and
    /// `param_types[0]` correspond to `self`, so the first user-supplied
    /// argument maps to index 1.
    pub fn arg_param_index(&self, arg_index: usize) -> usize {
        if self.has_self_receiver {
            arg_index + 1
        } else {
            arg_index
        }
    }

    /// Get the ownership mode for a call-site argument, accounting for `self`.
    pub fn param_ownership_for_arg(&self, arg_index: usize) -> Option<&OwnershipMode> {
        self.param_ownership.get(self.arg_param_index(arg_index))
    }

    /// Get the type for a call-site argument, accounting for `self`.
    pub fn param_type_for_arg(&self, arg_index: usize) -> Option<&Type> {
        self.param_types.get(self.arg_param_index(arg_index))
    }
}

static STDLIB_BASELINE: OnceLock<SignatureRegistry> = OnceLock::new();

#[derive(Debug, Clone)]
pub struct SignatureRegistry {
    pub signatures: HashMap<String, FunctionSignature>,
    /// Param-type mismatches (namespace collisions) — used for int→float cast safety.
    type_collision_keys: HashSet<String>,
    /// Same param types but different ownership — used for auto-borrow safety.
    ownership_collision_keys: HashSet<String>,
    method_index: HashMap<String, Vec<String>>,
}

impl Default for SignatureRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl SignatureRegistry {
    pub fn new() -> Self {
        let baseline = STDLIB_BASELINE.get_or_init(|| {
            let mut registry = SignatureRegistry {
                signatures: HashMap::new(),
                type_collision_keys: HashSet::new(),
                ownership_collision_keys: HashSet::new(),
                method_index: HashMap::new(),
            };

            if let Err(e) = crate::stdlib_scanner::populate_runtime_signatures(&mut registry) {
                eprintln!("Warning: Failed to scan runtime signatures: {}", e);
                eprintln!("Continuing with empty registry - may generate incorrect borrows");
            }

            Self::load_stdlib_meta(&mut registry);
            registry
        });

        baseline.clone()
    }

    /// Lightweight empty registry (no stdlib) for building deltas.
    pub fn empty() -> Self {
        SignatureRegistry {
            signatures: HashMap::new(),
            type_collision_keys: HashSet::new(),
            ownership_collision_keys: HashSet::new(),
            method_index: HashMap::new(),
        }
    }

    fn load_stdlib_meta(registry: &mut Self) {
        use std::path::Path;

        let candidates = [
            Path::new("stdlib_meta").to_path_buf(),
            Path::new(env!("CARGO_MANIFEST_DIR")).join("stdlib_meta"),
        ];

        for dir in &candidates {
            if dir.is_dir() {
                crate::metadata::merge_wj_meta_signatures_from_dir(dir, registry);
                return;
            }
        }
    }

    pub fn add_function(&mut self, name: String, sig: FunctionSignature) {
        if let Some(existing) = self.signatures.get(&name) {
            if existing.param_types != sig.param_types {
                // Empty-param runtime/stdlib stubs (e.g. `Config::new()`) are
                // intentionally shadowed by user-defined constructors — not
                // ambiguous collisions.
                let stub_like = existing.param_types.is_empty() || sig.param_types.is_empty();
                if !stub_like {
                    self.type_collision_keys.insert(name.clone());
                }
            } else if existing.param_ownership != sig.param_ownership {
                self.ownership_collision_keys.insert(name.clone());
            }
            if name.contains("::") && existing.has_self_receiver && !sig.has_self_receiver {
                return;
            }
        }
        if let Some(suffix) = name.rsplit_once("::").map(|(_, s)| s.to_string()) {
            self.method_index
                .entry(suffix)
                .or_default()
                .push(name.clone());
        }
        self.signatures.insert(name, sig);
    }

    pub fn get_signature(&self, name: &str) -> Option<&FunctionSignature> {
        self.signatures.get(name)
    }

    /// Check if a signature key has been registered with conflicting param types
    /// from different modules (namespace collision).
    pub fn has_collision(&self, name: &str) -> bool {
        if self.type_collision_keys.contains(name) || self.ownership_collision_keys.contains(name) {
            return true;
        }
        self.has_method_name_collision(name)
    }

    /// True when multiple qualified methods share this suffix (e.g. `new`) with
    /// incompatible param types — used for unqualified calls like `Emitter::new`.
    pub fn has_method_name_collision(&self, method: &str) -> bool {
        self.has_method_name_collision_for_type(None, method)
    }

    /// Whether int→float auto-cast should be skipped for safety.
    ///
    /// Skips when the exact qualified key has a param-type collision, or when
    /// multiple implementations of `method` on the same (or unknown) type disagree
    /// on parameter types.
    pub fn should_skip_int_to_float_auto_cast(
        &self,
        type_name: Option<&str>,
        method: &str,
        qualified_key: Option<&str>,
    ) -> bool {
        if qualified_key.is_some_and(|k| self.type_collision_keys.contains(k)) {
            return true;
        }
        self.has_method_name_collision_for_type(type_name, method)
    }

    /// Like [`has_method_name_collision`] but only considers signatures whose key
    /// contains `type_name` (e.g. `Emitter` for `Emitter::new` calls).
    pub fn has_method_name_collision_for_type(
        &self,
        type_name: Option<&str>,
        method: &str,
    ) -> bool {
        let Some(keys) = self.method_index.get(method) else {
            return false;
        };
        let filtered: Vec<&String> = if let Some(tn) = type_name {
            keys.iter()
                .filter(|k| {
                    k.ends_with(&format!("::{method}"))
                        && (k.as_str() == format!("{tn}::{method}")
                            || k.contains(&format!("::{tn}::")))
                })
                .collect()
        } else {
            keys.iter().collect()
        };
        if filtered.len() < 2 {
            return false;
        }
        let mut first: Option<&FunctionSignature> = None;
        for key in filtered {
            if let Some(sig) = self.signatures.get(key) {
                if let Some(f) = first {
                    if f.param_types != sig.param_types || f.param_ownership != sig.param_ownership
                    {
                        return true;
                    }
                } else {
                    first = Some(sig);
                }
            }
        }
        false
    }

    pub fn all_signatures(&self) -> impl Iterator<Item = (&String, &FunctionSignature)> {
        self.signatures.iter()
    }

    /// Look up a method by name, trying exact match first then falling back to
    /// a qualified name ending with `::name`.
    ///
    /// This is the canonical lookup most call sites should use instead of
    /// `get_signature(m).or_else(|| find_signature_ending_with(m))`.
    pub fn lookup_method(&self, name: &str) -> Option<&FunctionSignature> {
        self.get_signature(name)
            .or_else(|| self.find_signature_ending_with(name))
    }

    /// Fallback lookup: find a signature whose key ends with `::name`.
    /// Uses the method index for O(1) lookup instead of scanning all entries.
    pub fn find_signature_ending_with(&self, suffix: &str) -> Option<&FunctionSignature> {
        if let Some(keys) = self.method_index.get(suffix) {
            for key in keys {
                if let Some(sig) = self.signatures.get(key) {
                    return Some(sig);
                }
            }
        }
        None
    }

    /// Find a signature matching the simple name with a specific argument count.
    /// Uses the method index for fast qualified-name lookup.
    pub fn find_signature_by_name_and_arg_count(
        &self,
        name: &str,
        arg_count: usize,
    ) -> Option<&FunctionSignature> {
        if let Some(sig) = self.signatures.get(name) {
            let sig_args = if sig.has_self_receiver {
                sig.param_ownership.len().saturating_sub(1)
            } else {
                sig.param_ownership.len()
            };
            if sig_args == arg_count {
                return Some(sig);
            }
        }
        if let Some(keys) = self.method_index.get(name) {
            for key in keys {
                if let Some(sig) = self.signatures.get(key) {
                    let sig_args = if sig.has_self_receiver {
                        sig.param_ownership.len().saturating_sub(1)
                    } else {
                        sig.param_ownership.len()
                    };
                    if sig_args == arg_count {
                        return Some(sig);
                    }
                }
            }
        }
        None
    }

    /// Register module-qualified aliases for all signatures in `source`.
    /// For each unqualified name, registers `file_stem::name` and optionally
    /// `module_path::name` to support cross-file lookups.
    pub fn register_module_aliases(
        &mut self,
        source: &SignatureRegistry,
        file_stem: &str,
        module_path: &str,
    ) {
        if file_stem.is_empty() {
            return;
        }
        for (name, sig) in &source.signatures {
            if !name.contains("::") {
                self.add_function(format!("{}::{}", file_stem, name), sig.clone());
            }
            if !module_path.is_empty() {
                self.add_function(format!("{}::{}", module_path, name), sig.clone());
            }
        }
    }

    /// Check if a signature's ownership has changed compared to a reference registry.
    pub fn ownership_changed(old: &FunctionSignature, new: &FunctionSignature) -> bool {
        old.param_ownership != new.param_ownership
            || old.return_ownership != new.return_ownership
            || old.has_self_receiver != new.has_self_receiver
    }

    /// Build declaration-only signature stubs from a parsed program (no ownership inference).
    /// Used by library multipass Step 2 to seed the global registry before Step 3 convergence.
    pub fn from_program_declarations(program: &crate::parser::Program<'_>) -> Self {
        let mut registry = Self::empty();
        Self::collect_declarations_from_items(&program.items, &mut registry);
        registry
    }

    fn collect_declarations_from_items(
        items: &[crate::parser::ast::core::Item<'_>],
        registry: &mut Self,
    ) {
        use crate::parser::ast::core::Item;

        for item in items {
            match item {
                Item::Function { decl, .. } => {
                    let sig = Self::signature_stub_from_decl(decl, &decl.name);
                    registry.add_function(decl.name.clone(), sig);
                }
                Item::Impl { block, .. } => {
                    let base_type_name = block
                        .type_name
                        .split('<')
                        .next()
                        .unwrap_or(&block.type_name);
                    for func in &block.functions {
                        let sig = Self::signature_stub_from_decl(func, &func.name);
                        let qualified_name = format!("{}::{}", base_type_name, func.name);
                        registry.add_function(qualified_name, sig.clone());
                        registry.add_function(func.name.clone(), sig);
                    }
                }
                Item::Trait { decl, .. } => {
                    for method in &decl.methods {
                        let has_self_receiver = method
                            .parameters
                            .first()
                            .is_some_and(|p| p.name == "self" || p.name == "mut self");
                        let param_types: Vec<Type> =
                            method.parameters.iter().map(|p| p.type_.clone()).collect();
                        let param_ownership = vec![OwnershipMode::Owned; param_types.len()];
                        let sig = FunctionSignature {
                            name: method.name.clone(),
                            param_types,
                            param_ownership,
                            return_type: method.return_type.clone(),
                            return_ownership: OwnershipMode::Owned,
                            has_self_receiver,
                            is_extern: false,
                        };
                        registry.add_function(format!("{}::{}", decl.name, method.name), sig);
                    }
                }
                Item::Mod { items, .. } => {
                    Self::collect_declarations_from_items(items, registry);
                }
                _ => {}
            }
        }
    }

    fn signature_stub_from_decl(
        func: &crate::parser::ast::core::FunctionDecl<'_>,
        name: &str,
    ) -> FunctionSignature {
        let has_self_receiver = func
            .parameters
            .first()
            .is_some_and(|p| p.name == "self" || p.name == "mut self");
        let param_types: Vec<Type> = func.parameters.iter().map(|p| p.type_.clone()).collect();
        let param_ownership = vec![OwnershipMode::Owned; param_types.len()];

        FunctionSignature {
            name: name.to_string(),
            param_types,
            param_ownership,
            return_type: func.return_type.clone(),
            return_ownership: OwnershipMode::Owned,
            has_self_receiver,
            is_extern: func.is_extern,
        }
    }

    /// BUG #8 FIX: Merge signatures from another registry.
    /// Detects collisions when different registries provide different
    /// param types for the same key (namespace collision from different modules).
    pub fn merge(&mut self, other: &SignatureRegistry) {
        for (name, sig) in &other.signatures {
            if let Some(existing) = self.signatures.get(name) {
                if existing.param_types != sig.param_types {
                    let stub_like = existing.param_types.is_empty() || sig.param_types.is_empty();
                    if !stub_like {
                        self.type_collision_keys.insert(name.clone());
                    }
                } else if existing.param_ownership != sig.param_ownership {
                    self.ownership_collision_keys.insert(name.clone());
                }
            }
            if let Some(suffix) = name.rsplit_once("::").map(|(_, s)| s.to_string()) {
                self.method_index
                    .entry(suffix)
                    .or_default()
                    .push(name.clone());
            }
            self.signatures.insert(name.clone(), sig.clone());
        }
        self.type_collision_keys
            .extend(other.type_collision_keys.iter().cloned());
        self.ownership_collision_keys
            .extend(other.ownership_collision_keys.iter().cloned());
    }

    /// Collect only signatures whose ownership differs from `base`.
    /// Used by multipass Step 3 to avoid deep-cloning the full registry each round.
    pub fn delta_from_base(
        base: &SignatureRegistry,
        updated: &SignatureRegistry,
    ) -> SignatureDelta {
        let mut changed = HashMap::new();
        for (name, sig) in &updated.signatures {
            let is_new_or_changed = match base.signatures.get(name) {
                None => true,
                Some(old) => Self::ownership_changed(old, sig),
            };
            if is_new_or_changed {
                changed.insert(name.clone(), sig.clone());
            }
        }
        SignatureDelta { changed }
    }

    /// Merge a delta into this registry (changed keys only).
    pub fn merge_delta(&mut self, delta: &SignatureDelta) {
        for (name, sig) in &delta.changed {
            if let Some(suffix) = name.rsplit_once("::").map(|(_, s)| s.to_string()) {
                self.method_index
                    .entry(suffix)
                    .or_default()
                    .push(name.clone());
            }
            self.signatures.insert(name.clone(), sig.clone());
        }
    }
}

/// Ownership-only changes from one analysis pass (avoids full registry clone).
#[derive(Debug, Clone, Default)]
pub struct SignatureDelta {
    pub changed: HashMap<String, FunctionSignature>,
}