wcc: tagged-union foundations (never, void, flatten, exhaust)

- `never` bottom type: TY_NEVER, assignable to anything; size 0.
- Type-set normalization for N_TTAGGED in resolve_type:
  - flatten nested anonymous (A|B)|C → (A|B|C); named aliases stay
    nominal (not flattened through)
  - dedup duplicates (NAMED pointer-id; others structural)
  - drop `never` variants
  - collapse single-element set: (T|never) → T, (T|T) → T
- Match exhaustiveness: error when a variant is unhandled and no
  default arm covers it. Multi-pattern `case T1 | T2 =>` counts
  each alt.
- (T | void) optionals: bare `return;` from a tagged-union-returning
  fn emits the void variant's tag (payload undefined; void size 0).

selfhost mirrored: TY_NEVER constant + tynever in tctx + seedprim
entry; voidvariantindex helper; cgreturn bare-return handling.
This commit is contained in:
2026-05-12 01:31:35 +09:00
parent 1ac1d985f6
commit fa070b6d07
11 changed files with 260 additions and 33 deletions

View File

@@ -2691,7 +2691,24 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
*
* Tagged-return ABI: AX=tag, DX=value0[, CX=value1]. CX is
* only meaningful when the union has a >8B variant (e.g.
* str, where ptr→DX and len→CX). */
* str, where ptr→DX and len→CX).
*
* Bare `return;` from a tagged-union-returning function: this
* is producing the void variant. Emit its tag; the payload is
* undefined (void has size 0). */
if (n->lhs == NULL && cg_ret_type) {
Type *rt = cg_ret_type;
if (rt->kind == TY_NAMED) rt = rt->under;
if (rt && rt->kind == TY_TAGGED) {
int tag = cg_tag_for_variant(rt, ty_void);
if (tag < 0) tag = 0;
ins2(c, A_MOVQ, aimm(tag), areg(D_AX));
ins2(c, A_MOVQ, areg(D_BP), areg(D_SP));
ins1(c, A_POPQ, areg(D_BP));
ins0(c, A_RET);
break;
}
}
if (n->lhs && cg_ret_type) {
Type *rt = cg_ret_type;
if (rt->kind == TY_NAMED) rt = rt->under;