authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-11-17 21:33:26+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-11-18 00:52:56+02:00
logbace1181b2f6d63e51b6e511aa8318481e2eafee
tree9b39b68f01cebd79808f051b6f6bc5e85491ca40
parentc1e19f4c0a62047c26d5baabe25887e533cc739f
signaturelock-open Commit is signed but in an unrecognized format.

stage2: handle opaque containers


3 files changed, 50 insertions(+), 4 deletions(-)

src/Module.zig+1-1
...@@ -1515,7 +1515,6 @@ pub fn analyzeContainer(self: *Module, container_scope: *Scope.Container) !void...@@ -1515,7 +1515,6 @@ pub fn analyzeContainer(self: *Module, container_scope: *Scope.Container) !void
1515 // an incremental update. This code handles both cases.1515 // an incremental update. This code handles both cases.
1516 const tree = try self.getAstTree(container_scope);1516 const tree = try self.getAstTree(container_scope);
1517 const decls = tree.root_node.decls();1517 const decls = tree.root_node.decls();
1518 // const decls = container_scope.root_node.decls();
15191518
1520 try self.comp.work_queue.ensureUnusedCapacity(decls.len);1519 try self.comp.work_queue.ensureUnusedCapacity(decls.len);
1521 try container_scope.decls.ensureCapacity(self.gpa, decls.len);1520 try container_scope.decls.ensureCapacity(self.gpa, decls.len);
...@@ -2302,6 +2301,7 @@ pub fn createContainerDecl(...@@ -2302,6 +2301,7 @@ pub fn createContainerDecl(
2302}2301}
23032302
2304fn getAnonTypeName(self: *Module, scope: *Scope, base_token: std.zig.ast.TokenIndex) ![]u8 {2303fn getAnonTypeName(self: *Module, scope: *Scope, base_token: std.zig.ast.TokenIndex) ![]u8 {
2304 // TODO add namespaces, generic function signatrues
2305 const tree = scope.tree();2305 const tree = scope.tree();
2306 const base_name = switch (tree.token_ids[base_token]) {2306 const base_name = switch (tree.token_ids[base_token]) {
2307 .Keyword_struct => "struct",2307 .Keyword_struct => "struct",
src/astgen.zig+13-1
...@@ -913,7 +913,19 @@ fn containerDecl(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node.Con...@@ -913,7 +913,19 @@ fn containerDecl(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node.Con
913 };913 };
914 break :blk Type.initPayload(&union_type.base);914 break :blk Type.initPayload(&union_type.base);
915 },915 },
916 .Keyword_opaque => return mod.fail(scope, src, "TODO opaque containers", .{}),916 .Keyword_opaque => blk: {
917 if (fields.items.len > 0) {
918 return mod.fail(scope, fields.items[0].src, "opaque types cannot have fields", .{});
919 }
920 const opaque_type = try arena.create(Type.Payload.Opaque);
921 opaque_type.* = .{
922 .scope = .{
923 .file_scope = scope.getFileScope(),
924 .ty = Type.initPayload(&opaque_type.base),
925 },
926 };
927 break :blk Type.initPayload(&opaque_type.base);
928 },
917 else => unreachable,929 else => unreachable,
918 };930 };
919 const type_payload = try arena.create(Value.Payload.Ty);931 const type_payload = try arena.create(Value.Payload.Ty);
src/type.zig+36-2
...@@ -49,7 +49,7 @@ pub const Type = extern union {...@@ -49,7 +49,7 @@ pub const Type = extern union {
49 .f128,49 .f128,
50 => return .Float,50 => return .Float,
5151
52 .c_void => return .Opaque,52 .c_void, .@"opaque" => return .Opaque,
53 .bool => return .Bool,53 .bool => return .Bool,
54 .void => return .Void,54 .void => return .Void,
55 .type => return .Type,55 .type => return .Type,
...@@ -449,6 +449,7 @@ pub const Type = extern union {...@@ -449,6 +449,7 @@ pub const Type = extern union {
449 .@"enum" => return self,449 .@"enum" => return self,
450 .@"struct" => return self,450 .@"struct" => return self,
451 .@"union" => return self,451 .@"union" => return self,
452 .@"opaque" => return self,
452 }453 }
453 }454 }
454455
...@@ -680,10 +681,11 @@ pub const Type = extern union {...@@ -680,10 +681,11 @@ pub const Type = extern union {
680 const payload = @fieldParentPtr(Payload.ErrorSetSingle, "base", ty.ptr_otherwise);681 const payload = @fieldParentPtr(Payload.ErrorSetSingle, "base", ty.ptr_otherwise);
681 return out_stream.print("error{{{}}}", .{payload.name});682 return out_stream.print("error{{{}}}", .{payload.name});
682 },683 },
683 // TODO improve684 // TODO use declaration name
684 .@"enum" => return out_stream.writeAll("enum {}"),685 .@"enum" => return out_stream.writeAll("enum {}"),
685 .@"struct" => return out_stream.writeAll("struct {}"),686 .@"struct" => return out_stream.writeAll("struct {}"),
686 .@"union" => return out_stream.writeAll("union {}"),687 .@"union" => return out_stream.writeAll("union {}"),
688 .@"opaque" => return out_stream.writeAll("opaque {}"),
687 }689 }
688 unreachable;690 unreachable;
689 }691 }
...@@ -809,6 +811,7 @@ pub const Type = extern union {...@@ -809,6 +811,7 @@ pub const Type = extern union {
809 .@"undefined",811 .@"undefined",
810 .enum_literal,812 .enum_literal,
811 .empty_struct,813 .empty_struct,
814 .@"opaque",
812 => false,815 => false,
813 };816 };
814 }817 }
...@@ -937,6 +940,7 @@ pub const Type = extern union {...@@ -937,6 +940,7 @@ pub const Type = extern union {
937 .@"undefined",940 .@"undefined",
938 .enum_literal,941 .enum_literal,
939 .empty_struct,942 .empty_struct,
943 .@"opaque",
940 => unreachable,944 => unreachable,
941 };945 };
942 }946 }
...@@ -960,6 +964,7 @@ pub const Type = extern union {...@@ -960,6 +964,7 @@ pub const Type = extern union {
960 .enum_literal => unreachable,964 .enum_literal => unreachable,
961 .single_const_pointer_to_comptime_int => unreachable,965 .single_const_pointer_to_comptime_int => unreachable,
962 .empty_struct => unreachable,966 .empty_struct => unreachable,
967 .@"opaque" => unreachable,
963968
964 .u8,969 .u8,
965 .i8,970 .i8,
...@@ -1143,6 +1148,7 @@ pub const Type = extern union {...@@ -1143,6 +1148,7 @@ pub const Type = extern union {
1143 .@"enum",1148 .@"enum",
1144 .@"struct",1149 .@"struct",
1145 .@"union",1150 .@"union",
1151 .@"opaque",
1146 => false,1152 => false,
11471153
1148 .single_const_pointer,1154 .single_const_pointer,
...@@ -1221,6 +1227,7 @@ pub const Type = extern union {...@@ -1221,6 +1227,7 @@ pub const Type = extern union {
1221 .@"enum",1227 .@"enum",
1222 .@"struct",1228 .@"struct",
1223 .@"union",1229 .@"union",
1230 .@"opaque",
1224 => false,1231 => false,
12251232
1226 .const_slice,1233 .const_slice,
...@@ -1296,6 +1303,7 @@ pub const Type = extern union {...@@ -1296,6 +1303,7 @@ pub const Type = extern union {
1296 .@"enum",1303 .@"enum",
1297 .@"struct",1304 .@"struct",
1298 .@"union",1305 .@"union",
1306 .@"opaque",
1299 => false,1307 => false,
13001308
1301 .single_const_pointer,1309 .single_const_pointer,
...@@ -1380,6 +1388,7 @@ pub const Type = extern union {...@@ -1380,6 +1388,7 @@ pub const Type = extern union {
1380 .@"enum",1388 .@"enum",
1381 .@"struct",1389 .@"struct",
1382 .@"union",1390 .@"union",
1391 .@"opaque",
1383 => false,1392 => false,
13841393
1385 .pointer => {1394 .pointer => {
...@@ -1459,6 +1468,7 @@ pub const Type = extern union {...@@ -1459,6 +1468,7 @@ pub const Type = extern union {
1459 .@"enum",1468 .@"enum",
1460 .@"struct",1469 .@"struct",
1461 .@"union",1470 .@"union",
1471 .@"opaque",
1462 => false,1472 => false,
14631473
1464 .pointer => {1474 .pointer => {
...@@ -1580,6 +1590,7 @@ pub const Type = extern union {...@@ -1580,6 +1590,7 @@ pub const Type = extern union {
1580 .@"enum",1590 .@"enum",
1581 .@"struct",1591 .@"struct",
1582 .@"union",1592 .@"union",
1593 .@"opaque",
1583 => unreachable,1594 => unreachable,
15841595
1585 .array => self.cast(Payload.Array).?.elem_type,1596 .array => self.cast(Payload.Array).?.elem_type,
...@@ -1711,6 +1722,7 @@ pub const Type = extern union {...@@ -1711,6 +1722,7 @@ pub const Type = extern union {
1711 .@"enum",1722 .@"enum",
1712 .@"struct",1723 .@"struct",
1713 .@"union",1724 .@"union",
1725 .@"opaque",
1714 => unreachable,1726 => unreachable,
17151727
1716 .array => self.cast(Payload.Array).?.len,1728 .array => self.cast(Payload.Array).?.len,
...@@ -1780,6 +1792,7 @@ pub const Type = extern union {...@@ -1780,6 +1792,7 @@ pub const Type = extern union {
1780 .@"enum",1792 .@"enum",
1781 .@"struct",1793 .@"struct",
1782 .@"union",1794 .@"union",
1795 .@"opaque",
1783 => unreachable,1796 => unreachable,
17841797
1785 .single_const_pointer,1798 .single_const_pointer,
...@@ -1866,6 +1879,7 @@ pub const Type = extern union {...@@ -1866,6 +1879,7 @@ pub const Type = extern union {
1866 .@"enum",1879 .@"enum",
1867 .@"struct",1880 .@"struct",
1868 .@"union",1881 .@"union",
1882 .@"opaque",
1869 => false,1883 => false,
18701884
1871 .int_signed,1885 .int_signed,
...@@ -1944,6 +1958,7 @@ pub const Type = extern union {...@@ -1944,6 +1958,7 @@ pub const Type = extern union {
1944 .@"enum",1958 .@"enum",
1945 .@"struct",1959 .@"struct",
1946 .@"union",1960 .@"union",
1961 .@"opaque",
1947 => false,1962 => false,
19481963
1949 .int_unsigned,1964 .int_unsigned,
...@@ -2012,6 +2027,7 @@ pub const Type = extern union {...@@ -2012,6 +2027,7 @@ pub const Type = extern union {
2012 .@"enum",2027 .@"enum",
2013 .@"struct",2028 .@"struct",
2014 .@"union",2029 .@"union",
2030 .@"opaque",
2015 => unreachable,2031 => unreachable,
20162032
2017 .int_unsigned => .{ .signed = false, .bits = self.cast(Payload.IntUnsigned).?.bits },2033 .int_unsigned => .{ .signed = false, .bits = self.cast(Payload.IntUnsigned).?.bits },
...@@ -2098,6 +2114,7 @@ pub const Type = extern union {...@@ -2098,6 +2114,7 @@ pub const Type = extern union {
2098 .@"enum",2114 .@"enum",
2099 .@"struct",2115 .@"struct",
2100 .@"union",2116 .@"union",
2117 .@"opaque",
2101 => false,2118 => false,
21022119
2103 .usize,2120 .usize,
...@@ -2213,6 +2230,7 @@ pub const Type = extern union {...@@ -2213,6 +2230,7 @@ pub const Type = extern union {
2213 .@"enum",2230 .@"enum",
2214 .@"struct",2231 .@"struct",
2215 .@"union",2232 .@"union",
2233 .@"opaque",
2216 => unreachable,2234 => unreachable,
2217 };2235 };
2218 }2236 }
...@@ -2294,6 +2312,7 @@ pub const Type = extern union {...@@ -2294,6 +2312,7 @@ pub const Type = extern union {
2294 .@"enum",2312 .@"enum",
2295 .@"struct",2313 .@"struct",
2296 .@"union",2314 .@"union",
2315 .@"opaque",
2297 => unreachable,2316 => unreachable,
2298 }2317 }
2299 }2318 }
...@@ -2374,6 +2393,7 @@ pub const Type = extern union {...@@ -2374,6 +2393,7 @@ pub const Type = extern union {
2374 .@"enum",2393 .@"enum",
2375 .@"struct",2394 .@"struct",
2376 .@"union",2395 .@"union",
2396 .@"opaque",
2377 => unreachable,2397 => unreachable,
2378 }2398 }
2379 }2399 }
...@@ -2454,6 +2474,7 @@ pub const Type = extern union {...@@ -2454,6 +2474,7 @@ pub const Type = extern union {
2454 .@"enum",2474 .@"enum",
2455 .@"struct",2475 .@"struct",
2456 .@"union",2476 .@"union",
2477 .@"opaque",
2457 => unreachable,2478 => unreachable,
2458 };2479 };
2459 }2480 }
...@@ -2531,6 +2552,7 @@ pub const Type = extern union {...@@ -2531,6 +2552,7 @@ pub const Type = extern union {
2531 .@"enum",2552 .@"enum",
2532 .@"struct",2553 .@"struct",
2533 .@"union",2554 .@"union",
2555 .@"opaque",
2534 => unreachable,2556 => unreachable,
2535 };2557 };
2536 }2558 }
...@@ -2608,6 +2630,7 @@ pub const Type = extern union {...@@ -2608,6 +2630,7 @@ pub const Type = extern union {
2608 .@"enum",2630 .@"enum",
2609 .@"struct",2631 .@"struct",
2610 .@"union",2632 .@"union",
2633 .@"opaque",
2611 => unreachable,2634 => unreachable,
2612 };2635 };
2613 }2636 }
...@@ -2685,6 +2708,7 @@ pub const Type = extern union {...@@ -2685,6 +2708,7 @@ pub const Type = extern union {
2685 .@"enum",2708 .@"enum",
2686 .@"struct",2709 .@"struct",
2687 .@"union",2710 .@"union",
2711 .@"opaque",
2688 => false,2712 => false,
2689 };2713 };
2690 }2714 }
...@@ -2742,6 +2766,7 @@ pub const Type = extern union {...@@ -2742,6 +2766,7 @@ pub const Type = extern union {
2742 .error_union,2766 .error_union,
2743 .error_set,2767 .error_set,
2744 .error_set_single,2768 .error_set_single,
2769 .@"opaque",
2745 => return null,2770 => return null,
27462771
2747 .@"enum" => @panic("TODO onePossibleValue enum"),2772 .@"enum" => @panic("TODO onePossibleValue enum"),
...@@ -2860,6 +2885,7 @@ pub const Type = extern union {...@@ -2860,6 +2885,7 @@ pub const Type = extern union {
2860 .@"enum",2885 .@"enum",
2861 .@"struct",2886 .@"struct",
2862 .@"union",2887 .@"union",
2888 .@"opaque",
2863 => return false,2889 => return false,
28642890
2865 .c_const_pointer,2891 .c_const_pointer,
...@@ -2951,6 +2977,7 @@ pub const Type = extern union {...@@ -2951,6 +2977,7 @@ pub const Type = extern union {
2951 .@"enum" => &self.cast(Type.Payload.Enum).?.scope,2977 .@"enum" => &self.cast(Type.Payload.Enum).?.scope,
2952 .@"struct" => &self.cast(Type.Payload.Struct).?.scope,2978 .@"struct" => &self.cast(Type.Payload.Struct).?.scope,
2953 .@"union" => &self.cast(Type.Payload.Union).?.scope,2979 .@"union" => &self.cast(Type.Payload.Union).?.scope,
2980 .@"opaque" => &self.cast(Type.Payload.Union).?.scope,
2954 };2981 };
2955 }2982 }
29562983
...@@ -3105,6 +3132,7 @@ pub const Type = extern union {...@@ -3105,6 +3132,7 @@ pub const Type = extern union {
3105 @"enum",3132 @"enum",
3106 @"struct",3133 @"struct",
3107 @"union",3134 @"union",
3135 @"opaque",
31083136
3109 pub const last_no_payload_tag = Tag.const_slice_u8;3137 pub const last_no_payload_tag = Tag.const_slice_u8;
3110 pub const no_payload_count = @enumToInt(last_no_payload_tag) + 1;3138 pub const no_payload_count = @enumToInt(last_no_payload_tag) + 1;
...@@ -3221,6 +3249,12 @@ pub const Type = extern union {...@@ -3221,6 +3249,12 @@ pub const Type = extern union {
3221 scope: *Module.Scope.Container,3249 scope: *Module.Scope.Container,
3222 };3250 };
32233251
3252 pub const Opaque = struct {
3253 base: Payload = .{ .tag = .@"opaque" },
3254
3255 scope: Module.Scope.Container,
3256 };
3257
3224 pub const Enum = @import("type/Enum.zig");3258 pub const Enum = @import("type/Enum.zig");
3225 pub const Struct = @import("type/Struct.zig");3259 pub const Struct = @import("type/Struct.zig");
3226 pub const Union = @import("type/Union.zig");3260 pub const Union = @import("type/Union.zig");