authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-03-10 15:52:03+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-03-10 15:52:03+02:00
logbaec74645d8757f51f1ac14483daf4438e2d77bd
tree65d2182dbe7989d448ab57b787cde0ac999b34ef
parent675f01f1768aa08c307640b53e8a5240fa190fab
signaturelock-open Commit is signed but in an unrecognized format.

translate-c add daurnimator's pointer check to macro cast


2 files changed, 24 insertions(+), 5 deletions(-)

src-self-hosted/translate_c.zig+20-1
...@@ -5437,7 +5437,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,...@@ -5437,7 +5437,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
54375437
5438 //if (@typeInfo(@TypeOf(x)) == .Pointer)5438 //if (@typeInfo(@TypeOf(x)) == .Pointer)
5439 // @ptrCast(dest, x)5439 // @ptrCast(dest, x)
5440 //else if (@typeInfo(@TypeOf(x)) == .Integer)5440 //else if (@typeInfo(@TypeOf(x)) == .Int and @typeInfo(dest) == .Pointer)
5441 // @intToPtr(dest, x)5441 // @intToPtr(dest, x)
5442 //else5442 //else
5443 // @as(dest, x)5443 // @as(dest, x)
...@@ -5487,6 +5487,25 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,...@@ -5487,6 +5487,25 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
5487 .rhs = try transCreateNodeEnumLiteral(c, "Int"),5487 .rhs = try transCreateNodeEnumLiteral(c, "Int"),
5488 };5488 };
5489 if_2.condition = &cmp_2.base;5489 if_2.condition = &cmp_2.base;
5490 const cmp_4 = try c.a().create(ast.Node.InfixOp);
5491 cmp_4.* = .{
5492 .op_token = try appendToken(c, .Keyword_and, "and"),
5493 .lhs = &cmp_2.base,
5494 .op = .BoolAnd,
5495 .rhs = undefined,
5496 };
5497 const type_id_3 = try transCreateNodeBuiltinFnCall(c, "@typeInfo");
5498 try type_id_3.params.push(inner_node);
5499 type_id_3.rparen_token = try appendToken(c, .LParen, ")");
5500 const cmp_3 = try c.a().create(ast.Node.InfixOp);
5501 cmp_3.* = .{
5502 .op_token = try appendToken(c, .EqualEqual, "=="),
5503 .lhs = &type_id_3.base,
5504 .op = .EqualEqual,
5505 .rhs = try transCreateNodeEnumLiteral(c, "Pointer"),
5506 };
5507 cmp_4.rhs = &cmp_3.base;
5508 if_2.condition = &cmp_4.base;
5490 else_1.body = &if_2.base;5509 else_1.body = &if_2.base;
5491 _ = try appendToken(c, .RParen, ")");5510 _ = try appendToken(c, .RParen, ")");
54925511
test/translate_c.zig+4-4
...@@ -1429,7 +1429,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1429,7 +1429,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1429 cases.add("macro pointer cast",1429 cases.add("macro pointer cast",
1430 \\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE)1430 \\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE)
1431 , &[_][]const u8{1431 , &[_][]const u8{
1432 \\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));1432 \\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));
1433 });1433 });
14341434
1435 cases.add("basic macro function",1435 cases.add("basic macro function",
...@@ -2613,11 +2613,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2613,11 +2613,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2613 \\#define FOO(bar) baz((void *)(baz))2613 \\#define FOO(bar) baz((void *)(baz))
2614 \\#define BAR (void*) a2614 \\#define BAR (void*) a
2615 , &[_][]const u8{2615 , &[_][]const u8{
2616 \\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)))) {2616 \\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)))) {
2617 \\ 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)));2617 \\ 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)));
2618 \\}2618 \\}
2619 ,2619 ,
2620 \\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));2620 \\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));
2621 });2621 });
26222622
2623 cases.add("macro conditional operator",2623 cases.add("macro conditional operator",