Skip to content

Commit c7587c0

Browse files
authored
cgen: minor cleanup in for_in_stmt() (#8962)
1 parent 2e381f4 commit c7587c0

File tree

1 file changed

+76
-76
lines changed

1 file changed

+76
-76
lines changed

vlib/v/gen/c/cgen.v

Lines changed: 76 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,147 +1334,147 @@ fn (mut g Gen) for_stmt(node ast.ForStmt) {
13341334
}
13351335
}
13361336

1337-
fn (mut g Gen) for_in_stmt(it ast.ForInStmt) {
1338-
if it.label.len > 0 {
1339-
g.writeln('\t$it.label: {}')
1337+
fn (mut g Gen) for_in_stmt(node ast.ForInStmt) {
1338+
if node.label.len > 0 {
1339+
g.writeln('\t$node.label: {}')
13401340
}
1341-
if it.is_range {
1341+
if node.is_range {
13421342
// `for x in 1..10 {`
1343-
i := if it.val_var == '_' { g.new_tmp_var() } else { c_name(it.val_var) }
1343+
i := if node.val_var == '_' { g.new_tmp_var() } else { c_name(node.val_var) }
13441344
g.write('for (int $i = ')
1345-
g.expr(it.cond)
1345+
g.expr(node.cond)
13461346
g.write('; $i < ')
1347-
g.expr(it.high)
1347+
g.expr(node.high)
13481348
g.writeln('; ++$i) {')
1349-
} else if it.kind == .array {
1349+
} else if node.kind == .array {
13501350
// `for num in nums {`
13511351
g.writeln('// FOR IN array')
1352-
styp := g.typ(it.val_type)
1353-
val_sym := g.table.get_type_symbol(it.val_type)
1352+
styp := g.typ(node.val_type)
1353+
val_sym := g.table.get_type_symbol(node.val_type)
13541354
tmp := g.new_tmp_var()
1355-
g.write(g.typ(it.cond_type))
1355+
g.write(g.typ(node.cond_type))
13561356
g.write(' $tmp = ')
1357-
g.expr(it.cond)
1357+
g.expr(node.cond)
13581358
g.writeln(';')
1359-
i := if it.key_var in ['', '_'] { g.new_tmp_var() } else { it.key_var }
1360-
op_field := if it.cond_type.is_ptr() { '->' } else { '.' } +
1361-
if it.cond_type.share() == .shared_t { 'val.' } else { '' }
1359+
i := if node.key_var in ['', '_'] { g.new_tmp_var() } else { node.key_var }
1360+
op_field := if node.cond_type.is_ptr() { '->' } else { '.' } +
1361+
if node.cond_type.share() == .shared_t { 'val.' } else { '' }
13621362
g.writeln('for (int $i = 0; $i < $tmp${op_field}len; ++$i) {')
1363-
if it.val_var != '_' {
1363+
if node.val_var != '_' {
13641364
if val_sym.kind == .function {
13651365
g.write('\t')
1366-
g.write_fn_ptr_decl(val_sym.info as table.FnType, c_name(it.val_var))
1366+
g.write_fn_ptr_decl(val_sym.info as table.FnType, c_name(node.val_var))
13671367
g.writeln(' = ((voidptr*)$tmp${op_field}data)[$i];')
13681368
} else {
13691369
// If val is mutable (pointer behind the scenes), we need to generate
13701370
// `int* val = ((int*)arr.data) + i;`
13711371
// instead of
13721372
// `int* val = ((int**)arr.data)[i];`
1373-
// right := if it.val_is_mut { styp } else { styp + '*' }
1374-
right := if it.val_is_mut {
1373+
// right := if node.val_is_mut { styp } else { styp + '*' }
1374+
right := if node.val_is_mut {
13751375
'(($styp)$tmp${op_field}data) + $i'
13761376
} else {
13771377
'(($styp*)$tmp${op_field}data)[$i]'
13781378
}
1379-
g.writeln('\t$styp ${c_name(it.val_var)} = $right;')
1379+
g.writeln('\t$styp ${c_name(node.val_var)} = $right;')
13801380
}
13811381
}
1382-
} else if it.kind == .array_fixed {
1382+
} else if node.kind == .array_fixed {
13831383
atmp := g.new_tmp_var()
1384-
atmp_type := g.typ(it.cond_type).trim('*')
1385-
if it.cond_type.is_ptr() || it.cond is ast.ArrayInit {
1386-
if !it.cond.is_lvalue() {
1384+
atmp_type := g.typ(node.cond_type).trim('*')
1385+
if node.cond_type.is_ptr() || node.cond is ast.ArrayInit {
1386+
if !node.cond.is_lvalue() {
13871387
g.write('$atmp_type *$atmp = (($atmp_type)')
13881388
} else {
13891389
g.write('$atmp_type *$atmp = (')
13901390
}
1391-
g.expr(it.cond)
1391+
g.expr(node.cond)
13921392
g.writeln(');')
13931393
}
1394-
i := if it.key_var in ['', '_'] { g.new_tmp_var() } else { it.key_var }
1395-
cond_sym := g.table.get_type_symbol(it.cond_type)
1394+
i := if node.key_var in ['', '_'] { g.new_tmp_var() } else { node.key_var }
1395+
cond_sym := g.table.get_type_symbol(node.cond_type)
13961396
info := cond_sym.info as table.ArrayFixed
13971397
g.writeln('for (int $i = 0; $i != $info.size; ++$i) {')
1398-
if it.val_var != '_' {
1399-
val_sym := g.table.get_type_symbol(it.val_type)
1398+
if node.val_var != '_' {
1399+
val_sym := g.table.get_type_symbol(node.val_type)
14001400
if val_sym.kind == .function {
14011401
g.write('\t')
1402-
g.write_fn_ptr_decl(val_sym.info as table.FnType, c_name(it.val_var))
1402+
g.write_fn_ptr_decl(val_sym.info as table.FnType, c_name(node.val_var))
14031403
} else {
1404-
styp := g.typ(it.val_type)
1405-
g.write('\t$styp ${c_name(it.val_var)}')
1404+
styp := g.typ(node.val_type)
1405+
g.write('\t$styp ${c_name(node.val_var)}')
14061406
}
1407-
addr := if it.val_is_mut { '&' } else { '' }
1408-
if it.cond_type.is_ptr() || it.cond is ast.ArrayInit {
1407+
addr := if node.val_is_mut { '&' } else { '' }
1408+
if node.cond_type.is_ptr() || node.cond is ast.ArrayInit {
14091409
g.writeln(' = ${addr}(*$atmp)[$i];')
14101410
} else {
14111411
g.write(' = $addr')
1412-
g.expr(it.cond)
1412+
g.expr(node.cond)
14131413
g.writeln('[$i];')
14141414
}
14151415
}
1416-
} else if it.kind == .map {
1416+
} else if node.kind == .map {
14171417
// `for key, val in map {
14181418
g.writeln('// FOR IN map')
14191419
idx := g.new_tmp_var()
14201420
atmp := g.new_tmp_var()
1421-
arw_or_pt := if it.cond_type.is_ptr() { '->' } else { '.' }
1422-
g.write(g.typ(it.cond_type))
1421+
arw_or_pt := if node.cond_type.is_ptr() { '->' } else { '.' }
1422+
g.write(g.typ(node.cond_type))
14231423
g.write(' $atmp = ')
1424-
g.expr(it.cond)
1424+
g.expr(node.cond)
14251425
g.writeln(';')
14261426
g.writeln('for (int $idx = 0; $idx < $atmp${arw_or_pt}key_values.len; ++$idx) {')
14271427
// TODO: don't have this check when the map has no deleted elements
14281428
g.writeln('\tif (!DenseArray_has_index(&$atmp${arw_or_pt}key_values, $idx)) {continue;}')
1429-
if it.key_var != '_' {
1430-
key_styp := g.typ(it.key_type)
1431-
key := c_name(it.key_var)
1429+
if node.key_var != '_' {
1430+
key_styp := g.typ(node.key_type)
1431+
key := c_name(node.key_var)
14321432
g.writeln('\t$key_styp $key = /*key*/ *($key_styp*)DenseArray_key(&$atmp${arw_or_pt}key_values, $idx);')
1433-
// TODO: analyze whether it.key_type has a .clone() method and call .clone() for all types:
1434-
if it.key_type == table.string_type {
1433+
// TODO: analyze whether node.key_type has a .clone() method and call .clone() for all types:
1434+
if node.key_type == table.string_type {
14351435
g.writeln('\t$key = string_clone($key);')
14361436
}
14371437
}
1438-
if it.val_var != '_' {
1439-
val_sym := g.table.get_type_symbol(it.val_type)
1438+
if node.val_var != '_' {
1439+
val_sym := g.table.get_type_symbol(node.val_type)
14401440
if val_sym.kind == .function {
14411441
g.write('\t')
1442-
g.write_fn_ptr_decl(val_sym.info as table.FnType, c_name(it.val_var))
1442+
g.write_fn_ptr_decl(val_sym.info as table.FnType, c_name(node.val_var))
14431443
g.write(' = (*(voidptr*)')
14441444
} else {
1445-
val_styp := g.typ(it.val_type)
1446-
if it.val_type.is_ptr() {
1447-
g.write('\t$val_styp ${c_name(it.val_var)} = &(*($val_styp)')
1445+
val_styp := g.typ(node.val_type)
1446+
if node.val_type.is_ptr() {
1447+
g.write('\t$val_styp ${c_name(node.val_var)} = &(*($val_styp)')
14481448
} else {
1449-
g.write('\t$val_styp ${c_name(it.val_var)} = (*($val_styp*)')
1449+
g.write('\t$val_styp ${c_name(node.val_var)} = (*($val_styp*)')
14501450
}
14511451
}
14521452
g.writeln('DenseArray_value(&$atmp${arw_or_pt}key_values, $idx));')
14531453
}
1454-
g.stmts(it.stmts)
1455-
if it.key_type == table.string_type && !g.is_builtin_mod {
1454+
g.stmts(node.stmts)
1455+
if node.key_type == table.string_type && !g.is_builtin_mod {
14561456
// g.writeln('string_free(&$key);')
14571457
}
1458-
if it.label.len > 0 {
1459-
g.writeln('\t${it.label}__continue: {}')
1458+
if node.label.len > 0 {
1459+
g.writeln('\t${node.label}__continue: {}')
14601460
}
14611461
g.writeln('}')
1462-
if it.label.len > 0 {
1463-
g.writeln('\t${it.label}__break: {}')
1462+
if node.label.len > 0 {
1463+
g.writeln('\t${node.label}__break: {}')
14641464
}
14651465
return
1466-
} else if it.kind == .string {
1467-
i := if it.key_var in ['', '_'] { g.new_tmp_var() } else { it.key_var }
1466+
} else if node.kind == .string {
1467+
i := if node.key_var in ['', '_'] { g.new_tmp_var() } else { node.key_var }
14681468
g.write('for (int $i = 0; $i < ')
1469-
g.expr(it.cond)
1469+
g.expr(node.cond)
14701470
g.writeln('.len; ++$i) {')
1471-
if it.val_var != '_' {
1472-
g.write('\tbyte ${c_name(it.val_var)} = ')
1473-
g.expr(it.cond)
1471+
if node.val_var != '_' {
1472+
g.write('\tbyte ${c_name(node.val_var)} = ')
1473+
g.expr(node.cond)
14741474
g.writeln('.str[$i];')
14751475
}
1476-
} else if it.kind == .struct_ {
1477-
cond_type_sym := g.table.get_type_symbol(it.cond_type)
1476+
} else if node.kind == .struct_ {
1477+
cond_type_sym := g.table.get_type_symbol(node.cond_type)
14781478
next_fn := cond_type_sym.find_method('next') or {
14791479
verror('`next` method not found')
14801480
return
@@ -1485,26 +1485,26 @@ fn (mut g Gen) for_in_stmt(it ast.ForInStmt) {
14851485
receiver_styp := g.typ(next_fn.params[0].typ)
14861486
fn_name := receiver_styp.replace_each(['*', '', '.', '__']) + '_next'
14871487
g.write('\t${g.typ(ret_typ)} $t = ${fn_name}(')
1488-
if !it.cond_type.is_ptr() {
1488+
if !node.cond_type.is_ptr() {
14891489
g.write('&')
14901490
}
1491-
g.expr(it.cond)
1491+
g.expr(node.cond)
14921492
g.writeln(');')
14931493
g.writeln('\tif (!${t}.ok) { break; }')
1494-
val := if it.val_var in ['', '_'] { g.new_tmp_var() } else { it.val_var }
1495-
val_styp := g.typ(it.val_type)
1494+
val := if node.val_var in ['', '_'] { g.new_tmp_var() } else { node.val_var }
1495+
val_styp := g.typ(node.val_type)
14961496
g.writeln('\t$val_styp $val = *($val_styp*)${t}.data;')
14971497
} else {
1498-
s := g.table.type_to_str(it.cond_type)
1499-
g.error('for in: unhandled symbol `$it.cond` of type `$s`', it.pos)
1498+
s := g.table.type_to_str(node.cond_type)
1499+
g.error('for in: unhandled symbol `$node.cond` of type `$s`', node.pos)
15001500
}
1501-
g.stmts(it.stmts)
1502-
if it.label.len > 0 {
1503-
g.writeln('\t${it.label}__continue: {}')
1501+
g.stmts(node.stmts)
1502+
if node.label.len > 0 {
1503+
g.writeln('\t${node.label}__continue: {}')
15041504
}
15051505
g.writeln('}')
1506-
if it.label.len > 0 {
1507-
g.writeln('\t${it.label}__break: {}')
1506+
if node.label.len > 0 {
1507+
g.writeln('\t${node.label}__break: {}')
15081508
}
15091509
}
15101510

0 commit comments

Comments
 (0)