feat(ffi-linkage): fn-path accepts postfix extern/export + lib/name fields (Phase 1.0a)

parseFnDecl now calls parseOptionalExternExport() after the callconv
slot and stores the modifier on FnDecl.extern_export. For 'extern' the
body is ';' (an empty-block placeholder — the modifier carries the
linkage, no *_expr node, per the naming constraint). Both fn-decl
lookahead predicates (isFunctionDef, hasFnBodyAfterArrow) now treat
kw_extern/kw_export as fn-body markers beside kw_callconv, so
'(...) -> R extern;' is recognized as a fn def rather than a fn-type
const.

Per user feedback, decision 4 ("library separate") is REVISED: extern
carries an optional LIB + "csym" axis mirroring '#foreign LIB "csym"',
so it is a true #foreign superset (Gate A->B requirement — the Part B
migration of 466 #foreign uses across 6 libs must preserve each
symbol's library). Added FnDecl.extern_lib/extern_name and
VarDecl.extern_lib (beside is_extern/extern_name).

All unconsumed by lowering: extern parses, but a fn still errors at
sema (body produces no value). Suite green (443 unit / 633 corpus).
lock commit.
This commit is contained in:
agra
2026-06-14 13:02:42 +03:00
parent 62a3b46f6e
commit df6b675e67
4 changed files with 85 additions and 38 deletions

View File

@@ -5,33 +5,40 @@ Companion to `current/PLAN-EXTERN-EXPORT.md` — one merged plan: **Part A** add
every commit, one step at a time per the cadence rule.
## Last completed step
**Phase 0.1** (lock) — added `ast.ExternExportModifier = enum { none, extern_,
export_ }` (beside `CallingConvention`), `FnDecl.extern_export` (default `.none`),
`VarDecl.is_extern`/`extern_name` (defaults absent), and
`parser.parseOptionalExternExport()` (mirrors `parseOptionalCallConv`, just below it
at `parser.zig:~3683`). **Helper + fields defined but NOT consumed by any decl path**
— no user-facing behavior change, corpus diff empty. Two inline parser unit tests
(`parseOptionalExternExport recognizes linkage keywords`, `extern/export AST fields
default to absent`). Suite green (443 unit [+2] / 633 corpus, 0 fail).
**Phase 1.0a** (lock, green — parse half of 1.0) — the fn-decl path now ACCEPTS
postfix `extern`/`export`. `parseFnDecl` calls `parseOptionalExternExport()` after the
callconv slot and stores it on `FnDecl.extern_export`; for `extern` the body is `;`
(an empty-block placeholder — modifier carries the linkage, no `*_expr` node). Both
lookahead predicates (`isFunctionDef` + `hasFnBodyAfterArrow`) now treat
`kw_extern`/`kw_export` as fn-body markers (beside `kw_callconv`) so `(...) -> R
extern;` is recognized as a fn def, not a fn-type const. **Per user feedback (decision
4 REVISED):** added `extern_lib`+`extern_name` to `FnDecl` and `extern_lib` to
`VarDecl` (next to `is_extern`/`extern_name`) — the optional `LIB`+`"csym"` axis
mirroring `#foreign LIB "csym"`, so `extern` is a true `#foreign` superset (Gate A→B).
All unconsumed by lowering — `extern` parses but a fn still errors at sema
(`body produces no value`). Suite green (443 unit / 633 corpus, 0 fail).
## Current state
Syntax decided + ratified: bare `extern`/`export`, postfix in the `callconv(.c)`
slot, `extern callconv(.c)`, library separate. Touch-points mapped — token
`token.zig:45,282`; parser `1950,3669,316,425,1305`; lowering
`decl.zig:1123,387,2110,2382,2514`; IR/emit already capable (no codegen change).
Export gap = 4 lowering conditions. Part B `foreign` footprint to purge: 643 lines /
~57 identifiers in `src/` + 28 doc lines. End-state invariant: **zero `foreign`** in
the live tree (Phase 9.4 gate). **Phase 0 done**: tokens (0.0) + AST/parser plumbing
(0.1) exist, unconsumed. Phase 1 wires `extern` into the fn/global decl paths.
Syntax: bare `extern`/`export`, postfix after `callconv(.c)`, `extern ⇒ callconv(.c)`.
**Decision 4 revised** (user 2026-06-14): `extern` carries an optional `LIB`+`"csym"`
axis (`extern_lib`/`extern_name`) like `#foreign`; the `#library` decl + build-flag
linking stays separate. Touch-points: token `token.zig:45,282`; parser
`1950,3669,316,425,1305`; lowering `decl.zig:1123,387,2110,2382,2514`; IR/emit already
capable (no codegen change). Part B `foreign` footprint to purge: 643 lines / ~57
identifiers in `src/` + 28 doc lines. End-state invariant: **zero `foreign`** (Phase
9.4 gate). **Done**: tokens (0.0) + AST/parser plumbing (0.1) + fn-path extern parsing
+ lib/name fields (1.0a), all unconsumed. Lowering not yet wired.
## Next step
**Phase 1.0** (xfail)accept postfix `extern` after the callconv slot in the
fn-decl path (`parser.zig:1950`: call `parseOptionalExternExport()`, store on
`FnDecl.extern_export`); add `examples/12xx-ffi-extern-fn.sx` that extern-binds a libc
symbol — **red** (parses, but lowering not wired yet). Then 1.1 green (lower via
`declareExtern`: `is_extern`, `.external`, `callconv(.c)`, no ctx — anchors
`decl.zig:1123,387,2110,2113`), 1.2 green (`extern "csym"` rename + extern-global
`g : T extern;`, `parser.zig:425`). Stop at end of Phase 1.
**Phase 1.0b** (xfail — example half of 1.0) — add `examples/12xx-ffi-extern-fn.sx`
that extern-binds a libc symbol (`abs`); expected files capture the SUCCESS output, so
the example is **red** now (parses, then errors at sema since lowering is unwired).
Then **1.1** (green): in `decl.zig`, when `extern_export == .extern_`, route the fn
through `declareExtern` (`is_extern`, `.external`, `callconv(.c)`, no ctx — anchors
`decl.zig:1123,387,2110,2113`) instead of lowering the placeholder body → example
green. Then **1.2** (green): `extern LIB "csym"` rename (consume `extern_lib`/
`extern_name`) + extern-global `g : T extern;` (`parser.zig:425`). Stop at end of
Phase 1.
## Open decisions
Part A ratified (bare / postfix / `⇒ callconv(.c)` / lib-separate). Part B (confirm

View File

@@ -10,14 +10,18 @@ superset of `#foreign`, and Part A isn't "done" until Part B reaches the invaria
**Decided syntax**
```sx
name :: (sig) -> Ret [callconv(.x)] [extern | export] [;|{…}]; // functions
Name :: #objc_class("X") [extern | export] { … }; // aggregates (mirrors `struct #compiler`)
g : Type extern ["csym"]; // extern global
name :: (sig) -> Ret [callconv(.x)] [extern | export] [LIB] ["csym"] [;|{…}]; // functions
Name :: #objc_class("X") [extern | export] { … }; // aggregates (mirrors `struct #compiler`)
g : Type extern [LIB] ["csym"]; // extern global
```
- `extern` = import (no body, external linkage, C ABI, no sx ctx) — `#foreign`'s role.
- `export` = define **and** expose (body + external linkage + C ABI + no ctx) — **new**.
- `extern`/`export` imply `callconv(.c)`; write `callconv` only to override.
- Library stays a separate axis (`#library`/build flags), not folded into `extern`.
- Optional `LIB` (a `#library` alias) + `"csym"` rename mirror `#foreign LIB "csym"`,
so `extern` is a true `#foreign` **superset** (Gate A→B): carried on
`extern_lib`/`extern_name`. The `#library` declaration + build-flag linking
mechanism stays a separate axis — `extern` *references* a lib, it doesn't fold
in `#library` itself. (Revises the original "library fully separate" decision 4.)
> **END-STATE INVARIANT (hard requirement).** After this stream, `foreign` appears
> **nowhere** in the live tree — not the `#foreign` surface, and **not** internal
@@ -29,8 +33,9 @@ g : Type extern ["csym"]; // extern g
`extern`-named representations only — do **not** reuse or extend
`ForeignExpr`/`foreign_expr`/`VarDecl.is_foreign`. Carry extern/export on a new
`FnDecl.extern_export` modifier with a `;`/`{…}` body (so there is **no** `*_expr`
node for it); add `VarDecl.is_extern`/`extern_name`. The IR is already extern-named
(`Function.is_extern`, `Builder.declareExtern`).
node for it) + `FnDecl.extern_lib`/`extern_name`; add `VarDecl.is_extern`/
`extern_lib`/`extern_name`. The IR is already extern-named (`Function.is_extern`,
`Builder.declareExtern`).
**Key finding (scopes Part A):** the IR + LLVM emit **already support everything**
`Function.linkage` (`.external/.internal/.private`), `is_extern`, `call_conv`, and a
@@ -147,7 +152,11 @@ per-file/subsystem commits — not one sweep.
## Open decisions
*Part A (ratified — recommendations stand):* 1. bare keywords (not `#extern`).
2. aggregate position postfix (`#objc_class(…) extern`, like `struct #compiler`).
3. `extern ⇒ callconv(.c)`. 4. library separate.
3. `extern ⇒ callconv(.c)`. 4. **REVISED** (user, 2026-06-14): `extern` carries an
optional `LIB`+`"csym"` axis (`extern_lib`/`extern_name`), mirroring `#foreign LIB
"csym"`, so it's a true `#foreign` superset (Gate A→B). The `#library` declaration +
build-flag linking mechanism stays separate — `extern` references a lib, doesn't
fold in `#library`. (Was: "library fully separate / not on `extern`".)
*Part B (confirm before Phase 9):* 5. runtime-class rename target — **`Runtime*Class*`**
(recommended; it's the object-model axis, not linkage) vs `Extern*Class*`.
6. historical carve-out — keep `issues/*.md` (+ design-doc prose) as provenance,