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 default182+
# expression is inlined into the caller, `self_arrow` consults183+
# this override to route `self->iv_X` against the call's184+
# 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
end1114011146
1114111147
def self_arrow11148+
# `@self_override` lets default-arg substitution redirect `self`11149+
# to an explicit receiver expression: when a callee's default11150+
# expression is inlined into the caller, `self` would otherwise11151+
# resolve to the caller's `self`, but the default was authored11152+
# against the callee's instance. Store the bare expression11153+
# (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] == 111156+
return @self_override + "."11157+
end11158+
return @self_override + "->"11159+
end1114211160
if @current_class_idx >= 01114311161
if @cls_is_value_type[@current_class_idx] == 11114411162
return "self."@@ -11147,6 +11165,17 @@
1114711165
"self->"1114811166
end1114911167
11168+
# The C expression that should be used wherever bare `self`11169+
# would normally appear (e.g. as the first arg to a same-class11170+
# method dispatch). Defaults to `"self"`; default-arg inlining11171+
# overrides it via `@self_override`.11172+
def self_expr11173+
if @self_override != nil && @self_override != ""11174+
return @self_override11175+
end11176+
"self"11177+
end11178+
1115011179
def subtree_has_ivar_write(nid)1115111180
if nid < 0 || nid >= @nd_count1115211181
return 0@@ -16407,7 +16436,7 @@
1640716436
bp = "0"1640816437
end1640916438
end16410-
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
end1641216441
# 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
end2115421183
result = result + aexpr2115521184
else21156-
# Caller omitted this trailing arg — emit the method's default.21185+
# Caller omitted this trailing arg — emit the method's21186+
# default expression. The default was authored in the21187+
# callee's class scope, so any `self` / `@ivar` references21188+
# need to resolve against the call's receiver, not the21189+
# caller's self. Switch @current_class_idx and override21190+
# `self_arrow` for the duration of the inline expansion.2115721191
if k < defaults.length2115821192
def_id = defaults[k].to_i2115921193
if def_id >= 021194+
recv_for_default = @nd_receiver[nid]21195+
saved_ci = @current_class_idx21196+
saved_override = @self_override21197+
if recv_for_default >= 021198+
# Materialize the recv into a C expression. Wrap in21199+
# parens so any cast/operator chain stays self-21200+
# contained when consumed as `(<recv>)->iv_X` (via21201+
# `self_arrow`) or as the first arg of a same-class21202+
# 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_ci21206+
end2116021207
if k < ptypes.length2116121208
aexpr = compile_expr_for_expected_type(def_id, ptypes[k])2116221209
if root_constructor_args == 1@@ -21170,6 +21217,8 @@
2117021217
end2117121218
result = result + aexpr2117221219
end21220+
@current_class_idx = saved_ci21221+
@self_override = saved_override2117321222
else2117421223
if k < ptypes.length && ptypes[k] == "poly"2117521224
result = result + "sp_box_nil()"test/default_arg_callee_scope.rbaddedView file
Loading diff…