Commit 27cef44c
27cef44c1104Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · ~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
- 27cef44c1104
- Parent
- 0114f2199821
spinel_analyze.rbmodifiedView file
end cx = find_class_idx(rname) if cx >= 0 return "class_" + rname # Issue #404 Phase 1: class constant in value position. # Class constants used as method-call receivers go through # find_class_idx directly via constructor_class_name and # never call infer_type on the receiver. return "class" end return "int" end end cx = find_class_idx(cpname) if cx >= 0 return "class_" + cpname # Issue #404 Phase 1: class constant in value position. return "class" end end parent = @nd_receiver[nid]spinel_codegen.rbmodifiedView file
Loading diff…