zig/src-self-hosted/Package.zig
Andrew Kelley a32d3a85d2 rework self-hosted compiler for incremental builds
* introduce std.ArrayListUnmanaged for when you have the allocator
   stored elsewhere
 * move std.heap.ArenaAllocator implementation to its own file. extract
   the main state into std.heap.ArenaAllocator.State, which can be
   stored as an alternative to storing the entire ArenaAllocator, saving
   24 bytes per ArenaAllocator on 64 bit targets.
 * std.LinkedList.Node pointer field now defaults to being null
   initialized.
 * Rework self-hosted compiler Package API
 * Delete almost all the bitrotted self-hosted compiler code. The only bit
   rotted code left is in main.zig and compilation.zig
 * Add call instruction to ZIR
 * self-hosted compiler ir API and link API are reworked to support
   a long-running compiler that incrementally updates declarations
 * Introduce the concept of scopes to ZIR semantic analysis
 * ZIR text format supports referencing named decls that are declared
   later in the file
 * Figure out how memory management works for the long-running compiler
   and incremental compilation. The main roots are top level
   declarations. There is a table of decls. The key is a cryptographic
   hash of the fully qualified decl name. Each decl has an arena
   allocator where all of the memory related to that decl is stored.
   Each code block has its own arena allocator for the lifetime of
   the block. Values that want to survive when going out of scope in
   a block must get copied into the outer block. Finally, values must
   get copied into the Decl arena to be long-lived.
 * Delete the unused MemoryCell struct. Instead, comptime pointers are
   based on references to Decl structs.
 * Figure out how caching works. Each Decl will store a set of other
   Decls which must be recompiled when it changes.

This branch is still work-in-progress; this commit breaks the build.
2020-05-10 02:05:54 -04:00

53 lines
1.5 KiB
Zig

pub const Table = std.StringHashMap(*Package);
root_src_dir: std.fs.Dir,
/// Relative to `root_src_dir`.
root_src_path: []const u8,
table: Table,
/// No references to `root_src_dir` and `root_src_path` are kept.
pub fn create(
allocator: *mem.Allocator,
base_dir: std.fs.Dir,
/// Relative to `base_dir`.
root_src_dir: []const u8,
/// Relative to `root_src_dir`.
root_src_path: []const u8,
) !*Package {
const ptr = try allocator.create(Package);
errdefer allocator.destroy(ptr);
const root_src_path_dupe = try mem.dupe(allocator, u8, root_src_path);
errdefer allocator.free(root_src_path_dupe);
ptr.* = .{
.root_src_dir = try base_dir.openDir(root_src_dir, .{}),
.root_src_path = root_src_path_dupe,
.table = Table.init(allocator),
};
return ptr;
}
pub fn destroy(self: *Package) void {
const allocator = self.table.allocator;
self.root_src_dir.close();
allocator.free(self.root_src_path);
{
var it = self.table.iterator();
while (it.next()) |kv| {
allocator.free(kv.key);
}
}
self.table.deinit();
allocator.destroy(self);
}
pub fn add(self: *Package, name: []const u8, package: *Package) !void {
const name_dupe = try mem.dupe(self.table.allocator, u8, name);
errdefer self.table.allocator.deinit(name_dupe);
const entry = try self.table.put(name_dupe, package);
assert(entry == null);
}
const std = @import("std");
const mem = std.mem;
const assert = std.debug.assert;