Skip to content

Commit de7fba1

Browse files
committed
v2: split transformer and cleanc into more files; move variadic fn logic from cleanc to transformer
1 parent 53bb04c commit de7fba1

20 files changed

Lines changed: 9054 additions & 8954 deletions

File tree

cmd/v2/test.v

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,46 @@ fn test_array_type_resolution() {
16191619
print_str('array type resolution (fn arg): ok')
16201620
}
16211621

1622+
// Helper: variadic function returning sum of all args
1623+
fn variadic_sum(vals ...int) int {
1624+
mut s := 0
1625+
for v in vals {
1626+
s += v
1627+
}
1628+
return s
1629+
}
1630+
1631+
// Helper: variadic function with a fixed first param
1632+
fn variadic_with_fixed(base int, extras ...int) int {
1633+
mut s := base
1634+
for v in extras {
1635+
s += v
1636+
}
1637+
return s
1638+
}
1639+
1640+
fn test_variadic_call_lowering() {
1641+
// 98.1 Basic variadic call with multiple args
1642+
r1 := variadic_sum(1, 2, 3)
1643+
assert r1 == 6
1644+
print_str('variadic call lowering (basic sum): ok')
1645+
1646+
// 98.2 Variadic call with two args
1647+
r2 := variadic_sum(10, 20)
1648+
assert r2 == 30
1649+
print_str('variadic call lowering (two args): ok')
1650+
1651+
// 98.3 Variadic call with single arg
1652+
r3 := variadic_sum(42)
1653+
assert r3 == 42
1654+
print_str('variadic call lowering (single arg): ok')
1655+
1656+
// 98.4 Variadic with fixed + variadic params
1657+
r4 := variadic_with_fixed(100, 10, 20, 30)
1658+
assert r4 == 160
1659+
print_str('variadic call lowering (fixed + variadic): ok')
1660+
}
1661+
16221662
// ===================== MAIN TEST FUNCTION =====================
16231663

16241664
fn main() {
@@ -5184,5 +5224,11 @@ fn main() {
51845224
print_str('--- Test 97: Array type resolution ---')
51855225
test_array_type_resolution()
51865226

5227+
// ==================== 98. VARIADIC CALL LOWERING ====================
5228+
// Tests that variadic function calls are lowered in the transformer:
5229+
// extra args beyond declared params are wrapped into an array literal.
5230+
print_str('--- Test 98: Variadic call lowering ---')
5231+
test_variadic_call_lowering()
5232+
51875233
print_str('=== All tests completed ===')
51885234
}

vlib/v2/gen/cleanc/array.v

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,6 @@ fn (mut g Gen) is_fixed_array_selector(sel ast.SelectorExpr) bool {
369369
return false
370370
}
371371

372-
// expr_contains_ident checks if an AST expression contains an identifier with the given name.
373-
374372
fn (mut g Gen) gen_array_init_expr(node ast.ArrayInitExpr) {
375373
raw_elem := g.extract_array_elem_type(node.typ)
376374
// Convert C pointer syntax to mangled name for composite types:
@@ -400,7 +398,7 @@ fn (mut g Gen) gen_array_init_expr(node ast.ArrayInitExpr) {
400398
if i > 0 {
401399
g.sb.write_string(', ')
402400
}
403-
g.gen_expr(e)
401+
g.expr(e)
404402
}
405403
g.sb.write_string('}')
406404
return
@@ -411,7 +409,7 @@ fn (mut g Gen) gen_array_init_expr(node ast.ArrayInitExpr) {
411409
if i > 0 {
412410
g.sb.write_string(', ')
413411
}
414-
g.gen_expr(e)
412+
g.expr(e)
415413
}
416414
g.sb.write_string('}')
417415
return
@@ -518,7 +516,7 @@ fn (mut g Gen) try_emit_const_dynamic_array_call(name string, value ast.Expr) bo
518516
if i > 0 {
519517
g.sb.write_string(', ')
520518
}
521-
g.gen_expr(e)
519+
g.expr(e)
522520
}
523521
g.sb.writeln('};')
524522
g.sb.writeln('array ${name} = ((array){ .data = ${data_name}, .offset = 0, .len = ${len_expr}, .cap = ${cap_expr}, .flags = 0, .element_size = sizeof(${elem_type}) });')
@@ -532,17 +530,17 @@ fn (mut g Gen) gen_fixed_array_cmp_operand(expr ast.Expr, fixed_type string) {
532530
// For ArrayInitExpr (fixed array literals), wrap in compound literal
533531
if expr is ast.ArrayInitExpr {
534532
g.sb.write_string('(${fixed_type})')
535-
g.gen_expr(expr)
533+
g.expr(expr)
536534
return
537535
}
538536
// Unwrap parens to find ArrayInitExpr inside
539537
if expr is ast.ParenExpr {
540538
inner := g.unwrap_parens(expr.expr)
541539
if inner is ast.ArrayInitExpr {
542540
g.sb.write_string('(${fixed_type})')
543-
g.gen_expr(inner)
541+
g.expr(inner)
544542
return
545543
}
546544
}
547-
g.gen_expr(expr)
545+
g.expr(expr)
548546
}

0 commit comments

Comments
 (0)