mem: implicit-context foundation + many compiler fixes
The session-long set of changes that lay the groundwork for the
Jai-literal implicit-Context-parameter refactor. Lots of accumulated
work; the new arrival is the implicit-ctx foundation (steps 1+2 of
the plan in current/CHECKPOINT-MEM.md):
Step 1 — `CAllocator :: struct {}` stateless allocator in
library/modules/allocators.sx, delegating directly to
libc_malloc/libc_free. `ConstantValue` in src/ir/inst.zig gains a
`func_ref: FuncId` leaf so nested aggregates can carry function
pointers (the inline Allocator value's fn-ptr fields). Switch
sites updated in emit_llvm.zig, print.zig, interp.zig.
Step 2 — `emitDefaultContextGlobal` in src/ir/lower.zig synthesises
a static `__sx_default_context` global with a nested-aggregate
init_val pointing at the CAllocator → Allocator thunks. The
second-pass `initVtableGlobals` in emit_llvm.zig is generalised
to handle `.aggregate` init_vals (re-emits after func_map is
populated so func_ref leaves resolve to real symbols).
Also folded in from earlier work this session:
- Phase 1.1: `xx value` heap-copy in `buildProtocolValue` routes
through `context.allocator` via the new `allocViaContext` helper.
- interp.zig: `marshalForeignArg` double-offset bug fixed —
`heapSlice` already adds `hp.offset` to the slice ptr, so the
extra `+ hp.offset` was scribbling memcpy/memset into adjacent
heap state, corrupting `heap.items[0]`. Symptom: `build_format`
at comptime produced zero bytes, all `print` calls failed.
- Lazy lowering: `lazyLowerFunction` now declares foreign-body
functions as extern stubs in the local (comptime) module so
cross-module foreign calls resolve.
- Allocator API: all stdlib allocators on one-line `init() -> *T`
(CAllocator/GPA: libc-backed; Arena/TrackingAllocator: parent-
backed; BufAlloc: embeds state at head of user buffer).
- issues 0038 (transitive #import), 0039 (chess + stdlib migration
fallout), 0040 (generic struct method dot-dispatch), 0041
(pointer types as type-arg), 0042 (alias name resolution) — all
fixed; regression tests in examples/.
- Diagnostic: `emitError` now embeds the lowering's
`current_source_file` and enclosing function in the literal
message; SX_TRACE_UNRESOLVED=1 dumps a Zig stack trace at the
emit site so misattributed spans can't hide where the failure
is.
- tools/verify-step.sh (all-platforms gate) and tools/scratch.sh
(interp/codegen parity tester) added.
Test suite: 152 example tests pass; chess builds + screenshots on
macOS / iOS sim / Android.
This commit is contained in:
@@ -2240,6 +2240,10 @@ pub const Parser = struct {
|
||||
if (self.isLambda()) {
|
||||
return self.parseLambda();
|
||||
}
|
||||
// Function-type literal: (T1, T2) -> R (no body — isLambda would have caught a body)
|
||||
if (self.isFunctionTypeExprAtLParen()) {
|
||||
return try self.parseTypeExpr();
|
||||
}
|
||||
self.advance(); // skip '('
|
||||
|
||||
// Check for named tuple: (name: expr, ...)
|
||||
@@ -2312,8 +2316,7 @@ pub const Parser = struct {
|
||||
null;
|
||||
return try self.createNode(start, .{ .return_stmt = .{ .value = value } });
|
||||
},
|
||||
.l_bracket => {
|
||||
// Type expression in expression position: []T.[...] or [N]T.[...]
|
||||
.l_bracket, .star, .question => {
|
||||
return try self.parseTypeExpr();
|
||||
},
|
||||
.l_brace => {
|
||||
@@ -2728,6 +2731,32 @@ pub const Parser = struct {
|
||||
return self.current.tag;
|
||||
}
|
||||
|
||||
/// Returns true when the current `(` opens a function-type literal `(T1, T2) -> R`
|
||||
/// rather than a tuple/grouping/lambda. Only meaningful after `isLambda` has
|
||||
/// returned false — at that point a trailing `->` after the matching `)` can
|
||||
/// only be a function type, since any body (`=>` or `{`) would have made it
|
||||
/// a lambda.
|
||||
fn isFunctionTypeExprAtLParen(self: *Parser) bool {
|
||||
const saved_lexer = self.lexer;
|
||||
const saved_current = self.current;
|
||||
const saved_prev_end = self.prev_end;
|
||||
defer {
|
||||
self.lexer = saved_lexer;
|
||||
self.current = saved_current;
|
||||
self.prev_end = saved_prev_end;
|
||||
}
|
||||
self.advance(); // skip '('
|
||||
var depth: u32 = 1;
|
||||
while (depth > 0 and self.current.tag != .eof) {
|
||||
if (self.current.tag == .l_paren) depth += 1;
|
||||
if (self.current.tag == .r_paren) depth -= 1;
|
||||
if (depth > 0) self.advance();
|
||||
}
|
||||
if (self.current.tag != .r_paren) return false;
|
||||
self.advance(); // skip ')'
|
||||
return self.current.tag == .arrow;
|
||||
}
|
||||
|
||||
fn isLambda(self: *Parser) bool {
|
||||
const saved_lexer = self.lexer;
|
||||
const saved_current = self.current;
|
||||
@@ -2845,7 +2874,55 @@ pub const Parser = struct {
|
||||
// ends with `;` directly after the param list — recognise it as a
|
||||
// function def (not a constant) so it goes through parseFnDecl.
|
||||
if (self.struct_default_compiler and tag == .semicolon) return true;
|
||||
return tag == .l_brace or tag == .arrow or tag == .hash_builtin or tag == .hash_compiler or tag == .hash_foreign or tag == .fat_arrow or tag == .kw_callconv;
|
||||
// `(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;
|
||||
}
|
||||
|
||||
fn hasFnBodyAfterArrow(self: *Parser) bool {
|
||||
const saved_lexer = self.lexer;
|
||||
const saved_current = self.current;
|
||||
const saved_prev_end = self.prev_end;
|
||||
defer {
|
||||
self.lexer = saved_lexer;
|
||||
self.current = saved_current;
|
||||
self.prev_end = saved_prev_end;
|
||||
}
|
||||
self.advance(); // skip '('
|
||||
var depth: u32 = 1;
|
||||
while (depth > 0 and self.current.tag != .eof) {
|
||||
if (self.current.tag == .l_paren) depth += 1;
|
||||
if (self.current.tag == .r_paren) depth -= 1;
|
||||
if (depth > 0) self.advance();
|
||||
}
|
||||
if (self.current.tag != .r_paren) return false;
|
||||
self.advance(); // skip ')'
|
||||
if (self.current.tag != .arrow) return false;
|
||||
self.advance(); // skip '->'
|
||||
while (self.current.tag != .eof) {
|
||||
if (self.current.tag == .fat_arrow) return true;
|
||||
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;
|
||||
// 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
|
||||
// shape is a function-type alias (no body) and falls through to
|
||||
// const-decl parsing.
|
||||
if (self.struct_default_compiler and self.current.tag == .semicolon) return true;
|
||||
if (self.current.tag == .identifier or self.current.tag.isTypeKeyword() or
|
||||
self.current.tag == .dot or self.current.tag == .dollar or
|
||||
self.current.tag == .l_bracket or self.current.tag == .r_bracket or
|
||||
self.current.tag == .l_paren or self.current.tag == .r_paren or
|
||||
self.current.tag == .comma or self.current.tag == .int_literal or
|
||||
self.current.tag == .star or self.current.tag == .question or
|
||||
self.current.tag == .colon or self.current.tag == .arrow)
|
||||
{
|
||||
self.advance();
|
||||
} else break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
fn parseOptionalCallConv(self: *Parser) anyerror!ast.CallingConvention {
|
||||
|
||||
Reference in New Issue
Block a user