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

@@ -1949,9 +1949,22 @@ pub const Parser = struct {
// Optional calling convention: callconv(.c)
const call_conv = try self.parseOptionalCallConv();
// Body: block `{ ... }`, arrow `=> expr;`, #builtin, #compiler, or #foreign marker
// Optional postfix linkage modifier: `extern` (import) / `export` (define).
const extern_export = self.parseOptionalExternExport();
// Body: block `{ ... }`, arrow `=> expr;`, #builtin, #compiler, or #foreign marker.
// An `extern` import has NO body — just `;`. The extern_export modifier
// carries the linkage; we synthesize an empty block as the (non-optional)
// body placeholder, and lowering routes on the modifier rather than this
// block (no `*_expr` node — naming-constraint rule). `export` keeps its
// `{ … }` body and flows through the normal chain below.
var is_arrow = false;
const body = if (self.current.tag == .hash_builtin) blk: {
const body = if (extern_export == .extern_) blk: {
const semi_start = self.current.loc.start;
try self.expect(.semicolon);
const stmts = try self.allocator.alloc(*Node, 0);
break :blk try self.createNode(semi_start, .{ .block = .{ .stmts = stmts, .produces_value = false } });
} else if (self.current.tag == .hash_builtin) blk: {
const bi_start = self.current.loc.start;
self.advance();
try self.expect(.semicolon);
@@ -2011,6 +2024,7 @@ pub const Parser = struct {
.type_params = type_params,
.is_arrow = is_arrow,
.call_conv = call_conv,
.extern_export = extern_export,
.name_span = name_span,
.is_raw = name_is_raw,
} });
@@ -3609,7 +3623,9 @@ pub const Parser = struct {
// `(T1, T2) -> R` without a trailing body (`{`, `=>`, or a foreign/
// builtin marker) is a function-type literal, not a function def.
if (tag == .arrow) return self.hasFnBodyAfterArrow();
return tag == .l_brace or tag == .hash_builtin or tag == .hash_compiler or tag == .hash_foreign or tag == .fat_arrow or tag == .kw_callconv;
// `kw_extern`/`kw_export`: a postfix linkage modifier (e.g. `f :: () extern;`
// with no return type) marks a fn decl just like `callconv`.
return tag == .l_brace or tag == .hash_builtin or tag == .hash_compiler or tag == .hash_foreign or tag == .fat_arrow or tag == .kw_callconv or tag == .kw_extern or tag == .kw_export;
}
fn hasFnBodyAfterArrow(self: *Parser) bool {
@@ -3637,6 +3653,9 @@ pub const Parser = struct {
if (self.current.tag == .l_brace) return true;
if (self.current.tag == .hash_builtin or self.current.tag == .hash_compiler or self.current.tag == .hash_foreign) return true;
if (self.current.tag == .kw_callconv) return true;
// Postfix linkage modifier after the return type: `-> R extern;` /
// `-> R export { … }` (and `-> R callconv(.c) extern`). Marks a fn def.
if (self.current.tag == .kw_extern or self.current.tag == .kw_export) return true;
// Inside a `struct #compiler` block, a `(...) -> Ret;` ending
// with `;` after the return type is a `#compiler` method
// declaration (body implicit). Outside that context, the same