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
signaturelock-open 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...@@ -1086,6 +1086,7 @@ pub extern fn ZigClangVarDecl_getTypeSourceInfo_getType(self: *const struct_ZigC
10861086
1087pub extern fn ZigClangIntegerLiteral_EvaluateAsInt(*const ZigClangIntegerLiteral, *ZigClangExprEvalResult, *const ZigClangASTContext) bool;1087pub extern fn ZigClangIntegerLiteral_EvaluateAsInt(*const ZigClangIntegerLiteral, *ZigClangExprEvalResult, *const ZigClangASTContext) bool;
1088pub extern fn ZigClangIntegerLiteral_getBeginLoc(*const ZigClangIntegerLiteral) ZigClangSourceLocation;1088pub extern fn ZigClangIntegerLiteral_getBeginLoc(*const ZigClangIntegerLiteral) ZigClangSourceLocation;
1089pub extern fn ZigClangIntegerLiteral_isZero(*const ZigClangIntegerLiteral, *bool, *const ZigClangASTContext) bool;
10891090
1090pub extern fn ZigClangReturnStmt_getRetValue(*const ZigClangReturnStmt) ?*const ZigClangExpr;1091pub extern fn ZigClangReturnStmt_getRetValue(*const ZigClangReturnStmt) ?*const ZigClangExpr;
10911092
src-self-hosted/translate_c.zig+8
...@@ -1684,6 +1684,14 @@ fn transBoolExpr(...@@ -1684,6 +1684,14 @@ fn transBoolExpr(
1684 lrvalue: LRValue,1684 lrvalue: LRValue,
1685 grouped: bool,1685 grouped: bool,
1686) TransError!*ast.Node {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 const lparen = if (grouped)1695 const lparen = if (grouped)
1688 try appendToken(rp.c, .LParen, "(")1696 try appendToken(rp.c, .LParen, "(")
1689 else1697 else
src/zig_clang.cpp+13
...@@ -2510,6 +2510,19 @@ struct ZigClangSourceLocation ZigClangIntegerLiteral_getBeginLoc(const struct Zi...@@ -2510,6 +2510,19 @@ struct ZigClangSourceLocation ZigClangIntegerLiteral_getBeginLoc(const struct Zi
2510 return bitcast(casted->getBeginLoc());2510 return bitcast(casted->getBeginLoc());
2511}2511}
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
2513const struct ZigClangExpr *ZigClangReturnStmt_getRetValue(const struct ZigClangReturnStmt *self) {2526const struct ZigClangExpr *ZigClangReturnStmt_getRetValue(const struct ZigClangReturnStmt *self) {
2514 auto casted = reinterpret_cast<const clang::ReturnStmt *>(self);2527 auto casted = reinterpret_cast<const clang::ReturnStmt *>(self);
2515 return reinterpret_cast<const struct ZigClangExpr *>(casted->getRetValue());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,6 +1086,7 @@ ZIG_EXTERN_C struct ZigClangQualType ZigClangCStyleCastExpr_getType(const struct
10861086
1087ZIG_EXTERN_C bool ZigClangIntegerLiteral_EvaluateAsInt(const struct ZigClangIntegerLiteral *, struct ZigClangExprEvalResult *, const struct ZigClangASTContext *);1087ZIG_EXTERN_C bool ZigClangIntegerLiteral_EvaluateAsInt(const struct ZigClangIntegerLiteral *, struct ZigClangExprEvalResult *, const struct ZigClangASTContext *);
1088ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangIntegerLiteral_getBeginLoc(const struct ZigClangIntegerLiteral *);1088ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangIntegerLiteral_getBeginLoc(const struct ZigClangIntegerLiteral *);
1089ZIG_EXTERN_C bool ZigClangIntegerLiteral_isZero(const struct ZigClangIntegerLiteral *, bool *, const struct ZigClangASTContext *);
10891090
1090ZIG_EXTERN_C const struct ZigClangExpr *ZigClangReturnStmt_getRetValue(const struct ZigClangReturnStmt *);1091ZIG_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 {...@@ -99,10 +99,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
99 \\}99 \\}
100 , &[_][]const u8{100 , &[_][]const u8{
101 \\pub export fn foo() void {101 \\pub export fn foo() void {
102 \\ while (@as(c_int, 0) != 0) while (@as(c_int, 0) != 0) {};102 \\ while (false) while (false) {};
103 \\ while (true) while (@as(c_int, 0) != 0) {};103 \\ while (true) while (false) {};
104 \\ while (true) while (true) {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,8 +1634,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1634 , &[_][]const u8{1634 , &[_][]const u8{
1635 \\pub export fn foo() c_int {1635 \\pub export fn foo() c_int {
1636 \\ var a: c_int = 5;1636 \\ var a: c_int = 5;
1637 \\ while (@as(c_int, 2) != 0) a = 2;1637 \\ while (true) a = 2;
1638 \\ while (@as(c_int, 4) != 0) {1638 \\ while (true) {
1639 \\ var a_1: c_int = 4;1639 \\ var a_1: c_int = 4;
1640 \\ a_1 = 9;1640 \\ a_1 = 9;
1641 \\ _ = @as(c_int, 6);1641 \\ _ = @as(c_int, 6);
...@@ -1644,11 +1644,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1644,11 +1644,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1644 \\ while (true) {1644 \\ while (true) {
1645 \\ var a_1: c_int = 2;1645 \\ var a_1: c_int = 2;
1646 \\ a_1 = 12;1646 \\ a_1 = 12;
1647 \\ if (!(@as(c_int, 4) != 0)) break;1647 \\ if (!true) break;
1648 \\ }1648 \\ }
1649 \\ while (true) {1649 \\ while (true) {
1650 \\ a = 7;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,8 +1702,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1702 \\}1702 \\}
1703 , &[_][]const u8{1703 , &[_][]const u8{
1704 \\pub export fn bar() c_int {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);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 (@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);1706 \\ return if (true) @as(c_int, 5) else if (true) @as(c_int, 4) else @as(c_int, 6);
1707 \\}1707 \\}
1708 });1708 });
17091709
...@@ -2214,7 +2214,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2214,7 +2214,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2214 \\}2214 \\}
2215 , &[_][]const u8{2215 , &[_][]const u8{
2216 \\pub export fn foo() c_int {2216 \\pub export fn foo() c_int {
2217 \\ if (@as(c_int, 2) != 0) {2217 \\ if (true) {
2218 \\ var a: c_int = 2;2218 \\ var a: c_int = 2;
2219 \\ }2219 \\ }
2220 \\ if ((blk: {2220 \\ if ((blk: {
...@@ -2748,8 +2748,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2748,8 +2748,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2748 \\}2748 \\}
2749 , &[_][]const u8{2749 , &[_][]const u8{
2750 \\pub fn foo() callconv(.C) void {2750 \\pub fn foo() callconv(.C) void {
2751 \\ if (@as(c_int, 1) != 0) while (true) {2751 \\ if (true) while (true) {
2752 \\ if (!(@as(c_int, 0) != 0)) break;2752 \\ if (!false) break;
2753 \\ };2753 \\ };
2754 \\}2754 \\}
2755 });2755 });