feat(imports): buildImportFacts raw-fact store (ModuleRawDeclIndex + NamespaceEdges) [stdlib A]

Phase A of the unified resolver (R5 locked design). Additive infrastructure
with NO behavior change — builds the import-side raw-fact store; nothing
consumes it yet.

- imports.zig: add RawDeclRef / RawAuthor / ModuleRawDeclIndex / ModuleDecls /
  NamespaceTarget / NamespaceEdges, plus buildImportFacts (mirrors
  buildModuleFns) producing a scalar per-module name→RawDeclRef index + the
  namespace edges. Callable without IR lowering (LSP reuses it later).
- ast.zig: NamespaceDecl gains target_module_path, captured at resolution time
  (the resolved_path otherwise lost on the node) so the namespace edge records
  the alias target.
- imports.zig: same-module duplicate top-level name is now DIAGNOSED
  ("duplicate top-level declaration 'X'") where addOwnDecl would silently drop
  the second author — replaces the discarded `_ =` at the three call sites.
- program_index.zig: borrowed views module_decls / namespace_edges (like
  module_fns); deinit does not free them.
- core.zig: build the facts alongside buildModuleFns and point the borrowed
  views at them.
- imports.test.zig: index unit tests (flat / directory / namespaced file /
  namespaced directory / C-import namespace / same-name fn / same-name struct /
  value-vs-type same spelling / raw const_decl) + the duplicate-name diagnostic
  regression (fails pre-fix, passes after).

Gate (worktree): zig build, zig build test (incl. LSP corpus sweep), and
run_examples (471, byte-identical) all green; m3te ios-sim build exits 0.
This commit is contained in:
agra
2026-06-06 23:34:32 +03:00
parent db7af02950
commit b5ec121645
5 changed files with 443 additions and 4 deletions

View File

@@ -32,6 +32,12 @@ pub const Compilation = struct {
/// Per-module authored-function index (`path → name → *const FnDecl`).
/// Borrowed by `ProgramIndex.module_fns`.
module_fns: imports.ModuleFns,
/// Per-module scalar raw-decl index (`path → name → RawDeclRef`), built by
/// `imports.buildImportFacts`. Borrowed by `ProgramIndex.module_decls`.
module_decls: imports.ModuleDecls,
/// Namespace import edges (`importer → alias → NamespaceTarget`), built by
/// `imports.buildImportFacts`. Borrowed by `ProgramIndex.namespace_edges`.
namespace_edges: imports.NamespaceEdges,
ir_emitter: ?ir.LLVMEmitter = null,
/// Lowered IR module, kept alive past `generateCode` so post-link
/// callbacks can re-enter the interpreter to invoke sx functions
@@ -60,6 +66,8 @@ pub const Compilation = struct {
.import_graph = std.StringHashMap(std.StringHashMap(void)).init(allocator),
.flat_import_graph = std.StringHashMap(std.StringHashMap(void)).init(allocator),
.module_fns = imports.ModuleFns.init(allocator),
.module_decls = imports.ModuleDecls.init(allocator),
.namespace_edges = imports.NamespaceEdges.init(allocator),
.target_config = target_config,
.stdlib_paths = stdlib_paths,
};
@@ -125,6 +133,15 @@ pub const Compilation = struct {
// cross-module authors stay distinct under their own paths.
imports.buildModuleFns(self.allocator, self.file_path, mod, &cache, &self.module_fns) catch {};
// Raw import facts (the unified-resolver store): scalar per-module
// raw-decl index + namespace edges, built from the SAME modules. Nothing
// consumes these yet — they are borrowed by `ProgramIndex` for later
// phases (and the LSP). Built without IR lowering.
if (imports.buildImportFacts(self.allocator, self.file_path, mod, &cache)) |facts| {
self.module_decls = facts.decls;
self.namespace_edges = facts.ns_edges;
} else |_| {}
// Store main file source in import_sources so error reporting can find it
self.import_sources.put(self.file_path, self.source) catch {};
@@ -292,6 +309,8 @@ pub const Compilation = struct {
lowering.program_index.import_graph = &self.import_graph;
lowering.program_index.flat_import_graph = &self.flat_import_graph;
lowering.program_index.module_fns = &self.module_fns;
lowering.program_index.module_decls = &self.module_decls;
lowering.program_index.namespace_edges = &self.namespace_edges;
lowering.lowerRoot(root);
if (self.diagnostics.hasErrors()) return error.CompileError;