A.6.2.0b worker hit a real shared-`.next`-aliasing bug and stopped
per rule 7. Wwstage's N_TTUPLE chained element type ASTs via the
nodes' own `.next` field. `exprtype` routinely returns shared
nodes (sym.decl.lhs, struct field's `.lhs`, another N_TTUPLE's
`.list` element). Naive chain construction in the checker
corrupts source ASTs.
Introduce N_TPARAM = 67 as a chain wrapper for N_TTUPLE.list:
- `.lhs` holds the (possibly-shared) element type AST.
- `.next` chains within the parent N_TTUPLE.
- Other fields unused; never appears outside N_TTUPLE.list.
Mirrors cstage's Tparam at cmd/wcc/check.c:1437-1451. Cstage
keeps it at the Type layer; wwstage has no separate type layer
for tuple chains so the wrapper sits at the AST. Hare's design
intent at ref/hare/hare/ast/type.ha:117 uses `[]*_type` slice-of-
pointer — same principle, slice-flavored.
Migrations:
- lib/ww/ast.ww: kind + nkname + pr() unwrap (transparent for
the 990 -a astprint byte-diff).
- lib/ww/parse/parse.ww: parsetype N_TTUPLE construction wraps
each element in N_TPARAM (sole construction site).
- selfhost/cmd/wcc/check.ww: 4 readers (astalign, astsize,
tinfofornode TY_TUPLE, exprtype N_DOT-tuple-positional). The
last change retires the latent A.6.1.5b shared-`p` return.
- selfhost/cmd/wcc/cgenutil.ww: slotsize TY_TUPLE arm.
- selfhost/cmd/wcc/cgenexpr.ww: cgdot tuple-positional
(size/load op + str-check).
- selfhost/cmd/wcc/cgenstmt.ww: cglet TTUPLE init, cgmlet
call-return walk, cgforrange elem-size + bind-walk.
Out of scope: N_TFN params, N_TTAGGED variants, N_TSTRUCT fields.
N_TFIELD already wraps struct fields; N_TFN/N_TTAGGED aren't
currently chain-mutated by checker synthesis. If they ever are,
the same pattern applies.
Unblocks A.6.2.0b stamp on a clean foundation. Retires task #16.
Verified 132/132 incl. 990 AST byte-diff (astprint unwrap) + 995
self-rebuild byte-identity.
2601 lines
93 KiB
Plaintext
2601 lines
93 KiB
Plaintext
// selfhost/cmd/wcc/check.ww — minimal port of cmd/wcc/check.c.
|
|
//
|
|
// Status: name-resolution + primitive-type seeding only. Full type
|
|
// inference, conversion rules, tagged-union dispatch typing, return-
|
|
// type checking, etc. all live in cmd/wcc/check.c (937 lines) and
|
|
// will land here in subsequent commits.
|
|
//
|
|
// What this version does:
|
|
// 1. Creates a top scope and seeds it with primitive type names so
|
|
// `i32`, `str`, `*u8` etc. resolve.
|
|
// 2. Walks the file's top-level decls (use/def/type/fn/let) and
|
|
// installs Sym entries for each.
|
|
// 3. Recursively walks fn bodies; for every nkind.N_IDENT used as an
|
|
// expression or as a type name, looks it up and counts the
|
|
// resolved vs. unresolved.
|
|
// 4. Returns a summary the caller (wwdump -r) prints; the test
|
|
// asserts unresolved == 0 on every selfhost fixture, which is
|
|
// the floor signal that the frontend can name-resolve real ww.
|
|
|
|
package wcc;
|
|
|
|
import os;
|
|
import tok;
|
|
|
|
type checker = struct {
|
|
tc: *tctx,
|
|
top: *scope,
|
|
cur: *scope,
|
|
nresolved: i32,
|
|
nunresolved: i32,
|
|
errs: i32,
|
|
verbose: i32, // when non-zero, log each unresolved name
|
|
fnret: *node, // enclosing fn's return type AST (for `?`)
|
|
curmod: str, // importing-module bareword for the decl
|
|
// currently being walked; "" for primary
|
|
// compilation unit. Drives same-module
|
|
// preference in bare-leaf lookups.
|
|
file: *node, // N_FILE root; used by checkmoduleshadow
|
|
// to consult the declaring source's own
|
|
// `use` directives.
|
|
};
|
|
|
|
// seedprimitives — install the built-in type names so `i32`, `str`,
|
|
// etc. can be looked up like ordinary symbols.
|
|
fn seedprimitives(c: *checker) void = {
|
|
scopedefine(c.top, "void", skind.SK_TYPE, c.tc.tyvoid, nil);
|
|
scopedefine(c.top, "bool", skind.SK_TYPE, c.tc.tybool, nil);
|
|
scopedefine(c.top, "rune", skind.SK_TYPE, c.tc.tyrune, nil);
|
|
scopedefine(c.top, "i8", skind.SK_TYPE, c.tc.tyi8, nil);
|
|
scopedefine(c.top, "i16", skind.SK_TYPE, c.tc.tyi16, nil);
|
|
scopedefine(c.top, "i32", skind.SK_TYPE, c.tc.tyi32, nil);
|
|
scopedefine(c.top, "i64", skind.SK_TYPE, c.tc.tyi64, nil);
|
|
scopedefine(c.top, "u8", skind.SK_TYPE, c.tc.tyu8, nil);
|
|
scopedefine(c.top, "u16", skind.SK_TYPE, c.tc.tyu16, nil);
|
|
scopedefine(c.top, "u32", skind.SK_TYPE, c.tc.tyu32, nil);
|
|
scopedefine(c.top, "u64", skind.SK_TYPE, c.tc.tyu64, nil);
|
|
scopedefine(c.top, "int", skind.SK_TYPE, c.tc.tyint, nil);
|
|
scopedefine(c.top, "uint", skind.SK_TYPE, c.tc.tyuint, nil);
|
|
scopedefine(c.top, "uintptr", skind.SK_TYPE, c.tc.tyuintptr, nil);
|
|
scopedefine(c.top, "f32", skind.SK_TYPE, c.tc.tyf32, nil);
|
|
scopedefine(c.top, "f64", skind.SK_TYPE, c.tc.tyf64, nil);
|
|
scopedefine(c.top, "str", skind.SK_TYPE, c.tc.tystr, nil);
|
|
scopedefine(c.top, "never", skind.SK_TYPE, c.tc.tynever, nil);
|
|
// #29: predeclare `type nomem = !void;` so user code needn't
|
|
// declare it locally. Synthesize an nkind.N_TYPEDECL whose lhs is
|
|
// nkind.N_TBANG{nkind.N_TNAME("void")} so varianterr and other
|
|
// iserror-aware paths treat `nomem` identically to a user-written
|
|
// alias. Mirrors cmd/wcc/check.c lookup_builtin returning
|
|
// ty_nomem (NAMED, under=ty_void, iserror=1). Note: cgen owns a
|
|
// separate alias chain — see collectaliases in cgen.ww for the
|
|
// companion seed.
|
|
let empty: str;
|
|
let tnvoid: *node = newnode(nkind.N_TNAME, empty, 0, 0);
|
|
tnvoid.str = "void";
|
|
let bang: *node = newnode(nkind.N_TBANG, empty, 0, 0);
|
|
bang.lhs = tnvoid;
|
|
let nomemdecl: *node = newnode(nkind.N_TYPEDECL, empty, 0, 0);
|
|
nomemdecl.str = "nomem";
|
|
nomemdecl.lhs = bang;
|
|
scopedefine(c.top, "nomem", skind.SK_TYPE, nil, nomemdecl);
|
|
// `nil`, `true`, `false` are keywords — handled at the lex/parser
|
|
// level, no symbol needed.
|
|
// `len`, `alloc`, `free`, `append` are pseudo-builtins; scopedefine
|
|
// them so their use sites resolve. The actual semantics live in cgen.
|
|
scopedefine(c.top, "len", skind.SK_FN, nil, nil);
|
|
scopedefine(c.top, "alloc", skind.SK_FN, nil, nil);
|
|
scopedefine(c.top, "free", skind.SK_FN, nil, nil);
|
|
scopedefine(c.top, "append", skind.SK_FN, nil, nil);
|
|
// #42: typed builtins folded to integer literals at check time —
|
|
// `size(T)` / `align(T)` (arg is a type-expression planted by the
|
|
// parser at lib/ww/parse/expr.ww:254-267) and `offset(e.f)` (arg is
|
|
// an N_DOT). exprtype intercepts these and rewrites the N_CALL to
|
|
// N_INTLIT so cgen never sees an unresolved size/align/offset symbol.
|
|
// Mirrors cmd/wcc/check.c:907-955.
|
|
scopedefine(c.top, "size", skind.SK_FN, nil, nil);
|
|
scopedefine(c.top, "align", skind.SK_FN, nil, nil);
|
|
scopedefine(c.top, "offset", skind.SK_FN, nil, nil);
|
|
};
|
|
|
|
// declmod — module-tag stamp for a top-level decl.
|
|
//
|
|
// The driver concatenates imported sources before the primary file and
|
|
// emits `// MODULE: foo` directives the lexer pins onto each decl's
|
|
// `module` field. We treat a decl as "imported" iff its module
|
|
// directive matches some `use IDENT;` bareword in this compilation
|
|
// unit. Primary-file decls return "" so they coexist (mod="") with
|
|
// imported decls of the same leaf name in scopelookupinmodule.
|
|
fn declmod(file: *node, d: *node) str = {
|
|
let empty: str;
|
|
if (d == nil) { return empty; };
|
|
if (d.nmod.len == 0) { return empty; };
|
|
if (file == nil) { return empty; };
|
|
let u: *node = file.list;
|
|
for (u != nil) {
|
|
if (u.kind == nkind.N_USE) {
|
|
if (streq(u.str, d.nmod)) { return d.nmod; };
|
|
};
|
|
u = u.next;
|
|
};
|
|
return empty;
|
|
};
|
|
|
|
// srcimports — does the source file that contributed decl-module
|
|
// `modtag` carry `use <name>;`? Mirrors cstage's src_imports —
|
|
// `modtag.len == 0` means primary, matching declmod's empty-str
|
|
// return for primary-source decls.
|
|
fn srcimports(file: *node, modtag: str, name: str) bool = {
|
|
if (file == nil) { return false; };
|
|
if (name.len == 0) { return false; };
|
|
let u: *node = file.list;
|
|
for (u != nil) {
|
|
if (u.kind == nkind.N_USE) {
|
|
// Skip self-imports: lib/fmt/fmttest.ww carries
|
|
// `use fmt;` while its module tag is also "fmt".
|
|
// That directive doesn't introduce a foreign
|
|
// module bareword and lib/fmt's own
|
|
// `fn bsprintf(fmt: str, ...)` is not a shadow.
|
|
if (u.nmod.len > 0) {
|
|
if (streq(u.nmod, u.str)) {
|
|
u = u.next;
|
|
continue;
|
|
};
|
|
};
|
|
let um: str = declmod(file, u);
|
|
let m: bool = false;
|
|
if (modtag.len == 0) {
|
|
if (um.len == 0) { m = true; };
|
|
} else { if (streq(um, modtag)) { m = true; }; };
|
|
if (m) {
|
|
if (streq(u.str, name)) { return true; };
|
|
};
|
|
};
|
|
u = u.next;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
// checkmoduleshadow — enforce "value names and module names are
|
|
// disjoint" at nested-scope binds. Mirrors cstage check_module_shadow
|
|
// (cmd/wcc/check.c). Fires for fn params / lets / forrange iters /
|
|
// mcase bindings whose name matches an in-scope `use foo;` import
|
|
// declared in the same source file. Top-level decls are exempt
|
|
// (their same-leaf-as-module pattern is the intentional coexistence
|
|
// shape — `use fnmatch; fn fnmatch(...)` etc.).
|
|
fn checkmoduleshadow(c: *checker, name: str, kindstr: str) void = {
|
|
if (name.len == 0) { return; };
|
|
if (c.cur == c.top) { return; };
|
|
let seen: bool = false;
|
|
let s: *scope = c.cur;
|
|
for (s != nil) {
|
|
let r: *sym = scopelookuplocal(s, name);
|
|
if (r != nil) {
|
|
if (r.skind == skind.SK_USE) {
|
|
seen = true;
|
|
s = nil;
|
|
};
|
|
};
|
|
if (s != nil) { s = s.parent; };
|
|
};
|
|
if (!seen) { return; };
|
|
if (!srcimports(c.file, c.curmod, name)) { return; };
|
|
os.write(2, kindstr.ptr, kindstr.len: u64);
|
|
os.write(2, " '".ptr, 2u64);
|
|
os.write(2, name.ptr, name.len: u64);
|
|
os.write(2, "' shadows imported module '".ptr, 27u64);
|
|
os.write(2, name.ptr, name.len: u64);
|
|
os.write(2, "'\n".ptr, 2u64);
|
|
c.errs += 1;
|
|
};
|
|
|
|
// installdecl — install the top-level decl's name into the top scope.
|
|
// We don't compute its type yet (that's the resolve pass) — just bind
|
|
// the name so forward references resolve.
|
|
//
|
|
// Architectural note: wwstage uses COEXISTENCE rather than the cstage
|
|
// promote-SK_USE-in-place approach in cmd/wcc/check.c. SK_USE and any
|
|
// same-leaf SK_TYPE/SK_FN/SK_DEF/SK_VAR live as separate entries in
|
|
// the same scope-bucket, distinguished by `sym.mod`. The dot-prefix
|
|
// lookup in resolvewalk + scopelookupinmodule's mod-filter already
|
|
// disambiguate `fnmatch.flag` against an `fn fnmatch(...)` of the same
|
|
// leaf — no `use_alias` flag needed. So the cstage L1722-class bug
|
|
// (promotion missing use_alias) is structurally non-reachable here.
|
|
// Don't port the use_alias flag from cstage without first re-reading
|
|
// the architecture: adding a field to `sym` changes its size and risks
|
|
// the wwstage cgen amalloc-undersize trap (rob-pike). #11 (wwstage
|
|
// checkfile pass) will reconsider this when wwstage grows a real check
|
|
// pass on the cgen path.
|
|
// TODO(#11): cstage check.c errors on duplicate top-level type/def/fn
|
|
// (see cmd/wcc/check.c L1800/L1839/L1860 "duplicate <kind>") and on
|
|
// duplicate top-level let (cmd/wcc/check.c L1880, "duplicate let %s")
|
|
// once #32 lands. Wwstage's installdecl just drops the second insert
|
|
// silently. Add `if (s == nil) err(...)` here once #11 wires checkfile
|
|
// into w6c_ww. Silent-accept matches the deferred-check design — see
|
|
// test/wcc/708 and test/wcc/696 for the same cstage-only neg-case
|
|
// precedent.
|
|
fn installdecl(c: *checker, file: *node, d: *node) void = {
|
|
if (d == nil) { return; };
|
|
let k: nkind = d.kind;
|
|
let nm: str = d.str;
|
|
let mod: str = declmod(file, d);
|
|
if (k == nkind.N_USE) { scopedefine(c.top, nm, skind.SK_USE, nil, d); return; };
|
|
if (k == nkind.N_DEF) { scopedefineinmodule(c.top, nm, mod, skind.SK_DEF, nil, d); return; };
|
|
if (k == nkind.N_TYPEDECL) { scopedefineinmodule(c.top, nm, mod, skind.SK_TYPE, nil, d); return; };
|
|
if (k == nkind.N_FNDECL) { scopedefineinmodule(c.top, nm, mod, skind.SK_FN, nil, d); return; };
|
|
if (k == nkind.N_LET) { scopedefineinmodule(c.top, nm, mod, skind.SK_VAR, nil, d); return; };
|
|
};
|
|
|
|
// resolvewalk — recursive AST walk that, for every nkind.N_IDENT and
|
|
// nkind.N_TNAME seen, looks up the name and bumps the resolved/unresolved
|
|
// counters. Local lets are installed in the current scope as soon as
|
|
// their init/type expressions have been walked (forward use of a let
|
|
// before its declaration would resolve to nothing — same semantics as
|
|
// the C checker's collect-then-resolve flow within a function).
|
|
// Also runs the typed checks (match exhaustiveness, ? subset) in
|
|
// the same pass — they need the same scope state.
|
|
fn resolvewalk(c: *checker, n: *node) void = {
|
|
if (n == nil) { return; };
|
|
let k: nkind = n.kind;
|
|
|
|
// Typed checks fire on the way down so the scrutinee/operand
|
|
// is examined before the arm bodies install new bindings.
|
|
if (k == nkind.N_MATCH) { checkmatchexhaust(c, n); };
|
|
if (k == nkind.N_TRYPROP) { checktryprop(c, n); };
|
|
if (k == nkind.N_TYPETEST) { checkisas(c, n); };
|
|
if (k == nkind.N_TYPEASSERT) { checkisas(c, n); };
|
|
if (k == nkind.N_LET) { checkletassign(c, n); };
|
|
if (k == nkind.N_RETURN) { checkretassign(c, n); };
|
|
|
|
// `use IDENT;` — name is a module label, not a free ident.
|
|
if (k == nkind.N_USE) { return; };
|
|
|
|
if (k == nkind.N_IDENT) {
|
|
let nm: str = n.str;
|
|
if (nm.len > 0) {
|
|
let s: *sym = scopelookupprefer(c.cur, c.curmod, nm);
|
|
if (s == nil) {
|
|
c.nunresolved += 1;
|
|
if (c.verbose != 0) {
|
|
os.write(2, " unresolved id: ".ptr, 17u64);
|
|
os.write(2, nm.ptr, nm.len: u64);
|
|
os.write(2, "\n".ptr, 1u64);
|
|
};
|
|
} else { c.nresolved += 1; };
|
|
};
|
|
};
|
|
|
|
if (k == nkind.N_TNAME) {
|
|
let nm: str = n.str;
|
|
if (nm.len > 0) {
|
|
let s: *sym = scopelookupprefer(c.cur, c.curmod, nm);
|
|
// `pkg.Type` — strip the last dot prefix and look up
|
|
// the leaf with a mod filter so same-leaf-name types
|
|
// from different imports (`bufio.stream` vs
|
|
// `io.stream`) disambiguate to the right one.
|
|
// Mirrors cmd/wcc/check.c resolve_typename.
|
|
if (s == nil) {
|
|
let dot: i32 = nm.len - 1;
|
|
for (dot >= 0) {
|
|
if (nm[dot] == 46u8) { break; };
|
|
dot -= 1;
|
|
};
|
|
if (dot > 0) {
|
|
let head: str;
|
|
head.ptr = nm.ptr;
|
|
head.len = dot;
|
|
let m: *sym = scopelookup(c.cur, head);
|
|
if (m != nil) {
|
|
let leaf: str;
|
|
leaf.ptr = nm.ptr + (dot + 1): u64;
|
|
leaf.len = nm.len - (dot + 1);
|
|
s = scopelookupinmodule(c.cur, head, leaf);
|
|
};
|
|
};
|
|
};
|
|
if (s == nil) {
|
|
c.nunresolved += 1;
|
|
if (c.verbose != 0) {
|
|
os.write(2, " unresolved tname: ".ptr, 20u64);
|
|
os.write(2, nm.ptr, nm.len: u64);
|
|
os.write(2, "\n".ptr, 1u64);
|
|
};
|
|
} else { c.nresolved += 1; };
|
|
};
|
|
};
|
|
|
|
// `for (let x .. slice) body` / `for (let (a, b) .. slice) body` —
|
|
// each binding name becomes a fresh local. Walk the slice expr first
|
|
// so its idents resolve before the bindings shadow anything, then
|
|
// install bindings and walk the body/else.
|
|
//
|
|
// TODO(#11): cstage check.c (post-#32) errors `binding '%s'
|
|
// redeclared in same scope` when the tuple-pattern lists the same
|
|
// name twice (`for (let (a, a) .. xs)`). Wwstage's resolvewalk has
|
|
// no per-block scope (see resolvefnbody's docstring) and is used
|
|
// only by wwdump_ww as a diagnostic, so silent-accept here avoids
|
|
// false-positives on legal cross-block shadow until #11 adds the
|
|
// scoping infrastructure.
|
|
if (k == nkind.N_FORRANGE) {
|
|
if (n.lhs != nil) { resolvewalk(c, n.lhs); };
|
|
if (n.list != nil) {
|
|
let m: *node = n.list;
|
|
for (m != nil) {
|
|
let bnm: str = m.str;
|
|
if (bnm.len > 0) {
|
|
checkmoduleshadow(c, bnm, "binding");
|
|
scopedefine(c.cur, bnm, skind.SK_VAR, nil, m);
|
|
};
|
|
m = m.next;
|
|
};
|
|
} else {
|
|
let bnm: str = n.str;
|
|
if (bnm.len > 0) {
|
|
checkmoduleshadow(c, bnm, "binding");
|
|
scopedefine(c.cur, bnm, skind.SK_VAR, nil, n);
|
|
};
|
|
};
|
|
if (n.body != nil) { resolvewalk(c, n.body); };
|
|
if (n.els != nil) { resolvewalk(c, n.els); };
|
|
return;
|
|
};
|
|
|
|
// `match (e) { case let v: T => stmt; ... }` — the binding `v`
|
|
// is declared by the case arm and visible inside its body. Push a
|
|
// fresh scope so `case let e: str` doesn't collide with an outer
|
|
// `let e: *T` (scopedefine drops same-scope dupes silently and
|
|
// would leave references to `e` resolving to the outer type).
|
|
// Mirrors cmd/wcc/check.c's newscope/saved-restore around cstmt.
|
|
if (k == nkind.N_MCASE) {
|
|
if (n.lhs != nil) { resolvewalk(c, n.lhs); };
|
|
let outer: *scope = c.cur;
|
|
c.cur = newscope(outer);
|
|
let nm: str = n.str;
|
|
if (nm.len > 0) {
|
|
checkmoduleshadow(c, nm, "binding");
|
|
scopedefine(c.cur, nm, skind.SK_VAR, nil, n);
|
|
};
|
|
if (n.body != nil) { resolvewalk(c, n.body); };
|
|
c.cur = outer;
|
|
return;
|
|
};
|
|
|
|
// #53: lexical block. Push a child scope so locals introduced by
|
|
// inner-block lets (and the `let` install at the tail of this fn) go
|
|
// out of scope at block exit. Without this, a deeply nested
|
|
// `let i: u64 = 0u64;` survived to shadow a same-named outer
|
|
// `let i: i32 = 1;` for the whole fn body, and exprtype handed
|
|
// stale primitive types to checkletassign — silent miscompile
|
|
// becomes a false-positive on the next driver (`wwdump_ww -r`
|
|
// flagged the u64→i32 pair in selfhost/cmd/ww/enumeratedir).
|
|
// Mirrors cstage cstmt N_BLOCK at cmd/wcc/check.c:1559-1566.
|
|
if (k == nkind.N_BLOCK) {
|
|
let outer: *scope = c.cur;
|
|
c.cur = newscope(outer);
|
|
let m: *node = n.list;
|
|
for (m != nil) {
|
|
resolvewalk(c, m);
|
|
m = m.next;
|
|
};
|
|
c.cur = outer;
|
|
return;
|
|
};
|
|
|
|
if (k == nkind.N_DOT) {
|
|
// Walk only the base; the .field name is a member, not a
|
|
// free identifier.
|
|
if (n.lhs != nil) { resolvewalk(c, n.lhs); };
|
|
// A.6.0: branch returns early; stamp here so the post-walk
|
|
// dispatch below sees N_DOT covered. exprtype N_DOT arm is
|
|
// added in A.6.1; for now this is a no-op nil return.
|
|
let _t: *node = exprtype(c, n, nil);
|
|
return;
|
|
};
|
|
|
|
if (k == nkind.N_FIELD) {
|
|
if (n.lhs != nil) { resolvewalk(c, n.lhs); };
|
|
return;
|
|
};
|
|
|
|
if (k == nkind.N_TFIELD) {
|
|
if (n.lhs != nil) { resolvewalk(c, n.lhs); };
|
|
return;
|
|
};
|
|
|
|
// Walk children (mirroring ast.ww's printer descent order).
|
|
if (n.attr != nil) { resolvewalk(c, n.attr); };
|
|
if (n.lhs != nil) { resolvewalk(c, n.lhs); };
|
|
if (n.rhs != nil) { resolvewalk(c, n.rhs); };
|
|
if (n.cond != nil) { resolvewalk(c, n.cond); };
|
|
if (n.body != nil) { resolvewalk(c, n.body); };
|
|
if (n.els != nil) { resolvewalk(c, n.els); };
|
|
if (n.list != nil) {
|
|
let m: *node = n.list;
|
|
for (m != nil) {
|
|
resolvewalk(c, m);
|
|
m = m.next;
|
|
};
|
|
};
|
|
|
|
// #61 audit §1.8 — A.2 population: stamp tinfo onto type-expression
|
|
// nodes once their children have been walked (sub-element TNAMEs
|
|
// are now in scope so resolvealias inside tinfofornode can follow
|
|
// user-defined aliases). Cgen's slotsize fast-path reads off
|
|
// n.type_; uncovered shapes fall through to the cstage-mirror
|
|
// walker until the next sub-commit graduates them.
|
|
if (k == nkind.N_TNAME || k == nkind.N_TPTR ||
|
|
k == nkind.N_TSLICE || k == nkind.N_TCHAN ||
|
|
k == nkind.N_TBANG || k == nkind.N_TARRAY ||
|
|
k == nkind.N_TFN || k == nkind.N_TSTRUCT ||
|
|
k == nkind.N_TTUPLE || k == nkind.N_TTAGGED ||
|
|
k == nkind.N_TENUM) {
|
|
if (n.type_ == nil) {
|
|
let ti: *tinfo = tinfofornode(c, n);
|
|
if (ti != nil) { n.type_ = ti: *void; };
|
|
};
|
|
};
|
|
|
|
// #42's size/align/offset fold trigger lived here pre-A.6.0; the
|
|
// A.6.0 end-of-fn general dispatch (below) now fires exprtype on
|
|
// every N_CALL — same context-free coverage, one dispatch site.
|
|
|
|
// After walking children: a local `let X: T = init;` registers
|
|
// `X` so subsequent statements can resolve it. Top-level lets
|
|
// are installed in installdecl, so this duplicate install at
|
|
// the file scope just no-ops (scopedefine returns nil on dup).
|
|
//
|
|
// Cross-block `let a; { let a; };` no longer trips dup-silence
|
|
// since #53 added N_BLOCK push/pop above — the inner `a` lands in
|
|
// the inner block's scope. Same-scope dup `let a=1; let a=2;`
|
|
// still silent-accepts here; promoting that to an error stays
|
|
// queued behind #11 (test/wcc/708 + test/wcc/696 are the cstage-
|
|
// only neg-case precedent).
|
|
if (k == nkind.N_LET) {
|
|
let nm: str = n.str;
|
|
if (nm.len > 0) {
|
|
checkmoduleshadow(c, nm, "let");
|
|
scopedefine(c.cur, nm, skind.SK_VAR, nil, n);
|
|
};
|
|
};
|
|
|
|
// A.6.0: post-order dispatch of exprtype on every expression-yielding
|
|
// node kind so n.type_ stamps fire universally — not only when reached
|
|
// through checkletassign / checkretassign / checktryprop / the size-
|
|
// align-offset fold. Mirrors cstage cmd/wcc/check.c cstmt's recursive
|
|
// cexpr (cmd/wcc/check.c:1567 N_EXPRSTMT, :1570 N_RETURN, :1584 N_IF
|
|
// cond, etc.). Plumbing-only: stamps fire from existing exprtype kind
|
|
// arms (literals + idents); per-kind stamp coverage lands in A.6.1.
|
|
// Stamps are tinfocache-backed idempotent so multi-walk via let /
|
|
// return / try entry points is safe. N_DOT is dispatched in its own
|
|
// early-return branch above; not listed here. N_LET / N_RETURN /
|
|
// N_EXPRSTMT / N_IF / N_FOR / N_FORRANGE / N_BLOCK / N_MATCH-as-stmt
|
|
// are not value-typed nodes; their expression children get stamped on
|
|
// the recursive descent into them. Type-expression kinds (N_T*) are
|
|
// covered separately by the tinfofornode block above.
|
|
if (k == nkind.N_INTLIT || k == nkind.N_FLOATLIT ||
|
|
k == nkind.N_STRLIT || k == nkind.N_RUNELIT ||
|
|
k == nkind.N_TRUE || k == nkind.N_FALSE ||
|
|
k == nkind.N_NIL || k == nkind.N_VOIDLIT ||
|
|
k == nkind.N_IDENT || k == nkind.N_BIN ||
|
|
k == nkind.N_UN || k == nkind.N_CALL ||
|
|
k == nkind.N_INDEX || k == nkind.N_CAST ||
|
|
k == nkind.N_STRUCTLIT || k == nkind.N_ARRLIT ||
|
|
k == nkind.N_ALLOC || k == nkind.N_RECV ||
|
|
k == nkind.N_SLICE || k == nkind.N_SPREAD ||
|
|
k == nkind.N_TUPLE || k == nkind.N_TRYPROP ||
|
|
k == nkind.N_TRYUNW || k == nkind.N_TYPETEST ||
|
|
k == nkind.N_TYPEASSERT || k == nkind.N_YIELD ||
|
|
k == nkind.N_MATCH) {
|
|
let _t: *node = exprtype(c, n, nil);
|
|
};
|
|
};
|
|
|
|
// ---- type-level helpers (AST-level, no resolved tinfo) --------------
|
|
//
|
|
// The selfhost check operates on AST type expressions rather than
|
|
// resolved Type structs. These helpers mirror what cmd/wcc/check.c
|
|
// does with tinfo, but only on the subset of cases this checker
|
|
// needs to enforce: tagged-union exhaustiveness, ? subset
|
|
// propagation, and !-flag semantics.
|
|
|
|
// unwrapbang — strip an nkind.N_TBANG wrapper; leaves other nodes alone.
|
|
fn unwrapbang(n: *node) *node = {
|
|
if (n == nil) { return nil; };
|
|
if (n.kind == nkind.N_TBANG) { return n.lhs; };
|
|
return n;
|
|
};
|
|
|
|
// resolvealias — if n is an nkind.N_TNAME pointing at a typedecl, return
|
|
// the typedecl's body (possibly recursively). Pass-through for any
|
|
// other node. The chain stops once we hit a non-nkind.N_TNAME node or a
|
|
// name we can't resolve.
|
|
fn resolvealias(c: *checker, n: *node) *node = {
|
|
let cur: *node = n;
|
|
for (cur != nil) {
|
|
if (cur.kind != nkind.N_TNAME) { return cur; };
|
|
let nm: str = cur.str;
|
|
// #51: pkg.alias type refs land here as a single TNAME whose
|
|
// str is the joined form (lib/ww/parse/parse.ww:258-265 in
|
|
// parsetype). Split on the rightmost '.' and bind the leaf in
|
|
// the head module's scope. Mirrors cstage resolve_typename
|
|
// cmd/wcc/check.c:74-83 strrchr branch — without this the
|
|
// raw `os.oserror` lookup misses and checkisas false-positives
|
|
// every cross-module tagged scrutinee.
|
|
let dotidx: i32 = -1;
|
|
let i: i32 = 0;
|
|
for (i < nm.len) {
|
|
if (nm[i] == 46u8) { dotidx = i; };
|
|
i += 1;
|
|
};
|
|
let s: *sym = nil;
|
|
if (dotidx >= 0) {
|
|
let head: str;
|
|
head.ptr = nm.ptr;
|
|
head.len = dotidx;
|
|
let leaf: str;
|
|
leaf.ptr = nm.ptr + ((dotidx + 1): u64);
|
|
leaf.len = nm.len - dotidx - 1;
|
|
s = scopelookupinmodule(c.cur, head, leaf);
|
|
} else {
|
|
s = scopelookup(c.cur, nm);
|
|
// #61 A.5: bare TNAME that collides with an imported
|
|
// module bareword. Two shapes hit this:
|
|
// - `let l: lex;` where `lex` struct lives in
|
|
// `package lex;` (mod matches leaf).
|
|
// - `let t: tok;` where `tok` struct lives in
|
|
// `package lex;` (mod differs from leaf — tok.ww
|
|
// declares `package lex;`).
|
|
// scopelookup bucket-walks the flat scope and can land
|
|
// on the SK_USE entry first; without the fallback we'd
|
|
// return the unresolved TNAME and tinfofornode aborts on
|
|
// body == n. scopelookuptype walks the same bucket but
|
|
// filters on SK_TYPE so the struct entry surfaces
|
|
// regardless of its declaring package. Mirrors the
|
|
// bare-vs-qualified pattern from task #57.
|
|
if (s != nil) {
|
|
if (s.skind != skind.SK_TYPE) {
|
|
let sm: *sym = scopelookuptype(c.cur, nm);
|
|
if (sm != nil) { s = sm; };
|
|
};
|
|
};
|
|
};
|
|
if (s == nil) { return cur; };
|
|
if (s.skind != skind.SK_TYPE) { return cur; };
|
|
let body: *node = nil;
|
|
if (s.decl != nil) { body = s.decl.lhs; };
|
|
if (body == nil) { return cur; };
|
|
cur = unwrapbang(body);
|
|
};
|
|
return n;
|
|
};
|
|
|
|
// typeeqast — structural equality on AST type expressions, mod
|
|
// the `!` wrapper. Mirrors variant_match in cgen + check.c: NAMED
|
|
// types compare by string (the closest stand-in for pointer
|
|
// identity at the AST level); other nodes recurse by kind.
|
|
fn typeeqast(a: *node, b: *node) bool = {
|
|
let aa: *node = unwrapbang(a);
|
|
let bb: *node = unwrapbang(b);
|
|
if (aa == nil) { return bb == nil; };
|
|
if (bb == nil) { return false; };
|
|
if (aa.kind != bb.kind) { return false; };
|
|
let k: nkind = aa.kind;
|
|
if (k == nkind.N_TNAME) { return streq(aa.str, bb.str); };
|
|
if (k == nkind.N_TPTR) { return typeeqast(aa.lhs, bb.lhs); };
|
|
if (k == nkind.N_TSLICE){ return typeeqast(aa.lhs, bb.lhs); };
|
|
if (k == nkind.N_TCHAN) { return typeeqast(aa.lhs, bb.lhs); };
|
|
// Conservative: anything else (struct/fn/tagged/tuple/array)
|
|
// fails the cheap check. Selfhost code doesn't currently rely
|
|
// on equality at these shapes for the targeted checks.
|
|
return false;
|
|
};
|
|
|
|
// varianterr — does this variant carry the `!` mark? Either
|
|
// the variant itself is nkind.N_TBANG or it's an alias whose typedecl
|
|
// body is `!T`. Mirrors C check.c's iserror-after-NAMED rule.
|
|
fn varianterr(c: *checker, v: *node) bool = {
|
|
if (v == nil) { return false; };
|
|
if (v.kind == nkind.N_TBANG) { return true; };
|
|
if (v.kind == nkind.N_TNAME) {
|
|
let s: *sym = scopelookup(c.cur, v.str);
|
|
if (s != nil) {
|
|
if (s.skind == skind.SK_TYPE) {
|
|
if (s.decl != nil) {
|
|
if (s.decl.lhs != nil) {
|
|
if (s.decl.lhs.kind == nkind.N_TBANG) {
|
|
return true;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
};
|
|
};
|
|
return false;
|
|
};
|
|
|
|
// taggedhaserr — true iff any variant of `n` (assumed
|
|
// nkind.N_TTAGGED) is `!`-marked. Picks the explicit-flag semantics over
|
|
// the legacy "first variant = success" rule.
|
|
fn taggedhaserr(c: *checker, n: *node) bool = {
|
|
let v: *node = n.list;
|
|
for (v != nil) {
|
|
if (varianterr(c, v)) { return true; };
|
|
v = v.next;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
// iserrvariant — under flag-aware mode (any !-marked variant),
|
|
// returns true iff `v` is `!`-marked. Under legacy mode (no flags),
|
|
// returns true iff `v` is not the first variant of `tagged`.
|
|
fn iserrvariant(c: *checker, tagged: *node, v: *node) bool = {
|
|
if (taggedhaserr(c, tagged)) {
|
|
return varianterr(c, v);
|
|
};
|
|
// Legacy: first variant of the union is success.
|
|
if (tagged.list == v) { return false; };
|
|
return true;
|
|
};
|
|
|
|
// scruttype — resolve the type expression for a match's
|
|
// scrutinee. Handles nkind.N_IDENT (look up local/param's declared
|
|
// type) and nkind.N_DOT (module-qualified ref). Returns nil if we
|
|
// can't statically determine the type. Used by exhaustiveness.
|
|
fn scruttype(c: *checker, e: *node) *node = {
|
|
if (e == nil) { return nil; };
|
|
if (e.kind == nkind.N_IDENT) {
|
|
let s: *sym = scopelookup(c.cur, e.str);
|
|
if (s == nil) { return nil; };
|
|
if (s.decl == nil) { return nil; };
|
|
// For nkind.N_LET / nkind.N_PARAM: declared type is decl.lhs.
|
|
return s.decl.lhs;
|
|
};
|
|
// #51: `match (pkg.var)` / `pkg.var is T` — module-qualified ref.
|
|
// lhs is N_IDENT (module bareword), str is the leaf. Bind via
|
|
// scopelookupinmodule so the declared type carries the same
|
|
// shape resolvealias' dotted-name branch now consumes. Falls
|
|
// silently to nil when lhs is a value (struct-field access) —
|
|
// the rest of the lenient-check contract.
|
|
if (e.kind == nkind.N_DOT) {
|
|
if (e.lhs == nil) { return nil; };
|
|
if (e.lhs.kind != nkind.N_IDENT) { return nil; };
|
|
let s: *sym = scopelookupinmodule(c.cur, e.lhs.str, e.str);
|
|
if (s == nil) { return nil; };
|
|
if (s.decl == nil) { return nil; };
|
|
return s.decl.lhs;
|
|
};
|
|
return nil;
|
|
};
|
|
|
|
// mktname — fabricate an nkind.N_TNAME node with str = `nm`. Used by
|
|
// exprtype to return primitive type nodes for literal
|
|
// expressions. The arena keeps them around as long as the checker.
|
|
fn mktname(c: *checker, nm: str) *node = {
|
|
let n: *node = newnode(nkind.N_TNAME, "", 0, 0);
|
|
n.str = nm;
|
|
return n;
|
|
};
|
|
|
|
// #43: SSoT for primitive type byte sizes. astsize's N_TNAME-primitive
|
|
// arm and every wwstage cgen size walker (slotsize/fieldsize/letemit-
|
|
// size/elemsizeof/paramfieldsize) consult this table so a future
|
|
// ty_str.size bump (#1) lands in one place. Returns -1 for non-prim
|
|
// names; callers fall back to alias/struct/enum lookup. Cstage's
|
|
// equivalent SSoT is cmd/wcc/type.c:46-79 (ty_void/ty_bool/.../ty_str).
|
|
fn primtypesize(nm: str) i64 = {
|
|
if (streq(nm, "void")) { return 0i64; };
|
|
if (streq(nm, "bool")) { return 1i64; };
|
|
if (streq(nm, "i8") || streq(nm, "u8")) { return 1i64; };
|
|
if (streq(nm, "i16") || streq(nm, "u16")) { return 2i64; };
|
|
if (streq(nm, "i32") || streq(nm, "u32") || streq(nm, "f32") || streq(nm, "rune")) { return 4i64; };
|
|
if (streq(nm, "i64") || streq(nm, "u64") || streq(nm, "f64")) { return 8i64; };
|
|
if (streq(nm, "int") || streq(nm, "uint") || streq(nm, "uintptr")) { return 8i64; };
|
|
if (streq(nm, "str")) { return 16i64; }; // sizelint-ok: SSoT for ty_str primtype (#64)
|
|
return -1i64;
|
|
};
|
|
|
|
// #43: SSoT for slice header size (ptr+len+cap = 24B today). Mirrors
|
|
// cstage cmd/wcc/type.c:103 (ty_slice->size = 24). Bumping a slice's
|
|
// header layout in #34 touches only this constant.
|
|
fn tyslicesize() i64 = { return 24i64; }; // sizelint-ok: SSoT for ty_slice header (#64)
|
|
|
|
// #42: AST-level layout helpers for the size(T)/align(T)/offset(e.f)
|
|
// fold. Mirror cstage resolve_type's size/align computation
|
|
// (cmd/wcc/check.c:286-528) on AST nodes — wwstage check.ww never
|
|
// materialises tinfo for user types so the fold has to walk the AST
|
|
// directly. Struct layout follows cstage check.c:471-526 (align each
|
|
// field, max align for the whole record, round size up to alignment).
|
|
fn astalign(c: *checker, t: *node) i64 = {
|
|
if (t == nil) { return 1i64; };
|
|
let k: nkind = t.kind;
|
|
if (k == nkind.N_TBANG) { return astalign(c, t.lhs); };
|
|
if (k == nkind.N_TPTR) { return 8i64; };
|
|
if (k == nkind.N_TSLICE) { return 8i64; };
|
|
if (k == nkind.N_TCHAN) { return 8i64; };
|
|
if (k == nkind.N_TFN) { return 8i64; };
|
|
if (k == nkind.N_TARRAY) { return astalign(c, t.lhs); };
|
|
if (k == nkind.N_TTAGGED) { return 8i64; };
|
|
if (k == nkind.N_TTUPLE) {
|
|
let m: i64 = 1i64;
|
|
let p: *node = t.list;
|
|
for (p != nil) {
|
|
let pa: i64 = astalign(c, p.lhs);
|
|
if (pa > m) { m = pa; };
|
|
p = p.next;
|
|
};
|
|
return m;
|
|
};
|
|
if (k == nkind.N_TSTRUCT) {
|
|
let m: i64 = 1i64;
|
|
let f: *node = t.list;
|
|
for (f != nil) {
|
|
if (f.kind == nkind.N_TFIELD) {
|
|
let fa: i64 = astalign(c, f.lhs);
|
|
if (fa > m) { m = fa; };
|
|
};
|
|
f = f.next;
|
|
};
|
|
return m;
|
|
};
|
|
if (k == nkind.N_TENUM) {
|
|
if (t.lhs != nil) { return astalign(c, t.lhs); };
|
|
return 4i64;
|
|
};
|
|
if (k == nkind.N_TNAME) {
|
|
let nm: str = t.str;
|
|
if (streq(nm, "void") || streq(nm, "bool") || streq(nm, "i8") || streq(nm, "u8")) { return 1i64; };
|
|
if (streq(nm, "i16") || streq(nm, "u16")) { return 2i64; };
|
|
if (streq(nm, "i32") || streq(nm, "u32") || streq(nm, "f32") || streq(nm, "rune")) { return 4i64; };
|
|
if (streq(nm, "i64") || streq(nm, "u64") || streq(nm, "f64") || streq(nm, "int") || streq(nm, "uint") || streq(nm, "uintptr") || streq(nm, "str")) { return 8i64; };
|
|
let resolved: *node = resolvealias(c, t);
|
|
if (resolved != nil && resolved != t) {
|
|
return astalign(c, resolved);
|
|
};
|
|
};
|
|
return 1i64;
|
|
};
|
|
|
|
fn astsize(c: *checker, t: *node) i64 = {
|
|
if (t == nil) { return 0i64; };
|
|
let k: nkind = t.kind;
|
|
if (k == nkind.N_TBANG) { return astsize(c, t.lhs); };
|
|
if (k == nkind.N_TPTR) { return 8i64; };
|
|
if (k == nkind.N_TSLICE) { return tyslicesize(); };
|
|
if (k == nkind.N_TCHAN) { return 8i64; };
|
|
if (k == nkind.N_TFN) { return 8i64; };
|
|
if (k == nkind.N_TARRAY) {
|
|
let elen: i64 = 0i64;
|
|
if (t.rhs != nil) {
|
|
if (t.rhs.kind == nkind.N_INTLIT) { elen = t.rhs.uval: i64; };
|
|
};
|
|
return astsize(c, t.lhs) * elen;
|
|
};
|
|
if (k == nkind.N_TTUPLE) {
|
|
let total: i64 = 0i64;
|
|
let p: *node = t.list;
|
|
for (p != nil) {
|
|
total += astsize(c, p.lhs);
|
|
p = p.next;
|
|
};
|
|
return total;
|
|
};
|
|
if (k == nkind.N_TSTRUCT) {
|
|
let off: i64 = 0i64;
|
|
let maxal: i64 = 1i64;
|
|
let f: *node = t.list;
|
|
for (f != nil) {
|
|
if (f.kind == nkind.N_TFIELD) {
|
|
let fa: i64 = astalign(c, f.lhs);
|
|
if (fa > maxal) { maxal = fa; };
|
|
off = (off + fa - 1i64) & ~(fa - 1i64);
|
|
off += astsize(c, f.lhs);
|
|
};
|
|
f = f.next;
|
|
};
|
|
return (off + maxal - 1i64) & ~(maxal - 1i64);
|
|
};
|
|
if (k == nkind.N_TTAGGED) {
|
|
// 8 (tag) + max variant payload, rounded up to 8.
|
|
let maxsz: i64 = 0i64;
|
|
let v: *node = t.list;
|
|
for (v != nil) {
|
|
let sz: i64 = astsize(c, v);
|
|
if (sz > maxsz) { maxsz = sz; };
|
|
v = v.next;
|
|
};
|
|
let pad: i64 = (maxsz + 7i64) & ~7i64;
|
|
return 8i64 + pad;
|
|
};
|
|
if (k == nkind.N_TENUM) {
|
|
if (t.lhs != nil) { return astsize(c, t.lhs); };
|
|
return 4i64;
|
|
};
|
|
if (k == nkind.N_TNAME) {
|
|
let nm: str = t.str;
|
|
let ps: i64 = primtypesize(nm);
|
|
if (ps >= 0i64) { return ps; };
|
|
let resolved: *node = resolvealias(c, t);
|
|
if (resolved != nil && resolved != t) {
|
|
return astsize(c, resolved);
|
|
};
|
|
};
|
|
return 0i64;
|
|
};
|
|
|
|
// astoffset — byte offset of `dot.str` inside the struct type of
|
|
// `dot.lhs`. Mirrors cstage cmd/wcc/check.c:932-961: peel one N_TPTR
|
|
// (for `p.field` where p is *Struct), require N_TSTRUCT, walk fields
|
|
// honouring per-field alignment, return -1 if the field name is
|
|
// absent so the caller can flag the error and fold to 0.
|
|
fn astoffset(c: *checker, dot: *node) i64 = {
|
|
if (dot == nil) { return -1i64; };
|
|
if (dot.kind != nkind.N_DOT) { return -1i64; };
|
|
let recv: *node = scruttype(c, dot.lhs);
|
|
if (recv == nil) { return -1i64; };
|
|
let rtyp: *node = resolvealias(c, unwrapbang(recv));
|
|
if (rtyp == nil) { return -1i64; };
|
|
if (rtyp.kind == nkind.N_TPTR) {
|
|
rtyp = resolvealias(c, unwrapbang(rtyp.lhs));
|
|
};
|
|
if (rtyp == nil) { return -1i64; };
|
|
if (rtyp.kind != nkind.N_TSTRUCT) { return -1i64; };
|
|
let off: i64 = 0i64;
|
|
let f: *node = rtyp.list;
|
|
for (f != nil) {
|
|
if (f.kind == nkind.N_TFIELD) {
|
|
let fa: i64 = astalign(c, f.lhs);
|
|
off = (off + fa - 1i64) & ~(fa - 1i64);
|
|
if (streq(f.str, dot.str)) { return off; };
|
|
off += astsize(c, f.lhs);
|
|
};
|
|
f = f.next;
|
|
};
|
|
return -1i64;
|
|
};
|
|
|
|
// arenau64tos — decimal string for the folded INTLIT's `str` field.
|
|
// Cstage uses aprintf("%llu") at the same site (cmd/wcc/check.c:921);
|
|
// wwstage cgen only reads `uval` for N_INTLIT codegen so `str` is
|
|
// just for the AST printer, but set it for parity with the parser's
|
|
// own literal-emit shape.
|
|
fn arenau64tos(v: u64) str = {
|
|
let buf: []u8 = alloc([], 24u64)!;
|
|
let i: i32 = 23;
|
|
buf[i] = 0u8;
|
|
if (v == 0u64) { i -= 1; buf[i] = 48u8; };
|
|
let n: u64 = v;
|
|
for (n > 0u64) {
|
|
i -= 1;
|
|
buf[i] = (48u64 + (n % 10u64)): u8;
|
|
n /= 10u64;
|
|
};
|
|
let r: str;
|
|
r.ptr = buf.ptr + (i: u64);
|
|
r.len = 23 - i;
|
|
return r;
|
|
};
|
|
|
|
// foldtointlit — mutate `n` in place to an N_INTLIT with value `v`.
|
|
// Used by the #42 size/align/offset intercepts so cgen sees the
|
|
// folded literal rather than an unresolved call. Mirrors cstage
|
|
// cmd/wcc/check.c:919-927 / :951-958.
|
|
fn foldtointlit(c: *checker, n: *node, v: i64) void = {
|
|
n.kind = nkind.N_INTLIT;
|
|
n.uval = v: u64;
|
|
n.str = arenau64tos(v: u64);
|
|
n.lhs = nil;
|
|
n.list = nil;
|
|
let empty: str;
|
|
n.tsuffix = empty;
|
|
};
|
|
|
|
// #61 A.5 helper: per-element slot size when `pt` appears inside a
|
|
// tuple. Mirrors cgenutil.ww slotsize TTUPLE — cstage's tuple ABI
|
|
// spills each element into its own register / 8B eightbyte, so narrow
|
|
// scalars pad to 8 (cgen's let_emit_size + AX:DX:CX positional layout).
|
|
// str/slice and composites consult `pt.size` so a future #1 bump on
|
|
// any primitive layout propagates through the typ.ww SSoT seed
|
|
// instead of getting baked into this detour. pointer/fn/chan stay
|
|
// 8; void contributes 0 (never appears in tuples emitted by user
|
|
// code, but kept for SSoT symmetry with cgen's N_TNAME-"void"
|
|
// fallback arm).
|
|
fn tupleelemslot(pt: *tinfo) u64 = {
|
|
if (pt == nil) { return 8u64; };
|
|
let pk: tykind = pt.kind;
|
|
if (pk == tykind.TY_VOID) { return 0u64; };
|
|
if (pk == tykind.TY_STR) { return pt.size; };
|
|
if (pk == tykind.TY_SLICE) { return pt.size; };
|
|
if (pk == tykind.TY_PTR || pk == tykind.TY_FN ||
|
|
pk == tykind.TY_CHAN || pk == tykind.TY_I64 ||
|
|
pk == tykind.TY_U64 || pk == tykind.TY_INT ||
|
|
pk == tykind.TY_UINT || pk == tykind.TY_UINTPTR ||
|
|
pk == tykind.TY_F64) { return 8u64; };
|
|
if (pk == tykind.TY_BOOL || pk == tykind.TY_RUNE ||
|
|
pk == tykind.TY_I8 || pk == tykind.TY_I16 ||
|
|
pk == tykind.TY_I32 || pk == tykind.TY_U8 ||
|
|
pk == tykind.TY_U16 || pk == tykind.TY_U32 ||
|
|
pk == tykind.TY_F32 || pk == tykind.TY_ENUM) { return 8u64; };
|
|
// Composite — struct/tuple/array/tagged carry their own slot total.
|
|
return pt.slotsize;
|
|
};
|
|
|
|
// #61 A.5 helper: per-field slot size mirroring cgenutil.ww
|
|
// registerstruct/fieldsize. Nested struct fields contribute their
|
|
// slot-padded total (si.totsize equivalent); primitives keep their
|
|
// natural width (struct interior packing is unaffected by stack-slot
|
|
// pad-to-8); arrays use their slot-padded element-stride * elen.
|
|
fn fieldslotsize(ft: *tinfo) u64 = {
|
|
if (ft == nil) { return 8u64; };
|
|
let fk: tykind = ft.kind;
|
|
if (fk == tykind.TY_STRUCT) { return ft.slotsize; };
|
|
if (fk == tykind.TY_ARRAY) { return ft.slotsize; };
|
|
if (fk == tykind.TY_TAGGED) { return ft.size; };
|
|
// str / slice read ft.size so the typ.ww SSoT seed is the single
|
|
// source for #1 (str→24) / #34 (slice graduation) — no hardcoded
|
|
// literal here to drift.
|
|
if (fk == tykind.TY_SLICE) { return ft.size; };
|
|
if (fk == tykind.TY_PTR || fk == tykind.TY_FN ||
|
|
fk == tykind.TY_CHAN) { return 8u64; };
|
|
if (fk == tykind.TY_STR) { return ft.size; };
|
|
// Primitives keep natural width inside structs (matches
|
|
// cgenutil fieldsize: primsize, not pad-to-8). TY_TUPLE inside a
|
|
// struct currently defaults to 8 in cgenutil — preserve that
|
|
// shape until a future graduation aligns the two.
|
|
if (fk == tykind.TY_BOOL || fk == tykind.TY_RUNE ||
|
|
fk == tykind.TY_I8 || fk == tykind.TY_I16 ||
|
|
fk == tykind.TY_I32 || fk == tykind.TY_I64 ||
|
|
fk == tykind.TY_U8 || fk == tykind.TY_U16 ||
|
|
fk == tykind.TY_U32 || fk == tykind.TY_U64 ||
|
|
fk == tykind.TY_INT || fk == tykind.TY_UINT ||
|
|
fk == tykind.TY_UINTPTR || fk == tykind.TY_F32 ||
|
|
fk == tykind.TY_F64 || fk == tykind.TY_ENUM) { return ft.size; };
|
|
return 8u64;
|
|
};
|
|
|
|
// #61 audit §1.8 — resolve a type-expression AST node to its *tinfo.
|
|
// Mirrors cstage's resolve_type (cmd/wcc/check.c:286-565) which
|
|
// produces ty_* singletons / arena-allocated composites from a Node*.
|
|
// Cache lives in c.tc (typ.ww) so the same shape can be reused across
|
|
// modules within one check pass. Rob+Drew convergence 2026-05-20: cgen
|
|
// reads sizes from here starting with slotsize in A.2; subsequent
|
|
// sub-commits graduate elemsize/fieldsize/letemitsize/etc. onto the
|
|
// same pivot.
|
|
//
|
|
// A.2 coverage: primitive TNAME singletons, TNAME aliases (via
|
|
// resolvealias), TBANG (inner unchanged — see iserror note), TPTR,
|
|
// TSLICE, TCHAN, TARRAY, TFN, TENUM, TTUPLE, TSTRUCT, TTAGGED. Size
|
|
// computation tracks cstage natural sizes; cgen's slot-padding
|
|
// contract (cmd/w6c/cgen.c let_emit_size:691-720 pads narrow scalars
|
|
// to 8B) stays in slotsize's fallback walker.
|
|
fn tinfofornode(c: *checker, n: *node) *tinfo = {
|
|
if (n == nil) { return nil; };
|
|
let cached: *tinfo = tinfocachelookup(c.tc, n);
|
|
if (cached != nil) { return cached; };
|
|
let r: *tinfo = nil;
|
|
let k: nkind = n.kind;
|
|
if (k == nkind.N_TNAME) {
|
|
let nm: str = n.str;
|
|
if (streq(nm, "void")) { r = c.tc.tyvoid; };
|
|
if (streq(nm, "bool")) { r = c.tc.tybool; };
|
|
if (streq(nm, "rune")) { r = c.tc.tyrune; };
|
|
if (streq(nm, "i8")) { r = c.tc.tyi8; };
|
|
if (streq(nm, "i16")) { r = c.tc.tyi16; };
|
|
if (streq(nm, "i32")) { r = c.tc.tyi32; };
|
|
if (streq(nm, "i64")) { r = c.tc.tyi64; };
|
|
if (streq(nm, "u8")) { r = c.tc.tyu8; };
|
|
if (streq(nm, "u16")) { r = c.tc.tyu16; };
|
|
if (streq(nm, "u32")) { r = c.tc.tyu32; };
|
|
if (streq(nm, "u64")) { r = c.tc.tyu64; };
|
|
if (streq(nm, "int")) { r = c.tc.tyint; };
|
|
if (streq(nm, "uint")) { r = c.tc.tyuint; };
|
|
if (streq(nm, "uintptr")) { r = c.tc.tyuintptr; };
|
|
if (streq(nm, "f32")) { r = c.tc.tyf32; };
|
|
if (streq(nm, "f64")) { r = c.tc.tyf64; };
|
|
if (streq(nm, "str")) { r = c.tc.tystr; };
|
|
if (streq(nm, "never")) { r = c.tc.tynever; };
|
|
if (streq(nm, "untyped_int")) { r = c.tc.tyuntypedint; };
|
|
if (streq(nm, "untyped_float")) { r = c.tc.tyuntypedfloat; };
|
|
if (streq(nm, "untyped_str")) { r = c.tc.tyuntypedstr; };
|
|
if (streq(nm, "untyped_rune")) { r = c.tc.tyuntypedrune; };
|
|
if (streq(nm, "untyped_bool")) { r = c.tc.tyuntypedbool; };
|
|
if (streq(nm, "untyped_nil")) { r = c.tc.tyuntypednil; };
|
|
if (r == nil) {
|
|
// Alias / user-defined name: resolve via scope and recurse.
|
|
// Mirrors astsize's TNAME fallback so the helpers stay in
|
|
// lockstep until A.2 collapses each cgen size-walker onto
|
|
// tinfo.size directly.
|
|
//
|
|
// #61 A.4: bind the resolved body too so future
|
|
// tinfofornode calls on either the TNAME or its target
|
|
// short-circuit on the cache hit instead of re-walking
|
|
// the chain. Pre-bind matches A.2's TSTRUCT/TFN/TTUPLE/
|
|
// TTAGGED cycle-break pattern (a self-referential
|
|
// struct field's *T → TNAME → body would otherwise
|
|
// re-enter the same chain).
|
|
let body: *node = resolvealias(c, n);
|
|
if (body != nil && body != n) {
|
|
let cached2: *tinfo = tinfocachelookup(c.tc, body);
|
|
if (cached2 != nil) {
|
|
r = cached2;
|
|
} else {
|
|
r = tinfofornode(c, body);
|
|
if (r != nil) {
|
|
tinfocachebind(c.tc, body, r);
|
|
};
|
|
};
|
|
};
|
|
};
|
|
} else { if (k == nkind.N_TBANG) {
|
|
// #61 audit §1.8: `!T` propagates the inner shape; cstage's
|
|
// resolve_type sets ty->iserror on the wrapper but no wwstage
|
|
// cgen reader consumes it yet, so A.1 drops the flag and
|
|
// returns the inner tinfo unchanged. Mirrors typeeqast's
|
|
// unwrapbang pre-walk; graduate alongside the first cgen
|
|
// site that needs iserror discrimination.
|
|
r = tinfofornode(c, n.lhs);
|
|
} else { if (k == nkind.N_TPTR) {
|
|
r = typeptr(tinfofornode(c, n.lhs));
|
|
} else { if (k == nkind.N_TSLICE) {
|
|
r = typeslice(tinfofornode(c, n.lhs));
|
|
} else { if (k == nkind.N_TCHAN) {
|
|
r = typechan(tinfofornode(c, n.lhs));
|
|
} else { if (k == nkind.N_TARRAY) {
|
|
// Cstage cmd/wcc/check.c:314-326: length must be an integer
|
|
// literal (`[_]T` keeps alen=0 as the inferred-length sentinel
|
|
// patched at letslotsize-time).
|
|
//
|
|
// #61 A.5: ti.size = natural (sub.size * elen), ti.slotsize =
|
|
// slot-padded (sub.slotsize * elen) — typearray handles both.
|
|
// Reverts A.4's r.size override (which conflated stride with
|
|
// natural size); the slot-padded stride now lives in slotsize
|
|
// where cgenutil's fast-path reads it.
|
|
let elen: u64 = 0u64;
|
|
if (n.rhs != nil) {
|
|
if (n.rhs.kind == nkind.N_INTLIT) { elen = n.rhs.uval; };
|
|
};
|
|
let sub: *tinfo = tinfofornode(c, n.lhs);
|
|
r = typearray(sub, elen);
|
|
} else { if (k == nkind.N_TFN) {
|
|
// Cstage cmd/wcc/check.c:437-466: function types are 8B / 8B
|
|
// (call-target pointer shape). Pre-bind before recursing into
|
|
// the return type so a recursive `type F = fn() F` self-ref
|
|
// doesn't spin (cycle-break mirror of the TSTRUCT/TTAGGED
|
|
// pattern below).
|
|
r = newtype(tykind.TY_FN);
|
|
r.size = 8u64;
|
|
r.align = 8u64;
|
|
r.slotsize = 8u64;
|
|
tinfocachebind(c.tc, n, r);
|
|
r.ret = tinfofornode(c, n.lhs);
|
|
} else { if (k == nkind.N_TENUM) {
|
|
// Cstage cmd/wcc/check.c:529-542: storage type's size/align
|
|
// (default i32 = 4B/4B). Cgen's slotsize-TENUM fallback pads
|
|
// to 8B per its stack-slot contract; tinfo.size carries the
|
|
// raw storage width so size(EnumT) folds to the correct value.
|
|
r = newtype(tykind.TY_ENUM);
|
|
let storage: *tinfo = nil;
|
|
if (n.lhs != nil) { storage = tinfofornode(c, n.lhs); };
|
|
if (storage == nil) { storage = c.tc.tyi32; };
|
|
r.sub = storage;
|
|
r.size = storage.size;
|
|
r.align = storage.align;
|
|
r.slotsize = storage.size;
|
|
} else { if (k == nkind.N_TTUPLE) {
|
|
// Cstage cmd/wcc/check.c:329-345: sum of element sizes with
|
|
// per-element alignment NOT padded — cstage uses raw sums for
|
|
// tuples and 8B-rounding lives at the call/return ABI layer.
|
|
// Pre-bind for cycle protection (recursive tuple shapes).
|
|
//
|
|
// #61 A.5: ti.size = natural sum (cstage parity); ti.slotsize
|
|
// = per-element slot sum mirroring cgenutil.ww:2018-2029
|
|
// slotsize TTUPLE — narrow scalars pad to 8 (cgen spills each
|
|
// tuple element into its own register / stack-slot eightbyte),
|
|
// composites contribute their own ti.slotsize.
|
|
r = newtype(tykind.TY_TUPLE);
|
|
tinfocachebind(c.tc, n, r);
|
|
let total: u64 = 0u64;
|
|
let slottotal: u64 = 0u64;
|
|
let maxal: u64 = 1u64;
|
|
let p: *node = n.list;
|
|
for (p != nil) {
|
|
let pt: *tinfo = tinfofornode(c, p.lhs);
|
|
if (pt != nil) {
|
|
if (pt.align > maxal) { maxal = pt.align; };
|
|
total += pt.size;
|
|
slottotal += tupleelemslot(pt);
|
|
};
|
|
p = p.next;
|
|
};
|
|
r.size = total;
|
|
r.align = maxal;
|
|
r.slotsize = slottotal;
|
|
} else { if (k == nkind.N_TSTRUCT) {
|
|
// Cstage cmd/wcc/check.c:468-527: per-field alignment, max
|
|
// align for the whole record, total rounded up to alignment.
|
|
// Anonymous-embed promotion is deferred (#13).
|
|
//
|
|
// Pre-bind into the cache BEFORE walking fields so a
|
|
// self-referential pointer field (e.g., `next: *node` inside
|
|
// `type node = struct {..., next: *node, ...}`) terminates:
|
|
// the inner tinfofornode(TNAME(node)) resolvealias-recurses
|
|
// back to this same body node, hits the cache, and returns
|
|
// the in-progress stub. r.size is filled in below; the stub's
|
|
// only consumer during the recursion is typeptr (8B/8B
|
|
// regardless of pointee size), so partial-fill is safe.
|
|
//
|
|
// #61 A.5: alongside the natural layout (cstage parity), walk
|
|
// the same fields with the slot-padded sizing cgenutil.ww
|
|
// registerstruct uses (fieldsize → si.totsize for nested
|
|
// struct; size-derived alignment; final round to 8). That
|
|
// slot total lands in ti.slotsize so the cgen fast-path can
|
|
// graduate TY_STRUCT off the AST walker.
|
|
r = newtype(tykind.TY_STRUCT);
|
|
tinfocachebind(c.tc, n, r);
|
|
let off: u64 = 0u64;
|
|
let maxalign: u64 = 1u64;
|
|
let soff: u64 = 0u64;
|
|
let f: *node = n.list;
|
|
for (f != nil) {
|
|
if (f.kind == nkind.N_TFIELD) {
|
|
let ft: *tinfo = tinfofornode(c, f.lhs);
|
|
if (ft != nil) {
|
|
if (ft.align > maxalign) { maxalign = ft.align; };
|
|
if (ft.align > 0u64) {
|
|
off = (off + ft.align - 1u64) & ~(ft.align - 1u64);
|
|
};
|
|
off += ft.size;
|
|
// Slot-padded layout (mirror of cgenutil
|
|
// fieldsize + registerstruct align rules).
|
|
let fsz: u64 = fieldslotsize(ft);
|
|
let faln: u64 = 1u64;
|
|
if (fsz >= 8u64) { faln = 8u64; }
|
|
else { if (fsz >= 4u64) { faln = 4u64; }
|
|
else { if (fsz >= 2u64) { faln = 2u64; }; }; };
|
|
if ((soff & (faln - 1u64)) != 0u64) {
|
|
soff = (soff + faln - 1u64) & ~(faln - 1u64);
|
|
};
|
|
soff += fsz;
|
|
};
|
|
};
|
|
f = f.next;
|
|
};
|
|
if (maxalign > 0u64) {
|
|
r.size = (off + maxalign - 1u64) & ~(maxalign - 1u64);
|
|
};
|
|
r.align = maxalign;
|
|
if ((soff & 7u64) != 0u64) {
|
|
soff = (soff + 7u64) & ~7u64;
|
|
};
|
|
r.slotsize = soff;
|
|
} else { if (k == nkind.N_TTAGGED) {
|
|
// Cstage cmd/wcc/check.c:347-435: 8B tag + max(variant)
|
|
// rounded up to 8. Pre-bind for cycle protection (recursive
|
|
// sum-type shapes through NAMED variants).
|
|
r = newtype(tykind.TY_TAGGED);
|
|
tinfocachebind(c.tc, n, r);
|
|
// #61 A.3 nullable fold: `(*T | void)` collapses to a single
|
|
// 8B pointer slot, null is the void variant. Mirrors
|
|
// cmd/wcc/check.c:412-426 — bare TNAME("void"), not `!void`,
|
|
// and not NAMED — so wwstage slotsize fast-path can graduate
|
|
// TY_TAGGED off the AST-walker fallback. Match before counting
|
|
// variants so the 8B fold lands in tinfo.size directly.
|
|
let a: *node = n.list;
|
|
if (a != nil) {
|
|
let b: *node = a.next;
|
|
if (b != nil && b.next == nil) {
|
|
let aptr: bool = (a.kind == nkind.N_TPTR);
|
|
let bptr: bool = (b.kind == nkind.N_TPTR);
|
|
let avoid: bool = (a.kind == nkind.N_TNAME);
|
|
if (avoid) { avoid = streq(a.str, "void"); };
|
|
let bvoid: bool = (b.kind == nkind.N_TNAME);
|
|
if (bvoid) { bvoid = streq(b.str, "void"); };
|
|
let isnull: bool = false;
|
|
if (aptr) { if (bvoid) { isnull = true; }; };
|
|
if (avoid) { if (bptr) { isnull = true; }; };
|
|
if (isnull) {
|
|
r.size = 8u64;
|
|
r.align = 8u64;
|
|
r.nullable = 1;
|
|
r.slotsize = 8u64;
|
|
tinfocachebind(c.tc, n, r);
|
|
return r;
|
|
};
|
|
};
|
|
};
|
|
let maxsz: u64 = 0u64;
|
|
let al: u64 = 8u64;
|
|
let v: *node = n.list;
|
|
for (v != nil) {
|
|
let vt: *tinfo = tinfofornode(c, v);
|
|
if (vt != nil) {
|
|
if (vt.size > maxsz) { maxsz = vt.size; };
|
|
if (vt.align > al) { al = vt.align; };
|
|
};
|
|
v = v.next;
|
|
};
|
|
let pad: u64 = (maxsz + 7u64) & ~7u64;
|
|
r.size = 8u64 + pad;
|
|
r.align = al;
|
|
r.slotsize = 8u64 + pad;
|
|
};};};};};};};};};};};
|
|
if (r != nil) {
|
|
// #61 A.5: any arm that didn't set slotsize gets ti.size as
|
|
// the default (covers primitives via prim() + the ptr/slice/
|
|
// chan paths which already populate slotsize, plus TBANG which
|
|
// inherits the inner's tinfo unchanged).
|
|
if (r.slotsize == 0u64) { r.slotsize = r.size; };
|
|
tinfocachebind(c.tc, n, r);
|
|
};
|
|
return r;
|
|
};
|
|
|
|
// unifyarith — usual-arithmetic-conversion analogue at the AST-tnode
|
|
// layer. Mirrors cstage cmd/wcc/check.c:580-596 `unify_arith`. The
|
|
// trailing typeeq fallback covers same-typed operands; mismatched typed
|
|
// pairs silently return ltn (cstage flags the same shape — wwstage's
|
|
// checker stays silent here per existing discipline).
|
|
fn unifyarith(c: *checker, ltn: *node, rtn: *node) *node = {
|
|
let lu: bool = isuntypedint(ltn) || isuntypedfloat(ltn);
|
|
let ru: bool = isuntypedint(rtn) || isuntypedfloat(rtn);
|
|
if (lu && ru) {
|
|
if (isuntypedfloat(ltn) || isuntypedfloat(rtn)) {
|
|
return mktname(c, "untyped_float");
|
|
};
|
|
return mktname(c, "untyped_int");
|
|
};
|
|
let conf: bool = false;
|
|
if (lu) {
|
|
if (isassignable(c, rtn, ltn, &conf)) { return rtn; };
|
|
};
|
|
if (ru) {
|
|
if (isassignable(c, ltn, rtn, &conf)) { return ltn; };
|
|
};
|
|
if (typeeqast(ltn, rtn)) { return ltn; };
|
|
return ltn;
|
|
};
|
|
|
|
// binoptype — derive the result tnode of an N_BIN operator expression.
|
|
// Mirrors cstage cmd/wcc/check.c:598-640 `cbinop`. Operates on tnodes
|
|
// returned by exprtype; ptr arithmetic / bitwise / shifts / comparisons /
|
|
// logicals all reflect cstage's rules. Type-mismatch diagnostics are
|
|
// elided here (cstage gates the same shape).
|
|
fn binoptype(c: *checker, e: *node) *node = {
|
|
let op: tkind = e.op;
|
|
let ltn: *node = exprtype(c, e.lhs, nil);
|
|
let rtn: *node = exprtype(c, e.rhs, nil);
|
|
if (op == tkind.TK_PLUS || op == tkind.TK_MINUS) {
|
|
if (ltn != nil && ltn.kind == nkind.N_TPTR && isinttypeast(rtn)) {
|
|
return ltn;
|
|
};
|
|
};
|
|
if (op == tkind.TK_PLUS) {
|
|
if (isinttypeast(ltn) && rtn != nil && rtn.kind == nkind.N_TPTR) {
|
|
return rtn;
|
|
};
|
|
};
|
|
if (op == tkind.TK_MINUS) {
|
|
if (ltn != nil && rtn != nil
|
|
&& ltn.kind == nkind.N_TPTR && rtn.kind == nkind.N_TPTR) {
|
|
return mktname(c, "i64");
|
|
};
|
|
};
|
|
if (op == tkind.TK_PLUS || op == tkind.TK_MINUS ||
|
|
op == tkind.TK_STAR || op == tkind.TK_SLASH ||
|
|
op == tkind.TK_PERCENT || op == tkind.TK_AMP ||
|
|
op == tkind.TK_PIPE || op == tkind.TK_CARET ||
|
|
op == tkind.TK_LSHIFT || op == tkind.TK_RSHIFT) {
|
|
return unifyarith(c, ltn, rtn);
|
|
};
|
|
if (op == tkind.TK_EQ || op == tkind.TK_NEQ ||
|
|
op == tkind.TK_LT || op == tkind.TK_LE ||
|
|
op == tkind.TK_GT || op == tkind.TK_GE ||
|
|
op == tkind.TK_AND || op == tkind.TK_OR) {
|
|
return mktname(c, "bool");
|
|
};
|
|
return nil;
|
|
};
|
|
|
|
// unoptype — derive the result tnode of an N_UN unary expression. Mirrors
|
|
// cstage cmd/wcc/check.c:642-687 `cunop`. The slice/str .len/.cap
|
|
// pseudo-field address-of widening to *i64 mirrors check.c:672-682
|
|
// directly — its purpose is documented at the cstage site.
|
|
fn unoptype(c: *checker, e: *node) *node = {
|
|
let op: tkind = e.op;
|
|
let opt: *node = exprtype(c, e.lhs, nil);
|
|
if (op == tkind.TK_MINUS || op == tkind.TK_PLUS) { return opt; };
|
|
if (op == tkind.TK_NOT) { return mktname(c, "bool"); };
|
|
if (op == tkind.TK_TILDE) { return opt; };
|
|
if (op == tkind.TK_STAR) {
|
|
if (opt == nil) { return nil; };
|
|
let u: *node = resolvealias(c, unwrapbang(opt));
|
|
if (u == nil) { return nil; };
|
|
if (u.kind != nkind.N_TPTR) { return nil; };
|
|
return u.lhs;
|
|
};
|
|
if (op == tkind.TK_AMP) {
|
|
if (e.lhs != nil && e.lhs.kind == nkind.N_DOT) {
|
|
let fld: str = e.lhs.str;
|
|
if (streq(fld, "len") || streq(fld, "cap")) {
|
|
let base: *node = e.lhs.lhs;
|
|
if (base != nil && base.type_ != nil) {
|
|
let bu: *tinfo = base.type_: *tinfo;
|
|
if (bu != nil && bu.kind == tykind.TY_NAMED) { bu = bu.under; };
|
|
if (bu != nil && bu.kind == tykind.TY_PTR) { bu = bu.sub; };
|
|
if (bu != nil && bu.kind == tykind.TY_NAMED) { bu = bu.under; };
|
|
if (bu != nil) {
|
|
if (bu.kind == tykind.TY_SLICE || bu.kind == tykind.TY_STR) {
|
|
let pp: *node = newnode(nkind.N_TPTR, "", 0, 0);
|
|
pp.lhs = mktname(c, "i64");
|
|
return pp;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
};
|
|
if (opt == nil) { return nil; };
|
|
let pp: *node = newnode(nkind.N_TPTR, "", 0, 0);
|
|
pp.lhs = opt;
|
|
return pp;
|
|
};
|
|
return nil;
|
|
};
|
|
|
|
// indexresult — derive the result tnode of an N_INDEX expression.
|
|
// Mirrors cstage cmd/wcc/check.c:870-894. Slice/array → elem; str →
|
|
// u8; `*[N]T` decays to T (pointer-to-array); `*[]T` does NOT decay
|
|
// (yields []T via the generic *U → U fallback — Hare-faithful, a
|
|
// pointer-to-slice is a 1D array of slices, not of T); generic *T → T.
|
|
fn indexresult(c: *checker, e: *node) *node = {
|
|
let basetn: *node = exprtype(c, e.lhs, nil);
|
|
let _idx: *node = exprtype(c, e.rhs, nil);
|
|
let u: *node = resolvealias(c, unwrapbang(basetn));
|
|
if (u == nil) { return nil; };
|
|
if (u.kind == nkind.N_TSLICE) { return u.lhs; };
|
|
if (u.kind == nkind.N_TARRAY) { return u.lhs; };
|
|
if (u.kind == nkind.N_TNAME) {
|
|
if (streq(u.str, "str")) { return mktname(c, "u8"); };
|
|
};
|
|
if (u.kind == nkind.N_TPTR) {
|
|
let inner: *node = u.lhs;
|
|
let iu: *node = resolvealias(c, unwrapbang(inner));
|
|
if (iu != nil && iu.kind == nkind.N_TARRAY) {
|
|
return iu.lhs;
|
|
};
|
|
return inner;
|
|
};
|
|
return nil;
|
|
};
|
|
|
|
// exprtype — best-effort type-AST inference for an expression
|
|
// node. Handles literals, identifiers, calls, casts, binary/unary
|
|
// ops, indexing, module-qualified refs + enum-member folds; returns
|
|
// nil for shapes we don't statically know (struct field access into
|
|
// non-primitive types, etc).
|
|
// `hint`: optional declared-type AST passed by the caller (let
|
|
// target, assign target). nil = "no hint, derive from self". Threaded
|
|
// for use by A.6.1's STRUCTLIT/ARRLIT arms which can't self-type and
|
|
// need the enclosing declared type to resolve. Ignored by every arm
|
|
// in A.6.0; the param is plumbed here so the per-kind work that
|
|
// follows doesn't ripple a fresh signature change. Mirrors harec's
|
|
// `check_expression(..., result_type, ...)` per
|
|
// `feedback_hare_frontend_reference.md`.
|
|
fn exprtype(c: *checker, e: *node, hint: *node) *node = {
|
|
if (e == nil) { return nil; };
|
|
let k: nkind = e.kind;
|
|
// #61 audit §1.8 — A.2 widens A.1's single N_INTLIT population to
|
|
// every primitive literal arm + N_IDENT. Cgen size walkers
|
|
// (slotsize first; elemsize/fieldsize/letemitsize follow) consult
|
|
// node.type_ as the SSoT; populating literals + idents closes the
|
|
// loop from the read side.
|
|
if (k == nkind.N_INTLIT) {
|
|
let tn: *node = mktname(c, "untyped_int");
|
|
e.type_ = tinfofornode(c, tn): *void;
|
|
return tn;
|
|
};
|
|
if (k == nkind.N_FLOATLIT) {
|
|
let tn: *node = mktname(c, "untyped_float");
|
|
e.type_ = tinfofornode(c, tn): *void;
|
|
return tn;
|
|
};
|
|
if (k == nkind.N_STRLIT) {
|
|
let tn: *node = mktname(c, "str");
|
|
e.type_ = tinfofornode(c, tn): *void;
|
|
return tn;
|
|
};
|
|
if (k == nkind.N_RUNELIT) {
|
|
let tn: *node = mktname(c, "rune");
|
|
e.type_ = tinfofornode(c, tn): *void;
|
|
return tn;
|
|
};
|
|
if (k == nkind.N_TRUE) {
|
|
let tn: *node = mktname(c, "bool");
|
|
e.type_ = tinfofornode(c, tn): *void;
|
|
return tn;
|
|
};
|
|
if (k == nkind.N_FALSE) {
|
|
let tn: *node = mktname(c, "bool");
|
|
e.type_ = tinfofornode(c, tn): *void;
|
|
return tn;
|
|
};
|
|
if (k == nkind.N_VOIDLIT) {
|
|
let tn: *node = mktname(c, "void");
|
|
e.type_ = tinfofornode(c, tn): *void;
|
|
return tn;
|
|
};
|
|
if (k == nkind.N_NIL) {
|
|
let tn: *node = mktname(c, "untyped_nil");
|
|
e.type_ = tinfofornode(c, tn): *void;
|
|
return tn;
|
|
};
|
|
if (k == nkind.N_IDENT) {
|
|
let s: *sym = scopelookup(c.cur, e.str);
|
|
if (s == nil) { return nil; };
|
|
if (s.decl == nil) { return nil; };
|
|
let t: *node = s.decl.lhs;
|
|
// Propagate the declared type's tinfo onto the use site so
|
|
// downstream cgen walkers can read n.type_ off an ident.
|
|
if (t != nil) {
|
|
if (t.type_ != nil) {
|
|
e.type_ = t.type_;
|
|
} else {
|
|
let ti: *tinfo = tinfofornode(c, t);
|
|
if (ti != nil) {
|
|
e.type_ = ti: *void;
|
|
t.type_ = ti: *void;
|
|
};
|
|
};
|
|
};
|
|
return t;
|
|
};
|
|
if (k == nkind.N_BIN) {
|
|
let tn: *node = binoptype(c, e);
|
|
e.type_ = tinfofornode(c, tn): *void;
|
|
return tn;
|
|
};
|
|
if (k == nkind.N_UN) {
|
|
let tn: *node = unoptype(c, e);
|
|
e.type_ = tinfofornode(c, tn): *void;
|
|
return tn;
|
|
};
|
|
if (k == nkind.N_INDEX) {
|
|
let tn: *node = indexresult(c, e);
|
|
e.type_ = tinfofornode(c, tn): *void;
|
|
return tn;
|
|
};
|
|
if (k == nkind.N_CAST) {
|
|
// `expr: T` — explicit cast; the type expr is e.rhs. Mirrors
|
|
// cstage cmd/wcc/check.c:737 `n->type = resolve_type(c, n->rhs)`.
|
|
e.type_ = tinfofornode(c, e.rhs): *void;
|
|
return e.rhs;
|
|
};
|
|
if (k == nkind.N_CALL) {
|
|
let callee: *node = e.lhs;
|
|
if (callee == nil) { return nil; };
|
|
// #31: synthesize the `alloc(value)` / `alloc([], n)` builtin
|
|
// return shape so checkletassign sees the same `(*T | nomem)` /
|
|
// `([]T | nomem)` cstage's check.c stamps at L981-1006. Without
|
|
// this, exprtype returns the seeded decl's nil lhs and the let
|
|
// silently accepts `let p: *T = alloc(v);` — rule 10 trap.
|
|
// Same-module gate mirrors cstage's `c->cur_mod &&
|
|
// scope_lookup_in_module(...)` check from task #23.
|
|
if (callee.kind == nkind.N_IDENT) {
|
|
if (streq(callee.str, "alloc")) {
|
|
let shadowed: bool = false;
|
|
if (c.curmod.len > 0) {
|
|
if (scopelookupinmodule(c.cur, c.curmod, "alloc") != nil) {
|
|
shadowed = true;
|
|
};
|
|
};
|
|
if (!shadowed) {
|
|
if (e.list != nil) {
|
|
// Slice form: `alloc([], n)`.
|
|
if (e.list.kind == nkind.N_ARRLIT) {
|
|
if (e.list.list == nil) {
|
|
if (e.list.next != nil) {
|
|
if (e.list.next.next == nil) {
|
|
let sl: *node = newnode(nkind.N_TSLICE, "", 0, 0);
|
|
sl.lhs = mktname(c, "u8");
|
|
let nome: *node = mktname(c, "nomem");
|
|
sl.next = nome;
|
|
let tt: *node = newnode(nkind.N_TTAGGED, "", 0, 0);
|
|
tt.list = sl;
|
|
e.type_ = tinfofornode(c, tt): *void;
|
|
return tt;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
// Value form: `alloc(value)`.
|
|
if (e.list.next == nil) {
|
|
let argt: *node = exprtype(c, e.list, nil);
|
|
let ptr: *node = newnode(nkind.N_TPTR, "", 0, 0);
|
|
ptr.lhs = argt;
|
|
let nome: *node = mktname(c, "nomem");
|
|
ptr.next = nome;
|
|
let tt: *node = newnode(nkind.N_TTAGGED, "", 0, 0);
|
|
tt.list = ptr;
|
|
e.type_ = tinfofornode(c, tt): *void;
|
|
return tt;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
};
|
|
// #42: size(T) / align(T) / offset(e.f) typed-builtin intercepts.
|
|
// Fold the N_CALL in place to an N_INTLIT so cgen never sees an
|
|
// unresolved size/align/offset symbol. Same-module shadow gate
|
|
// mirrors the alloc precedent (#23) so a user `fn size(...)`
|
|
// inside this module suppresses the builtin. Mirrors cstage
|
|
// cmd/wcc/check.c:907-960.
|
|
if (callee.kind == nkind.N_IDENT) {
|
|
let bname: str = callee.str;
|
|
let issize: bool = streq(bname, "size");
|
|
let isalign: bool = streq(bname, "align");
|
|
let isoffset: bool = streq(bname, "offset");
|
|
if (issize || isalign || isoffset) {
|
|
let shadowed: bool = false;
|
|
if (c.curmod.len > 0) {
|
|
if (scopelookupinmodule(c.cur, c.curmod, bname) != nil) {
|
|
shadowed = true;
|
|
};
|
|
};
|
|
if (!shadowed) {
|
|
if (e.list != nil) {
|
|
// Post-fold the node IS an N_INTLIT-shaped
|
|
// untyped_int constant. Mirrors cstage
|
|
// cmd/wcc/check.c:926/958 which stamps
|
|
// ty_untyped_int after the fold. The return
|
|
// tnode mktname("i32") is the assignability
|
|
// target for callers, not the constant's
|
|
// own type.
|
|
let utn: *node = mktname(c, "untyped_int");
|
|
if (issize) {
|
|
let v: i64 = astsize(c, e.list);
|
|
foldtointlit(c, e, v);
|
|
e.type_ = tinfofornode(c, utn): *void;
|
|
return mktname(c, "i32");
|
|
};
|
|
if (isalign) {
|
|
let v: i64 = astalign(c, e.list);
|
|
foldtointlit(c, e, v);
|
|
e.type_ = tinfofornode(c, utn): *void;
|
|
return mktname(c, "i32");
|
|
};
|
|
// offset(e.f): the arg is a value expression
|
|
// (N_DOT), parsed via parsearglist — not a
|
|
// type expression.
|
|
if (isoffset) {
|
|
if (e.list.next == nil && e.list.kind == nkind.N_DOT) {
|
|
let off: i64 = astoffset(c, e.list);
|
|
if (off < 0i64) {
|
|
os.write(2, "offset: no field '".ptr, 18u64);
|
|
os.write(2, e.list.str.ptr, e.list.str.len: u64);
|
|
os.write(2, "'\n".ptr, 2u64);
|
|
c.errs += 1;
|
|
off = 0i64;
|
|
};
|
|
foldtointlit(c, e, off);
|
|
e.type_ = tinfofornode(c, utn): *void;
|
|
return mktname(c, "i32");
|
|
};
|
|
};
|
|
};
|
|
};
|
|
};
|
|
};
|
|
let nm: str;
|
|
nm.ptr = nil; nm.len = 0;
|
|
if (callee.kind == nkind.N_IDENT) { nm = callee.str; };
|
|
if (callee.kind == nkind.N_DOT) { nm = callee.str; };
|
|
if (nm.len == 0) { return nil; };
|
|
// #56: bare-leaf N_IDENT calls go through scopelookupprefer so
|
|
// `foo()` inside module M binds to M.foo rather than another
|
|
// module's same-leaf foo at the head of the flat scope bucket.
|
|
// Mirrors cstage cexpr N_IDENT routing through
|
|
// scope_lookup_prefer with c->cur_mod. N_DOT keeps the bare
|
|
// scopelookup — its module-qualified resolution is a separate
|
|
// gap (parser stores the leaf in callee.str; mod is in
|
|
// callee.lhs.str, not consumed here yet).
|
|
let s: *sym = nil;
|
|
if (callee.kind == nkind.N_IDENT) {
|
|
s = scopelookupprefer(c.cur, c.curmod, nm);
|
|
} else {
|
|
s = scopelookup(c.cur, nm);
|
|
};
|
|
if (s == nil) { return nil; };
|
|
if (s.skind != skind.SK_FN) { return nil; };
|
|
if (s.decl == nil) { return nil; };
|
|
// fn-decl's lhs is the return-type AST node. Mirrors cstage
|
|
// cmd/wcc/check.c:984+ regular-CALL `n->type = build_fn_type(c,
|
|
// s->decl)->ret` shape.
|
|
e.type_ = tinfofornode(c, s.decl.lhs): *void;
|
|
return s.decl.lhs;
|
|
};
|
|
if (k == nkind.N_DOT) {
|
|
// A.6.1.5a — fold cases only. Mirrors cstage cmd/wcc/check.c
|
|
// :740-832. Struct field + pseudo-field (.len/.cap/.ptr) lands
|
|
// in A.6.1.5b. Wwstage has no use_alias (sym.mod disambiguates
|
|
// — see installdecl docstring at L195-207); SK_USE alone gates
|
|
// case 1. Enum-member fold here is best-effort: cgen.ww's
|
|
// enumevalmember covers N_INTLIT + N_IDENT-backref + N_BIN/N_UN,
|
|
// but every selfhost+lib enum body uses explicit integer
|
|
// literals (audited 2026-05-21), so the check-side eval stays
|
|
// N_INTLIT + auto-increment to avoid duplicating cgen's full
|
|
// const-folder. A non-literal member's lhs makes the auto-
|
|
// increment prev-tracking unsafe (we'd fold subsequent members
|
|
// off a wrong base), so we bail rather than guess — cgen's own
|
|
// N_DOT enum path still resolves the constant.
|
|
let lhsn: *node = e.lhs;
|
|
if (lhsn != nil) { if (lhsn.kind == nkind.N_IDENT) {
|
|
let ms: *sym = scopelookupprefer(c.cur, c.curmod, lhsn.str);
|
|
if (ms != nil) {
|
|
// Fold case 1: module-qualified ref. Mirror cstage
|
|
// check.c:749-775. cstage returns ty_err on SK_USE
|
|
// with missing leaf (extern decl); wwstage falls
|
|
// through to outer case — cgen has its own module-
|
|
// qualified resolution and the lenient checker policy
|
|
// keeps the silent miss documented at scruttype L656.
|
|
if (ms.skind == skind.SK_USE) {
|
|
let fs: *sym = scopelookupinmodule(c.cur, lhsn.str, e.str);
|
|
if (fs != nil) { if (fs.decl != nil) {
|
|
let tn: *node = fs.decl.lhs;
|
|
if (tn != nil) {
|
|
e.type_ = tinfofornode(c, tn): *void;
|
|
return tn;
|
|
};
|
|
}; };
|
|
};
|
|
// Fold case 2 inner: bare `EnumT.MEMBER` where EnumT
|
|
// is an SK_TYPE in the flat scope. Mirror cstage
|
|
// check.c:780-803.
|
|
if (ms.skind == skind.SK_TYPE) { if (ms.decl != nil) {
|
|
let body: *node = ms.decl.lhs;
|
|
let ub: *node = resolvealias(c, unwrapbang(body));
|
|
if (ub != nil) { if (ub.kind == nkind.N_TENUM) {
|
|
let prev: u64 = (-1i64): u64;
|
|
let m: *node = ub.list;
|
|
for (m != nil) {
|
|
let val: u64 = 0u64;
|
|
if (m.lhs == nil) {
|
|
val = prev + 1u64;
|
|
} else { if (m.lhs.kind == nkind.N_INTLIT) {
|
|
val = m.lhs.uval;
|
|
} else {
|
|
return nil;
|
|
}; };
|
|
prev = val;
|
|
if (streq(m.str, e.str)) {
|
|
foldtointlit(c, e, val: i64);
|
|
e.type_ = tinfofornode(c, body): *void;
|
|
return body;
|
|
};
|
|
m = m.next;
|
|
};
|
|
}; };
|
|
}; };
|
|
};
|
|
}; };
|
|
// Fold case 2 outer: base resolves to enum, e.g.
|
|
// `pkg.EnumT.MEMBER` where the inner N_DOT (pkg.EnumT) folded
|
|
// via case 1 above to the enum body. Mirror cstage
|
|
// check.c:805-832. Peel one TPTR for `(*EnumT).MEMBER` (rare
|
|
// but cstage handles it at L808).
|
|
let basetn: *node = exprtype(c, lhsn, nil);
|
|
if (basetn != nil) {
|
|
let bu: *node = resolvealias(c, unwrapbang(basetn));
|
|
if (bu != nil) { if (bu.kind == nkind.N_TPTR) {
|
|
bu = resolvealias(c, unwrapbang(bu.lhs));
|
|
}; };
|
|
if (bu != nil) { if (bu.kind == nkind.N_TENUM) {
|
|
let prev: u64 = (-1i64): u64;
|
|
let m: *node = bu.list;
|
|
for (m != nil) {
|
|
let val: u64 = 0u64;
|
|
if (m.lhs == nil) {
|
|
val = prev + 1u64;
|
|
} else { if (m.lhs.kind == nkind.N_INTLIT) {
|
|
val = m.lhs.uval;
|
|
} else {
|
|
return nil;
|
|
}; };
|
|
prev = val;
|
|
if (streq(m.str, e.str)) {
|
|
foldtointlit(c, e, val: i64);
|
|
e.type_ = tinfofornode(c, basetn): *void;
|
|
return basetn;
|
|
};
|
|
m = m.next;
|
|
};
|
|
}; };
|
|
// A.6.1.5b stamp cases — mirror cstage check.c:833-866. Pure
|
|
// type-AST stamps; never rewrite e.kind. Lenient on misses
|
|
// (cstage errors); falls through to nil under scruttype L656.
|
|
//
|
|
// Pseudo-fields .len/.cap/.ptr on slice/str/array. Cstage
|
|
// L833-842. `str` lives as N_TNAME("str") in wwstage — no
|
|
// dedicated N_TSTR kind — so test the trio shape here.
|
|
if (bu != nil) {
|
|
let isstr: bool = (bu.kind == nkind.N_TNAME) && streq(bu.str, "str");
|
|
if (bu.kind == nkind.N_TSLICE || bu.kind == nkind.N_TARRAY || isstr) {
|
|
if (streq(e.str, "len")) {
|
|
let tn: *node = mktname(c, "i32");
|
|
e.type_ = tinfofornode(c, tn): *void;
|
|
return tn;
|
|
};
|
|
if (streq(e.str, "cap")) {
|
|
let tn: *node = mktname(c, "i32");
|
|
e.type_ = tinfofornode(c, tn): *void;
|
|
return tn;
|
|
};
|
|
if (streq(e.str, "ptr")) {
|
|
let elem: *node = bu.lhs;
|
|
if (isstr) { elem = mktname(c, "u8"); };
|
|
let pp: *node = newnode(nkind.N_TPTR, "", 0, 0);
|
|
pp.lhs = elem;
|
|
e.type_ = tinfofornode(c, pp): *void;
|
|
return pp;
|
|
};
|
|
};
|
|
};
|
|
// Struct field walk. Cstage L843-849 errors on missing field.
|
|
if (bu != nil) { if (bu.kind == nkind.N_TSTRUCT) {
|
|
let f: *node = bu.list;
|
|
for (f != nil) {
|
|
if (f.kind == nkind.N_TFIELD) { if (streq(f.str, e.str)) {
|
|
e.type_ = tinfofornode(c, f.lhs): *void;
|
|
return f.lhs;
|
|
}; };
|
|
f = f.next;
|
|
};
|
|
}; };
|
|
// Tuple positional access `t.0`, `t.1`, …. Cstage L850-866
|
|
// errors on non-numeric / out-of-range; wwstage falls
|
|
// through. fldnumidx (cgenutil) returns -1 on non-digit.
|
|
if (bu != nil) { if (bu.kind == nkind.N_TTUPLE) {
|
|
let idx: i32 = fldnumidx(e.str);
|
|
if (idx >= 0) {
|
|
let p: *node = bu.list;
|
|
for (idx > 0 && p != nil) {
|
|
p = p.next;
|
|
idx -= 1;
|
|
};
|
|
if (p != nil) {
|
|
let pt: *node = p.lhs;
|
|
e.type_ = tinfofornode(c, pt): *void;
|
|
return pt;
|
|
};
|
|
};
|
|
}; };
|
|
};
|
|
return nil;
|
|
};
|
|
if (k == nkind.N_STRUCTLIT) {
|
|
// A.6.1.6 — head-only stamp of the struct-lit's overall type.
|
|
// Mirror cstage cmd/wcc/check.c:1161-1197; field-level walk
|
|
// (cstage L1178-1194) parked behind #23 / Phase 2 — field
|
|
// values are walked by the post-order exprtype dispatch at
|
|
// L460-489, so each field expr still gets its own n.type_.
|
|
//
|
|
// Parser at lib/ww/parse/expr.ww:147-148 always plants the
|
|
// TYPE_IDENT in e.lhs; e.lhs == nil would be a future Hare-
|
|
// style anonymous struct lit we don't yet parse — bail.
|
|
if (e.lhs == nil) { return nil; };
|
|
if (e.lhs.kind == nkind.N_IDENT) {
|
|
let ms: *sym = scopelookupprefer(c.cur, c.curmod, e.lhs.str);
|
|
if (ms != nil) { if (ms.skind == skind.SK_TYPE) { if (ms.decl != nil) {
|
|
let tn: *node = ms.decl.lhs;
|
|
if (tn != nil) {
|
|
e.type_ = tinfofornode(c, tn): *void;
|
|
return tn;
|
|
};
|
|
}; }; };
|
|
// Lenient on miss: cstage L1170 errors, wwstage falls
|
|
// through (scruttype L656 / A.6.1.5b N_DOT struct-miss).
|
|
return nil;
|
|
};
|
|
// Synthetic type-expr (`(*T){...}` etc). Mirror cstage L1175
|
|
// resolve_type(c, n->lhs).
|
|
e.type_ = tinfofornode(c, e.lhs): *void;
|
|
return e.lhs;
|
|
};
|
|
if (k == nkind.N_ARRLIT) {
|
|
// A.6.1.7 — head-only stamp of the array-lit's overall type.
|
|
// Mirror cstage cmd/wcc/check.c:1198-1211: walk elements,
|
|
// skip the `...` repeat sentinel (parse/expr.ww:101-105),
|
|
// first-element-wins for the elem type, count non-skipped
|
|
// elements, synthesize an N_TARRAY{elt, INTLIT count}. Empty
|
|
// list defaults to `[0]i32` per cstage L1209. Per-element
|
|
// stamps still fire via the post-order dispatch at L460-489
|
|
// (N_ARRLIT is in the kind list since A.6.0); the re-walk in
|
|
// the loop below is tinfocache-idempotent (L467).
|
|
//
|
|
// Documented cstage divergence: cstage L1206 applies
|
|
// type_default to lift untyped_int → i32 etc; wwstage stamps
|
|
// the raw exprtype result, matching the alloc-value-form
|
|
// precedent at L1509. Consumers default-type via the declared
|
|
// `let` slot until A.6.3 lands. Mixed-type elements follow
|
|
// cstage first-element-wins; no unify check (future scope).
|
|
let elt: *node = nil;
|
|
let count: u64 = 0u64;
|
|
let it: *node = e.list;
|
|
for (it != nil) {
|
|
let skip: bool = false;
|
|
if (it.kind == nkind.N_FIELD) {
|
|
if (streq(it.str, "...")) { skip = true; };
|
|
};
|
|
if (!skip) {
|
|
let t: *node = exprtype(c, it, nil);
|
|
if (elt == nil) { elt = t; };
|
|
count += 1u64;
|
|
};
|
|
it = it.next;
|
|
};
|
|
if (elt == nil) { elt = mktname(c, "i32"); };
|
|
let arr: *node = newnode(nkind.N_TARRAY, "", 0, 0);
|
|
arr.lhs = elt;
|
|
let cn: *node = newnode(nkind.N_INTLIT, "", 0, 0);
|
|
cn.uval = count;
|
|
arr.rhs = cn;
|
|
e.type_ = tinfofornode(c, arr): *void;
|
|
return arr;
|
|
};
|
|
if (k == nkind.N_SLICE) {
|
|
// A.6.2.0a — head-only stamp of the slice expression's overall
|
|
// type. Mirrors cstage cmd/wcc/check.c:1214-1228 N_SLICE: peel
|
|
// alias on the base; [N]T → []T, []T → []T (return base), str
|
|
// → str, *T (non-nil sub) → []T. Slice bounds (e.rhs start,
|
|
// e.cond end) are already covered by the post-order dispatch
|
|
// at L460-489 (typically N_INTLIT/N_IDENT/N_BIN, all in the
|
|
// dispatch list), so we do not double-walk them here.
|
|
// Documented cstage divergence: cstage L1227 errors on a
|
|
// non-sliceable base; wwstage returns nil (lenient on miss),
|
|
// matching scruttype L656 / A.6.1.5b N_DOT precedent.
|
|
let basetn: *node = exprtype(c, e.lhs, nil);
|
|
let bu: *node = resolvealias(c, unwrapbang(basetn));
|
|
if (bu == nil) { return nil; };
|
|
if (bu.kind == nkind.N_TARRAY) {
|
|
let sl: *node = newnode(nkind.N_TSLICE, "", 0, 0);
|
|
sl.lhs = bu.lhs;
|
|
e.type_ = tinfofornode(c, sl): *void;
|
|
return sl;
|
|
};
|
|
if (bu.kind == nkind.N_TSLICE) {
|
|
e.type_ = tinfofornode(c, basetn): *void;
|
|
return basetn;
|
|
};
|
|
if (bu.kind == nkind.N_TNAME) {
|
|
if (streq(bu.str, "str")) {
|
|
let tn: *node = mktname(c, "str");
|
|
e.type_ = tinfofornode(c, tn): *void;
|
|
return tn;
|
|
};
|
|
};
|
|
if (bu.kind == nkind.N_TPTR && bu.lhs != nil) {
|
|
let sl: *node = newnode(nkind.N_TSLICE, "", 0, 0);
|
|
sl.lhs = bu.lhs;
|
|
e.type_ = tinfofornode(c, sl): *void;
|
|
return sl;
|
|
};
|
|
return nil;
|
|
};
|
|
if (k == nkind.N_TRYPROP) {
|
|
// success unwrap: the success-variant type of operand's
|
|
// tagged union.
|
|
let opt: *node = exprtype(c, e.lhs, nil);
|
|
let ou: *node = resolvealias(c, unwrapbang(opt));
|
|
if (ou == nil) { return nil; };
|
|
if (ou.kind != nkind.N_TTAGGED) { return nil; };
|
|
// Hare semantics: success = first non-error variant if
|
|
// any !-flag is present; else first variant.
|
|
if (taggedhaserr(c, ou)) {
|
|
let v: *node = ou.list;
|
|
for (v != nil) {
|
|
if (!iserrvariant(c, ou, v)) {
|
|
e.type_ = tinfofornode(c, v): *void;
|
|
return v;
|
|
};
|
|
v = v.next;
|
|
};
|
|
return nil;
|
|
};
|
|
e.type_ = tinfofornode(c, ou.list): *void;
|
|
return ou.list;
|
|
};
|
|
if (k == nkind.N_TRYUNW) {
|
|
// `e!` abort-on-error unwrap; success variant is what the
|
|
// receiver gets, identical to `?` shape modulo control flow.
|
|
// #31: required so `let p: *T = alloc(v)!;` resolves to *T.
|
|
let opt: *node = exprtype(c, e.lhs, nil);
|
|
let ou: *node = resolvealias(c, unwrapbang(opt));
|
|
if (ou == nil) { return nil; };
|
|
if (ou.kind != nkind.N_TTAGGED) { return nil; };
|
|
if (taggedhaserr(c, ou)) {
|
|
let v: *node = ou.list;
|
|
for (v != nil) {
|
|
if (!iserrvariant(c, ou, v)) {
|
|
e.type_ = tinfofornode(c, v): *void;
|
|
return v;
|
|
};
|
|
v = v.next;
|
|
};
|
|
return nil;
|
|
};
|
|
e.type_ = tinfofornode(c, ou.list): *void;
|
|
return ou.list;
|
|
};
|
|
if (k == nkind.N_TYPEASSERT) {
|
|
// `e as T` → T. Mirrors cstage cmd/wcc/check.c TYPEASSERT
|
|
// `n->type = resolve_type(c, n->rhs)`.
|
|
e.type_ = tinfofornode(c, e.rhs): *void;
|
|
return e.rhs;
|
|
};
|
|
if (k == nkind.N_TYPETEST) {
|
|
// `e is T` → bool
|
|
let tn: *node = mktname(c, "bool");
|
|
e.type_ = tinfofornode(c, tn): *void;
|
|
return tn;
|
|
};
|
|
return nil;
|
|
};
|
|
|
|
// isuntypedint / is_str_like / is_bool_like — helpers used
|
|
// by the assignability check below to allow common AST shapes
|
|
// through without needing real type inference.
|
|
fn isuntypedint(t: *node) bool = {
|
|
if (t == nil) { return false; };
|
|
if (t.kind != nkind.N_TNAME) { return false; };
|
|
return streq(t.str, "untyped_int");
|
|
};
|
|
|
|
fn isuntypedfloat(t: *node) bool = {
|
|
if (t == nil) { return false; };
|
|
if (t.kind != nkind.N_TNAME) { return false; };
|
|
return streq(t.str, "untyped_float");
|
|
};
|
|
|
|
fn isuntypednil(t: *node) bool = {
|
|
if (t == nil) { return false; };
|
|
if (t.kind != nkind.N_TNAME) { return false; };
|
|
return streq(t.str, "untyped_nil");
|
|
};
|
|
|
|
// isinttypeast — int-typed AST node. Either a primitive int name
|
|
// (i8..i64/u8..u64/int/uint/uintptr/rune) or an N_TENUM. Floats are
|
|
// excluded so the enum↔int reinterpret in checkisas (#52) refuses a
|
|
// surprise `enum as f64` shape. Mirrors cstage's type_isint
|
|
// (cmd/wcc/type.c) restricted to the kinds reachable from AST.
|
|
fn isinttypeast(t: *node) bool = {
|
|
if (t == nil) { return false; };
|
|
if (t.kind == nkind.N_TENUM) { return true; };
|
|
if (t.kind != nkind.N_TNAME) { return false; };
|
|
let s: str = t.str;
|
|
if (streq(s, "i8")) { return true; };
|
|
if (streq(s, "i16")) { return true; };
|
|
if (streq(s, "i32")) { return true; };
|
|
if (streq(s, "i64")) { return true; };
|
|
if (streq(s, "u8")) { return true; };
|
|
if (streq(s, "u16")) { return true; };
|
|
if (streq(s, "u32")) { return true; };
|
|
if (streq(s, "u64")) { return true; };
|
|
if (streq(s, "int")) { return true; };
|
|
if (streq(s, "uint")) { return true; };
|
|
if (streq(s, "uintptr")) { return true; };
|
|
if (streq(s, "rune")) { return true; };
|
|
return false;
|
|
};
|
|
|
|
fn isnumerictname(t: *node) bool = {
|
|
if (t == nil) { return false; };
|
|
if (t.kind != nkind.N_TNAME) { return false; };
|
|
let s: str = t.str;
|
|
if (streq(s, "i8")) { return true; };
|
|
if (streq(s, "i16")) { return true; };
|
|
if (streq(s, "i32")) { return true; };
|
|
if (streq(s, "i64")) { return true; };
|
|
if (streq(s, "u8")) { return true; };
|
|
if (streq(s, "u16")) { return true; };
|
|
if (streq(s, "u32")) { return true; };
|
|
if (streq(s, "u64")) { return true; };
|
|
if (streq(s, "int")) { return true; };
|
|
if (streq(s, "uint")) { return true; };
|
|
if (streq(s, "uintptr")) { return true; };
|
|
if (streq(s, "rune")) { return true; };
|
|
if (streq(s, "f32")) { return true; };
|
|
if (streq(s, "f64")) { return true; };
|
|
return false;
|
|
};
|
|
|
|
fn isstrtname(t: *node) bool = {
|
|
if (t == nil) { return false; };
|
|
if (t.kind != nkind.N_TNAME) { return false; };
|
|
return streq(t.str, "str");
|
|
};
|
|
|
|
// isassignable — AST-level approximation of C check.c
|
|
// type_assignable. Returns true when we know the assignment is
|
|
// OK, false only when we're confident it isn't, and "skip" (true)
|
|
// when we can't tell — to avoid false positives. The trailing bool
|
|
// `confident` lets the caller decide whether to emit an error
|
|
// when the result is false: if !confident, the caller should not
|
|
// flag it.
|
|
fn isassignable(c: *checker, dst: *node, src: *node, confident: *bool) bool = {
|
|
*confident = false;
|
|
if (dst == nil) { return true; }; // no declared target
|
|
if (src == nil) { return true; }; // unknown src type
|
|
*confident = true;
|
|
let du: *node = resolvealias(c, unwrapbang(dst));
|
|
let su: *node = resolvealias(c, unwrapbang(src));
|
|
if (du == nil) { *confident = false; return true; };
|
|
if (su == nil) { *confident = false; return true; };
|
|
if (typeeqast(du, su)) { return true; };
|
|
// untyped numeric → any numeric named type.
|
|
if (isuntypedint(su)) {
|
|
if (isnumerictname(du)) { return true; };
|
|
// (T | ...) tagged: only OK if some variant accepts untyped_int.
|
|
if (du.kind == nkind.N_TTAGGED) {
|
|
let v: *node = du.list;
|
|
for (v != nil) {
|
|
let vu: *node = resolvealias(c, unwrapbang(v));
|
|
if (vu != nil) {
|
|
if (isnumerictname(vu)) { return true; };
|
|
};
|
|
v = v.next;
|
|
};
|
|
*confident = false;
|
|
return true;
|
|
};
|
|
// Known non-numeric primitive: confidently wrong.
|
|
if (du.kind == nkind.N_TNAME) {
|
|
if (streq(du.str, "bool")) { return false; };
|
|
if (streq(du.str, "void")) { return false; };
|
|
if (streq(du.str, "str")) { return false; };
|
|
};
|
|
// Unknown shapes: stay quiet.
|
|
*confident = false;
|
|
return true;
|
|
};
|
|
if (isuntypedfloat(su)) {
|
|
if (isnumerictname(du)) { return true; };
|
|
if (du.kind == nkind.N_TNAME) {
|
|
if (streq(du.str, "bool")) { return false; };
|
|
if (streq(du.str, "void")) { return false; };
|
|
if (streq(du.str, "str")) { return false; };
|
|
};
|
|
*confident = false;
|
|
return true;
|
|
};
|
|
if (isuntypednil(su)) {
|
|
// nil → ptr/slice/chan/fn/nullable
|
|
if (du.kind == nkind.N_TPTR) { return true; };
|
|
if (du.kind == nkind.N_TSLICE) { return true; };
|
|
if (du.kind == nkind.N_TCHAN) { return true; };
|
|
if (du.kind == nkind.N_TFN) { return true; };
|
|
// nullable `(*T | void)` — already accepted by typeeqast
|
|
// when matched whole; nil is OK there too.
|
|
if (du.kind == nkind.N_TTAGGED) {
|
|
let v: *node = du.list;
|
|
for (v != nil) {
|
|
if (v.kind == nkind.N_TPTR) { return true; };
|
|
if (v.kind == nkind.N_TSLICE){ return true; };
|
|
v = v.next;
|
|
};
|
|
};
|
|
*confident = false;
|
|
return true;
|
|
};
|
|
// Tagged-union variant inclusion: src is one of dst's variants.
|
|
if (du.kind == nkind.N_TTAGGED && su.kind != nkind.N_TTAGGED) {
|
|
// #55: nominal-first compare. Cstage variant_match
|
|
// (cmd/wcc/check.c:90-100) takes NAMED types as
|
|
// pointer-identical, so two `err`s match before bodies
|
|
// are resolved. Wwstage's typeeqast already does
|
|
// string-nominal on N_TNAME, but pre-fix this branch
|
|
// resolvealias-d both v and src to their bodies (e.g.
|
|
// N_TSTRUCT), and typeeqast's conservative struct arm
|
|
// returned false — `return e;` inside `fn f() (void | err)`
|
|
// got flagged. Compare surface forms first; fall through
|
|
// to resolved compare only when the surface mismatches
|
|
// (covers structurally-anonymous variant cases that
|
|
// resolvealias actually disambiguates). Residual: bare-vs-
|
|
// qualified TNAME (`(void | modM.err)` variant vs bare `err`
|
|
// inside modM) still misses both arms — filed as #57.
|
|
let srcraw: *node = unwrapbang(src);
|
|
let v: *node = du.list;
|
|
for (v != nil) {
|
|
let vraw: *node = unwrapbang(v);
|
|
if (typeeqast(vraw, srcraw)) { return true; };
|
|
let vu: *node = resolvealias(c, vraw);
|
|
if (vu != nil) {
|
|
if (typeeqast(vu, su)) { return true; };
|
|
};
|
|
v = v.next;
|
|
};
|
|
return false;
|
|
};
|
|
// tagged → tagged: structural variant list compare. Skip
|
|
// (don't be confident) — common when forwarding a fallible
|
|
// return through another fn with the same shape but possibly
|
|
// a different surface spelling.
|
|
if (du.kind == nkind.N_TTAGGED && su.kind == nkind.N_TTAGGED) {
|
|
*confident = false;
|
|
return true;
|
|
};
|
|
// tagged → non-tagged: requires `?` / `!` / match to project a
|
|
// variant. #31: this is what traps `let p: *T = alloc(v);`
|
|
// where the builtin returns `(*T | nomem)` and the LHS is bare.
|
|
if (su.kind == nkind.N_TTAGGED && du.kind != nkind.N_TTAGGED) {
|
|
return false;
|
|
};
|
|
// Two known primitives with different names are confidently
|
|
// incompatible. `i32 ↔ bool`, `str ↔ i32`, etc.
|
|
if (du.kind == nkind.N_TNAME && su.kind == nkind.N_TNAME) {
|
|
let known_d: bool = isnumerictname(du) || isstrtname(du);
|
|
if (!known_d) { if (streq(du.str, "bool")) { known_d = true; }; };
|
|
if (!known_d) { if (streq(du.str, "void")) { known_d = true; }; };
|
|
let known_s: bool = isnumerictname(su) || isstrtname(su);
|
|
if (!known_s) { if (streq(su.str, "bool")) { known_s = true; }; };
|
|
if (!known_s) { if (streq(su.str, "void")) { known_s = true; }; };
|
|
if (known_d) {
|
|
if (known_s) {
|
|
// Both primitives, different names → no.
|
|
return false;
|
|
};
|
|
};
|
|
};
|
|
// Anything else: don't claim confidence.
|
|
*confident = false;
|
|
return true;
|
|
};
|
|
|
|
// ---- match exhaustiveness --------------------------------------------
|
|
//
|
|
// For every match arm, verify that every variant of the scrutinee's
|
|
// tagged-union type is handled by some case (or a default arm
|
|
// exists). Multi-pattern `case A | B =>` covers all alts.
|
|
|
|
fn casecovers(c: *checker, cs: *node, want: *node) bool = {
|
|
if (cs.lhs != nil) {
|
|
if (typeeqast(cs.lhs, want)) { return true; };
|
|
};
|
|
let alt: *node = cs.list;
|
|
for (alt != nil) {
|
|
if (typeeqast(alt, want)) { return true; };
|
|
alt = alt.next;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
fn errmatchvariant(c: *checker, n: *node, vname: *node) void = {
|
|
os.write(2, "match: variant not handled".ptr, 26u64);
|
|
if (vname != nil) {
|
|
if (vname.kind == nkind.N_TNAME) {
|
|
os.write(2, " (".ptr, 2u64);
|
|
os.write(2, vname.str.ptr, vname.str.len: u64);
|
|
os.write(2, ")".ptr, 1u64);
|
|
};
|
|
};
|
|
os.write(2, "\n".ptr, 1u64);
|
|
c.errs += 1;
|
|
};
|
|
|
|
// casevariantin — true iff `pat` (a `case T` pattern, including
|
|
// each alt of a multi-pattern) names a variant of the tagged
|
|
// union `tagged`.
|
|
fn casevariantin(tagged: *node, pat: *node) bool = {
|
|
let v: *node = tagged.list;
|
|
for (v != nil) {
|
|
if (typeeqast(v, pat)) { return true; };
|
|
v = v.next;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
fn errbadcase(c: *checker, pat: *node) void = {
|
|
os.write(2, "case: not a variant of scrutinee".ptr, 32u64);
|
|
if (pat != nil) {
|
|
if (pat.kind == nkind.N_TNAME) {
|
|
os.write(2, " (".ptr, 2u64);
|
|
os.write(2, pat.str.ptr, pat.str.len: u64);
|
|
os.write(2, ")".ptr, 1u64);
|
|
};
|
|
};
|
|
os.write(2, "\n".ptr, 1u64);
|
|
c.errs += 1;
|
|
};
|
|
|
|
fn checkmatchexhaust(c: *checker, n: *node) void = {
|
|
if (n == nil) { return; };
|
|
if (n.lhs == nil) { return; };
|
|
let st: *node = scruttype(c, n.lhs);
|
|
let u: *node = resolvealias(c, unwrapbang(st));
|
|
if (u == nil) { return; };
|
|
if (u.kind != nkind.N_TTAGGED) { return; };
|
|
// Validity: every `case T` pattern (and multi-pattern alts)
|
|
// must name a variant of u. Catches typos and dead arms that
|
|
// the dispatch would never reach.
|
|
let cs0: *node = n.list;
|
|
for (cs0 != nil) {
|
|
if (cs0.lhs != nil) {
|
|
if (!casevariantin(u, cs0.lhs)) {
|
|
errbadcase(c, cs0.lhs);
|
|
};
|
|
let alt: *node = cs0.list;
|
|
for (alt != nil) {
|
|
if (!casevariantin(u, alt)) {
|
|
errbadcase(c, alt);
|
|
};
|
|
alt = alt.next;
|
|
};
|
|
};
|
|
cs0 = cs0.next;
|
|
};
|
|
// Default arm absorbs anything; skip exhaustiveness.
|
|
let cs: *node = n.list;
|
|
for (cs != nil) {
|
|
if (cs.lhs == nil) { return; }; // default
|
|
cs = cs.next;
|
|
};
|
|
// For each variant of u, look for a covering case.
|
|
let v: *node = u.list;
|
|
for (v != nil) {
|
|
let covered: bool = false;
|
|
let cs2: *node = n.list;
|
|
for (cs2 != nil) {
|
|
if (casecovers(c, cs2, v)) {
|
|
covered = true;
|
|
cs2 = nil;
|
|
} else {
|
|
cs2 = cs2.next;
|
|
};
|
|
};
|
|
if (!covered) { errmatchvariant(c, n, v); };
|
|
v = v.next;
|
|
};
|
|
};
|
|
|
|
// ---- let init / return assignability --------------------------------
|
|
//
|
|
// AST-level approximation: when we can infer src's type and dst is
|
|
// explicitly declared, verify isassignable. We only emit an error
|
|
// when isassignable says "false with confidence." If we can't tell
|
|
// (binary ops, complex exprs we don't infer), we stay quiet — full
|
|
// type inference lives only on the C side.
|
|
|
|
fn errnotassign(c: *checker, dst: *node, src: *node, where: str) void = {
|
|
os.write(2, where.ptr, where.len: u64);
|
|
os.write(2, ": not assignable".ptr, 16u64);
|
|
if (src != nil) {
|
|
if (src.kind == nkind.N_TNAME) {
|
|
os.write(2, " (".ptr, 2u64);
|
|
os.write(2, src.str.ptr, src.str.len: u64);
|
|
os.write(2, " → ".ptr, 5u64);
|
|
if (dst != nil) {
|
|
if (dst.kind == nkind.N_TNAME) {
|
|
os.write(2, dst.str.ptr, dst.str.len: u64);
|
|
};
|
|
};
|
|
os.write(2, ")".ptr, 1u64);
|
|
};
|
|
};
|
|
os.write(2, "\n".ptr, 1u64);
|
|
c.errs += 1;
|
|
};
|
|
|
|
fn checkletassign(c: *checker, n: *node) void = {
|
|
if (n == nil) { return; };
|
|
if (n.lhs == nil) { return; }; // no declared type, nothing to check
|
|
if (n.rhs == nil) { return; }; // no init
|
|
// hint = nil for A.6.0; A.6.1 will pass n.lhs once STRUCTLIT/ARRLIT
|
|
// arms consume it. Plumbing-only at this point.
|
|
let src: *node = exprtype(c, n.rhs, nil);
|
|
if (src == nil) { return; }; // can't infer
|
|
// #45: alloc([], n) defers element type to the let-init context
|
|
// (Hare-style). exprtype's alloc-slice branch synthesizes
|
|
// ([]u8 | nomem) / []u8 (for the ?/! wrap) with no LHS context;
|
|
// when the let declares []T, retype src to []T / ([]T | nomem)
|
|
// so isassignable sees exact equality. cgenstmt cglet drives the
|
|
// element size from n.lhs already (cmd/wcc/cgenstmt.ww), so this
|
|
// stays symmetric with cstage check.c clet's parallel retype.
|
|
if (n.lhs.kind == nkind.N_TSLICE) {
|
|
let wrapped: bool = false;
|
|
let inner: *node = n.rhs;
|
|
if (inner.kind == nkind.N_TRYPROP) {
|
|
wrapped = true;
|
|
inner = inner.lhs;
|
|
} else { if (inner.kind == nkind.N_TRYUNW) {
|
|
wrapped = true;
|
|
inner = inner.lhs;
|
|
}; };
|
|
if (inner != nil && inner.kind == nkind.N_CALL) {
|
|
let callee: *node = inner.lhs;
|
|
let a0: *node = inner.list;
|
|
let a1: *node = nil;
|
|
let a2: *node = nil;
|
|
if (a0 != nil) { a1 = a0.next; };
|
|
if (a1 != nil) { a2 = a1.next; };
|
|
if (callee != nil
|
|
&& callee.kind == nkind.N_IDENT
|
|
&& streq(callee.str, "alloc")
|
|
&& a0 != nil && a0.kind == nkind.N_ARRLIT
|
|
&& a0.list == nil
|
|
&& a1 != nil && a2 == nil) {
|
|
let shadowed: bool = false;
|
|
if (c.curmod.len > 0) {
|
|
if (scopelookupinmodule(c.cur, c.curmod, "alloc") != nil) {
|
|
shadowed = true;
|
|
};
|
|
};
|
|
if (!shadowed) {
|
|
let sl: *node = newnode(nkind.N_TSLICE, "", 0, 0);
|
|
sl.lhs = n.lhs.lhs;
|
|
if (wrapped) {
|
|
src = sl;
|
|
} else {
|
|
let nome: *node = mktname(c, "nomem");
|
|
sl.next = nome;
|
|
let tt: *node = newnode(nkind.N_TTAGGED, "", 0, 0);
|
|
tt.list = sl;
|
|
src = tt;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
};
|
|
let conf: bool = false;
|
|
let ok: bool = isassignable(c, n.lhs, src, &conf);
|
|
if (!conf) { return; };
|
|
if (!ok) { errnotassign(c, n.lhs, src, "let"); };
|
|
};
|
|
|
|
fn checkretassign(c: *checker, n: *node) void = {
|
|
if (n == nil) { return; };
|
|
if (n.lhs == nil) {
|
|
// bare `return;` — OK iff fnret is void or a tagged union
|
|
// with a void variant. Skip flagging for now; cgen handles
|
|
// the void-variant tag synthesis already.
|
|
return;
|
|
};
|
|
if (c.fnret == nil) { return; };
|
|
let src: *node = exprtype(c, n.lhs, nil);
|
|
if (src == nil) { return; };
|
|
let conf: bool = false;
|
|
let ok: bool = isassignable(c, c.fnret, src, &conf);
|
|
if (!conf) { return; };
|
|
if (!ok) { errnotassign(c, c.fnret, src, "return"); };
|
|
};
|
|
|
|
// ---- is / as validity ------------------------------------------------
|
|
//
|
|
// `e is T` and `e as T` require that e's declared type be a tagged
|
|
// union and that T name one of its variants. Operates on AST type
|
|
// expressions; falls back silently when we can't determine e's
|
|
// type (matches the case-variant rule for match).
|
|
fn checkisas(c: *checker, n: *node) void = {
|
|
if (n == nil) { return; };
|
|
// e is in n.lhs (value), T is in n.rhs (type expr).
|
|
let st: *node = scruttype(c, n.lhs);
|
|
let u: *node = resolvealias(c, unwrapbang(st));
|
|
if (u == nil) { return; };
|
|
// #52: enum ↔ int reinterpret (`enum as intT` / `intT as enum`).
|
|
// Mirrors cstage cmd/wcc/check.c:1346-1357 — N_TYPEASSERT with an
|
|
// enum on either side and integer types on both reinterprets in
|
|
// the same register, no tag check involved. Returns early before
|
|
// the tagged-union gate so lib/time/instant.ww `(c as i32)` and
|
|
// the lib/os syscall casts stop false-positiving. `is` (TYPETEST)
|
|
// stays rejected on non-tagged operands — cstage cmd/wcc/check.c
|
|
// gates the bypass on N_TYPEASSERT only.
|
|
if (n.kind == nkind.N_TYPEASSERT) {
|
|
let v: *node = resolvealias(c, unwrapbang(n.rhs));
|
|
let lhsenum: bool = false;
|
|
let rhsenum: bool = false;
|
|
if (u != nil) { if (u.kind == nkind.N_TENUM) { lhsenum = true; }; };
|
|
if (v != nil) { if (v.kind == nkind.N_TENUM) { rhsenum = true; }; };
|
|
if (lhsenum || rhsenum) {
|
|
if (isinttypeast(u)) { if (isinttypeast(v)) { return; }; };
|
|
};
|
|
};
|
|
if (u.kind != nkind.N_TTAGGED) {
|
|
os.write(2, "is/as: operand is not a tagged union\n".ptr, 37u64);
|
|
c.errs += 1;
|
|
return;
|
|
};
|
|
let want: *node = n.rhs;
|
|
if (want == nil) { return; };
|
|
if (!casevariantin(u, want)) {
|
|
os.write(2, "is/as: not a variant of operand".ptr, 31u64);
|
|
if (want.kind == nkind.N_TNAME) {
|
|
os.write(2, " (".ptr, 2u64);
|
|
os.write(2, want.str.ptr, want.str.len: u64);
|
|
os.write(2, ")".ptr, 1u64);
|
|
};
|
|
os.write(2, "\n".ptr, 1u64);
|
|
c.errs += 1;
|
|
};
|
|
};
|
|
|
|
// ---- ? subset propagation --------------------------------------------
|
|
//
|
|
// For `expr?`, the operand's error subset must be a subset of the
|
|
// enclosing fn's return-type variants. Mirrors C check.c. Operand
|
|
// is nkind.N_TRYPROP; its lhs is the value-bearing expr; we look at the
|
|
// expr's *declared* type for nkind.N_IDENT/nkind.N_CALL cases.
|
|
|
|
fn exprtypeoftry(c: *checker, e: *node) *node = {
|
|
if (e == nil) { return nil; };
|
|
if (e.kind == nkind.N_IDENT) {
|
|
let s: *sym = scopelookup(c.cur, e.str);
|
|
if (s == nil) { return nil; };
|
|
if (s.decl == nil) { return nil; };
|
|
return s.decl.lhs;
|
|
};
|
|
if (e.kind == nkind.N_CALL) {
|
|
// callee return type lookup: callee is e.lhs (nkind.N_IDENT or
|
|
// nkind.N_DOT). We need the fn-decl's lhs (return-type AST).
|
|
let callee: *node = e.lhs;
|
|
if (callee == nil) { return nil; };
|
|
let nm: str;
|
|
nm.ptr = nil; nm.len = 0;
|
|
if (callee.kind == nkind.N_IDENT) { nm = callee.str; };
|
|
if (callee.kind == nkind.N_DOT) { nm = callee.str; };
|
|
if (nm.len == 0) { return nil; };
|
|
let s: *sym = scopelookup(c.cur, nm);
|
|
if (s == nil) { return nil; };
|
|
if (s.skind != skind.SK_FN) { return nil; };
|
|
if (s.decl == nil) { return nil; };
|
|
return s.decl.lhs;
|
|
};
|
|
return nil;
|
|
};
|
|
|
|
fn checktryprop(c: *checker, n: *node) void = {
|
|
if (n == nil) { return; };
|
|
let t: *node = exprtypeoftry(c, n.lhs);
|
|
let u: *node = resolvealias(c, unwrapbang(t));
|
|
if (u == nil) { return; };
|
|
if (u.kind != nkind.N_TTAGGED) { return; };
|
|
// Does the operand have any error variants?
|
|
let haserr: bool = false;
|
|
let v: *node = u.list;
|
|
for (v != nil) {
|
|
if (iserrvariant(c, u, v)) { haserr = true; };
|
|
v = v.next;
|
|
};
|
|
if (!haserr) { return; };
|
|
// Enclosing fn must return a tagged union with each operand
|
|
// error variant present.
|
|
let r: *node = resolvealias(c, unwrapbang(c.fnret));
|
|
if (r == nil) {
|
|
os.write(2, "?: enclosing fn has no tagged-union return\n".ptr, 43u64);
|
|
c.errs += 1;
|
|
return;
|
|
};
|
|
if (r.kind != nkind.N_TTAGGED) {
|
|
os.write(2, "?: enclosing fn return is not tagged\n".ptr, 37u64);
|
|
c.errs += 1;
|
|
return;
|
|
};
|
|
let ev: *node = u.list;
|
|
for (ev != nil) {
|
|
if (iserrvariant(c, u, ev)) {
|
|
let found: bool = false;
|
|
let rv: *node = r.list;
|
|
for (rv != nil) {
|
|
if (typeeqast(rv, ev)) {
|
|
found = true;
|
|
rv = nil;
|
|
} else { rv = rv.next; };
|
|
};
|
|
if (!found) {
|
|
os.write(2, "?: error variant not in enclosing return\n".ptr, 41u64);
|
|
c.errs += 1;
|
|
};
|
|
};
|
|
ev = ev.next;
|
|
};
|
|
};
|
|
|
|
// install_param — when entering a fn body, define its params in a
|
|
// fresh local scope.
|
|
//
|
|
// TODO(#11): cstage check.c (post-#32) errors `param '%s' redeclared`
|
|
// when two params share a name. The fn body's scope IS fresh here
|
|
// (resolvefnbody opens it before calling us), so guarding scopedefine's
|
|
// nil return would be sound — but we defer until #11 wires checkfile
|
|
// into w6c_ww so the diagnostic class lands as a single coordinated
|
|
// step rather than dribbling in. Matches the cstage-only neg-case
|
|
// precedent at test/wcc/708 + test/wcc/696.
|
|
fn installparams(c: *checker, params: *node) void = {
|
|
let p: *node = params;
|
|
for (p != nil) {
|
|
if (p.kind == nkind.N_PARAM) {
|
|
let nm: str = p.str;
|
|
if (nm.len > 0) {
|
|
checkmoduleshadow(c, nm, "param");
|
|
scopedefine(c.cur, nm, skind.SK_PARAM, nil, p);
|
|
};
|
|
};
|
|
p = p.next;
|
|
};
|
|
};
|
|
|
|
// resolvefnbody — open a child scope for the fn, install its params,
|
|
// then walk the body. Local lets installed by walk_stmt (a future
|
|
// extension); for the current pass we just resolve-walk without
|
|
// per-statement scopes.
|
|
fn resolvefnbody(c: *checker, fnnode: *node) void = {
|
|
let outer: *scope = c.cur;
|
|
c.cur = newscope(c.cur);
|
|
installparams(c, fnnode.list);
|
|
// #61 audit §1.8 — A.2: walk each param's declared type-expr so
|
|
// tinfofornode stamps n.type_ on it. installparams binds the name
|
|
// but never recurses into the type; without this, cgen's slotsize
|
|
// fast-path hits the fallback for every param load/store.
|
|
let p: *node = fnnode.list;
|
|
for (p != nil) {
|
|
if (p.kind == nkind.N_PARAM) {
|
|
if (p.lhs != nil) { resolvewalk(c, p.lhs); };
|
|
};
|
|
p = p.next;
|
|
};
|
|
let prevret: *node = c.fnret;
|
|
c.fnret = fnnode.lhs; // return type AST, used by `?` check
|
|
if (fnnode.body != nil) {
|
|
resolvewalk(c, fnnode.body);
|
|
};
|
|
c.fnret = prevret;
|
|
c.cur = outer;
|
|
};
|
|
|
|
export fn checkinit(c: *checker, tc: *tctx) void = {
|
|
c.tc = tc;
|
|
c.top = newscope(nil);
|
|
c.cur = c.top;
|
|
c.nresolved = 0;
|
|
c.nunresolved = 0;
|
|
c.errs = 0;
|
|
c.verbose = 0;
|
|
c.fnret = nil;
|
|
let empty: str;
|
|
c.curmod = empty;
|
|
c.file = nil;
|
|
seedprimitives(c);
|
|
};
|
|
|
|
export fn checkfile(c: *checker, file: *node) void = {
|
|
if (file == nil) { return; };
|
|
if (file.kind != nkind.N_FILE) { return; };
|
|
c.file = file;
|
|
|
|
// Pass 1: install all top-level names.
|
|
let d: *node = file.list;
|
|
for (d != nil) {
|
|
installdecl(c, file, d);
|
|
d = d.next;
|
|
};
|
|
|
|
// Pass 2: walk decl bodies/types and resolve identifiers.
|
|
// Track the per-decl module bareword so bare-leaf lookups inside
|
|
// the body prefer same-module entries over alphabetically-earlier
|
|
// same-leaf imports.
|
|
d = file.list;
|
|
for (d != nil) {
|
|
c.curmod = declmod(file, d);
|
|
let k: nkind = d.kind;
|
|
if (k == nkind.N_FNDECL) {
|
|
if (d.lhs != nil) { resolvewalk(c, d.lhs); }; // return type
|
|
resolvefnbody(c, d);
|
|
} else { if (k == nkind.N_DEF) {
|
|
if (d.lhs != nil) { resolvewalk(c, d.lhs); };
|
|
if (d.rhs != nil) { resolvewalk(c, d.rhs); };
|
|
} else { if (k == nkind.N_TYPEDECL) {
|
|
if (d.lhs != nil) { resolvewalk(c, d.lhs); };
|
|
} else { if (k == nkind.N_LET) {
|
|
if (d.lhs != nil) { resolvewalk(c, d.lhs); };
|
|
if (d.rhs != nil) { resolvewalk(c, d.rhs); };
|
|
};};};};
|
|
d = d.next;
|
|
};
|
|
let empty: str;
|
|
c.curmod = empty;
|
|
|
|
};
|