aboutsummaryrefslogtreecommitdiff
path: root/src/compiler
diff options
context:
space:
mode:
Diffstat (limited to 'src/compiler')
-rw-r--r--src/compiler/expr.zc4
-rw-r--r--src/compiler/types.zc6
2 files changed, 7 insertions, 3 deletions
diff --git a/src/compiler/expr.zc b/src/compiler/expr.zc
index c6d29c6..40de6ef 100644
--- a/src/compiler/expr.zc
+++ b/src/compiler/expr.zc
@@ -1014,7 +1014,9 @@ int primary() {
return C_PLAIN;
}
s = source_lookup(n);
- if (mstruct && !ginst) { ms = lookup(methname(mstruct, n)); if (ms >= 0 && skind[ms] == K_CONST) s = ms; }
+ // in a method of a struct a bare name is that struct's own constant or static function first, so it
+ // wins over a global (a top-level function or constant) of the same name
+ if (mstruct && !ginst) { ms = lookup(methname(mstruct, n)); if (ms >= 0 && (skind[ms] == K_CONST || (skind[ms] == K_FN && (sflag[ms] & F_STATIC)))) s = ms; }
if (s < 0 && rt.eq_w(n, "new")) s = source_lookup(s_new);
if (s >= 0) rs_put(toff, RS_SYM, rs_sym(s), s);
if (s >= 0 && skind[s] == K_KW) {
diff --git a/src/compiler/types.zc b/src/compiler/types.zc
index c643f0b..6dda110 100644
--- a/src/compiler/types.zc
+++ b/src/compiler/types.zc
@@ -497,8 +497,9 @@ int dtype() {
}
return 0;
}
-// the struct whose body or methods are being compiled, when it declares constants: a bare
-// name there is looked up as Struct.name before the global scope
+// the struct whose body or methods are being compiled: a bare name there is looked up as Struct.name
+// (a constant, or a static function called by its bare name) before the global scope, so the struct's
+// own wins over a global of the same name
char* mstruct;
// does a constant declaration start in a struct body? const NAME = e; const T NAME = e;
// (a field of a const type has no initializer, so the = tells them apart)
@@ -652,6 +653,7 @@ int kw_struct() {
}
accept(s_semi);
i = 0;
+ if (!gen) mstruct = n;
while (i < nm) { fn_method_compile = 1; methdef(n, mn[i], mq[i], mst[i], mty[i], mrt[i], mrg[i]); fn_method_compile = 0; i++; }
mdg = 0;
mstruct = msv;