Files
c3c/test/test_suite/statements/foreach_custom_macro.c3t

94 lines
2.4 KiB
C

// #target: macos-x64
module foo;
struct Foo
{
int[] x;
}
macro int Foo.@operator_element_at(Foo* &foo, usz index) @operator([])
{
return foo.x[index];
}
macro usz Foo.@operator_len(Foo* &foo) @operator(len)
{
return foo.x.len;
}
fn void main()
{
int[*] i = { 1, 3, 10 };
Foo x = { &i };
foreach FOO: (int f : x) {
printf("%d\n", f);
while (1)
{
break FOO;
}
}
}
extern fn int printf(char *fmt, ...);
/* #expect: foo.ll
%Foo = type { %"int[]" }
%"int[]" = type { ptr, i64 }
@"$ct.foo.Foo" = linkonce global %.introspect { i8 10, i64 0, ptr null, i64 16, i64 0, i64 1, [0 x i64] zeroinitializer }, align 8
@.__const = private unnamed_addr constant [3 x i32] [i32 1, i32 3, i32 10], align 4
@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
; Function Attrs:
define void @foo.main() #0 {
entry:
%i = alloca [3 x i32], align 4
%x = alloca %Foo, align 8
%.anon = alloca i64, align 8
%.anon1 = alloca i64, align 8
%f = alloca i32, align 4
%index = alloca i64, align 8
call void @llvm.memcpy.p0.p0.i32(ptr align 4 %i, ptr align 4 @.__const, i32 12, i1 false)
%0 = insertvalue %"int[]" undef, ptr %i, 0
%1 = insertvalue %"int[]" %0, i64 3, 1
store %"int[]" %1, ptr %x, align 8
%ptradd = getelementptr inbounds i8, ptr %x, i64 8
%2 = load i64, ptr %ptradd, align 8
store i64 %2, ptr %.anon, align 8
store i64 0, ptr %.anon1, align 8
br label %loop.cond
loop.cond: ; preds = %entry
%3 = load i64, ptr %.anon1, align 8
%4 = load i64, ptr %.anon, align 8
%lt = icmp ult i64 %3, %4
br i1 %lt, label %loop.body, label %loop.exit
loop.body: ; preds = %loop.cond
%5 = load i64, ptr %.anon1, align 8
store i64 %5, ptr %index, align 8
%6 = load ptr, ptr %x, align 8
%7 = load i64, ptr %index, align 8
%ptroffset = getelementptr inbounds [4 x i8], ptr %6, i64 %7
%8 = load i32, ptr %ptroffset, align 4
store i32 %8, ptr %f, align 4
%9 = load i32, ptr %f, align 4
%10 = call i32 (ptr, ...) @printf(ptr @.str, i32 %9)
br label %loop.body2
loop.body2: ; preds = %loop.body
br label %loop.exit
loop.exit: ; preds = %loop.body2, %loop.cond
ret void
}
; Function Attrs:
declare i32 @printf(ptr, ...) #0
; Function Attrs:
define i32 @main(i32 %0, ptr %1) #0 {
entry:
call void @foo.main()
ret i32 0
}