commit d01bb211732f1d0aebf558c43825a52254d034ab (tree)
parent 5d215cc76bcac12afbda7b10ec04bbf6c5b47910
Author: Evan Haas <evan@lagerdata.com>
Date: Fri, 5 Mar 2021 12:09:57 -0800
translate-c: Explicitly cast decayed array to pointer with @ptrCast
This enables translation of code that uses pointer arithmetic with arrays
Diffstat:
2 files changed, 8 insertions(+), 3 deletions(-)
diff --git a/src/translate_c.zig b/src/translate_c.zig
@@ -1493,7 +1493,8 @@ fn transImplicitCastExpr(
}
const addr = try Tag.address_of.create(c.arena, try transExpr(c, scope, sub_expr, .used));
- return maybeSuppressResult(c, scope, result_used, addr);
+ const casted = try transCPtrCast(c, scope, expr.getBeginLoc(), dest_type, src_type, addr);
+ return maybeSuppressResult(c, scope, result_used, casted);
},
.NullToPointer => {
return Tag.null_literal.init();
@@ -3156,10 +3157,10 @@ fn transCPtrCast(
const src_child_type = src_ty.getPointeeType();
const dst_type_node = try transType(c, scope, ty, loc);
- if ((src_child_type.isConstQualified() and
+ if (!src_ty.isArrayType() and ((src_child_type.isConstQualified() and
!child_type.isConstQualified()) or
(src_child_type.isVolatileQualified() and
- !child_type.isVolatileQualified()))
+ !child_type.isVolatileQualified())))
{
// Casting away const or volatile requires us to use @intToPtr
const ptr_to_int = try Tag.ptr_to_int.create(c.arena, expr);
diff --git a/test/run_translated_c.zig b/test/run_translated_c.zig
@@ -1164,6 +1164,10 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
\\ y = (x -= idx);
\\ if (y != x || y != &array[5]) abort();
\\
+ \\ if (array + idx != &array[1] || array + 1 != &array[1]) abort();
+ \\ idx = -1;
+ \\ if (array - idx != &array[1]) abort();
+ \\
\\ return 0;
\\}
, "");