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,
54375437
54385438 //if (@typeInfo(@TypeOf(x)) == .Pointer)
54395439 // @ptrCast(dest, x)
5440 //else if (@typeInfo(@TypeOf(x)) == .Integer)
5440 //else if (@typeInfo(@TypeOf(x)) == .Int and @typeInfo(dest) == .Pointer)
54415441 // @intToPtr(dest, x)
54425442 //else
54435443 // @as(dest, x)
......@@ -5487,6 +5487,25 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
54875487 .rhs = try transCreateNodeEnumLiteral(c, "Int"),
54885488 };
54895489 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;
54905509 else_1.body = &if_2.base;
54915510 _ = try appendToken(c, .RParen, ")");
54925511
test/translate_c.zig+4-4
......@@ -1429,7 +1429,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
14291429 cases.add("macro pointer cast",
14301430 \\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE)
14311431 , &[_][]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));
14331433 });
14341434
14351435 cases.add("basic macro function",
......@@ -2613,11 +2613,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
26132613 \\#define FOO(bar) baz((void *)(baz))
26142614 \\#define BAR (void*) a
26152615 , &[_][]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)))) {
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)));
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 and @typeInfo(*c_void) == .Pointer) @intToPtr(*c_void, baz) else @as(*c_void, baz)));
26182618 \\}
26192619 ,
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));
26212621 });
26222622
26232623 cases.add("macro conditional operator",