refactor(ir): extract ProgramIndex, move low-fanout decl facts (A1.1a)

Architecture phase A1.1a. Introduce src/ir/program_index.zig as the single
storage owner for declaration-name / import / visibility facts, and move the
three low-fanout maps out of the Lowering state bag:

- import_flags     (owned by ProgramIndex)
- module_scopes    (borrowed pointer into a core.zig-owned map)
- import_graph     (borrowed pointer into a core.zig-owned map)

Lowering embeds one ProgramIndex by value and reaches every moved fact through
self.program_index.<field>; later phases hand collaborator modules a
*ProgramIndex instead of *Lowering. 8 call sites in lower.zig + 2 setters in
core.zig repointed. No duplicate storage, no fallback path; zig build enforces
no missed reference.

Mutation-heavy registration (registerStructDecl etc.) stays in Lowering and
now writes import_flags through the index. High-fanout maps are deferred to
A1.1b.

Adds src/ir/program_index.test.zig (init-empty, import_flags round-trip,
borrowed-view ownership) wired into the ir.zig barrel.

Behavior-preserving: zig build, zig build test, and bash tests/run_examples.sh
(350 passed, 0 failed) all green.
This commit is contained in:
agra
2026-06-02 12:04:31 +03:00
parent 795ce3dc7d
commit 90520eefeb
5 changed files with 96 additions and 14 deletions

36
src/ir/program_index.zig Normal file
View File

@@ -0,0 +1,36 @@
const std = @import("std");
/// Single storage owner for declaration-name facts: declarations, imports,
/// visibility, names, aliases, templates, protocols, foreign classes, and
/// module constants. The architecture stream (`current/PLAN-ARCH.md`, phase
/// A1) extracts these out of the `Lowering` state bag incrementally; this is
/// the A1.1a slice — the low-fanout import/visibility facts. `Lowering` embeds
/// one `ProgramIndex` by value and reaches every moved fact through it; later
/// phases hand collaborator modules a `*ProgramIndex` instead of `*Lowering`.
///
/// `import_flags` is owned here. `module_scopes` / `import_graph` are borrowed
/// views into maps owned by the compilation driver (`core.zig`); this index
/// only reads through them.
pub const ProgramIndex = struct {
/// Declaration name → is the function imported (declared `extern`)?
/// Populated by the declaration scan / registration code in `Lowering`.
import_flags: std.StringHashMap(bool),
/// Per-module visible names, keyed by source file (from import
/// resolution). Borrowed — the backing map lives in the compilation
/// driver, so this index does not free it.
module_scopes: ?*std.StringHashMap(std.StringHashMap(void)) = null,
/// Module path → set of directly imported paths; drives the
/// `param_impl_map` cross-module visibility filter. Borrowed — owned by
/// the compilation driver.
import_graph: ?*std.StringHashMap(std.StringHashMap(void)) = null,
pub fn init(alloc: std.mem.Allocator) ProgramIndex {
return .{ .import_flags = std.StringHashMap(bool).init(alloc) };
}
pub fn deinit(self: *ProgramIndex) void {
self.import_flags.deinit();
}
};