Skip to content

Commit 0a6cd25

Browse files
ehaasVexu
authored andcommitted
translate-c: Use @constCast and @volatileCast to remove CV-qualifiers
Instead of converting a pointer to an int and then back to a pointer.
1 parent 614bc67 commit 0a6cd25

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/translate_c.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4063,12 +4063,12 @@ fn transCreateCompoundAssign(
40634063
return block_scope.complete(c);
40644064
}
40654065

4066-
// Casting away const or volatile requires us to use @ptrFromInt
40674066
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);
40694069
return Tag.as.create(c.arena, .{
40704070
.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),
40724072
});
40734073
}
40744074

src/translate_c/ast.zig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ pub const Node = extern union {
117117
import_c_builtin,
118118
/// @intCast(operand)
119119
int_cast,
120+
/// @constCast(operand)
121+
const_cast,
122+
/// @volatileCast(operand)
123+
volatile_cast,
120124
/// @import("std").zig.c_translation.promoteIntLiteral(value, type, base)
121125
helpers_promoteIntLiteral,
122126
/// @import("std").zig.c_translation.signedRemainder(lhs, rhs)
@@ -278,6 +282,8 @@ pub const Node = extern union {
278282
.ptr_from_int,
279283
.ptr_cast,
280284
.int_cast,
285+
.const_cast,
286+
.volatile_cast,
281287
=> Payload.UnOp,
282288

283289
.add,
@@ -1315,6 +1321,14 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
13151321
const payload = node.castTag(.int_cast).?.data;
13161322
return renderBuiltinCall(c, "@intCast", &.{payload});
13171323
},
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+
},
13181332
.signed_remainder => {
13191333
const payload = node.castTag(.signed_remainder).?.data;
13201334
const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "signedRemainder" });
@@ -2291,6 +2305,8 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
22912305
.div_trunc,
22922306
.signed_remainder,
22932307
.int_cast,
2308+
.const_cast,
2309+
.volatile_cast,
22942310
.as,
22952311
.truncate,
22962312
.bit_cast,

test/translate_c.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3473,11 +3473,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
34733473
\\}
34743474
\\pub export fn bar(arg_a: [*c]const c_int) void {
34753475
\\ var a = arg_a;
3476-
\\ foo(@as([*c]c_int, @ptrFromInt(@intFromPtr(a))));
3476+
\\ foo(@as([*c]c_int, @ptrCast(@volatileCast(@constCast(a)))));
34773477
\\}
34783478
\\pub export fn baz(arg_a: [*c]volatile c_int) void {
34793479
\\ var a = arg_a;
3480-
\\ foo(@as([*c]c_int, @ptrFromInt(@intFromPtr(a))));
3480+
\\ foo(@as([*c]c_int, @ptrCast(@volatileCast(@constCast(a)))));
34813481
\\}
34823482
});
34833483

0 commit comments

Comments
 (0)