Files
sx/examples/ffi-foreign-cvariadic.c
agra 30fed66616 ffi #foreign: C-variadic tail via args: ..T
Trailing `args: ..T` on a #foreign declaration now lowers to the C
calling convention's `...` instead of sx-side slice-packing. Drops
the per-arity #foreign-shim workaround for callers of variadic C
APIs (__android_log_print, printf-family, etc.). Closes issue-0043.

- IR: Function.is_variadic on inst.Function; declareFunction drops
  the variadic param from the IR signature for foreign+variadic
  decls.
- emit_llvm: LLVMFunctionType receives is_var_arg=1 when the flag
  is set; call lowering passes extras through unchanged.
- Lowering: packVariadicCallArgs early-outs for foreign+variadic
  (no slice-pack); new promoteCVariadicArgs applies C default
  argument promotion (bool/s8/s16/u8/u16 -> s32, f32 -> f64) to
  extras past the fixed param count.
- Test: examples/ffi-foreign-cvariadic.sx + .c exercise s64/f64/s32
  returns through C va_arg over s32/f64/*u8 element types.

134 host + 6 cross tests pass on the WIP-less baseline.
2026-05-22 13:13:43 +03:00

31 lines
656 B
C

#include <stdarg.h>
long long sx_ffi_sum_ints(int n, ...) {
va_list ap;
va_start(ap, n);
long long total = 0;
for (int i = 0; i < n; i++) total += va_arg(ap, int);
va_end(ap);
return total;
}
double sx_ffi_avg_doubles(int n, ...) {
va_list ap;
va_start(ap, n);
double total = 0.0;
for (int i = 0; i < n; i++) total += va_arg(ap, double);
va_end(ap);
if (n == 0) return 0.0;
return total / n;
}
int sx_ffi_count_args(const char *tag, ...) {
(void) tag;
va_list ap;
va_start(ap, tag);
int count = 0;
while (va_arg(ap, const char *) != 0) count++;
va_end(ap);
return count;
}