| author | |
| committer | |
| log | e65b9e8f7bcc38943f48ed44560a22c62ca222ce |
| tree | 692c76e7a24331e71b2dec5f033993d97a4cd466 |
| parent | 62bfff5e8774413856d0c548f0fd73a4b5134f7f |
| signature | Commit is signed but in an unrecognized format. |
3 files changed, 71 insertions(+), 20 deletions(-)
src-self-hosted/clang.zig+3| ... | ... | @@ -79,6 +79,7 @@ pub const ZigClangPreprocessingRecord = @OpaqueType(); |
| 79 | 79 | pub const ZigClangFloatingLiteral = @OpaqueType(); |
| 80 | 80 | pub const ZigClangConstantExpr = @OpaqueType(); |
| 81 | 81 | pub const ZigClangCharacterLiteral = @OpaqueType(); |
| 82 | pub const ZigClangStmtExpr = @OpaqueType(); | |
| 82 | 83 | |
| 83 | 84 | pub const ZigClangBO = extern enum { |
| 84 | 85 | PtrMemD, |
| ... | ... | @@ -1095,3 +1096,5 @@ pub extern fn ZigClangCharacterLiteral_getBeginLoc(*const ZigClangCharacterLiter |
| 1095 | 1096 | pub extern fn ZigClangCharacterLiteral_getKind(*const ZigClangCharacterLiteral) ZigClangCharacterLiteral_CharacterKind; |
| 1096 | 1097 | pub extern fn ZigClangCharacterLiteral_getValue(*const ZigClangCharacterLiteral) c_uint; |
| 1097 | 1098 | |
| 1099 | pub extern fn ZigClangStmtExpr_getSubStmt( *const ZigClangStmtExpr) *const ZigClangCompoundStmt; | |
| 1100 |
src-self-hosted/translate_c.zig+34-4| ... | ... | @@ -855,6 +855,7 @@ fn transStmt( |
| 855 | 855 | .ConstantExprClass => return transConstantExpr(rp, scope, @ptrCast(*const ZigClangExpr, stmt), result_used), |
| 856 | 856 | .PredefinedExprClass => return transPredefinedExpr(rp, scope, @ptrCast(*const ZigClangPredefinedExpr, stmt), result_used), |
| 857 | 857 | .CharacterLiteralClass => return transCharLiteral(rp, scope, @ptrCast(*const ZigClangCharacterLiteral, stmt), result_used), |
| 858 | .StmtExprClass => return transStmtExpr(rp, scope, @ptrCast(*const ZigClangStmtExpr, stmt), result_used), | |
| 858 | 859 | else => { |
| 859 | 860 | return revertAndWarn( |
| 860 | 861 | rp, |
| ... | ... | @@ -1165,12 +1166,9 @@ fn transImplicitCastExpr( |
| 1165 | 1166 | const src_type = getExprQualType(c, sub_expr); |
| 1166 | 1167 | return transCCast(rp, scope, ZigClangImplicitCastExpr_getBeginLoc(expr), dest_type, src_type, sub_expr_node); |
| 1167 | 1168 | }, |
| 1168 | .FunctionToPointerDecay, .ArrayToPointerDecay => { | |
| 1169 | .LValueToRValue, .NoOp, .FunctionToPointerDecay, .ArrayToPointerDecay => { | |
| 1169 | 1170 | return maybeSuppressResult(rp, scope, result_used, sub_expr_node); |
| 1170 | 1171 | }, |
| 1171 | .LValueToRValue, .NoOp => { | |
| 1172 | return transExpr(rp, scope, sub_expr, .used, .r_value); | |
| 1173 | }, | |
| 1174 | 1172 | .NullToPointer => { |
| 1175 | 1173 | return try transCreateNodeNullLiteral(rp.c); |
| 1176 | 1174 | }, |
| ... | ... | @@ -1960,6 +1958,38 @@ fn transCharLiteral( |
| 1960 | 1958 | } |
| 1961 | 1959 | } |
| 1962 | 1960 | |
| 1961 | fn transStmtExpr(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangStmtExpr, used: ResultUsed) TransError!*ast.Node { | |
| 1962 | const comp = ZigClangStmtExpr_getSubStmt(stmt); | |
| 1963 | if (used == .unused) { | |
| 1964 | return transCompoundStmt(rp, scope, comp); | |
| 1965 | } | |
| 1966 | const lparen = try appendToken(rp.c, .LParen, "("); | |
| 1967 | const block_scope = try Scope.Block.init(rp.c, scope, "blk"); | |
| 1968 | const block = try transCreateNodeBlock(rp.c, "blk"); | |
| 1969 | block_scope.block_node = block; | |
| 1970 | ||
| 1971 | var it = ZigClangCompoundStmt_body_begin(comp); | |
| 1972 | const end_it = ZigClangCompoundStmt_body_end(comp); | |
| 1973 | while (it != end_it - 1) : (it += 1) { | |
| 1974 | const result = try transStmt(rp, &block_scope.base, it[0], .unused, .r_value); | |
| 1975 | if (result != &block.base) | |
| 1976 | try block.statements.push(result); | |
| 1977 | } | |
| 1978 | const break_node = try transCreateNodeBreak(rp.c, "blk"); | |
| 1979 | break_node.rhs = try transStmt(rp, &block_scope.base, it[0], .used, .r_value); | |
| 1980 | _ = try appendToken(rp.c, .Semicolon, ";"); | |
| 1981 | try block.statements.push(&break_node.base); | |
| 1982 | block.rbrace = try appendToken(rp.c, .RBrace, "}"); | |
| 1983 | const rparen = try appendToken(rp.c, .RParen, ")"); | |
| 1984 | const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression); | |
| 1985 | grouped_expr.* = .{ | |
| 1986 | .lparen = lparen, | |
| 1987 | .expr = &block.base, | |
| 1988 | .rparen = rparen, | |
| 1989 | }; | |
| 1990 | return maybeSuppressResult(rp, scope, used, &grouped_expr.base); | |
| 1991 | } | |
| 1992 | ||
| 1963 | 1993 | fn transCPtrCast( |
| 1964 | 1994 | rp: RestorePoint, |
| 1965 | 1995 | loc: ZigClangSourceLocation, |
test/translate_c.zig+34-16| ... | ... | @@ -1476,6 +1476,24 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1476 | 1476 | \\} |
| 1477 | 1477 | }); |
| 1478 | 1478 | |
| 1479 | cases.add_2("statement expression", | |
| 1480 | \\int foo(void) { | |
| 1481 | \\ return ({ | |
| 1482 | \\ int a = 1; | |
| 1483 | \\ a; | |
| 1484 | \\ a; | |
| 1485 | \\ }); | |
| 1486 | \\} | |
| 1487 | , &[_][]const u8{ | |
| 1488 | \\pub export fn foo() c_int { | |
| 1489 | \\ return (blk: { | |
| 1490 | \\ var a: c_int = 1; | |
| 1491 | \\ _ = a; | |
| 1492 | \\ break :blk a; | |
| 1493 | \\ }); | |
| 1494 | \\} | |
| 1495 | }); | |
| 1496 | ||
| 1479 | 1497 | /////////////// Cases for only stage1 which are TODO items for stage2 //////////////// |
| 1480 | 1498 | |
| 1481 | 1499 | cases.addAllowWarnings("simple data types", |
| ... | ... | @@ -1723,22 +1741,6 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1723 | 1741 | \\} |
| 1724 | 1742 | }); |
| 1725 | 1743 | |
| 1726 | cases.addC("statement expression", | |
| 1727 | \\int foo(void) { | |
| 1728 | \\ return ({ | |
| 1729 | \\ int a = 1; | |
| 1730 | \\ a; | |
| 1731 | \\ }); | |
| 1732 | \\} | |
| 1733 | , &[_][]const u8{ | |
| 1734 | \\pub export fn foo() c_int { | |
| 1735 | \\ return x: { | |
| 1736 | \\ var a: c_int = 1; | |
| 1737 | \\ break :x a; | |
| 1738 | \\ }; | |
| 1739 | \\} | |
| 1740 | }); | |
| 1741 | ||
| 1742 | 1744 | cases.addC("__extension__ cast", |
| 1743 | 1745 | \\int foo(void) { |
| 1744 | 1746 | \\ return __extension__ 1; |
| ... | ... | @@ -2609,4 +2611,20 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2609 | 2611 | \\ return (a & b) ^ (a | b); |
| 2610 | 2612 | \\} |
| 2611 | 2613 | }); |
| 2614 | ||
| 2615 | cases.addC("statement expression", | |
| 2616 | \\int foo(void) { | |
| 2617 | \\ return ({ | |
| 2618 | \\ int a = 1; | |
| 2619 | \\ a; | |
| 2620 | \\ }); | |
| 2621 | \\} | |
| 2622 | , &[_][]const u8{ | |
| 2623 | \\pub export fn foo() c_int { | |
| 2624 | \\ return x: { | |
| 2625 | \\ var a: c_int = 1; | |
| 2626 | \\ break :x a; | |
| 2627 | \\ }; | |
| 2628 | \\} | |
| 2629 | }); | |
| 2612 | 2630 | } |