cgen: minor cleanup of cgen.v (#9108)

pull/9075/head
yuyi 2021-03-04 20:40:57 +08:00 committed by GitHub
parent c65d65a3f5
commit 6097045b46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 27 deletions

View File

@ -61,18 +61,18 @@ mut:
file ast.File file ast.File
fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0 fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0
last_fn_c_name string last_fn_c_name string
tmp_count int // counter for unique tmp vars (_tmp1, tmp2 etc) tmp_count int // counter for unique tmp vars (_tmp1, tmp2 etc)
tmp_count2 int // a separate tmp var counter for autofree fn calls tmp_count2 int // a separate tmp var counter for autofree fn calls
is_c_call bool // e.g. `C.printf("v")` is_c_call bool // e.g. `C.printf("v")`
is_assign_lhs bool // inside left part of assign expr (for array_set(), etc) is_assign_lhs bool // inside left part of assign expr (for array_set(), etc)
discard_or_result bool // do not safe last ExprStmt of `or` block in tmp variable to defer ongoing expr usage discard_or_result bool // do not safe last ExprStmt of `or` block in tmp variable to defer ongoing expr usage
is_void_expr_stmt bool // ExprStmt whos result is discarded is_void_expr_stmt bool // ExprStmt whos result is discarded
is_array_set bool is_arraymap_set bool // map or array set value state
is_amp bool // for `&Foo{}` to merge PrefixExpr `&` and StructInit `Foo{}`; also for `&byte(0)` etc is_amp bool // for `&Foo{}` to merge PrefixExpr `&` and StructInit `Foo{}`; also for `&byte(0)` etc
is_sql bool // Inside `sql db{}` statement, generating sql instead of C (e.g. `and` instead of `&&` etc) is_sql bool // Inside `sql db{}` statement, generating sql instead of C (e.g. `and` instead of `&&` etc)
is_shared bool // for initialization of hidden mutex in `[rw]shared` literals is_shared bool // for initialization of hidden mutex in `[rw]shared` literals
is_vlines_enabled bool // is it safe to generate #line directives when -g is passed is_vlines_enabled bool // is it safe to generate #line directives when -g is passed
array_set_pos int arraymap_set_pos int // map or array set value position
vlines_path string // set to the proper path for generating #line directives vlines_path string // set to the proper path for generating #line directives
optionals []string // to avoid duplicates TODO perf, use map optionals []string // to avoid duplicates TODO perf, use map
chan_pop_optionals []string // types for `x := <-ch or {...}` chan_pop_optionals []string // types for `x := <-ch or {...}`
@ -1960,7 +1960,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
g.assign_op = assign_stmt.op g.assign_op = assign_stmt.op
g.expr(left) g.expr(left)
g.is_assign_lhs = false g.is_assign_lhs = false
g.is_array_set = false g.is_arraymap_set = false
if left is ast.IndexExpr { if left is ast.IndexExpr {
sym := g.table.get_type_symbol(left.left_type) sym := g.table.get_type_symbol(left.left_type)
if sym.kind in [.map, .array] { if sym.kind in [.map, .array] {
@ -2008,11 +2008,11 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
pos := g.out.len pos := g.out.len
g.expr(left) g.expr(left)
if g.is_array_set && g.array_set_pos > 0 { if g.is_arraymap_set && g.arraymap_set_pos > 0 {
g.out.go_back_to(g.array_set_pos) g.out.go_back_to(g.arraymap_set_pos)
g.write(', &$v_var)') g.write(', &$v_var)')
g.is_array_set = false g.is_arraymap_set = false
g.array_set_pos = 0 g.arraymap_set_pos = 0
} else { } else {
g.out.go_back_to(pos) g.out.go_back_to(pos)
is_var_mut := !is_decl && left.is_auto_deref_var() is_var_mut := !is_decl && left.is_auto_deref_var()
@ -2101,7 +2101,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
if is_decl { if is_decl {
g.writeln(';') g.writeln(';')
} }
} else if !g.is_array_set && !str_add && !op_overloaded { } else if !g.is_arraymap_set && !str_add && !op_overloaded {
g.write(' $op ') g.write(' $op ')
} else if str_add || op_overloaded { } else if str_add || op_overloaded {
g.write(', ') g.write(', ')
@ -2179,9 +2179,9 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
if str_add || op_overloaded { if str_add || op_overloaded {
g.write(')') g.write(')')
} }
if g.is_array_set { if g.is_arraymap_set {
g.write(' })') g.write(' })')
g.is_array_set = false g.is_arraymap_set = false
} }
g.is_shared = false g.is_shared = false
} }
@ -4619,7 +4619,7 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
if is_amp { if is_amp {
g.out.go_back(1) // delete the `&` already generated in `prefix_expr() g.out.go_back(1) // delete the `&` already generated in `prefix_expr()
} }
if g.is_shared && !g.inside_opt_data && !g.is_array_set { if g.is_shared && !g.inside_opt_data && !g.is_arraymap_set {
mut shared_typ := struct_init.typ.set_flag(.shared_f) mut shared_typ := struct_init.typ.set_flag(.shared_f)
shared_styp = g.typ(shared_typ) shared_styp = g.typ(shared_typ)
g.writeln('($shared_styp*)__dup${shared_styp}(&($shared_styp){.val = ($styp){') g.writeln('($shared_styp*)__dup${shared_styp}(&($shared_styp){.val = ($styp){')
@ -4793,7 +4793,7 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
} }
g.write('}') g.write('}')
if g.is_shared && !g.inside_opt_data && !g.is_array_set { if g.is_shared && !g.inside_opt_data && !g.is_arraymap_set {
g.write('}, sizeof($shared_styp))') g.write('}, sizeof($shared_styp))')
} else if is_amp { } else if is_amp {
g.write(', sizeof($styp))') g.write(', sizeof($styp))')

View File

@ -116,7 +116,7 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym table.TypeSymbol) {
g.write('*') g.write('*')
} }
} else { } else {
g.is_array_set = true // special handling of assign_op and closing with '})' g.is_arraymap_set = true // special handling of assign_op and closing with '})'
g.write('array_set(') g.write('array_set(')
if !left_is_ptr || node.left_type.has_flag(.shared_f) { if !left_is_ptr || node.left_type.has_flag(.shared_f) {
g.write('&') g.write('&')
@ -297,9 +297,9 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym table.TypeSymbol) {
elem_type_str := g.typ(elem_type) elem_type_str := g.typ(elem_type)
elem_typ := g.table.get_type_symbol(elem_type) elem_typ := g.table.get_type_symbol(elem_type)
get_and_set_types := elem_typ.kind in [.struct_, .map] get_and_set_types := elem_typ.kind in [.struct_, .map]
if g.is_assign_lhs && !g.is_array_set && !get_and_set_types { if g.is_assign_lhs && !g.is_arraymap_set && !get_and_set_types {
if g.assign_op == .assign || info.value_type == table.string_type { if g.assign_op == .assign || info.value_type == table.string_type {
g.is_array_set = true g.is_arraymap_set = true
g.write('map_set_1(') g.write('map_set_1(')
} else { } else {
if node.is_setter { if node.is_setter {
@ -331,7 +331,7 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym table.TypeSymbol) {
if elem_typ.kind == .function { if elem_typ.kind == .function {
g.write(', &(voidptr[]) { ') g.write(', &(voidptr[]) { ')
} else { } else {
g.array_set_pos = g.out.len g.arraymap_set_pos = g.out.len
g.write(', &($elem_type_str[]) { ') g.write(', &($elem_type_str[]) { ')
} }
if g.assign_op != .assign && info.value_type != table.string_type { if g.assign_op != .assign && info.value_type != table.string_type {
@ -339,7 +339,7 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym table.TypeSymbol) {
g.write('$zero })))') g.write('$zero })))')
} }
} else if g.inside_map_postfix || g.inside_map_infix || g.inside_map_index } else if g.inside_map_postfix || g.inside_map_infix || g.inside_map_index
|| (g.is_assign_lhs && !g.is_array_set && get_and_set_types) { || (g.is_assign_lhs && !g.is_arraymap_set && get_and_set_types) {
zero := g.type_default(info.value_type) zero := g.type_default(info.value_type)
if node.is_setter { if node.is_setter {
g.write('(*($elem_type_str*)map_get_and_set_1(') g.write('(*($elem_type_str*)map_get_and_set_1(')