comptime compiler-API: Phase 1 foundation + Phase 2.1 weld plan
Introduce the welded comptime `compiler` library (`#library "compiler"` +
`abi(.zig) extern compiler`), per design/comptime-compiler-api.md, and unify
`callconv(...)` into the new `abi(...)` annotation.
abi(...) replaces callconv(...):
- New ABI enum { default, c, zig, pure }; `abi(.c|.zig|.pure)` parses in the
postfix slot before extern/export (and standalone). `kw_callconv` -> `kw_abi`.
- Migrated 52 sx files, the call-convention-mismatch diagnostic, and docs
(readme/specs) from `callconv(.c)` to `abi(.c)`.
Phase 1 — welded compiler library (parse -> registry -> validation -> bridge):
- `abi(.zig) extern compiler` parses on fn decls (carries abi/extern_lib) and
struct decls (StructDecl.abi/extern_lib).
- `#library "compiler"` is the comptime-only internal surface — never dlopen'd.
- src/ir/compiler_lib.zig: the binding registry (the safety boundary). `Field`
welded to StructInfo.Field with layout baked from the real Zig type
(@offsetOf/@sizeOf); `findType`/`findFn`. Welded structs are layout-validated
at registration (field set + total size) as a header checked against the impl.
- Host-call bridge: a `fn abi(.zig) extern compiler` dispatches under the
comptime interp to its registered Zig handler (intern/text_of round-trip),
never dlsym. IR Function.compiler_welded; validated in declareFunction.
- Comptime-only enforcement: a runtime call to a welded fn is a clean
build-gating error (emitCall), not an undefined-symbol link failure.
Phase 2.1 — byte-layout weld foundation:
- Decision: full byte-layout weld (sx struct laid out byte-identically to the
bound Zig type). Registered StructInfo (first non-natural / Zig-reordered
layout). `computeWeldPlan` — pure offset-ordered element plan + padding +
sx-field->LLVM-element remap; unit-tested. Emit/interp wiring is the next
sub-step (2.2+, see current/CHECKPOINT-COMPILER-API.md).
Examples: 0625/0626 (welded struct + fn round-trip), 1183/1184/1185
(layout-mismatch, unexported-fn, runtime-call diagnostics).
This commit is contained in:
@@ -78,3 +78,148 @@ test "parser: comptime type-metaprogramming surface parses" {
|
||||
try std.testing.expect(d.data.fn_decl.return_type != null);
|
||||
}
|
||||
}
|
||||
|
||||
// Lock: the `compiler`-library binding surface PARSES — `name :: #library "x";`
|
||||
// (already supported) plus the new postfix `abi(.zig)` annotation (in the slot
|
||||
// before `extern`) followed by the library handle, on a function declaration. The
|
||||
// AST must carry the binding: `abi == .zig`, `extern_export == .extern_`, and the
|
||||
// library handle in `extern_lib`. No semantics yet — this is the first testable
|
||||
// sub-step of Phase 1 (parse only).
|
||||
test "parser: abi(.zig) extern <lib> binding parses on a fn decl" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
|
||||
const src =
|
||||
\\compiler :: #library "compiler";
|
||||
\\text_of :: (id: StringId) -> string abi(.zig) extern compiler;
|
||||
\\intern :: (s: string) -> StringId abi(.zig) extern compiler;
|
||||
\\
|
||||
;
|
||||
var parser = Parser.init(alloc, src);
|
||||
const root = try parser.parse();
|
||||
|
||||
try std.testing.expect(root.data == .root);
|
||||
const decls = root.data.root.decls;
|
||||
try std.testing.expectEqual(@as(usize, 3), decls.len);
|
||||
|
||||
// The `#library` decl still parses to a `library_decl` node carrying the name.
|
||||
try std.testing.expect(decls[0].data == .library_decl);
|
||||
try std.testing.expectEqualStrings("compiler", decls[0].data.library_decl.name);
|
||||
try std.testing.expectEqualStrings("compiler", decls[0].data.library_decl.lib_name);
|
||||
|
||||
// The two `abi(.zig) extern compiler` fns: `.fn_decl` with the binding fields set.
|
||||
for ([_][]const u8{ "text_of", "intern" }) |bn| {
|
||||
var found: ?*const Node = null;
|
||||
for (decls) |d| {
|
||||
if (d.data.declName()) |n| {
|
||||
if (std.mem.eql(u8, n, bn)) found = d;
|
||||
}
|
||||
}
|
||||
const d = found orelse return error.MissingDecl;
|
||||
try std.testing.expect(d.data == .fn_decl);
|
||||
const fd = d.data.fn_decl;
|
||||
try std.testing.expectEqual(ast.ABI.zig, fd.abi);
|
||||
try std.testing.expectEqual(ast.ExternExportModifier.extern_, fd.extern_export);
|
||||
try std.testing.expect(fd.extern_lib != null);
|
||||
try std.testing.expectEqualStrings("compiler", fd.extern_lib.?);
|
||||
// Bodyless extern import: synthesized empty block, no `#builtin`/`#compiler`.
|
||||
try std.testing.expect(fd.body.data == .block);
|
||||
}
|
||||
}
|
||||
|
||||
// Lock: a bare `extern` (no abi annotation) leaves `abi == .default` — the
|
||||
// unannotated case is unchanged by the new `abi(...)` slot.
|
||||
test "parser: bare extern leaves abi == .default" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
|
||||
const src =
|
||||
\\puts :: (s: *u8) -> i32 extern;
|
||||
\\
|
||||
;
|
||||
var parser = Parser.init(alloc, src);
|
||||
const root = try parser.parse();
|
||||
const decls = root.data.root.decls;
|
||||
try std.testing.expectEqual(@as(usize, 1), decls.len);
|
||||
try std.testing.expect(decls[0].data == .fn_decl);
|
||||
const fd = decls[0].data.fn_decl;
|
||||
try std.testing.expectEqual(ast.ExternExportModifier.extern_, fd.extern_export);
|
||||
try std.testing.expectEqual(ast.ABI.default, fd.abi);
|
||||
}
|
||||
|
||||
// Lock: `abi(.c)` parses standalone (no extern/export) in the postfix slot — the
|
||||
// migrated spelling of the old `callconv(.c)` on an ordinary function pointer /
|
||||
// fn decl. And `abi(.pure)` parses (naked-asm ABI).
|
||||
test "parser: abi(.c) and abi(.pure) parse standalone" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
|
||||
const src =
|
||||
\\cb :: () -> i64 abi(.c) { 0; }
|
||||
\\nk :: () -> i64 abi(.pure) { 0; }
|
||||
\\
|
||||
;
|
||||
var parser = Parser.init(alloc, src);
|
||||
const root = try parser.parse();
|
||||
const decls = root.data.root.decls;
|
||||
try std.testing.expectEqual(@as(usize, 2), decls.len);
|
||||
try std.testing.expect(decls[0].data == .fn_decl);
|
||||
try std.testing.expectEqual(ast.ABI.c, decls[0].data.fn_decl.abi);
|
||||
try std.testing.expectEqual(ast.ExternExportModifier.none, decls[0].data.fn_decl.extern_export);
|
||||
try std.testing.expect(decls[1].data == .fn_decl);
|
||||
try std.testing.expectEqual(ast.ABI.pure, decls[1].data.fn_decl.abi);
|
||||
}
|
||||
|
||||
// Lock: the `compiler`-library binding PARSES on a STRUCT decl — `Name :: struct
|
||||
// abi(.zig) extern <lib> { … }`. The AST struct_decl must carry `abi == .zig` and
|
||||
// the library handle in `extern_lib`, with the field list intact. No semantics
|
||||
// yet (parse-only) — this is the second testable sub-step of Phase 1.
|
||||
test "parser: abi(.zig) extern <lib> binding parses on a struct decl" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
|
||||
const src =
|
||||
\\compiler :: #library "compiler";
|
||||
\\Field :: struct abi(.zig) extern compiler { name: StringId; ty: Type; }
|
||||
\\
|
||||
;
|
||||
var parser = Parser.init(alloc, src);
|
||||
const root = try parser.parse();
|
||||
const decls = root.data.root.decls;
|
||||
try std.testing.expectEqual(@as(usize, 2), decls.len);
|
||||
|
||||
try std.testing.expect(decls[1].data == .struct_decl);
|
||||
const sd = decls[1].data.struct_decl;
|
||||
try std.testing.expectEqual(ast.ABI.zig, sd.abi);
|
||||
try std.testing.expect(sd.extern_lib != null);
|
||||
try std.testing.expectEqualStrings("compiler", sd.extern_lib.?);
|
||||
// Field list survives the binding annotation.
|
||||
try std.testing.expectEqual(@as(usize, 2), sd.field_names.len);
|
||||
try std.testing.expectEqualStrings("name", sd.field_names[0]);
|
||||
try std.testing.expectEqualStrings("ty", sd.field_names[1]);
|
||||
}
|
||||
|
||||
// Lock: an ordinary struct (no binding) leaves `abi == .default` / `extern_lib ==
|
||||
// null` — the new annotation slot doesn't perturb the common case.
|
||||
test "parser: plain struct leaves abi == .default, extern_lib == null" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
|
||||
const src =
|
||||
\\Point :: struct { x: i64; y: i64; }
|
||||
\\
|
||||
;
|
||||
var parser = Parser.init(alloc, src);
|
||||
const root = try parser.parse();
|
||||
const decls = root.data.root.decls;
|
||||
try std.testing.expectEqual(@as(usize, 1), decls.len);
|
||||
try std.testing.expect(decls[0].data == .struct_decl);
|
||||
const sd = decls[0].data.struct_decl;
|
||||
try std.testing.expectEqual(ast.ABI.default, sd.abi);
|
||||
try std.testing.expect(sd.extern_lib == null);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user