wcc: c-side mangles private decls; main exempt as entry-point convention

This commit is contained in:
2026-05-11 15:29:29 +09:00
parent 895f221b6c
commit e301a198f4
7 changed files with 153 additions and 25 deletions

View File

@@ -94,6 +94,27 @@ skipws(Lex *l)
continue;
}
if (c == '/' && lpeek(l, 1) == '/') {
lget(l); lget(l); /* consume '//' */
/* `// MODULE: foo` directive emitted by the ww
* driver before each source-file's section in
* combined.ww. Captured so cgen can mangle
* non-exported symbols by module. */
if (lpeek(l, 0) == ' '
&& lpeek(l, 1) == 'M' && lpeek(l, 2) == 'O'
&& lpeek(l, 3) == 'D' && lpeek(l, 4) == 'U'
&& lpeek(l, 5) == 'L' && lpeek(l, 6) == 'E'
&& lpeek(l, 7) == ':' && lpeek(l, 8) == ' ') {
for (int i = 0; i < 9; i++) lget(l);
u64 start = l->pos;
while ((c = lpeek(l, 0)) >= 0
&& c != '\n' && c != '\r')
lget(l);
u64 n = l->pos - start;
char *m = amalloc(l->a, n + 1);
memcpy(m, l->src + start, n);
m[n] = '\0';
l->module = m;
}
while ((c = lpeek(l, 0)) >= 0 && c != '\n')
lget(l);
continue;

View File

@@ -1174,6 +1174,10 @@ parsefile(Parser *p)
advance(p);
continue;
}
/* Stamp the lex's current `// MODULE: foo` directive on the
* decl. cgen uses it to mangle non-exported names so two
* modules can each privately define `cstrlen`/`streq`/etc. */
if (d != NULL) d->module = p->l->module;
if (head == NULL) head = d;
else tail->next = d;
tail = d;

View File

@@ -196,6 +196,7 @@ struct Lex {
i32 col;
Arena *a; /* token-text arena */
int errs;
const char *module; /* current `// MODULE: foo` directive, or NULL */
};
void lexinit(Lex*, Arena*, const char *file, const char *src, u64 len);
@@ -303,6 +304,11 @@ struct Node {
int export;
Type *type; /* filled in by checker */
const char *tsuffix; /* typed numeric literal suffix */
const char *module; /* `// MODULE: foo` directive at the
* decl's section in combined.ww.
* NULL for nested nodes; only top-
* level decls (fn/def/type/let)
* carry it. */
};
Node *newnode(Arena*, Nkind, Pos);