authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-02-05 17:52:46+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-02-05 17:52:46+02:00
logf196ddd251d80fac74685d822ec26a646c7890fa
tree0f8448b6c621c838104e3decd58525d955c315e5
parent1f49460dcb532a146022ef5e9f037477b4c13314
signaturelock-open Commit is signed but in an unrecognized format.

translate c type names


2 files changed, 31 insertions(+), 11 deletions(-)

src-self-hosted/translate_c.zig+29-9
......@@ -4811,6 +4811,15 @@ fn transCreateNodeIdentifier(c: *Context, name: []const u8) !*ast.Node {
48114811 return &identifier.base;
48124812}
48134813
4814fn transCreateNodeTypeIdentifier(c: *Context, name: []const u8) !*ast.Node {
4815 const token_index = try appendTokenFmt(c, .Identifier, "{}", .{name});
4816 const identifier = try c.a().create(ast.Node.Identifier);
4817 identifier.* = .{
4818 .token = token_index,
4819 };
4820 return &identifier.base;
4821}
4822
48144823pub fn freeErrors(errors: []ClangErrMsg) void {
48154824 ZigClangErrorMsg_delete(errors.ptr, errors.len);
48164825}
......@@ -5314,14 +5323,15 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
53145323 return parseCNumLit(c, tok, source, source_loc);
53155324 },
53165325 // eventually this will be replaced by std.c.parse which will handle these correctly
5317 .Keyword_void,
5318 .Keyword_bool,
5319 .Keyword_double,
5320 .Keyword_long,
5321 .Keyword_int,
5322 .Keyword_float,
5323 .Keyword_short,
5324 .Keyword_char,
5326 .Keyword_void => return transCreateNodeTypeIdentifier(c, "c_void"),
5327 .Keyword_bool => return transCreateNodeTypeIdentifier(c, "bool"),
5328 .Keyword_double => return transCreateNodeTypeIdentifier(c, "f64"),
5329 .Keyword_long => return transCreateNodeTypeIdentifier(c, "c_long"),
5330 .Keyword_int => return transCreateNodeTypeIdentifier(c, "c_int"),
5331 .Keyword_float => return transCreateNodeTypeIdentifier(c, "f32"),
5332 .Keyword_short => return transCreateNodeTypeIdentifier(c, "c_short"),
5333 .Keyword_char => return transCreateNodeTypeIdentifier(c, "c_char"),
5334 .Keyword_unsigned => return transCreateNodeTypeIdentifier(c, "c_uint"),
53255335 .Identifier => {
53265336 const mangled_name = scope.getAlias(source[tok.start..tok.end]);
53275337 return transCreateNodeIdentifier(c, mangled_name);
......@@ -5483,7 +5493,17 @@ fn parseCSuffixOpExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
54835493 // hack to get zig fmt to render a comma in builtin calls
54845494 _ = try appendToken(c, .Comma, ",");
54855495
5486 const ptr = try transCreateNodePtrType(c, false, false, .Identifier);
5496 const ptr_kind = blk:{
5497 // * token
5498 _ = it.prev();
5499 // last token of `node`
5500 const prev_id = it.prev().?.id;
5501 _ = it.next();
5502 _ = it.next();
5503 break :blk if (prev_id == .Keyword_void) .Asterisk else Token.Id.Identifier;
5504 };
5505
5506 const ptr = try transCreateNodePtrType(c, false, false, ptr_kind);
54875507 ptr.rhs = node;
54885508 return &ptr.base;
54895509 } else {
test/translate_c.zig+2-2
......@@ -2530,8 +2530,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
25302530 cases.add("macro cast",
25312531 \\#define FOO(bar) baz((void *)(baz))
25322532 , &[_][]const u8{
2533 \\pub inline fn FOO(bar: var) @TypeOf(baz(if (@typeId(@TypeOf(baz)) == .Pointer) @ptrCast([*c]void, baz) else if (@typeId(@TypeOf(baz)) == .Int) @intToPtr([*c]void, baz) else @as([*c]void, baz))) {
2534 \\ return baz(if (@typeId(@TypeOf(baz)) == .Pointer) @ptrCast([*c]void, baz) else if (@typeId(@TypeOf(baz)) == .Int) @intToPtr([*c]void, baz) else @as([*c]void, baz));
2533 \\pub inline fn FOO(bar: var) @TypeOf(baz(if (@typeId(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeId(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz))) {
2534 \\ return baz(if (@typeId(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeId(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz));
25352535 \\}
25362536 });
25372537