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_Class struct in sp_runtime.h. A thin value-type wrapper over mrb_int cls_id that indexes into the per-program sp_class_names[] table.

  • infer_type for 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_expr recv_type == "class" lowers .to_s to sp_class_to_s(c). Bound-checks the cls_id and returns "" for the -1 sentinel.

  • emit_class_runtime emits 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 reference sp_Class without 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

@@ -2060,7 +2060,11 @@
20602060 end
20612061 cx = find_class_idx(rname)
20622062 if cx >= 0
2063- return "class_" + rname
2063+ # Issue #404 Phase 1: class constant in value position.
2064+ # Class constants used as method-call receivers go through
2065+ # find_class_idx directly via constructor_class_name and
2066+ # never call infer_type on the receiver.
2067+ return "class"
20642068 end
20652069 return "int"
20662070 end
@@ -2073,7 +2077,8 @@
20732077 end
20742078 cx = find_class_idx(cpname)
20752079 if cx >= 0
2076- return "class_" + cpname
2080+ # Issue #404 Phase 1: class constant in value position.
2081+ return "class"
20772082 end
20782083 end
20792084 parent = @nd_receiver[nid]