Commit Graph

153 Commits

Author SHA1 Message Date
ea599150cd astgen: implement error set declarations
Port errorSetDecl from upstream AstGen.zig:5905-5955. Replaces the
SET_ERROR placeholder at the ERROR_SET_DECL case. Loops tokens between
lbrace and rbrace, collecting identifier strings into the ErrorSetDecl
payload.

Also add error_set_decl to the test comparison functions.

Tests added: empty error set, error set with members.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 20:35:58 +00:00
906c271284 astgen: implement struct field emission in structDeclInner
Port WipMembers, field processing loop, nodeImpliesMoreThanOnePossibleValue,
and nodeImpliesComptimeOnly from upstream AstGen.zig. Struct fields are now
properly emitted with type expressions, default values, alignment, and
comptime annotations.

Also fix structDeclInner to add the reserved instruction to the GenZir
body (matching upstream gz.reserveInstructionIndex behavior) and use
AST_NODE_OFFSET_NONE for break_inline src_node in field bodies.

Tests added: single field, multiple fields, field with default, field
with alignment, comptime field.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 20:33:21 +00:00
fc8f27ebdd astgen: enable corpus test for test_all.zig
test_all.zig is 5 lines of @import statements and already produces
matching ZIR. Enable it as a standalone corpus test while keeping
the full corpus test skipped.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 20:23:55 +00:00
710686de5c more astgen 2026-02-12 21:48:39 +02:00
897c464f8a astgen: fix continue and for loop scope handling
- continue: emit check_comptime_control_flow and
  restore_err_ret_index_unconditional (matching AstGen.zig:2328-2334)
- forExpr: set loop_scope.continue_block = cond_block
  (matching AstGen.zig:6974), allowing continue inside for loops
  to target the correct scope

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 18:18:31 +00:00
79b19d4aa4 astgen: fix corpus mismatches (gap 20→-17)
- endsWithNoReturn: add missing noreturn tags (RET_ERR_VALUE,
  RET_LOAD, COMPILE_ERROR, UNREACHABLE, TRAP,
  CHECK_COMPTIME_CONTROL_FLOW, SWITCH_CONTINUE)
- defer body: emit dbg_node + ensure_result (unusedResultExpr pattern)
- unwrap_optional: add emitDbgStmt before OPTIONAL_PAYLOAD_SAFE
- slice: add emitDbgStmt to SLICE_OPEN, SLICE, SLICE_SENTINEL
- switchExpr: remove erroneous save_err_ret_index (only in ErrUnion variant)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 17:58:52 +00:00
c525da4553 astgen: typeCast DBG_STMT, builtinEvalToError, memset/memcpy fixes
- Add emitDbgStmt and result type from RL to typeCast builtins
  (@intCast, @truncate, @ptrCast, @enumFromInt, @bitCast)
- Pass ResultLoc to builtinCall for result type access
- Fix @memset: upstream derives elem_ty via typeof+indexable_ptr_elem_type
  and evaluates value with coerced_ty RL
- Fix @memcpy/@memset to return void_value (not instruction ref)
- Add builtinEvalToError: per-builtin eval_to_error lookup instead of
  always returning MAYBE for all builtins
- Fix nodeMayAppendToErrorTrace: pass loop var 'n' to nodeMayEvalToError
  instead of original 'node' parameter

Corpus: ref=4177 got=4160, mismatch at inst[557], gap=17

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 15:09:25 +00:00
0d9afc0ae6 astgen: add genDefers, ret_err_value fast path, fix scope chain
- Add genDefers() with DEFER_NORMAL_ONLY/DEFER_BOTH_SANS_ERR modes
- Add countDefers() for checking defer types in scope chain
- Add genDefers calls to breakExpr, continueExpr, retExpr, tryExpr
- Add fn_block tracking to AstGenCtx (set in fnDecl/testDecl)
- Add return error.Foo fast path using ret_err_value instruction
- Fix fullBodyExpr scope: pass &body_gz.base instead of params_scope
- Fix blockExprStmts: guard genDefers with noreturn_stmt check
- Fix retExpr MAYBE path: correct dbg_stmt/restore ordering
- Save/restore fn_block in containerDecl (set NULL for nested structs)
- addEnsureResult now returns bool indicating noreturn

First ZIR tag mismatch moved from inst[211] to inst[428].

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 14:23:19 +00:00
5cffc20ef3 remove obsolete functions/decls 2026-02-12 16:13:56 +02:00
78298a6bb0 stricter test suite 2026-02-12 15:42:38 +02:00
4fc156d637 astgen: RL threading, labeled blocks, comptime block payload
Port several AstGen.zig patterns to C:

- Thread ResultLoc through fullBodyExpr, ifExpr, switchExpr, callExpr,
  calleeExpr (for proper type coercion and decl_literal handling)
- Add rlBr() and breakResultInfo() helpers mirroring upstream ri.br()
  and setBreakResultInfo
- Implement labeled blocks with label on GenZir (matching upstream),
  restoreErrRetIndex before break, and break_result_info
- Fix breakExpr to emit restoreErrRetIndex and use break_result_info
  for value/void breaks (AstGen.zig:2150-2237)
- Add setBlockComptimeBody with comptime_reason field (was using
  setBlockBody which omitted the reason, causing wrong extra layout)
- Add comptime_reason parameter to comptimeExpr with correct reasons
  for type/array_sentinel/switch_item/comptime_keyword contexts
- Handle enum_literal in calleeExpr (decl_literal_no_coerce)
- Fix decl_literal rvalue wrapping for ty/coerced_ty result locs

All 5 corpus files now pass byte-by-byte ZIR comparison.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 13:24:27 +00:00
7b0dd08921 disable leak checker for now 2026-02-12 14:08:49 +02:00
2998dd1122 quicker zig build test 2026-02-12 14:06:15 +02:00
47c9f3e038 disable gcc_analyze for now 2026-02-12 13:52:53 +02:00
71f570ee3d lint per file
gives paralellism
2026-02-12 13:51:53 +02:00
0295bb4651 astgen: port Phases 4-5 (control flow, expressions, scope chain)
Port scope chain infrastructure, function parameters, local var_decl,
control flow (if/for/while/switch/orelse/catch/defer), labeled blocks,
break/continue, comparison/boolean/unary operators, array access,
field access rvalue, rvalue type coercion optimization, and many
builtins from upstream AstGen.zig. test_all.zig corpus passes;
4 remaining corpus files still have mismatches (WIP).

Also fix cppcheck/lint issues: safe realloc pattern, null checks,
const correctness, enable inline suppressions, comment out test
debug output for clean `zig build`.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 11:40:51 +00:00
d6d60fbebf valgrind no timeout 2026-02-12 09:29:33 +02:00
9a6341a23b test timeouts 2026-02-12 08:16:50 +02:00
5527ad61e6 astgen: port Phases 1-3 from upstream AstGen.zig
Replace fixed-size GenZir instruction array with shared dynamic scratch
array matching the upstream design. Add expression types: grouped_expression,
unreachable_literal, enum_literal, multiline_string_literal, return, call,
struct_init, try. Add @cImport/@cInclude support. Fix fn_decl src_node
to use the fn_decl node (not proto_node). Fix GenZir unstack ordering
so fn_block is unstacked before adding instructions to decl_block.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 00:42:01 +02:00
b12d338f4f use zig's x86 backend 2026-02-12 00:32:57 +02:00
b5880e3ce2 build: subtract avx512f when running under valgrind
Valgrind 3.26.0 cannot decode AVX-512 instructions. On AVX-512 capable
CPUs (e.g. Zen 4), Zig's standard library emits these instructions when
targeting native, causing immediate crashes. Subtract avx512f from the
CPU features when -Dvalgrind is set.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 00:20:12 +02:00
bf200f7ef9 Add structural AST consistency check to parser tests
Compare the C parser's AST against Zig's std.zig.Ast.parse() output in
every testParse call. This catches structural mismatches (tokens, nodes,
extra_data) without needing a separate corpus.

Also fix two C parser bugs found by the new check:
- Empty anonymous init `.{}` now uses struct_init_dot_two (not
  array_init_dot_two), matching the Zig parser.
- for-type-expr with single input and no else now emits for_simple
  (not for with extra_data), matching the Zig parser's parseFor.

Skip the check under valgrind since Zig's tokenizer uses AVX-512.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-11 23:58:18 +02:00
202733edbc astgen: implement ZIR generation for basic expressions and declarations
Mechanical translation of AstGen.zig into C. Implements:
- Container members: comptime, simple_var_decl, test_decl, fn_decl
- Expressions: number_literal, string_literal, identifier (with
  primitive types, integer types, and decl_val/decl_ref resolution),
  field_access (field_val/field_ptr), address_of, builtin_call
  (@import), array_type, array_init (with inferred [_] length),
  array_cat (++), ptr_type
- Statement types: assign with _ = expr discard pattern
- Test infrastructure: testDecl, addFunc, fullBodyExpr,
  blockExprStmts, emitDbgNode/emitDbgStmt, rvalueDiscard
- Support: GenZir sub-block instruction tracking, result location
  propagation (RL_NONE/RL_REF/RL_DISCARD), string dedup, import
  tracking, namespace decl table, lastToken, firstToken

1/5 corpus files pass (test_all.zig). Remaining 4 skip gracefully
via has_compile_errors when encountering unimplemented features.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-11 21:32:37 +00:00
280997f658 remove usage of __attribute__((__cleanup__(fn)))
problematic with tcc.
2026-02-11 22:45:54 +02:00
b5300c4d9b more instructions for agents 2026-02-11 20:05:33 +00:00
85e7abd0b7 Merge branch 'zig01' 2026-02-11 18:37:51 +00:00
08f46bb10b update AGENTS.md 2026-02-11 18:37:25 +00:00
5fb7a1ab9c Add astgen scaffolding with ZIR data structures and first passing test
Introduce zir.h/zir.c with ZIR instruction types (269 tags, 56 extended
opcodes, 8-byte Data union) ported from lib/std/zig/Zir.zig, and
astgen.h/astgen.c implementing the empty-container fast path that produces
correct ZIR for empty source files.

The test infrastructure in astgen_test.zig compares C astGen() output
field-by-field against Zig's std.zig.AstGen.generate() using tag-based
dispatch, avoiding raw byte comparison since Zig's Data union has no
guaranteed in-memory layout.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-11 18:37:07 +00:00
0a563abefa valgrind 2026-02-11 18:36:29 +00:00
a3e8198477 valgrind 2026-02-11 18:27:44 +00:00
d6e65fe565 update LICENSE again 2026-02-11 18:14:32 +00:00
ab77c21bcb Update README 2026-02-11 18:10:49 +00:00
9d15552f1c parser: align structural logic with upstream Parse.zig
Fix 11 divergences where parser.c differed from Parse.zig in logic or
structure, not justified by C vs Zig language differences:

- parseContainerMembers: set trailing=false after test decl, add
  field_state tracking (A1, A2)
- expectStatement: guard defer/errdefer behind allow_defer_var (A3)
- expectVarDeclExprStatement: wrap assignment in comptime node when
  comptime_token is set (A4)
- parseBlock: guard semicolon check with statements_len != 0 (A5)
- parseLabeledStatement: add parseSwitchExpr call (A6)
- parseWhileStatement: restructure with else_required and early
  returns to match upstream control flow (A7)
- parseForStatement: restructure with else_required/has_else and
  early returns to match upstream control flow (A8)
- parseFnProto: fail when return_type_expr is missing (A9)
- expectTopLevelDecl: track is_extern, reject extern fn body (A10)
- parsePrefixExpr: remove TOKEN_KEYWORD_AWAIT case (A11)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-11 18:10:26 +00:00
67706f86f3 more debugging tips 2026-02-11 14:54:18 +00:00
2929100c15 parser: reorder function definitions to match upstream Parse.zig
Reorder function definitions so they follow the same order as upstream
zig/lib/std/zig/Parse.zig, making cross-referencing easier. Move
OperInfo and NodeContainerField typedefs to the header section, and add
forward declarations for parseParamDeclList and operTable that are now
needed due to the new ordering.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-11 14:40:34 +00:00
57e033e4b3 parser: align function names with upstream Parse.zig
Rename assignOpTag to assignOpNode to match upstream. Extract inlined
code into separate functions to match upstream's structure:
expectTestDecl, expectIfStatement, expectParamDecl, parseSwitchProngList.
Add parseSingleAssignExpr for upstream API surface alignment.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-11 14:40:33 +00:00
237a05a2fc sort forward declarations by name 2026-02-11 14:40:33 +00:00
5bd533d40c parser: replace fprintf+longjmp with fail(), add forward declarations
Introduce a fail(p, "msg") inline function that stores the error message
in a buffer and longjmps, replacing ~52 fprintf(stderr,...)+longjmp pairs.
The error message is propagated through Ast.err_msg so callers can decide
whether/how to display it. Also add forward declarations for all static
functions and move PtrModifiers typedef to the type definitions section.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-11 14:40:33 +00:00
cd07751d13 parser: port upstream error detection, unskip all 14 tests
Mechanically port error handling patterns from upstream Parse.zig:
- &&/whitespace validation in binary operator parsing
- varargs state tracking in parameter lists
- invalid_bit_range check for slice types
- same-line doc comment detection in eatDocComments
- required for-loop payload validation
- error keyword requiring '.' for error values
- expected_semi_or_else checks in if/for/while statements
- labeled for/while/inline expressions in parsePrimaryExpr
- doc comment validation for test/comptime blocks
- EOF check in parseRoot
- comptime handling in else-branch context

All 381/381 tests pass with 0 skipped.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-11 14:40:32 +00:00
f3e70a0568 parser: skip 14 tests, replace assertToken assert with longjmp
Skip 14 tests that require unimplemented parser features:
- 5 testCanonical/testTransform (primitive type symbols, invalid bit
  range, doc comment validation, multiline string in blockless if)
- 9 testError/recovery (error detection for comptime, varargs,
  semicolons, brackets, whitespace, ampersand)

Replace assert() in assertToken with longjmp to prevent crashes on
malformed input during testError tests.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-11 14:40:32 +00:00
e5cbd806c4 parser: refactor expectVarDeclExprStatement to match upstream
Restructure expectVarDeclExprStatement to match upstream Parse.zig's
approach: check for '=' first, then handle var decl init vs expression
statement separately. This fixes parsing of var decls with container
types (e.g., `const x: struct {} = val`), where the '}' of the type
was incorrectly treated as a block-terminated expression.

Also make container member parsing strict (longjmp on unexpected tokens
instead of recovery), and add for/while/labeled-block handling in
parseTypeExpr for function return types.

376/381 tests pass.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-11 14:40:31 +00:00
fdefdc98c2 parser: sync tests with upstream, fix tokenizer and parser
Sync parser_test.zig test section with upstream, adding ~40 new tests
(testError, testCanonical, testTransform). Remove extra blank lines
between tests to match upstream formatting.

Fix tokenizer keyword lookup bug: getKeyword() returned TOKEN_INVALID
when input was longer than a keyword prefix (e.g., "orelse" matched
"or" prefix then bailed out instead of continuing to find "orelse").

Fix parser to handle if/for/while expressions in type position (e.g.,
function return types like `fn foo() if (cond) i32 else void`). Add
labeled block support in parsePrimaryTypeExpr. Replace assert for
chained comparison operators with longjmp error.

365/381 tests pass. Remaining 16 failures are parser limitations for
specific syntax patterns and error recovery.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-11 14:40:31 +00:00
f5f54fcbe8 parser: propagate errors via setjmp/longjmp instead of exit(1)
Replace 32 parse-error exit(1) calls with longjmp to allow callers to
detect and handle parse failures. The OOM exit(1) in
astNodeListEnsureCapacity is kept as-is.

Add has_error flag to Ast, wrap parseRoot() with setjmp in astParse(),
and update test infrastructure to use the C parser for testError tests.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-11 14:40:31 +00:00
bda3329eee parser: refactor to match upstream Parse.zig structure
Extract shared helpers and fix error handling to align with upstream:

- Replace 7 assert() calls that crash on valid input with fprintf+exit
- Extract parsePtrModifiers() and makePtrTypeNode() to deduplicate
  pointer modifier parsing from 4 inline copies into 1 shared function
- Extract parseBlockExpr() and parseWhileContinueExpr() helpers
- Move comptime wrapping into expectVarDeclExprStatement() via
  comptime_token parameter
- Extract finishAssignExpr(), parseSwitchItem(), parseSwitchProng()

Net effect: 3233 → 3106 lines. All 298+ parser tests pass.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-11 14:40:30 +00:00
7193385f94 parser: reorder tests, fix check_test_order.py for new file layout
Update check_test_order.py to handle header/footer split correctly
when infrastructure code is at both start and end of file.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-11 14:40:30 +00:00
174a275c76 parser: port canonicalize and cast builtin tests
Port tests:
- "canonicalize symbols (simple)"
- "canonicalize symbols (character escapes)"
- "canonicalize symbols (asm)"
- "canonicalize cast builtins"
- "do not canonicalize invalid cast builtins"

Update test bodies to match upstream:
- "comptime block in container"
- "comment after empty comment"
- "comment after params"
- "decimal float literals with underscore separators"
- "container doc comments"

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-11 14:40:29 +00:00
801dfc6c6e parser: update test bodies to match upstream verbatim
Update test content to match upstream exactly:
- "comptime block in container"
- "comment after empty comment"
- "comment after params"
- "decimal float literals with underscore separators"
- "container doc comments"

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-11 14:40:29 +00:00
7a414e8731 parser: update defer test body to match upstream
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-11 14:40:29 +00:00
02621037a2 parser: fix duplicate while test content, match upstream verbatim
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-11 14:40:28 +00:00
83da26c183 parser: update test bodies to match upstream verbatim
Update test content to match upstream:
- "arrays" (full upstream test content)
- "blocks" (add labeled block and blk: variants)
- "comptime" (full upstream test with comptime var, expressions)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-11 14:40:28 +00:00