authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-03 17:22:17-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-03 17:22:17-04:00
log5dfcd09e496160054d4f333500d084e35f2fbdd2
tree08f4177b091848c1b78ec8e7c1d7973ee230391a
parent7f6e97cb26ffabbc192e7ccdf44aebbbc3be751d

self-hosted: watch files and trigger a rebuild


15 files changed, 756 insertions(+), 330 deletions(-)

src-self-hosted/compilation.zig+112-46
......@@ -230,6 +230,8 @@ pub const Compilation = struct {
230230
231231 c_int_types: [CInt.list.len]*Type.Int,
232232
233 fs_watch: *fs.Watch(*Scope.Root),
234
233235 const IntTypeTable = std.HashMap(*const Type.Int.Key, *Type.Int, Type.Int.Key.hash, Type.Int.Key.eql);
234236 const ArrayTypeTable = std.HashMap(*const Type.Array.Key, *Type.Array, Type.Array.Key.hash, Type.Array.Key.eql);
235237 const PtrTypeTable = std.HashMap(*const Type.Pointer.Key, *Type.Pointer, Type.Pointer.Key.hash, Type.Pointer.Key.eql);
......@@ -285,6 +287,7 @@ pub const Compilation = struct {
285287 LibCMissingDynamicLinker,
286288 InvalidDarwinVersionString,
287289 UnsupportedLinkArchitecture,
290 UserResourceLimitReached,
288291 };
289292
290293 pub const Event = union(enum) {
......@@ -331,7 +334,8 @@ pub const Compilation = struct {
331334 zig_lib_dir: []const u8,
332335 ) !*Compilation {
333336 const loop = event_loop_local.loop;
334 const comp = try event_loop_local.loop.allocator.create(Compilation{
337 const comp = try event_loop_local.loop.allocator.createOne(Compilation);
338 comp.* = Compilation{
335339 .loop = loop,
336340 .arena_allocator = std.heap.ArenaAllocator.init(loop.allocator),
337341 .event_loop_local = event_loop_local,
......@@ -376,7 +380,7 @@ pub const Compilation = struct {
376380 .fn_link_set = event.Locked(FnLinkSet).init(loop, FnLinkSet.init()),
377381 .windows_subsystem_windows = false,
378382 .windows_subsystem_console = false,
379 .link_libs_list = undefined,
383 .link_libs_list = ArrayList(*LinkLib).init(comp.arena()),
380384 .libc_link_lib = null,
381385 .err_color = errmsg.Color.Auto,
382386 .darwin_frameworks = [][]const u8{},
......@@ -417,8 +421,10 @@ pub const Compilation = struct {
417421 .override_libc = null,
418422 .destroy_handle = undefined,
419423 .have_err_ret_tracing = false,
420 .primitive_type_table = undefined,
421 });
424 .primitive_type_table = TypeTable.init(comp.arena()),
425
426 .fs_watch = undefined,
427 };
422428 errdefer {
423429 comp.int_type_table.private_data.deinit();
424430 comp.array_type_table.private_data.deinit();
......@@ -431,9 +437,7 @@ pub const Compilation = struct {
431437 comp.name = try Buffer.init(comp.arena(), name);
432438 comp.llvm_triple = try target.getTriple(comp.arena());
433439 comp.llvm_target = try Target.llvmTargetFromTriple(comp.llvm_triple);
434 comp.link_libs_list = ArrayList(*LinkLib).init(comp.arena());
435440 comp.zig_std_dir = try std.os.path.join(comp.arena(), zig_lib_dir, "std");
436 comp.primitive_type_table = TypeTable.init(comp.arena());
437441
438442 const opt_level = switch (build_mode) {
439443 builtin.Mode.Debug => llvm.CodeGenLevelNone,
......@@ -485,6 +489,9 @@ pub const Compilation = struct {
485489 comp.root_package = try Package.create(comp.arena(), ".", "");
486490 }
487491
492 comp.fs_watch = try fs.Watch(*Scope.Root).create(loop, 16);
493 errdefer comp.fs_watch.destroy();
494
488495 try comp.initTypes();
489496
490497 comp.destroy_handle = try async<loop.allocator> comp.internalDeinit();
......@@ -686,6 +693,7 @@ pub const Compilation = struct {
686693 os.deleteTree(self.arena(), tmp_dir) catch {};
687694 } else |_| {};
688695
696 self.fs_watch.destroy();
689697 self.events.destroy();
690698
691699 llvm.DisposeMessage(self.target_layout_str);
......@@ -720,7 +728,9 @@ pub const Compilation = struct {
720728 var build_result = await (async self.initialCompile() catch unreachable);
721729
722730 while (true) {
723 const link_result = if (build_result) self.maybeLink() else |err| err;
731 const link_result = if (build_result) blk: {
732 break :blk await (async self.maybeLink() catch unreachable);
733 } else |err| err;
724734 // this makes a handy error return trace and stack trace in debug mode
725735 if (std.debug.runtime_safety) {
726736 link_result catch unreachable;
......@@ -745,9 +755,35 @@ pub const Compilation = struct {
745755 await (async self.events.put(Event{ .Error = err }) catch unreachable);
746756 }
747757
758 // First, get an item from the watch channel, waiting on the channel.
748759 var group = event.Group(BuildError!void).init(self.loop);
749 while (self.fs_watch.channel.getOrNull()) |root_scope| {
750 try group.call(rebuildFile, self, root_scope);
760 {
761 const ev = await (async self.fs_watch.channel.get() catch unreachable);
762 const root_scope = switch (ev) {
763 fs.Watch(*Scope.Root).Event.CloseWrite => |x| x,
764 fs.Watch(*Scope.Root).Event.Err => |err| {
765 build_result = err;
766 continue;
767 },
768 };
769 group.call(rebuildFile, self, root_scope) catch |err| {
770 build_result = err;
771 continue;
772 };
773 }
774 // Next, get all the items from the channel that are buffered up.
775 while (await (async self.fs_watch.channel.getOrNull() catch unreachable)) |ev| {
776 const root_scope = switch (ev) {
777 fs.Watch(*Scope.Root).Event.CloseWrite => |x| x,
778 fs.Watch(*Scope.Root).Event.Err => |err| {
779 build_result = err;
780 continue;
781 },
782 };
783 group.call(rebuildFile, self, root_scope) catch |err| {
784 build_result = err;
785 continue;
786 };
751787 }
752788 build_result = await (async group.wait() catch unreachable);
753789 }
......@@ -757,11 +793,11 @@ pub const Compilation = struct {
757793 const tree_scope = blk: {
758794 const source_code = (await (async fs.readFile(
759795 self.loop,
760 root_src_real_path,
796 root_scope.realpath,
761797 max_src_size,
762798 ) catch unreachable)) catch |err| {
763 try printError("unable to open '{}': {}", root_src_real_path, err);
764 return err;
799 try self.addCompileErrorCli(root_scope.realpath, "unable to open: {}", @errorName(err));
800 return;
765801 };
766802 errdefer self.gpa().free(source_code);
767803
......@@ -793,9 +829,9 @@ pub const Compilation = struct {
793829 var decl_group = event.Group(BuildError!void).init(self.loop);
794830 defer decl_group.deinit();
795831
796 try self.rebuildChangedDecls(
832 try await try async self.rebuildChangedDecls(
797833 &decl_group,
798 locked_table,
834 locked_table.value,
799835 root_scope.decls,
800836 &tree_scope.tree.root_node.decls,
801837 tree_scope,
......@@ -809,7 +845,7 @@ pub const Compilation = struct {
809845 group: *event.Group(BuildError!void),
810846 locked_table: *Decl.Table,
811847 decl_scope: *Scope.Decls,
812 ast_decls: &ast.Node.Root.DeclList,
848 ast_decls: *ast.Node.Root.DeclList,
813849 tree_scope: *Scope.AstTree,
814850 ) !void {
815851 var existing_decls = try locked_table.clone();
......@@ -824,14 +860,14 @@ pub const Compilation = struct {
824860
825861 // TODO connect existing comptime decls to updated source files
826862
827 try self.prelink_group.call(addCompTimeBlock, self, &decl_scope.base, comptime_node);
863 try self.prelink_group.call(addCompTimeBlock, self, tree_scope, &decl_scope.base, comptime_node);
828864 },
829865 ast.Node.Id.VarDecl => @panic("TODO"),
830866 ast.Node.Id.FnProto => {
831867 const fn_proto = @fieldParentPtr(ast.Node.FnProto, "base", decl);
832868
833869 const name = if (fn_proto.name_token) |name_token| tree_scope.tree.tokenSlice(name_token) else {
834 try self.addCompileError(root_scope, Span{
870 try self.addCompileError(tree_scope, Span{
835871 .first = fn_proto.fn_token,
836872 .last = fn_proto.fn_token + 1,
837873 }, "missing function name");
......@@ -856,10 +892,12 @@ pub const Compilation = struct {
856892 .visib = parseVisibToken(tree_scope.tree, fn_proto.visib_token),
857893 .resolution = event.Future(BuildError!void).init(self.loop),
858894 .parent_scope = &decl_scope.base,
895 .tree_scope = tree_scope,
859896 },
860897 .value = Decl.Fn.Val{ .Unresolved = {} },
861898 .fn_proto = fn_proto,
862899 });
900 tree_scope.base.ref();
863901 errdefer self.gpa().destroy(fn_decl);
864902
865903 try group.call(addTopLevelDecl, self, &fn_decl.base, locked_table);
......@@ -883,8 +921,8 @@ pub const Compilation = struct {
883921 const root_scope = blk: {
884922 // TODO async/await os.path.real
885923 const root_src_real_path = os.path.real(self.gpa(), root_src_path) catch |err| {
886 try printError("unable to get real path '{}': {}", root_src_path, err);
887 return err;
924 try self.addCompileErrorCli(root_src_path, "unable to open: {}", @errorName(err));
925 return;
888926 };
889927 errdefer self.gpa().free(root_src_real_path);
890928
......@@ -892,7 +930,8 @@ pub const Compilation = struct {
892930 };
893931 defer root_scope.base.deref(self);
894932
895 try self.rebuildFile(root_scope);
933 assert((try await try async self.fs_watch.addFile(root_scope.realpath, root_scope)) == null);
934 try await try async self.rebuildFile(root_scope);
896935 }
897936 }
898937
......@@ -917,6 +956,7 @@ pub const Compilation = struct {
917956 /// caller takes ownership of resulting Code
918957 async fn genAndAnalyzeCode(
919958 comp: *Compilation,
959 tree_scope: *Scope.AstTree,
920960 scope: *Scope,
921961 node: *ast.Node,
922962 expected_type: ?*Type,
......@@ -924,6 +964,7 @@ pub const Compilation = struct {
924964 const unanalyzed_code = try await (async ir.gen(
925965 comp,
926966 node,
967 tree_scope,
927968 scope,
928969 ) catch unreachable);
929970 defer unanalyzed_code.destroy(comp.gpa());
......@@ -950,6 +991,7 @@ pub const Compilation = struct {
950991
951992 async fn addCompTimeBlock(
952993 comp: *Compilation,
994 tree_scope: *Scope.AstTree,
953995 scope: *Scope,
954996 comptime_node: *ast.Node.Comptime,
955997 ) !void {
......@@ -958,6 +1000,7 @@ pub const Compilation = struct {
9581000
9591001 const analyzed_code = (await (async genAndAnalyzeCode(
9601002 comp,
1003 tree_scope,
9611004 scope,
9621005 comptime_node.expr,
9631006 &void_type.base,
......@@ -975,25 +1018,37 @@ pub const Compilation = struct {
9751018 decl: *Decl,
9761019 locked_table: *Decl.Table,
9771020 ) !void {
978 const tree = decl.findRootScope().tree;
979 const is_export = decl.isExported(tree);
1021 const is_export = decl.isExported(decl.tree_scope.tree);
9801022
9811023 if (is_export) {
9821024 try self.prelink_group.call(verifyUniqueSymbol, self, decl);
9831025 try self.prelink_group.call(resolveDecl, self, decl);
9841026 }
9851027
986 if (try locked_table.put(decl.name, decl)) |other_decl| {
987 try self.addCompileError(decls.base.findRoot(), decl.getSpan(), "redefinition of '{}'", decl.name);
1028 const gop = try locked_table.getOrPut(decl.name);
1029 if (gop.found_existing) {
1030 try self.addCompileError(decl.tree_scope, decl.getSpan(), "redefinition of '{}'", decl.name);
9881031 // TODO note: other definition here
1032 } else {
1033 gop.kv.value = decl;
9891034 }
9901035 }
9911036
992 fn addCompileError(self: *Compilation, root: *Scope.Root, span: Span, comptime fmt: []const u8, args: ...) !void {
1037 fn addCompileError(self: *Compilation, tree_scope: *Scope.AstTree, span: Span, comptime fmt: []const u8, args: ...) !void {
1038 const text = try std.fmt.allocPrint(self.gpa(), fmt, args);
1039 errdefer self.gpa().free(text);
1040
1041 const msg = try Msg.createFromScope(self, tree_scope, span, text);
1042 errdefer msg.destroy();
1043
1044 try self.prelink_group.call(addCompileErrorAsync, self, msg);
1045 }
1046
1047 fn addCompileErrorCli(self: *Compilation, realpath: []const u8, comptime fmt: []const u8, args: ...) !void {
9931048 const text = try std.fmt.allocPrint(self.gpa(), fmt, args);
9941049 errdefer self.gpa().free(text);
9951050
996 const msg = try Msg.createFromScope(self, root, span, text);
1051 const msg = try Msg.createFromCli(self, realpath, text);
9971052 errdefer msg.destroy();
9981053
9991054 try self.prelink_group.call(addCompileErrorAsync, self, msg);
......@@ -1017,7 +1072,7 @@ pub const Compilation = struct {
10171072
10181073 if (try exported_symbol_names.value.put(decl.name, decl)) |other_decl| {
10191074 try self.addCompileError(
1020 decl.findRootScope(),
1075 decl.tree_scope,
10211076 decl.getSpan(),
10221077 "exported symbol collision: '{}'",
10231078 decl.name,
......@@ -1141,18 +1196,24 @@ pub const Compilation = struct {
11411196 }
11421197
11431198 /// Returns a value which has been ref()'d once
1144 async fn analyzeConstValue(comp: *Compilation, scope: *Scope, node: *ast.Node, expected_type: *Type) !*Value {
1145 const analyzed_code = try await (async comp.genAndAnalyzeCode(scope, node, expected_type) catch unreachable);
1199 async fn analyzeConstValue(
1200 comp: *Compilation,
1201 tree_scope: *Scope.AstTree,
1202 scope: *Scope,
1203 node: *ast.Node,
1204 expected_type: *Type,
1205 ) !*Value {
1206 const analyzed_code = try await (async comp.genAndAnalyzeCode(tree_scope, scope, node, expected_type) catch unreachable);
11461207 defer analyzed_code.destroy(comp.gpa());
11471208
11481209 return analyzed_code.getCompTimeResult(comp);
11491210 }
11501211
1151 async fn analyzeTypeExpr(comp: *Compilation, scope: *Scope, node: *ast.Node) !*Type {
1212 async fn analyzeTypeExpr(comp: *Compilation, tree_scope: *Scope.AstTree, scope: *Scope, node: *ast.Node) !*Type {
11521213 const meta_type = &Type.MetaType.get(comp).base;
11531214 defer meta_type.base.deref(comp);
11541215
1155 const result_val = try await (async comp.analyzeConstValue(scope, node, meta_type) catch unreachable);
1216 const result_val = try await (async comp.analyzeConstValue(tree_scope, scope, node, meta_type) catch unreachable);
11561217 errdefer result_val.base.deref(comp);
11571218
11581219 return result_val.cast(Type).?;
......@@ -1168,13 +1229,6 @@ pub const Compilation = struct {
11681229 }
11691230};
11701231
1171fn printError(comptime format: []const u8, args: ...) !void {
1172 var stderr_file = try std.io.getStdErr();
1173 var stderr_file_out_stream = std.io.FileOutStream.init(&stderr_file);
1174 const out_stream = &stderr_file_out_stream.stream;
1175 try out_stream.print(format, args);
1176}
1177
11781232fn parseVisibToken(tree: *ast.Tree, optional_token_index: ?ast.TokenIndex) Visib {
11791233 if (optional_token_index) |token_index| {
11801234 const token = tree.tokens.at(token_index);
......@@ -1198,12 +1252,14 @@ async fn generateDecl(comp: *Compilation, decl: *Decl) !void {
11981252}
11991253
12001254async fn generateDeclFn(comp: *Compilation, fn_decl: *Decl.Fn) !void {
1255 const tree_scope = fn_decl.base.tree_scope;
1256
12011257 const body_node = fn_decl.fn_proto.body_node orelse return await (async generateDeclFnProto(comp, fn_decl) catch unreachable);
12021258
12031259 const fndef_scope = try Scope.FnDef.create(comp, fn_decl.base.parent_scope);
12041260 defer fndef_scope.base.deref(comp);
12051261
1206 const fn_type = try await (async analyzeFnType(comp, fn_decl.base.parent_scope, fn_decl.fn_proto) catch unreachable);
1262 const fn_type = try await (async analyzeFnType(comp, tree_scope, fn_decl.base.parent_scope, fn_decl.fn_proto) catch unreachable);
12071263 defer fn_type.base.base.deref(comp);
12081264
12091265 var symbol_name = try std.Buffer.init(comp.gpa(), fn_decl.base.name);
......@@ -1216,18 +1272,17 @@ async fn generateDeclFn(comp: *Compilation, fn_decl: *Decl.Fn) !void {
12161272 symbol_name_consumed = true;
12171273
12181274 // Define local parameter variables
1219 const root_scope = fn_decl.base.findRootScope();
12201275 for (fn_type.key.data.Normal.params) |param, i| {
12211276 //AstNode *param_decl_node = get_param_decl_node(fn_table_entry, i);
12221277 const param_decl = @fieldParentPtr(ast.Node.ParamDecl, "base", fn_decl.fn_proto.params.at(i).*);
12231278 const name_token = param_decl.name_token orelse {
1224 try comp.addCompileError(root_scope, Span{
1279 try comp.addCompileError(tree_scope, Span{
12251280 .first = param_decl.firstToken(),
12261281 .last = param_decl.type_node.firstToken(),
12271282 }, "missing parameter name");
12281283 return error.SemanticAnalysisFailed;
12291284 };
1230 const param_name = root_scope.tree.tokenSlice(name_token);
1285 const param_name = tree_scope.tree.tokenSlice(name_token);
12311286
12321287 // if (is_noalias && get_codegen_ptr_type(param_type) == nullptr) {
12331288 // add_node_error(g, param_decl_node, buf_sprintf("noalias on non-pointer parameter"));
......@@ -1249,6 +1304,7 @@ async fn generateDeclFn(comp: *Compilation, fn_decl: *Decl.Fn) !void {
12491304 }
12501305
12511306 const analyzed_code = try await (async comp.genAndAnalyzeCode(
1307 tree_scope,
12521308 fn_val.child_scope,
12531309 body_node,
12541310 fn_type.key.data.Normal.return_type,
......@@ -1279,12 +1335,17 @@ fn getZigDir(allocator: *mem.Allocator) ![]u8 {
12791335 return os.getAppDataDir(allocator, "zig");
12801336}
12811337
1282async fn analyzeFnType(comp: *Compilation, scope: *Scope, fn_proto: *ast.Node.FnProto) !*Type.Fn {
1338async fn analyzeFnType(
1339 comp: *Compilation,
1340 tree_scope: *Scope.AstTree,
1341 scope: *Scope,
1342 fn_proto: *ast.Node.FnProto,
1343) !*Type.Fn {
12831344 const return_type_node = switch (fn_proto.return_type) {
12841345 ast.Node.FnProto.ReturnType.Explicit => |n| n,
12851346 ast.Node.FnProto.ReturnType.InferErrorSet => |n| n,
12861347 };
1287 const return_type = try await (async comp.analyzeTypeExpr(scope, return_type_node) catch unreachable);
1348 const return_type = try await (async comp.analyzeTypeExpr(tree_scope, scope, return_type_node) catch unreachable);
12881349 return_type.base.deref(comp);
12891350
12901351 var params = ArrayList(Type.Fn.Param).init(comp.gpa());
......@@ -1300,7 +1361,7 @@ async fn analyzeFnType(comp: *Compilation, scope: *Scope, fn_proto: *ast.Node.Fn
13001361 var it = fn_proto.params.iterator(0);
13011362 while (it.next()) |param_node_ptr| {
13021363 const param_node = param_node_ptr.*.cast(ast.Node.ParamDecl).?;
1303 const param_type = try await (async comp.analyzeTypeExpr(scope, param_node.type_node) catch unreachable);
1364 const param_type = try await (async comp.analyzeTypeExpr(tree_scope, scope, param_node.type_node) catch unreachable);
13041365 errdefer param_type.base.deref(comp);
13051366 try params.append(Type.Fn.Param{
13061367 .typ = param_type,
......@@ -1337,7 +1398,12 @@ async fn analyzeFnType(comp: *Compilation, scope: *Scope, fn_proto: *ast.Node.Fn
13371398}
13381399
13391400async fn generateDeclFnProto(comp: *Compilation, fn_decl: *Decl.Fn) !void {
1340 const fn_type = try await (async analyzeFnType(comp, fn_decl.base.parent_scope, fn_decl.fn_proto) catch unreachable);
1401 const fn_type = try await (async analyzeFnType(
1402 comp,
1403 fn_decl.base.tree_scope,
1404 fn_decl.base.parent_scope,
1405 fn_decl.fn_proto,
1406 ) catch unreachable);
13411407 defer fn_type.base.base.deref(comp);
13421408
13431409 var symbol_name = try std.Buffer.init(comp.gpa(), fn_decl.base.name);
src-self-hosted/decl.zig+2
......@@ -16,6 +16,8 @@ pub const Decl = struct {
1616 visib: Visib,
1717 resolution: event.Future(Compilation.BuildError!void),
1818 parent_scope: *Scope,
19
20 // TODO when we destroy the decl, deref the tree scope
1921 tree_scope: *Scope.AstTree,
2022
2123 pub const Table = std.HashMap([]const u8, *Decl, mem.hash_slice_u8, mem.eql_slice_u8);
src-self-hosted/errmsg.zig+75-28
......@@ -33,35 +33,48 @@ pub const Span = struct {
3333};
3434
3535pub const Msg = struct {
36 span: Span,
3736 text: []u8,
37 realpath: []u8,
3838 data: Data,
3939
4040 const Data = union(enum) {
41 Cli: Cli,
4142 PathAndTree: PathAndTree,
4243 ScopeAndComp: ScopeAndComp,
4344 };
4445
4546 const PathAndTree = struct {
46 realpath: []const u8,
47 span: Span,
4748 tree: *ast.Tree,
4849 allocator: *mem.Allocator,
4950 };
5051
5152 const ScopeAndComp = struct {
53 span: Span,
5254 tree_scope: *Scope.AstTree,
5355 compilation: *Compilation,
5456 };
5557
58 const Cli = struct {
59 allocator: *mem.Allocator,
60 };
61
5662 pub fn destroy(self: *Msg) void {
5763 switch (self.data) {
64 Data.Cli => |cli| {
65 cli.allocator.free(self.text);
66 cli.allocator.free(self.realpath);
67 cli.allocator.destroy(self);
68 },
5869 Data.PathAndTree => |path_and_tree| {
5970 path_and_tree.allocator.free(self.text);
71 path_and_tree.allocator.free(self.realpath);
6072 path_and_tree.allocator.destroy(self);
6173 },
6274 Data.ScopeAndComp => |scope_and_comp| {
6375 scope_and_comp.tree_scope.base.deref(scope_and_comp.compilation);
6476 scope_and_comp.compilation.gpa().free(self.text);
77 scope_and_comp.compilation.gpa().free(self.realpath);
6578 scope_and_comp.compilation.gpa().destroy(self);
6679 },
6780 }
......@@ -69,6 +82,7 @@ pub const Msg = struct {
6982
7083 fn getAllocator(self: *const Msg) *mem.Allocator {
7184 switch (self.data) {
85 Data.Cli => |cli| return cli.allocator,
7286 Data.PathAndTree => |path_and_tree| {
7387 return path_and_tree.allocator;
7488 },
......@@ -78,19 +92,9 @@ pub const Msg = struct {
7892 }
7993 }
8094
81 pub fn getRealPath(self: *const Msg) []const u8 {
82 switch (self.data) {
83 Data.PathAndTree => |path_and_tree| {
84 return path_and_tree.realpath;
85 },
86 Data.ScopeAndComp => |scope_and_comp| {
87 return scope_and_comp.tree_scope.root().realpath;
88 },
89 }
90 }
91
9295 pub fn getTree(self: *const Msg) *ast.Tree {
9396 switch (self.data) {
97 Data.Cli => unreachable,
9498 Data.PathAndTree => |path_and_tree| {
9599 return path_and_tree.tree;
96100 },
......@@ -100,16 +104,28 @@ pub const Msg = struct {
100104 }
101105 }
102106
107 pub fn getSpan(self: *const Msg) Span {
108 return switch (self.data) {
109 Data.Cli => unreachable,
110 Data.PathAndTree => |path_and_tree| path_and_tree.span,
111 Data.ScopeAndComp => |scope_and_comp| scope_and_comp.span,
112 };
113 }
114
103115 /// Takes ownership of text
104116 /// References tree_scope, and derefs when the msg is freed
105117 pub fn createFromScope(comp: *Compilation, tree_scope: *Scope.AstTree, span: Span, text: []u8) !*Msg {
118 const realpath = try mem.dupe(comp.gpa(), u8, tree_scope.root().realpath);
119 errdefer comp.gpa().free(realpath);
120
106121 const msg = try comp.gpa().create(Msg{
107122 .text = text,
108 .span = span,
123 .realpath = realpath,
109124 .data = Data{
110125 .ScopeAndComp = ScopeAndComp{
111126 .tree_scope = tree_scope,
112127 .compilation = comp,
128 .span = span,
113129 },
114130 },
115131 });
......@@ -117,6 +133,22 @@ pub const Msg = struct {
117133 return msg;
118134 }
119135
136 /// Caller owns returned Msg and must free with `allocator`
137 /// allocator will additionally be used for printing messages later.
138 pub fn createFromCli(comp: *Compilation, realpath: []const u8, text: []u8) !*Msg {
139 const realpath_copy = try mem.dupe(comp.gpa(), u8, realpath);
140 errdefer comp.gpa().free(realpath_copy);
141
142 const msg = try comp.gpa().create(Msg{
143 .text = text,
144 .realpath = realpath_copy,
145 .data = Data{
146 .Cli = Cli{ .allocator = comp.gpa() },
147 },
148 });
149 return msg;
150 }
151
120152 pub fn createFromParseErrorAndScope(
121153 comp: *Compilation,
122154 tree_scope: *Scope.AstTree,
......@@ -126,19 +158,23 @@ pub const Msg = struct {
126158 var text_buf = try std.Buffer.initSize(comp.gpa(), 0);
127159 defer text_buf.deinit();
128160
161 const realpath_copy = try mem.dupe(comp.gpa(), u8, tree_scope.root().realpath);
162 errdefer comp.gpa().free(realpath_copy);
163
129164 var out_stream = &std.io.BufferOutStream.init(&text_buf).stream;
130165 try parse_error.render(&tree_scope.tree.tokens, out_stream);
131166
132167 const msg = try comp.gpa().create(Msg{
133168 .text = undefined,
134 .span = Span{
135 .first = loc_token,
136 .last = loc_token,
137 },
169 .realpath = realpath_copy,
138170 .data = Data{
139171 .ScopeAndComp = ScopeAndComp{
140172 .tree_scope = tree_scope,
141173 .compilation = comp,
174 .span = Span{
175 .first = loc_token,
176 .last = loc_token,
177 },
142178 },
143179 },
144180 });
......@@ -161,22 +197,25 @@ pub const Msg = struct {
161197 var text_buf = try std.Buffer.initSize(allocator, 0);
162198 defer text_buf.deinit();
163199
200 const realpath_copy = try mem.dupe(allocator, u8, realpath);
201 errdefer allocator.free(realpath_copy);
202
164203 var out_stream = &std.io.BufferOutStream.init(&text_buf).stream;
165204 try parse_error.render(&tree.tokens, out_stream);
166205
167206 const msg = try allocator.create(Msg{
168207 .text = undefined,
208 .realpath = realpath_copy,
169209 .data = Data{
170210 .PathAndTree = PathAndTree{
171211 .allocator = allocator,
172 .realpath = realpath,
173212 .tree = tree,
213 .span = Span{
214 .first = loc_token,
215 .last = loc_token,
216 },
174217 },
175218 },
176 .span = Span{
177 .first = loc_token,
178 .last = loc_token,
179 },
180219 });
181220 msg.text = text_buf.toOwnedSlice();
182221 errdefer allocator.destroy(msg);
......@@ -185,20 +224,28 @@ pub const Msg = struct {
185224 }
186225
187226 pub fn printToStream(msg: *const Msg, stream: var, color_on: bool) !void {
227 switch (msg.data) {
228 Data.Cli => {
229 try stream.print("{}:-:-: error: {}\n", msg.realpath, msg.text);
230 return;
231 },
232 else => {},
233 }
234
188235 const allocator = msg.getAllocator();
189 const realpath = msg.getRealPath();
190236 const tree = msg.getTree();
191237
192238 const cwd = try os.getCwd(allocator);
193239 defer allocator.free(cwd);
194240
195 const relpath = try os.path.relative(allocator, cwd, realpath);
241 const relpath = try os.path.relative(allocator, cwd, msg.realpath);
196242 defer allocator.free(relpath);
197243
198 const path = if (relpath.len < realpath.len) relpath else realpath;
244 const path = if (relpath.len < msg.realpath.len) relpath else msg.realpath;
245 const span = msg.getSpan();
199246
200 const first_token = tree.tokens.at(msg.span.first);
201 const last_token = tree.tokens.at(msg.span.last);
247 const first_token = tree.tokens.at(span.first);
248 const last_token = tree.tokens.at(span.last);
202249 const start_loc = tree.tokenLocationPtr(0, first_token);
203250 const end_loc = tree.tokenLocationPtr(first_token.end, last_token);
204251 if (!color_on) {
src-self-hosted/ir.zig+23-20
......@@ -961,6 +961,7 @@ pub const Code = struct {
961961 basic_block_list: std.ArrayList(*BasicBlock),
962962 arena: std.heap.ArenaAllocator,
963963 return_type: ?*Type,
964 tree_scope: *Scope.AstTree,
964965
965966 /// allocator is comp.gpa()
966967 pub fn destroy(self: *Code, allocator: *Allocator) void {
......@@ -990,14 +991,14 @@ pub const Code = struct {
990991 return ret_value.val.KnownValue.getRef();
991992 }
992993 try comp.addCompileError(
993 ret_value.scope.findRoot(),
994 self.tree_scope,
994995 ret_value.span,
995996 "unable to evaluate constant expression",
996997 );
997998 return error.SemanticAnalysisFailed;
998999 } else if (inst.hasSideEffects()) {
9991000 try comp.addCompileError(
1000 inst.scope.findRoot(),
1001 self.tree_scope,
10011002 inst.span,
10021003 "unable to evaluate constant expression",
10031004 );
......@@ -1013,25 +1014,24 @@ pub const Builder = struct {
10131014 code: *Code,
10141015 current_basic_block: *BasicBlock,
10151016 next_debug_id: usize,
1016 root_scope: *Scope.Root,
10171017 is_comptime: bool,
10181018 is_async: bool,
10191019 begin_scope: ?*Scope,
10201020
10211021 pub const Error = Analyze.Error;
10221022
1023 pub fn init(comp: *Compilation, root_scope: *Scope.Root, begin_scope: ?*Scope) !Builder {
1023 pub fn init(comp: *Compilation, tree_scope: *Scope.AstTree, begin_scope: ?*Scope) !Builder {
10241024 const code = try comp.gpa().create(Code{
10251025 .basic_block_list = undefined,
10261026 .arena = std.heap.ArenaAllocator.init(comp.gpa()),
10271027 .return_type = null,
1028 .tree_scope = tree_scope,
10281029 });
10291030 code.basic_block_list = std.ArrayList(*BasicBlock).init(&code.arena.allocator);
10301031 errdefer code.destroy(comp.gpa());
10311032
10321033 return Builder{
10331034 .comp = comp,
1034 .root_scope = root_scope,
10351035 .current_basic_block = undefined,
10361036 .code = code,
10371037 .next_debug_id = 0,
......@@ -1292,6 +1292,7 @@ pub const Builder = struct {
12921292 Scope.Id.FnDef => return false,
12931293 Scope.Id.Decls => unreachable,
12941294 Scope.Id.Root => unreachable,
1295 Scope.Id.AstTree => unreachable,
12951296 Scope.Id.Block,
12961297 Scope.Id.Defer,
12971298 Scope.Id.DeferExpr,
......@@ -1302,7 +1303,7 @@ pub const Builder = struct {
13021303 }
13031304
13041305 pub fn genIntLit(irb: *Builder, int_lit: *ast.Node.IntegerLiteral, scope: *Scope) !*Inst {
1305 const int_token = irb.root_scope.tree.tokenSlice(int_lit.token);
1306 const int_token = irb.code.tree_scope.tree.tokenSlice(int_lit.token);
13061307
13071308 var base: u8 = undefined;
13081309 var rest: []const u8 = undefined;
......@@ -1341,7 +1342,7 @@ pub const Builder = struct {
13411342 }
13421343
13431344 pub async fn genStrLit(irb: *Builder, str_lit: *ast.Node.StringLiteral, scope: *Scope) !*Inst {
1344 const str_token = irb.root_scope.tree.tokenSlice(str_lit.token);
1345 const str_token = irb.code.tree_scope.tree.tokenSlice(str_lit.token);
13451346 const src_span = Span.token(str_lit.token);
13461347
13471348 var bad_index: usize = undefined;
......@@ -1349,7 +1350,7 @@ pub const Builder = struct {
13491350 error.OutOfMemory => return error.OutOfMemory,
13501351 error.InvalidCharacter => {
13511352 try irb.comp.addCompileError(
1352 irb.root_scope,
1353 irb.code.tree_scope,
13531354 src_span,
13541355 "invalid character in string literal: '{c}'",
13551356 str_token[bad_index],
......@@ -1427,7 +1428,7 @@ pub const Builder = struct {
14271428
14281429 if (statement_node.cast(ast.Node.Defer)) |defer_node| {
14291430 // defer starts a new scope
1430 const defer_token = irb.root_scope.tree.tokens.at(defer_node.defer_token);
1431 const defer_token = irb.code.tree_scope.tree.tokens.at(defer_node.defer_token);
14311432 const kind = switch (defer_token.id) {
14321433 Token.Id.Keyword_defer => Scope.Defer.Kind.ScopeExit,
14331434 Token.Id.Keyword_errdefer => Scope.Defer.Kind.ErrorExit,
......@@ -1513,7 +1514,7 @@ pub const Builder = struct {
15131514 const src_span = Span.token(control_flow_expr.ltoken);
15141515 if (scope.findFnDef() == null) {
15151516 try irb.comp.addCompileError(
1516 irb.root_scope,
1517 irb.code.tree_scope,
15171518 src_span,
15181519 "return expression outside function definition",
15191520 );
......@@ -1523,7 +1524,7 @@ pub const Builder = struct {
15231524 if (scope.findDeferExpr()) |scope_defer_expr| {
15241525 if (!scope_defer_expr.reported_err) {
15251526 try irb.comp.addCompileError(
1526 irb.root_scope,
1527 irb.code.tree_scope,
15271528 src_span,
15281529 "cannot return from defer expression",
15291530 );
......@@ -1599,7 +1600,7 @@ pub const Builder = struct {
15991600
16001601 pub async fn genIdentifier(irb: *Builder, identifier: *ast.Node.Identifier, scope: *Scope, lval: LVal) !*Inst {
16011602 const src_span = Span.token(identifier.token);
1602 const name = irb.root_scope.tree.tokenSlice(identifier.token);
1603 const name = irb.code.tree_scope.tree.tokenSlice(identifier.token);
16031604
16041605 //if (buf_eql_str(variable_name, "_") && lval == LValPtr) {
16051606 // IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, node);
......@@ -1622,7 +1623,7 @@ pub const Builder = struct {
16221623 }
16231624 } else |err| switch (err) {
16241625 error.Overflow => {
1625 try irb.comp.addCompileError(irb.root_scope, src_span, "integer too large");
1626 try irb.comp.addCompileError(irb.code.tree_scope, src_span, "integer too large");
16261627 return error.SemanticAnalysisFailed;
16271628 },
16281629 error.OutOfMemory => return error.OutOfMemory,
......@@ -1656,7 +1657,7 @@ pub const Builder = struct {
16561657 // TODO put a variable of same name with invalid type in global scope
16571658 // so that future references to this same name will find a variable with an invalid type
16581659
1659 try irb.comp.addCompileError(irb.root_scope, src_span, "unknown identifier '{}'", name);
1660 try irb.comp.addCompileError(irb.code.tree_scope, src_span, "unknown identifier '{}'", name);
16601661 return error.SemanticAnalysisFailed;
16611662 }
16621663
......@@ -1689,6 +1690,7 @@ pub const Builder = struct {
16891690 => scope = scope.parent orelse break,
16901691
16911692 Scope.Id.DeferExpr => unreachable,
1693 Scope.Id.AstTree => unreachable,
16921694 }
16931695 }
16941696 return result;
......@@ -1740,6 +1742,7 @@ pub const Builder = struct {
17401742 => scope = scope.parent orelse return is_noreturn,
17411743
17421744 Scope.Id.DeferExpr => unreachable,
1745 Scope.Id.AstTree => unreachable,
17431746 }
17441747 }
17451748 }
......@@ -1968,8 +1971,8 @@ const Analyze = struct {
19681971 OutOfMemory,
19691972 };
19701973
1971 pub fn init(comp: *Compilation, root_scope: *Scope.Root, explicit_return_type: ?*Type) !Analyze {
1972 var irb = try Builder.init(comp, root_scope, null);
1974 pub fn init(comp: *Compilation, tree_scope: *Scope.AstTree, explicit_return_type: ?*Type) !Analyze {
1975 var irb = try Builder.init(comp, tree_scope, null);
19731976 errdefer irb.abort();
19741977
19751978 return Analyze{
......@@ -2047,7 +2050,7 @@ const Analyze = struct {
20472050 }
20482051
20492052 fn addCompileError(self: *Analyze, span: Span, comptime fmt: []const u8, args: ...) !void {
2050 return self.irb.comp.addCompileError(self.irb.root_scope, span, fmt, args);
2053 return self.irb.comp.addCompileError(self.irb.code.tree_scope, span, fmt, args);
20512054 }
20522055
20532056 fn resolvePeerTypes(self: *Analyze, expected_type: ?*Type, peers: []const *Inst) Analyze.Error!*Type {
......@@ -2535,9 +2538,10 @@ const Analyze = struct {
25352538pub async fn gen(
25362539 comp: *Compilation,
25372540 body_node: *ast.Node,
2541 tree_scope: *Scope.AstTree,
25382542 scope: *Scope,
25392543) !*Code {
2540 var irb = try Builder.init(comp, scope.findRoot(), scope);
2544 var irb = try Builder.init(comp, tree_scope, scope);
25412545 errdefer irb.abort();
25422546
25432547 const entry_block = try irb.createBasicBlock(scope, c"Entry");
......@@ -2555,9 +2559,8 @@ pub async fn gen(
25552559
25562560pub async fn analyze(comp: *Compilation, old_code: *Code, expected_type: ?*Type) !*Code {
25572561 const old_entry_bb = old_code.basic_block_list.at(0);
2558 const root_scope = old_entry_bb.scope.findRoot();
25592562
2560 var ira = try Analyze.init(comp, root_scope, expected_type);
2563 var ira = try Analyze.init(comp, old_code.tree_scope, expected_type);
25612564 errdefer ira.abort();
25622565
25632566 const new_entry_bb = try ira.getNewBasicBlock(old_entry_bb, null);
src-self-hosted/main.zig+33-27
......@@ -24,6 +24,8 @@ var stderr_file: os.File = undefined;
2424var stderr: *io.OutStream(io.FileOutStream.Error) = undefined;
2525var stdout: *io.OutStream(io.FileOutStream.Error) = undefined;
2626
27const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB
28
2729const usage =
2830 \\usage: zig [command] [options]
2931 \\
......@@ -71,26 +73,26 @@ pub fn main() !void {
7173 }
7274
7375 const commands = []Command{
74 //Command{
75 // .name = "build-exe",
76 // .exec = cmdBuildExe,
77 //},
78 //Command{
79 // .name = "build-lib",
80 // .exec = cmdBuildLib,
81 //},
82 //Command{
83 // .name = "build-obj",
84 // .exec = cmdBuildObj,
85 //},
76 Command{
77 .name = "build-exe",
78 .exec = cmdBuildExe,
79 },
80 Command{
81 .name = "build-lib",
82 .exec = cmdBuildLib,
83 },
84 Command{
85 .name = "build-obj",
86 .exec = cmdBuildObj,
87 },
8688 Command{
8789 .name = "fmt",
8890 .exec = cmdFmt,
8991 },
90 //Command{
91 // .name = "libc",
92 // .exec = cmdLibC,
93 //},
92 Command{
93 .name = "libc",
94 .exec = cmdLibC,
95 },
9496 Command{
9597 .name = "targets",
9698 .exec = cmdTargets,
......@@ -472,16 +474,21 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
472474}
473475
474476async fn processBuildEvents(comp: *Compilation, color: errmsg.Color) void {
477 var count: usize = 0;
475478 while (true) {
476479 // TODO directly awaiting async should guarantee memory allocation elision
477480 const build_event = await (async comp.events.get() catch unreachable);
481 count += 1;
478482
479483 switch (build_event) {
480 Compilation.Event.Ok => {},
484 Compilation.Event.Ok => {
485 stderr.print("Build {} succeeded\n", count) catch os.exit(1);
486 },
481487 Compilation.Event.Error => |err| {
482 stderr.print("build failed: {}\n", @errorName(err)) catch os.exit(1);
488 stderr.print("Build {} failed: {}\n", count, @errorName(err)) catch os.exit(1);
483489 },
484490 Compilation.Event.Fail => |msgs| {
491 stderr.print("Build {} compile errors:\n", count) catch os.exit(1);
485492 for (msgs) |msg| {
486493 defer msg.destroy();
487494 msg.printToFile(&stderr_file, color) catch os.exit(1);
......@@ -614,7 +621,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
614621 var stdin_file = try io.getStdIn();
615622 var stdin = io.FileInStream.init(&stdin_file);
616623
617 const source_code = try stdin.stream.readAllAlloc(allocator, @maxValue(usize));
624 const source_code = try stdin.stream.readAllAlloc(allocator, max_src_size);
618625 defer allocator.free(source_code);
619626
620627 var tree = std.zig.parse(allocator, source_code) catch |err| {
......@@ -697,12 +704,6 @@ async fn asyncFmtMain(
697704 suspend {
698705 resume @handle();
699706 }
700 // Things we need to make event-based:
701 // * opening the file in the first place - the open()
702 // * read()
703 // * readdir()
704 // * the actual parsing and rendering
705 // * rename()
706707 var fmt = Fmt{
707708 .seen = event.Locked(Fmt.SeenMap).init(loop, Fmt.SeenMap.init(loop.allocator)),
708709 .any_error = false,
......@@ -714,7 +715,10 @@ async fn asyncFmtMain(
714715 for (flags.positionals.toSliceConst()) |file_path| {
715716 try group.call(fmtPath, &fmt, file_path);
716717 }
717 return await (async group.wait() catch unreachable);
718 try await (async group.wait() catch unreachable);
719 if (fmt.any_error) {
720 os.exit(1);
721 }
718722}
719723
720724async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8) FmtError!void {
......@@ -731,9 +735,10 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8) FmtError!void {
731735 const source_code = (await try async event.fs.readFile(
732736 fmt.loop,
733737 file_path,
734 2 * 1024 * 1024 * 1024,
738 max_src_size,
735739 )) catch |err| switch (err) {
736740 error.IsDir => {
741 // TODO make event based (and dir.next())
737742 var dir = try std.os.Dir.open(fmt.loop.allocator, file_path);
738743 defer dir.close();
739744
......@@ -774,6 +779,7 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8) FmtError!void {
774779 return;
775780 }
776781
782 // TODO make this evented
777783 const baf = try io.BufferedAtomicFile.create(fmt.loop.allocator, file_path);
778784 defer baf.destroy();
779785
src-self-hosted/scope.zig+8-4
......@@ -63,6 +63,8 @@ pub const Scope = struct {
6363 Id.CompTime,
6464 Id.Var,
6565 => scope = scope.parent.?,
66
67 Id.AstTree => unreachable,
6668 }
6769 }
6870 }
......@@ -83,6 +85,8 @@ pub const Scope = struct {
8385 Id.Root,
8486 Id.Var,
8587 => scope = scope.parent orelse return null,
88
89 Id.AstTree => unreachable,
8690 }
8791 }
8892 }
......@@ -132,6 +136,7 @@ pub const Scope = struct {
132136 }
133137
134138 pub fn destroy(self: *Root, comp: *Compilation) void {
139 // TODO comp.fs_watch.removeFile(self.realpath);
135140 self.decls.base.deref(comp);
136141 comp.gpa().free(self.realpath);
137142 comp.gpa().destroy(self);
......@@ -144,13 +149,13 @@ pub const Scope = struct {
144149
145150 /// Creates a scope with 1 reference
146151 /// Takes ownership of tree, will deinit and destroy when done.
147 pub fn create(comp: *Compilation, tree: *ast.Tree, root: *Root) !*AstTree {
148 const self = try comp.gpa().createOne(Root);
152 pub fn create(comp: *Compilation, tree: *ast.Tree, root_scope: *Root) !*AstTree {
153 const self = try comp.gpa().createOne(AstTree);
149154 self.* = AstTree{
150155 .base = undefined,
151156 .tree = tree,
152157 };
153 self.base.init(Id.AstTree, &root.base);
158 self.base.init(Id.AstTree, &root_scope.base);
154159
155160 return self;
156161 }
......@@ -181,7 +186,6 @@ pub const Scope = struct {
181186 self.* = Decls{
182187 .base = undefined,
183188 .table = event.RwLocked(Decl.Table).init(comp.loop, Decl.Table.init(comp.gpa())),
184 .name_future = event.Future(void).init(comp.loop),
185189 };
186190 self.base.init(Id.Decls, parent);
187191 return self;
src-self-hosted/test.zig+4-3
......@@ -212,9 +212,10 @@ pub const TestContext = struct {
212212 Compilation.Event.Fail => |msgs| {
213213 assertOrPanic(msgs.len != 0);
214214 for (msgs) |msg| {
215 if (mem.endsWith(u8, msg.getRealPath(), path) and mem.eql(u8, msg.text, text)) {
216 const first_token = msg.getTree().tokens.at(msg.span.first);
217 const last_token = msg.getTree().tokens.at(msg.span.first);
215 if (mem.endsWith(u8, msg.realpath, path) and mem.eql(u8, msg.text, text)) {
216 const span = msg.getSpan();
217 const first_token = msg.getTree().tokens.at(span.first);
218 const last_token = msg.getTree().tokens.at(span.first);
218219 const start_loc = msg.getTree().tokenLocationPtr(0, first_token);
219220 if (start_loc.line + 1 == line and start_loc.column + 1 == column) {
220221 return;
src/ir.cpp+10-1
......@@ -9614,6 +9614,9 @@ static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, Un
96149614 case ConstValSpecialStatic:
96159615 return &value->value;
96169616 case ConstValSpecialRuntime:
9617 if (!type_has_bits(value->value.type)) {
9618 return &value->value;
9619 }
96179620 ir_add_error(ira, value, buf_sprintf("unable to evaluate constant expression"));
96189621 return nullptr;
96199622 case ConstValSpecialUndef:
......@@ -16115,8 +16118,14 @@ static TypeTableEntry *ir_analyze_container_init_fields_union(IrAnalyze *ira, Ir
1611516118 if (casted_field_value == ira->codegen->invalid_instruction)
1611616119 return ira->codegen->builtin_types.entry_invalid;
1611716120
16121 type_ensure_zero_bits_known(ira->codegen, casted_field_value->value.type);
16122 if (type_is_invalid(casted_field_value->value.type))
16123 return ira->codegen->builtin_types.entry_invalid;
16124
1611816125 bool is_comptime = ir_should_inline(ira->new_irb.exec, instruction->scope);
16119 if (is_comptime || casted_field_value->value.special != ConstValSpecialRuntime) {
16126 if (is_comptime || casted_field_value->value.special != ConstValSpecialRuntime ||
16127 !type_has_bits(casted_field_value->value.type))
16128 {
1612016129 ConstExprValue *field_val = ir_resolve_const(ira, casted_field_value, UndefOk);
1612116130 if (!field_val)
1612216131 return ira->codegen->builtin_types.entry_invalid;
std/build.zig+59-50
......@@ -424,60 +424,69 @@ pub const Builder = struct {
424424 return mode;
425425 }
426426
427 pub fn addUserInputOption(self: *Builder, name: []const u8, value: []const u8) bool {
428 if (self.user_input_options.put(name, UserInputOption{
429 .name = name,
430 .value = UserValue{ .Scalar = value },
431 .used = false,
432 }) catch unreachable) |*prev_value| {
433 // option already exists
434 switch (prev_value.value) {
435 UserValue.Scalar => |s| {
436 // turn it into a list
437 var list = ArrayList([]const u8).init(self.allocator);
438 list.append(s) catch unreachable;
439 list.append(value) catch unreachable;
440 _ = self.user_input_options.put(name, UserInputOption{
441 .name = name,
442 .value = UserValue{ .List = list },
443 .used = false,
444 }) catch unreachable;
445 },
446 UserValue.List => |*list| {
447 // append to the list
448 list.append(value) catch unreachable;
449 _ = self.user_input_options.put(name, UserInputOption{
450 .name = name,
451 .value = UserValue{ .List = list.* },
452 .used = false,
453 }) catch unreachable;
454 },
455 UserValue.Flag => {
456 warn("Option '-D{}={}' conflicts with flag '-D{}'.\n", name, value, name);
457 return true;
458 },
459 }
427 pub fn addUserInputOption(self: *Builder, name: []const u8, value: []const u8) !bool {
428 const gop = try self.user_input_options.getOrPut(name);
429 if (!gop.found_existing) {
430 gop.kv.value = UserInputOption{
431 .name = name,
432 .value = UserValue{ .Scalar = value },
433 .used = false,
434 };
435 return false;
436 }
437
438 // option already exists
439 switch (gop.kv.value.value) {
440 UserValue.Scalar => |s| {
441 // turn it into a list
442 var list = ArrayList([]const u8).init(self.allocator);
443 list.append(s) catch unreachable;
444 list.append(value) catch unreachable;
445 _ = self.user_input_options.put(name, UserInputOption{
446 .name = name,
447 .value = UserValue{ .List = list },
448 .used = false,
449 }) catch unreachable;
450 },
451 UserValue.List => |*list| {
452 // append to the list
453 list.append(value) catch unreachable;
454 _ = self.user_input_options.put(name, UserInputOption{
455 .name = name,
456 .value = UserValue{ .List = list.* },
457 .used = false,
458 }) catch unreachable;
459 },
460 UserValue.Flag => {
461 warn("Option '-D{}={}' conflicts with flag '-D{}'.\n", name, value, name);
462 return true;
463 },
460464 }
461465 return false;
462466 }
463467
464 pub fn addUserInputFlag(self: *Builder, name: []const u8) bool {
465 if (self.user_input_options.put(name, UserInputOption{
466 .name = name,
467 .value = UserValue{ .Flag = {} },
468 .used = false,
469 }) catch unreachable) |*prev_value| {
470 switch (prev_value.value) {
471 UserValue.Scalar => |s| {
472 warn("Flag '-D{}' conflicts with option '-D{}={}'.\n", name, name, s);
473 return true;
474 },
475 UserValue.List => {
476 warn("Flag '-D{}' conflicts with multiple options of the same name.\n", name);
477 return true;
478 },
479 UserValue.Flag => {},
480 }
468 pub fn addUserInputFlag(self: *Builder, name: []const u8) !bool {
469 const gop = try self.user_input_options.getOrPut(name);
470 if (!gop.found_existing) {
471 gop.kv.value = UserInputOption{
472 .name = name,
473 .value = UserValue{ .Flag = {} },
474 .used = false,
475 };
476 return false;
477 }
478
479 // option already exists
480 switch (gop.kv.value.value) {
481 UserValue.Scalar => |s| {
482 warn("Flag '-D{}' conflicts with option '-D{}={}'.\n", name, name, s);
483 return true;
484 },
485 UserValue.List => {
486 warn("Flag '-D{}' conflicts with multiple options of the same name.\n", name);
487 return true;
488 },
489 UserValue.Flag => {},
481490 }
482491 return false;
483492 }
std/event/fs.zig+180-94
......@@ -367,109 +367,193 @@ pub async fn readFile(loop: *event.Loop, file_path: []const u8, max_size: usize)
367367 }
368368}
369369
370pub const Watch = struct {
371 channel: *event.Channel(Event),
372 putter: promise,
373
374 pub const Event = union(enum) {
375 CloseWrite,
376 Err: Error,
377 };
378
379 pub const Error = error{
380 UserResourceLimitReached,
381 SystemResources,
382 };
383
384 pub fn destroy(self: *Watch) void {
385 // TODO https://github.com/ziglang/zig/issues/1261
386 cancel self.putter;
387 }
388};
389
390pub fn watchFile(loop: *event.Loop, file_path: []const u8) !*Watch {
391 const path_with_null = try std.cstr.addNullByte(loop.allocator, file_path);
392 defer loop.allocator.free(path_with_null);
370pub fn Watch(comptime V: type) type {
371 return struct {
372 channel: *event.Channel(Event),
373 putter: promise,
374 wd_table: WdTable,
375 table_lock: event.Lock,
376 inotify_fd: i32,
377
378 const WdTable = std.AutoHashMap(i32, Dir);
379 const FileTable = std.AutoHashMap([]const u8, V);
380
381 const Self = this;
382
383 const Dir = struct {
384 dirname: []const u8,
385 file_table: FileTable,
386 };
393387
394 const inotify_fd = try os.linuxINotifyInit1(os.linux.IN_NONBLOCK | os.linux.IN_CLOEXEC);
395 errdefer os.close(inotify_fd);
388 pub const Event = union(enum) {
389 CloseWrite: V,
390 Err: Error,
396391
397 const wd = try os.linuxINotifyAddWatchC(inotify_fd, path_with_null.ptr, os.linux.IN_CLOSE_WRITE);
398 errdefer os.close(wd);
392 pub const Error = error{
393 UserResourceLimitReached,
394 SystemResources,
395 };
396 };
399397
400 const channel = try event.Channel(Watch.Event).create(loop, 0);
401 errdefer channel.destroy();
398 pub fn create(loop: *event.Loop, event_buf_count: usize) !*Self {
399 const inotify_fd = try os.linuxINotifyInit1(os.linux.IN_NONBLOCK | os.linux.IN_CLOEXEC);
400 errdefer os.close(inotify_fd);
402401
403 var result: *Watch = undefined;
404 _ = try async<loop.allocator> watchEventPutter(inotify_fd, wd, channel, &result);
405 return result;
406}
407
408async fn watchEventPutter(inotify_fd: i32, wd: i32, channel: *event.Channel(Watch.Event), out_watch: **Watch) void {
409 // TODO https://github.com/ziglang/zig/issues/1194
410 suspend {
411 resume @handle();
412 }
402 const channel = try event.Channel(Self.Event).create(loop, event_buf_count);
403 errdefer channel.destroy();
413404
414 var watch = Watch{
415 .putter = @handle(),
416 .channel = channel,
417 };
418 out_watch.* = &watch;
405 var result: *Self = undefined;
406 _ = try async<loop.allocator> eventPutter(inotify_fd, channel, &result);
407 return result;
408 }
419409
420 const loop = channel.loop;
421 loop.beginOneEvent();
410 pub fn destroy(self: *Self) void {
411 cancel self.putter;
412 }
422413
423 defer {
424 channel.destroy();
425 os.close(wd);
426 os.close(inotify_fd);
427 loop.finishOneEvent();
428 }
414 pub async fn addFile(self: *Self, file_path: []const u8, value: V) !?V {
415 const dirname = os.path.dirname(file_path) orelse ".";
416 const dirname_with_null = try std.cstr.addNullByte(self.channel.loop.allocator, dirname);
417 var dirname_with_null_consumed = false;
418 defer if (!dirname_with_null_consumed) self.channel.loop.allocator.free(dirname_with_null);
419
420 const basename = os.path.basename(file_path);
421 const basename_with_null = try std.cstr.addNullByte(self.channel.loop.allocator, basename);
422 var basename_with_null_consumed = false;
423 defer if (!basename_with_null_consumed) self.channel.loop.allocator.free(basename_with_null);
424
425 const wd = try os.linuxINotifyAddWatchC(
426 self.inotify_fd,
427 dirname_with_null.ptr,
428 os.linux.IN_CLOSE_WRITE | os.linux.IN_ONLYDIR | os.linux.IN_EXCL_UNLINK,
429 );
430 // wd is either a newly created watch or an existing one.
431
432 const held = await (async self.table_lock.acquire() catch unreachable);
433 defer held.release();
434
435 const gop = try self.wd_table.getOrPut(wd);
436 if (!gop.found_existing) {
437 gop.kv.value = Dir{
438 .dirname = dirname_with_null,
439 .file_table = FileTable.init(self.channel.loop.allocator),
440 };
441 dirname_with_null_consumed = true;
442 }
443 const dir = &gop.kv.value;
444
445 const file_table_gop = try dir.file_table.getOrPut(basename_with_null);
446 if (file_table_gop.found_existing) {
447 const prev_value = file_table_gop.kv.value;
448 file_table_gop.kv.value = value;
449 return prev_value;
450 } else {
451 file_table_gop.kv.value = value;
452 basename_with_null_consumed = true;
453 return null;
454 }
455 }
429456
430 var event_buf: [4096]u8 align(@alignOf(os.linux.inotify_event)) = undefined;
457 pub async fn removeFile(self: *Self, file_path: []const u8) ?V {
458 @panic("TODO");
459 }
431460
432 while (true) {
433 const rc = os.linux.read(inotify_fd, &event_buf, event_buf.len);
434 const errno = os.linux.getErrno(rc);
435 switch (errno) {
436 0 => {
437 // can't use @bytesToSlice because of the special variable length name field
438 var ptr = event_buf[0..].ptr;
439 const end_ptr = ptr + event_buf.len;
440 var ev: *os.linux.inotify_event = undefined;
441 while (@ptrToInt(ptr) < @ptrToInt(end_ptr)) : (ptr += @sizeOf(os.linux.inotify_event) + ev.len) {
442 ev = @ptrCast(*os.linux.inotify_event, ptr);
443 if (ev.mask & os.linux.IN_CLOSE_WRITE == os.linux.IN_CLOSE_WRITE) {
444 await (async channel.put(Watch.Event.CloseWrite) catch unreachable);
461 async fn eventPutter(inotify_fd: i32, channel: *event.Channel(Event), out_watch: **Self) void {
462 // TODO https://github.com/ziglang/zig/issues/1194
463 suspend {
464 resume @handle();
465 }
466
467 const loop = channel.loop;
468
469 var watch = Self{
470 .putter = @handle(),
471 .channel = channel,
472 .wd_table = WdTable.init(loop.allocator),
473 .table_lock = event.Lock.init(loop),
474 .inotify_fd = inotify_fd,
475 };
476 out_watch.* = &watch;
477
478 loop.beginOneEvent();
479
480 defer {
481 watch.table_lock.deinit();
482 {
483 var wd_it = watch.wd_table.iterator();
484 while (wd_it.next()) |wd_entry| {
485 var file_it = wd_entry.value.file_table.iterator();
486 while (file_it.next()) |file_entry| {
487 loop.allocator.free(file_entry.key);
488 }
489 loop.allocator.free(wd_entry.value.dirname);
445490 }
446491 }
447 },
448 os.linux.EINTR => continue,
449 os.linux.EINVAL => unreachable,
450 os.linux.EFAULT => unreachable,
451 os.linux.EAGAIN => {
452 (await (async loop.linuxWaitFd(
453 inotify_fd,
454 os.linux.EPOLLET | os.linux.EPOLLIN,
455 ) catch unreachable)) catch |err| {
456 const transformed_err = switch (err) {
457 error.InvalidFileDescriptor => unreachable,
458 error.FileDescriptorAlreadyPresentInSet => unreachable,
459 error.InvalidSyscall => unreachable,
460 error.OperationCausesCircularLoop => unreachable,
461 error.FileDescriptorNotRegistered => unreachable,
462 error.SystemResources => error.SystemResources,
463 error.UserResourceLimitReached => error.UserResourceLimitReached,
464 error.FileDescriptorIncompatibleWithEpoll => unreachable,
465 error.Unexpected => unreachable,
466 };
467 await (async channel.put(Watch.Event{ .Err = transformed_err }) catch unreachable);
468 };
469 },
470 else => unreachable,
492 loop.finishOneEvent();
493 os.close(inotify_fd);
494 channel.destroy();
495 }
496
497 var event_buf: [4096]u8 align(@alignOf(os.linux.inotify_event)) = undefined;
498
499 while (true) {
500 const rc = os.linux.read(inotify_fd, &event_buf, event_buf.len);
501 const errno = os.linux.getErrno(rc);
502 switch (errno) {
503 0 => {
504 // can't use @bytesToSlice because of the special variable length name field
505 var ptr = event_buf[0..].ptr;
506 const end_ptr = ptr + event_buf.len;
507 var ev: *os.linux.inotify_event = undefined;
508 while (@ptrToInt(ptr) < @ptrToInt(end_ptr)) : (ptr += @sizeOf(os.linux.inotify_event) + ev.len) {
509 ev = @ptrCast(*os.linux.inotify_event, ptr);
510 if (ev.mask & os.linux.IN_CLOSE_WRITE == os.linux.IN_CLOSE_WRITE) {
511 const basename_ptr = ptr + @sizeOf(os.linux.inotify_event);
512 const basename_with_null = basename_ptr[0 .. std.cstr.len(basename_ptr) + 1];
513 const user_value = blk: {
514 const held = await (async watch.table_lock.acquire() catch unreachable);
515 defer held.release();
516
517 const dir = &watch.wd_table.get(ev.wd).?.value;
518 if (dir.file_table.get(basename_with_null)) |entry| {
519 break :blk entry.value;
520 } else {
521 break :blk null;
522 }
523 };
524 if (user_value) |v| {
525 await (async channel.put(Self.Event{ .CloseWrite = v }) catch unreachable);
526 }
527 }
528 }
529 },
530 os.linux.EINTR => continue,
531 os.linux.EINVAL => unreachable,
532 os.linux.EFAULT => unreachable,
533 os.linux.EAGAIN => {
534 (await (async loop.linuxWaitFd(
535 inotify_fd,
536 os.linux.EPOLLET | os.linux.EPOLLIN,
537 ) catch unreachable)) catch |err| {
538 const transformed_err = switch (err) {
539 error.InvalidFileDescriptor => unreachable,
540 error.FileDescriptorAlreadyPresentInSet => unreachable,
541 error.InvalidSyscall => unreachable,
542 error.OperationCausesCircularLoop => unreachable,
543 error.FileDescriptorNotRegistered => unreachable,
544 error.SystemResources => error.SystemResources,
545 error.UserResourceLimitReached => error.UserResourceLimitReached,
546 error.FileDescriptorIncompatibleWithEpoll => unreachable,
547 error.Unexpected => unreachable,
548 };
549 await (async channel.put(Self.Event{ .Err = transformed_err }) catch unreachable);
550 };
551 },
552 else => unreachable,
553 }
554 }
471555 }
472 }
556 };
473557}
474558
475559const test_tmp_dir = "std_event_fs_test";
......@@ -517,9 +601,11 @@ async fn testFsWatch(loop: *event.Loop) !void {
517601 assert(mem.eql(u8, read_contents, contents));
518602
519603 // now watch the file
520 var watch = try watchFile(loop, file_path);
604 var watch = try Watch(void).create(loop, 0);
521605 defer watch.destroy();
522606
607 assert((try await try async watch.addFile(file_path, {})) == null);
608
523609 const ev = try async watch.channel.get();
524610 var ev_consumed = false;
525611 defer if (!ev_consumed) cancel ev;
......@@ -534,8 +620,8 @@ async fn testFsWatch(loop: *event.Loop) !void {
534620
535621 ev_consumed = true;
536622 switch (await ev) {
537 Watch.Event.CloseWrite => {},
538 Watch.Event.Err => |err| return err,
623 Watch(void).Event.CloseWrite => {},
624 Watch(void).Event.Err => |err| return err,
539625 }
540626
541627 const contents_updated = try await try async readFile(loop, file_path, 1024 * 1024);
std/event/rwlock.zig+2
......@@ -10,6 +10,8 @@ const Loop = std.event.Loop;
1010/// Does not make any syscalls - coroutines which are waiting for the lock are suspended, and
1111/// are resumed when the lock is released, in order.
1212/// Many readers can hold the lock at the same time; however locking for writing is exclusive.
13/// When a read lock is held, it will not be released until the reader queue is empty.
14/// When a write lock is held, it will not be released until the writer queue is empty.
1315pub const RwLock = struct {
1416 loop: *Loop,
1517 shared_state: u8, // TODO make this an enum
std/hash_map.zig+244-54
......@@ -9,6 +9,10 @@ const builtin = @import("builtin");
99const want_modification_safety = builtin.mode != builtin.Mode.ReleaseFast;
1010const debug_u32 = if (want_modification_safety) u32 else void;
1111
12pub fn AutoHashMap(comptime K: type, comptime V: type) type {
13 return HashMap(K, V, getAutoHashFn(K), getAutoEqlFn(K));
14}
15
1216pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u32, comptime eql: fn (a: K, b: K) bool) type {
1317 return struct {
1418 entries: []Entry,
......@@ -20,13 +24,22 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
2024
2125 const Self = this;
2226
23 pub const Entry = struct {
24 used: bool,
25 distance_from_start_index: usize,
27 pub const KV = struct {
2628 key: K,
2729 value: V,
2830 };
2931
32 const Entry = struct {
33 used: bool,
34 distance_from_start_index: usize,
35 kv: KV,
36 };
37
38 pub const GetOrPutResult = struct {
39 kv: *KV,
40 found_existing: bool,
41 };
42
3043 pub const Iterator = struct {
3144 hm: *const Self,
3245 // how many items have we returned
......@@ -36,7 +49,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
3649 // used to detect concurrent modification
3750 initial_modification_count: debug_u32,
3851
39 pub fn next(it: *Iterator) ?*Entry {
52 pub fn next(it: *Iterator) ?*KV {
4053 if (want_modification_safety) {
4154 assert(it.initial_modification_count == it.hm.modification_count); // concurrent modification
4255 }
......@@ -46,7 +59,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
4659 if (entry.used) {
4760 it.index += 1;
4861 it.count += 1;
49 return entry;
62 return &entry.kv;
5063 }
5164 }
5265 unreachable; // no next item
......@@ -71,7 +84,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
7184 };
7285 }
7386
74 pub fn deinit(hm: *const Self) void {
87 pub fn deinit(hm: Self) void {
7588 hm.allocator.free(hm.entries);
7689 }
7790
......@@ -84,34 +97,65 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
8497 hm.incrementModificationCount();
8598 }
8699
87 pub fn count(hm: *const Self) usize {
88 return hm.size;
100 pub fn count(self: Self) usize {
101 return self.size;
89102 }
90103
91 /// Returns the value that was already there.
92 pub fn put(hm: *Self, key: K, value: *const V) !?V {
93 if (hm.entries.len == 0) {
94 try hm.initCapacity(16);
104 /// If key exists this function cannot fail.
105 /// If there is an existing item with `key`, then the result
106 /// kv pointer points to it, and found_existing is true.
107 /// Otherwise, puts a new item with undefined value, and
108 /// the kv pointer points to it. Caller should then initialize
109 /// the data.
110 pub fn getOrPut(self: *Self, key: K) !GetOrPutResult {
111 // TODO this implementation can be improved - we should only
112 // have to hash once and find the entry once.
113 if (self.get(key)) |kv| {
114 return GetOrPutResult{
115 .kv = kv,
116 .found_existing = true,
117 };
118 }
119 self.incrementModificationCount();
120 try self.ensureCapacity();
121 const put_result = self.internalPut(key);
122 assert(put_result.old_kv == null);
123 return GetOrPutResult{
124 .kv = &put_result.new_entry.kv,
125 .found_existing = false,
126 };
127 }
128
129 fn ensureCapacity(self: *Self) !void {
130 if (self.entries.len == 0) {
131 return self.initCapacity(16);
95132 }
96 hm.incrementModificationCount();
97133
98134 // if we get too full (60%), double the capacity
99 if (hm.size * 5 >= hm.entries.len * 3) {
100 const old_entries = hm.entries;
101 try hm.initCapacity(hm.entries.len * 2);
135 if (self.size * 5 >= self.entries.len * 3) {
136 const old_entries = self.entries;
137 try self.initCapacity(self.entries.len * 2);
102138 // dump all of the old elements into the new table
103139 for (old_entries) |*old_entry| {
104140 if (old_entry.used) {
105 _ = hm.internalPut(old_entry.key, old_entry.value);
141 self.internalPut(old_entry.kv.key).new_entry.kv.value = old_entry.kv.value;
106142 }
107143 }
108 hm.allocator.free(old_entries);
144 self.allocator.free(old_entries);
109145 }
146 }
110147
111 return hm.internalPut(key, value);
148 /// Returns the kv pair that was already there.
149 pub fn put(self: *Self, key: K, value: V) !?KV {
150 self.incrementModificationCount();
151 try self.ensureCapacity();
152
153 const put_result = self.internalPut(key);
154 put_result.new_entry.kv.value = value;
155 return put_result.old_kv;
112156 }
113157
114 pub fn get(hm: *const Self, key: K) ?*Entry {
158 pub fn get(hm: *const Self, key: K) ?*KV {
115159 if (hm.entries.len == 0) {
116160 return null;
117161 }
......@@ -122,7 +166,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
122166 return hm.get(key) != null;
123167 }
124168
125 pub fn remove(hm: *Self, key: K) ?*Entry {
169 pub fn remove(hm: *Self, key: K) ?*KV {
126170 if (hm.entries.len == 0) return null;
127171 hm.incrementModificationCount();
128172 const start_index = hm.keyToIndex(key);
......@@ -134,7 +178,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
134178
135179 if (!entry.used) return null;
136180
137 if (!eql(entry.key, key)) continue;
181 if (!eql(entry.kv.key, key)) continue;
138182
139183 while (roll_over < hm.entries.len) : (roll_over += 1) {
140184 const next_index = (start_index + roll_over + 1) % hm.entries.len;
......@@ -142,7 +186,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
142186 if (!next_entry.used or next_entry.distance_from_start_index == 0) {
143187 entry.used = false;
144188 hm.size -= 1;
145 return entry;
189 return &entry.kv;
146190 }
147191 entry.* = next_entry.*;
148192 entry.distance_from_start_index -= 1;
......@@ -168,7 +212,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
168212 try other.initCapacity(self.entries.len);
169213 var it = self.iterator();
170214 while (it.next()) |entry| {
171 try other.put(entry.key, entry.value);
215 assert((try other.put(entry.key, entry.value)) == null);
172216 }
173217 return other;
174218 }
......@@ -188,60 +232,81 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
188232 }
189233 }
190234
191 /// Returns the value that was already there.
192 fn internalPut(hm: *Self, orig_key: K, orig_value: *const V) ?V {
235 const InternalPutResult = struct {
236 new_entry: *Entry,
237 old_kv: ?KV,
238 };
239
240 /// Returns a pointer to the new entry.
241 /// Asserts that there is enough space for the new item.
242 fn internalPut(self: *Self, orig_key: K) InternalPutResult {
193243 var key = orig_key;
194 var value = orig_value.*;
195 const start_index = hm.keyToIndex(key);
244 var value: V = undefined;
245 const start_index = self.keyToIndex(key);
196246 var roll_over: usize = 0;
197247 var distance_from_start_index: usize = 0;
198 while (roll_over < hm.entries.len) : ({
248 var got_result_entry = false;
249 var result = InternalPutResult{
250 .new_entry = undefined,
251 .old_kv = null,
252 };
253 while (roll_over < self.entries.len) : ({
199254 roll_over += 1;
200255 distance_from_start_index += 1;
201256 }) {
202 const index = (start_index + roll_over) % hm.entries.len;
203 const entry = &hm.entries[index];
257 const index = (start_index + roll_over) % self.entries.len;
258 const entry = &self.entries[index];
204259
205 if (entry.used and !eql(entry.key, key)) {
260 if (entry.used and !eql(entry.kv.key, key)) {
206261 if (entry.distance_from_start_index < distance_from_start_index) {
207262 // robin hood to the rescue
208263 const tmp = entry.*;
209 hm.max_distance_from_start_index = math.max(hm.max_distance_from_start_index, distance_from_start_index);
264 self.max_distance_from_start_index = math.max(self.max_distance_from_start_index, distance_from_start_index);
265 if (!got_result_entry) {
266 got_result_entry = true;
267 result.new_entry = entry;
268 }
210269 entry.* = Entry{
211270 .used = true,
212271 .distance_from_start_index = distance_from_start_index,
213 .key = key,
214 .value = value,
272 .kv = KV{
273 .key = key,
274 .value = value,
275 },
215276 };
216 key = tmp.key;
217 value = tmp.value;
277 key = tmp.kv.key;
278 value = tmp.kv.value;
218279 distance_from_start_index = tmp.distance_from_start_index;
219280 }
220281 continue;
221282 }
222283
223 var result: ?V = null;
224284 if (entry.used) {
225 result = entry.value;
285 result.old_kv = entry.kv;
226286 } else {
227287 // adding an entry. otherwise overwriting old value with
228288 // same key
229 hm.size += 1;
289 self.size += 1;
230290 }
231291
232 hm.max_distance_from_start_index = math.max(distance_from_start_index, hm.max_distance_from_start_index);
292 self.max_distance_from_start_index = math.max(distance_from_start_index, self.max_distance_from_start_index);
293 if (!got_result_entry) {
294 result.new_entry = entry;
295 }
233296 entry.* = Entry{
234297 .used = true,
235298 .distance_from_start_index = distance_from_start_index,
236 .key = key,
237 .value = value,
299 .kv = KV{
300 .key = key,
301 .value = value,
302 },
238303 };
239304 return result;
240305 }
241306 unreachable; // put into a full map
242307 }
243308
244 fn internalGet(hm: *const Self, key: K) ?*Entry {
309 fn internalGet(hm: Self, key: K) ?*KV {
245310 const start_index = hm.keyToIndex(key);
246311 {
247312 var roll_over: usize = 0;
......@@ -250,13 +315,13 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
250315 const entry = &hm.entries[index];
251316
252317 if (!entry.used) return null;
253 if (eql(entry.key, key)) return entry;
318 if (eql(entry.kv.key, key)) return &entry.kv;
254319 }
255320 }
256321 return null;
257322 }
258323
259 fn keyToIndex(hm: *const Self, key: K) usize {
324 fn keyToIndex(hm: Self, key: K) usize {
260325 return usize(hash(key)) % hm.entries.len;
261326 }
262327 };
......@@ -266,7 +331,7 @@ test "basic hash map usage" {
266331 var direct_allocator = std.heap.DirectAllocator.init();
267332 defer direct_allocator.deinit();
268333
269 var map = HashMap(i32, i32, hash_i32, eql_i32).init(&direct_allocator.allocator);
334 var map = AutoHashMap(i32, i32).init(&direct_allocator.allocator);
270335 defer map.deinit();
271336
272337 assert((try map.put(1, 11)) == null);
......@@ -275,8 +340,19 @@ test "basic hash map usage" {
275340 assert((try map.put(4, 44)) == null);
276341 assert((try map.put(5, 55)) == null);
277342
278 assert((try map.put(5, 66)).? == 55);
279 assert((try map.put(5, 55)).? == 66);
343 assert((try map.put(5, 66)).?.value == 55);
344 assert((try map.put(5, 55)).?.value == 66);
345
346 const gop1 = try map.getOrPut(5);
347 assert(gop1.found_existing == true);
348 assert(gop1.kv.value == 55);
349 gop1.kv.value = 77;
350 assert(map.get(5).?.value == 77);
351
352 const gop2 = try map.getOrPut(99);
353 assert(gop2.found_existing == false);
354 gop2.kv.value = 42;
355 assert(map.get(99).?.value == 42);
280356
281357 assert(map.contains(2));
282358 assert(map.get(2).?.value == 22);
......@@ -289,7 +365,7 @@ test "iterator hash map" {
289365 var direct_allocator = std.heap.DirectAllocator.init();
290366 defer direct_allocator.deinit();
291367
292 var reset_map = HashMap(i32, i32, hash_i32, eql_i32).init(&direct_allocator.allocator);
368 var reset_map = AutoHashMap(i32, i32).init(&direct_allocator.allocator);
293369 defer reset_map.deinit();
294370
295371 assert((try reset_map.put(1, 11)) == null);
......@@ -332,10 +408,124 @@ test "iterator hash map" {
332408 assert(entry.value == values[0]);
333409}
334410
335fn hash_i32(x: i32) u32 {
336 return @bitCast(u32, x);
411pub fn getAutoHashFn(comptime K: type) (fn (K) u32) {
412 return struct {
413 fn hash(key: K) u32 {
414 comptime var rng = comptime std.rand.DefaultPrng.init(0);
415 return autoHash(key, &rng.random, u32);
416 }
417 }.hash;
418}
419
420pub fn getAutoEqlFn(comptime K: type) (fn (K, K) bool) {
421 return struct {
422 fn eql(a: K, b: K) bool {
423 return autoEql(a, b);
424 }
425 }.eql;
426}
427
428// TODO improve these hash functions
429pub fn autoHash(key: var, comptime rng: *std.rand.Random, comptime HashInt: type) HashInt {
430 switch (@typeInfo(@typeOf(key))) {
431 builtin.TypeId.NoReturn,
432 builtin.TypeId.Opaque,
433 builtin.TypeId.Undefined,
434 builtin.TypeId.ArgTuple,
435 => @compileError("cannot hash this type"),
436
437 builtin.TypeId.Void,
438 builtin.TypeId.Null,
439 => return 0,
440
441 builtin.TypeId.Int => |info| {
442 const unsigned_x = @bitCast(@IntType(false, info.bits), key);
443 if (info.bits <= HashInt.bit_count) {
444 return HashInt(unsigned_x) *% comptime rng.scalar(HashInt);
445 } else {
446 return @truncate(HashInt, unsigned_x *% comptime rng.scalar(@typeOf(unsigned_x)));
447 }
448 },
449
450 builtin.TypeId.Float => |info| {
451 return autoHash(@bitCast(@IntType(false, info.bits), key), rng);
452 },
453 builtin.TypeId.Bool => return autoHash(@boolToInt(key), rng),
454 builtin.TypeId.Enum => return autoHash(@enumToInt(key), rng),
455 builtin.TypeId.ErrorSet => return autoHash(@errorToInt(key), rng),
456 builtin.TypeId.Promise, builtin.TypeId.Fn => return autoHash(@ptrToInt(key), rng),
457
458 builtin.TypeId.Namespace,
459 builtin.TypeId.Block,
460 builtin.TypeId.BoundFn,
461 builtin.TypeId.ComptimeFloat,
462 builtin.TypeId.ComptimeInt,
463 builtin.TypeId.Type,
464 => return 0,
465
466 builtin.TypeId.Pointer => |info| switch (info.size) {
467 builtin.TypeInfo.Pointer.Size.One => @compileError("TODO auto hash for single item pointers"),
468 builtin.TypeInfo.Pointer.Size.Many => @compileError("TODO auto hash for many item pointers"),
469 builtin.TypeInfo.Pointer.Size.Slice => {
470 const interval = std.math.max(1, key.len / 256);
471 var i: usize = 0;
472 var h = comptime rng.scalar(HashInt);
473 while (i < key.len) : (i += interval) {
474 h ^= autoHash(key[i], rng, HashInt);
475 }
476 return h;
477 },
478 },
479
480 builtin.TypeId.Optional => @compileError("TODO auto hash for optionals"),
481 builtin.TypeId.Array => @compileError("TODO auto hash for arrays"),
482 builtin.TypeId.Struct => @compileError("TODO auto hash for structs"),
483 builtin.TypeId.Union => @compileError("TODO auto hash for unions"),
484 builtin.TypeId.ErrorUnion => @compileError("TODO auto hash for unions"),
485 }
337486}
338487
339fn eql_i32(a: i32, b: i32) bool {
340 return a == b;
488pub fn autoEql(a: var, b: @typeOf(a)) bool {
489 switch (@typeInfo(@typeOf(a))) {
490 builtin.TypeId.NoReturn,
491 builtin.TypeId.Opaque,
492 builtin.TypeId.Undefined,
493 builtin.TypeId.ArgTuple,
494 => @compileError("cannot test equality of this type"),
495 builtin.TypeId.Void,
496 builtin.TypeId.Null,
497 => return true,
498 builtin.TypeId.Bool,
499 builtin.TypeId.Int,
500 builtin.TypeId.Float,
501 builtin.TypeId.ComptimeFloat,
502 builtin.TypeId.ComptimeInt,
503 builtin.TypeId.Namespace,
504 builtin.TypeId.Block,
505 builtin.TypeId.Promise,
506 builtin.TypeId.Enum,
507 builtin.TypeId.BoundFn,
508 builtin.TypeId.Fn,
509 builtin.TypeId.ErrorSet,
510 builtin.TypeId.Type,
511 => return a == b,
512
513 builtin.TypeId.Pointer => |info| switch (info.size) {
514 builtin.TypeInfo.Pointer.Size.One => @compileError("TODO auto eql for single item pointers"),
515 builtin.TypeInfo.Pointer.Size.Many => @compileError("TODO auto eql for many item pointers"),
516 builtin.TypeInfo.Pointer.Size.Slice => {
517 if (a.len != b.len) return false;
518 for (a) |a_item, i| {
519 if (!autoEql(a_item, b[i])) return false;
520 }
521 return true;
522 },
523 },
524
525 builtin.TypeId.Optional => @compileError("TODO auto eql for optionals"),
526 builtin.TypeId.Array => @compileError("TODO auto eql for arrays"),
527 builtin.TypeId.Struct => @compileError("TODO auto eql for structs"),
528 builtin.TypeId.Union => @compileError("TODO auto eql for unions"),
529 builtin.TypeId.ErrorUnion => @compileError("TODO auto eql for unions"),
530 }
341531}
std/index.zig+1
......@@ -5,6 +5,7 @@ pub const BufSet = @import("buf_set.zig").BufSet;
55pub const Buffer = @import("buffer.zig").Buffer;
66pub const BufferOutStream = @import("buffer.zig").BufferOutStream;
77pub const HashMap = @import("hash_map.zig").HashMap;
8pub const AutoHashMap = @import("hash_map.zig").AutoHashMap;
89pub const LinkedList = @import("linked_list.zig").LinkedList;
910pub const SegmentedList = @import("segmented_list.zig").SegmentedList;
1011pub const DynLib = @import("dynamic_library.zig").DynLib;
std/json.zig+1-1
......@@ -1318,7 +1318,7 @@ pub const Parser = struct {
13181318 _ = p.stack.pop();
13191319
13201320 var object = &p.stack.items[p.stack.len - 1].Object;
1321 _ = try object.put(key, value);
1321 _ = try object.put(key, value.*);
13221322 p.state = State.ObjectKey;
13231323 },
13241324 // Array Parent -> [ ..., <array>, value ]
std/special/build_runner.zig+2-2
......@@ -72,10 +72,10 @@ pub fn main() !void {
7272 if (mem.indexOfScalar(u8, option_contents, '=')) |name_end| {
7373 const option_name = option_contents[0..name_end];
7474 const option_value = option_contents[name_end + 1 ..];
75 if (builder.addUserInputOption(option_name, option_value))
75 if (try builder.addUserInputOption(option_name, option_value))
7676 return usageAndErr(&builder, false, try stderr_stream);
7777 } else {
78 if (builder.addUserInputFlag(option_contents))
78 if (try builder.addUserInputFlag(option_contents))
7979 return usageAndErr(&builder, false, try stderr_stream);
8080 }
8181 } else if (mem.startsWith(u8, arg, "-")) {