translate-c: Explicitly cast decayed array to pointer with @ptrCast

This enables translation of code that uses pointer arithmetic with arrays
This commit is contained in:
Evan Haas
2021-03-05 12:09:57 -08:00
committed by Veikka Tuominen
parent 5d215cc76b
commit d01bb21173
2 changed files with 8 additions and 3 deletions

View File

@@ -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);

View File

@@ -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;
\\}
, "");