Commit dac2f2f3

dac2f2f3c80dMerged

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

Description

Merge pull request #191 from mame/pr/default-arg-callee-scope

fix(codegen): default-arg inlining honors callee class scope

Conflicts:

spinel_codegen.rb

Details

Commit
dac2f2f3c80d
Parent
d9c69d5c5db5
Merge parent
c4d4d683f472

spinel_codegen.rbmodifiedView file

@@ -178,6 +178,12 @@
178178 @instance_eval_self_var = ""
179179 @instance_eval_self_type = ""
180180
181+ # During default-arg substitution, when the callee's default
182+ # expression is inlined into the caller, `self_arrow` consults
183+ # this override to route `self->iv_X` against the call's
184+ # explicit receiver instead of the caller's self.
185+ @self_override = ""
186+
181187 # Yield/block tracking (parallel with meth_names / cls_meth_names)
182188 @meth_has_yield = []
183189 @cls_meth_has_yield = "".split(",")
@@ -11139,6 +11145,18 @@
1113911145 end
1114011146
1114111147 def self_arrow
11148+ # `@self_override` lets default-arg substitution redirect `self`
11149+ # to an explicit receiver expression: when a callee's default
11150+ # expression is inlined into the caller, `self` would otherwise
11151+ # resolve to the caller's `self`, but the default was authored
11152+ # against the callee's instance. Store the bare expression
11153+ # (e.g. `lv_f`) and append the arrow / dot here.
11154+ if @self_override != nil && @self_override != ""
11155+ if @current_class_idx >= 0 && @cls_is_value_type[@current_class_idx] == 1
11156+ return @self_override + "."
11157+ end
11158+ return @self_override + "->"
11159+ end
1114211160 if @current_class_idx >= 0
1114311161 if @cls_is_value_type[@current_class_idx] == 1
1114411162 return "self."
@@ -11147,6 +11165,17 @@
1114711165 "self->"
1114811166 end
1114911167
11168+ # The C expression that should be used wherever bare `self`
11169+ # would normally appear (e.g. as the first arg to a same-class
11170+ # method dispatch). Defaults to `"self"`; default-arg inlining
11171+ # overrides it via `@self_override`.
11172+ def self_expr
11173+ if @self_override != nil && @self_override != ""
11174+ return @self_override
11175+ end
11176+ "self"
11177+ end
11178+
1115011179 def subtree_has_ivar_write(nid)
1115111180 if nid < 0 || nid >= @nd_count
1115211181 return 0
@@ -16407,7 +16436,7 @@
1640716436 bp = "0"
1640816437 end
1640916438 end
16410- return "sp_" + owner + "_" + sanitize_name(mname) + "(self" + build_call_tail(ca, bp) + ")"
16439+ return "sp_" + owner + "_" + sanitize_name(mname) + "(" + self_expr + build_call_tail(ca, bp) + ")"
1641116440 end
1641216441 # Check attr_readers (bare method call like `x` meaning self.x)
1641316442 readers = @cls_attr_readers[@current_class_idx].split(";")
@@ -21153,10 +21182,28 @@
2115321182 end
2115421183 result = result + aexpr
2115521184 else
21156- # Caller omitted this trailing arg — emit the method's default.
21185+ # Caller omitted this trailing arg — emit the method's
21186+ # default expression. The default was authored in the
21187+ # callee's class scope, so any `self` / `@ivar` references
21188+ # need to resolve against the call's receiver, not the
21189+ # caller's self. Switch @current_class_idx and override
21190+ # `self_arrow` for the duration of the inline expansion.
2115721191 if k < defaults.length
2115821192 def_id = defaults[k].to_i
2115921193 if def_id >= 0
21194+ recv_for_default = @nd_receiver[nid]
21195+ saved_ci = @current_class_idx
21196+ saved_override = @self_override
21197+ if recv_for_default >= 0
21198+ # Materialize the recv into a C expression. Wrap in
21199+ # parens so any cast/operator chain stays self-
21200+ # contained when consumed as `(<recv>)->iv_X` (via
21201+ # `self_arrow`) or as the first arg of a same-class
21202+ # method call (via `self_expr`).
21203+ recv_c = compile_expr_gc_rooted(recv_for_default)
21204+ @self_override = "(" + recv_c + ")"
21205+ @current_class_idx = target_ci
21206+ end
2116021207 if k < ptypes.length
2116121208 aexpr = compile_expr_for_expected_type(def_id, ptypes[k])
2116221209 if root_constructor_args == 1
@@ -21170,6 +21217,8 @@
2117021217 end
2117121218 result = result + aexpr
2117221219 end
21220+ @current_class_idx = saved_ci
21221+ @self_override = saved_override
2117321222 else
2117421223 if k < ptypes.length && ptypes[k] == "poly"
2117521224 result = result + "sp_box_nil()"