1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: clean up ident() in cgen.v (#15121)

This commit is contained in:
yuyi 2022-07-19 15:54:03 +08:00 committed by GitHub
parent c7ec71cd06
commit a39fe68af1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3845,7 +3845,7 @@ fn (mut g Gen) ident(node ast.Ident) {
return return
} }
mut name := c_name(node.name) mut name := c_name(node.name)
if node.kind == .constant { // && !node.name.starts_with('g_') { if node.kind == .constant {
if g.pref.translated && !g.is_builtin_mod if g.pref.translated && !g.is_builtin_mod
&& !util.module_is_builtin(node.name.all_before_last('.')) { && !util.module_is_builtin(node.name.all_before_last('.')) {
// Don't prepend "_const" to translated C consts, // Don't prepend "_const" to translated C consts,
@ -3861,45 +3861,43 @@ fn (mut g Gen) ident(node ast.Ident) {
g.write('_const_') g.write('_const_')
} }
} }
// TODO: temporary, remove this
node_info := node.info
mut is_auto_heap := false mut is_auto_heap := false
if node_info is ast.IdentVar { if node.info is ast.IdentVar {
// x ?int // x ?int
// `x = 10` => `x.data = 10` (g.right_is_opt == false) // `x = 10` => `x.data = 10` (g.right_is_opt == false)
// `x = new_opt()` => `x = new_opt()` (g.right_is_opt == true) // `x = new_opt()` => `x = new_opt()` (g.right_is_opt == true)
// `println(x)` => `println(*(int*)x.data)` // `println(x)` => `println(*(int*)x.data)`
if node_info.is_optional && !(g.is_assign_lhs && g.right_is_opt) { if node.info.is_optional && !(g.is_assign_lhs && g.right_is_opt) {
g.write('/*opt*/') g.write('/*opt*/')
styp := g.base_type(node_info.typ) styp := g.base_type(node.info.typ)
g.write('(*($styp*)${name}.data)') g.write('(*($styp*)${name}.data)')
return return
} }
if !g.is_assign_lhs && node_info.share == .shared_t { if !g.is_assign_lhs && node.info.share == .shared_t {
g.write('${name}.val') g.write('${name}.val')
return return
} }
v := node.obj if node.obj is ast.Var {
if v is ast.Var { is_auto_heap = node.obj.is_auto_heap
is_auto_heap = v.is_auto_heap && (!g.is_assign_lhs || g.assign_op != .decl_assign) && (!g.is_assign_lhs || g.assign_op != .decl_assign)
if is_auto_heap { if is_auto_heap {
g.write('(*(') g.write('(*(')
} }
if v.smartcasts.len > 0 { if node.obj.smartcasts.len > 0 {
v_sym := g.table.sym(v.typ) obj_sym := g.table.sym(node.obj.typ)
if !prevent_sum_type_unwrapping_once { if !prevent_sum_type_unwrapping_once {
for _ in v.smartcasts { for _ in node.obj.smartcasts {
g.write('(') g.write('(')
if v_sym.kind == .sum_type && !is_auto_heap { if obj_sym.kind == .sum_type && !is_auto_heap {
g.write('*') g.write('*')
} }
} }
for i, typ in v.smartcasts { for i, typ in node.obj.smartcasts {
cast_sym := g.table.sym(g.unwrap_generic(typ)) cast_sym := g.table.sym(g.unwrap_generic(typ))
mut is_ptr := false mut is_ptr := false
if i == 0 { if i == 0 {
g.write(name) g.write(name)
if v.orig_type.is_ptr() { if node.obj.orig_type.is_ptr() {
is_ptr = true is_ptr = true
} }
} }
@ -3918,16 +3916,16 @@ fn (mut g Gen) ident(node ast.Ident) {
return return
} }
} }
if v.is_inherited { if node.obj.is_inherited {
g.write(closure_ctx + '->') g.write(closure_ctx + '->')
} }
} }
} else if node_info is ast.IdentFn { } else if node.info is ast.IdentFn {
if g.pref.translated || g.file.is_translated { if g.pref.translated || g.file.is_translated {
// `p_mobjthinker` => `P_MobjThinker` // `p_mobjthinker` => `P_MobjThinker`
if f := g.table.find_fn(node.name) { if func := g.table.find_fn(node.name) {
// TODO PERF fn lookup for each fn call in translated mode // TODO PERF fn lookup for each fn call in translated mode
if cattr := f.attrs.find_first('c') { if cattr := func.attrs.find_first('c') {
name = cattr.arg name = cattr.arg
} }
} }