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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user