Commit 2916e19c
2916e19ce7bdMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 8 files · +6 added · ~2 modified
Description
Treat builtin class reopens as open-class methods
Register reopened builtin classes such as Object and Array through the open-class method path instead of adding them to the user class table. This avoids re-emitting builtin structs and constructors, while keeping Object methods available as fallback methods for builtin and user-class receivers with boxed self.
Fixes #1306.
Co-Authored-by: Codex [email protected]
Details
- Commit
- 2916e19ce7bd
- Parent
- 0408f5ac26d4
spinel_analyze.rbmodifiedView file
@@ -2848,13 +2848,17 @@
28482848
return "void"28492849
end28502850
if t == "SelfNode"2851-
if @current_class_idx >= 02852-
return "obj_" + @cls_names[@current_class_idx]2851+
oc_st = open_class_self_type_for_method(@current_method_name)2852+
if oc_st != ""2853+
return oc_st28532854
end28542855
st = find_var_type("__self_type")28552856
if st != ""28562857
return st28572858
end2859+
if @current_class_idx >= 02860+
return "obj_" + @cls_names[@current_class_idx]2861+
end28582862
return "int"28592863
end28602864
if t == "LambdaNode"@@ -7757,23 +7761,117 @@
77577761
# Check open class methods for receiver type77587762
if recv >= 077597763
rt = infer_type(recv)7760-
oc_prefix = ""7761-
if rt == "int"7762-
oc_prefix = "__oc_Integer_"7764+
if base_type(rt) == "bool"7765+
brt = infer_bool_open_class_type(mname, recv)7766+
if brt != ""7767+
return brt7768+
end77637769
end7764-
if rt == "string"7765-
oc_prefix = "__oc_String_"7766-
end7767-
if rt == "float"7768-
oc_prefix = "__oc_Float_"7769-
end7770-
if oc_prefix != ""7771-
oc_name = oc_prefix + mname7770+
oc_cname = open_class_name_for_type(rt)7771+
if oc_cname != ""7772+
oc_name = "__oc_" + oc_cname + "_" + mname77727773
oc_mi = find_method_idx(oc_name)77737774
if oc_mi >= 077747775
return @meth_return_types[oc_mi]77757776
end77767777
end7778+
oc_name_obj = "__oc_Object_" + mname7779+
oc_mi_obj = find_method_idx(oc_name_obj)7780+
if oc_mi_obj >= 07781+
return @meth_return_types[oc_mi_obj]7782+
end7783+
end7784+
""7785+
end7786+
7787+
def infer_bool_open_class_type(mname, recv)7788+
types = []7789+
nt = @nd_type[recv]7790+
if nt != "FalseNode"7791+
mi_true = find_method_idx("__oc_TrueClass_" + mname)7792+
if mi_true >= 07793+
types.push(@meth_return_types[mi_true])7794+
end7795+
end7796+
if nt != "TrueNode"7797+
mi_false = find_method_idx("__oc_FalseClass_" + mname)7798+
if mi_false >= 07799+
types.push(@meth_return_types[mi_false])7800+
end7801+
end7802+
mi_obj = find_method_idx("__oc_Object_" + mname)7803+
if mi_obj >= 07804+
types.push(@meth_return_types[mi_obj])7805+
end7806+
if types.length == 07807+
return ""7808+
end7809+
unify_return_type(types)7810+
end7811+
7812+
def open_class_name_for_type(t)7813+
bt = base_type(t)7814+
if bt == "int" || bt == "bigint"7815+
return "Integer"7816+
end7817+
if bt == "string" || bt == "mutable_str"7818+
return "String"7819+
end7820+
if bt == "float"7821+
return "Float"7822+
end7823+
if bt == "symbol"7824+
return "Symbol"7825+
end7826+
if bt == "nil"7827+
return "NilClass"7828+
end7829+
if is_array_type(bt) == 17830+
return "Array"7831+
end7832+
if is_hash_type(bt) == 17833+
return "Hash"7834+
end7835+
if bt == "range"7836+
return "Range"7837+
end7838+
if bt == "time"7839+
return "Time"7840+
end7841+
if bt == "proc" || bt == "lambda"7842+
return "Proc"7843+
end7844+
if bt == "class"7845+
return "Class"7846+
end7847+
if bt == "encoding"7848+
return "Encoding"7849+
end7850+
""7851+
end7852+
7853+
def open_class_poly_self_method?(mfn)7854+
if !mfn.start_with?("__oc_")7855+
return 07856+
end7857+
if mfn.start_with?("__oc_Integer_") || mfn.start_with?("__oc_String_") || mfn.start_with?("__oc_Float_")7858+
return 07859+
end7860+
17861+
end7862+
7863+
def open_class_self_type_for_method(mfn)7864+
if mfn.start_with?("__oc_Integer_")7865+
return "int"7866+
end7867+
if mfn.start_with?("__oc_String_")7868+
return "string"7869+
end7870+
if mfn.start_with?("__oc_Float_")7871+
return "float"7872+
end7873+
if open_class_poly_self_method?(mfn) == 17874+
return "poly"77777875
end77787876
""77797877
end@@ -9342,19 +9440,6 @@
93429440
is_user_exception_subclass(name)93439441
end93449442
9345-
def is_builtin_type_name(name)9346-
if name == "Integer"9347-
return 19348-
end9349-
if name == "String"9350-
return 19351-
end9352-
if name == "Float"9353-
return 19354-
end9355-
09356-
end9357-
93589443
# built-in class / module names that93599444
# get a reserved cls_id. Kept in sync with93609445
# spinel_codegen.rb's @builtin_class_names array.@@ -10596,7 +10681,7 @@
1059610681
end1059710682
1059810683
# Check for open class on built-in type10599-
if is_builtin_type_name(cname) == 110684+
if is_builtin_class_const_name(cname) == 11060010685
@open_class_names.push(cname)1060110686
# Collect methods as top-level functions with special naming1060210687
body = @nd_body[nid]@@ -22245,6 +22330,9 @@
2224522330
if mfn.start_with?("__oc_Float_")2224622331
declare_var("__self_type", "float")2224722332
end22333+
if open_class_poly_self_method?(mfn) == 122334+
declare_var("__self_type", "poly")22335+
end2224822336
pnames = @meth_param_names[i].split(",", -1)2224922337
ptypes = @meth_param_types[i].split(",", -1)2225022338
j = 0spinel_codegen.rbmodifiedView file
Loading diff…
test/open_class_array_builtin.rbaddedView file
Loading diff…
test/open_class_array_builtin.rb.expectedaddedView file
Loading diff…
test/open_class_bool_builtin.rbaddedView file
Loading diff…
test/open_class_bool_builtin.rb.expectedaddedView file
Loading diff…
test/open_class_object.rbaddedView file
Loading diff…
test/open_class_object.rb.expectedaddedView file
Loading diff…