cgen: minor cleanup in for_in_stmt() (#8962)

pull/8968/head
yuyi 2021-02-25 20:24:42 +08:00 committed by GitHub
parent 2e381f427a
commit c7587c0760
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 76 additions and 76 deletions

View File

@ -1334,147 +1334,147 @@ fn (mut g Gen) for_stmt(node ast.ForStmt) {
} }
} }
fn (mut g Gen) for_in_stmt(it ast.ForInStmt) { fn (mut g Gen) for_in_stmt(node ast.ForInStmt) {
if it.label.len > 0 { if node.label.len > 0 {
g.writeln('\t$it.label: {}') g.writeln('\t$node.label: {}')
} }
if it.is_range { if node.is_range {
// `for x in 1..10 {` // `for x in 1..10 {`
i := if it.val_var == '_' { g.new_tmp_var() } else { c_name(it.val_var) } i := if node.val_var == '_' { g.new_tmp_var() } else { c_name(node.val_var) }
g.write('for (int $i = ') g.write('for (int $i = ')
g.expr(it.cond) g.expr(node.cond)
g.write('; $i < ') g.write('; $i < ')
g.expr(it.high) g.expr(node.high)
g.writeln('; ++$i) {') g.writeln('; ++$i) {')
} else if it.kind == .array { } else if node.kind == .array {
// `for num in nums {` // `for num in nums {`
g.writeln('// FOR IN array') g.writeln('// FOR IN array')
styp := g.typ(it.val_type) styp := g.typ(node.val_type)
val_sym := g.table.get_type_symbol(it.val_type) val_sym := g.table.get_type_symbol(node.val_type)
tmp := g.new_tmp_var() tmp := g.new_tmp_var()
g.write(g.typ(it.cond_type)) g.write(g.typ(node.cond_type))
g.write(' $tmp = ') g.write(' $tmp = ')
g.expr(it.cond) g.expr(node.cond)
g.writeln(';') g.writeln(';')
i := if it.key_var in ['', '_'] { g.new_tmp_var() } else { it.key_var } i := if node.key_var in ['', '_'] { g.new_tmp_var() } else { node.key_var }
op_field := if it.cond_type.is_ptr() { '->' } else { '.' } + op_field := if node.cond_type.is_ptr() { '->' } else { '.' } +
if it.cond_type.share() == .shared_t { 'val.' } else { '' } if node.cond_type.share() == .shared_t { 'val.' } else { '' }
g.writeln('for (int $i = 0; $i < $tmp${op_field}len; ++$i) {') g.writeln('for (int $i = 0; $i < $tmp${op_field}len; ++$i) {')
if it.val_var != '_' { if node.val_var != '_' {
if val_sym.kind == .function { if val_sym.kind == .function {
g.write('\t') g.write('\t')
g.write_fn_ptr_decl(val_sym.info as table.FnType, c_name(it.val_var)) g.write_fn_ptr_decl(val_sym.info as table.FnType, c_name(node.val_var))
g.writeln(' = ((voidptr*)$tmp${op_field}data)[$i];') g.writeln(' = ((voidptr*)$tmp${op_field}data)[$i];')
} else { } else {
// If val is mutable (pointer behind the scenes), we need to generate // If val is mutable (pointer behind the scenes), we need to generate
// `int* val = ((int*)arr.data) + i;` // `int* val = ((int*)arr.data) + i;`
// instead of // instead of
// `int* val = ((int**)arr.data)[i];` // `int* val = ((int**)arr.data)[i];`
// right := if it.val_is_mut { styp } else { styp + '*' } // right := if node.val_is_mut { styp } else { styp + '*' }
right := if it.val_is_mut { right := if node.val_is_mut {
'(($styp)$tmp${op_field}data) + $i' '(($styp)$tmp${op_field}data) + $i'
} else { } else {
'(($styp*)$tmp${op_field}data)[$i]' '(($styp*)$tmp${op_field}data)[$i]'
} }
g.writeln('\t$styp ${c_name(it.val_var)} = $right;') g.writeln('\t$styp ${c_name(node.val_var)} = $right;')
} }
} }
} else if it.kind == .array_fixed { } else if node.kind == .array_fixed {
atmp := g.new_tmp_var() atmp := g.new_tmp_var()
atmp_type := g.typ(it.cond_type).trim('*') atmp_type := g.typ(node.cond_type).trim('*')
if it.cond_type.is_ptr() || it.cond is ast.ArrayInit { if node.cond_type.is_ptr() || node.cond is ast.ArrayInit {
if !it.cond.is_lvalue() { if !node.cond.is_lvalue() {
g.write('$atmp_type *$atmp = (($atmp_type)') g.write('$atmp_type *$atmp = (($atmp_type)')
} else { } else {
g.write('$atmp_type *$atmp = (') g.write('$atmp_type *$atmp = (')
} }
g.expr(it.cond) g.expr(node.cond)
g.writeln(');') g.writeln(');')
} }
i := if it.key_var in ['', '_'] { g.new_tmp_var() } else { it.key_var } i := if node.key_var in ['', '_'] { g.new_tmp_var() } else { node.key_var }
cond_sym := g.table.get_type_symbol(it.cond_type) cond_sym := g.table.get_type_symbol(node.cond_type)
info := cond_sym.info as table.ArrayFixed info := cond_sym.info as table.ArrayFixed
g.writeln('for (int $i = 0; $i != $info.size; ++$i) {') g.writeln('for (int $i = 0; $i != $info.size; ++$i) {')
if it.val_var != '_' { if node.val_var != '_' {
val_sym := g.table.get_type_symbol(it.val_type) val_sym := g.table.get_type_symbol(node.val_type)
if val_sym.kind == .function { if val_sym.kind == .function {
g.write('\t') g.write('\t')
g.write_fn_ptr_decl(val_sym.info as table.FnType, c_name(it.val_var)) g.write_fn_ptr_decl(val_sym.info as table.FnType, c_name(node.val_var))
} else { } else {
styp := g.typ(it.val_type) styp := g.typ(node.val_type)
g.write('\t$styp ${c_name(it.val_var)}') g.write('\t$styp ${c_name(node.val_var)}')
} }
addr := if it.val_is_mut { '&' } else { '' } addr := if node.val_is_mut { '&' } else { '' }
if it.cond_type.is_ptr() || it.cond is ast.ArrayInit { if node.cond_type.is_ptr() || node.cond is ast.ArrayInit {
g.writeln(' = ${addr}(*$atmp)[$i];') g.writeln(' = ${addr}(*$atmp)[$i];')
} else { } else {
g.write(' = $addr') g.write(' = $addr')
g.expr(it.cond) g.expr(node.cond)
g.writeln('[$i];') g.writeln('[$i];')
} }
} }
} else if it.kind == .map { } else if node.kind == .map {
// `for key, val in map { // `for key, val in map {
g.writeln('// FOR IN map') g.writeln('// FOR IN map')
idx := g.new_tmp_var() idx := g.new_tmp_var()
atmp := g.new_tmp_var() atmp := g.new_tmp_var()
arw_or_pt := if it.cond_type.is_ptr() { '->' } else { '.' } arw_or_pt := if node.cond_type.is_ptr() { '->' } else { '.' }
g.write(g.typ(it.cond_type)) g.write(g.typ(node.cond_type))
g.write(' $atmp = ') g.write(' $atmp = ')
g.expr(it.cond) g.expr(node.cond)
g.writeln(';') g.writeln(';')
g.writeln('for (int $idx = 0; $idx < $atmp${arw_or_pt}key_values.len; ++$idx) {') g.writeln('for (int $idx = 0; $idx < $atmp${arw_or_pt}key_values.len; ++$idx) {')
// TODO: don't have this check when the map has no deleted elements // TODO: don't have this check when the map has no deleted elements
g.writeln('\tif (!DenseArray_has_index(&$atmp${arw_or_pt}key_values, $idx)) {continue;}') g.writeln('\tif (!DenseArray_has_index(&$atmp${arw_or_pt}key_values, $idx)) {continue;}')
if it.key_var != '_' { if node.key_var != '_' {
key_styp := g.typ(it.key_type) key_styp := g.typ(node.key_type)
key := c_name(it.key_var) key := c_name(node.key_var)
g.writeln('\t$key_styp $key = /*key*/ *($key_styp*)DenseArray_key(&$atmp${arw_or_pt}key_values, $idx);') g.writeln('\t$key_styp $key = /*key*/ *($key_styp*)DenseArray_key(&$atmp${arw_or_pt}key_values, $idx);')
// TODO: analyze whether it.key_type has a .clone() method and call .clone() for all types: // TODO: analyze whether node.key_type has a .clone() method and call .clone() for all types:
if it.key_type == table.string_type { if node.key_type == table.string_type {
g.writeln('\t$key = string_clone($key);') g.writeln('\t$key = string_clone($key);')
} }
} }
if it.val_var != '_' { if node.val_var != '_' {
val_sym := g.table.get_type_symbol(it.val_type) val_sym := g.table.get_type_symbol(node.val_type)
if val_sym.kind == .function { if val_sym.kind == .function {
g.write('\t') g.write('\t')
g.write_fn_ptr_decl(val_sym.info as table.FnType, c_name(it.val_var)) g.write_fn_ptr_decl(val_sym.info as table.FnType, c_name(node.val_var))
g.write(' = (*(voidptr*)') g.write(' = (*(voidptr*)')
} else { } else {
val_styp := g.typ(it.val_type) val_styp := g.typ(node.val_type)
if it.val_type.is_ptr() { if node.val_type.is_ptr() {
g.write('\t$val_styp ${c_name(it.val_var)} = &(*($val_styp)') g.write('\t$val_styp ${c_name(node.val_var)} = &(*($val_styp)')
} else { } else {
g.write('\t$val_styp ${c_name(it.val_var)} = (*($val_styp*)') g.write('\t$val_styp ${c_name(node.val_var)} = (*($val_styp*)')
} }
} }
g.writeln('DenseArray_value(&$atmp${arw_or_pt}key_values, $idx));') g.writeln('DenseArray_value(&$atmp${arw_or_pt}key_values, $idx));')
} }
g.stmts(it.stmts) g.stmts(node.stmts)
if it.key_type == table.string_type && !g.is_builtin_mod { if node.key_type == table.string_type && !g.is_builtin_mod {
// g.writeln('string_free(&$key);') // g.writeln('string_free(&$key);')
} }
if it.label.len > 0 { if node.label.len > 0 {
g.writeln('\t${it.label}__continue: {}') g.writeln('\t${node.label}__continue: {}')
} }
g.writeln('}') g.writeln('}')
if it.label.len > 0 { if node.label.len > 0 {
g.writeln('\t${it.label}__break: {}') g.writeln('\t${node.label}__break: {}')
} }
return return
} else if it.kind == .string { } else if node.kind == .string {
i := if it.key_var in ['', '_'] { g.new_tmp_var() } else { it.key_var } i := if node.key_var in ['', '_'] { g.new_tmp_var() } else { node.key_var }
g.write('for (int $i = 0; $i < ') g.write('for (int $i = 0; $i < ')
g.expr(it.cond) g.expr(node.cond)
g.writeln('.len; ++$i) {') g.writeln('.len; ++$i) {')
if it.val_var != '_' { if node.val_var != '_' {
g.write('\tbyte ${c_name(it.val_var)} = ') g.write('\tbyte ${c_name(node.val_var)} = ')
g.expr(it.cond) g.expr(node.cond)
g.writeln('.str[$i];') g.writeln('.str[$i];')
} }
} else if it.kind == .struct_ { } else if node.kind == .struct_ {
cond_type_sym := g.table.get_type_symbol(it.cond_type) cond_type_sym := g.table.get_type_symbol(node.cond_type)
next_fn := cond_type_sym.find_method('next') or { next_fn := cond_type_sym.find_method('next') or {
verror('`next` method not found') verror('`next` method not found')
return return
@ -1485,26 +1485,26 @@ fn (mut g Gen) for_in_stmt(it ast.ForInStmt) {
receiver_styp := g.typ(next_fn.params[0].typ) receiver_styp := g.typ(next_fn.params[0].typ)
fn_name := receiver_styp.replace_each(['*', '', '.', '__']) + '_next' fn_name := receiver_styp.replace_each(['*', '', '.', '__']) + '_next'
g.write('\t${g.typ(ret_typ)} $t = ${fn_name}(') g.write('\t${g.typ(ret_typ)} $t = ${fn_name}(')
if !it.cond_type.is_ptr() { if !node.cond_type.is_ptr() {
g.write('&') g.write('&')
} }
g.expr(it.cond) g.expr(node.cond)
g.writeln(');') g.writeln(');')
g.writeln('\tif (!${t}.ok) { break; }') g.writeln('\tif (!${t}.ok) { break; }')
val := if it.val_var in ['', '_'] { g.new_tmp_var() } else { it.val_var } val := if node.val_var in ['', '_'] { g.new_tmp_var() } else { node.val_var }
val_styp := g.typ(it.val_type) val_styp := g.typ(node.val_type)
g.writeln('\t$val_styp $val = *($val_styp*)${t}.data;') g.writeln('\t$val_styp $val = *($val_styp*)${t}.data;')
} else { } else {
s := g.table.type_to_str(it.cond_type) s := g.table.type_to_str(node.cond_type)
g.error('for in: unhandled symbol `$it.cond` of type `$s`', it.pos) g.error('for in: unhandled symbol `$node.cond` of type `$s`', node.pos)
} }
g.stmts(it.stmts) g.stmts(node.stmts)
if it.label.len > 0 { if node.label.len > 0 {
g.writeln('\t${it.label}__continue: {}') g.writeln('\t${node.label}__continue: {}')
} }
g.writeln('}') g.writeln('}')
if it.label.len > 0 { if node.label.len > 0 {
g.writeln('\t${it.label}__break: {}') g.writeln('\t${node.label}__break: {}')
} }
} }