From fc9430f56798a53f9393a697f4ccd6bf9981b970 Mon Sep 17 00:00:00 2001 From: Martin Wickham Date: Thu, 3 Jun 2021 15:39:26 -0500 Subject: Breaking hash map changes for 0.8.0 - hash/eql functions moved into a Context object - *Context functions pass an explicit context - *Adapted functions pass specialized keys and contexts - new getPtr() function returns a pointer to value - remove functions renamed to fetchRemove - new remove functions return bool - removeAssertDiscard deleted, use assert(remove(...)) instead - Keys and values are stored in separate arrays - Entry is now {*K, *V}, the new KV is {K, V} - BufSet/BufMap functions renamed to match other set/map types - fixed iterating-while-modifying bug in src/link/C.zig --- src/AstGen.zig | 20 ++-- src/Cache.zig | 8 +- src/Compilation.zig | 196 ++++++++++++++++++--------------- src/Module.zig | 235 +++++++++++++++++++--------------------- src/Package.zig | 6 +- src/Sema.zig | 47 ++++---- src/air.zig | 7 +- src/codegen.zig | 61 ++++++----- src/codegen/c.zig | 17 +-- src/codegen/llvm.zig | 2 +- src/codegen/spirv.zig | 13 ++- src/codegen/wasm.zig | 6 +- src/libc_installation.zig | 4 +- src/link.zig | 16 +-- src/link/C.zig | 53 ++++----- src/link/Coff.zig | 18 +-- src/link/Elf.zig | 64 +++++------ src/link/MachO.zig | 128 ++++++++++++---------- src/link/MachO/Archive.zig | 12 +- src/link/MachO/DebugSymbols.zig | 40 ++++--- src/link/MachO/Dylib.zig | 6 +- src/link/MachO/Zld.zig | 43 ++++---- src/link/SpirV.zig | 8 +- src/link/Wasm.zig | 14 +-- src/liveness.zig | 55 +++++----- src/main.zig | 12 +- src/musl.zig | 7 +- src/translate_c.zig | 10 +- src/type.zig | 53 +++++---- src/value.zig | 17 +++ 30 files changed, 617 insertions(+), 561 deletions(-) (limited to 'src') diff --git a/src/AstGen.zig b/src/AstGen.zig index 790984dc8f..564813f9e3 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -144,9 +144,7 @@ pub fn generate(gpa: *Allocator, tree: ast.Tree) InnerError!Zir { astgen.extra.items[imports_index] = astgen.addExtraAssumeCapacity(Zir.Inst.Imports{ .imports_len = @intCast(u32, astgen.imports.count()), }); - for (astgen.imports.items()) |entry| { - astgen.extra.appendAssumeCapacity(entry.key); - } + astgen.extra.appendSliceAssumeCapacity(astgen.imports.keys()); } return Zir{ @@ -7932,13 +7930,13 @@ fn identAsString(astgen: *AstGen, ident_token: ast.TokenIndex) !u32 { const gop = try astgen.string_table.getOrPut(gpa, key); if (gop.found_existing) { string_bytes.shrinkRetainingCapacity(str_index); - return gop.entry.value; + return gop.value_ptr.*; } else { // We have to dupe the key into the arena, otherwise the memory // becomes invalidated when string_bytes gets data appended. // TODO https://github.com/ziglang/zig/issues/8528 - gop.entry.key = try astgen.arena.dupe(u8, key); - gop.entry.value = str_index; + gop.key_ptr.* = try astgen.arena.dupe(u8, key); + gop.value_ptr.* = str_index; try string_bytes.append(gpa, 0); return str_index; } @@ -7957,15 +7955,15 @@ fn strLitAsString(astgen: *AstGen, str_lit_token: ast.TokenIndex) !IndexSlice { if (gop.found_existing) { string_bytes.shrinkRetainingCapacity(str_index); return IndexSlice{ - .index = gop.entry.value, + .index = gop.value_ptr.*, .len = @intCast(u32, key.len), }; } else { // We have to dupe the key into the arena, otherwise the memory // becomes invalidated when string_bytes gets data appended. // TODO https://github.com/ziglang/zig/issues/8528 - gop.entry.key = try astgen.arena.dupe(u8, key); - gop.entry.value = str_index; + gop.key_ptr.* = try astgen.arena.dupe(u8, key); + gop.value_ptr.* = str_index; // Still need a null byte because we are using the same table // to lookup null terminated strings, so if we get a match, it has to // be null terminated for that to work. @@ -9122,10 +9120,10 @@ fn declareNewName( return astgen.failNodeNotes(node, "redeclaration of '{s}'", .{ name, }, &[_]u32{ - try astgen.errNoteNode(gop.entry.value, "other declaration here", .{}), + try astgen.errNoteNode(gop.value_ptr.*, "other declaration here", .{}), }); } - gop.entry.value = node; + gop.value_ptr.* = node; break; }, .top => break, diff --git a/src/Cache.zig b/src/Cache.zig index 6c17f52d69..6d20ca6367 100644 --- a/src/Cache.zig +++ b/src/Cache.zig @@ -90,10 +90,10 @@ pub const HashHelper = struct { } pub fn addStringSet(hh: *HashHelper, hm: std.StringArrayHashMapUnmanaged(void)) void { - const entries = hm.items(); - hh.add(entries.len); - for (entries) |entry| { - hh.addBytes(entry.key); + const keys = hm.keys(); + hh.add(keys.len); + for (keys) |key| { + hh.addBytes(key); } } diff --git a/src/Compilation.zig b/src/Compilation.zig index e20ee038c1..4c015cb58f 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -729,18 +729,21 @@ fn addPackageTableToCacheHash( ) (error{OutOfMemory} || std.os.GetCwdError)!void { const allocator = &arena.allocator; - const packages = try allocator.alloc(Package.Table.Entry, pkg_table.count()); + const packages = try allocator.alloc(Package.Table.KV, pkg_table.count()); { // Copy over the hashmap entries to our slice var table_it = pkg_table.iterator(); var idx: usize = 0; while (table_it.next()) |entry| : (idx += 1) { - packages[idx] = entry.*; + packages[idx] = .{ + .key = entry.key_ptr.*, + .value = entry.value_ptr.*, + }; } } // Sort the slice by package name - std.sort.sort(Package.Table.Entry, packages, {}, struct { - fn lessThan(_: void, lhs: Package.Table.Entry, rhs: Package.Table.Entry) bool { + std.sort.sort(Package.Table.KV, packages, {}, struct { + fn lessThan(_: void, lhs: Package.Table.KV, rhs: Package.Table.KV) bool { return std.mem.lessThan(u8, lhs.key, rhs.key); } }.lessThan); @@ -1525,8 +1528,8 @@ pub fn destroy(self: *Compilation) void { { var it = self.crt_files.iterator(); while (it.next()) |entry| { - gpa.free(entry.key); - entry.value.deinit(gpa); + gpa.free(entry.key_ptr.*); + entry.value_ptr.deinit(gpa); } self.crt_files.deinit(gpa); } @@ -1554,14 +1557,14 @@ pub fn destroy(self: *Compilation) void { glibc_file.deinit(gpa); } - for (self.c_object_table.items()) |entry| { - entry.key.destroy(gpa); + for (self.c_object_table.keys()) |key| { + key.destroy(gpa); } self.c_object_table.deinit(gpa); self.c_object_cache_digest_set.deinit(gpa); - for (self.failed_c_objects.items()) |entry| { - entry.value.destroy(gpa); + for (self.failed_c_objects.values()) |value| { + value.destroy(gpa); } self.failed_c_objects.deinit(gpa); @@ -1578,8 +1581,8 @@ pub fn destroy(self: *Compilation) void { } pub fn clearMiscFailures(comp: *Compilation) void { - for (comp.misc_failures.items()) |*entry| { - entry.value.deinit(comp.gpa); + for (comp.misc_failures.values()) |*value| { + value.deinit(comp.gpa); } comp.misc_failures.deinit(comp.gpa); comp.misc_failures = .{}; @@ -1599,9 +1602,10 @@ pub fn update(self: *Compilation) !void { // For compiling C objects, we rely on the cache hash system to avoid duplicating work. // Add a Job for each C object. - try self.c_object_work_queue.ensureUnusedCapacity(self.c_object_table.items().len); - for (self.c_object_table.items()) |entry| { - self.c_object_work_queue.writeItemAssumeCapacity(entry.key); + try self.c_object_work_queue.ensureUnusedCapacity(self.c_object_table.count()); + for (self.c_object_table.keys()) |key| { + assert(@ptrToInt(key) != 0xaaaa_aaaa_aaaa_aaaa); + self.c_object_work_queue.writeItemAssumeCapacity(key); } const use_stage1 = build_options.omit_stage2 or @@ -1620,8 +1624,8 @@ pub fn update(self: *Compilation) !void { // it changed, and, if so, re-compute ZIR and then queue the job // to update it. try self.astgen_work_queue.ensureUnusedCapacity(module.import_table.count()); - for (module.import_table.items()) |entry| { - self.astgen_work_queue.writeItemAssumeCapacity(entry.value); + for (module.import_table.values()) |value| { + self.astgen_work_queue.writeItemAssumeCapacity(value); } try self.work_queue.writeItem(.{ .analyze_pkg = std_pkg }); @@ -1635,12 +1639,12 @@ pub fn update(self: *Compilation) !void { // Process the deletion set. We use a while loop here because the // deletion set may grow as we call `clearDecl` within this loop, // and more unreferenced Decls are revealed. - while (module.deletion_set.entries.items.len != 0) { - const decl = module.deletion_set.entries.items[0].key; + while (module.deletion_set.count() != 0) { + const decl = module.deletion_set.keys()[0]; assert(decl.deletion_flag); assert(decl.dependants.count() == 0); const is_anon = if (decl.zir_decl_index == 0) blk: { - break :blk decl.namespace.anon_decls.swapRemove(decl) != null; + break :blk decl.namespace.anon_decls.swapRemove(decl); } else false; try module.clearDecl(decl, null); @@ -1677,8 +1681,7 @@ pub fn update(self: *Compilation) !void { // to reference the ZIR. if (self.totalErrorCount() == 0 and !self.keep_source_files_loaded) { if (self.bin_file.options.module) |module| { - for (module.import_table.items()) |entry| { - const file = entry.value; + for (module.import_table.values()) |file| { file.unloadTree(self.gpa); file.unloadSource(self.gpa); } @@ -1702,18 +1705,21 @@ pub fn totalErrorCount(self: *Compilation) usize { var total: usize = self.failed_c_objects.count() + self.misc_failures.count(); if (self.bin_file.options.module) |module| { - total += module.failed_exports.items().len; + total += module.failed_exports.count(); - for (module.failed_files.items()) |entry| { - if (entry.value) |_| { - total += 1; - } else { - const file = entry.key; - assert(file.zir_loaded); - const payload_index = file.zir.extra[@enumToInt(Zir.ExtraIndex.compile_errors)]; - assert(payload_index != 0); - const header = file.zir.extraData(Zir.Inst.CompileErrors, payload_index); - total += header.data.items_len; + { + var it = module.failed_files.iterator(); + while (it.next()) |entry| { + if (entry.value_ptr.*) |_| { + total += 1; + } else { + const file = entry.key_ptr.*; + assert(file.zir_loaded); + const payload_index = file.zir.extra[@enumToInt(Zir.ExtraIndex.compile_errors)]; + assert(payload_index != 0); + const header = file.zir.extraData(Zir.Inst.CompileErrors, payload_index); + total += header.data.items_len; + } } } @@ -1721,14 +1727,14 @@ pub fn totalErrorCount(self: *Compilation) usize { // When a parse error is introduced, we keep all the semantic analysis for // the previous parse success, including compile errors, but we cannot // emit them until the file succeeds parsing. - for (module.failed_decls.items()) |entry| { - if (entry.key.namespace.file_scope.okToReportErrors()) { + for (module.failed_decls.keys()) |key| { + if (key.namespace.file_scope.okToReportErrors()) { total += 1; } } if (module.emit_h) |emit_h| { - for (emit_h.failed_decls.items()) |entry| { - if (entry.key.namespace.file_scope.okToReportErrors()) { + for (emit_h.failed_decls.keys()) |key| { + if (key.namespace.file_scope.okToReportErrors()) { total += 1; } } @@ -1743,7 +1749,7 @@ pub fn totalErrorCount(self: *Compilation) usize { // Compile log errors only count if there are no other errors. if (total == 0) { if (self.bin_file.options.module) |module| { - total += @boolToInt(module.compile_log_decls.items().len != 0); + total += @boolToInt(module.compile_log_decls.count() != 0); } } @@ -1757,57 +1763,67 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors { var errors = std.ArrayList(AllErrors.Message).init(self.gpa); defer errors.deinit(); - for (self.failed_c_objects.items()) |entry| { - const c_object = entry.key; - const err_msg = entry.value; - // TODO these fields will need to be adjusted when we have proper - // C error reporting bubbling up. - try errors.append(.{ - .src = .{ - .src_path = try arena.allocator.dupe(u8, c_object.src.src_path), - .msg = try std.fmt.allocPrint(&arena.allocator, "unable to build C object: {s}", .{ - err_msg.msg, - }), - .byte_offset = 0, - .line = err_msg.line, - .column = err_msg.column, - .source_line = null, // TODO - }, - }); + { + var it = self.failed_c_objects.iterator(); + while (it.next()) |entry| { + const c_object = entry.key_ptr.*; + const err_msg = entry.value_ptr.*; + // TODO these fields will need to be adjusted when we have proper + // C error reporting bubbling up. + try errors.append(.{ + .src = .{ + .src_path = try arena.allocator.dupe(u8, c_object.src.src_path), + .msg = try std.fmt.allocPrint(&arena.allocator, "unable to build C object: {s}", .{ + err_msg.msg, + }), + .byte_offset = 0, + .line = err_msg.line, + .column = err_msg.column, + .source_line = null, // TODO + }, + }); + } } - for (self.misc_failures.items()) |entry| { - try AllErrors.addPlainWithChildren(&arena, &errors, entry.value.msg, entry.value.children); + for (self.misc_failures.values()) |*value| { + try AllErrors.addPlainWithChildren(&arena, &errors, value.msg, value.children); } if (self.bin_file.options.module) |module| { - for (module.failed_files.items()) |entry| { - if (entry.value) |msg| { - try AllErrors.add(module, &arena, &errors, msg.*); - } else { - // Must be ZIR errors. In order for ZIR errors to exist, the parsing - // must have completed successfully. - const tree = try entry.key.getTree(module.gpa); - assert(tree.errors.len == 0); - try AllErrors.addZir(&arena.allocator, &errors, entry.key); + { + var it = module.failed_files.iterator(); + while (it.next()) |entry| { + if (entry.value_ptr.*) |msg| { + try AllErrors.add(module, &arena, &errors, msg.*); + } else { + // Must be ZIR errors. In order for ZIR errors to exist, the parsing + // must have completed successfully. + const tree = try entry.key_ptr.*.getTree(module.gpa); + assert(tree.errors.len == 0); + try AllErrors.addZir(&arena.allocator, &errors, entry.key_ptr.*); + } } } - for (module.failed_decls.items()) |entry| { - // Skip errors for Decls within files that had a parse failure. - // We'll try again once parsing succeeds. - if (entry.key.namespace.file_scope.okToReportErrors()) { - try AllErrors.add(module, &arena, &errors, entry.value.*); + { + var it = module.failed_decls.iterator(); + while (it.next()) |entry| { + // Skip errors for Decls within files that had a parse failure. + // We'll try again once parsing succeeds. + if (entry.key_ptr.*.namespace.file_scope.okToReportErrors()) { + try AllErrors.add(module, &arena, &errors, entry.value_ptr.*.*); + } } } if (module.emit_h) |emit_h| { - for (emit_h.failed_decls.items()) |entry| { + var it = emit_h.failed_decls.iterator(); + while (it.next()) |entry| { // Skip errors for Decls within files that had a parse failure. // We'll try again once parsing succeeds. - if (entry.key.namespace.file_scope.okToReportErrors()) { - try AllErrors.add(module, &arena, &errors, entry.value.*); + if (entry.key_ptr.*.namespace.file_scope.okToReportErrors()) { + try AllErrors.add(module, &arena, &errors, entry.value_ptr.*.*); } } } - for (module.failed_exports.items()) |entry| { - try AllErrors.add(module, &arena, &errors, entry.value.*); + for (module.failed_exports.values()) |value| { + try AllErrors.add(module, &arena, &errors, value.*); } } @@ -1820,20 +1836,21 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors { } if (self.bin_file.options.module) |module| { - const compile_log_items = module.compile_log_decls.items(); - if (errors.items.len == 0 and compile_log_items.len != 0) { + if (errors.items.len == 0 and module.compile_log_decls.count() != 0) { + const keys = module.compile_log_decls.keys(); + const values = module.compile_log_decls.values(); // First one will be the error; subsequent ones will be notes. - const src_loc = compile_log_items[0].key.nodeOffsetSrcLoc(compile_log_items[0].value); + const src_loc = keys[0].nodeOffsetSrcLoc(values[0]); const err_msg = Module.ErrorMsg{ .src_loc = src_loc, .msg = "found compile log statement", - .notes = try self.gpa.alloc(Module.ErrorMsg, compile_log_items.len - 1), + .notes = try self.gpa.alloc(Module.ErrorMsg, module.compile_log_decls.count() - 1), }; defer self.gpa.free(err_msg.notes); - for (compile_log_items[1..]) |entry, i| { + for (keys[1..]) |key, i| { err_msg.notes[i] = .{ - .src_loc = entry.key.nodeOffsetSrcLoc(entry.value), + .src_loc = key.nodeOffsetSrcLoc(values[i+1]), .msg = "also here", }; } @@ -1898,6 +1915,7 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor } while (self.c_object_work_queue.readItem()) |c_object| { + assert(@ptrToInt(c_object) != 0xaaaa_aaaa_aaaa_aaaa); self.work_queue_wait_group.start(); try self.thread_pool.spawn(workerUpdateCObject, .{ self, c_object, &c_obj_prog_node, &self.work_queue_wait_group, @@ -1964,7 +1982,7 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor continue; }, else => { - try module.failed_decls.ensureCapacity(module.gpa, module.failed_decls.items().len + 1); + try module.failed_decls.ensureCapacity(module.gpa, module.failed_decls.count() + 1); module.failed_decls.putAssumeCapacityNoClobber(decl, try Module.ErrorMsg.create( module.gpa, decl.srcLoc(), @@ -2036,7 +2054,7 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor @panic("sadly stage2 is omitted from this build to save memory on the CI server"); const module = self.bin_file.options.module.?; self.bin_file.updateDeclLineNumber(module, decl) catch |err| { - try module.failed_decls.ensureCapacity(module.gpa, module.failed_decls.items().len + 1); + try module.failed_decls.ensureCapacity(module.gpa, module.failed_decls.count() + 1); module.failed_decls.putAssumeCapacityNoClobber(decl, try Module.ErrorMsg.create( module.gpa, decl.srcLoc(), @@ -2101,7 +2119,7 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor }; }, .windows_import_lib => |index| { - const link_lib = self.bin_file.options.system_libs.items()[index].key; + const link_lib = self.bin_file.options.system_libs.keys()[index]; mingw.buildImportLib(self, link_lib) catch |err| { // TODO Surface more error details. try self.setMiscFailure( @@ -3023,7 +3041,7 @@ fn failCObjWithOwnedErrorMsg( defer lock.release(); { errdefer err_msg.destroy(comp.gpa); - try comp.failed_c_objects.ensureCapacity(comp.gpa, comp.failed_c_objects.items().len + 1); + try comp.failed_c_objects.ensureCapacity(comp.gpa, comp.failed_c_objects.count() + 1); } comp.failed_c_objects.putAssumeCapacityNoClobber(c_object, err_msg); } @@ -3953,8 +3971,8 @@ fn updateStage1Module(comp: *Compilation, main_progress_node: *std.Progress.Node // We need to save the inferred link libs to the cache, otherwise if we get a cache hit // next time we will be missing these libs. var libs_txt = std.ArrayList(u8).init(arena); - for (comp.bin_file.options.system_libs.items()[inferred_lib_start_index..]) |entry| { - try libs_txt.writer().print("{s}\n", .{entry.key}); + for (comp.bin_file.options.system_libs.keys()[inferred_lib_start_index..]) |key| { + try libs_txt.writer().print("{s}\n", .{key}); } try directory.handle.writeFile(libs_txt_basename, libs_txt.items); } @@ -4017,7 +4035,7 @@ fn createStage1Pkg( var children = std.ArrayList(*stage1.Pkg).init(arena); var it = pkg.table.iterator(); while (it.next()) |entry| { - try children.append(try createStage1Pkg(arena, entry.key, entry.value, child_pkg)); + try children.append(try createStage1Pkg(arena, entry.key_ptr.*, entry.value_ptr.*, child_pkg)); } break :blk children.items; }; diff --git a/src/Module.zig b/src/Module.zig index 0ae6158758..c20bb2eb08 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -268,15 +268,7 @@ pub const Decl = struct { /// typed_value may need to be regenerated. dependencies: DepsTable = .{}, - /// The reason this is not `std.AutoArrayHashMapUnmanaged` is a workaround for - /// stage1 compiler giving me: `error: struct 'Module.Decl' depends on itself` - pub const DepsTable = std.ArrayHashMapUnmanaged( - *Decl, - void, - std.array_hash_map.getAutoHashFn(*Decl), - std.array_hash_map.getAutoEqlFn(*Decl), - false, - ); + pub const DepsTable = std.AutoArrayHashMapUnmanaged(*Decl, void); pub fn clearName(decl: *Decl, gpa: *Allocator) void { gpa.free(mem.spanZ(decl.name)); @@ -287,7 +279,7 @@ pub const Decl = struct { const gpa = module.gpa; log.debug("destroy {*} ({s})", .{ decl, decl.name }); if (decl.deletion_flag) { - module.deletion_set.swapRemoveAssertDiscard(decl); + assert(module.deletion_set.swapRemove(decl)); } if (decl.has_tv) { if (decl.getInnerNamespace()) |namespace| { @@ -550,11 +542,11 @@ pub const Decl = struct { } fn removeDependant(decl: *Decl, other: *Decl) void { - decl.dependants.removeAssertDiscard(other); + assert(decl.dependants.swapRemove(other)); } fn removeDependency(decl: *Decl, other: *Decl) void { - decl.dependencies.removeAssertDiscard(other); + assert(decl.dependencies.swapRemove(other)); } }; @@ -683,7 +675,7 @@ pub const EnumFull = struct { /// Offset from `owner_decl`, points to the enum decl AST node. node_offset: i32, - pub const ValueMap = std.ArrayHashMapUnmanaged(Value, void, Value.hash_u32, Value.eql, false); + pub const ValueMap = std.ArrayHashMapUnmanaged(Value, void, Value.ArrayHashContext, false); pub fn srcLoc(self: EnumFull) SrcLoc { return .{ @@ -895,13 +887,13 @@ pub const Scope = struct { var anon_decls = ns.anon_decls; ns.anon_decls = .{}; - for (decls.items()) |entry| { - entry.value.destroy(mod); + for (decls.values()) |value| { + value.destroy(mod); } decls.deinit(gpa); - for (anon_decls.items()) |entry| { - entry.key.destroy(mod); + for (anon_decls.keys()) |key| { + key.destroy(mod); } anon_decls.deinit(gpa); } @@ -924,15 +916,13 @@ pub const Scope = struct { // TODO rework this code to not panic on OOM. // (might want to coordinate with the clearDecl function) - for (decls.items()) |entry| { - const child_decl = entry.value; + for (decls.values()) |child_decl| { mod.clearDecl(child_decl, outdated_decls) catch @panic("out of memory"); child_decl.destroy(mod); } decls.deinit(gpa); - for (anon_decls.items()) |entry| { - const child_decl = entry.key; + for (anon_decls.keys()) |child_decl| { mod.clearDecl(child_decl, outdated_decls) catch @panic("out of memory"); child_decl.destroy(mod); } @@ -2120,9 +2110,11 @@ pub const InnerError = error{ OutOfMemory, AnalysisFail }; pub fn deinit(mod: *Module) void { const gpa = mod.gpa; - for (mod.import_table.items()) |entry| { - gpa.free(entry.key); - entry.value.destroy(mod); + for (mod.import_table.keys()) |key| { + gpa.free(key); + } + for (mod.import_table.values()) |value| { + value.destroy(mod); } mod.import_table.deinit(gpa); @@ -2130,16 +2122,16 @@ pub fn deinit(mod: *Module) void { // The callsite of `Compilation.create` owns the `root_pkg`, however // Module owns the builtin and std packages that it adds. - if (mod.root_pkg.table.remove("builtin")) |entry| { - gpa.free(entry.key); - entry.value.destroy(gpa); + if (mod.root_pkg.table.fetchRemove("builtin")) |kv| { + gpa.free(kv.key); + kv.value.destroy(gpa); } - if (mod.root_pkg.table.remove("std")) |entry| { - gpa.free(entry.key); - entry.value.destroy(gpa); + if (mod.root_pkg.table.fetchRemove("std")) |kv| { + gpa.free(kv.key); + kv.value.destroy(gpa); } - if (mod.root_pkg.table.remove("root")) |entry| { - gpa.free(entry.key); + if (mod.root_pkg.table.fetchRemove("root")) |kv| { + gpa.free(kv.key); } mod.compile_log_text.deinit(gpa); @@ -2148,46 +2140,45 @@ pub fn deinit(mod: *Module) void { mod.local_zir_cache.handle.close(); mod.global_zir_cache.handle.close(); - for (mod.failed_decls.items()) |entry| { - entry.value.destroy(gpa); + for (mod.failed_decls.values()) |value| { + value.destroy(gpa); } mod.failed_decls.deinit(gpa); if (mod.emit_h) |emit_h| { - for (emit_h.failed_decls.items()) |entry| { - entry.value.destroy(gpa); + for (emit_h.failed_decls.values()) |value| { + value.destroy(gpa); } emit_h.failed_decls.deinit(gpa); emit_h.decl_table.deinit(gpa); gpa.destroy(emit_h); } - for (mod.failed_files.items()) |entry| { - if (entry.value) |msg| msg.destroy(gpa); + for (mod.failed_files.values()) |value| { + if (value) |msg| msg.destroy(gpa); } mod.failed_files.deinit(gpa); - for (mod.failed_exports.items()) |entry| { - entry.value.destroy(gpa); + for (mod.failed_exports.values()) |value| { + value.destroy(gpa); } mod.failed_exports.deinit(gpa); mod.compile_log_decls.deinit(gpa); - for (mod.decl_exports.items()) |entry| { - const export_list = entry.value; + for (mod.decl_exports.values()) |export_list| { gpa.free(export_list); } mod.decl_exports.deinit(gpa); - for (mod.export_owners.items()) |entry| { - freeExportList(gpa, entry.value); + for (mod.export_owners.values()) |value| { + freeExportList(gpa, value); } mod.export_owners.deinit(gpa); - var it = mod.global_error_set.iterator(); - while (it.next()) |entry| { - gpa.free(entry.key); + var it = mod.global_error_set.keyIterator(); + while (it.next()) |key| { + gpa.free(key.*); } mod.global_error_set.deinit(gpa); @@ -2670,12 +2661,10 @@ fn updateZirRefs(gpa: *Allocator, file: *Scope.File, old_zir: Zir) !void { } if (decl.getInnerNamespace()) |namespace| { - for (namespace.decls.items()) |entry| { - const sub_decl = entry.value; + for (namespace.decls.values()) |sub_decl| { try decl_stack.append(gpa, sub_decl); } - for (namespace.anon_decls.items()) |entry| { - const sub_decl = entry.key; + for (namespace.anon_decls.keys()) |sub_decl| { try decl_stack.append(gpa, sub_decl); } } @@ -2769,8 +2758,7 @@ pub fn ensureDeclAnalyzed(mod: *Module, decl: *Decl) InnerError!void { // prior to re-analysis. mod.deleteDeclExports(decl); // Dependencies will be re-discovered, so we remove them here prior to re-analysis. - for (decl.dependencies.items()) |entry| { - const dep = entry.key; + for (decl.dependencies.keys()) |dep| { dep.removeDependant(decl); if (dep.dependants.count() == 0 and !dep.deletion_flag) { log.debug("insert {*} ({s}) dependant {*} ({s}) into deletion set", .{ @@ -2817,8 +2805,7 @@ pub fn ensureDeclAnalyzed(mod: *Module, decl: *Decl) InnerError!void { // We may need to chase the dependants and re-analyze them. // However, if the decl is a function, and the type is the same, we do not need to. if (type_changed or decl.ty.zigTypeTag() != .Fn) { - for (decl.dependants.items()) |entry| { - const dep = entry.key; + for (decl.dependants.keys()) |dep| { switch (dep.analysis) { .unreferenced => unreachable, .in_progress => continue, // already doing analysis, ok @@ -3128,7 +3115,7 @@ pub fn declareDeclDependency(mod: *Module, depender: *Decl, dependee: *Decl) !vo if (dependee.deletion_flag) { dependee.deletion_flag = false; - mod.deletion_set.removeAssertDiscard(dependee); + assert(mod.deletion_set.swapRemove(dependee)); } dependee.dependants.putAssumeCapacity(depender, {}); @@ -3154,7 +3141,7 @@ pub fn importPkg(mod: *Module, cur_pkg: *Package, pkg: *Package) !ImportFileResu const gop = try mod.import_table.getOrPut(gpa, resolved_path); if (gop.found_existing) return ImportFileResult{ - .file = gop.entry.value, + .file = gop.value_ptr.*, .is_new = false, }; keep_resolved_path = true; // It's now owned by import_table. @@ -3165,7 +3152,7 @@ pub fn importPkg(mod: *Module, cur_pkg: *Package, pkg: *Package) !ImportFileResu const new_file = try gpa.create(Scope.File); errdefer gpa.destroy(new_file); - gop.entry.value = new_file; + gop.value_ptr.* = new_file; new_file.* = .{ .sub_file_path = sub_file_path, .source = undefined, @@ -3209,7 +3196,7 @@ pub fn importFile( const gop = try mod.import_table.getOrPut(gpa, resolved_path); if (gop.found_existing) return ImportFileResult{ - .file = gop.entry.value, + .file = gop.value_ptr.*, .is_new = false, }; keep_resolved_path = true; // It's now owned by import_table. @@ -3231,7 +3218,7 @@ pub fn importFile( resolved_root_path, resolved_path, sub_file_path, import_string, }); - gop.entry.value = new_file; + gop.value_ptr.* = new_file; new_file.* = .{ .sub_file_path = sub_file_path, .source = undefined, @@ -3366,7 +3353,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) InnerError!vo log.debug("scan new {*} ({s}) into {*}", .{ new_decl, decl_name, namespace }); new_decl.src_line = line; new_decl.name = decl_name; - gop.entry.value = new_decl; + gop.value_ptr.* = new_decl; // Exported decls, comptime decls, usingnamespace decls, and // test decls if in test mode, get analyzed. const want_analysis = is_exported or switch (decl_name_index) { @@ -3385,7 +3372,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) InnerError!vo return; } gpa.free(decl_name); - const decl = gop.entry.value; + const decl = gop.value_ptr.*; log.debug("scan existing {*} ({s}) of {*}", .{ decl, decl.name, namespace }); // Update the AST node of the decl; even if its contents are unchanged, it may // have been re-ordered. @@ -3438,10 +3425,9 @@ pub fn clearDecl( } // Remove itself from its dependencies. - for (decl.dependencies.items()) |entry| { - const dep = entry.key; + for (decl.dependencies.keys()) |dep| { dep.removeDependant(decl); - if (dep.dependants.items().len == 0 and !dep.deletion_flag) { + if (dep.dependants.count() == 0 and !dep.deletion_flag) { // We don't recursively perform a deletion here, because during the update, // another reference to it may turn up. dep.deletion_flag = true; @@ -3451,8 +3437,7 @@ pub fn clearDecl( decl.dependencies.clearRetainingCapacity(); // Anything that depends on this deleted decl needs to be re-analyzed. - for (decl.dependants.items()) |entry| { - const dep = entry.key; + for (decl.dependants.keys()) |dep| { dep.removeDependency(decl); if (outdated_decls) |map| { map.putAssumeCapacity(dep, {}); @@ -3467,14 +3452,14 @@ pub fn clearDecl( } decl.dependants.clearRetainingCapacity(); - if (mod.failed_decls.swapRemove(decl)) |entry| { - entry.value.destroy(gpa); + if (mod.failed_decls.fetchSwapRemove(decl)) |kv| { + kv.value.destroy(gpa); } if (mod.emit_h) |emit_h| { - if (emit_h.failed_decls.swapRemove(decl)) |entry| { - entry.value.destroy(gpa); + if (emit_h.failed_decls.fetchSwapRemove(decl)) |kv| { + kv.value.destroy(gpa); } - emit_h.decl_table.removeAssertDiscard(decl); + assert(emit_h.decl_table.swapRemove(decl)); } _ = mod.compile_log_decls.swapRemove(decl); mod.deleteDeclExports(decl); @@ -3510,7 +3495,7 @@ pub fn clearDecl( if (decl.deletion_flag) { decl.deletion_flag = false; - mod.deletion_set.swapRemoveAssertDiscard(decl); + assert(mod.deletion_set.swapRemove(decl)); } decl.analysis = .unreferenced; @@ -3519,12 +3504,12 @@ pub fn clearDecl( /// Delete all the Export objects that are caused by this Decl. Re-analysis of /// this Decl will cause them to be re-created (or not). fn deleteDeclExports(mod: *Module, decl: *Decl) void { - const kv = mod.export_owners.swapRemove(decl) orelse return; + const kv = mod.export_owners.fetchSwapRemove(decl) orelse return; for (kv.value) |exp| { - if (mod.decl_exports.getEntry(exp.exported_decl)) |decl_exports_kv| { + if (mod.decl_exports.getPtr(exp.exported_decl)) |value_ptr| { // Remove exports with owner_decl matching the regenerating decl. - const list = decl_exports_kv.value; + const list = value_ptr.*; var i: usize = 0; var new_len = list.len; while (i < new_len) { @@ -3535,9 +3520,9 @@ fn deleteDeclExports(mod: *Module, decl: *Decl) void { i += 1; } } - decl_exports_kv.value = mod.gpa.shrink(list, new_len); + value_ptr.* = mod.gpa.shrink(list, new_len); if (new_len == 0) { - mod.decl_exports.removeAssertDiscard(exp.exported_decl); + assert(mod.decl_exports.swapRemove(exp.exported_decl)); } } if (mod.comp.bin_file.cast(link.File.Elf)) |elf| { @@ -3546,8 +3531,8 @@ fn deleteDeclExports(mod: *Module, decl: *Decl) void { if (mod.comp.bin_file.cast(link.File.MachO)) |macho| { macho.deleteExport(exp.link.macho); } - if (mod.failed_exports.swapRemove(exp)) |entry| { - entry.value.destroy(mod.gpa); + if (mod.failed_exports.fetchSwapRemove(exp)) |failed_kv| { + failed_kv.value.destroy(mod.gpa); } mod.gpa.free(exp.options.name); mod.gpa.destroy(exp); @@ -3623,12 +3608,12 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn) !void { fn markOutdatedDecl(mod: *Module, decl: *Decl) !void { log.debug("mark outdated {*} ({s})", .{ decl, decl.name }); try mod.comp.work_queue.writeItem(.{ .analyze_decl = decl }); - if (mod.failed_decls.swapRemove(decl)) |entry| { - entry.value.destroy(mod.gpa); + if (mod.failed_decls.fetchSwapRemove(decl)) |kv| { + kv.value.destroy(mod.gpa); } if (mod.emit_h) |emit_h| { - if (emit_h.failed_decls.swapRemove(decl)) |entry| { - entry.value.destroy(mod.gpa); + if (emit_h.failed_decls.fetchSwapRemove(decl)) |kv| { + kv.value.destroy(mod.gpa); } } _ = mod.compile_log_decls.swapRemove(decl); @@ -3686,17 +3671,24 @@ fn allocateNewDecl(mod: *Module, namespace: *Scope.Namespace, src_node: ast.Node } /// Get error value for error tag `name`. -pub fn getErrorValue(mod: *Module, name: []const u8) !std.StringHashMapUnmanaged(ErrorInt).Entry { +pub fn getErrorValue(mod: *Module, name: []const u8) !std.StringHashMapUnmanaged(ErrorInt).KV { const gop = try mod.global_error_set.getOrPut(mod.gpa, name); - if (gop.found_existing) - return gop.entry.*; + if (gop.found_existing) { + return std.StringHashMapUnmanaged(ErrorInt).KV{ + .key = gop.key_ptr.*, + .value = gop.value_ptr.*, + }; + } - errdefer mod.global_error_set.removeAssertDiscard(name); + errdefer assert(mod.global_error_set.remove(name)); try mod.error_name_list.ensureCapacity(mod.gpa, mod.error_name_list.items.len + 1); - gop.entry.key = try mod.gpa.dupe(u8, name); - gop.entry.value = @intCast(ErrorInt, mod.error_name_list.items.len); - mod.error_name_list.appendAssumeCapacity(gop.entry.key); - return gop.entry.*; + gop.key_ptr.* = try mod.gpa.dupe(u8, name); + gop.value_ptr.* = @intCast(ErrorInt, mod.error_name_list.items.len); + mod.error_name_list.appendAssumeCapacity(gop.key_ptr.*); + return std.StringHashMapUnmanaged(ErrorInt).KV{ + .key = gop.key_ptr.*, + .value = gop.value_ptr.*, + }; } pub fn analyzeExport( @@ -3712,8 +3704,8 @@ pub fn analyzeExport( else => return mod.fail(scope, src, "unable to export type '{}'", .{exported_decl.ty}), } - try mod.decl_exports.ensureCapacity(mod.gpa, mod.decl_exports.items().len + 1); - try mod.export_owners.ensureCapacity(mod.gpa, mod.export_owners.items().len + 1); + try mod.decl_exports.ensureCapacity(mod.gpa, mod.decl_exports.count() + 1); + try mod.export_owners.ensureCapacity(mod.gpa, mod.export_owners.count() + 1); const new_export = try mod.gpa.create(Export); errdefer mod.gpa.destroy(new_export); @@ -3746,20 +3738,20 @@ pub fn analyzeExport( // Add to export_owners table. const eo_gop = mod.export_owners.getOrPutAssumeCapacity(owner_decl); if (!eo_gop.found_existing) { - eo_gop.entry.value = &[0]*Export{}; + eo_gop.value_ptr.* = &[0]*Export{}; } - eo_gop.entry.value = try mod.gpa.realloc(eo_gop.entry.value, eo_gop.entry.value.len + 1); - eo_gop.entry.value[eo_gop.entry.value.len - 1] = new_export; - errdefer eo_gop.entry.value = mod.gpa.shrink(eo_gop.entry.value, eo_gop.entry.value.len - 1); + eo_gop.value_ptr.* = try mod.gpa.realloc(eo_gop.value_ptr.*, eo_gop.value_ptr.len + 1); + eo_gop.value_ptr.*[eo_gop.value_ptr.len - 1] = new_export; + errdefer eo_gop.value_ptr.* = mod.gpa.shrink(eo_gop.value_ptr.*, eo_gop.value_ptr.len - 1); // Add to exported_decl table. const de_gop = mod.decl_exports.getOrPutAssumeCapacity(exported_decl); if (!de_gop.found_existing) { - de_gop.entry.value = &[0]*Export{}; + de_gop.value_ptr.* = &[0]*Export{}; } - de_gop.entry.value = try mod.gpa.realloc(de_gop.entry.value, de_gop.entry.value.len + 1); - de_gop.entry.value[de_gop.entry.value.len - 1] = new_export; - errdefer de_gop.entry.value = mod.gpa.shrink(de_gop.entry.value, de_gop.entry.value.len - 1); + de_gop.value_ptr.* = try mod.gpa.realloc(de_gop.value_ptr.*, de_gop.value_ptr.len + 1); + de_gop.value_ptr.*[de_gop.value_ptr.len - 1] = new_export; + errdefer de_gop.value_ptr.* = mod.gpa.shrink(de_gop.value_ptr.*, de_gop.value_ptr.len - 1); } pub fn constInst(mod: *Module, arena: *Allocator, src: LazySrcLoc, typed_value: TypedValue) !*ir.Inst { const const_inst = try arena.create(ir.Inst.Constant); @@ -3851,7 +3843,7 @@ pub fn constIntBig(mod: *Module, arena: *Allocator, src: LazySrcLoc, ty: Type, b pub fn deleteAnonDecl(mod: *Module, scope: *Scope, decl: *Decl) void { const scope_decl = scope.ownerDecl().?; - scope_decl.namespace.anon_decls.swapRemoveAssertDiscard(decl); + assert(scope_decl.namespace.anon_decls.swapRemove(decl)); decl.destroy(mod); } @@ -4001,8 +3993,8 @@ pub fn failWithOwnedErrorMsg(mod: *Module, scope: *Scope, err_msg: *ErrorMsg) In { errdefer err_msg.destroy(mod.gpa); - try mod.failed_decls.ensureCapacity(mod.gpa, mod.failed_decls.items().len + 1); - try mod.failed_files.ensureCapacity(mod.gpa, mod.failed_files.items().len + 1); + try mod.failed_decls.ensureCapacity(mod.gpa, mod.failed_decls.count() + 1); + try mod.failed_files.ensureCapacity(mod.gpa, mod.failed_files.count() + 1); } switch (scope.tag) { .block => { @@ -4420,8 +4412,8 @@ fn lockAndClearFileCompileError(mod: *Module, file: *Scope.File) void { .never_loaded, .parse_failure, .astgen_failure => { const lock = mod.comp.mutex.acquire(); defer lock.release(); - if (mod.failed_files.swapRemove(file)) |entry| { - if (entry.value) |msg| msg.destroy(mod.gpa); // Delete previous error message. + if (mod.failed_files.fetchSwapRemove(file)) |kv| { + if (kv.value) |msg| msg.destroy(mod.gpa); // Delete previous error message. } }, } @@ -4649,7 +4641,7 @@ pub fn analyzeStructFields(mod: *Module, struct_obj: *Struct) InnerError!void { const gop = struct_obj.fields.getOrPutAssumeCapacity(field_name); assert(!gop.found_existing); - gop.entry.value = .{ + gop.value_ptr.* = .{ .ty = field_ty, .abi_align = Value.initTag(.abi_align_default), .default_val = Value.initTag(.unreachable_value), @@ -4663,7 +4655,7 @@ pub fn analyzeStructFields(mod: *Module, struct_obj: *Struct) InnerError!void { // TODO: if we need to report an error here, use a source location // that points to this alignment expression rather than the struct. // But only resolve the source location if we need to emit a compile error. - gop.entry.value.abi_align = (try sema.resolveInstConst(&block, src, align_ref)).val; + gop.value_ptr.abi_align = (try sema.resolveInstConst(&block, src, align_ref)).val; } if (has_default) { const default_ref = @intToEnum(Zir.Inst.Ref, zir.extra[extra_index]); @@ -4671,7 +4663,7 @@ pub fn analyzeStructFields(mod: *Module, struct_obj: *Struct) InnerError!void { // TODO: if we need to report an error here, use a source location // that points to this default value expression rather than the struct. // But only resolve the source location if we need to emit a compile error. - gop.entry.value.default_val = (try sema.resolveInstConst(&block, src, default_ref)).val; + gop.value_ptr.default_val = (try sema.resolveInstConst(&block, src, default_ref)).val; } } } @@ -4816,7 +4808,7 @@ pub fn analyzeUnionFields(mod: *Module, union_obj: *Union) InnerError!void { const gop = union_obj.fields.getOrPutAssumeCapacity(field_name); assert(!gop.found_existing); - gop.entry.value = .{ + gop.value_ptr.* = .{ .ty = field_ty, .abi_align = Value.initTag(.abi_align_default), }; @@ -4825,7 +4817,7 @@ pub fn analyzeUnionFields(mod: *Module, union_obj: *Union) InnerError!void { // TODO: if we need to report an error here, use a source location // that points to this alignment expression rather than the struct. // But only resolve the source location if we need to emit a compile error. - gop.entry.value.abi_align = (try sema.resolveInstConst(&block, src, align_ref)).val; + gop.value_ptr.abi_align = (try sema.resolveInstConst(&block, src, align_ref)).val; } } @@ -4841,9 +4833,7 @@ pub fn processOutdatedAndDeletedDecls(mod: *Module) !void { // deleted Decl pointers in the work queue. var outdated_decls = std.AutoArrayHashMap(*Decl, void).init(mod.gpa); defer outdated_decls.deinit(); - for (mod.import_table.items()) |import_table_entry| { - const file = import_table_entry.value; - + for (mod.import_table.values()) |file| { try outdated_decls.ensureUnusedCapacity(file.outdated_decls.items.len); for (file.outdated_decls.items) |decl| { outdated_decls.putAssumeCapacity(decl, {}); @@ -4872,8 +4862,8 @@ pub fn processOutdatedAndDeletedDecls(mod: *Module) !void { } // Finally we can queue up re-analysis tasks after we have processed // the deleted decls. - for (outdated_decls.items()) |entry| { - try mod.markOutdatedDecl(entry.key); + for (outdated_decls.keys()) |key| { + try mod.markOutdatedDecl(key); } } @@ -4886,9 +4876,10 @@ pub fn processExports(mod: *Module) !void { var symbol_exports: std.StringArrayHashMapUnmanaged(*Export) = .{}; defer symbol_exports.deinit(gpa); - for (mod.decl_exports.items()) |entry| { - const exported_decl = entry.key; - const exports = entry.value; + var it = mod.decl_exports.iterator(); + while (it.next()) |entry| { + const exported_decl = entry.key_ptr.*; + const exports = entry.value_ptr.*; for (exports) |new_export| { const gop = try symbol_exports.getOrPut(gpa, new_export.options.name); if (gop.found_existing) { @@ -4899,13 +4890,13 @@ pub fn processExports(mod: *Module) !void { new_export.options.name, }); errdefer msg.destroy(gpa); - const other_export = gop.entry.value; + const other_export = gop.value_ptr.*; const other_src_loc = other_export.getSrcLoc(); try mod.errNoteNonLazy(other_src_loc, msg, "other symbol here", .{}); mod.failed_exports.putAssumeCapacityNoClobber(new_export, msg); new_export.status = .failed; } else { - gop.entry.value = new_export; + gop.value_ptr.* = new_export; } } mod.comp.bin_file.updateDeclExports(mod, exported_decl, exports) catch |err| switch (err) { diff --git a/src/Package.zig b/src/Package.zig index 5fec4be3d2..1f19c1d43a 100644 --- a/src/Package.zig +++ b/src/Package.zig @@ -100,9 +100,9 @@ pub fn destroy(pkg: *Package, gpa: *Allocator) void { } { - var it = pkg.table.iterator(); - while (it.next()) |kv| { - gpa.free(kv.key); + var it = pkg.table.keyIterator(); + while (it.next()) |key| { + gpa.free(key.*); } } diff --git a/src/Sema.zig b/src/Sema.zig index 64b0988f07..718e145a7d 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -1350,7 +1350,7 @@ fn zirValidateStructInitPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Ind }; // Maps field index to field_ptr index of where it was already initialized. - const found_fields = try gpa.alloc(Zir.Inst.Index, struct_obj.fields.entries.items.len); + const found_fields = try gpa.alloc(Zir.Inst.Index, struct_obj.fields.count()); defer gpa.free(found_fields); mem.set(Zir.Inst.Index, found_fields, 0); @@ -1382,7 +1382,7 @@ fn zirValidateStructInitPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Ind for (found_fields) |field_ptr, i| { if (field_ptr != 0) continue; - const field_name = struct_obj.fields.entries.items[i].key; + const field_name = struct_obj.fields.keys()[i]; const template = "missing struct field: {s}"; const args = .{field_name}; if (root_msg) |msg| { @@ -1687,7 +1687,7 @@ fn zirCompileLog( const gop = try sema.mod.compile_log_decls.getOrPut(sema.gpa, sema.owner_decl); if (!gop.found_existing) { - gop.entry.value = src_node; + gop.value_ptr.* = src_node; } return sema.mod.constInst(sema.arena, src, .{ .ty = Type.initTag(.void), @@ -1954,7 +1954,7 @@ fn zirExport(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError! const section_index = struct_obj.fields.getIndex("section").?; const export_name = try fields[name_index].toAllocatedBytes(sema.arena); const linkage = fields[linkage_index].toEnum( - struct_obj.fields.items()[linkage_index].value.ty, + struct_obj.fields.values()[linkage_index].ty, std.builtin.GlobalLinkage, ); @@ -2426,12 +2426,12 @@ fn zirErrorValue(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerEr const src = inst_data.src(); // Create an anonymous error set type with only this error value, and return the value. - const entry = try sema.mod.getErrorValue(inst_data.get(sema.code)); - const result_type = try Type.Tag.error_set_single.create(sema.arena, entry.key); + const kv = try sema.mod.getErrorValue(inst_data.get(sema.code)); + const result_type = try Type.Tag.error_set_single.create(sema.arena, kv.key); return sema.mod.constInst(sema.arena, src, .{ .ty = result_type, .val = try Value.Tag.@"error".create(sema.arena, .{ - .name = entry.key, + .name = kv.key, }), }); } @@ -2558,10 +2558,10 @@ fn zirMergeErrorSets(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Inn } const new_names = try sema.arena.alloc([]const u8, set.count()); - var it = set.iterator(); + var it = set.keyIterator(); var i: usize = 0; - while (it.next()) |entry| : (i += 1) { - new_names[i] = entry.key; + while (it.next()) |key| : (i += 1) { + new_names[i] = key.*; } const new_error_set = try sema.arena.create(Module.ErrorSet); @@ -2636,7 +2636,7 @@ fn zirEnumToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerErr .enum_full => { const enum_full = enum_tag.ty.castTag(.enum_full).?.data; if (enum_full.values.count() != 0) { - const val = enum_full.values.entries.items[field_index].key; + const val = enum_full.values.keys()[field_index]; return mod.constInst(arena, src, .{ .ty = int_tag_ty, .val = val, @@ -4360,7 +4360,7 @@ fn validateSwitchItemBool( } } -const ValueSrcMap = std.HashMap(Value, Module.SwitchProngSrc, Value.hash, Value.eql, std.hash_map.DefaultMaxLoadPercentage); +const ValueSrcMap = std.HashMap(Value, Module.SwitchProngSrc, Value.HashContext, std.hash_map.default_max_load_percentage); fn validateSwitchItemSparse( sema: *Sema, @@ -4371,8 +4371,8 @@ fn validateSwitchItemSparse( switch_prong_src: Module.SwitchProngSrc, ) InnerError!void { const item_val = (try sema.resolveSwitchItemVal(block, item_ref, src_node_offset, switch_prong_src, .none)).val; - const entry = (try seen_values.fetchPut(item_val, switch_prong_src)) orelse return; - return sema.validateSwitchDupe(block, entry.value, switch_prong_src, src_node_offset); + const kv = (try seen_values.fetchPut(item_val, switch_prong_src)) orelse return; + return sema.validateSwitchDupe(block, kv.value, switch_prong_src, src_node_offset); } fn validateSwitchNoRange( @@ -5470,12 +5470,12 @@ fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref: // Maps field index to field_type index of where it was already initialized. // For making sure all fields are accounted for and no fields are duplicated. - const found_fields = try gpa.alloc(Zir.Inst.Index, struct_obj.fields.entries.items.len); + const found_fields = try gpa.alloc(Zir.Inst.Index, struct_obj.fields.count()); defer gpa.free(found_fields); mem.set(Zir.Inst.Index, found_fields, 0); // The init values to use for the struct instance. - const field_inits = try gpa.alloc(*ir.Inst, struct_obj.fields.entries.items.len); + const field_inits = try gpa.alloc(*ir.Inst, struct_obj.fields.count()); defer gpa.free(field_inits); var field_i: u32 = 0; @@ -5513,9 +5513,9 @@ fn zirStructInit(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index, is_ref: if (field_type_inst != 0) continue; // Check if the field has a default init. - const field = struct_obj.fields.entries.items[i].value; + const field = struct_obj.fields.values()[i]; if (field.default_val.tag() == .unreachable_value) { - const field_name = struct_obj.fields.entries.items[i].key; + const field_name = struct_obj.fields.keys()[i]; const template = "missing struct field: {s}"; const args = .{field_name}; if (root_msg) |msg| { @@ -6402,7 +6402,7 @@ fn analyzeStructFieldPtr( const field_index = struct_obj.fields.getIndex(field_name) orelse return sema.failWithBadFieldAccess(block, struct_obj, field_name_src, field_name); - const field = struct_obj.fields.entries.items[field_index].value; + const field = struct_obj.fields.values()[field_index]; const ptr_field_ty = try mod.simplePtrType(arena, field.ty, true, .One); if (try sema.resolveDefinedValue(block, src, struct_ptr)) |struct_ptr_val| { @@ -6438,7 +6438,7 @@ fn analyzeUnionFieldPtr( const field_index = union_obj.fields.getIndex(field_name) orelse return sema.failWithBadUnionFieldAccess(block, union_obj, field_name_src, field_name); - const field = union_obj.fields.entries.items[field_index].value; + const field = union_obj.fields.values()[field_index]; const ptr_field_ty = try mod.simplePtrType(arena, field.ty, true, .One); if (try sema.resolveDefinedValue(block, src, union_ptr)) |union_ptr_val| { @@ -7476,9 +7476,8 @@ fn typeHasOnePossibleValue( .@"struct" => { const resolved_ty = try sema.resolveTypeFields(block, src, ty); const s = resolved_ty.castTag(.@"struct").?.data; - for (s.fields.entries.items) |entry| { - const field_ty = entry.value.ty; - if ((try sema.typeHasOnePossibleValue(block, src, field_ty)) == null) { + for (s.fields.values()) |value| { + if ((try sema.typeHasOnePossibleValue(block, src, value.ty)) == null) { return null; } } @@ -7488,7 +7487,7 @@ fn typeHasOnePossibleValue( const resolved_ty = try sema.resolveTypeFields(block, src, ty); const enum_full = resolved_ty.castTag(.enum_full).?.data; if (enum_full.fields.count() == 1) { - return enum_full.values.entries.items[0].key; + return enum_full.values.keys()[0]; } else { return null; } diff --git a/src/air.zig b/src/air.zig index 6373592e7b..490816ecd7 100644 --- a/src/air.zig +++ b/src/air.zig @@ -696,10 +696,11 @@ const DumpTzir = struct { std.debug.print("Module.Function(name={s}):\n", .{dtz.module_fn.owner_decl.name}); - for (dtz.const_table.items()) |entry| { - const constant = entry.key.castTag(.constant).?; + var it = dtz.const_table.iterator(); + while (it.next()) |entry| { + const constant = entry.key_ptr.*.castTag(.constant).?; try writer.print(" @{d}: {} = {};\n", .{ - entry.value, constant.base.ty, constant.val, + entry.value_ptr.*, constant.base.ty, constant.val, }); } diff --git a/src/codegen.zig b/src/codegen.zig index 407848641a..c3895b7778 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -794,7 +794,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { fn ensureProcessDeathCapacity(self: *Self, additional_count: usize) !void { const table = &self.branch_stack.items[self.branch_stack.items.len - 1].inst_table; - try table.ensureCapacity(self.gpa, table.items().len + additional_count); + try table.ensureCapacity(self.gpa, table.count() + additional_count); } /// Adds a Type to the .debug_info at the current position. The bytes will be populated later, @@ -808,12 +808,12 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { const gop = try dbg_out.dbg_info_type_relocs.getOrPut(self.gpa, ty); if (!gop.found_existing) { - gop.entry.value = .{ + gop.value_ptr.* = .{ .off = undefined, .relocs = .{}, }; } - try gop.entry.value.relocs.append(self.gpa, @intCast(u32, index)); + try gop.value_ptr.relocs.append(self.gpa, @intCast(u32, index)); }, .none => {}, } @@ -2877,58 +2877,67 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { // assert that parent_branch.free_registers equals the saved_then_branch.free_registers // rather than assigning it. const parent_branch = &self.branch_stack.items[self.branch_stack.items.len - 2]; - try parent_branch.inst_table.ensureCapacity(self.gpa, parent_branch.inst_table.items().len + - else_branch.inst_table.items().len); - for (else_branch.inst_table.items()) |else_entry| { - const canon_mcv = if (saved_then_branch.inst_table.swapRemove(else_entry.key)) |then_entry| blk: { + try parent_branch.inst_table.ensureCapacity(self.gpa, parent_branch.inst_table.count() + + else_branch.inst_table.count()); + + const else_slice = else_branch.inst_table.entries.slice(); + const else_keys = else_slice.items(.key); + const else_values = else_slice.items(.value); + for (else_keys) |else_key, else_idx| { + const else_value = else_values[else_idx]; + const canon_mcv = if (saved_then_branch.inst_table.fetchSwapRemove(else_key)) |then_entry| blk: { // The instruction's MCValue is overridden in both branches. - parent_branch.inst_table.putAssumeCapacity(else_entry.key, then_entry.value); - if (else_entry.value == .dead) { + parent_branch.inst_table.putAssumeCapacity(else_key, then_entry.value); + if (else_value == .dead) { assert(then_entry.value == .dead); continue; } break :blk then_entry.value; } else blk: { - if (else_entry.value == .dead) + if (else_value == .dead) continue; // The instruction is only overridden in the else branch. var i: usize = self.branch_stack.items.len - 2; while (true) { i -= 1; // If this overflows, the question is: why wasn't the instruction marked dead? - if (self.branch_stack.items[i].inst_table.get(else_entry.key)) |mcv| { + if (self.branch_stack.items[i].inst_table.get(else_key)) |mcv| { assert(mcv != .dead); break :blk mcv; } } }; - log.debug("consolidating else_entry {*} {}=>{}", .{ else_entry.key, else_entry.value, canon_mcv }); + log.debug("consolidating else_entry {*} {}=>{}", .{ else_key, else_value, canon_mcv }); // TODO make sure the destination stack offset / register does not already have something // going on there. - try self.setRegOrMem(inst.base.src, else_entry.key.ty, canon_mcv, else_entry.value); + try self.setRegOrMem(inst.base.src, else_key.ty, canon_mcv, else_value); // TODO track the new register / stack allocation } - try parent_branch.inst_table.ensureCapacity(self.gpa, parent_branch.inst_table.items().len + - saved_then_branch.inst_table.items().len); - for (saved_then_branch.inst_table.items()) |then_entry| { + try parent_branch.inst_table.ensureCapacity(self.gpa, parent_branch.inst_table.count() + + saved_then_branch.inst_table.count()); + const then_slice = saved_then_branch.inst_table.entries.slice(); + const then_keys = then_slice.items(.key); + const then_values = then_slice.items(.value); + for (then_keys) |then_key, then_idx| { + const then_value = then_values[then_idx]; // We already deleted the items from this table that matched the else_branch. // So these are all instructions that are only overridden in the then branch. - parent_branch.inst_table.putAssumeCapacity(then_entry.key, then_entry.value); - if (then_entry.value == .dead) + parent_branch.inst_table.putAssumeCapacity(then_key, then_value); + if (then_value == .dead) continue; const parent_mcv = blk: { var i: usize = self.branch_stack.items.len - 2; while (true) { i -= 1; - if (self.branch_stack.items[i].inst_table.get(then_entry.key)) |mcv| { + if (self.branch_stack.items[i].inst_table.get(then_key)) |mcv| { assert(mcv != .dead); break :blk mcv; } } }; - log.debug("consolidating then_entry {*} {}=>{}", .{ then_entry.key, parent_mcv, then_entry.value }); + log.debug("consolidating then_entry {*} {}=>{}", .{ then_key, parent_mcv, then_value }); // TODO make sure the destination stack offset / register does not already have something // going on there. - try self.setRegOrMem(inst.base.src, then_entry.key.ty, parent_mcv, then_entry.value); + try self.setRegOrMem(inst.base.src, then_key.ty, parent_mcv, then_value); // TODO track the new register / stack allocation } @@ -3028,7 +3037,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { // block results. .mcv = MCValue{ .none = {} }, }); - const block_data = &self.blocks.getEntry(inst).?.value; + const block_data = self.blocks.getPtr(inst).?; defer block_data.relocs.deinit(self.gpa); try self.genBody(inst.body); @@ -3109,7 +3118,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { } fn br(self: *Self, src: LazySrcLoc, block: *ir.Inst.Block, operand: *ir.Inst) !MCValue { - const block_data = &self.blocks.getEntry(block).?.value; + const block_data = self.blocks.getPtr(block).?; if (operand.ty.hasCodeGenBits()) { const operand_mcv = try self.resolveInst(operand); @@ -3124,7 +3133,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { } fn brVoid(self: *Self, src: LazySrcLoc, block: *ir.Inst.Block) !MCValue { - const block_data = &self.blocks.getEntry(block).?.value; + const block_data = self.blocks.getPtr(block).?; // Emit a jump with a relocation. It will be patched up after the block ends. try block_data.relocs.ensureCapacity(self.gpa, block_data.relocs.items.len + 1); @@ -4118,9 +4127,9 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { const branch = &self.branch_stack.items[0]; const gop = try branch.inst_table.getOrPut(self.gpa, inst); if (!gop.found_existing) { - gop.entry.value = try self.genTypedValue(inst.src, .{ .ty = inst.ty, .val = const_inst.val }); + gop.value_ptr.* = try self.genTypedValue(inst.src, .{ .ty = inst.ty, .val = const_inst.val }); } - return gop.entry.value; + return gop.value_ptr.*; } return self.getResolvedInstValue(inst); diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 55c9c5b641..68b74d7659 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -39,7 +39,7 @@ const BlockData = struct { }; pub const CValueMap = std.AutoHashMap(*Inst, CValue); -pub const TypedefMap = std.HashMap(Type, struct { name: []const u8, rendered: []u8 }, Type.hash, Type.eql, std.hash_map.default_max_load_percentage); +pub const TypedefMap = std.HashMap(Type, struct { name: []const u8, rendered: []u8 }, Type.HashContext, std.hash_map.default_max_load_percentage); fn formatTypeAsCIdentifier( data: Type, @@ -309,7 +309,7 @@ pub const DeclGen = struct { .enum_full, .enum_nonexhaustive => { const enum_full = t.cast(Type.Payload.EnumFull).?.data; if (enum_full.values.count() != 0) { - const tag_val = enum_full.values.entries.items[field_index].key; + const tag_val = enum_full.values.keys()[field_index]; return dg.renderValue(writer, enum_full.tag_ty, tag_val); } else { return writer.print("{d}", .{field_index}); @@ -493,10 +493,13 @@ pub const DeclGen = struct { defer buffer.deinit(); try buffer.appendSlice("typedef struct {\n"); - for (struct_obj.fields.entries.items) |entry| { - try buffer.append(' '); - try dg.renderType(buffer.writer(), entry.value.ty); - try buffer.writer().print(" {s};\n", .{fmtIdent(entry.key)}); + { + var it = struct_obj.fields.iterator(); + while (it.next()) |entry| { + try buffer.append(' '); + try dg.renderType(buffer.writer(), entry.value_ptr.ty); + try buffer.writer().print(" {s};\n", .{fmtIdent(entry.key_ptr.*)}); + } } try buffer.appendSlice("} "); @@ -1186,7 +1189,7 @@ fn genStructFieldPtr(o: *Object, inst: *Inst.StructFieldPtr) !CValue { const writer = o.writer(); const struct_ptr = try o.resolveInst(inst.struct_ptr); const struct_obj = inst.struct_ptr.ty.elemType().castTag(.@"struct").?.data; - const field_name = struct_obj.fields.entries.items[inst.field_index].key; + const field_name = struct_obj.fields.keys()[inst.field_index]; const local = try o.allocLocal(inst.base.ty, .Const); switch (struct_ptr) { diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index a7e61f6baa..4e4621ca29 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -789,7 +789,7 @@ pub const FuncGen = struct { .break_vals = &break_vals, }); defer { - self.blocks.removeAssertDiscard(inst); + assert(self.blocks.remove(inst)); break_bbs.deinit(self.gpa()); break_vals.deinit(self.gpa()); } diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig index 399a314bf0..4ce5de7523 100644 --- a/src/codegen/spirv.zig +++ b/src/codegen/spirv.zig @@ -2,6 +2,7 @@ const std = @import("std"); const Allocator = std.mem.Allocator; const Target = std.Target; const log = std.log.scoped(.codegen); +const assert = std.debug.assert; const spec = @import("spirv/spec.zig"); const Opcode = spec.Opcode; @@ -17,7 +18,7 @@ const Inst = ir.Inst; pub const Word = u32; pub const ResultId = u32; -pub const TypeMap = std.HashMap(Type, ResultId, Type.hash, Type.eql, std.hash_map.default_max_load_percentage); +pub const TypeMap = std.HashMap(Type, u32, Type.HashContext, std.hash_map.default_max_load_percentage); pub const InstMap = std.AutoHashMap(*Inst, ResultId); const IncomingBlock = struct { @@ -141,16 +142,16 @@ pub const SPIRVModule = struct { const path = decl.namespace.file_scope.sub_file_path; const result = try self.file_names.getOrPut(path); if (!result.found_existing) { - result.entry.value = self.allocResultId(); - try writeInstructionWithString(&self.binary.debug_strings, .OpString, &[_]Word{result.entry.value}, path); + result.value_ptr.* = self.allocResultId(); + try writeInstructionWithString(&self.binary.debug_strings, .OpString, &[_]Word{result.value_ptr.*}, path); try writeInstruction(&self.binary.debug_strings, .OpSource, &[_]Word{ @enumToInt(spec.SourceLanguage.Unknown), // TODO: Register Zig source language. 0, // TODO: Zig version as u32? - result.entry.value, + result.value_ptr.*, }); } - return result.entry.value; + return result.value_ptr.*; } }; @@ -847,7 +848,7 @@ pub const DeclGen = struct { .incoming_blocks = &incoming_blocks, }); defer { - self.blocks.removeAssertDiscard(inst); + assert(self.blocks.remove(inst)); incoming_blocks.deinit(self.spv.gpa); } diff --git a/src/codegen/wasm.zig b/src/codegen/wasm.zig index 528b233589..d7fe239d3b 100644 --- a/src/codegen/wasm.zig +++ b/src/codegen/wasm.zig @@ -625,10 +625,10 @@ pub const Context = struct { const struct_data: *Module.Struct = ty.castTag(.@"struct").?.data; const fields_len = @intCast(u32, struct_data.fields.count()); try self.locals.ensureCapacity(self.gpa, self.locals.items.len + fields_len); - for (struct_data.fields.items()) |entry| { + for (struct_data.fields.values()) |*value| { const val_type = try self.genValtype( .{ .node_offset = struct_data.node_offset }, - entry.value.ty, + value.ty, ); self.locals.appendAssumeCapacity(val_type); self.local_index += 1; @@ -1018,7 +1018,7 @@ pub const Context = struct { .enum_full, .enum_nonexhaustive => { const enum_full = ty.cast(Type.Payload.EnumFull).?.data; if (enum_full.values.count() != 0) { - const tag_val = enum_full.values.entries.items[field_index.data].key; + const tag_val = enum_full.values.keys()[field_index.data]; try self.emitConstant(src, tag_val, enum_full.tag_ty); } else { try writer.writeByte(wasm.opcode(.i32_const)); diff --git a/src/libc_installation.zig b/src/libc_installation.zig index b9f0f0ecc7..2286b2c851 100644 --- a/src/libc_installation.zig +++ b/src/libc_installation.zig @@ -252,7 +252,7 @@ pub const LibCInstallation = struct { // Detect infinite loops. const inf_loop_env_key = "ZIG_IS_DETECTING_LIBC_PATHS"; if (env_map.get(inf_loop_env_key) != null) return error.ZigIsTheCCompiler; - try env_map.set(inf_loop_env_key, "1"); + try env_map.put(inf_loop_env_key, "1"); const exec_res = std.ChildProcess.exec(.{ .allocator = allocator, @@ -564,7 +564,7 @@ fn ccPrintFileName(args: CCPrintFileNameOptions) ![:0]u8 { // Detect infinite loops. const inf_loop_env_key = "ZIG_IS_DETECTING_LIBC_PATHS"; if (env_map.get(inf_loop_env_key) != null) return error.ZigIsTheCCompiler; - try env_map.set(inf_loop_env_key, "1"); + try env_map.put(inf_loop_env_key, "1"); const exec_res = std.ChildProcess.exec(.{ .allocator = allocator, diff --git a/src/link.zig b/src/link.zig index 7338593a0e..fcb263f03e 100644 --- a/src/link.zig +++ b/src/link.zig @@ -162,7 +162,7 @@ pub const File = struct { }; /// For DWARF .debug_info. - pub const DbgInfoTypeRelocsTable = std.HashMapUnmanaged(Type, DbgInfoTypeReloc, Type.hash, Type.eql, std.hash_map.DefaultMaxLoadPercentage); + pub const DbgInfoTypeRelocsTable = std.HashMapUnmanaged(Type, DbgInfoTypeReloc, Type.HashContext, std.hash_map.default_max_load_percentage); /// For DWARF .debug_info. pub const DbgInfoTypeReloc = struct { @@ -406,8 +406,8 @@ pub const File = struct { const full_out_path = try emit.directory.join(comp.gpa, &[_][]const u8{emit.sub_path}); defer comp.gpa.free(full_out_path); assert(comp.c_object_table.count() == 1); - const the_entry = comp.c_object_table.items()[0]; - const cached_pp_file_path = the_entry.key.status.success.object_path; + const the_key = comp.c_object_table.keys()[0]; + const cached_pp_file_path = the_key.status.success.object_path; try fs.cwd().copyFile(cached_pp_file_path, fs.cwd(), full_out_path, .{}); return; } @@ -545,8 +545,8 @@ pub const File = struct { base.releaseLock(); try man.addListOfFiles(base.options.objects); - for (comp.c_object_table.items()) |entry| { - _ = try man.addFile(entry.key.status.success.object_path, null); + for (comp.c_object_table.keys()) |key| { + _ = try man.addFile(key.status.success.object_path, null); } try man.addOptionalFile(module_obj_path); try man.addOptionalFile(compiler_rt_path); @@ -580,12 +580,12 @@ pub const File = struct { var object_files = std.ArrayList([*:0]const u8).init(base.allocator); defer object_files.deinit(); - try object_files.ensureCapacity(base.options.objects.len + comp.c_object_table.items().len + 2); + try object_files.ensureCapacity(base.options.objects.len + comp.c_object_table.count() + 2); for (base.options.objects) |obj_path| { object_files.appendAssumeCapacity(try arena.dupeZ(u8, obj_path)); } - for (comp.c_object_table.items()) |entry| { - object_files.appendAssumeCapacity(try arena.dupeZ(u8, entry.key.status.success.object_path)); + for (comp.c_object_table.keys()) |key| { + object_files.appendAssumeCapacity(try arena.dupeZ(u8, key.status.success.object_path)); } if (module_obj_path) |p| { object_files.appendAssumeCapacity(try arena.dupeZ(u8, p)); diff --git a/src/link/C.zig b/src/link/C.zig index 5fb62c20af..6cb219db41 100644 --- a/src/link/C.zig +++ b/src/link/C.zig @@ -70,8 +70,8 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio } pub fn deinit(self: *C) void { - for (self.decl_table.items()) |entry| { - self.freeDecl(entry.key); + for (self.decl_table.keys()) |key| { + deinitDecl(self.base.allocator, key); } self.decl_table.deinit(self.base.allocator); } @@ -80,13 +80,17 @@ pub fn allocateDeclIndexes(self: *C, decl: *Module.Decl) !void {} pub fn freeDecl(self: *C, decl: *Module.Decl) void { _ = self.decl_table.swapRemove(decl); - decl.link.c.code.deinit(self.base.allocator); - decl.fn_link.c.fwd_decl.deinit(self.base.allocator); - var it = decl.fn_link.c.typedefs.iterator(); - while (it.next()) |some| { - self.base.allocator.free(some.value.rendered); + deinitDecl(self.base.allocator, decl); +} + +fn deinitDecl(gpa: *Allocator, decl: *Module.Decl) void { + decl.link.c.code.deinit(gpa); + decl.fn_link.c.fwd_decl.deinit(gpa); + var it = decl.fn_link.c.typedefs.valueIterator(); + while (it.next()) |value| { + gpa.free(value.rendered); } - decl.fn_link.c.typedefs.deinit(self.base.allocator); + decl.fn_link.c.typedefs.deinit(gpa); } pub fn updateDecl(self: *C, module: *Module, decl: *Module.Decl) !void { @@ -101,9 +105,9 @@ pub fn updateDecl(self: *C, module: *Module, decl: *Module.Decl) !void { const code = &decl.link.c.code; fwd_decl.shrinkRetainingCapacity(0); { - var it = typedefs.iterator(); - while (it.next()) |entry| { - module.gpa.free(entry.value.rendered); + var it = typedefs.valueIterator(); + while (it.next()) |value| { + module.gpa.free(value.rendered); } } typedefs.clearRetainingCapacity(); @@ -128,9 +132,9 @@ pub fn updateDecl(self: *C, module: *Module, decl: *Module.Decl) !void { object.blocks.deinit(module.gpa); object.code.deinit(); object.dg.fwd_decl.deinit(); - var it = object.dg.typedefs.iterator(); - while (it.next()) |some| { - module.gpa.free(some.value.rendered); + var it = object.dg.typedefs.valueIterator(); + while (it.next()) |value| { + module.gpa.free(value.rendered); } object.dg.typedefs.deinit(); } @@ -194,31 +198,30 @@ pub fn flushModule(self: *C, comp: *Compilation) !void { if (module.global_error_set.size == 0) break :render_errors; var it = module.global_error_set.iterator(); while (it.next()) |entry| { - try err_typedef_writer.print("#define zig_error_{s} {d}\n", .{ entry.key, entry.value }); + try err_typedef_writer.print("#define zig_error_{s} {d}\n", .{ entry.key_ptr.*, entry.value_ptr.* }); } try err_typedef_writer.writeByte('\n'); } var fn_count: usize = 0; - var typedefs = std.HashMap(Type, []const u8, Type.hash, Type.eql, std.hash_map.default_max_load_percentage).init(comp.gpa); + var typedefs = std.HashMap(Type, []const u8, Type.HashContext, std.hash_map.default_max_load_percentage).init(comp.gpa); defer typedefs.deinit(); // Typedefs, forward decls and non-functions first. // TODO: performance investigation: would keeping a list of Decls that we should // generate, rather than querying here, be faster? - for (self.decl_table.items()) |kv| { - const decl = kv.key; + for (self.decl_table.keys()) |decl| { if (!decl.has_tv) continue; const buf = buf: { if (decl.val.castTag(.function)) |_| { var it = decl.fn_link.c.typedefs.iterator(); while (it.next()) |new| { - if (typedefs.get(new.key)) |previous| { - try err_typedef_writer.print("typedef {s} {s};\n", .{ previous, new.value.name }); + if (typedefs.get(new.key_ptr.*)) |previous| { + try err_typedef_writer.print("typedef {s} {s};\n", .{ previous, new.value_ptr.name }); } else { try typedefs.ensureCapacity(typedefs.capacity() + 1); - try err_typedef_writer.writeAll(new.value.rendered); - typedefs.putAssumeCapacityNoClobber(new.key, new.value.name); + try err_typedef_writer.writeAll(new.value_ptr.rendered); + typedefs.putAssumeCapacityNoClobber(new.key_ptr.*, new.value_ptr.name); } } fn_count += 1; @@ -242,8 +245,7 @@ pub fn flushModule(self: *C, comp: *Compilation) !void { // Now the function bodies. try all_buffers.ensureCapacity(all_buffers.items.len + fn_count); - for (self.decl_table.items()) |kv| { - const decl = kv.key; + for (self.decl_table.keys()) |decl| { if (!decl.has_tv) continue; if (decl.val.castTag(.function)) |_| { const buf = decl.link.c.code.items; @@ -278,8 +280,7 @@ pub fn flushEmitH(module: *Module) !void { .iov_len = zig_h.len, }); - for (emit_h.decl_table.items()) |kv| { - const decl = kv.key; + for (emit_h.decl_table.keys()) |decl| { const decl_emit_h = decl.getEmitH(module); const buf = decl_emit_h.fwd_decl.items; all_buffers.appendAssumeCapacity(.{ diff --git a/src/link/Coff.zig b/src/link/Coff.zig index 611250c943..983e78e9e9 100644 --- a/src/link/Coff.zig +++ b/src/link/Coff.zig @@ -735,7 +735,7 @@ pub fn updateDeclExports(self: *Coff, module: *Module, decl: *Module.Decl, expor for (exports) |exp| { if (exp.options.section) |section_name| { if (!mem.eql(u8, section_name, ".text")) { - try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1); + try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.count() + 1); module.failed_exports.putAssumeCapacityNoClobber( exp, try Module.ErrorMsg.create(self.base.allocator, decl.srcLoc(), "Unimplemented: ExportOptions.section", .{}), @@ -746,7 +746,7 @@ pub fn updateDeclExports(self: *Coff, module: *Module, decl: *Module.Decl, expor if (mem.eql(u8, exp.options.name, "_start")) { self.entry_addr = decl.link.coff.getVAddr(self.*) - default_image_base; } else { - try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1); + try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.count() + 1); module.failed_exports.putAssumeCapacityNoClobber( exp, try Module.ErrorMsg.create(self.base.allocator, decl.srcLoc(), "Unimplemented: Exports other than '_start'", .{}), @@ -861,8 +861,8 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void { self.base.releaseLock(); try man.addListOfFiles(self.base.options.objects); - for (comp.c_object_table.items()) |entry| { - _ = try man.addFile(entry.key.status.success.object_path, null); + for (comp.c_object_table.keys()) |key| { + _ = try man.addFile(key.status.success.object_path, null); } try man.addOptionalFile(module_obj_path); man.hash.addOptional(self.base.options.stack_size_override); @@ -928,7 +928,7 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void { break :blk self.base.options.objects[0]; if (comp.c_object_table.count() != 0) - break :blk comp.c_object_table.items()[0].key.status.success.object_path; + break :blk comp.c_object_table.keys()[0].status.success.object_path; if (module_obj_path) |p| break :blk p; @@ -1026,8 +1026,8 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void { try argv.appendSlice(self.base.options.objects); - for (comp.c_object_table.items()) |entry| { - try argv.append(entry.key.status.success.object_path); + for (comp.c_object_table.keys()) |key| { + try argv.append(key.status.success.object_path); } if (module_obj_path) |p| { @@ -1221,8 +1221,8 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void { try argv.append(comp.compiler_rt_static_lib.?.full_object_path); } - for (self.base.options.system_libs.items()) |entry| { - const lib_basename = try allocPrint(arena, "{s}.lib", .{entry.key}); + for (self.base.options.system_libs.keys()) |key| { + const lib_basename = try allocPrint(arena, "{s}.lib", .{key}); if (comp.crt_files.get(lib_basename)) |crt_file| { try argv.append(crt_file.full_object_path); } else { diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 599d6fa2bf..5d99a4c3f6 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -1318,8 +1318,8 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void { try man.addOptionalFile(self.base.options.linker_script); try man.addOptionalFile(self.base.options.version_script); try man.addListOfFiles(self.base.options.objects); - for (comp.c_object_table.items()) |entry| { - _ = try man.addFile(entry.key.status.success.object_path, null); + for (comp.c_object_table.keys()) |key| { + _ = try man.addFile(key.status.success.object_path, null); } try man.addOptionalFile(module_obj_path); try man.addOptionalFile(compiler_rt_path); @@ -1394,7 +1394,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void { break :blk self.base.options.objects[0]; if (comp.c_object_table.count() != 0) - break :blk comp.c_object_table.items()[0].key.status.success.object_path; + break :blk comp.c_object_table.keys()[0].status.success.object_path; if (module_obj_path) |p| break :blk p; @@ -1518,8 +1518,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void { var test_path = std.ArrayList(u8).init(self.base.allocator); defer test_path.deinit(); for (self.base.options.lib_dirs) |lib_dir_path| { - for (self.base.options.system_libs.items()) |entry| { - const link_lib = entry.key; + for (self.base.options.system_libs.keys()) |link_lib| { test_path.shrinkRetainingCapacity(0); const sep = fs.path.sep_str; try test_path.writer().print("{s}" ++ sep ++ "lib{s}.so", .{ lib_dir_path, link_lib }); @@ -1568,8 +1567,8 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void { // Positional arguments to the linker such as object files. try argv.appendSlice(self.base.options.objects); - for (comp.c_object_table.items()) |entry| { - try argv.append(entry.key.status.success.object_path); + for (comp.c_object_table.keys()) |key| { + try argv.append(key.status.success.object_path); } if (module_obj_path) |p| { @@ -1598,10 +1597,9 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void { // Shared libraries. if (is_exe_or_dyn_lib) { - const system_libs = self.base.options.system_libs.items(); + const system_libs = self.base.options.system_libs.keys(); try argv.ensureCapacity(argv.items.len + system_libs.len); - for (system_libs) |entry| { - const link_lib = entry.key; + for (system_libs) |link_lib| { // By this time, we depend on these libs being dynamically linked libraries and not static libraries // (the check for that needs to be earlier), but they could be full paths to .so files, in which // case we want to avoid prepending "-l". @@ -2168,9 +2166,9 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void { var dbg_info_type_relocs: File.DbgInfoTypeRelocsTable = .{}; defer { - var it = dbg_info_type_relocs.iterator(); - while (it.next()) |entry| { - entry.value.relocs.deinit(self.base.allocator); + var it = dbg_info_type_relocs.valueIterator(); + while (it.next()) |value| { + value.relocs.deinit(self.base.allocator); } dbg_info_type_relocs.deinit(self.base.allocator); } @@ -2235,12 +2233,12 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void { if (fn_ret_has_bits) { const gop = try dbg_info_type_relocs.getOrPut(self.base.allocator, fn_ret_type); if (!gop.found_existing) { - gop.entry.value = .{ + gop.value_ptr.* = .{ .off = undefined, .relocs = .{}, }; } - try gop.entry.value.relocs.append(self.base.allocator, @intCast(u32, dbg_info_buffer.items.len)); + try gop.value_ptr.relocs.append(self.base.allocator, @intCast(u32, dbg_info_buffer.items.len)); dbg_info_buffer.items.len += 4; // DW.AT_type, DW.FORM_ref4 } dbg_info_buffer.appendSliceAssumeCapacity(decl_name_with_null); // DW.AT_name, DW.FORM_string @@ -2448,24 +2446,28 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void { // Now we emit the .debug_info types of the Decl. These will count towards the size of // the buffer, so we have to do it before computing the offset, and we can't perform the actual // relocations yet. - var it = dbg_info_type_relocs.iterator(); - while (it.next()) |entry| { - entry.value.off = @intCast(u32, dbg_info_buffer.items.len); - try self.addDbgInfoType(entry.key, &dbg_info_buffer); + { + var it = dbg_info_type_relocs.iterator(); + while (it.next()) |entry| { + entry.value_ptr.off = @intCast(u32, dbg_info_buffer.items.len); + try self.addDbgInfoType(entry.key_ptr.*, &dbg_info_buffer); + } } try self.updateDeclDebugInfoAllocation(text_block, @intCast(u32, dbg_info_buffer.items.len)); - // Now that we have the offset assigned we can finally perform type relocations. - it = dbg_info_type_relocs.iterator(); - while (it.next()) |entry| { - for (entry.value.relocs.items) |off| { - mem.writeInt( - u32, - dbg_info_buffer.items[off..][0..4], - text_block.dbg_info_off + entry.value.off, - target_endian, - ); + { + // Now that we have the offset assigned we can finally perform type relocations. + var it = dbg_info_type_relocs.valueIterator(); + while (it.next()) |value| { + for (value.relocs.items) |off| { + mem.writeInt( + u32, + dbg_info_buffer.items[off..][0..4], + text_block.dbg_info_off + value.off, + target_endian, + ); + } } } @@ -2636,7 +2638,7 @@ pub fn updateDeclExports( for (exports) |exp| { if (exp.options.section) |section_name| { if (!mem.eql(u8, section_name, ".text")) { - try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1); + try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.count() + 1); module.failed_exports.putAssumeCapacityNoClobber( exp, try Module.ErrorMsg.create(self.base.allocator, decl.srcLoc(), "Unimplemented: ExportOptions.section", .{}), @@ -2654,7 +2656,7 @@ pub fn updateDeclExports( }, .Weak => elf.STB_WEAK, .LinkOnce => { - try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1); + try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.count() + 1); module.failed_exports.putAssumeCapacityNoClobber( exp, try Module.ErrorMsg.create(self.base.allocator, decl.srcLoc(), "Unimplemented: GlobalLinkage.LinkOnce", .{}), diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 8785005a93..d4d7de15e4 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -567,8 +567,8 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { try man.addOptionalFile(self.base.options.linker_script); try man.addOptionalFile(self.base.options.version_script); try man.addListOfFiles(self.base.options.objects); - for (comp.c_object_table.items()) |entry| { - _ = try man.addFile(entry.key.status.success.object_path, null); + for (comp.c_object_table.keys()) |key| { + _ = try man.addFile(key.status.success.object_path, null); } try man.addOptionalFile(module_obj_path); // We can skip hashing libc and libc++ components that we are in charge of building from Zig @@ -632,7 +632,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { break :blk self.base.options.objects[0]; if (comp.c_object_table.count() != 0) - break :blk comp.c_object_table.items()[0].key.status.success.object_path; + break :blk comp.c_object_table.keys()[0].status.success.object_path; if (module_obj_path) |p| break :blk p; @@ -682,8 +682,8 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { try positionals.appendSlice(self.base.options.objects); - for (comp.c_object_table.items()) |entry| { - try positionals.append(entry.key.status.success.object_path); + for (comp.c_object_table.keys()) |key| { + try positionals.append(key.status.success.object_path); } if (module_obj_path) |p| { @@ -702,9 +702,8 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { var libs = std.ArrayList([]const u8).init(arena); var search_lib_names = std.ArrayList([]const u8).init(arena); - const system_libs = self.base.options.system_libs.items(); - for (system_libs) |entry| { - const link_lib = entry.key; + const system_libs = self.base.options.system_libs.keys(); + for (system_libs) |link_lib| { // By this time, we depend on these libs being dynamically linked libraries and not static libraries // (the check for that needs to be earlier), but they could be full paths to .dylib files, in which // case we want to avoid prepending "-l". @@ -804,8 +803,8 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { var rpaths = std.ArrayList([]const u8).init(arena); try rpaths.ensureCapacity(rpath_table.count()); - for (rpath_table.items()) |entry| { - rpaths.appendAssumeCapacity(entry.key); + for (rpath_table.keys()) |*key| { + rpaths.appendAssumeCapacity(key.*); } if (self.base.options.verbose_link) { @@ -973,8 +972,8 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { // Positional arguments to the linker such as object files. try argv.appendSlice(self.base.options.objects); - for (comp.c_object_table.items()) |entry| { - try argv.append(entry.key.status.success.object_path); + for (comp.c_object_table.keys()) |key| { + try argv.append(key.status.success.object_path); } if (module_obj_path) |p| { try argv.append(p); @@ -986,10 +985,9 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { } // Shared libraries. - const system_libs = self.base.options.system_libs.items(); + const system_libs = self.base.options.system_libs.keys(); try argv.ensureCapacity(argv.items.len + system_libs.len); - for (system_libs) |entry| { - const link_lib = entry.key; + for (system_libs) |link_lib| { // By this time, we depend on these libs being dynamically linked libraries and not static libraries // (the check for that needs to be earlier), but they could be full paths to .dylib files, in which // case we want to avoid prepending "-l". @@ -1153,12 +1151,12 @@ pub fn deinit(self: *MachO) void { if (self.d_sym) |*ds| { ds.deinit(self.base.allocator); } - for (self.lazy_imports.items()) |*entry| { - self.base.allocator.free(entry.key); + for (self.lazy_imports.keys()) |*key| { + self.base.allocator.free(key.*); } self.lazy_imports.deinit(self.base.allocator); - for (self.nonlazy_imports.items()) |*entry| { - self.base.allocator.free(entry.key); + for (self.nonlazy_imports.keys()) |*key| { + self.base.allocator.free(key.*); } self.nonlazy_imports.deinit(self.base.allocator); self.pie_fixups.deinit(self.base.allocator); @@ -1167,9 +1165,9 @@ pub fn deinit(self: *MachO) void { self.offset_table.deinit(self.base.allocator); self.offset_table_free_list.deinit(self.base.allocator); { - var it = self.string_table_directory.iterator(); - while (it.next()) |entry| { - self.base.allocator.free(entry.key); + var it = self.string_table_directory.keyIterator(); + while (it.next()) |key| { + self.base.allocator.free(key.*); } } self.string_table_directory.deinit(self.base.allocator); @@ -1318,9 +1316,9 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void { if (debug_buffers) |*dbg| { dbg.dbg_line_buffer.deinit(); dbg.dbg_info_buffer.deinit(); - var it = dbg.dbg_info_type_relocs.iterator(); - while (it.next()) |entry| { - entry.value.relocs.deinit(self.base.allocator); + var it = dbg.dbg_info_type_relocs.valueIterator(); + while (it.next()) |value| { + value.relocs.deinit(self.base.allocator); } dbg.dbg_info_type_relocs.deinit(self.base.allocator); } @@ -1543,7 +1541,7 @@ pub fn updateDeclExports( if (exp.options.section) |section_name| { if (!mem.eql(u8, section_name, "__text")) { - try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1); + try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.count() + 1); module.failed_exports.putAssumeCapacityNoClobber( exp, try Module.ErrorMsg.create(self.base.allocator, decl.srcLoc(), "Unimplemented: ExportOptions.section", .{}), @@ -1578,7 +1576,7 @@ pub fn updateDeclExports( n_desc |= macho.N_WEAK_DEF; }, .LinkOnce => { - try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1); + try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.count() + 1); module.failed_exports.putAssumeCapacityNoClobber( exp, try Module.ErrorMsg.create(self.base.allocator, decl.srcLoc(), "Unimplemented: GlobalLinkage.LinkOnce", .{}), @@ -2259,7 +2257,7 @@ pub fn populateMissingMetadata(self: *MachO) !void { self.load_commands_dirty = true; } if (!self.nonlazy_imports.contains("dyld_stub_binder")) { - const index = @intCast(u32, self.nonlazy_imports.items().len); + const index = @intCast(u32, self.nonlazy_imports.count()); const name = try self.base.allocator.dupe(u8, "dyld_stub_binder"); const offset = try self.makeString("dyld_stub_binder"); try self.nonlazy_imports.putNoClobber(self.base.allocator, name, .{ @@ -2440,7 +2438,7 @@ fn updateString(self: *MachO, old_str_off: u32, new_name: []const u8) !u32 { } pub fn addExternSymbol(self: *MachO, name: []const u8) !u32 { - const index = @intCast(u32, self.lazy_imports.items().len); + const index = @intCast(u32, self.lazy_imports.count()); const offset = try self.makeString(name); const sym_name = try self.base.allocator.dupe(u8, name); const dylib_ordinal = 1; // TODO this is now hardcoded, since we only support libSystem. @@ -2627,7 +2625,7 @@ fn writeOffsetTableEntry(self: *MachO, index: usize) !void { break :blk self.locals.items[got_entry.symbol]; }, .Extern => { - break :blk self.nonlazy_imports.items()[got_entry.symbol].value.symbol; + break :blk self.nonlazy_imports.values()[got_entry.symbol].symbol; }, } }; @@ -2910,7 +2908,7 @@ fn relocateSymbolTable(self: *MachO) !void { const symtab = &self.load_commands.items[self.symtab_cmd_index.?].Symtab; const nlocals = self.locals.items.len; const nglobals = self.globals.items.len; - const nundefs = self.lazy_imports.items().len + self.nonlazy_imports.items().len; + const nundefs = self.lazy_imports.count() + self.nonlazy_imports.count(); const nsyms = nlocals + nglobals + nundefs; if (symtab.nsyms < nsyms) { @@ -2957,15 +2955,15 @@ fn writeAllGlobalAndUndefSymbols(self: *MachO) !void { const nlocals = self.locals.items.len; const nglobals = self.globals.items.len; - const nundefs = self.lazy_imports.items().len + self.nonlazy_imports.items().len; + const nundefs = self.lazy_imports.count() + self.nonlazy_imports.count(); var undefs = std.ArrayList(macho.nlist_64).init(self.base.allocator); defer undefs.deinit(); try undefs.ensureCapacity(nundefs); - for (self.lazy_imports.items()) |entry| { - undefs.appendAssumeCapacity(entry.value.symbol); + for (self.lazy_imports.values()) |*value| { + undefs.appendAssumeCapacity(value.symbol); } - for (self.nonlazy_imports.items()) |entry| { - undefs.appendAssumeCapacity(entry.value.symbol); + for (self.nonlazy_imports.values()) |*value| { + undefs.appendAssumeCapacity(value.symbol); } const locals_off = symtab.symoff; @@ -3005,10 +3003,10 @@ fn writeIndirectSymbolTable(self: *MachO) !void { const la_symbol_ptr = &data_segment.sections.items[self.la_symbol_ptr_section_index.?]; const dysymtab = &self.load_commands.items[self.dysymtab_cmd_index.?].Dysymtab; - const lazy = self.lazy_imports.items(); + const lazy_count = self.lazy_imports.count(); const got_entries = self.offset_table.items; const allocated_size = self.allocatedSizeLinkedit(dysymtab.indirectsymoff); - const nindirectsyms = @intCast(u32, lazy.len * 2 + got_entries.len); + const nindirectsyms = @intCast(u32, lazy_count * 2 + got_entries.len); const needed_size = @intCast(u32, nindirectsyms * @sizeOf(u32)); if (needed_size > allocated_size) { @@ -3027,12 +3025,15 @@ fn writeIndirectSymbolTable(self: *MachO) !void { var writer = stream.writer(); stubs.reserved1 = 0; - for (lazy) |_, i| { - const symtab_idx = @intCast(u32, dysymtab.iundefsym + i); - try writer.writeIntLittle(u32, symtab_idx); + { + var i: usize = 0; + while (i < lazy_count) : (i += 1) { + const symtab_idx = @intCast(u32, dysymtab.iundefsym + i); + try writer.writeIntLittle(u32, symtab_idx); + } } - const base_id = @intCast(u32, lazy.len); + const base_id = @intCast(u32, lazy_count); got.reserved1 = base_id; for (got_entries) |entry| { switch (entry.kind) { @@ -3047,9 +3048,12 @@ fn writeIndirectSymbolTable(self: *MachO) !void { } la_symbol_ptr.reserved1 = got.reserved1 + @intCast(u32, got_entries.len); - for (lazy) |_, i| { - const symtab_idx = @intCast(u32, dysymtab.iundefsym + i); - try writer.writeIntLittle(u32, symtab_idx); + { + var i: usize = 0; + while (i < lazy_count) : (i += 1) { + const symtab_idx = @intCast(u32, dysymtab.iundefsym + i); + try writer.writeIntLittle(u32, symtab_idx); + } } try self.base.file.?.pwriteAll(buf, dysymtab.indirectsymoff); @@ -3183,15 +3187,15 @@ fn writeRebaseInfoTable(self: *MachO) !void { } if (self.la_symbol_ptr_section_index) |idx| { - try pointers.ensureCapacity(pointers.items.len + self.lazy_imports.items().len); + try pointers.ensureCapacity(pointers.items.len + self.lazy_imports.count()); const seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment; const sect = seg.sections.items[idx]; const base_offset = sect.addr - seg.inner.vmaddr; const segment_id = self.data_segment_cmd_index.?; - for (self.lazy_imports.items()) |entry| { + for (self.lazy_imports.values()) |*value| { pointers.appendAssumeCapacity(.{ - .offset = base_offset + entry.value.index * @sizeOf(u64), + .offset = base_offset + value.index * @sizeOf(u64), .segment_id = segment_id, }); } @@ -3241,12 +3245,13 @@ fn writeBindingInfoTable(self: *MachO) !void { for (self.offset_table.items) |entry| { if (entry.kind == .Local) continue; - const import = self.nonlazy_imports.items()[entry.symbol]; + const import_key = self.nonlazy_imports.keys()[entry.symbol]; + const import_ordinal = self.nonlazy_imports.values()[entry.symbol].dylib_ordinal; try pointers.append(.{ .offset = base_offset + entry.index * @sizeOf(u64), .segment_id = segment_id, - .dylib_ordinal = import.value.dylib_ordinal, - .name = import.key, + .dylib_ordinal = import_ordinal, + .name = import_key, }); } } @@ -3286,18 +3291,21 @@ fn writeLazyBindingInfoTable(self: *MachO) !void { defer pointers.deinit(); if (self.la_symbol_ptr_section_index) |idx| { - try pointers.ensureCapacity(self.lazy_imports.items().len); + try pointers.ensureCapacity(self.lazy_imports.count()); const seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment; const sect = seg.sections.items[idx]; const base_offset = sect.addr - seg.inner.vmaddr; const segment_id = @intCast(u16, self.data_segment_cmd_index.?); - for (self.lazy_imports.items()) |entry| { + const slice = self.lazy_imports.entries.slice(); + const keys = slice.items(.key); + const values = slice.items(.value); + for (keys) |*key, i| { pointers.appendAssumeCapacity(.{ - .offset = base_offset + entry.value.index * @sizeOf(u64), + .offset = base_offset + values[i].index * @sizeOf(u64), .segment_id = segment_id, - .dylib_ordinal = entry.value.dylib_ordinal, - .name = entry.key, + .dylib_ordinal = values[i].dylib_ordinal, + .name = key.*, }); } } @@ -3329,7 +3337,7 @@ fn writeLazyBindingInfoTable(self: *MachO) !void { } fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void { - if (self.lazy_imports.items().len == 0) return; + if (self.lazy_imports.count() == 0) return; var stream = std.io.fixedBufferStream(buffer); var reader = stream.reader(); @@ -3375,7 +3383,7 @@ fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void { else => {}, } } - assert(self.lazy_imports.items().len <= offsets.items.len); + assert(self.lazy_imports.count() <= offsets.items.len); const stub_size: u4 = switch (self.base.options.target.cpu.arch) { .x86_64 => 10, @@ -3388,9 +3396,9 @@ fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void { else => unreachable, }; var buf: [@sizeOf(u32)]u8 = undefined; - for (self.lazy_imports.items()) |_, i| { + for (offsets.items[0..self.lazy_imports.count()]) |offset, i| { const placeholder_off = self.stub_helper_stubs_start_off.? + i * stub_size + off; - mem.writeIntLittle(u32, &buf, offsets.items[i]); + mem.writeIntLittle(u32, &buf, offset); try self.base.file.?.pwriteAll(&buf, placeholder_off); } } diff --git a/src/link/MachO/Archive.zig b/src/link/MachO/Archive.zig index f519aa27be..f47228077c 100644 --- a/src/link/MachO/Archive.zig +++ b/src/link/MachO/Archive.zig @@ -92,9 +92,11 @@ pub fn init(allocator: *Allocator) Archive { } pub fn deinit(self: *Archive) void { - for (self.toc.items()) |*entry| { - self.allocator.free(entry.key); - entry.value.deinit(self.allocator); + for (self.toc.keys()) |*key| { + self.allocator.free(key.*); + } + for (self.toc.values()) |*value| { + value.deinit(self.allocator); } self.toc.deinit(self.allocator); @@ -187,10 +189,10 @@ fn parseTableOfContents(self: *Archive, reader: anytype) !void { defer if (res.found_existing) self.allocator.free(owned_name); if (!res.found_existing) { - res.entry.value = .{}; + res.value_ptr.* = .{}; } - try res.entry.value.append(self.allocator, object_offset); + try res.value_ptr.append(self.allocator, object_offset); } } diff --git a/src/link/MachO/DebugSymbols.zig b/src/link/MachO/DebugSymbols.zig index 7c653d1983..ca6e5157b3 100644 --- a/src/link/MachO/DebugSymbols.zig +++ b/src/link/MachO/DebugSymbols.zig @@ -997,12 +997,12 @@ pub fn initDeclDebugBuffers( if (fn_ret_has_bits) { const gop = try dbg_info_type_relocs.getOrPut(allocator, fn_ret_type); if (!gop.found_existing) { - gop.entry.value = .{ + gop.value_ptr.* = .{ .off = undefined, .relocs = .{}, }; } - try gop.entry.value.relocs.append(allocator, @intCast(u32, dbg_info_buffer.items.len)); + try gop.value_ptr.relocs.append(allocator, @intCast(u32, dbg_info_buffer.items.len)); dbg_info_buffer.items.len += 4; // DW.AT_type, DW.FORM_ref4 } dbg_info_buffer.appendSliceAssumeCapacity(decl_name_with_null); // DW.AT_name, DW.FORM_string @@ -1158,26 +1158,30 @@ pub fn commitDeclDebugInfo( if (dbg_info_buffer.items.len == 0) return; - // Now we emit the .debug_info types of the Decl. These will count towards the size of - // the buffer, so we have to do it before computing the offset, and we can't perform the actual - // relocations yet. - var it = dbg_info_type_relocs.iterator(); - while (it.next()) |entry| { - entry.value.off = @intCast(u32, dbg_info_buffer.items.len); - try self.addDbgInfoType(entry.key, dbg_info_buffer, target); + { + // Now we emit the .debug_info types of the Decl. These will count towards the size of + // the buffer, so we have to do it before computing the offset, and we can't perform the actual + // relocations yet. + var it = dbg_info_type_relocs.iterator(); + while (it.next()) |entry| { + entry.value_ptr.off = @intCast(u32, dbg_info_buffer.items.len); + try self.addDbgInfoType(entry.key_ptr.*, dbg_info_buffer, target); + } } try self.updateDeclDebugInfoAllocation(allocator, text_block, @intCast(u32, dbg_info_buffer.items.len)); - // Now that we have the offset assigned we can finally perform type relocations. - it = dbg_info_type_relocs.iterator(); - while (it.next()) |entry| { - for (entry.value.relocs.items) |off| { - mem.writeIntLittle( - u32, - dbg_info_buffer.items[off..][0..4], - text_block.dbg_info_off + entry.value.off, - ); + { + // Now that we have the offset assigned we can finally perform type relocations. + var it = dbg_info_type_relocs.valueIterator(); + while (it.next()) |value| { + for (value.relocs.items) |off| { + mem.writeIntLittle( + u32, + dbg_info_buffer.items[off..][0..4], + text_block.dbg_info_off + value.off, + ); + } } } diff --git a/src/link/MachO/Dylib.zig b/src/link/MachO/Dylib.zig index d7039f7f6c..a2a703dc08 100644 --- a/src/link/MachO/Dylib.zig +++ b/src/link/MachO/Dylib.zig @@ -50,9 +50,9 @@ pub fn deinit(self: *Dylib) void { } self.load_commands.deinit(self.allocator); - for (self.symbols.items()) |entry| { - entry.value.deinit(self.allocator); - self.allocator.destroy(entry.value); + for (self.symbols.values()) |value| { + value.deinit(self.allocator); + self.allocator.destroy(value); } self.symbols.deinit(self.allocator); diff --git a/src/link/MachO/Zld.zig b/src/link/MachO/Zld.zig index e1ad69b925..c5362b8d7d 100644 --- a/src/link/MachO/Zld.zig +++ b/src/link/MachO/Zld.zig @@ -168,9 +168,9 @@ pub fn deinit(self: *Zld) void { self.strtab.deinit(self.allocator); { - var it = self.strtab_dir.iterator(); - while (it.next()) |entry| { - self.allocator.free(entry.key); + var it = self.strtab_dir.keyIterator(); + while (it.next()) |key| { + self.allocator.free(key.*); } } self.strtab_dir.deinit(self.allocator); @@ -954,9 +954,8 @@ fn sortSections(self: *Zld) !void { } } - var it = self.mappings.iterator(); - while (it.next()) |entry| { - const mapping = &entry.value; + var it = self.mappings.valueIterator(); + while (it.next()) |mapping| { if (self.text_segment_cmd_index.? == mapping.target_seg_id) { const new_index = text_index_mapping.get(mapping.target_sect_id) orelse unreachable; mapping.target_sect_id = new_index; @@ -1400,16 +1399,16 @@ fn resolveSymbolsInObject(self: *Zld, object: *Object) !void { if (sym.cast(Symbol.Regular)) |reg| { if (reg.linkage == .translation_unit) continue; // Symbol local to TU. - if (self.unresolved.swapRemove(sym.name)) |entry| { + if (self.unresolved.fetchSwapRemove(sym.name)) |kv| { // Create link to the global. - entry.value.alias = sym; + kv.value.alias = sym; } - const entry = self.globals.getEntry(sym.name) orelse { + const sym_ptr = self.globals.getPtr(sym.name) orelse { // Put new global symbol into the symbol table. try self.globals.putNoClobber(self.allocator, sym.name, sym); continue; }; - const g_sym = entry.value; + const g_sym = sym_ptr.*; const g_reg = g_sym.cast(Symbol.Regular) orelse unreachable; switch (g_reg.linkage) { @@ -1432,7 +1431,7 @@ fn resolveSymbolsInObject(self: *Zld, object: *Object) !void { } g_sym.alias = sym; - entry.value = sym; + sym_ptr.* = sym; } else if (sym.cast(Symbol.Unresolved)) |und| { if (self.globals.get(sym.name)) |g_sym| { sym.alias = g_sym; @@ -1458,8 +1457,7 @@ fn resolveSymbols(self: *Zld) !void { while (true) { if (next_sym == self.unresolved.count()) break; - const entry = self.unresolved.items()[next_sym]; - const sym = entry.value; + const sym = self.unresolved.values()[next_sym]; var reset: bool = false; for (self.archives.items) |archive| { @@ -1492,8 +1490,8 @@ fn resolveSymbols(self: *Zld) !void { defer unresolved.deinit(); try unresolved.ensureCapacity(self.unresolved.count()); - for (self.unresolved.items()) |entry| { - unresolved.appendAssumeCapacity(entry.value); + for (self.unresolved.values()) |value| { + unresolved.appendAssumeCapacity(value); } self.unresolved.clearAndFree(self.allocator); @@ -2780,8 +2778,7 @@ fn writeSymbolTable(self: *Zld) !void { var undefs = std.ArrayList(macho.nlist_64).init(self.allocator); defer undefs.deinit(); - for (self.imports.items()) |entry| { - const sym = entry.value; + for (self.imports.values()) |sym| { const ordinal = ordinal: { const dylib = sym.cast(Symbol.Proxy).?.dylib orelse break :ordinal 1; // TODO handle libSystem break :ordinal dylib.ordinal.?; @@ -3071,9 +3068,9 @@ pub fn parseName(name: *const [16]u8) []const u8 { fn printSymbols(self: *Zld) void { log.debug("globals", .{}); - for (self.globals.items()) |entry| { - const sym = entry.value.cast(Symbol.Regular) orelse unreachable; - log.debug(" | {s} @ {*}", .{ sym.base.name, entry.value }); + for (self.globals.values()) |value| { + const sym = value.cast(Symbol.Regular) orelse unreachable; + log.debug(" | {s} @ {*}", .{ sym.base.name, value }); log.debug(" => alias of {*}", .{sym.base.alias}); log.debug(" => linkage {s}", .{sym.linkage}); log.debug(" => defined in {s}", .{sym.file.name.?}); @@ -3091,9 +3088,9 @@ fn printSymbols(self: *Zld) void { } } log.debug("proxies", .{}); - for (self.imports.items()) |entry| { - const sym = entry.value.cast(Symbol.Proxy) orelse unreachable; - log.debug(" | {s} @ {*}", .{ sym.base.name, entry.value }); + for (self.imports.values()) |value| { + const sym = value.cast(Symbol.Proxy) orelse unreachable; + log.debug(" | {s} @ {*}", .{ sym.base.name, value }); log.debug(" => alias of {*}", .{sym.base.alias}); log.debug(" => defined in libSystem.B.dylib", .{}); } diff --git a/src/link/SpirV.zig b/src/link/SpirV.zig index a84a777e61..f80e4aec01 100644 --- a/src/link/SpirV.zig +++ b/src/link/SpirV.zig @@ -114,7 +114,7 @@ pub fn updateDeclExports( ) !void {} pub fn freeDecl(self: *SpirV, decl: *Module.Decl) void { - self.decl_table.removeAssertDiscard(decl); + assert(self.decl_table.swapRemove(decl)); } pub fn flush(self: *SpirV, comp: *Compilation) !void { @@ -141,8 +141,7 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void { // declarations which don't generate a result? // TODO: fn_link is used here, but thats probably not the right field. It will work anyway though. { - for (self.decl_table.items()) |entry| { - const decl = entry.key; + for (self.decl_table.keys()) |decl| { if (!decl.has_tv) continue; decl.fn_link.spirv.id = spv.allocResultId(); @@ -154,8 +153,7 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void { var decl_gen = codegen.DeclGen.init(&spv); defer decl_gen.deinit(); - for (self.decl_table.items()) |entry| { - const decl = entry.key; + for (self.decl_table.keys()) |decl| { if (!decl.has_tv) continue; if (try decl_gen.gen(decl)) |msg| { diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index fe72017327..77283384be 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -422,8 +422,8 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { const header_offset = try reserveVecSectionHeader(file); const writer = file.writer(); var count: u32 = 0; - for (module.decl_exports.entries.items) |entry| { - for (entry.value) |exprt| { + for (module.decl_exports.values()) |exports| { + for (exports) |exprt| { // Export name length + name try leb.writeULEB128(writer, @intCast(u32, exprt.options.name.len)); try writer.writeAll(exprt.options.name); @@ -590,8 +590,8 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { self.base.releaseLock(); try man.addListOfFiles(self.base.options.objects); - for (comp.c_object_table.items()) |entry| { - _ = try man.addFile(entry.key.status.success.object_path, null); + for (comp.c_object_table.keys()) |key| { + _ = try man.addFile(key.status.success.object_path, null); } try man.addOptionalFile(module_obj_path); try man.addOptionalFile(compiler_rt_path); @@ -638,7 +638,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { break :blk self.base.options.objects[0]; if (comp.c_object_table.count() != 0) - break :blk comp.c_object_table.items()[0].key.status.success.object_path; + break :blk comp.c_object_table.keys()[0].status.success.object_path; if (module_obj_path) |p| break :blk p; @@ -712,8 +712,8 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { // Positional arguments to the linker such as object files. try argv.appendSlice(self.base.options.objects); - for (comp.c_object_table.items()) |entry| { - try argv.append(entry.key.status.success.object_path); + for (comp.c_object_table.keys()) |key| { + try argv.append(key.status.success.object_path); } if (module_obj_path) |p| { try argv.append(p); diff --git a/src/liveness.zig b/src/liveness.zig index 2dc04be4ef..d115af77ed 100644 --- a/src/liveness.zig +++ b/src/liveness.zig @@ -2,6 +2,7 @@ const std = @import("std"); const ir = @import("air.zig"); const trace = @import("tracy.zig").trace; const log = std.log.scoped(.liveness); +const assert = std.debug.assert; /// Perform Liveness Analysis over the `Body`. Each `Inst` will have its `deaths` field populated. pub fn analyze( @@ -86,9 +87,9 @@ fn analyzeInst( // Reset the table back to its state from before the branch. { - var it = then_table.iterator(); - while (it.next()) |entry| { - table.removeAssertDiscard(entry.key); + var it = then_table.keyIterator(); + while (it.next()) |key| { + assert(table.remove(key.*)); } } @@ -102,9 +103,9 @@ fn analyzeInst( defer else_entry_deaths.deinit(); { - var it = else_table.iterator(); - while (it.next()) |entry| { - const else_death = entry.key; + var it = else_table.keyIterator(); + while (it.next()) |key| { + const else_death = key.*; if (!then_table.contains(else_death)) { try then_entry_deaths.append(else_death); } @@ -113,9 +114,9 @@ fn analyzeInst( // This loop is the same, except it's for the then branch, and it additionally // has to put its items back into the table to undo the reset. { - var it = then_table.iterator(); - while (it.next()) |entry| { - const then_death = entry.key; + var it = then_table.keyIterator(); + while (it.next()) |key| { + const then_death = key.*; if (!else_table.contains(then_death)) { try else_entry_deaths.append(then_death); } @@ -125,13 +126,13 @@ fn analyzeInst( // Now we have to correctly populate new_set. if (new_set) |ns| { try ns.ensureCapacity(@intCast(u32, ns.count() + then_table.count() + else_table.count())); - var it = then_table.iterator(); - while (it.next()) |entry| { - _ = ns.putAssumeCapacity(entry.key, {}); + var it = then_table.keyIterator(); + while (it.next()) |key| { + _ = ns.putAssumeCapacity(key.*, {}); } - it = else_table.iterator(); - while (it.next()) |entry| { - _ = ns.putAssumeCapacity(entry.key, {}); + it = else_table.keyIterator(); + while (it.next()) |key| { + _ = ns.putAssumeCapacity(key.*, {}); } } inst.then_death_count = std.math.cast(@TypeOf(inst.then_death_count), then_entry_deaths.items.len) catch return error.OutOfMemory; @@ -159,18 +160,18 @@ fn analyzeInst( try analyzeWithTable(arena, table, &case_tables[i], case.body); // Reset the table back to its state from before the case. - var it = case_tables[i].iterator(); - while (it.next()) |entry| { - table.removeAssertDiscard(entry.key); + var it = case_tables[i].keyIterator(); + while (it.next()) |key| { + assert(table.remove(key.*)); } } { // else try analyzeWithTable(arena, table, &case_tables[case_tables.len - 1], inst.else_body); // Reset the table back to its state from before the case. - var it = case_tables[case_tables.len - 1].iterator(); - while (it.next()) |entry| { - table.removeAssertDiscard(entry.key); + var it = case_tables[case_tables.len - 1].keyIterator(); + while (it.next()) |key| { + assert(table.remove(key.*)); } } @@ -184,9 +185,9 @@ fn analyzeInst( var total_deaths: u32 = 0; for (case_tables) |*ct, i| { total_deaths += ct.count(); - var it = ct.iterator(); - while (it.next()) |entry| { - const case_death = entry.key; + var it = ct.keyIterator(); + while (it.next()) |key| { + const case_death = key.*; for (case_tables) |*ct_inner, j| { if (i == j) continue; if (!ct_inner.contains(case_death)) { @@ -203,9 +204,9 @@ fn analyzeInst( if (new_set) |ns| { try ns.ensureCapacity(@intCast(u32, ns.count() + total_deaths)); for (case_tables) |*ct| { - var it = ct.iterator(); - while (it.next()) |entry| { - _ = ns.putAssumeCapacity(entry.key, {}); + var it = ct.keyIterator(); + while (it.next()) |key| { + _ = ns.putAssumeCapacity(key.*, {}); } } } diff --git a/src/main.zig b/src/main.zig index 2017f17789..9245f5fd8a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -180,7 +180,7 @@ pub fn mainArgs(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v "in order to determine where libc is installed. However the system C " ++ "compiler is `zig cc`, so no libc installation was found.", .{}); } - try env_map.set(inf_loop_env_key, "1"); + try env_map.put(inf_loop_env_key, "1"); // Some programs such as CMake will strip the `cc` and subsequent args from the // CC environment variable. We detect and support this scenario here because of @@ -2310,9 +2310,9 @@ fn updateModule(gpa: *Allocator, comp: *Compilation, hook: AfterUpdateHook) !voi fn freePkgTree(gpa: *Allocator, pkg: *Package, free_parent: bool) void { { - var it = pkg.table.iterator(); - while (it.next()) |kv| { - freePkgTree(gpa, kv.value, true); + var it = pkg.table.valueIterator(); + while (it.next()) |value| { + freePkgTree(gpa, value.*, true); } } if (free_parent) { @@ -3895,7 +3895,7 @@ pub fn cmdChangelist( var it = inst_map.iterator(); while (it.next()) |entry| { try stdout.print(" %{d} => %{d}\n", .{ - entry.key, entry.value, + entry.key_ptr.*, entry.value_ptr.*, }); } } @@ -3904,7 +3904,7 @@ pub fn cmdChangelist( var it = extra_map.iterator(); while (it.next()) |entry| { try stdout.print(" {d} => {d}\n", .{ - entry.key, entry.value, + entry.key_ptr.*, entry.value_ptr.*, }); } } diff --git a/src/musl.zig b/src/musl.zig index 5049dbb371..4246313eb5 100644 --- a/src/musl.zig +++ b/src/musl.zig @@ -135,9 +135,10 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void { const s = path.sep_str; - for (source_table.items()) |entry| { - const src_file = entry.key; - const ext = entry.value; + var it = source_table.iterator(); + while (it.next()) |entry| { + const src_file = entry.key_ptr.*; + const ext = entry.value_ptr.*; const dirname = path.dirname(src_file).?; const basename = path.basename(src_file); diff --git a/src/translate_c.zig b/src/translate_c.zig index ff51dd0f9f..d43ec7bcb1 100644 --- a/src/translate_c.zig +++ b/src/translate_c.zig @@ -453,7 +453,7 @@ fn declVisitorNamesOnly(c: *Context, decl: *const clang.Decl) Error!void { // Don't put this one in `decl_table` so it's processed later. return; } - result.entry.value = name; + result.value_ptr.* = name; // Put this typedef in the decl_table to avoid redefinitions. try c.decl_table.putNoClobber(c.gpa, @ptrToInt(typedef_decl.getCanonicalDecl()), name); } @@ -5765,14 +5765,14 @@ fn getFnProto(c: *Context, ref: Node) ?*ast.Payload.Func { fn addMacros(c: *Context) !void { var it = c.global_scope.macro_table.iterator(); - while (it.next()) |kv| { - if (getFnProto(c, kv.value)) |proto_node| { + while (it.next()) |entry| { + if (getFnProto(c, entry.value_ptr.*)) |proto_node| { // If a macro aliases a global variable which is a function pointer, we conclude that // the macro is intended to represent a function that assumes the function pointer // variable is non-null and calls it. - try addTopLevelDecl(c, kv.key, try transCreateNodeMacroFn(c, kv.key, kv.value, proto_node)); + try addTopLevelDecl(c, entry.key_ptr.*, try transCreateNodeMacroFn(c, entry.key_ptr.*, entry.value_ptr.*, proto_node)); } else { - try addTopLevelDecl(c, kv.key, kv.value); + try addTopLevelDecl(c, entry.key_ptr.*, entry.value_ptr.*); } } } diff --git a/src/type.zig b/src/type.zig index 14646450aa..3e6c810b20 100644 --- a/src/type.zig +++ b/src/type.zig @@ -596,6 +596,15 @@ pub const Type = extern union { return hasher.final(); } + pub const HashContext = struct { + pub fn hash(self: @This(), t: Type) u64 { + return t.hash(); + } + pub fn eql(self: @This(), a: Type, b: Type) bool { + return a.eql(b); + } + }; + pub fn copy(self: Type, allocator: *Allocator) error{OutOfMemory}!Type { if (self.tag_if_small_enough < Tag.no_payload_count) { return Type{ .tag_if_small_enough = self.tag_if_small_enough }; @@ -1147,8 +1156,8 @@ pub const Type = extern union { .@"struct" => { // TODO introduce lazy value mechanism const struct_obj = self.castTag(.@"struct").?.data; - for (struct_obj.fields.entries.items) |entry| { - if (entry.value.ty.hasCodeGenBits()) + for (struct_obj.fields.values()) |value| { + if (value.ty.hasCodeGenBits()) return true; } else { return false; @@ -1169,8 +1178,8 @@ pub const Type = extern union { }, .@"union" => { const union_obj = self.castTag(.@"union").?.data; - for (union_obj.fields.entries.items) |entry| { - if (entry.value.ty.hasCodeGenBits()) + for (union_obj.fields.values()) |value| { + if (value.ty.hasCodeGenBits()) return true; } else { return false; @@ -1181,8 +1190,8 @@ pub const Type = extern union { if (union_obj.tag_ty.hasCodeGenBits()) { return true; } - for (union_obj.fields.entries.items) |entry| { - if (entry.value.ty.hasCodeGenBits()) + for (union_obj.fields.values()) |value| { + if (value.ty.hasCodeGenBits()) return true; } else { return false; @@ -1380,10 +1389,9 @@ pub const Type = extern union { // like we have in stage1. const struct_obj = self.castTag(.@"struct").?.data; var biggest: u32 = 0; - for (struct_obj.fields.entries.items) |entry| { - const field_ty = entry.value.ty; - if (!field_ty.hasCodeGenBits()) continue; - const field_align = field_ty.abiAlignment(target); + for (struct_obj.fields.values()) |field| { + if (!field.ty.hasCodeGenBits()) continue; + const field_align = field.ty.abiAlignment(target); if (field_align > biggest) { return field_align; } @@ -1399,10 +1407,9 @@ pub const Type = extern union { .union_tagged => { const union_obj = self.castTag(.union_tagged).?.data; var biggest: u32 = union_obj.tag_ty.abiAlignment(target); - for (union_obj.fields.entries.items) |entry| { - const field_ty = entry.value.ty; - if (!field_ty.hasCodeGenBits()) continue; - const field_align = field_ty.abiAlignment(target); + for (union_obj.fields.values()) |field| { + if (!field.ty.hasCodeGenBits()) continue; + const field_align = field.ty.abiAlignment(target); if (field_align > biggest) { biggest = field_align; } @@ -1413,10 +1420,9 @@ pub const Type = extern union { .@"union" => { const union_obj = self.castTag(.@"union").?.data; var biggest: u32 = 0; - for (union_obj.fields.entries.items) |entry| { - const field_ty = entry.value.ty; - if (!field_ty.hasCodeGenBits()) continue; - const field_align = field_ty.abiAlignment(target); + for (union_obj.fields.values()) |field| { + if (!field.ty.hasCodeGenBits()) continue; + const field_align = field.ty.abiAlignment(target); if (field_align > biggest) { biggest = field_align; } @@ -2415,9 +2421,8 @@ pub const Type = extern union { .@"struct" => { const s = ty.castTag(.@"struct").?.data; assert(s.haveFieldTypes()); - for (s.fields.entries.items) |entry| { - const field_ty = entry.value.ty; - if (field_ty.onePossibleValue() == null) { + for (s.fields.values()) |field| { + if (field.ty.onePossibleValue() == null) { return null; } } @@ -2426,7 +2431,7 @@ pub const Type = extern union { .enum_full => { const enum_full = ty.castTag(.enum_full).?.data; if (enum_full.fields.count() == 1) { - return enum_full.values.entries.items[0].key; + return enum_full.values.keys()[0]; } else { return null; } @@ -2583,11 +2588,11 @@ pub const Type = extern union { switch (ty.tag()) { .enum_full, .enum_nonexhaustive => { const enum_full = ty.cast(Payload.EnumFull).?.data; - return enum_full.fields.entries.items[field_index].key; + return enum_full.fields.keys()[field_index]; }, .enum_simple => { const enum_simple = ty.castTag(.enum_simple).?.data; - return enum_simple.fields.entries.items[field_index].key; + return enum_simple.fields.keys()[field_index]; }, .atomic_ordering, .atomic_rmw_op, diff --git a/src/value.zig b/src/value.zig index d8256315da..9c29bdf80c 100644 --- a/src/value.zig +++ b/src/value.zig @@ -1256,6 +1256,23 @@ pub const Value = extern union { return hasher.final(); } + pub const ArrayHashContext = struct { + pub fn hash(self: @This(), v: Value) u32 { + return v.hash_u32(); + } + pub fn eql(self: @This(), a: Value, b: Value) bool { + return a.eql(b); + } + }; + pub const HashContext = struct { + pub fn hash(self: @This(), v: Value) u64 { + return v.hash(); + } + pub fn eql(self: @This(), a: Value, b: Value) bool { + return a.eql(b); + } + }; + /// Asserts the value is a pointer and dereferences it. /// Returns error.AnalysisFail if the pointer points to a Decl that failed semantic analysis. pub fn pointerDeref(self: Value, allocator: *Allocator) error{ AnalysisFail, OutOfMemory }!Value { -- cgit v1.2.3