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

cgen/parser: fix unions

This commit is contained in:
Alexander Medvednikov 2020-04-08 01:20:55 +02:00
parent cdcb8b6c06
commit 7ff0c3aaa9
5 changed files with 26 additions and 12 deletions

View File

@ -74,5 +74,5 @@ fn test_atof() {
// DOUBLE_MINUS_ZERO // DOUBLE_MINUS_ZERO
f1=-0.0 f1=-0.0
assert *ptr == u64(0x8000000000000000) assert *ptr == u64(0x8000000000000000)
//println("DONE!") println("DONE!")
} }

View File

@ -150,6 +150,7 @@ pub:
pub_pos int // pub: pub_pos int // pub:
pub_mut_pos int // pub mut: pub_mut_pos int // pub mut:
is_c bool is_c bool
is_union bool
} }
pub struct InterfaceDecl { pub struct InterfaceDecl {

View File

@ -460,7 +460,12 @@ fn (g mut Gen) stmt(node ast.Stmt) {
// g.writeln('\t$field_type_sym.name $field.name;') // g.writeln('\t$field_type_sym.name $field.name;')
// } // }
// g.writeln('} $name;') // g.writeln('} $name;')
if !it.is_c { if it.is_c {
return
}
if it.is_union {
g.typedefs.writeln('typedef union $name $name;')
} else {
g.typedefs.writeln('typedef struct $name $name;') g.typedefs.writeln('typedef struct $name $name;')
} }
} }
@ -2121,7 +2126,11 @@ fn (g mut Gen) write_types(types []table.TypeSymbol) {
table.Struct { table.Struct {
info := typ.info as table.Struct info := typ.info as table.Struct
// g.definitions.writeln('typedef struct {') // g.definitions.writeln('typedef struct {')
if info.is_union {
g.definitions.writeln('union $name {')
} else {
g.definitions.writeln('struct $name {') g.definitions.writeln('struct $name {')
}
if info.fields.len > 0 { if info.fields.len > 0 {
for field in info.fields { for field in info.fields {
type_name := g.typ(field.typ) type_name := g.typ(field.typ)

View File

@ -1447,6 +1447,7 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
if is_pub { if is_pub {
p.next() p.next()
} }
is_union := p.tok.kind == .key_union
if p.tok.kind == .key_struct { if p.tok.kind == .key_struct {
p.check(.key_struct) p.check(.key_struct)
} else { } else {
@ -1535,6 +1536,7 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
info: table.Struct{ info: table.Struct{
fields: fields fields: fields
is_typedef: is_typedef is_typedef: is_typedef
is_union: is_union
} }
} }
mut ret := 0 mut ret := 0
@ -1558,6 +1560,7 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
pub_pos: pub_pos pub_pos: pub_pos
pub_mut_pos: pub_mut_pos pub_mut_pos: pub_mut_pos
is_c: is_c is_c: is_c
is_union: is_union
} }
} }

View File

@ -540,6 +540,7 @@ pub struct Struct {
pub mut: pub mut:
fields []Field fields []Field
is_typedef bool // C. [typedef] is_typedef bool // C. [typedef]
is_union bool
} }
pub struct Enum { pub struct Enum {