Commit 14dd502c
14dd502c6f36Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
feat(codegen): Module ancestors / include weaving (#404 Phase 3 Tier 2)
Tier 2 of #404 Phase 3 unifies the cls_id space across user
classes and modules and weaves include relationships into the
precomputed ancestors table. Pre-Tier-2 the ancestors table
walked parent chains only; Polite.ancestors of a class that
include Greeting skipped the module entirely.
Changes:
- Analyze tracks per-class includes in a new
@cls_includesparallel array (semicolon-separated module names). Populated from the sameincludecollection that already merges module methods into the class; the new path keeps the module link alive for the ancestors table. - Codegen's cls_id space spans
[0 .. N-1]for@cls_namesand[N .. N+M-1]for@module_names. The names table, parents table, and ancestors table all extend to cover the module range; modules have parent = -1. compute_ancestors_for_unifiedlowers the MRO over the unified space: for a class, walk includes in reverse then the parent's ancestors with dedup at append; for a module, just self (the include collector doesn't fire inside module bodies in spinel's current model, so module-includes-module nesting stays out of scope).- ConstantReadNode for a module name now lowers to a
((sp_Class){<unified_id>LL})in value position, parallel to the class-name path.infer_typereturns "class" for module names too so chained method dispatch on the sp_Class value goes through the Phase 3 hierarchy arms uniformly.
What works after this commit (covered in test/class_hierarchy_module.rb):
M = Greeting; M.to_s-> "Greeting"Polite.ancestors-> Polite, Politeness, Greeting, Base (include-reverse weaving between the class and its parent chain, matching CRuby's MRO for the include-only shape)Polite <= Greeting-> true via the ancestors tablePolite == Greeting-> false (distinct cls_ids)
Optcarrot: checksum 59662 unchanged, fps within noise (649.7).
Refs #404 Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 14dd502c6f36
- Parent
- 240333ca4623
spinel_analyze.rbmodifiedView file
@@ -133,6 +133,10 @@
133133
# ---- Classes (parallel arrays) ----134134
@cls_names = "".split(",")135135
@cls_parents = "".split(",")136+
# Issue #404 Phase 3 Tier 2: per-class list of included module137+
# names, semicolon-separated. Modules live in @module_names so138+
# the resolution from name -> id is deferred to codegen time.139+
@cls_includes = "".split(",")136140
@cls_ivar_names = "".split(",")137141
@cls_ivar_types = "".split(",")138142
# Per-ivar flag: was the ivar's first scanned write a definite@@ -2066,6 +2070,17 @@
20662070
# never call infer_type on the receiver.20672071
return "class"20682072
end2073+
# Issue #404 Phase 3 Tier 2: module constant in value2074+
# position. Modules share the sp_Class representation; the2075+
# codegen mapping to the unified cls_id space happens on the2076+
# emit side.2077+
mx = 02078+
while mx < @module_names.length2079+
if @module_names[mx] == rname2080+
return "class"2081+
end2082+
mx = mx + 12083+
end20692084
return "int"20702085
end20712086
if t == "ConstantPathNode"@@ -6314,7 +6329,9 @@
63146329
# name is `<prefix>_<name>`. Try the qualified form63156330
# first; fall back to the bare name for top-level63166331
# modules.6317-
collect_module_methods_into_class(ci, resolve_include_module_name(mod_name, module_prefix))6332+
resolved_mod_name = resolve_include_module_name(mod_name, module_prefix)6333+
collect_module_methods_into_class(ci, resolved_mod_name)6334+
record_class_include(ci, resolved_mod_name)63186335
end63196336
ik = ik + 163206337
end@@ -6402,6 +6419,7 @@
64026419
@cls_is_value_type.push(0)64036420
@cls_is_sra.push(0)64046421
@cls_parents.push(parent)6422+
@cls_includes.push("")64056423
# Initialize struct fields as ivars64066424
ivar_names = ""64076425
ivar_types = ""@@ -6541,7 +6559,14 @@
65416559
# class-reopening branch -- the include arg is a65426560
# bare name but the registered module's name is65436561
# `<prefix>_<name>` when nested in a module.6544-
collect_module_methods_into_class(ci, resolve_include_module_name(mod_name, module_prefix))6562+
resolved_mod_name = resolve_include_module_name(mod_name, module_prefix)6563+
collect_module_methods_into_class(ci, resolved_mod_name)6564+
# Issue #404 Phase 3 Tier 2: record the include6565+
# relationship for ancestors-table emission. The6566+
# methods are already merged into the class above;6567+
# this pass keeps the module link alive so the6568+
# codegen can weave it into the MRO.6569+
record_class_include(ci, resolved_mod_name)65456570
end65466571
ik = ik + 165476572
end@@ -6653,6 +6678,42 @@
66536678
# unqualified `Helper`, but the registered module's name (from66546679
# collect_module_with_prefix) is `Ns_Helper`. Try the qualified66556680
# form first; fall back to the bare name for top-level modules.6681+
# Issue #404 Phase 3 Tier 2: append a module name to the per-class6682+
# includes list. Skipped when the module name doesn't resolve to6683+
# a registered module (collect_module_methods_into_class already6684+
# warns / no-ops in that case; the ancestors table only carries6685+
# modules known to the program).6686+
def record_class_include(ci, mod_name)6687+
mi = 06688+
found = 06689+
while mi < @module_names.length6690+
if @module_names[mi] == mod_name6691+
found = 16692+
mi = @module_names.length6693+
else6694+
mi = mi + 16695+
end6696+
end6697+
if found == 06698+
return6699+
end6700+
cur = @cls_includes[ci]6701+
# Dedup: a re-included module shouldn't duplicate in ancestors.6702+
parts = cur.split(";")6703+
pk = 06704+
while pk < parts.length6705+
if parts[pk] == mod_name6706+
return6707+
end6708+
pk = pk + 16709+
end6710+
if cur == ""6711+
@cls_includes[ci] = mod_name6712+
else6713+
@cls_includes[ci] = cur + ";" + mod_name6714+
end6715+
end6716+
66566717
def resolve_include_module_name(mod_name, module_prefix)66576718
if module_prefix != ""66586719
qualified = module_prefix + "_" + mod_name@@ -8859,6 +8920,7 @@
88598920
@cls_is_value_type.push(0)88608921
@cls_is_sra.push(0)88618922
@cls_parents.push("")8923+
@cls_includes.push("")88628924
@cls_ivar_names.push("@self_obj;@fn_ptr")88638925
@cls_ivar_types.push("obj_Method;int")88648926
@cls_ivar_init_definite.push("1;1")@@ -8888,6 +8950,7 @@
88888950
@cls_is_value_type.push(0)88898951
@cls_is_sra.push(0)88908952
@cls_parents.push("")8953+
@cls_includes.push("")88918954
@cls_ivar_names.push("")88928955
@cls_ivar_types.push("")88938956
@cls_ivar_init_definite.push("")@@ -19224,6 +19287,7 @@
1922419287
# Class tables1922519288
buf = ir_emit_sa(buf, "@cls_names", @cls_names)1922619289
buf = ir_emit_sa(buf, "@cls_parents", @cls_parents)19290+
buf = ir_emit_sa(buf, "@cls_includes", @cls_includes)1922719291
buf = ir_emit_sa(buf, "@cls_ivar_names", @cls_ivar_names)1922819292
buf = ir_emit_sa(buf, "@cls_ivar_types", @cls_ivar_types)1922919293
buf = ir_emit_sa(buf, "@cls_ivar_init_definite", @cls_ivar_init_definite)spinel_codegen.rbmodifiedView file
Loading diff…
test/class_hierarchy_module.rbaddedView file
Loading diff…
test/class_hierarchy_module.rb.expectedaddedView file
Loading diff…