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 aclassCallNode whose inner recv has a known obj_ type. When that holds, bypass the sp_Class value and dispatch directly tosp_<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" forobj.classand 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 aclassCallNode, 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 emitssp_class_to_s(<expr>)instead of casting to long long. Without this,"#<#{self.class}>"(optcarrot's default Inspect shape) compiled tosp_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
return "time" end end # Issue #419: `obj.class` on a statically-typed instance returns # a sp_Class value. The codegen-side mirror in # compile_object_method_expr emits the matching compound literal # (`((sp_Class){<cls_idx>LL})`). if mname == "class" if recv >= 0 rt_419 = infer_type(recv) if is_obj_type(rt_419) == 1 return "class" end end end # Issue #419: `<obj>.class.<cmeth>` chained dispatch. When the # recv is itself a `class` CallNode whose own recv is # statically-typed obj_<C>, the chain resolves to <C>.<cmeth> # and the return type is whatever the cmeth returns. Same # detection shape as the codegen lowering. if recv >= 0 && @nd_type[recv] == "CallNode" && @nd_name[recv] == "class" inner_recv_419 = @nd_receiver[recv] if inner_recv_419 >= 0 inner_t_419 = infer_type(inner_recv_419) if is_obj_type(inner_t_419) == 1 inner_bt_419 = base_type(inner_t_419) inner_cname_419 = inner_bt_419[4, inner_bt_419.length - 4] inner_ci_419 = find_class_idx(inner_cname_419) if inner_ci_419 >= 0 owner_419 = cls_cmethod_owner(inner_ci_419, mname) if owner_419 >= 0 cmnames_419 = @cls_cmeth_names[owner_419].split(";") cmreturns_419 = @cls_cmeth_returns[owner_419].split(";") cmidx_419 = 0 while cmidx_419 < cmnames_419.length if cmnames_419[cmidx_419] == mname && cmidx_419 < cmreturns_419.length return cmreturns_419[cmidx_419] end cmidx_419 = cmidx_419 + 1 end end end end end end # Kernel coercion methods: Integer(x) / Float(x) return their class. # Only treat as a Kernel call when there's no explicit receiver — with # a receiver, "Integer" / "Float" would be ConstantReadNode lookups, end end end # Issue #419: `<obj>.class.<cmeth>` — recv is a `class` # CallNode. The lowered call dispatches to <C>::cmeth at # codegen time when the inner recv has a known obj_<C> # type. At DCE time we don't have method-body scope # context (so infer_type on a LocalVariableReadNode here # falls back to "int"), so over-approximate: mark cmeth # live on every class that defines one. Matches the # ConstantReadNode / SymbolNode arms above which take the # same shape — DCE prefers false-negatives (a kept-but- # unused method) over false-positives (a stripped live # method that produces a linker error). if @nd_name[recv] == "class" mark_idx_419 = 0 while mark_idx_419 < @cls_names.length cls_cmeth_mark_live(mark_idx_419, mname) mark_idx_419 = mark_idx_419 + 1 end end end else if ctx_ci >= 0spinel_codegen.rbmodifiedView file
Loading diff…
test/obj_class_dispatch.rbaddedView file
Loading diff…
test/obj_class_dispatch.rb.expectedaddedView file
Loading diff…