Files
c3c/test/test_suite/from_docs/examples_forswitch.c3t
Christoffer Lerno 09d50ebf6c New import rules.
2022-06-04 01:41:23 +02:00

237 lines
7.3 KiB
C

// #target: macos-x64
module examples;
import libc;
import std;
fn void example_for()
{
// the for-loop is the same as C99.
for (int i = 0; i < 10; i++)
{
libc::printf("%d\n", i);
}
// also equal
for (;;)
{
// ..
}
}
enum Height : uint
{
LOW,
MEDIUM,
HIGH,
}
fn void demo_enum(Height h)
{
switch (h)
{
case LOW:
case MEDIUM:
io::println("Not high");
// Implicit break.
case HIGH:
io::println("High");
}
// This also works
switch (h)
{
case LOW:
case MEDIUM:
io::println("Not high");
// Implicit break.
case Height.HIGH:
io::println("High");
}
// Completely empty cases are not allowed.
switch (h)
{
case LOW:
break; // Explicit break required, since switches can't be empty.
case MEDIUM:
io::println("Medium");
case HIGH:
break;
}
// special checking of switching on enum types
switch (h)
{
case LOW:
case MEDIUM:
case HIGH:
break;
default: // warning: default label in switch which covers all enumeration value
break;
}
// Using "nextcase" will fallthrough to the next case statement,
// and each case statement starts its own scope.
switch (h)
{
case LOW:
int a = 1;
io::println("A");
nextcase;
case MEDIUM:
int a = 2;
io::println("B");
nextcase;
case HIGH:
// a is not defined here
io::println("C");
}
}
/* #expect: examples.ll
define void @examples.example_for() #0 {
entry:
%i = alloca i32, align 4
store i32 0, i32* %i, align 4
br label %loop.cond
loop.cond: ; preds = %loop.body, %entry
%0 = load i32, i32* %i, align 4
%lt = icmp slt i32 %0, 10
br i1 %lt, label %loop.body, label %loop.exit
loop.body: ; preds = %loop.cond
%1 = load i32, i32* %i, align 4
%2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i32 %1)
%3 = load i32, i32* %i, align 4
%add = add i32 %3, 1
store i32 %add, i32* %i, align 4
br label %loop.cond
loop.exit: ; preds = %loop.cond
call void @"std::builtin.panic"(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @0, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @1, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @2, i64 0, i64 0), i32 14)
unreachable
unreachable_block: ; No predecessors!
ret void
}
; Function Attrs: nounwind
define void @examples.demo_enum(i32 %0) #0 {
entry:
%switch = alloca i32, align 4
%switch2 = alloca i32, align 4
%switch7 = alloca i32, align 4
%switch13 = alloca i32, align 4
%switch17 = alloca i32, align 4
%a = alloca i32, align 4
%a21 = alloca i32, align 4
store i32 %0, i32* %switch, align 4
br label %switch.entry
switch.entry: ; preds = %entry
%1 = load i32, i32* %switch, align 4
switch i32 %1, label %switch.exit [
i32 0, label %switch.case
i32 1, label %switch.case
i32 2, label %switch.case1
]
switch.case: ; preds = %switch.entry, %switch.entry
%2 = call i32 @"std::io.println"(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i32 0, i32 0)) #1
br label %switch.exit
switch.case1: ; preds = %switch.entry
%3 = call i32 @"std::io.println"(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i32 0, i32 0)) #1
br label %switch.exit
switch.exit: ; preds = %switch.case1, %switch.case, %switch.entry
store i32 %0, i32* %switch2, align 4
br label %switch.entry3
switch.entry3: ; preds = %switch.exit
%4 = load i32, i32* %switch2, align 4
switch i32 %4, label %switch.exit6 [
i32 0, label %switch.case4
i32 1, label %switch.case4
i32 2, label %switch.case5
]
switch.case4: ; preds = %switch.entry3, %switch.entry3
%5 = call i32 @"std::io.println"(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i32 0, i32 0)) #1
br label %switch.exit6
switch.case5: ; preds = %switch.entry3
%6 = call i32 @"std::io.println"(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i32 0, i32 0)) #1
br label %switch.exit6
switch.exit6: ; preds = %switch.case5, %switch.case4, %switch.entry3
store i32 %0, i32* %switch7, align 4
br label %switch.entry8
switch.entry8: ; preds = %switch.exit6
%7 = load i32, i32* %switch7, align 4
switch i32 %7, label %switch.exit12 [
i32 0, label %switch.case9
i32 1, label %switch.case10
i32 2, label %switch.case11
]
switch.case9: ; preds = %switch.entry8
br label %switch.exit12
switch.case10: ; preds = %switch.entry8
%8 = call i32 @"std::io.println"(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i32 0, i32 0)) #1
br label %switch.exit12
switch.case11: ; preds = %switch.entry8
br label %switch.exit12
switch.exit12: ; preds = %switch.case11, %switch.case10, %switch.case9, %switch.entry8
store i32 %0, i32* %switch13, align 4
br label %switch.entry14
switch.entry14: ; preds = %switch.exit12
%9 = load i32, i32* %switch13, align 4
switch i32 %9, label %switch.default [
i32 0, label %switch.case15
i32 1, label %switch.case15
i32 2, label %switch.case15
]
switch.case15: ; preds = %switch.entry14, %switch.entry14, %switch.entry14
br label %switch.exit16
switch.default: ; preds = %switch.entry14
br label %switch.exit16
switch.exit16: ; preds = %switch.default, %switch.case15
store i32 %0, i32* %switch17, align 4
br label %switch.entry18
switch.entry18: ; preds = %switch.exit16
%10 = load i32, i32* %switch17, align 4
switch i32 %10, label %switch.exit23 [
i32 0, label %switch.case19
i32 1, label %switch.case20
i32 2, label %switch.case22
]
switch.case19: ; preds = %switch.entry18
store i32 1, i32* %a, align 4
%11 = call i32 @"std::io.println"(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i32 0, i32 0)) #1
br label %switch.case20
switch.case20: ; preds = %switch.entry18, %switch.case19
store i32 2, i32* %a21, align 4
%12 = call i32 @"std::io.println"(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i32 0, i32 0)) #1
br label %switch.case22
switch.case22: ; preds = %switch.entry18, %switch.case20
%13 = call i32 @"std::io.println"(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i32 0, i32 0)) #1
br label %switch.exit23
switch.exit23: ; preds = %switch.case22, %switch.entry18
ret void
}