diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index be3e43f3d..949178b37 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -5437,7 +5437,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, //if (@typeInfo(@TypeOf(x)) == .Pointer) // @ptrCast(dest, x) - //else if (@typeInfo(@TypeOf(x)) == .Integer) + //else if (@typeInfo(@TypeOf(x)) == .Int and @typeInfo(dest) == .Pointer) // @intToPtr(dest, x) //else // @as(dest, x) @@ -5487,6 +5487,25 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, .rhs = try transCreateNodeEnumLiteral(c, "Int"), }; if_2.condition = &cmp_2.base; + const cmp_4 = try c.a().create(ast.Node.InfixOp); + cmp_4.* = .{ + .op_token = try appendToken(c, .Keyword_and, "and"), + .lhs = &cmp_2.base, + .op = .BoolAnd, + .rhs = undefined, + }; + const type_id_3 = try transCreateNodeBuiltinFnCall(c, "@typeInfo"); + try type_id_3.params.push(inner_node); + type_id_3.rparen_token = try appendToken(c, .LParen, ")"); + const cmp_3 = try c.a().create(ast.Node.InfixOp); + cmp_3.* = .{ + .op_token = try appendToken(c, .EqualEqual, "=="), + .lhs = &type_id_3.base, + .op = .EqualEqual, + .rhs = try transCreateNodeEnumLiteral(c, "Pointer"), + }; + cmp_4.rhs = &cmp_3.base; + if_2.condition = &cmp_4.base; else_1.body = &if_2.base; _ = try appendToken(c, .RParen, ")"); diff --git a/test/translate_c.zig b/test/translate_c.zig index 209a301f8..7e70e698d 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -1429,7 +1429,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, NRF_GPIO_BASE) else if (@typeInfo(@TypeOf(NRF_GPIO_BASE)) == .Int and @typeInfo([*c]NRF_GPIO_Type) == .Pointer) @intToPtr([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else @as([*c]NRF_GPIO_Type, NRF_GPIO_BASE)); }); cases.add("basic macro function", @@ -2613,11 +2613,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, baz) else if (@typeInfo(@TypeOf(baz)) == .Int and @typeInfo(*c_void) == .Pointer) @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 and @typeInfo(*c_void) == .Pointer) @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, a) else if (@typeInfo(@TypeOf(a)) == .Int and @typeInfo(*c_void) == .Pointer) @intToPtr(*c_void, a) else @as(*c_void, a)); }); cases.add("macro conditional operator",