P1: implement real Zig FFI matching the Idris2 ABI#42
Merged
Conversation
The Zig FFI at src/interface/ffi/src/main.zig did not compile: the engine
handle was declared as `pub const Handle = opaque { ... }` with struct
fields, which Zig rejects ("opaque types cannot have fields"). The file was
effectively an unrendered/half-rendered template that never built and so
could not honour the ABI contract.
This rewrites main.zig as a self-contained, compiling reference FFI driven
by the Idris2 ABI (the source of truth):
- Exports exactly the 20 `C:atsiser_*` symbols declared in
src/interface/abi/Atsiser/ABI/Foreign.idr, with C-compatible signatures
matching the Idris types (Bits64 handle <-> opaque `?*Engine`; Idris
`String` arg <-> `?[*:0]const u8`; returns-a-string Bits64 <->
`?[*:0]const u8`; result-returning Bits32 <-> `Result` enum as c_int;
counts <-> c_uint). All `callconv(.C)`.
- `Result = enum(c_int)` whose values exactly match
Atsiser.ABI.Types.resultToInt: Ok=0, Error=1, InvalidParam=2,
OutOfMemory=3, NullPointer=4, OwnershipViolation=5, BoundsViolation=6.
- A real in-memory engine (allocation sites, ownership edges, buffer/proof
counts) using std.heap.c_allocator, with proper deinit and C-string
ownership via dup / std.mem.span / allocPrintZ / atsiser_free_string.
libclang parsing and patsopt (ATS2 -> C) bridging remain stubs, but
signatures, symbols, and result codes match the ABI.
- Two `test` blocks: the main analysis pipeline (parse -> analyse ->
ownership graph -> proofs -> report) and null-handle / invalid-param
rejection.
Verification:
- `zig test src/main.zig -lc` -> all tests pass, zero errors/warnings.
- `idris2 --build atsiser-abi.ipkg` -> exits 0 clean.
- Every `C:<name>` in Foreign.idr has a matching `export fn <name>`; Result
values match resultToInt.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019xMKB3T4Vo5FYC7Czx3JSH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
The Zig FFI at
src/interface/ffi/src/main.zigdid not compile. The analysis-engine handle was declared as:Zig rejects this with
error: opaque types cannot have fields. The file was effectively a half-/unrendered template that never built, so it could not honour the ABI contract defined by the Idris2 ABI (the source of truth).The fix
main.zigis rewritten as a self-contained, compiling reference FFI driven bysrc/interface/abi/Atsiser/ABI/Foreign.idrandTypes.idr:C:atsiser_*symbols declared inForeign.idr, verbatim (atsiser_init,atsiser_free,atsiser_parse_header,atsiser_analyse_allocations,atsiser_build_ownership_graph,atsiser_detect_buffers,atsiser_generate_viewtypes,atsiser_generate_proofs,atsiser_generate_bounds_proofs,atsiser_compile_ats2,atsiser_verify_linkage,atsiser_allocation_count,atsiser_ownership_edge_count,atsiser_proof_count,atsiser_analysis_report,atsiser_free_string,atsiser_last_error,atsiser_version,atsiser_build_info,atsiser_is_initialized). Allcallconv(.C).?*Engine; IdrisStringarg ↔?[*:0]const u8; returns-a-string Bits64 ↔?[*:0]const u8; result-returning Bits32 ↔Resultenum asc_int; counts ↔c_uint.Result = enum(c_int)whose values exactly matchAtsiser.ABI.Types.resultToInt— Ok=0, Error=1 (err), InvalidParam=2, OutOfMemory=3, NullPointer=4, OwnershipViolation=5, BoundsViolation=6 (7 codes).Enginetracking allocation sites, ownership edges, and buffer/proof counts viastd.heap.c_allocator, with properdeinitand C-string ownership (dup/std.mem.span/allocPrintZ/atsiser_free_string). libclang parsing andpatsopt(ATS2 → C) bridging remain stubs, but signatures, symbols, and result codes match the ABI.testblocks — the main analysis pipeline (parse → analyse → ownership graph → proofs → JSON report) and null-handle / invalid-param rejection.Only
src/interface/ffi/(the Zig) was touched. The Idris ABI, Cargo, and other files are unchanged.Verification
cd src/interface/ffi && zig test src/main.zig -lc→ all tests pass, zero errors/warnings:cd src/interface/abi && idris2 --build atsiser-abi.ipkg→ exits 0 clean (builds Types, Layout, Proofs, Foreign), build dir removed afterward.C:<name>inForeign.idrhas a matchingexport fn <name>inmain.zig(20/20, perfect diff match).resultToIntexactly (7/7).🤖 Generated with Claude Code
https://claude.ai/code/session_019xMKB3T4Vo5FYC7Czx3JSH
Generated by Claude Code