Files
ww/test/wcc/989_m1mangle_run.c
Hojun-Cho f308818b4b wcc/ww: mangle imported symbols on dotted import path (#22 M1, #32)
Switch symbol mangling from the import leaf clause to the full dotted import path for directory packages; single-file imports keep package-clause mangling (isdir-gate: imported<=>directory-import). The root build unit's fn main stays bare, every other top-level decl mangles, closing #31's duplicate-main hazard by construction (#32). Both stages, byte-identical.

Single commit, not split: the bare rename (f244af3) is red on its own because it unmasks cross-module resolution gaps that do not reproduce pre-M1, so the fixes are intrinsic to making the rename correct. Included: wwstage fnret/fnparamslookupmod map import alias->path (#199b cross-module union-variant scrutinee resolved the wrong fn's union); cstage use_path prefers the referencing module's import for an ambiguous leaf alias (sha256 crypto.math vs strconv math). Tests table-driven: 989_m1mangle_run/_sym, 989_m1union_run (gate-visible per-arm exit codes + cs==ww byte-id).
2026-06-15 17:37:18 +09:00

213 lines
6.1 KiB
C

/*
* 989_m1mangle_run — M1 (#22 + #32): path-qualified symbol mangling and
* root-unit entry detection. Table-driven, both stages (rule-10: cstage
* `ww` and wwstage `ww_ww` must AGREE and hit want_exit).
*
* Each row lays out a tiny package tree next to a root `main.ww` and
* builds+runs the root. Because a directory import path-mangles its
* symbols (`aa.bb.val` etc.), a broken hint or skip rule shows up as a
* link failure (-1) or wrong runtime value — observable end-to-end.
*
* row | proves
* -----------------+--------------------------------------------------
* nested_call | DIRECTORY import path-mangles + the qualified-ref
* | hint resolves to the PATH (aa.bb.val), not the
* | leaf alias — cross-boundary call returns 42
* imported_main | #32/#31: an imported pkg's `fn main` mangles
* | (aa.bb.main) instead of colliding with the bare
* | root entry; both link, root main runs → 7
* priv_global | a module-PRIVATE value global mangles on the path
* | (aa.bb.pv) and the owning module resolves it → 9
* single_level | control: a single-level package's symbols are
* | UNCHANGED (cc.v stays cc.v) — still resolves → 5
* deep_nest | 2-level nest aa.bb.cc → aa.bb.cc.val path-mangle → 3
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
static int
runwait(const char *cmd)
{
int rc = system(cmd);
if (rc == -1) return -1;
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
return -1;
}
struct file { const char *path; const char *content; };
struct row {
const char *label;
struct file files[4]; /* {NULL,NULL}-terminated package files */
const char *root; /* root main.ww content */
int want_exit;
};
static const struct row rows[] = {
{ "nested_call",
{ { "aa/bb/bb.ww",
"package bb;\n"
"export fn val() int = { return 42; };\n" },
{ NULL, NULL } },
"package main;\n"
"import aa.bb;\n"
"export fn main() int = { return bb.val(); };\n",
42 },
{ "imported_main",
{ { "aa/bb/bb.ww",
"package bb;\n"
"fn main() int = { return 7; };\n"
"export fn run() int = { return main(); };\n" },
{ NULL, NULL } },
"package main;\n"
"import aa.bb;\n"
"export fn main() int = { return bb.run(); };\n",
7 },
{ "priv_global",
{ { "aa/bb/bb.ww",
"package bb;\n"
"let pv: int = 9;\n"
"export fn getpv() int = { return pv; };\n" },
{ NULL, NULL } },
"package main;\n"
"import aa.bb;\n"
"export fn main() int = { return bb.getpv(); };\n",
9 },
{ "single_level",
{ { "cc/cc.ww",
"package cc;\n"
"export fn v() int = { return 5; };\n" },
{ NULL, NULL } },
"package main;\n"
"import cc;\n"
"export fn main() int = { return cc.v(); };\n",
5 },
{ "deep_nest",
{ { "aa/bb/cc/cc.ww",
"package cc;\n"
"export fn val() int = { return 3; };\n" },
{ NULL, NULL } },
"package main;\n"
"import aa.bb.cc;\n"
"export fn main() int = { return cc.val(); };\n",
3 },
};
/* write_file — create `dir/rel` (mkdir -p its parents) with `content`. */
static int
write_file(const char *dir, const char *rel, const char *content)
{
char path[512], cmd[1024];
snprintf(path, sizeof path, "%s/%s", dir, rel);
/* mkdir -p the parent of `path` */
char parent[512];
snprintf(parent, sizeof parent, "%s", path);
char *slash = strrchr(parent, '/');
if (slash) {
*slash = '\0';
snprintf(cmd, sizeof cmd, "mkdir -p '%s'", parent);
if (runwait(cmd) != 0) return -1;
}
FILE *f = fopen(path, "wb");
if (!f) return -1;
fputs(content, f);
fclose(f);
return 0;
}
/* run_build — lay out the row's package tree + root under a temp dir,
* build+run the root via `driver`; return the binary's exit code, or -1
* on a build failure. */
static int
run_build(const char *driver, const struct row *r, int i)
{
char dir[64], cmd[2048];
snprintf(dir, sizeof dir, "/tmp/m1mangle_%d_%d", getpid(), i);
snprintf(cmd, sizeof cmd, "rm -rf '%s' && mkdir -p '%s'", dir, dir);
if (runwait(cmd) != 0) return -2;
for (int k = 0; r->files[k].path; k++)
if (write_file(dir, r->files[k].path, r->files[k].content) != 0)
return -2;
if (write_file(dir, "main.ww", r->root) != 0) return -2;
/* cd into the build dir so the source-dir-first search path finds the
* sibling package tree (mirrors Hare's CWD == module-dir). */
snprintf(cmd, sizeof cmd, "cd '%s' && %s build main.ww 2>/dev/null",
dir, driver);
int brc = runwait(cmd);
int got = -1;
if (brc == 0) {
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/main", dir);
got = runwait(outbin);
}
snprintf(cmd, sizeof cmd, "rm -rf '%s'", dir);
runwait(cmd);
return brc == 0 ? got : -1;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[1024];
if (bin[0] != '/') {
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
char cdrv[1024], wdrv[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
struct { const char *name; const char *drv; int gated; }
drivers[] = {
{ "cstage", cdrv, 0 },
{ "wwstage", wdrv, 1 },
{ NULL, NULL, 0 },
};
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int d = 0; drivers[d].name; d++) {
if (drivers[d].gated && access(drivers[d].drv, X_OK) != 0) {
fprintf(stderr, "m1mangle_run: skip %s (no %s)\n",
drivers[d].name, drivers[d].drv);
continue;
}
for (int i = 0; i < n; i++) {
total++;
int got = run_build(drivers[d].drv, &rows[i], i);
if (got != rows[i].want_exit) {
fprintf(stderr, "m1mangle_run[%s][%s]: exit=%d "
"want=%d\n", drivers[d].name, rows[i].label,
got, rows[i].want_exit);
fail++;
}
}
}
if (fail) {
fprintf(stderr, "m1mangle_run: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("m1mangle_run: %d/%d ok\n", total, total);
return 0;
}