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

cgen: allow dump(unsafe{nil}) and dump(voidptr(123)) in the same program

This commit is contained in:
Delyan Angelov 2023-07-29 10:14:07 +03:00
parent 2f2dde8ad0
commit c881e7284d
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 19 additions and 0 deletions

View File

@ -82,11 +82,13 @@ fn (mut g Gen) dump_expr(node ast.DumpExpr) {
} }
fn (mut g Gen) dump_expr_definitions() { fn (mut g Gen) dump_expr_definitions() {
mut dump_already_generated_fns := map[string]bool{}
mut dump_typedefs := map[string]bool{} mut dump_typedefs := map[string]bool{}
mut dump_fns := strings.new_builder(100) mut dump_fns := strings.new_builder(100)
mut dump_fn_defs := strings.new_builder(100) mut dump_fn_defs := strings.new_builder(100)
for dump_type, cname in g.table.dumps { for dump_type, cname in g.table.dumps {
dump_sym := g.table.sym(dump_type) dump_sym := g.table.sym(dump_type)
// eprintln('>>> dump_type: $dump_type | cname: $cname | dump_sym: $dump_sym.name')
mut name := cname mut name := cname
if dump_sym.language == .c { if dump_sym.language == .c {
name = name[3..] name = name[3..]
@ -147,6 +149,13 @@ fn (mut g Gen) dump_expr_definitions() {
} }
dump_fn_name := '_v_dump_expr_${name}' + dump_fn_name := '_v_dump_expr_${name}' +
(if is_ptr { '_ptr'.repeat(typ.nr_muls()) } else { '' }) (if is_ptr { '_ptr'.repeat(typ.nr_muls()) } else { '' })
// protect against duplicate declarations:
if dump_already_generated_fns[dump_fn_name] {
continue
}
dump_already_generated_fns[dump_fn_name] = true
dump_fn_defs.writeln('${str_dumparg_ret_type} ${dump_fn_name}(string fpath, int line, string sexpr, ${str_dumparg_type} dump_arg);') dump_fn_defs.writeln('${str_dumparg_ret_type} ${dump_fn_name}(string fpath, int line, string sexpr, ${str_dumparg_type} dump_arg);')
if g.writeln_fn_header('${str_dumparg_ret_type} ${dump_fn_name}(string fpath, int line, string sexpr, ${str_dumparg_type} dump_arg)', mut if g.writeln_fn_header('${str_dumparg_ret_type} ${dump_fn_name}(string fpath, int line, string sexpr, ${str_dumparg_type} dump_arg)', mut
dump_fns) dump_fns)

View File

@ -67,3 +67,13 @@ fn test_dump_of_type_that_has_no_custom_str_method() {
ps := &StructWithoutStrMethod{456} ps := &StructWithoutStrMethod{456}
assert dump(ps).x == 456 assert dump(ps).x == 456
} }
fn test_nil_values_and_voidptr_values_can_be_dumped_in_the_same_program() {
// Note, that nil is its own type in the main v repo,
// while dump() generates `_v_dump_expr_voidptr` for *both* `nil` and `voidptr` values.
a := unsafe { nil }
b := voidptr(456)
dump(a)
dump(b)
assert true
}