Merge branch 'LakeByTheWoods-translate_ptr_int'

closes #4688
master
Andrew Kelley 2020-04-08 14:11:16 -04:00
commit 29ea425ca2
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
2 changed files with 96 additions and 24 deletions

View File

@ -5479,18 +5479,20 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
.LParen => {
const inner_node = try parseCExpr(c, it, source, source_loc, scope);
if (it.next().?.id != .RParen) {
const next_id = it.next().?.id;
if (next_id != .RParen) {
const first_tok = it.list.at(0);
try failDecl(
c,
source_loc,
source[first_tok.start..first_tok.end],
"unable to translate C expr: expected ')'' here",
.{},
"unable to translate C expr: expected ')'' instead got: {}",
.{@tagName(next_id)},
);
return error.ParseError;
}
var saw_l_paren = false;
var saw_integer_literal = false;
switch (it.peek().?.id) {
// (type)(to_cast)
.LParen => {
@ -5499,6 +5501,10 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
},
// (type)identifier
.Identifier => {},
// (type)integer
.IntegerLiteral => {
saw_integer_literal = true;
},
else => return inner_node,
}
@ -5519,12 +5525,21 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
return error.ParseError;
}
//if (@typeInfo(@TypeOf(x)) == .Pointer)
// @ptrCast(dest, x)
//else if (@typeInfo(@TypeOf(x)) == .Int and @typeInfo(dest) == .Pointer)
if (saw_integer_literal) {
// @intToPtr(dest, x)
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 (@typeInfo(@TypeOf(x)) == .Pointer)
// @ptrCast(dest, @alignCast(@alignOf(dest.Child), x))
//else if (@typeInfo(@TypeOf(x)) == .Integer and @typeInfo(dest) == .Pointer))
// @intToPtr(dest, x)
//else
// @as(dest, x)
// @as(dest, x) )
const lparen = try appendToken(c, .LParen, "(");
@ -5546,9 +5561,30 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
if_1.condition = &cmp_1.base;
_ = try appendToken(c, .RParen, ")");
const period_tok = try appendToken(c, .Period, ".");
const child_ident = try transCreateNodeIdentifier(c, "Child");
const inner_node_child = try c.a().create(ast.Node.InfixOp);
inner_node_child.* = .{
.op_token = period_tok,
.lhs = inner_node,
.op = .Period,
.rhs = child_ident,
};
const align_of = try transCreateNodeBuiltinFnCall(c, "@alignOf");
try align_of.params.push(&inner_node_child.base);
align_of.rparen_token = try appendToken(c, .RParen, ")");
// hack to get zig fmt to render a comma in builtin calls
_ = try appendToken(c, .Comma, ",");
const align_cast = try transCreateNodeBuiltinFnCall(c, "@alignCast");
try align_cast.params.push(&align_of.base);
try align_cast.params.push(node_to_cast);
align_cast.rparen_token = try appendToken(c, .RParen, ")");
const ptr_cast = try transCreateNodeBuiltinFnCall(c, "@ptrCast");
try ptr_cast.params.push(inner_node);
try ptr_cast.params.push(node_to_cast);
try ptr_cast.params.push(&align_cast.base);
ptr_cast.rparen_token = try appendToken(c, .RParen, ")");
if_1.body = &ptr_cast.base;
@ -5729,19 +5765,24 @@ fn parseCSuffixOpExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
// hack to get zig fmt to render a comma in builtin calls
_ = try appendToken(c, .Comma, ",");
const ptr_kind = blk: {
// * token
_ = it.prev();
// last token of `node`
const prev_id = it.prev().?.id;
_ = it.next();
_ = it.next();
break :blk if (prev_id == .Keyword_void) .Asterisk else Token.Id.Identifier;
};
// * token
_ = it.prev();
// last token of `node`
const prev_id = it.prev().?.id;
_ = it.next();
_ = it.next();
const ptr = try transCreateNodePtrType(c, false, false, ptr_kind);
ptr.rhs = node;
return &ptr.base;
if (prev_id == .Keyword_void) {
const ptr = try transCreateNodePtrType(c, false, false, .Asterisk);
ptr.rhs = node;
const optional_node = try transCreateNodePrefixOp(c, .OptionalType, .QuestionMark, "?");
optional_node.rhs = &ptr.base;
return &optional_node.base;
} else {
const ptr = try transCreateNodePtrType(c, false, false, Token.Id.Identifier);
ptr.rhs = node;
return &ptr.base;
}
} else {
// expr * expr
op_token = try appendToken(c, .Asterisk, "*");

View File

@ -1458,7 +1458,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 and @typeInfo([*c]NRF_GPIO_Type) == .Pointer) @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, @alignCast(@alignOf([*c]NRF_GPIO_Type.Child), 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",
@ -2668,11 +2668,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 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 inline fn FOO(bar: var) @TypeOf(baz((if (@typeInfo(@TypeOf(baz)) == .Pointer) @ptrCast(?*c_void, @alignCast(@alignOf(?*c_void.Child), 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, @alignCast(@alignOf(?*c_void.Child), 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 and @typeInfo(*c_void) == .Pointer) @intToPtr(*c_void, a) else @as(*c_void, a));
\\pub const BAR = (if (@typeInfo(@TypeOf(a)) == .Pointer) @ptrCast(?*c_void, @alignCast(@alignOf(?*c_void.Child), 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",
@ -2879,4 +2879,35 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
, &[_][]const u8{
\\pub const FOO = 0x61626364;
});
cases.add("Make sure casts are grouped",
\\typedef struct
\\{
\\ int i;
\\}
\\*_XPrivDisplay;
\\typedef struct _XDisplay Display;
\\#define DefaultScreen(dpy) (((_XPrivDisplay)(dpy))->default_screen)
\\
, &[_][]const u8{
\\pub inline fn DefaultScreen(dpy: var) @TypeOf((if (@typeInfo(@TypeOf(dpy)) == .Pointer) @ptrCast(_XPrivDisplay, @alignCast(@alignOf(_XPrivDisplay.Child), dpy)) else if (@typeInfo(@TypeOf(dpy)) == .Int and @typeInfo(_XPrivDisplay) == .Pointer) @intToPtr(_XPrivDisplay, dpy) else @as(_XPrivDisplay, dpy)).*.default_screen) {
\\ return (if (@typeInfo(@TypeOf(dpy)) == .Pointer) @ptrCast(_XPrivDisplay, @alignCast(@alignOf(_XPrivDisplay.Child), dpy)) else if (@typeInfo(@TypeOf(dpy)) == .Int and @typeInfo(_XPrivDisplay) == .Pointer) @intToPtr(_XPrivDisplay, dpy) else @as(_XPrivDisplay, dpy)).*.default_screen;
\\}
});
cases.add("Cast from integer literals to poiter",
\\#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)
\\
, &[_][]const u8{
\\pub const NULL = @intToPtr(?*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);
});
}