Commit 240333ca
240333ca4623Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified
Description
feat(codegen): Class#superclass / #ancestors / <, <=, > (#404 Phase 3 Tier 1)
Phase 3 of the Class-object work lands user-class hierarchy introspection on the sp_Class value: precomputed parents + flat ancestors table emitted per program, runtime helpers that walk those tables, and codegen arms that lower the Class-recv hierarchy methods.
What works after this commit:
Foo.superclassreturns a sp_Class for the parent (or the{-1}sentinel for layout-root classes, which stringifies to "").Foo.ancestorsreturns a sp_PolyArray whose elements are boxed sp_Class values (new SP_TAG_CLASS poly tag + sp_box_class helper). Iteration viaeach |klass|; klass.to_s; endlands on the program-emitted sp_class_to_s via a new SP_TAG_CLASS arm in sp_poly_to_s.Foo < Bar,Foo <= Bar,Foo > Bar,Foo >= Bar-- proper / non-proper subclass checks via sp_class_lt / sp_class_le over the flat ancestors table.
Out of scope for this tier (deferred follow-ups on #404):
- Module / include integration. @cls_kinds and @cls_includes are not yet collected; ancestors currently walk parent chains only. Mixing in a module shows up as "module is unknown to the class table" -- the module hierarchy stays in the parallel @module_names space until the next tier unifies cls_id numbering.
- Dynamic
is_a?(C)against a poly receiver -- needs the same ancestors-table lookup but routed throughsp_class_for_polyto recover the runtime cls_id of a poly value. case obj when Klass1, Klass2on a Class subject -- samesp_class_lemachinery, threaded through case-when's WhenNode condition compile.
Emission policy: Phase 1 / 2's "always emit when @cls_names > 0, let attribute((unused)) + link-time DCE drop the dead helpers" policy carries over. emit_class_runtime runs before compile_expr toggles per-feature flags, so flag-gated emission would always miss; the residual cost is a few hundred bytes of read-only data per program with classes (Optcarrot self-host: checksum 59662 unchanged, fps within noise).
To keep the SP_TAG_CLASS arm in sp_poly_to_s (lib/sp_runtime.h)
linkable for programs that never declare a user class, the
class names table + sp_class_to_s now emit even when
@cls_names is empty (sentinel 1-slot array; ISO C forbids a
zero-length brace initializer). Programs without classes never
construct a SP_TAG_CLASS value, so the arm stays cold but the
forward decl resolves.
Refs #404 Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 240333ca4623
- Parent
- 5d13c0299186
lib/sp_runtime.hmodifiedView file
#define SP_TAG_NIL 4#define SP_TAG_OBJ 5#define SP_TAG_SYM 6/* Issue #404 Phase 3: Class values boxed into a poly slot (e.g. as ancestors-array elements). cls_id of the sp_RbVal carries the boxed sp_Class's cls_id directly so unboxing is just a field read. */#define SP_TAG_CLASS 7/* Negative cls_id values let SP_TAG_OBJ also carry built-in pointer types (IntArray, FloatArray, ...) — avoids minting a new SP_TAG_* per type. Non-negative cls_id stays an index into the user-classstatic sp_RbVal sp_box_nil(void) { sp_RbVal r; r.tag = SP_TAG_NIL; r.cls_id = 0; r.v.i = 0; return r; }static sp_RbVal sp_box_obj(void *p, int cls_id) { sp_RbVal r; r.tag = SP_TAG_OBJ; r.cls_id = cls_id; r.v.p = p; return r; }static sp_RbVal sp_box_sym(sp_sym v) { sp_RbVal r; r.tag = SP_TAG_SYM; r.cls_id = 0; r.v.i = (mrb_int)v; return r; }/* Issue #404 Phase 3: box a sp_Class into a poly slot. */static sp_RbVal sp_box_class(sp_Class c) { sp_RbVal r; r.tag = SP_TAG_CLASS; r.cls_id = (int)c.cls_id; r.v.i = c.cls_id; return r; }static sp_RbVal sp_box_nullable_str(const char *v) { return v ? sp_box_str(v) : sp_box_nil(); }static sp_RbVal sp_box_nullable_obj(void *p, int cls_id) { return p ? sp_box_obj(p, cls_id) : sp_box_nil(); }/* Built-in pointer boxes — share SP_TAG_OBJ with a reserved negative}static mrb_bool sp_poly_nil_p(sp_RbVal v) { return v.tag == SP_TAG_NIL; }static mrb_bool sp_poly_truthy(sp_RbVal v) { return !(v.tag == SP_TAG_NIL || (v.tag == SP_TAG_BOOL && !v.v.b)); }/* Issue #404 Phase 3: forward-declare the program-emitted class name lookup so sp_poly_to_s's SP_TAG_CLASS arm resolves. The codegen emits sp_class_to_s unconditionally (even for programs with no user classes), so the link is always satisfied. */static const char *sp_class_to_s(sp_Class c);static const char *sp_poly_to_s(sp_RbVal v) { switch (v.tag) { case SP_TAG_INT: return sp_int_to_s(v.v.i); case SP_TAG_BOOL: return v.v.b ? SPL("true") : SPL("false"); case SP_TAG_NIL: return sp_str_empty; case SP_TAG_SYM: return sp_sym_to_s((sp_sym)v.v.i); case SP_TAG_CLASS: { sp_Class c = {v.v.i}; return sp_class_to_s(c); } case SP_TAG_OBJ: switch (v.cls_id) { case SP_BUILTIN_INT_ARRAY: return sp_IntArray_inspect((sp_IntArray *)v.v.p);spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/class_hierarchy.rbaddedView file
Loading diff…
test/class_hierarchy.rb.expectedaddedView file
Loading diff…