Commit 0adae777
0adae7775bbfMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
feat(codegen): class constants as first-class values, .to_s only (#404 Phase 1)
A class constant in value position used to emit a bare C identifier:
c = Row -> lv_c = Row; /* Row undeclared */ puts c.to_s -> warning: cannot resolve call to 'to_s' on class_Row
Phase 1 wires the minimum surface for class-as-value to round-trip through a local + an attr_accessor:
-
sp_Classstruct in sp_runtime.h. A thin value-type wrapper overmrb_int cls_idthat indexes into the per-programsp_class_names[]table. -
infer_typefor ConstantReadNode / ConstantPathNode resolving to a registered class returns "class" (was "class_", which was a sentinel string that no consumer interpreted, so downstream typing fell back to int). -
c_type "class"-> "sp_Class";c_default_val "class"->((sp_Class){-1LL})(the -1 sentinel that sp_class_to_s turns into "" -- nil-equivalent for to_s purposes). -
compile_expr's ConstantReadNode arm emits a sp_Class compound literal carrying the class index (matched by @cls_names ordering) instead of the bare class name. Class constants used as method-call receivers go through constructor_class_name / cls_cmethod_owner, which never call compile_expr on the receiver, so this branch only fires in a true value-position site. -
compile_object_method_exprrecv_type == "class" lowers.to_stosp_class_to_s(c). Bound-checks the cls_id and returns "" for the -1 sentinel. -
emit_class_runtimeemits the names array + helper near the start of generate_code (right after the regexp runtime), ahead of emit_class_structs so user-class forward decls can referencesp_Classwithout a separate forward decl. Runs whenever any user class exists -- gating it on a flag set inside compile_expr is too late since generate_code finishes the runtime emission before bodies are compiled. The names array is ~one cache line for typical small programs and the sp_class_to_s helper is__attribute__((unused))-marked so it gets link-eliminated when no Class-as-value site exists.
Out of scope, deferred to Phase 2 (#404 stays open):
.name/.inspect/.==/.!=on Class values.- Module distinction (Module vs Class kind).
- Dispatch through a Class-typed local (
c.new(...)). - is_a?(Klass-typed-variable), case/when on Class values.
- Precomputed ancestors / .superclass /
</<=.
Test class_const_as_value.rb covers the canonical local-binding
shape (c = Row; puts c.to_s), a subclass binding (d = Article), and the round-trip through an attr_accessor (Holder
storing a sp_Class on its ivar and reading it back). The earlier
broader attempt in the May 10 stash hung the bootstrap because
its obj.class arm ran on every method call site and recursed
into infer_type on the receiver; this narrower lowering avoids
both the broad-scope arm and the recursion.
Refs #404
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 0adae7775bbf
- Parent
- e5a9ae21ea02
lib/sp_runtime.hmodifiedView file
typedef struct{mrb_int first;mrb_int last;}sp_Range;static sp_Range sp_range_new(mrb_int f,mrb_int l){sp_Range r;r.first=f;r.last=l;return r;}/* ---- Class object (issue #404 Phase 1) ---- Value-type Class reference: a single class id that indexes into the per-program sp_class_names[] table emitted by codegen. Lets `c = Foo` produce a runtime value (`(sp_Class){<id>}`) instead of a bare C identifier, and `c.to_s` lower to a names-table lookup. Other Class methods (`.name`, `.inspect`, `.==`, `.!=`, `.superclass`, `.ancestors`, dynamic `is_a?(c)` against a variable, etc.) are out of scope for Phase 1. */typedef struct{mrb_int cls_id;}sp_Class;/* ---- Complex runtime ---- *//* Value-type Cartesian Complex: 16 bytes, passed by value. Used by optcarrot's nestopia palette generator; the palette is precomputedspinel_codegen.rbmodifiedView file
Loading diff…
test/class_const_as_value.rbaddedView file
Loading diff…
test/class_const_as_value.rb.expectedaddedView file
Loading diff…