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
//! Unified call signature resolution.
//!
//! Every call expression in the compiler resolves its callee signature through
//! this single module. Resolution follows a strict precedence chain with NO
//! bare unqualified lookups (the root cause of the int-cast collision bug).

use std::collections::HashMap;

use crate::analyzer::{FunctionSignature, SignatureRegistry};

#[derive(Debug, Clone)]
pub struct ResolvedSignature {
    pub sig: FunctionSignature,
    pub qualified_key: String,
    pub resolution_method: ResolutionMethod,
    pub has_collision: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResolutionMethod {
    /// `"Vec::push"` matched directly in the registry.
    ExactQualified,
    /// Receiver type provided context: `"{ReceiverType}::{method}"`.
    ReceiverQualified,
    /// Module alias resolved: `"gpu::fn"` → `"gpu_safe::fn"`.
    ModuleAlias,
    /// Progressive qualification: `"a::b::fn"` → tried `"b::fn"`.
    ProgressiveQualified,
    /// Suffix match with arg-count validation (last resort for registry).
    ArgCountValidated,
}

/// Resolve a call signature from the registry.
///
/// Resolution precedence (each step tried only if previous returned `None`):
///
/// 1. **Exact key** — `registry.get_signature(func_name)`
/// 2. **Receiver-qualified** — `"{receiver_type}::{method}"` (and base-type variant)
/// 3. **Identifier-as-qualifier** — for `Foo::bar()` parsed as `Call(FieldAccess)`,
///    try `"{identifier}::{method}"` when identifier differs from receiver_type
/// 4. **Module alias** — resolve alias, retry with resolved qualifier
/// 5. **Progressive qualification** — for `a::b::c`, try `b::c`, then `c` qualified
/// 6. **Arg-count-validated suffix** — `find_signature_by_name_and_arg_count`
/// 7. **None** — caller handles the no-signature case
///
/// **Key invariant**: bare `get_signature("push")` is NEVER attempted.
pub fn resolve_call_signature(
    registry: &SignatureRegistry,
    func_name: &str,
    receiver_type: Option<&str>,
    arg_count: usize,
    module_aliases: &HashMap<String, String>,
) -> Option<ResolvedSignature> {
    // Step 1: Exact key match (handles already-qualified names like "Vec::push").
    if let Some(sig) = registry.get_signature(func_name) {
        if validate_arg_count(sig, arg_count) {
            return Some(ResolvedSignature {
                sig: sig.clone(),
                qualified_key: func_name.to_string(),
                resolution_method: ResolutionMethod::ExactQualified,
                has_collision: registry.has_collision(func_name),
            });
        }
        // Key exists but arg count is wrong. If there's no collision, the
        // exact key is the only registration — don't fall through to suffix
        // matching which could pick a different type's method.
        // If there IS a collision, fall through: the right variant may be
        // registered under a longer-qualified key.
        if func_name.contains("::") && !registry.has_collision(func_name) {
            return None;
        }
    }

    // Step 2: Receiver-qualified — try `"{receiver_type}::{method}"`.
    let method_part = func_name.rsplit("::").next().unwrap_or(func_name);
    if let Some(recv) = receiver_type {
        if let Some(resolved) = try_receiver_qualified(registry, method_part, recv, arg_count) {
            return Some(resolved);
        }
    }

    // Step 3: Identifier-as-qualifier — for `Emitter::new`, the identifier "Emitter"
    // may be in the func_name even when receiver_type is None or different.
    if let Some(pos) = func_name.rfind("::") {
        let qualifier = &func_name[..pos];

        // Step 3a: Direct qualified name already tried in step 1.
        // Try base-type stripping (e.g., "Vec<i32>::push" → "Vec::push").
        let base_qualifier = qualifier.split('<').next().unwrap_or(qualifier);
        if base_qualifier != qualifier {
            let base_key = format!("{}::{}", base_qualifier, method_part);
            if let Some(sig) = registry.get_signature(&base_key) {
                if validate_arg_count(sig, arg_count) {
                    let has_collision = registry.has_collision(&base_key);
                    return Some(ResolvedSignature {
                        sig: sig.clone(),
                        qualified_key: base_key,
                        resolution_method: ResolutionMethod::ExactQualified,
                        has_collision,
                    });
                }
            }
        }

        // Step 4: Module alias resolution.
        if let Some(original_module) = module_aliases.get(qualifier) {
            let resolved_name = format!("{}::{}", original_module, method_part);
            if let Some(sig) = registry.get_signature(&resolved_name) {
                if validate_arg_count(sig, arg_count) {
                    let has_collision = registry.has_collision(&resolved_name);
                    return Some(ResolvedSignature {
                        sig: sig.clone(),
                        qualified_key: resolved_name,
                        resolution_method: ResolutionMethod::ModuleAlias,
                        has_collision,
                    });
                }
            }
        }

        // Step 5: Progressive qualification for module paths.
        // For `a::b::method`, try `b::method`.
        let parts: Vec<&str> = func_name.split("::").collect();
        if parts.len() > 2 {
            for start in (1..parts.len().saturating_sub(1)).rev() {
                let candidate = parts[start..].join("::");
                if let Some(sig) = registry.get_signature(&candidate) {
                    if validate_arg_count(sig, arg_count) {
                        let has_collision = registry.has_collision(&candidate);
                        return Some(ResolvedSignature {
                            sig: sig.clone(),
                            qualified_key: candidate,
                            resolution_method: ResolutionMethod::ProgressiveQualified,
                            has_collision,
                        });
                    }
                }
            }
        }
    }

    // Step 5b: Collision-aware module-qualified search.
    // When the direct key (e.g., "Ability::activate") has a collision and wrong arg
    // count, search for module-qualified registrations (e.g.,
    // "combat_abilities::Ability::activate") that have the correct arg count.
    // The library multipass registers these longer keys for disambiguation.
    if func_name.contains("::") && registry.has_collision(func_name) {
        let suffix = format!("::{}", func_name);
        for (key, sig) in registry.all_signatures() {
            if key.ends_with(&suffix) && key != func_name && validate_arg_count(sig, arg_count) {
                return Some(ResolvedSignature {
                    sig: sig.clone(),
                    qualified_key: key.clone(),
                    resolution_method: ResolutionMethod::ProgressiveQualified,
                    has_collision: true,
                });
            }
        }
    }

    // Step 6: Arg-count-validated suffix match (last resort).
    // Uses find_signature_by_name_and_arg_count which searches all `::method` entries
    // but validates arg count. Only matches when the func_name already contains `::`
    // (type or module qualifier) — bare names like `update` should not match
    // `Component::update` because methods and free functions have different semantics.
    if func_name.contains("::") {
        if let Some(sig) = registry.find_signature_by_name_and_arg_count(method_part, arg_count) {
            let qualified_key = registry
                .signatures
                .iter()
                .find(|(_, v)| std::ptr::eq(*v, sig))
                .map(|(k, _)| k.clone())
                .unwrap_or_else(|| method_part.to_string());
            return Some(ResolvedSignature {
                sig: sig.clone(),
                qualified_key,
                resolution_method: ResolutionMethod::ArgCountValidated,
                has_collision: registry.has_collision(method_part),
            });
        }
    }

    // Step 7: Bare unqualified name with collision — try arg-count disambiguation.
    // For imported free functions like `check_collision(a, b)`, the registry may have
    // multiple entries (e.g., `collision2d::check_collision` with 2 args and
    // `Tilemap::check_collision` with 4 args). Find the one matching our arg count
    // but only for non-self-receiver entries (free functions, not methods).
    if !func_name.contains("::") {
        let suffix = format!("::{}", func_name);
        for (key, sig) in registry.all_signatures() {
            if key.ends_with(&suffix)
                && !sig.has_self_receiver
                && validate_arg_count(sig, arg_count)
            {
                return Some(ResolvedSignature {
                    sig: sig.clone(),
                    qualified_key: key.clone(),
                    resolution_method: ResolutionMethod::ArgCountValidated,
                    has_collision: true,
                });
            }
        }
    }

    None
}

/// Try receiver-type-qualified lookup with base-type stripping.
fn try_receiver_qualified(
    registry: &SignatureRegistry,
    method: &str,
    receiver_type: &str,
    arg_count: usize,
) -> Option<ResolvedSignature> {
    let base = receiver_type.split('<').next().unwrap_or(receiver_type);

    let qualified = format!("{}::{}", base, method);
    if let Some(sig) = registry.get_signature(&qualified) {
        if validate_arg_count(sig, arg_count) {
            let has_collision = registry.has_collision(&qualified);
            return Some(ResolvedSignature {
                sig: sig.clone(),
                qualified_key: qualified,
                resolution_method: ResolutionMethod::ReceiverQualified,
                has_collision,
            });
        }
    }

    if base != receiver_type {
        let full_qualified = format!("{}::{}", receiver_type, method);
        if let Some(sig) = registry.get_signature(&full_qualified) {
            if validate_arg_count(sig, arg_count) {
                let has_collision = registry.has_collision(&full_qualified);
                return Some(ResolvedSignature {
                    sig: sig.clone(),
                    qualified_key: full_qualified,
                    resolution_method: ResolutionMethod::ReceiverQualified,
                    has_collision,
                });
            }
        }
    }

    None
}

/// Validate that a signature's expected argument count matches the call site.
fn validate_arg_count(sig: &FunctionSignature, call_arg_count: usize) -> bool {
    let expected = if sig.has_self_receiver {
        sig.param_ownership.len().saturating_sub(1)
    } else {
        sig.param_ownership.len()
    };
    expected == call_arg_count
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::analyzer::OwnershipMode;
    use crate::parser::Type;

    fn make_sig(name: &str, param_count: usize, has_self: bool) -> FunctionSignature {
        FunctionSignature {
            name: name.to_string(),
            param_types: vec![Type::Custom("i32".into()); param_count],
            param_ownership: vec![OwnershipMode::Owned; param_count + if has_self { 1 } else { 0 }],
            return_type: None,
            return_ownership: OwnershipMode::Owned,
            has_self_receiver: has_self,
            is_extern: false,
        }
    }

    fn make_sig_with_types(name: &str, types: Vec<Type>, has_self: bool) -> FunctionSignature {
        let ownership_len = types.len() + if has_self { 1 } else { 0 };
        FunctionSignature {
            name: name.to_string(),
            param_types: types,
            param_ownership: vec![OwnershipMode::Owned; ownership_len],
            return_type: None,
            return_ownership: OwnershipMode::Owned,
            has_self_receiver: has_self,
            is_extern: false,
        }
    }

    fn empty_aliases() -> HashMap<String, String> {
        HashMap::new()
    }

    #[test]
    fn exact_qualified_match() {
        let mut reg = SignatureRegistry::new();
        reg.add_function("Vec::push".into(), make_sig("push", 1, true));

        let result = resolve_call_signature(&reg, "Vec::push", None, 1, &empty_aliases());
        assert!(result.is_some());
        let r = result.unwrap();
        assert_eq!(r.resolution_method, ResolutionMethod::ExactQualified);
        assert_eq!(r.qualified_key, "Vec::push");
    }

    #[test]
    fn receiver_qualified_match() {
        let mut reg = SignatureRegistry::new();
        reg.add_function("Emitter::new".into(), make_sig("new", 2, false));

        let result = resolve_call_signature(&reg, "new", Some("Emitter"), 2, &empty_aliases());
        assert!(result.is_some());
        let r = result.unwrap();
        assert_eq!(r.resolution_method, ResolutionMethod::ReceiverQualified);
    }

    #[test]
    fn bare_name_never_matches_wrong_type() {
        let mut reg = SignatureRegistry::new();
        // Vec3::new takes 3 f32 args
        reg.add_function(
            "Vec3::new".into(),
            make_sig_with_types(
                "new",
                vec![
                    Type::Custom("f32".into()),
                    Type::Custom("f32".into()),
                    Type::Custom("f32".into()),
                ],
                false,
            ),
        );
        // Emitter::new takes 2 args (Vec3, i32)
        reg.add_function(
            "Emitter::new".into(),
            make_sig_with_types(
                "new",
                vec![Type::Custom("Vec3".into()), Type::Custom("i32".into())],
                false,
            ),
        );

        // Looking up "new" bare with 2 args should NOT match Vec3::new (3 args)
        // and SHOULD match Emitter::new (2 args) via arg-count validation
        let result = resolve_call_signature(&reg, "Emitter::new", None, 2, &empty_aliases());
        assert!(result.is_some());
        let r = result.unwrap();
        assert_eq!(r.qualified_key, "Emitter::new");
        assert!(!r
            .sig
            .param_types
            .iter()
            .any(|t| matches!(t, Type::Custom(n) if n == "f32")));
    }

    #[test]
    fn module_alias_resolution() {
        let mut reg = SignatureRegistry::new();
        reg.add_function(
            "gpu_safe::load_shader".into(),
            make_sig("load_shader", 1, false),
        );

        let mut aliases = HashMap::new();
        aliases.insert("gpu".into(), "gpu_safe".into());

        let result = resolve_call_signature(&reg, "gpu::load_shader", None, 1, &aliases);
        assert!(result.is_some());
        let r = result.unwrap();
        assert_eq!(r.resolution_method, ResolutionMethod::ModuleAlias);
    }

    #[test]
    fn arg_count_mismatch_rejects() {
        let mut reg = SignatureRegistry::new();
        reg.add_function("Foo::new".into(), make_sig("new", 3, false));

        // Call with 2 args should NOT match a 3-param signature
        let result = resolve_call_signature(&reg, "Foo::new", None, 2, &empty_aliases());
        assert!(result.is_none());
    }

    #[test]
    fn collision_detected() {
        let mut reg = SignatureRegistry::new();
        reg.add_function(
            "Emitter::new".into(),
            make_sig_with_types(
                "new",
                vec![Type::Custom("Vec3".into()), Type::Custom("i32".into())],
                false,
            ),
        );
        reg.add_function(
            "Emitter::new".into(),
            make_sig_with_types(
                "new",
                vec![Type::Custom("f32".into()), Type::Custom("f32".into())],
                false,
            ),
        );

        let result = resolve_call_signature(&reg, "Emitter::new", None, 2, &empty_aliases());
        assert!(result.is_some());
        assert!(result.unwrap().has_collision);
    }

    #[test]
    fn progressive_qualified_match() {
        let mut reg = SignatureRegistry::new();
        reg.add_function(
            "rendering::Camera::update".into(),
            make_sig("update", 1, true),
        );

        let result = resolve_call_signature(
            &reg,
            "scene::rendering::Camera::update",
            None,
            1,
            &empty_aliases(),
        );
        assert!(result.is_some());
        let r = result.unwrap();
        assert_eq!(r.resolution_method, ResolutionMethod::ProgressiveQualified);
    }
}