hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 60445330ef8d3433a6b8f1187b2dec6838742dbb
parent 70643e4bf228bbf06031c4ccedf574437b431104
Author: Drew DeVault <sir@cmpwn.com>
Date:   Tue,  4 May 2021 14:13:52 -0400

hare::types: test explicit offsets

Signed-off-by: Drew DeVault <sir@cmpwn.com>

Diffstat:
Mhare/types/+test.ha | 35++++++++++++++++++++++++++++++-----
Mhare/types/store.ha | 1-
2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/hare/types/+test.ha b/hare/types/+test.ha @@ -1,4 +1,5 @@ use bufio; +use errors; use hare::ast; use hare::lex; use hare::parse; @@ -34,8 +35,20 @@ fn parse_type(in: str) ast::_type = { assert(htype.referent._type as builtin == builtin::INT); }; +fn resolve( + rstate: *void, + store: *typestore, + expr: const *ast::expr, +) (size | deferred | errors::opaque) = { + let expr = *expr as ast::constant_expr; + let val = expr as lex::value; + let ival = val as i64; + assert(ival >= 0); + return ival: size; +}; + @test fn structs() void = { - let st = store(x86_64, null, null); + let st = store(x86_64, &resolve, null); defer store_free(st); // Basic struct @@ -165,8 +178,20 @@ fn parse_type(in: str) ast::_type = { // TODO // Explicit offsets - // TODO - - // C compatibility testing - // TODO + let atype = parse_type("struct { + @offset(8) x: int, + @offset(16) y: int, + @offset(32) z: int, + }"); + defer ast::type_free(atype); + let htype = lookup(st, &atype)!; + assert(htype.sz == 36); + assert(htype.align == 4); + let stype = htype._type as _struct; + assert(stype.fields[0].name == "x"); + assert(stype.fields[0].offs == 8); + assert(stype.fields[1].name == "y"); + assert(stype.fields[1].offs == 16); + assert(stype.fields[2].name == "z"); + assert(stype.fields[2].offs == 32); }; diff --git a/hare/types/store.ha b/hare/types/store.ha @@ -311,7 +311,6 @@ fn struct_from_ast( return _struct { kind = if (is_union) struct_union::UNION else struct_union::STRUCT, fields = fields, - c_compat = false, // TODO: Determine C compat properly }; };