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 end
28502850 if t == "SelfNode"
2851- if @current_class_idx >= 0
2852- 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_st
28532854 end
28542855 st = find_var_type("__self_type")
28552856 if st != ""
28562857 return st
28572858 end
2859+ if @current_class_idx >= 0
2860+ return "obj_" + @cls_names[@current_class_idx]
2861+ end
28582862 return "int"
28592863 end
28602864 if t == "LambdaNode"
@@ -7757,23 +7761,117 @@
77577761 # Check open class methods for receiver type
77587762 if recv >= 0
77597763 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 brt
7768+ end
77637769 end
7764- if rt == "string"
7765- oc_prefix = "__oc_String_"
7766- end
7767- if rt == "float"
7768- oc_prefix = "__oc_Float_"
7769- end
7770- if oc_prefix != ""
7771- oc_name = oc_prefix + mname
7770+ oc_cname = open_class_name_for_type(rt)
7771+ if oc_cname != ""
7772+ oc_name = "__oc_" + oc_cname + "_" + mname
77727773 oc_mi = find_method_idx(oc_name)
77737774 if oc_mi >= 0
77747775 return @meth_return_types[oc_mi]
77757776 end
77767777 end
7778+ oc_name_obj = "__oc_Object_" + mname
7779+ oc_mi_obj = find_method_idx(oc_name_obj)
7780+ if oc_mi_obj >= 0
7781+ return @meth_return_types[oc_mi_obj]
7782+ end
7783+ end
7784+ ""
7785+ end
7786+
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 >= 0
7793+ types.push(@meth_return_types[mi_true])
7794+ end
7795+ end
7796+ if nt != "TrueNode"
7797+ mi_false = find_method_idx("__oc_FalseClass_" + mname)
7798+ if mi_false >= 0
7799+ types.push(@meth_return_types[mi_false])
7800+ end
7801+ end
7802+ mi_obj = find_method_idx("__oc_Object_" + mname)
7803+ if mi_obj >= 0
7804+ types.push(@meth_return_types[mi_obj])
7805+ end
7806+ if types.length == 0
7807+ return ""
7808+ end
7809+ unify_return_type(types)
7810+ end
7811+
7812+ def open_class_name_for_type(t)
7813+ bt = base_type(t)
7814+ if bt == "int" || bt == "bigint"
7815+ return "Integer"
7816+ end
7817+ if bt == "string" || bt == "mutable_str"
7818+ return "String"
7819+ end
7820+ if bt == "float"
7821+ return "Float"
7822+ end
7823+ if bt == "symbol"
7824+ return "Symbol"
7825+ end
7826+ if bt == "nil"
7827+ return "NilClass"
7828+ end
7829+ if is_array_type(bt) == 1
7830+ return "Array"
7831+ end
7832+ if is_hash_type(bt) == 1
7833+ return "Hash"
7834+ end
7835+ if bt == "range"
7836+ return "Range"
7837+ end
7838+ if bt == "time"
7839+ return "Time"
7840+ end
7841+ if bt == "proc" || bt == "lambda"
7842+ return "Proc"
7843+ end
7844+ if bt == "class"
7845+ return "Class"
7846+ end
7847+ if bt == "encoding"
7848+ return "Encoding"
7849+ end
7850+ ""
7851+ end
7852+
7853+ def open_class_poly_self_method?(mfn)
7854+ if !mfn.start_with?("__oc_")
7855+ return 0
7856+ end
7857+ if mfn.start_with?("__oc_Integer_") || mfn.start_with?("__oc_String_") || mfn.start_with?("__oc_Float_")
7858+ return 0
7859+ end
7860+ 1
7861+ end
7862+
7863+ def open_class_self_type_for_method(mfn)
7864+ if mfn.start_with?("__oc_Integer_")
7865+ return "int"
7866+ end
7867+ if mfn.start_with?("__oc_String_")
7868+ return "string"
7869+ end
7870+ if mfn.start_with?("__oc_Float_")
7871+ return "float"
7872+ end
7873+ if open_class_poly_self_method?(mfn) == 1
7874+ return "poly"
77777875 end
77787876 ""
77797877 end
@@ -9342,19 +9440,6 @@
93429440 is_user_exception_subclass(name)
93439441 end
93449442
9345- def is_builtin_type_name(name)
9346- if name == "Integer"
9347- return 1
9348- end
9349- if name == "String"
9350- return 1
9351- end
9352- if name == "Float"
9353- return 1
9354- end
9355- 0
9356- end
9357-
93589443 # built-in class / module names that
93599444 # get a reserved cls_id. Kept in sync with
93609445 # spinel_codegen.rb's @builtin_class_names array.
@@ -10596,7 +10681,7 @@
1059610681 end
1059710682
1059810683 # Check for open class on built-in type
10599- if is_builtin_type_name(cname) == 1
10684+ if is_builtin_class_const_name(cname) == 1
1060010685 @open_class_names.push(cname)
1060110686 # Collect methods as top-level functions with special naming
1060210687 body = @nd_body[nid]
@@ -22245,6 +22330,9 @@
2224522330 if mfn.start_with?("__oc_Float_")
2224622331 declare_var("__self_type", "float")
2224722332 end
22333+ if open_class_poly_self_method?(mfn) == 1
22334+ declare_var("__self_type", "poly")
22335+ end
2224822336 pnames = @meth_param_names[i].split(",", -1)
2224922337 ptypes = @meth_param_types[i].split(",", -1)
2225022338 j = 0