Commit 3af1c7e7

3af1c7e7431bMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

Unique operator-method mangling + reopened-class last-def-wins

mc() mapped every operator character to a single '_', so distinct operator methods collided (def & vs def |, def << vs def >>) and the C compile failed with "redefinition". Each operator now mangles to a distinct token (_amp / _bar / _lt / _gt / ...).

A reopened class with a duplicate method name now keeps the last definition: comp_method_in_class / comp_cmethod_in_class resolve to the last matching scope, and shadowed earlier scopes are skipped at emission.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
3af1c7e7431b
Parent
f2f8031ea03a

src/codegen.cmodifiedView file

@@ -343,19 +343,54 @@
343343 static const char *mc(const char *name) {
344344 static char buf[256];
345345 int j = 0;
346- for (const char *p = name; *p && j < (int)sizeof buf - 6; p++) {
346+ for (const char *p = name; *p && j < (int)sizeof buf - 8; p++) {
347347 char ch = *p;
348348 if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
349- (ch >= '0' && ch <= '9') || ch == '_') buf[j++] = ch;
350- else if (ch == '?') { buf[j++] = '_'; buf[j++] = 'p'; }
351- else if (ch == '!') { memcpy(buf + j, "_bang", 5); j += 5; }
352- else if (ch == '=') { memcpy(buf + j, "_set", 4); j += 4; }
353- else buf[j++] = '_';
349+ (ch >= '0' && ch <= '9') || ch == '_') { buf[j++] = ch; continue; }
350+ /* operator characters map to distinct tokens so that, e.g., `&` and `|`
351+ (or `<<` and `>>`) don't mangle to the same C identifier */
352+ const char *tok;
353+ switch (ch) {
354+ case '?': tok = "_p"; break;
355+ case '!': tok = "_bang"; break;
356+ case '=': tok = "_set"; break;
357+ case '+': tok = "_plus"; break;
358+ case '-': tok = "_minus"; break;
359+ case '*': tok = "_star"; break;
360+ case '/': tok = "_slash"; break;
361+ case '%': tok = "_pct"; break;
362+ case '<': tok = "_lt"; break;
363+ case '>': tok = "_gt"; break;
364+ case '&': tok = "_amp"; break;
365+ case '|': tok = "_bar"; break;
366+ case '^': tok = "_caret"; break;
367+ case '~': tok = "_tilde"; break;
368+ case '@': tok = "_at"; break;
369+ case '[': tok = "_lb"; break;
370+ case ']': tok = "_rb"; break;
371+ default: tok = "_"; break;
372+ }
373+ size_t tl = strlen(tok);
374+ memcpy(buf + j, tok, tl); j += (int)tl;
354375 }
355376 buf[j] = '\0';
356377 return buf;
357378 }
358379
380+/* A class method scope is shadowed (and must not be emitted) when a later
381+ scope redefines the same (class, name, is_cmethod) -- a reopened class
382+ where the last definition wins, matching comp_method_in_class. */
383+static int scope_is_shadowed(Compiler *c, int s) {
384+ Scope *sc = &c->scopes[s];
385+ if (sc->class_id < 0 || !sc->name) return 0;
386+ for (int k = s + 1; k < c->nscopes; k++) {
387+ Scope *o = &c->scopes[k];
388+ if (o->class_id == sc->class_id && o->is_cmethod == sc->is_cmethod &&
389+ o->name && !strcmp(o->name, sc->name)) return 1;
390+ }
391+ return 0;
392+}
393+
359394 /* Value node for keyword `name` inside a KeywordHashNode, or -1. */
360395 static int struct_kwarg_value(Compiler *c, int kwh, const char *name) {
361396 const NodeTable *nt = c->nt;
@@ -5037,7 +5072,7 @@
50375072 }
50385073
50395074 /* method prototypes (scope 0 is top-level) */
5040- for (int s = 1; s < c->nscopes; s++) { if (c->scopes[s].yields || !c->scopes[s].reachable) continue; emit_method_signature(c, &c->scopes[s], &b); buf_puts(&b, ";\n"); }
5075+ for (int s = 1; s < c->nscopes; s++) { if (c->scopes[s].yields || !c->scopes[s].reachable || scope_is_shadowed(c, s)) continue; emit_method_signature(c, &c->scopes[s], &b); buf_puts(&b, ";\n"); }
50415076 /* constructor prototypes + definitions (after method protos: new calls initialize) */
50425077 for (int i = 0; i < c->nclasses; i++) {
50435078 ClassInfo *ci = &c->classes[i];
@@ -5079,7 +5114,7 @@
50795114 function must be declared before the body that references it. */
50805115 Buf body; memset(&body, 0, sizeof body);
50815116 for (int i = 0; i < c->nclasses; i++) emit_class_new(c, &c->classes[i], &body);
5082- for (int s = 1; s < c->nscopes; s++) { if (c->scopes[s].yields || !c->scopes[s].reachable) continue; emit_method(c, &c->scopes[s], &body); }
5117+ for (int s = 1; s < c->nscopes; s++) { if (c->scopes[s].yields || !c->scopes[s].reachable || scope_is_shadowed(c, s)) continue; emit_method(c, &c->scopes[s], &body); }
50835118
50845119 buf_puts(&body, "int main(int argc,char**argv){\n");
50855120 buf_puts(&body, " SP_GC_SAVE();\n");