aboutsummaryrefslogtreecommitdiff
path: root/src/Module.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-11-30 01:44:34 -0500
committerGitHub <noreply@github.com>2022-11-30 01:44:34 -0500
commite35f297aeb993ec956ae80379ddf7f86069e109b (patch)
tree45cbb5b3ebbe23a46e27b04aa5898a6c00ec4a61 /src/Module.zig
parentdeda6b514691c3a7ffc7931469886d0e7be2f67e (diff)
parentf4666678886c2a7a993ad30b63de4ff25594085a (diff)
downloadzig-e35f297aeb993ec956ae80379ddf7f86069e109b.tar.gz
zig-e35f297aeb993ec956ae80379ddf7f86069e109b.zip
Merge pull request #13666 from ziglang/allocator-interface
std.mem.Allocator: allow shrink to fail
Diffstat (limited to 'src/Module.zig')
-rw-r--r--src/Module.zig60
1 files changed, 34 insertions, 26 deletions
diff --git a/src/Module.zig b/src/Module.zig
index 8c45fc233f..a18297c8ff 100644
--- a/src/Module.zig
+++ b/src/Module.zig
@@ -53,12 +53,12 @@ local_zir_cache: Compilation.Directory,
/// map of Decl indexes to details about them being exported.
/// The Export memory is owned by the `export_owners` table; the slice itself
/// is owned by this table. The slice is guaranteed to not be empty.
-decl_exports: std.AutoArrayHashMapUnmanaged(Decl.Index, []*Export) = .{},
+decl_exports: std.AutoArrayHashMapUnmanaged(Decl.Index, ArrayListUnmanaged(*Export)) = .{},
/// This models the Decls that perform exports, so that `decl_exports` can be updated when a Decl
/// is modified. Note that the key of this table is not the Decl being exported, but the Decl that
/// is performing the export of another Decl.
/// This table owns the Export memory.
-export_owners: std.AutoArrayHashMapUnmanaged(Decl.Index, []*Export) = .{},
+export_owners: std.AutoArrayHashMapUnmanaged(Decl.Index, ArrayListUnmanaged(*Export)) = .{},
/// The set of all the Zig source files in the Module. We keep track of this in order
/// to iterate over it and check which source files have been modified on the file system when
/// an update is requested, as well as to cache `@import` results.
@@ -80,7 +80,7 @@ embed_table: std.StringHashMapUnmanaged(*EmbedFile) = .{},
/// This table uses an optional index so that when a Decl is destroyed, the string literal
/// is still reclaimable by a future Decl.
string_literal_table: std.HashMapUnmanaged(StringLiteralContext.Key, Decl.OptionalIndex, StringLiteralContext, std.hash_map.default_max_load_percentage) = .{},
-string_literal_bytes: std.ArrayListUnmanaged(u8) = .{},
+string_literal_bytes: ArrayListUnmanaged(u8) = .{},
/// The set of all the generic function instantiations. This is used so that when a generic
/// function is called twice with the same comptime parameter arguments, both calls dispatch
@@ -163,7 +163,7 @@ test_functions: std.AutoArrayHashMapUnmanaged(Decl.Index, void) = .{},
/// multi-threaded contention on an atomic counter.
allocated_decls: std.SegmentedList(Decl, 0) = .{},
/// When a Decl object is freed from `allocated_decls`, it is pushed into this stack.
-decls_free_list: std.ArrayListUnmanaged(Decl.Index) = .{},
+decls_free_list: ArrayListUnmanaged(Decl.Index) = .{},
global_assembly: std.AutoHashMapUnmanaged(Decl.Index, []u8) = .{},
@@ -173,7 +173,7 @@ reference_table: std.AutoHashMapUnmanaged(Decl.Index, struct {
}) = .{},
pub const StringLiteralContext = struct {
- bytes: *std.ArrayListUnmanaged(u8),
+ bytes: *ArrayListUnmanaged(u8),
pub const Key = struct {
index: u32,
@@ -192,7 +192,7 @@ pub const StringLiteralContext = struct {
};
pub const StringLiteralAdapter = struct {
- bytes: *std.ArrayListUnmanaged(u8),
+ bytes: *ArrayListUnmanaged(u8),
pub fn eql(self: @This(), a_slice: []const u8, b: StringLiteralContext.Key) bool {
const b_slice = self.bytes.items[b.index..][0..b.len];
@@ -1896,11 +1896,11 @@ pub const File = struct {
/// Used by change detection algorithm, after astgen, contains the
/// set of decls that existed in the previous ZIR but not in the new one.
- deleted_decls: std.ArrayListUnmanaged(Decl.Index) = .{},
+ deleted_decls: ArrayListUnmanaged(Decl.Index) = .{},
/// Used by change detection algorithm, after astgen, contains the
/// set of decls that existed both in the previous ZIR and in the new one,
/// but their source code has been modified.
- outdated_decls: std.ArrayListUnmanaged(Decl.Index) = .{},
+ outdated_decls: ArrayListUnmanaged(Decl.Index) = .{},
/// The most recent successful ZIR for this file, with no errors.
/// This is only populated when a previously successful ZIR
@@ -3438,12 +3438,12 @@ pub fn deinit(mod: *Module) void {
mod.compile_log_decls.deinit(gpa);
- for (mod.decl_exports.values()) |export_list| {
- gpa.free(export_list);
+ for (mod.decl_exports.values()) |*export_list| {
+ export_list.deinit(gpa);
}
mod.decl_exports.deinit(gpa);
- for (mod.export_owners.values()) |value| {
+ for (mod.export_owners.values()) |*value| {
freeExportList(gpa, value);
}
mod.export_owners.deinit(gpa);
@@ -3533,13 +3533,13 @@ pub fn declIsRoot(mod: *Module, decl_index: Decl.Index) bool {
return decl_index == decl.src_namespace.getDeclIndex();
}
-fn freeExportList(gpa: Allocator, export_list: []*Export) void {
- for (export_list) |exp| {
+fn freeExportList(gpa: Allocator, export_list: *ArrayListUnmanaged(*Export)) void {
+ for (export_list.items) |exp| {
gpa.free(exp.options.name);
if (exp.options.section) |s| gpa.free(s);
gpa.destroy(exp);
}
- gpa.free(export_list);
+ export_list.deinit(gpa);
}
const data_has_safety_tag = @sizeOf(Zir.Inst.Data) != 8;
@@ -3822,7 +3822,7 @@ pub fn astGenFile(mod: *Module, file: *File) !void {
.byte_abs = token_starts[parse_err.token] + extra_offset,
},
},
- .msg = msg.toOwnedSlice(),
+ .msg = try msg.toOwnedSlice(),
};
if (token_tags[parse_err.token + @boolToInt(parse_err.token_is_prev)] == .invalid) {
const bad_off = @intCast(u32, file.tree.tokenSlice(parse_err.token + @boolToInt(parse_err.token_is_prev)).len);
@@ -3845,7 +3845,7 @@ pub fn astGenFile(mod: *Module, file: *File) !void {
.parent_decl_node = 0,
.lazy = .{ .token_abs = note.token },
},
- .msg = msg.toOwnedSlice(),
+ .msg = try msg.toOwnedSlice(),
};
}
@@ -3981,7 +3981,7 @@ fn updateZirRefs(mod: *Module, file: *File, old_zir: Zir) !void {
// Walk the Decl graph, updating ZIR indexes, strings, and populating
// the deleted and outdated lists.
- var decl_stack: std.ArrayListUnmanaged(Decl.Index) = .{};
+ var decl_stack: ArrayListUnmanaged(Decl.Index) = .{};
defer decl_stack.deinit(gpa);
const root_decl = file.root_decl.unwrap().?;
@@ -4146,7 +4146,7 @@ pub fn mapOldZirToNew(
old_inst: Zir.Inst.Index,
new_inst: Zir.Inst.Index,
};
- var match_stack: std.ArrayListUnmanaged(MatchedZirDecl) = .{};
+ var match_stack: ArrayListUnmanaged(MatchedZirDecl) = .{};
defer match_stack.deinit(gpa);
// Main struct inst is always the same
@@ -5488,12 +5488,12 @@ pub fn abortAnonDecl(mod: *Module, decl_index: Decl.Index) void {
/// 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_index: Decl.Index) void {
- const kv = mod.export_owners.fetchSwapRemove(decl_index) orelse return;
+ var export_owners = (mod.export_owners.fetchSwapRemove(decl_index) orelse return).value;
- for (kv.value) |exp| {
+ for (export_owners.items) |exp| {
if (mod.decl_exports.getPtr(exp.exported_decl)) |value_ptr| {
// Remove exports with owner_decl matching the regenerating decl.
- const list = value_ptr.*;
+ const list = value_ptr.items;
var i: usize = 0;
var new_len = list.len;
while (i < new_len) {
@@ -5504,7 +5504,7 @@ fn deleteDeclExports(mod: *Module, decl_index: Decl.Index) void {
i += 1;
}
}
- value_ptr.* = mod.gpa.shrink(list, new_len);
+ value_ptr.shrinkAndFree(mod.gpa, new_len);
if (new_len == 0) {
assert(mod.decl_exports.swapRemove(exp.exported_decl));
}
@@ -5527,7 +5527,7 @@ fn deleteDeclExports(mod: *Module, decl_index: Decl.Index) void {
mod.gpa.free(exp.options.name);
mod.gpa.destroy(exp);
}
- mod.gpa.free(kv.value);
+ export_owners.deinit(mod.gpa);
}
pub fn analyzeFnBody(mod: *Module, func: *Fn, arena: Allocator) SemaError!Air {
@@ -5745,8 +5745,8 @@ pub fn analyzeFnBody(mod: *Module, func: *Fn, arena: Allocator) SemaError!Air {
return Air{
.instructions = sema.air_instructions.toOwnedSlice(),
- .extra = sema.air_extra.toOwnedSlice(gpa),
- .values = sema.air_values.toOwnedSlice(gpa),
+ .extra = try sema.air_extra.toOwnedSlice(gpa),
+ .values = try sema.air_values.toOwnedSlice(gpa),
};
}
@@ -6415,7 +6415,7 @@ pub fn processExports(mod: *Module) !void {
var it = mod.decl_exports.iterator();
while (it.next()) |entry| {
const exported_decl = entry.key_ptr.*;
- const exports = entry.value_ptr.*;
+ const exports = entry.value_ptr.items;
for (exports) |new_export| {
const gop = try symbol_exports.getOrPut(gpa, new_export.options.name);
if (gop.found_existing) {
@@ -6695,3 +6695,11 @@ pub fn addGlobalAssembly(mod: *Module, decl_index: Decl.Index, source: []const u
pub fn wantDllExports(mod: Module) bool {
return mod.comp.bin_file.options.dll_export_fns and mod.getTarget().os.tag == .windows;
}
+
+pub fn getDeclExports(mod: Module, decl_index: Decl.Index) []const *Export {
+ if (mod.decl_exports.get(decl_index)) |l| {
+ return l.items;
+ } else {
+ return &[0]*Export{};
+ }
+}