Commit 4510cc9c

4510cc9c4f62Merged

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

Description

Merge pull request #233 from mame/pr/super-arg-cast

fix(codegen): cast super-call args to parent's declared param types

Details

Commit
4510cc9c4f62
Parent
ff464f13ad05
Merge parent
75d32b64f405

spinel_codegen.rbmodifiedView file

@@ -13987,6 +13987,23 @@
1398713987 if @cls_parents[ci] != ""
1398813988 pi = find_class_idx(@cls_parents[ci])
1398913989 if pi >= 0
13990+ # Look up the parent's init signature so we can cast
13991+ # each forwarded arg to the parent's declared type
13992+ # when it differs (parent's param-type inference may
13993+ # not see this child's type narrowing).
13994+ p_pnames = "".split(",")
13995+ p_ptypes = "".split(",")
13996+ p_init_idx = cls_find_method_direct(pi, "initialize")
13997+ if p_init_idx >= 0
13998+ p_all_params = @cls_meth_params[pi].split("|")
13999+ p_all_ptypes = @cls_meth_ptypes[pi].split("|")
14000+ if p_init_idx < p_all_params.length
14001+ p_pnames = p_all_params[p_init_idx].split(",")
14002+ end
14003+ if p_init_idx < p_all_ptypes.length
14004+ p_ptypes = p_all_ptypes[p_init_idx].split(",")
14005+ end
14006+ end
1399014007 super_args = ""
1399114008 if @nd_type[sid] == "ForwardingSuperNode"
1399214009 # Forward the current init's params 1:1.
@@ -13995,7 +14012,13 @@
1399514012 if fk > 0
1399614013 super_args = super_args + ", "
1399714014 end
13998- super_args = super_args + "lv_" + pnames[fk]
14015+ arg_expr = "lv_" + pnames[fk]
14016+ if fk < p_ptypes.length && fk < ptypes.length
14017+ if p_ptypes[fk] != ptypes[fk] && p_ptypes[fk] != ""
14018+ arg_expr = "(" + c_type(p_ptypes[fk]) + ")" + arg_expr
14019+ end
14020+ end
14021+ super_args = super_args + arg_expr
1399914022 fk = fk + 1
1400014023 end
1400114024 else
@@ -14007,7 +14030,14 @@
1400714030 if ak > 0
1400814031 super_args = super_args + ", "
1400914032 end
14010- super_args = super_args + compile_expr(arg_ids[ak])
14033+ arg_expr2 = compile_expr(arg_ids[ak])
14034+ if ak < p_ptypes.length
14035+ at_arg = infer_type(arg_ids[ak])
14036+ if p_ptypes[ak] != at_arg && p_ptypes[ak] != ""
14037+ arg_expr2 = "(" + c_type(p_ptypes[ak]) + ")" + arg_expr2
14038+ end
14039+ end
14040+ super_args = super_args + arg_expr2
1401114041 ak = ak + 1
1401214042 end
1401314043 end