Commit a4ae9d30
a4ae9d3001edMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · ~4 modified
Description
Add M10a: attr_reader / attr_writer / attr_accessor
- ClassInfo tracks reader/writer attribute names; register_attrs scans class-body attr_* CallNodes, registering backing ivars and reader/writer names.
- analyze: attr readers return the backing ivar type; attr writers (obj.x=) return the assigned value type and feed @x's inferred type from call sites.
- codegen: attr reader obj.x -> (recv).iv_x field access; attr writer obj.x = v -> (recv).iv_x = v statement.
make test: 14 pass.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- a4ae9d3001ed
- Parent
- 5d688451179b
src/analyze.cmodifiedView file
@@ -1,5 +1,6 @@
11
#include "analyze.h"22
3+
#include <stdio.h>34
#include <stdlib.h>45
#include <string.h>56
@@ -56,9 +57,25 @@
5657
/* obj.method(...) -> the method's return type */5758
if (recv >= 0 && ty_is_object(rt)) {5859
int cid = ty_object_class(rt);60+
ClassInfo *cls = &c->classes[cid];61+
/* attr reader */62+
if (comp_is_reader(cls, name)) {63+
char ivn[256];64+
snprintf(ivn, sizeof ivn, "@%s", name);65+
int iv = comp_ivar_index(cls, ivn);66+
if (iv >= 0) return cls->ivar_types[iv];67+
}68+
/* attr writer: obj.x= returns the assigned value */69+
size_t ln = strlen(name);70+
if (ln >= 2 && name[ln - 1] == '=') {71+
char base[256];72+
if (ln - 1 < sizeof base) {73+
memcpy(base, name, ln - 1); base[ln - 1] = '\0';74+
if (comp_is_writer(cls, base) && argc >= 1) return infer_type(c, argv[0]);75+
}76+
}5977
int mi = comp_method_in_class(c, cid, name);6078
if (mi >= 0) return c->scopes[mi].ret;61-
/* built-in object queries */6279
if (!strcmp(name, "to_s") || !strcmp(name, "inspect")) return TY_STRING;6380
}6481
@@ -338,6 +355,42 @@
338355
}339356
}340357
358+
/* Collect attr_reader/attr_writer/attr_accessor declarations in class359+
bodies, registering backing ivars + reader/writer method names. */360+
static void register_attrs(Compiler *c) {361+
const NodeTable *nt = c->nt;362+
for (int ci = 0; ci < c->nclasses; ci++) {363+
ClassInfo *cls = &c->classes[ci];364+
int body = nt_ref(nt, cls->def_node, "body");365+
int n = 0;366+
const int *stmts = body >= 0 ? nt_arr(nt, body, "body", &n) : NULL;367+
for (int k = 0; k < n; k++) {368+
int s = stmts[k];369+
const char *sty = nt_type(nt, s);370+
if (!sty || strcmp(sty, "CallNode")) continue;371+
const char *nm = nt_str(nt, s, "name");372+
if (!nm) continue;373+
int reader = !strcmp(nm, "attr_reader") || !strcmp(nm, "attr_accessor");374+
int writer = !strcmp(nm, "attr_writer") || !strcmp(nm, "attr_accessor");375+
if (!reader && !writer) continue;376+
int args = nt_ref(nt, s, "arguments");377+
int an = 0;378+
const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;379+
for (int a = 0; a < an; a++) {380+
const char *aty = nt_type(nt, argv[a]);381+
if (!aty || strcmp(aty, "SymbolNode")) continue;382+
const char *base = nt_str(nt, argv[a], "value");383+
if (!base) continue;384+
char ivname[256];385+
snprintf(ivname, sizeof ivname, "@%s", base);386+
comp_ivar_intern(cls, ivname);387+
if (reader) comp_add_reader(cls, base);388+
if (writer) comp_add_writer(cls, base);389+
}390+
}391+
}392+
}393+
341394
/* @ivar types from their assignments across the class's methods. */342395
static int infer_ivar_types(Compiler *c) {343396
const NodeTable *nt = c->nt;@@ -345,21 +398,41 @@
345398
for (int id = 0; id < nt->count; id++) {346399
const char *ty = nt_type(nt, id);347400
if (!ty) continue;348-
const char *nm = NULL;349-
TyKind vt = TY_UNKNOWN;350401
if (!strcmp(ty, "InstanceVariableWriteNode")) {351-
nm = nt_str(nt, id, "name");352-
vt = infer_type(c, nt_ref(nt, id, "value"));353-
} else {354-
continue;402+
const char *nm = nt_str(nt, id, "name");403+
TyKind vt = infer_type(c, nt_ref(nt, id, "value"));404+
Scope *s = comp_scope_of(c, id);405+
if (!nm || s->class_id < 0) continue;406+
ClassInfo *ci = &c->classes[s->class_id];407+
int iv = comp_ivar_index(ci, nm);408+
if (iv < 0) continue;409+
TyKind merged = ty_unify(ci->ivar_types[iv], vt);410+
if (merged != ci->ivar_types[iv]) { ci->ivar_types[iv] = merged; changed = 1; }411+
} else if (!strcmp(ty, "CallNode")) {412+
/* attr-writer assignment: obj.x = v (CallNode "x=") */413+
const char *nm = nt_str(nt, id, "name");414+
int recv = nt_ref(nt, id, "receiver");415+
size_t ln = nm ? strlen(nm) : 0;416+
if (!nm || recv < 0 || ln < 2 || nm[ln - 1] != '=') continue;417+
TyKind rt = infer_type(c, recv);418+
if (!ty_is_object(rt)) continue;419+
ClassInfo *ci = &c->classes[ty_object_class(rt)];420+
char base[256];421+
if (ln - 1 >= sizeof base) continue;422+
memcpy(base, nm, ln - 1); base[ln - 1] = '\0';423+
if (!comp_is_writer(ci, base)) continue;424+
int args = nt_ref(nt, id, "arguments");425+
int an = 0;426+
const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;427+
if (an < 1) continue;428+
char ivname[256];429+
snprintf(ivname, sizeof ivname, "@%s", base);430+
int iv = comp_ivar_index(ci, ivname);431+
if (iv < 0) continue;432+
TyKind vt = infer_type(c, argv[0]);433+
TyKind merged = ty_unify(ci->ivar_types[iv], vt);434+
if (merged != ci->ivar_types[iv]) { ci->ivar_types[iv] = merged; changed = 1; }355435
}356-
Scope *s = comp_scope_of(c, id);357-
if (!nm || s->class_id < 0) continue;358-
ClassInfo *ci = &c->classes[s->class_id];359-
int iv = comp_ivar_index(ci, nm);360-
if (iv < 0) continue;361-
TyKind merged = ty_unify(ci->ivar_types[iv], vt);362-
if (merged != ci->ivar_types[iv]) { ci->ivar_types[iv] = merged; changed = 1; }363436
}364437
return changed;365438
}@@ -548,6 +621,7 @@
548621
549622
walk_scope(c, c->nt->root_id, 0, -1);550623
register_locals(c);624+
register_attrs(c);551625
552626
/* intern every symbol literal so codegen can emit the id table */553627
for (int id = 0; id < c->nt->count; id++) {src/codegen.cmodifiedView file
Loading diff…
src/compiler.cmodifiedView file
Loading diff…
src/compiler.hmodifiedView file
Loading diff…