Support distrinct types as the base type of bitstructs. #2218

This commit is contained in:
Christoffer Lerno
2025-06-17 16:49:46 +02:00
parent 99e29bff8d
commit 93640699be
3 changed files with 51 additions and 2 deletions

View File

@@ -22,6 +22,7 @@
- The form-feed character '\f' is no longer valid white space.
- Show code that caused unreachable code #2207
- Allow generics over distinct types #2216.
- Support distrinct types as the base type of bitstructs. #2218
### Fixes
- `-2147483648`, MIN literals work correctly.

View File

@@ -1061,8 +1061,8 @@ static bool sema_analyse_bitstruct(SemaContext *context, Decl *decl, bool *erase
if (*erase_decl) return true;
DEBUG_LOG("Beginning analysis of %s.", decl->name ? decl->name : ".anon");
if (!sema_resolve_type_info(context, decl->strukt.container_type, RESOLVE_TYPE_DEFAULT)) return false;
Type *type = decl->strukt.container_type->type->canonical;
Type *base_type = type->type_kind == TYPE_ARRAY ? type->array.base : type;
Type *type = type_flatten(decl->strukt.container_type->type->canonical);
Type *base_type = type->type_kind == TYPE_ARRAY ? type_flatten(type->array.base) : type;
if (!type_is_integer(base_type))
{
SEMA_ERROR(decl->strukt.container_type, "The type of the bitstruct cannot be %s but must be an integer or an array of integers.",

View File

@@ -0,0 +1,48 @@
// #target: macos-x64
module test;
typedef Foo = int;
bitstruct Bar : Foo
{
bool a : 0;
Foo x : 1..4;
}
typedef Baz = char;
bitstruct Bar2 : Baz[3]
{
bool x;
}
fn int main()
{
Bar x;
Bar2 y;
x.a = true;
x.x = 100;
return 0;
}
/* #expect: test.ll
define i32 @main() #0 {
entry:
%x = alloca i32, align 4
%y = alloca [3 x i8], align 1
store i32 0, ptr %x, align 4
store i8 0, ptr %y, align 1
%ptradd = getelementptr inbounds i8, ptr %y, i64 1
store i8 0, ptr %ptradd, align 1
%ptradd1 = getelementptr inbounds i8, ptr %y, i64 2
store i8 0, ptr %ptradd1, align 1
%0 = load i32, ptr %x, align 4
%1 = and i32 %0, -2
%2 = or i32 %1, 1
store i32 %2, ptr %x, align 4
%3 = load i32, ptr %x, align 4
%4 = and i32 %3, -31
%5 = or i32 %4, 8
store i32 %5, ptr %x, align 4
ret i32 0
}