unions (untagged)

This commit is contained in:
2026-06-28 09:16:57 +02:00
parent 3007910526
commit f328c44154
6 changed files with 77 additions and 5 deletions
+11 -4
View File
@@ -1727,16 +1727,19 @@ parse_function :: proc(parser: ^Parser, name: token.Token, c_abi: bool) {
})
}
parse_struct :: proc(parser: ^Parser, name: token.Token, c_layout: bool) {
parse_struct :: proc(parser: ^Parser, name: token.Token, c_layout: bool, is_union := false) {
start := advance(parser)
id := types.named(&parser.module.type_store, u32(parser.pkg), u32(name.symbol))
ended_by_newline := current(parser).kind == .Newline
skip_newlines(parser)
if current(parser).kind != .Left_Brace {
if !c_layout {
source.add(parser.diagnostics, start.span, "native struct declarations require a body")
source.add(
parser.diagnostics, start.span,
"native union declarations require a body" if is_union else "native struct declarations require a body",
)
}
if !types.define_struct(&parser.module.type_store, id, nil, c_layout, true) {
if !types.define_record(&parser.module.type_store, id, nil, c_layout, true, is_union) {
source.addf(parser.diagnostics, name.span, "duplicate type declaration '%s'", token_text(parser, name))
}
if !ended_by_newline {
@@ -1772,7 +1775,7 @@ parse_struct :: proc(parser: ^Parser, name: token.Token, c_layout: bool) {
if _, ok := allow(parser, .Right_Brace); !ok {
source.add(parser.diagnostics, current(parser).span, "expected '}' after struct fields")
}
if !types.define_struct(&parser.module.type_store, id, fields[:], c_layout, false) {
if !types.define_record(&parser.module.type_store, id, fields[:], c_layout, false, is_union) {
source.addf(parser.diagnostics, name.span, "duplicate type declaration '%s'", token_text(parser, name))
}
_ = finish_statement(parser)
@@ -2054,6 +2057,10 @@ parse_top_level :: proc(parser: ^Parser) {
parse_struct(parser, name, current(parser).kind == .Keyword_C_Struct)
return
}
if operator.kind == .Colon_Colon && current(parser).kind == .Keyword_Union {
parse_struct(parser, name, false, is_union=true)
return
}
if operator.kind == .Colon_Colon && current(parser).kind == .Keyword_Enum {
parse_enum(parser, name)
return