Files
ww/lib/CLAUDE.md
Hojun-Cho b6cf68f2b8 w6c+selfhost+lib: Hare-style variadic call sites
Param-decl `name: T...` (Tparam.variadic=1, type []T), call-site
gather of N args into a fresh `[N]T`, forward via `xs...`, full
selfhost mirror, and lib/fmt graduated to the Hare shape.

Frontend:
  - parse: `T...` after a param's type stamps Node.op=TK_ELLIPSIS
    and breaks out (variadic must be last).
  - check: resolve_type N_TFN / build_fn_type wrap the param type
    as []T and set tp->variadic. N_CALL accepts either a tail of
    args assignable to T (gather) or a single `xs...` spread of
    []T (forward); both bypass the "too many args" check on the
    variadic slot.
  - type: type_eq compares Tparam.variadic.

Cgen (cstage):
  - call site: when the callee has a variadic last param,
    materialise the tail args into a frame-resident `[N]T` via
    localoff, write a 24B slice descriptor (ptr,len,cap), and
    splice a synthesised N_IDENT into args[] so the downstream
    widen/eval/pop loops see one slice slot. Tagged-element types
    route each store through cg_widen_tagged_store. Forwarding
    skips gather: the N_SPREAD wrapper is replaced with its inner
    slice expression. Empty form writes {nil,0,0}. args[] / widen[]
    bump from 16 to 64 to accommodate Hare's mixed-arg printers.

Selfhost mirror:
  - lib/ww/parse: `T...` mark on N_PARAM.op.
  - cgen: varargseq counter on Cg; scanlocals reserves
    @vararg_d_N + @vararg_sl_N per variadic call (seq recorded on
    N_CALL.uval so cgcall picks the same names). cgcall does the
    same gather/forward and N_IDENT splice. cgfnparams treats
    variadic params as 24B slice slots via a synthesised TSLICE
    tnode. pushargsrev skips the tagged-widen detection for
    variadic params (effective type is []T, not tagged).
  - rhstargetname now recognises N_TRUE/N_FALSE/N_RUNELIT and
    typed N_INTLIT so the variant-tag lookup finds bool/rune/iN
    variants instead of falling through to "first non-str" (which
    misassigned tag 0 to bool in tagged unions like formattable).

lib/fmt graduated: print/println/fprint/fprintln/errorln/fatal
take `args: formattable...`. Bare `error` (no -ln) is skipped —
the leaf name collides with strconv's `type error = !(invalid |
overflow)` under the driver's flat namespace.

Tests: 5 new e2e rows (plain gather, zero-arg, tagged element,
forwarding, fmt.println end-to-end). lib/CLAUDE.md workaround
paragraph replaced with the Hare-shape description.
2026-05-13 08:56:01 +09:00

3.3 KiB

lib/ — Hare-shaped standard library.

Scope: every lib/* directory except lib/ww/, which is the compiler frontend port and has its own rules (see lib/ww/CLAUDE.md).

Names mirror Hare. Before adding a function, find its counterpart in ref/hare/<module>/ and copy the name — drop Hare's underscores per plan 9 style (trim_prefixtrimprefix, next_tokennexttoken). Don't invent. Don't shorten further. Don't reorder parameters.

Signatures mirror Hare too, modulo:

  • Tagged-union returns are spelled with the ww ! error tag where Hare uses !void / !T, and indices use the underlying length type (i32 today, since str.len: i32). Example: strconv.stoi64(s: str, b: strconv.base) (i64 | invalid | overflow) — same shape as Hare's. The base parameter is the named enum strconv.base (Hare uses enum uint; we pick enum i32 to match the index type).

  • Static-buffer str returns where Hare uses them. strconv.*tos returns a const str view into a module-level buffer that is overwritten on the next call to the same function. Callers that need the bytes to outlive the next call duplicate via strings.dup. Functions that genuinely allocate a fresh buffer (strings.dup, strings.concat) still return an owned str that callers free via os.free(r.ptr, r.len: u64).

  • Call-site variadic sugar matches Hare. fn f(args: T...) declares a Hare-style variadic; call sites either gather N args into a fresh []T (fmt.println(42, "hi", true)) or forward an existing slice with xs... (fprintln(fd, args...)). The bare T... form in tagged unions still means spread-flatten ((...inner | E)); the two uses don't overlap because T... only attaches to a param decl. lib/fmt is intentionally print-string-only — no printf-family.

  • (T | U) sum-typed parameters dispatch via match inside the callee. strings.byteindex(haystack: str, needle: (str | rune)), bytes.index(s: []u8, needle: (u8 | []u8)), and rbyteindex/ rindex follow Hare's shape directly. The rune-indexed strings.index (rune-wise position) isn't shipped yet — we don't have UTF-8 rune iteration in the language stack.

Don't ship a richer surface than Hare has. A documented subset is fine; an extension, rename, or convenience-wrapper is not — callers should not bake the current subset shape into themselves.

Modules with intentional divergence:

  • lib/os and lib/net stay below the Hare abstraction — they are syscall wrappers, not the high-level io::handle / net::socket API. Use them as the foundation that lib/io and the buffered layers build on.
  • lib/io keeps the ww-specific stream struct (vtable of fn pointers, no closures, no methods). The Hare io::handle family needs language features we don't have yet.
  • lib/bufio and lib/sort will be redesigned to Hare's scanner / cmpfunc shapes; the present minimal forms are placeholders until then.
  • lib/time and lib/math ship only what callers need today; they're not aiming for parity yet.

When the compiler can express a Hare signature that's still in the ww-specific shape (e.g., once a module-level *u8 is mutable, the strconv *tos family graduates to static-buffer returns), graduate the module in one go — replace the current shape with the Hare shape and fix the callers. Don't keep both around.