commit c3f15effd8ebc305f83d3d34f88c2f353e469fe1
parent 385d8cfb02dce6140657c980dd0f69c19a86daf5
Author: Drew DeVault <sir@cmpwn.com>
Date: Tue, 4 May 2021 13:24:20 -0400
hare::types: basic union implementation
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
2 files changed, 20 insertions(+), 3 deletions(-)
diff --git a/hare/types/+test.ha b/hare/types/+test.ha
@@ -58,7 +58,23 @@ fn parse_type(in: str) ast::_type = {
assert(y._type._type as builtin == builtin::INT);
// Basic union
- // TODO
+ let atype = parse_type("union { x: int, y: int }");
+ defer ast::type_free(atype);
+ let htype = lookup(st, &atype)!;
+ assert(htype.sz == 4);
+ let stype = htype._type as _struct;
+ assert(stype.kind == struct_union::UNION);
+ assert(len(stype.fields) == 2);
+
+ let x = stype.fields[0];
+ assert(x.name == "x");
+ assert(x.offs == 0);
+ assert(x._type._type as builtin == builtin::INT);
+
+ let y = stype.fields[1];
+ assert(y.name == "y");
+ assert(y.offs == 0);
+ assert(y._type._type as builtin == builtin::INT);
// Alignment
// TODO
diff --git a/hare/types/store.ha b/hare/types/store.ha
@@ -265,7 +265,6 @@ fn _struct_from_ast(
sf: ast::struct_field => sf,
};
- assert(!is_union); // TODO
let _type = lookup(store, memb._type)?;
if (*offs % _type.align != 0) {
*offs += _type.align - (*offs % _type.align);
@@ -277,7 +276,9 @@ fn _struct_from_ast(
_type = _type,
});
- *offs += _type.sz;
+ if (!is_union) {
+ *offs += _type.sz;
+ };
};
};