commit 2390c388e0aed49bfe2ffbe4e594d70b630b4c5a
parent cfd4528c2d3d79dd2adc4c302f46df9ed8390c2d
Author: Drew DeVault <sir@cmpwn.com>
Date: Tue, 4 May 2021 17:42:07 -0400
hare::types: tagged union data structures & hash
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
3 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/hare/types/hash.ha b/hare/types/hash.ha
@@ -41,6 +41,7 @@ fn type_storage(t: *_type) u8 = match (t._type) {
storage::STRUCT
else storage::UNION,
tuple => storage::TUPLE,
+ tagged => storage::TAGGED,
};
fn write8(h: *hash::hash, u: u8) void = {
@@ -90,6 +91,9 @@ export fn hash(t: *_type) u32 = {
tu: tuple => for (let i = 0z; i < len(tu); i += 1) {
write32(id, hash(tu[i]._type));
},
+ ta: tagged => for (let i = 0z; i < len(ta); i += 1) {
+ write32(id, hash(ta[i]));
+ },
};
return fnv::sum32(id);
@@ -192,4 +196,23 @@ export fn hash(t: *_type) u32 = {
},
};
assert(hash(&sample) == 1353959835);
+
+ let _uint = _type {
+ flags = flags::NONE,
+ _type = builtin::UINT,
+ };
+ let _void = _type {
+ flags = flags::NONE,
+ _type = builtin::VOID,
+ };
+
+ let sample = _type {
+ flags = flags::NONE,
+ _type = [
+ &_int,
+ &_uint,
+ &_void,
+ ]: tagged,
+ };
+ assert(hash(&sample) == 3951208153);
};
diff --git a/hare/types/store.ha b/hare/types/store.ha
@@ -236,6 +236,7 @@ fn fromast(store: *typestore, atype: *ast::_type) (_type | deferred | error) = {
};
tu;
},
+ ta: ast::tagged_type => abort(), // TODO
* => abort(), // TODO
};
return _type {
diff --git a/hare/types/types.ha b/hare/types/types.ha
@@ -57,6 +57,9 @@ export type struct_field = struct {
_type: const *_type,
};
+// A tagged union type, e.g. (int | uint | void).
+export type tagged = []*_type;
+
// A tuple type, e.g. (a, b, c)
export type tuple = []tuple_value;
@@ -81,7 +84,7 @@ export def SIZE_UNDEFINED: size = -1: size;
// A Hare type.
export type _type = struct {
flags: flags,
- _type: (alias | array | builtin | pointer | _struct | tuple),
+ _type: (alias | array | builtin | pointer | _struct | tagged | tuple),
id: u32,
sz: size,
align: size,