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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! Plain function call lowering (after `Call(FieldAccess)` is handled elsewhere).

use crate::analyzer::OwnershipMode;
use crate::parser::*;

use super::super::CodeGenerator;

#[allow(clippy::too_many_lines)]
pub(in crate::codegen::rust) fn generate_plain_function_call<'ast>(
    gen: &mut CodeGenerator<'ast>,
    func_name: &str,
    function: &Expression<'ast>,
    arguments: &[(Option<String>, &'ast Expression<'ast>)],
) -> String {
    let mut func_str = gen.generate_expression(function);

    // Windjammer stdlib type mapping: Map::method → HashMap::method
    if func_str.starts_with("Map::") {
        func_str = func_str.replacen("Map::", "HashMap::", 1);
    }

    // E0282 turbofish: Vec::new() / HashSet::new() → Vec::<T>::new() / HashSet::<T>::new()
    // when the function return type provides the element type.
    // Skip when suppress_collection_turbofish is set (let binding already has type ascription).
    if arguments.is_empty() && !gen.suppress_collection_turbofish {
        if func_str == "Vec::new" {
            if let Some(Type::Vec(inner)) = &gen.current_function_return_type {
                func_str = format!("Vec::<{}>::new", gen.type_to_rust(inner));
            }
        } else if func_str == "HashSet::new" {
            if let Some(Type::Parameterized(base, args)) = &gen.current_function_return_type {
                if base == "HashSet" && args.len() == 1 {
                    func_str = format!("HashSet::<{}>::new", gen.type_to_rust(&args[0]));
                }
            }
        } else if func_str == "HashMap::new" {
            if let Some(Type::Parameterized(base, args)) = &gen.current_function_return_type {
                if base == "HashMap" && args.len() == 2 {
                    func_str = format!(
                        "HashMap::<{}, {}>::new",
                        gen.type_to_rust(&args[0]),
                        gen.type_to_rust(&args[1])
                    );
                }
            }
        }
    }

    // In an impl block, bare function calls to sibling methods need qualified dispatch.
    // Instance methods (take self) → self.method(args)
    // Static methods → Self::method(args)
    if gen.in_impl_block
        && !func_name.contains("::")
        && gen.current_impl_methods.contains(func_name)
    {
        if gen.current_impl_instance_methods.contains(func_name) {
            func_str = format!("self.{}", func_str);
        } else {
            func_str = format!("Self::{}", func_str);
        }
    }

    // E0282 turbofish: Some(expr) → Some::<T>(expr)
    // Only needed when the type parameter is truly ambiguous
    // (e.g. numeric literals outside a typed context). In return
    // position or when the inner type involves references/structs,
    // Rust infers the type from the function signature.
    if func_str == "Some" && arguments.len() == 1 {
        if let Some(Type::Option(inner)) = &gen.current_function_return_type {
            let inner_rust = gen.type_to_rust(inner);
            let is_ambiguous_primitive = matches!(
                inner.as_ref(),
                Type::Int | Type::Int32 | Type::Uint | Type::Float | Type::Bool
            );
            if is_ambiguous_primitive {
                func_str = format!("Some::<{}>", inner_rust);
            }
        }
    }

    // WINDJAMMER PHILOSOPHY: Some/Ok/Err with string literals need .to_string()
    // Some("literal") -> Some("literal".to_string())
    // Ok("literal") -> Ok("literal".to_string())
    // Err("literal") -> Err("literal".to_string())
    // Also: Some(borrowed_iterator_var) -> Some(borrowed_iterator_var.clone())

    // TDD FIX (Bug #2): Detect ALL enum constructors AND tuple struct constructors
    // Pattern: Some/Ok/Err, Module::Variant, or TupleStruct(args)
    let is_std_enum = matches!(func_name, "Some" | "Ok" | "Err");
    let is_custom_enum = func_name.contains("::") && {
        let parts: Vec<&str> = func_name.split("::").collect();
        parts.len() == 2
            && parts[0].chars().next().is_some_and(|c| c.is_uppercase())
            && parts[1].chars().next().is_some_and(|c| c.is_uppercase())
    };
    // Tuple struct constructors: Point(x, y), Id(42)
    // Uppercase name without :: that is a known tuple struct
    let is_tuple_struct_constructor = !is_std_enum
        && !is_custom_enum
        && !func_name.contains("::")
        && func_name.chars().next().is_some_and(|c| c.is_uppercase())
        && gen.tuple_struct_names.contains(func_name);

    if is_std_enum || is_custom_enum || is_tuple_struct_constructor {
        // Enum variant constructors need owned values (Some(T), Ok(T), Err(E)).
        // Set owned context so index expressions use .clone() instead of &,
        // BUT only for arguments that aren't already explicit references.
        let prev_owned_context = gen.in_owned_value_context;
        let generated_args: Vec<String> = arguments
            .iter()
            .map(|(_label, arg)| {
                let is_explicit_ref = matches!(
                    arg,
                    Expression::Unary {
                        op: crate::parser::UnaryOp::Ref | crate::parser::UnaryOp::MutRef,
                        ..
                    }
                );
                if !is_explicit_ref {
                    gen.in_owned_value_context = true;
                }
                let prev_in_call_arg = gen.in_call_argument_generation;
                gen.in_call_argument_generation = true;
                let result = gen.generate_expression(arg);
                gen.in_call_argument_generation = prev_in_call_arg;
                gen.in_owned_value_context = prev_owned_context;
                result
            })
            .collect();

        let has_format_arg = generated_args
            .iter()
            .any(|arg_str| arg_str.contains("format!("));

        if has_format_arg {
            // Extract format!() macros to temp variables
            let mut temp_decls = String::new();
            let mut temp_counter = 0;
            let fixed_args: Vec<String> = generated_args
                .iter()
                .map(|arg_str| {
                    if arg_str.starts_with("format!(") || arg_str.starts_with("&format!(") {
                        // Strip leading & if present
                        let format_expr = if arg_str.starts_with("&") {
                            arg_str.strip_prefix("&").unwrap()
                        } else {
                            arg_str
                        };
                        // Extract to temp var
                        let temp_name = format!("_temp{}", temp_counter);
                        temp_counter += 1;
                        temp_decls.push_str(&format!("let {} = {}; ", temp_name, format_expr));

                        // TDD FIX: Don't add & for owned parameters
                        // Err(format!(...)) should be Err(_temp0), not Err(&_temp0)
                        // Original arg didn't have &, so pass owned value
                        if arg_str.starts_with("&") {
                            format!("&{}", temp_name)
                        } else {
                            temp_name
                        }
                    } else {
                        arg_str.clone()
                    }
                })
                .collect();

            return format!(
                "{{ {}{}({}) }}",
                temp_decls,
                func_str,
                fixed_args.join(", ")
            );
        }

        let args: Vec<String> = generated_args
            .iter()
            .enumerate()
            .map(|(i, arg_str)| {
                // Get the original argument expression for type checking
                let arg = &arguments[i].1;
                let result = arg_str.clone();

                // Auto-convert string literals to String for Option/Result wrappers
                if matches!(
                    arg,
                    Expression::Literal {
                        value: Literal::String(_),
                        ..
                    }
                ) {
                    format!("{}.to_string()", result)
                } else if let Expression::Identifier { name, .. } = arg {
                    // BUGFIX: Don't clone if function returns Option<&T>, Option<&mut T>, or Result<&T, E>
                    // When returning Option<&Squad>, Some(squad) should NOT become Some(squad.clone())

                    // Check if return type is Option<&T> or Option<&mut T> (reference inside)
                    let returns_option_ref = match &gen.current_function_return_type {
                        Some(Type::Option(inner_type)) => {
                            matches!(**inner_type, Type::Reference(_) | Type::MutableReference(_))
                        }
                        _ => false,
                    };

                    // Check if return type is Result<&T, E> or Result<&mut T, E>
                    let returns_result_ref = match &gen.current_function_return_type {
                        Some(Type::Result(ok_type, _err_type)) => {
                            matches!(**ok_type, Type::Reference(_) | Type::MutableReference(_))
                        }
                        _ => false,
                    };

                    // AUTO-CONVERT: Borrowed variables in enum constructors need
                    // ownership conversion since the wrapper takes ownership.
                    // &str params → .to_string(), other borrowed → .clone()
                    // UNLESS returning Option<&T>, Result<&T, E>, etc.
                    if !returns_option_ref
                        && !returns_result_ref
                        && !result.ends_with(".clone()")
                        && !result.ends_with(".to_string()")
                        && !result.trim_start().starts_with('*')
                    {
                        if gen.str_ref_optimized_params.contains(name.as_str()) {
                            format!("{}.to_string()", result)
                        } else if gen.borrowed_iterator_vars.contains(name)
                            || gen.inferred_borrowed_params.contains(name.as_str())
                        {
                            format!("{}.clone()", result)
                        } else {
                            gen.maybe_auto_clone(name, &result)
                        }
                    } else {
                        result
                    }
                } else {
                    result
                }
            })
            .collect();
        return format!("{}({})", func_str, args.join(", "));
    }

    // Function pointer signature extraction: when calling a function pointer
    // parameter (e.g., has_item(arg1, arg2)), build the signature from the
    // parameter's type instead of registry lookup.
    let mut signature = gen
        .current_function_params
        .iter()
        .find(|p| p.name == func_name)
        .and_then(|param| {
            if let Type::FunctionPointer {
                params,
                return_type,
            } = &param.type_
            {
                let param_ownership: Vec<OwnershipMode> = params
                    .iter()
                    .map(|ty| match ty {
                        Type::String => OwnershipMode::Borrowed,
                        Type::Custom(name) if name == "string" => OwnershipMode::Borrowed,
                        Type::Reference(_) | Type::MutableReference(_) => OwnershipMode::Borrowed,
                        Type::Int | Type::Int32 | Type::Uint | Type::Float | Type::Bool => {
                            OwnershipMode::Owned
                        }
                        Type::Custom(name)
                            if matches!(
                                name.as_str(),
                                "i32"
                                    | "i64"
                                    | "u32"
                                    | "u64"
                                    | "f32"
                                    | "f64"
                                    | "bool"
                                    | "char"
                                    | "usize"
                                    | "isize"
                            ) =>
                        {
                            OwnershipMode::Owned
                        }
                        _ => OwnershipMode::Owned,
                    })
                    .collect();

                Some(crate::analyzer::FunctionSignature {
                    name: func_name.to_string(),
                    param_types: params.clone(),
                    param_ownership,
                    return_type: return_type.as_ref().map(|t| (**t).clone()),
                    return_ownership: OwnershipMode::Owned,
                    has_self_receiver: false,
                    is_extern: false,
                })
            } else {
                None
            }
        });

    // Unified signature resolution: single resolver with no bare unqualified lookups.
    let mut resolved_via_fallback = false;
    if signature.is_none() {
        let resolved = crate::codegen::rust::call_signature_resolution::resolve_call_signature(
            &gen.signature_registry,
            func_name,
            None,
            arguments.len(),
            &gen.module_alias_map,
        );
        if let Some(r) = resolved {
            resolved_via_fallback = matches!(
                r.resolution_method,
                crate::codegen::rust::call_signature_resolution::ResolutionMethod::ArgCountValidated
            );
            signature = Some(r.sig);
        }
    }

    // Extern detection: resolved signature is authoritative. For fallback
    // resolutions on module-qualified calls, only trust explicit is_extern.
    let is_extern_call = if resolved_via_fallback && func_name.contains("::") {
        signature.as_ref().is_some_and(|sig| sig.is_extern)
    } else if let Some(ref sig) = signature {
        sig.is_extern
    } else if func_name.contains("::") {
        // Module-qualified call without a resolved signature — check if the
        // base name (after the last `::`) is a known extern function. This
        // handles cross-module extern calls like `api::gpu_create_buffer()`
        // where signature resolution may miss the extern flag.
        let base_name = func_name.rsplit("::").next().unwrap_or(func_name);
        gen.extern_function_names.contains(base_name)
    } else {
        gen.extern_function_names.contains(func_name)
    };

    let mut args: Vec<String> = super::argument_generation::collect_regular_function_arguments(
        gen,
        func_name,
        func_str.as_str(),
        arguments,
        &signature,
        resolved_via_fallback,
        is_extern_call,
    );

    // Borrow owned String args when registry says callee takes borrowed `string` (&str in Rust).
    // Never borrow `string_to_ffi(...)` — extern FFI expects owned FfiString.
    if let Some(ref sig) = signature {
        args = args
            .iter()
            .enumerate()
            .map(|(i, arg_str)| {
                let sig_param_idx = sig.arg_param_index(i);
                let borrow = !is_extern_call
                    && !sig.is_extern
                    && !arg_str.contains("string_to_ffi(")
                    && sig
                        .param_ownership
                        .get(sig_param_idx)
                        .is_some_and(|&o| matches!(o, OwnershipMode::Borrowed))
                    && sig
                        .param_types
                        .get(sig_param_idx)
                        .is_some_and(crate::codegen::rust::types::is_windjammer_text_type);
                if borrow && !arg_str.starts_with('&') && !arg_str.starts_with('"') {
                    let arg_already_ref = if let Some((_, arg_expr)) = arguments.get(i) {
                        if let Expression::Identifier { name, .. } = arg_expr {
                            gen.identifier_already_ref(name)
                        } else {
                            false
                        }
                    } else {
                        false
                    };
                    if arg_already_ref {
                        arg_str.clone()
                    } else {
                        format!("&{arg_str}")
                    }
                } else {
                    arg_str.clone()
                }
            })
            .collect();
    }

    // TDD FIX (Bug #3): Extract format!() / write!-block macros in arguments to temp variables
    let needs_format_temp = |arg_str: &str| -> bool {
        arg_str.contains("format!(")
            || arg_str.contains("write!(&mut __s,")
            || (arg_str.contains("string_to_ffi(")
                && (arg_str.contains("format!(") || arg_str.contains("write!(&mut __s,")))
    };
    let has_format_arg = args.iter().any(|arg_str| needs_format_temp(arg_str));

    /// Strip `string_to_ffi(...)` wrapper for temp extraction of the inner expression.
    fn unwrap_string_to_ffi(arg_str: &str) -> (&str, bool) {
        const PREFIX: &str = "windjammer_runtime::ffi::string_to_ffi(";
        if let Some(rest) = arg_str.strip_prefix(PREFIX) {
            if let Some(inner) = rest.strip_suffix(')') {
                return (inner, true);
            }
        }
        (arg_str, false)
    }

    fn extract_format_like_arg(
        arg_str: &str,
        temp_decls: &mut String,
        temp_counter: &mut i32,
    ) -> Option<String> {
        let (inner, was_ffi) = unwrap_string_to_ffi(arg_str);
        let has_borrow_prefix = inner.starts_with('&');
        let format_expr = if has_borrow_prefix {
            &inner[1..]
        } else {
            inner
        };
        let needs_extract = format_expr.starts_with("format!(")
            || format_expr.starts_with("{") && format_expr.contains("write!(&mut __s,");
        if !needs_extract {
            return None;
        }
        let temp_name = format!("_temp{}", temp_counter);
        *temp_counter += 1;
        temp_decls.push_str(&format!("let {} = {}; ", temp_name, format_expr));
        let pass_expr = if has_borrow_prefix {
            format!("&{}", temp_name)
        } else {
            temp_name
        };
        Some(if was_ffi {
            format!("windjammer_runtime::ffi::string_to_ffi({})", pass_expr)
        } else {
            pass_expr
        })
    }

    // WINDJAMMER FFI: Extern functions returning string use FfiString - wrap with ffi_to_string
    let returns_string = signature
        .as_ref()
        .and_then(|s| s.return_type.as_ref())
        .is_some_and(|t| {
            matches!(t, Type::String)
                || matches!(t, Type::Custom(n) if n == "string" || n == "String")
        });

    // WINDJAMMER PHILOSOPHY: Auto-wrap extern function calls in unsafe blocks
    // THE WINDJAMMER WAY: Users shouldn't have to write `unsafe` manually
    let call_result = if has_format_arg {
        // Extract format!() macros to temp variables
        let mut temp_decls = String::new();
        let mut temp_counter = 0i32;
        let fixed_args: Vec<String> = args
            .iter()
            .map(|arg_str| {
                if let Some(fixed) =
                    extract_format_like_arg(arg_str, &mut temp_decls, &mut temp_counter)
                {
                    fixed
                } else {
                    arg_str.clone()
                }
            })
            .collect();

        let call_expr = format!("{}({})", func_str, fixed_args.join(", "));

        // Wrap in unsafe block if extern, otherwise regular block
        // Parenthesize so the block can be used as a sub-expression (e.g., in comparisons)
        if is_extern_call && !gen.in_unsafe_block {
            format!("(unsafe {{ {}{}  }})", temp_decls, call_expr)
        } else {
            format!("{{ {}{} }}", temp_decls, call_expr)
        }
    } else {
        // No format!() args - generate normally with optional unsafe wrapper
        let call_str = format!("{}({})", func_str, args.join(", "));
        if is_extern_call && !gen.in_unsafe_block {
            format!("(unsafe {{ {} }})", call_str)
        } else {
            call_str
        }
    };

    // Wrap extern string return with ffi_to_string
    if is_extern_call && returns_string {
        format!("windjammer_runtime::ffi::ffi_to_string({})", call_result)
    } else {
        call_result
    }
}