cgen: fix a c error, when using dump(ptr) (#9468)

pull/9475/head
zakuro 2021-03-26 15:35:12 +09:00 committed by GitHub
parent 478bb9ce8e
commit 91ea76797a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 10 deletions

View File

@ -1,6 +1,7 @@
module c
import v.ast
import v.table
fn (mut g Gen) dump_expr(node ast.DumpExpr) {
sexpr := ctoslit(node.expr.str())
@ -10,28 +11,41 @@ fn (mut g Gen) dump_expr(node ast.DumpExpr) {
g.expr(node.expr)
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.write(' )')
}
fn (mut g Gen) dump_expr_definitions() {
if g.pref.build_mode == .build_module {
for _, cname in g.table.dumps {
g.definitions.writeln('$cname _v_dump_expr_${cname}(string fpath, int line, string sexpr, $cname x);')
for dump_type, cname in g.table.dumps {
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 {
for dump_type, cname in g.table.dumps {
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(fpath);')
g.definitions.writeln('\teprint(${ctoslit(':')});')
g.definitions.writeln('\teprint(int_str(line));')
g.definitions.writeln('\teprint(${ctoslit('] ')});')
g.definitions.writeln('\teprint(sexpr);')
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('}')
}

View File

@ -1,6 +1,16 @@
[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: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
y: 2
z: 3

View File

@ -16,10 +16,19 @@ mut:
}
fn dump_of_struct() {
point := Point{1, 2, 3}
mut x := dump(point)
x.x += 100
assert x == Point{101, 2, 3}
p := Point{1, 2, 3}
mut p_mut := Point{1, 2, 3}
p_ptr := &Point{1, 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() {