hare

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

commit 0814147ba9dd087e0e3cf1b85c5e705f8aa0e06a
parent 2743b7f149a001f62972264ef7da67e0b7278009
Author: Drew DeVault <sir@cmpwn.com>
Date:   Fri, 20 Aug 2021 17:42:49 +0200

hare::types: add trailing padding to types

This adjusts the padding on any type which does not have a size which is
a multiple of its alignment, which provides a similar fix as was added
in harec 3899d4bd6f27663d8256cf48209dbe788bf241ce.

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

Diffstat:
Mhare/types/+test.ha | 6++++++
Mhare/types/store.ha | 3+++
2 files changed, 9 insertions(+), 0 deletions(-)

diff --git a/hare/types/+test.ha b/hare/types/+test.ha @@ -5,6 +5,7 @@ use hare::lex; use hare::parse; use io; use strings; +use fmt; fn parse_type(in: str) ast::_type = { let buf = bufio::fixed(strings::toutf8(in), io::mode::READ); @@ -109,6 +110,11 @@ fn resolve( let z = stype.fields[3]; assert(z.offs == 16); + let atype = parse_type("struct { x: u8, y: size, z: u8 }"); + defer ast::type_free(atype); + let htype = lookup(st, &atype)!; + assert(htype.sz == 24); + // Sort order let atype = parse_type("struct { z: u8, y: u8, x: u8, q: u8 }"); defer ast::type_free(atype); diff --git a/hare/types/store.ha b/hare/types/store.ha @@ -261,6 +261,9 @@ fn fromast(store: *typestore, atype: *ast::_type) (_type | deferred | error) = { }, et: ast::enum_type => abort(), // TODO }; + if (sz != SIZE_UNDEFINED && sz != 0 && sz % align != 0) { + sz += align - (sz - align) % align; + }; return _type { flags = atype.flags: flags, _type = underlying,