mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Added "distinct" types.
This commit is contained in:
committed by
Christoffer Lerno
parent
3a24fbfa6d
commit
7fc12192f4
28
test/test_suite/distinct/distinct_struct.c3
Normal file
28
test/test_suite/distinct/distinct_struct.c3
Normal 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 };
|
||||
}
|
||||
20
test/test_suite/distinct/distinct_struct_array.c3
Normal file
20
test/test_suite/distinct/distinct_struct_array.c3
Normal 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;
|
||||
}
|
||||
34
test/test_suite/distinct/distinct_union.c3
Normal file
34
test/test_suite/distinct/distinct_union.c3
Normal 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 };
|
||||
}
|
||||
11
test/test_suite/distinct/test_errors.c3
Normal file
11
test/test_suite/distinct/test_errors.c3
Normal 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'
|
||||
}
|
||||
29
test/test_suite/distinct/test_ops_on_int.c3
Normal file
29
test/test_suite/distinct/test_ops_on_int.c3
Normal 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);
|
||||
}
|
||||
24
test/test_suite/distinct/test_ops_on_struct.c3
Normal file
24
test/test_suite/distinct/test_ops_on_struct.c3
Normal 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 } };
|
||||
}
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user