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_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
0adae7775bbf
Parent
e5a9ae21ea02

lib/sp_runtime.hmodifiedView file

@@ -99,6 +99,16 @@
9999 typedef struct{mrb_int first;mrb_int last;}sp_Range;
100100 static sp_Range sp_range_new(mrb_int f,mrb_int l){sp_Range r;r.first=f;r.last=l;return r;}
101101
102+/* ---- Class object (issue #404 Phase 1) ----
103+ Value-type Class reference: a single class id that indexes into
104+ the per-program sp_class_names[] table emitted by codegen. Lets
105+ `c = Foo` produce a runtime value (`(sp_Class){<id>}`) instead of
106+ a bare C identifier, and `c.to_s` lower to a names-table lookup.
107+ Other Class methods (`.name`, `.inspect`, `.==`, `.!=`,
108+ `.superclass`, `.ancestors`, dynamic `is_a?(c)` against a
109+ variable, etc.) are out of scope for Phase 1. */
110+typedef struct{mrb_int cls_id;}sp_Class;
111+
102112 /* ---- Complex runtime ---- */
103113 /* Value-type Cartesian Complex: 16 bytes, passed by value. Used by
104114 optcarrot's nestopia palette generator; the palette is precomputed