authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-09 19:24:30+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-11 12:24:45+03:00
logcf5932b236424d8e1b52500bc80ffb6d79e20134
tree54e4caa1a621f4a96ef13d98d1a831a5614de1a9
parentdfcac3cd76c899c412fa3b78946ebce4f42f2ae8
signature Commit is signed but in an unrecognized format.

translate-c: convert int to bool if bool is expected


5 files changed, 35 insertions(+), 12 deletions(-)

src-self-hosted/clang.zig+1
......@@ -1086,6 +1086,7 @@ pub extern fn ZigClangVarDecl_getTypeSourceInfo_getType(self: *const struct_ZigC
10861086
10871087pub extern fn ZigClangIntegerLiteral_EvaluateAsInt(*const ZigClangIntegerLiteral, *ZigClangExprEvalResult, *const ZigClangASTContext) bool;
10881088pub extern fn ZigClangIntegerLiteral_getBeginLoc(*const ZigClangIntegerLiteral) ZigClangSourceLocation;
1089pub extern fn ZigClangIntegerLiteral_isZero(*const ZigClangIntegerLiteral, *bool, *const ZigClangASTContext) bool;
10891090
10901091pub extern fn ZigClangReturnStmt_getRetValue(*const ZigClangReturnStmt) ?*const ZigClangExpr;
10911092
src-self-hosted/translate_c.zig+8
......@@ -1684,6 +1684,14 @@ fn transBoolExpr(
16841684 lrvalue: LRValue,
16851685 grouped: bool,
16861686) TransError!*ast.Node {
1687 if (ZigClangStmt_getStmtClass(@ptrCast(*const ZigClangStmt, expr)) == .IntegerLiteralClass) {
1688 var is_zero: bool = undefined;
1689 if (!ZigClangIntegerLiteral_isZero(@ptrCast(*const ZigClangIntegerLiteral, expr), &is_zero, rp.c.clang_context)) {
1690 return revertAndWarn(rp, error.UnsupportedTranslation, ZigClangExpr_getBeginLoc(expr), "invalid integer literal", .{});
1691 }
1692 return try transCreateNodeBoolLiteral(rp.c, !is_zero);
1693 }
1694
16871695 const lparen = if (grouped)
16881696 try appendToken(rp.c, .LParen, "(")
16891697 else
src/zig_clang.cpp+13
......@@ -2510,6 +2510,19 @@ struct ZigClangSourceLocation ZigClangIntegerLiteral_getBeginLoc(const struct Zi
25102510 return bitcast(casted->getBeginLoc());
25112511}
25122512
2513bool ZigClangIntegerLiteral_isZero(const struct ZigClangIntegerLiteral *self, bool *result, const struct ZigClangASTContext *ctx) {
2514 auto casted_self = reinterpret_cast<const clang::IntegerLiteral *>(self);
2515 auto casted_ctx = reinterpret_cast<const clang::ASTContext *>(ctx);
2516 clang::Expr::EvalResult eval_result;
2517 if (!casted_self->EvaluateAsInt(eval_result, *casted_ctx)) {
2518 return false;
2519 }
2520 const llvm::APSInt result_int = eval_result.Val.getInt();
2521 const llvm::APSInt zero(result_int.getBitWidth(), result_int.isUnsigned());
2522 *result = zero == result_int;
2523 return true;
2524}
2525
25132526const struct ZigClangExpr *ZigClangReturnStmt_getRetValue(const struct ZigClangReturnStmt *self) {
25142527 auto casted = reinterpret_cast<const clang::ReturnStmt *>(self);
25152528 return reinterpret_cast<const struct ZigClangExpr *>(casted->getRetValue());
src/zig_clang.h+1
......@@ -1086,6 +1086,7 @@ ZIG_EXTERN_C struct ZigClangQualType ZigClangCStyleCastExpr_getType(const struct
10861086
10871087ZIG_EXTERN_C bool ZigClangIntegerLiteral_EvaluateAsInt(const struct ZigClangIntegerLiteral *, struct ZigClangExprEvalResult *, const struct ZigClangASTContext *);
10881088ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangIntegerLiteral_getBeginLoc(const struct ZigClangIntegerLiteral *);
1089ZIG_EXTERN_C bool ZigClangIntegerLiteral_isZero(const struct ZigClangIntegerLiteral *, bool *, const struct ZigClangASTContext *);
10891090
10901091ZIG_EXTERN_C const struct ZigClangExpr *ZigClangReturnStmt_getRetValue(const struct ZigClangReturnStmt *);
10911092
test/translate_c.zig+12-12
......@@ -99,10 +99,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
9999 \\}
100100 , &[_][]const u8{
101101 \\pub export fn foo() void {
102 \\ while (@as(c_int, 0) != 0) while (@as(c_int, 0) != 0) {};
103 \\ while (true) while (@as(c_int, 0) != 0) {};
102 \\ while (false) while (false) {};
103 \\ while (true) while (false) {};
104104 \\ while (true) while (true) {
105 \\ if (!(@as(c_int, 0) != 0)) break;
105 \\ if (!false) break;
106106 \\ };
107107 \\}
108108 });
......@@ -1634,8 +1634,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
16341634 , &[_][]const u8{
16351635 \\pub export fn foo() c_int {
16361636 \\ var a: c_int = 5;
1637 \\ while (@as(c_int, 2) != 0) a = 2;
1638 \\ while (@as(c_int, 4) != 0) {
1637 \\ while (true) a = 2;
1638 \\ while (true) {
16391639 \\ var a_1: c_int = 4;
16401640 \\ a_1 = 9;
16411641 \\ _ = @as(c_int, 6);
......@@ -1644,11 +1644,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
16441644 \\ while (true) {
16451645 \\ var a_1: c_int = 2;
16461646 \\ a_1 = 12;
1647 \\ if (!(@as(c_int, 4) != 0)) break;
1647 \\ if (!true) break;
16481648 \\ }
16491649 \\ while (true) {
16501650 \\ a = 7;
1651 \\ if (!(@as(c_int, 4) != 0)) break;
1651 \\ if (!true) break;
16521652 \\ }
16531653 \\}
16541654 });
......@@ -1702,8 +1702,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
17021702 \\}
17031703 , &[_][]const u8{
17041704 \\pub export fn bar() c_int {
1705 \\ if ((if (@as(c_int, 2) != 0) @as(c_int, 5) else (if (@as(c_int, 5) != 0) @as(c_int, 4) else @as(c_int, 6))) != 0) _ = @as(c_int, 2);
1706 \\ return if (@as(c_int, 2) != 0) @as(c_int, 5) else if (@as(c_int, 5) != 0) @as(c_int, 4) else @as(c_int, 6);
1705 \\ if ((if (true) @as(c_int, 5) else (if (true) @as(c_int, 4) else @as(c_int, 6))) != 0) _ = @as(c_int, 2);
1706 \\ return if (true) @as(c_int, 5) else if (true) @as(c_int, 4) else @as(c_int, 6);
17071707 \\}
17081708 });
17091709
......@@ -2214,7 +2214,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
22142214 \\}
22152215 , &[_][]const u8{
22162216 \\pub export fn foo() c_int {
2217 \\ if (@as(c_int, 2) != 0) {
2217 \\ if (true) {
22182218 \\ var a: c_int = 2;
22192219 \\ }
22202220 \\ if ((blk: {
......@@ -2748,8 +2748,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
27482748 \\}
27492749 , &[_][]const u8{
27502750 \\pub fn foo() callconv(.C) void {
2751 \\ if (@as(c_int, 1) != 0) while (true) {
2752 \\ if (!(@as(c_int, 0) != 0)) break;
2751 \\ if (true) while (true) {
2752 \\ if (!false) break;
27532753 \\ };
27542754 \\}
27552755 });