w6c+selfhost: cross-module same-leaf type disambiguation via Sym.mod
This commit is contained in:
@@ -63,10 +63,11 @@ resolve_typename(Checker *c, Node *n)
|
||||
Sym *s = scope_lookup(c->cur, nm);
|
||||
if (s == NULL && nm) {
|
||||
/* module-qualified: io.stream → strip the last dot prefix
|
||||
* and look up the leaf if `io` is a `use`-imported name.
|
||||
* `m->use_alias` covers the self-import case where the
|
||||
* imported module declares a type with the same name as
|
||||
* the module itself (e.g. `random.random`). */
|
||||
* and look up the leaf, filtering on the importing module's
|
||||
* name so `bufio.stream` and `io.stream` can coexist in the
|
||||
* same flat scope. `m->use_alias` covers the self-import
|
||||
* case where the imported module declares a type with the
|
||||
* same name as the module itself (e.g. `random.random`). */
|
||||
const char *dot = strrchr(nm, '.');
|
||||
if (dot) {
|
||||
char head[128] = {0};
|
||||
@@ -74,7 +75,8 @@ resolve_typename(Checker *c, Node *n)
|
||||
if (hl < sizeof head) memcpy(head, nm, hl);
|
||||
Sym *m = scope_lookup(c->cur, head);
|
||||
if (m && (m->kind == SK_USE || m->use_alias))
|
||||
s = scope_lookup(c->cur, dot + 1);
|
||||
s = scope_lookup_in_module(c->cur, head,
|
||||
dot + 1);
|
||||
}
|
||||
}
|
||||
if (s == NULL || s->kind != SK_TYPE)
|
||||
@@ -709,11 +711,12 @@ cexpr(Checker *c, Node *n)
|
||||
* the self-import case where the module's
|
||||
* type name shadowed the SK_USE; the leaf
|
||||
* still resolves through the flat scope.
|
||||
* fs == ms is the `mod.mod` case (the
|
||||
* imported module's leaf type is named after
|
||||
* the module itself — both names point at
|
||||
* the same SK_TYPE sym in the flat scope). */
|
||||
Sym *fs = scope_lookup(c->cur, n->str);
|
||||
* Filter on the importing module name so
|
||||
* same-leaf-name types from different
|
||||
* imports (`bufio.stream`/`io.stream`)
|
||||
* disambiguate to the right one. */
|
||||
Sym *fs = scope_lookup_in_module(c->cur,
|
||||
n->lhs->str, n->str);
|
||||
if (fs)
|
||||
return n->type = fs->type;
|
||||
if (ms->kind == SK_USE) {
|
||||
@@ -1608,6 +1611,28 @@ check_init(Checker *c, Arena *a)
|
||||
c->cur = c->top;
|
||||
}
|
||||
|
||||
/*
|
||||
* decl_mod — 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 NULL so they coexist (mod=NULL)
|
||||
* with imported decls of the same leaf name in scope_lookup_in_module.
|
||||
*/
|
||||
static const char *
|
||||
decl_mod(Node *file, Node *d)
|
||||
{
|
||||
if (d == NULL || d->module == NULL || file == NULL) return NULL;
|
||||
for (Node *u = file->list; u; u = u->next) {
|
||||
if (u->kind == N_USE && u->str
|
||||
&& strcmp(u->str, d->module) == 0)
|
||||
return d->module;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
check_file(Checker *c, Node *file)
|
||||
{
|
||||
@@ -1639,6 +1664,7 @@ check_file(Checker *c, Node *file)
|
||||
if (d->kind != N_TYPEDECL) continue;
|
||||
Type *named = type_named(c->a, d->str, NULL);
|
||||
Sym *prev = scope_lookup_local(c->cur, d->str);
|
||||
const char *mod = decl_mod(file, d);
|
||||
if (prev && prev->kind == SK_USE) {
|
||||
/* `use mod; ... type mod = ...;` — promote the
|
||||
* SK_USE to the type symbol but remember it was
|
||||
@@ -1647,7 +1673,9 @@ check_file(Checker *c, Node *file)
|
||||
prev->type = named;
|
||||
prev->decl = d;
|
||||
prev->use_alias = 1;
|
||||
} else if (!scope_define(c->cur, d->str, SK_TYPE, named, d)) {
|
||||
if (mod && prev->mod == NULL) prev->mod = mod;
|
||||
} else if (!scope_define_in_module(c->cur, d->str, mod,
|
||||
SK_TYPE, named, d)) {
|
||||
err(c, d->pos, "duplicate type %s", d->str);
|
||||
}
|
||||
d->type = named;
|
||||
@@ -1672,9 +1700,12 @@ check_file(Checker *c, Node *file)
|
||||
Type *t = resolve_type(c, d->lhs);
|
||||
d->type = t;
|
||||
Sym *prev = scope_lookup_local(c->cur, d->str);
|
||||
const char *mod = decl_mod(file, d);
|
||||
if (prev && prev->kind == SK_USE) {
|
||||
prev->kind = SK_DEF; prev->type = t; prev->decl = d;
|
||||
} else if (!scope_define(c->cur, d->str, SK_DEF, t, d))
|
||||
if (mod && prev->mod == NULL) prev->mod = mod;
|
||||
} else if (!scope_define_in_module(c->cur, d->str, mod,
|
||||
SK_DEF, t, d))
|
||||
err(c, d->pos, "duplicate def %s", d->str);
|
||||
break;
|
||||
}
|
||||
@@ -1682,9 +1713,12 @@ check_file(Checker *c, Node *file)
|
||||
Type *t = build_fn_type(c, d);
|
||||
d->type = t;
|
||||
Sym *prev = scope_lookup_local(c->cur, d->str);
|
||||
const char *mod = decl_mod(file, d);
|
||||
if (prev && prev->kind == SK_USE) {
|
||||
prev->kind = SK_FN; prev->type = t; prev->decl = d;
|
||||
} else if (!scope_define(c->cur, d->str, SK_FN, t, d))
|
||||
if (mod && prev->mod == NULL) prev->mod = mod;
|
||||
} else if (!scope_define_in_module(c->cur, d->str, mod,
|
||||
SK_FN, t, d))
|
||||
err(c, d->pos, "duplicate fn %s", d->str);
|
||||
break;
|
||||
}
|
||||
@@ -1693,11 +1727,14 @@ check_file(Checker *c, Node *file)
|
||||
d->type = t;
|
||||
if (d->str && d->str[0]) {
|
||||
Sym *prev = scope_lookup_local(c->cur, d->str);
|
||||
const char *mod = decl_mod(file, d);
|
||||
if (prev && prev->kind == SK_USE) {
|
||||
prev->kind = SK_VAR; prev->type = t;
|
||||
prev->decl = d;
|
||||
if (mod && prev->mod == NULL) prev->mod = mod;
|
||||
} else
|
||||
scope_define(c->cur, d->str, SK_VAR, t, d);
|
||||
scope_define_in_module(c->cur, d->str,
|
||||
mod, SK_VAR, t, d);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1340,6 +1340,14 @@ parsefile(Parser *p)
|
||||
Node *file = newnode(p->a, N_FILE, pp);
|
||||
Node *head = NULL, *tail = NULL;
|
||||
while (p->cur.kind != TK_EOF) {
|
||||
/* Stamp the lex's current `// MODULE: foo` directive on the
|
||||
* decl BEFORE parsing. cgen uses this for name-mangling and
|
||||
* check uses it for cross-module type disambiguation. Capture
|
||||
* before parsing so the closing `expect(SEMI)` doesn't
|
||||
* accidentally advance the lexer past the *next* `// MODULE:`
|
||||
* directive — that would stamp this decl with the next
|
||||
* module's name. */
|
||||
const char *mod = p->l->module;
|
||||
Node *attrs = parseattrs(p);
|
||||
int exp = accept(p, TK_EXPORT);
|
||||
Node *d = NULL;
|
||||
@@ -1363,10 +1371,7 @@ 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 (d != NULL) d->module = mod;
|
||||
if (head == NULL) head = d;
|
||||
else tail->next = d;
|
||||
tail = d;
|
||||
|
||||
@@ -53,18 +53,66 @@ scope_lookup(Scope *s, const char *name)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* scope_lookup_in_module — module-filtered chain walk.
|
||||
*
|
||||
* Same FNV bucket + hashnext chain + parent walk as scope_lookup,
|
||||
* plus a (b->mod != NULL && strcmp(b->mod, mod) == 0) filter. When
|
||||
* `mod` is NULL we fall back to unfiltered scope_lookup semantics,
|
||||
* so callers that don't care about disambiguation get the default.
|
||||
*
|
||||
* Used by resolve_typename and the cexpr N_DOT branch to pick the
|
||||
* right same-leaf-name type when two imports each export it
|
||||
* (`bufio.stream` vs `io.stream`).
|
||||
*/
|
||||
Sym *
|
||||
scope_lookup_in_module(Scope *s, const char *mod, const char *name)
|
||||
{
|
||||
if (mod == NULL) return scope_lookup(s, name);
|
||||
for (; s; s = s->parent) {
|
||||
u64 h = hashstr(name) % s->nbuckets;
|
||||
for (Sym *b = s->buckets[h]; b; b = b->hashnext) {
|
||||
if (strcmp(b->name, name) != 0) continue;
|
||||
if (b->mod && strcmp(b->mod, mod) == 0) return b;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Sym *
|
||||
scope_define(Scope *s, const char *name, Skind k, Type *t, Node *decl)
|
||||
{
|
||||
if (scope_lookup_local(s, name) != NULL)
|
||||
return NULL;
|
||||
return scope_define_in_module(s, name, NULL, k, t, decl);
|
||||
}
|
||||
|
||||
/*
|
||||
* scope_define_in_module — bucket insert with per-mod dedup.
|
||||
*
|
||||
* Same insertion as scope_define, but the duplicate-rejection key is
|
||||
* (name, mod) rather than name alone. This lets two imports each
|
||||
* register their own `stream` SK_TYPE in the flat scope, and lets the
|
||||
* primary register `stream` (mod=NULL) alongside imported `stream`s.
|
||||
*
|
||||
* Within a single (name, mod) pair the first registration wins; later
|
||||
* attempts return NULL and the caller emits a duplicate-type error.
|
||||
*/
|
||||
Sym *
|
||||
scope_define_in_module(Scope *s, const char *name, const char *mod,
|
||||
Skind k, Type *t, Node *decl)
|
||||
{
|
||||
u64 h = hashstr(name) % s->nbuckets;
|
||||
for (Sym *b = s->buckets[h]; b; b = b->hashnext) {
|
||||
if (strcmp(b->name, name) != 0) continue;
|
||||
if (b->mod == NULL && mod == NULL) return NULL;
|
||||
if (b->mod && mod && strcmp(b->mod, mod) == 0) return NULL;
|
||||
}
|
||||
Sym *sy = amalloc(s->a, sizeof *sy);
|
||||
sy->name = name;
|
||||
sy->mod = mod;
|
||||
sy->kind = k;
|
||||
sy->type = t;
|
||||
sy->decl = decl;
|
||||
sy->scope = s;
|
||||
u64 h = hashstr(name) % s->nbuckets;
|
||||
sy->hashnext = s->buckets[h];
|
||||
s->buckets[h] = sy;
|
||||
if (s->first == NULL) s->first = sy;
|
||||
|
||||
10
cmd/wcc/ww.h
10
cmd/wcc/ww.h
@@ -482,6 +482,13 @@ struct Sym {
|
||||
* Lets resolve_typename treat `foo.x`
|
||||
* as module-qualified even though the
|
||||
* primary kind isn't SK_USE. */
|
||||
const char *mod; /* importing module's bareword for
|
||||
* symbols originating in a `use`-
|
||||
* imported module. NULL for primary
|
||||
* (root) compilation unit symbols.
|
||||
* Used by scope_lookup_in_module to
|
||||
* disambiguate same-leaf-name types
|
||||
* coming from different imports. */
|
||||
Sym *next; /* iteration */
|
||||
Sym *hashnext; /* bucket chain */
|
||||
Scope *scope;
|
||||
@@ -497,8 +504,11 @@ struct Scope {
|
||||
|
||||
Scope *newscope(Arena*, Scope *parent);
|
||||
Sym *scope_define(Scope*, const char *name, Skind, Type*, Node *decl);
|
||||
Sym *scope_define_in_module(Scope*, const char *name, const char *mod,
|
||||
Skind, Type*, Node *decl);
|
||||
Sym *scope_lookup(Scope*, const char *name); /* walk up parents */
|
||||
Sym *scope_lookup_local(Scope*, const char *name);
|
||||
Sym *scope_lookup_in_module(Scope*, const char *mod, const char *name);
|
||||
|
||||
/* ---- checker (check.c) -------------------------------------------- */
|
||||
typedef struct Checker Checker;
|
||||
|
||||
Reference in New Issue
Block a user