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 {...@@ -4811,6 +4811,15 @@ fn transCreateNodeIdentifier(c: *Context, name: []const u8) !*ast.Node {
4811 return &identifier.base;4811 return &identifier.base;
4812}4812}
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
4814pub fn freeErrors(errors: []ClangErrMsg) void {4823pub fn freeErrors(errors: []ClangErrMsg) void {
4815 ZigClangErrorMsg_delete(errors.ptr, errors.len);4824 ZigClangErrorMsg_delete(errors.ptr, errors.len);
4816}4825}
...@@ -5314,14 +5323,15 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,...@@ -5314,14 +5323,15 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
5314 return parseCNumLit(c, tok, source, source_loc);5323 return parseCNumLit(c, tok, source, source_loc);
5315 },5324 },
5316 // eventually this will be replaced by std.c.parse which will handle these correctly5325 // eventually this will be replaced by std.c.parse which will handle these correctly
5317 .Keyword_void,5326 .Keyword_void => return transCreateNodeTypeIdentifier(c, "c_void"),
5318 .Keyword_bool,5327 .Keyword_bool => return transCreateNodeTypeIdentifier(c, "bool"),
5319 .Keyword_double,5328 .Keyword_double => return transCreateNodeTypeIdentifier(c, "f64"),
5320 .Keyword_long,5329 .Keyword_long => return transCreateNodeTypeIdentifier(c, "c_long"),
5321 .Keyword_int,5330 .Keyword_int => return transCreateNodeTypeIdentifier(c, "c_int"),
5322 .Keyword_float,5331 .Keyword_float => return transCreateNodeTypeIdentifier(c, "f32"),
5323 .Keyword_short,5332 .Keyword_short => return transCreateNodeTypeIdentifier(c, "c_short"),
5324 .Keyword_char,5333 .Keyword_char => return transCreateNodeTypeIdentifier(c, "c_char"),
5334 .Keyword_unsigned => return transCreateNodeTypeIdentifier(c, "c_uint"),
5325 .Identifier => {5335 .Identifier => {
5326 const mangled_name = scope.getAlias(source[tok.start..tok.end]);5336 const mangled_name = scope.getAlias(source[tok.start..tok.end]);
5327 return transCreateNodeIdentifier(c, mangled_name);5337 return transCreateNodeIdentifier(c, mangled_name);
...@@ -5483,7 +5493,17 @@ fn parseCSuffixOpExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,...@@ -5483,7 +5493,17 @@ fn parseCSuffixOpExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
5483 // hack to get zig fmt to render a comma in builtin calls5493 // hack to get zig fmt to render a comma in builtin calls
5484 _ = try appendToken(c, .Comma, ",");5494 _ = 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);
5487 ptr.rhs = node;5507 ptr.rhs = node;
5488 return &ptr.base;5508 return &ptr.base;
5489 } else {5509 } else {
test/translate_c.zig+2-2
...@@ -2530,8 +2530,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2530,8 +2530,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2530 cases.add("macro cast",2530 cases.add("macro cast",
2531 \\#define FOO(bar) baz((void *)(baz))2531 \\#define FOO(bar) baz((void *)(baz))
2532 , &[_][]const u8{2532 , &[_][]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))) {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));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));
2535 \\}2535 \\}
2536 });2536 });
25372537