Commit d5882636

d58826368fefMerged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): preserve namespace prefix in ivar / local types

When a class lives inside a module and another class in the same module is referenced via a bare name from one of its method bodies, the type spinel records for the resulting ivar / local must carry the same namespace prefix the class is registered under. Three sites broke this:

  1. collect_class_with_prefix (and its class-reopen path) did not set @current_class_idx before calling collect_ivars. Without that, current_lexical_scope_name returned "" and any @x = Foo.new(...) inside a method body resolved Foo to bare "Foo" — even when the actual class was registered under Optcarrot_Foo. The recorded ivar type was then obj_Foo and c_type emitted a sp_Foo * field that did not match the sp_Optcarrot_Foo typedef.

  2. The top-level method body scan in compile (line ~5975) did not pin @current_method_name. The existing _cls_ parsing in current_lexical_scope_name already extracts a module prefix from <Mod>_cls_<m> names, but only when the method name is set. Pinning it lets Foo.new inside e.g. Optcarrot::Driver.load resolve against the same scope chain a class body sees.

  3. emit_main's first locals-scan pass was the only one of three passes missing the ModuleNode filter. It dragged every nested method's locals into main()'s frame and mis-resolved their constants (no lexical scope is in effect at the top-level scan).

Together these account for ~70 of the C-compile errors when generating optcarrot — every sp_Config * / sp_CPU * / sp_Triangle * / etc. field in the structs of Optcarrot::Pads, Optcarrot::APU, Optcarrot::PPU, etc. and every local in inlined module-function bodies.

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

Details

Commit
d58826368fef
Parent
a09a6b8b339b

spinel_codegen.rbmodifiedView file

@@ -4941,7 +4941,12 @@
49414941 end
49424942 end
49434943 }
4944+ # Pin lexical scope while collecting ivars. See longer comment
4945+ # at the other collect_ivars call site below for rationale.
4946+ saved_idx = @current_class_idx
4947+ @current_class_idx = ci
49444948 collect_ivars(ci)
4949+ @current_class_idx = saved_idx
49454950 return
49464951 end
49474952
@@ -5116,8 +5121,17 @@
51165121 end
51175122 }
51185123
5119- # Collect ivars
5124+ # Collect ivars. Pin the lexical scope to this class so any
5125+ # `@x = Foo.new(...)` inside its methods resolves `Foo` against
5126+ # the same scope chain the call site sees — without this,
5127+ # current_lexical_scope_name returns "" and a `Config` reference
5128+ # inside `Optcarrot::NES` resolves to bare "Config" (no
5129+ # Optcarrot_ prefix), poisoning the ivar's recorded type and the
5130+ # eventual `sp_Config *` field declaration that fails to compile.
5131+ saved_idx = @current_class_idx
5132+ @current_class_idx = ci
51205133 collect_ivars(ci)
5134+ @current_class_idx = saved_idx
51215135 end
51225136
51235137 def collect_module_methods_into_class(ci, mod_name)
@@ -7266,6 +7280,14 @@
72667280 i = 0
72677281 while i < @meth_names.length
72687282 push_scope
7283+ # Pin @current_method_name so current_lexical_scope_name can pull
7284+ # the module prefix out of `<Mod>_cls_<m>` style names. Without
7285+ # this, a `Foo.new` inside e.g. `Optcarrot::Driver.load` resolves
7286+ # `Foo` against the empty scope and lands on bare `Foo` instead
7287+ # of `Optcarrot_Foo`, which then poisons the local variable's
7288+ # recorded type.
7289+ saved_meth = @current_method_name
7290+ @current_method_name = @meth_names[i]
72697291 pnames = @meth_param_names[i].split(",")
72707292 ptypes = @meth_param_types[i].split(",")
72717293 j = 0
@@ -7289,6 +7311,7 @@
72897311 end
72907312 scan_writer_calls(@meth_body_ids[i])
72917313 end
7314+ @current_method_name = saved_meth
72927315 pop_scope
72937316 i = i + 1
72947317 end
@@ -13659,7 +13682,17 @@
1365913682 if @nd_type[sid] != "DefNode"
1366013683 if @nd_type[sid] != "ClassNode"
1366113684 if @nd_type[sid] != "ConstantWriteNode"
13662- scan_locals(sid, lnames, ltypes, empty_params)
13685+ # Skip ModuleNode bodies — their nested classes/methods are
13686+ # collected separately, and recursing into them here would
13687+ # pull every nested method's locals into main()'s frame and
13688+ # also mis-resolve constants (no lexical scope is in effect
13689+ # at this scan, so e.g. `Video.new(...)` inside a module
13690+ # function lands on bare `Video` instead of the namespaced
13691+ # form). The other passes below already filter ModuleNode;
13692+ # this one was the outlier.
13693+ if @nd_type[sid] != "ModuleNode"
13694+ scan_locals(sid, lnames, ltypes, empty_params)
13695+ end
1366313696 end
1366413697 end
1366513698 end