unions (untagged)
This commit is contained in:
@@ -18,6 +18,7 @@ keyword_kind :: proc(text: string) -> token.Kind {
|
||||
case "c_func": return .Keyword_C_Func
|
||||
case "struct": return .Keyword_Struct
|
||||
case "c_struct": return .Keyword_C_Struct
|
||||
case "union": return .Keyword_Union
|
||||
case "enum": return .Keyword_Enum
|
||||
case "distinct": return .Keyword_Distinct
|
||||
case "alias": return .Keyword_Alias
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -53,6 +53,7 @@ Kind :: enum u8 {
|
||||
Keyword_C_Func,
|
||||
Keyword_Struct,
|
||||
Keyword_C_Struct,
|
||||
Keyword_Union,
|
||||
Keyword_Enum,
|
||||
Keyword_Distinct,
|
||||
Keyword_Alias,
|
||||
|
||||
Reference in New Issue
Block a user