adam cimport
A C library's headers turned into an Adam binding unit.
Examples: res\examples\language\cimport holds small, commented programs for this page's features, each listing the output it prints; run one with adam build <file> -r.
What it writes
adam cimport <spec> turns the C headers an import spec (<name>.cimport.toml) names into an Adam .C binding unit. The unit holds the headers' routines and variables as external declarations with C linkage, records laid out as C lays them out, routine types, and the headers' macros as typed constants and routines, all under C's own names.
The standard library's .C units are written this way, from specs that ship in res/cimport beside howto.C.cimport.toml, an annotated spec of every option. A program imports such a unit as any other:
module exe cimport_zlib;
import System.Zlib.C;
var
LData: array[0..4] of Bytef;
begin
// the bytes of "hello"; crc32 takes a pointer to the first
LData[0] := 104;
LData[1] := 101;
LData[2] := 108;
LData[3] := 108;
LData[4] := 111;
println("crc32 of hello = {}", crc32(0, addr of LData[0], 5));
end.
crc32 of hello = 907060870
The command
| Option | What it does |
|---|---|
-o <folder> | writes the unit into that folder |
-t <triple> | imports for that one target |
--quiet | prints only the reports |
--emit-pp | prints the preprocessed headers and stops |
--emit-unit | also prints the unit written |
--emit-layout | also prints the layout probe |
The spec
A key or table a spec does not know is an error at its line (CP0004), never ignored, and a format Adam does not read is CP0005. Every folder a spec names is relative to the spec's own folder, and a folder read from must exist (CP0009).
| Table | What it holds |
|---|---|
[import] | the header or headers, the unit written, the library (one name, or one per target triple), the linkage ("static", the default, or "dll": each external clause then names the target's DLL), the output folder, the sources folders whose declarations are bound, layoutprobe and the targets |
[preprocess] | defines, undefines, includes and systemincludes: each one list for every target, or a table of lists by triple |
[uses] | header globs whose declarations another unit owns: the written unit imports it instead of writing them again |
[skip] | C names left out, by glob |
[renames] | an Adam name for a C name |
[[insert]] | text put before, after or in place of an anchor in the written unit |
Headers are searched in clang's order: the includes, then the system includes, then the system headers Adam ships. Only the sources folders' declarations are written: another header the imported ones include is read, so its types are known, and a struct of it held by value is not bound (CI0001).
With several targets, the headers are imported once per target and one unit is written: the lines every target shares once, the lines they differ in under @ifdef TARGET_<triple> branches (Directives).
A renamed external routine keeps binding the C symbol through name "sym" (Another name for a symbol), and a C name that is an Adam keyword gets a trailing _ by itself. An [[insert]] whose anchor is not there is CI0002, and no unit is written.
How C becomes Adam
- Bitfields, anonymous members, flexible array members (
array[0..] of T) and#pragma pack/packed/alignedare laid out by the target's C rules (Choices, records and arrays).layoutprobewrites a C file of_Static_asserts that compiles only if Adam's layouts equal C's. - An enum is an alias of its integer type, with each value a typed constant, which a translated
static inlinefunction or function-like macro names rather than writing its number. - An object-like macro is a typed constant by C's constant rules; a function-like macro is an Adam routine, generic where no bound routine types it.
- A
static inlinefunction is an Adam routine. - What Adam cannot express is warning CI0001, naming the declaration and the reason: never dropped silently.