Closed jkwoods closed 3 years ago
I appreciate your attention to this. Yeah, we seem to be incorrect here. However, I'm not sure that converting the integer to a boolean in this context is consistent with C semantics. Instead, the boolean should be converted to an integer. Let's consider an example program to see that the latter semantics are what GCC uses.
#include "stdio.h"
int main() {
// _Bool z = 0 ? (_Bool)(0) : (int)(5); // ill-typed
int z = 0 ? (_Bool)(0) : (int)(5); // well-typed
printf("%d\n", z);
}
The right way to handle this is probably to implement usual arithmetic conversions (C11 standard 6.3.1.8) as a function in the C frontend, and to use that function in the implementation of cIte
, as is specified by the semantics of C's conditional operator (C11 standard 6.5.15.5). I'll take care of this.
Mk, I believe I've handled this correctly now. PR (for your review, if you'd like) here: https://github.com/circify/compiler/pull/23.
C does implicit casting from ints to bools, which causes problems in the construction of ITE statements in the compiler, since the compiler only supports ITE statements with T/F branches of the same type. You get errors like "Cannot construct conditional ....", even when the code branches are typed correctly.
I have a fix for this, by adding a few lines here. Let me know if you guys want a PR of it.