Zane C Interface

This document specifies how a Zane package calls C: the C code a package holds and how the toolchain builds it, foreign verbs and the types that cross into C, foreign handles, and what foreign code is taken to read and write.

It states what a Zane program and a package’s C code can rely on: what is accepted, the behaviour of a call, and when a call is evaluated at compile time. How the compiler implements these rules, from compiling the C and generating the code around each foreign verb to renaming symbols, caching, and running C at compile time, is the compiler’s design (docs/design/ffi.md in the compiler repository), which this document does not specify.

See also: effects.md §5 and §6 for capability access and compile-time evaluation. memory.md §2.9 for passing modes. dependencies.md §3 and §6 for release archives and symbol rewriting. syntax.md §2.16 and §3.11 for the declaration forms.

Story: stories/ffi.md records the story behind this design.


1. Overview

A package may hold C code beside its Zane source. The toolchain compiles that C for the build’s target, links it into the package, and lets the package’s own declarations call it. A call into C is an ordinary call in Zane: it has a Zane signature, Zane passing modes, and the effects that signature states.

  • The toolchain builds the C. A package’s C sources are compiled by the toolchain’s own C compiler, for whatever target the build is for, with settings from a c.coda file in the package’s directory. No build script runs.
  • Foreign verbs. A function or method whose body is a C function, named by its C symbol. Its Zane signature is checked against the C declaration in the package’s interface headers.
  • C never writes Zane memory. Zane values cross into C as values or as read-only views for the duration of the call; the only Zane memory C writes is the bytes it allocates for a string it returns. State that C keeps lives in C memory, reached from Zane through a foreign handle, which releases it when it is destroyed and replaces it in place when it is overwritten.
  • Outside state is a capability. A package’s C code offers its reach outside the program as a C global, which the root package declares as a foreign variable and passes on. A foreign verb that is a method on such a capability reads or writes outside state; any other foreign verb is taken to read and write nothing outside the program, so its calls fold at compile time like any other computation. A call marked comptime may also read through a capability at compile time (effects.md §5.6).
  • A callback's state is the subject. A foreign method’s subject of a reference type that is not a handle reaches C as an opaque void * context that C hands back to the call’s function values (§3.8).
  • C may call back in through exports. A dynamic library’s host calls exported lambdas, and holds the objects they return as opaque pointers (packages.md §8).
  • The declaration is trusted. The compiler cannot see what C code touches. A foreign verb declared without a capability that in fact reads outside state is evaluated at compile time, and the value the build machine produced is built into the program (§5.5).

Story: stories/ffi.md — “A C interface that keeps compile-time evaluation”.


2. A Package’s C Code

2.1 C sources and interface headers

Every package may hold C code: a library, program, or test package, and a subpackage. Its C code is:

  • its C sources: the .c files directly in the package’s directory, together with any listed in its c.coda (§2.3);
  • its interface headers: the .h files directly in the package’s directory.

A foreign verb of the package names a C function that one of its interface headers declares (§3.7). Headers elsewhere, including those an interface header includes, are seen by the C compiler; of their declarations, a foreign declaration names only a struct, as a foreign handle’s C type (§4.1).

zlib/
  lib/
    zlib/
      zlib.zn          package zlib: the foreign verbs
      binding.h        interface header
      binding.c        C source
      c.coda           C build settings
      vendor/          the upstream library's own tree
        deflate.c
        zlib.h
        ...
  zane.coda

A directory inside a package that holds no .zn file is not a subpackage (packages.md §2.3), so a vendored tree such as vendor/ is part of the package that holds it.

A package’s C code is reached only through that package’s foreign verbs. Another package, in the same project or another, calls those verbs like any other.

2.2 The toolchain compiles a package’s C code

The toolchain compiles each C source of a package as C17, with the C compiler it carries, for the target the build is for. One machine therefore builds a package’s C for every target, as it builds its Zane.

Two settings are fixed for every C source, and no setting of a package’s c.coda changes them:

  • floating-point expressions are evaluated as written, each operation rounded on its own, with no contraction of a multiply and an add into one operation;
  • the C compiler makes no assumption that a floating-point value is finite or that its operations are associative.

So a C floating-point computation gives the same result each time it is evaluated, at compile time or at run time (§5.4).

The toolchain runs no program the package supplies to build its C: no build script, configure step, or other build system. What a package’s C build needs is stated as data in its c.coda (§2.3).

The C code of a package is compiled once per build, with the settings of that package alone, and a parameterized package’s C code once for the package, never per instance (packages.md §2.6). Two packages’ C code never share a compilation, so a definition in one package’s settings has no effect on another’s.

A warning from the C compiler fails no build. The toolchain shows the warnings of the C sources directly in the directory of a package of the project being built, each once per command. A source in a subdirectory, such as a vendored library’s, and a dependency’s C are shown only when they fail to compile.

Story: stories/ffi.md — “The toolchain builds the C, and CMake does not”.

Story: stories/ffi.md — “C strings, allocated results and callback arguments cross without shims”.

2.3 A package’s c.coda

A package whose C build needs settings holds a file named c.coda directly in its directory. It is written by hand, as the package’s source is, and no command records anything in it. Its one table, c, gives one setting per row:

c [
    setting  value            target
    source   vendor/*.c       *
    include  vendor           *
    define   HAVE_UNISTD_H=1  *-linux-*
    define   HAVE_UNISTD_H=1  *-apple-*
]

Each row records:

  • setting: one of the settings below.
  • value: the setting’s value.
  • target: a pattern over the target triple, in which * matches any run of characters. The row applies to a build whose target the pattern matches; * alone applies to every build.

The settings belong to the package whose directory holds the file, so they move with it: a package moved to another directory or another project keeps its C build, and the project’s manifest and lock file (dependencies.md §2) record nothing about C. A package without a c.coda compiles the C sources in its directory with no settings of its own.

The settings are:

Setting Value Effect
source a path or a pattern, relative to the package’s directory the matching .c files are C sources of the package
include a directory, relative to the package’s directory the C compiler searches it for included headers
define NAME or NAME=VALUE the C compiler defines the macro for every C source of the package
archive a path, relative to the package’s directory a prebuilt static library linked with the package (§2.4)
system a library name a library of the target system linked with the program (§2.4)

A path MUST stay inside the package’s directory, so that everything a package’s C build reads moves with the package; one that is absolute or leaves the package’s directory through .. is an error. C that two packages share is vendored into each, or kept in one package whose foreign verbs the other calls. A source pattern that matches no file in a build it applies to is an error. The rows apply in the order they are written.

2.4 Prebuilt archives and system libraries

C code reaches a build in one of three forms:

  • from source: the package’s C sources, compiled by the toolchain (§2.2);
  • as a prebuilt archive: an archive row names a static library the project holds, built for the targets its target pattern matches;
  • from the system: a system row names a library of the target system, such as m or user32, which the program links against.

Building for a target needs what each applying row names for that target. The toolchain carries, for every target it builds for, the system libraries a Zane program links already. Any other system library, and the headers that declare it, is supplied to the build for its target. A build that lacks one fails with an error naming the package and the library.

The three forms differ in what the compiler can evaluate ahead of time. C code compiled from source is evaluated at compile time where a call to it folds (§5.4). C code from an archive or from the system is run only by the running program, so a call marked comptime that reaches it is a compile-time error (effects.md §5.6).

2.5 A package’s C symbols belong to the package

Every C function and variable that a package’s C sources or archives define with external linkage belongs to that package and version, as the package’s Zane symbols do (dependencies.md §6.1). The package’s own C code and Zane code reach it by its C name, and no other package’s C code does.

So two versions of one package, or two packages that hold the same C library, coexist in one program as their Zane code does (dependencies.md §11). A symbol the package’s C code references and does not define, such as a function of the C library or of a system row’s library, keeps its name, and every package that references it reaches the same definition.

A C name may be another name for a function or variable the same source defines, an alias. <zane_ffi.h> (§3.4) gives the form of one that holds on every target, Mach-O’s among them, whose C has no alias attribute: ZANE_ALIAS("target") after a declaration makes the declared name an alias of target.

int64_t helper(void) { return 2; }
int64_t named(void) ZANE_ALIAS("helper");

The source defines target with external linkage, a function for a declared function and a variable for a declared variable, and does not define the declared name itself. It is a build error, at the mark, when any of these does not hold. An alias belongs to the package as the definition it names does.

A section is gathered by its name rather than renamed. A linker set is a section the linker gathers from every object into one range that C reaches, in the form the target’s object format gives it:

  • On ELF, a section whose name is a C identifier, whose range C reaches through the __start_ and __stop_ symbols named after it.
  • On Mach-O, a section named SEGMENT,section where either name lacks the leading __ of the system’s own segments and sections, whose range C reaches through the section$start$SEGMENT$section and section$end$SEGMENT$section symbols.
  • On COFF, a grouped section name$suffix, which the linker gathers under name in the order of its suffixes, where name is none of the sections the compiler, the linker, and the C library group themselves, such as .text, .CRT, and .idata.

A package’s linker set would be shared with every other package, and every other version of the package, that places data in a section of that name. It is a build error, naming the package and the section, when the package’s C sources or archives place anything in one.

2.6 Release archives hold the C objects

A project’s release archive holds the objects compiled from its library packages’ C sources beside their Zane objects (dependencies.md §3.1), for the archive’s target. Its prebuilt archives are part of its source tree. A consumer compiles against a dependency’s source (dependencies.md §14), C sources and interface headers included, so the compiler reads a dependency’s C code as it reads its Zane code: to check foreign verbs against their declarations (§3.7) and to evaluate calls at compile time (§5.4).

A release dependency’s objects were compiled by that package’s own compiler (dependencies.md §14), which may compile its C differently from the consumer’s, such as against other system headers. A call into the dependency’s C is evaluated at compile time only where the consumer, compiling the dependency’s source, gets the same code the dependency’s object was compiled from. Where it does not, the call runs only in the running program, as a call into an archive does (§2.4), and a call marked comptime that reaches it is a compile-time error.

Story: stories/ffi.md — “The toolchain builds the C, and CMake does not”.


3. Foreign Verbs

3.1 A foreign verb’s body is a C function

A foreign verb is a function or method whose body is a C function. In place of a body, it names the C function’s symbol:

@primitives$I64 crc32(crc @primitives$I64, data @primitives$String) foreign "zane_crc32";

A call to a foreign verb calls the C function with the arguments the call passes, converted as §3.3 states, and gives its result converted back. Everything else about the verb is Zane: it is declared at package scope, it overloads, it is imported, it may be private through a leading _, and its calls follow its passing modes and the kind of verb it is (effects.md §3).

The symbol is the C function’s name as the interface header declares it. A foreign verb may be a function, a method, or a mut method. It is never a constructor, an operator, or a subscript, and it takes no type or number parameter, no concept-typed parameter, and no block.

A package’s own Zane code wraps its foreign verbs in the verbs it offers. The foreign verbs are usually private to the package:

package zlib;

import core$;

@primitives$I64 _crc32(crc @primitives$I64, data @primitives$String) foreign "zane_crc32";

Int crc32(data String) => Int(_crc32(@primitives$I64(0), data));

See also: syntax.md §3.11 for the declaration forms.

3.2 Parameters become the C function’s arguments in order

A foreign verb’s parameters are the C function’s parameters, in order. A method’s subject is the first of them.

3.3 Types at the boundary

A foreign verb’s parameter and result types are drawn from the table below. Each corresponds to C types, and the C declaration’s type MUST be one of them (§3.7).

Zane type C type Crosses as
@primitives$I32 int32_t, uint32_t, int, unsigned int its 32 bits
@primitives$I32 int8_t, uint8_t, int16_t, uint16_t, char, signed char, unsigned char, short, unsigned short its low bits when passed; widened by the C type’s sign when returned
@primitives$I64 int64_t, uint64_t, long long, unsigned long long, and, on a target whose pointers are 64 bits, size_t, ptrdiff_t, intptr_t, uintptr_t its 64 bits
@primitives$U8 uint8_t, int8_t, unsigned char, signed char, char its 8 bits
@primitives$F32 float its value
@primitives$F64 double its value
@primitives$Bool bool its value
@primitives$Unit void, as a result only nothing
@primitives$String zane_bytes a view of its bytes (§3.4); a returned view is copied (§3.5)
@primitives$String, as a parameter only const char * its bytes followed by a NUL byte (§3.4)
@primitives$Array<T, n> or @primitives$ArrayRef<T, n>, T a C-compatible element type (below) const E *, E the C type of T a view of its n elements (§3.4)
@primitives$List<T> or @primitives$ListRef<T>, T a C-compatible element type zane_span, its elements of the C type of T a view of its elements (§3.4)
a C-compatible value type (below) a struct of corresponding fields (below) a copy of its value, laid out as the struct
a foreign handle H (§4) C *, C the handle’s C type; as a parameter, const C * where §4.4 says so the C object it refers to (§4.2, §4.4)
a function type (§3.6) a pointer to a function of corresponding types a callable C function (§3.6)
the subject of a foreign method whose type is a reference type and not a handle, a context (§3.8) void * an opaque pointer to the subject, valid for the call

A C type of the first, third, and fourth rows corresponds to its primitive only on a target where it has the primitive’s size, 32, 64, or 8 bits, and no padding bits, so that every bit of the primitive is a value bit of the C type: int on a target whose int is 32 bits, long long on one whose long long is 64. A C type of the second row corresponds to @primitives$I32 only on a target where it is at most 32 bits and has no padding bits. On any other target such a type corresponds to no Zane type and is reached through a shim (below). Likewise float and double correspond to @primitives$F32 and @primitives$F64 only on a target where they are IEEE 754 binary32 and binary64. A signed C type corresponds only on a target where it is two’s complement, as Zane’s integers are. An unsigned C type reads and writes the same bits as the signed primitive. So a uint32_t of 4294967295 is the @primitives$I32 -1. A signed 8-bit C type likewise reads and writes the same bits as @primitives$U8, which is unsigned, so a signed char of -1 is the @primitives$U8 255. A scalar primitive’s or @primitives$U8’s full-width C types are those of its own size: the four 32-bit types of the first row for @primitives$I32, the 64-bit types for @primitives$I64, float, double, and the five 8-bit types for @primitives$U8. Only they lay out as the primitive does, so only they appear inside an array, a list, or a struct.

A C-compatible element type is a scalar primitive, @primitives$U8, or a C-compatible value type. Its C type is a full-width C type of the primitive, or the struct corresponding to the value type. Only the element type determines whether a container crosses: an array or a list of C-compatible elements crosses whatever those elements are.

A C-compatible value type is a value type declared with struct whose every field is a C-compatible element type, an @primitives$Array<T, n> of one, or an @primitives$List<T> of one. Its C struct has the same number of fields, in the same order, each of the C type of the Zane field’s element type, an array of that C type, or a zane_span for a list; their names need not match. A C-compatible value type that holds a list, at any depth, crosses only where a list does (§3.5). A value of the type crosses as a copy, which the program lays out as that C struct, each field at the offset of the C field it corresponds to; placement determines the order in which Zane lays out the type’s own fields (memory.md §3.3). Zane never shares a C-compatible value in place: C receives a copy and returns one. The struct’s padding bytes hold no Zane value, so Zane fills them with zero whenever such a value crosses into C, as an argument or as a function value’s result (§3.6). C that reads the object’s bytes then sees the same bytes at compile time (§5.4) and at run time.

type Vec3 = struct {
    x @primitives$F64;
    y @primitives$F64;
    z @primitives$F64;
}
typedef struct { double x, y, z; } vec3;

A C type whose size differs between the targets the toolchain builds for, such as long, unsigned long, wchar_t, or long double, corresponds to no Zane type, and neither do size_t, ptrdiff_t, intptr_t, and uintptr_t on a target whose pointers are not 64 bits, so a C function that takes or returns one is reached through a C function of the package that converts it. A variadic C function is reached the same way.

Story: stories/memory.md — “The compiler orders a struct’s fields, and C receives a copy in its own order”.

3.4 Strings, arrays, and lists are lent for the call

A string, array, or list argument is a borrow (memory.md §2.9), and C receives a read-only view of it that is valid until the call returns. <zane_ffi.h>, a header the toolchain provides to every package’s C code, declares the two view types:

typedef struct zane_bytes { const uint8_t *data; size_t size; } zane_bytes;
typedef struct zane_span  { const void    *data; size_t count; } zane_span;

A string’s view holds its bytes, with no terminator. A list’s view holds its elements, which the program lays out as an array of the C type of T; it lays out an element of a C-compatible value type as its C struct, each list inside it as a view of its own. C reads a view and never writes through it or keeps it after the call returns.

A string parameter whose C type is const char * reaches C as a pointer to its bytes followed by a NUL byte, valid until the call returns, as a view is. C reads it as a C string, up to its first NUL, so a string holding a NUL byte reaches C as the bytes before it. The toolchain makes the terminated bytes for the call, so a foreign verb whose C function takes a zane_bytes pays nothing for them. C never writes through the pointer or keeps it after the call returns.

3.5 Results

A foreign verb’s result is a scalar primitive, @primitives$U8, @primitives$Bool, @primitives$Unit, @primitives$String, a C-compatible value type that holds no list, or a handle. An array, a list, a C-compatible value type that holds a list, and a function type cross into C only as parameters, apart from the result of an exported lambda (packages.md §8.5).

  • A returned zane_bytes is a view of bytes C owns, or of bytes zane_bytes_alloc gave it (below). The call copies bytes C owns into a new string primitive before it returns, so they stay valid after the C function has returned, until the copy is made: they are C memory that outlives the call, such as memory a handle owns, and never storage that ends when the C function returns. A view whose size is zero gives an empty string, whatever its data.
  • <zane_ffi.h> declares uint8_t *zane_bytes_alloc(size_t size), which gives C size writable bytes for the string the call returns. A returned view that starts at the bytes the call’s last zane_bytes_alloc gave, and whose size is at most the size passed to that call, becomes the string without a copy; any other view is copied like a view of any other bytes. C calls zane_bytes_alloc only on the thread of the foreign call, during it, and never frees what it gives: the bytes the string does not take are released when the call returns.
  • A returned handle is a new roaming owner (§4.2), of a type with a free function. A foreign verb that returns one may declare @primitives$Unit as its abort type, and the call aborts when the C function returns a null pointer:
^Deflater?@primitives$Unit _deflater(level @primitives$I32) foreign "zane_deflater_new";

A foreign verb returning a handle without an abort type stops the program when the C function returns a null pointer. No other foreign verb declares an abort type: a C function signals failure through its result, and the package’s Zane code turns that into an abort.

3.6 Function values cross as C functions

A parameter of function type (syntax.md §2.13) whose parameter types are among those below, whose result type is a scalar primitive, @primitives$U8, @primitives$Bool, a C-compatible value type that holds no list, or @primitives$Unit, and which has no abort type and no subject other than a context (§3.8), corresponds to a C function pointer of the corresponding types. C receives a function that calls the Zane function value it was passed.

Parameter or result C type
a scalar primitive or @primitives$U8 a full-width C type of it (§3.3)
@primitives$Bool bool
a C-compatible value type the corresponding struct
@primitives$Unit, as the result only void
@primitives$String, as a parameter zane_bytes
@primitives$List<T>, T a C-compatible element type (§3.3), as a parameter zane_span, its elements of the C type of T
@primitives$Array<T, n>, T a C-compatible element type, as a parameter const E *, E the C type of T
a data handle H (§4), as a parameter C * or const C *, C the handle’s C type
a roaming owner ^H of a data handle, as a parameter C * or const C *

A context subject this T or this T mut corresponds to a first parameter void * (§3.8). A C integer type narrower than its primitive, such as char or int16_t for @primitives$I32, corresponds to no function value’s parameter or result, and a string reaches a function value only as a zane_bytes.

C lends what it passes for the call, as Zane lends C a view (§3.4): the function value receives a string, a list, or an array of its own, which the program copies from the bytes or elements C passed before the function value runs, so it keeps nothing of C’s. A data handle H is a borrow of the C object C passes, which the function value never releases. A roaming owner ^H takes the C object: C gives up the object, and the handle releases it through its free function when it is destroyed, as a handle a foreign verb returns does (§4.2). C passes no null pointer for either; a null one stops the program, as a foreign verb’s null handle result does (§3.5).

C may call the function value any number of times before the foreign verb returns, and never after. What the function value reads and writes counts toward what the call reads and writes (effects.md §5.2).

C may call a function value from several threads at once when it touches no capability and declares no context of a mut foreign method. C calls one at a time the function values one foreign call is passed that read or write capability-backed state, together with those that declare the context of a mut foreign method (§3.8): while a call to any of them is running, C starts no call to it or to another of them. That keeps their accesses ordered against each other, since the compiler orders the foreign call as a whole against the state it reaches (concurrency.md §4.5) and against its subject, and sees none of the calls C makes inside it.

3.7 The declaration is checked against the interface header

The compiler checks each foreign verb against the declaration of its symbol in the package’s interface headers, as the package’s C code sees them when compiled for the build’s target. It is a compile-time error, reported at the foreign verb, when:

  • no interface header of the package declares the symbol as a function;
  • the C function takes a different number of parameters than the foreign verb passes (§3.2), or is variadic;
  • a parameter’s or the result’s C type is not one §3.3 gives for the Zane type;
  • a handle’s pointer is const where §4.2 or §4.4 requires it writable, or writable where §4.4 requires it const;
  • a handle stands where §4.4 does not allow it;
  • the C layout of a C-compatible value type’s fields in declaration order differs from its C struct in a corresponding field’s offset or type, or in size or alignment.

The check sees each target’s C types, so a declaration that is correct for one target and wrong for another fails in the build for that other target.

3.8 A foreign method’s subject is the context of its callbacks

A Zane lambda captures nothing (concurrency.md §5.2), so a function value that C calls back reaches program state only through what C passes it. C libraries pass that state as a pointer the caller supplies and the callback receives back, often named userdata. In Zane that pointer is the subject of a foreign method whose subject’s type is a reference type and not a handle: the context. C receives it as a void *, and a function value the call is passed may declare the same subject, which C passes back as the callback’s first argument.

The subject’s type determines how the subject crosses. A subject of a C-compatible value type crosses as a copy of its struct (§3.3), which C reads. A mut foreign method on such a subject is a compile-time error, because C never writes Zane memory and a change to the copy would never reach the caller’s value. A subject of a reference type crosses as a context, which C only passes back. A context is an object with identity and a place, which is what a callback needs to change in place. A foreign method whose subject is of any other value type is a compile-time error. State of such a type, such as an app whose widgets hold strings and lists, travels in a reference type that holds it:

type Running = #struct { app App; }

Unit runLoop(this Running mut, onResize Unit[this Running, @primitives$I32, @primitives$I32] mut)
    foreign "app_run_loop";

running!runLoop(Unit(this Running, w @primitives$I32, h @primitives$I32) mut {
    this.app!frame(gui$Size(Px(Float(w)), Px(Float(h))));
    return Unit();
});
void app_run_loop(void *app, void (*on_resize)(void *app, int32_t w, int32_t h));

A function value’s subject MUST have the context’s type. It may be this T mut only when the foreign method is mut, and then whatever the function value writes counts as the foreign call writing its subject (effects.md §5.2); a non-mut foreign method passes a context its function values only read. C never reads or writes through the pointer, and passes back only the pointer the call received, during the call: the context is a borrow of the foreign method’s subject, and ends when the call returns. When the foreign method is mut, C calls the function values that declare the context one call at a time, together with those that touch a capability (§3.6).

Story: stories/ffi.md — “A callback reaches its state through the subject”.

Story: stories/ffi.md — “C never writes Zane memory”.

Story: stories/ffi.md — “C strings, allocated results and callback arguments cross without shims”.


4. Foreign Handles

4.1 #handle

A foreign handle is a reference type whose value refers to one C object. Its declaration names the C type of those objects and, where Zane owns them, the C functions that replace and release one:

type Fs       = #handle{ cType = "zane_fs_t"; }
type File     = #handle{ cType = "zane_file_t"; free = "zane_fclose"; }
type Deflater = #handle{ cType = "zane_deflater_t"; set = "zane_deflater_set"; free = "zane_deflater_free"; }
  • cType is a struct type, or a typedef name of one, that an interface header declares, completely or not, itself or through a header it includes, such as a C library’s own header.
  • free is the free function: a C function of the package’s interface headers that takes one non-const pointer to the C type and returns void or an integer type, such as the status SQLite’s sqlite3_close returns, which Zane ignores. It releases an object when its handle is destroyed (§4.2).
  • set is the setter: a C function of the package’s interface headers that takes two non-const pointers to the C type and returns void or an integer type, which Zane ignores. It replaces an object in place when a settled handle is overwritten (§4.3). A handle type with a setter has a free function.

A handle type without a free function is a foreign capability. Its values refer to C globals, are made only by foreign variables (§5.1), and stand for state outside the program. A handle type with a free function owns its C object, and is one of two kinds according to how it is made (§4.2):

  • an outside handle, made by a method on a foreign capability, whose C object is part of the state outside the program that the capability reaches, such as an open file or a database connection;
  • a data handle, made by any other foreign verb or by C passing its object to a function value (§3.6), whose C object holds state of the program’s own, such as a compression stream or a parsed document. A handle type no foreign verb returns is a data handle.

Every handle type is a reference type (types.md §2.1). A handle is moved rather than copied, is settled or roaming, and may be referenced through & like any reference type (memory.md §2). It has no fields: C reaches the object it refers to, and Zane reaches it only through foreign verbs.

See also: syntax.md §2.16 for the declaration form.

Story: stories/ffi.md — “C strings, allocated results and callback arguments cross without shims”.

4.2 A handle is made by a foreign verb and released when it is destroyed

A handle with a free function is made only from a pointer C gives up: one a foreign verb returns (§3.5), as a writable C *, since it is later passed to its free function, or one C passes to a function value’s roaming owner parameter ^H (§3.6), which takes only a data handle. It owns a non-null pointer to its C object from then on, and is released as below however it was made. A foreign verb never returns a foreign capability.

The foreign verbs that return one handle type are all methods on a foreign capability or none of them are. A type that one makes and another does not is a compile-time error, reported at the type’s declaration, because it would be outside state and program state at once.

When a handle is destroyed (lifetimes.md §2), its free function is called with that pointer, exactly once, before the handle’s storage is reused. A handle whose owner moves on keeps its object, and only the handle that is finally destroyed releases it. A foreign capability has no free function, and destroying one releases nothing.

Free functions are C code that runs while a scope drains, so their order is observable where the scope’s other destruction is not. When one drain destroys several handles, their free functions run in the reverse of the order the handles were made. A handle made from another, such as a statement prepared on a connection, is made after it, so it is released before it (§4.5).

C keeps each object at the address its handle holds until the free function releases it, and other C objects may hold that address. Zane therefore never moves a C object, and replaces one only through its setter.

4.3 A settled handle is overwritten through its setter

Overwriting a settled owner writes the replacement at the same address, so references to it see the replacement (memory.md §2.2). A foreign handle in a settled place, directly or through the owning fields of the objects that place holds, is overwritten the same way through its setter: the setter receives the pointer the place holds and the replacement’s pointer, and leaves the object at the first address holding the replacement.

void zane_deflater_set(zane_deflater_t *dst, zane_deflater_t *src);

The setter releases the object dst held, moves src’s object into dst, and frees what remains of src. The replacement handle is consumed by the call, and its free function is not called. The object keeps its address, so a C object that holds that address reaches the replacement.

The handle keeps its place in the release order of §4.2 too. Handles made from its object hold that address and now reach the replacement, so they are still released before it. A replacement made from a handle that is younger than the handle it replaces is therefore released after that handle, and a binding whose replacement depends on such an object has to keep the dependency alive itself, as it must for any dependency the order does not cover (§4.5).

One overwrite can reach several handles, such as a struct that holds a connection and a statement prepared on it. Their setters run in the reverse of the order the handles were made, the order a drain releases them in (§4.2), so a handle’s old object is released before the old object it was made from.

A replacement is a roaming value, and a roaming owner is never referenced (memory.md §2.1), so no handle that holds an & to its dependency (§4.5) depends on a replacement, in the same overwrite or anywhere else. Only a binding that hands C a dependency without such a reference can make an object depend on one, and it never passes that dependency to a setter as the replacement, since the setter frees the replacement’s storage (§6).

An assignment that would overwrite a handle in a settled place is a compile-time error when the handle’s type has no setter. A foreign capability has none, so a settled place holding one is never overwritten. A roaming owner, a ListRef element, and a variant payload are overwritten as any reference-type owner is: the old handle is destroyed (§4.2) and the new handle takes its place.

d Deflater = _deflater(@primitives$I32(6));
d = _deflater(@primitives$I32(9));          // calls zane_deflater_set

log fs$File = files!fs$open("a.log");
log = files!fs$open("b.log");             // ERROR: File has no setter

Story: stories/ffi.md — “Replacing a handle in place”.

4.4 Passing a handle

A handle crosses into C as the pointer it holds. The position of the handle in the foreign verb’s signature determines how C may use the object:

  • A foreign capability stands, in a foreign verb, only as the subject of a foreign method (§5.1). C receives C * when the method is mut, and const C * otherwise.
  • An outside handle stands, in a foreign verb, only as a parameter other than the subject of a method on a foreign capability, or as its result. C receives C * when the method is mut, and const C * otherwise: the object is part of the outside state the capability reaches, so a write through the capability may change it.
  • A data handle stands anywhere. C receives C * when it is the subject of a mut foreign method, and const C * in every other position, where it only reads the object.

A data handle is written only as the subject of a mut method, because a mut method writes this and no other state of the program (effects.md §4.2). A C function that changes two data handles is reached through C functions of the package that each change one of them.

@primitives$String _deflate(this Deflater, input @primitives$String) mut foreign "zane_deflate";
@primitives$I64 _totalIn(this Deflater) foreign "zane_deflater_total_in";

@primitives$Unit _write(this Fs, file File, data @primitives$String) mut foreign "zane_fwrite";
zane_bytes zane_deflate(zane_deflater_t *deflater, zane_bytes input);
int64_t zane_deflater_total_in(const zane_deflater_t *deflater);

void zane_fwrite(zane_fs_t *fs, zane_file_t *file, zane_bytes data);

4.5 A C object that depends on another

A C object that refers to another, such as a prepared statement and the connection it belongs to, needs that other object to outlive it. A handle expresses this through an & to the other held beside it, so that the store rule keeps the referenced handle alive at least as long (lifetimes.md §1.1):

type Sqlite        = #handle{ cType = "zane_sqlite_t"; }
type Database      = #handle{ cType = "sqlite3"; free = "zane_sqlite_close"; }
type _RawStatement = #handle{ cType = "sqlite3_stmt"; free = "zane_sqlite_finalize"; }

type Statement = #struct {
    _raw _RawStatement;
    _database &Database;
}

The store rule allows the two to share a scope, and then both die in one drain; the reverse order of §4.2 releases the statement first, since it was made from the connection. A settled Database is never overwritten, since it has no setter, so the connection a statement holds stays the connection it was prepared on. The compiler knows nothing of how C objects refer to one another, so a binding that hands C a dependency without such a reference, or makes the dependent first, has to keep the dependency alive itself (§6).

Story: stories/ffi.md — “C never writes Zane memory”.


5. Effects of Foreign Code

5.1 Foreign capabilities come from C globals

A package whose C code reaches state outside the program offers that reach as a C global, of a C type its interface header declares and a foreign capability names:

// fs.h
typedef struct zane_fs_t   zane_fs_t;
typedef struct zane_file_t zane_file_t;

extern zane_fs_t zane_fs;

zane_file_t *zane_fopen(zane_fs_t *fs, zane_bytes path);
void zane_fwrite(zane_fs_t *fs, zane_file_t *file, zane_bytes data);
void zane_fclose(zane_file_t *file);
package fs;

type Fs   = #handle{ cType = "zane_fs_t"; }
type File = #handle{ cType = "zane_file_t"; free = "zane_fclose"; }

^File open(this Fs, path @primitives$String) mut foreign "zane_fopen";
@primitives$Unit write(this Fs, file File, data @primitives$String) mut foreign "zane_fwrite";

A foreign variable declares a symbol whose value is a handle to such a global. It is written like a symbol declaration, with foreign and the global’s symbol in place of an initializer:

Unit main() {
    files fs$Fs foreign "zane_fs";
    log fs$File = files!fs$open("app.log");
    files!fs$write(log, "started\n");
}

The symbol is a settled owner of a foreign capability. The global is looked up in the package that declares the capability’s type: its C code defines it with external linkage, and one of its interface headers declares it with the type’s C type. It is a compile-time error, reported at the declaration, when no such global exists, its C type is another, or it is thread-local (_Thread_local), since a thread-local global is a separate object on each thread and a call that reaches it may run on any thread.

Only the root package and exported lambdas declare foreign variables (packages.md §6.1, §8.4). A foreign variable anywhere else is a compile-time error. Every other package reaches a foreign capability only through one passed to it, as it reaches the program’s console (effects.md §6.6), so a package that touches outside state shows it in what it receives. Two foreign variables that name one global are two handles to one C object, and since a foreign capability has no free function or setter, neither releases or replaces it.

Story: stories/ffi.md — “Capabilities come from C”.

5.2 What a foreign verb touches follows from its signature

What a foreign verb reads and writes outside the program is determined by its signature, and the compiler derives the effects of each call from that signature (effects.md §5.2):

Foreign verb Outside the program, it
A function, or a method on a data handle reads and writes nothing itself
A method without mut on a foreign capability reads capability-backed state
A mut method on a foreign capability reads and writes capability-backed state

The state an outside handle refers to is capability-backed state, and a foreign verb reaches it only as a method on a foreign capability (§4.4). A foreign verb on a data handle that is mut writes the handle, which is state of the program, and nothing outside it.

A call also reads and writes whatever the function values it passes read and write (§3.6), as the compiler derives it for any call through a function value (effects.md §5.2, §5.5). So a call that passes no capability, but passes a function value that writes the console, writes the console.

Calls then follow the ordinary rules (effects.md §3, §4). A read needs a verb that reaches the capability. A write needs the capability writable: in the root package or an exported lambda, a foreign variable it declares; elsewhere, a capability reached through this of a mut method, stored from a writable source (effects.md §6.2).

package time;

type Clock = #handle{ cType = "zane_clock_t"; }

@primitives$I64 _nowNanoseconds(this Clock) foreign "zane_clock_now_ns";

type Timer = #struct { _clock &Clock; }

Timer(clock &Clock) => init{_clock = clock;}

Int nanoseconds(this Timer) => Int(this._clock:_nowNanoseconds());

Destroying an outside handle calls its free function (§4.2), which may change outside state, such as by closing a file, and overwriting one calls its setter (§4.3). Both count as writes to capability-backed state: they happen at run time, where the destruction or the assignment falls, ordered as §5.6 orders every capability write. Destruction is not a write in the sense of effects.md §4.1 (effects.md §7.2), so the owner of an outside handle may let it be destroyed whether or not the verb may write the capability that made it.

5.3 Making an outside handle is a write

A foreign verb that returns an outside handle MUST be a mut method on a foreign capability. Opening a file or a connection is a write to outside state, and it gives its owner a handle to that state, so only a verb that may write the capability may make one.

^Database?@primitives$Unit _open(this Sqlite, path @primitives$String) mut foreign "zane_sqlite_open";
^_RawStatement?@primitives$Unit _prepare(this Sqlite, database Database, sql @primitives$String) mut foreign "zane_sqlite_prepare";

A foreign verb that returns a data handle is any foreign verb that is not a method on a foreign capability.

5.4 Compile-time evaluation of foreign calls

A call to a foreign verb that reads no capability-backed state computes its result from its arguments and from the data handles it is passed. It is therefore a computation like any other under effects.md §5.3: when its arguments are known, the compiler may evaluate it at compile time. A computation in Zane that calls C folds through the call, and so does a data handle made, written, and destroyed within it.

Two computations that call C are never evaluated at compile time, because what they give is a fact of the running process rather than of their arguments:

  • one whose result depends on where an object is stored: the numeric value of an address, such as a pointer converted to an integer, or the order of two different objects’ addresses;
  • one that ends with a data handle it made still alive, since that handle’s C object exists only in the process that made it.

A call marked comptime that makes either computation is a compile-time error (effects.md §5.6).

Evaluating a foreign call at compile time changes nothing the program computes. Its result is the value the C function returns on the build’s target, as the C code is compiled for that target (§2.2), and for a release dependency as the dependency’s object was compiled (§2.6).

A call to a foreign verb that reads capability-backed state is not evaluated at compile time under effects.md §5.3, and neither is anything that depends on its result. Inside a call marked comptime it is evaluated with the call, reading the build machine’s state, when the compiler has the body of every C function it reaches (effects.md §5.6). A call that writes capability-backed state happens at run time, at the point and in the order the program makes it.

Story: stories/ffi.md — “Evaluating C as the target runs it”.

5.5 A foreign verb that touches outside state MUST be marked

The compiler cannot see what a C function reads or writes. It trusts each foreign verb’s signature (§5.2): a foreign verb that is not a method on a foreign capability is taken to read and write nothing outside the program.

A foreign verb whose C function reads or writes state outside the program MUST be declared as a method on the foreign capability that reaches that state, mut if it writes. Outside state includes anything one run of the program can see differently from another: a file, the clock, the environment, a random source, a device, another process, and a C variable that another call changes.

A data handle’s free function and setter MUST read and write no outside state. They are named by the handle’s type rather than declared as verbs, so no capability can mark them, and they run where the program destroys or overwrites the handle, which a computation evaluated at compile time may do (§5.4). An outside handle’s free function and setter may touch outside state, since running them counts as a capability write (§5.3).

A foreign verb, free function, or setter that breaks these rules gives the program no meaning the spec defines. In particular, the compiler may:

  • evaluate the call at compile time, so that the value the outside state held on the build machine, while the compiler ran, is built into the program, and every run of the program gives that value;
  • leave the call out when its result is unused, so that what it writes is never written;
  • make one call where the program makes two with the same arguments, or reorder the call, or run it in parallel with other work (concurrency.md §2.1).

A package that counts with a C variable shows how:

static int64_t count;

int64_t zane_counter_next(void) { return ++count; }
package counter;

@primitives$I64 next() foreign "zane_counter_next";   // WRONG: changes a C variable the next call reads, and is no method on a capability
console!print(Int(counter$next()));
console!print(Int(counter$next()));
console!print(Int(counter$next()));

Each call counter$next() depends on nothing the program reads at run time, so an optimized build may evaluate each one while it compiles, from the variable’s first value. The program then prints 1 three times where an unoptimized build prints 1, 2, and 3, and no diagnostic reports this. The same holds for a C function that reads the environment or the clock, whose fold builds the value the build machine saw into the program. Declared as a method on a capability, the call is a write, and it happens where the program runs, in the order the program makes it:

typedef struct zane_counter_t { int64_t count; } zane_counter_t;

zane_counter_t zane_counter;

int64_t zane_counter_next(zane_counter_t *counter) { return ++counter->count; }
package counter;

type Counter = #handle{ cType = "zane_counter_t"; }

@primitives$I64 next(this Counter) mut foreign "zane_counter_next";
Unit main() {
    counter counter$Counter foreign "zane_counter";
    console!print(Int(counter!counter$next()));
}

Only the author of a binding can keep this rule, and every user of the binding relies on it.

Story: stories/ffi.md — “Impurity is a capability, not a comment”.

5.6 Concurrency

A call to a foreign verb that touches no capability may run in parallel with other work, including other calls to the same C function, as any call that writes nothing its caller can see (concurrency.md §2.1). Its C code is therefore called from more than one thread at a time.

C state is independent per handle. A mut method on a foreign capability that takes an outside handle writes that handle’s state and only reads the capability’s own; one that takes no outside handle writes the capability’s own state. A method without mut reads the same state. Two calls therefore conflict only when they reach the same capability’s own state or the same outside handle’s, and the destruction and overwriting of an outside handle (§5.2) count as writes to that handle’s state and to the own state of the capability whose method made it (§4.4). Conflicting calls are ordered by concurrency.md §4.5: reads may run together, and a write is serialized with every other read and write of the same state. The compiler cannot see what C code behind two handles shares, so a package whose C does share state between them routes every call that reaches it through methods of one capability that take no outside handle; the destruction of that capability’s outside handles is ordered against those calls already.

A foreign handle is a reference type, so a spawned call never mutates one (concurrency.md §4.2), though a worker pool’s element may own one (concurrency.md §6.4). Calls that only read a handle may run at the same time.


6. What Foreign Code Upholds

The compiler takes a foreign verb’s signature as a description of its C function, and builds the rest of its guarantees on it. A C function reached through a foreign verb upholds each of the following. One that breaks any of them gives the program no meaning the spec defines.

  • It touches outside state only as declared (§5.5).
  • It writes only what it is given writable: the C object of its mut subject, the outside handles a mut method on a foreign capability is passed, C memory that C owns, and the bytes zane_bytes_alloc gives it during the call (§3.5). It writes no view (§3.4), and no handle passed as const.
  • It keeps nothing Zane lent it. A view, a function pointer for a function value (§3.6), a context (§3.8), and the bytes zane_bytes_alloc gives it are used only until the call returns. It reads and writes nothing through a context, and passes it back only to the call’s own function values.
  • It holds a C-held object only as a pointer. C never reads or writes through the pointer to an object an exported lambda returned, passes it back only to exported lambdas that take that object’s type, and obeys the host rule (packages.md §8.5, concurrency.md §4.7).
  • It is safe to call from several threads at once when it touches no capability (§5.6), and when it only reads a handle, alongside other reads of that handle.
  • It returns. It leaves by returning to its caller, not by a long jump, an exception, or exiting the process. A C function that stops the program, such as by calling abort, stops it as a fault does.
  • It keeps its C objects consistent with their handles. A free function is called once per object, as §4.2 states. A C object stays at the address its handle holds until its free function releases it (§4.2), and stays alive while a C object that refers to it does (§4.5). A setter leaves the object at the address it was given, holding the replacement (§4.3), and no C object holds the address of a replacement passed to a setter.
  • It treats a function value as a call. A function value’s C function is called with the types §3.6 gives, on any thread, while the foreign verb runs, and one call at a time among the function values the call is passed that touch a capability or declare the context of a mut foreign method (§3.8).

7. Language Comparisons

Feature Zane Rust Zig Go (cgo)
C compiled by the language toolchain for any target ✅ ❌ (a build script and the cc crate) ✅ ⚠️ Note: a C cross compiler must be installed
A call into C can be evaluated at compile time ✅ ❌ ❌ ❌
Effects of a C function visible in its signature ✅ ❌ ❌ ❌
C signatures checked against headers ✅ ⚠️ Note: generated from them by bindgen, or written by hand unchecked ✅ (@cImport) ✅
C objects released deterministically by their owner ✅ ⚠️ Note: by a hand-written Drop ❌ (by defer) ❌
C may keep a pointer to the language’s memory ❌ ✅ ✅ ⚠️ Note: only within cgo’s pointer rules

7.1 Zane vs. Rust (extern blocks)

Rust declares a C function in an extern block and calls it in unsafe code. The compiler sees only the declaration, so every call is opaque.

Rust:

extern "C" { fn crc32(crc: u64, data: *const u8, len: usize) -> u64; }
let c = unsafe { crc32(0, b"hello".as_ptr(), 5) }; // never computed at compile time

Zane:

@primitives$I64 _crc32(crc @primitives$I64, data @primitives$String) foreign "zane_crc32";
c @primitives$I64 = _crc32(@primitives$I64(0), @primitives$String("hello")); // may fold
Difference Rust Zane
What the compiler knows of the C function its declaration its declaration and its body, compiled from source
Building the C a build.rs program run on the build machine the toolchain, from settings in the package’s c.coda
Effects none are stated; any call may do anything stated by the signature, and taken as true

7.2 Zane vs. Zig (@cImport)

Zig imports a C header and compiles C with its own toolchain for any target, which Zane’s toolchain does as well. Zig exposes C’s types to Zig code as they are, pointers included, and runs C only at run time.

Difference Zig Zane
What crosses the boundary C’s own types, pointers included the types of §3.3; a C object only behind a handle
C at compile time never when the call touches no capability, and inside a comptime call when it reads one
Releasing a C object defer written at each use the handle’s free function, when the handle is destroyed

8. Summary

Concept Rule
A package’s C code Its C sources (the .c files in its directory and those listed in its c.coda) and its interface headers (the .h files in its directory)
Building The toolchain compiles a package’s C as C17 for the build’s target; no build script runs; floating-point expressions are evaluated as written; the C compiler’s warnings fail no build and are shown for the project’s own package sources
c.coda A hand-written file in the package’s directory whose rows give its source, include, define, archive, and system settings, each for the targets a pattern matches
C symbols Belong to the package and version that defines them, so versions coexist; ZANE_ALIAS("target") makes a declaration an alias of a definition on every target; a linker set (a section the linker gathers by its name into a range C reaches, in each object format’s form) is a build error
Foreign verb A function or method whose body is a C function named by its symbol, checked against the package’s interface headers
Boundary types Scalar primitives, U8, Bool, Unit, strings, and arrays and lists of C-compatible elements as read-only views, C-compatible value types, handles, and function values
Function values Take scalar primitives, U8, Bool, C-compatible value types, strings, and lists and arrays of C-compatible elements, copied before the function runs, and data handles, borrowed or as a roaming owner ^H, which takes the C object; give a scalar, U8, Bool, a C-compatible value type that holds no list, or Unit
Context The subject of a foreign method whose type is a reference type and not a handle; a C-compatible value subject crosses as a copy and is never mut, and any other value subject is an error; C receives it as void * and passes it back, only during the call, as the first argument of a function value declaring the same subject; that function value may write it only when the foreign method is mut
C-compatible element type A scalar primitive, U8, or a C-compatible value type; an array or a list of one crosses, whatever the element
C-compatible value type A value struct of C-compatible elements and arrays and lists of them; it crosses as a copy laid out as its C struct, its fields in declaration order, a list as a zane_span; one holding a list crosses only where a list does
Views Valid for the call only; C never writes or keeps them; a returned zane_bytes is copied, unless it starts at the bytes the call’s last zane_bytes_alloc gave and its size is at most the size passed to that call; a string parameter C declares const char * arrives NUL-terminated
Foreign handle A #handle{ cType; set; free; } reference type referring to one C object; its free function releases the object when the handle is destroyed, and one drain releases its handles in the reverse of the order they were made
Overwriting a handle A settled handle is overwritten only through its setter, which keeps the C object’s address and the handle’s place in the release order; one overwrite runs its setters in the reverse of the order the handles were made; a type without one is never overwritten in a settled place
Writing a handle A data handle only as the subject of a mut foreign method; an outside handle in a mut method on a foreign capability; everywhere else C receives a const pointer
Null handle results Abort with Unit when the verb declares it, and stop the program otherwise
Foreign capability A handle type without a free function, referring to a C global; made only by a foreign variable
Foreign variable name T foreign "cGlobal", declared only in the root package or in the body of an exported lambda, giving a handle to a global of T’s package that is not thread-local
Outside and data handles A handle made by a method on a foreign capability is an outside handle, part of capability-backed state; any other is a data handle, program state
Effects A method on a foreign capability without mut: a read; with mut: a write; any other foreign verb: nothing outside the program
Making an outside handle Only a mut method on a foreign capability returns one
Compile-time evaluation A foreign call that reads no capability-backed state may be evaluated at compile time, giving the target’s result, unless the computation observes an address or leaves a data handle it made alive; inside a comptime call one that reads is evaluated too, when every C function it reaches has a body in the build
Undeclared outside state A foreign verb that touches outside state without its capability, or a data handle’s free function or setter that touches it at all, may be folded with the build machine’s value, dropped, merged, reordered, or parallelized
Concurrency Capability-free foreign calls may run in parallel; capability calls conflict only on the same capability’s own state or the same outside handle’s, since C state is independent per handle