authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-05 15:01:21-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-01-05 15:01:21-05:00
logd7bff05098445ec56082a6455b8a00d87a982e23
tree5cd56e04fb41b049de89b2b0b18146bd88cd715b
parenta0ca34979ea5ff9be0d353f539e7c86dedbf8693
parentac867cc45f8e99297647bd29ddfa746462500b72
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #4064 from ziglang/fix-4054

use @intCast instead of @as for shift rhs

2 files changed, 26 insertions(+), 26 deletions(-)

src-self-hosted/translate_c.zig+15-15
...@@ -2781,13 +2781,13 @@ fn transCreateCompoundAssign(...@@ -2781,13 +2781,13 @@ fn transCreateCompoundAssign(
2781 try transExpr(rp, scope, rhs, .used, .r_value);2781 try transExpr(rp, scope, rhs, .used, .r_value);
27822782
2783 if (is_shift) {2783 if (is_shift) {
2784 const as_node = try transCreateNodeBuiltinFnCall(rp.c, "@as");2784 const cast_node = try transCreateNodeBuiltinFnCall(rp.c, "@intCast");
2785 const rhs_type = try qualTypeToLog2IntRef(rp, getExprQualType(rp.c, rhs), loc);2785 const rhs_type = try qualTypeToLog2IntRef(rp, getExprQualType(rp.c, rhs), loc);
2786 try as_node.params.push(rhs_type);2786 try cast_node.params.push(rhs_type);
2787 _ = try appendToken(rp.c, .Comma, ",");2787 _ = try appendToken(rp.c, .Comma, ",");
2788 try as_node.params.push(rhs_node);2788 try cast_node.params.push(rhs_node);
2789 as_node.rparen_token = try appendToken(rp.c, .RParen, ")");2789 cast_node.rparen_token = try appendToken(rp.c, .RParen, ")");
2790 rhs_node = &as_node.base;2790 rhs_node = &cast_node.base;
2791 }2791 }
2792 if (scope.id != .Condition)2792 if (scope.id != .Condition)
2793 _ = try appendToken(rp.c, .Semicolon, ";");2793 _ = try appendToken(rp.c, .Semicolon, ";");
...@@ -2818,13 +2818,13 @@ fn transCreateCompoundAssign(...@@ -2818,13 +2818,13 @@ fn transCreateCompoundAssign(
2818 const bin_token = try appendToken(rp.c, bin_tok_id, bin_bytes);2818 const bin_token = try appendToken(rp.c, bin_tok_id, bin_bytes);
2819 var rhs_node = try transExpr(rp, scope, rhs, .used, .r_value);2819 var rhs_node = try transExpr(rp, scope, rhs, .used, .r_value);
2820 if (is_shift) {2820 if (is_shift) {
2821 const as_node = try transCreateNodeBuiltinFnCall(rp.c, "@as");2821 const cast_node = try transCreateNodeBuiltinFnCall(rp.c, "@intCast");
2822 const rhs_type = try qualTypeToLog2IntRef(rp, getExprQualType(rp.c, rhs), loc);2822 const rhs_type = try qualTypeToLog2IntRef(rp, getExprQualType(rp.c, rhs), loc);
2823 try as_node.params.push(rhs_type);2823 try cast_node.params.push(rhs_type);
2824 _ = try appendToken(rp.c, .Comma, ",");2824 _ = try appendToken(rp.c, .Comma, ",");
2825 try as_node.params.push(rhs_node);2825 try cast_node.params.push(rhs_node);
2826 as_node.rparen_token = try appendToken(rp.c, .RParen, ")");2826 cast_node.rparen_token = try appendToken(rp.c, .RParen, ")");
2827 rhs_node = &as_node.base;2827 rhs_node = &cast_node.base;
2828 }2828 }
2829 const rhs_bin = try transCreateNodeInfixOp(rp, scope, ref_node, bin_op, bin_token, rhs_node, .used, false);2829 const rhs_bin = try transCreateNodeInfixOp(rp, scope, ref_node, bin_op, bin_token, rhs_node, .used, false);
28302830
...@@ -3886,20 +3886,20 @@ fn transCreateNodeShiftOp(...@@ -3886,20 +3886,20 @@ fn transCreateNodeShiftOp(
3886 const lhs = try transExpr(rp, scope, lhs_expr, .used, .l_value);3886 const lhs = try transExpr(rp, scope, lhs_expr, .used, .l_value);
3887 const op_token = try appendToken(rp.c, op_tok_id, bytes);3887 const op_token = try appendToken(rp.c, op_tok_id, bytes);
38883888
3889 const as_node = try transCreateNodeBuiltinFnCall(rp.c, "@as");3889 const cast_node = try transCreateNodeBuiltinFnCall(rp.c, "@intCast");
3890 const rhs_type = try qualTypeToLog2IntRef(rp, ZigClangBinaryOperator_getType(stmt), rhs_location);3890 const rhs_type = try qualTypeToLog2IntRef(rp, ZigClangBinaryOperator_getType(stmt), rhs_location);
3891 try as_node.params.push(rhs_type);3891 try cast_node.params.push(rhs_type);
3892 _ = try appendToken(rp.c, .Comma, ",");3892 _ = try appendToken(rp.c, .Comma, ",");
3893 const rhs = try transExprCoercing(rp, scope, rhs_expr, .used, .r_value);3893 const rhs = try transExprCoercing(rp, scope, rhs_expr, .used, .r_value);
3894 try as_node.params.push(rhs);3894 try cast_node.params.push(rhs);
3895 as_node.rparen_token = try appendToken(rp.c, .RParen, ")");3895 cast_node.rparen_token = try appendToken(rp.c, .RParen, ")");
38963896
3897 const node = try rp.c.a().create(ast.Node.InfixOp);3897 const node = try rp.c.a().create(ast.Node.InfixOp);
3898 node.* = ast.Node.InfixOp{3898 node.* = ast.Node.InfixOp{
3899 .op_token = op_token,3899 .op_token = op_token,
3900 .lhs = lhs,3900 .lhs = lhs,
3901 .op = op,3901 .op = op,
3902 .rhs = &as_node.base,3902 .rhs = &cast_node.base,
3903 };3903 };
39043904
3905 return &node.base;3905 return &node.base;
test/translate_c.zig+11-11
...@@ -835,7 +835,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -835,7 +835,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
835 \\}835 \\}
836 , &[_][]const u8{836 , &[_][]const u8{
837 \\pub export fn foo() c_int {837 \\pub export fn foo() c_int {
838 \\ return (@as(c_int, 1) << @as(@import("std").math.Log2Int(c_int), 2)) >> @as(@import("std").math.Log2Int(c_int), 1);838 \\ return (@as(c_int, 1) << @intCast(@import("std").math.Log2Int(c_int), 2)) >> @intCast(@import("std").math.Log2Int(c_int), 1);
839 \\}839 \\}
840 });840 });
841841
...@@ -2001,7 +2001,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2001,7 +2001,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2001 \\ var a = arg_a;2001 \\ var a = arg_a;
2002 \\ var i: c_int = 0;2002 \\ var i: c_int = 0;
2003 \\ while (a > @bitCast(c_uint, @as(c_int, 0))) {2003 \\ while (a > @bitCast(c_uint, @as(c_int, 0))) {
2004 \\ a >>= @as(@import("std").math.Log2Int(c_int), 1);2004 \\ a >>= @intCast(@import("std").math.Log2Int(c_int), 1);
2005 \\ }2005 \\ }
2006 \\ return i;2006 \\ return i;
2007 \\}2007 \\}
...@@ -2021,7 +2021,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2021,7 +2021,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2021 \\ var a = arg_a;2021 \\ var a = arg_a;
2022 \\ var i: c_int = 0;2022 \\ var i: c_int = 0;
2023 \\ while (a > @bitCast(c_uint, @as(c_int, 0))) {2023 \\ while (a > @bitCast(c_uint, @as(c_int, 0))) {
2024 \\ a >>= @as(@import("std").math.Log2Int(c_int), 1);2024 \\ a >>= @intCast(@import("std").math.Log2Int(c_int), 1);
2025 \\ }2025 \\ }
2026 \\ return i;2026 \\ return i;
2027 \\}2027 \\}
...@@ -2072,14 +2072,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2072,14 +2072,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2072 \\ ref.* = ref.* ^ @as(c_int, 1);2072 \\ ref.* = ref.* ^ @as(c_int, 1);
2073 \\ break :blk ref.*;2073 \\ break :blk ref.*;
2074 \\ });2074 \\ });
2075 \\ a >>= @as(@import("std").math.Log2Int(c_int), (blk: {2075 \\ a >>= @intCast(@import("std").math.Log2Int(c_int), (blk: {
2076 \\ const ref = &a;2076 \\ const ref = &a;
2077 \\ ref.* = ref.* >> @as(@import("std").math.Log2Int(c_int), @as(c_int, 1));2077 \\ ref.* = ref.* >> @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));
2078 \\ break :blk ref.*;2078 \\ break :blk ref.*;
2079 \\ }));2079 \\ }));
2080 \\ a <<= @as(@import("std").math.Log2Int(c_int), (blk: {2080 \\ a <<= @intCast(@import("std").math.Log2Int(c_int), (blk: {
2081 \\ const ref = &a;2081 \\ const ref = &a;
2082 \\ ref.* = ref.* << @as(@import("std").math.Log2Int(c_int), @as(c_int, 1));2082 \\ ref.* = ref.* << @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));
2083 \\ break :blk ref.*;2083 \\ break :blk ref.*;
2084 \\ }));2084 \\ }));
2085 \\}2085 \\}
...@@ -2130,14 +2130,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2130,14 +2130,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2130 \\ ref.* = ref.* ^ @bitCast(c_uint, @as(c_int, 1));2130 \\ ref.* = ref.* ^ @bitCast(c_uint, @as(c_int, 1));
2131 \\ break :blk ref.*;2131 \\ break :blk ref.*;
2132 \\ });2132 \\ });
2133 \\ a >>= @as(@import("std").math.Log2Int(c_uint), (blk: {2133 \\ a >>= @intCast(@import("std").math.Log2Int(c_uint), (blk: {
2134 \\ const ref = &a;2134 \\ const ref = &a;
2135 \\ ref.* = ref.* >> @as(@import("std").math.Log2Int(c_int), @as(c_int, 1));2135 \\ ref.* = ref.* >> @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));
2136 \\ break :blk ref.*;2136 \\ break :blk ref.*;
2137 \\ }));2137 \\ }));
2138 \\ a <<= @as(@import("std").math.Log2Int(c_uint), (blk: {2138 \\ a <<= @intCast(@import("std").math.Log2Int(c_uint), (blk: {
2139 \\ const ref = &a;2139 \\ const ref = &a;
2140 \\ ref.* = ref.* << @as(@import("std").math.Log2Int(c_int), @as(c_int, 1));2140 \\ ref.* = ref.* << @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));
2141 \\ break :blk ref.*;2141 \\ break :blk ref.*;
2142 \\ }));2142 \\ }));
2143 \\}2143 \\}