From f196ddd251d80fac74685d822ec26a646c7890fa Mon Sep 17 00:00:00 2001 From: Vexu Date: Wed, 5 Feb 2020 17:52:46 +0200 Subject: [PATCH] translate c type names --- src-self-hosted/translate_c.zig | 38 +++++++++++++++++++++++++-------- test/translate_c.zig | 4 ++-- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index 663793dbe9..221e4201ca 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -4811,6 +4811,15 @@ fn transCreateNodeIdentifier(c: *Context, name: []const u8) !*ast.Node { return &identifier.base; } +fn transCreateNodeTypeIdentifier(c: *Context, name: []const u8) !*ast.Node { + const token_index = try appendTokenFmt(c, .Identifier, "{}", .{name}); + const identifier = try c.a().create(ast.Node.Identifier); + identifier.* = .{ + .token = token_index, + }; + return &identifier.base; +} + pub fn freeErrors(errors: []ClangErrMsg) void { ZigClangErrorMsg_delete(errors.ptr, errors.len); } @@ -5314,14 +5323,15 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, return parseCNumLit(c, tok, source, source_loc); }, // eventually this will be replaced by std.c.parse which will handle these correctly - .Keyword_void, - .Keyword_bool, - .Keyword_double, - .Keyword_long, - .Keyword_int, - .Keyword_float, - .Keyword_short, - .Keyword_char, + .Keyword_void => return transCreateNodeTypeIdentifier(c, "c_void"), + .Keyword_bool => return transCreateNodeTypeIdentifier(c, "bool"), + .Keyword_double => return transCreateNodeTypeIdentifier(c, "f64"), + .Keyword_long => return transCreateNodeTypeIdentifier(c, "c_long"), + .Keyword_int => return transCreateNodeTypeIdentifier(c, "c_int"), + .Keyword_float => return transCreateNodeTypeIdentifier(c, "f32"), + .Keyword_short => return transCreateNodeTypeIdentifier(c, "c_short"), + .Keyword_char => return transCreateNodeTypeIdentifier(c, "c_char"), + .Keyword_unsigned => return transCreateNodeTypeIdentifier(c, "c_uint"), .Identifier => { const mangled_name = scope.getAlias(source[tok.start..tok.end]); return transCreateNodeIdentifier(c, mangled_name); @@ -5483,7 +5493,17 @@ 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 = try transCreateNodePtrType(c, false, false, .Identifier); + 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; + }; + + const ptr = try transCreateNodePtrType(c, false, false, ptr_kind); ptr.rhs = node; return &ptr.base; } else { diff --git a/test/translate_c.zig b/test/translate_c.zig index 4a9478be8e..6246742c5f 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -2530,8 +2530,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void { cases.add("macro cast", \\#define FOO(bar) baz((void *)(baz)) , &[_][]const u8{ - \\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))) { - \\ 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)); + \\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))) { + \\ 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)); \\} });