Files
c3c/test/test_suite/statements/foreach_r_custom_macro.c3t
2022-08-16 16:51:06 +02:00

94 lines
2.6 KiB
C

// #target: macos-x64
module test;
struct Foo
{
int[] x;
}
macro int Foo.@operator_element_at(Foo &foo, usize index) @operator(elementat)
{
return foo.x[index];
}
macro usize Foo.@operator_len(Foo &foo) @operator(len)
{
return foo.x.len;
}
fn void main()
{
int[*] i = { 1, 3, 10 };
Foo x = { &i };
foreach_r FOO: (int f : x) {
printf("%d\n", f);
while (1)
{
break FOO;
}
}
}
extern fn int printf(char *fmt, ...);
/* #expect: test.ll
; Function Attrs: nounwind
define void @test_main() #0 {
entry:
%i = alloca [3 x i32], align 4
%x = alloca %Foo, align 8
%.anon = alloca i64, align 8
%f = alloca i32, align 4
%index = alloca i64, align 8
%0 = bitcast [3 x i32]* %i to i8*
call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 %0, i8* align 4 bitcast ([3 x i32]* @.__const to i8*), i32 12, i1 false)
%1 = getelementptr inbounds %Foo, %Foo* %x, i32 0, i32 0
%2 = bitcast [3 x i32]* %i to i32*
%3 = insertvalue %"int[]" undef, i32* %2, 0
%4 = insertvalue %"int[]" %3, i64 3, 1
store %"int[]" %4, %"int[]"* %1, align 8
%5 = getelementptr inbounds %Foo, %Foo* %x, i32 0, i32 0
%6 = getelementptr inbounds %"int[]", %"int[]"* %5, i32 0, i32 1
%7 = load i64, i64* %6, align 8
store i64 %7, i64* %.anon, align 8
br label %loop.cond
loop.cond: ; preds = %entry
%8 = load i64, i64* %.anon, align 8
%gt = icmp ugt i64 %8, 0
br i1 %gt, label %loop.body, label %loop.exit
loop.body: ; preds = %loop.cond
%9 = load i64, i64* %.anon, align 8
%sub = sub i64 %9, 1
store i64 %sub, i64* %.anon, align 8
%10 = load i64, i64* %.anon, align 8
store i64 %10, i64* %index, align 8
%11 = getelementptr inbounds %Foo, %Foo* %x, i32 0, i32 0
%12 = getelementptr inbounds %"int[]", %"int[]"* %11, i32 0, i32 0
%13 = load i32*, i32** %12, align 8
%14 = load i64, i64* %index, align 8
%ptroffset = getelementptr inbounds i32, i32* %13, i64 %14
%15 = load i32, i32* %ptroffset, align 4
store i32 %15, i32* %f, align 4
%16 = load i32, i32* %f, align 4
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i32 %16)
br label %loop.body1
loop.body1: ; preds = %loop.body
br label %loop.exit
loop.exit: ; preds = %loop.body1, %loop.cond
ret void
}
; Function Attrs: nounwind
declare i32 @printf(i8*, ...) #0
; Function Attrs: nounwind
define i32 @main(i32 %0, i8** %1) #0 {
entry:
call void @test_main()
ret i32 0
}