fic/foreign/mod.rs
1//! Foreign declarations: the functions a program can call through an
2//! `import "file" as alias;`, and the readers that produce them from
3//! the imported source. No toolchain is embedded beyond what a reader
4//! itself needs (libclang for C; the Pancake reader and this module's own
5//! types need nothing).
6//!
7//! fic reads the imported source itself and records each exported function
8//! as an AST node (rustc's `ast::ForeignMod`/`ForeignItemKind::Fn`, but
9//! derived rather than hand-written). One [`ForeignModule`] per imported
10//! file is loaded into [`ForeignModules`], beside the AST, numbered by its
11//! `ModuleId`, with each program's imported files (`links`). `rast` resolves
12//! an `alias.f` to the export and keeps each called function once, by
13//! `FfnId`: its name and signature, lowered to fic's types
14//! (`ty::foreign`, one lowering per language), in `Env.ffis`, and where it
15//! is, in the store's `ffis`. The backends read the files, the called
16//! functions and the links from the store.
17//!
18//! Signatures are kept in a **surface** form ([`ForeignType`]): the type as
19//! the foreign language spells it, reduced to what any C-ABI boundary can
20//! express. Lowering to fic types (`uint8_t` → a fic scalar, a pointer →
21//! `Address`, `struct Slot` → a fic struct) is `ty::foreign`'s, which
22//! `rast` runs on each called function, not this module's.
23//!
24//! Which files can be imported, what counts as exported, and how each is
25//! read:
26//!
27//! | file | language | reader | exported functions |
28//! |-----------|----------|--------------------------------------------------------|--------------------|
29//! | `.h`/`.c` | C | [`c`]: libclang, the real C front end | non-`static`, non-`inline` prototypes/definitions |
30//! | `.pnk` | Pancake | [`pnk`]: a text scanner, no external tool run; its `/@ … @/` contracts parsed by the `pancake2viper` crate (linked in) and lifted in `pnk::annotation` | `export fun` (≤ 4 word arguments, one word return), with its `/@ … @/` annotations |
31//!
32//! Every language has a reader, so every module is complete: a call
33//! resolves to one of its functions or is an error (`rast`).
34//!
35//! Calling convention, layout, symbol location and any runtime set-up
36//! (Pancake's heap initialisation) are the backend's concern, not this
37//! module's. This module keeps what the reader found — a [`ForeignContract`]
38//! beside each signature; `pnk::annotation` and `c::acsl` give it meaning.
39
40pub mod c;
41pub mod calls;
42pub mod contract;
43pub(crate) mod export;
44mod extract;
45mod load;
46mod module;
47pub mod pnk;
48
49pub use export::{export, Header, DIR as INCLUDE_DIR, MARKER};
50pub use extract::{extract, Lift};
51pub use load::{load, parse, Reader};
52pub use module::{
53 Annotation, ForeignContract, ForeignFn, ForeignFnRef, ForeignKind, ForeignLang, ForeignModule,
54 ForeignModules, ForeignParam, ForeignType, Link,
55};