Commit 2165500
authored
Translate switches containing fallthrough (#38)
All switch tests pass now.
This PR translates switches containing fallthrough using the new
`switch!` macro defined in `libcc2rs-macros`:
```
switch!(match <condition> {
<pat> [if <guard>] => { /* body; may contain break or continue */ },
...
_ => <body>,
});
Desugars to a goto_block! with a synthetic dispatch arm prepended.
goto_block! {
'__dispatch => {
match <condition> {
<pat_1> => { __s = 1; continue '__sm; }
...
_ => break '__sm,
}
},
'__c1 => { /* body_1 with `break` rewritten to `break '__sm` */ },
...
'__cN => { /* body_N with same rewrite */ },
};
__sm is the inner label used to describe the state machine insinde goto_block. See goto_block!
for more info.
```
It's necessary to translate the switch into a goto because later we will
add support for jumping between switch arms using goto.
Below is an example of how the `switch!` macro expands:
```cpp
int fallthrough_one(int x) {
int r = 0;
switch (x) {
case 1:
r += 10;
case 2:
r += 20;
break;
default:
r = -1;
break;
}
return r;
}
```
Will be translated as:
```rs
switch!(match x {
v if v == 1 => {
r += 10;
}
v if v == 2 => {
r += 20;
break;
}
_ => {
r = -1_i32;
break;
}
});
```
Which will expand into (see comments attached to each arm):
```rs
{
let mut __s: u32 = 0;
#[allow(unreachable_code, unused_labels)]
'__sm: loop {
match __s {
// First arm is the dispatch arm. It decides which is the first state: [1u32, ...]
0u32 => {
#[allow(unreachable_code)]
{
{
#[allow(unreachable_patterns)]
match x {
v if v == 1 => { __s = 1u32; continue '__sm; }
v if v == 2 => { __s = 2u32; continue '__sm; }
_ => { __s = 3u32; continue '__sm; }
_ => break '__sm,
}
};
__s = 1u32;
continue '__sm;
}
}
1u32 => {
// First real arm, it contains the body of the original arm + fallthrough to the following state, i.e. 2u32
#[allow(unreachable_code)]
{ { r += 10; }; __s = 2u32; continue '__sm; }
}
2u32 => {
// Second real arm, inside the body there is a break statement that stops the fallthrough
#[allow(unreachable_code)]
{ { r += 20; break '__sm; }; __s = 3u32; continue '__sm; }
}
3u32 => {
// Default arm from the original match, the last arm breaks the state machine instea of continuing
#[allow(unreachable_code)]
{ { r = -1_i32; break '__sm; }; break '__sm; }
}
// This is here only for match exhaustiveness, it's never used
_ => break '__sm,
}
}
};
```1 parent 5bdf6a1 commit 2165500
36 files changed
Lines changed: 1047 additions & 191 deletions
File tree
- .github/workflows
- cpp2rust/converter
- libcc2rs-macros
- src
- tests
- ui
- libcc2rs
- src
- tests/unit
- out
- refcount
- unsafe
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
| 56 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
| 103 | + | |
103 | 104 | | |
104 | 105 | | |
105 | 106 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2706 | 2706 | | |
2707 | 2707 | | |
2708 | 2708 | | |
2709 | | - | |
| 2709 | + | |
| 2710 | + | |
| 2711 | + | |
| 2712 | + | |
2710 | 2713 | | |
2711 | 2714 | | |
2712 | 2715 | | |
2713 | | - | |
2714 | | - | |
2715 | | - | |
2716 | | - | |
| 2716 | + | |
| 2717 | + | |
| 2718 | + | |
| 2719 | + | |
| 2720 | + | |
| 2721 | + | |
| 2722 | + | |
| 2723 | + | |
| 2724 | + | |
| 2725 | + | |
| 2726 | + | |
| 2727 | + | |
| 2728 | + | |
| 2729 | + | |
| 2730 | + | |
| 2731 | + | |
| 2732 | + | |
| 2733 | + | |
2717 | 2734 | | |
2718 | 2735 | | |
2719 | 2736 | | |
| |||
2730 | 2747 | | |
2731 | 2748 | | |
2732 | 2749 | | |
2733 | | - | |
2734 | | - | |
2735 | 2750 | | |
2736 | 2751 | | |
2737 | 2752 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
501 | 501 | | |
502 | 502 | | |
503 | 503 | | |
504 | | - | |
| 504 | + | |
505 | 505 | | |
506 | 506 | | |
507 | 507 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
744 | 744 | | |
745 | 745 | | |
746 | 746 | | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
747 | 777 | | |
748 | 778 | | |
749 | 779 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
| 167 | + | |
| 168 | + | |
167 | 169 | | |
168 | 170 | | |
169 | 171 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
0 commit comments