| author | |
| committer | |
| log | 9e60c89601ab205cd3e63215b318beb89ad1b471 |
| tree | 26b94818f3aed2230c2f8bc6f1b4b48d7b555e62 |
| parent | 8b80cb3072536069d745d6f0bee03ed3ea0e523a |
| signature |
This reverts commit 895672b3f96aab1f5bad3446f5186a047f29412c.3 files changed, 9 insertions(+), 74 deletions(-)
src-self-hosted/translate_c.zig+5-36| ... | ... | @@ -5424,15 +5424,12 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5424 | 5424 | return error.ParseError; |
| 5425 | 5425 | } |
| 5426 | 5426 | |
| 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)) | |
| 5427 | //if (@typeInfo(@TypeOf(x)) == .Pointer) | |
| 5428 | // @ptrCast(dest, x) | |
| 5430 | 5429 | //else if (@typeInfo(@TypeOf(x)) == .Integer) |
| 5431 | 5430 | // @intToPtr(dest, x) |
| 5432 | 5431 | //else |
| 5433 | // @as(dest, x) ) | |
| 5434 | ||
| 5435 | const group_lparen = try appendToken(c, .LParen, "("); | |
| 5432 | // @as(dest, x) | |
| 5436 | 5433 | |
| 5437 | 5434 | const if_1 = try transCreateNodeIf(c); |
| 5438 | 5435 | const type_id_1 = try transCreateNodeBuiltinFnCall(c, "@typeInfo"); |
| ... | ... | @@ -5452,30 +5449,9 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5452 | 5449 | if_1.condition = &cmp_1.base; |
| 5453 | 5450 | _ = try appendToken(c, .RParen, ")"); |
| 5454 | 5451 | |
| 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 | ||
| 5476 | 5452 | const ptr_cast = try transCreateNodeBuiltinFnCall(c, "@ptrCast"); |
| 5477 | 5453 | try ptr_cast.params.push(inner_node); |
| 5478 | try ptr_cast.params.push(&align_cast.base); | |
| 5454 | try ptr_cast.params.push(node_to_cast); | |
| 5479 | 5455 | ptr_cast.rparen_token = try appendToken(c, .RParen, ")"); |
| 5480 | 5456 | if_1.body = &ptr_cast.base; |
| 5481 | 5457 | |
| ... | ... | @@ -5516,14 +5492,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, |
| 5516 | 5492 | as.rparen_token = try appendToken(c, .RParen, ")"); |
| 5517 | 5493 | else_2.body = &as.base; |
| 5518 | 5494 | |
| 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; | |
| 5495 | return &if_1.base; | |
| 5527 | 5496 | }, |
| 5528 | 5497 | else => { |
| 5529 | 5498 | const first_tok = it.list.at(0); |
test/run_translated_c.zig-18| ... | ... | @@ -195,22 +195,4 @@ 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"); | |
| 216 | 198 | } |
test/translate_c.zig+4-20| ... | ... | @@ -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, @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)); | |
| 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); | |
| 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, @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))); | |
| 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)); | |
| 2593 | 2593 | \\} |
| 2594 | 2594 | , |
| 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)); | |
| 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); | |
| 2596 | 2596 | }); |
| 2597 | 2597 | |
| 2598 | 2598 | cases.add("macro conditional operator", |
| ... | ... | @@ -2754,20 +2754,4 @@ 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 | ||
| 2773 | 2757 | } |