Commit bd18403c

bd18403c6e4bMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

feat(codegen): obj.class reflection + chained cmeth dispatch (#419)

obj.class was unresolved on an instance -- emitted cannot resolve call to 'class' on obj_<C> (emitting 0) and downstream methods cascaded through int -> 0. Sibling to #404 Phase 1 (which closed the literal-reference direction c = Foo); this is the reflection direction, fetching the class FROM an instance.

Four coordinated changes:

  • compile_object_method_expr (is_obj_type arm): when mname is "class", emit ((sp_Class){<cls_idx>LL}). The class id is statically known from the receiver's obj_ type, so no runtime lookup is needed.

  • compile_object_method_expr (recv_type=="class" arm): detect chained <obj>.class.<cmeth>(...) via AST shape -- recv is itself a class CallNode whose inner recv has a known obj_ type. When that holds, bypass the sp_Class value and dispatch directly to sp_<C>_cls_<m>(...) against the statically-resolved class. Falls back to the existing runtime path otherwise (Phase 3 will need it for dynamic poly Class values).

  • infer_method_name_type: returns "class" for obj.class and the cmeth's return type for <obj>.class.<cmeth>, so downstream local-variable types and string-interpolation formatters pick the right shape.

  • collect_cls_calls (DCE walker): when the recv is a class CallNode, mark cmeth live on every class that defines one. Over-approximates because LocalVariableReadNode types aren't resolvable at DCE time (walk_and_cache intentionally skips them per the comment at line 19498), but DCE prefers a kept-but-unused method over a stripped live one. Same shape as the existing ConstantReadNode / SymbolNode arms.

  • String interpolation ("#{self.class}"): added a "class" branch to compile_string_interp's per-fragment dispatch that emits sp_class_to_s(<expr>) instead of casting to long long. Without this, "#<#{self.class}>" (optcarrot's default Inspect shape) compiled to sp_sprintf("#<%lld>", (long long)((sp_Class){<idx>})) which fails C-compile because sp_Class is a struct.

Test obj_class_dispatch.rb covers chained .class.cmeth, plain obj.class.to_s (the recv_type=="class" -> to_s arm from #404), the string-interpolation form, and a subclass receiver that exercises cls_cmethod_owner's parent walk.

Closes #419

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
bd18403c6e4b
Parent
623100cceea8

spinel_analyze.rbmodifiedView file

@@ -3181,6 +3181,48 @@
31813181 return "time"
31823182 end
31833183 end
3184+ # Issue #419: `obj.class` on a statically-typed instance returns
3185+ # a sp_Class value. The codegen-side mirror in
3186+ # compile_object_method_expr emits the matching compound literal
3187+ # (`((sp_Class){<cls_idx>LL})`).
3188+ if mname == "class"
3189+ if recv >= 0
3190+ rt_419 = infer_type(recv)
3191+ if is_obj_type(rt_419) == 1
3192+ return "class"
3193+ end
3194+ end
3195+ end
3196+ # Issue #419: `<obj>.class.<cmeth>` chained dispatch. When the
3197+ # recv is itself a `class` CallNode whose own recv is
3198+ # statically-typed obj_<C>, the chain resolves to <C>.<cmeth>
3199+ # and the return type is whatever the cmeth returns. Same
3200+ # detection shape as the codegen lowering.
3201+ if recv >= 0 && @nd_type[recv] == "CallNode" && @nd_name[recv] == "class"
3202+ inner_recv_419 = @nd_receiver[recv]
3203+ if inner_recv_419 >= 0
3204+ inner_t_419 = infer_type(inner_recv_419)
3205+ if is_obj_type(inner_t_419) == 1
3206+ inner_bt_419 = base_type(inner_t_419)
3207+ inner_cname_419 = inner_bt_419[4, inner_bt_419.length - 4]
3208+ inner_ci_419 = find_class_idx(inner_cname_419)
3209+ if inner_ci_419 >= 0
3210+ owner_419 = cls_cmethod_owner(inner_ci_419, mname)
3211+ if owner_419 >= 0
3212+ cmnames_419 = @cls_cmeth_names[owner_419].split(";")
3213+ cmreturns_419 = @cls_cmeth_returns[owner_419].split(";")
3214+ cmidx_419 = 0
3215+ while cmidx_419 < cmnames_419.length
3216+ if cmnames_419[cmidx_419] == mname && cmidx_419 < cmreturns_419.length
3217+ return cmreturns_419[cmidx_419]
3218+ end
3219+ cmidx_419 = cmidx_419 + 1
3220+ end
3221+ end
3222+ end
3223+ end
3224+ end
3225+ end
31843226 # Kernel coercion methods: Integer(x) / Float(x) return their class.
31853227 # Only treat as a Kernel call when there's no explicit receiver — with
31863228 # a receiver, "Integer" / "Float" would be ConstantReadNode lookups,
@@ -16618,6 +16660,24 @@
1661816660 end
1661916661 end
1662016662 end
16663+ # Issue #419: `<obj>.class.<cmeth>` — recv is a `class`
16664+ # CallNode. The lowered call dispatches to <C>::cmeth at
16665+ # codegen time when the inner recv has a known obj_<C>
16666+ # type. At DCE time we don't have method-body scope
16667+ # context (so infer_type on a LocalVariableReadNode here
16668+ # falls back to "int"), so over-approximate: mark cmeth
16669+ # live on every class that defines one. Matches the
16670+ # ConstantReadNode / SymbolNode arms above which take the
16671+ # same shape — DCE prefers false-negatives (a kept-but-
16672+ # unused method) over false-positives (a stripped live
16673+ # method that produces a linker error).
16674+ if @nd_name[recv] == "class"
16675+ mark_idx_419 = 0
16676+ while mark_idx_419 < @cls_names.length
16677+ cls_cmeth_mark_live(mark_idx_419, mname)
16678+ mark_idx_419 = mark_idx_419 + 1
16679+ end
16680+ end
1662116681 end
1662216682 else
1662316683 if ctx_ci >= 0