Commit 1e7d63da
1e7d63da5252Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 8 files · +6 added · ~2 modified
Description
Merge pull request #1327 from matz/fix-1306-open-class
[codex] Treat builtin class reopens as open-class methods
Details
- Commit
- 1e7d63da5252
- Parent
- 5b8f9ed7e01a
- Merge parent
- 2916e19ce7bd
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"@@ -7773,23 +7777,117 @@
77737777
# Check open class methods for receiver type77747778
if recv >= 077757779
rt = infer_type(recv)7776-
oc_prefix = ""7777-
if rt == "int"7778-
oc_prefix = "__oc_Integer_"7780+
if base_type(rt) == "bool"7781+
brt = infer_bool_open_class_type(mname, recv)7782+
if brt != ""7783+
return brt7784+
end77797785
end7780-
if rt == "string"7781-
oc_prefix = "__oc_String_"7782-
end7783-
if rt == "float"7784-
oc_prefix = "__oc_Float_"7785-
end7786-
if oc_prefix != ""7787-
oc_name = oc_prefix + mname7786+
oc_cname = open_class_name_for_type(rt)7787+
if oc_cname != ""7788+
oc_name = "__oc_" + oc_cname + "_" + mname77887789
oc_mi = find_method_idx(oc_name)77897790
if oc_mi >= 077907791
return @meth_return_types[oc_mi]77917792
end77927793
end7794+
oc_name_obj = "__oc_Object_" + mname7795+
oc_mi_obj = find_method_idx(oc_name_obj)7796+
if oc_mi_obj >= 07797+
return @meth_return_types[oc_mi_obj]7798+
end7799+
end7800+
""7801+
end7802+
7803+
def infer_bool_open_class_type(mname, recv)7804+
types = []7805+
nt = @nd_type[recv]7806+
if nt != "FalseNode"7807+
mi_true = find_method_idx("__oc_TrueClass_" + mname)7808+
if mi_true >= 07809+
types.push(@meth_return_types[mi_true])7810+
end7811+
end7812+
if nt != "TrueNode"7813+
mi_false = find_method_idx("__oc_FalseClass_" + mname)7814+
if mi_false >= 07815+
types.push(@meth_return_types[mi_false])7816+
end7817+
end7818+
mi_obj = find_method_idx("__oc_Object_" + mname)7819+
if mi_obj >= 07820+
types.push(@meth_return_types[mi_obj])7821+
end7822+
if types.length == 07823+
return ""7824+
end7825+
unify_return_type(types)7826+
end7827+
7828+
def open_class_name_for_type(t)7829+
bt = base_type(t)7830+
if bt == "int" || bt == "bigint"7831+
return "Integer"7832+
end7833+
if bt == "string" || bt == "mutable_str"7834+
return "String"7835+
end7836+
if bt == "float"7837+
return "Float"7838+
end7839+
if bt == "symbol"7840+
return "Symbol"7841+
end7842+
if bt == "nil"7843+
return "NilClass"7844+
end7845+
if is_array_type(bt) == 17846+
return "Array"7847+
end7848+
if is_hash_type(bt) == 17849+
return "Hash"7850+
end7851+
if bt == "range"7852+
return "Range"7853+
end7854+
if bt == "time"7855+
return "Time"7856+
end7857+
if bt == "proc" || bt == "lambda"7858+
return "Proc"7859+
end7860+
if bt == "class"7861+
return "Class"7862+
end7863+
if bt == "encoding"7864+
return "Encoding"7865+
end7866+
""7867+
end7868+
7869+
def open_class_poly_self_method?(mfn)7870+
if !mfn.start_with?("__oc_")7871+
return 07872+
end7873+
if mfn.start_with?("__oc_Integer_") || mfn.start_with?("__oc_String_") || mfn.start_with?("__oc_Float_")7874+
return 07875+
end7876+
17877+
end7878+
7879+
def open_class_self_type_for_method(mfn)7880+
if mfn.start_with?("__oc_Integer_")7881+
return "int"7882+
end7883+
if mfn.start_with?("__oc_String_")7884+
return "string"7885+
end7886+
if mfn.start_with?("__oc_Float_")7887+
return "float"7888+
end7889+
if open_class_poly_self_method?(mfn) == 17890+
return "poly"77937891
end77947892
""77957893
end@@ -9358,19 +9456,6 @@
93589456
is_user_exception_subclass(name)93599457
end93609458
9361-
def is_builtin_type_name(name)9362-
if name == "Integer"9363-
return 19364-
end9365-
if name == "String"9366-
return 19367-
end9368-
if name == "Float"9369-
return 19370-
end9371-
09372-
end9373-
93749459
# built-in class / module names that93759460
# get a reserved cls_id. Kept in sync with93769461
# spinel_codegen.rb's @builtin_class_names array.@@ -10612,7 +10697,7 @@
1061210697
end1061310698
1061410699
# Check for open class on built-in type10615-
if is_builtin_type_name(cname) == 110700+
if is_builtin_class_const_name(cname) == 11061610701
@open_class_names.push(cname)1061710702
# Collect methods as top-level functions with special naming1061810703
body = @nd_body[nid]@@ -22261,6 +22346,9 @@
2226122346
if mfn.start_with?("__oc_Float_")2226222347
declare_var("__self_type", "float")2226322348
end22349+
if open_class_poly_self_method?(mfn) == 122350+
declare_var("__self_type", "poly")22351+
end2226422352
pnames = @meth_param_names[i].split(",", -1)2226522353
ptypes = @meth_param_types[i].split(",", -1)2226622354
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…