cgen: fix interface with nested fields (fix #10077) (#10589)

pull/10597/head
yuyi 2021-06-28 15:24:05 +08:00 committed by GitHub
parent c3b9eaf146
commit 67d1b72e36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 2 deletions

View File

@ -866,7 +866,9 @@ pub fn (mut g Gen) write_alias_typesymbol_declaration(sym ast.TypeSymbol) {
pub fn (mut g Gen) write_interface_typesymbol_declaration(sym ast.TypeSymbol) {
info := sym.info as ast.Interface
g.type_definitions.writeln('typedef struct {')
struct_name := c_name(sym.name)
g.type_definitions.writeln('typedef struct $struct_name $struct_name;')
g.type_definitions.writeln('struct $struct_name {')
g.type_definitions.writeln('\tunion {')
g.type_definitions.writeln('\t\tvoid* _object;')
for variant in info.types {
@ -880,7 +882,7 @@ pub fn (mut g Gen) write_interface_typesymbol_declaration(sym ast.TypeSymbol) {
cname := c_name(field.name)
g.type_definitions.writeln('\t$styp* $cname;')
}
g.type_definitions.writeln('} ${c_name(sym.name)};')
g.type_definitions.writeln('};')
}
pub fn (mut g Gen) write_fn_typesymbol_declaration(sym ast.TypeSymbol) {

View File

@ -0,0 +1,39 @@
struct Base {
}
interface Foo {
parent Foo
thing(mut b Base, value i64) string
}
struct Bar {
parent Foo
}
fn (f Bar) thing(mut b Base, value i64) string {
return 'bar'
}
struct SubBar {
parent Foo = Bar{}
}
fn (f SubBar) thing(mut b Base, value i64) string {
return 'subbar'
}
fn test_interface_nested_field() {
mut foo_group := []Foo{}
foo_group << Bar{}
foo_group << SubBar{}
mut b := Base{}
mut ret := []string{}
for foo in foo_group {
println(foo.thing(mut b, 22))
ret << foo.thing(mut b, 22)
}
assert ret.len == 2
assert ret[0] == 'bar'
assert ret[1] == 'subbar'
}