From 895672b3f96aab1f5bad3446f5186a047f29412c Mon Sep 17 00:00:00 2001 From: Lachlan Easton Date: Tue, 3 Mar 2020 21:42:38 +1100 Subject: [PATCH] Translate C: Group generated casts 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 Translate C: Fix @typeId -> @typeInfo Add test case to run_translated_c for casting from pointer to opaque type --- src-self-hosted/translate_c.zig | 41 +++++++++++++++++++++++++++++---- test/run_translated_c.zig | 18 +++++++++++++++ test/translate_c.zig | 24 +++++++++++++++---- 3 files changed, 74 insertions(+), 9 deletions(-) diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index d24001cf9..b13a0d4bf 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -5424,12 +5424,15 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, return error.ParseError; } - //if (@typeInfo(@TypeOf(x)) == .Pointer) - // @ptrCast(dest, x) + // TODO: It might be nice if we only did the alignCasting for opaque types + //( if (@typeInfo(@TypeOf(x)) == .Pointer) + // @ptrCast(dest, @alignCast(@alignOf(dest.Child), x)) //else if (@typeInfo(@TypeOf(x)) == .Integer) // @intToPtr(dest, x) //else - // @as(dest, x) + // @as(dest, x) ) + + const group_lparen = try appendToken(c, .LParen, "("); const if_1 = try transCreateNodeIf(c); const type_id_1 = try transCreateNodeBuiltinFnCall(c, "@typeInfo"); @@ -5449,9 +5452,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; @@ -5492,7 +5516,14 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, as.rparen_token = try appendToken(c, .RParen, ")"); else_2.body = &as.base; - return &if_1.base; + const group_rparen = try appendToken(c, .RParen, ")"); + const grouped_expr = try c.a().create(ast.Node.GroupedExpression); + grouped_expr.* = .{ + .lparen = group_lparen, + .expr = &if_1.base, + .rparen = group_rparen, + }; + return &grouped_expr.base; }, else => { const first_tok = it.list.at(0); diff --git a/test/run_translated_c.zig b/test/run_translated_c.zig index 629a85083..313a83705 100644 --- a/test/run_translated_c.zig +++ b/test/run_translated_c.zig @@ -195,4 +195,22 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void { \\ return 0; \\} , ""); + + cases.add("cast from pointer to opaque type to struct", + \\#include + \\typedef struct + \\{ + \\ int i; + \\} + \\StructType,*StructPtrType; + \\ + \\typedef struct OpaqueStruct OpaqueStructTypedef; + \\#define Macro(opaquePtr) (((StructPtrType)(opaquePtr))->i) + \\int main(int argc, char **argv) { + \\ StructType localStruct = {88}; + \\ OpaqueStructTypedef *opaquePtrToLocal = &localStruct; + \\ printf("%d!\n", Macro(opaquePtrToLocal)); + \\ return 0; + \\} + , "88!\n"); } diff --git a/test/translate_c.zig b/test/translate_c.zig index 3d0843d27..01614ae9c 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -1404,7 +1404,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) @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) @intToPtr([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else @as([*c]NRF_GPIO_Type, NRF_GPIO_BASE)); }); cases.add("basic macro function", @@ -2588,11 +2588,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) @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) @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) @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) @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) @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) @intToPtr(*c_void, a) else @as(*c_void, a)); }); cases.add("macro conditional operator", @@ -2754,4 +2754,20 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ return if (x > y) x else y; \\} }); + + 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) @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) @intToPtr(_XPrivDisplay, dpy) else @as(_XPrivDisplay, dpy)).*.default_screen; + \\} + }); + }