Translate C: Put an alignCast in c style pointer casts to allow opaque types to cast properly in C macros

Translate C: add test case for aligning opaque types in pointer casts
master
Vexu 2020-03-07 17:36:17 +02:00 committed by Andrew Kelley
parent b3aef49eea
commit 7b5fb79b5b
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
2 changed files with 45 additions and 9 deletions

View File

@ -5519,12 +5519,12 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
return error.ParseError;
}
//if (@typeInfo(@TypeOf(x)) == .Pointer)
// @ptrCast(dest, x)
//else if (@typeInfo(@TypeOf(x)) == .Int and @typeInfo(dest) == .Pointer)
//( if (@typeInfo(@TypeOf(x)) == .Pointer)
// @ptrCast(dest, @alignCast(@alignOf(dest.Child), x))
//else if (@typeInfo(@TypeOf(x)) == .Integer and @typeInfo(dest) == .Pointer))
// @intToPtr(dest, x)
//else
// @as(dest, x)
// @as(dest, x) )
const lparen = try appendToken(c, .LParen, "(");
@ -5546,9 +5546,30 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
if_1.condition = &cmp_1.base;
_ = try appendToken(c, .RParen, ")");
const period_tok = try appendToken(c, .Period, ".");
const child_ident = try transCreateNodeIdentifier(c, "Child");
const inner_node_child = try c.a().create(ast.Node.InfixOp);
inner_node_child.* = .{
.op_token = period_tok,
.lhs = inner_node,
.op = .Period,
.rhs = child_ident,
};
const align_of = try transCreateNodeBuiltinFnCall(c, "@alignOf");
try align_of.params.push(&inner_node_child.base);
align_of.rparen_token = try appendToken(c, .RParen, ")");
// hack to get zig fmt to render a comma in builtin calls
_ = try appendToken(c, .Comma, ",");
const align_cast = try transCreateNodeBuiltinFnCall(c, "@alignCast");
try align_cast.params.push(&align_of.base);
try align_cast.params.push(node_to_cast);
align_cast.rparen_token = try appendToken(c, .RParen, ")");
const ptr_cast = try transCreateNodeBuiltinFnCall(c, "@ptrCast");
try ptr_cast.params.push(inner_node);
try ptr_cast.params.push(node_to_cast);
try ptr_cast.params.push(&align_cast.base);
ptr_cast.rparen_token = try appendToken(c, .RParen, ")");
if_1.body = &ptr_cast.base;

View File

@ -1458,7 +1458,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
cases.add("macro pointer cast",
\\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE)
, &[_][]const u8{
\\pub const NRF_GPIO = (if (@typeInfo(@TypeOf(NRF_GPIO_BASE)) == .Pointer) @ptrCast([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else if (@typeInfo(@TypeOf(NRF_GPIO_BASE)) == .Int and @typeInfo([*c]NRF_GPIO_Type) == .Pointer) @intToPtr([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else @as([*c]NRF_GPIO_Type, NRF_GPIO_BASE));
\\pub const NRF_GPIO = (if (@typeInfo(@TypeOf(NRF_GPIO_BASE)) == .Pointer) @ptrCast([*c]NRF_GPIO_Type, @alignCast(@alignOf([*c]NRF_GPIO_Type.Child), NRF_GPIO_BASE)) else if (@typeInfo(@TypeOf(NRF_GPIO_BASE)) == .Int and @typeInfo([*c]NRF_GPIO_Type) == .Pointer) @intToPtr([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else @as([*c]NRF_GPIO_Type, NRF_GPIO_BASE));
});
cases.add("basic macro function",
@ -2668,11 +2668,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\#define FOO(bar) baz((void *)(baz))
\\#define BAR (void*) a
, &[_][]const u8{
\\pub inline fn FOO(bar: var) @TypeOf(baz((if (@typeInfo(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeInfo(@TypeOf(baz)) == .Int and @typeInfo(*c_void) == .Pointer) @intToPtr(*c_void, baz) else @as(*c_void, baz)))) {
\\ return baz((if (@typeInfo(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeInfo(@TypeOf(baz)) == .Int and @typeInfo(*c_void) == .Pointer) @intToPtr(*c_void, baz) else @as(*c_void, baz)));
\\pub inline fn FOO(bar: var) @TypeOf(baz((if (@typeInfo(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, @alignCast(@alignOf(*c_void.Child), baz)) else if (@typeInfo(@TypeOf(baz)) == .Int and @typeInfo(*c_void) == .Pointer) @intToPtr(*c_void, baz) else @as(*c_void, baz)))) {
\\ return baz((if (@typeInfo(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, @alignCast(@alignOf(*c_void.Child), baz)) else if (@typeInfo(@TypeOf(baz)) == .Int and @typeInfo(*c_void) == .Pointer) @intToPtr(*c_void, baz) else @as(*c_void, baz)));
\\}
,
\\pub const BAR = (if (@typeInfo(@TypeOf(a)) == .Pointer) @ptrCast(*c_void, a) else if (@typeInfo(@TypeOf(a)) == .Int and @typeInfo(*c_void) == .Pointer) @intToPtr(*c_void, a) else @as(*c_void, a));
\\pub const BAR = (if (@typeInfo(@TypeOf(a)) == .Pointer) @ptrCast(*c_void, @alignCast(@alignOf(*c_void.Child), a)) else if (@typeInfo(@TypeOf(a)) == .Int and @typeInfo(*c_void) == .Pointer) @intToPtr(*c_void, a) else @as(*c_void, a));
});
cases.add("macro conditional operator",
@ -2879,4 +2879,19 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
, &[_][]const u8{
\\pub const FOO = 0x61626364;
});
cases.add("Make sure casts are grouped",
\\typedef struct
\\{
\\ int i;
\\}
\\*_XPrivDisplay;
\\typedef struct _XDisplay Display;
\\#define DefaultScreen(dpy) (((_XPrivDisplay)(dpy))->default_screen)
\\
, &[_][]const u8{
\\pub inline fn DefaultScreen(dpy: var) @TypeOf((if (@typeInfo(@TypeOf(dpy)) == .Pointer) @ptrCast(_XPrivDisplay, @alignCast(@alignOf(_XPrivDisplay.Child), dpy)) else if (@typeInfo(@TypeOf(dpy)) == .Int and @typeInfo(_XPrivDisplay) == .Pointer) @intToPtr(_XPrivDisplay, dpy) else @as(_XPrivDisplay, dpy)).*.default_screen) {
\\ return (if (@typeInfo(@TypeOf(dpy)) == .Pointer) @ptrCast(_XPrivDisplay, @alignCast(@alignOf(_XPrivDisplay.Child), dpy)) else if (@typeInfo(@TypeOf(dpy)) == .Int and @typeInfo(_XPrivDisplay) == .Pointer) @intToPtr(_XPrivDisplay, dpy) else @as(_XPrivDisplay, dpy)).*.default_screen;
\\}
});
}