| author | |
| committer | |
| log | 0a6cd257b9c8a9093b966e3851dc8261e19b531a |
| tree | 84276cf942339bac3d0a63c2d81ad26f04e7dc64 |
| parent | 614bc6755e99645e529fb0b648ffa34b1e3fc7ff |
Instead of converting a pointer to an int and then back to a pointer.3 files changed, 21 insertions(+), 5 deletions(-)
src/translate_c.zig+3-3| ... | ... | @@ -4063,12 +4063,12 @@ fn transCreateCompoundAssign( |
| 4063 | 4063 | return block_scope.complete(c); |
| 4064 | 4064 | } |
| 4065 | 4065 | |
| 4066 | // Casting away const or volatile requires us to use @ptrFromInt | |
| 4067 | 4066 | fn removeCVQualifiers(c: *Context, dst_type_node: Node, expr: Node) Error!Node { |
| 4068 | const int_from_ptr = try Tag.int_from_ptr.create(c.arena, expr); | |
| 4067 | const const_casted = try Tag.const_cast.create(c.arena, expr); | |
| 4068 | const volatile_casted = try Tag.volatile_cast.create(c.arena, const_casted); | |
| 4069 | 4069 | return Tag.as.create(c.arena, .{ |
| 4070 | 4070 | .lhs = dst_type_node, |
| 4071 | .rhs = try Tag.ptr_from_int.create(c.arena, int_from_ptr), | |
| 4071 | .rhs = try Tag.ptr_cast.create(c.arena, volatile_casted), | |
| 4072 | 4072 | }); |
| 4073 | 4073 | } |
| 4074 | 4074 |
src/translate_c/ast.zig+16| ... | ... | @@ -117,6 +117,10 @@ pub const Node = extern union { |
| 117 | 117 | import_c_builtin, |
| 118 | 118 | /// @intCast(operand) |
| 119 | 119 | int_cast, |
| 120 | /// @constCast(operand) | |
| 121 | const_cast, | |
| 122 | /// @volatileCast(operand) | |
| 123 | volatile_cast, | |
| 120 | 124 | /// @import("std").zig.c_translation.promoteIntLiteral(value, type, base) |
| 121 | 125 | helpers_promoteIntLiteral, |
| 122 | 126 | /// @import("std").zig.c_translation.signedRemainder(lhs, rhs) |
| ... | ... | @@ -278,6 +282,8 @@ pub const Node = extern union { |
| 278 | 282 | .ptr_from_int, |
| 279 | 283 | .ptr_cast, |
| 280 | 284 | .int_cast, |
| 285 | .const_cast, | |
| 286 | .volatile_cast, | |
| 281 | 287 | => Payload.UnOp, |
| 282 | 288 | |
| 283 | 289 | .add, |
| ... | ... | @@ -1315,6 +1321,14 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex { |
| 1315 | 1321 | const payload = node.castTag(.int_cast).?.data; |
| 1316 | 1322 | return renderBuiltinCall(c, "@intCast", &.{payload}); |
| 1317 | 1323 | }, |
| 1324 | .const_cast => { | |
| 1325 | const payload = node.castTag(.const_cast).?.data; | |
| 1326 | return renderBuiltinCall(c, "@constCast", &.{payload}); | |
| 1327 | }, | |
| 1328 | .volatile_cast => { | |
| 1329 | const payload = node.castTag(.volatile_cast).?.data; | |
| 1330 | return renderBuiltinCall(c, "@volatileCast", &.{payload}); | |
| 1331 | }, | |
| 1318 | 1332 | .signed_remainder => { |
| 1319 | 1333 | const payload = node.castTag(.signed_remainder).?.data; |
| 1320 | 1334 | const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "signedRemainder" }); |
| ... | ... | @@ -2291,6 +2305,8 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex { |
| 2291 | 2305 | .div_trunc, |
| 2292 | 2306 | .signed_remainder, |
| 2293 | 2307 | .int_cast, |
| 2308 | .const_cast, | |
| 2309 | .volatile_cast, | |
| 2294 | 2310 | .as, |
| 2295 | 2311 | .truncate, |
| 2296 | 2312 | .bit_cast, |
test/translate_c.zig+2-2| ... | ... | @@ -3473,11 +3473,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3473 | 3473 | \\} |
| 3474 | 3474 | \\pub export fn bar(arg_a: [*c]const c_int) void { |
| 3475 | 3475 | \\ var a = arg_a; |
| 3476 | \\ foo(@as([*c]c_int, @ptrFromInt(@intFromPtr(a)))); | |
| 3476 | \\ foo(@as([*c]c_int, @ptrCast(@volatileCast(@constCast(a))))); | |
| 3477 | 3477 | \\} |
| 3478 | 3478 | \\pub export fn baz(arg_a: [*c]volatile c_int) void { |
| 3479 | 3479 | \\ var a = arg_a; |
| 3480 | \\ foo(@as([*c]c_int, @ptrFromInt(@intFromPtr(a)))); | |
| 3480 | \\ foo(@as([*c]c_int, @ptrCast(@volatileCast(@constCast(a))))); | |
| 3481 | 3481 | \\} |
| 3482 | 3482 | }); |
| 3483 | 3483 |