cgen: allow `shared` initialization from return values of functions (#8512)

pull/8519/head
Uwe Krüger 2021-02-02 13:13:13 +01:00 committed by GitHub
parent 975206f38e
commit 1de299ad22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 4 deletions

View File

@ -2530,6 +2530,19 @@ fn (mut g Gen) expr(node ast.Expr) {
// if g.fileis('1.strings') {
// println('\ncall_expr()()')
// }
mut shared_styp := ''
if g.is_shared {
ret_sym := g.table.get_type_symbol(node.return_type)
shared_typ := node.return_type.set_flag(.shared_f)
shared_styp = g.typ(shared_typ)
if ret_sym.kind == .array {
g.writeln('($shared_styp*)__dup_shared_array(&($shared_styp){.val = ')
} else if ret_sym.kind == .map {
g.writeln('($shared_styp*)__dup_shared_map(&($shared_styp){.val = ')
} else {
g.writeln('($shared_styp*)__dup${shared_styp}(&($shared_styp){.val = ')
}
}
g.call_expr(node)
// if g.fileis('1.strings') {
// println('before:' + node.autofree_pregen)
@ -2546,6 +2559,9 @@ fn (mut g Gen) expr(node ast.Expr) {
g.strs_to_free0 = []
// println('pos=$node.pos.pos')
}
if g.is_shared {
g.writeln('}, sizeof($shared_styp))')
}
// if g.autofree && node.autofree_pregen != '' { // g.strs_to_free0.len != 0 {
/*
if g.autofree {

View File

@ -46,20 +46,29 @@ fn test_shared_init_syntax() {
shared bar := [-12.5, 23.125, 6.0625, 12.5]
shared baz := &[]int{len: 5, cap: 12}
shared qux := []f64{len: 7}
shared quux := new_array()
lock foo {
foo[2] = 20
}
lock bar {
bar[3] = 12.5
}
lock baz, qux {
lock baz, qux, quux {
baz[3] = 12
qux[6] = -17.0625
quux[2] = 7.0625
}
rlock foo, bar, baz, qux {
rlock foo, bar, baz, qux, quux {
assert foo[2] == 20
assert bar[3] == 12.5
assert baz[3] == 12
assert qux[6] == -17.0625
assert quux[1] == 6.25
assert quux[2] == 7.0625
}
}
fn new_array() []f64 {
a := [12.5, 6.25, -3.125, 1.75]
return a
}

View File

@ -41,20 +41,29 @@ fn test_shared_init_syntax() {
shared bar := {'wer': 13.75, 'cvbn': -7.25, 'asd': -0.0625}
shared baz := &map[string]int{}
shared qux := map[string]f64{}
shared quux := new_map()
lock foo {
foo['q'] = 20
}
lock bar {
bar['asd'] = 12.5
}
lock baz, qux {
lock baz, qux, quux {
baz['wer'] = 12
qux['abc'] = -17.0625
quux['tzu'] = 1.125
}
rlock foo, bar, baz, qux {
rlock foo, bar, baz, qux, quux {
assert foo['q'] == 20
assert bar['asd'] == 12.5
assert baz['wer'] == 12
assert qux['abc'] == -17.0625
assert quux['tzu'] == 1.125
assert quux['yxc'] == 9.125
}
}
fn new_map() map[string]f64 {
m := { 'qwe': 34.25, 'yxc': 9.125, 'tzu': -7.5 }
return m
}