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

View File

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