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

This commit is contained in:
Vexu 2020-03-10 15:52:03 +02:00
parent 675f01f176
commit baec74645d
No known key found for this signature in database
GPG Key ID: 59AEB8936E16A6AC
2 changed files with 24 additions and 5 deletions

View File

@ -5437,7 +5437,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
//if (@typeInfo(@TypeOf(x)) == .Pointer) //if (@typeInfo(@TypeOf(x)) == .Pointer)
// @ptrCast(dest, x) // @ptrCast(dest, x)
//else if (@typeInfo(@TypeOf(x)) == .Integer) //else if (@typeInfo(@TypeOf(x)) == .Int and @typeInfo(dest) == .Pointer)
// @intToPtr(dest, x) // @intToPtr(dest, x)
//else //else
// @as(dest, x) // @as(dest, x)
@ -5487,6 +5487,25 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
.rhs = try transCreateNodeEnumLiteral(c, "Int"), .rhs = try transCreateNodeEnumLiteral(c, "Int"),
}; };
if_2.condition = &cmp_2.base; 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; else_1.body = &if_2.base;
_ = try appendToken(c, .RParen, ")"); _ = try appendToken(c, .RParen, ")");

View File

@ -1429,7 +1429,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
cases.add("macro pointer cast", cases.add("macro pointer cast",
\\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE) \\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE)
, &[_][]const u8{ , &[_][]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", cases.add("basic macro function",
@ -2613,11 +2613,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\#define FOO(bar) baz((void *)(baz)) \\#define FOO(bar) baz((void *)(baz))
\\#define BAR (void*) a \\#define BAR (void*) a
, &[_][]const u8{ , &[_][]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)))) { \\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) @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", cases.add("macro conditional operator",