aboutsummaryrefslogtreecommitdiff
path: root/src-self-hosted
diff options
context:
space:
mode:
authorLachlan Easton <lachlan@lakebythewoods.xyz>2020-03-09 20:46:18 +1100
committerAndrew Kelley <andrew@ziglang.org>2020-04-08 14:11:01 -0400
commitd7902707bcc1faede9ef5490d02bcea935e3b8fc (patch)
tree244557b0172b781f1dcaa6b2538cf9e515943576 /src-self-hosted
parent7b5fb79b5b9625a1c2b7359c6653653a799ca114 (diff)
downloadzig-d7902707bcc1faede9ef5490d02bcea935e3b8fc.tar.gz
zig-d7902707bcc1faede9ef5490d02bcea935e3b8fc.zip
Translate C: Allow casting literal ints to pointers
Diffstat (limited to 'src-self-hosted')
-rw-r--r--src-self-hosted/translate_c.zig50
1 files changed, 35 insertions, 15 deletions
diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig
index d5d751443d..e4d8cc4397 100644
--- a/src-self-hosted/translate_c.zig
+++ b/src-self-hosted/translate_c.zig
@@ -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,6 +5525,15 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
return error.ParseError;
}
+ 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))
@@ -5750,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, "*");