1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix nested struct generation for globals for gcc (#16614)

This commit is contained in:
Felipe Pena 2022-12-08 16:38:01 -03:00 committed by GitHub
parent d62fc777c9
commit f365771499
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 1 deletions

View File

@ -0,0 +1,21 @@
pub struct Foo {
val Bar
}
pub struct Bar {
number int
}
fn test_get() {
}
[cinit]
__global (
baz = Foo{Bar{number: 5}}
)
fn main() {
print(baz)
}

View File

@ -140,6 +140,7 @@ mut:
inside_const_opt_or_res bool
inside_lambda bool
inside_for_in_any_cond bool
inside_cinit bool
loop_depth int
ternary_names map[string]string
ternary_level_names map[string][]string
@ -5050,6 +5051,10 @@ fn (mut g Gen) global_decl(node ast.GlobalDecl) {
}
// should the global be initialized now, not later in `vinit()`
cinit := node.attrs.contains('cinit')
g.inside_cinit = cinit
defer {
g.inside_cinit = false
}
cextern := node.attrs.contains('c_extern')
should_init := (!g.pref.use_cache && g.pref.build_mode != .build_module)
|| (g.pref.build_mode == .build_module && g.module_built == node.mod)

View File

@ -42,7 +42,8 @@ fn (mut g Gen) struct_init(node ast.StructInit) {
is_anon = info.is_anon
}
if !is_anon {
if !g.inside_cinit && !is_anon {
g.write('(')
defer {
g.write(')')
@ -63,6 +64,12 @@ fn (mut g Gen) struct_init(node ast.StructInit) {
} else {
g.write('&(${basetyp}){')
}
} else if g.inside_cinit {
if is_multiline {
g.writeln('{')
} else {
g.write('{')
}
} else {
if is_multiline {
g.writeln('(${styp}){')