Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Aug 14, 2024
1 parent bbce82c commit b5c11f9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
22 changes: 21 additions & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -7204,6 +7204,23 @@ fn c_fn_name(name_ string) string {
return name
}

fn (mut g Gen) type_default_sumtype(typ_ ast.Type, sym ast.TypeSymbol) string {
first_typ := g.unwrap_generic((sym.info as ast.SumType).variants[0])
first_sym := g.table.sym(first_typ)
first_styp := g.typ(first_typ)
first_field := g.get_sumtype_variant_name(first_typ, first_sym)
default_str := if first_typ.has_flag(.option) {
'(${first_styp}){ .state=2, .err=_const_none__, .data={EMPTY_STRUCT_INITIALIZATION} }'
} else {
g.type_default(first_typ)
}
if default_str == '{0}' {
return '(${g.typ(typ_)}){._${first_field}=HEAP(${first_styp}, ${default_str}),._typ=${int(first_typ)}}'
} else {
return '(${g.typ(typ_)}){._${first_field}=HEAP(${first_styp}, (${default_str})),._typ=${int(first_typ)}}'
}
}

fn (mut g Gen) type_default(typ_ ast.Type) string {
typ := g.unwrap_generic(typ_)
if typ.has_option_or_result() {
Expand All @@ -7228,7 +7245,10 @@ fn (mut g Gen) type_default(typ_ ast.Type) string {
}
return '{0}'
}
.interface_, .sum_type, .multi_return, .thread {
.sum_type {
return '{0}' // g.type_default_sumtype(typ, sym)
}
.interface_, .multi_return, .thread {
return '{0}'
}
.alias {
Expand Down
20 changes: 8 additions & 12 deletions vlib/v/gen/c/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ fn (mut g Gen) struct_init(node ast.StructInit) {
return
}
mut sym := g.table.final_sym(g.unwrap_generic(node.typ))
if sym.kind == .sum_type {
g.write(g.type_default_sumtype(g.unwrap_generic(node.typ), sym))
return
}
is_amp := g.is_amp
is_multiline := node.init_fields.len > 5
g.is_amp = false // reset the flag immediately so that other struct inits in this expr are handled correctly
Expand Down Expand Up @@ -354,18 +358,7 @@ fn (mut g Gen) struct_init(node ast.StructInit) {
}

if !initialized {
if sym.kind == .sum_type {
first_typ := (sym.info as ast.SumType).variants[0]
first_sym := g.table.sym(first_typ)
first_styp := g.typ(first_typ)
default_str := if first_typ.has_flag(.option) {
'(${first_styp}){ .state=2, .err=_const_none__, .data={EMPTY_STRUCT_INITIALIZATION} }'
} else {
g.type_default(first_typ)
}
first_field := g.get_sumtype_variant_name(first_typ, first_sym)
g.write('._${first_field}=(${first_styp}*)memdup(ADDR(${first_styp}, (${default_str})), sizeof(${first_styp})), ._typ=${int(first_typ)}')
} else if nr_fields > 0 {
if nr_fields > 0 {
g.write('0')
} else {
g.write('EMPTY_STRUCT_INITIALIZATION')
Expand Down Expand Up @@ -454,6 +447,9 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool {
} else if field.typ.has_flag(.option) {
g.gen_option_error(field.typ, ast.None{})
return true
// } else if sym.info is ast.SumType {
// g.write(g.type_default_sumtype(field.typ, sym))
// return true
} else if sym.info is ast.ArrayFixed {
g.write('{')
for i in 0 .. sym.info.size {
Expand Down

0 comments on commit b5c11f9

Please sign in to comment.