import c
This commit is contained in:
@@ -2,6 +2,7 @@ const std = @import("std");
|
||||
const ast = @import("ast.zig");
|
||||
const parser = @import("parser.zig");
|
||||
const errors = @import("errors.zig");
|
||||
const c_import = @import("c_import.zig");
|
||||
const Node = ast.Node;
|
||||
|
||||
pub fn dirName(path: []const u8) []const u8 {
|
||||
@@ -87,6 +88,50 @@ pub fn resolveImports(
|
||||
var decl_list = std.ArrayList(*Node).empty;
|
||||
|
||||
for (root.data.root.decls) |decl| {
|
||||
if (decl.data == .c_import_decl) {
|
||||
const ci = decl.data.c_import_decl;
|
||||
|
||||
// Parse headers to get synthetic function declarations
|
||||
const result = c_import.processCImport(
|
||||
allocator,
|
||||
ci.includes,
|
||||
ci.defines,
|
||||
ci.flags,
|
||||
) catch |err| {
|
||||
if (diagnostics) |diags| {
|
||||
diags.addFmt(.err, decl.span, "#import c failed: {}", .{err});
|
||||
}
|
||||
return error.ImportError;
|
||||
};
|
||||
|
||||
if (ci.name) |ns_name| {
|
||||
// Namespaced: wrap fn_decls + c_import_decl in a namespace
|
||||
var ns_decls = std.ArrayList(*Node).empty;
|
||||
for (result.fn_decls) |fd| {
|
||||
try ns_decls.append(allocator, fd);
|
||||
}
|
||||
// Keep c_import_decl inside namespace so codegen can find sources
|
||||
try ns_decls.append(allocator, decl);
|
||||
|
||||
const ns_node = try allocator.create(Node);
|
||||
ns_node.* = .{
|
||||
.span = decl.span,
|
||||
.data = .{ .namespace_decl = .{
|
||||
.name = ns_name,
|
||||
.decls = try ns_decls.toOwnedSlice(allocator),
|
||||
} },
|
||||
};
|
||||
try mod.scope.put(ns_name, {});
|
||||
try decl_list.append(allocator, ns_node);
|
||||
} else {
|
||||
// Flat: add fn_decls directly + keep c_import_decl
|
||||
for (result.fn_decls) |fd| {
|
||||
_ = try mod.addDecl(allocator, &decl_list, fd);
|
||||
}
|
||||
_ = try mod.addDecl(allocator, &decl_list, decl);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (decl.data != .import_decl) {
|
||||
_ = try mod.addDecl(allocator, &decl_list, decl);
|
||||
continue;
|
||||
|
||||
Reference in New Issue
Block a user