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_includes parallel array (semicolon-separated module names). Populated from the same include collection 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_names and [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_unified lowers 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_type returns "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 table
  • Polite == 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 module
137+ # names, semicolon-separated. Modules live in @module_names so
138+ # 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 end
2073+ # Issue #404 Phase 3 Tier 2: module constant in value
2074+ # position. Modules share the sp_Class representation; the
2075+ # codegen mapping to the unified cls_id space happens on the
2076+ # emit side.
2077+ mx = 0
2078+ while mx < @module_names.length
2079+ if @module_names[mx] == rname
2080+ return "class"
2081+ end
2082+ mx = mx + 1
2083+ end
20692084 return "int"
20702085 end
20712086 if t == "ConstantPathNode"
@@ -6314,7 +6329,9 @@
63146329 # name is `<prefix>_<name>`. Try the qualified form
63156330 # first; fall back to the bare name for top-level
63166331 # 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 end
63196336 ik = ik + 1
63206337 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 ivars
64066424 ivar_names = ""
64076425 ivar_types = ""
@@ -6541,7 +6559,14 @@
65416559 # class-reopening branch -- the include arg is a
65426560 # bare name but the registered module's name is
65436561 # `<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 include
6565+ # relationship for ancestors-table emission. The
6566+ # methods are already merged into the class above;
6567+ # this pass keeps the module link alive so the
6568+ # codegen can weave it into the MRO.
6569+ record_class_include(ci, resolved_mod_name)
65456570 end
65466571 ik = ik + 1
65476572 end
@@ -6653,6 +6678,42 @@
66536678 # unqualified `Helper`, but the registered module's name (from
66546679 # collect_module_with_prefix) is `Ns_Helper`. Try the qualified
66556680 # 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-class
6682+ # includes list. Skipped when the module name doesn't resolve to
6683+ # a registered module (collect_module_methods_into_class already
6684+ # warns / no-ops in that case; the ancestors table only carries
6685+ # modules known to the program).
6686+ def record_class_include(ci, mod_name)
6687+ mi = 0
6688+ found = 0
6689+ while mi < @module_names.length
6690+ if @module_names[mi] == mod_name
6691+ found = 1
6692+ mi = @module_names.length
6693+ else
6694+ mi = mi + 1
6695+ end
6696+ end
6697+ if found == 0
6698+ return
6699+ end
6700+ cur = @cls_includes[ci]
6701+ # Dedup: a re-included module shouldn't duplicate in ancestors.
6702+ parts = cur.split(";")
6703+ pk = 0
6704+ while pk < parts.length
6705+ if parts[pk] == mod_name
6706+ return
6707+ end
6708+ pk = pk + 1
6709+ end
6710+ if cur == ""
6711+ @cls_includes[ci] = mod_name
6712+ else
6713+ @cls_includes[ci] = cur + ";" + mod_name
6714+ end
6715+ end
6716+
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 tables
1922519288 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)