-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix C++ compatibility of unions with degenerate variant names
This fixes the C codegen so that when a union has a variant with the same name as the union, the generated code can be compiled as C or C++. Prior to this change it was valid C but not valid C++ due to differences in the rules for anonymous structs. I also added a test that fails before this change and passes afterwards.
- Loading branch information
Showing
4 changed files
with
52 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
22 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
default Order dec | ||
|
||
$include <prelude.sail> | ||
$include <string.sail> | ||
|
||
// Ensure that unions with variants that match the union name work. | ||
// In the past the generated C code was not compatible with C++ due | ||
// to different rules around anonymous unions. | ||
|
||
union n0 = { | ||
n0 : int, | ||
} | ||
|
||
union n1 = { | ||
n2 : int, | ||
n1 : int, | ||
} | ||
|
||
// newtype is syntactic sugar for a single entry union. | ||
newtype n3 = n3 : int | ||
|
||
enum n4 = { n4 } | ||
|
||
function main() -> unit = { | ||
// Prevent dead-code elimination of the types. | ||
// This is not very robust but it works for now. | ||
let v0 = n0(1); | ||
let v1 = n1(2); | ||
let v3 = n3(3); | ||
let v4 = n4; | ||
let x : int = match v1 { | ||
n2(d) => d + 10, | ||
n1(d) => d + 20, | ||
}; | ||
print_endline(dec_str(x)); | ||
} |