Architecture

Architecture overview

z3_mojo uses the same release-pinned generator structure as cvc5_mojo. Hand-written edits go into bindgen/ (not the generated .mojo files) so fixes survive future Z3 upgrades.

Four layers

Layer Files Role
Pythonic API src/z3_mojo/pythonic.mojo, src/z3_mojo/theories.mojo, src/z3_mojo/callbacks.mojo Pythonic free functions, theory builders, operators, and callback helpers re-exported from z3_mojo.
Safe Handle & Context API src/z3_mojo/api.mojo, src/z3_mojo/__init__.mojo, src/z3_mojo/enums.mojo Typed handle structs with RAII cleanup, 769 safe C-API bindings, safe callback traits (UserPropagator, ClauseHandler, ModelHandler), apn_mojo number conversions, and generated enums.
Low-level C API src/z3_mojo/c.mojo, src/z3_mojo/_c/ffi.mojo, src/z3_mojo/_c/raw.mojo Public 1-to-1 C API (z3_mojo.c) over all 817 Z3_* functions; loads libz3 once per process, resolves every C function into a cached _DLCallable, converts C bool via UInt8, and raises Mojo errors from Z3_get_error_code.
Native Z3 libz3.so / libz3.dylib The official MIT-licensed Z3 shared library pinned by SHA-256 in z3-version.toml.

How generation works

  1. bindgen/fetch.py downloads the pinned Z3 binaries for linux-64, linux-aarch64, and osx-arm64, plus the matching source archive, and verifies every SHA-256 hash into third_party/.
  2. bindgen/c_headers.py parses all nine headers included by z3.h, extracts Z3's def_API/extra_API parameter direction and array-length annotations, and checks every parsed prototype against z3.h by compiling a generated C file with cc -fsyntax-only -Werror.
  3. bindgen/c_library.py inspects libz3 across all three platforms with llvm-nm, llvm-readelf, and llvm-objdump (so Linux can inspect macOS Mach-O binaries without running them) and checks that every declared symbol is exported everywhere.
  4. bindgen/enums.py compiles and runs a small C program against z3.h that prints every enumerator's exact value, then writes src/z3_mojo/enums.mojo.
  5. bindgen/snapshot.py writes api/z3-api.json and enforces the completeness gate (pixi run check-api): every handle needs an ownership rule, every C function must be accounted for (auto, manual, lifecycle, or listed in bindgen/overrides.toml), and stale overrides fail the build.
  6. bindgen/gen_raw.py checks scalar, bool, enum, and pointer sizes and signedness with _Static_assert, then writes src/z3_mojo/_c/raw.mojo.
  7. bindgen/gen_api.py and bindgen/gen_docs.py generate src/z3_mojo/api.mojo, src/z3_mojo/__init__.mojo, and the reference pages under docs/content/reference/.

Direct access to the low-level C API

All 817 C functions, 29 raw handle structs, 10 enums, and FFI helpers (including the 8 raw-only functions such as Z3_mk_context_rc, Z3_inc_ref/Z3_dec_ref, Z3_set_proof_check_callback, and raw Z3_fixedpoint_* callbacks) are exposed publicly via from z3_mojo.c import c_api, .... See the Low-level C API guide and Low-Level C API Reference.

z3_mojo v5.3.3.0 for Mojo 1.2 and Z3 5.3.3 · Apache License 2.0 with LLVM Exceptions · Source on GitHub