authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-04-15 15:14:10+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-04-15 15:14:10+03:00
loga016fb8c627063e43b3694384e6082ef1e4ee11d
treea5f2c9c2e910625cfd539caadf01ba899c11de88
parent1e23a3cd9162df098a302f22d70f2ac50b5eae21
signature Commit is signed but in an unrecognized format.

translate-c: correct invalid shortcut


2 files changed, 43 insertions(+), 16 deletions(-)

src-self-hosted/translate_c.zig+39-5
......@@ -5523,24 +5523,58 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
55235523 return error.ParseError;
55245524 }
55255525
5526 const lparen = try appendToken(c, .LParen, "(");
5527
55265528 if (saw_integer_literal) {
5527 // @intToPtr(dest, x)
5529 //( if (@typeInfo(dest) == .Pointer))
5530 // @intToPtr(dest, x)
5531 //else
5532 // @as(dest, x) )
5533 const if_node = try transCreateNodeIf(c);
5534 const type_info_node = try transCreateNodeBuiltinFnCall(c, "@typeInfo");
5535 try type_info_node.params.push(inner_node);
5536 type_info_node.rparen_token = try appendToken(c, .LParen, ")");
5537 const cmp_node = try c.a().create(ast.Node.InfixOp);
5538 cmp_node.* = .{
5539 .op_token = try appendToken(c, .EqualEqual, "=="),
5540 .lhs = &type_info_node.base,
5541 .op = .EqualEqual,
5542 .rhs = try transCreateNodeEnumLiteral(c, "Pointer"),
5543 };
5544 if_node.condition = &cmp_node.base;
5545 _ = try appendToken(c, .RParen, ")");
5546
55285547 const int_to_ptr = try transCreateNodeBuiltinFnCall(c, "@intToPtr");
55295548 try int_to_ptr.params.push(inner_node);
55305549 try int_to_ptr.params.push(node_to_cast);
55315550 int_to_ptr.rparen_token = try appendToken(c, .RParen, ")");
5532 return &int_to_ptr.base;
5551 if_node.body = &int_to_ptr.base;
5552
5553 const else_node = try transCreateNodeElse(c);
5554 if_node.@"else" = else_node;
5555
5556 const as_node = try transCreateNodeBuiltinFnCall(c, "@as");
5557 try as_node.params.push(inner_node);
5558 try as_node.params.push(node_to_cast);
5559 as_node.rparen_token = try appendToken(c, .RParen, ")");
5560 else_node.body = &as_node.base;
5561
5562 const group_node = try c.a().create(ast.Node.GroupedExpression);
5563 group_node.* = .{
5564 .lparen = lparen,
5565 .expr = &if_node.base,
5566 .rparen = try appendToken(c, .RParen, ")"),
5567 };
5568 return &group_node.base;
55335569 }
55345570
55355571 //( if (@typeInfo(@TypeOf(x)) == .Pointer)
55365572 // @ptrCast(dest, @alignCast(@alignOf(dest.Child), x))
5537 //else if (@typeInfo(@TypeOf(x)) == .Integer and @typeInfo(dest) == .Pointer))
5573 //else if (@typeInfo(@TypeOf(x)) == .Int and @typeInfo(dest) == .Pointer))
55385574 // @intToPtr(dest, x)
55395575 //else
55405576 // @as(dest, x) )
55415577
5542 const lparen = try appendToken(c, .LParen, "(");
5543
55445578 const if_1 = try transCreateNodeIf(c);
55455579 const type_id_1 = try transCreateNodeBuiltinFnCall(c, "@typeInfo");
55465580 const type_of_1 = try transCreateNodeBuiltinFnCall(c, "@TypeOf");
test/translate_c.zig+4-11
......@@ -2895,20 +2895,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
28952895 \\}
28962896 });
28972897
2898 cases.add("Cast from integer literals to poiter",
2898 cases.add("macro integer literal casts",
28992899 \\#define NULL ((void*)0)
2900 \\#define GPIO_0_MEM_MAP ((unsigned*)0x8000)
2901 \\#define GPIO_1_MEM_MAP ((unsigned*)0x8004)
2902 \\#define GPIO_2_MEM_MAP ((unsigned*)0x8008)
2903 \\
2900 \\#define FOO ((int)0x8000)
29042901 , &[_][]const u8{
2905 \\pub const NULL = @intToPtr(?*c_void, 0);
2906 ,
2907 \\pub const GPIO_0_MEM_MAP = @intToPtr([*c]c_uint, 0x8000);
2908 ,
2909 \\pub const GPIO_1_MEM_MAP = @intToPtr([*c]c_uint, 0x8004);
2902 \\pub const NULL = (if (@typeInfo(?*c_void) == .Pointer) @intToPtr(?*c_void, 0) else @as(?*c_void, 0));
29102903 ,
2911 \\pub const GPIO_2_MEM_MAP = @intToPtr([*c]c_uint, 0x8008);
2904 \\pub const FOO = (if (@typeInfo(c_int) == .Pointer) @intToPtr(c_int, 0x8000) else @as(c_int, 0x8000));
29122905 });
29132906
29142907 if (std.Target.current.abi == .msvc) {