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_const_opt_or_res bool
inside_lambda bool inside_lambda bool
inside_for_in_any_cond bool inside_for_in_any_cond bool
inside_cinit bool
loop_depth int loop_depth int
ternary_names map[string]string ternary_names map[string]string
ternary_level_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()` // should the global be initialized now, not later in `vinit()`
cinit := node.attrs.contains('cinit') cinit := node.attrs.contains('cinit')
g.inside_cinit = cinit
defer {
g.inside_cinit = false
}
cextern := node.attrs.contains('c_extern') cextern := node.attrs.contains('c_extern')
should_init := (!g.pref.use_cache && g.pref.build_mode != .build_module) should_init := (!g.pref.use_cache && g.pref.build_mode != .build_module)
|| (g.pref.build_mode == .build_module && g.module_built == node.mod) || (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 is_anon = info.is_anon
} }
if !is_anon {
if !g.inside_cinit && !is_anon {
g.write('(') g.write('(')
defer { defer {
g.write(')') g.write(')')
@ -63,6 +64,12 @@ fn (mut g Gen) struct_init(node ast.StructInit) {
} else { } else {
g.write('&(${basetyp}){') g.write('&(${basetyp}){')
} }
} else if g.inside_cinit {
if is_multiline {
g.writeln('{')
} else {
g.write('{')
}
} else { } else {
if is_multiline { if is_multiline {
g.writeln('(${styp}){') g.writeln('(${styp}){')