Added "distinct" types.

This commit is contained in:
Christoffer Lerno
2021-01-24 21:08:43 +01:00
committed by Christoffer Lerno
parent 3a24fbfa6d
commit 7fc12192f4
29 changed files with 402 additions and 111 deletions

View File

@@ -0,0 +1,28 @@
struct Struct
{
int x;
double y;
}
typedef Struct as distinct Foo;
struct Struct2
{
Foo f;
int d;
struct bar
{
Foo x;
}
}
Foo f = { 1, 1.0 };
func void test(int x)
{
Struct s = { 1, 2.0 };
Foo f2 = cast(s as Foo);
Foo f3 = { .x = 1 };
Struct2 s2 = { .f = { 1, 2.0 } };
Struct2 s3 = { .bar.x.y = 3.0 };
}

View File

@@ -0,0 +1,20 @@
module test;
typedef int as distinct Foo;
struct Struct
{
Foo x;
int y;
}
typedef Struct as distinct Struct2;
typedef Struct2[3] as distinct StructArr;
func void test(int x)
{
StructArr z = { { .x = 1 }, { .y = x }, { 1, 2 }};
usize xr = z.sizeof;
usize len = z.len;
Foo zz = z[2].x;
}

View File

@@ -0,0 +1,34 @@
module test;
union Union
{
int x;
double y;
}
typedef Union as distinct Foo;
union Union2
{
Foo f;
int d;
struct bar
{
Foo x;
}
}
Foo f = { .x = 1 };
typedef Union2 as distinct Union3;
typedef Union3[3] as distinct UnionArr;
func void test(int x)
{
Union s = { .y = 2.0 };
Foo f2 = cast(s as Foo);
Foo f3 = { .x = 1 };
Union2 s2 = { .f = { .y = 1 } };
Union2 s3 = { .bar.x.y = 3.0 };
UnionArr a = { [0].bar.x.x = 100 };
}

View File

@@ -0,0 +1,11 @@
module test;
typedef int as distinct Int2;
func void test()
{
Int2 a = 1;
a = a + 1;
int b;
a = b; // #error: Cannot implicitly cast 'int' to 'Int2'
}

View File

@@ -0,0 +1,29 @@
module test;
typedef int as distinct Foo;
func int test1()
{
Foo x = 1;
x += 2;
Foo y = 3;
y = x + y;
int z = 4;
y += cast(z as Foo);
y = y << z;
y = y >> z;
y = y + y;
y = y - y;
y = y * y;
y = y / y;
y = y & y;
y = y ^ y;
y = y | y;
bool a1 = y != y;
bool a2 = y < y;
bool a3 = y <= y;
bool a4 = y == y;
y = y == 1 ? 1 : y;
y = y < (y + 1) ? 1 : y;
return cast(y as int);
}

View File

@@ -0,0 +1,24 @@
module test;
struct Struct
{
int x;
double y;
}
typedef Struct as distinct Foo;
struct Struct2
{
Foo f;
int d;
}
Foo f = { 1, 1.0 };
func void test(int x)
{
Struct s = { 1, 2.0 };
Foo f2 = cast(s as Foo);
Foo f3 = { .x = 1 };
Struct2 s2 = { .f = { 1, 2.0 } };
}

View File

@@ -21,10 +21,11 @@ func void test()
entry:
%b = alloca %test.UnionB, align 8
%1 = bitcast %test.UnionB* %b to i32*
%2 = call i32 @bar()
store i32 %2, i32* %1, align 4
%3 = bitcast %test.UnionB* %b to %test.b*
%4 = bitcast %test.b* %3 to i8*
call void @llvm.memset.p0i8.i64(i8* %4, i8 0, i64 4, i1 false)
%0 = bitcast %test.UnionB* %b to i32*
%1 = call i32 @bar()
store i32 %1, i32* %0, align 4
%2 = bitcast %test.UnionB* %b to %test.b*
%3 = bitcast %test.b* %2 to i8*
call void @llvm.memset.p0i8.i64(i8* align 4 %3, i8 0, i64 4, i1 false)
ret void