cgen: fix a c error, when using dump(ptr) (#9468)
parent
478bb9ce8e
commit
91ea76797a
|
@ -1,6 +1,7 @@
|
||||||
module c
|
module c
|
||||||
|
|
||||||
import v.ast
|
import v.ast
|
||||||
|
import v.table
|
||||||
|
|
||||||
fn (mut g Gen) dump_expr(node ast.DumpExpr) {
|
fn (mut g Gen) dump_expr(node ast.DumpExpr) {
|
||||||
sexpr := ctoslit(node.expr.str())
|
sexpr := ctoslit(node.expr.str())
|
||||||
|
@ -10,28 +11,41 @@ fn (mut g Gen) dump_expr(node ast.DumpExpr) {
|
||||||
g.expr(node.expr)
|
g.expr(node.expr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
g.write(' _v_dump_expr_${node.cname}(${ctoslit(fpath)}, $line, $sexpr, ')
|
dump_fn_name := '_v_dump_expr_$node.cname' + (if node.expr_type.is_ptr() { '_ptr' } else { '' })
|
||||||
|
g.write(' ${dump_fn_name}(${ctoslit(fpath)}, $line, $sexpr, ')
|
||||||
g.expr(node.expr)
|
g.expr(node.expr)
|
||||||
g.write(' )')
|
g.write(' )')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut g Gen) dump_expr_definitions() {
|
fn (mut g Gen) dump_expr_definitions() {
|
||||||
if g.pref.build_mode == .build_module {
|
if g.pref.build_mode == .build_module {
|
||||||
for _, cname in g.table.dumps {
|
for dump_type, cname in g.table.dumps {
|
||||||
g.definitions.writeln('$cname _v_dump_expr_${cname}(string fpath, int line, string sexpr, $cname x);')
|
is_ptr := table.Type(dump_type).is_ptr()
|
||||||
|
ptr_suffix := if is_ptr { '*' } else { '' }
|
||||||
|
dump_fn_name := '_v_dump_expr_$cname' + (if is_ptr { '_ptr' } else { '' })
|
||||||
|
g.definitions.writeln('$cname$ptr_suffix ${dump_fn_name}(string fpath, int line, string sexpr, $cname$ptr_suffix x) {')
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for dump_type, cname in g.table.dumps {
|
for dump_type, cname in g.table.dumps {
|
||||||
to_string_fn_name := g.gen_str_for_type(dump_type)
|
to_string_fn_name := g.gen_str_for_type(dump_type)
|
||||||
g.definitions.writeln('$cname _v_dump_expr_${cname}(string fpath, int line, string sexpr, $cname x) {')
|
is_ptr := table.Type(dump_type).is_ptr()
|
||||||
|
ptr_astarisk := if is_ptr { '*' } else { '' }
|
||||||
|
dump_fn_name := '_v_dump_expr_$cname' + (if is_ptr { '_ptr' } else { '' })
|
||||||
|
g.definitions.writeln('$cname$ptr_astarisk ${dump_fn_name}(string fpath, int line, string sexpr, $cname$ptr_astarisk x) {')
|
||||||
g.definitions.writeln('\teprint(${ctoslit('[')});')
|
g.definitions.writeln('\teprint(${ctoslit('[')});')
|
||||||
g.definitions.writeln('\teprint(fpath);')
|
g.definitions.writeln('\teprint(fpath);')
|
||||||
g.definitions.writeln('\teprint(${ctoslit(':')});')
|
g.definitions.writeln('\teprint(${ctoslit(':')});')
|
||||||
g.definitions.writeln('\teprint(int_str(line));')
|
g.definitions.writeln('\teprint(int_str(line));')
|
||||||
g.definitions.writeln('\teprint(${ctoslit('] ')});')
|
g.definitions.writeln('\teprint(${ctoslit('] ')});')
|
||||||
|
|
||||||
g.definitions.writeln('\teprint(sexpr);')
|
g.definitions.writeln('\teprint(sexpr);')
|
||||||
g.definitions.writeln('\teprint(${ctoslit(': ')});')
|
g.definitions.writeln('\teprint(${ctoslit(': ')});')
|
||||||
g.definitions.writeln('\teprintln(${to_string_fn_name}(x));')
|
|
||||||
|
if is_ptr {
|
||||||
|
g.definitions.writeln('\teprint(${ctoslit('&')});')
|
||||||
|
}
|
||||||
|
g.definitions.writeln('\teprintln(${to_string_fn_name}(${ptr_astarisk}x));')
|
||||||
|
|
||||||
g.definitions.writeln('\treturn x;')
|
g.definitions.writeln('\treturn x;')
|
||||||
g.definitions.writeln('}')
|
g.definitions.writeln('}')
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,16 @@
|
||||||
[vlib/v/tests/inout/dump_expression.vv:2] 1: 1
|
[vlib/v/tests/inout/dump_expression.vv:2] 1: 1
|
||||||
[vlib/v/tests/inout/dump_expression.vv:7] 'a': a
|
[vlib/v/tests/inout/dump_expression.vv:7] 'a': a
|
||||||
[vlib/v/tests/inout/dump_expression.vv:20] point: Point{
|
[vlib/v/tests/inout/dump_expression.vv:23] p: Point{
|
||||||
|
x: 1
|
||||||
|
y: 2
|
||||||
|
z: 3
|
||||||
|
}
|
||||||
|
[vlib/v/tests/inout/dump_expression.vv:24] p_mut: Point{
|
||||||
|
x: 1
|
||||||
|
y: 2
|
||||||
|
z: 3
|
||||||
|
}
|
||||||
|
[vlib/v/tests/inout/dump_expression.vv:25] p_ptr: &Point{
|
||||||
x: 1
|
x: 1
|
||||||
y: 2
|
y: 2
|
||||||
z: 3
|
z: 3
|
||||||
|
|
|
@ -16,10 +16,19 @@ mut:
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dump_of_struct() {
|
fn dump_of_struct() {
|
||||||
point := Point{1, 2, 3}
|
p := Point{1, 2, 3}
|
||||||
mut x := dump(point)
|
mut p_mut := Point{1, 2, 3}
|
||||||
x.x += 100
|
p_ptr := &Point{1, 2, 3}
|
||||||
assert x == Point{101, 2, 3}
|
|
||||||
|
mut x1 := dump(p)
|
||||||
|
mut x2 := dump(p_mut)
|
||||||
|
mut x3 := dump(p_ptr)
|
||||||
|
x1.x += 100
|
||||||
|
x2.x += 100
|
||||||
|
x3.x += 100
|
||||||
|
assert x1 == Point{101, 2, 3}
|
||||||
|
assert x2 == Point{101, 2, 3}
|
||||||
|
assert x3 == Point{101, 2, 3}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
Loading…
Reference in New Issue