table,cgen: fix bug preventing `t := []thread{}` to compile (#8913)

pull/8926/head
Uwe Krüger 2021-02-23 08:37:29 +01:00 committed by GitHub
parent 7c9d280233
commit 5674d46965
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 8 deletions

View File

@ -620,18 +620,19 @@ fn (mut g Gen) find_or_register_shared(t table.Type, base string) string {
}
fn (mut g Gen) register_thread_array_wait_call(eltyp string) string {
thread_typ := '__v_thread_$eltyp'
ret_typ := if eltyp == '' { 'void' } else { 'Array_$eltyp' }
is_void := eltyp == 'void'
thread_typ := if is_void { '__v_thread' } else { '__v_thread_$eltyp' }
ret_typ := if is_void { 'void' } else { 'Array_$eltyp' }
thread_arr_typ := 'Array_$thread_typ'
fn_name := '${thread_arr_typ}_wait'
if fn_name !in g.waiter_fns {
g.waiter_fns << fn_name
if eltyp == 'void' {
if is_void {
g.gowrappers.writeln('
void ${fn_name}($thread_arr_typ a) {
for (int i = 0; i < a.len; ++i) {
$thread_typ t = (($thread_typ*)a.data)[i];
__v_thread_${eltyp}_wait(t);
__v_thread_wait(t);
}
}')
} else {
@ -5376,7 +5377,7 @@ fn (mut g Gen) write_types(types []table.TypeSymbol) {
}
table.Thread {
if g.pref.os == .windows {
if name == '__v_thread_void' {
if name == '__v_thread' {
g.type_definitions.writeln('typedef HANDLE $name;')
} else {
// Windows can only return `u32` (no void*) from a thread, so the
@ -5869,8 +5870,11 @@ fn (mut g Gen) go_stmt(node ast.GoStmt, joinable bool) string {
if g.pref.os == .windows && node.call_expr.return_type != table.void_type {
g.writeln('$arg_tmp_var->ret_ptr = malloc(sizeof($s_ret_typ));')
}
gohandle_name := '__v_thread_' +
g.table.get_type_symbol(g.unwrap_generic(node.call_expr.return_type)).cname
gohandle_name := if node.call_expr.return_type == table.void_type {
'__v_thread'
} else {
'__v_thread_' + g.table.get_type_symbol(g.unwrap_generic(node.call_expr.return_type)).cname
}
if g.pref.os == .windows {
simple_handle := if joinable && node.call_expr.return_type != table.void_type {
'thread_handle_$tmp'

View File

@ -487,6 +487,9 @@ pub fn (t &Table) chan_cname(elem_type Type, is_mut bool) string {
[inline]
pub fn (t &Table) thread_name(return_type Type) string {
if return_type == void_type {
return 'thread'
}
return_type_sym := t.get_type_symbol(return_type)
ptr := if return_type.is_ptr() { '&' } else { '' }
return 'thread $ptr$return_type_sym.name'
@ -494,6 +497,9 @@ pub fn (t &Table) thread_name(return_type Type) string {
[inline]
pub fn (t &Table) thread_cname(return_type Type) string {
if return_type == void_type {
return '__v_thread'
}
return_type_sym := t.get_type_symbol(return_type)
suffix := if return_type.is_ptr() { '_ptr' } else { '' }
return '__v_thread_$return_type_sym.cname$suffix'

View File

@ -540,7 +540,15 @@ pub fn (mut t Table) register_builtin_type_symbols() {
cname: 'int_literal'
mod: 'builtin'
)
t.register_type_symbol(kind: .thread, name: 'thread', cname: '__v_thread', mod: 'builtin')
t.register_type_symbol(
kind: .thread
name: 'thread'
cname: '__v_thread'
mod: 'builtin'
info: Thread{
return_type: table.void_type
}
)
}
[inline]

View File

@ -36,3 +36,16 @@ fn test_array_thread_void_wait() {
}
}
fn test_void_thread_decl() {
shared a := [2 3 9]
mut t1 := thread(0)
mut tarr := []thread{len: 2}
t1 = go g(shared a, 0)
tarr[0] = go g(shared a, 1)
tarr[1] = go g(shared a, 2)
t1.wait()
tarr.wait()
rlock a {
assert a == [6, 12, 90]
}
}