mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix anon struct auto string method generation
This commit is contained in:
parent
64eab72f4c
commit
69aaf68f2f
@ -46,6 +46,7 @@ pub mut:
|
||||
pointer_size int
|
||||
// cache for type_to_str_using_aliases
|
||||
cached_type_to_str map[u64]string
|
||||
anon_struct_names map[string]int // anon struct name -> struct sym idx
|
||||
}
|
||||
|
||||
// used by vls to avoid leaks
|
||||
@ -834,6 +835,11 @@ pub fn (mut t Table) register_enum_decl(enum_decl EnumDecl) {
|
||||
t.enum_decls[enum_decl.name] = enum_decl
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (mut t Table) register_anon_struct(name string, sym_idx int) {
|
||||
t.anon_struct_names[name] = sym_idx
|
||||
}
|
||||
|
||||
pub fn (t &Table) known_type(name string) bool {
|
||||
return t.find_type_idx(name) != 0 || t.parsing_type == name
|
||||
}
|
||||
|
@ -1037,8 +1037,13 @@ fn (mut g Gen) write_results() {
|
||||
}
|
||||
done << base
|
||||
g.typedefs.writeln('typedef struct $styp $styp;')
|
||||
dump(g.result_type_text(styp, base))
|
||||
g.out_results.write_string(g.result_type_text(styp, base) + ';\n\n')
|
||||
}
|
||||
for k, _ in g.table.anon_struct_names {
|
||||
ck := c_name(k)
|
||||
g.typedefs.writeln('typedef struct $ck $ck;')
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut g Gen) find_or_register_shared(t ast.Type, base string) string {
|
||||
|
@ -350,7 +350,8 @@ fn (mut g Gen) struct_decl(s ast.Struct, name string, is_anon bool) {
|
||||
g.type_definitions.writeln(pre_pragma)
|
||||
|
||||
if is_anon {
|
||||
g.type_definitions.writeln('struct {')
|
||||
g.type_definitions.write_string('\t$name ')
|
||||
return
|
||||
} else if s.is_union {
|
||||
g.type_definitions.writeln('union $name {')
|
||||
} else {
|
||||
|
@ -350,6 +350,9 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
|
||||
return ast.StructDecl{}
|
||||
}
|
||||
mut ret := p.table.register_sym(sym)
|
||||
if is_anon {
|
||||
p.table.register_anon_struct(name, ret)
|
||||
}
|
||||
// allow duplicate c struct declarations
|
||||
if ret == -1 && language != .c {
|
||||
if _ := p.table.find_fn('main.main') {
|
||||
|
@ -447,3 +447,11 @@ fn test_anon() {
|
||||
assert book2.author.age == 24
|
||||
println(book2.author.name)
|
||||
}
|
||||
|
||||
fn test_anon_auto_stringify() {
|
||||
b := Book{}
|
||||
s := b.str()
|
||||
assert s.contains('author: ')
|
||||
assert s.contains('name: ')
|
||||
assert s.contains('age: 0')
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user