From a016fb8c627063e43b3694384e6082ef1e4ee11d Mon Sep 17 00:00:00 2001 From: Vexu Date: Wed, 15 Apr 2020 15:14:10 +0300 Subject: [PATCH] translate-c: correct invalid shortcut --- src-self-hosted/translate_c.zig | 44 +++++++++++++++++++++++++++++---- test/translate_c.zig | 15 +++-------- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index a88cc8e81..155862acc 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -5523,24 +5523,58 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, return error.ParseError; } + const lparen = try appendToken(c, .LParen, "("); + if (saw_integer_literal) { - // @intToPtr(dest, x) + //( if (@typeInfo(dest) == .Pointer)) + // @intToPtr(dest, x) + //else + // @as(dest, x) ) + const if_node = try transCreateNodeIf(c); + const type_info_node = try transCreateNodeBuiltinFnCall(c, "@typeInfo"); + try type_info_node.params.push(inner_node); + type_info_node.rparen_token = try appendToken(c, .LParen, ")"); + const cmp_node = try c.a().create(ast.Node.InfixOp); + cmp_node.* = .{ + .op_token = try appendToken(c, .EqualEqual, "=="), + .lhs = &type_info_node.base, + .op = .EqualEqual, + .rhs = try transCreateNodeEnumLiteral(c, "Pointer"), + }; + if_node.condition = &cmp_node.base; + _ = try appendToken(c, .RParen, ")"); + const int_to_ptr = try transCreateNodeBuiltinFnCall(c, "@intToPtr"); try int_to_ptr.params.push(inner_node); try int_to_ptr.params.push(node_to_cast); int_to_ptr.rparen_token = try appendToken(c, .RParen, ")"); - return &int_to_ptr.base; + if_node.body = &int_to_ptr.base; + + const else_node = try transCreateNodeElse(c); + if_node.@"else" = else_node; + + const as_node = try transCreateNodeBuiltinFnCall(c, "@as"); + try as_node.params.push(inner_node); + try as_node.params.push(node_to_cast); + as_node.rparen_token = try appendToken(c, .RParen, ")"); + else_node.body = &as_node.base; + + const group_node = try c.a().create(ast.Node.GroupedExpression); + group_node.* = .{ + .lparen = lparen, + .expr = &if_node.base, + .rparen = try appendToken(c, .RParen, ")"), + }; + return &group_node.base; } //( if (@typeInfo(@TypeOf(x)) == .Pointer) // @ptrCast(dest, @alignCast(@alignOf(dest.Child), x)) - //else if (@typeInfo(@TypeOf(x)) == .Integer and @typeInfo(dest) == .Pointer)) + //else if (@typeInfo(@TypeOf(x)) == .Int and @typeInfo(dest) == .Pointer)) // @intToPtr(dest, x) //else // @as(dest, x) ) - const lparen = try appendToken(c, .LParen, "("); - const if_1 = try transCreateNodeIf(c); const type_id_1 = try transCreateNodeBuiltinFnCall(c, "@typeInfo"); const type_of_1 = try transCreateNodeBuiltinFnCall(c, "@TypeOf"); diff --git a/test/translate_c.zig b/test/translate_c.zig index 799c0fd78..f4d10a618 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -2895,20 +2895,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\} }); - cases.add("Cast from integer literals to poiter", + cases.add("macro integer literal casts", \\#define NULL ((void*)0) - \\#define GPIO_0_MEM_MAP ((unsigned*)0x8000) - \\#define GPIO_1_MEM_MAP ((unsigned*)0x8004) - \\#define GPIO_2_MEM_MAP ((unsigned*)0x8008) - \\ + \\#define FOO ((int)0x8000) , &[_][]const u8{ - \\pub const NULL = @intToPtr(?*c_void, 0); + \\pub const NULL = (if (@typeInfo(?*c_void) == .Pointer) @intToPtr(?*c_void, 0) else @as(?*c_void, 0)); , - \\pub const GPIO_0_MEM_MAP = @intToPtr([*c]c_uint, 0x8000); - , - \\pub const GPIO_1_MEM_MAP = @intToPtr([*c]c_uint, 0x8004); - , - \\pub const GPIO_2_MEM_MAP = @intToPtr([*c]c_uint, 0x8008); + \\pub const FOO = (if (@typeInfo(c_int) == .Pointer) @intToPtr(c_int, 0x8000) else @as(c_int, 0x8000)); }); if (std.Target.current.abi == .msvc) {