authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-07-11 22:04:38+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-07-11 22:04:38+03:00
log3e095d8ef32fc93f5050cead708849846d626d1d
tree64b9b837d024c459bb87e96e050d9e6657c458f6
parent1a989ba39dbf2b718df7cc9792b6abaac7f5986d
signaturelock-open Commit is signed but in an unrecognized format.

use 'anytype' in translate-c


5 files changed, 22 insertions(+), 23 deletions(-)

lib/std/zig/ast.zig+4-4
......@@ -993,7 +993,7 @@ pub const Node = struct {
993993 param_type: ParamType,
994994
995995 pub const ParamType = union(enum) {
996 var_type: *Node,
996 any_type: *Node,
997997 var_args: TokenIndex,
998998 type_expr: *Node,
999999 };
......@@ -1004,7 +1004,7 @@ pub const Node = struct {
10041004 if (i < 1) {
10051005 switch (self.param_type) {
10061006 .var_args => return null,
1007 .var_type, .type_expr => |node| return node,
1007 .any_type, .type_expr => |node| return node,
10081008 }
10091009 }
10101010 i -= 1;
......@@ -1018,14 +1018,14 @@ pub const Node = struct {
10181018 if (self.name_token) |name_token| return name_token;
10191019 switch (self.param_type) {
10201020 .var_args => |tok| return tok,
1021 .var_type, .type_expr => |node| return node.firstToken(),
1021 .any_type, .type_expr => |node| return node.firstToken(),
10221022 }
10231023 }
10241024
10251025 pub fn lastToken(self: *const ParamDecl) TokenIndex {
10261026 switch (self.param_type) {
10271027 .var_args => |tok| return tok,
1028 .var_type, .type_expr => |node| return node.lastToken(),
1028 .any_type, .type_expr => |node| return node.lastToken(),
10291029 }
10301030 }
10311031 };
lib/std/zig/parse.zig+3-3
......@@ -519,7 +519,7 @@ const Parser = struct {
519519 const callconv_expr = try p.parseCallconv();
520520 const exclamation_token = p.eatToken(.Bang);
521521
522 const return_type_expr = (try p.parseVarType()) orelse
522 const return_type_expr = (try p.parseAnyType()) orelse
523523 try p.expectNodeRecoverable(parseTypeExpr, .{
524524 // most likely the user forgot to specify the return type.
525525 // Mark return type as invalid and try to continue.
......@@ -2028,7 +2028,7 @@ const Parser = struct {
20282028 fn parseParamType(p: *Parser) !?Node.FnProto.ParamDecl.ParamType {
20292029 // TODO cast from tuple to error union is broken
20302030 const P = Node.FnProto.ParamDecl.ParamType;
2031 if (try p.parseVarType()) |node| return P{ .var_type = node };
2031 if (try p.parseAnyType()) |node| return P{ .any_type = node };
20322032 if (p.eatToken(.Ellipsis3)) |token| return P{ .var_args = token };
20332033 if (try p.parseTypeExpr()) |node| return P{ .type_expr = node };
20342034 return null;
......@@ -3057,7 +3057,7 @@ const Parser = struct {
30573057 return &node.base;
30583058 }
30593059
3060 fn parseVarType(p: *Parser) !?*Node {
3060 fn parseAnyType(p: *Parser) !?*Node {
30613061 const token = p.eatToken(.Keyword_anytype) orelse
30623062 p.eatToken(.Keyword_var) orelse return null; // TODO remove in next release cycle
30633063 const node = try p.arena.allocator.create(Node.AnyType);
lib/std/zig/render.zig+1-1
......@@ -2198,7 +2198,7 @@ fn renderParamDecl(
21982198 }
21992199 switch (param_decl.param_type) {
22002200 .var_args => |token| try renderToken(tree, stream, token, indent, start_col, space),
2201 .var_type, .type_expr => |node| try renderExpression(allocator, stream, tree, indent, start_col, node, space),
2201 .any_type, .type_expr => |node| try renderExpression(allocator, stream, tree, indent, start_col, node, space),
22022202 }
22032203}
22042204
src-self-hosted/translate_c.zig+4-5
......@@ -5215,10 +5215,9 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8,
52155215 const param_name_tok = try appendIdentifier(c, mangled_name);
52165216 _ = try appendToken(c, .Colon, ":");
52175217
5218 const token_index = try appendToken(c, .Keyword_var, "var");
5219 const identifier = try c.arena.create(ast.Node.Identifier);
5220 identifier.* = .{
5221 .token = token_index,
5218 const any_type = try c.arena.create(ast.Node.AnyType);
5219 any_type.* = .{
5220 .token = try appendToken(c, .Keyword_anytype, "anytype"),
52225221 };
52235222
52245223 (try fn_params.addOne()).* = .{
......@@ -5226,7 +5225,7 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8,
52265225 .comptime_token = null,
52275226 .noalias_token = null,
52285227 .name_token = param_name_tok,
5229 .param_type = .{ .type_expr = &identifier.base },
5228 .param_type = .{ .any_type = &any_type.base },
52305229 };
52315230
52325231 if (it.peek().?.id != .Comma)
test/translate_c.zig+10-10
......@@ -21,7 +21,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2121 cases.add("correct semicolon after infixop",
2222 \\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0)
2323 , &[_][]const u8{
24 \\pub inline fn __ferror_unlocked_body(_fp: var) @TypeOf(((_fp.*._flags) & _IO_ERR_SEEN) != 0) {
24 \\pub inline fn __ferror_unlocked_body(_fp: anytype) @TypeOf(((_fp.*._flags) & _IO_ERR_SEEN) != 0) {
2525 \\ return ((_fp.*._flags) & _IO_ERR_SEEN) != 0;
2626 \\}
2727 });
......@@ -30,7 +30,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3030 \\#define FOO(x) ((x >= 0) + (x >= 0))
3131 \\#define BAR 1 && 2 > 4
3232 , &[_][]const u8{
33 \\pub inline fn FOO(x: var) @TypeOf(@boolToInt(x >= 0) + @boolToInt(x >= 0)) {
33 \\pub inline fn FOO(x: anytype) @TypeOf(@boolToInt(x >= 0) + @boolToInt(x >= 0)) {
3434 \\ return @boolToInt(x >= 0) + @boolToInt(x >= 0);
3535 \\}
3636 ,
......@@ -81,7 +81,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
8181 \\ break :blk bar;
8282 \\};
8383 ,
84 \\pub inline fn bar(x: var) @TypeOf(baz(1, 2)) {
84 \\pub inline fn bar(x: anytype) @TypeOf(baz(1, 2)) {
8585 \\ return blk: {
8686 \\ _ = &x;
8787 \\ _ = 3;
......@@ -1483,11 +1483,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
14831483 , &[_][]const u8{
14841484 \\pub extern var c: c_int;
14851485 ,
1486 \\pub inline fn BASIC(c_1: var) @TypeOf(c_1 * 2) {
1486 \\pub inline fn BASIC(c_1: anytype) @TypeOf(c_1 * 2) {
14871487 \\ return c_1 * 2;
14881488 \\}
14891489 ,
1490 \\pub inline fn FOO(L: var, b: var) @TypeOf(L + b) {
1490 \\pub inline fn FOO(L: anytype, b: anytype) @TypeOf(L + b) {
14911491 \\ return L + b;
14921492 \\}
14931493 });
......@@ -2123,7 +2123,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
21232123 cases.add("macro call",
21242124 \\#define CALL(arg) bar(arg)
21252125 , &[_][]const u8{
2126 \\pub inline fn CALL(arg: var) @TypeOf(bar(arg)) {
2126 \\pub inline fn CALL(arg: anytype) @TypeOf(bar(arg)) {
21272127 \\ return bar(arg);
21282128 \\}
21292129 });
......@@ -2683,7 +2683,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
26832683 \\#define FOO(bar) baz((void *)(baz))
26842684 \\#define BAR (void*) a
26852685 , &[_][]const u8{
2686 \\pub inline fn FOO(bar: var) @TypeOf(baz((@import("std").meta.cast(?*c_void, baz)))) {
2686 \\pub inline fn FOO(bar: anytype) @TypeOf(baz((@import("std").meta.cast(?*c_void, baz)))) {
26872687 \\ return baz((@import("std").meta.cast(?*c_void, baz)));
26882688 \\}
26892689 ,
......@@ -2713,11 +2713,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
27132713 \\#define MIN(a, b) ((b) < (a) ? (b) : (a))
27142714 \\#define MAX(a, b) ((b) > (a) ? (b) : (a))
27152715 , &[_][]const u8{
2716 \\pub inline fn MIN(a: var, b: var) @TypeOf(if (b < a) b else a) {
2716 \\pub inline fn MIN(a: anytype, b: anytype) @TypeOf(if (b < a) b else a) {
27172717 \\ return if (b < a) b else a;
27182718 \\}
27192719 ,
2720 \\pub inline fn MAX(a: var, b: var) @TypeOf(if (b > a) b else a) {
2720 \\pub inline fn MAX(a: anytype, b: anytype) @TypeOf(if (b > a) b else a) {
27212721 \\ return if (b > a) b else a;
27222722 \\}
27232723 });
......@@ -2905,7 +2905,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
29052905 \\#define DefaultScreen(dpy) (((_XPrivDisplay)(dpy))->default_screen)
29062906 \\
29072907 , &[_][]const u8{
2908 \\pub inline fn DefaultScreen(dpy: var) @TypeOf((@import("std").meta.cast(_XPrivDisplay, dpy)).*.default_screen) {
2908 \\pub inline fn DefaultScreen(dpy: anytype) @TypeOf((@import("std").meta.cast(_XPrivDisplay, dpy)).*.default_screen) {
29092909 \\ return (@import("std").meta.cast(_XPrivDisplay, dpy)).*.default_screen;
29102910 \\}
29112911 });