Skip to content

Commit 6b23b1d

Browse files
authored
cgen: fix code generation for a struct field, having a type of fixed array of options field [5]?Type (#21082)
1 parent f66b595 commit 6b23b1d

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

vlib/v/gen/c/cgen.v

+47-5
Original file line numberDiff line numberDiff line change
@@ -1473,12 +1473,20 @@ pub fn (mut g Gen) write_typedef_types() {
14731473
def_str = def_str.replace_once('(*)', '(*${styp}[${len}])')
14741474
g.type_definitions.writeln(def_str)
14751475
} else if !info.is_fn_ret {
1476-
g.type_definitions.writeln('typedef ${fixed} ${styp} [${len}];')
14771476
base := g.typ(info.elem_type.clear_option_and_result())
14781477
if info.elem_type.has_flag(.option) && base !in g.options_forward {
1479-
g.options_forward << base
1480-
} else if info.elem_type.has_flag(.result) && base !in g.results_forward {
1481-
g.results_forward << base
1478+
lock g.done_options {
1479+
if base !in g.done_options {
1480+
g.type_definitions.writeln('typedef ${fixed} ${styp} [${len}];')
1481+
g.options_forward << base
1482+
g.done_options << styp
1483+
}
1484+
}
1485+
} else {
1486+
g.type_definitions.writeln('typedef ${fixed} ${styp} [${len}];')
1487+
if info.elem_type.has_flag(.result) && base !in g.results_forward {
1488+
g.results_forward << base
1489+
}
14821490
}
14831491
}
14841492
}
@@ -6396,6 +6404,27 @@ fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) {
63966404
match sym.info {
63976405
ast.Struct {
63986406
if !struct_names[name] {
6407+
// generate field option types for fixed array of option struct before struct declaration
6408+
opt_fields := sym.info.fields.filter(g.table.final_sym(it.typ).kind == .array_fixed)
6409+
for opt_field in opt_fields {
6410+
field_sym := g.table.final_sym(opt_field.typ)
6411+
arr := field_sym.info as ast.ArrayFixed
6412+
if !arr.elem_type.has_flag(.option) {
6413+
continue
6414+
}
6415+
styp := field_sym.cname
6416+
mut fixed_elem_name := g.typ(arr.elem_type.set_nr_muls(0))
6417+
if arr.elem_type.is_ptr() {
6418+
fixed_elem_name += '*'.repeat(arr.elem_type.nr_muls())
6419+
}
6420+
len := arr.size
6421+
lock g.done_options {
6422+
if styp !in g.done_options {
6423+
g.type_definitions.writeln('typedef ${fixed_elem_name} ${styp} [${len}];')
6424+
g.done_options << styp
6425+
}
6426+
}
6427+
}
63996428
g.struct_decl(sym.info, name, false)
64006429
struct_names[name] = true
64016430
}
@@ -6479,7 +6508,20 @@ fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) {
64796508
g.type_definitions.writeln(def_str)
64806509
} else if elem_sym.info !is ast.ArrayFixed
64816510
|| (elem_sym.info as ast.ArrayFixed).size > 0 {
6482-
g.type_definitions.writeln('typedef ${fixed_elem_name} ${styp} [${len}];')
6511+
// fixed array of option struct must be defined backwards
6512+
if sym.info.elem_type.has_flag(.option) && elem_sym.info is ast.Struct {
6513+
styp_elem, base := g.option_type_name(sym.info.elem_type)
6514+
lock g.done_options {
6515+
if base !in g.done_options {
6516+
g.done_options << base
6517+
g.typedefs.writeln('typedef struct ${styp_elem} ${styp_elem};')
6518+
g.type_definitions.writeln('${g.option_type_text(styp_elem,
6519+
base)};')
6520+
}
6521+
}
6522+
} else {
6523+
g.type_definitions.writeln('typedef ${fixed_elem_name} ${styp} [${len}];')
6524+
}
64836525
}
64846526
}
64856527
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
struct Test {}
2+
3+
struct Test2 {
4+
a [10]?Test
5+
}
6+
7+
struct Test3 {
8+
a [10]?Test
9+
}
10+
11+
fn test_main() {
12+
a := Test2{}
13+
b := Test3{}
14+
assert dump(Test2{}) == a
15+
assert dump(Test3{}) == b
16+
}

0 commit comments

Comments
 (0)