cgen: write alias typedefs just before interfaces (#10272)

pull/10285/head
Lukas Neubert 2021-05-31 11:26:06 +02:00 committed by GitHub
parent 09e3099580
commit 5b9256ba0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 22 deletions

View File

@ -770,27 +770,6 @@ pub fn (mut g Gen) write_typedef_types() {
continue
}
match typ.kind {
.alias {
parent := unsafe { &g.table.type_symbols[typ.parent_idx] }
is_c_parent := parent.name.len > 2 && parent.name[0] == `C` && parent.name[1] == `.`
mut is_typedef := false
if parent.info is ast.Struct {
is_typedef = parent.info.is_typedef
}
mut parent_styp := parent.cname
if is_c_parent {
if !is_typedef {
parent_styp = 'struct ' + parent.cname[3..]
} else {
parent_styp = parent.cname[3..]
}
} else {
if typ.info is ast.Alias {
parent_styp = g.typ(typ.info.parent_type)
}
}
g.type_definitions.writeln('typedef $parent_styp $typ.cname;')
}
.array {
g.type_definitions.writeln('typedef array $typ.cname;')
}
@ -851,7 +830,11 @@ static inline void __${typ.cname}_pushval($typ.cname ch, $el_stype val) {
}
}
}
for typ in g.table.type_symbols {
if typ.kind == .alias && typ.name !in c.builtins {
g.write_alias_typesymbol_declaration(typ)
}
}
// Generating interfaces after all the common types have been defined
// to prevent generating interface struct before definition of field types
for typ in g.table.type_symbols {
@ -861,6 +844,28 @@ static inline void __${typ.cname}_pushval($typ.cname ch, $el_stype val) {
}
}
pub fn (mut g Gen) write_alias_typesymbol_declaration(sym ast.TypeSymbol) {
parent := unsafe { &g.table.type_symbols[sym.parent_idx] }
is_c_parent := parent.name.len > 2 && parent.name[0] == `C` && parent.name[1] == `.`
mut is_typedef := false
if parent.info is ast.Struct {
is_typedef = parent.info.is_typedef
}
mut parent_styp := parent.cname
if is_c_parent {
if !is_typedef {
parent_styp = 'struct ' + parent.cname[3..]
} else {
parent_styp = parent.cname[3..]
}
} else {
if sym.info is ast.Alias {
parent_styp = g.typ(sym.info.parent_type)
}
}
g.type_definitions.writeln('typedef $parent_styp $sym.cname;')
}
pub fn (mut g Gen) write_interface_typesymbol_declaration(sym ast.TypeSymbol) {
info := sym.info as ast.Interface
g.type_definitions.writeln('typedef struct {')

View File

@ -0,0 +1,8 @@
import geometry
fn test_aliases_map_init() {
a := geometry.ShapeMap(map{
geometry.Shape.circle: 'Shape is a circle.'
})
assert a[geometry.Shape.circle] == 'Shape is a circle.'
}

View File

@ -10,6 +10,8 @@ pub enum Shape {
triangle
}
pub type ShapeMap = map[Shape]string
// used by vlib/v/tests/map_enum_keys_test.v
pub enum Form3D {
sphere