Files
ww/test/wcc/738_module_decl.c
Hojun-Cho 9e0816e199 cmd+selfhost+lib+test: directory-as-module enumeration in driver (#22)
Replace the cmd/ww + selfhost driver's file-walk import resolver
with true directory enumeration. `import encoding.utf8;` now finds
the lib/encoding/utf8/ directory and concatenates every *.ww file
in it (excluding *test.ww and the driver's *.combined.ww artifacts)
in byte-wise sorted order, instead of just finding the single
lib/encoding/utf8/utf8.ww file. Mirrors Hare's
hare/module/srcs.ha:183 _findsrcs minus tag handling.

Lookup order in both stages: (1) <dir>/<dot-as-slash>/ as directory
→ enumerate. (2) <dir>/<dot-as-slash>.ww as file. The legacy
<dir>/<name>/<name>.ww shape from #18's retained divergence is
dropped per rule-9 Hare-fidelity — Hare has no foo/foo.ha fallback;
a module IS the directory.

Symmetric across cstage (cmd/ww/main.c via opendir+qsort+stat) and
wwstage (selfhost/cmd/ww/main.ww via existing lib/os.getdents64 +
os.stat — no new lib/os surface needed; the rundirtests() walker
in main.ww from #18 was the model). Bootstrap ww2.s==ww3.s==ww4.s
byte-identical post-change.

Bundling justification (rule 11): strict-same-package validation is
bundled because the failure mode is dir-enum's own (a non-dir-enum
compilation unit cannot trigger mismatch across enumerated files).
The natural enforcement site is the driver — the parser can't
distinguish dir-enum concat from file-walk concat. Both stages
peek each file's first `package <name>;` line in expand_dir /
expanddir and exit(1) on mismatch with a precise error pointing
at the offending file. Hare's hare/module/srcs.ha:131 has the
same constraint via its README gate. Other half of #23 (strict
missing-package error tightening — 63 inline-source test wrappers
blocker) stays deferred per its filing.

Parser side (cmd/wcc/parse.c parseuse + lib/ww/parse/decl.ww
parseuse): n->str now carries only the LEAF identifier from a
dotted import. With the driver translating the full dotted path
to a directory walk, the checker only needs the package bareword
(last component) for the N_USE → decl disambiguation walk in
check.c's src_imports / decl_mod. Mirrors Hare's
`use encoding::utf8;` → `utf8::name` semantics
(ref/hare/hare/ast/import.ha:7).

Migration: lib/ww/sym.ww drops `import typ; import ast;`;
lib/ww/parse/parse.ww drops `import expr; import stmt; import
decl;`; lib/ww/lex/lex.ww drops `import tok;` — all sibling
imports auto-resolve via the new dir-enum when callers import the
package directory. lib/strings/, lib/encoding/utf8/utf8test.ww
migrate `import utf8;` → `import encoding.utf8;`. Makefile drops
-I lib/encoding/utf8 stopgap from wwdump_ww + w6c_ww. Seven test
wrappers (700_e2e, 966_strings_run, 970_fmt_run, 971_log_run,
972_fnmatch_run, 982_getopt_run, 990_selfhost) and 995_self_rebuild
drop the -I lib/encoding/utf8 runtime stopgap.

Tests: new 737_direnum C wrapper + test/wcc/data/direnum/ fixtures
pin (a) cross-pkg multi-file dir-enum build at runtime (both stages
must succeed) and (b) strict-same-package mismatch error (both
stages must surface "differs from" + exit non-zero). 738_module_decl
gains row 6 pinning the n_use->str leaf-only storage post-parser
change.

Retained workaround at selfhost/cmd/ww/main.ww expanddir loop:
`names[i][k]` nested-deref-then-index split into
`let nm: *u8 = names[i]; nm[k]` because wwstage cgen miscompiles
the chained form (treats inner u8 element as 8B sizeof *u8 instead
of 1B sizeof u8: extra MOVQ $8 + IMULQ on the inner index, MOVQ
instead of MOVZBQ load). Inline rule-8 WHY comment cites task #24
(wwstage cgen chained-index inner element size on **T). Two-step
form routes through the bare-pointer index path which both stages
handle byte-identically.

Class A wwstage cgen UNDER (chained-index inner element size on
**T) surfaced first time the codebase exercises the **T[i][k]
shape via enumeratedir() — corpus-coverage-blind landmine pattern,
same family as the trio (#27/#28/#31) from STATUS-5.

112/112 ok. ww2 == ww3 == ww4 byte-id holds.
2026-05-18 19:22:27 +09:00

128 lines
4.1 KiB
C

/*
* 738_module_decl — sentinel for the `package <name>;` keyword.
*
* Pins three invariants from the module-system rewrite:
* 1. The parser ACCEPTS `package foo;` as the first non-comment item.
* 2. The parser ACCEPTS multiple `package X;` decls (concatenated
* multi-file streams from the driver) and stamps subsequent
* decls with whichever package is "current".
* 3. Bare comments + a `package foo;` is still legal — the keyword
* may follow leading comments.
*
* The relaxed-or-strict missing-package check was softened to a
* silent default (curmod=NULL) so legacy fragment-driven tests
* still parse. That softening is documented at parsefile() in
* cmd/wcc/parse.c.
*/
#include "ww.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static int
parses_clean(const char *src)
{
Arena *a = newarena();
Lex l;
Parser p;
lexinit(&l, a, "<test>", src, strlen(src));
parserinit(&p, a, &l);
Node *n = parsefile(&p);
int ok = (n != NULL && p.errs == 0 && l.errs == 0);
freearena(a);
return ok;
}
static const char *first_decl_module(Node *file) {
if (file == NULL || file->list == NULL) return NULL;
return file->list->module;
}
static int
check_module_stamps(const char *src, const char *want_first, const char *want_last)
{
Arena *a = newarena();
Lex l;
Parser p;
lexinit(&l, a, "<test>", src, strlen(src));
parserinit(&p, a, &l);
Node *n = parsefile(&p);
int ok = (n != NULL && p.errs == 0 && l.errs == 0);
if (ok) {
const char *first = first_decl_module(n);
Node *last = n->list;
while (last && last->next) last = last->next;
const char *lastmod = last ? last->module : NULL;
int match_first = (first == NULL && want_first == NULL)
|| (first != NULL && want_first != NULL
&& strcmp(first, want_first) == 0);
int match_last = (lastmod == NULL && want_last == NULL)
|| (lastmod != NULL && want_last != NULL
&& strcmp(lastmod, want_last) == 0);
ok = match_first && match_last;
}
freearena(a);
return ok;
}
int
main(void)
{
int pass = 0, fail = 0;
/* Row 1: `package foo;` accepted as first non-comment item. */
if (parses_clean("package foo;\nfn x() void = {};\n")) pass++;
else { fprintf(stderr, "738[1] basic package accept FAILED\n"); fail++; }
/* Row 2: comments before `package foo;` legal. */
if (parses_clean("// header\n// more\npackage foo;\nfn x() void = {};\n")) pass++;
else { fprintf(stderr, "738[2] package after comments FAILED\n"); fail++; }
/* Row 3: subsequent decls stamped with current package. */
if (check_module_stamps(
"package foo;\nfn x() void = {};\nfn y() void = {};\n",
"foo", "foo")) pass++;
else { fprintf(stderr, "738[3] decl module stamping FAILED\n"); fail++; }
/* Row 4: concatenated multi-section stream — second package
* decl switches the stamp for subsequent decls. Mirrors driver-
* emitted multi-file modules. */
if (check_module_stamps(
"package foo;\nfn a() void = {};\n"
"package bar;\nfn b() void = {};\n",
"foo", "bar")) pass++;
else { fprintf(stderr, "738[4] mid-stream package switch FAILED\n"); fail++; }
/* Row 5: dotted import accepted with leaf stored on N_USE. */
if (parses_clean(
"package foo;\nimport encoding.utf8;\nfn x() void = {};\n")) pass++;
else { fprintf(stderr, "738[5] dotted import accept FAILED\n"); fail++; }
/* Row 6: dotted import stores only the leaf identifier on
* N_USE.str (post-task-#22 — the driver translates the full
* dotted path to a directory walk; the checker only needs the
* package bareword for n_use → decl disambiguation). */
{
Arena *a = newarena();
Lex l;
Parser p;
const char *src = "package foo;\nimport encoding.utf8;\n";
lexinit(&l, a, "<test>", src, strlen(src));
parserinit(&p, a, &l);
Node *n = parsefile(&p);
int ok = 0;
if (n != NULL && p.errs == 0 && l.errs == 0 && n->list != NULL) {
Node *u = n->list;
ok = (u->kind == N_USE
&& u->str != NULL
&& strcmp(u->str, "utf8") == 0);
}
freearena(a);
if (ok) pass++;
else { fprintf(stderr, "738[6] dotted import leaf-store FAILED\n"); fail++; }
}
printf("738_module_decl: %d pass, %d fail\n", pass, fail);
return fail == 0 ? 0 : 1;
}