| author | |
| committer | |
| log | cf5932b236424d8e1b52500bc80ffb6d79e20134 |
| tree | 54e4caa1a621f4a96ef13d98d1a831a5614de1a9 |
| parent | dfcac3cd76c899c412fa3b78946ebce4f42f2ae8 |
| signature | Commit is signed but in an unrecognized format. |
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 |
| 1086 | 1086 | |
| 1087 | 1087 | pub extern fn ZigClangIntegerLiteral_EvaluateAsInt(*const ZigClangIntegerLiteral, *ZigClangExprEvalResult, *const ZigClangASTContext) bool; |
| 1088 | 1088 | pub extern fn ZigClangIntegerLiteral_getBeginLoc(*const ZigClangIntegerLiteral) ZigClangSourceLocation; |
| 1089 | pub extern fn ZigClangIntegerLiteral_isZero(*const ZigClangIntegerLiteral, *bool, *const ZigClangASTContext) bool; | |
| 1089 | 1090 | |
| 1090 | 1091 | pub extern fn ZigClangReturnStmt_getRetValue(*const ZigClangReturnStmt) ?*const ZigClangExpr; |
| 1091 | 1092 |
src-self-hosted/translate_c.zig+8| ... | ... | @@ -1684,6 +1684,14 @@ fn transBoolExpr( |
| 1684 | 1684 | lrvalue: LRValue, |
| 1685 | 1685 | grouped: bool, |
| 1686 | 1686 | ) 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 | ||
| 1687 | 1695 | const lparen = if (grouped) |
| 1688 | 1696 | try appendToken(rp.c, .LParen, "(") |
| 1689 | 1697 | else |
src/zig_clang.cpp+13| ... | ... | @@ -2510,6 +2510,19 @@ struct ZigClangSourceLocation ZigClangIntegerLiteral_getBeginLoc(const struct Zi |
| 2510 | 2510 | return bitcast(casted->getBeginLoc()); |
| 2511 | 2511 | } |
| 2512 | 2512 | |
| 2513 | bool 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 | ||
| 2513 | 2526 | const struct ZigClangExpr *ZigClangReturnStmt_getRetValue(const struct ZigClangReturnStmt *self) { |
| 2514 | 2527 | auto casted = reinterpret_cast<const clang::ReturnStmt *>(self); |
| 2515 | 2528 | 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 |
| 1086 | 1086 | |
| 1087 | 1087 | ZIG_EXTERN_C bool ZigClangIntegerLiteral_EvaluateAsInt(const struct ZigClangIntegerLiteral *, struct ZigClangExprEvalResult *, const struct ZigClangASTContext *); |
| 1088 | 1088 | ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangIntegerLiteral_getBeginLoc(const struct ZigClangIntegerLiteral *); |
| 1089 | ZIG_EXTERN_C bool ZigClangIntegerLiteral_isZero(const struct ZigClangIntegerLiteral *, bool *, const struct ZigClangASTContext *); | |
| 1089 | 1090 | |
| 1090 | 1091 | ZIG_EXTERN_C const struct ZigClangExpr *ZigClangReturnStmt_getRetValue(const struct ZigClangReturnStmt *); |
| 1091 | 1092 |
test/translate_c.zig+12-12| ... | ... | @@ -99,10 +99,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 99 | 99 | \\} |
| 100 | 100 | , &[_][]const u8{ |
| 101 | 101 | \\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) {}; | |
| 104 | 104 | \\ while (true) while (true) { |
| 105 | \\ if (!(@as(c_int, 0) != 0)) break; | |
| 105 | \\ if (!false) break; | |
| 106 | 106 | \\ }; |
| 107 | 107 | \\} |
| 108 | 108 | }); |
| ... | ... | @@ -1634,8 +1634,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1634 | 1634 | , &[_][]const u8{ |
| 1635 | 1635 | \\pub export fn foo() c_int { |
| 1636 | 1636 | \\ 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) { | |
| 1639 | 1639 | \\ var a_1: c_int = 4; |
| 1640 | 1640 | \\ a_1 = 9; |
| 1641 | 1641 | \\ _ = @as(c_int, 6); |
| ... | ... | @@ -1644,11 +1644,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1644 | 1644 | \\ while (true) { |
| 1645 | 1645 | \\ var a_1: c_int = 2; |
| 1646 | 1646 | \\ a_1 = 12; |
| 1647 | \\ if (!(@as(c_int, 4) != 0)) break; | |
| 1647 | \\ if (!true) break; | |
| 1648 | 1648 | \\ } |
| 1649 | 1649 | \\ while (true) { |
| 1650 | 1650 | \\ a = 7; |
| 1651 | \\ if (!(@as(c_int, 4) != 0)) break; | |
| 1651 | \\ if (!true) break; | |
| 1652 | 1652 | \\ } |
| 1653 | 1653 | \\} |
| 1654 | 1654 | }); |
| ... | ... | @@ -1702,8 +1702,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1702 | 1702 | \\} |
| 1703 | 1703 | , &[_][]const u8{ |
| 1704 | 1704 | \\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); | |
| 1707 | 1707 | \\} |
| 1708 | 1708 | }); |
| 1709 | 1709 | |
| ... | ... | @@ -2214,7 +2214,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2214 | 2214 | \\} |
| 2215 | 2215 | , &[_][]const u8{ |
| 2216 | 2216 | \\pub export fn foo() c_int { |
| 2217 | \\ if (@as(c_int, 2) != 0) { | |
| 2217 | \\ if (true) { | |
| 2218 | 2218 | \\ var a: c_int = 2; |
| 2219 | 2219 | \\ } |
| 2220 | 2220 | \\ if ((blk: { |
| ... | ... | @@ -2748,8 +2748,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2748 | 2748 | \\} |
| 2749 | 2749 | , &[_][]const u8{ |
| 2750 | 2750 | \\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; | |
| 2753 | 2753 | \\ }; |
| 2754 | 2754 | \\} |
| 2755 | 2755 | }); |