| author | |
| committer | |
| log | 895672b3f96aab1f5bad3446f5186a047f29412c |
| tree | e6ba1f679f7007624233262454365f6ab99243d5 |
| parent | 80ff549e2602ecfc4eaf9ade0108e1779dff1874 |
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 type3 files changed, 74 insertions(+), 9 deletions(-)
src-self-hosted/translate_c.zig+36-5| ... | ... | @@ -5424,12 +5424,15 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5424 | 5424 | return error.ParseError; |
| 5425 | 5425 | } |
| 5426 | 5426 | |
| 5427 | //if (@typeInfo(@TypeOf(x)) == .Pointer) | |
| 5428 | // @ptrCast(dest, x) | |
| 5427 | // TODO: It might be nice if we only did the alignCasting for opaque types | |
| 5428 | //( if (@typeInfo(@TypeOf(x)) == .Pointer) | |
| 5429 | // @ptrCast(dest, @alignCast(@alignOf(dest.Child), x)) | |
| 5429 | 5430 | //else if (@typeInfo(@TypeOf(x)) == .Integer) |
| 5430 | 5431 | // @intToPtr(dest, x) |
| 5431 | 5432 | //else |
| 5432 | // @as(dest, x) | |
| 5433 | // @as(dest, x) ) | |
| 5434 | ||
| 5435 | const group_lparen = try appendToken(c, .LParen, "("); | |
| 5433 | 5436 | |
| 5434 | 5437 | const if_1 = try transCreateNodeIf(c); |
| 5435 | 5438 | const type_id_1 = try transCreateNodeBuiltinFnCall(c, "@typeInfo"); |
| ... | ... | @@ -5449,9 +5452,30 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5449 | 5452 | if_1.condition = &cmp_1.base; |
| 5450 | 5453 | _ = try appendToken(c, .RParen, ")"); |
| 5451 | 5454 | |
| 5455 | const period_tok = try appendToken(c, .Period, "."); | |
| 5456 | const child_ident = try transCreateNodeIdentifier(c, "Child"); | |
| 5457 | const inner_node_child = try c.a().create(ast.Node.InfixOp); | |
| 5458 | inner_node_child.* = .{ | |
| 5459 | .op_token = period_tok, | |
| 5460 | .lhs = inner_node, | |
| 5461 | .op = .Period, | |
| 5462 | .rhs = child_ident, | |
| 5463 | }; | |
| 5464 | ||
| 5465 | const align_of = try transCreateNodeBuiltinFnCall(c, "@alignOf"); | |
| 5466 | try align_of.params.push(&inner_node_child.base); | |
| 5467 | align_of.rparen_token = try appendToken(c, .RParen, ")"); | |
| 5468 | // hack to get zig fmt to render a comma in builtin calls | |
| 5469 | _ = try appendToken(c, .Comma, ","); | |
| 5470 | ||
| 5471 | const align_cast = try transCreateNodeBuiltinFnCall(c, "@alignCast"); | |
| 5472 | try align_cast.params.push(&align_of.base); | |
| 5473 | try align_cast.params.push(node_to_cast); | |
| 5474 | align_cast.rparen_token = try appendToken(c, .RParen, ")"); | |
| 5475 | ||
| 5452 | 5476 | const ptr_cast = try transCreateNodeBuiltinFnCall(c, "@ptrCast"); |
| 5453 | 5477 | try ptr_cast.params.push(inner_node); |
| 5454 | try ptr_cast.params.push(node_to_cast); | |
| 5478 | try ptr_cast.params.push(&align_cast.base); | |
| 5455 | 5479 | ptr_cast.rparen_token = try appendToken(c, .RParen, ")"); |
| 5456 | 5480 | if_1.body = &ptr_cast.base; |
| 5457 | 5481 | |
| ... | ... | @@ -5492,7 +5516,14 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5492 | 5516 | as.rparen_token = try appendToken(c, .RParen, ")"); |
| 5493 | 5517 | else_2.body = &as.base; |
| 5494 | 5518 | |
| 5495 | return &if_1.base; | |
| 5519 | const group_rparen = try appendToken(c, .RParen, ")"); | |
| 5520 | const grouped_expr = try c.a().create(ast.Node.GroupedExpression); | |
| 5521 | grouped_expr.* = .{ | |
| 5522 | .lparen = group_lparen, | |
| 5523 | .expr = &if_1.base, | |
| 5524 | .rparen = group_rparen, | |
| 5525 | }; | |
| 5526 | return &grouped_expr.base; | |
| 5496 | 5527 | }, |
| 5497 | 5528 | else => { |
| 5498 | 5529 | const first_tok = it.list.at(0); |
test/run_translated_c.zig+18| ... | ... | @@ -195,4 +195,22 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void { |
| 195 | 195 | \\ return 0; |
| 196 | 196 | \\} |
| 197 | 197 | , ""); |
| 198 | ||
| 199 | cases.add("cast from pointer to opaque type to struct", | |
| 200 | \\#include <stdio.h> | |
| 201 | \\typedef struct | |
| 202 | \\{ | |
| 203 | \\ int i; | |
| 204 | \\} | |
| 205 | \\StructType,*StructPtrType; | |
| 206 | \\ | |
| 207 | \\typedef struct OpaqueStruct OpaqueStructTypedef; | |
| 208 | \\#define Macro(opaquePtr) 	(((StructPtrType)(opaquePtr))->i) | |
| 209 | \\int main(int argc, char **argv) { | |
| 210 | \\ StructType localStruct = {88}; | |
| 211 | \\ OpaqueStructTypedef *opaquePtrToLocal = &localStruct; | |
| 212 | \\ printf("%d!\n", Macro(opaquePtrToLocal)); | |
| 213 | \\ return 0; | |
| 214 | \\} | |
| 215 | , "88!\n"); | |
| 198 | 216 | } |
test/translate_c.zig+20-4| ... | ... | @@ -1404,7 +1404,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1404 | 1404 | cases.add("macro pointer cast", |
| 1405 | 1405 | \\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE) |
| 1406 | 1406 | , &[_][]const u8{ |
| 1407 | \\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); | |
| 1407 | \\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)); | |
| 1408 | 1408 | }); |
| 1409 | 1409 | |
| 1410 | 1410 | cases.add("basic macro function", |
| ... | ... | @@ -2588,11 +2588,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2588 | 2588 | \\#define FOO(bar) baz((void *)(baz)) |
| 2589 | 2589 | \\#define BAR (void*) a |
| 2590 | 2590 | , &[_][]const u8{ |
| 2591 | \\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))) { | |
| 2592 | \\ 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)); | |
| 2591 | \\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)))) { | |
| 2592 | \\ 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))); | |
| 2593 | 2593 | \\} |
| 2594 | 2594 | , |
| 2595 | \\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); | |
| 2595 | \\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)); | |
| 2596 | 2596 | }); |
| 2597 | 2597 | |
| 2598 | 2598 | cases.add("macro conditional operator", |
| ... | ... | @@ -2754,4 +2754,20 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2754 | 2754 | \\ return if (x > y) x else y; |
| 2755 | 2755 | \\} |
| 2756 | 2756 | }); |
| 2757 | ||
| 2758 | cases.add("Make sure casts are grouped", | |
| 2759 | \\typedef struct | |
| 2760 | \\{ | |
| 2761 | \\ int i; | |
| 2762 | \\} | |
| 2763 | \\*_XPrivDisplay; | |
| 2764 | \\typedef struct _XDisplay Display; | |
| 2765 | \\#define DefaultScreen(dpy) 	(((_XPrivDisplay)(dpy))->default_screen) | |
| 2766 | \\ | |
| 2767 | , &[_][]const u8{ | |
| 2768 | \\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) { | |
| 2769 | \\ 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; | |
| 2770 | \\} | |
| 2771 | }); | |
| 2772 | ||
| 2757 | 2773 | } |