This commit is contained in:
agra
2026-02-22 17:24:04 +02:00
parent 775dcb44cc
commit d3e574eae5
38 changed files with 16135 additions and 33 deletions

View File

@@ -4,6 +4,7 @@ const sx = struct {
pub const parser = @import("../parser.zig");
pub const sema = @import("../sema.zig");
pub const imports = @import("../imports.zig");
pub const c_import = @import("../c_import.zig");
};
pub const Import = struct {
@@ -28,6 +29,10 @@ pub const Document = struct {
last_good_sema: ?sx.sema.SemaResult = null,
/// Import declarations parsed from this file.
imports: []const Import,
/// Last successful imports (preserved across parse failures for completions).
last_good_imports: []const Import = &.{},
/// Source locations for C import functions (name → file:line for go-to-definition).
c_source_locations: std.StringHashMap(sx.c_import.CSourceLocation),
/// True while this document is being analyzed (circular import guard).
is_analyzing: bool = false,
@@ -110,6 +115,7 @@ pub const DocumentStore = struct {
.root = null,
.sema = null,
.imports = &.{},
.c_source_locations = std.StringHashMap(sx.c_import.CSourceLocation).init(self.allocator),
};
try self.by_path.put(path_owned, doc);
return doc;
@@ -126,6 +132,43 @@ pub const DocumentStore = struct {
var p = sx.parser.Parser.init(self.allocator, doc.source);
doc.root = p.parse() catch return;
}
// Expand root with synthetic fn_decls from #import c { ... } declarations.
// This makes C functions visible to sema, completions, and hover.
doc.c_source_locations = std.StringHashMap(sx.c_import.CSourceLocation).init(self.allocator);
if (doc.root) |parsed_root| {
if (parsed_root.data == .root) {
var expanded = std.ArrayList(*sx.ast.Node).empty;
for (parsed_root.data.root.decls) |decl| {
if (decl.data == .c_import_decl) {
const ci = decl.data.c_import_decl;
if (sx.c_import.processCImport(
self.allocator,
ci.includes,
ci.defines,
ci.flags,
)) |result| {
for (result.fn_decls, result.locations) |fd, loc| {
try expanded.append(self.allocator, fd);
if (fd.data == .fn_decl) {
try doc.c_source_locations.put(fd.data.fn_decl.name, loc);
}
}
} else |_| {}
}
try expanded.append(self.allocator, decl);
}
if (expanded.items.len != parsed_root.data.root.decls.len) {
const new_root = try self.allocator.create(sx.ast.Node);
new_root.* = .{
.span = parsed_root.span,
.data = .{ .root = .{ .decls = try expanded.toOwnedSlice(self.allocator) } },
};
doc.root = new_root;
}
}
}
const root = doc.root orelse return;
// Extract imports from AST
@@ -146,6 +189,7 @@ pub const DocumentStore = struct {
}
}
doc.imports = try import_list.toOwnedSlice(self.allocator);
doc.last_good_imports = doc.imports;
// Recursively analyze imported documents and pre-register their symbols
var analyzer = sx.sema.Analyzer.init(self.allocator);